Skip to content

Commit e7a0900

Browse files
committed
Some fixes in spirv-optimizer PR to be merge-able
1 parent 77a7366 commit e7a0900

File tree

11 files changed

+94
-63
lines changed

11 files changed

+94
-63
lines changed

include/IDriver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace video
2222
}
2323
}
2424

25+
#include "nbl/asset/ISPIRVOptimizer.h"
2526
#include "nbl/video/IGPUPipelineCache.h"
2627
#include "nbl/video/IGPUImageView.h"
2728
#include "IFrameBuffer.h"
@@ -280,7 +281,7 @@ class IDriver : public virtual core::IReferenceCounted, public IVideoCapabilityR
280281
virtual core::smart_refctd_ptr<IGPUShader> createGPUShader(core::smart_refctd_ptr<const asset::ICPUShader>&& _cpushader) { return nullptr; }
281282

282283
//! Specialize the plain shader (@see ICPUSpecializedShader)
283-
virtual core::smart_refctd_ptr<IGPUSpecializedShader> createGPUSpecializedShader(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo) { return nullptr; }
284+
virtual core::smart_refctd_ptr<IGPUSpecializedShader> createGPUSpecializedShader(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo, const asset::ISPIRVOptimizer* _spvopt = nullptr) { return nullptr; }
284285

285286
//! Create a descriptor set layout (@see ICPUDescriptorSetLayout)
286287
virtual core::smart_refctd_ptr<IGPUDescriptorSetLayout> createGPUDescriptorSetLayout(const IGPUDescriptorSetLayout::SBinding* _begin, const IGPUDescriptorSetLayout::SBinding* _end) { return nullptr; }

include/nbl/asset/IAssetManager.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "IReadFile.h"
1616
#include "IWriteFile.h"
1717

18-
#include "nbl/asset/ISPIRVOptimizer.h"
1918
#include "nbl/core/Types.h"
2019
#include "nbl/asset/IGLSLCompiler.h"
2120

@@ -127,7 +126,6 @@ class IAssetManager : public core::IReferenceCounted, public core::QuitSignallin
127126
core::smart_refctd_ptr<IGeometryCreator> m_geometryCreator;
128127
core::smart_refctd_ptr<IMeshManipulator> m_meshManipulator;
129128
core::smart_refctd_ptr<IGLSLCompiler> m_glslCompiler;
130-
core::smart_refctd_ptr<ISPIRVOptimizer> m_spirvOptimizer;
131129
// called as a part of constructor only
132130
void initializeMeshTools();
133131

@@ -153,7 +151,6 @@ class IAssetManager : public core::IReferenceCounted, public core::QuitSignallin
153151
const IGeometryCreator* getGeometryCreator() const;
154152
IMeshManipulator* getMeshManipulator();
155153
IGLSLCompiler* getGLSLCompiler() const { return m_glslCompiler.get(); }
156-
ISPIRVOptimizer* getSPIRVOptimizer() const { return m_spirvOptimizer.get(); }
157154

158155
protected:
159156
virtual ~IAssetManager()

