clean up buffer creation

This commit is contained in:
Greg Wells
2025-06-08 17:25:08 -04:00
parent 93d81966e3
commit ffdde93ed2
11 changed files with 72 additions and 42 deletions

View File

@@ -55,10 +55,6 @@ gnReturnCode gnCreateShaderModuleFn(struct gnShaderModule_t *module, struct gnOu
spvc_result res = spvc_compiler_compile(compiler, &result); spvc_result res = spvc_compiler_compile(compiler, &result);
if (res != SPVC_SUCCESS) if (res != SPVC_SUCCESS)
return GN_FAILED_TO_CONVERT_SHADER_CODE; return GN_FAILED_TO_CONVERT_SHADER_CODE;
printf("Res %s\n", result);
NSError* error = nil; NSError* error = nil;
MTLCompileOptions* mtloptions = nil; MTLCompileOptions* mtloptions = nil;

View File

@@ -15,7 +15,7 @@ VkBufferUsageFlags vkGryphnBufferType(gnBufferType type) {
} }
gnReturnCode VkCreateBuffer( gnReturnCode VkCreateBuffer(
VkBuffer* buffer, VkDeviceMemory* memory, gnBufferInfo info, VkGryphnBuffer* buffer, gnBufferInfo info,
VkDevice device, VkPhysicalDevice physcialDevice, VkDevice device, VkPhysicalDevice physcialDevice,
VkMemoryPropertyFlags flags, VkBufferUsageFlags usage VkMemoryPropertyFlags flags, VkBufferUsageFlags usage
) { ) {
@@ -26,11 +26,11 @@ gnReturnCode VkCreateBuffer(
.sharingMode = VK_SHARING_MODE_EXCLUSIVE .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; return GN_FAILED_TO_CREATE_BUFFER;
VkMemoryRequirements bufferRequirements; VkMemoryRequirements bufferRequirements;
vkGetBufferMemoryRequirements(device, *buffer, &bufferRequirements); vkGetBufferMemoryRequirements(device, buffer->buffer, &bufferRequirements);
VkMemoryAllocateInfo memoryAllocateInfo = { VkMemoryAllocateInfo memoryAllocateInfo = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
@@ -49,10 +49,10 @@ gnReturnCode VkCreateBuffer(
} // this whole thing was adapted from vulkan-tutorial.com } // this whole thing was adapted from vulkan-tutorial.com
if (!foundMemory) return GN_FAILED_TO_ALLOCATE_MEMORY; 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; return GN_FAILED_TO_ALLOCATE_MEMORY;
} }
vkBindBufferMemory(device, *buffer, *memory, 0); vkBindBufferMemory(device, buffer->buffer, buffer->memory, 0);
return GN_SUCCESS; return GN_SUCCESS;
} }
@@ -98,21 +98,21 @@ gnReturnCode gnCreateBufferFn(gnBufferHandle buffer, gnOutputDeviceHandle device
if (info.usage == GN_STATIC_DRAW) { if (info.usage == GN_STATIC_DRAW) {
buffer->buffer->useStagingBuffer = gnTrue; buffer->buffer->useStagingBuffer = gnTrue;
VkCreateBuffer( VkCreateBuffer(
&buffer->buffer->stagingBuffer, &buffer->buffer->stagingBufferMemory, &buffer->buffer->stagingBuffer,
info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT VK_BUFFER_USAGE_TRANSFER_SRC_BIT
); );
return VkCreateBuffer( return VkCreateBuffer(
&buffer->buffer->buffer, &buffer->buffer->bufferMemory, &buffer->buffer->buffer,
info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vkGryphnBufferType(info.type) | VK_BUFFER_USAGE_TRANSFER_DST_BIT vkGryphnBufferType(info.type) | VK_BUFFER_USAGE_TRANSFER_DST_BIT
); );
} else { } else {
return VkCreateBuffer( return VkCreateBuffer(
&buffer->buffer->buffer, &buffer->buffer->bufferMemory, &buffer->buffer->buffer,
info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vkGryphnBufferType(info.type) vkGryphnBufferType(info.type)
@@ -126,31 +126,31 @@ gnReturnCode gnCreateBufferFn(gnBufferHandle buffer, gnOutputDeviceHandle device
void gnBufferDataFn(gnBufferHandle buffer, size_t dataSize, void* data) { void gnBufferDataFn(gnBufferHandle buffer, size_t dataSize, void* data) {
void* bufferData; void* bufferData;
if (buffer->buffer->useStagingBuffer) { 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); memcpy(bufferData, data, dataSize);
vkUnmapMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBufferMemory); vkUnmapMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.memory);
VkCopyBuffer( 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->transferCommandPool, buffer->device->outputDevice->device,
buffer->device->outputDevice->transferQueue); buffer->device->outputDevice->transferQueue);
} else { } 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); 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* gnMapBufferFn(gnBufferHandle buffer) {
void* data; 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; return data;
} }
void gnDestroyBufferFn(gnBufferHandle buffer) { void gnDestroyBufferFn(gnBufferHandle buffer) {
if (buffer->buffer->useStagingBuffer == gnTrue) { if (buffer->buffer->useStagingBuffer == gnTrue) {
vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer, NULL); vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.buffer, NULL);
vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBufferMemory, NULL); vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->stagingBuffer.memory, NULL);
} }
vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->buffer, NULL); vkDestroyBuffer(buffer->device->outputDevice->device, buffer->buffer->buffer.buffer, NULL);
vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->bufferMemory, NULL); vkFreeMemory(buffer->device->outputDevice->device, buffer->buffer->buffer.memory, NULL);
free(buffer->buffer); free(buffer->buffer);
} }

