From 9d1cbbf17a5bce7597ab84e597cd3d8f5591f5fb Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Tue, 10 Jun 2025 15:54:29 -0400 Subject: [PATCH] very bad texture API --- .../vulkan/src/buffers/vulkan_buffer.c | 24 ++++++++++--------- .../vulkan/src/buffers/vulkan_buffer.h | 1 + .../vulkan/src/textures/vulkan_texture.h | 1 + .../vulkan/src/textures/vulkan_texure.c | 17 +++++++++++++ src/utils | 2 +- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/rendering_api/vulkan/src/buffers/vulkan_buffer.c b/rendering_api/vulkan/src/buffers/vulkan_buffer.c index 25c96a5..1f71a11 100644 --- a/rendering_api/vulkan/src/buffers/vulkan_buffer.c +++ b/rendering_api/vulkan/src/buffers/vulkan_buffer.c @@ -14,6 +14,17 @@ VkBufferUsageFlags vkGryphnBufferType(gnBufferType type) { return usageFlags; } +uint32_t VkMemoryIndex(VkPhysicalDevice device, uint32_t memoryType, VkMemoryPropertyFlags flags, gnBool* foundMemory) { + VkPhysicalDeviceMemoryProperties memoryProperties; + vkGetPhysicalDeviceMemoryProperties(device, &memoryProperties); + for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) { + if ((memoryType & (1 << i)) && (memoryProperties.memoryTypes[i].propertyFlags & flags) == flags) { + *foundMemory = gnTrue; + return i; + } + } // this whole thing was adapted from vulkan-tutorial.com + return 0; +} gnReturnCode VkCreateBuffer( VkGryphnBuffer* buffer, size_t size, gnDevice device, VkMemoryPropertyFlags flags, VkBufferUsageFlags usage @@ -31,21 +42,12 @@ gnReturnCode VkCreateBuffer( VkMemoryRequirements bufferRequirements; vkGetBufferMemoryRequirements(device->outputDevice->device, buffer->buffer, &bufferRequirements); + gnBool foundMemory = gnFalse; VkMemoryAllocateInfo memoryAllocateInfo = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = bufferRequirements.size, + .memoryTypeIndex = VkMemoryIndex(device->physicalDevice.physicalDevice->device, bufferRequirements.memoryTypeBits, flags, &foundMemory) }; - - VkPhysicalDeviceMemoryProperties memoryProperties; - vkGetPhysicalDeviceMemoryProperties(device->physicalDevice.physicalDevice->device, &memoryProperties); - - gnBool foundMemory = gnFalse; - for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) { - if ((bufferRequirements.memoryTypeBits & (1 << i)) && (memoryProperties.memoryTypes[i].propertyFlags & flags) == flags) { - memoryAllocateInfo.memoryTypeIndex = i; - foundMemory = gnTrue; - } - } // this whole thing was adapted from vulkan-tutorial.com if (!foundMemory) return GN_FAILED_TO_ALLOCATE_MEMORY; if (vkAllocateMemory(device->outputDevice->device, &memoryAllocateInfo, NULL, &buffer->memory) != VK_SUCCESS) diff --git a/rendering_api/vulkan/src/buffers/vulkan_buffer.h b/rendering_api/vulkan/src/buffers/vulkan_buffer.h index 446756a..9febcbb 100644 --- a/rendering_api/vulkan/src/buffers/vulkan_buffer.h +++ b/rendering_api/vulkan/src/buffers/vulkan_buffer.h @@ -20,3 +20,4 @@ gnReturnCode VkCreateBuffer( VkGryphnBuffer* buffer, size_t size, gnDevice device, VkMemoryPropertyFlags flags, VkBufferUsageFlags usage ); +uint32_t VkMemoryIndex(VkPhysicalDevice device, uint32_t memoryType, VkMemoryPropertyFlags flags, gnBool* foundMemory); diff --git a/rendering_api/vulkan/src/textures/vulkan_texture.h b/rendering_api/vulkan/src/textures/vulkan_texture.h index e9fc944..aeffd1b 100644 --- a/rendering_api/vulkan/src/textures/vulkan_texture.h +++ b/rendering_api/vulkan/src/textures/vulkan_texture.h @@ -7,5 +7,6 @@ typedef struct gnPlatformTexture_t { VkGryphnBuffer buffer; VkImage image; + VkDeviceMemory memory; VkImageView imageView; } gnPlatformTexture; diff --git a/rendering_api/vulkan/src/textures/vulkan_texure.c b/rendering_api/vulkan/src/textures/vulkan_texure.c index 9ec106b..ac94957 100644 --- a/rendering_api/vulkan/src/textures/vulkan_texure.c +++ b/rendering_api/vulkan/src/textures/vulkan_texure.c @@ -1,6 +1,7 @@ #include "vulkan_surface/vulkan_surface.h" #include "vulkan_texture.h" #include "output_device/vulkan_output_devices.h" +#include "output_device/vulkan_physical_device.h" VkImageType vkGryphnTextureType(gnTextureType type) { switch(type) { @@ -32,5 +33,21 @@ gnReturnCode gnCreateTextureFn(gnTexture texture, gnDevice device, const gnTextu if (vkCreateImage(device->outputDevice->device, &imageInfo, NULL, &texture->texture->image) != VK_SUCCESS) return GN_FAILED_TO_CREATE_IMAGE; + VkMemoryRequirements memRequirements; + vkGetImageMemoryRequirements(device->outputDevice->device, texture->texture->image, &memRequirements); + + gnBool foundMemory = gnFalse; + VkMemoryAllocateInfo allocInfo = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = memRequirements.size, + .memoryTypeIndex = VkMemoryIndex(device->physicalDevice.physicalDevice->device, memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &foundMemory) + }; + if (!foundMemory) return GN_FAILED_TO_ALLOCATE_MEMORY; + + if (vkAllocateMemory(device->outputDevice->device, &allocInfo, NULL, &texture->texture->memory) != VK_SUCCESS) + return GN_FAILED_TO_ALLOCATE_MEMORY; + + vkBindImageMemory(device->outputDevice->device, texture->texture->image, texture->texture->memory, 0); + return GN_SUCCESS; } diff --git a/src/utils b/src/utils index 04a2f55..1331f60 160000 --- a/src/utils +++ b/src/utils @@ -1 +1 @@ -Subproject commit 04a2f55c4ce1732f37abc31dffa088de8b9fbc54 +Subproject commit 1331f60fc7f109b187c0a7c0abea3004bc177525