include/nbl/asset/ISPIRVOptimizer.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,38 @@ namespace asset
1313
class ISPIRVOptimizer final : public core::IReferenceCounted
1414
{
1515
public:
16+
enum E_OPTIMIZER_PASS
17+
{
18+
EOP_MERGE_RETURN,
19+
EOP_INLINE,
20+
EOP_ELIM_DEAD_FUNCTIONS,
21+
EOP_SCALAR_REPLACEMENT,
22+
EOP_LOCAL_SINGLE_BLOCK_LOAD_STORE_ELIM,
23+
EOP_LOCAL_SINGLE_STORE_ELIM,
24+
EOP_SIMPLIFICATION,
25+
EOP_VECTOR_DCE,
26+
EOP_DEAD_INSERT_ELIM,
27+
EOP_AGGRESSIVE_DCE,
28+
EOP_DEAD_BRANCH_ELIM,
29+
EOP_BLOCK_MERGE,
30+
EOP_LOCAL_MULTI_STORE_ELIM,
31+
EOP_REDUNDANCY_ELIM,
32+
EOP_LOOP_INVARIANT_CODE_MOTION,
33+
EOP_CCP,
34+
EOP_REDUCE_LOAD_SIZE,
35+
EOP_STRENGTH_REDUCTION,
36+
EOP_IF_CONVERSION,
37+
38+
EOP_COUNT
39+
};
40+
41+
ISPIRVOptimizer(std::initializer_list<E_OPTIMIZER_PASS> _passes) : m_passes(std::move(_passes)) {}
42+
1643
core::smart_refctd_ptr<ICPUBuffer> optimize(const uint32_t* _spirv, uint32_t _dwordCount) const;
1744
core::smart_refctd_ptr<ICPUBuffer> optimize(const ICPUBuffer* _spirv) const;
45+
46+
protected:
47+
const std::initializer_list<E_OPTIMIZER_PASS> m_passes;
1848
};
1949

2050
}

include/nbl/asset/IShader.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ class IShader : public virtual core::IReferenceCounted
5353
std::string insertion = "\n";
5454
for (const std::string& ext : (*_exts))
5555
{
56-
std::string str = "#ifndef " + ext + "\n";
57-
str += "\t#define NBL_" + ext + "\n";
58-
str += "#endif //" + ext + "\n";
56+
std::string str;
57+
//str += "#ifndef " + ext + "\n";
58+
str += "\t#define NBL_IMPL_" + ext + "\n";
59+
//str += "#endif //" + ext + "\n";
5960

6061
insertion += str;
6162
}
63+
64+
return insertion;
6265
}
6366
static inline void insertAfterVersionAndPragmaShaderStage(std::string& _glsl, const std::string& _ins)
6467
{

source/Nabla/CIrrDeviceLinux.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ namespace nbl
5959
namespace video
6060
{
6161
IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
62-
io::IFileSystem* io, CIrrDeviceLinux* device, const asset::IGLSLCompiler* glslcomp,
63-
const asset::ISPIRVOptimizer* spvopt
62+
io::IFileSystem* io, CIrrDeviceLinux* device, const asset::IGLSLCompiler* glslcomp
6463
#ifdef _NBL_COMPILE_WITH_OPENGL_
6564
,COpenGLDriver::SAuxContext* auxCtxts
6665
#endif // _NBL_COMPILE_WITH_OPENGL_

source/Nabla/CIrrDeviceWin32.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace nbl
4040
{
4141
#ifdef _NBL_COMPILE_WITH_OPENGL_
4242
IVideoDriver* createOpenGLDriver(const nbl::SIrrlichtCreationParameters& params,
43-
io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp, const asset::ISPIRVOptimizer* spvoptimizer);
43+
io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp);
4444
#endif
4545
}
4646
} // end namespace nbl
@@ -1099,7 +1099,7 @@ void CIrrDeviceWin32::createDriver()
10991099
#ifdef _NBL_COMPILE_WITH_OPENGL_
11001100
switchToFullScreen();
11011101

1102-
VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem.get(), this, getAssetManager()->getGLSLCompiler(), getAssetManager()->getSPIRVOptimizer());
1102+
VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem.get(), this, getAssetManager()->getGLSLCompiler());
11031103
if (!VideoDriver)
11041104
{
11051105
os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);

