From ffdde93ed28e24f15491c3c825ef8570a917e200 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sun, 8 Jun 2025 17:25:08 -0400 Subject: [PATCH] clean up buffer creation --- .../core/shader_module/metal_shader_module.m | 4 --- .../vulkan/src/buffers/vulkan_buffer.c | 36 +++++++++---------- .../vulkan/src/buffers/vulkan_buffer.h | 17 ++++++--- .../src/commands/commands/vulkan_commands.c | 4 +-- .../vulkan_render_pass_descriptor.c | 6 ++-- .../vulkan/src/textures/vulkan_texture.h | 3 ++ src/core/command/commands/gryphn_command.c | 2 ++ src/core/gryphn_platform_functions.h | 6 ++-- src/core/instance/init/gryphn_init.c | 1 + .../gryphn_render_pass_descriptor.h | 16 ++++----- src/core/textures/gryphn_texture.h | 19 ++++++++++ 11 files changed, 72 insertions(+), 42 deletions(-) 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 4e5d21c..3afd686 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 @@ -55,10 +55,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; - printf("Res %s\n", result); - - - NSError* error = nil; MTLCompileOptions* mtloptions = nil; diff --git a/rendering_api/vulkan/src/buffers/vulkan_buffer.c b/rendering_api/vulkan/src/buffers/vulkan_buffer.c index c54f645..cbb1e1d 100644 --- a/rendering_api/vulkan/src/buffers/vulkan_buffer.c +++ b/rendering_api/vulkan/src/buffers/vulkan_buffer.c @@ -15,7 +15,7 @@ VkBufferUsageFlags vkGryphnBufferType(gnBufferType type) { } gnReturnCode VkCreateBuffer( - VkBuffer* buffer, VkDeviceMemory* memory, gnBufferInfo info, + VkGryphnBuffer* buffer, gnBufferInfo info, VkDevice device, VkPhysicalDevice physcialDevice, VkMemoryPropertyFlags flags, VkBufferUsageFlags usage ) { @@ -26,11 +26,11 @@ gnReturnCode VkCreateBuffer( .sharingMode = VK_SHARING_MODE_EXCLUSIVE }; - if (vkCreateBuffer(device, &bufferInfo, NULL, buffer) != VK_SUCCESS) + if (vkCreateBuffer(device, &bufferInfo, NULL, &buffer->buffer) != VK_SUCCESS) return GN_FAILED_TO_CREATE_BUFFER; VkMemoryRequirements bufferRequirements; - vkGetBufferMemoryRequirements(device, *buffer, &bufferRequirements); + vkGetBufferMemoryRequirements(device, buffer->buffer, &bufferRequirements); VkMemoryAllocateInfo memoryAllocateInfo = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, @@ -49,10 +49,10 @@ gnReturnCode VkCreateBuffer( } // this whole thing was adapted from vulkan-tutorial.com if (!foundMemory) return GN_FAILED_TO_ALLOCATE_MEMORY; - if (vkAllocateMemory(device, &memoryAllocateInfo, NULL, memory) != VK_SUCCESS) { + if (vkAllocateMemory(device, &memoryAllocateInfo, NULL, &buffer->memory) != VK_SUCCESS) { return GN_FAILED_TO_ALLOCATE_MEMORY; } - vkBindBufferMemory(device, *buffer, *memory, 0); + vkBindBufferMemory(device, buffer->buffer, buffer->memory, 0); return GN_SUCCESS; } @@ -98,21 +98,21 @@ gnReturnCode gnCreateBufferFn(gnBufferHandle buffer, gnOutputDeviceHandle device if (info.usage == GN_STATIC_DRAW) { buffer->buffer->useStagingBuffer = gnTrue; VkCreateBuffer( - &buffer->buffer->stagingBuffer, &buffer->buffer->stagingBufferMemory, + &buffer->buffer->stagingBuffer, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT ); return VkCreateBuffer( - &buffer->buffer->buffer, &buffer->buffer->bufferMemory, + &buffer->buffer->buffer, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vkGryphnBufferType(info.type) | VK_BUFFER_USAGE_TRANSFER_DST_BIT ); } else { return VkCreateBuffer( - &buffer->buffer->buffer, &buffer->buffer->bufferMemory, + &buffer->buffer->buffer, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vkGryphnBufferType(info.type) @@ -126,31 +126,31 @@ gnReturnCode gnCreateBufferFn(gnBufferHandle buffer, gnOutputDeviceHandle device void gnBufferDataFn(gnBufferHandle buffer, size_t dataSize, void* data) { void* bufferData; if (buffer->buffer->useStagingBuffer) { - vkMapMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBufferMemory, 0, dataSize, 0, &bufferData); + vkMapMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.memory, 0, dataSize, 0, &bufferData); memcpy(bufferData, data, dataSize); - vkUnmapMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBufferMemory); + vkUnmapMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.memory); VkCopyBuffer( - buffer->buffer->stagingBuffer, buffer->buffer->buffer, dataSize, + buffer->buffer->stagingBuffer.buffer, buffer->buffer->buffer.buffer, dataSize, buffer->device->outputDevice->transferCommandPool, buffer->device->outputDevice->device, buffer->device->outputDevice->transferQueue); } else { - vkMapMemory(buffer->device->outputDevice->device, buffer->buffer->bufferMemory, 0, dataSize, 0, &bufferData); + vkMapMemory(buffer->device->outputDevice->device, buffer->buffer->buffer.memory, 0, dataSize, 0, &bufferData); memcpy(bufferData, data, dataSize); - vkUnmapMemory(buffer->device->outputDevice->device, buffer->buffer->bufferMemory); + vkUnmapMemory(buffer->device->outputDevice->device, buffer->buffer->buffer.memory); } } void* gnMapBufferFn(gnBufferHandle buffer) { void* data; - vkMapMemory(buffer->device->outputDevice->device, buffer->buffer->bufferMemory, 0, buffer->info.size, 0, &data); + vkMapMemory(buffer->device->outputDevice->device, buffer->buffer->buffer.memory, 0, buffer->info.size, 0, &data); return data; } void gnDestroyBufferFn(gnBufferHandle buffer) { if (buffer->buffer->useStagingBuffer == gnTrue) { - vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer, NULL); - vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBufferMemory, NULL); + vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.buffer, NULL); + vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.memory, NULL); } - vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->buffer, NULL); - vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->bufferMemory, NULL); + vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->buffer.buffer, NULL); + vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->buffer.memory, NULL); free(buffer->buffer); } diff --git a/rendering_api/vulkan/src/buffers/vulkan_buffer.h b/rendering_api/vulkan/src/buffers/vulkan_buffer.h index 57eafd0..8f9a82d 100644 --- a/rendering_api/vulkan/src/buffers/vulkan_buffer.h +++ b/rendering_api/vulkan/src/buffers/vulkan_buffer.h @@ -3,12 +3,21 @@ #include "utils/gryphn_bool.h" #include -struct gnPlatformBuffer_t { +typedef struct VkGryphnBuffer { VkBuffer buffer; - VkDeviceMemory bufferMemory; + VkDeviceMemory memory; +} VkGryphnBuffer; + +struct gnPlatformBuffer_t { + VkGryphnBuffer buffer; // for if static draw - VkBuffer stagingBuffer; - VkDeviceMemory stagingBufferMemory; + VkGryphnBuffer stagingBuffer; gnBool useStagingBuffer; }; + +gnReturnCode VkCreateBuffer( + VkGryphnBuffer*, gnBufferInfo info, + VkDevice device, VkPhysicalDevice physcialDevice, + VkMemoryPropertyFlags flags, VkBufferUsageFlags usage +); diff --git a/rendering_api/vulkan/src/commands/commands/vulkan_commands.c b/rendering_api/vulkan/src/commands/commands/vulkan_commands.c index beb44a2..5db6715 100644 --- a/rendering_api/vulkan/src/commands/commands/vulkan_commands.c +++ b/rendering_api/vulkan/src/commands/commands/vulkan_commands.c @@ -60,7 +60,7 @@ void gnCommandSetScissorFn(gnCommandBuffer buffer, struct gnScissor_t scissor) { 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); + vkCmdBindVertexBuffers(buffer->commandBuffer->buffer, 0, 1, &bufferToBind->buffer->buffer.buffer, offsets); else if (type == GN_INDEX_BUFFER) { buffer->commandBuffer->changedBuffer = gnTrue; buffer->commandBuffer->boundIndexBuffer = bufferToBind; @@ -70,7 +70,7 @@ void gnCommandDrawFn(gnCommandBuffer buffer, int vertexCount, int firstVertex, i 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); + if (buffer->commandBuffer->changedBuffer) vkCmdBindIndexBuffer(buffer->commandBuffer->buffer, buffer->commandBuffer->boundIndexBuffer->buffer->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/rendering_api/vulkan/src/renderpass/vulkan_render_pass_descriptor.c b/rendering_api/vulkan/src/renderpass/vulkan_render_pass_descriptor.c index fc880a2..df41202 100644 --- a/rendering_api/vulkan/src/renderpass/vulkan_render_pass_descriptor.c +++ b/rendering_api/vulkan/src/renderpass/vulkan_render_pass_descriptor.c @@ -2,7 +2,7 @@ #include "vulkan_surface/vulkan_surface.h" #include "output_device/vulkan_output_devices.h" -VkAttachmentLoadOp vkGryphnLoadOperation(enum gnLoadOperation_e loadOperation) { +VkAttachmentLoadOp vkGryphnLoadOperation(gnLoadOperation loadOperation) { switch(loadOperation) { case GN_LOAD_OPERATION_LOAD: return VK_ATTACHMENT_LOAD_OP_LOAD; case GN_LOAD_OPERATION_CLEAR: return VK_ATTACHMENT_LOAD_OP_CLEAR; @@ -10,14 +10,14 @@ VkAttachmentLoadOp vkGryphnLoadOperation(enum gnLoadOperation_e loadOperation) { } } -VkAttachmentStoreOp vkGryphnStoreOperation(enum gnStoreOperation_e storeOperation) { +VkAttachmentStoreOp vkGryphnStoreOperation(gnStoreOperation storeOperation) { switch (storeOperation) { case GN_STORE_OPERATION_STORE: return VK_ATTACHMENT_STORE_OP_STORE; case GN_STORE_OPERATION_DONT_CARE: return VK_ATTACHMENT_STORE_OP_DONT_CARE; } } -VkImageLayout vkGryphnImageLayout(enum gnImageLayout_e layout) { +VkImageLayout vkGryphnImageLayout(gnImageLayout layout) { switch(layout) { case GN_LAYOUT_UNDEFINED: return VK_IMAGE_LAYOUT_UNDEFINED; case GN_LAYOUT_PRESENTATION_QUEUE_IMAGE: return VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; diff --git a/rendering_api/vulkan/src/textures/vulkan_texture.h b/rendering_api/vulkan/src/textures/vulkan_texture.h index 32d0e01..e9fc944 100644 --- a/rendering_api/vulkan/src/textures/vulkan_texture.h +++ b/rendering_api/vulkan/src/textures/vulkan_texture.h @@ -1,8 +1,11 @@ #pragma once #include #include "core/textures/gryphn_texture.h" +#include "buffers/vulkan_buffer.h" typedef struct gnPlatformTexture_t { + VkGryphnBuffer buffer; + VkImage image; VkImageView imageView; } gnPlatformTexture; diff --git a/src/core/command/commands/gryphn_command.c b/src/core/command/commands/gryphn_command.c index 275168a..623ed37 100644 --- a/src/core/command/commands/gryphn_command.c +++ b/src/core/command/commands/gryphn_command.c @@ -1,4 +1,6 @@ #include "gryphn_command.h" +#include "core/command/command_buffer/gryphn_command_buffer.h" +#include "core/command/command_pool/gryphn_command_pool.h" #include "core/gryphn_platform_functions.h" void gnCommandBeginRenderPass(struct gnCommandBuffer_t* buffer, struct gnRenderPassInfo_t passInfo) { diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index 21425dd..fecf2c4 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -12,14 +12,12 @@ #include "pipelines/graphics_pipeline/gryphn_graphics_pipeline.h" #include "framebuffer/gryphn_framebuffer.h" #include "command/command_pool/gryphn_command_pool.h" -#include "command/command_buffer/gryphn_command_buffer.h" #include "renderpass/gryphn_render_pass.h" -#include "sync/fence/gryphn_fence.h" -#include "sync/semaphore/gryphn_semaphore.h" #include "core/submit/gryphn_submit.h" #include "core/present/gryphn_present.h" #include "core/buffers/gryphn_buffer.h" #include "core/uniforms/gryphn_uniform.h" +#include "core/textures/gryphn_texture.h" typedef struct gnFunctions_t { gnReturnCode (*_gnCreateInstance)(gnInstanceHandle instance, gnInstanceInfo info); @@ -94,6 +92,8 @@ typedef struct gnDeviceFunctions_t { void (*_gnUpdateBufferUniform)(gnUniform uniform, gnBufferUniformInfo* bufferInfo); + gnReturnCode (*_gnCreateTexture)(gnTexture texture, gnDevice device, const gnTextureInfo info); + gnReturnCode (*_gnCreateFence)(gnFenceHandle fence, gnOutputDeviceHandle device); void (*_gnSignalFence)(gnFenceHandle fence); void (*_gnWaitForFence)(gnFenceHandle fence, uint64_t timeout); diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index 25a18c5..2aafd70 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -93,6 +93,7 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti gnLoadDLLFunction(lib, functions->_gnUniformPoolAllocateUniforms, "gnUniformPoolAllocateUniformsFn"); gnLoadDLLFunction(lib, functions->_gnDestroyUniformPool, "gnDestroyUniformPoolFn"); gnLoadDLLFunction(lib, functions->_gnUpdateBufferUniform, "gnUpdateBufferUniformFn"); + gnLoadDLLFunction(lib, functions->_gnCreateTexture, "gnCreateTextureFn"); gnLoadDLLFunction(lib, functions->_gnCreateFence, "gnCreateFenceFn"); gnLoadDLLFunction(lib, functions->_gnSignalFence, "gnSignalFenceFn"); gnLoadDLLFunction(lib, functions->_gnWaitForFence, "gnWaitForFenceFn"); diff --git a/src/core/renderpass/gryphn_render_pass_descriptor.h b/src/core/renderpass/gryphn_render_pass_descriptor.h index f9c673b..f59c698 100644 --- a/src/core/renderpass/gryphn_render_pass_descriptor.h +++ b/src/core/renderpass/gryphn_render_pass_descriptor.h @@ -21,20 +21,20 @@ typedef enum gnStoreOperation_e { } gnStoreOperation; typedef struct gnRenderPassAttachmentInfo_t { - enum gnImageFormat_e format; - enum gnLoadOperation_e loadOperation; - enum gnStoreOperation_e storeOperation; + gnImageFormat format; + gnLoadOperation loadOperation; + gnStoreOperation storeOperation; - enum gnLoadOperation_e stencilLoadOperation; - enum gnStoreOperation_e stencilStoreOperation; + gnLoadOperation stencilLoadOperation; + gnStoreOperation stencilStoreOperation; - enum gnImageLayout_e initialLayout; - enum gnImageLayout_e finalLayout; + gnImageLayout initialLayout; + gnImageLayout finalLayout; } gnRenderPassAttachmentInfo; typedef struct gnSubpassAttachmentInfo_t { uint32_t index; - enum gnImageLayout_e imageLayout; + gnImageLayout imageLayout; } gnSubpassAttachmentInfo; typedef struct gnSubpassInfo_t { diff --git a/src/core/textures/gryphn_texture.h b/src/core/textures/gryphn_texture.h index fd5c4da..49ac719 100644 --- a/src/core/textures/gryphn_texture.h +++ b/src/core/textures/gryphn_texture.h @@ -1,7 +1,26 @@ #pragma once +#include "stdint.h" +#include "stdlib.h" +#include "utils/gryphn_image_format.h" +#include "utils/gryphn_error_code.h" +#include + +typedef enum gnTextureType { + GN_TEXTURE_2D +} gnTextureType; + +typedef struct gnTextureInfo { + uint32_t width; + uint32_t height; + gnTextureType type; + gnImageFormat format; +} gnTextureInfo; #ifdef GN_REVEAL_IMPL struct gnTexture_t { struct gnPlatformTexture_t* texture; + gnDeviceHandle device; }; #endif + +gnReturnCode gnCreateTexture(gnTexture* texture, gnDevice device, const gnTextureInfo info);