@@ -156,33 +156,15 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
156156 }
157157
158158 // Compute shader workgroup (threadgroup) size. A value of 0 in any dimension
159- // means that dimension is sized by a specialization constant and is resolved
160- // by the backend at pipeline creation (for example, to the device maximum).
161- // Only meaningful for compute shaders.
159+ // means that dimension is sized by a specialization constant, which the
160+ // runtime does not support: pipeline creation rejects it. Only meaningful for
161+ // compute shaders.
162162 {
163- uint32_t workgroup_size_x = 0u ;
164- uint32_t workgroup_size_y = 0u ;
165- uint32_t workgroup_size_z = 0u ;
166- if (execution_model == spv::ExecutionModel::ExecutionModelGLCompute) {
167- spirv_cross::SpecializationConstant spec_x, spec_y, spec_z;
168- compiler_->get_work_group_size_specialization_constants (spec_x, spec_y,
169- spec_z);
170- const auto local_size = [&](spirv_cross::SpecializationConstant& spec,
171- uint32_t index) -> uint32_t {
172- // A non-zero id means this dimension is driven by a specialization
173- // constant; leave it as the runtime-resolved sentinel of 0.
174- return spec.id != 0
175- ? 0u
176- : compiler_->get_execution_mode_argument (
177- spv::ExecutionMode::ExecutionModeLocalSize, index);
178- };
179- workgroup_size_x = local_size (spec_x, 0u );
180- workgroup_size_y = local_size (spec_y, 1u );
181- workgroup_size_z = local_size (spec_z, 2u );
182- }
183- root[" workgroup_size_x" ] = workgroup_size_x;
184- root[" workgroup_size_y" ] = workgroup_size_y;
185- root[" workgroup_size_z" ] = workgroup_size_z;
163+ const std::array<uint32_t , 3 > workgroup_size =
164+ ReflectWorkgroupSize ().value_or (std::array<uint32_t , 3 >{0u , 0u , 0u });
165+ root[" workgroup_size_x" ] = workgroup_size[0 ];
166+ root[" workgroup_size_y" ] = workgroup_size[1 ];
167+ root[" workgroup_size_z" ] = workgroup_size[2 ];
186168 }
187169
188170 const auto shader_resources = compiler_->get_shader_resources ();
@@ -608,6 +590,48 @@ std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
608590 data->AddUniformTexture (uniform_texture);
609591 }
610592
593+ const auto storage_buffers =
594+ compiler_->get_shader_resources ().storage_buffers ;
595+ for (const auto & buffer : storage_buffers) {
596+ ShaderBundleData::ShaderStorageBuffer storage_buffer;
597+ storage_buffer.name = buffer.name ;
598+ storage_buffer.ext_res_0 = compiler_.GetExtendedMSLResourceBinding (
599+ CompilerBackend::ExtendedResourceIndex::kPrimary , buffer.id );
600+ storage_buffer.set = compiler_->get_decoration (
601+ buffer.id , spv::Decoration::DecorationDescriptorSet);
602+ storage_buffer.binding = compiler_->get_decoration (
603+ buffer.id , spv::Decoration::DecorationBinding);
604+
605+ // `readonly` and `writeonly` may decorate the block variable or every
606+ // member of the block. `get_buffer_block_flags` folds both cases together.
607+ const auto flags = compiler_->get_buffer_block_flags (buffer.id );
608+ const bool non_writable = flags.get (spv::Decoration::DecorationNonWritable);
609+ const bool non_readable = flags.get (spv::Decoration::DecorationNonReadable);
610+ if (non_writable && !non_readable) {
611+ storage_buffer.access = fb::shaderbundle::ShaderResourceAccess::kReadOnly ;
612+ } else if (non_readable && !non_writable) {
613+ storage_buffer.access =
614+ fb::shaderbundle::ShaderResourceAccess::kWriteOnly ;
615+ } else {
616+ storage_buffer.access =
617+ fb::shaderbundle::ShaderResourceAccess::kReadWrite ;
618+ }
619+
620+ const auto & type = compiler_->get_type (buffer.base_type_id );
621+ const size_t empty_size =
622+ compiler_->get_declared_struct_size_runtime_array (type, 0 );
623+ storage_buffer.size_in_bytes = empty_size;
624+ storage_buffer.runtime_array_stride =
625+ compiler_->get_declared_struct_size_runtime_array (type, 1 ) - empty_size;
626+
627+ data->AddStorageBuffer (std::move (storage_buffer));
628+ }
629+
630+ if (auto workgroup_size = ReflectWorkgroupSize ();
631+ workgroup_size.has_value ()) {
632+ data->SetWorkgroupSize (workgroup_size.value ());
633+ }
634+
611635 // We only need to worry about storing vertex attributes.
612636 if (entrypoints.front ().execution_model == spv::ExecutionModelVertex) {
613637 const auto inputs = compiler_->get_shader_resources ().stage_inputs ;
@@ -637,6 +661,29 @@ std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
637661 return data;
638662}
639663
664+ std::optional<std::array<uint32_t , 3 >> Reflector::ReflectWorkgroupSize () const {
665+ const auto & entrypoints = compiler_->get_entry_points_and_stages ();
666+ if (entrypoints.size () != 1u ||
667+ entrypoints.front ().execution_model !=
668+ spv::ExecutionModel::ExecutionModelGLCompute) {
669+ return std::nullopt ;
670+ }
671+ spirv_cross::SpecializationConstant spec_x, spec_y, spec_z;
672+ compiler_->get_work_group_size_specialization_constants (spec_x, spec_y,
673+ spec_z);
674+ const auto local_size = [&](const spirv_cross::SpecializationConstant& spec,
675+ uint32_t index) -> uint32_t {
676+ // A non-zero id means this dimension is driven by a specialization
677+ // constant; report it as 0.
678+ return spec.id != 0
679+ ? 0u
680+ : compiler_->get_execution_mode_argument (
681+ spv::ExecutionMode::ExecutionModeLocalSize, index);
682+ };
683+ return std::array<uint32_t , 3 >{local_size (spec_x, 0u ), local_size (spec_y, 1u ),
684+ local_size (spec_z, 2u )};
685+ }
686+
640687std::optional<uint32_t > Reflector::GetArrayElements (
641688 const spirv_cross::SPIRType& type) const {
642689 if (type.array .empty ()) {
0 commit comments