| 34 | } |
| 35 | |
| 36 | std::unique_ptr<spirv_cross::Compiler> CompileShader(glslang::TProgram& program, EShLanguage stage, gsl::span<const spirv_cross::HLSLVertexAttributeRemap> attributes, ID3DBlob** blob) |
| 37 | { |
| 38 | std::vector<uint32_t> spirv; |
| 39 | glslang::GlslangToSpv(*program.getIntermediate(stage), spirv); |
| 40 | |
| 41 | spirv_cross::Parser parser{std::move(spirv)}; |
| 42 | parser.parse(); |
| 43 | |
| 44 | auto compiler = std::make_unique<spirv_cross::CompilerHLSL>(parser.get_parsed_ir()); |
| 45 | compiler->set_hlsl_options({40}); |
| 46 | |
| 47 | for (const auto& attribute : attributes) |
| 48 | { |
| 49 | compiler->add_vertex_attribute_remap(attribute); |
| 50 | } |
| 51 | |
| 52 | std::string hlsl = compiler->compile(); |
| 53 | |
| 54 | Microsoft::WRL::ComPtr<ID3DBlob> errorMsgs; |
| 55 | const char* target = stage == EShLangVertex ? "vs_4_0" : "ps_4_0"; |
| 56 | |
| 57 | UINT flags = 0; |
| 58 | |
| 59 | #ifdef _DEBUG |
| 60 | flags |= D3DCOMPILE_DEBUG; |
| 61 | #endif |
| 62 | |
| 63 | if (FAILED(D3DCompile(hlsl.data(), hlsl.size(), nullptr, nullptr, nullptr, "main", target, flags, 0, blob, &errorMsgs))) |
| 64 | { |
| 65 | throw std::exception(static_cast<const char*>(errorMsgs->GetBufferPointer())); |
| 66 | } |
| 67 | |
| 68 | return std::move(compiler); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | ShaderCompiler::ShaderCompiler() |