Common: Simplify D3D shader object creation with helpers

This commit is contained in:
Connor McLaughlin 2020-02-19 22:04:21 +09:00
parent 11b90b2beb
commit 46846a10f6
3 changed files with 74 additions and 57 deletions

View file

@ -2,6 +2,7 @@
#include "../file_system.h" #include "../file_system.h"
#include "../log.h" #include "../log.h"
#include "../md5_digest.h" #include "../md5_digest.h"
#include "shader_compiler.h"
#include <d3dcompiler.h> #include <d3dcompiler.h>
Log_SetChannel(D3D11::ShaderCache); Log_SetChannel(D3D11::ShaderCache);
@ -234,15 +235,7 @@ ShaderCache::ComPtr<ID3D11VertexShader> ShaderCache::GetVertexShader(ID3D11Devic
if (!blob) if (!blob)
return {}; return {};
ComPtr<ID3D11VertexShader> vs; return D3D11::ShaderCompiler::CreateVertexShader(device, blob.Get());
HRESULT hr = device->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, vs.GetAddressOf());
if (FAILED(hr))
{
Log_ErrorPrintf("Failed to create vertex shader from blob: 0x%08X", hr);
return {};
}
return vs;
} }
ShaderCache::ComPtr<ID3D11GeometryShader> ShaderCache::GetGeometryShader(ID3D11Device* device, ShaderCache::ComPtr<ID3D11GeometryShader> ShaderCache::GetGeometryShader(ID3D11Device* device,
@ -252,16 +245,7 @@ ShaderCache::ComPtr<ID3D11GeometryShader> ShaderCache::GetGeometryShader(ID3D11D
if (!blob) if (!blob)
return {}; return {};
ComPtr<ID3D11GeometryShader> gs; return D3D11::ShaderCompiler::CreateGeometryShader(device, blob.Get());
HRESULT hr =
device->CreateGeometryShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, gs.GetAddressOf());
if (FAILED(hr))
{
Log_ErrorPrintf("Failed to create geometry shader from blob: 0x%08X", hr);
return {};
}
return gs;
} }
ShaderCache::ComPtr<ID3D11PixelShader> ShaderCache::GetPixelShader(ID3D11Device* device, std::string_view shader_code) ShaderCache::ComPtr<ID3D11PixelShader> ShaderCache::GetPixelShader(ID3D11Device* device, std::string_view shader_code)
@ -270,15 +254,7 @@ ShaderCache::ComPtr<ID3D11PixelShader> ShaderCache::GetPixelShader(ID3D11Device*
if (!blob) if (!blob)
return {}; return {};
ComPtr<ID3D11PixelShader> ps; return D3D11::ShaderCompiler::CreatePixelShader(device, blob.Get());
HRESULT hr = device->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, ps.GetAddressOf());
if (FAILED(hr))
{
Log_ErrorPrintf("Failed to create pixel shader from blob: 0x%08X", hr);
return {};
}
return ps;
} }
ShaderCache::ComPtr<ID3D11ComputeShader> ShaderCache::GetComputeShader(ID3D11Device* device, ShaderCache::ComPtr<ID3D11ComputeShader> ShaderCache::GetComputeShader(ID3D11Device* device,
@ -288,15 +264,7 @@ ShaderCache::ComPtr<ID3D11ComputeShader> ShaderCache::GetComputeShader(ID3D11Dev
if (!blob) if (!blob)
return {}; return {};
ComPtr<ID3D11ComputeShader> cs; return D3D11::ShaderCompiler::CreateComputeShader(device, blob.Get());
HRESULT hr = device->CreateComputeShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, cs.GetAddressOf());
if (FAILED(hr))
{
Log_ErrorPrintf("Failed to create compute shader from blob: 0x%08X", hr);
return {};
}
return cs;
} }
ShaderCache::ComPtr<ID3DBlob> ShaderCache::CompileAndAddShaderBlob(const CacheIndexKey& key, ShaderCache::ComPtr<ID3DBlob> ShaderCache::CompileAndAddShaderBlob(const CacheIndexKey& key,

View file

@ -90,9 +90,40 @@ ComPtr<ID3D11VertexShader> CompileAndCreateVertexShader(ID3D11Device* device, st
if (!blob) if (!blob)
return {}; return {};
return CreateVertexShader(device, blob.Get());
}
ComPtr<ID3D11GeometryShader> CompileAndCreateGeometryShader(ID3D11Device* device, std::string_view code, bool debug)
{
ComPtr<ID3DBlob> blob = CompileShader(Type::Geometry, device->GetFeatureLevel(), std::move(code), debug);
if (!blob)
return {};
return CreateGeometryShader(device, blob.Get());
}
ComPtr<ID3D11PixelShader> CompileAndCreatePixelShader(ID3D11Device* device, std::string_view code, bool debug)
{
ComPtr<ID3DBlob> blob = CompileShader(Type::Pixel, device->GetFeatureLevel(), std::move(code), debug);
if (!blob)
return {};
return CreatePixelShader(device, blob.Get());
}
ComPtr<ID3D11ComputeShader> CompileAndCreateComputeShader(ID3D11Device* device, std::string_view code, bool debug)
{
ComPtr<ID3DBlob> blob = CompileShader(Type::Compute, device->GetFeatureLevel(), std::move(code), debug);
if (!blob)
return {};
return CreateComputeShader(device, blob.Get());
}
ComPtr<ID3D11VertexShader> CreateVertexShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length)
{
ComPtr<ID3D11VertexShader> shader; ComPtr<ID3D11VertexShader> shader;
const HRESULT hr = const HRESULT hr = device->CreateVertexShader(bytecode, bytecode_length, nullptr, shader.GetAddressOf());
device->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, shader.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
Log_ErrorPrintf("Failed to create vertex shader: 0x%08X", hr); Log_ErrorPrintf("Failed to create vertex shader: 0x%08X", hr);
@ -102,15 +133,16 @@ ComPtr<ID3D11VertexShader> CompileAndCreateVertexShader(ID3D11Device* device, st
return shader; return shader;
} }
ComPtr<ID3D11GeometryShader> CompileAndCreateGeometryShader(ID3D11Device* device, std::string_view code, bool debug) ComPtr<ID3D11VertexShader> CreateVertexShader(ID3D11Device* device, const ID3DBlob* blob)
{ {
ComPtr<ID3DBlob> blob = CompileShader(Type::Geometry, device->GetFeatureLevel(), std::move(code), debug); return CreateVertexShader(device, const_cast<ID3DBlob*>(blob)->GetBufferPointer(),
if (!blob) const_cast<ID3DBlob*>(blob)->GetBufferSize());
return {}; }
ComPtr<ID3D11GeometryShader> CreateGeometryShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length)
{
ComPtr<ID3D11GeometryShader> shader; ComPtr<ID3D11GeometryShader> shader;
const HRESULT hr = const HRESULT hr = device->CreateGeometryShader(bytecode, bytecode_length, nullptr, shader.GetAddressOf());
device->CreateGeometryShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, shader.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
Log_ErrorPrintf("Failed to create geometry shader: 0x%08X", hr); Log_ErrorPrintf("Failed to create geometry shader: 0x%08X", hr);
@ -120,15 +152,16 @@ ComPtr<ID3D11GeometryShader> CompileAndCreateGeometryShader(ID3D11Device* device
return shader; return shader;
} }
ComPtr<ID3D11PixelShader> CompileAndCreatePixelShader(ID3D11Device* device, std::string_view code, bool debug) ComPtr<ID3D11GeometryShader> CreateGeometryShader(ID3D11Device* device, const ID3DBlob* blob)
{ {
ComPtr<ID3DBlob> blob = CompileShader(Type::Pixel, device->GetFeatureLevel(), std::move(code), debug); return CreateGeometryShader(device, const_cast<ID3DBlob*>(blob)->GetBufferPointer(),
if (!blob) const_cast<ID3DBlob*>(blob)->GetBufferSize());
return {}; }
ComPtr<ID3D11PixelShader> CreatePixelShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length)
{
ComPtr<ID3D11PixelShader> shader; ComPtr<ID3D11PixelShader> shader;
const HRESULT hr = const HRESULT hr = device->CreatePixelShader(bytecode, bytecode_length, nullptr, shader.GetAddressOf());
device->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, shader.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
Log_ErrorPrintf("Failed to create pixel shader: 0x%08X", hr); Log_ErrorPrintf("Failed to create pixel shader: 0x%08X", hr);
@ -138,15 +171,16 @@ ComPtr<ID3D11PixelShader> CompileAndCreatePixelShader(ID3D11Device* device, std:
return shader; return shader;
} }
ComPtr<ID3D11ComputeShader> CompileAndCreateComputeShader(ID3D11Device* device, std::string_view code, bool debug) ComPtr<ID3D11PixelShader> CreatePixelShader(ID3D11Device* device, const ID3DBlob* blob)
{ {
ComPtr<ID3DBlob> blob = CompileShader(Type::Compute, device->GetFeatureLevel(), std::move(code), debug); return CreatePixelShader(device, const_cast<ID3DBlob*>(blob)->GetBufferPointer(),
if (!blob) const_cast<ID3DBlob*>(blob)->GetBufferSize());
return {}; }
ComPtr<ID3D11ComputeShader> CreateComputeShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length)
{
ComPtr<ID3D11ComputeShader> shader; ComPtr<ID3D11ComputeShader> shader;
const HRESULT hr = const HRESULT hr = device->CreateComputeShader(bytecode, bytecode_length, nullptr, shader.GetAddressOf());
device->CreateComputeShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, shader.GetAddressOf());
if (FAILED(hr)) if (FAILED(hr))
{ {
Log_ErrorPrintf("Failed to create compute shader: 0x%08X", hr); Log_ErrorPrintf("Failed to create compute shader: 0x%08X", hr);
@ -156,4 +190,10 @@ ComPtr<ID3D11ComputeShader> CompileAndCreateComputeShader(ID3D11Device* device,
return shader; return shader;
} }
ComPtr<ID3D11ComputeShader> CreateComputeShader(ID3D11Device* device, const ID3DBlob* blob)
{
return CreateComputeShader(device, const_cast<ID3DBlob*>(blob)->GetBufferPointer(),
const_cast<ID3DBlob*>(blob)->GetBufferSize());
}
} // namespace D3D11::ShaderCompiler } // namespace D3D11::ShaderCompiler

View file

@ -24,4 +24,13 @@ ComPtr<ID3D11GeometryShader> CompileAndCreateGeometryShader(ID3D11Device* device
ComPtr<ID3D11PixelShader> CompileAndCreatePixelShader(ID3D11Device* device, std::string_view code, bool debug); ComPtr<ID3D11PixelShader> CompileAndCreatePixelShader(ID3D11Device* device, std::string_view code, bool debug);
ComPtr<ID3D11ComputeShader> CompileAndCreateComputeShader(ID3D11Device* device, std::string_view code, bool debug); ComPtr<ID3D11ComputeShader> CompileAndCreateComputeShader(ID3D11Device* device, std::string_view code, bool debug);
ComPtr<ID3D11VertexShader> CreateVertexShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length);
ComPtr<ID3D11VertexShader> CreateVertexShader(ID3D11Device* device, const ID3DBlob* blob);
ComPtr<ID3D11GeometryShader> CreateGeometryShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length);
ComPtr<ID3D11GeometryShader> CreateGeometryShader(ID3D11Device* device, const ID3DBlob* blob);
ComPtr<ID3D11PixelShader> CreatePixelShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length);
ComPtr<ID3D11PixelShader> CreatePixelShader(ID3D11Device* device, const ID3DBlob* blob);
ComPtr<ID3D11ComputeShader> CreateComputeShader(ID3D11Device* device, const void* bytecode, size_t bytecode_length);
ComPtr<ID3D11ComputeShader> CreateComputeShader(ID3D11Device* device, const ID3DBlob* blob);
}; // namespace D3D11::ShaderCompiler }; // namespace D3D11::ShaderCompiler