@@ -18,24 +18,26 @@ CGLSLCompiler::CGLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
18
18
{
19
19
}
20
20
21
- core::smart_refctd_ptr<ICPUShader> CGLSLCompiler::compileToSPIRV (const char * code, const CGLSLCompiler ::SOptions& options) const
21
+ core::smart_refctd_ptr<ICPUShader> CGLSLCompiler::compileToSPIRV (const char * code, const IShaderCompiler ::SOptions& options) const
22
22
{
23
+ auto glslOptions = option_cast (options);
24
+
23
25
if (!code)
24
26
{
25
- options .logger .log (" code is nullptr" , system::ILogger::ELL_ERROR);
27
+ glslOptions .logger .log (" code is nullptr" , system::ILogger::ELL_ERROR);
26
28
return nullptr ;
27
29
}
28
30
29
- if (options .entryPoint .compare (" main" ) != 0 )
31
+ if (glslOptions .entryPoint .compare (" main" ) != 0 )
30
32
{
31
- options .logger .log (" shaderc requires entry point to be \" main\" in GLSL" , system::ILogger::ELL_ERROR);
33
+ glslOptions .logger .log (" shaderc requires entry point to be \" main\" in GLSL" , system::ILogger::ELL_ERROR);
32
34
return nullptr ;
33
35
}
34
36
35
37
core::smart_refctd_ptr<asset::ICPUShader> cpuShader;
36
- if (options .includeFinder != nullptr )
38
+ if (glslOptions .includeFinder != nullptr )
37
39
{
38
- cpuShader = resolveIncludeDirectives (code, options .stage , options .sourceIdentifier .data (), options .maxSelfInclusionCount , options .logger );
40
+ cpuShader = resolveIncludeDirectives (code, glslOptions .stage , glslOptions .sourceIdentifier .data (), glslOptions .maxSelfInclusionCount , glslOptions .logger );
39
41
if (cpuShader)
40
42
{
41
43
code = reinterpret_cast <const char *>(cpuShader->getContent ()->getPointer ());
@@ -44,39 +46,26 @@ core::smart_refctd_ptr<ICPUShader> CGLSLCompiler::compileToSPIRV(const char* cod
44
46
45
47
shaderc::Compiler comp;
46
48
shaderc::CompileOptions shadercOptions; // default options
47
- shadercOptions.SetTargetSpirv (static_cast <shaderc_spirv_version>(options .targetSpirvVersion ));
48
- const shaderc_shader_kind stage = options .stage == IShader::ESS_UNKNOWN ? shaderc_glsl_infer_from_source : ESStoShadercEnum (options .stage );
49
+ shadercOptions.SetTargetSpirv (static_cast <shaderc_spirv_version>(glslOptions .targetSpirvVersion ));
50
+ const shaderc_shader_kind stage = glslOptions .stage == IShader::ESS_UNKNOWN ? shaderc_glsl_infer_from_source : ESStoShadercEnum (glslOptions .stage );
49
51
const size_t glsl_len = strlen (code);
50
- if (options .genDebugInfo )
52
+ if (glslOptions .genDebugInfo )
51
53
shadercOptions.SetGenerateDebugInfo ();
52
54
53
- shaderc::SpvCompilationResult bin_res = comp.CompileGlslToSpv (code, glsl_len, stage, options .sourceIdentifier .data () ? options .sourceIdentifier .data () : " " , options .entryPoint .data (), shadercOptions);
55
+ shaderc::SpvCompilationResult bin_res = comp.CompileGlslToSpv (code, glsl_len, stage, glslOptions .sourceIdentifier .data () ? glslOptions .sourceIdentifier .data () : " " , glslOptions .entryPoint .data (), shadercOptions);
54
56
55
57
if (bin_res.GetCompilationStatus () == shaderc_compilation_status_success)
56
58
{
57
59
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(std::distance (bin_res.cbegin (), bin_res.cend ()) * sizeof (uint32_t ));
58
60
memcpy (outSpirv->getPointer (), bin_res.cbegin (), outSpirv->getSize ());
59
61
60
- if (options .spirvOptimizer )
61
- outSpirv = options .spirvOptimizer ->optimize (outSpirv.get (), options .logger );
62
- return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move (outSpirv), options .stage , IShader::E_CONTENT_TYPE::ECT_SPIRV, options .sourceIdentifier .data ());
62
+ if (glslOptions .spirvOptimizer )
63
+ outSpirv = glslOptions .spirvOptimizer ->optimize (outSpirv.get (), glslOptions .logger );
64
+ return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move (outSpirv), glslOptions .stage , IShader::E_CONTENT_TYPE::ECT_SPIRV, glslOptions .sourceIdentifier .data ());
63
65
}
64
66
else
65
67
{
66
- options .logger .log (bin_res.GetErrorMessage (), system::ILogger::ELL_ERROR);
68
+ glslOptions .logger .log (bin_res.GetErrorMessage (), system::ILogger::ELL_ERROR);
67
69
return nullptr ;
68
70
}
69
- }
70
-
71
- core::smart_refctd_ptr<ICPUShader> CGLSLCompiler::compileToSPIRV (system::IFile* sourceFile, const CGLSLCompiler::SOptions& options) const
72
- {
73
- size_t fileSize = sourceFile->getSize ();
74
- std::string code (fileSize, ' \0 ' );
75
-
76
- system::IFile::success_t success;
77
- sourceFile->read (success, code.data (), 0 , fileSize);
78
- if (success)
79
- return compileToSPIRV (code.c_str (), options);
80
- else
81
- return nullptr ;
82
71
}
0 commit comments