Skip to content

Commit 40ba05f

Browse files
committed
made shaderCompiler functions virtual
1 parent d88e08e commit 40ba05f

File tree

5 files changed

+61
-47
lines changed

5 files changed

+61
-47
lines changed

include/nbl/asset/utils/CGLSLCompiler.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ class NBL_API2 CGLSLCompiler final : public IShaderCompiler
4343
@returns Shader containing SPIR-V bytecode.
4444
*/
4545

46-
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(const char* code, const CGLSLCompiler::SOptions& options) const;
47-
48-
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(system::IFile* sourceFile, const CGLSLCompiler::SOptions& options) const;
46+
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(const char* code, const IShaderCompiler::SOptions& options) const override;
4947

5048
/*
5149
If original code contains #version specifier,
@@ -126,6 +124,19 @@ class NBL_API2 CGLSLCompiler final : public IShaderCompiler
126124
return "";
127125
}
128126
}
127+
128+
protected:
129+
130+
static CGLSLCompiler::SOptions option_cast(const IShaderCompiler::SOptions& options)
131+
{
132+
CGLSLCompiler::SOptions ret = {};
133+
if (options.getCodeContentType() == IShader::E_CONTENT_TYPE::ECT_GLSL)
134+
ret = static_cast<const CGLSLCompiler::SOptions&>(options);
135+
else
136+
ret.setCommonData(options);
137+
return ret;
138+
}
139+
129140
};
130141

131142
}

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
2424
IShader::E_CONTENT_TYPE getCodeContentType() const override { return IShader::E_CONTENT_TYPE::ECT_HLSL; };
2525
};
2626

27-
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(const char* code, const CHLSLCompiler::SOptions& options) const;
28-
29-
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(system::IFile* sourceFile, const CHLSLCompiler::SOptions& options) const;
27+
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(const char* code, const IShaderCompiler::SOptions& options) const override;
3028

3129
template<typename... Args>
3230
static core::smart_refctd_ptr<ICPUShader> createOverridenCopy(const ICPUShader* original, const char* fmt, Args... args)
@@ -39,6 +37,18 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
3937
//{
4038
// return "";
4139
//}
40+
41+
protected:
42+
43+
static CHLSLCompiler::SOptions option_cast(const IShaderCompiler::SOptions& options)
44+
{
45+
CHLSLCompiler::SOptions ret = {};
46+
if (options.getCodeContentType() == IShader::E_CONTENT_TYPE::ECT_GLSL)
47+
ret = static_cast<const CHLSLCompiler::SOptions&>(options);
48+
else
49+
ret.setCommonData(options);
50+
return ret;
51+
}
4252
};
4353

4454
}

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
141141
virtual IShader::E_CONTENT_TYPE getCodeContentType() const { return IShader::E_CONTENT_TYPE::ECT_UNKNOWN; };
142142
};
143143

144+
virtual core::smart_refctd_ptr<ICPUShader> compileToSPIRV(const char* code, const SOptions& options) const = 0;
145+
146+
inline core::smart_refctd_ptr<ICPUShader> compileToSPIRV(system::IFile* sourceFile, const SOptions& options) const
147+
{
148+
size_t fileSize = sourceFile->getSize();
149+
std::string code(fileSize, '\0');
150+
151+
system::IFile::success_t success;
152+
sourceFile->read(success, code.data(), 0, fileSize);
153+
if (success)
154+
return compileToSPIRV(code.c_str(), options);
155+
else
156+
return nullptr;
157+
}
158+
144159
/**
145160
Resolves ALL #include directives regardless of any other preprocessor directive.
146161
This is done in order to support `#include` AND simultaneulsy be able to store (serialize) such ICPUShader (mostly High Level source) into ONE file which, upon loading, will compile on every hardware/driver predicted by shader's author.

src/nbl/asset/utils/CGLSLCompiler.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@ CGLSLCompiler::CGLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
1818
{
1919
}
2020

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
2222
{
23+
auto glslOptions = option_cast(options);
24+
2325
if (!code)
2426
{
25-
options.logger.log("code is nullptr", system::ILogger::ELL_ERROR);
27+
glslOptions.logger.log("code is nullptr", system::ILogger::ELL_ERROR);
2628
return nullptr;
2729
}
2830

29-
if (options.entryPoint.compare("main") != 0)
31+
if (glslOptions.entryPoint.compare("main") != 0)
3032
{
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);
3234
return nullptr;
3335
}
3436

3537
core::smart_refctd_ptr<asset::ICPUShader> cpuShader;
36-
if (options.includeFinder != nullptr)
38+
if (glslOptions.includeFinder != nullptr)
3739
{
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);
3941
if (cpuShader)
4042
{
4143
code = reinterpret_cast<const char*>(cpuShader->getContent()->getPointer());
@@ -44,39 +46,26 @@ core::smart_refctd_ptr<ICPUShader> CGLSLCompiler::compileToSPIRV(const char* cod
4446

4547
shaderc::Compiler comp;
4648
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);
4951
const size_t glsl_len = strlen(code);
50-
if (options.genDebugInfo)
52+
if (glslOptions.genDebugInfo)
5153
shadercOptions.SetGenerateDebugInfo();
5254

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);
5456

5557
if (bin_res.GetCompilationStatus() == shaderc_compilation_status_success)
5658
{
5759
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(std::distance(bin_res.cbegin(), bin_res.cend()) * sizeof(uint32_t));
5860
memcpy(outSpirv->getPointer(), bin_res.cbegin(), outSpirv->getSize());
5961

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());
6365
}
6466
else
6567
{
66-
options.logger.log(bin_res.GetErrorMessage(), system::ILogger::ELL_ERROR);
68+
glslOptions.logger.log(bin_res.GetErrorMessage(), system::ILogger::ELL_ERROR);
6769
return nullptr;
6870
}
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;
8271
}

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ CHLSLCompiler::CHLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
1818
{
1919
}
2020

21-
core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* code, const CHLSLCompiler::SOptions& options) const
21+
core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* code, const IShaderCompiler::SOptions& options) const
2222
{
23+
auto hlslOptions = option_cast(options);
24+
2325
if (!code)
2426
{
2527
options.logger.log("code is nullptr", system::ILogger::ELL_ERROR);
@@ -30,16 +32,3 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
3032
// TODO: Use DXC
3133
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move(spirv), options.stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, options.sourceIdentifier.data());
3234
}
33-
34-
core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(system::IFile* sourceFile, const CHLSLCompiler::SOptions& options) const
35-
{
36-
size_t fileSize = sourceFile->getSize();
37-
std::string code(fileSize, '\0');
38-
39-
system::IFile::success_t success;
40-
sourceFile->read(success, code.data(), 0, fileSize);
41-
if (success)
42-
return compileToSPIRV(code.c_str(), options);
43-
else
44-
return nullptr;
45-
}

0 commit comments

Comments
 (0)