View File

@@ -3,12 +3,21 @@
#include "utils/gryphn_bool.h" #include "utils/gryphn_bool.h"
#include <core/buffers/gryphn_buffer.h> #include <core/buffers/gryphn_buffer.h>
struct gnPlatformBuffer_t { typedef struct VkGryphnBuffer {
VkBuffer buffer; VkBuffer buffer;
VkDeviceMemory bufferMemory; VkDeviceMemory memory;
} VkGryphnBuffer;
struct gnPlatformBuffer_t {
VkGryphnBuffer buffer;
// for if static draw // for if static draw
VkBuffer stagingBuffer; VkGryphnBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
gnBool useStagingBuffer; gnBool useStagingBuffer;
}; };
gnReturnCode VkCreateBuffer(
VkGryphnBuffer*, gnBufferInfo info,
VkDevice device, VkPhysicalDevice physcialDevice,
VkMemoryPropertyFlags flags, VkBufferUsageFlags usage
);

View File

@@ -60,7 +60,7 @@ void gnCommandSetScissorFn(gnCommandBuffer buffer, struct gnScissor_t scissor) {
VkDeviceSize offsets[] = {0}; VkDeviceSize offsets[] = {0};
void gnCommandBindBufferFn(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type) { void gnCommandBindBufferFn(gnCommandBufferHandle buffer, gnBufferHandle bufferToBind, gnBufferType type) {
if (type == GN_VERTEX_BUFFER) 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) { else if (type == GN_INDEX_BUFFER) {
buffer->commandBuffer->changedBuffer = gnTrue; buffer->commandBuffer->changedBuffer = gnTrue;
buffer->commandBuffer->boundIndexBuffer = bufferToBind; 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); vkCmdDraw(buffer->commandBuffer->buffer, vertexCount, instanceCount, firstVertex, firstInstance);
} }
void gnCommandDrawIndexedFn(gnCommandBufferHandle buffer, gnIndexType type, int indexCount, int firstIndex, int vertexOffset, int instanceCount, int 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); vkCmdDrawIndexed(buffer->commandBuffer->buffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
buffer->commandBuffer->changedBuffer = gnFalse; buffer->commandBuffer->changedBuffer = gnFalse;
} }

View File

