Skip to content

Commit c227ccf

Browse files
committed
Merge branch 'compiler_cleanup'
2 parents dac9855 + ba53410 commit c227ccf

File tree

85 files changed

+2981
-4263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2981
-4263
lines changed

include/nbl/asset/IAssetManager.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "nbl/asset/interchange/IAssetLoader.h"
1818
#include "nbl/asset/interchange/IAssetWriter.h"
1919

20-
#include "nbl/asset/utils/IGLSLCompiler.h"
20+
#include "nbl/asset/utils/CCompilerSet.h"
2121
#include "nbl/asset/utils/IGeometryCreator.h"
2222

2323

@@ -119,14 +119,15 @@ class NBL_API IAssetManager : public core::IReferenceCounted, public core::QuitS
119119

120120
core::smart_refctd_ptr<IGeometryCreator> m_geometryCreator;
121121
core::smart_refctd_ptr<IMeshManipulator> m_meshManipulator;
122-
core::smart_refctd_ptr<IGLSLCompiler> m_glslCompiler;
122+
core::smart_refctd_ptr<CCompilerSet> m_compilerSet;
123123
// called as a part of constructor only
124124
void initializeMeshTools();
125125

126126
public:
127127
//! Constructor
128-
explicit IAssetManager(core::smart_refctd_ptr<system::ISystem>&& _s) :
129-
m_system(std::move(_s)),
128+
explicit IAssetManager(core::smart_refctd_ptr<system::ISystem>&& system, core::smart_refctd_ptr<CCompilerSet>&& compilerSet = nullptr) :
129+
m_system(std::move(system)),
130+
m_compilerSet(std::move(compilerSet)),
130131
m_defaultLoaderOverride(this)
131132
{
132133
initializeMeshTools();
@@ -144,7 +145,7 @@ class NBL_API IAssetManager : public core::IReferenceCounted, public core::QuitS
144145

145146
const IGeometryCreator* getGeometryCreator() const;
146147
IMeshManipulator* getMeshManipulator();
147-
IGLSLCompiler* getGLSLCompiler() const { return m_glslCompiler.get(); }
148+
CCompilerSet* getCompilerSet() const { return m_compilerSet.get(); }
148149

149150
protected:
150151
virtual ~IAssetManager()

include/nbl/asset/ICPUBuffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ template<
125125
>
126126
class NBL_API CCustomAllocatorCPUBuffer;
127127

128+
using CDummyCPUBuffer = CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t>, true>;
129+
128130
//! Specialization of ICPUBuffer capable of taking custom allocators
129131
/*
130132
Take a look that with this usage you have to specify custom alloctor

include/nbl/asset/ICPUShader.h

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,22 @@ class NBL_API ICPUShader : public IAsset, public IShader
2929
protected:
3030
virtual ~ICPUShader() = default;
3131

32-
private:
33-
ICPUShader(core::smart_refctd_ptr<ICPUBuffer>&& _code, bool _isGLSL, const E_SHADER_STAGE stage, std::string&& filepathHint)
34-
: IShader(stage, std::move(filepathHint)), m_code(std::move(_code)), m_containsGLSL(_isGLSL)
35-
{}
36-
3732
public:
38-
ICPUShader(
39-
core::smart_refctd_ptr<ICPUBuffer>&& _spirv,
40-
const E_SHADER_STAGE stage,
41-
std::string&& filepathHint)
42-
: ICPUShader(std::move(_spirv), false, stage, std::move(filepathHint))
43-
{}
4433

45-
ICPUShader(
46-
core::smart_refctd_ptr<ICPUBuffer>&& _glsl,
47-
buffer_contains_glsl_t _buffer_contains_glsl,
48-
const E_SHADER_STAGE stage,
49-
std::string&& filepathHint)
50-
: ICPUShader(std::move(_glsl), true, stage, std::move(filepathHint))
34+
ICPUShader(core::smart_refctd_ptr<ICPUBuffer>&& code, const E_SHADER_STAGE stage, E_CONTENT_TYPE contentType, std::string&& filepathHint)
35+
: IShader(stage, std::move(filepathHint)), m_code(std::move(code))
36+
, m_contentType(contentType)
5137
{}
5238

5339
ICPUShader(
54-
const char* _glsl,
40+
const char* code,
5541
const E_SHADER_STAGE stage,
56-
std::string&& filepathHint)
57-
: ICPUShader(core::make_smart_refctd_ptr<ICPUBuffer>(strlen(_glsl) + 1u), true,
58-
stage, std::move(filepathHint))
42+
E_CONTENT_TYPE contentType,
43+
std::string&& filepathHint)
44+
: ICPUShader(core::make_smart_refctd_ptr<ICPUBuffer>(strlen(code) + 1u), stage, contentType, std::move(filepathHint))
5945
{
60-
memcpy(m_code->getPointer(), _glsl, m_code->getSize());
46+
assert(contentType != E_CONTENT_TYPE::ECT_SPIRV); // because using strlen needs `code` to be null-terminated
47+
memcpy(m_code->getPointer(), code, m_code->getSize());
6148
}
6249

6350
_NBL_STATIC_INLINE_CONSTEXPR auto AssetType = ET_SHADER;
@@ -70,26 +57,32 @@ class NBL_API ICPUShader : public IAsset, public IShader
7057
return estimate;
7158
}
7259

73-
core::smart_refctd_ptr<IAsset> clone(uint32_t _depth = ~0u) const override
74-
{
75-
auto buf = (_depth > 0u && m_code) ? core::smart_refctd_ptr_static_cast<ICPUBuffer>(m_code->clone(_depth-1u)) : m_code;
76-
auto cp = core::smart_refctd_ptr<ICPUShader>(new ICPUShader(std::move(buf), m_containsGLSL, getStage(), std::string(getFilepathHint())), core::dont_grab);
77-
clone_common(cp.get());
60+
core::smart_refctd_ptr<IAsset> clone(uint32_t _depth = ~0u) const override
61+
{
62+
auto buf = (_depth > 0u && m_code) ? core::smart_refctd_ptr_static_cast<ICPUBuffer>(m_code->clone(_depth-1u)) : m_code;
63+
auto cp = core::smart_refctd_ptr<ICPUShader>(new ICPUShader(std::move(buf), getStage(), m_contentType, std::string(getFilepathHint())), core::dont_grab);
64+
clone_common(cp.get());
7865

79-
return cp;
80-
}
66+
return cp;
67+
}
8168

8269
void convertToDummyObject(uint32_t referenceLevelsBelowToConvert=0u) override
8370
{
84-
convertToDummyObject_common(referenceLevelsBelowToConvert);
71+
convertToDummyObject_common(referenceLevelsBelowToConvert);
8572

8673
if (referenceLevelsBelowToConvert)
8774
m_code->convertToDummyObject(referenceLevelsBelowToConvert-1u);
8875
}
8976

90-
const ICPUBuffer* getSPVorGLSL() const { return m_code.get(); };
91-
bool containsGLSL() const { return m_containsGLSL; }
77+
const ICPUBuffer* getContent() const { return m_code.get(); };
78+
79+
inline E_CONTENT_TYPE getContentType() const { return m_contentType; }
9280

81+
inline bool isContentHighLevelLanguage() const
82+
{
83+
return (m_contentType == E_CONTENT_TYPE::ECT_GLSL || m_contentType == E_CONTENT_TYPE::ECT_HLSL);
84+
}
85+
9386
bool setShaderStage(const E_SHADER_STAGE stage)
9487
{
9588
if(isImmutable_debug())
@@ -109,7 +102,7 @@ class NBL_API ICPUShader : public IAsset, public IShader
109102
bool canBeRestoredFrom(const IAsset* _other) const override
110103
{
111104
auto* other = static_cast<const ICPUShader*>(_other);
112-
if (m_containsGLSL != other->m_containsGLSL)
105+
if (m_contentType != other->m_contentType)
113106
return false;
114107
if (getFilepathHint() != other->getFilepathHint())
115108
return false;
@@ -140,9 +133,8 @@ class NBL_API ICPUShader : public IAsset, public IShader
140133
return m_code->isAnyDependencyDummy(_levelsBelow);
141134
}
142135

143-
//! Might be GLSL null-terminated string or SPIR-V bytecode (denoted by m_containsGLSL)
144-
core::smart_refctd_ptr<ICPUBuffer> m_code;
145-
const bool m_containsGLSL;
136+
core::smart_refctd_ptr<ICPUBuffer> m_code;
137+
E_CONTENT_TYPE m_contentType;
146138
};
147139

148140
}

include/nbl/asset/ISPIR_VProgram.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

include/nbl/asset/IShader.h

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5-
#ifndef __NBL_ASSET_I_SHADER_H_INCLUDED__
6-
#define __NBL_ASSET_I_SHADER_H_INCLUDED__
5+
#ifndef _NBL_ASSET_I_SHADER_H_INCLUDED_
6+
#define _NBL_ASSET_I_SHADER_H_INCLUDED_
77

88
#include <algorithm>
99
#include <string>
@@ -13,9 +13,9 @@
1313

1414
namespace spirv_cross
1515
{
16-
class ParsedIR;
17-
class Compiler;
18-
struct SPIRType;
16+
class ParsedIR;
17+
class Compiler;
18+
struct SPIRType;
1919
}
2020

2121
namespace nbl::asset
@@ -32,6 +32,7 @@ namespace nbl::asset
3232
class NBL_API IShader : public virtual core::IReferenceCounted
3333
{
3434
public:
35+
// TODO: make this enum class
3536
enum E_SHADER_STAGE : uint32_t
3637
{
3738
ESS_UNKNOWN = 0,
@@ -53,8 +54,13 @@ class NBL_API IShader : public virtual core::IReferenceCounted
5354
ESS_ALL = 0x7fffffff
5455
};
5556

56-
struct buffer_contains_glsl_t {};
57-
_NBL_STATIC_INLINE const buffer_contains_glsl_t buffer_contains_glsl = {};
57+
enum class E_CONTENT_TYPE : uint8_t
58+
{
59+
ECT_UNKNOWN = 0,
60+
ECT_GLSL,
61+
ECT_HLSL,
62+
ECT_SPIRV,
63+
};
5864

5965
IShader(const E_SHADER_STAGE shaderStage, std::string&& filepathHint)
6066
: m_shaderStage(shaderStage), m_filepathHint(std::move(filepathHint))
@@ -64,55 +70,6 @@ class NBL_API IShader : public virtual core::IReferenceCounted
6470

6571
inline const std::string& getFilepathHint() const { return m_filepathHint; }
6672

67-
static inline void insertDefines(std::string& _glsl, const core::SRange<const char* const>& _defines)
68-
{
69-
if (_defines.empty())
70-
return;
71-
72-
std::ostringstream insertion;
73-
for (auto def : _defines)
74-
{
75-
insertion << "#define "<<def<<"\n";
76-
}
77-
insertAfterVersionAndPragmaShaderStage(_glsl,std::move(insertion));
78-
}
79-
80-
// TODO: can make it protected again AFTER we get rid of `COpenGLShader::k_openGL2VulkanExtensionMap`
81-
//protected:
82-
static inline void insertAfterVersionAndPragmaShaderStage(std::string& _glsl, std::ostringstream&& _ins)
83-
{
84-
auto findLineJustAfterVersionOrPragmaShaderStageDirective = [&_glsl] () -> size_t
85-
{
86-
size_t hashPos = _glsl.find_first_of('#');
87-
if (hashPos >= _glsl.length())
88-
return _glsl.npos;
89-
if (_glsl.compare(hashPos, 8, "#version"))
90-
return _glsl.npos;
91-
92-
size_t searchPos = hashPos + 8ull;
93-
94-
size_t hashPos2 = _glsl.find_first_of('#', hashPos + 8ull);
95-
if (hashPos2 < _glsl.length())
96-
{
97-
char pragma_stage_str[] = "#pragma shader_stage";
98-
if (_glsl.compare(hashPos2, sizeof(pragma_stage_str) - 1ull, pragma_stage_str) == 0)
99-
searchPos = hashPos2 + sizeof(pragma_stage_str) - 1ull;
100-
}
101-
size_t nlPos = _glsl.find_first_of('\n', searchPos);
102-
103-
return (nlPos >= _glsl.length()) ? _glsl.npos : nlPos + 1ull;
104-
};
105-
106-
const size_t pos = findLineJustAfterVersionOrPragmaShaderStageDirective();
107-
if (pos == _glsl.npos)
108-
return;
109-
110-
const size_t ln = std::count(_glsl.begin(), _glsl.begin() + pos, '\n') + 1;//+1 to count from 1
111-
112-
_ins << "#line "<<std::to_string(ln)<<"\n";
113-
_glsl.insert(pos,_ins.str());
114-
}
115-
11673
protected:
11774
E_SHADER_STAGE m_shaderStage;
11875
std::string m_filepathHint;

include/nbl/asset/asset.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@
3939
#include "nbl/asset/filters/CSummedAreaTableImageFilter.h"
4040

4141
// shaders
42-
#include "nbl/asset/ISPIR_VProgram.h"
4342
#include "nbl/asset/ICPUShader.h"
4443
#include "nbl/asset/ICPUSpecializedShader.h"
4544
#include "nbl/asset/utils/ShaderRes.h"
46-
#include "nbl/asset/utils/IIncluder.h"
47-
#include "nbl/asset/utils/IIncludeHandler.h"
48-
#include "nbl/asset/utils/IBuiltinIncludeLoader.h"
49-
#include "nbl/asset/utils/IGLSLCompiler.h"
50-
#include "nbl/asset/utils/CShaderIntrospector.h"
45+
#include "nbl/asset/utils/IShaderCompiler.h"
46+
#include "nbl/asset/utils/CGLSLCompiler.h"
47+
#include "nbl/asset/utils/CSPIRVIntrospector.h"
5148

5249
// pipelines
5350

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2018-2022 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef _NBL_ASSET_C_COMPILER_SET_H_INCLUDED_
6+
#define _NBL_ASSET_C_COMPILER_SET_H_INCLUDED_
7+
8+
#include "nbl/core/declarations.h"
9+
#include "CGLSLCompiler.h"
10+
#include "CHLSLCompiler.h"
11+
12+
namespace nbl::asset
13+
{
14+
class NBL_API2 CCompilerSet : public core::IReferenceCounted
15+
{
16+
public:
17+
CCompilerSet(core::smart_refctd_ptr<system::ISystem>&& sys)
18+
: m_HLSLCompiler(core::make_smart_refctd_ptr<CHLSLCompiler>(core::smart_refctd_ptr(sys)))
19+
, m_GLSLCompiler(core::make_smart_refctd_ptr<CGLSLCompiler>(core::smart_refctd_ptr(sys)))
20+
{}
21+
22+
core::smart_refctd_ptr<ICPUShader> compileToSPIRV(const asset::ICPUShader* shader, const IShaderCompiler::SCompilerOptions& options) const;
23+
24+
core::smart_refctd_ptr<ICPUShader> preprocessShader(const asset::ICPUShader* shader, const IShaderCompiler::SPreprocessorOptions& preprocessOptions) const;
25+
26+
inline core::smart_refctd_ptr<IShaderCompiler> getShaderCompiler(IShader::E_CONTENT_TYPE contentType) const
27+
{
28+
if (contentType == IShader::E_CONTENT_TYPE::ECT_HLSL)
29+
return m_HLSLCompiler;
30+
else if (contentType == IShader::E_CONTENT_TYPE::ECT_GLSL)
31+
return m_GLSLCompiler;
32+
else
33+
return nullptr;
34+
}
35+
36+
protected:
37+
core::smart_refctd_ptr<CHLSLCompiler> m_HLSLCompiler = nullptr;
38+
core::smart_refctd_ptr<CGLSLCompiler> m_GLSLCompiler = nullptr;
39+
};
40+
}
41+
42+
#endif

0 commit comments

Comments
 (0)