source/Nabla/COpenGLDriver.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ namespace video
182182
#ifdef _NBL_COMPILE_WITH_WINDOWS_DEVICE_
183183
//! Windows constructor and init code
184184
COpenGLDriver::COpenGLDriver(const SIrrlichtCreationParameters& params,
185-
io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp, const asset::ISPIRVOptimizer* spvoptimizer)
185+
io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp)
186186
: CNullDriver(device, io, params), COpenGLExtensionHandler(),
187187
runningInRenderDoc(false), ColorFormat(asset::EF_R8G8B8_UNORM),
188188
HDc(0), Window(static_cast<HWND>(params.WindowId)), Win32Device(device),
189-
AuxContexts(0), GLSLCompiler(glslcomp), SPIRVOptimizer(spvoptimizer), DeviceType(EIDT_WIN32)
189+
AuxContexts(0), GLSLCompiler(glslcomp), DeviceType(EIDT_WIN32)
190190
{
191191
#ifdef _NBL_DEBUG
192192
setDebugName("COpenGLDriver");
@@ -1252,7 +1252,7 @@ core::smart_refctd_ptr<IGPUShader> COpenGLDriver::createGPUShader(core::smart_re
12521252
return core::make_smart_refctd_ptr<COpenGLShader>(std::move(clone));
12531253
}
12541254

1255-
core::smart_refctd_ptr<IGPUSpecializedShader> COpenGLDriver::createGPUSpecializedShader(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo)
1255+
core::smart_refctd_ptr<IGPUSpecializedShader> COpenGLDriver::createGPUSpecializedShader(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo, const asset::ISPIRVOptimizer* _spvopt)
12561256
{
12571257
const COpenGLShader* glUnspec = static_cast<const COpenGLShader*>(_unspecialized);
12581258

@@ -1286,10 +1286,10 @@ core::smart_refctd_ptr<IGPUSpecializedShader> COpenGLDriver::createGPUSpecialize
12861286
{
12871287
spirv = glUnspec->m_code;
12881288
}
1289-
#ifndef _IRR_DEBUG
1290-
if (SPIRVOptimizer)
1291-
spirv = SPIRVOptimizer->optimize(spirv.get());
1292-
#endif
1289+
1290+
if (_spvopt)
1291+
spirv = _spvopt->optimize(spirv.get());
1292+
12931293
if (!spirv)
12941294
return nullptr;
12951295

@@ -3212,10 +3212,10 @@ namespace video
32123212
// -----------------------------------
32133213
#ifdef _NBL_COMPILE_WITH_WINDOWS_DEVICE_
32143214
IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
3215-
io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp, const asset::ISPIRVOptimizer* spvoptimizer)
3215+
io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp)
32163216
{
32173217
#ifdef _NBL_COMPILE_WITH_OPENGL_
3218-
COpenGLDriver* ogl = new COpenGLDriver(params, io, device, glslcomp, spvoptimizer);
3218+
COpenGLDriver* ogl = new COpenGLDriver(params, io, device, glslcomp);
32193219

32203220
if (!ogl->initDriver(device))
32213221
{
@@ -3234,15 +3234,14 @@ IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
32343234
// -----------------------------------
32353235
#ifdef _NBL_COMPILE_WITH_X11_DEVICE_
32363236
IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
3237-
io::IFileSystem* io, CIrrDeviceLinux* device, const asset::IGLSLCompiler* glslcomp,
3238-
const asset::ISPIRVOptimizer* spvopt
3237+
io::IFileSystem* io, CIrrDeviceLinux* device, const asset::IGLSLCompiler* glslcomp
32393238
#ifdef _IRR_COMPILE_WITH_OPENGL_
32403239
, COpenGLDriver::SAuxContext* auxCtxts
32413240
#endif // _NBL_COMPILE_WITH_OPENGL_
32423241
)
32433242
{
32443243
#ifdef _NBL_COMPILE_WITH_OPENGL_
3245-
COpenGLDriver* ogl = new COpenGLDriver(params, io, device, glslcomp, spvopt);
3244+
COpenGLDriver* ogl = new COpenGLDriver(params, io, device, glslcomp);
32463245
if (!ogl->initDriver(device,auxCtxts))
32473246
{
32483247
ogl->drop();

source/Nabla/COpenGLDriver.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class COpenGLDriver final : public CNullDriver, public COpenGLExtensionHandler
220220
struct SAuxContext;
221221

222222
#ifdef _NBL_COMPILE_WITH_WINDOWS_DEVICE_
223-
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp, const asset::ISPIRVOptimizer* spvoptimizer);
223+
COpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, CIrrDeviceWin32* device, const asset::IGLSLCompiler* glslcomp);
224224
//! inits the windows specific parts of the open gl driver
225225
bool initDriver(CIrrDeviceWin32* device);
226226
bool changeRenderContext(const SExposedVideoData& videoData, CIrrDeviceWin32* device);
@@ -654,7 +654,7 @@ class COpenGLDriver final : public CNullDriver, public COpenGLExtensionHandler
654654
core::smart_refctd_ptr<IGPUImageView> createGPUImageView(IGPUImageView::SCreationParams&& params) override;
655655

656656
core::smart_refctd_ptr<IGPUShader> createGPUShader(core::smart_refctd_ptr<const asset::ICPUShader>&& _cpushader) override;
657-
core::smart_refctd_ptr<IGPUSpecializedShader> createGPUSpecializedShader(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo) override;
657+
core::smart_refctd_ptr<IGPUSpecializedShader> createGPUSpecializedShader(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo, const asset::ISPIRVOptimizer* _spvopt) override;
658658

659659
core::smart_refctd_ptr<IGPUDescriptorSetLayout> createGPUDescriptorSetLayout(const IGPUDescriptorSetLayout::SBinding* _begin, const IGPUDescriptorSetLayout::SBinding* _end) override;
660660

@@ -1083,7 +1083,6 @@ class COpenGLDriver final : public CNullDriver, public COpenGLExtensionHandler
10831083
std::mutex glContextMutex;
10841084
SAuxContext* AuxContexts;
10851085
core::smart_refctd_ptr<const asset::IGLSLCompiler> GLSLCompiler;
1086-
core::smart_refctd_ptr<const asset::ISPIRVOptimizer> SPIRVOptimizer;
10871086

10881087
E_DEVICE_TYPE DeviceType;
10891088
};

