clean up buffer creation
This commit is contained in:
@@ -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;
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -3,12 +3,21 @@
|
||||
#include "utils/gryphn_bool.h"
|
||||
#include <core/buffers/gryphn_buffer.h>
|
||||
|
||||
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
|
||||
);
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
#include <vulkan/vulkan.h>
|
||||
#include "core/textures/gryphn_texture.h"
|
||||
#include "buffers/vulkan_buffer.h"
|
||||
|
||||
typedef struct gnPlatformTexture_t {
|
||||
VkGryphnBuffer buffer;
|
||||
|
||||
VkImage image;
|
||||
VkImageView imageView;
|
||||
} gnPlatformTexture;
|
||||
|
@@ -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) {
|
||||
|
@@ -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);
|
||||
|
@@ -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");
|
||||
|
@@ -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 {
|
||||
|
@@ -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 <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
|
||||
struct gnTexture_t {
|
||||
struct gnPlatformTexture_t* texture;
|
||||
gnDeviceHandle device;
|
||||
};
|
||||
#endif
|
||||
|
||||
gnReturnCode gnCreateTexture(gnTexture* texture, gnDevice device, const gnTextureInfo info);
|
||||
|
Reference in New Issue
Block a user