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

@@ -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) {