2020-06-16 15:11:30 +02:00
|
|
|
#include "framework.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include <winerror.h>
|
2020-08-09 15:25:56 +02:00
|
|
|
#include <iostream>
|
2020-08-09 22:15:32 +02:00
|
|
|
#include <wrl/client.h>
|
2020-09-29 21:40:02 +02:00
|
|
|
#include <d3dcompiler.h>
|
2021-08-30 18:03:21 +03:00
|
|
|
namespace TEN::Renderer::Utils {
|
2020-08-02 09:02:33 +02:00
|
|
|
using std::wstring;
|
|
|
|
using std::string;
|
2020-08-09 22:15:32 +02:00
|
|
|
using Microsoft::WRL::ComPtr;
|
|
|
|
using std::vector;
|
2020-08-02 09:02:33 +02:00
|
|
|
void Utils::throwIfFailed(const HRESULT& res) noexcept {
|
2020-08-09 15:25:56 +02:00
|
|
|
if(FAILED(res)){
|
|
|
|
std::string message = std::system_category().message(res);
|
2021-08-23 22:06:36 +01:00
|
|
|
TENLog(message, LogLevel::Error);
|
2020-08-09 15:25:56 +02:00
|
|
|
throw std::runtime_error("An error occured!");
|
|
|
|
}
|
|
|
|
|
2020-08-02 09:02:33 +02:00
|
|
|
}
|
2020-06-16 15:11:30 +02:00
|
|
|
|
2020-08-10 20:44:12 +02:00
|
|
|
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) {
|
2020-08-09 22:15:32 +02:00
|
|
|
ComPtr<ID3D10Blob> errors;
|
|
|
|
throwIfFailed(D3DCompileFromFile(fileName.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, function.c_str(), model.c_str(), GetShaderFlags(), 0, bytecode.GetAddressOf(),errors.GetAddressOf()));
|
2020-08-10 20:44:12 +02:00
|
|
|
ComPtr<ID3D11VertexShader> shader;
|
|
|
|
throwIfFailed(device->CreateVertexShader(bytecode->GetBufferPointer(), bytecode->GetBufferSize(), nullptr, shader.GetAddressOf()));
|
|
|
|
if constexpr(DebugBuild){
|
|
|
|
char buffer[100];
|
|
|
|
size_t sz = std::wcstombs(buffer, fileName.c_str(), 100);
|
|
|
|
shader->SetPrivateData(WKPDID_D3DDebugObjectName, sz, buffer);
|
|
|
|
}
|
2020-08-02 09:02:33 +02:00
|
|
|
return shader;
|
|
|
|
}
|
2020-08-10 20:44:12 +02:00
|
|
|
ComPtr<ID3D11PixelShader> compilePixelShader(ID3D11Device* device, const wstring& fileName, const string& function, const string& model, const D3D_SHADER_MACRO* defines, ComPtr<ID3D10Blob>& bytecode) {
|
2020-08-09 22:15:32 +02:00
|
|
|
ComPtr<ID3D10Blob> errors;
|
2020-08-02 09:02:33 +02:00
|
|
|
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG | D3DCOMPILE_PACK_MATRIX_ROW_MAJOR | D3DCOMPILE_SKIP_OPTIMIZATION;
|
2020-08-09 22:15:32 +02:00
|
|
|
throwIfFailed(D3DCompileFromFile(fileName.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE, function.c_str(), model.c_str(), GetShaderFlags(), 0, bytecode.GetAddressOf(), errors.GetAddressOf()));
|
2020-08-10 20:44:12 +02:00
|
|
|
ComPtr<ID3D11PixelShader> shader;
|
|
|
|
throwIfFailed(device->CreatePixelShader(bytecode->GetBufferPointer(), bytecode->GetBufferSize(), nullptr, shader.GetAddressOf()));
|
|
|
|
if constexpr(DebugBuild){
|
|
|
|
char buffer[100];
|
|
|
|
size_t sz = std::wcstombs(buffer, fileName.c_str(), 100);
|
|
|
|
shader->SetPrivateData(WKPDID_D3DDebugObjectName, sz, buffer);
|
|
|
|
}
|
2020-08-02 09:02:33 +02:00
|
|
|
return shader;
|
|
|
|
}
|
2020-08-09 22:15:32 +02:00
|
|
|
|
|
|
|
|
2020-08-10 20:44:12 +02:00
|
|
|
constexpr UINT Utils::GetShaderFlags()
|
2020-08-02 09:02:33 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-06-16 15:11:30 +02:00
|
|
|
}
|