From 484bea4762236d5d07890647469e9a95f18a4b29 Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Mon, 21 Jul 2025 09:40:16 -0400 Subject: [PATCH] generate used resources --- .../src/commands/commands/metal_commands.m | 2 +- .../apis/metal/src/uniforms/metal_uniform.h | 5 +++-- .../apis/metal/src/uniforms/metal_uniform.m | 17 ++++++++++++++--- .../metal/src/uniforms/metal_uniform_pool.m | 2 ++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/projects/apis/metal/src/commands/commands/metal_commands.m b/projects/apis/metal/src/commands/commands/metal_commands.m index 54bdc15..cb1e343 100644 --- a/projects/apis/metal/src/commands/commands/metal_commands.m +++ b/projects/apis/metal/src/commands/commands/metal_commands.m @@ -114,7 +114,7 @@ void metalDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexC void metalBindUniform(gnCommandBufferHandle buffer, gnUniform uniform, uint32_t set) { id encoder = (id)buffer->commandBuffer->encoder; - [encoder useResources:uniform->uniform->resources.data count:uniform->uniform->resources.count usage:MTLResourceUsageRead stages:MTLRenderStageFragment]; + [encoder useResources:uniform->uniform->usedResources count:uniform->uniform->usedResourceCount usage:MTLResourceUsageRead stages:MTLRenderStageFragment]; [encoder setFragmentBuffer:uniform->uniform->argumentBuffer offset:0 atIndex:2]; // for (int i = 0; i < uniform->uniform->bindingCount; i++) { diff --git a/projects/apis/metal/src/uniforms/metal_uniform.h b/projects/apis/metal/src/uniforms/metal_uniform.h index 13f9090..404471c 100644 --- a/projects/apis/metal/src/uniforms/metal_uniform.h +++ b/projects/apis/metal/src/uniforms/metal_uniform.h @@ -11,14 +11,15 @@ typedef struct metalUniformBinding { } metalUniformBinding; typedef id mtlResource; -GN_ARRAY_LIST(mtlResource); typedef struct gnPlatformUniform_t { uint32_t index[MAX_METAL_BINDINGS]; id encoder; id argumentBuffer; - mtlResourceArrayList resources; + mtlResource usedResources[MAX_METAL_BINDINGS]; + int indexMap[MAX_METAL_BINDINGS]; + uint32_t usedResourceCount; } gnPlatformUniform; void updateMetalBufferUniform(gnUniform uniform, gnBufferUniformInfo* info); diff --git a/projects/apis/metal/src/uniforms/metal_uniform.m b/projects/apis/metal/src/uniforms/metal_uniform.m index fac2e6a..148d56c 100644 --- a/projects/apis/metal/src/uniforms/metal_uniform.m +++ b/projects/apis/metal/src/uniforms/metal_uniform.m @@ -20,14 +20,25 @@ void updateMetalStorageUniform(gnUniform uniform, gnStorageUniformInfo* info) { } void updateMetalImageUniform(gnUniform uniform, gnImageUniformInfo* info) { - printf("%p\n", info->texture->texture->texture); + // mtlResource usedResources[MAX_METAL_BINDINGS]; + // uint32_t indexMap[MAX_METAL_BINDINGS]; + // uint32_t usedResourceCount; + + if (uniform->uniform->indexMap[info->binding] == -1) { + uniform->uniform->indexMap[info->binding] = uniform->uniform->usedResourceCount; + uniform->uniform->usedResourceCount++; + } + + uniform->uniform->usedResources[uniform->uniform->indexMap[info->binding]] = info->texture->texture->texture; + [uniform->uniform->encoder setTexture:info->texture->texture->texture atIndex:uniform->uniform->index[info->binding]]; [uniform->uniform->encoder setSamplerState:info->texture->texture->sampler atIndex:uniform->uniform->index[info->binding] + 1]; - uniform->uniform->resources = mtlResourceArrayListCreate(); - mtlResourceArrayListAdd(&uniform->uniform->resources, info->texture->texture->texture); + // uniform->uniform->resources = mtlResourceArrayListCreate(); + // mtlResourceArrayListAdd(&uniform->uniform->resources, info->texture->texture->texture); // mtlResourceArrayListAdd(&uniform->uniform->resources, info->texture->texture->sampler); + // printf("updating metal image uniform %i\n", uniform->uniform->index[info->binding]); // printf("binding: %i\n", info->binding); diff --git a/projects/apis/metal/src/uniforms/metal_uniform_pool.m b/projects/apis/metal/src/uniforms/metal_uniform_pool.m index 4ada68e..65985ad 100644 --- a/projects/apis/metal/src/uniforms/metal_uniform_pool.m +++ b/projects/apis/metal/src/uniforms/metal_uniform_pool.m @@ -60,6 +60,8 @@ gnUniform* allocateMetalUniforms(gnUniformPool pool, const gnUniformAllocationIn } for (int k = 0; k < arguments.count; k++) [[arguments objectAtIndex:k] release]; + + for (int g = 0; g < MAX_METAL_BINDINGS; g++) uniforms[i]->uniform->indexMap[g] = -1; } return uniforms; }