feat: 实现 D3D12Shader 着色器类

- 添加 D3D12Shader.h/cpp
- 支持从文件编译着色器
- 支持从内存编译着色器
- 测试通过
This commit is contained in:
2026-03-15 18:51:38 +08:00
parent db8e8633c8
commit c3feeda5d4
3 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include <d3dcompiler.h>
namespace XCEngine {
namespace RHI {
D3D12Shader::D3D12Shader()
: m_type(ShaderType::Vertex) {
}
D3D12Shader::~D3D12Shader() {
Shutdown();
}
bool D3D12Shader::CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) {
HRESULT hResult = D3DCompileFromFile(filePath, nullptr, nullptr, entryPoint, target,
D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, 0, &m_bytecode, &m_error);
if (FAILED(hResult)) {
if (m_error) {
const char* errorMsg = static_cast<const char*>(m_error->GetBufferPointer());
OutputDebugStringA(errorMsg);
}
return false;
}
if (strstr(target, "vs_")) {
m_type = ShaderType::Vertex;
} else if (strstr(target, "ps_")) {
m_type = ShaderType::Pixel;
} else if (strstr(target, "gs_")) {
m_type = ShaderType::Geometry;
} else if (strstr(target, "cs_")) {
m_type = ShaderType::Compute;
}
return true;
}
bool D3D12Shader::Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) {
HRESULT hResult = D3DCompile(sourceData, sourceSize, nullptr, nullptr, nullptr, entryPoint, target,
D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, 0, &m_bytecode, &m_error);
if (FAILED(hResult)) {
if (m_error) {
const char* errorMsg = static_cast<const char*>(m_error->GetBufferPointer());
OutputDebugStringA(errorMsg);
}
return false;
}
return true;
}
void D3D12Shader::Shutdown() {
m_bytecode.Reset();
m_error.Reset();
}
const D3D12_SHADER_BYTECODE D3D12Shader::GetBytecode() const {
D3D12_SHADER_BYTECODE bytecode = {};
if (m_bytecode) {
bytecode.pShaderBytecode = m_bytecode->GetBufferPointer();
bytecode.BytecodeLength = m_bytecode->GetBufferSize();
}
return bytecode;
}
} // namespace RHI
} // namespace XCEngine