11#include " shadercompiler.h"
22
33#include " baserendercontext.h"
4- #include < vector>
54#include " spdlog/spdlog.h"
65
6+ #include < vector>
7+
78using namespace slang ;
89
910ShaderCompiler::ShaderCompiler ()
1011{
1112 createGlobalSession ( m_globalSession.writeRef () );
1213}
1314
14- ShaderCompiler::~ShaderCompiler () { }
15+ ShaderCompiler::~ShaderCompiler ()
16+ {
17+ }
18+
19+ const char * KindToString ( slang::TypeReflection::Kind kind )
20+ {
21+ switch ( kind )
22+ {
23+ case slang::TypeReflection::Kind::None:
24+ return " None" ;
25+ case slang::TypeReflection::Kind::Struct:
26+ return " Struct" ;
27+ case slang::TypeReflection::Kind::Array:
28+ return " Array" ;
29+ case slang::TypeReflection::Kind::Matrix:
30+ return " Matrix" ;
31+ case slang::TypeReflection::Kind::Vector:
32+ return " Vector" ;
33+ case slang::TypeReflection::Kind::Scalar:
34+ return " Scalar" ;
35+ case slang::TypeReflection::Kind::ConstantBuffer:
36+ return " ConstantBuffer" ;
37+ case slang::TypeReflection::Kind::Resource:
38+ return " Resource" ;
39+ case slang::TypeReflection::Kind::SamplerState:
40+ return " SamplerState" ;
41+ case slang::TypeReflection::Kind::TextureBuffer:
42+ return " TextureBuffer" ;
43+ case slang::TypeReflection::Kind::ShaderStorageBuffer:
44+ return " ShaderStorageBuffer" ;
45+ case slang::TypeReflection::Kind::ParameterBlock:
46+ return " ParameterBlock" ;
47+ case slang::TypeReflection::Kind::GenericTypeParameter:
48+ return " GenericTypeParameter" ;
49+ case slang::TypeReflection::Kind::Interface:
50+ return " Interface" ;
51+ case slang::TypeReflection::Kind::OutputStream:
52+ return " OutputStream" ;
53+ case slang::TypeReflection::Kind::Specialized:
54+ return " Specialized" ;
55+ case slang::TypeReflection::Kind::Feedback:
56+ return " Feedback" ;
57+ case slang::TypeReflection::Kind::Pointer:
58+ return " Pointer" ;
59+ case slang::TypeReflection::Kind::DynamicResource:
60+ return " DynamicResource" ;
61+ default :
62+ return " Unknown" ;
63+ }
64+ }
65+
66+ const char * BindingTypeToString ( slang::BindingType type )
67+ {
68+ switch ( type )
69+ {
70+ case slang::BindingType::Unknown:
71+ return " Unknown" ;
72+ case slang::BindingType::Sampler:
73+ return " Sampler" ;
74+ case slang::BindingType::Texture:
75+ return " Texture" ;
76+ case slang::BindingType::ConstantBuffer:
77+ return " ConstantBuffer" ;
78+ case slang::BindingType::ParameterBlock:
79+ return " ParameterBlock" ;
80+ case slang::BindingType::TypedBuffer:
81+ return " TypedBuffer" ;
82+ case slang::BindingType::RawBuffer:
83+ return " RawBuffer" ;
84+ case slang::BindingType::CombinedTextureSampler:
85+ return " CombinedTextureSampler" ;
86+ case slang::BindingType::InputRenderTarget:
87+ return " InputRenderTarget" ;
88+ case slang::BindingType::InlineUniformData:
89+ return " InlineUniformData" ;
90+ case slang::BindingType::RayTracingAccelerationStructure:
91+ return " RayTracingAccelerationStructure" ;
92+ case slang::BindingType::VaryingInput:
93+ return " VaryingInput" ;
94+ case slang::BindingType::VaryingOutput:
95+ return " VaryingOutput" ;
96+ case slang::BindingType::ExistentialValue:
97+ return " ExistentialValue" ;
98+ case slang::BindingType::PushConstant:
99+ return " PushConstant" ;
100+ case slang::BindingType::MutableFlag:
101+ return " MutableFlag" ;
102+ case slang::BindingType::MutableTexture:
103+ return " MutableTexture" ;
104+ case slang::BindingType::MutableTypedBuffer:
105+ return " MutableTypedBuffer" ;
106+ case slang::BindingType::MutableRawBuffer:
107+ return " MutableRawBuffer" ;
108+ case slang::BindingType::BaseMask:
109+ return " BaseMask" ;
110+ case slang::BindingType::ExtMask:
111+ return " ExtMask" ;
112+ default :
113+ return " Unknown" ;
114+ }
115+ }
15116
16- bool ShaderCompiler::Compile ( const ShaderType shaderType, const char * pShader, std::vector< uint32_t >& outSpirv )
117+ bool ShaderCompiler::Compile ( const ShaderType shaderType, const char * pShader, ShaderCompilerResult& outResult )
17118{
119+ outResult = ShaderCompilerResult ();
120+
18121 Slang::ComPtr<ISession> session;
19122
20123 TargetDesc targetDesc{};
@@ -34,8 +137,7 @@ bool ShaderCompiler::Compile( const ShaderType shaderType, const char* pShader,
34137 m_globalSession->createSession ( sessionDesc, session.writeRef () );
35138
36139 Slang::ComPtr<IBlob> diagnostics;
37- IModule* module =
38- session->loadModuleFromSourceString ( " Shader" , " Shader.slang" , pShader, diagnostics.writeRef () );
140+ IModule* module = session->loadModuleFromSourceString ( " Shader" , " Shader.slang" , pShader, diagnostics.writeRef () );
39141
40142 if ( diagnostics )
41143 spdlog::error ( " Shader compiler: {}" , ( const char * )diagnostics->getBufferPointer () );
@@ -68,6 +170,58 @@ bool ShaderCompiler::Compile( const ShaderType shaderType, const char* pShader,
68170
69171 const uint32_t * data = static_cast <const uint32_t *>( kernelBlob->getBufferPointer () );
70172 size_t wordCount = kernelBlob->getBufferSize () / sizeof ( uint32_t );
71- outSpirv = std::vector<uint32_t >( data, data + wordCount );
173+
174+ outResult.ShaderData = UtilArray::FromVector ( std::vector<uint32_t >( data, data + wordCount ) );
175+
176+ std::vector<ShaderReflectionBinding> reflectionBindings = {};
177+ ShaderReflectionInfo shaderReflectionInfo = {};
178+
179+ {
180+ slang::ProgramLayout* layout = program->getLayout ( targetIndex );
181+ auto globalScope = layout->getGlobalParamsVarLayout ();
182+ auto globalTypeLayout = globalScope->getTypeLayout ();
183+
184+ int paramCount = globalTypeLayout->getFieldCount ();
185+ for ( int i = 0 ; i < paramCount; i++ )
186+ {
187+ auto param = globalTypeLayout->getFieldByIndex ( i );
188+ auto type = globalTypeLayout->getBindingRangeType ( i );
189+
190+ // get binding info
191+ size_t binding = param->getOffset ( SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT );
192+ size_t set = param->getBindingSpace ( SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT );
193+
194+ SlangResourceShape shape = param->getType ()->getResourceShape ();
195+
196+ // get param name/type
197+ const char * name = param->getName ();
198+ slang::TypeReflection::Kind kind = param->getType ()->getKind ();
199+
200+ spdlog::info ( " [{}, {}] {} {}, {}" , binding, set, KindToString ( kind ), name, BindingTypeToString ( type ) );
201+
202+ auto mochaReflectionType = SHADER_REFLECTION_TYPE_UNKNOWN;
203+ switch ( type )
204+ {
205+ case slang::BindingType::Unknown:
206+ mochaReflectionType = SHADER_REFLECTION_TYPE_UNKNOWN;
207+ break ;
208+ case slang::BindingType::Texture:
209+ mochaReflectionType = SHADER_REFLECTION_TYPE_TEXTURE;
210+ break ;
211+ case slang::BindingType::Sampler:
212+ mochaReflectionType = SHADER_REFLECTION_TYPE_SAMPLER;
213+ break ;
214+ case slang::BindingType::ConstantBuffer:
215+ mochaReflectionType = SHADER_REFLECTION_TYPE_BUFFER;
216+ break ;
217+ }
218+
219+ reflectionBindings.push_back ( ShaderReflectionBinding{
220+ .Set = ( int )set, .Binding = ( int )binding, .Type = mochaReflectionType, .Name = name } );
221+ }
222+ }
223+
224+ outResult.ReflectionData = ShaderReflectionInfo { .Bindings = UtilArray::FromVector ( reflectionBindings ) };
225+
72226 return true ;
73227}
0 commit comments