From 2634359b48408d3ed58d2de08d89c1e2de15d771 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 15 Jan 2024 16:36:56 -0800 Subject: [PATCH] Texture size calculation fixes --- src/Refresh_Driver_Vulkan.c | 107 ++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 60 deletions(-) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 0f71dc9..0e5e702 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -1991,59 +1991,51 @@ static inline uint32_t VULKAN_INTERNAL_BytesPerPixel(VkFormat format) { switch (format) { + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8_UINT: + return 1; + case VK_FORMAT_R5G6B5_UNORM_PACK16: + case VK_FORMAT_B4G4R4A4_UNORM_PACK16: + case VK_FORMAT_A1R5G5B5_UNORM_PACK16: + case VK_FORMAT_R16_SFLOAT: + case VK_FORMAT_R8G8_SNORM: + case VK_FORMAT_R8G8_UINT: + case VK_FORMAT_R16_UINT: + case VK_FORMAT_D16_UNORM: + return 2; + case VK_FORMAT_D16_UNORM_S8_UINT: + return 3; + case VK_FORMAT_R8G8B8A8_UNORM: + case VK_FORMAT_B8G8R8A8_UNORM: + case VK_FORMAT_R32_SFLOAT: + case VK_FORMAT_R16G16_UNORM: + case VK_FORMAT_R16G16_SFLOAT: + case VK_FORMAT_R8G8B8A8_SNORM: + case VK_FORMAT_A2R10G10B10_UNORM_PACK32: + case VK_FORMAT_R8G8B8A8_UINT: + case VK_FORMAT_R16G16_UINT: + case VK_FORMAT_D32_SFLOAT: + return 4; + case VK_FORMAT_D32_SFLOAT_S8_UINT: + return 5; + case VK_FORMAT_R16G16B16A16_SFLOAT: + case VK_FORMAT_R16G16B16A16_UNORM: + case VK_FORMAT_R32G32_SFLOAT: + case VK_FORMAT_R16G16B16A16_UINT: + case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: + return 8; case VK_FORMAT_R32G32B32A32_SFLOAT: case VK_FORMAT_BC2_UNORM_BLOCK: case VK_FORMAT_BC3_UNORM_BLOCK: case VK_FORMAT_BC7_UNORM_BLOCK: - case VK_FORMAT_R16G16B16A16_UINT: return 16; - - case VK_FORMAT_R8G8B8A8_UNORM: - case VK_FORMAT_R8G8B8A8_SNORM: - case VK_FORMAT_B8G8R8A8_UNORM: - case VK_FORMAT_B8G8R8A8_SNORM: - case VK_FORMAT_A2R10G10B10_UNORM_PACK32: - case VK_FORMAT_R16G16_UNORM: - case VK_FORMAT_R16G16_SFLOAT: - case VK_FORMAT_R32_SFLOAT: - case VK_FORMAT_D32_SFLOAT: - case VK_FORMAT_R8G8B8A8_UINT: - case VK_FORMAT_R16G16_UINT: - return 4; - - case VK_FORMAT_R5G6B5_UNORM_PACK16: - case VK_FORMAT_A1R5G5B5_UNORM_PACK16: - case VK_FORMAT_B4G4R4A4_UNORM_PACK16: - case VK_FORMAT_R8G8_SNORM: - case VK_FORMAT_R16_SFLOAT: - case VK_FORMAT_D16_UNORM: - case VK_FORMAT_R8G8_UINT: - case VK_FORMAT_R16_UINT: - return 2; - - case VK_FORMAT_R16G16B16A16_UNORM: - case VK_FORMAT_R32G32_SFLOAT: - case VK_FORMAT_R16G16B16A16_SFLOAT: - case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: - case VK_FORMAT_R8_UINT: - return 8; - - case VK_FORMAT_R8_UNORM: - return 1; - - case VK_FORMAT_D16_UNORM_S8_UINT: - return 3; - - case VK_FORMAT_D32_SFLOAT_S8_UINT: - return 5; - default: - Refresh_LogError("Invalid texture format!"); + Refresh_LogError("Texture format not recognized!"); return 0; } } -static inline uint32_t VULKAN_INTERNAL_GetTextureBlockSize( +static inline uint32_t VULKAN_INTERNAL_TextureBlockSize( VkFormat format ) { switch (format) @@ -2058,24 +2050,28 @@ static inline uint32_t VULKAN_INTERNAL_GetTextureBlockSize( case VK_FORMAT_R5G6B5_UNORM_PACK16: case VK_FORMAT_A1R5G5B5_UNORM_PACK16: case VK_FORMAT_B4G4R4A4_UNORM_PACK16: - case VK_FORMAT_R8G8_SNORM: - case VK_FORMAT_R8G8B8A8_SNORM: case VK_FORMAT_A2R10G10B10_UNORM_PACK32: case VK_FORMAT_R16G16_UNORM: case VK_FORMAT_R16G16B16A16_UNORM: case VK_FORMAT_R8_UNORM: - case VK_FORMAT_R32_SFLOAT: - case VK_FORMAT_R32G32_SFLOAT: - case VK_FORMAT_R32G32B32A32_SFLOAT: + case VK_FORMAT_R8G8_SNORM: + case VK_FORMAT_R8G8B8A8_SNORM: case VK_FORMAT_R16_SFLOAT: case VK_FORMAT_R16G16_SFLOAT: case VK_FORMAT_R16G16B16A16_SFLOAT: + case VK_FORMAT_R32_SFLOAT: + case VK_FORMAT_R32G32_SFLOAT: + case VK_FORMAT_R32G32B32A32_SFLOAT: case VK_FORMAT_R8_UINT: case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R16_UINT: case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16G16B16A16_UINT: + case VK_FORMAT_D16_UNORM: + case VK_FORMAT_D32_SFLOAT: + case VK_FORMAT_D16_UNORM_S8_UINT: + case VK_FORMAT_D32_SFLOAT_S8_UINT: return 1; default: Refresh_LogError("Unrecognized texture format!"); @@ -2088,17 +2084,8 @@ static inline VkDeviceSize VULKAN_INTERNAL_BytesPerImage( uint32_t height, VkFormat format ) { - uint32_t blocksPerRow = width; - uint32_t blocksPerColumn = height; - uint32_t blockSize = VULKAN_INTERNAL_GetTextureBlockSize(format); - - if (blockSize > 1) - { - blocksPerRow = (width + blockSize - 1) / blockSize; - blocksPerColumn = (height + blockSize - 1) / blockSize; - } - - return blocksPerRow * blocksPerColumn * VULKAN_INTERNAL_BytesPerPixel(format); + uint32_t blockSize = VULKAN_INTERNAL_TextureBlockSize(format); + return (width * height * VULKAN_INTERNAL_BytesPerPixel(format)) / (blockSize * blockSize); } static inline Refresh_SampleCount VULKAN_INTERNAL_GetMaxMultiSampleCount( @@ -7352,7 +7339,7 @@ static void VULKAN_SetTextureData( VulkanTransferBuffer *transferBuffer; VkBufferImageCopy imageCopy; uint8_t *stagingBufferPointer; - uint32_t blockSize = VULKAN_INTERNAL_GetTextureBlockSize(vulkanTexture->format); + uint32_t blockSize = VULKAN_INTERNAL_TextureBlockSize(vulkanTexture->format); uint32_t bufferRowLength; uint32_t bufferImageHeight;