From dd84b3bef3eb2fcc32cb708210fd658eca2edcad Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Tue, 1 Jul 2025 12:32:44 -0400 Subject: [PATCH] push constants --- .../metal/src/commands/commands/metal_commands.m | 4 ++-- .../metal/src/shader_module/metal_shader_module.h | 1 + .../metal/src/shader_module/metal_shader_module.m | 12 +++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/projects/apis/metal/src/commands/commands/metal_commands.m b/projects/apis/metal/src/commands/commands/metal_commands.m index 556ee1a..3fce495 100644 --- a/projects/apis/metal/src/commands/commands/metal_commands.m +++ b/projects/apis/metal/src/commands/commands/metal_commands.m @@ -120,7 +120,7 @@ void metalBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t if (map.set == set && map.binding == uniform->uniform->bindings[i].binding) { [encoder setVertexBuffer:info.buffer->buffer->buffer offset:info.offset - atIndex:1 + atIndex:map.metalBindingIndex ]; break; } @@ -140,5 +140,5 @@ void metalBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t void metalBindVertexBytes(gnCommandBufferHandle buffer, gnPushConstantLayout layout, void* data) { id encoder = (id)buffer->commandBuffer->encoder; - // [encoder setVertexBytes:data length:layout.size atIndex:0]; // TODO: fix this + [encoder setVertexBytes:data length:layout.size atIndex:buffer->commandBuffer->boundGraphcisPipeline->graphicsPipeline->vertexShaderMaps.pushConstantIndex]; // TODO: fix this } diff --git a/projects/apis/metal/src/shader_module/metal_shader_module.h b/projects/apis/metal/src/shader_module/metal_shader_module.h index 410c3b1..727abf6 100644 --- a/projects/apis/metal/src/shader_module/metal_shader_module.h +++ b/projects/apis/metal/src/shader_module/metal_shader_module.h @@ -12,6 +12,7 @@ GN_ARRAY_LIST(metalBindingMap); typedef struct metalBindingMaps { metalBindingMapArrayList uniformBufferMaps; + uint32_t pushConstantIndex; metalBindingMapArrayList textureMaps; } metalBindingMaps; diff --git a/projects/apis/metal/src/shader_module/metal_shader_module.m b/projects/apis/metal/src/shader_module/metal_shader_module.m index 3ece068..a231a60 100644 --- a/projects/apis/metal/src/shader_module/metal_shader_module.m +++ b/projects/apis/metal/src/shader_module/metal_shader_module.m @@ -83,14 +83,16 @@ gnReturnCode createMetalShaderModule(gnShaderModule module, gnDevice device, gnS spvc_compiler_create_shader_resources(compiler, &resources); - spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_PUSH_CONSTANT, &list, &count); - for (int i = 0; i < count; i++) { - // TODO: get the buffer index - } - module->shaderModule->maps.uniformBufferMaps = loadUniformBufferInformation(resources, compiler, shaderModuleInfo.stage); module->shaderModule->maps.textureMaps = loadTextureBindingInformation(resources, compiler); + spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_PUSH_CONSTANT, &list, &count); + for (int i = 0; i < count; i++) { + spvc_compiler_unset_decoration(compiler, list[i].id, SpvDecorationBinding); + module->shaderModule->maps.pushConstantIndex = module->shaderModule->maps.uniformBufferMaps.data[module->shaderModule->maps.uniformBufferMaps.count - 1].metalBindingIndex + 1; + spvc_compiler_set_decoration(compiler, list[i].id, SpvDecorationBinding, module->shaderModule->maps.pushConstantIndex); + } + spvc_compiler_create_compiler_options(compiler, &options); spvc_compiler_options_set_uint(options, SPVC_COMPILER_OPTION_MSL_VERSION, 200); spvc_compiler_options_set_bool(options, SPVC_COMPILER_OPTION_MSL_ENABLE_DECORATION_BINDING, true);