@@ -69,7 +69,7 @@ std::shared_ptr<ShaderCode> glsl_to_spv(const ShaderCode *glsl_code) {
6969 return {};
7070 }
7171
72- // Link the shader
72+ // Link the shader.
7373 glslang::TProgram program;
7474 program.addShader (&shader);
7575
@@ -159,11 +159,14 @@ std::shared_ptr<ShaderCode> spv_to_glsl(const ShaderCode *spv_code, bool is_es,
159159 auto resource = glsl.get_shader_resources ();
160160
161161 // Update glsl resource binding map for GLES 3.0.
162+
163+ // Update texture binding map.
162164 for (const auto &r : resource.sampled_images ) {
163165 uint32_t binding = glsl.get_decoration (r.id , spv::DecorationBinding);
164166 glsl_code->texture_binding_map .emplace_back (binding, r.name );
165167 }
166168
169+ // Update uniform binding map.
167170 std::vector<UniformBufferInfo> ubo_infos_;
168171
169172 for (const auto &r : resource.uniform_buffers ) {
@@ -173,7 +176,7 @@ std::shared_ptr<ShaderCode> spv_to_glsl(const ShaderCode *spv_code, bool is_es,
173176 // Update uniform buffer info.
174177 ubo_infos_.emplace_back ();
175178 auto &ubo_info = ubo_infos_.back ();
176- auto ub_type = glsl.get_type (r.type_id );
179+ const auto & ub_type = glsl.get_type (r.type_id );
177180 ubo_info.name = r.name ;
178181 ubo_info.binding_point = binding;
179182 ubo_info.size = std::ceil (glsl.get_declared_struct_size (ub_type) / 16 .0f ) * 16 ;
@@ -189,26 +192,26 @@ std::shared_ptr<ShaderCode> spv_to_glsl(const ShaderCode *spv_code, bool is_es,
189192 return glsl_code;
190193}
191194
192- std::shared_ptr<ShaderCode> spv2msl (const ShaderCode *spv_code, bool need_framebuffer_fetch) {
195+ std::shared_ptr<ShaderCode> spv_to_msl (const ShaderCode *spv_code, bool need_framebuffer_fetch) {
193196 spirv_cross::CompilerMSL msl (reinterpret_cast <const uint32_t *>(spv_code->code .data ()), spv_code->code .size () / 4 );
194197 spirv_cross::ShaderResources resources = msl.get_shader_resources ();
195198
196199 // Metal 专有的绑定偏移逻辑
197200 // 预留前 8 个索引给 Vertex Buffers ([[buffer(0)]] 到 [[buffer(7)]])
198201 const unsigned METAL_BUFFER_OFFSET = 8 ;
199202
200- for (auto &resource : resources.uniform_buffers ) {
201- unsigned set = msl.get_decoration (resource.id , spv::DecorationDescriptorSet);
202- unsigned binding = msl.get_decoration (resource.id , spv::DecorationBinding);
203+ for (const auto &resource : resources.uniform_buffers ) {
204+ const unsigned set = msl.get_decoration (resource.id , spv::DecorationDescriptorSet);
205+ const unsigned binding = msl.get_decoration (resource.id , spv::DecorationBinding);
203206
204207 msl.unset_decoration (resource.id , spv::DecorationDescriptorSet);
205208 // MSL 中 UBO 和 Vertex Buffer 共享 buffer 索引,所以必须偏移
206209 msl.set_decoration (resource.id , spv::DecorationBinding, set * 16 + binding + METAL_BUFFER_OFFSET );
207210 }
208211
209- for (auto &resource : resources.sampled_images ) {
210- unsigned set = msl.get_decoration (resource.id , spv::DecorationDescriptorSet);
211- unsigned binding = msl.get_decoration (resource.id , spv::DecorationBinding);
212+ for (const auto &resource : resources.sampled_images ) {
213+ const unsigned set = msl.get_decoration (resource.id , spv::DecorationDescriptorSet);
214+ const unsigned binding = msl.get_decoration (resource.id , spv::DecorationBinding);
212215
213216 msl.unset_decoration (resource.id , spv::DecorationDescriptorSet);
214217 // 采样器通常使用 [[texture(N)]] 和 [[sampler(N)]],与 buffer 空间分开,
@@ -227,13 +230,13 @@ std::shared_ptr<ShaderCode> spv2msl(const ShaderCode *spv_code, bool need_frameb
227230
228231 auto msl_code = std::make_shared<ShaderCode>();
229232 msl_code->stage = spv_code->stage ;
230- msl_code->entry_point = msl_code ->entry_point ;
233+ msl_code->entry_point = spv_code ->entry_point ;
231234 msl_code->code = msl.compile ();
232235
233236 return msl_code;
234237}
235238
236- void ShaderTranslator::set_shader (const ShaderCodeKey &shader_key, const std::shared_ptr<ShaderCode> &code) {
239+ void ShaderTranslator::set_shader_code (const ShaderCodeKey &shader_key, const std::shared_ptr<ShaderCode> &code) {
237240 shader_->update_shader_code (shader_key, code);
238241}
239242
@@ -248,11 +251,15 @@ void ShaderTranslator::compile_from_glsl(const std::string &entry_point,
248251 glsl_code->entry_point = entry_point;
249252 glsl_code->code = shader_code;
250253
251- set_shader ({ShaderSourceType::GLSL , 4 , 5 }, glsl_code);
254+ set_shader_code ({ShaderSourceType::GLSL , 4 , 5 }, glsl_code);
252255
253256 prepare (need_framebuffer_fetch);
254257}
255258
259+ std::shared_ptr<Shader> ShaderTranslator::get_shader () const {
260+ return shader_;
261+ }
262+
256263bool ShaderTranslator::prepare (bool need_framebuffer_fetch) {
257264 auto spv_code_ptr = shader_->get_shader_code ({ShaderSourceType::SPIRV , 1 , 1 });
258265 auto glsl_code_ptr = shader_->get_shader_code ({ShaderSourceType::GLSL , 4 , 5 });
@@ -287,7 +294,7 @@ bool ShaderTranslator::prepare(bool need_framebuffer_fetch) {
287294
288295 // Generate msl code.
289296 if (msl_code_ptr == nullptr ) {
290- auto msl_code = spv2msl (spv_code_ptr.get (), need_framebuffer_fetch);
297+ auto msl_code = spv_to_msl (spv_code_ptr.get (), need_framebuffer_fetch);
291298 shader_->update_shader_code ({ShaderSourceType::MSL , 1 , 2 }, msl_code);
292299 }
293300
0 commit comments