@@ -2,7 +2,7 @@
#include "vulkan_surface/vulkan_surface.h" #include "vulkan_surface/vulkan_surface.h"
#include "output_device/vulkan_output_devices.h" #include "output_device/vulkan_output_devices.h"
VkAttachmentLoadOp vkGryphnLoadOperation(enum gnLoadOperation_e loadOperation) { VkAttachmentLoadOp vkGryphnLoadOperation(gnLoadOperation loadOperation) {
switch(loadOperation) { switch(loadOperation) {
case GN_LOAD_OPERATION_LOAD: return VK_ATTACHMENT_LOAD_OP_LOAD; case GN_LOAD_OPERATION_LOAD: return VK_ATTACHMENT_LOAD_OP_LOAD;
case GN_LOAD_OPERATION_CLEAR: return VK_ATTACHMENT_LOAD_OP_CLEAR; 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) { switch (storeOperation) {
case GN_STORE_OPERATION_STORE: return VK_ATTACHMENT_STORE_OP_STORE; case GN_STORE_OPERATION_STORE: return VK_ATTACHMENT_STORE_OP_STORE;
case GN_STORE_OPERATION_DONT_CARE: return VK_ATTACHMENT_STORE_OP_DONT_CARE; 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) { switch(layout) {
case GN_LAYOUT_UNDEFINED: return VK_IMAGE_LAYOUT_UNDEFINED; case GN_LAYOUT_UNDEFINED: return VK_IMAGE_LAYOUT_UNDEFINED;
case GN_LAYOUT_PRESENTATION_QUEUE_IMAGE: return VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; case GN_LAYOUT_PRESENTATION_QUEUE_IMAGE: return VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;

View File

@@ -1,8 +1,11 @@
#pragma once #pragma once
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include "core/textures/gryphn_texture.h" #include "core/textures/gryphn_texture.h"
#include "buffers/vulkan_buffer.h"
typedef struct gnPlatformTexture_t { typedef struct gnPlatformTexture_t {
VkGryphnBuffer buffer;
VkImage image; VkImage image;
VkImageView imageView; VkImageView imageView;
} gnPlatformTexture; } gnPlatformTexture;

View File

@@ -1,4 +1,6 @@
#include "gryphn_command.h" #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" #include "core/gryphn_platform_functions.h"
void gnCommandBeginRenderPass(struct gnCommandBuffer_t* buffer, struct gnRenderPassInfo_t passInfo) { void gnCommandBeginRenderPass(struct gnCommandBuffer_t* buffer, struct gnRenderPassInfo_t passInfo) {

View File

@@ -12,14 +12,12 @@
#include "pipelines/graphics_pipeline/gryphn_graphics_pipeline.h" #include "pipelines/graphics_pipeline/gryphn_graphics_pipeline.h"
#include "framebuffer/gryphn_framebuffer.h" #include "framebuffer/gryphn_framebuffer.h"
#include "command/command_pool/gryphn_command_pool.h" #include "command/command_pool/gryphn_command_pool.h"
#include "command/command_buffer/gryphn_command_buffer.h"
#include "renderpass/gryphn_render_pass.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/submit/gryphn_submit.h"
#include "core/present/gryphn_present.h" #include "core/present/gryphn_present.h"
#include "core/buffers/gryphn_buffer.h" #include "core/buffers/gryphn_buffer.h"
#include "core/uniforms/gryphn_uniform.h" #include "core/uniforms/gryphn_uniform.h"
#include "core/textures/gryphn_texture.h"
typedef struct gnFunctions_t { typedef struct gnFunctions_t {
gnReturnCode (*_gnCreateInstance)(gnInstanceHandle instance, gnInstanceInfo info); gnReturnCode (*_gnCreateInstance)(gnInstanceHandle instance, gnInstanceInfo info);
@@ -94,6 +92,8 @@ typedef struct gnDeviceFunctions_t {
void (*_gnUpdateBufferUniform)(gnUniform uniform, gnBufferUniformInfo* bufferInfo); void (*_gnUpdateBufferUniform)(gnUniform uniform, gnBufferUniformInfo* bufferInfo);
gnReturnCode (*_gnCreateTexture)(gnTexture texture, gnDevice device, const gnTextureInfo info);
gnReturnCode (*_gnCreateFence)(gnFenceHandle fence, gnOutputDeviceHandle device); gnReturnCode (*_gnCreateFence)(gnFenceHandle fence, gnOutputDeviceHandle device);
void (*_gnSignalFence)(gnFenceHandle fence); void (*_gnSignalFence)(gnFenceHandle fence);
void (*_gnWaitForFence)(gnFenceHandle fence, uint64_t timeout); void (*_gnWaitForFence)(gnFenceHandle fence, uint64_t timeout);

View File

@@ -93,6 +93,7 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti
gnLoadDLLFunction(lib, functions->_gnUniformPoolAllocateUniforms, "gnUniformPoolAllocateUniformsFn"); gnLoadDLLFunction(lib, functions->_gnUniformPoolAllocateUniforms, "gnUniformPoolAllocateUniformsFn");
gnLoadDLLFunction(lib, functions->_gnDestroyUniformPool, "gnDestroyUniformPoolFn"); gnLoadDLLFunction(lib, functions->_gnDestroyUniformPool, "gnDestroyUniformPoolFn");
gnLoadDLLFunction(lib, functions->_gnUpdateBufferUniform, "gnUpdateBufferUniformFn"); gnLoadDLLFunction(lib, functions->_gnUpdateBufferUniform, "gnUpdateBufferUniformFn");
gnLoadDLLFunction(lib, functions->_gnCreateTexture, "gnCreateTextureFn");
gnLoadDLLFunction(lib, functions->_gnCreateFence, "gnCreateFenceFn"); gnLoadDLLFunction(lib, functions->_gnCreateFence, "gnCreateFenceFn");
gnLoadDLLFunction(lib, functions->_gnSignalFence, "gnSignalFenceFn"); gnLoadDLLFunction(lib, functions->_gnSignalFence, "gnSignalFenceFn");
gnLoadDLLFunction(lib, functions->_gnWaitForFence, "gnWaitForFenceFn"); gnLoadDLLFunction(lib, functions->_gnWaitForFence, "gnWaitForFenceFn");

View File

@@ -21,20 +21,20 @@ typedef enum gnStoreOperation_e {
} gnStoreOperation; } gnStoreOperation;
typedef struct gnRenderPassAttachmentInfo_t { typedef struct gnRenderPassAttachmentInfo_t {
enum gnImageFormat_e format; gnImageFormat format;
enum gnLoadOperation_e loadOperation; gnLoadOperation loadOperation;
enum gnStoreOperation_e storeOperation; gnStoreOperation storeOperation;
enum gnLoadOperation_e stencilLoadOperation; gnLoadOperation stencilLoadOperation;
enum gnStoreOperation_e stencilStoreOperation; gnStoreOperation stencilStoreOperation;
enum gnImageLayout_e initialLayout; gnImageLayout initialLayout;
enum gnImageLayout_e finalLayout; gnImageLayout finalLayout;
} gnRenderPassAttachmentInfo; } gnRenderPassAttachmentInfo;
typedef struct gnSubpassAttachmentInfo_t { typedef struct gnSubpassAttachmentInfo_t {
uint32_t index; uint32_t index;
enum gnImageLayout_e imageLayout; gnImageLayout imageLayout;
} gnSubpassAttachmentInfo; } gnSubpassAttachmentInfo;
typedef struct gnSubpassInfo_t { typedef struct gnSubpassInfo_t {

View File

@@ -1,7 +1,26 @@
#pragma once #pragma once
#include "stdint.h"
#include "stdlib.h"
#include "utils/gryphn_image_format.h"
#include "utils/gryphn_error_code.h"
#include <core/gryphn_handles.h>
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 #ifdef GN_REVEAL_IMPL
struct gnTexture_t { struct gnTexture_t {
struct gnPlatformTexture_t* texture; struct gnPlatformTexture_t* texture;
gnDeviceHandle device;
}; };
#endif #endif
gnReturnCode gnCreateTexture(gnTexture* texture, gnDevice device, const gnTextureInfo info);