Skip to content

Commit 7cc96a1

Browse files
committed
NBL_GL_* ext defines translations to Vulkan extensions
1 parent a2feb40 commit 7cc96a1

File tree

3 files changed

+121
-39
lines changed

3 files changed

+121
-39
lines changed

include/nbl/asset/IShader.h

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,56 @@ class IShader : public virtual core::IReferenceCounted
4242
if (!_exts)
4343
return;
4444

45-
auto findLineJustAfterVersionOrPragmaShaderStageDirective = [&_glsl]
46-
{
47-
size_t hashPos = _glsl.find_first_of('#');
48-
if (hashPos >= _glsl.length())
49-
return _glsl.npos;
50-
if (_glsl.compare(hashPos, 8, "#version"))
51-
return _glsl.npos;
52-
53-
size_t searchPos = hashPos + 8ull;
54-
55-
size_t hashPos2 = _glsl.find_first_of('#', hashPos+8ull);
56-
if (hashPos2<_glsl.length())
57-
{
58-
char pragma_stage_str[] = "#pragma shader_stage";
59-
if (_glsl.compare(hashPos2, sizeof(pragma_stage_str)-1ull, pragma_stage_str) == 0)
60-
searchPos = hashPos2 + sizeof(pragma_stage_str) - 1ull;
61-
}
62-
size_t nlPos = _glsl.find_first_of('\n', searchPos);
63-
64-
return (nlPos >= _glsl.length()) ? _glsl.npos : nlPos+1ull;
65-
};
66-
67-
const size_t pos = findLineJustAfterVersionOrPragmaShaderStageDirective();
68-
if (pos == _glsl.npos)
69-
return;
70-
71-
const size_t ln = std::count(_glsl.begin(),_glsl.begin()+pos, '\n')+1;//+1 to count from 1
72-
73-
std::string insertion = "\n";
74-
for (const std::string& ext : (*_exts))
75-
{
76-
std::string str = "#ifndef " + ext + "\n";
77-
str += "\t#define IRR_" + ext + "\n";
78-
str += "#endif //" + ext + "\n";
45+
const std::string insertion = genGLSLExtensionDefines(_exts);
7946

80-
insertion += str;
81-
}
82-
insertion += "#line " + std::to_string(ln) + "\n";
47+
insertAfterVersionAndPragmaShaderStage(_glsl, insertion);
48+
}
8349

84-
_glsl.insert(pos, insertion);
50+
protected:
51+
static inline std::string genGLSLExtensionDefines(const core::refctd_dynamic_array<std::string>* _exts)
52+
{
53+
std::string insertion = "\n";
54+
for (const std::string& ext : (*_exts))
55+
{
56+
std::string str = "#ifndef " + ext + "\n";
57+
str += "\t#define NBL_" + ext + "\n";
58+
str += "#endif //" + ext + "\n";
59+
60+
insertion += str;
8561
}
62+
}
63+
static inline void insertAfterVersionAndPragmaShaderStage(std::string& _glsl, const std::string& _ins)
64+
{
65+
auto findLineJustAfterVersionOrPragmaShaderStageDirective = [&_glsl]
66+
{
67+
size_t hashPos = _glsl.find_first_of('#');
68+
if (hashPos >= _glsl.length())
69+
return _glsl.npos;
70+
if (_glsl.compare(hashPos, 8, "#version"))
71+
return _glsl.npos;
72+
73+
size_t searchPos = hashPos + 8ull;
74+
75+
size_t hashPos2 = _glsl.find_first_of('#', hashPos + 8ull);
76+
if (hashPos2 < _glsl.length())
77+
{
78+
char pragma_stage_str[] = "#pragma shader_stage";
79+
if (_glsl.compare(hashPos2, sizeof(pragma_stage_str) - 1ull, pragma_stage_str) == 0)
80+
searchPos = hashPos2 + sizeof(pragma_stage_str) - 1ull;
81+
}
82+
size_t nlPos = _glsl.find_first_of('\n', searchPos);
83+
84+
return (nlPos >= _glsl.length()) ? _glsl.npos : nlPos + 1ull;
85+
};
86+
87+
const size_t pos = findLineJustAfterVersionOrPragmaShaderStageDirective();
88+
if (pos == _glsl.npos)
89+
return;
90+
91+
const size_t ln = std::count(_glsl.begin(), _glsl.begin() + pos, '\n') + 1;//+1 to count from 1
92+
93+
_glsl.insert(pos, _ins + "#line " + std::to_string(ln) + "\n");
94+
}
8695
};
8796

8897
}

source/Nabla/COpenGLDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ core::smart_refctd_ptr<IGPUSpecializedShader> COpenGLDriver::createGPUSpecialize
12651265
auto begin = reinterpret_cast<const char*>(glUnspec->getSPVorGLSL()->getPointer());
12661266
auto end = begin+glUnspec->getSPVorGLSL()->getSize();
12671267
std::string glsl(begin,end);
1268-
asset::ICPUShader::insertGLSLExtensionsDefines(glsl, getSupportedGLSLExtensions().get());
1268+
COpenGLShader::insertGLtoVKextensionsMapping(glsl, getSupportedGLSLExtensions().get());
12691269
auto glslShader_woIncludes = GLSLCompiler->resolveIncludeDirectives(glsl.c_str(), stage, _specInfo.m_filePathHint.c_str());
12701270
//{
12711271
// auto fl = fopen("shader.glsl", "w");

