Using Smart Pointers

This commit is contained in:
Raildex 2020-08-09 22:15:32 +02:00
parent b58d18ade2
commit 94eabe8918
16 changed files with 269 additions and 314 deletions

View file

@ -2,9 +2,12 @@
#include "Utils.h"
#include <winerror.h>
#include <iostream>
#include <wrl/client.h>
namespace T5M::Renderer::Utils {
using std::wstring;
using std::string;
using Microsoft::WRL::ComPtr;
using std::vector;
void Utils::throwIfFailed(const HRESULT& res) noexcept {
if(FAILED(res)){
std::string message = std::system_category().message(res);
@ -14,32 +17,26 @@ namespace T5M::Renderer::Utils {
}
ID3D11VertexShader* compileVertexShader(ID3D11Device* device, const wstring& fileName, const string& function, const string& model, const D3D_SHADER_MACRO* defines, ID3D10Blob** bytecode) {
HRESULT res;
*bytecode = nullptr;
ID3DBlob* errors = nullptr;
ComPtr<ID3D11VertexShader> compileVertexShader(ID3D11Device* device, const std::wstring& fileName, const std::string& function, const std::string& model, const D3D_SHADER_MACRO * defines, ComPtr<ID3D10Blob>& bytecode) noexcept {
ComPtr<ID3D10Blob> errors;
logD("Compiling vertex shader");
res = D3DCompileFromFile(fileName.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, function.c_str(), model.c_str(), GetShaderFlags(), 0, bytecode, &errors);
throwIfFailed(res);
throwIfFailed(D3DCompileFromFile(fileName.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, function.c_str(), model.c_str(), GetShaderFlags(), 0, bytecode.GetAddressOf(),errors.GetAddressOf()));
ID3D11VertexShader* shader = nullptr;
res = device->CreateVertexShader((*bytecode)->GetBufferPointer(), (*bytecode)->GetBufferSize(), nullptr, &shader);
throwIfFailed(res);
throwIfFailed(device->CreateVertexShader(bytecode->GetBufferPointer(), bytecode->GetBufferSize(), nullptr, &shader));
return shader;
}
ID3D11PixelShader* compilePixelShader(ID3D11Device* device, const wstring& fileName, const string& function, const string& model, const D3D_SHADER_MACRO* defines, ID3D10Blob** bytecode) noexcept {
HRESULT res;
*bytecode = nullptr;
ID3DBlob* errors = nullptr;
ComPtr<ID3D11PixelShader> compilePixelShader(ID3D11Device* device, const wstring& fileName, const string& function, const string& model, const D3D_SHADER_MACRO* defines, ComPtr<ID3D10Blob>& bytecode) noexcept {
ComPtr<ID3D10Blob> errors;
logD("Compiling pixel shader");
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG | D3DCOMPILE_PACK_MATRIX_ROW_MAJOR | D3DCOMPILE_SKIP_OPTIMIZATION;
throwIfFailed(D3DCompileFromFile(fileName.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, function.c_str(), model.c_str(), GetShaderFlags(), 0, bytecode, &errors));
throwIfFailed(D3DCompileFromFile(fileName.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, function.c_str(), model.c_str(), GetShaderFlags(), 0, bytecode.GetAddressOf(), errors.GetAddressOf()));
ID3D11PixelShader* shader = nullptr;
throwIfFailed(device->CreatePixelShader((*bytecode)->GetBufferPointer(), (*bytecode)->GetBufferSize(), nullptr, &shader));
throwIfFailed(device->CreatePixelShader(bytecode->GetBufferPointer(), bytecode->GetBufferSize(), nullptr, &shader));
return shader;
}
UINT Utils::GetShaderFlags()
UINT Utils::GetShaderFlags() noexcept
{
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
if constexpr(DebugBuild){