From 0a2c1089c55fb67b5310852358ddcedd5515141d Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sun, 8 Jun 2025 15:05:38 -0400 Subject: [PATCH] upload metal buffers --- .../core/commands/commands/metal_commands.m | 20 ++++++++++++------- .../core/shader_module/metal_shader_module.m | 1 - .../metal/src/core/uniforms/metal_uniform.c | 7 +++++++ .../metal/src/core/uniforms/metal_uniform.h | 8 ++++++++ .../src/core/uniforms/vulkan_uniform_pool.c | 19 ++++++++++++++++++ .../src/core/uniforms/vulkan_uniform_pool.h | 6 ++++++ .../vulkan/src/uniforms/vulkan_uniform_pool.c | 4 ++-- src/core/uniforms/gryphn_uniform.h | 1 - 8 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 rendering_api/metal/src/core/uniforms/metal_uniform.c create mode 100644 rendering_api/metal/src/core/uniforms/metal_uniform.h create mode 100644 rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.c create mode 100644 rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.h diff --git a/rendering_api/metal/src/core/commands/commands/metal_commands.m b/rendering_api/metal/src/core/commands/commands/metal_commands.m index f4df232..a5892eb 100644 --- a/rendering_api/metal/src/core/commands/commands/metal_commands.m +++ b/rendering_api/metal/src/core/commands/commands/metal_commands.m @@ -3,6 +3,7 @@ #include "core/commands/command_buffer/metal_command_buffer.h" #include "core/pipelines/graphics_pipeline/metal_graphics_pipeline.h" #include "core/buffer/metal_buffer.h" +#include "core/uniforms/metal_uniform.h" #import void gnCommandBeginRenderPassFn(struct gnCommandBuffer_t* buffer, struct gnRenderPassInfo_t passInfo) { @@ -111,11 +112,16 @@ void gnCommandDrawIndexedFn(gnCommandBufferHandle buffer, gnIndexType type, int baseVertex:vertexOffset baseInstance:firstInstance ]; - - // [encoder drawIndexedPrimitives:(MTLPrimitiveType) - // indexCount:indexCount - // indexType:((type == GN_UINT32) ? MTLIndexTypeUInt32 : MTLIndexTypeUInt16) - // indexBuffer:buffer->commandBuffer->indexBuffer->buffer->buffer - // indexBufferOffset:firstIndex - // ]; +} + +void gnCommandBindUniformFn(gnCommandBufferHandle buffer, gnUniform uniform) { + id encoder = (id)buffer->commandBuffer->encoder; + if (uniform->uniform->type == GN_UNIFORM_BUFFER_DESCRIPTOR) { + gnBufferUniformInfo info = *(gnBufferUniformInfo*)uniform->uniform->data; + + [encoder setVertexBuffer:info.buffer->buffer->buffer + offset:info.offset + atIndex:(info.binding + 1) + ]; + } } diff --git a/rendering_api/metal/src/core/shader_module/metal_shader_module.m b/rendering_api/metal/src/core/shader_module/metal_shader_module.m index 42e6fc9..3d9539d 100644 --- a/rendering_api/metal/src/core/shader_module/metal_shader_module.m +++ b/rendering_api/metal/src/core/shader_module/metal_shader_module.m @@ -40,7 +40,6 @@ gnReturnCode gnCreateShaderModuleFn(struct gnShaderModule_t *module, struct gnOu spvc_result res = spvc_compiler_compile(compiler, &result); if (res != SPVC_SUCCESS) return GN_FAILED_TO_CONVERT_SHADER_CODE; - // this is where to use result NSError* error = nil; MTLCompileOptions* mtloptions = nil; diff --git a/rendering_api/metal/src/core/uniforms/metal_uniform.c b/rendering_api/metal/src/core/uniforms/metal_uniform.c new file mode 100644 index 0000000..4251241 --- /dev/null +++ b/rendering_api/metal/src/core/uniforms/metal_uniform.c @@ -0,0 +1,7 @@ +#include +#include "metal_uniform.h" + +void gnUpdateBufferUniformFn(gnUniform uniform, gnBufferUniformInfo* info) { + uniform->uniform->data = malloc(sizeof(gnBufferUniformInfo)); + memcpy(uniform->uniform->data, info, sizeof(gnBufferUniformInfo)); +} diff --git a/rendering_api/metal/src/core/uniforms/metal_uniform.h b/rendering_api/metal/src/core/uniforms/metal_uniform.h new file mode 100644 index 0000000..4c01c36 --- /dev/null +++ b/rendering_api/metal/src/core/uniforms/metal_uniform.h @@ -0,0 +1,8 @@ +#pragma once +#include "core/uniforms/gryphn_uniform.h" +#include + +typedef struct gnPlatformUniform_t { + gnUniformType type; + void* data; +} gnPlatformUniform; diff --git a/rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.c b/rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.c new file mode 100644 index 0000000..172c924 --- /dev/null +++ b/rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.c @@ -0,0 +1,19 @@ +#include +#include +#include "metal_uniform.h" + +gnReturnCode gnCreateUniformPoolFn(gnUniformPool pool, gnDeviceHandle device) { + return GN_SUCCESS; +} + +gnUniform* gnUniformPoolAllocateUniformsFn(gnUniformPool pool, const gnUniformLayout layout) { + gnUniform* uniforms = malloc(sizeof(gnUniform) * layout.uniformBindingCount); + for (int i = 0; i < layout.uniformBindingCount; i++) { + uniforms[i] = malloc(sizeof(struct gnUniform_t)); + uniforms[i]->uniform = malloc(sizeof(struct gnPlatformUniform_t)); + uniforms[i]->uniform->type = layout.uniformBindings[i].type; + } + return uniforms; +} + +void gnDestroyUniformPoolFn(gnUniformPool pool) { } diff --git a/rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.h b/rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.h new file mode 100644 index 0000000..d145a50 --- /dev/null +++ b/rendering_api/metal/src/core/uniforms/vulkan_uniform_pool.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include + +struct gnPlatformUniformPool_t { +}; diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c index c3e641e..a91611a 100644 --- a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c @@ -47,11 +47,11 @@ gnUniform* gnUniformPoolAllocateUniformsFn(gnUniformPool pool, const gnUniformLa .pSetLayouts = pool->uniformPool->pools[pool->uniformPool->poolCount].layouts }; - VkDescriptorSet* sets = malloc(sizeof(VkDescriptorSet) * pool->uniformPool->pools[pool->uniformPool->poolCount].layoutCount); + VkDescriptorSet* sets = malloc(sizeof(VkDescriptorSet) * layout.uniformBindingCount); if (vkAllocateDescriptorSets(pool->device->outputDevice->device, &allocInfo, sets) != VK_SUCCESS) return NULL; - gnUniform* uniforms = malloc(sizeof(gnUniform) * pool->uniformPool->pools[pool->uniformPool->poolCount].layoutCount); + gnUniform* uniforms = malloc(sizeof(gnUniform) * layout.uniformBindingCount); for (int i = 0; i < pool->uniformPool->pools[pool->uniformPool->poolCount].layoutCount; i++) { uniforms[i] = malloc(sizeof(struct gnUniform_t)); uniforms[i]->uniform = malloc(sizeof(struct gnPlatformUniform_t)); diff --git a/src/core/uniforms/gryphn_uniform.h b/src/core/uniforms/gryphn_uniform.h index 5d84bec..b7be46a 100644 --- a/src/core/uniforms/gryphn_uniform.h +++ b/src/core/uniforms/gryphn_uniform.h @@ -4,7 +4,6 @@ #include "core/gryphn_handles.h" typedef struct gnBufferUniformInfo { - gnUniform uniform; uint32_t binding; gnBuffer buffer; size_t offset;