TombEngine/TR5Main/Renderer/Utils.cpp
2020-08-09 22:15:32 +02:00

49 lines
2.2 KiB
C++

#include "framework.h"
#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);
std::cout << message << std::endl;
throw std::runtime_error("An error occured!");
}
}
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");
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;
throwIfFailed(device->CreateVertexShader(bytecode->GetBufferPointer(), bytecode->GetBufferSize(), nullptr, &shader));
return shader;
}
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.GetAddressOf(), errors.GetAddressOf()));
ID3D11PixelShader* shader = nullptr;
throwIfFailed(device->CreatePixelShader(bytecode->GetBufferPointer(), bytecode->GetBufferSize(), nullptr, &shader));
return shader;
}
UINT Utils::GetShaderFlags() noexcept
{
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
if constexpr(DebugBuild){
flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
} else{
flags |= D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE_IEEE_STRICTNESS;
}
return flags;
}
}