src/nbl/asset/IAssetManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ void IAssetManager::initializeMeshTools()
113113
m_meshManipulator = core::make_smart_refctd_ptr<CMeshManipulator>();
114114
m_geometryCreator = core::make_smart_refctd_ptr<CGeometryCreator>(m_meshManipulator.get());
115115
m_glslCompiler = core::make_smart_refctd_ptr<IGLSLCompiler>(m_fileSystem.get());
116-
m_spirvOptimizer = core::make_smart_refctd_ptr<ISPIRVOptimizer>();
117116
}
118117

119118
const IGeometryCreator* IAssetManager::getGeometryCreator() const

src/nbl/asset/ISPIRVOptimizer.cpp

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,34 @@ static constexpr spv_target_env SPIRV_VERSION = spv_target_env::SPV_ENV_UNIVERSA
1313

1414
nbl::core::smart_refctd_ptr<ICPUBuffer> ISPIRVOptimizer::optimize(const uint32_t* _spirv, uint32_t _dwordCount) const
1515
{
16-
spvtools::Optimizer opt(SPIRV_VERSION);
16+
//https://www.lunarg.com/wp-content/uploads/2020/05/SPIR-V-Shader-Legalization-and-Size-Reduction-Using-spirv-opt_v1.2.pdf
17+
18+
auto CreateScalarReplacementPass = [] {
19+
return spvtools::CreateScalarReplacementPass();
20+
};
1721

18-
// lots of these taken from https://www.lunarg.com/wp-content/uploads/2020/05/SPIR-V-Shader-Legalization-and-Size-Reduction-Using-spirv-opt_v1.2.pdf
19-
opt .RegisterPass(spvtools::CreateMergeReturnPass())
20-
.RegisterPass(spvtools::CreateInlineExhaustivePass())
21-
.RegisterPass(spvtools::CreateEliminateDeadFunctionsPass())
22-
.RegisterPass(spvtools::CreateScalarReplacementPass())
23-
.RegisterPass(spvtools::CreateLocalSingleBlockLoadStoreElimPass())
24-
.RegisterPass(spvtools::CreateLocalSingleStoreElimPass())
25-
.RegisterPass(spvtools::CreateSimplificationPass())
26-
.RegisterPass(spvtools::CreateVectorDCEPass())
27-
.RegisterPass(spvtools::CreateDeadInsertElimPass())
28-
.RegisterPass(spvtools::CreateAggressiveDCEPass())
29-
.RegisterPass(spvtools::CreateDeadBranchElimPass())
30-
.RegisterPass(spvtools::CreateBlockMergePass())
31-
.RegisterPass(spvtools::CreateLocalSingleBlockLoadStoreElimPass())
32-
.RegisterPass(spvtools::CreateLocalSingleStoreElimPass())
33-
.RegisterPass(spvtools::CreateLocalMultiStoreElimPass())
34-
.RegisterPass(spvtools::CreateSimplificationPass())
35-
.RegisterPass(spvtools::CreateVectorDCEPass())
36-
.RegisterPass(spvtools::CreateDeadInsertElimPass())
37-
.RegisterPass(spvtools::CreateRedundancyEliminationPass())
38-
.RegisterPass(spvtools::CreateAggressiveDCEPass())
39-
//.RegisterPass(spvtools::CreateLoopInvariantCodeMotionPass()) //this completely breaks material compiler
40-
.RegisterPass(spvtools::CreateCCPPass())
41-
.RegisterPass(spvtools::CreateReduceLoadSizePass())
42-
.RegisterPass(spvtools::CreateStrengthReductionPass())
43-
.RegisterPass(spvtools::CreateIfConversionPass())
44-
;
22+
using create_pass_f_t = spvtools::Optimizer::PassToken(*)();
23+
create_pass_f_t create_pass_f[EOP_COUNT]{
24+
&spvtools::CreateMergeReturnPass,
25+
&spvtools::CreateInlineExhaustivePass,
26+
&spvtools::CreateEliminateDeadFunctionsPass,
27+
CreateScalarReplacementPass,
28+
&spvtools::CreateLocalSingleBlockLoadStoreElimPass,
29+
&spvtools::CreateLocalSingleStoreElimPass,
30+
&spvtools::CreateSimplificationPass,
31+
&spvtools::CreateVectorDCEPass,
32+
&spvtools::CreateDeadInsertElimPass,
33+
&spvtools::CreateAggressiveDCEPass,
34+
&spvtools::CreateDeadBranchElimPass,
35+
&spvtools::CreateBlockMergePass,
36+
&spvtools::CreateLocalMultiStoreElimPass,
37+
&spvtools::CreateRedundancyEliminationPass,
38+
&spvtools::CreateLoopInvariantCodeMotionPass,
39+
&spvtools::CreateCCPPass,
40+
&spvtools::CreateReduceLoadSizePass,
41+
&spvtools::CreateStrengthReductionPass,
42+
&spvtools::CreateIfConversionPass
43+
};
4544

4645
auto msgConsumer = [](spv_message_level_t level, const char* src, const spv_position_t& pos, const char* msg)
4746
{
@@ -61,6 +60,11 @@ nbl::core::smart_refctd_ptr<ICPUBuffer> ISPIRVOptimizer::optimize(const uint32_t
6160
os::Printer::log(location, msg, lvl);
6261
};
6362

63+
spvtools::Optimizer opt(SPIRV_VERSION);
64+
65+
for (E_OPTIMIZER_PASS pass : m_passes)
66+
opt.RegisterPass(create_pass_f[pass]());
67+
6468
opt.SetMessageConsumer(msgConsumer);
6569

6670
std::vector<uint32_t> optimized;

0 commit comments

Comments
 (0)