From e66075beaf55afc1093e49e20461620a5c3f0ee5 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Fri, 6 Jun 2025 17:35:37 -0400 Subject: [PATCH] indexed drawing and index buffers --- .../core/commands/command_buffer/metal_command_buffer.h | 1 + .../metal/src/core/commands/commands/metal_commands.m | 7 ++++--- rendering_api/vulkan/src/buffers/vulkan_buffer.c | 2 ++ .../src/commands/command_buffer/vulkan_command_buffer.h | 4 ++++ .../vulkan/src/commands/commands/vulkan_commands.c | 9 +++++++++ src/core/buffers/gryphn_buffer.h | 7 ++++++- src/core/command/commands/gryphn_command.c | 3 +++ src/core/command/commands/gryphn_command.h | 1 + src/core/gryphn_platform_functions.h | 1 + src/core/instance/init/gryphn_init.c | 1 + 10 files changed, 32 insertions(+), 4 deletions(-) diff --git a/rendering_api/metal/src/core/commands/command_buffer/metal_command_buffer.h b/rendering_api/metal/src/core/commands/command_buffer/metal_command_buffer.h index 96094dd..f0335a0 100644 --- a/rendering_api/metal/src/core/commands/command_buffer/metal_command_buffer.h +++ b/rendering_api/metal/src/core/commands/command_buffer/metal_command_buffer.h @@ -8,4 +8,5 @@ typedef struct gnPlatformCommandBuffer_t { id commandBuffer; id encoder; struct gnGraphicsPipeline_t* boundGraphcisPipeline; + gnBufferHandle indexBuffer; } gnPlatformCommandBuffer; 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 b402809..ad3578b 100644 --- a/rendering_api/metal/src/core/commands/commands/metal_commands.m +++ b/rendering_api/metal/src/core/commands/commands/metal_commands.m @@ -70,10 +70,11 @@ void gnCommandSetScissorFn(struct gnCommandBuffer_t* buffer, struct gnScissor_t [encoder setScissorRect:scissorRect]; } void gnCommandBindBufferFn(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type) { - if (type == GN_VERTEX_BUFFER) { - id encoder = (id)buffer->commandBuffer->encoder; + id encoder = (id)buffer->commandBuffer->encoder; + if (type == GN_VERTEX_BUFFER) [encoder setVertexBuffer:bufferToBind->buffer->buffer offset:0 atIndex:0]; - } + else if (type == GN_INDEX_BUFFER) + buffer->commandBuffer->indexBuffer = bufferToBind; } void gnCommandDrawFn(struct gnCommandBuffer_t* buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance) { if (buffer->commandBuffer->boundGraphcisPipeline != NULL) { diff --git a/rendering_api/vulkan/src/buffers/vulkan_buffer.c b/rendering_api/vulkan/src/buffers/vulkan_buffer.c index 615fd3e..36a7a0b 100644 --- a/rendering_api/vulkan/src/buffers/vulkan_buffer.c +++ b/rendering_api/vulkan/src/buffers/vulkan_buffer.c @@ -7,6 +7,7 @@ VkBufferUsageFlags vkGryphnBufferType(gnBufferType type) { switch (type) { case GN_VERTEX_BUFFER: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; +case GN_INDEX_BUFFER: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT; } } @@ -90,6 +91,7 @@ void VkCopyBuffer(VkBuffer source, VkBuffer destination, size_t size, VkCommandP gnReturnCode gnCreateBufferFn(gnBufferHandle buffer, gnOutputDeviceHandle device, gnBufferInfo info) { buffer->buffer = malloc(sizeof(struct gnPlatformBuffer_t)); VkBufferUsageFlags usage = vkGryphnBufferType(info.type); + buffer->buffer->useStagingBuffer = gnFalse; if (info.usage == GN_STATIC_DRAW) { gnReturnCode createdBuffer = VkCreateBuffer( &buffer->buffer->stagingBuffer, &buffer->buffer->stagingBufferMemory, diff --git a/rendering_api/vulkan/src/commands/command_buffer/vulkan_command_buffer.h b/rendering_api/vulkan/src/commands/command_buffer/vulkan_command_buffer.h index cfc1dc3..30f0e22 100644 --- a/rendering_api/vulkan/src/commands/command_buffer/vulkan_command_buffer.h +++ b/rendering_api/vulkan/src/commands/command_buffer/vulkan_command_buffer.h @@ -1,7 +1,11 @@ #pragma once #include "core/command/command_buffer/gryphn_command_buffer.h" +#include "utils/gryphn_bool.h" #include typedef struct gnPlatformCommandBuffer_t { VkCommandBuffer buffer; + + gnBool changedBuffer; + gnBufferHandle boundIndexBuffer; } gnPlatformCommandBuffer; diff --git a/rendering_api/vulkan/src/commands/commands/vulkan_commands.c b/rendering_api/vulkan/src/commands/commands/vulkan_commands.c index 86b93fe..d4686b5 100644 --- a/rendering_api/vulkan/src/commands/commands/vulkan_commands.c +++ b/rendering_api/vulkan/src/commands/commands/vulkan_commands.c @@ -59,7 +59,16 @@ VkDeviceSize offsets[] = {0}; void gnCommandBindBufferFn(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type) { if (type == GN_VERTEX_BUFFER) vkCmdBindVertexBuffers(buffer->commandBuffer->buffer, 0, 1, &bufferToBind->buffer->buffer, offsets); + else if (type == GN_INDEX_BUFFER) { + buffer->commandBuffer->changedBuffer = gnTrue; + buffer->commandBuffer->boundIndexBuffer = bufferToBind; + } } void gnCommandDrawFn(struct gnCommandBuffer_t* buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance) { vkCmdDraw(buffer->commandBuffer->buffer, vertexCount, instanceCount, firstVertex, firstInstance); } +void gnCommandDrawIndexedFn(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance) { + if (buffer->commandBuffer->changedBuffer) vkCmdBindIndexBuffer(buffer->commandBuffer->buffer, buffer->commandBuffer->boundIndexBuffer->buffer->buffer, 0, (type == GN_UINT32) ? VK_INDEX_TYPE_UINT32 : VK_INDEX_TYPE_UINT16); + vkCmdDrawIndexed(buffer->commandBuffer->buffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); + buffer->commandBuffer->changedBuffer = gnFalse; +} diff --git a/src/core/buffers/gryphn_buffer.h b/src/core/buffers/gryphn_buffer.h index c5a9012..e0bc2f4 100644 --- a/src/core/buffers/gryphn_buffer.h +++ b/src/core/buffers/gryphn_buffer.h @@ -3,8 +3,13 @@ #include "utils/gryphn_error_code.h" #include +typedef enum gnIndexType { + GN_UINT16, GN_UINT32 +} gnIndexType; + typedef enum gnBufferType { - GN_VERTEX_BUFFER = 0x00000001 + GN_VERTEX_BUFFER = 0x00000001f, + GN_INDEX_BUFFER = 0x00000002f, } gnBufferType; typedef enum gnBufferUsage { diff --git a/src/core/command/commands/gryphn_command.c b/src/core/command/commands/gryphn_command.c index 40e195e..c965853 100644 --- a/src/core/command/commands/gryphn_command.c +++ b/src/core/command/commands/gryphn_command.c @@ -23,3 +23,6 @@ void gnCommandBindBuffer(gnCommandBufferHandle buffer, gnBufferHandle bufferToBi void gnCommandDraw(struct gnCommandBuffer_t* buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance) { buffer->commandPool->commandFunctions->_gnCommandDraw(buffer, vertexCount, firstVertex, instanceCount, firstInstance); } +void gnCommandDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance) { + buffer->commandPool->commandFunctions->_gnCommandDrawIndexed(buffer, type, indexCount, firstIndex, vertexOffset, instanceCount, firstInstance); +} diff --git a/src/core/command/commands/gryphn_command.h b/src/core/command/commands/gryphn_command.h index b2d0c76..43951b9 100644 --- a/src/core/command/commands/gryphn_command.h +++ b/src/core/command/commands/gryphn_command.h @@ -13,3 +13,4 @@ void gnCommandSetScissor(gnCommandBufferHandle buffer, gnScissor scissor); void gnCommandBindBuffer(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type); void gnCommandDraw(gnCommandBufferHandle buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance); +void gnCommandDrawIndexed(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance); diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index e965884..a2919e1 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -113,4 +113,5 @@ typedef struct gnCommandFunctions_t { void (*_gnCommandBindBuffer)(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type); void (*_gnCommandDraw)(gnCommandBufferHandle buffer, int vertexCount, int firstVertex, int instanceCount, int firstInstance); + void (*_gnCommandDrawIndexed)(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int firstInstance); } gnCommandFunctions; diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index c010ab6..70bbf0f 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -109,4 +109,5 @@ void gnLoadCommandFunctions(struct gnDynamicLibrary_t* lib, struct gnCommandFunc gnLoadDLLFunction(lib, functions->_gnCommandSetScissor, "gnCommandSetScissorFn"); gnLoadDLLFunction(lib, functions->_gnCommandBindBuffer, "gnCommandBindBufferFn"); gnLoadDLLFunction(lib, functions->_gnCommandDraw, "gnCommandDrawFn"); + gnLoadDLLFunction(lib, functions->_gnCommandDrawIndexed, "gnCommandDrawIndexedFn"); }