RHI: Add embedded shader source support via ShaderCompileDesc

- Add ShaderLanguage enum (HLSL, GLSL, SPIRV)
- Extend ShaderCompileDesc with source/sourceLanguage fields
- D3D12Device::CompileShader supports both file and embedded source
- OpenGLDevice::CompileShader supports embedded GLSL source
- Refactor test_shader.cpp to use embedded source for both backends

This enables consistent shader compilation across D3D12 and OpenGL
backends while maintaining backend-specific shader language support.
This commit is contained in:
2026-03-25 12:00:26 +08:00
parent 600892bbe2
commit 32c04b86b7
5 changed files with 178 additions and 45 deletions

View File

@@ -19,6 +19,13 @@ enum class ShaderType : uint8_t {
Library
};
enum class ShaderLanguage : uint8_t {
Unknown,
HLSL,
GLSL,
SPIRV
};
enum class CullMode : uint8_t {
None,
Front,

View File

@@ -43,10 +43,14 @@ struct ShaderCompileMacro {
std::wstring definition;
};
enum class ShaderLanguage : uint8_t;
struct ShaderCompileDesc {
std::wstring fileName;
std::vector<uint8_t> source;
ShaderLanguage sourceLanguage = ShaderLanguage::Unknown;
std::wstring entryPoint;
std::wstring profile;
std::wstring fileName;
std::vector<ShaderCompileMacro> macros;
};

View File

@@ -293,9 +293,17 @@ RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc) {
RHIShader* D3D12Device::CompileShader(const ShaderCompileDesc& desc) {
auto* shader = new D3D12Shader();
if (shader->CompileFromFile(desc.fileName.c_str(),
reinterpret_cast<const char*>(desc.entryPoint.c_str()),
reinterpret_cast<const char*>(desc.profile.c_str()))) {
const char* entryPoint = desc.entryPoint.empty() ? nullptr : reinterpret_cast<const char*>(desc.entryPoint.c_str());
const char* profile = desc.profile.empty() ? nullptr : reinterpret_cast<const char*>(desc.profile.c_str());
bool success = false;
if (!desc.source.empty()) {
success = shader->Compile(desc.source.data(), desc.source.size(), entryPoint, profile);
} else if (!desc.fileName.empty()) {
success = shader->CompileFromFile(desc.fileName.c_str(), entryPoint, profile);
}
if (success) {
return shader;
}
delete shader;

View File

@@ -353,15 +353,40 @@ RHICommandQueue* OpenGLDevice::CreateCommandQueue(const CommandQueueDesc& desc)
}
RHIShader* OpenGLDevice::CompileShader(const ShaderCompileDesc& desc) {
std::wstring filePath = desc.fileName;
if (filePath.empty()) {
auto* shader = new OpenGLShader();
if (desc.sourceLanguage == ShaderLanguage::GLSL && !desc.source.empty()) {
const char* sourceStr = reinterpret_cast<const char*>(desc.source.data());
ShaderType shaderType = ShaderType::Vertex;
std::string profile(desc.profile.begin(), desc.profile.end());
if (profile.find("vs") != std::string::npos) {
shaderType = ShaderType::Vertex;
} else if (profile.find("ps") != std::string::npos || profile.find("fs") != std::string::npos) {
shaderType = ShaderType::Fragment;
} else if (profile.find("gs") != std::string::npos) {
shaderType = ShaderType::Geometry;
} else if (profile.find("cs") != std::string::npos) {
shaderType = ShaderType::Compute;
}
if (shader->Compile(sourceStr, shaderType)) {
return shader;
}
delete shader;
return nullptr;
}
auto* shader = new OpenGLShader();
std::string entryPoint(desc.entryPoint.begin(), desc.entryPoint.end());
std::string profile(desc.profile.begin(), desc.profile.end());
shader->CompileFromFile(filePath.c_str(), entryPoint.c_str(), profile.c_str());
return shader;
if (!desc.fileName.empty()) {
std::wstring filePath = desc.fileName;
std::string entryPoint(desc.entryPoint.begin(), desc.entryPoint.end());
std::string profile(desc.profile.begin(), desc.profile.end());
shader->CompileFromFile(filePath.c_str(), entryPoint.c_str(), profile.c_str());
return shader;
}
delete shader;
return nullptr;
}
RHIPipelineState* OpenGLDevice::CreatePipelineState(const GraphicsPipelineDesc& desc) {