very bad texture API

This commit is contained in:
Greg Wells
2025-06-10 15:54:29 -04:00
parent 01b1e1fb7a
commit 9d1cbbf17a
5 changed files with 33 additions and 12 deletions

View File

@@ -14,6 +14,17 @@ VkBufferUsageFlags vkGryphnBufferType(gnBufferType type) {
return usageFlags; 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( gnReturnCode VkCreateBuffer(
VkGryphnBuffer* buffer, size_t size, gnDevice device, VkGryphnBuffer* buffer, size_t size, gnDevice device,
VkMemoryPropertyFlags flags, VkBufferUsageFlags usage VkMemoryPropertyFlags flags, VkBufferUsageFlags usage
@@ -31,21 +42,12 @@ gnReturnCode VkCreateBuffer(
VkMemoryRequirements bufferRequirements; VkMemoryRequirements bufferRequirements;
vkGetBufferMemoryRequirements(device->outputDevice->device, buffer->buffer, &bufferRequirements); vkGetBufferMemoryRequirements(device->outputDevice->device, buffer->buffer, &bufferRequirements);
gnBool foundMemory = gnFalse;
VkMemoryAllocateInfo memoryAllocateInfo = { VkMemoryAllocateInfo memoryAllocateInfo = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.allocationSize = bufferRequirements.size, .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 (!foundMemory) return GN_FAILED_TO_ALLOCATE_MEMORY;
if (vkAllocateMemory(device->outputDevice->device, &memoryAllocateInfo, NULL, &buffer->memory) != VK_SUCCESS) if (vkAllocateMemory(device->outputDevice->device, &memoryAllocateInfo, NULL, &buffer->memory) != VK_SUCCESS)

View File

@@ -20,3 +20,4 @@ gnReturnCode VkCreateBuffer(
VkGryphnBuffer* buffer, size_t size, gnDevice device, VkGryphnBuffer* buffer, size_t size, gnDevice device,
VkMemoryPropertyFlags flags, VkBufferUsageFlags usage VkMemoryPropertyFlags flags, VkBufferUsageFlags usage
); );
uint32_t VkMemoryIndex(VkPhysicalDevice device, uint32_t memoryType, VkMemoryPropertyFlags flags, gnBool* foundMemory);

View File

@@ -7,5 +7,6 @@ typedef struct gnPlatformTexture_t {
VkGryphnBuffer buffer; VkGryphnBuffer buffer;
VkImage image; VkImage image;
VkDeviceMemory memory;
VkImageView imageView; VkImageView imageView;
} gnPlatformTexture; } gnPlatformTexture;

View File

@@ -1,6 +1,7 @@
#include "vulkan_surface/vulkan_surface.h" #include "vulkan_surface/vulkan_surface.h"
#include "vulkan_texture.h" #include "vulkan_texture.h"
#include "output_device/vulkan_output_devices.h" #include "output_device/vulkan_output_devices.h"
#include "output_device/vulkan_physical_device.h"
VkImageType vkGryphnTextureType(gnTextureType type) { VkImageType vkGryphnTextureType(gnTextureType type) {
switch(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) if (vkCreateImage(device->outputDevice->device, &imageInfo, NULL, &texture->texture->image) != VK_SUCCESS)
return GN_FAILED_TO_CREATE_IMAGE; 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; return GN_SUCCESS;
} }