src/nbl/video/COpenGLShader.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,79 @@ class COpenGLShader : public IGPUShader
2222
const asset::ICPUBuffer* getSPVorGLSL() const { return m_code.get(); };
2323
bool containsGLSL() const { return m_containsGLSL; }
2424

25+
static inline void insertGLtoVKextensionsMapping(std::string& _glsl, const core::refctd_dynamic_array<std::string>* _exts)
26+
{
27+
if (!_exts)
28+
return;
29+
30+
const std::string insertion = genGLSLExtensionDefines(_exts) +
31+
R"(
32+
#ifdef NBL_GL_AMD_gpu_shader_half_float
33+
#define NBL_GL_EXT_shader_explicit_arithmetic_types_float16
34+
#endif
35+
36+
#ifdef NBL_GL_NV_gpu_shader5
37+
#define NBL_GL_EXT_shader_explicit_arithmetic_types_float16
38+
#define NBL_GL_EXT_nonuniform_qualifier
39+
#define NBL_GL_KHR_shader_subgroup_vote_subgroup_any_all_equal_bool
40+
#endif
41+
42+
#ifdef NBL_GL_AMD_gpu_shader_int16
43+
#define NBL_GL_EXT_shader_explicit_arithmetic_types_int16
44+
#endif
45+
46+
#ifdef NBL_GL_NV_shader_thread_group
47+
#define NBL_GL_KHR_shader_subgroup_ballot_subgroup_mask
48+
#define NBL_GL_KHR_shader_subgroup_basic_subgroup_size
49+
#define NBL_GL_KHR_shader_subgroup_basic_subgroup_invocation_id
50+
#define NBL_GL_KHR_shader_subgroup_ballot_subgroup_ballot
51+
#define NBL_GL_KHR_shader_subgroup_ballot_inverse_ballot_bit_count
52+
#endif
53+
54+
#if defined(NBL_GL_ARB_shader_ballot) && defined(NBL_GL_ARB_shader_int64)
55+
#define NBL_GL_KHR_shader_subgroup_ballot_subgroup_mask
56+
#define NBL_GL_KHR_shader_subgroup_basic_subgroup_size
57+
#define NBL_GL_KHR_shader_subgroup_basic_subgroup_invocation_id
58+
#define NBL_GL_KHR_shader_subgroup_ballot_subgroup_broadcast_first
59+
#define NBL_GL_KHR_shader_subgroup_ballot_subgroup_ballot
60+
#define NBL_GL_KHR_shader_subgroup_ballot_inverse_ballot_bit_count
61+
#endif
62+
63+
#if defined(NBL_GL_AMD_gcn_shader) && (defined(NBL_GL_AMD_gpu_shader_int64) || defined(GL_NV_gpu_shader5))
64+
#define NBL_GL_KHR_shader_subgroup_basic_subgroup_size
65+
#define NBL_GL_KHR_shader_subgroup_vote_subgroup_any_all_equal_bool
66+
#endif
67+
68+
#ifdef NBL_GL_NV_shader_thread_shuffle
69+
#define NBL_GL_KHR_shader_subgroup_ballot_subgroup_broadcast_first
70+
#endif
71+
72+
#ifdef NBL_GL_ARB_shader_group_vote
73+
#define NBL_GL_KHR_shader_subgroup_vote_subgroup_any_all_equal_bool
74+
#endif
75+
76+
#if defined(NBL_GL_KHR_shader_subgroup_ballot_subgroup_broadcast_first) && defined(NBL_GL_KHR_shader_subgroup_vote_subgroup_any_all_equal_bool)
77+
#define NBL_GL_KHR_shader_subgroup_vote_subgroup_all_equal_T
78+
#endif
79+
80+
#if defined(NBL_GL_KHR_shader_subgroup_ballot_subgroup_ballot) && defined(NBL_GL_KHR_shader_subgroup_basic_subgroup_invocation_id)
81+
#define NBL_GL_KHR_shader_subgroup_basic_subgroup_elect
82+
#endif
83+
84+
#ifdef NBL_GL_KHR_shader_subgroup_ballot_subgroup_mask
85+
#define NBL_GL_KHR_shader_subgroup_ballot_inverse_ballot
86+
#define NBL_GL_KHR_shader_subgroup_ballot_inclusive_bit_count
87+
#define NBL_GL_KHR_shader_subgroup_ballot_exclusive_bit_count
88+
#endif
89+
90+
#ifdef NBL_GL_KHR_shader_subgroup_ballot_subgroup_ballot
91+
#define NBL_GL_KHR_shader_subgroup_ballot_bit_count
92+
#endif
93+
)";
94+
95+
insertAfterVersionAndPragmaShaderStage(_glsl, insertion);
96+
}
97+
2598
private:
2699
friend class COpenGLDriver;
27100
//! Might be GLSL null-terminated string or SPIR-V bytecode (denoted by m_containsGLSL)

0 commit comments

Comments
 (0)