#include "XCEngine/RHI/D3D12/D3D12Shader.h" #include 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(m_error->GetBufferPointer()); OutputDebugStringA(errorMsg); } return false; } if (strstr(target, "vs_")) { m_type = ShaderType::Vertex; } else if (strstr(target, "ps_")) { m_type = ShaderType::Fragment; } 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(m_error->GetBufferPointer()); OutputDebugStringA(errorMsg); } return false; } return true; } void D3D12Shader::Shutdown() { m_bytecode.Reset(); m_error.Reset(); } const D3D12_SHADER_BYTECODE D3D12Shader::GetD3D12Bytecode() const { D3D12_SHADER_BYTECODE bytecode = {}; if (m_bytecode) { bytecode.pShaderBytecode = m_bytecode->GetBufferPointer(); bytecode.BytecodeLength = m_bytecode->GetBufferSize(); } return bytecode; } const void* D3D12Shader::GetBytecode() const { if (m_bytecode) { return m_bytecode->GetBufferPointer(); } return nullptr; } size_t D3D12Shader::GetBytecodeSize() const { if (m_bytecode) { return m_bytecode->GetBufferSize(); } return 0; } const InputLayoutDesc& D3D12Shader::GetInputLayout() const { return m_inputLayout; } ShaderType D3D12Shader::GetType() const { return m_type; } } // namespace RHI } // namespace XCEngine