Refresh/src/Refresh_Driver_Vulkan.c

9771 lines
272 KiB
C
Raw Normal View History

2020-12-17 00:27:14 +00:00
/* Refresh - XNA-inspired 3D Graphics Library with modern capabilities
*
* Copyright (c) 2020 Evan Hemsley
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Evan "cosmonaut" Hemsley <evan@moonside.games>
*
*/
#if REFRESH_DRIVER_VULKAN
#define VK_NO_PROTOTYPES
#include "vulkan/vulkan.h"
#include "Refresh_Driver.h"
#include <SDL.h>
#include <SDL_syswm.h>
#include <SDL_vulkan.h>
2020-12-17 02:38:22 +00:00
/* Global Vulkan Loader Entry Points */
static PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
#define VULKAN_GLOBAL_FUNCTION(name) \
static PFN_##name name = NULL;
#include "Refresh_Driver_Vulkan_vkfuncs.h"
/* vkInstance/vkDevice function typedefs */
#define VULKAN_INSTANCE_FUNCTION(ext, ret, func, params) \
typedef ret (VKAPI_CALL *vkfntype_##func) params;
#define VULKAN_DEVICE_FUNCTION(ext, ret, func, params) \
typedef ret (VKAPI_CALL *vkfntype_##func) params;
#include "Refresh_Driver_Vulkan_vkfuncs.h"
/* Required extensions */
static const char* deviceExtensionNames[] =
{
/* Globally supported */
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
/* Core since 1.1 */
VK_KHR_MAINTENANCE1_EXTENSION_NAME,
VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
/* Core since 1.2 */
VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME,
/* EXT, probably not going to be Core */
VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
};
static uint32_t deviceExtensionCount = SDL_arraysize(deviceExtensionNames);
2020-12-17 08:19:02 +00:00
/* Defines */
2020-12-19 04:08:07 +00:00
#define STARTING_ALLOCATION_SIZE 64000000 /* 64MB */
#define MAX_ALLOCATION_SIZE 256000000 /* 256MB */
#define TEXTURE_STAGING_SIZE 8000000 /* 8MB */
#define MAX_TEXTURE_STAGING_SIZE 128000000 /* 128MB */
2020-12-19 04:08:07 +00:00
#define UBO_BUFFER_SIZE 8000000 /* 8MB */
#define UBO_ACTUAL_SIZE (UBO_BUFFER_SIZE * 2)
2020-12-30 00:53:10 +00:00
#define DESCRIPTOR_POOL_STARTING_SIZE 128
2020-12-23 21:11:09 +00:00
#define UBO_POOL_SIZE 1000
2020-12-19 04:08:07 +00:00
#define SUB_BUFFER_COUNT 2
2020-12-27 23:34:15 +00:00
#define DESCRIPTOR_SET_DEACTIVATE_FRAMES 10
2020-12-18 22:35:33 +00:00
#define IDENTITY_SWIZZLE \
{ \
VK_COMPONENT_SWIZZLE_IDENTITY, \
VK_COMPONENT_SWIZZLE_IDENTITY, \
VK_COMPONENT_SWIZZLE_IDENTITY, \
VK_COMPONENT_SWIZZLE_IDENTITY \
}
#define NULL_DESC_LAYOUT (VkDescriptorSetLayout) 0
#define NULL_PIPELINE_LAYOUT (VkPipelineLayout) 0
2021-01-05 23:00:51 +00:00
#define NULL_RENDER_PASS (Refresh_RenderPass*) 0
2020-12-17 08:19:02 +00:00
2020-12-29 00:28:14 +00:00
#define EXPAND_ELEMENTS_IF_NEEDED(arr, initialValue, type) \
if (arr->count == arr->capacity) \
{ \
if (arr->capacity == 0) \
{ \
arr->capacity = initialValue; \
} \
else \
{ \
arr->capacity *= 2; \
} \
arr->elements = (type*) SDL_realloc( \
arr->elements, \
arr->capacity * sizeof(type) \
); \
}
2020-12-29 00:42:51 +00:00
#define EXPAND_ARRAY_IF_NEEDED(arr, elementType, newCount, capacity, newCapacity) \
if (newCount >= capacity) \
{ \
capacity = newCapacity; \
arr = (elementType*) SDL_realloc( \
arr, \
sizeof(elementType) * capacity \
); \
2020-12-29 00:28:14 +00:00
}
2020-12-29 00:56:49 +00:00
#define MOVE_ARRAY_CONTENTS_AND_RESET(i, dstArr, dstCount, srcArr, srcCount) \
for (i = 0; i < srcCount; i += 1) \
{ \
dstArr[i] = srcArr[i]; \
} \
dstCount = srcCount; \
srcCount = 0;
2020-12-17 03:28:02 +00:00
/* Enums */
typedef enum VulkanResourceAccessType
{
/* Reads */
RESOURCE_ACCESS_NONE, /* For initialization */
RESOURCE_ACCESS_INDEX_BUFFER,
RESOURCE_ACCESS_VERTEX_BUFFER,
RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER,
RESOURCE_ACCESS_VERTEX_SHADER_READ_SAMPLED_IMAGE,
RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER,
RESOURCE_ACCESS_FRAGMENT_SHADER_READ_SAMPLED_IMAGE,
RESOURCE_ACCESS_FRAGMENT_SHADER_READ_COLOR_ATTACHMENT,
RESOURCE_ACCESS_FRAGMENT_SHADER_READ_DEPTH_STENCIL_ATTACHMENT,
2020-12-31 04:39:47 +00:00
RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER,
2020-12-31 06:28:37 +00:00
RESOURCE_ACCESS_COMPUTE_SHADER_READ_OTHER,
RESOURCE_ACCESS_ANY_SHADER_READ_SAMPLED_IMAGE,
2020-12-17 03:28:02 +00:00
RESOURCE_ACCESS_COLOR_ATTACHMENT_READ,
RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ,
RESOURCE_ACCESS_TRANSFER_READ,
RESOURCE_ACCESS_HOST_READ,
RESOURCE_ACCESS_PRESENT,
RESOURCE_ACCESS_END_OF_READ,
/* Writes */
RESOURCE_ACCESS_VERTEX_SHADER_WRITE,
RESOURCE_ACCESS_FRAGMENT_SHADER_WRITE,
RESOURCE_ACCESS_COLOR_ATTACHMENT_WRITE,
RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE,
RESOURCE_ACCESS_TRANSFER_WRITE,
RESOURCE_ACCESS_HOST_WRITE,
/* Read-Writes */
RESOURCE_ACCESS_COLOR_ATTACHMENT_READ_WRITE,
RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_WRITE,
RESOURCE_ACCESS_MEMORY_TRANSFER_READ_WRITE,
RESOURCE_ACCESS_GENERAL,
/* Count */
RESOURCE_ACCESS_TYPES_COUNT
} VulkanResourceAccessType;
2020-12-17 03:50:31 +00:00
typedef enum CreateSwapchainResult
{
CREATE_SWAPCHAIN_FAIL,
CREATE_SWAPCHAIN_SUCCESS,
CREATE_SWAPCHAIN_SURFACE_ZERO,
} CreateSwapchainResult;
2020-12-17 08:19:02 +00:00
/* Conversions */
static VkFormat RefreshToVK_SurfaceFormat[] =
{
VK_FORMAT_R8G8B8A8_UNORM, /* R8G8B8A8 */
VK_FORMAT_R5G6B5_UNORM_PACK16, /* R5G6B5 */
VK_FORMAT_A1R5G5B5_UNORM_PACK16, /* A1R5G5B5 */
VK_FORMAT_B4G4R4A4_UNORM_PACK16, /* B4G4R4A4 */
VK_FORMAT_BC1_RGBA_UNORM_BLOCK, /* BC1 */
VK_FORMAT_BC2_UNORM_BLOCK, /* BC3 */
VK_FORMAT_BC3_UNORM_BLOCK, /* BC5 */
VK_FORMAT_R8G8_SNORM, /* R8G8_SNORM */
VK_FORMAT_R8G8B8A8_SNORM, /* R8G8B8A8_SNORM */
VK_FORMAT_A2R10G10B10_UNORM_PACK32, /* A2R10G10B10 */
VK_FORMAT_R16G16_UNORM, /* R16G16 */
VK_FORMAT_R16G16B16A16_UNORM, /* R16G16B16A16 */
VK_FORMAT_R8_UNORM, /* R8 */
VK_FORMAT_R32_SFLOAT, /* R32_SFLOAT */
VK_FORMAT_R32G32_SFLOAT, /* R32G32_SFLOAT */
VK_FORMAT_R32G32B32A32_SFLOAT, /* R32G32B32A32_SFLOAT */
VK_FORMAT_R16_SFLOAT, /* R16_SFLOAT */
VK_FORMAT_R16G16_SFLOAT, /* R16G16_SFLOAT */
VK_FORMAT_R16G16B16A16_SFLOAT /* R16G16B16A16_SFLOAT */
};
static VkFormat RefreshToVK_DepthFormat[] =
{
VK_FORMAT_D16_UNORM,
VK_FORMAT_D32_SFLOAT,
VK_FORMAT_D16_UNORM_S8_UINT,
VK_FORMAT_D32_SFLOAT_S8_UINT
};
2020-12-17 19:40:49 +00:00
static VkFormat RefreshToVK_VertexFormat[] =
{
VK_FORMAT_R32_SFLOAT, /* SINGLE */
VK_FORMAT_R32G32_SFLOAT, /* VECTOR2 */
VK_FORMAT_R32G32B32_SFLOAT, /* VECTOR3 */
VK_FORMAT_R32G32B32A32_SFLOAT, /* VECTOR4 */
VK_FORMAT_R8G8B8A8_UNORM, /* COLOR */
VK_FORMAT_R8G8B8A8_USCALED, /* BYTE4 */
VK_FORMAT_R16G16_SSCALED, /* SHORT2 */
VK_FORMAT_R16G16B16A16_SSCALED, /* SHORT4 */
VK_FORMAT_R16G16_SNORM, /* NORMALIZEDSHORT2 */
VK_FORMAT_R16G16B16A16_SNORM, /* NORMALIZEDSHORT4 */
VK_FORMAT_R16G16_SFLOAT, /* HALFVECTOR2 */
VK_FORMAT_R16G16B16A16_SFLOAT /* HALFVECTOR4 */
};
2020-12-20 07:31:55 +00:00
static VkIndexType RefreshToVK_IndexType[] =
{
VK_INDEX_TYPE_UINT16,
VK_INDEX_TYPE_UINT32
};
2020-12-17 19:40:49 +00:00
static VkPrimitiveTopology RefreshToVK_PrimitiveType[] =
{
VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
};
static VkPolygonMode RefreshToVK_PolygonMode[] =
{
VK_POLYGON_MODE_FILL,
VK_POLYGON_MODE_LINE,
VK_POLYGON_MODE_POINT
};
static VkCullModeFlags RefreshToVK_CullMode[] =
{
VK_CULL_MODE_NONE,
VK_CULL_MODE_FRONT_BIT,
VK_CULL_MODE_BACK_BIT,
VK_CULL_MODE_FRONT_AND_BACK
};
static VkFrontFace RefreshToVK_FrontFace[] =
{
VK_FRONT_FACE_COUNTER_CLOCKWISE,
VK_FRONT_FACE_CLOCKWISE
};
static VkBlendFactor RefreshToVK_BlendFactor[] =
{
VK_BLEND_FACTOR_ZERO,
VK_BLEND_FACTOR_ONE,
VK_BLEND_FACTOR_SRC_COLOR,
VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
VK_BLEND_FACTOR_DST_COLOR,
VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
VK_BLEND_FACTOR_SRC_ALPHA,
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
VK_BLEND_FACTOR_DST_ALPHA,
VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
VK_BLEND_FACTOR_CONSTANT_COLOR,
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
VK_BLEND_FACTOR_CONSTANT_ALPHA,
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,
VK_BLEND_FACTOR_SRC_ALPHA_SATURATE,
VK_BLEND_FACTOR_SRC1_COLOR,
VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR,
VK_BLEND_FACTOR_SRC1_ALPHA,
VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
};
static VkBlendOp RefreshToVK_BlendOp[] =
{
VK_BLEND_OP_ADD,
VK_BLEND_OP_SUBTRACT,
VK_BLEND_OP_REVERSE_SUBTRACT,
VK_BLEND_OP_MIN,
VK_BLEND_OP_MAX
};
static VkLogicOp RefreshToVK_LogicOp[] =
{
VK_LOGIC_OP_CLEAR,
VK_LOGIC_OP_AND,
VK_LOGIC_OP_AND_REVERSE,
VK_LOGIC_OP_COPY,
VK_LOGIC_OP_AND_INVERTED,
VK_LOGIC_OP_NO_OP,
VK_LOGIC_OP_XOR,
VK_LOGIC_OP_OR,
VK_LOGIC_OP_NOR,
VK_LOGIC_OP_EQUIVALENT,
VK_LOGIC_OP_INVERT,
VK_LOGIC_OP_OR_REVERSE,
VK_LOGIC_OP_COPY_INVERTED,
VK_LOGIC_OP_OR_INVERTED,
VK_LOGIC_OP_NAND,
VK_LOGIC_OP_SET
};
static VkCompareOp RefreshToVK_CompareOp[] =
{
VK_COMPARE_OP_NEVER,
VK_COMPARE_OP_LESS,
VK_COMPARE_OP_EQUAL,
VK_COMPARE_OP_LESS_OR_EQUAL,
VK_COMPARE_OP_GREATER,
VK_COMPARE_OP_NOT_EQUAL,
VK_COMPARE_OP_GREATER_OR_EQUAL,
VK_COMPARE_OP_ALWAYS
};
static VkStencilOp RefreshToVK_StencilOp[] =
{
VK_STENCIL_OP_KEEP,
VK_STENCIL_OP_ZERO,
VK_STENCIL_OP_REPLACE,
VK_STENCIL_OP_INCREMENT_AND_CLAMP,
VK_STENCIL_OP_DECREMENT_AND_CLAMP,
VK_STENCIL_OP_INVERT,
VK_STENCIL_OP_INCREMENT_AND_WRAP,
VK_STENCIL_OP_DECREMENT_AND_WRAP
};
2020-12-17 08:19:02 +00:00
static VkAttachmentLoadOp RefreshToVK_LoadOp[] =
{
VK_ATTACHMENT_LOAD_OP_LOAD,
VK_ATTACHMENT_LOAD_OP_CLEAR,
VK_ATTACHMENT_LOAD_OP_DONT_CARE
};
static VkAttachmentStoreOp RefreshToVK_StoreOp[] =
{
VK_ATTACHMENT_STORE_OP_STORE,
VK_ATTACHMENT_STORE_OP_DONT_CARE
};
static VkSampleCountFlagBits RefreshToVK_SampleCount[] =
{
VK_SAMPLE_COUNT_1_BIT,
VK_SAMPLE_COUNT_2_BIT,
VK_SAMPLE_COUNT_4_BIT,
VK_SAMPLE_COUNT_8_BIT,
VK_SAMPLE_COUNT_16_BIT,
VK_SAMPLE_COUNT_32_BIT,
VK_SAMPLE_COUNT_64_BIT
};
2020-12-17 19:40:49 +00:00
static VkVertexInputRate RefreshToVK_VertexInputRate[] =
{
VK_VERTEX_INPUT_RATE_VERTEX,
VK_VERTEX_INPUT_RATE_INSTANCE
};
2021-01-03 21:01:29 +00:00
static VkFilter RefreshToVK_Filter[] =
2020-12-18 01:48:26 +00:00
{
VK_FILTER_NEAREST,
2021-01-03 21:01:29 +00:00
VK_FILTER_LINEAR,
VK_FILTER_CUBIC_EXT
2020-12-18 01:48:26 +00:00
};
static VkSamplerMipmapMode RefreshToVK_SamplerMipmapMode[] =
{
VK_SAMPLER_MIPMAP_MODE_NEAREST,
VK_SAMPLER_MIPMAP_MODE_LINEAR
};
static VkSamplerAddressMode RefreshToVK_SamplerAddressMode[] =
{
VK_SAMPLER_ADDRESS_MODE_REPEAT,
VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
};
2020-12-18 20:58:03 +00:00
static VkBorderColor RefreshToVK_BorderColor[] =
{
VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
VK_BORDER_COLOR_INT_TRANSPARENT_BLACK,
VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
VK_BORDER_COLOR_INT_OPAQUE_BLACK,
VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
VK_BORDER_COLOR_INT_OPAQUE_WHITE
};
2020-12-17 03:28:02 +00:00
/* Structures */
2020-12-19 00:39:03 +00:00
/* Memory Allocation */
2020-12-18 22:35:33 +00:00
typedef struct VulkanMemoryAllocation VulkanMemoryAllocation;
typedef struct VulkanMemoryFreeRegion
{
VulkanMemoryAllocation *allocation;
VkDeviceSize offset;
VkDeviceSize size;
uint32_t allocationIndex;
uint32_t sortedIndex;
} VulkanMemoryFreeRegion;
typedef struct VulkanMemorySubAllocator
{
VkDeviceSize nextAllocationSize;
VulkanMemoryAllocation **allocations;
uint32_t allocationCount;
VulkanMemoryFreeRegion **sortedFreeRegions;
uint32_t sortedFreeRegionCount;
uint32_t sortedFreeRegionCapacity;
} VulkanMemorySubAllocator;
struct VulkanMemoryAllocation
{
VulkanMemorySubAllocator *allocator;
VkDeviceMemory memory;
VkDeviceSize size;
VulkanMemoryFreeRegion **freeRegions;
uint32_t freeRegionCount;
uint32_t freeRegionCapacity;
uint8_t dedicated;
2021-01-14 02:02:45 +00:00
uint8_t *mapPointer;
2021-01-03 21:12:12 +00:00
SDL_mutex *memoryLock;
2020-12-18 22:35:33 +00:00
};
typedef struct VulkanMemoryAllocator
{
VulkanMemorySubAllocator subAllocators[VK_MAX_MEMORY_TYPES];
} VulkanMemoryAllocator;
2020-12-19 00:39:03 +00:00
/* Memory Barriers */
typedef struct VulkanResourceAccessInfo
{
VkPipelineStageFlags stageMask;
VkAccessFlags accessMask;
VkImageLayout imageLayout;
} VulkanResourceAccessInfo;
static const VulkanResourceAccessInfo AccessMap[RESOURCE_ACCESS_TYPES_COUNT] =
{
/* RESOURCE_ACCESS_NONE */
{
0,
0,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_INDEX_BUFFER */
{
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
VK_ACCESS_INDEX_READ_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_VERTEX_BUFFER */
{
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
VK_ACCESS_INDEX_READ_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER */
{
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_VERTEX_SHADER_READ_SAMPLED_IMAGE */
{
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
},
/* RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER */
{
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_ACCESS_UNIFORM_READ_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_FRAGMENT_SHADER_READ_SAMPLED_IMAGE */
{
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
},
/* RESOURCE_ACCESS_FRAGMENT_SHADER_READ_COLOR_ATTACHMENT */
{
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
},
/* RESOURCE_ACCESS_FRAGMENT_SHADER_READ_DEPTH_STENCIL_ATTACHMENT */
{
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
},
2020-12-31 04:39:47 +00:00
/* RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER */
{
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_ACCESS_UNIFORM_READ_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
2020-12-31 06:28:37 +00:00
/* RESOURCE_ACCESS_COMPUTE_SHADER_READ_OTHER */
{
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_ANY_SHADER_READ_SAMPLED_IMAGE */
{
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
},
2020-12-19 00:39:03 +00:00
/* RESOURCE_ACCESS_COLOR_ATTACHMENT_READ */
{
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
},
/* RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ */
{
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
},
/* RESOURCE_ACCESS_TRANSFER_READ */
{
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_TRANSFER_READ_BIT,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
},
/* RESOURCE_ACCESS_HOST_READ */
{
VK_PIPELINE_STAGE_HOST_BIT,
VK_ACCESS_HOST_READ_BIT,
VK_IMAGE_LAYOUT_GENERAL
},
/* RESOURCE_ACCESS_PRESENT */
{
0,
0,
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
},
/* RESOURCE_ACCESS_END_OF_READ */
{
0,
0,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_VERTEX_SHADER_WRITE */
{
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
VK_ACCESS_SHADER_WRITE_BIT,
VK_IMAGE_LAYOUT_GENERAL
},
/* RESOURCE_ACCESS_FRAGMENT_SHADER_WRITE */
{
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_ACCESS_SHADER_WRITE_BIT,
VK_IMAGE_LAYOUT_GENERAL
},
/* RESOURCE_ACCESS_COLOR_ATTACHMENT_WRITE */
{
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
},
/* RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE */
{
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
},
/* RESOURCE_ACCESS_TRANSFER_WRITE */
{
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
},
/* RESOURCE_ACCESS_HOST_WRITE */
{
VK_PIPELINE_STAGE_HOST_BIT,
VK_ACCESS_HOST_WRITE_BIT,
VK_IMAGE_LAYOUT_GENERAL
},
/* RESOURCE_ACCESS_COLOR_ATTACHMENT_READ_WRITE */
{
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
},
/* RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_WRITE */
{
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
},
/* RESOURCE_ACCESS_MEMORY_TRANSFER_READ_WRITE */
{
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
VK_IMAGE_LAYOUT_UNDEFINED
},
/* RESOURCE_ACCESS_GENERAL */
{
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
VK_IMAGE_LAYOUT_GENERAL
}
};
/* Memory structures */
2020-12-19 04:08:07 +00:00
typedef struct VulkanBuffer VulkanBuffer;
typedef struct VulkanSubBuffer
2020-12-19 01:03:26 +00:00
{
VulkanMemoryAllocation *allocation;
VkBuffer buffer;
VkDeviceSize offset;
VkDeviceSize size;
VulkanResourceAccessType resourceAccessType;
int8_t bound;
2020-12-19 04:08:07 +00:00
} VulkanSubBuffer;
/*
* Our VulkanBuffer is actually a series of sub-buffers
* so we can properly support updates while a frame is in flight
* without needing a sync point
*/
struct VulkanBuffer /* cast from FNA3D_Buffer */
{
VkDeviceSize size;
VulkanSubBuffer **subBuffers;
2020-12-21 20:37:54 +00:00
uint32_t subBufferCount;
2020-12-23 06:05:01 +00:00
uint32_t currentSubBufferIndex;
2020-12-19 04:08:07 +00:00
VulkanResourceAccessType resourceAccessType;
VkBufferUsageFlags usage;
uint8_t bound;
uint8_t boundSubmitted;
};
2020-12-19 01:03:26 +00:00
2020-12-19 00:39:03 +00:00
/* Renderer Structure */
2020-12-17 02:38:22 +00:00
typedef struct QueueFamilyIndices
{
uint32_t graphicsFamily;
uint32_t presentFamily;
2021-01-03 03:03:25 +00:00
uint32_t computeFamily;
2021-01-02 21:31:17 +00:00
uint32_t transferFamily;
2020-12-17 02:38:22 +00:00
} QueueFamilyIndices;
2020-12-17 03:28:02 +00:00
typedef struct SwapChainSupportDetails
{
VkSurfaceCapabilitiesKHR capabilities;
VkSurfaceFormatKHR *formats;
uint32_t formatsLength;
VkPresentModeKHR *presentModes;
uint32_t presentModesLength;
} SwapChainSupportDetails;
2020-12-30 00:53:10 +00:00
typedef struct BufferDescriptorSetCache BufferDescriptorSetCache;
2020-12-30 00:19:19 +00:00
typedef struct ImageDescriptorSetCache ImageDescriptorSetCache;
2020-12-27 23:20:59 +00:00
typedef struct VulkanGraphicsPipelineLayout
2020-12-23 06:56:26 +00:00
{
VkPipelineLayout pipelineLayout;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetCache *vertexSamplerDescriptorSetCache;
ImageDescriptorSetCache *fragmentSamplerDescriptorSetCache;
} VulkanGraphicsPipelineLayout;
typedef struct VulkanGraphicsPipeline
{
VkPipeline pipeline;
VulkanGraphicsPipelineLayout *pipelineLayout;
2021-01-05 23:00:51 +00:00
Refresh_PrimitiveType primitiveType;
2021-01-06 01:00:06 +00:00
VkDescriptorSet vertexSamplerDescriptorSet; /* updated by BindVertexSamplers */
VkDescriptorSet fragmentSamplerDescriptorSet; /* updated by BindFragmentSamplers */
2020-12-23 21:11:09 +00:00
VkDescriptorSet vertexUBODescriptorSet; /* permanently set in Create function */
VkDescriptorSet fragmentUBODescriptorSet; /* permanently set in Create function */
VkDeviceSize vertexUBOBlockSize; /* permanently set in Create function */
VkDeviceSize fragmentUBOBlockSize; /* permantenly set in Create function */
2020-12-23 06:56:26 +00:00
} VulkanGraphicsPipeline;
2020-12-29 22:52:24 +00:00
typedef struct VulkanComputePipelineLayout
{
VkPipelineLayout pipelineLayout;
2020-12-30 00:53:10 +00:00
BufferDescriptorSetCache *bufferDescriptorSetCache;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetCache *imageDescriptorSetCache;
2020-12-29 22:52:24 +00:00
} VulkanComputePipelineLayout;
typedef struct VulkanComputePipeline
{
VkPipeline pipeline;
VulkanComputePipelineLayout *pipelineLayout;
2020-12-31 00:47:13 +00:00
VkDescriptorSet bufferDescriptorSet; /* updated by BindComputeBuffers */
VkDescriptorSet imageDescriptorSet; /* updated by BindComputeTextures */
2020-12-31 04:39:47 +00:00
VkDescriptorSet computeUBODescriptorSet; /* permanently set in Create function */
VkDeviceSize computeUBOBlockSize; /* permanently set in Create function */
2020-12-29 22:52:24 +00:00
} VulkanComputePipeline;
2020-12-23 07:17:09 +00:00
typedef struct VulkanTexture
{
VulkanMemoryAllocation *allocation;
VkDeviceSize offset;
VkDeviceSize memorySize;
VkImage image;
VkImageView view;
VkExtent2D dimensions;
2021-01-03 22:37:02 +00:00
uint8_t is3D;
uint8_t isCube;
2020-12-23 07:17:09 +00:00
uint32_t depth;
uint32_t layerCount;
uint32_t levelCount;
VkFormat format;
2021-01-05 23:00:51 +00:00
Refresh_ColorFormat refreshFormat;
2020-12-23 07:17:09 +00:00
VulkanResourceAccessType resourceAccessType;
2021-01-02 21:31:17 +00:00
uint32_t queueFamilyIndex;
2021-01-05 23:00:51 +00:00
Refresh_TextureUsageFlags usageFlags;
2020-12-29 07:41:59 +00:00
REFRESHNAMELESS union
{
2021-01-05 23:00:51 +00:00
Refresh_ColorFormat colorFormat;
Refresh_DepthFormat depthStencilFormat;
2020-12-29 07:41:59 +00:00
};
2020-12-23 07:17:09 +00:00
} VulkanTexture;
typedef struct VulkanColorTarget
{
VulkanTexture *texture;
uint32_t layer;
VkImageView view;
VulkanTexture *multisampleTexture;
VkSampleCountFlags multisampleCount;
} VulkanColorTarget;
typedef struct VulkanDepthStencilTarget
{
2020-12-29 04:09:31 +00:00
VulkanTexture *texture;
2020-12-23 07:17:09 +00:00
VkImageView view;
} VulkanDepthStencilTarget;
typedef struct VulkanFramebuffer
{
VkFramebuffer framebuffer;
VulkanColorTarget *colorTargets[MAX_COLOR_TARGET_BINDINGS];
uint32_t colorTargetCount;
VulkanDepthStencilTarget *depthStencilTarget;
2020-12-29 06:19:46 +00:00
uint32_t width;
uint32_t height;
2020-12-23 07:17:09 +00:00
} VulkanFramebuffer;
/* Cache structures */
2020-12-29 22:52:24 +00:00
/* Descriptor Set Layout Caches*/
#define NUM_DESCRIPTOR_SET_LAYOUT_BUCKETS 1031
2020-12-29 23:05:26 +00:00
typedef struct DescriptorSetLayoutHash
{
2020-12-29 23:05:26 +00:00
VkDescriptorType descriptorType;
uint32_t bindingCount;
VkShaderStageFlagBits stageFlag;
2020-12-29 23:05:26 +00:00
} DescriptorSetLayoutHash;
2020-12-29 23:05:26 +00:00
typedef struct DescriptorSetLayoutHashMap
{
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHash key;
VkDescriptorSetLayout value;
2020-12-29 23:05:26 +00:00
} DescriptorSetLayoutHashMap;
2020-12-29 23:05:26 +00:00
typedef struct DescriptorSetLayoutHashArray
{
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHashMap *elements;
int32_t count;
int32_t capacity;
2020-12-29 23:05:26 +00:00
} DescriptorSetLayoutHashArray;
2020-12-29 23:05:26 +00:00
typedef struct DescriptorSetLayoutHashTable
{
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHashArray buckets[NUM_DESCRIPTOR_SET_LAYOUT_BUCKETS];
} DescriptorSetLayoutHashTable;
2020-12-29 23:05:26 +00:00
static inline uint64_t DescriptorSetLayoutHashTable_GetHashCode(DescriptorSetLayoutHash key)
{
const uint64_t HASH_FACTOR = 97;
uint64_t result = 1;
result = result * HASH_FACTOR + key.descriptorType;
2020-12-29 23:05:26 +00:00
result = result * HASH_FACTOR + key.bindingCount;
result = result * HASH_FACTOR + key.stageFlag;
return result;
}
2020-12-29 23:05:26 +00:00
static inline VkDescriptorSetLayout DescriptorSetLayoutHashTable_Fetch(
DescriptorSetLayoutHashTable *table,
DescriptorSetLayoutHash key
) {
int32_t i;
2020-12-29 23:05:26 +00:00
uint64_t hashcode = DescriptorSetLayoutHashTable_GetHashCode(key);
DescriptorSetLayoutHashArray *arr = &table->buckets[hashcode % NUM_DESCRIPTOR_SET_LAYOUT_BUCKETS];
for (i = 0; i < arr->count; i += 1)
{
2020-12-29 23:05:26 +00:00
const DescriptorSetLayoutHash *e = &arr->elements[i].key;
if ( key.descriptorType == e->descriptorType &&
2020-12-29 23:05:26 +00:00
key.bindingCount == e->bindingCount &&
key.stageFlag == e->stageFlag )
{
return arr->elements[i].value;
}
}
return VK_NULL_HANDLE;
}
2020-12-29 23:05:26 +00:00
static inline void DescriptorSetLayoutHashTable_Insert(
DescriptorSetLayoutHashTable *table,
DescriptorSetLayoutHash key,
VkDescriptorSetLayout value
) {
2020-12-29 23:05:26 +00:00
uint64_t hashcode = DescriptorSetLayoutHashTable_GetHashCode(key);
DescriptorSetLayoutHashArray *arr = &table->buckets[hashcode % NUM_DESCRIPTOR_SET_LAYOUT_BUCKETS];
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHashMap map;
map.key = key;
map.value = value;
2020-12-29 23:05:26 +00:00
EXPAND_ELEMENTS_IF_NEEDED(arr, 4, DescriptorSetLayoutHashMap);
arr->elements[arr->count] = map;
arr->count += 1;
}
2020-12-29 22:52:24 +00:00
/* Descriptor Set Caches */
2020-12-30 00:53:10 +00:00
#define NUM_DESCRIPTOR_SET_HASH_BUCKETS 1031
2020-12-30 00:19:19 +00:00
typedef struct ImageDescriptorSetData
2020-12-27 23:20:59 +00:00
{
VkDescriptorImageInfo descriptorImageInfo[MAX_TEXTURE_SAMPLERS]; /* used for vertex samplers as well */
2020-12-30 00:19:19 +00:00
} ImageDescriptorSetData;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
typedef struct ImageDescriptorSetHashMap
2020-12-27 23:20:59 +00:00
{
uint64_t key;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetData descriptorSetData;
2020-12-27 23:20:59 +00:00
VkDescriptorSet descriptorSet;
uint8_t inactiveFrameCount;
2020-12-30 00:19:19 +00:00
} ImageDescriptorSetHashMap;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
typedef struct ImageDescriptorSetHashArray
2020-12-27 23:20:59 +00:00
{
uint32_t *elements;
int32_t count;
int32_t capacity;
2020-12-30 00:19:19 +00:00
} ImageDescriptorSetHashArray;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
static inline uint64_t ImageDescriptorSetHashTable_GetHashCode(
ImageDescriptorSetData *descriptorSetData,
2020-12-27 23:20:59 +00:00
uint32_t samplerCount
) {
const uint64_t HASH_FACTOR = 97;
uint32_t i;
uint64_t result = 1;
2020-12-30 00:53:10 +00:00
for (i = 0; i < samplerCount; i += 1)
2020-12-27 23:20:59 +00:00
{
result = result * HASH_FACTOR + (uint64_t) descriptorSetData->descriptorImageInfo[i].imageView;
result = result * HASH_FACTOR + (uint64_t) descriptorSetData->descriptorImageInfo[i].sampler;
}
return result;
}
2020-12-30 00:19:19 +00:00
struct ImageDescriptorSetCache
2020-12-27 23:20:59 +00:00
{
VkDescriptorSetLayout descriptorSetLayout;
2020-12-30 00:19:19 +00:00
uint32_t bindingCount;
2020-12-31 00:47:13 +00:00
VkDescriptorType descriptorType;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
ImageDescriptorSetHashArray buckets[NUM_DESCRIPTOR_SET_HASH_BUCKETS]; /* these buckets store indices */
ImageDescriptorSetHashMap *elements; /* where the hash map elements are stored */
2020-12-27 23:20:59 +00:00
uint32_t count;
uint32_t capacity;
2020-12-30 00:19:19 +00:00
VkDescriptorPool *imageDescriptorPools;
uint32_t imageDescriptorPoolCount;
2020-12-27 23:20:59 +00:00
uint32_t nextPoolSize;
VkDescriptorSet *inactiveDescriptorSets;
uint32_t inactiveDescriptorSetCount;
uint32_t inactiveDescriptorSetCapacity;
};
2020-12-30 00:53:10 +00:00
typedef struct BufferDescriptorSetData
{
VkDescriptorBufferInfo descriptorBufferInfo[MAX_BUFFER_BINDINGS];
} BufferDescriptorSetData;
typedef struct BufferDescriptorSetHashMap
{
uint64_t key;
BufferDescriptorSetData descriptorSetData;
VkDescriptorSet descriptorSet;
uint8_t inactiveFrameCount;
} BufferDescriptorSetHashMap;
typedef struct BufferDescriptorSetHashArray
{
uint32_t *elements;
int32_t count;
int32_t capacity;
} BufferDescriptorSetHashArray;
static inline uint64_t BufferDescriptorSetHashTable_GetHashCode(
BufferDescriptorSetData *descriptorSetData,
uint32_t bindingCount
) {
const uint64_t HASH_FACTOR = 97;
uint32_t i;
uint64_t result = 1;
for (i = 0; i < bindingCount; i += 1)
{
result = result * HASH_FACTOR + (uint64_t) descriptorSetData->descriptorBufferInfo[i].buffer;
result = result * HASH_FACTOR + (uint64_t) descriptorSetData->descriptorBufferInfo[i].offset;
result = result * HASH_FACTOR + (uint64_t) descriptorSetData->descriptorBufferInfo[i].range;
}
return result;
}
struct BufferDescriptorSetCache
{
VkDescriptorSetLayout descriptorSetLayout;
uint32_t bindingCount;
2020-12-31 04:39:47 +00:00
VkDescriptorType descriptorType;
2020-12-30 00:53:10 +00:00
BufferDescriptorSetHashArray buckets[NUM_DESCRIPTOR_SET_HASH_BUCKETS];
BufferDescriptorSetHashMap *elements;
uint32_t count;
uint32_t capacity;
VkDescriptorPool *bufferDescriptorPools;
uint32_t bufferDescriptorPoolCount;
uint32_t nextPoolSize;
VkDescriptorSet *inactiveDescriptorSets;
uint32_t inactiveDescriptorSetCount;
uint32_t inactiveDescriptorSetCapacity;
};
2020-12-29 22:52:24 +00:00
/* Pipeline Caches */
2021-01-03 02:02:20 +00:00
#define NUM_PIPELINE_LAYOUT_BUCKETS 1031
2020-12-29 22:52:24 +00:00
typedef struct GraphicsPipelineLayoutHash
{
VkDescriptorSetLayout vertexSamplerLayout;
VkDescriptorSetLayout fragmentSamplerLayout;
VkDescriptorSetLayout vertexUniformLayout;
VkDescriptorSetLayout fragmentUniformLayout;
} GraphicsPipelineLayoutHash;
typedef struct GraphicsPipelineLayoutHashMap
{
GraphicsPipelineLayoutHash key;
VulkanGraphicsPipelineLayout *value;
} GraphicsPipelineLayoutHashMap;
typedef struct GraphicsPipelineLayoutHashArray
{
GraphicsPipelineLayoutHashMap *elements;
int32_t count;
int32_t capacity;
} GraphicsPipelineLayoutHashArray;
typedef struct GraphicsPipelineLayoutHashTable
{
GraphicsPipelineLayoutHashArray buckets[NUM_PIPELINE_LAYOUT_BUCKETS];
} GraphicsPipelineLayoutHashTable;
static inline uint64_t GraphicsPipelineLayoutHashTable_GetHashCode(GraphicsPipelineLayoutHash key)
{
const uint64_t HASH_FACTOR = 97;
uint64_t result = 1;
result = result * HASH_FACTOR + (uint64_t) key.vertexSamplerLayout;
result = result * HASH_FACTOR + (uint64_t) key.fragmentSamplerLayout;
result = result * HASH_FACTOR + (uint64_t) key.vertexUniformLayout;
result = result * HASH_FACTOR + (uint64_t) key.fragmentUniformLayout;
return result;
}
static inline VulkanGraphicsPipelineLayout* GraphicsPipelineLayoutHashArray_Fetch(
GraphicsPipelineLayoutHashTable *table,
GraphicsPipelineLayoutHash key
) {
int32_t i;
uint64_t hashcode = GraphicsPipelineLayoutHashTable_GetHashCode(key);
GraphicsPipelineLayoutHashArray *arr = &table->buckets[hashcode % NUM_PIPELINE_LAYOUT_BUCKETS];
for (i = 0; i < arr->count; i += 1)
{
const GraphicsPipelineLayoutHash *e = &arr->elements[i].key;
if ( key.vertexSamplerLayout == e->vertexSamplerLayout &&
key.fragmentSamplerLayout == e->fragmentSamplerLayout &&
key.vertexUniformLayout == e->vertexUniformLayout &&
key.fragmentUniformLayout == e->fragmentUniformLayout )
{
return arr->elements[i].value;
}
}
return NULL;
}
static inline void GraphicsPipelineLayoutHashArray_Insert(
GraphicsPipelineLayoutHashTable *table,
GraphicsPipelineLayoutHash key,
VulkanGraphicsPipelineLayout *value
) {
uint64_t hashcode = GraphicsPipelineLayoutHashTable_GetHashCode(key);
GraphicsPipelineLayoutHashArray *arr = &table->buckets[hashcode % NUM_PIPELINE_LAYOUT_BUCKETS];
GraphicsPipelineLayoutHashMap map;
map.key = key;
map.value = value;
EXPAND_ELEMENTS_IF_NEEDED(arr, 4, GraphicsPipelineLayoutHashMap)
arr->elements[arr->count] = map;
arr->count += 1;
}
typedef struct ComputePipelineLayoutHash
{
VkDescriptorSetLayout bufferLayout;
VkDescriptorSetLayout imageLayout;
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayout uniformLayout;
2020-12-29 22:52:24 +00:00
} ComputePipelineLayoutHash;
typedef struct ComputePipelineLayoutHashMap
{
ComputePipelineLayoutHash key;
VulkanComputePipelineLayout *value;
} ComputePipelineLayoutHashMap;
typedef struct ComputePipelineLayoutHashArray
{
ComputePipelineLayoutHashMap *elements;
int32_t count;
int32_t capacity;
} ComputePipelineLayoutHashArray;
typedef struct ComputePipelineLayoutHashTable
{
ComputePipelineLayoutHashArray buckets[NUM_PIPELINE_LAYOUT_BUCKETS];
} ComputePipelineLayoutHashTable;
static inline uint64_t ComputePipelineLayoutHashTable_GetHashCode(ComputePipelineLayoutHash key)
{
const uint64_t HASH_FACTOR = 97;
uint64_t result = 1;
result = result * HASH_FACTOR + (uint64_t) key.bufferLayout;
result = result * HASH_FACTOR + (uint64_t) key.imageLayout;
2020-12-31 04:39:47 +00:00
result = result * HASH_FACTOR + (uint64_t) key.uniformLayout;
2020-12-29 22:52:24 +00:00
return result;
}
static inline VulkanComputePipelineLayout* ComputePipelineLayoutHashArray_Fetch(
ComputePipelineLayoutHashTable *table,
ComputePipelineLayoutHash key
) {
int32_t i;
uint64_t hashcode = ComputePipelineLayoutHashTable_GetHashCode(key);
ComputePipelineLayoutHashArray *arr = &table->buckets[hashcode % NUM_PIPELINE_LAYOUT_BUCKETS];
for (i = 0; i < arr->count; i += 1)
{
const ComputePipelineLayoutHash *e = &arr->elements[i].key;
if ( key.bufferLayout == e->bufferLayout &&
2020-12-31 04:39:47 +00:00
key.imageLayout == e->imageLayout &&
key.uniformLayout == e->uniformLayout )
2020-12-29 22:52:24 +00:00
{
return arr->elements[i].value;
}
}
return NULL;
}
static inline void ComputePipelineLayoutHashArray_Insert(
ComputePipelineLayoutHashTable *table,
ComputePipelineLayoutHash key,
VulkanComputePipelineLayout *value
) {
uint64_t hashcode = ComputePipelineLayoutHashTable_GetHashCode(key);
ComputePipelineLayoutHashArray *arr = &table->buckets[hashcode % NUM_PIPELINE_LAYOUT_BUCKETS];
ComputePipelineLayoutHashMap map;
map.key = key;
map.value = value;
EXPAND_ELEMENTS_IF_NEEDED(arr, 4, ComputePipelineLayoutHashMap)
arr->elements[arr->count] = map;
arr->count += 1;
}
2021-01-03 02:02:20 +00:00
/* Command structures */
2021-01-02 06:07:15 +00:00
typedef struct VulkanCommandPool VulkanCommandPool;
typedef struct VulkanCommandBuffer
{
VkCommandBuffer commandBuffer;
uint8_t fixed;
uint8_t submitted;
VulkanCommandPool *commandPool;
VulkanComputePipeline *currentComputePipeline;
VulkanGraphicsPipeline *currentGraphicsPipeline;
VulkanFramebuffer *currentFramebuffer;
VulkanBuffer *boundComputeBuffers[MAX_BUFFER_BINDINGS];
uint32_t boundComputeBufferCount;
} VulkanCommandBuffer;
struct VulkanCommandPool
{
SDL_threadID threadID;
VkCommandPool commandPool;
VulkanCommandBuffer **inactiveCommandBuffers;
uint32_t inactiveCommandBufferCapacity;
uint32_t inactiveCommandBufferCount;
};
2021-01-03 02:02:20 +00:00
#define NUM_COMMAND_POOL_BUCKETS 1031
typedef struct CommandPoolHash
{
SDL_threadID threadID;
} CommandPoolHash;
typedef struct CommandPoolHashMap
{
CommandPoolHash key;
VulkanCommandPool *value;
} CommandPoolHashMap;
typedef struct CommandPoolHashArray
{
CommandPoolHashMap *elements;
uint32_t count;
uint32_t capacity;
} CommandPoolHashArray;
typedef struct CommandPoolHashTable
{
CommandPoolHashArray buckets[NUM_COMMAND_POOL_BUCKETS];
} CommandPoolHashTable;
static inline uint64_t CommandPoolHashTable_GetHashCode(CommandPoolHash key)
{
const uint64_t HASH_FACTOR = 97;
uint64_t result = 1;
result = result * HASH_FACTOR + (uint64_t) key.threadID;
return result;
}
static inline VulkanCommandPool* CommandPoolHashTable_Fetch(
CommandPoolHashTable *table,
CommandPoolHash key
) {
uint32_t i;
uint64_t hashcode = CommandPoolHashTable_GetHashCode(key);
CommandPoolHashArray *arr = &table->buckets[hashcode % NUM_COMMAND_POOL_BUCKETS];
for (i = 0; i < arr->count; i += 1)
{
const CommandPoolHash *e = &arr->elements[i].key;
if (key.threadID == e->threadID)
{
return arr->elements[i].value;
}
}
return NULL;
}
static inline void CommandPoolHashTable_Insert(
CommandPoolHashTable *table,
CommandPoolHash key,
VulkanCommandPool *value
) {
uint64_t hashcode = CommandPoolHashTable_GetHashCode(key);
CommandPoolHashArray *arr = &table->buckets[hashcode % NUM_COMMAND_POOL_BUCKETS];
CommandPoolHashMap map;
map.key = key;
map.value = value;
EXPAND_ELEMENTS_IF_NEEDED(arr, 4, CommandPoolHashMap)
arr->elements[arr->count] = map;
arr->count += 1;
}
/* Context */
2020-12-17 03:28:02 +00:00
typedef struct VulkanRenderer
2020-12-17 01:23:49 +00:00
{
2020-12-17 02:12:20 +00:00
VkInstance instance;
2020-12-17 02:38:22 +00:00
VkPhysicalDevice physicalDevice;
VkPhysicalDeviceProperties2 physicalDeviceProperties;
VkPhysicalDeviceDriverPropertiesKHR physicalDeviceDriverProperties;
VkDevice logicalDevice;
void* deviceWindowHandle;
2020-12-17 03:28:02 +00:00
uint8_t supportsDebugUtils;
uint8_t debugMode;
2020-12-17 03:50:31 +00:00
uint8_t headless;
2020-12-17 03:28:02 +00:00
2020-12-18 22:35:33 +00:00
VulkanMemoryAllocator *memoryAllocator;
2021-01-05 23:00:51 +00:00
Refresh_PresentMode presentMode;
2020-12-17 03:28:02 +00:00
VkSurfaceKHR surface;
2020-12-17 03:50:31 +00:00
VkSwapchainKHR swapChain;
VkFormat swapChainFormat;
VkComponentMapping swapChainSwizzle;
VkImage *swapChainImages;
VkImageView *swapChainImageViews;
2021-01-03 03:03:25 +00:00
uint32_t *swapChainQueueFamilyIndices;
2020-12-17 03:28:02 +00:00
VulkanResourceAccessType *swapChainResourceAccessTypes;
2020-12-17 03:50:31 +00:00
uint32_t swapChainImageCount;
VkExtent2D swapChainExtent;
2020-12-22 00:18:21 +00:00
2020-12-21 23:44:43 +00:00
uint8_t needNewSwapChain;
uint8_t shouldPresent;
uint8_t swapChainImageAcquired;
uint32_t currentSwapChainIndex;
2020-12-17 03:28:02 +00:00
2020-12-17 02:38:22 +00:00
QueueFamilyIndices queueFamilyIndices;
VkQueue graphicsQueue;
VkQueue presentQueue;
2021-01-03 03:03:25 +00:00
VkQueue computeQueue;
2021-01-02 21:31:17 +00:00
VkQueue transferQueue;
2020-12-17 02:38:22 +00:00
2020-12-17 04:04:47 +00:00
VkFence inFlightFence;
2021-01-02 21:31:17 +00:00
VkSemaphore transferFinishedSemaphore;
2020-12-17 04:04:47 +00:00
VkSemaphore imageAvailableSemaphore;
VkSemaphore renderFinishedSemaphore;
2021-01-02 21:31:17 +00:00
VkCommandPool transferCommandPool;
VkCommandBuffer transferCommandBuffers[2]; /* frame count */
uint8_t pendingTransfer;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer **submittedCommandBuffers;
2020-12-17 02:38:22 +00:00
uint32_t submittedCommandBufferCount;
2021-01-02 06:07:15 +00:00
uint32_t submittedCommandBufferCapacity;
2020-12-31 06:28:37 +00:00
2021-01-03 02:02:20 +00:00
CommandPoolHashTable commandPoolHashTable;
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHashTable descriptorSetLayoutHashTable;
2020-12-29 22:52:24 +00:00
GraphicsPipelineLayoutHashTable graphicsPipelineLayoutHashTable;
ComputePipelineLayoutHashTable computePipelineLayoutHashTable;
2020-12-19 05:35:21 +00:00
/* initialize baseline descriptor info */
VkDescriptorPool defaultDescriptorPool;
2020-12-29 22:52:24 +00:00
VkDescriptorSetLayout emptyVertexSamplerLayout;
VkDescriptorSetLayout emptyFragmentSamplerLayout;
2020-12-29 22:52:24 +00:00
VkDescriptorSetLayout emptyComputeBufferDescriptorSetLayout;
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayout emptyComputeImageDescriptorSetLayout;
2020-12-29 22:52:24 +00:00
VkDescriptorSet emptyVertexSamplerDescriptorSet;
VkDescriptorSet emptyFragmentSamplerDescriptorSet;
2020-12-29 22:52:24 +00:00
VkDescriptorSet emptyComputeBufferDescriptorSet;
2020-12-31 04:39:47 +00:00
VkDescriptorSet emptyComputeImageDescriptorSet;
2020-12-17 19:40:49 +00:00
VkDescriptorSetLayout vertexParamLayout;
VkDescriptorSetLayout fragmentParamLayout;
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayout computeParamLayout;
2020-12-28 22:07:13 +00:00
VulkanBuffer *dummyVertexUniformBuffer;
VulkanBuffer *dummyFragmentUniformBuffer;
2020-12-31 04:39:47 +00:00
VulkanBuffer *dummyComputeUniformBuffer;
2020-12-17 19:40:49 +00:00
2020-12-19 04:08:07 +00:00
VulkanBuffer *textureStagingBuffer;
uint32_t textureStagingBufferOffset;
2020-12-21 21:02:07 +00:00
2020-12-21 23:44:43 +00:00
VulkanBuffer** buffersInUse;
uint32_t buffersInUseCount;
uint32_t buffersInUseCapacity;
VulkanBuffer** submittedBuffers;
uint32_t submittedBufferCount;
uint32_t submittedBufferCapacity;
2020-12-19 04:08:07 +00:00
VulkanBuffer *vertexUBO;
VulkanBuffer *fragmentUBO;
2020-12-31 04:39:47 +00:00
VulkanBuffer *computeUBO;
2020-12-21 21:02:07 +00:00
uint32_t minUBOAlignment;
uint32_t vertexUBOOffset;
2020-12-23 21:11:09 +00:00
VkDeviceSize vertexUBOBlockIncrement;
2020-12-21 21:02:07 +00:00
uint32_t fragmentUBOOffset;
2020-12-23 21:11:09 +00:00
VkDeviceSize fragmentUBOBlockIncrement;
2020-12-21 21:02:07 +00:00
2020-12-31 04:39:47 +00:00
uint32_t computeUBOOffset;
VkDeviceSize computeUBOBlockIncrement;
2020-12-21 21:02:07 +00:00
uint32_t frameIndex;
2020-12-19 04:08:07 +00:00
2020-12-18 22:35:33 +00:00
SDL_mutex *allocatorLock;
2020-12-28 23:11:05 +00:00
SDL_mutex *disposeLock;
2021-01-02 06:07:15 +00:00
SDL_mutex *uniformBufferLock;
SDL_mutex *descriptorSetLock;
SDL_mutex *boundBufferLock;
2021-01-03 21:17:51 +00:00
SDL_mutex *stagingLock;
2020-12-28 23:11:05 +00:00
/* Deferred destroy storage */
2020-12-29 00:28:14 +00:00
VulkanColorTarget **colorTargetsToDestroy;
uint32_t colorTargetsToDestroyCount;
uint32_t colorTargetsToDestroyCapacity;
VulkanColorTarget **submittedColorTargetsToDestroy;
uint32_t submittedColorTargetsToDestroyCount;
uint32_t submittedColorTargetsToDestroyCapacity;
2020-12-29 04:09:31 +00:00
VulkanDepthStencilTarget **depthStencilTargetsToDestroy;
uint32_t depthStencilTargetsToDestroyCount;
uint32_t depthStencilTargetsToDestroyCapacity;
VulkanDepthStencilTarget **submittedDepthStencilTargetsToDestroy;
uint32_t submittedDepthStencilTargetsToDestroyCount;
uint32_t submittedDepthStencilTargetsToDestroyCapacity;
2020-12-28 23:11:05 +00:00
VulkanTexture **texturesToDestroy;
uint32_t texturesToDestroyCount;
uint32_t texturesToDestroyCapacity;
VulkanTexture **submittedTexturesToDestroy;
uint32_t submittedTexturesToDestroyCount;
uint32_t submittedTexturesToDestroyCapacity;
2020-12-18 22:35:33 +00:00
2020-12-29 00:28:14 +00:00
VulkanBuffer **buffersToDestroy;
uint32_t buffersToDestroyCount;
uint32_t buffersToDestroyCapacity;
VulkanBuffer **submittedBuffersToDestroy;
uint32_t submittedBuffersToDestroyCount;
uint32_t submittedBuffersToDestroyCapacity;
VulkanGraphicsPipeline **graphicsPipelinesToDestroy;
uint32_t graphicsPipelinesToDestroyCount;
uint32_t graphicsPipelinesToDestroyCapacity;
VulkanGraphicsPipeline **submittedGraphicsPipelinesToDestroy;
uint32_t submittedGraphicsPipelinesToDestroyCount;
uint32_t submittedGraphicsPipelinesToDestroyCapacity;
2020-12-31 07:02:12 +00:00
VulkanComputePipeline **computePipelinesToDestroy;
uint32_t computePipelinesToDestroyCount;
uint32_t computePipelinesToDestroyCapacity;
VulkanComputePipeline **submittedComputePipelinesToDestroy;
uint32_t submittedComputePipelinesToDestroyCount;
uint32_t submittedComputePipelinesToDestroyCapacity;
2020-12-29 00:42:51 +00:00
VkShaderModule *shaderModulesToDestroy;
uint32_t shaderModulesToDestroyCount;
uint32_t shaderModulesToDestroyCapacity;
VkShaderModule *submittedShaderModulesToDestroy;
uint32_t submittedShaderModulesToDestroyCount;
uint32_t submittedShaderModulesToDestroyCapacity;
2020-12-29 00:56:49 +00:00
VkSampler *samplersToDestroy;
uint32_t samplersToDestroyCount;
uint32_t samplersToDestroyCapacity;
VkSampler *submittedSamplersToDestroy;
uint32_t submittedSamplersToDestroyCount;
uint32_t submittedSamplersToDestroyCapacity;
2020-12-29 01:35:18 +00:00
VulkanFramebuffer **framebuffersToDestroy;
uint32_t framebuffersToDestroyCount;
uint32_t framebuffersToDestroyCapacity;
VulkanFramebuffer **submittedFramebuffersToDestroy;
uint32_t submittedFramebuffersToDestroyCount;
uint32_t submittedFramebuffersToDestroyCapacity;
2020-12-29 03:44:34 +00:00
VkRenderPass *renderPassesToDestroy;
uint32_t renderPassesToDestroyCount;
uint32_t renderPassesToDestroyCapacity;
VkRenderPass *submittedRenderPassesToDestroy;
uint32_t submittedRenderPassesToDestroyCount;
uint32_t submittedRenderPassesToDestroyCapacity;
2021-01-14 01:37:54 +00:00
/* External Interop */
uint8_t usesExternalDevice;
2020-12-17 02:38:22 +00:00
#define VULKAN_INSTANCE_FUNCTION(ext, ret, func, params) \
vkfntype_##func func;
#define VULKAN_DEVICE_FUNCTION(ext, ret, func, params) \
vkfntype_##func func;
#include "Refresh_Driver_Vulkan_vkfuncs.h"
2020-12-17 03:28:02 +00:00
} VulkanRenderer;
2020-12-19 00:39:03 +00:00
/* Forward declarations */
2021-01-02 06:07:15 +00:00
static void VULKAN_INTERNAL_BeginCommandBuffer(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer);
2021-01-05 23:00:51 +00:00
static void VULKAN_Submit(Refresh_Renderer *driverData, uint32_t commandBufferCount, Refresh_CommandBuffer **pCommandBuffers);
static void VULKAN_INTERNAL_FlushTransfers(VulkanRenderer *renderer);
2020-12-19 00:39:03 +00:00
2020-12-17 03:28:02 +00:00
/* Error Handling */
static inline const char* VkErrorMessages(VkResult code)
{
#define ERR_TO_STR(e) \
case e: return #e;
switch (code)
{
ERR_TO_STR(VK_ERROR_OUT_OF_HOST_MEMORY)
ERR_TO_STR(VK_ERROR_OUT_OF_DEVICE_MEMORY)
ERR_TO_STR(VK_ERROR_FRAGMENTED_POOL)
ERR_TO_STR(VK_ERROR_OUT_OF_POOL_MEMORY)
ERR_TO_STR(VK_ERROR_INITIALIZATION_FAILED)
ERR_TO_STR(VK_ERROR_LAYER_NOT_PRESENT)
ERR_TO_STR(VK_ERROR_EXTENSION_NOT_PRESENT)
ERR_TO_STR(VK_ERROR_FEATURE_NOT_PRESENT)
ERR_TO_STR(VK_ERROR_TOO_MANY_OBJECTS)
ERR_TO_STR(VK_ERROR_DEVICE_LOST)
ERR_TO_STR(VK_ERROR_INCOMPATIBLE_DRIVER)
ERR_TO_STR(VK_ERROR_OUT_OF_DATE_KHR)
ERR_TO_STR(VK_ERROR_SURFACE_LOST_KHR)
ERR_TO_STR(VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
ERR_TO_STR(VK_SUBOPTIMAL_KHR)
default: return "Unhandled VkResult!";
}
#undef ERR_TO_STR
}
static inline void LogVulkanResult(
const char* vulkanFunctionName,
VkResult result
) {
if (result != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"%s: %s",
vulkanFunctionName,
VkErrorMessages(result)
);
}
}
2020-12-17 01:23:49 +00:00
2020-12-18 22:35:33 +00:00
/* Utility */
static inline uint8_t DepthFormatContainsStencil(VkFormat format)
{
switch(format)
{
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_D32_SFLOAT:
return 0;
case VK_FORMAT_D16_UNORM_S8_UINT:
case VK_FORMAT_D24_UNORM_S8_UINT:
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return 1;
default:
SDL_assert(0 && "Invalid depth format");
return 0;
}
}
/* Memory Management */
static inline VkDeviceSize VULKAN_INTERNAL_NextHighestAlignment(
VkDeviceSize n,
VkDeviceSize align
) {
return align * ((n + align - 1) / align);
}
static VulkanMemoryFreeRegion* VULKAN_INTERNAL_NewMemoryFreeRegion(
VulkanMemoryAllocation *allocation,
VkDeviceSize offset,
VkDeviceSize size
) {
VulkanMemoryFreeRegion *newFreeRegion;
uint32_t insertionIndex = 0;
uint32_t i;
/* TODO: an improvement here could be to merge contiguous free regions */
allocation->freeRegionCount += 1;
if (allocation->freeRegionCount > allocation->freeRegionCapacity)
{
allocation->freeRegionCapacity *= 2;
allocation->freeRegions = SDL_realloc(
allocation->freeRegions,
sizeof(VulkanMemoryFreeRegion*) * allocation->freeRegionCapacity
);
}
newFreeRegion = SDL_malloc(sizeof(VulkanMemoryFreeRegion));
newFreeRegion->offset = offset;
newFreeRegion->size = size;
newFreeRegion->allocation = allocation;
allocation->freeRegions[allocation->freeRegionCount - 1] = newFreeRegion;
newFreeRegion->allocationIndex = allocation->freeRegionCount - 1;
for (i = 0; i < allocation->allocator->sortedFreeRegionCount; i += 1)
{
if (allocation->allocator->sortedFreeRegions[i]->size < size)
{
/* this is where the new region should go */
break;
}
insertionIndex += 1;
}
if (allocation->allocator->sortedFreeRegionCount + 1 > allocation->allocator->sortedFreeRegionCapacity)
{
allocation->allocator->sortedFreeRegionCapacity *= 2;
allocation->allocator->sortedFreeRegions = SDL_realloc(
allocation->allocator->sortedFreeRegions,
sizeof(VulkanMemoryFreeRegion*) * allocation->allocator->sortedFreeRegionCapacity
);
}
/* perform insertion sort */
if (allocation->allocator->sortedFreeRegionCount > 0 && insertionIndex != allocation->allocator->sortedFreeRegionCount)
{
for (i = allocation->allocator->sortedFreeRegionCount; i > insertionIndex && i > 0; i -= 1)
{
allocation->allocator->sortedFreeRegions[i] = allocation->allocator->sortedFreeRegions[i - 1];
allocation->allocator->sortedFreeRegions[i]->sortedIndex = i;
}
}
allocation->allocator->sortedFreeRegionCount += 1;
allocation->allocator->sortedFreeRegions[insertionIndex] = newFreeRegion;
newFreeRegion->sortedIndex = insertionIndex;
return newFreeRegion;
}
static void VULKAN_INTERNAL_RemoveMemoryFreeRegion(
VulkanMemoryFreeRegion *freeRegion
) {
uint32_t i;
/* close the gap in the sorted list */
if (freeRegion->allocation->allocator->sortedFreeRegionCount > 1)
{
for (i = freeRegion->sortedIndex; i < freeRegion->allocation->allocator->sortedFreeRegionCount - 1; i += 1)
{
freeRegion->allocation->allocator->sortedFreeRegions[i] =
freeRegion->allocation->allocator->sortedFreeRegions[i + 1];
freeRegion->allocation->allocator->sortedFreeRegions[i]->sortedIndex = i;
}
}
freeRegion->allocation->allocator->sortedFreeRegionCount -= 1;
/* close the gap in the buffer list */
if (freeRegion->allocation->freeRegionCount > 1 && freeRegion->allocationIndex != freeRegion->allocation->freeRegionCount - 1)
{
freeRegion->allocation->freeRegions[freeRegion->allocationIndex] =
freeRegion->allocation->freeRegions[freeRegion->allocation->freeRegionCount - 1];
freeRegion->allocation->freeRegions[freeRegion->allocationIndex]->allocationIndex =
freeRegion->allocationIndex;
}
freeRegion->allocation->freeRegionCount -= 1;
SDL_free(freeRegion);
}
static uint8_t VULKAN_INTERNAL_FindMemoryType(
VulkanRenderer *renderer,
uint32_t typeFilter,
VkMemoryPropertyFlags properties,
uint32_t *result
) {
VkPhysicalDeviceMemoryProperties memoryProperties;
uint32_t i;
renderer->vkGetPhysicalDeviceMemoryProperties(
renderer->physicalDevice,
&memoryProperties
);
for (i = 0; i < memoryProperties.memoryTypeCount; i += 1)
{
if ( (typeFilter & (1 << i)) &&
(memoryProperties.memoryTypes[i].propertyFlags & properties) == properties )
{
*result = i;
return 1;
}
}
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to find memory properties %X, filter %X", properties, typeFilter);
2020-12-18 22:35:33 +00:00
return 0;
}
static uint8_t VULKAN_INTERNAL_FindBufferMemoryRequirements(
VulkanRenderer *renderer,
VkBuffer buffer,
VkMemoryRequirements2KHR *pMemoryRequirements,
uint32_t *pMemoryTypeIndex
) {
VkBufferMemoryRequirementsInfo2KHR bufferRequirementsInfo;
bufferRequirementsInfo.sType =
VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2_KHR;
bufferRequirementsInfo.pNext = NULL;
bufferRequirementsInfo.buffer = buffer;
renderer->vkGetBufferMemoryRequirements2KHR(
renderer->logicalDevice,
&bufferRequirementsInfo,
pMemoryRequirements
);
if (!VULKAN_INTERNAL_FindMemoryType(
renderer,
pMemoryRequirements->memoryRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
pMemoryTypeIndex
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-18 22:35:33 +00:00
"Could not find valid memory type for buffer creation"
);
return 0;
}
return 1;
}
static uint8_t VULKAN_INTERNAL_FindImageMemoryRequirements(
VulkanRenderer *renderer,
VkImage image,
VkMemoryRequirements2KHR *pMemoryRequirements,
uint32_t *pMemoryTypeIndex
) {
VkImageMemoryRequirementsInfo2KHR imageRequirementsInfo;
imageRequirementsInfo.sType =
VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2_KHR;
imageRequirementsInfo.pNext = NULL;
imageRequirementsInfo.image = image;
renderer->vkGetImageMemoryRequirements2KHR(
renderer->logicalDevice,
&imageRequirementsInfo,
pMemoryRequirements
);
if (!VULKAN_INTERNAL_FindMemoryType(
renderer,
pMemoryRequirements->memoryRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
pMemoryTypeIndex
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-18 22:35:33 +00:00
"Could not find valid memory type for image creation"
);
return 0;
}
return 1;
}
static uint8_t VULKAN_INTERNAL_AllocateMemory(
VulkanRenderer *renderer,
VkBuffer buffer,
VkImage image,
uint32_t memoryTypeIndex,
VkDeviceSize allocationSize,
uint8_t dedicated,
VulkanMemoryAllocation **pMemoryAllocation
) {
VulkanMemoryAllocation *allocation;
VulkanMemorySubAllocator *allocator = &renderer->memoryAllocator->subAllocators[memoryTypeIndex];
VkMemoryAllocateInfo allocInfo;
VkMemoryDedicatedAllocateInfoKHR dedicatedInfo;
VkResult result;
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.memoryTypeIndex = memoryTypeIndex;
allocInfo.allocationSize = allocationSize;
allocation = SDL_malloc(sizeof(VulkanMemoryAllocation));
allocation->size = allocationSize;
2021-01-03 21:12:12 +00:00
allocation->memoryLock = SDL_CreateMutex();
2020-12-18 22:35:33 +00:00
if (dedicated)
{
dedicatedInfo.sType =
VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR;
dedicatedInfo.pNext = NULL;
dedicatedInfo.buffer = buffer;
dedicatedInfo.image = image;
allocInfo.pNext = &dedicatedInfo;
allocation->dedicated = 1;
}
else
{
allocInfo.pNext = NULL;
/* allocate a non-dedicated texture buffer */
allocator->allocationCount += 1;
allocator->allocations = SDL_realloc(
allocator->allocations,
sizeof(VulkanMemoryAllocation*) * allocator->allocationCount
);
allocator->allocations[
allocator->allocationCount - 1
] = allocation;
allocation->dedicated = 0;
}
allocation->freeRegions = SDL_malloc(sizeof(VulkanMemoryFreeRegion*));
allocation->freeRegionCount = 0;
allocation->freeRegionCapacity = 1;
allocation->allocator = allocator;
result = renderer->vkAllocateMemory(
renderer->logicalDevice,
&allocInfo,
NULL,
&allocation->memory
);
if (result != VK_SUCCESS)
{
LogVulkanResult("vkAllocateMemory", result);
return 0;
}
2021-01-14 02:02:45 +00:00
if (buffer != NULL)
{
result = renderer->vkMapMemory(
renderer->logicalDevice,
allocation->memory,
0,
allocation->size,
0,
(void**) &allocation->mapPointer
);
if (result != VK_SUCCESS)
{
LogVulkanResult("vkMapMemory", result);
return 0;
}
}
else
{
allocation->mapPointer = NULL;
}
2020-12-18 22:35:33 +00:00
VULKAN_INTERNAL_NewMemoryFreeRegion(
allocation,
0,
allocation->size
);
*pMemoryAllocation = allocation;
return 1;
}
static uint8_t VULKAN_INTERNAL_FindAvailableMemory(
VulkanRenderer *renderer,
VkBuffer buffer,
VkImage image,
VulkanMemoryAllocation **pMemoryAllocation,
VkDeviceSize *pOffset,
VkDeviceSize *pSize
) {
VkMemoryDedicatedRequirementsKHR dedicatedRequirements =
{
VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR,
NULL
};
VkMemoryRequirements2KHR memoryRequirements =
{
VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR,
&dedicatedRequirements
};
uint32_t memoryTypeIndex;
VulkanMemoryAllocation *allocation;
VulkanMemorySubAllocator *allocator;
VulkanMemoryFreeRegion *region;
VkDeviceSize requiredSize, allocationSize;
VkDeviceSize alignedOffset;
2020-12-21 20:37:54 +00:00
VkDeviceSize newRegionSize, newRegionOffset;
2020-12-18 22:35:33 +00:00
uint8_t allocationResult;
if (buffer != VK_NULL_HANDLE && image != VK_NULL_HANDLE)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Calling FindAvailableMemory with both a buffer and image handle is invalid!");
2020-12-18 22:35:33 +00:00
return 0;
}
else if (buffer != VK_NULL_HANDLE)
{
if (!VULKAN_INTERNAL_FindBufferMemoryRequirements(
renderer,
buffer,
&memoryRequirements,
&memoryTypeIndex
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to acquire buffer memory requirements!");
2020-12-18 22:35:33 +00:00
return 0;
}
}
else if (image != VK_NULL_HANDLE)
{
if (!VULKAN_INTERNAL_FindImageMemoryRequirements(
renderer,
image,
&memoryRequirements,
&memoryTypeIndex
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to acquire image memory requirements!");
2020-12-18 22:35:33 +00:00
return 0;
}
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Calling FindAvailableMemory with neither buffer nor image handle is invalid!");
2020-12-18 22:35:33 +00:00
return 0;
}
allocator = &renderer->memoryAllocator->subAllocators[memoryTypeIndex];
requiredSize = memoryRequirements.memoryRequirements.size;
SDL_LockMutex(renderer->allocatorLock);
/* find the largest free region and use it */
if (allocator->sortedFreeRegionCount > 0)
{
region = allocator->sortedFreeRegions[0];
allocation = region->allocation;
alignedOffset = VULKAN_INTERNAL_NextHighestAlignment(
region->offset,
memoryRequirements.memoryRequirements.alignment
);
if (alignedOffset + requiredSize <= region->offset + region->size)
{
*pMemoryAllocation = allocation;
/* not aligned - create a new free region */
if (region->offset != alignedOffset)
{
VULKAN_INTERNAL_NewMemoryFreeRegion(
allocation,
region->offset,
alignedOffset - region->offset
);
}
*pOffset = alignedOffset;
*pSize = requiredSize;
newRegionSize = region->size - ((alignedOffset - region->offset) + requiredSize);
newRegionOffset = alignedOffset + requiredSize;
/* remove and add modified region to re-sort */
VULKAN_INTERNAL_RemoveMemoryFreeRegion(region);
/* if size is 0, no need to re-insert */
if (newRegionSize != 0)
{
VULKAN_INTERNAL_NewMemoryFreeRegion(
allocation,
newRegionOffset,
newRegionSize
);
}
SDL_UnlockMutex(renderer->allocatorLock);
return 1;
}
}
/* No suitable free regions exist, allocate a new memory region */
if (dedicatedRequirements.prefersDedicatedAllocation || dedicatedRequirements.requiresDedicatedAllocation)
{
allocationSize = requiredSize;
}
else if (requiredSize > allocator->nextAllocationSize)
{
/* allocate a page of required size aligned to STARTING_ALLOCATION_SIZE increments */
allocationSize =
VULKAN_INTERNAL_NextHighestAlignment(requiredSize, STARTING_ALLOCATION_SIZE);
}
else
{
allocationSize = allocator->nextAllocationSize;
allocator->nextAllocationSize = SDL_min(allocator->nextAllocationSize * 2, MAX_ALLOCATION_SIZE);
}
allocationResult = VULKAN_INTERNAL_AllocateMemory(
renderer,
buffer,
image,
memoryTypeIndex,
allocationSize,
dedicatedRequirements.prefersDedicatedAllocation || dedicatedRequirements.requiresDedicatedAllocation,
&allocation
);
2020-12-17 04:04:47 +00:00
2020-12-18 22:35:33 +00:00
/* Uh oh, we're out of memory */
if (allocationResult == 0)
{
/* Responsibility of the caller to handle being out of memory */
2021-01-05 23:00:51 +00:00
Refresh_LogWarn("Failed to allocate memory!");
2020-12-18 22:35:33 +00:00
SDL_UnlockMutex(renderer->allocatorLock);
return 2;
}
*pMemoryAllocation = allocation;
*pOffset = 0;
*pSize = requiredSize;
region = allocation->freeRegions[0];
newRegionOffset = region->offset + requiredSize;
newRegionSize = region->size - requiredSize;
VULKAN_INTERNAL_RemoveMemoryFreeRegion(region);
if (newRegionSize != 0)
{
VULKAN_INTERNAL_NewMemoryFreeRegion(
allocation,
newRegionOffset,
newRegionSize
);
}
SDL_UnlockMutex(renderer->allocatorLock);
return 1;
}
2020-12-19 00:39:03 +00:00
/* Memory Barriers */
2020-12-19 01:03:26 +00:00
static void VULKAN_INTERNAL_BufferMemoryBarrier(
VulkanRenderer *renderer,
2021-01-02 21:31:17 +00:00
VkCommandBuffer commandBuffer,
2020-12-19 01:03:26 +00:00
VulkanResourceAccessType nextResourceAccessType,
2020-12-19 04:08:07 +00:00
VulkanBuffer *buffer,
VulkanSubBuffer *subBuffer
2020-12-19 01:03:26 +00:00
) {
VkPipelineStageFlags srcStages = 0;
VkPipelineStageFlags dstStages = 0;
VkBufferMemoryBarrier memoryBarrier;
VulkanResourceAccessType prevAccess, nextAccess;
const VulkanResourceAccessInfo *prevAccessInfo, *nextAccessInfo;
if (buffer->resourceAccessType == nextResourceAccessType)
{
return;
}
memoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
memoryBarrier.pNext = NULL;
memoryBarrier.srcAccessMask = 0;
memoryBarrier.dstAccessMask = 0;
memoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
memoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
2020-12-19 04:08:07 +00:00
memoryBarrier.buffer = subBuffer->buffer;
2020-12-19 01:03:26 +00:00
memoryBarrier.offset = 0;
memoryBarrier.size = buffer->size;
prevAccess = buffer->resourceAccessType;
prevAccessInfo = &AccessMap[prevAccess];
srcStages |= prevAccessInfo->stageMask;
if (prevAccess > RESOURCE_ACCESS_END_OF_READ)
{
memoryBarrier.srcAccessMask |= prevAccessInfo->accessMask;
}
nextAccess = nextResourceAccessType;
nextAccessInfo = &AccessMap[nextAccess];
dstStages |= nextAccessInfo->stageMask;
if (memoryBarrier.srcAccessMask != 0)
{
memoryBarrier.dstAccessMask |= nextAccessInfo->accessMask;
}
if (srcStages == 0)
{
srcStages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
}
if (dstStages == 0)
{
dstStages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
}
2021-01-02 06:07:15 +00:00
renderer->vkCmdPipelineBarrier(
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 01:03:26 +00:00
srcStages,
dstStages,
0,
0,
NULL,
1,
&memoryBarrier,
0,
NULL
2021-01-02 06:07:15 +00:00
);
2020-12-19 01:03:26 +00:00
buffer->resourceAccessType = nextResourceAccessType;
}
2020-12-19 00:39:03 +00:00
static void VULKAN_INTERNAL_ImageMemoryBarrier(
VulkanRenderer *renderer,
2021-01-02 21:31:17 +00:00
VkCommandBuffer commandBuffer,
2020-12-19 00:39:03 +00:00
VulkanResourceAccessType nextAccess,
VkImageAspectFlags aspectMask,
uint32_t baseLayer,
uint32_t layerCount,
uint32_t baseLevel,
uint32_t levelCount,
uint8_t discardContents,
VkImage image,
VulkanResourceAccessType *resourceAccessType
) {
VkPipelineStageFlags srcStages = 0;
VkPipelineStageFlags dstStages = 0;
VkImageMemoryBarrier memoryBarrier;
VulkanResourceAccessType prevAccess;
const VulkanResourceAccessInfo *pPrevAccessInfo, *pNextAccessInfo;
if (*resourceAccessType == nextAccess)
{
return;
}
memoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
memoryBarrier.pNext = NULL;
memoryBarrier.srcAccessMask = 0;
memoryBarrier.dstAccessMask = 0;
memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
memoryBarrier.newLayout = VK_IMAGE_LAYOUT_UNDEFINED;
2021-01-03 04:03:07 +00:00
memoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
memoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
2020-12-19 00:39:03 +00:00
memoryBarrier.image = image;
memoryBarrier.subresourceRange.aspectMask = aspectMask;
memoryBarrier.subresourceRange.baseArrayLayer = baseLayer;
memoryBarrier.subresourceRange.layerCount = layerCount;
memoryBarrier.subresourceRange.baseMipLevel = baseLevel;
memoryBarrier.subresourceRange.levelCount = levelCount;
prevAccess = *resourceAccessType;
pPrevAccessInfo = &AccessMap[prevAccess];
srcStages |= pPrevAccessInfo->stageMask;
if (prevAccess > RESOURCE_ACCESS_END_OF_READ)
{
memoryBarrier.srcAccessMask |= pPrevAccessInfo->accessMask;
}
if (discardContents)
{
memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
}
else
{
memoryBarrier.oldLayout = pPrevAccessInfo->imageLayout;
}
pNextAccessInfo = &AccessMap[nextAccess];
dstStages |= pNextAccessInfo->stageMask;
memoryBarrier.dstAccessMask |= pNextAccessInfo->accessMask;
memoryBarrier.newLayout = pNextAccessInfo->imageLayout;
if (srcStages == 0)
{
srcStages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
}
if (dstStages == 0)
{
dstStages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
}
2021-01-02 06:07:15 +00:00
renderer->vkCmdPipelineBarrier(
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 00:39:03 +00:00
srcStages,
dstStages,
0,
0,
NULL,
0,
NULL,
1,
&memoryBarrier
2021-01-02 06:07:15 +00:00
);
2021-01-02 21:31:17 +00:00
2020-12-21 23:44:43 +00:00
*resourceAccessType = nextAccess;
}
/* Resource Disposal */
2020-12-29 00:28:14 +00:00
static void VULKAN_INTERNAL_DestroyTexture(
VulkanRenderer* renderer,
VulkanTexture* texture
) {
if (texture->allocation->dedicated)
{
renderer->vkFreeMemory(
renderer->logicalDevice,
texture->allocation->memory,
NULL
);
2021-01-03 21:12:12 +00:00
SDL_DestroyMutex(texture->allocation->memoryLock);
2020-12-29 00:28:14 +00:00
SDL_free(texture->allocation->freeRegions);
SDL_free(texture->allocation);
}
else
{
SDL_LockMutex(renderer->allocatorLock);
VULKAN_INTERNAL_NewMemoryFreeRegion(
texture->allocation,
texture->offset,
texture->memorySize
);
SDL_UnlockMutex(renderer->allocatorLock);
}
renderer->vkDestroyImageView(
renderer->logicalDevice,
texture->view,
NULL
);
renderer->vkDestroyImage(
renderer->logicalDevice,
texture->image,
NULL
);
SDL_free(texture);
}
static void VULKAN_INTERNAL_DestroyColorTarget(
VulkanRenderer *renderer,
VulkanColorTarget *colorTarget
) {
renderer->vkDestroyImageView(
renderer->logicalDevice,
colorTarget->view,
NULL
);
/* The texture is not owned by the ColorTarget
* so we don't free it here
* But the multisampleTexture is!
*/
if (colorTarget->multisampleTexture != NULL)
{
VULKAN_INTERNAL_DestroyTexture(
renderer,
colorTarget->multisampleTexture
);
}
SDL_free(colorTarget);
}
2020-12-29 04:09:31 +00:00
static void VULKAN_INTERNAL_DestroyDepthStencilTarget(
VulkanRenderer *renderer,
VulkanDepthStencilTarget *depthStencilTarget
) {
VULKAN_INTERNAL_DestroyTexture(renderer, depthStencilTarget->texture);
SDL_free(depthStencilTarget);
}
2020-12-21 23:44:43 +00:00
static void VULKAN_INTERNAL_DestroyBuffer(
2020-12-28 22:57:14 +00:00
VulkanRenderer* renderer,
VulkanBuffer* buffer
2020-12-21 23:44:43 +00:00
) {
uint32_t i;
if (buffer->bound || buffer->boundSubmitted)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Cannot destroy a bound buffer!");
2020-12-21 23:44:43 +00:00
return;
}
for (i = 0; i < buffer->subBufferCount; i += 1)
{
if (buffer->subBuffers[i]->allocation->dedicated)
{
renderer->vkFreeMemory(
renderer->logicalDevice,
buffer->subBuffers[i]->allocation->memory,
NULL
);
2021-01-03 21:12:12 +00:00
SDL_DestroyMutex(buffer->subBuffers[i]->allocation->memoryLock);
2020-12-21 23:44:43 +00:00
SDL_free(buffer->subBuffers[i]->allocation->freeRegions);
SDL_free(buffer->subBuffers[i]->allocation);
}
else
{
SDL_LockMutex(renderer->allocatorLock);
VULKAN_INTERNAL_NewMemoryFreeRegion(
buffer->subBuffers[i]->allocation,
buffer->subBuffers[i]->offset,
buffer->subBuffers[i]->size
);
SDL_UnlockMutex(renderer->allocatorLock);
}
renderer->vkDestroyBuffer(
renderer->logicalDevice,
buffer->subBuffers[i]->buffer,
NULL
);
SDL_free(buffer->subBuffers[i]);
}
SDL_free(buffer->subBuffers);
buffer->subBuffers = NULL;
SDL_free(buffer);
}
2021-01-03 02:02:20 +00:00
static void VULKAN_INTERNAL_DestroyCommandPool(
VulkanRenderer *renderer,
VulkanCommandPool *commandPool
) {
renderer->vkDestroyCommandPool(
2021-01-03 02:02:20 +00:00
renderer->logicalDevice,
commandPool->commandPool,
NULL
2021-01-03 02:02:20 +00:00
);
SDL_free(commandPool->inactiveCommandBuffers);
SDL_free(commandPool);
}
2020-12-29 00:28:14 +00:00
static void VULKAN_INTERNAL_DestroyGraphicsPipeline(
VulkanRenderer *renderer,
VulkanGraphicsPipeline *graphicsPipeline
) {
VkDescriptorSet descriptorSets[2];
descriptorSets[0] = graphicsPipeline->vertexUBODescriptorSet;
descriptorSets[1] = graphicsPipeline->fragmentUBODescriptorSet;
renderer->vkFreeDescriptorSets(
renderer->logicalDevice,
renderer->defaultDescriptorPool,
2,
descriptorSets
);
renderer->vkDestroyPipeline(
renderer->logicalDevice,
graphicsPipeline->pipeline,
NULL
);
SDL_free(graphicsPipeline);
}
2020-12-31 07:02:12 +00:00
static void VULKAN_INTERNAL_DestroyComputePipeline(
VulkanRenderer *renderer,
VulkanComputePipeline *computePipeline
) {
renderer->vkFreeDescriptorSets(
renderer->logicalDevice,
renderer->defaultDescriptorPool,
1,
&computePipeline->computeUBODescriptorSet
);
renderer->vkDestroyPipeline(
renderer->logicalDevice,
computePipeline->pipeline,
NULL
);
SDL_free(computePipeline);
}
2020-12-29 00:42:51 +00:00
static void VULKAN_INTERNAL_DestroyShaderModule(
VulkanRenderer *renderer,
VkShaderModule shaderModule
) {
renderer->vkDestroyShaderModule(
renderer->logicalDevice,
shaderModule,
NULL
);
}
2020-12-29 00:56:49 +00:00
static void VULKAN_INTERNAL_DestroySampler(
VulkanRenderer *renderer,
VkSampler sampler
) {
renderer->vkDestroySampler(
renderer->logicalDevice,
sampler,
NULL
);
}
2020-12-29 01:35:18 +00:00
/* The framebuffer doesn't own any targets so we don't have to do much. */
static void VULKAN_INTERNAL_DestroyFramebuffer(
VulkanRenderer *renderer,
VulkanFramebuffer *framebuffer
) {
renderer->vkDestroyFramebuffer(
renderer->logicalDevice,
framebuffer->framebuffer,
NULL
);
}
2020-12-29 03:44:34 +00:00
static void VULKAN_INTERNAL_DestroyRenderPass(
VulkanRenderer *renderer,
VkRenderPass renderPass
) {
renderer->vkDestroyRenderPass(
renderer->logicalDevice,
renderPass,
NULL
);
}
2020-12-21 23:44:43 +00:00
static void VULKAN_INTERNAL_DestroySwapchain(VulkanRenderer* renderer)
{
uint32_t i;
for (i = 0; i < renderer->swapChainImageCount; i += 1)
{
renderer->vkDestroyImageView(
renderer->logicalDevice,
renderer->swapChainImageViews[i],
NULL
);
}
SDL_free(renderer->swapChainImages);
renderer->swapChainImages = NULL;
SDL_free(renderer->swapChainImageViews);
renderer->swapChainImageViews = NULL;
SDL_free(renderer->swapChainResourceAccessTypes);
renderer->swapChainResourceAccessTypes = NULL;
2021-01-03 03:03:25 +00:00
SDL_free(renderer->swapChainQueueFamilyIndices);
renderer->swapChainQueueFamilyIndices = NULL;
2020-12-21 23:44:43 +00:00
renderer->vkDestroySwapchainKHR(
renderer->logicalDevice,
renderer->swapChain,
NULL
);
}
2020-12-28 22:57:14 +00:00
static void VULKAN_INTERNAL_DestroyTextureStagingBuffer(
VulkanRenderer* renderer
) {
VULKAN_INTERNAL_DestroyBuffer(
renderer,
renderer->textureStagingBuffer
);
}
2020-12-31 07:02:12 +00:00
static void VULKAN_INTERNAL_DestroyBufferDescriptorSetCache(
VulkanRenderer *renderer,
BufferDescriptorSetCache *cache
) {
uint32_t i;
if (cache == NULL)
{
return;
}
for (i = 0; i < cache->bufferDescriptorPoolCount; i += 1)
{
renderer->vkDestroyDescriptorPool(
renderer->logicalDevice,
cache->bufferDescriptorPools[i],
NULL
);
}
SDL_free(cache->bufferDescriptorPools);
SDL_free(cache->inactiveDescriptorSets);
SDL_free(cache->elements);
for (i = 0; i < NUM_DESCRIPTOR_SET_HASH_BUCKETS; i += 1)
{
SDL_free(cache->buckets[i].elements);
}
SDL_free(cache);
}
2020-12-30 00:53:10 +00:00
static void VULKAN_INTERNAL_DestroyImageDescriptorSetCache(
2020-12-31 07:02:12 +00:00
VulkanRenderer *renderer,
ImageDescriptorSetCache *cache
2020-12-28 22:57:14 +00:00
) {
uint32_t i;
2020-12-28 23:11:05 +00:00
if (cache == NULL)
{
return;
}
2020-12-30 00:19:19 +00:00
for (i = 0; i < cache->imageDescriptorPoolCount; i += 1)
2020-12-28 22:57:14 +00:00
{
renderer->vkDestroyDescriptorPool(
renderer->logicalDevice,
2020-12-30 00:19:19 +00:00
cache->imageDescriptorPools[i],
2020-12-28 22:57:14 +00:00
NULL
);
}
2020-12-30 00:19:19 +00:00
SDL_free(cache->imageDescriptorPools);
2020-12-28 22:57:14 +00:00
SDL_free(cache->inactiveDescriptorSets);
SDL_free(cache->elements);
for (i = 0; i < NUM_DESCRIPTOR_SET_HASH_BUCKETS; i += 1)
{
SDL_free(cache->buckets[i].elements);
}
SDL_free(cache);
}
2020-12-29 03:44:34 +00:00
static void VULKAN_INTERNAL_PostWorkCleanup(VulkanRenderer* renderer)
2020-12-28 23:11:05 +00:00
{
2020-12-29 00:28:14 +00:00
uint32_t i, j;
2020-12-28 23:11:05 +00:00
/* Destroy submitted resources */
SDL_LockMutex(renderer->disposeLock);
2020-12-29 00:28:14 +00:00
for (i = 0; i < renderer->submittedColorTargetsToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyColorTarget(
renderer,
renderer->submittedColorTargetsToDestroy[i]
);
}
renderer->submittedColorTargetsToDestroyCount = 0;
2020-12-29 04:09:31 +00:00
for (i = 0; i < renderer->submittedDepthStencilTargetsToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyDepthStencilTarget(
renderer,
renderer->submittedDepthStencilTargetsToDestroy[i]
);
}
renderer->submittedDepthStencilTargetsToDestroyCount = 0;
2020-12-28 23:11:05 +00:00
for (i = 0; i < renderer->submittedTexturesToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyTexture(
renderer,
renderer->submittedTexturesToDestroy[i]
);
}
renderer->submittedTexturesToDestroyCount = 0;
2020-12-29 00:28:14 +00:00
for (i = 0; i < renderer->submittedBuffersToDestroyCount; i += 1)
2020-12-28 23:11:05 +00:00
{
2020-12-29 00:28:14 +00:00
VULKAN_INTERNAL_DestroyBuffer(
renderer,
renderer->submittedBuffersToDestroy[i]
2020-12-28 23:11:05 +00:00
);
2020-12-29 00:28:14 +00:00
}
renderer->submittedBuffersToDestroyCount = 0;
2020-12-28 23:11:05 +00:00
2020-12-29 00:28:14 +00:00
for (i = 0; i < renderer->submittedGraphicsPipelinesToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyGraphicsPipeline(
renderer,
renderer->submittedGraphicsPipelinesToDestroy[i]
);
2020-12-28 23:11:05 +00:00
}
2020-12-29 00:28:14 +00:00
renderer->submittedGraphicsPipelinesToDestroyCount = 0;
2020-12-31 07:02:12 +00:00
for (i = 0; i < renderer->submittedComputePipelinesToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyComputePipeline(
renderer,
renderer->submittedComputePipelinesToDestroy[i]
);
}
renderer->submittedComputePipelinesToDestroyCount = 0;
2020-12-29 00:42:51 +00:00
for (i = 0; i < renderer->submittedShaderModulesToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyShaderModule(
renderer,
renderer->submittedShaderModulesToDestroy[i]
);
}
renderer->submittedShaderModulesToDestroyCount = 0;
2020-12-29 00:56:49 +00:00
for (i = 0; i < renderer->submittedSamplersToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroySampler(
renderer,
renderer->submittedSamplersToDestroy[i]
);
}
renderer->submittedSamplersToDestroyCount = 0;
2020-12-29 01:35:18 +00:00
for (i = 0; i < renderer->submittedFramebuffersToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyFramebuffer(
renderer,
renderer->submittedFramebuffersToDestroy[i]
);
}
renderer->submittedFramebuffersToDestroyCount = 0;
2020-12-29 03:44:34 +00:00
for (i = 0; i < renderer->submittedRenderPassesToDestroyCount; i += 1)
{
VULKAN_INTERNAL_DestroyRenderPass(
renderer,
renderer->submittedRenderPassesToDestroy[i]
);
}
renderer->submittedRenderPassesToDestroyCount = 0;
2020-12-29 00:28:14 +00:00
/* Re-size submitted destroy lists */
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedColorTargetsToDestroy,
VulkanColorTarget*,
renderer->colorTargetsToDestroyCount,
renderer->submittedColorTargetsToDestroyCapacity,
renderer->colorTargetsToDestroyCount
)
2020-12-29 04:09:31 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedDepthStencilTargetsToDestroy,
VulkanDepthStencilTarget*,
renderer->depthStencilTargetsToDestroyCount,
renderer->submittedDepthStencilTargetsToDestroyCapacity,
renderer->depthStencilTargetsToDestroyCount
)
2020-12-29 00:28:14 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedTexturesToDestroy,
VulkanTexture*,
renderer->texturesToDestroyCount,
renderer->submittedTexturesToDestroyCapacity,
renderer->texturesToDestroyCount
)
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedBuffersToDestroy,
VulkanBuffer*,
renderer->buffersToDestroyCount,
renderer->submittedBuffersToDestroyCapacity,
renderer->buffersToDestroyCount
)
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedGraphicsPipelinesToDestroy,
VulkanGraphicsPipeline*,
renderer->graphicsPipelinesToDestroyCount,
renderer->submittedGraphicsPipelinesToDestroyCapacity,
renderer->graphicsPipelinesToDestroyCount
)
2020-12-28 23:11:05 +00:00
2020-12-31 07:02:12 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedComputePipelinesToDestroy,
VulkanComputePipeline*,
renderer->computePipelinesToDestroyCount,
renderer->submittedComputePipelinesToDestroyCapacity,
renderer->computePipelinesToDestroyCount
)
2020-12-29 00:42:51 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedShaderModulesToDestroy,
VkShaderModule,
renderer->shaderModulesToDestroyCount,
renderer->submittedShaderModulesToDestroyCapacity,
renderer->shaderModulesToDestroyCount
)
2020-12-29 00:56:49 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedSamplersToDestroy,
VkSampler,
renderer->samplersToDestroyCount,
renderer->submittedSamplersToDestroyCapacity,
renderer->samplersToDestroyCount
)
2020-12-29 01:35:18 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedFramebuffersToDestroy,
VulkanFramebuffer*,
renderer->framebuffersToDestroyCount,
renderer->submittedFramebuffersToDestroyCapacity,
renderer->framebuffersToDestroyCount
)
2020-12-29 03:44:34 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->submittedRenderPassesToDestroy,
VkRenderPass,
renderer->renderPassesToDestroyCount,
renderer->submittedRenderPassesToDestroyCapacity,
renderer->renderPassesToDestroyCount
)
2020-12-28 23:11:05 +00:00
/* Rotate destroy lists */
2020-12-29 00:56:49 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedColorTargetsToDestroy,
renderer->submittedColorTargetsToDestroyCount,
renderer->colorTargetsToDestroy,
renderer->colorTargetsToDestroyCount
)
2020-12-29 00:28:14 +00:00
2020-12-29 04:09:31 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedDepthStencilTargetsToDestroy,
renderer->submittedDepthStencilTargetsToDestroyCount,
renderer->depthStencilTargetsToDestroy,
renderer->depthStencilTargetsToDestroyCount
)
2020-12-29 00:56:49 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedTexturesToDestroy,
renderer->submittedTexturesToDestroyCount,
renderer->texturesToDestroy,
renderer->texturesToDestroyCount
)
2020-12-28 23:11:05 +00:00
2020-12-29 00:56:49 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedBuffersToDestroy,
renderer->submittedBuffersToDestroyCount,
renderer->buffersToDestroy,
renderer->buffersToDestroyCount
)
2020-12-29 00:28:14 +00:00
2020-12-29 00:56:49 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedGraphicsPipelinesToDestroy,
renderer->submittedGraphicsPipelinesToDestroyCount,
renderer->graphicsPipelinesToDestroy,
renderer->graphicsPipelinesToDestroyCount
)
2020-12-29 00:28:14 +00:00
2020-12-31 07:02:12 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedComputePipelinesToDestroy,
renderer->submittedComputePipelinesToDestroyCount,
renderer->computePipelinesToDestroy,
renderer->computePipelinesToDestroyCount
)
2020-12-29 00:56:49 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedShaderModulesToDestroy,
renderer->submittedShaderModulesToDestroyCount,
renderer->shaderModulesToDestroy,
renderer->shaderModulesToDestroyCount
)
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedSamplersToDestroy,
renderer->submittedSamplersToDestroyCount,
renderer->samplersToDestroy,
renderer->samplersToDestroyCount
)
2020-12-29 00:42:51 +00:00
2020-12-29 01:35:18 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedFramebuffersToDestroy,
renderer->submittedFramebuffersToDestroyCount,
renderer->framebuffersToDestroy,
renderer->framebuffersToDestroyCount
)
2020-12-29 03:44:34 +00:00
MOVE_ARRAY_CONTENTS_AND_RESET(
i,
renderer->submittedRenderPassesToDestroy,
renderer->submittedRenderPassesToDestroyCount,
renderer->renderPassesToDestroy,
renderer->renderPassesToDestroyCount
)
2020-12-28 23:11:05 +00:00
SDL_UnlockMutex(renderer->disposeLock);
2020-12-29 00:28:14 +00:00
/* Increment the frame index */
/* FIXME: need a better name, and to get rid of the magic value % 2 */
renderer->frameIndex = (renderer->frameIndex + 1) % 2;
/* Mark sub buffers of previously submitted buffers as unbound */
for (i = 0; i < renderer->submittedBufferCount; i += 1)
{
if (renderer->submittedBuffers[i] != NULL)
{
renderer->submittedBuffers[i]->boundSubmitted = 0;
for (j = 0; j < renderer->submittedBuffers[i]->subBufferCount; j += 1)
{
if (renderer->submittedBuffers[i]->subBuffers[j]->bound == renderer->frameIndex)
{
renderer->submittedBuffers[i]->subBuffers[j]->bound = -1;
}
}
renderer->submittedBuffers[i] = NULL;
}
}
renderer->submittedBufferCount = 0;
/* Mark currently bound buffers as submitted buffers */
if (renderer->buffersInUseCount > renderer->submittedBufferCapacity)
{
renderer->submittedBuffers = SDL_realloc(
renderer->submittedBuffers,
sizeof(VulkanBuffer*) * renderer->buffersInUseCount
);
renderer->submittedBufferCapacity = renderer->buffersInUseCount;
}
for (i = 0; i < renderer->buffersInUseCount; i += 1)
{
if (renderer->buffersInUse[i] != NULL)
{
renderer->buffersInUse[i]->bound = 0;
renderer->buffersInUse[i]->boundSubmitted = 1;
renderer->submittedBuffers[i] = renderer->buffersInUse[i];
renderer->buffersInUse[i] = NULL;
}
}
renderer->submittedBufferCount = renderer->buffersInUseCount;
renderer->buffersInUseCount = 0;
2020-12-28 23:11:05 +00:00
}
2020-12-21 23:44:43 +00:00
/* Swapchain */
static inline VkExtent2D VULKAN_INTERNAL_ChooseSwapExtent(
void* windowHandle,
const VkSurfaceCapabilitiesKHR capabilities
) {
VkExtent2D actualExtent;
int32_t drawableWidth, drawableHeight;
if (capabilities.currentExtent.width != UINT32_MAX)
{
return capabilities.currentExtent;
}
else
{
SDL_Vulkan_GetDrawableSize(
(SDL_Window*) windowHandle,
&drawableWidth,
&drawableHeight
);
actualExtent.width = drawableWidth;
actualExtent.height = drawableHeight;
return actualExtent;
}
}
static uint8_t VULKAN_INTERNAL_QuerySwapChainSupport(
VulkanRenderer *renderer,
VkPhysicalDevice physicalDevice,
VkSurfaceKHR surface,
SwapChainSupportDetails *outputDetails
) {
VkResult result;
uint32_t formatCount;
uint32_t presentModeCount;
result = renderer->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
physicalDevice,
surface,
&outputDetails->capabilities
);
if (result != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-21 23:44:43 +00:00
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR: %s",
VkErrorMessages(result)
);
return 0;
}
renderer->vkGetPhysicalDeviceSurfaceFormatsKHR(
physicalDevice,
surface,
&formatCount,
NULL
);
if (formatCount != 0)
{
outputDetails->formats = (VkSurfaceFormatKHR*) SDL_malloc(
sizeof(VkSurfaceFormatKHR) * formatCount
);
outputDetails->formatsLength = formatCount;
if (!outputDetails->formats)
{
SDL_OutOfMemory();
return 0;
}
result = renderer->vkGetPhysicalDeviceSurfaceFormatsKHR(
physicalDevice,
surface,
&formatCount,
outputDetails->formats
);
if (result != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-21 23:44:43 +00:00
"vkGetPhysicalDeviceSurfaceFormatsKHR: %s",
VkErrorMessages(result)
);
SDL_free(outputDetails->formats);
return 0;
}
}
renderer->vkGetPhysicalDeviceSurfacePresentModesKHR(
physicalDevice,
surface,
&presentModeCount,
NULL
);
if (presentModeCount != 0)
{
outputDetails->presentModes = (VkPresentModeKHR*) SDL_malloc(
sizeof(VkPresentModeKHR) * presentModeCount
);
outputDetails->presentModesLength = presentModeCount;
if (!outputDetails->presentModes)
{
SDL_OutOfMemory();
return 0;
}
result = renderer->vkGetPhysicalDeviceSurfacePresentModesKHR(
physicalDevice,
surface,
&presentModeCount,
outputDetails->presentModes
);
if (result != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-21 23:44:43 +00:00
"vkGetPhysicalDeviceSurfacePresentModesKHR: %s",
VkErrorMessages(result)
);
SDL_free(outputDetails->formats);
SDL_free(outputDetails->presentModes);
return 0;
}
}
return 1;
}
static uint8_t VULKAN_INTERNAL_ChooseSwapSurfaceFormat(
VkFormat desiredFormat,
VkSurfaceFormatKHR *availableFormats,
uint32_t availableFormatsLength,
VkSurfaceFormatKHR *outputFormat
) {
uint32_t i;
for (i = 0; i < availableFormatsLength; i += 1)
{
if ( availableFormats[i].format == desiredFormat &&
availableFormats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR )
{
*outputFormat = availableFormats[i];
return 1;
}
}
2021-01-05 23:00:51 +00:00
Refresh_LogError("Desired surface format is unavailable.");
2020-12-21 23:44:43 +00:00
return 0;
}
static uint8_t VULKAN_INTERNAL_ChooseSwapPresentMode(
2021-01-05 23:00:51 +00:00
Refresh_PresentMode desiredPresentInterval,
2020-12-21 23:44:43 +00:00
VkPresentModeKHR *availablePresentModes,
uint32_t availablePresentModesLength,
VkPresentModeKHR *outputPresentMode
) {
#define CHECK_MODE(m) \
for (i = 0; i < availablePresentModesLength; i += 1) \
{ \
if (availablePresentModes[i] == m) \
{ \
*outputPresentMode = m; \
2021-01-05 23:00:51 +00:00
Refresh_LogInfo("Using " #m "!"); \
2020-12-21 23:44:43 +00:00
return 1; \
} \
} \
2021-01-05 23:00:51 +00:00
Refresh_LogInfo(#m " unsupported.");
2020-12-21 23:44:43 +00:00
uint32_t i;
if (desiredPresentInterval == REFRESH_PRESENTMODE_IMMEDIATE)
{
CHECK_MODE(VK_PRESENT_MODE_IMMEDIATE_KHR)
}
else if (desiredPresentInterval == REFRESH_PRESENTMODE_MAILBOX)
{
CHECK_MODE(VK_PRESENT_MODE_MAILBOX_KHR)
}
else if (desiredPresentInterval == REFRESH_PRESENTMODE_FIFO)
{
CHECK_MODE(VK_PRESENT_MODE_FIFO_KHR)
}
else if (desiredPresentInterval == REFRESH_PRESENTMODE_FIFO_RELAXED)
{
CHECK_MODE(VK_PRESENT_MODE_FIFO_RELAXED_KHR)
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-21 23:44:43 +00:00
"Unrecognized PresentInterval: %d",
desiredPresentInterval
);
return 0;
}
#undef CHECK_MODE
2021-01-05 23:00:51 +00:00
Refresh_LogInfo("Fall back to VK_PRESENT_MODE_FIFO_KHR.");
2020-12-21 23:44:43 +00:00
*outputPresentMode = VK_PRESENT_MODE_FIFO_KHR;
return 1;
}
static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain(
VulkanRenderer *renderer
) {
VkResult vulkanResult;
SwapChainSupportDetails swapChainSupportDetails;
VkSurfaceFormatKHR surfaceFormat;
VkPresentModeKHR presentMode;
VkExtent2D extent;
uint32_t imageCount, swapChainImageCount, i;
VkSwapchainCreateInfoKHR swapChainCreateInfo;
VkImage *swapChainImages;
VkImageViewCreateInfo createInfo;
VkImageView swapChainImageView;
if (!VULKAN_INTERNAL_QuerySwapChainSupport(
renderer,
renderer->physicalDevice,
renderer->surface,
&swapChainSupportDetails
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Device does not support swap chain creation");
2020-12-21 23:44:43 +00:00
return CREATE_SWAPCHAIN_FAIL;
}
renderer->swapChainFormat = VK_FORMAT_B8G8R8A8_UNORM;
renderer->swapChainSwizzle.r = VK_COMPONENT_SWIZZLE_IDENTITY;
renderer->swapChainSwizzle.g = VK_COMPONENT_SWIZZLE_IDENTITY;
renderer->swapChainSwizzle.b = VK_COMPONENT_SWIZZLE_IDENTITY;
renderer->swapChainSwizzle.a = VK_COMPONENT_SWIZZLE_IDENTITY;
if (!VULKAN_INTERNAL_ChooseSwapSurfaceFormat(
renderer->swapChainFormat,
swapChainSupportDetails.formats,
swapChainSupportDetails.formatsLength,
&surfaceFormat
)) {
SDL_free(swapChainSupportDetails.formats);
SDL_free(swapChainSupportDetails.presentModes);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Device does not support swap chain format");
2020-12-21 23:44:43 +00:00
return CREATE_SWAPCHAIN_FAIL;
}
if (!VULKAN_INTERNAL_ChooseSwapPresentMode(
renderer->presentMode,
swapChainSupportDetails.presentModes,
swapChainSupportDetails.presentModesLength,
&presentMode
)) {
SDL_free(swapChainSupportDetails.formats);
SDL_free(swapChainSupportDetails.presentModes);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Device does not support swap chain present mode");
2020-12-21 23:44:43 +00:00
return CREATE_SWAPCHAIN_FAIL;
}
extent = VULKAN_INTERNAL_ChooseSwapExtent(
renderer->deviceWindowHandle,
swapChainSupportDetails.capabilities
);
if (extent.width == 0 || extent.height == 0)
{
return CREATE_SWAPCHAIN_SURFACE_ZERO;
}
imageCount = swapChainSupportDetails.capabilities.minImageCount + 1;
if ( swapChainSupportDetails.capabilities.maxImageCount > 0 &&
imageCount > swapChainSupportDetails.capabilities.maxImageCount )
{
imageCount = swapChainSupportDetails.capabilities.maxImageCount;
}
if (presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
{
/* Required for proper triple-buffering.
* Note that this is below the above maxImageCount check!
* If the driver advertises MAILBOX but does not support 3 swap
* images, it's not real mailbox support, so let it fail hard.
* -flibit
*/
imageCount = SDL_max(imageCount, 3);
}
swapChainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapChainCreateInfo.pNext = NULL;
swapChainCreateInfo.flags = 0;
swapChainCreateInfo.surface = renderer->surface;
swapChainCreateInfo.minImageCount = imageCount;
swapChainCreateInfo.imageFormat = surfaceFormat.format;
swapChainCreateInfo.imageColorSpace = surfaceFormat.colorSpace;
swapChainCreateInfo.imageExtent = extent;
swapChainCreateInfo.imageArrayLayers = 1;
swapChainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapChainCreateInfo.queueFamilyIndexCount = 0;
swapChainCreateInfo.pQueueFamilyIndices = NULL;
swapChainCreateInfo.preTransform = swapChainSupportDetails.capabilities.currentTransform;
swapChainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapChainCreateInfo.presentMode = presentMode;
swapChainCreateInfo.clipped = VK_TRUE;
swapChainCreateInfo.oldSwapchain = VK_NULL_HANDLE;
vulkanResult = renderer->vkCreateSwapchainKHR(
renderer->logicalDevice,
&swapChainCreateInfo,
NULL,
&renderer->swapChain
);
SDL_free(swapChainSupportDetails.formats);
SDL_free(swapChainSupportDetails.presentModes);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateSwapchainKHR", vulkanResult);
return CREATE_SWAPCHAIN_FAIL;
}
renderer->vkGetSwapchainImagesKHR(
renderer->logicalDevice,
renderer->swapChain,
&swapChainImageCount,
NULL
);
renderer->swapChainImages = (VkImage*) SDL_malloc(
sizeof(VkImage) * swapChainImageCount
);
if (!renderer->swapChainImages)
{
SDL_OutOfMemory();
return CREATE_SWAPCHAIN_FAIL;
}
renderer->swapChainImageViews = (VkImageView*) SDL_malloc(
sizeof(VkImageView) * swapChainImageCount
);
if (!renderer->swapChainImageViews)
{
SDL_OutOfMemory();
return CREATE_SWAPCHAIN_FAIL;
}
renderer->swapChainResourceAccessTypes = (VulkanResourceAccessType*) SDL_malloc(
sizeof(VulkanResourceAccessType) * swapChainImageCount
);
if (!renderer->swapChainResourceAccessTypes)
{
SDL_OutOfMemory();
return CREATE_SWAPCHAIN_FAIL;
}
2021-01-03 03:03:25 +00:00
renderer->swapChainQueueFamilyIndices = (uint32_t*) SDL_malloc(
sizeof(uint32_t) * swapChainImageCount
);
if (!renderer->swapChainQueueFamilyIndices)
{
SDL_OutOfMemory();
return CREATE_SWAPCHAIN_FAIL;
}
2020-12-21 23:44:43 +00:00
swapChainImages = SDL_stack_alloc(VkImage, swapChainImageCount);
renderer->vkGetSwapchainImagesKHR(
renderer->logicalDevice,
renderer->swapChain,
&swapChainImageCount,
swapChainImages
);
renderer->swapChainImageCount = swapChainImageCount;
renderer->swapChainExtent = extent;
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.pNext = NULL;
createInfo.flags = 0;
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = surfaceFormat.format;
createInfo.components = renderer->swapChainSwizzle;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
for (i = 0; i < swapChainImageCount; i += 1)
{
createInfo.image = swapChainImages[i];
vulkanResult = renderer->vkCreateImageView(
renderer->logicalDevice,
&createInfo,
NULL,
&swapChainImageView
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateImageView", vulkanResult);
SDL_stack_free(swapChainImages);
return CREATE_SWAPCHAIN_FAIL;
}
renderer->swapChainImages[i] = swapChainImages[i];
renderer->swapChainImageViews[i] = swapChainImageView;
renderer->swapChainResourceAccessTypes[i] = RESOURCE_ACCESS_NONE;
2021-01-03 03:03:25 +00:00
renderer->swapChainQueueFamilyIndices[i] = renderer->queueFamilyIndices.graphicsFamily;
2020-12-21 23:44:43 +00:00
}
SDL_stack_free(swapChainImages);
return CREATE_SWAPCHAIN_SUCCESS;
2020-12-19 00:39:03 +00:00
}
2020-12-21 23:44:43 +00:00
static void VULKAN_INTERNAL_RecreateSwapchain(VulkanRenderer* renderer)
{
CreateSwapchainResult createSwapchainResult;
SwapChainSupportDetails swapChainSupportDetails;
VkExtent2D extent;
2020-12-19 04:08:07 +00:00
2020-12-21 23:44:43 +00:00
renderer->vkDeviceWaitIdle(renderer->logicalDevice);
2020-12-19 04:08:07 +00:00
2020-12-21 23:44:43 +00:00
VULKAN_INTERNAL_QuerySwapChainSupport(
renderer,
renderer->physicalDevice,
renderer->surface,
&swapChainSupportDetails
);
extent = VULKAN_INTERNAL_ChooseSwapExtent(
renderer->deviceWindowHandle,
swapChainSupportDetails.capabilities
);
if (extent.width == 0 || extent.height == 0)
2020-12-19 04:08:07 +00:00
{
return;
}
2020-12-21 23:44:43 +00:00
VULKAN_INTERNAL_DestroySwapchain(renderer);
createSwapchainResult = VULKAN_INTERNAL_CreateSwapchain(renderer);
2020-12-19 04:08:07 +00:00
2020-12-21 23:44:43 +00:00
if (createSwapchainResult == CREATE_SWAPCHAIN_FAIL)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to recreate swapchain");
2020-12-21 23:44:43 +00:00
return;
2020-12-19 04:08:07 +00:00
}
2020-12-21 23:44:43 +00:00
renderer->vkDeviceWaitIdle(renderer->logicalDevice);
2020-12-19 04:08:07 +00:00
}
2020-12-19 01:03:26 +00:00
/* Data Buffer */
/* buffer should be an alloc'd but uninitialized VulkanTexture */
static uint8_t VULKAN_INTERNAL_CreateBuffer(
VulkanRenderer *renderer,
VkDeviceSize size,
VulkanResourceAccessType resourceAccessType,
VkBufferUsageFlags usage,
2020-12-19 04:08:07 +00:00
uint32_t subBufferCount,
2020-12-19 01:03:26 +00:00
VulkanBuffer *buffer
) {
VkResult vulkanResult;
VkBufferCreateInfo bufferCreateInfo;
uint8_t findMemoryResult;
2020-12-19 04:08:07 +00:00
uint32_t i;
buffer->size = size;
2020-12-23 06:05:01 +00:00
buffer->currentSubBufferIndex = 0;
2020-12-19 04:08:07 +00:00
buffer->bound = 0;
buffer->boundSubmitted = 0;
buffer->resourceAccessType = resourceAccessType;
buffer->usage = usage;
buffer->subBufferCount = subBufferCount;
buffer->subBuffers = SDL_malloc(
2020-12-22 01:59:08 +00:00
sizeof(VulkanSubBuffer*) * buffer->subBufferCount
2020-12-19 04:08:07 +00:00
);
2020-12-19 01:03:26 +00:00
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.pNext = NULL;
bufferCreateInfo.flags = 0;
bufferCreateInfo.size = size;
bufferCreateInfo.usage = usage;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
bufferCreateInfo.queueFamilyIndexCount = 1;
bufferCreateInfo.pQueueFamilyIndices = &renderer->queueFamilyIndices.graphicsFamily;
2020-12-19 04:08:07 +00:00
for (i = 0; i < subBufferCount; i += 1)
2020-12-19 01:03:26 +00:00
{
2020-12-22 01:59:08 +00:00
buffer->subBuffers[i] = SDL_malloc(
sizeof(VulkanSubBuffer) * buffer->subBufferCount
);
2020-12-19 04:08:07 +00:00
vulkanResult = renderer->vkCreateBuffer(
renderer->logicalDevice,
&bufferCreateInfo,
NULL,
&buffer->subBuffers[i]->buffer
);
2020-12-19 01:03:26 +00:00
2020-12-19 04:08:07 +00:00
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateBuffer", vulkanResult);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create VkBuffer");
2020-12-19 04:08:07 +00:00
return 0;
}
2020-12-19 01:03:26 +00:00
2020-12-19 04:08:07 +00:00
findMemoryResult = VULKAN_INTERNAL_FindAvailableMemory(
renderer,
buffer->subBuffers[i]->buffer,
VK_NULL_HANDLE,
&buffer->subBuffers[i]->allocation,
&buffer->subBuffers[i]->offset,
&buffer->subBuffers[i]->size
);
2020-12-19 01:03:26 +00:00
2020-12-19 04:08:07 +00:00
/* We're out of available memory */
if (findMemoryResult == 2)
{
2021-01-05 23:00:51 +00:00
Refresh_LogWarn("Out of buffer memory!");
2020-12-19 04:08:07 +00:00
return 2;
}
else if (findMemoryResult == 0)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to find buffer memory!");
2020-12-19 04:08:07 +00:00
return 0;
}
2020-12-19 01:03:26 +00:00
2021-01-03 21:12:12 +00:00
SDL_LockMutex(buffer->subBuffers[i]->allocation->memoryLock);
2020-12-19 04:08:07 +00:00
vulkanResult = renderer->vkBindBufferMemory(
renderer->logicalDevice,
buffer->subBuffers[i]->buffer,
buffer->subBuffers[i]->allocation->memory,
buffer->subBuffers[i]->offset
);
2020-12-19 01:03:26 +00:00
2021-01-03 21:12:12 +00:00
SDL_UnlockMutex(buffer->subBuffers[i]->allocation->memoryLock);
2020-12-19 04:08:07 +00:00
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to bind buffer memory!");
2020-12-19 04:08:07 +00:00
return 0;
}
2020-12-19 01:03:26 +00:00
2020-12-19 04:08:07 +00:00
buffer->subBuffers[i]->resourceAccessType = resourceAccessType;
2020-12-23 06:05:01 +00:00
buffer->subBuffers[i]->bound = -1;
2020-12-19 04:08:07 +00:00
}
2020-12-19 01:03:26 +00:00
return 1;
}
2020-12-18 22:35:33 +00:00
/* Command Buffers */
2020-12-17 04:04:47 +00:00
2021-01-02 06:07:15 +00:00
static void VULKAN_INTERNAL_BeginCommandBuffer(
VulkanRenderer *renderer,
VulkanCommandBuffer *commandBuffer
) {
2020-12-17 04:04:47 +00:00
VkCommandBufferBeginInfo beginInfo;
VkResult result;
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.pNext = NULL;
2021-01-02 06:07:15 +00:00
beginInfo.flags = 0;
2020-12-17 04:04:47 +00:00
beginInfo.pInheritanceInfo = NULL;
2021-01-02 06:07:15 +00:00
if (!commandBuffer->fixed)
2020-12-17 04:04:47 +00:00
{
2021-01-02 06:07:15 +00:00
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
2020-12-17 04:04:47 +00:00
}
result = renderer->vkBeginCommandBuffer(
2021-01-02 06:07:15 +00:00
commandBuffer->commandBuffer,
2020-12-17 04:04:47 +00:00
&beginInfo
);
if (result != VK_SUCCESS)
{
LogVulkanResult("vkBeginCommandBuffer", result);
}
}
2020-12-22 02:34:57 +00:00
static void VULKAN_INTERNAL_EndCommandBuffer(
2021-01-02 06:07:15 +00:00
VulkanRenderer* renderer,
VulkanCommandBuffer *commandBuffer
2020-12-22 02:34:57 +00:00
) {
VkResult result;
result = renderer->vkEndCommandBuffer(
2021-01-02 06:07:15 +00:00
commandBuffer->commandBuffer
2020-12-22 02:34:57 +00:00
);
if (result != VK_SUCCESS)
{
LogVulkanResult("vkEndCommandBuffer", result);
}
2021-01-02 06:07:15 +00:00
commandBuffer->currentComputePipeline = NULL;
commandBuffer->boundComputeBufferCount = 0;
2020-12-22 02:34:57 +00:00
}
2020-12-17 04:04:47 +00:00
/* Public API */
2020-12-17 01:23:49 +00:00
static void VULKAN_DestroyDevice(
2021-01-05 23:00:51 +00:00
Refresh_Device *device
2020-12-17 01:23:49 +00:00
) {
2020-12-28 22:57:14 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) device->driverData;
VkResult waitResult;
2021-01-03 02:02:20 +00:00
CommandPoolHashArray commandPoolHashArray;
2020-12-31 07:02:12 +00:00
GraphicsPipelineLayoutHashArray graphicsPipelineLayoutHashArray;
ComputePipelineLayoutHashArray computePipelineLayoutHashArray;
2020-12-28 22:57:14 +00:00
VulkanMemorySubAllocator *allocator;
uint32_t i, j, k;
waitResult = renderer->vkDeviceWaitIdle(renderer->logicalDevice);
if (waitResult != VK_SUCCESS)
{
LogVulkanResult("vkDeviceWaitIdle", waitResult);
}
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyVertexUniformBuffer);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyFragmentUniformBuffer);
2020-12-31 04:41:42 +00:00
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyComputeUniformBuffer);
2020-12-29 00:28:14 +00:00
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->vertexUBO);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->fragmentUBO);
2020-12-31 04:41:42 +00:00
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->computeUBO);
2020-12-28 22:57:14 +00:00
/* We have to do this twice so the rotation happens correctly */
2020-12-29 03:44:34 +00:00
VULKAN_INTERNAL_PostWorkCleanup(renderer);
VULKAN_INTERNAL_PostWorkCleanup(renderer);
2020-12-28 22:57:14 +00:00
VULKAN_INTERNAL_DestroyTextureStagingBuffer(renderer);
2021-01-02 21:31:17 +00:00
renderer->vkDestroySemaphore(
renderer->logicalDevice,
renderer->transferFinishedSemaphore,
NULL
);
2020-12-28 22:57:14 +00:00
renderer->vkDestroySemaphore(
renderer->logicalDevice,
renderer->imageAvailableSemaphore,
NULL
);
renderer->vkDestroySemaphore(
renderer->logicalDevice,
renderer->renderFinishedSemaphore,
NULL
);
renderer->vkDestroyFence(
renderer->logicalDevice,
renderer->inFlightFence,
NULL
);
2021-01-03 02:02:20 +00:00
for (i = 0; i < NUM_COMMAND_POOL_BUCKETS; i += 1)
{
commandPoolHashArray = renderer->commandPoolHashTable.buckets[i];
for (j = 0; j < commandPoolHashArray.count; j += 1)
{
VULKAN_INTERNAL_DestroyCommandPool(
renderer,
commandPoolHashArray.elements[j].value
);
}
if (commandPoolHashArray.elements != NULL)
{
SDL_free(commandPoolHashArray.elements);
}
}
2020-12-28 22:57:14 +00:00
renderer->vkDestroyCommandPool(
renderer->logicalDevice,
renderer->transferCommandPool,
NULL
);
2020-12-28 22:57:14 +00:00
for (i = 0; i < NUM_PIPELINE_LAYOUT_BUCKETS; i += 1)
{
2020-12-31 07:02:12 +00:00
graphicsPipelineLayoutHashArray = renderer->graphicsPipelineLayoutHashTable.buckets[i];
for (j = 0; j < graphicsPipelineLayoutHashArray.count; j += 1)
2020-12-28 22:57:14 +00:00
{
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_DestroyImageDescriptorSetCache(
2020-12-28 22:57:14 +00:00
renderer,
2020-12-31 07:02:12 +00:00
graphicsPipelineLayoutHashArray.elements[j].value->vertexSamplerDescriptorSetCache
2020-12-28 22:57:14 +00:00
);
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_DestroyImageDescriptorSetCache(
2020-12-28 22:57:14 +00:00
renderer,
2020-12-31 07:02:12 +00:00
graphicsPipelineLayoutHashArray.elements[j].value->fragmentSamplerDescriptorSetCache
2020-12-28 22:57:14 +00:00
);
renderer->vkDestroyPipelineLayout(
renderer->logicalDevice,
2020-12-31 07:02:12 +00:00
graphicsPipelineLayoutHashArray.elements[j].value->pipelineLayout,
2020-12-28 22:57:14 +00:00
NULL
);
}
2020-12-31 07:02:12 +00:00
if (graphicsPipelineLayoutHashArray.elements != NULL)
2020-12-28 22:57:14 +00:00
{
2020-12-31 07:02:12 +00:00
SDL_free(graphicsPipelineLayoutHashArray.elements);
}
computePipelineLayoutHashArray = renderer->computePipelineLayoutHashTable.buckets[i];
for (j = 0; j < computePipelineLayoutHashArray.count; j += 1)
{
VULKAN_INTERNAL_DestroyBufferDescriptorSetCache(
renderer,
computePipelineLayoutHashArray.elements[j].value->bufferDescriptorSetCache
);
VULKAN_INTERNAL_DestroyImageDescriptorSetCache(
renderer,
computePipelineLayoutHashArray.elements[j].value->imageDescriptorSetCache
);
renderer->vkDestroyPipelineLayout(
renderer->logicalDevice,
computePipelineLayoutHashArray.elements[j].value->pipelineLayout,
NULL
);
}
if (computePipelineLayoutHashArray.elements != NULL)
{
SDL_free(computePipelineLayoutHashArray.elements);
2020-12-28 22:57:14 +00:00
}
}
renderer->vkDestroyDescriptorPool(
renderer->logicalDevice,
renderer->defaultDescriptorPool,
NULL
);
for (i = 0; i < NUM_DESCRIPTOR_SET_LAYOUT_BUCKETS; i += 1)
{
2020-12-29 23:05:26 +00:00
for (j = 0; j < renderer->descriptorSetLayoutHashTable.buckets[i].count; j += 1)
2020-12-28 22:57:14 +00:00
{
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
2020-12-29 23:05:26 +00:00
renderer->descriptorSetLayoutHashTable.buckets[i].elements[j].value,
2020-12-28 22:57:14 +00:00
NULL
);
}
2020-12-29 23:05:26 +00:00
SDL_free(renderer->descriptorSetLayoutHashTable.buckets[i].elements);
2020-12-28 22:57:14 +00:00
}
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->emptyVertexSamplerLayout,
NULL
);
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->emptyFragmentSamplerLayout,
NULL
);
2020-12-30 01:33:56 +00:00
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->emptyComputeBufferDescriptorSetLayout,
NULL
);
2020-12-31 04:41:42 +00:00
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->emptyComputeImageDescriptorSetLayout,
NULL
);
2020-12-29 00:28:14 +00:00
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->vertexParamLayout,
NULL
);
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->fragmentParamLayout,
NULL
);
2020-12-31 04:41:42 +00:00
renderer->vkDestroyDescriptorSetLayout(
renderer->logicalDevice,
renderer->computeParamLayout,
NULL
);
2020-12-28 22:57:14 +00:00
VULKAN_INTERNAL_DestroySwapchain(renderer);
if (!renderer->headless)
{
renderer->vkDestroySurfaceKHR(
renderer->instance,
renderer->surface,
NULL
);
}
for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1)
{
allocator = &renderer->memoryAllocator->subAllocators[i];
for (j = 0; j < allocator->allocationCount; j += 1)
{
for (k = 0; k < allocator->allocations[j]->freeRegionCount; k += 1)
{
SDL_free(allocator->allocations[j]->freeRegions[k]);
}
SDL_free(allocator->allocations[j]->freeRegions);
renderer->vkFreeMemory(
renderer->logicalDevice,
allocator->allocations[j]->memory,
NULL
);
2021-01-03 21:12:12 +00:00
SDL_DestroyMutex(allocator->allocations[j]->memoryLock);
2020-12-28 22:57:14 +00:00
SDL_free(allocator->allocations[j]);
}
SDL_free(allocator->allocations);
SDL_free(allocator->sortedFreeRegions);
}
SDL_free(renderer->memoryAllocator);
SDL_DestroyMutex(renderer->allocatorLock);
2020-12-28 23:11:05 +00:00
SDL_DestroyMutex(renderer->disposeLock);
2021-01-02 06:07:15 +00:00
SDL_DestroyMutex(renderer->uniformBufferLock);
SDL_DestroyMutex(renderer->descriptorSetLock);
SDL_DestroyMutex(renderer->boundBufferLock);
2021-01-03 21:17:51 +00:00
SDL_DestroyMutex(renderer->stagingLock);
2020-12-28 22:57:14 +00:00
SDL_free(renderer->buffersInUse);
2021-01-14 01:37:54 +00:00
if (!renderer->usesExternalDevice)
{
renderer->vkDestroyDevice(renderer->logicalDevice, NULL);
renderer->vkDestroyInstance(renderer->instance, NULL);
}
2020-12-28 22:57:14 +00:00
SDL_free(renderer);
SDL_free(device);
2020-12-17 01:23:49 +00:00
}
static void VULKAN_Clear(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_Rect *clearRect,
Refresh_ClearOptions options,
Refresh_Color *colors,
2020-12-29 06:19:46 +00:00
uint32_t colorCount,
2020-12-17 01:23:49 +00:00
float depth,
int32_t stencil
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2020-12-29 06:19:46 +00:00
uint32_t attachmentCount, i;
VkClearAttachment clearAttachments[MAX_COLOR_TARGET_BINDINGS + 1];
VkClearRect vulkanClearRect;
VkClearValue clearValues[4];
uint8_t shouldClearColor = options & REFRESH_CLEAROPTIONS_COLOR;
uint8_t shouldClearDepth = options & REFRESH_CLEAROPTIONS_DEPTH;
uint8_t shouldClearStencil = options & REFRESH_CLEAROPTIONS_STENCIL;
uint8_t shouldClearDepthStencil = (
(shouldClearDepth || shouldClearStencil) &&
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentFramebuffer->depthStencilTarget != NULL
2020-12-29 06:19:46 +00:00
);
if (!shouldClearColor && !shouldClearDepthStencil)
{
return;
}
vulkanClearRect.baseArrayLayer = 0;
vulkanClearRect.layerCount = 1;
vulkanClearRect.rect.offset.x = clearRect->x;
vulkanClearRect.rect.offset.y = clearRect->y;
vulkanClearRect.rect.extent.width = clearRect->w;
vulkanClearRect.rect.extent.height = clearRect->h;
attachmentCount = 0;
if (shouldClearColor)
{
for (i = 0; i < colorCount; i += 1)
{
clearValues[i].color.float32[0] = colors[i].r / 255.0f;
clearValues[i].color.float32[1] = colors[i].g / 255.0f;
clearValues[i].color.float32[2] = colors[i].b / 255.0f;
clearValues[i].color.float32[3] = colors[i].a / 255.0f;
}
for (i = 0; i < colorCount; i += 1)
{
clearAttachments[attachmentCount].aspectMask =
VK_IMAGE_ASPECT_COLOR_BIT;
clearAttachments[attachmentCount].colorAttachment =
attachmentCount;
clearAttachments[attachmentCount].clearValue =
clearValues[attachmentCount];
attachmentCount += 1;
/* Do NOT clear the multisample image here!
* Vulkan treats them both as the same color attachment.
* Vulkan is a very good and not confusing at all API.
*/
}
}
if (shouldClearDepthStencil)
{
clearAttachments[attachmentCount].aspectMask = 0;
clearAttachments[attachmentCount].colorAttachment = 0;
if (shouldClearDepth)
{
if (depth < 0.0f)
{
depth = 0.0f;
}
else if (depth > 1.0f)
{
depth = 1.0f;
}
clearAttachments[attachmentCount].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
clearAttachments[attachmentCount].clearValue.depthStencil.depth = depth;
}
else
{
clearAttachments[attachmentCount].clearValue.depthStencil.depth = 0.0f;
}
if (shouldClearStencil)
{
clearAttachments[attachmentCount].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
clearAttachments[attachmentCount].clearValue.depthStencil.stencil = stencil;
}
else
{
clearAttachments[attachmentCount].clearValue.depthStencil.stencil = 0;
}
attachmentCount += 1;
}
2021-01-02 06:07:15 +00:00
renderer->vkCmdClearAttachments(
vulkanCommandBuffer->commandBuffer,
2020-12-29 06:19:46 +00:00
attachmentCount,
clearAttachments,
1,
&vulkanClearRect
2021-01-02 06:07:15 +00:00
);
2020-12-17 01:23:49 +00:00
}
2020-12-20 09:29:15 +00:00
static void VULKAN_DrawInstancedPrimitives(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-17 01:23:49 +00:00
uint32_t baseVertex,
uint32_t startIndex,
uint32_t primitiveCount,
2020-12-20 09:29:15 +00:00
uint32_t instanceCount,
uint32_t vertexParamOffset,
uint32_t fragmentParamOffset
2020-12-17 01:23:49 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2020-12-20 09:29:15 +00:00
VkDescriptorSet descriptorSets[4];
uint32_t dynamicOffsets[2];
2021-01-02 06:07:15 +00:00
descriptorSets[0] = vulkanCommandBuffer->currentGraphicsPipeline->vertexSamplerDescriptorSet;
descriptorSets[1] = vulkanCommandBuffer->currentGraphicsPipeline->fragmentSamplerDescriptorSet;
descriptorSets[2] = vulkanCommandBuffer->currentGraphicsPipeline->vertexUBODescriptorSet;
descriptorSets[3] = vulkanCommandBuffer->currentGraphicsPipeline->fragmentUBODescriptorSet;
2020-12-20 09:29:15 +00:00
dynamicOffsets[0] = vertexParamOffset;
dynamicOffsets[1] = fragmentParamOffset;
2020-12-20 09:29:15 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdBindDescriptorSets(
vulkanCommandBuffer->commandBuffer,
2020-12-20 09:29:15 +00:00
VK_PIPELINE_BIND_POINT_GRAPHICS,
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline->pipelineLayout->pipelineLayout,
2020-12-20 09:29:15 +00:00
0,
4,
descriptorSets,
2,
dynamicOffsets
2021-01-02 06:07:15 +00:00
);
2020-12-20 09:29:15 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdDrawIndexed(
vulkanCommandBuffer->commandBuffer,
2020-12-23 06:56:26 +00:00
PrimitiveVerts(
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline->primitiveType,
2020-12-23 06:56:26 +00:00
primitiveCount
),
2020-12-20 09:29:15 +00:00
instanceCount,
startIndex,
baseVertex,
0
2021-01-02 06:07:15 +00:00
);
2020-12-17 01:23:49 +00:00
}
2020-12-20 09:29:15 +00:00
static void VULKAN_DrawIndexedPrimitives(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-17 01:23:49 +00:00
uint32_t baseVertex,
uint32_t startIndex,
uint32_t primitiveCount,
uint32_t vertexParamOffset,
uint32_t fragmentParamOffset
2020-12-17 01:23:49 +00:00
) {
2020-12-20 09:29:15 +00:00
VULKAN_DrawInstancedPrimitives(
driverData,
2021-01-02 06:07:15 +00:00
commandBuffer,
2020-12-20 09:29:15 +00:00
baseVertex,
startIndex,
primitiveCount,
1,
vertexParamOffset,
fragmentParamOffset
2020-12-20 09:29:15 +00:00
);
2020-12-17 01:23:49 +00:00
}
static void VULKAN_DrawPrimitives(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-17 01:23:49 +00:00
uint32_t vertexStart,
uint32_t primitiveCount,
2020-12-31 04:39:47 +00:00
uint32_t vertexParamOffset,
uint32_t fragmentParamOffset
2020-12-17 01:23:49 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2021-01-02 21:31:17 +00:00
2020-12-20 09:29:15 +00:00
VkDescriptorSet descriptorSets[4];
uint32_t dynamicOffsets[2];
2021-01-02 06:07:15 +00:00
descriptorSets[0] = vulkanCommandBuffer->currentGraphicsPipeline->vertexSamplerDescriptorSet;
descriptorSets[1] = vulkanCommandBuffer->currentGraphicsPipeline->fragmentSamplerDescriptorSet;
descriptorSets[2] = vulkanCommandBuffer->currentGraphicsPipeline->vertexUBODescriptorSet;
descriptorSets[3] = vulkanCommandBuffer->currentGraphicsPipeline->fragmentUBODescriptorSet;
2020-12-20 09:29:15 +00:00
2020-12-31 04:39:47 +00:00
dynamicOffsets[0] = vertexParamOffset;
dynamicOffsets[1] = fragmentParamOffset;
2020-12-20 09:29:15 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdBindDescriptorSets(
vulkanCommandBuffer->commandBuffer,
2020-12-20 09:29:15 +00:00
VK_PIPELINE_BIND_POINT_GRAPHICS,
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline->pipelineLayout->pipelineLayout,
2020-12-20 09:29:15 +00:00
0,
4,
descriptorSets,
2,
dynamicOffsets
2021-01-02 06:07:15 +00:00
);
2020-12-20 09:29:15 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdDraw(
vulkanCommandBuffer->commandBuffer,
2020-12-20 09:29:15 +00:00
PrimitiveVerts(
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline->primitiveType,
2020-12-20 09:29:15 +00:00
primitiveCount
),
1,
vertexStart,
0
2021-01-02 06:07:15 +00:00
);
2020-12-17 01:23:49 +00:00
}
2020-12-30 01:31:39 +00:00
static void VULKAN_DispatchCompute(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-30 01:31:39 +00:00
uint32_t groupCountX,
uint32_t groupCountY,
2020-12-31 04:39:47 +00:00
uint32_t groupCountZ,
uint32_t computeParamOffset
2020-12-30 01:31:39 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanComputePipeline *computePipeline = vulkanCommandBuffer->currentComputePipeline;
2021-01-02 21:31:17 +00:00
2020-12-31 06:28:37 +00:00
VulkanBuffer *currentBuffer;
2020-12-31 04:39:47 +00:00
VkDescriptorSet descriptorSets[3];
2020-12-31 06:28:37 +00:00
uint32_t i;
2021-01-02 06:07:15 +00:00
for (i = 0; i < vulkanCommandBuffer->boundComputeBufferCount; i += 1)
2020-12-31 06:28:37 +00:00
{
2021-01-02 06:07:15 +00:00
currentBuffer = vulkanCommandBuffer->boundComputeBuffers[i];
2020-12-31 06:28:37 +00:00
VULKAN_INTERNAL_BufferMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-31 06:28:37 +00:00
RESOURCE_ACCESS_COMPUTE_SHADER_READ_OTHER,
currentBuffer,
currentBuffer->subBuffers[currentBuffer->currentSubBufferIndex]
);
}
2020-12-31 00:47:13 +00:00
descriptorSets[0] = computePipeline->bufferDescriptorSet;
descriptorSets[1] = computePipeline->imageDescriptorSet;
2020-12-31 04:39:47 +00:00
descriptorSets[2] = computePipeline->computeUBODescriptorSet;
2020-12-31 00:47:13 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdBindDescriptorSets(
vulkanCommandBuffer->commandBuffer,
2020-12-31 00:47:13 +00:00
VK_PIPELINE_BIND_POINT_COMPUTE,
computePipeline->pipelineLayout->pipelineLayout,
0,
2020-12-31 04:39:47 +00:00
3,
2020-12-31 00:47:13 +00:00
descriptorSets,
2020-12-31 04:39:47 +00:00
1,
&computeParamOffset
2021-01-02 06:07:15 +00:00
);
2020-12-31 00:47:13 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdDispatch(
vulkanCommandBuffer->commandBuffer,
2020-12-31 00:47:13 +00:00
groupCountX,
groupCountY,
groupCountZ
2021-01-02 06:07:15 +00:00
);
2020-12-31 06:28:37 +00:00
2021-01-02 06:07:15 +00:00
for (i = 0; i < vulkanCommandBuffer->boundComputeBufferCount; i += 1)
2020-12-31 06:28:37 +00:00
{
2021-01-02 06:07:15 +00:00
currentBuffer = vulkanCommandBuffer->boundComputeBuffers[i];
2020-12-31 06:28:37 +00:00
if (currentBuffer->usage & VK_BUFFER_USAGE_VERTEX_BUFFER_BIT)
{
VULKAN_INTERNAL_BufferMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-31 06:28:37 +00:00
RESOURCE_ACCESS_VERTEX_BUFFER,
currentBuffer,
currentBuffer->subBuffers[currentBuffer->currentSubBufferIndex]
);
}
else if (currentBuffer->usage & VK_BUFFER_USAGE_INDEX_BUFFER_BIT)
{
VULKAN_INTERNAL_BufferMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-31 06:28:37 +00:00
RESOURCE_ACCESS_INDEX_BUFFER,
currentBuffer,
currentBuffer->subBuffers[currentBuffer->currentSubBufferIndex]
);
}
}
2020-12-30 01:31:39 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_RenderPass* VULKAN_CreateRenderPass(
Refresh_Renderer *driverData,
Refresh_RenderPassCreateInfo *renderPassCreateInfo
2020-12-17 01:23:49 +00:00
) {
2020-12-17 08:19:02 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VkResult vulkanResult;
2020-12-23 07:17:09 +00:00
VkAttachmentDescription attachmentDescriptions[2 * MAX_COLOR_TARGET_BINDINGS + 1];
VkAttachmentReference colorAttachmentReferences[MAX_COLOR_TARGET_BINDINGS];
VkAttachmentReference resolveReferences[MAX_COLOR_TARGET_BINDINGS + 1];
2020-12-17 08:19:02 +00:00
VkAttachmentReference depthStencilAttachmentReference;
VkRenderPassCreateInfo vkRenderPassCreateInfo;
VkSubpassDescription subpass;
VkRenderPass renderPass;
uint32_t i;
2020-12-22 02:34:57 +00:00
uint8_t multisampling = 0;
2020-12-17 08:19:02 +00:00
uint32_t attachmentDescriptionCount = 0;
uint32_t colorAttachmentReferenceCount = 0;
uint32_t resolveReferenceCount = 0;
for (i = 0; i < renderPassCreateInfo->colorTargetCount; i += 1)
{
if (renderPassCreateInfo->colorTargetDescriptions[attachmentDescriptionCount].multisampleCount > REFRESH_SAMPLECOUNT_1)
{
2020-12-22 02:34:57 +00:00
multisampling = 1;
2020-12-17 08:19:02 +00:00
/* Resolve attachment and multisample attachment */
attachmentDescriptions[attachmentDescriptionCount].flags = 0;
attachmentDescriptions[attachmentDescriptionCount].format = RefreshToVK_SurfaceFormat[
renderPassCreateInfo->colorTargetDescriptions[i].format
];
attachmentDescriptions[attachmentDescriptionCount].samples =
VK_SAMPLE_COUNT_1_BIT;
attachmentDescriptions[attachmentDescriptionCount].loadOp = RefreshToVK_LoadOp[
renderPassCreateInfo->colorTargetDescriptions[i].loadOp
];
attachmentDescriptions[attachmentDescriptionCount].storeOp = RefreshToVK_StoreOp[
renderPassCreateInfo->colorTargetDescriptions[i].storeOp
];
attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp =
VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp =
VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescriptions[attachmentDescriptionCount].initialLayout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentDescriptions[attachmentDescriptionCount].finalLayout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
resolveReferences[resolveReferenceCount].attachment =
attachmentDescriptionCount;
resolveReferences[resolveReferenceCount].layout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentDescriptionCount += 1;
resolveReferenceCount += 1;
attachmentDescriptions[attachmentDescriptionCount].flags = 0;
attachmentDescriptions[attachmentDescriptionCount].format = RefreshToVK_SurfaceFormat[
renderPassCreateInfo->colorTargetDescriptions[i].format
];
attachmentDescriptions[attachmentDescriptionCount].samples = RefreshToVK_SampleCount[
renderPassCreateInfo->colorTargetDescriptions[i].multisampleCount
];
attachmentDescriptions[attachmentDescriptionCount].loadOp = RefreshToVK_LoadOp[
renderPassCreateInfo->colorTargetDescriptions[i].loadOp
];
attachmentDescriptions[attachmentDescriptionCount].storeOp = RefreshToVK_StoreOp[
renderPassCreateInfo->colorTargetDescriptions[i].storeOp
];
attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp =
VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp =
VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescriptions[attachmentDescriptionCount].initialLayout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentDescriptions[attachmentDescriptionCount].finalLayout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
colorAttachmentReferences[colorAttachmentReferenceCount].attachment =
attachmentDescriptionCount;
colorAttachmentReferences[colorAttachmentReferenceCount].layout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentDescriptionCount += 1;
colorAttachmentReferenceCount += 1;
}
else
{
attachmentDescriptions[attachmentDescriptionCount].flags = 0;
attachmentDescriptions[attachmentDescriptionCount].format = RefreshToVK_SurfaceFormat[
renderPassCreateInfo->colorTargetDescriptions[i].format
];
attachmentDescriptions[attachmentDescriptionCount].samples =
VK_SAMPLE_COUNT_1_BIT;
attachmentDescriptions[attachmentDescriptionCount].loadOp = RefreshToVK_LoadOp[
renderPassCreateInfo->colorTargetDescriptions[i].loadOp
];
attachmentDescriptions[attachmentDescriptionCount].storeOp = RefreshToVK_StoreOp[
renderPassCreateInfo->colorTargetDescriptions[i].storeOp
];
attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp =
VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp =
VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescriptions[attachmentDescriptionCount].initialLayout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentDescriptions[attachmentDescriptionCount].finalLayout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachmentDescriptionCount += 1;
colorAttachmentReferences[colorAttachmentReferenceCount].attachment = i;
colorAttachmentReferences[colorAttachmentReferenceCount].layout =
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
colorAttachmentReferenceCount += 1;
}
}
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.flags = 0;
subpass.inputAttachmentCount = 0;
subpass.pInputAttachments = NULL;
subpass.colorAttachmentCount = renderPassCreateInfo->colorTargetCount;
subpass.pColorAttachments = colorAttachmentReferences;
subpass.preserveAttachmentCount = 0;
subpass.pPreserveAttachments = NULL;
if (renderPassCreateInfo->depthTargetDescription == NULL)
{
subpass.pDepthStencilAttachment = NULL;
}
else
{
attachmentDescriptions[attachmentDescriptionCount].flags = 0;
attachmentDescriptions[attachmentDescriptionCount].format = RefreshToVK_DepthFormat[
renderPassCreateInfo->depthTargetDescription->depthFormat
];
attachmentDescriptions[attachmentDescriptionCount].samples =
VK_SAMPLE_COUNT_1_BIT; /* FIXME: do these take multisamples? */
attachmentDescriptions[attachmentDescriptionCount].loadOp = RefreshToVK_LoadOp[
renderPassCreateInfo->depthTargetDescription->loadOp
];
attachmentDescriptions[attachmentDescriptionCount].storeOp = RefreshToVK_StoreOp[
renderPassCreateInfo->depthTargetDescription->storeOp
];
attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp = RefreshToVK_LoadOp[
renderPassCreateInfo->depthTargetDescription->stencilLoadOp
];
attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp = RefreshToVK_StoreOp[
renderPassCreateInfo->depthTargetDescription->stencilStoreOp
];
attachmentDescriptions[attachmentDescriptionCount].initialLayout =
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachmentDescriptions[attachmentDescriptionCount].finalLayout =
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
depthStencilAttachmentReference.attachment =
attachmentDescriptionCount;
depthStencilAttachmentReference.layout =
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
subpass.pDepthStencilAttachment =
&depthStencilAttachmentReference;
attachmentDescriptionCount += 1;
}
2020-12-22 02:34:57 +00:00
if (multisampling)
{
subpass.pResolveAttachments = resolveReferences;
}
else
{
subpass.pResolveAttachments = NULL;
}
2020-12-17 08:19:02 +00:00
vkRenderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
vkRenderPassCreateInfo.pNext = NULL;
vkRenderPassCreateInfo.flags = 0;
vkRenderPassCreateInfo.pAttachments = attachmentDescriptions;
vkRenderPassCreateInfo.attachmentCount = attachmentDescriptionCount;
vkRenderPassCreateInfo.subpassCount = 1;
vkRenderPassCreateInfo.pSubpasses = &subpass;
vkRenderPassCreateInfo.dependencyCount = 0;
vkRenderPassCreateInfo.pDependencies = NULL;
vulkanResult = renderer->vkCreateRenderPass(
renderer->logicalDevice,
&vkRenderPassCreateInfo,
NULL,
&renderPass
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateRenderPass", vulkanResult);
return NULL_RENDER_PASS;
}
2021-01-05 23:00:51 +00:00
return (Refresh_RenderPass*) renderPass;
2020-12-17 01:23:49 +00:00
}
2020-12-30 00:53:10 +00:00
static uint8_t VULKAN_INTERNAL_CreateDescriptorPool(
2020-12-19 04:08:07 +00:00
VulkanRenderer *renderer,
2020-12-27 23:20:59 +00:00
VkDescriptorType descriptorType,
uint32_t descriptorSetCount,
uint32_t descriptorCount,
2020-12-19 04:08:07 +00:00
VkDescriptorPool *pDescriptorPool
) {
VkResult vulkanResult;
2020-12-27 23:20:59 +00:00
VkDescriptorPoolSize descriptorPoolSize;
2020-12-19 04:08:07 +00:00
VkDescriptorPoolCreateInfo descriptorPoolInfo;
2020-12-27 23:20:59 +00:00
descriptorPoolSize.type = descriptorType;
descriptorPoolSize.descriptorCount = descriptorCount;
2020-12-19 04:08:07 +00:00
descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptorPoolInfo.pNext = NULL;
descriptorPoolInfo.flags = 0;
2020-12-27 23:20:59 +00:00
descriptorPoolInfo.maxSets = descriptorSetCount;
descriptorPoolInfo.poolSizeCount = 1;
descriptorPoolInfo.pPoolSizes = &descriptorPoolSize;
2020-12-19 04:08:07 +00:00
vulkanResult = renderer->vkCreateDescriptorPool(
renderer->logicalDevice,
&descriptorPoolInfo,
NULL,
pDescriptorPool
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateDescriptorPool", vulkanResult);
return 0;
}
return 1;
}
2020-12-30 00:53:10 +00:00
static uint8_t VULKAN_INTERNAL_AllocateDescriptorSets(
2020-12-27 23:20:59 +00:00
VulkanRenderer *renderer,
VkDescriptorPool descriptorPool,
VkDescriptorSetLayout descriptorSetLayout,
uint32_t descriptorSetCount,
VkDescriptorSet *descriptorSetArray
) {
VkResult vulkanResult;
uint32_t i;
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo;
VkDescriptorSetLayout *descriptorSetLayouts = SDL_stack_alloc(VkDescriptorSetLayout, descriptorSetCount);
2021-01-02 06:07:15 +00:00
SDL_LockMutex(renderer->descriptorSetLock);
2020-12-27 23:20:59 +00:00
for (i = 0; i < descriptorSetCount; i += 1)
{
descriptorSetLayouts[i] = descriptorSetLayout;
}
descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorSetAllocateInfo.pNext = NULL;
descriptorSetAllocateInfo.descriptorPool = descriptorPool;
descriptorSetAllocateInfo.descriptorSetCount = descriptorSetCount;
descriptorSetAllocateInfo.pSetLayouts = descriptorSetLayouts;
vulkanResult = renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&descriptorSetAllocateInfo,
descriptorSetArray
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkAllocateDescriptorSets", vulkanResult);
SDL_stack_free(descriptorSetLayouts);
return 0;
}
2021-01-02 06:07:15 +00:00
SDL_UnlockMutex(renderer->descriptorSetLock);
2020-12-27 23:20:59 +00:00
SDL_stack_free(descriptorSetLayouts);
return 1;
}
2020-12-30 00:19:19 +00:00
static ImageDescriptorSetCache* VULKAN_INTERNAL_CreateImageDescriptorSetCache(
2020-12-27 23:20:59 +00:00
VulkanRenderer *renderer,
2020-12-30 00:53:10 +00:00
VkDescriptorType descriptorType,
2020-12-27 23:20:59 +00:00
VkDescriptorSetLayout descriptorSetLayout,
2020-12-30 00:19:19 +00:00
uint32_t bindingCount
2020-12-27 23:20:59 +00:00
) {
2020-12-28 06:45:12 +00:00
uint32_t i;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetCache *imageDescriptorSetCache = SDL_malloc(sizeof(ImageDescriptorSetCache));
2020-12-28 06:45:12 +00:00
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->elements = SDL_malloc(sizeof(ImageDescriptorSetHashMap) * 16);
imageDescriptorSetCache->count = 0;
imageDescriptorSetCache->capacity = 16;
2020-12-28 06:45:12 +00:00
for (i = 0; i < NUM_DESCRIPTOR_SET_HASH_BUCKETS; i += 1)
{
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->buckets[i].elements = NULL;
imageDescriptorSetCache->buckets[i].count = 0;
imageDescriptorSetCache->buckets[i].capacity = 0;
2020-12-28 06:45:12 +00:00
}
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->descriptorSetLayout = descriptorSetLayout;
imageDescriptorSetCache->bindingCount = bindingCount;
2020-12-31 00:47:13 +00:00
imageDescriptorSetCache->descriptorType = descriptorType;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->imageDescriptorPools = SDL_malloc(sizeof(VkDescriptorPool));
imageDescriptorSetCache->imageDescriptorPoolCount = 1;
2020-12-30 00:53:10 +00:00
imageDescriptorSetCache->nextPoolSize = DESCRIPTOR_POOL_STARTING_SIZE * 2;
2020-12-28 06:45:12 +00:00
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_CreateDescriptorPool(
2020-12-27 23:20:59 +00:00
renderer,
2020-12-30 00:53:10 +00:00
descriptorType,
DESCRIPTOR_POOL_STARTING_SIZE,
DESCRIPTOR_POOL_STARTING_SIZE * bindingCount,
2020-12-30 00:19:19 +00:00
&imageDescriptorSetCache->imageDescriptorPools[0]
2020-12-27 23:20:59 +00:00
);
2020-12-30 00:53:10 +00:00
imageDescriptorSetCache->inactiveDescriptorSetCapacity = DESCRIPTOR_POOL_STARTING_SIZE;
imageDescriptorSetCache->inactiveDescriptorSetCount = DESCRIPTOR_POOL_STARTING_SIZE;
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->inactiveDescriptorSets = SDL_malloc(
2020-12-30 00:53:10 +00:00
sizeof(VkDescriptorSet) * DESCRIPTOR_POOL_STARTING_SIZE
2020-12-27 23:20:59 +00:00
);
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_AllocateDescriptorSets(
2020-12-27 23:20:59 +00:00
renderer,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->imageDescriptorPools[0],
imageDescriptorSetCache->descriptorSetLayout,
2020-12-30 00:53:10 +00:00
DESCRIPTOR_POOL_STARTING_SIZE,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->inactiveDescriptorSets
2020-12-27 23:20:59 +00:00
);
2020-12-30 00:19:19 +00:00
return imageDescriptorSetCache;
2020-12-27 23:20:59 +00:00
}
2020-12-30 00:53:10 +00:00
static BufferDescriptorSetCache* VULKAN_INTERNAL_CreateBufferDescriptorSetCache(
VulkanRenderer *renderer,
2020-12-31 04:39:47 +00:00
VkDescriptorType descriptorType,
2020-12-30 00:53:10 +00:00
VkDescriptorSetLayout descriptorSetLayout,
uint32_t bindingCount
) {
uint32_t i;
BufferDescriptorSetCache *bufferDescriptorSetCache = SDL_malloc(sizeof(BufferDescriptorSetCache));
bufferDescriptorSetCache->elements = SDL_malloc(sizeof(BufferDescriptorSetHashMap) * 16);
bufferDescriptorSetCache->count = 0;
bufferDescriptorSetCache->capacity = 16;
for (i = 0; i < NUM_DESCRIPTOR_SET_HASH_BUCKETS; i += 1)
{
bufferDescriptorSetCache->buckets[i].elements = NULL;
bufferDescriptorSetCache->buckets[i].count = 0;
bufferDescriptorSetCache->buckets[i].capacity = 0;
}
2020-12-31 04:39:47 +00:00
bufferDescriptorSetCache->descriptorSetLayout = descriptorSetLayout;
bufferDescriptorSetCache->bindingCount = bindingCount;
bufferDescriptorSetCache->descriptorType = descriptorType;
2020-12-30 00:53:10 +00:00
bufferDescriptorSetCache->bufferDescriptorPools = SDL_malloc(sizeof(VkDescriptorPool));
bufferDescriptorSetCache->bufferDescriptorPoolCount = 1;
bufferDescriptorSetCache->nextPoolSize = DESCRIPTOR_POOL_STARTING_SIZE * 2;
VULKAN_INTERNAL_CreateDescriptorPool(
renderer,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
DESCRIPTOR_POOL_STARTING_SIZE,
DESCRIPTOR_POOL_STARTING_SIZE * bindingCount,
&bufferDescriptorSetCache->bufferDescriptorPools[0]
);
bufferDescriptorSetCache->inactiveDescriptorSetCapacity = DESCRIPTOR_POOL_STARTING_SIZE;
bufferDescriptorSetCache->inactiveDescriptorSetCount = DESCRIPTOR_POOL_STARTING_SIZE;
bufferDescriptorSetCache->inactiveDescriptorSets = SDL_malloc(
sizeof(VkDescriptorSet) * DESCRIPTOR_POOL_STARTING_SIZE
);
VULKAN_INTERNAL_AllocateDescriptorSets(
renderer,
bufferDescriptorSetCache->bufferDescriptorPools[0],
bufferDescriptorSetCache->descriptorSetLayout,
DESCRIPTOR_POOL_STARTING_SIZE,
bufferDescriptorSetCache->inactiveDescriptorSets
);
return bufferDescriptorSetCache;
}
2020-12-29 23:05:26 +00:00
static VkDescriptorSetLayout VULKAN_INTERNAL_FetchDescriptorSetLayout(
VulkanRenderer *renderer,
2020-12-29 23:05:26 +00:00
VkDescriptorType descriptorType,
uint32_t bindingCount,
VkShaderStageFlagBits shaderStageFlagBit
) {
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHash descriptorSetLayoutHash;
VkDescriptorSetLayout descriptorSetLayout;
VkDescriptorSetLayoutBinding setLayoutBindings[MAX_TEXTURE_SAMPLERS];
VkDescriptorSetLayoutCreateInfo setLayoutCreateInfo;
VkResult vulkanResult;
uint32_t i;
2020-12-29 23:05:26 +00:00
if (bindingCount == 0)
{
if (shaderStageFlagBit == VK_SHADER_STAGE_VERTEX_BIT)
{
return renderer->emptyVertexSamplerLayout;
}
else if (shaderStageFlagBit == VK_SHADER_STAGE_FRAGMENT_BIT)
{
return renderer->emptyFragmentSamplerLayout;
}
2020-12-29 23:05:26 +00:00
else if (shaderStageFlagBit == VK_SHADER_STAGE_COMPUTE_BIT)
{
2020-12-31 04:39:47 +00:00
if (descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
{
return renderer->emptyComputeBufferDescriptorSetLayout;
}
else if (descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
{
return renderer->emptyComputeImageDescriptorSetLayout;
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Invalid descriptor type for compute shader: ", descriptorType);
2020-12-31 04:39:47 +00:00
return NULL_DESC_LAYOUT;
}
2020-12-29 23:05:26 +00:00
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Invalid shader stage flag bit: ", shaderStageFlagBit);
return NULL_DESC_LAYOUT;
}
}
2020-12-31 00:47:13 +00:00
descriptorSetLayoutHash.descriptorType = descriptorType;
2020-12-29 23:05:26 +00:00
descriptorSetLayoutHash.bindingCount = bindingCount;
descriptorSetLayoutHash.stageFlag = shaderStageFlagBit;
2020-12-29 23:05:26 +00:00
descriptorSetLayout = DescriptorSetLayoutHashTable_Fetch(
&renderer->descriptorSetLayoutHashTable,
descriptorSetLayoutHash
);
if (descriptorSetLayout != VK_NULL_HANDLE)
{
return descriptorSetLayout;
}
2020-12-29 23:05:26 +00:00
for (i = 0; i < bindingCount; i += 1)
{
setLayoutBindings[i].binding = i;
setLayoutBindings[i].descriptorCount = 1;
2020-12-31 00:47:13 +00:00
setLayoutBindings[i].descriptorType = descriptorType;
setLayoutBindings[i].stageFlags = shaderStageFlagBit;
setLayoutBindings[i].pImmutableSamplers = NULL;
}
setLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
setLayoutCreateInfo.pNext = NULL;
setLayoutCreateInfo.flags = 0;
2020-12-29 23:05:26 +00:00
setLayoutCreateInfo.bindingCount = bindingCount;
setLayoutCreateInfo.pBindings = setLayoutBindings;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&descriptorSetLayout
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateDescriptorSetLayout", vulkanResult);
return NULL_DESC_LAYOUT;
}
2020-12-29 23:05:26 +00:00
DescriptorSetLayoutHashTable_Insert(
&renderer->descriptorSetLayoutHashTable,
descriptorSetLayoutHash,
descriptorSetLayout
);
return descriptorSetLayout;
}
static VulkanGraphicsPipelineLayout* VULKAN_INTERNAL_FetchGraphicsPipelineLayout(
VulkanRenderer *renderer,
uint32_t vertexSamplerBindingCount,
uint32_t fragmentSamplerBindingCount
) {
VkDescriptorSetLayout setLayouts[4];
2020-12-29 22:52:24 +00:00
GraphicsPipelineLayoutHash pipelineLayoutHash;
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo;
VkResult vulkanResult;
VulkanGraphicsPipelineLayout *vulkanGraphicsPipelineLayout;
2020-12-29 23:05:26 +00:00
pipelineLayoutHash.vertexSamplerLayout = VULKAN_INTERNAL_FetchDescriptorSetLayout(
renderer,
2020-12-29 23:05:26 +00:00
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
vertexSamplerBindingCount,
VK_SHADER_STAGE_VERTEX_BIT
);
2020-12-29 23:05:26 +00:00
pipelineLayoutHash.fragmentSamplerLayout = VULKAN_INTERNAL_FetchDescriptorSetLayout(
renderer,
2020-12-29 23:05:26 +00:00
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
fragmentSamplerBindingCount,
VK_SHADER_STAGE_FRAGMENT_BIT
);
pipelineLayoutHash.vertexUniformLayout = renderer->vertexParamLayout;
pipelineLayoutHash.fragmentUniformLayout = renderer->fragmentParamLayout;
2020-12-29 22:52:24 +00:00
vulkanGraphicsPipelineLayout = GraphicsPipelineLayoutHashArray_Fetch(
&renderer->graphicsPipelineLayoutHashTable,
pipelineLayoutHash
);
2020-12-29 22:52:24 +00:00
if (vulkanGraphicsPipelineLayout != NULL)
{
return vulkanGraphicsPipelineLayout;
}
vulkanGraphicsPipelineLayout = SDL_malloc(sizeof(VulkanGraphicsPipelineLayout));
setLayouts[0] = pipelineLayoutHash.vertexSamplerLayout;
setLayouts[1] = pipelineLayoutHash.fragmentSamplerLayout;
setLayouts[2] = renderer->vertexParamLayout;
setLayouts[3] = renderer->fragmentParamLayout;
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo.pNext = NULL;
pipelineLayoutCreateInfo.flags = 0;
pipelineLayoutCreateInfo.setLayoutCount = 4;
pipelineLayoutCreateInfo.pSetLayouts = setLayouts;
pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pipelineLayoutCreateInfo.pPushConstantRanges = NULL;
vulkanResult = renderer->vkCreatePipelineLayout(
renderer->logicalDevice,
&pipelineLayoutCreateInfo,
NULL,
&vulkanGraphicsPipelineLayout->pipelineLayout
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreatePipelineLayout", vulkanResult);
return NULL;
}
2020-12-29 22:52:24 +00:00
GraphicsPipelineLayoutHashArray_Insert(
&renderer->graphicsPipelineLayoutHashTable,
pipelineLayoutHash,
vulkanGraphicsPipelineLayout
);
2020-12-27 23:34:15 +00:00
/* If the binding count is 0
* we can just bind the same descriptor set
* so no cache is needed
*/
2020-12-27 23:20:59 +00:00
if (vertexSamplerBindingCount == 0)
{
vulkanGraphicsPipelineLayout->vertexSamplerDescriptorSetCache = NULL;
}
else
{
vulkanGraphicsPipelineLayout->vertexSamplerDescriptorSetCache =
2020-12-30 00:19:19 +00:00
VULKAN_INTERNAL_CreateImageDescriptorSetCache(
2020-12-27 23:20:59 +00:00
renderer,
2020-12-30 00:53:10 +00:00
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2020-12-27 23:20:59 +00:00
pipelineLayoutHash.vertexSamplerLayout,
vertexSamplerBindingCount
);
}
2020-12-27 23:20:59 +00:00
if (fragmentSamplerBindingCount == 0)
{
vulkanGraphicsPipelineLayout->fragmentSamplerDescriptorSetCache = NULL;
}
else
{
vulkanGraphicsPipelineLayout->fragmentSamplerDescriptorSetCache =
2020-12-30 00:19:19 +00:00
VULKAN_INTERNAL_CreateImageDescriptorSetCache(
2020-12-27 23:20:59 +00:00
renderer,
2020-12-30 00:53:10 +00:00
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2020-12-27 23:20:59 +00:00
pipelineLayoutHash.fragmentSamplerLayout,
fragmentSamplerBindingCount
);
}
return vulkanGraphicsPipelineLayout;
}
2021-01-05 23:00:51 +00:00
static Refresh_GraphicsPipeline* VULKAN_CreateGraphicsPipeline(
Refresh_Renderer *driverData,
Refresh_GraphicsPipelineCreateInfo *pipelineCreateInfo
2020-12-17 01:23:49 +00:00
) {
2020-12-17 19:40:49 +00:00
VkResult vulkanResult;
uint32_t i;
2020-12-19 04:08:07 +00:00
VulkanGraphicsPipeline *graphicsPipeline = (VulkanGraphicsPipeline*) SDL_malloc(sizeof(VulkanGraphicsPipeline));
2020-12-17 19:40:49 +00:00
VkGraphicsPipelineCreateInfo vkPipelineCreateInfo;
VkPipelineShaderStageCreateInfo shaderStageCreateInfos[2];
VkPipelineVertexInputStateCreateInfo vertexInputStateCreateInfo;
VkVertexInputBindingDescription *vertexInputBindingDescriptions = SDL_stack_alloc(VkVertexInputBindingDescription, pipelineCreateInfo->vertexInputState.vertexBindingCount);
VkVertexInputAttributeDescription *vertexInputAttributeDescriptions = SDL_stack_alloc(VkVertexInputAttributeDescription, pipelineCreateInfo->vertexInputState.vertexAttributeCount);
2020-12-17 19:40:49 +00:00
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo;
VkPipelineViewportStateCreateInfo viewportStateCreateInfo;
VkViewport *viewports = SDL_stack_alloc(VkViewport, pipelineCreateInfo->viewportState.viewportCount);
VkRect2D *scissors = SDL_stack_alloc(VkRect2D, pipelineCreateInfo->viewportState.scissorCount);
2020-12-17 19:40:49 +00:00
VkPipelineRasterizationStateCreateInfo rasterizationStateCreateInfo;
VkPipelineMultisampleStateCreateInfo multisampleStateCreateInfo;
VkPipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo;
VkStencilOpState frontStencilState;
VkStencilOpState backStencilState;
VkPipelineColorBlendStateCreateInfo colorBlendStateCreateInfo;
VkPipelineColorBlendAttachmentState *colorBlendAttachmentStates = SDL_stack_alloc(
VkPipelineColorBlendAttachmentState,
2020-12-17 19:40:49 +00:00
pipelineCreateInfo->colorBlendState.blendStateCount
);
2020-12-17 19:40:49 +00:00
2020-12-23 21:11:09 +00:00
VkDescriptorSetAllocateInfo vertexUBODescriptorAllocateInfo;
VkDescriptorSetAllocateInfo fragmentUBODescriptorAllocateInfo;
VkWriteDescriptorSet uboWriteDescriptorSets[2];
VkDescriptorBufferInfo vertexUniformBufferInfo;
VkDescriptorBufferInfo fragmentUniformBufferInfo;
2020-12-17 19:40:49 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
/* Shader stages */
shaderStageCreateInfos[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStageCreateInfos[0].pNext = NULL;
shaderStageCreateInfos[0].flags = 0;
shaderStageCreateInfos[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
shaderStageCreateInfos[0].module = (VkShaderModule) pipelineCreateInfo->vertexShaderState.shaderModule;
shaderStageCreateInfos[0].pName = pipelineCreateInfo->vertexShaderState.entryPointName;
shaderStageCreateInfos[0].pSpecializationInfo = NULL;
2020-12-23 21:11:09 +00:00
graphicsPipeline->vertexUBOBlockSize =
VULKAN_INTERNAL_NextHighestAlignment(
pipelineCreateInfo->vertexShaderState.uniformBufferSize,
renderer->minUBOAlignment
);
2020-12-17 19:40:49 +00:00
shaderStageCreateInfos[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStageCreateInfos[1].pNext = NULL;
shaderStageCreateInfos[1].flags = 0;
shaderStageCreateInfos[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shaderStageCreateInfos[1].module = (VkShaderModule) pipelineCreateInfo->fragmentShaderState.shaderModule;
shaderStageCreateInfos[1].pName = pipelineCreateInfo->fragmentShaderState.entryPointName;
shaderStageCreateInfos[1].pSpecializationInfo = NULL;
2020-12-23 21:11:09 +00:00
graphicsPipeline->fragmentUBOBlockSize =
VULKAN_INTERNAL_NextHighestAlignment(
pipelineCreateInfo->fragmentShaderState.uniformBufferSize,
renderer->minUBOAlignment
);
2020-12-17 19:40:49 +00:00
/* Vertex input */
for (i = 0; i < pipelineCreateInfo->vertexInputState.vertexBindingCount; i += 1)
{
vertexInputBindingDescriptions[i].binding = pipelineCreateInfo->vertexInputState.vertexBindings[i].binding;
vertexInputBindingDescriptions[i].inputRate = RefreshToVK_VertexInputRate[
pipelineCreateInfo->vertexInputState.vertexBindings[i].inputRate
];
vertexInputBindingDescriptions[i].stride = pipelineCreateInfo->vertexInputState.vertexBindings[i].stride;
}
for (i = 0; i < pipelineCreateInfo->vertexInputState.vertexAttributeCount; i += 1)
{
vertexInputAttributeDescriptions[i].binding = pipelineCreateInfo->vertexInputState.vertexAttributes[i].binding;
vertexInputAttributeDescriptions[i].format = RefreshToVK_VertexFormat[
pipelineCreateInfo->vertexInputState.vertexAttributes[i].format
];
vertexInputAttributeDescriptions[i].location = pipelineCreateInfo->vertexInputState.vertexAttributes[i].location;
vertexInputAttributeDescriptions[i].offset = pipelineCreateInfo->vertexInputState.vertexAttributes[i].offset;
}
vertexInputStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputStateCreateInfo.pNext = NULL;
vertexInputStateCreateInfo.flags = 0;
vertexInputStateCreateInfo.vertexBindingDescriptionCount = pipelineCreateInfo->vertexInputState.vertexBindingCount;
vertexInputStateCreateInfo.pVertexBindingDescriptions = vertexInputBindingDescriptions;
vertexInputStateCreateInfo.vertexAttributeDescriptionCount = pipelineCreateInfo->vertexInputState.vertexAttributeCount;
vertexInputStateCreateInfo.pVertexAttributeDescriptions = vertexInputAttributeDescriptions;
/* Topology */
inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssemblyStateCreateInfo.pNext = NULL;
inputAssemblyStateCreateInfo.flags = 0;
inputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE;
inputAssemblyStateCreateInfo.topology = RefreshToVK_PrimitiveType[
pipelineCreateInfo->primitiveType
2020-12-17 19:40:49 +00:00
];
graphicsPipeline->primitiveType = pipelineCreateInfo->primitiveType;
2020-12-17 19:40:49 +00:00
/* Viewport */
for (i = 0; i < pipelineCreateInfo->viewportState.viewportCount; i += 1)
{
viewports[i].x = pipelineCreateInfo->viewportState.viewports[i].x;
viewports[i].y = pipelineCreateInfo->viewportState.viewports[i].y;
viewports[i].width = pipelineCreateInfo->viewportState.viewports[i].w;
viewports[i].height = pipelineCreateInfo->viewportState.viewports[i].h;
viewports[i].minDepth = pipelineCreateInfo->viewportState.viewports[i].minDepth;
viewports[i].maxDepth = pipelineCreateInfo->viewportState.viewports[i].maxDepth;
}
for (i = 0; i < pipelineCreateInfo->viewportState.scissorCount; i += 1)
{
scissors[i].offset.x = pipelineCreateInfo->viewportState.scissors[i].x;
scissors[i].offset.y = pipelineCreateInfo->viewportState.scissors[i].y;
scissors[i].extent.width = pipelineCreateInfo->viewportState.scissors[i].w;
scissors[i].extent.height = pipelineCreateInfo->viewportState.scissors[i].h;
}
viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportStateCreateInfo.pNext = NULL;
viewportStateCreateInfo.flags = 0;
viewportStateCreateInfo.viewportCount = pipelineCreateInfo->viewportState.viewportCount;
viewportStateCreateInfo.pViewports = viewports;
viewportStateCreateInfo.scissorCount = pipelineCreateInfo->viewportState.scissorCount;
viewportStateCreateInfo.pScissors = scissors;
/* Rasterization */
rasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizationStateCreateInfo.pNext = NULL;
rasterizationStateCreateInfo.flags = 0;
rasterizationStateCreateInfo.depthClampEnable = pipelineCreateInfo->rasterizerState.depthClampEnable;
rasterizationStateCreateInfo.rasterizerDiscardEnable = VK_FALSE;
rasterizationStateCreateInfo.polygonMode = RefreshToVK_PolygonMode[
pipelineCreateInfo->rasterizerState.fillMode
];
rasterizationStateCreateInfo.cullMode = RefreshToVK_CullMode[
pipelineCreateInfo->rasterizerState.cullMode
];
rasterizationStateCreateInfo.frontFace = RefreshToVK_FrontFace[
pipelineCreateInfo->rasterizerState.frontFace
];
rasterizationStateCreateInfo.depthBiasEnable =
pipelineCreateInfo->rasterizerState.depthBiasEnable;
rasterizationStateCreateInfo.depthBiasConstantFactor =
pipelineCreateInfo->rasterizerState.depthBiasConstantFactor;
rasterizationStateCreateInfo.depthBiasClamp =
pipelineCreateInfo->rasterizerState.depthBiasClamp;
rasterizationStateCreateInfo.depthBiasSlopeFactor =
pipelineCreateInfo->rasterizerState.depthBiasSlopeFactor;
rasterizationStateCreateInfo.lineWidth =
pipelineCreateInfo->rasterizerState.lineWidth;
/* Multisample */
multisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampleStateCreateInfo.pNext = NULL;
multisampleStateCreateInfo.flags = 0;
multisampleStateCreateInfo.rasterizationSamples = RefreshToVK_SampleCount[
pipelineCreateInfo->multisampleState.multisampleCount
];
multisampleStateCreateInfo.sampleShadingEnable = VK_FALSE;
multisampleStateCreateInfo.minSampleShading = 1.0f;
multisampleStateCreateInfo.pSampleMask =
2021-01-05 23:53:16 +00:00
&pipelineCreateInfo->multisampleState.sampleMask;
2020-12-17 19:40:49 +00:00
multisampleStateCreateInfo.alphaToCoverageEnable = VK_FALSE;
multisampleStateCreateInfo.alphaToOneEnable = VK_FALSE;
/* Depth Stencil State */
frontStencilState.failOp = RefreshToVK_StencilOp[
pipelineCreateInfo->depthStencilState.frontStencilState.failOp
];
frontStencilState.passOp = RefreshToVK_StencilOp[
pipelineCreateInfo->depthStencilState.frontStencilState.passOp
];
frontStencilState.depthFailOp = RefreshToVK_StencilOp[
pipelineCreateInfo->depthStencilState.frontStencilState.depthFailOp
];
frontStencilState.compareOp = RefreshToVK_CompareOp[
pipelineCreateInfo->depthStencilState.frontStencilState.compareOp
];
frontStencilState.compareMask =
pipelineCreateInfo->depthStencilState.frontStencilState.compareMask;
frontStencilState.writeMask =
pipelineCreateInfo->depthStencilState.frontStencilState.writeMask;
frontStencilState.reference =
pipelineCreateInfo->depthStencilState.frontStencilState.reference;
backStencilState.failOp = RefreshToVK_StencilOp[
pipelineCreateInfo->depthStencilState.backStencilState.failOp
];
backStencilState.passOp = RefreshToVK_StencilOp[
pipelineCreateInfo->depthStencilState.backStencilState.passOp
];
backStencilState.depthFailOp = RefreshToVK_StencilOp[
pipelineCreateInfo->depthStencilState.backStencilState.depthFailOp
];
backStencilState.compareOp = RefreshToVK_CompareOp[
pipelineCreateInfo->depthStencilState.backStencilState.compareOp
];
backStencilState.compareMask =
pipelineCreateInfo->depthStencilState.backStencilState.compareMask;
backStencilState.writeMask =
pipelineCreateInfo->depthStencilState.backStencilState.writeMask;
backStencilState.reference =
pipelineCreateInfo->depthStencilState.backStencilState.reference;
depthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthStencilStateCreateInfo.pNext = NULL;
depthStencilStateCreateInfo.flags = 0;
depthStencilStateCreateInfo.depthTestEnable =
pipelineCreateInfo->depthStencilState.depthTestEnable;
depthStencilStateCreateInfo.depthWriteEnable =
pipelineCreateInfo->depthStencilState.depthWriteEnable;
depthStencilStateCreateInfo.depthCompareOp = RefreshToVK_CompareOp[
pipelineCreateInfo->depthStencilState.compareOp
];
depthStencilStateCreateInfo.depthBoundsTestEnable =
pipelineCreateInfo->depthStencilState.depthBoundsTestEnable;
depthStencilStateCreateInfo.stencilTestEnable =
pipelineCreateInfo->depthStencilState.stencilTestEnable;
depthStencilStateCreateInfo.front = frontStencilState;
depthStencilStateCreateInfo.back = backStencilState;
depthStencilStateCreateInfo.minDepthBounds =
pipelineCreateInfo->depthStencilState.minDepthBounds;
depthStencilStateCreateInfo.maxDepthBounds =
pipelineCreateInfo->depthStencilState.maxDepthBounds;
/* Color Blend */
for (i = 0; i < pipelineCreateInfo->colorBlendState.blendStateCount; i += 1)
{
colorBlendAttachmentStates[i].blendEnable =
pipelineCreateInfo->colorBlendState.blendStates[i].blendEnable;
colorBlendAttachmentStates[i].srcColorBlendFactor = RefreshToVK_BlendFactor[
pipelineCreateInfo->colorBlendState.blendStates[i].srcColorBlendFactor
];
colorBlendAttachmentStates[i].dstColorBlendFactor = RefreshToVK_BlendFactor[
pipelineCreateInfo->colorBlendState.blendStates[i].dstColorBlendFactor
];
colorBlendAttachmentStates[i].colorBlendOp = RefreshToVK_BlendOp[
pipelineCreateInfo->colorBlendState.blendStates[i].colorBlendOp
];
colorBlendAttachmentStates[i].srcAlphaBlendFactor = RefreshToVK_BlendFactor[
pipelineCreateInfo->colorBlendState.blendStates[i].srcAlphaBlendFactor
];
colorBlendAttachmentStates[i].dstAlphaBlendFactor = RefreshToVK_BlendFactor[
pipelineCreateInfo->colorBlendState.blendStates[i].dstAlphaBlendFactor
];
colorBlendAttachmentStates[i].alphaBlendOp = RefreshToVK_BlendOp[
pipelineCreateInfo->colorBlendState.blendStates[i].alphaBlendOp
];
colorBlendAttachmentStates[i].colorWriteMask =
pipelineCreateInfo->colorBlendState.blendStates[i].colorWriteMask;
}
colorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlendStateCreateInfo.pNext = NULL;
colorBlendStateCreateInfo.flags = 0;
colorBlendStateCreateInfo.logicOpEnable =
2020-12-31 04:39:47 +00:00
pipelineCreateInfo->colorBlendState.logicOpEnable;
2020-12-17 19:40:49 +00:00
colorBlendStateCreateInfo.logicOp = RefreshToVK_LogicOp[
pipelineCreateInfo->colorBlendState.logicOp
];
colorBlendStateCreateInfo.attachmentCount =
pipelineCreateInfo->colorBlendState.blendStateCount;
colorBlendStateCreateInfo.pAttachments =
colorBlendAttachmentStates;
colorBlendStateCreateInfo.blendConstants[0] =
pipelineCreateInfo->colorBlendState.blendConstants[0];
colorBlendStateCreateInfo.blendConstants[1] =
pipelineCreateInfo->colorBlendState.blendConstants[1];
colorBlendStateCreateInfo.blendConstants[2] =
pipelineCreateInfo->colorBlendState.blendConstants[2];
colorBlendStateCreateInfo.blendConstants[3] =
pipelineCreateInfo->colorBlendState.blendConstants[3];
/* Pipeline Layout */
2020-12-23 21:11:09 +00:00
graphicsPipeline->pipelineLayout = VULKAN_INTERNAL_FetchGraphicsPipelineLayout(
renderer,
pipelineCreateInfo->pipelineLayoutCreateInfo.vertexSamplerBindingCount,
pipelineCreateInfo->pipelineLayoutCreateInfo.fragmentSamplerBindingCount
2020-12-17 19:40:49 +00:00
);
/* Pipeline */
vkPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
2020-12-23 04:22:17 +00:00
vkPipelineCreateInfo.pNext = NULL;
2020-12-17 19:40:49 +00:00
vkPipelineCreateInfo.flags = 0;
vkPipelineCreateInfo.stageCount = 2;
vkPipelineCreateInfo.pStages = shaderStageCreateInfos;
vkPipelineCreateInfo.pVertexInputState = &vertexInputStateCreateInfo;
vkPipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCreateInfo;
vkPipelineCreateInfo.pTessellationState = VK_NULL_HANDLE;
vkPipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
vkPipelineCreateInfo.pRasterizationState = &rasterizationStateCreateInfo;
vkPipelineCreateInfo.pMultisampleState = &multisampleStateCreateInfo;
vkPipelineCreateInfo.pDepthStencilState = &depthStencilStateCreateInfo;
vkPipelineCreateInfo.pColorBlendState = &colorBlendStateCreateInfo;
vkPipelineCreateInfo.pDynamicState = VK_NULL_HANDLE;
vkPipelineCreateInfo.layout = graphicsPipeline->pipelineLayout->pipelineLayout;
2020-12-17 19:40:49 +00:00
vkPipelineCreateInfo.renderPass = (VkRenderPass) pipelineCreateInfo->renderPass;
vkPipelineCreateInfo.subpass = 0;
vkPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
vkPipelineCreateInfo.basePipelineIndex = 0;
/* TODO: enable pipeline caching */
vulkanResult = renderer->vkCreateGraphicsPipelines(
renderer->logicalDevice,
VK_NULL_HANDLE,
1,
&vkPipelineCreateInfo,
NULL,
2020-12-19 04:08:07 +00:00
&graphicsPipeline->pipeline
2020-12-17 19:40:49 +00:00
);
if (vulkanResult != VK_SUCCESS)
{
2020-12-18 01:48:26 +00:00
LogVulkanResult("vkCreateGraphicsPipelines", vulkanResult);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create graphics pipeline!");
2020-12-18 01:48:26 +00:00
SDL_stack_free(vertexInputBindingDescriptions);
SDL_stack_free(vertexInputAttributeDescriptions);
SDL_stack_free(viewports);
SDL_stack_free(scissors);
SDL_stack_free(colorBlendAttachmentStates);
2020-12-17 19:40:49 +00:00
return NULL;
}
SDL_stack_free(vertexInputBindingDescriptions);
SDL_stack_free(vertexInputAttributeDescriptions);
SDL_stack_free(viewports);
SDL_stack_free(scissors);
SDL_stack_free(colorBlendAttachmentStates);
2020-12-19 04:08:07 +00:00
2020-12-23 21:11:09 +00:00
/* Allocate uniform buffer descriptors */
vertexUBODescriptorAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
vertexUBODescriptorAllocateInfo.pNext = NULL;
vertexUBODescriptorAllocateInfo.descriptorPool = renderer->defaultDescriptorPool;
vertexUBODescriptorAllocateInfo.descriptorSetCount = 1;
vertexUBODescriptorAllocateInfo.pSetLayouts = &renderer->vertexParamLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&vertexUBODescriptorAllocateInfo,
&graphicsPipeline->vertexUBODescriptorSet
);
fragmentUBODescriptorAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
fragmentUBODescriptorAllocateInfo.pNext = NULL;
fragmentUBODescriptorAllocateInfo.descriptorPool = renderer->defaultDescriptorPool;
fragmentUBODescriptorAllocateInfo.descriptorSetCount = 1;
fragmentUBODescriptorAllocateInfo.pSetLayouts = &renderer->fragmentParamLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&fragmentUBODescriptorAllocateInfo,
&graphicsPipeline->fragmentUBODescriptorSet
);
2020-12-28 22:07:13 +00:00
if (graphicsPipeline->vertexUBOBlockSize == 0)
{
vertexUniformBufferInfo.buffer = renderer->dummyVertexUniformBuffer->subBuffers[0]->buffer;
vertexUniformBufferInfo.offset = 0;
vertexUniformBufferInfo.range = renderer->dummyVertexUniformBuffer->subBuffers[0]->size;
}
else
{
vertexUniformBufferInfo.buffer = renderer->vertexUBO->subBuffers[0]->buffer;
vertexUniformBufferInfo.offset = 0;
vertexUniformBufferInfo.range = graphicsPipeline->vertexUBOBlockSize;
}
2020-12-23 21:11:09 +00:00
2020-12-28 22:07:13 +00:00
if (graphicsPipeline->fragmentUBOBlockSize == 0)
{
fragmentUniformBufferInfo.buffer = renderer->dummyFragmentUniformBuffer->subBuffers[0]->buffer;
fragmentUniformBufferInfo.offset = 0;
fragmentUniformBufferInfo.range = renderer->dummyFragmentUniformBuffer->subBuffers[0]->size;
}
else
{
fragmentUniformBufferInfo.buffer = renderer->fragmentUBO->subBuffers[0]->buffer;
fragmentUniformBufferInfo.offset = 0;
fragmentUniformBufferInfo.range = graphicsPipeline->fragmentUBOBlockSize;
}
2020-12-23 21:11:09 +00:00
uboWriteDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
uboWriteDescriptorSets[0].pNext = NULL;
uboWriteDescriptorSets[0].descriptorCount = 1;
uboWriteDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
uboWriteDescriptorSets[0].dstArrayElement = 0;
uboWriteDescriptorSets[0].dstBinding = 0;
uboWriteDescriptorSets[0].dstSet = graphicsPipeline->vertexUBODescriptorSet;
uboWriteDescriptorSets[0].pBufferInfo = &vertexUniformBufferInfo;
uboWriteDescriptorSets[0].pImageInfo = NULL;
uboWriteDescriptorSets[0].pTexelBufferView = NULL;
uboWriteDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
uboWriteDescriptorSets[1].pNext = NULL;
uboWriteDescriptorSets[1].descriptorCount = 1;
uboWriteDescriptorSets[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
uboWriteDescriptorSets[1].dstArrayElement = 0;
uboWriteDescriptorSets[1].dstBinding = 0;
uboWriteDescriptorSets[1].dstSet = graphicsPipeline->fragmentUBODescriptorSet;
uboWriteDescriptorSets[1].pBufferInfo = &fragmentUniformBufferInfo;
uboWriteDescriptorSets[1].pImageInfo = NULL;
uboWriteDescriptorSets[1].pTexelBufferView = NULL;
renderer->vkUpdateDescriptorSets(
renderer->logicalDevice,
2,
uboWriteDescriptorSets,
0,
NULL
);
2021-01-05 23:00:51 +00:00
return (Refresh_GraphicsPipeline*) graphicsPipeline;
2020-12-17 01:23:49 +00:00
}
2020-12-29 22:52:24 +00:00
static VulkanComputePipelineLayout* VULKAN_INTERNAL_FetchComputePipelineLayout(
VulkanRenderer *renderer,
uint32_t bufferBindingCount,
uint32_t imageBindingCount
) {
VkResult vulkanResult;
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayout setLayouts[3];
2020-12-29 22:52:24 +00:00
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo;
ComputePipelineLayoutHash pipelineLayoutHash;
VulkanComputePipelineLayout *vulkanComputePipelineLayout;
2020-12-29 23:05:26 +00:00
pipelineLayoutHash.bufferLayout = VULKAN_INTERNAL_FetchDescriptorSetLayout(
2020-12-29 22:52:24 +00:00
renderer,
2020-12-29 23:05:26 +00:00
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
bufferBindingCount,
VK_SHADER_STAGE_COMPUTE_BIT
2020-12-29 22:52:24 +00:00
);
2020-12-29 23:05:26 +00:00
pipelineLayoutHash.imageLayout = VULKAN_INTERNAL_FetchDescriptorSetLayout(
2020-12-29 22:52:24 +00:00
renderer,
2020-12-30 00:19:19 +00:00
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
2020-12-29 23:05:26 +00:00
imageBindingCount,
VK_SHADER_STAGE_COMPUTE_BIT
2020-12-29 22:52:24 +00:00
);
2020-12-31 04:39:47 +00:00
pipelineLayoutHash.uniformLayout = renderer->computeParamLayout;
2020-12-29 22:52:24 +00:00
vulkanComputePipelineLayout = ComputePipelineLayoutHashArray_Fetch(
&renderer->computePipelineLayoutHashTable,
pipelineLayoutHash
);
if (vulkanComputePipelineLayout != NULL)
{
return vulkanComputePipelineLayout;
}
vulkanComputePipelineLayout = SDL_malloc(sizeof(VulkanComputePipelineLayout));
setLayouts[0] = pipelineLayoutHash.bufferLayout;
setLayouts[1] = pipelineLayoutHash.imageLayout;
2020-12-31 04:39:47 +00:00
setLayouts[2] = pipelineLayoutHash.uniformLayout;
2020-12-29 22:52:24 +00:00
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo.pNext = NULL;
pipelineLayoutCreateInfo.flags = 0;
2020-12-31 04:39:47 +00:00
pipelineLayoutCreateInfo.setLayoutCount = 3;
2020-12-29 22:52:24 +00:00
pipelineLayoutCreateInfo.pSetLayouts = setLayouts;
pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pipelineLayoutCreateInfo.pPushConstantRanges = NULL;
vulkanResult = renderer->vkCreatePipelineLayout(
renderer->logicalDevice,
&pipelineLayoutCreateInfo,
NULL,
&vulkanComputePipelineLayout->pipelineLayout
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreatePipelineLayout", vulkanResult);
return NULL;
}
ComputePipelineLayoutHashArray_Insert(
&renderer->computePipelineLayoutHashTable,
pipelineLayoutHash,
vulkanComputePipelineLayout
);
/* If the binding count is 0
* we can just bind the same descriptor set
* so no cache is needed
*/
if (bufferBindingCount == 0)
{
vulkanComputePipelineLayout->bufferDescriptorSetCache = NULL;
}
else
{
vulkanComputePipelineLayout->bufferDescriptorSetCache =
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_CreateBufferDescriptorSetCache(
2020-12-29 22:52:24 +00:00
renderer,
2020-12-31 04:39:47 +00:00
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
2020-12-29 22:52:24 +00:00
pipelineLayoutHash.bufferLayout,
bufferBindingCount
);
}
if (imageBindingCount == 0)
{
vulkanComputePipelineLayout->imageDescriptorSetCache = NULL;
}
else
{
vulkanComputePipelineLayout->imageDescriptorSetCache =
2020-12-30 00:19:19 +00:00
VULKAN_INTERNAL_CreateImageDescriptorSetCache(
2020-12-29 22:52:24 +00:00
renderer,
2020-12-30 00:53:10 +00:00
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
2020-12-29 22:52:24 +00:00
pipelineLayoutHash.imageLayout,
imageBindingCount
);
}
return vulkanComputePipelineLayout;
}
2021-01-05 23:00:51 +00:00
static Refresh_ComputePipeline* VULKAN_CreateComputePipeline(
Refresh_Renderer *driverData,
Refresh_ComputePipelineCreateInfo *pipelineCreateInfo
2020-12-29 22:52:24 +00:00
) {
VkComputePipelineCreateInfo computePipelineCreateInfo;
VkPipelineShaderStageCreateInfo pipelineShaderStageCreateInfo;
2020-12-31 04:39:47 +00:00
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo;
VkDescriptorBufferInfo uniformBufferInfo;
VkWriteDescriptorSet writeDescriptorSet;
2020-12-29 22:52:24 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanComputePipeline *vulkanComputePipeline = SDL_malloc(sizeof(VulkanComputePipeline));
pipelineShaderStageCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
pipelineShaderStageCreateInfo.pNext = NULL;
pipelineShaderStageCreateInfo.flags = 0;
pipelineShaderStageCreateInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
pipelineShaderStageCreateInfo.module = (VkShaderModule) pipelineCreateInfo->computeShaderState.shaderModule;
pipelineShaderStageCreateInfo.pName = pipelineCreateInfo->computeShaderState.entryPointName;
pipelineShaderStageCreateInfo.pSpecializationInfo = NULL;
2020-12-31 04:39:47 +00:00
vulkanComputePipeline->pipelineLayout = VULKAN_INTERNAL_FetchComputePipelineLayout(
2020-12-29 22:52:24 +00:00
renderer,
pipelineCreateInfo->pipelineLayoutCreateInfo.bufferBindingCount,
pipelineCreateInfo->pipelineLayoutCreateInfo.imageBindingCount
);
computePipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
computePipelineCreateInfo.pNext = NULL;
computePipelineCreateInfo.flags = 0;
computePipelineCreateInfo.stage = pipelineShaderStageCreateInfo;
2020-12-31 04:39:47 +00:00
computePipelineCreateInfo.layout =
vulkanComputePipeline->pipelineLayout->pipelineLayout;
2020-12-29 22:52:24 +00:00
computePipelineCreateInfo.basePipelineHandle = NULL;
computePipelineCreateInfo.basePipelineIndex = 0;
renderer->vkCreateComputePipelines(
renderer->logicalDevice,
NULL,
1,
&computePipelineCreateInfo,
NULL,
&vulkanComputePipeline->pipeline
);
2020-12-31 04:39:47 +00:00
vulkanComputePipeline->computeUBOBlockSize =
VULKAN_INTERNAL_NextHighestAlignment(
pipelineCreateInfo->computeShaderState.uniformBufferSize,
renderer->minUBOAlignment
);
descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorSetAllocateInfo.pNext = NULL;
descriptorSetAllocateInfo.descriptorPool = renderer->defaultDescriptorPool;
descriptorSetAllocateInfo.descriptorSetCount = 1;
descriptorSetAllocateInfo.pSetLayouts = &renderer->computeParamLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&descriptorSetAllocateInfo,
&vulkanComputePipeline->computeUBODescriptorSet
);
if (vulkanComputePipeline->computeUBOBlockSize == 0)
{
uniformBufferInfo.buffer = renderer->dummyComputeUniformBuffer->subBuffers[0]->buffer;
uniformBufferInfo.offset = 0;
uniformBufferInfo.range = renderer->dummyComputeUniformBuffer->subBuffers[0]->size;
}
else
{
uniformBufferInfo.buffer = renderer->computeUBO->subBuffers[0]->buffer;
uniformBufferInfo.offset = 0;
uniformBufferInfo.range = vulkanComputePipeline->computeUBOBlockSize;
}
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSet.pNext = NULL;
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
writeDescriptorSet.dstArrayElement = 0;
writeDescriptorSet.dstBinding = 0;
writeDescriptorSet.dstSet = vulkanComputePipeline->computeUBODescriptorSet;
writeDescriptorSet.pBufferInfo = &uniformBufferInfo;
writeDescriptorSet.pImageInfo = NULL;
writeDescriptorSet.pTexelBufferView = NULL;
renderer->vkUpdateDescriptorSets(
renderer->logicalDevice,
1,
&writeDescriptorSet,
0,
NULL
);
2021-01-05 23:00:51 +00:00
return (Refresh_ComputePipeline*) vulkanComputePipeline;
2020-12-29 22:52:24 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_Sampler* VULKAN_CreateSampler(
Refresh_Renderer *driverData,
Refresh_SamplerStateCreateInfo *samplerStateCreateInfo
2020-12-17 01:23:49 +00:00
) {
2020-12-18 01:48:26 +00:00
VkResult vulkanResult;
VkSampler sampler;
2020-12-18 20:58:03 +00:00
2020-12-18 01:48:26 +00:00
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
VkSamplerCreateInfo vkSamplerCreateInfo;
vkSamplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
vkSamplerCreateInfo.pNext = NULL;
vkSamplerCreateInfo.flags = 0;
2021-01-03 21:01:29 +00:00
vkSamplerCreateInfo.magFilter = RefreshToVK_Filter[
2020-12-18 01:48:26 +00:00
samplerStateCreateInfo->magFilter
];
2021-01-03 21:01:29 +00:00
vkSamplerCreateInfo.minFilter = RefreshToVK_Filter[
2020-12-18 01:48:26 +00:00
samplerStateCreateInfo->minFilter
];
vkSamplerCreateInfo.mipmapMode = RefreshToVK_SamplerMipmapMode[
samplerStateCreateInfo->mipmapMode
];
vkSamplerCreateInfo.addressModeU = RefreshToVK_SamplerAddressMode[
samplerStateCreateInfo->addressModeU
];
vkSamplerCreateInfo.addressModeV = RefreshToVK_SamplerAddressMode[
samplerStateCreateInfo->addressModeV
];
vkSamplerCreateInfo.addressModeW = RefreshToVK_SamplerAddressMode[
samplerStateCreateInfo->addressModeW
];
vkSamplerCreateInfo.mipLodBias = samplerStateCreateInfo->mipLodBias;
vkSamplerCreateInfo.anisotropyEnable = samplerStateCreateInfo->anisotropyEnable;
vkSamplerCreateInfo.maxAnisotropy = samplerStateCreateInfo->maxAnisotropy;
vkSamplerCreateInfo.compareEnable = samplerStateCreateInfo->compareEnable;
vkSamplerCreateInfo.compareOp = RefreshToVK_CompareOp[
samplerStateCreateInfo->compareOp
];
vkSamplerCreateInfo.minLod = samplerStateCreateInfo->minLod;
vkSamplerCreateInfo.maxLod = samplerStateCreateInfo->maxLod;
2020-12-18 20:58:03 +00:00
vkSamplerCreateInfo.borderColor = RefreshToVK_BorderColor[
samplerStateCreateInfo->borderColor
];
2020-12-18 01:48:26 +00:00
vkSamplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
vulkanResult = renderer->vkCreateSampler(
renderer->logicalDevice,
&vkSamplerCreateInfo,
NULL,
&sampler
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateSampler", vulkanResult);
return NULL;
}
2021-01-05 23:00:51 +00:00
return (Refresh_Sampler*) sampler;
2020-12-17 01:23:49 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_Framebuffer* VULKAN_CreateFramebuffer(
Refresh_Renderer *driverData,
Refresh_FramebufferCreateInfo *framebufferCreateInfo
2020-12-17 01:23:49 +00:00
) {
2020-12-18 20:58:03 +00:00
VkResult vulkanResult;
VkFramebufferCreateInfo vkFramebufferCreateInfo;
VkImageView *imageViews;
uint32_t colorAttachmentCount = framebufferCreateInfo->colorTargetCount;
uint32_t attachmentCount = colorAttachmentCount;
2020-12-18 20:58:03 +00:00
uint32_t i;
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
2020-12-23 07:17:09 +00:00
VulkanFramebuffer *vulkanFramebuffer = (VulkanFramebuffer*) SDL_malloc(sizeof(VulkanFramebuffer));
2020-12-18 20:58:03 +00:00
2020-12-23 07:17:09 +00:00
if (framebufferCreateInfo->pDepthStencilTarget != NULL)
{
attachmentCount += 1;
}
imageViews = SDL_stack_alloc(VkImageView, attachmentCount);
2020-12-18 20:58:03 +00:00
for (i = 0; i < colorAttachmentCount; i += 1)
{
2020-12-19 00:39:03 +00:00
imageViews[i] = ((VulkanColorTarget*)framebufferCreateInfo->pColorTargets[i])->view;
2020-12-18 20:58:03 +00:00
}
2020-12-23 07:17:09 +00:00
if (framebufferCreateInfo->pDepthStencilTarget != NULL)
{
2020-12-23 07:17:09 +00:00
imageViews[colorAttachmentCount] = ((VulkanDepthStencilTarget*)framebufferCreateInfo->pDepthStencilTarget)->view;
}
2020-12-18 20:58:03 +00:00
vkFramebufferCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
vkFramebufferCreateInfo.pNext = NULL;
vkFramebufferCreateInfo.flags = 0;
vkFramebufferCreateInfo.renderPass = (VkRenderPass) framebufferCreateInfo->renderPass;
vkFramebufferCreateInfo.attachmentCount = attachmentCount;
2020-12-18 20:58:03 +00:00
vkFramebufferCreateInfo.pAttachments = imageViews;
vkFramebufferCreateInfo.width = framebufferCreateInfo->width;
vkFramebufferCreateInfo.height = framebufferCreateInfo->height;
2020-12-19 00:39:03 +00:00
vkFramebufferCreateInfo.layers = 1;
2020-12-18 20:58:03 +00:00
vulkanResult = renderer->vkCreateFramebuffer(
renderer->logicalDevice,
&vkFramebufferCreateInfo,
NULL,
2020-12-23 07:17:09 +00:00
&vulkanFramebuffer->framebuffer
2020-12-18 20:58:03 +00:00
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateFramebuffer", vulkanResult);
SDL_stack_free(imageViews);
return NULL;
}
2020-12-23 07:17:09 +00:00
for (i = 0; i < colorAttachmentCount; i += 1)
{
vulkanFramebuffer->colorTargets[i] =
(VulkanColorTarget*) framebufferCreateInfo->pColorTargets[i];
}
vulkanFramebuffer->colorTargetCount = colorAttachmentCount;
vulkanFramebuffer->depthStencilTarget =
(VulkanDepthStencilTarget*) framebufferCreateInfo->pDepthStencilTarget;
2020-12-29 06:19:46 +00:00
vulkanFramebuffer->width = framebufferCreateInfo->width;
vulkanFramebuffer->height = framebufferCreateInfo->height;
2020-12-18 20:58:03 +00:00
SDL_stack_free(imageViews);
2021-01-05 23:00:51 +00:00
return (Refresh_Framebuffer*) vulkanFramebuffer;
2020-12-17 01:23:49 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_ShaderModule* VULKAN_CreateShaderModule(
Refresh_Renderer *driverData,
Refresh_ShaderModuleCreateInfo *shaderModuleCreateInfo
2020-12-17 01:23:49 +00:00
) {
2020-12-19 01:25:22 +00:00
VkResult vulkanResult;
VkShaderModule shaderModule;
VkShaderModuleCreateInfo vkShaderModuleCreateInfo;
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
vkShaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
vkShaderModuleCreateInfo.pNext = NULL;
vkShaderModuleCreateInfo.flags = 0;
vkShaderModuleCreateInfo.codeSize = shaderModuleCreateInfo->codeSize;
vkShaderModuleCreateInfo.pCode = (uint32_t*) shaderModuleCreateInfo->byteCode;
vulkanResult = renderer->vkCreateShaderModule(
renderer->logicalDevice,
&vkShaderModuleCreateInfo,
NULL,
&shaderModule
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateShaderModule", vulkanResult);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create shader module!");
2020-12-19 01:25:22 +00:00
return NULL;
}
2021-01-05 23:00:51 +00:00
return (Refresh_ShaderModule*) shaderModule;
2020-12-17 01:23:49 +00:00
}
2020-12-18 22:35:33 +00:00
/* texture should be an alloc'd but uninitialized VulkanTexture */
static uint8_t VULKAN_INTERNAL_CreateTexture(
VulkanRenderer *renderer,
uint32_t width,
uint32_t height,
uint32_t depth,
uint32_t isCube,
VkSampleCountFlagBits samples,
uint32_t levelCount,
VkFormat format,
VkImageAspectFlags aspectMask,
VkImageTiling tiling,
VkImageType imageType,
VkImageUsageFlags imageUsageFlags,
2021-01-05 23:00:51 +00:00
Refresh_TextureUsageFlags textureUsageFlags,
2020-12-18 22:35:33 +00:00
VulkanTexture *texture
) {
VkResult vulkanResult;
VkImageCreateInfo imageCreateInfo;
2020-12-19 00:39:03 +00:00
VkImageCreateFlags imageCreateFlags = 0;
2020-12-18 22:35:33 +00:00
VkImageViewCreateInfo imageViewCreateInfo;
uint8_t findMemoryResult;
2020-12-19 00:39:03 +00:00
uint8_t is3D = depth > 1 ? 1 : 0;
2020-12-18 22:35:33 +00:00
uint8_t layerCount = isCube ? 6 : 1;
VkComponentMapping swizzle = IDENTITY_SWIZZLE;
2021-01-03 22:37:02 +00:00
texture->isCube = 0;
texture->is3D = 0;
2020-12-19 00:39:03 +00:00
if (isCube)
{
imageCreateFlags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
2021-01-03 22:37:02 +00:00
texture->isCube = 1;
2020-12-19 00:39:03 +00:00
}
else if (is3D)
{
imageCreateFlags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
2021-01-03 22:37:02 +00:00
texture->is3D = 1;
2020-12-19 00:39:03 +00:00
}
2020-12-18 22:35:33 +00:00
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.pNext = NULL;
2020-12-19 00:39:03 +00:00
imageCreateInfo.flags = imageCreateFlags;
2020-12-18 22:35:33 +00:00
imageCreateInfo.imageType = imageType;
imageCreateInfo.format = format;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.extent.depth = depth;
imageCreateInfo.mipLevels = levelCount;
imageCreateInfo.arrayLayers = layerCount;
imageCreateInfo.samples = samples;
imageCreateInfo.tiling = tiling;
imageCreateInfo.usage = imageUsageFlags;
2020-12-18 22:35:33 +00:00
// FIXME: would this interfere with pixel data sharing?
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.queueFamilyIndexCount = 0;
imageCreateInfo.pQueueFamilyIndices = NULL;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
vulkanResult = renderer->vkCreateImage(
renderer->logicalDevice,
&imageCreateInfo,
NULL,
&texture->image
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateImage", vulkanResult);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create texture!");
2020-12-18 22:35:33 +00:00
}
findMemoryResult = VULKAN_INTERNAL_FindAvailableMemory(
renderer,
VK_NULL_HANDLE,
texture->image,
&texture->allocation,
&texture->offset,
&texture->memorySize
);
/* No device memory available, time to die */
if (findMemoryResult == 0 || findMemoryResult == 2)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to find texture memory!");
2020-12-18 22:35:33 +00:00
return 0;
}
2021-01-03 21:12:12 +00:00
SDL_LockMutex(texture->allocation->memoryLock);
2020-12-18 22:35:33 +00:00
vulkanResult = renderer->vkBindImageMemory(
renderer->logicalDevice,
texture->image,
texture->allocation->memory,
texture->offset
);
2021-01-03 21:12:12 +00:00
SDL_UnlockMutex(texture->allocation->memoryLock);
2020-12-18 22:35:33 +00:00
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkBindImageMemory", vulkanResult);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to bind texture memory!");
2020-12-18 22:35:33 +00:00
return 0;
}
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.pNext = NULL;
imageViewCreateInfo.flags = 0;
imageViewCreateInfo.image = texture->image;
imageViewCreateInfo.format = format;
imageViewCreateInfo.components = swizzle;
imageViewCreateInfo.subresourceRange.aspectMask = aspectMask;
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = levelCount;
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imageViewCreateInfo.subresourceRange.layerCount = layerCount;
if (isCube)
{
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
}
else if (imageType == VK_IMAGE_TYPE_2D)
{
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
}
else if (imageType == VK_IMAGE_TYPE_3D)
{
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_3D;
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("invalid image type: %u", imageType);
2020-12-18 22:35:33 +00:00
}
vulkanResult = renderer->vkCreateImageView(
renderer->logicalDevice,
&imageViewCreateInfo,
NULL,
&texture->view
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateImageView", vulkanResult);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create texture image view");
2020-12-18 22:35:33 +00:00
return 0;
}
texture->dimensions.width = width;
texture->dimensions.height = height;
texture->depth = depth;
2020-12-19 00:39:03 +00:00
texture->format = format;
2020-12-18 22:35:33 +00:00
texture->levelCount = levelCount;
texture->layerCount = layerCount;
texture->resourceAccessType = RESOURCE_ACCESS_NONE;
2021-01-03 03:03:25 +00:00
texture->queueFamilyIndex = renderer->queueFamilyIndices.graphicsFamily;
texture->usageFlags = textureUsageFlags;
2020-12-18 22:35:33 +00:00
return 1;
}
static Refresh_Texture* VULKAN_CreateTexture(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_TextureCreateInfo *textureCreateInfo
2020-12-17 01:23:49 +00:00
) {
2020-12-18 22:35:33 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanTexture *result;
VkImageUsageFlags imageUsageFlags = (
2020-12-18 22:35:33 +00:00
VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT
);
if (textureCreateInfo->usageFlags & REFRESH_TEXTUREUSAGE_COLOR_TARGET_BIT)
2020-12-18 22:35:33 +00:00
{
imageUsageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
2020-12-18 22:35:33 +00:00
}
result = (VulkanTexture*) SDL_malloc(sizeof(VulkanTexture));
VULKAN_INTERNAL_CreateTexture(
renderer,
textureCreateInfo->width,
textureCreateInfo->height,
textureCreateInfo->depth,
textureCreateInfo->isCube,
2020-12-18 22:35:33 +00:00
VK_SAMPLE_COUNT_1_BIT,
textureCreateInfo->levelCount,
RefreshToVK_SurfaceFormat[textureCreateInfo->format],
2020-12-18 22:35:33 +00:00
VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_TYPE_2D,
imageUsageFlags,
textureCreateInfo->usageFlags,
2020-12-18 22:35:33 +00:00
result
);
result->colorFormat = textureCreateInfo->format;
2020-12-18 22:35:33 +00:00
2021-01-05 23:00:51 +00:00
return (Refresh_Texture*) result;
2020-12-18 22:35:33 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_ColorTarget* VULKAN_CreateColorTarget(
Refresh_Renderer *driverData,
Refresh_SampleCount multisampleCount,
Refresh_TextureSlice *textureSlice
2020-12-18 22:35:33 +00:00
) {
2020-12-19 00:39:03 +00:00
VkResult vulkanResult;
2020-12-18 22:35:33 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
2020-12-19 00:39:03 +00:00
VulkanColorTarget *colorTarget = (VulkanColorTarget*) SDL_malloc(sizeof(VulkanColorTarget));
VkImageViewCreateInfo imageViewCreateInfo;
VkComponentMapping swizzle = IDENTITY_SWIZZLE;
2020-12-18 22:35:33 +00:00
colorTarget->texture = (VulkanTexture*) textureSlice->texture;
2020-12-23 07:17:09 +00:00
colorTarget->layer = textureSlice->layer;
2020-12-19 00:39:03 +00:00
colorTarget->multisampleTexture = NULL;
colorTarget->multisampleCount = 1;
2020-12-18 22:35:33 +00:00
2020-12-19 00:39:03 +00:00
/* create resolve target for multisample */
2020-12-22 01:59:08 +00:00
if (multisampleCount > REFRESH_SAMPLECOUNT_1)
2020-12-19 00:39:03 +00:00
{
colorTarget->multisampleTexture =
(VulkanTexture*) SDL_malloc(sizeof(VulkanTexture));
VULKAN_INTERNAL_CreateTexture(
renderer,
colorTarget->texture->dimensions.width,
colorTarget->texture->dimensions.height,
1,
0,
RefreshToVK_SampleCount[multisampleCount],
1,
2020-12-29 07:41:59 +00:00
colorTarget->texture->format,
2020-12-19 00:39:03 +00:00
VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_TYPE_2D,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
REFRESH_TEXTUREUSAGE_COLOR_TARGET_BIT,
2020-12-19 00:39:03 +00:00
colorTarget->multisampleTexture
);
2020-12-29 07:41:59 +00:00
colorTarget->multisampleTexture->colorFormat = colorTarget->texture->colorFormat;
2020-12-19 00:39:03 +00:00
colorTarget->multisampleCount = multisampleCount;
}
/* create framebuffer compatible views for RenderTarget */
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.pNext = NULL;
imageViewCreateInfo.flags = 0;
imageViewCreateInfo.image = colorTarget->texture->image;
imageViewCreateInfo.format = colorTarget->texture->format;
imageViewCreateInfo.components = swizzle;
imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = 1;
2021-01-03 22:37:02 +00:00
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
if (colorTarget->texture->is3D)
{
imageViewCreateInfo.subresourceRange.baseArrayLayer = textureSlice->depth;
}
else if (colorTarget->texture->isCube)
{
imageViewCreateInfo.subresourceRange.baseArrayLayer = textureSlice->layer;
}
2020-12-19 00:39:03 +00:00
imageViewCreateInfo.subresourceRange.layerCount = 1;
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
vulkanResult = renderer->vkCreateImageView(
renderer->logicalDevice,
&imageViewCreateInfo,
NULL,
&colorTarget->view
2020-12-18 22:35:33 +00:00
);
2020-12-19 00:39:03 +00:00
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult(
"vkCreateImageView",
vulkanResult
);
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create color attachment image view");
2020-12-19 00:39:03 +00:00
return NULL;
}
2020-12-17 01:23:49 +00:00
2021-01-05 23:00:51 +00:00
return (Refresh_ColorTarget*) colorTarget;
2020-12-17 01:23:49 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_DepthStencilTarget* VULKAN_CreateDepthStencilTarget(
Refresh_Renderer *driverData,
2020-12-17 01:23:49 +00:00
uint32_t width,
uint32_t height,
2021-01-05 23:00:51 +00:00
Refresh_DepthFormat format
2020-12-17 01:23:49 +00:00
) {
2020-12-19 00:39:03 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanDepthStencilTarget *depthStencilTarget =
(VulkanDepthStencilTarget*) SDL_malloc(
sizeof(VulkanDepthStencilTarget)
);
2020-12-29 04:09:31 +00:00
VulkanTexture *texture =
(VulkanTexture*) SDL_malloc(sizeof(VulkanTexture));
2020-12-19 00:39:03 +00:00
2020-12-29 04:09:31 +00:00
VkImageAspectFlags imageAspectFlags = VK_IMAGE_ASPECT_DEPTH_BIT;
VkImageUsageFlags imageUsageFlags =
VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
if (DepthFormatContainsStencil(RefreshToVK_DepthFormat[format]))
{
imageAspectFlags |= VK_IMAGE_ASPECT_STENCIL_BIT;
}
VULKAN_INTERNAL_CreateTexture(
2020-12-19 00:39:03 +00:00
renderer,
width,
height,
2020-12-29 04:09:31 +00:00
1,
0,
VK_SAMPLE_COUNT_1_BIT,
1,
2020-12-19 00:39:03 +00:00
RefreshToVK_DepthFormat[format],
2020-12-29 04:09:31 +00:00
imageAspectFlags,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_TYPE_2D,
imageUsageFlags,
0,
2020-12-19 00:39:03 +00:00
texture
);
2020-12-29 07:41:59 +00:00
texture->depthStencilFormat = format;
2020-12-19 00:39:03 +00:00
depthStencilTarget->texture = texture;
depthStencilTarget->view = texture->view;
2021-01-05 23:00:51 +00:00
return (Refresh_DepthStencilTarget*) depthStencilTarget;
2020-12-17 01:23:49 +00:00
}
2021-01-05 23:00:51 +00:00
static Refresh_Buffer* VULKAN_CreateBuffer(
Refresh_Renderer *driverData,
Refresh_BufferUsageFlags usageFlags,
2020-12-17 01:23:49 +00:00
uint32_t sizeInBytes
) {
2020-12-19 01:32:27 +00:00
VulkanBuffer *buffer = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
2021-01-03 03:03:25 +00:00
VkBufferUsageFlags vulkanUsageFlags =
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
2020-12-31 04:39:47 +00:00
if (usageFlags & REFRESH_BUFFERUSAGE_VERTEX_BIT)
{
vulkanUsageFlags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
2020-12-19 01:32:27 +00:00
}
2020-12-31 04:39:47 +00:00
if (usageFlags & REFRESH_BUFFERUSAGE_INDEX_BIT)
{
vulkanUsageFlags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
}
2020-12-17 01:23:49 +00:00
2020-12-31 06:28:37 +00:00
if (usageFlags & REFRESH_BUFFERUSAGE_COMPUTE_BIT)
2020-12-31 04:39:47 +00:00
{
vulkanUsageFlags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
}
2020-12-19 01:32:27 +00:00
2020-12-31 04:39:47 +00:00
if(!VULKAN_INTERNAL_CreateBuffer(
2020-12-19 01:32:27 +00:00
(VulkanRenderer*) driverData,
sizeInBytes,
2020-12-31 04:39:47 +00:00
RESOURCE_ACCESS_VERTEX_BUFFER,
vulkanUsageFlags,
2020-12-19 04:08:07 +00:00
SUB_BUFFER_COUNT,
2020-12-19 01:32:27 +00:00
buffer
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create vertex buffer!");
2020-12-19 01:32:27 +00:00
return NULL;
}
2021-01-05 23:00:51 +00:00
return (Refresh_Buffer*) buffer;
2020-12-17 01:23:49 +00:00
}
2020-12-19 04:08:07 +00:00
/* Setters */
static void VULKAN_INTERNAL_MaybeExpandStagingBuffer(
VulkanRenderer *renderer,
uint32_t textureSize
2020-12-19 04:08:07 +00:00
) {
VkDeviceSize currentStagingSize = renderer->textureStagingBuffer->size;
if (renderer->textureStagingBufferOffset + textureSize <= renderer->textureStagingBuffer->size)
2020-12-19 04:08:07 +00:00
{
return;
}
/* not enough room in the staging buffer, time to flush */
VULKAN_INTERNAL_FlushTransfers(renderer);
2020-12-19 04:08:07 +00:00
/* double staging buffer size up to max */
if (currentStagingSize * 2 <= MAX_TEXTURE_STAGING_SIZE)
{
VULKAN_INTERNAL_DestroyTextureStagingBuffer(renderer);
2020-12-19 04:08:07 +00:00
renderer->textureStagingBuffer = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
currentStagingSize * 2,
RESOURCE_ACCESS_MEMORY_TRANSFER_READ_WRITE,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
1,
renderer->textureStagingBuffer
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to expand texture staging buffer!");
return;
}
2020-12-19 04:08:07 +00:00
}
}
2021-01-03 03:03:25 +00:00
static void VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(
VulkanRenderer *renderer
) {
VkCommandBufferBeginInfo transferCommandBufferBeginInfo;
if (!renderer->pendingTransfer)
{
transferCommandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
transferCommandBufferBeginInfo.pNext = NULL;
transferCommandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
transferCommandBufferBeginInfo.pInheritanceInfo = NULL;
renderer->vkBeginCommandBuffer(
renderer->transferCommandBuffers[renderer->frameIndex],
&transferCommandBufferBeginInfo
);
renderer->pendingTransfer = 1;
}
}
static void VULKAN_INTERNAL_EndTransferCommandBuffer(
VulkanRenderer *renderer
) {
if (renderer->pendingTransfer)
{
renderer->vkEndCommandBuffer(
renderer->transferCommandBuffers[renderer->frameIndex]
);
}
}
/* Hard sync point! */
static void VULKAN_INTERNAL_FlushTransfers(
VulkanRenderer *renderer
) {
VkSubmitInfo transferSubmitInfo;
VkResult vulkanResult;
if (renderer->pendingTransfer)
{
VULKAN_INTERNAL_EndTransferCommandBuffer(renderer);
transferSubmitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
transferSubmitInfo.pNext = NULL;
transferSubmitInfo.commandBufferCount = 1;
transferSubmitInfo.pCommandBuffers = &renderer->transferCommandBuffers[renderer->frameIndex];
transferSubmitInfo.pWaitDstStageMask = NULL;
transferSubmitInfo.pWaitSemaphores = NULL;
transferSubmitInfo.waitSemaphoreCount = 0;
transferSubmitInfo.pSignalSemaphores = NULL;
transferSubmitInfo.signalSemaphoreCount = 0;
/* Wait for the previous submission to complete */
vulkanResult = renderer->vkWaitForFences(
renderer->logicalDevice,
1,
&renderer->inFlightFence,
VK_TRUE,
UINT64_MAX
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkWaitForFences", vulkanResult);
return;
}
renderer->vkResetFences(
renderer->logicalDevice,
1,
&renderer->inFlightFence
);
/* Submit transfers */
vulkanResult = renderer->vkQueueSubmit(
renderer->transferQueue,
1,
&transferSubmitInfo,
NULL
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkQueueSubmit", vulkanResult);
return;
}
/* Wait again */
vulkanResult = renderer->vkWaitForFences(
renderer->logicalDevice,
1,
&renderer->inFlightFence,
VK_TRUE,
UINT64_MAX
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkWaitForFences", vulkanResult);
return;
}
renderer->vkResetFences(
renderer->logicalDevice,
1,
&renderer->inFlightFence
);
renderer->pendingTransfer = 0;
renderer->textureStagingBufferOffset = 0;
}
}
2021-01-03 22:37:02 +00:00
static void VULKAN_SetTextureData(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_TextureSlice *textureSlice,
2020-12-17 01:23:49 +00:00
void *data,
uint32_t dataLengthInBytes
) {
2020-12-19 01:03:26 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
2021-01-03 22:37:02 +00:00
VulkanTexture *vulkanTexture = (VulkanTexture*) textureSlice->texture;
2021-01-02 21:31:17 +00:00
VkCommandBuffer commandBuffer = renderer->transferCommandBuffers[renderer->frameIndex];
2020-12-19 01:03:26 +00:00
VkBufferImageCopy imageCopy;
2021-01-14 02:02:45 +00:00
uint8_t *stagingBufferPointer;
2020-12-19 01:03:26 +00:00
2021-01-03 21:17:51 +00:00
SDL_LockMutex(renderer->stagingLock);
2020-12-19 04:08:07 +00:00
VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLengthInBytes);
VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer);
2020-12-19 04:08:07 +00:00
2021-01-14 02:02:45 +00:00
stagingBufferPointer =
renderer->textureStagingBuffer->subBuffers[0]->allocation->mapPointer +
renderer->textureStagingBuffer->subBuffers[0]->offset +
renderer->textureStagingBufferOffset;
2020-12-19 01:03:26 +00:00
2021-01-14 02:02:45 +00:00
SDL_memcpy(
stagingBufferPointer,
data,
dataLengthInBytes
2020-12-19 01:03:26 +00:00
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-03 04:03:07 +00:00
commandBuffer,
2020-12-19 01:03:26 +00:00
RESOURCE_ACCESS_TRANSFER_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
2021-01-03 22:37:02 +00:00
textureSlice->layer,
1,
textureSlice->level,
1,
2020-12-19 01:03:26 +00:00
0,
vulkanTexture->image,
&vulkanTexture->resourceAccessType
);
2021-01-03 22:37:02 +00:00
imageCopy.imageExtent.width = textureSlice->rectangle.w;
imageCopy.imageExtent.height = textureSlice->rectangle.h;
2020-12-19 01:03:26 +00:00
imageCopy.imageExtent.depth = 1;
2021-01-03 22:37:02 +00:00
imageCopy.imageOffset.x = textureSlice->rectangle.x;
imageCopy.imageOffset.y = textureSlice->rectangle.y;
imageCopy.imageOffset.z = textureSlice->depth;
2020-12-19 01:03:26 +00:00
imageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2021-01-03 22:37:02 +00:00
imageCopy.imageSubresource.baseArrayLayer = textureSlice->layer;
2020-12-19 01:03:26 +00:00
imageCopy.imageSubresource.layerCount = 1;
2021-01-03 22:37:02 +00:00
imageCopy.imageSubresource.mipLevel = textureSlice->level;
imageCopy.bufferOffset = renderer->textureStagingBufferOffset;
2020-12-19 01:03:26 +00:00
imageCopy.bufferRowLength = 0;
imageCopy.bufferImageHeight = 0;
2021-01-02 06:07:15 +00:00
renderer->vkCmdCopyBufferToImage(
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 04:08:07 +00:00
renderer->textureStagingBuffer->subBuffers[0]->buffer,
2020-12-19 01:03:26 +00:00
vulkanTexture->image,
AccessMap[vulkanTexture->resourceAccessType].imageLayout,
1,
&imageCopy
2021-01-02 06:07:15 +00:00
);
renderer->textureStagingBufferOffset += dataLengthInBytes;
if (vulkanTexture->usageFlags & REFRESH_TEXTUREUSAGE_SAMPLER_BIT)
{
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
commandBuffer,
RESOURCE_ACCESS_ANY_SHADER_READ_SAMPLED_IMAGE,
VK_IMAGE_ASPECT_COLOR_BIT,
2021-01-03 22:37:02 +00:00
textureSlice->layer,
1,
textureSlice->level,
1,
0,
vulkanTexture->image,
&vulkanTexture->resourceAccessType
);
}
2021-01-03 21:17:51 +00:00
SDL_UnlockMutex(renderer->stagingLock);
2020-12-17 01:23:49 +00:00
}
static void VULKAN_SetTextureDataYUV(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Texture *y,
Refresh_Texture *u,
Refresh_Texture *v,
2020-12-17 01:23:49 +00:00
uint32_t yWidth,
uint32_t yHeight,
uint32_t uvWidth,
uint32_t uvHeight,
void* data,
uint32_t dataLength
) {
2020-12-19 02:38:15 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanTexture *tex;
2021-01-02 21:31:17 +00:00
VkCommandBuffer commandBuffer = renderer->transferCommandBuffers[renderer->frameIndex];
2020-12-19 02:38:15 +00:00
uint8_t *dataPtr = (uint8_t*) data;
2021-01-03 22:53:12 +00:00
int32_t yDataLength = BytesPerImage(yWidth, yHeight, REFRESH_COLORFORMAT_R8);
int32_t uvDataLength = BytesPerImage(uvWidth, uvHeight, REFRESH_COLORFORMAT_R8);
2020-12-19 02:38:15 +00:00
VkBufferImageCopy imageCopy;
2021-01-14 02:02:45 +00:00
uint8_t * stagingBufferPointer;
2020-12-19 02:38:15 +00:00
2021-01-03 21:17:51 +00:00
SDL_LockMutex(renderer->stagingLock);
2020-12-19 04:08:07 +00:00
VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength);
VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer);
2020-12-19 04:08:07 +00:00
2021-01-14 02:02:45 +00:00
stagingBufferPointer =
renderer->textureStagingBuffer->subBuffers[0]->allocation->mapPointer +
renderer->textureStagingBuffer->subBuffers[0]->offset +
renderer->textureStagingBufferOffset;
2021-01-03 21:12:12 +00:00
2020-12-19 02:38:15 +00:00
/* Initialize values that are the same for Y, U, and V */
imageCopy.imageExtent.depth = 1;
imageCopy.imageOffset.x = 0;
imageCopy.imageOffset.y = 0;
imageCopy.imageOffset.z = 0;
imageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageCopy.imageSubresource.baseArrayLayer = 0;
imageCopy.imageSubresource.layerCount = 1;
imageCopy.imageSubresource.mipLevel = 0;
/* Y */
tex = (VulkanTexture*) y;
SDL_memcpy(
2021-01-14 02:02:45 +00:00
stagingBufferPointer,
2020-12-19 02:38:15 +00:00
dataPtr,
yDataLength
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 02:38:15 +00:00
RESOURCE_ACCESS_TRANSFER_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
0,
tex->layerCount,
0,
tex->levelCount,
0,
tex->image,
&tex->resourceAccessType
);
2020-12-19 02:38:15 +00:00
imageCopy.imageExtent.width = yWidth;
imageCopy.imageExtent.height = yHeight;
imageCopy.bufferOffset = renderer->textureStagingBufferOffset;
2020-12-19 02:38:15 +00:00
imageCopy.bufferRowLength = yWidth;
imageCopy.bufferImageHeight = yHeight;
2021-01-02 06:07:15 +00:00
renderer->vkCmdCopyBufferToImage(
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 04:08:07 +00:00
renderer->textureStagingBuffer->subBuffers[0]->buffer,
2020-12-19 02:38:15 +00:00
tex->image,
AccessMap[tex->resourceAccessType].imageLayout,
1,
&imageCopy
2021-01-02 06:07:15 +00:00
);
2020-12-19 02:38:15 +00:00
/* These apply to both U and V */
imageCopy.imageExtent.width = uvWidth;
imageCopy.imageExtent.height = uvHeight;
imageCopy.bufferRowLength = uvWidth;
imageCopy.bufferImageHeight = uvHeight;
/* U */
imageCopy.bufferOffset = renderer->textureStagingBufferOffset + yDataLength;
2020-12-19 02:38:15 +00:00
tex = (VulkanTexture*) u;
SDL_memcpy(
2021-01-14 02:02:45 +00:00
stagingBufferPointer + yDataLength,
2020-12-19 02:38:15 +00:00
dataPtr + yDataLength,
uvDataLength
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 02:38:15 +00:00
RESOURCE_ACCESS_TRANSFER_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
0,
tex->layerCount,
0,
tex->levelCount,
0,
tex->image,
&tex->resourceAccessType
);
2021-01-02 06:07:15 +00:00
renderer->vkCmdCopyBufferToImage(
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 04:08:07 +00:00
renderer->textureStagingBuffer->subBuffers[0]->buffer,
2020-12-19 02:38:15 +00:00
tex->image,
AccessMap[tex->resourceAccessType].imageLayout,
1,
&imageCopy
2021-01-02 06:07:15 +00:00
);
2020-12-19 02:38:15 +00:00
/* V */
imageCopy.bufferOffset = renderer->textureStagingBufferOffset + uvDataLength;
2020-12-19 02:38:15 +00:00
tex = (VulkanTexture*) v;
SDL_memcpy(
2021-01-14 02:02:45 +00:00
stagingBufferPointer + yDataLength + uvDataLength,
2020-12-19 02:38:15 +00:00
dataPtr + yDataLength + uvDataLength,
uvDataLength
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 02:38:15 +00:00
RESOURCE_ACCESS_TRANSFER_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
0,
tex->layerCount,
0,
tex->levelCount,
0,
tex->image,
&tex->resourceAccessType
);
2021-01-02 06:07:15 +00:00
renderer->vkCmdCopyBufferToImage(
2021-01-02 21:31:17 +00:00
commandBuffer,
2020-12-19 04:08:07 +00:00
renderer->textureStagingBuffer->subBuffers[0]->buffer,
2020-12-19 02:38:15 +00:00
tex->image,
AccessMap[tex->resourceAccessType].imageLayout,
1,
&imageCopy
2021-01-02 06:07:15 +00:00
);
renderer->textureStagingBufferOffset += dataLength;
if (tex->usageFlags & REFRESH_TEXTUREUSAGE_SAMPLER_BIT)
{
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
commandBuffer,
RESOURCE_ACCESS_ANY_SHADER_READ_SAMPLED_IMAGE,
VK_IMAGE_ASPECT_COLOR_BIT,
0,
tex->layerCount,
0,
tex->levelCount,
0,
tex->image,
&tex->resourceAccessType
);
}
2021-01-03 21:17:51 +00:00
SDL_UnlockMutex(renderer->stagingLock);
2020-12-17 01:23:49 +00:00
}
2021-01-03 21:01:29 +00:00
static void VULKAN_INTERNAL_BlitImage(
VulkanRenderer *renderer,
VkCommandBuffer commandBuffer,
2021-01-05 23:00:51 +00:00
Refresh_Rect *sourceRectangle,
2021-01-03 22:37:02 +00:00
uint32_t sourceDepth,
2021-01-03 21:01:29 +00:00
uint32_t sourceLayer,
uint32_t sourceLevel,
VkImage sourceImage,
VulkanResourceAccessType *currentSourceAccessType,
VulkanResourceAccessType nextSourceAccessType,
2021-01-05 23:00:51 +00:00
Refresh_Rect *destinationRectangle,
2021-01-03 22:37:02 +00:00
uint32_t destinationDepth,
2021-01-03 21:01:29 +00:00
uint32_t destinationLayer,
uint32_t destinationLevel,
VkImage destinationImage,
VulkanResourceAccessType *currentDestinationAccessType,
VulkanResourceAccessType nextDestinationAccessType,
VkFilter filter
) {
VkImageBlit blit;
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
commandBuffer,
RESOURCE_ACCESS_TRANSFER_READ,
VK_IMAGE_ASPECT_COLOR_BIT,
sourceLayer,
1,
sourceLevel,
1,
0,
sourceImage,
currentSourceAccessType
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
commandBuffer,
RESOURCE_ACCESS_TRANSFER_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
destinationLayer,
1,
destinationLevel,
1,
0,
destinationImage,
currentDestinationAccessType
);
blit.srcOffsets[0].x = sourceRectangle->x;
blit.srcOffsets[0].y = sourceRectangle->y;
2021-01-03 22:37:02 +00:00
blit.srcOffsets[0].z = sourceDepth;
2021-01-03 21:01:29 +00:00
blit.srcOffsets[1].x = sourceRectangle->x + sourceRectangle->w;
blit.srcOffsets[1].y = sourceRectangle->y + sourceRectangle->h;
blit.srcOffsets[1].z = 1;
blit.srcSubresource.mipLevel = sourceLevel;
blit.srcSubresource.baseArrayLayer = sourceLayer;
blit.srcSubresource.layerCount = 1;
blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
blit.dstOffsets[0].x = destinationRectangle->x;
blit.dstOffsets[0].y = destinationRectangle->y;
2021-01-03 22:37:02 +00:00
blit.dstOffsets[0].z = destinationDepth;
2021-01-03 21:01:29 +00:00
blit.dstOffsets[1].x = destinationRectangle->x + destinationRectangle->w;
blit.dstOffsets[1].y = destinationRectangle->y + destinationRectangle->h;
blit.dstOffsets[1].z = 1;
blit.dstSubresource.mipLevel = destinationLevel;
blit.dstSubresource.baseArrayLayer = destinationLayer;
blit.dstSubresource.layerCount = 1;
blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
renderer->vkCmdBlitImage(
commandBuffer,
sourceImage,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
destinationImage,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&blit,
filter
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
commandBuffer,
nextSourceAccessType,
VK_IMAGE_ASPECT_COLOR_BIT,
sourceLayer,
1,
sourceLevel,
1,
0,
sourceImage,
currentSourceAccessType
);
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
commandBuffer,
nextDestinationAccessType,
VK_IMAGE_ASPECT_COLOR_BIT,
destinationLayer,
1,
destinationLevel,
1,
0,
destinationImage,
currentDestinationAccessType
);
}
REFRESHAPI void VULKAN_CopyTextureToTexture(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_TextureSlice *sourceTextureSlice,
Refresh_TextureSlice *destinationTextureSlice,
Refresh_Filter filter
2021-01-03 21:01:29 +00:00
) {
VulkanRenderer *renderer = (VulkanRenderer*)driverData;
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanTexture *sourceTexture = (VulkanTexture*) sourceTextureSlice->texture;
VulkanTexture *destinationTexture = (VulkanTexture*) destinationTextureSlice->texture;
VULKAN_INTERNAL_BlitImage(
renderer,
vulkanCommandBuffer->commandBuffer,
2021-01-03 22:37:02 +00:00
&sourceTextureSlice->rectangle,
sourceTextureSlice->depth,
2021-01-03 21:01:29 +00:00
sourceTextureSlice->layer,
sourceTextureSlice->level,
sourceTexture->image,
&sourceTexture->resourceAccessType,
sourceTexture->resourceAccessType,
2021-01-03 22:37:02 +00:00
&destinationTextureSlice->rectangle,
destinationTextureSlice->depth,
2021-01-03 21:01:29 +00:00
destinationTextureSlice->layer,
destinationTextureSlice->level,
destinationTexture->image,
&destinationTexture->resourceAccessType,
destinationTexture->resourceAccessType,
RefreshToVK_Filter[filter]
);
}
2020-12-31 04:39:47 +00:00
static void VULKAN_SetBufferData(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Buffer *buffer,
2020-12-21 20:37:54 +00:00
uint32_t offsetInBytes,
void* data,
uint32_t dataLength
) {
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
VulkanBuffer* vulkanBuffer = (VulkanBuffer*)buffer;
2020-12-23 06:05:01 +00:00
uint32_t i;
2020-12-21 20:37:54 +00:00
2020-12-23 06:05:01 +00:00
#define CURIDX vulkanBuffer->currentSubBufferIndex
#define SUBBUF vulkanBuffer->subBuffers[CURIDX]
2020-12-21 20:37:54 +00:00
2020-12-23 06:05:01 +00:00
/* If buffer has not been bound this frame, set the first unbound index */
if (!vulkanBuffer->bound)
{
for (i = 0; i < vulkanBuffer->subBufferCount; i += 1)
{
if (vulkanBuffer->subBuffers[i]->bound == -1)
{
break;
}
}
CURIDX = i;
}
else
2020-12-21 20:37:54 +00:00
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Buffer already bound. It is an error to set vertex data after binding but before submitting.");
2020-12-21 20:37:54 +00:00
return;
}
SDL_memcpy(
2021-01-14 02:02:45 +00:00
SUBBUF->allocation->mapPointer + SUBBUF->offset + offsetInBytes,
2020-12-21 20:37:54 +00:00
data,
dataLength
);
2020-12-23 06:05:01 +00:00
#undef CURIDX
2020-12-21 20:37:54 +00:00
#undef SUBBUF
}
static uint32_t VULKAN_PushVertexShaderParams(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
void *data,
2020-12-23 21:11:09 +00:00
uint32_t elementCount
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
SDL_LockMutex(renderer->uniformBufferLock);
2020-12-21 21:02:07 +00:00
renderer->vertexUBOOffset += renderer->vertexUBOBlockIncrement;
2021-01-02 06:07:15 +00:00
renderer->vertexUBOBlockIncrement = vulkanCommandBuffer->currentGraphicsPipeline->vertexUBOBlockSize;
2020-12-21 21:02:07 +00:00
2020-12-23 21:11:09 +00:00
if (
renderer->vertexUBOOffset +
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline->vertexUBOBlockSize >=
2020-12-23 21:14:36 +00:00
UBO_BUFFER_SIZE * (renderer->frameIndex + 1)
2020-12-23 21:11:09 +00:00
) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Vertex UBO overflow!");
return 0;
2020-12-21 21:02:07 +00:00
}
2020-12-31 04:39:47 +00:00
VULKAN_SetBufferData(
2021-01-05 23:00:51 +00:00
(Refresh_Renderer*) renderer,
(Refresh_Buffer*) renderer->vertexUBO,
2020-12-21 21:02:07 +00:00
renderer->vertexUBOOffset,
data,
2021-01-02 06:07:15 +00:00
elementCount * vulkanCommandBuffer->currentGraphicsPipeline->vertexUBOBlockSize
2020-12-21 21:02:07 +00:00
);
2021-01-02 06:07:15 +00:00
SDL_UnlockMutex(renderer->uniformBufferLock);
return renderer->vertexUBOOffset;
}
static uint32_t VULKAN_PushFragmentShaderParams(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-17 01:23:49 +00:00
void *data,
2020-12-23 21:11:09 +00:00
uint32_t elementCount
2020-12-17 01:23:49 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
SDL_LockMutex(renderer->uniformBufferLock);
2020-12-21 21:02:07 +00:00
renderer->fragmentUBOOffset += renderer->fragmentUBOBlockIncrement;
2021-01-02 06:07:15 +00:00
renderer->fragmentUBOBlockIncrement = vulkanCommandBuffer->currentGraphicsPipeline->fragmentUBOBlockSize;
2020-12-21 21:02:07 +00:00
2020-12-23 21:11:09 +00:00
if (
renderer->fragmentUBOOffset +
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline->fragmentUBOBlockSize >=
2020-12-23 21:11:09 +00:00
UBO_BUFFER_SIZE * (renderer->frameIndex + 1)
) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Fragment UBO overflow!");
return 0;
2020-12-21 21:02:07 +00:00
}
2020-12-31 04:39:47 +00:00
VULKAN_SetBufferData(
2021-01-05 23:00:51 +00:00
(Refresh_Renderer*) renderer,
(Refresh_Buffer*) renderer->fragmentUBO,
2020-12-21 21:02:07 +00:00
renderer->fragmentUBOOffset,
data,
2021-01-02 06:07:15 +00:00
elementCount * vulkanCommandBuffer->currentGraphicsPipeline->fragmentUBOBlockSize
2020-12-21 21:02:07 +00:00
);
2021-01-02 06:07:15 +00:00
SDL_UnlockMutex(renderer->uniformBufferLock);
return renderer->fragmentUBOOffset;
2020-12-17 01:23:49 +00:00
}
2020-12-31 04:39:47 +00:00
static uint32_t VULKAN_PushComputeShaderParams(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-31 04:39:47 +00:00
void *data,
uint32_t elementCount
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
SDL_LockMutex(renderer->uniformBufferLock);
2020-12-31 04:39:47 +00:00
renderer->computeUBOOffset += renderer->computeUBOBlockIncrement;
2021-01-02 06:07:15 +00:00
renderer->computeUBOBlockIncrement = vulkanCommandBuffer->currentComputePipeline->computeUBOBlockSize;
2020-12-31 04:39:47 +00:00
if (
renderer->computeUBOOffset +
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentComputePipeline->computeUBOBlockSize >=
2020-12-31 04:39:47 +00:00
UBO_BUFFER_SIZE * (renderer->frameIndex + 1)
) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Compute UBO overflow!");
2020-12-31 04:39:47 +00:00
return 0;
}
VULKAN_SetBufferData(
2021-01-05 23:00:51 +00:00
(Refresh_Renderer*) renderer,
(Refresh_Buffer*) renderer->computeUBO,
2020-12-31 04:39:47 +00:00
renderer->computeUBOOffset,
data,
2021-01-02 06:07:15 +00:00
elementCount * vulkanCommandBuffer->currentComputePipeline->computeUBOBlockSize
2020-12-31 04:39:47 +00:00
);
2021-01-02 06:07:15 +00:00
SDL_UnlockMutex(renderer->uniformBufferLock);
2020-12-31 04:39:47 +00:00
return renderer->computeUBOOffset;
}
2020-12-31 00:47:13 +00:00
static inline uint8_t BufferDescriptorSetDataEqual(
BufferDescriptorSetData *a,
BufferDescriptorSetData *b,
uint8_t bindingCount
) {
uint32_t i;
for (i = 0; i < bindingCount; i += 1)
{
if ( a->descriptorBufferInfo[i].buffer != b->descriptorBufferInfo[i].buffer ||
a->descriptorBufferInfo[i].offset != b->descriptorBufferInfo[i].offset ||
a->descriptorBufferInfo[i].range != b->descriptorBufferInfo[i].range )
{
return 0;
}
}
return 1;
}
/* FIXME: this can probably be cleverly folded into the same cache structure as image descriptors */
static VkDescriptorSet VULKAN_INTERNAL_FetchBufferDescriptorSet(
VulkanRenderer *renderer,
BufferDescriptorSetCache *bufferDescriptorSetCache,
BufferDescriptorSetData *bufferDescriptorSetData
) {
uint32_t i;
uint64_t hashcode;
BufferDescriptorSetHashArray *arr;
VkDescriptorSet newDescriptorSet;
VkWriteDescriptorSet writeDescriptorSets[MAX_BUFFER_BINDINGS];
BufferDescriptorSetHashMap *map;
hashcode = BufferDescriptorSetHashTable_GetHashCode(
bufferDescriptorSetData,
bufferDescriptorSetCache->bindingCount
);
arr = &bufferDescriptorSetCache->buckets[hashcode % NUM_DESCRIPTOR_SET_HASH_BUCKETS];
for (i = 0; i < arr->count; i += 1)
{
BufferDescriptorSetHashMap *e = &bufferDescriptorSetCache->elements[arr->elements[i]];
if (BufferDescriptorSetDataEqual(
bufferDescriptorSetData,
&e->descriptorSetData,
bufferDescriptorSetCache->bindingCount
)) {
e->inactiveFrameCount = 0;
return e->descriptorSet;
}
}
/* If no match exists, assign a new descriptor set and prepare it for update */
/* If no inactive descriptor sets remain, create a new pool and allocate new inactive sets */
if (bufferDescriptorSetCache->inactiveDescriptorSetCount == 0)
{
bufferDescriptorSetCache->bufferDescriptorPoolCount += 1;
bufferDescriptorSetCache->bufferDescriptorPools = SDL_realloc(
bufferDescriptorSetCache->bufferDescriptorPools,
sizeof(VkDescriptorPool) * bufferDescriptorSetCache->bufferDescriptorPoolCount
);
VULKAN_INTERNAL_CreateDescriptorPool(
renderer,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
bufferDescriptorSetCache->nextPoolSize,
bufferDescriptorSetCache->nextPoolSize * bufferDescriptorSetCache->bindingCount,
&bufferDescriptorSetCache->bufferDescriptorPools[bufferDescriptorSetCache->bufferDescriptorPoolCount - 1]
);
bufferDescriptorSetCache->inactiveDescriptorSetCapacity += bufferDescriptorSetCache->nextPoolSize;
bufferDescriptorSetCache->inactiveDescriptorSets = SDL_realloc(
bufferDescriptorSetCache->inactiveDescriptorSets,
sizeof(VkDescriptorSet) * bufferDescriptorSetCache->inactiveDescriptorSetCapacity
);
VULKAN_INTERNAL_AllocateDescriptorSets(
renderer,
bufferDescriptorSetCache->bufferDescriptorPools[bufferDescriptorSetCache->bufferDescriptorPoolCount - 1],
bufferDescriptorSetCache->descriptorSetLayout,
bufferDescriptorSetCache->nextPoolSize,
bufferDescriptorSetCache->inactiveDescriptorSets
);
bufferDescriptorSetCache->inactiveDescriptorSetCount = bufferDescriptorSetCache->nextPoolSize;
bufferDescriptorSetCache->nextPoolSize *= 2;
}
newDescriptorSet = bufferDescriptorSetCache->inactiveDescriptorSets[bufferDescriptorSetCache->inactiveDescriptorSetCount - 1];
bufferDescriptorSetCache->inactiveDescriptorSetCount -= 1;
for (i = 0; i < bufferDescriptorSetCache->bindingCount; i += 1)
{
writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[i].pNext = NULL;
writeDescriptorSets[i].descriptorCount = 1;
writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
writeDescriptorSets[i].dstArrayElement = 0;
writeDescriptorSets[i].dstBinding = i;
writeDescriptorSets[i].dstSet = newDescriptorSet;
writeDescriptorSets[i].pBufferInfo = &bufferDescriptorSetData->descriptorBufferInfo[i];
writeDescriptorSets[i].pImageInfo = NULL;
writeDescriptorSets[i].pTexelBufferView = NULL;
}
renderer->vkUpdateDescriptorSets(
renderer->logicalDevice,
bufferDescriptorSetCache->bindingCount,
writeDescriptorSets,
0,
NULL
);
EXPAND_ELEMENTS_IF_NEEDED(arr, 2, uint32_t)
arr->elements[arr->count] = bufferDescriptorSetCache->count;
arr->count += 1;
if (bufferDescriptorSetCache->count == bufferDescriptorSetCache->capacity)
{
bufferDescriptorSetCache->capacity *= 2;
bufferDescriptorSetCache->elements = SDL_realloc(
bufferDescriptorSetCache->elements,
sizeof(BufferDescriptorSetHashMap) * bufferDescriptorSetCache->capacity
);
}
map = &bufferDescriptorSetCache->elements[bufferDescriptorSetCache->count];
map->key = hashcode;
for (i = 0; i < bufferDescriptorSetCache->bindingCount; i += 1)
{
map->descriptorSetData.descriptorBufferInfo[i].buffer =
bufferDescriptorSetData->descriptorBufferInfo[i].buffer;
map->descriptorSetData.descriptorBufferInfo[i].offset =
bufferDescriptorSetData->descriptorBufferInfo[i].offset;
map->descriptorSetData.descriptorBufferInfo[i].range =
bufferDescriptorSetData->descriptorBufferInfo[i].range;
}
map->descriptorSet = newDescriptorSet;
map->inactiveFrameCount = 0;
bufferDescriptorSetCache->count += 1;
return newDescriptorSet;
}
static inline uint8_t ImageDescriptorSetDataEqual(
2020-12-30 00:19:19 +00:00
ImageDescriptorSetData *a,
ImageDescriptorSetData *b,
2020-12-31 00:47:13 +00:00
uint8_t bindingCount
2020-12-17 01:23:49 +00:00
) {
2020-12-19 05:35:21 +00:00
uint32_t i;
2020-12-31 00:47:13 +00:00
for (i = 0; i < bindingCount; i += 1)
2020-12-27 23:20:59 +00:00
{
if ( a->descriptorImageInfo[i].imageLayout != b->descriptorImageInfo[i].imageLayout ||
a->descriptorImageInfo[i].imageView != b->descriptorImageInfo[i].imageView ||
a->descriptorImageInfo[i].sampler != b->descriptorImageInfo[i].sampler )
{
return 0;
}
}
2020-12-19 05:35:21 +00:00
2020-12-27 23:20:59 +00:00
return 1;
}
2020-12-30 00:53:10 +00:00
static VkDescriptorSet VULKAN_INTERNAL_FetchImageDescriptorSet(
2020-12-27 23:20:59 +00:00
VulkanRenderer *renderer,
2020-12-30 00:19:19 +00:00
ImageDescriptorSetCache *imageDescriptorSetCache,
2020-12-30 00:53:10 +00:00
ImageDescriptorSetData *imageDescriptorSetData
2020-12-27 23:20:59 +00:00
) {
uint32_t i;
uint64_t hashcode;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetHashArray *arr;
2020-12-27 23:20:59 +00:00
VkDescriptorSet newDescriptorSet;
VkWriteDescriptorSet writeDescriptorSets[MAX_TEXTURE_SAMPLERS];
2020-12-30 00:19:19 +00:00
ImageDescriptorSetHashMap *map;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
hashcode = ImageDescriptorSetHashTable_GetHashCode(
2020-12-30 00:53:10 +00:00
imageDescriptorSetData,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->bindingCount
);
2020-12-30 00:19:19 +00:00
arr = &imageDescriptorSetCache->buckets[hashcode % NUM_DESCRIPTOR_SET_HASH_BUCKETS];
2020-12-19 05:35:21 +00:00
2020-12-27 23:20:59 +00:00
for (i = 0; i < arr->count; i += 1)
{
2020-12-30 00:19:19 +00:00
ImageDescriptorSetHashMap *e = &imageDescriptorSetCache->elements[arr->elements[i]];
2020-12-31 00:47:13 +00:00
if (ImageDescriptorSetDataEqual(
2020-12-30 00:53:10 +00:00
imageDescriptorSetData,
2020-12-27 23:20:59 +00:00
&e->descriptorSetData,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->bindingCount
2020-12-27 23:20:59 +00:00
)) {
e->inactiveFrameCount = 0;
return e->descriptorSet;
}
}
2020-12-19 05:35:21 +00:00
2020-12-27 23:20:59 +00:00
/* If no match exists, assign a new descriptor set and prepare it for update */
/* If no inactive descriptor sets remain, create a new pool and allocate new inactive sets */
2020-12-19 05:35:21 +00:00
2020-12-30 00:19:19 +00:00
if (imageDescriptorSetCache->inactiveDescriptorSetCount == 0)
2020-12-19 05:35:21 +00:00
{
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->imageDescriptorPoolCount += 1;
imageDescriptorSetCache->imageDescriptorPools = SDL_realloc(
imageDescriptorSetCache->imageDescriptorPools,
sizeof(VkDescriptorPool) * imageDescriptorSetCache->imageDescriptorPoolCount
2020-12-27 23:20:59 +00:00
);
2020-12-19 05:35:21 +00:00
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_CreateDescriptorPool(
2020-12-27 23:20:59 +00:00
renderer,
2020-12-31 00:47:13 +00:00
imageDescriptorSetCache->descriptorType,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->nextPoolSize,
imageDescriptorSetCache->nextPoolSize * imageDescriptorSetCache->bindingCount,
&imageDescriptorSetCache->imageDescriptorPools[imageDescriptorSetCache->imageDescriptorPoolCount - 1]
2020-12-27 23:20:59 +00:00
);
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->inactiveDescriptorSetCapacity += imageDescriptorSetCache->nextPoolSize;
2020-12-19 05:35:21 +00:00
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->inactiveDescriptorSets = SDL_realloc(
imageDescriptorSetCache->inactiveDescriptorSets,
sizeof(VkDescriptorSet) * imageDescriptorSetCache->inactiveDescriptorSetCapacity
2020-12-27 23:20:59 +00:00
);
2020-12-30 00:53:10 +00:00
VULKAN_INTERNAL_AllocateDescriptorSets(
2020-12-27 23:20:59 +00:00
renderer,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->imageDescriptorPools[imageDescriptorSetCache->imageDescriptorPoolCount - 1],
imageDescriptorSetCache->descriptorSetLayout,
imageDescriptorSetCache->nextPoolSize,
imageDescriptorSetCache->inactiveDescriptorSets
2020-12-27 23:20:59 +00:00
);
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->inactiveDescriptorSetCount = imageDescriptorSetCache->nextPoolSize;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->nextPoolSize *= 2;
2020-12-27 23:20:59 +00:00
}
2020-12-30 00:19:19 +00:00
newDescriptorSet = imageDescriptorSetCache->inactiveDescriptorSets[imageDescriptorSetCache->inactiveDescriptorSetCount - 1];
imageDescriptorSetCache->inactiveDescriptorSetCount -= 1;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
for (i = 0; i < imageDescriptorSetCache->bindingCount; i += 1)
2020-12-27 23:20:59 +00:00
{
2020-12-19 05:35:21 +00:00
writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
writeDescriptorSets[i].pNext = NULL;
writeDescriptorSets[i].descriptorCount = 1;
2020-12-31 00:47:13 +00:00
writeDescriptorSets[i].descriptorType = imageDescriptorSetCache->descriptorType;
2020-12-27 23:20:59 +00:00
writeDescriptorSets[i].dstArrayElement = 0;
writeDescriptorSets[i].dstBinding = i;
writeDescriptorSets[i].dstSet = newDescriptorSet;
writeDescriptorSets[i].pBufferInfo = NULL;
2020-12-30 00:53:10 +00:00
writeDescriptorSets[i].pImageInfo = &imageDescriptorSetData->descriptorImageInfo[i];
2020-12-31 00:47:13 +00:00
writeDescriptorSets[i].pTexelBufferView = NULL;
2020-12-19 05:35:21 +00:00
}
renderer->vkUpdateDescriptorSets(
renderer->logicalDevice,
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->bindingCount,
2020-12-19 05:35:21 +00:00
writeDescriptorSets,
0,
NULL
);
2020-12-29 00:28:14 +00:00
EXPAND_ELEMENTS_IF_NEEDED(arr, 2, uint32_t)
2020-12-30 00:19:19 +00:00
arr->elements[arr->count] = imageDescriptorSetCache->count;
2020-12-27 23:20:59 +00:00
arr->count += 1;
2020-12-20 09:29:15 +00:00
2020-12-30 00:19:19 +00:00
if (imageDescriptorSetCache->count == imageDescriptorSetCache->capacity)
2020-12-27 23:20:59 +00:00
{
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->capacity *= 2;
2020-12-27 23:20:59 +00:00
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->elements = SDL_realloc(
imageDescriptorSetCache->elements,
sizeof(ImageDescriptorSetHashMap) * imageDescriptorSetCache->capacity
2020-12-27 23:20:59 +00:00
);
}
2020-12-30 00:19:19 +00:00
map = &imageDescriptorSetCache->elements[imageDescriptorSetCache->count];
2020-12-27 23:20:59 +00:00
map->key = hashcode;
2020-12-30 00:19:19 +00:00
for (i = 0; i < imageDescriptorSetCache->bindingCount; i += 1)
2020-12-27 23:20:59 +00:00
{
map->descriptorSetData.descriptorImageInfo[i].imageLayout =
2020-12-30 00:53:10 +00:00
imageDescriptorSetData->descriptorImageInfo[i].imageLayout;
2020-12-27 23:20:59 +00:00
map->descriptorSetData.descriptorImageInfo[i].imageView =
2020-12-30 00:53:10 +00:00
imageDescriptorSetData->descriptorImageInfo[i].imageView;
2020-12-27 23:20:59 +00:00
map->descriptorSetData.descriptorImageInfo[i].sampler =
2020-12-30 00:53:10 +00:00
imageDescriptorSetData->descriptorImageInfo[i].sampler;
2020-12-27 23:20:59 +00:00
}
map->descriptorSet = newDescriptorSet;
map->inactiveFrameCount = 0;
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->count += 1;
2020-12-27 23:20:59 +00:00
return newDescriptorSet;
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:00:06 +00:00
static void VULKAN_BindVertexSamplers(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_Texture **pTextures,
Refresh_Sampler **pSamplers
2020-12-17 01:23:49 +00:00
) {
2020-12-19 05:35:21 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanGraphicsPipeline *graphicsPipeline = vulkanCommandBuffer->currentGraphicsPipeline;
2021-01-02 21:31:17 +00:00
VulkanTexture *currentTexture;
uint32_t i, samplerCount;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetData vertexSamplerDescriptorSetData;
2020-12-19 05:35:21 +00:00
2020-12-27 23:38:58 +00:00
if (graphicsPipeline->pipelineLayout->vertexSamplerDescriptorSetCache == NULL)
2020-12-27 23:20:59 +00:00
{
return;
}
2020-12-19 05:35:21 +00:00
2020-12-30 00:19:19 +00:00
samplerCount = graphicsPipeline->pipelineLayout->vertexSamplerDescriptorSetCache->bindingCount;
2020-12-27 23:38:58 +00:00
2020-12-27 23:20:59 +00:00
for (i = 0; i < samplerCount; i += 1)
2020-12-19 05:35:21 +00:00
{
currentTexture = (VulkanTexture*) pTextures[i];
2020-12-27 23:20:59 +00:00
vertexSamplerDescriptorSetData.descriptorImageInfo[i].imageView = currentTexture->view;
vertexSamplerDescriptorSetData.descriptorImageInfo[i].sampler = (VkSampler) pSamplers[i];
vertexSamplerDescriptorSetData.descriptorImageInfo[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
2020-12-19 05:35:21 +00:00
}
2020-12-30 00:53:10 +00:00
graphicsPipeline->vertexSamplerDescriptorSet = VULKAN_INTERNAL_FetchImageDescriptorSet(
2020-12-27 23:20:59 +00:00
renderer,
graphicsPipeline->pipelineLayout->vertexSamplerDescriptorSetCache,
&vertexSamplerDescriptorSetData
2020-12-19 05:35:21 +00:00
);
2020-12-27 23:20:59 +00:00
}
2020-12-19 05:35:21 +00:00
2021-01-06 01:00:06 +00:00
static void VULKAN_BindFragmentSamplers(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_Texture **pTextures,
Refresh_Sampler **pSamplers
2020-12-27 23:20:59 +00:00
) {
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanGraphicsPipeline *graphicsPipeline = vulkanCommandBuffer->currentGraphicsPipeline;
2021-01-02 21:31:17 +00:00
VulkanTexture *currentTexture;
uint32_t i, samplerCount;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetData fragmentSamplerDescriptorSetData;
2020-12-27 23:20:59 +00:00
2020-12-28 06:45:12 +00:00
if (graphicsPipeline->pipelineLayout->fragmentSamplerDescriptorSetCache == NULL)
2020-12-27 23:20:59 +00:00
{
return;
}
2020-12-30 00:19:19 +00:00
samplerCount = graphicsPipeline->pipelineLayout->fragmentSamplerDescriptorSetCache->bindingCount;
2020-12-27 23:38:58 +00:00
2020-12-27 23:20:59 +00:00
for (i = 0; i < samplerCount; i += 1)
{
currentTexture = (VulkanTexture*) pTextures[i];
fragmentSamplerDescriptorSetData.descriptorImageInfo[i].imageView = currentTexture->view;
fragmentSamplerDescriptorSetData.descriptorImageInfo[i].sampler = (VkSampler) pSamplers[i];
fragmentSamplerDescriptorSetData.descriptorImageInfo[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
2020-12-30 00:53:10 +00:00
graphicsPipeline->fragmentSamplerDescriptorSet = VULKAN_INTERNAL_FetchImageDescriptorSet(
2020-12-27 23:20:59 +00:00
renderer,
graphicsPipeline->pipelineLayout->fragmentSamplerDescriptorSetCache,
&fragmentSamplerDescriptorSetData
);
2020-12-17 01:23:49 +00:00
}
2021-01-03 01:00:52 +00:00
static void VULKAN_GetBufferData(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Buffer *buffer,
2021-01-03 01:00:52 +00:00
void *data,
uint32_t dataLengthInBytes
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer;
uint8_t *dataPtr = (uint8_t*) data;
uint8_t *mapPointer;
2021-01-14 02:02:45 +00:00
mapPointer =
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->mapPointer +
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->offset;
2021-01-03 01:00:52 +00:00
SDL_memcpy(
dataPtr,
mapPointer,
dataLengthInBytes
);
}
2021-01-03 22:37:02 +00:00
static void VULKAN_CopyTextureToBuffer(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_TextureSlice *textureSlice,
Refresh_Buffer *buffer
2020-12-29 07:41:59 +00:00
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
2021-01-03 01:00:52 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2021-01-03 22:37:02 +00:00
VulkanTexture *vulkanTexture = (VulkanTexture*) textureSlice->texture;
2021-01-03 01:00:52 +00:00
VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer;
2021-01-02 21:31:17 +00:00
2020-12-29 07:41:59 +00:00
VulkanResourceAccessType prevResourceAccess;
VkBufferImageCopy imageCopy;
/* Cache this so we can restore it later */
prevResourceAccess = vulkanTexture->resourceAccessType;
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-03 01:00:52 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-29 07:41:59 +00:00
RESOURCE_ACCESS_TRANSFER_READ,
VK_IMAGE_ASPECT_COLOR_BIT,
2021-01-03 22:37:02 +00:00
textureSlice->layer,
1,
textureSlice->level,
1,
2020-12-29 07:41:59 +00:00
0,
vulkanTexture->image,
&vulkanTexture->resourceAccessType
);
2021-01-03 01:00:52 +00:00
/* Save texture data to buffer */
2020-12-29 07:41:59 +00:00
2021-01-03 22:37:02 +00:00
imageCopy.imageExtent.width = textureSlice->rectangle.w;
imageCopy.imageExtent.height = textureSlice->rectangle.h;
2020-12-29 07:41:59 +00:00
imageCopy.imageExtent.depth = 1;
2021-01-03 22:37:02 +00:00
imageCopy.bufferRowLength = textureSlice->rectangle.w;
imageCopy.bufferImageHeight = textureSlice->rectangle.h;
imageCopy.imageOffset.x = textureSlice->rectangle.x;
imageCopy.imageOffset.y = textureSlice->rectangle.y;
imageCopy.imageOffset.z = textureSlice->depth;
2020-12-29 07:41:59 +00:00
imageCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2021-01-03 22:37:02 +00:00
imageCopy.imageSubresource.baseArrayLayer = textureSlice->layer;
2020-12-29 07:41:59 +00:00
imageCopy.imageSubresource.layerCount = 1;
2021-01-03 22:37:02 +00:00
imageCopy.imageSubresource.mipLevel = textureSlice->level;
2020-12-29 07:41:59 +00:00
imageCopy.bufferOffset = 0;
2021-01-02 06:07:15 +00:00
renderer->vkCmdCopyImageToBuffer(
2021-01-03 01:00:52 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-29 07:41:59 +00:00
vulkanTexture->image,
AccessMap[vulkanTexture->resourceAccessType].imageLayout,
2021-01-03 01:00:52 +00:00
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->buffer,
2020-12-29 07:41:59 +00:00
1,
&imageCopy
2021-01-02 06:07:15 +00:00
);
2020-12-29 07:41:59 +00:00
2021-01-03 03:03:25 +00:00
/* Restore the image layout */
2020-12-29 07:41:59 +00:00
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-03 01:00:52 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-29 07:41:59 +00:00
prevResourceAccess,
VK_IMAGE_ASPECT_COLOR_BIT,
2021-01-03 22:37:02 +00:00
textureSlice->layer,
1,
textureSlice->level,
1,
2020-12-29 07:41:59 +00:00
0,
vulkanTexture->image,
&vulkanTexture->resourceAccessType
);
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyTexture(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Texture *texture
2020-12-17 01:23:49 +00:00
) {
2020-12-28 23:11:05 +00:00
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
VulkanTexture* vulkanTexture = (VulkanTexture*)texture;
SDL_LockMutex(renderer->disposeLock);
2020-12-29 00:28:14 +00:00
EXPAND_ARRAY_IF_NEEDED(
renderer->texturesToDestroy,
VulkanTexture*,
2020-12-29 00:42:51 +00:00
renderer->texturesToDestroyCount + 1,
2020-12-29 00:28:14 +00:00
renderer->texturesToDestroyCapacity,
renderer->texturesToDestroyCapacity * 2
)
2020-12-28 23:11:05 +00:00
renderer->texturesToDestroy[renderer->texturesToDestroyCount] = vulkanTexture;
renderer->texturesToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroySampler(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Sampler *sampler
2020-12-17 01:23:49 +00:00
) {
2020-12-29 00:56:49 +00:00
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
VkSampler vulkanSampler = (VkSampler) sampler;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->samplersToDestroy,
VkSampler,
renderer->samplersToDestroyCount + 1,
renderer->samplersToDestroyCapacity,
renderer->samplersToDestroyCapacity * 2
)
renderer->samplersToDestroy[renderer->samplersToDestroyCount] = vulkanSampler;
renderer->samplersToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyBuffer(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Buffer *buffer
2020-12-17 01:23:49 +00:00
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer;
2020-12-17 01:23:49 +00:00
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->buffersToDestroy,
VulkanBuffer*,
renderer->buffersToDestroyCount + 1,
renderer->buffersToDestroyCapacity,
renderer->buffersToDestroyCapacity * 2
)
renderer->buffersToDestroy[
renderer->buffersToDestroyCount
] = vulkanBuffer;
renderer->buffersToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyColorTarget(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_ColorTarget *colorTarget
2020-12-17 01:23:49 +00:00
) {
2020-12-29 00:28:14 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanColorTarget *vulkanColorTarget = (VulkanColorTarget*) colorTarget;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->colorTargetsToDestroy,
VulkanColorTarget*,
2020-12-29 00:42:51 +00:00
renderer->colorTargetsToDestroyCount + 1,
2020-12-29 00:28:14 +00:00
renderer->colorTargetsToDestroyCapacity,
renderer->colorTargetsToDestroyCapacity * 2
)
renderer->colorTargetsToDestroy[renderer->colorTargetsToDestroyCount] = vulkanColorTarget;
renderer->colorTargetsToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyDepthStencilTarget(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_DepthStencilTarget *depthStencilTarget
2020-12-17 01:23:49 +00:00
) {
2020-12-29 04:09:31 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanDepthStencilTarget *vulkanDepthStencilTarget = (VulkanDepthStencilTarget*) depthStencilTarget;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->depthStencilTargetsToDestroy,
VulkanDepthStencilTarget*,
renderer->depthStencilTargetsToDestroyCount + 1,
renderer->depthStencilTargetsToDestroyCapacity,
renderer->depthStencilTargetsToDestroyCapacity * 2
)
renderer->depthStencilTargetsToDestroy[renderer->depthStencilTargetsToDestroyCount] = vulkanDepthStencilTarget;
renderer->depthStencilTargetsToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyFramebuffer(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_Framebuffer *framebuffer
2020-12-17 01:23:49 +00:00
) {
2020-12-29 01:35:18 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanFramebuffer *vulkanFramebuffer = (VulkanFramebuffer*) framebuffer;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->framebuffersToDestroy,
VulkanFramebuffer*,
renderer->framebuffersToDestroyCount + 1,
renderer->framebuffersToDestroyCapacity,
2021-01-05 07:31:56 +00:00
renderer->framebuffersToDestroyCapacity * 2
2020-12-29 01:35:18 +00:00
)
renderer->framebuffersToDestroy[renderer->framebuffersToDestroyCount] = vulkanFramebuffer;
renderer->framebuffersToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyShaderModule(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_ShaderModule *shaderModule
2020-12-17 01:23:49 +00:00
) {
2020-12-29 00:42:51 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VkShaderModule vulkanShaderModule = (VkShaderModule) shaderModule;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->shaderModulesToDestroy,
VkShaderModule,
renderer->shaderModulesToDestroyCount + 1,
renderer->shaderModulesToDestroyCapacity,
renderer->shaderModulesToDestroyCapacity * 2
)
renderer->shaderModulesToDestroy[renderer->shaderModulesToDestroyCount] = vulkanShaderModule;
renderer->shaderModulesToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyRenderPass(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_RenderPass *renderPass
2020-12-17 01:23:49 +00:00
) {
2020-12-29 03:44:34 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VkRenderPass vulkanRenderPass = (VkRenderPass) renderPass;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->renderPassesToDestroy,
VkRenderPass,
renderer->renderPassesToDestroyCount + 1,
renderer->renderPassesToDestroyCapacity,
renderer->renderPassesToDestroyCapacity * 2
)
renderer->renderPassesToDestroy[renderer->renderPassesToDestroyCount] = vulkanRenderPass;
renderer->renderPassesToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyComputePipeline(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_ComputePipeline *computePipeline
2020-12-29 22:52:24 +00:00
) {
2020-12-31 07:02:12 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanComputePipeline *vulkanComputePipeline = (VulkanComputePipeline*) computePipeline;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->computePipelinesToDestroy,
VulkanComputePipeline*,
renderer->computePipelinesToDestroyCount + 1,
renderer->computePipelinesToDestroyCapacity,
renderer->computePipelinesToDestroyCapacity * 2
)
renderer->computePipelinesToDestroy[renderer->computePipelinesToDestroyCount] = vulkanComputePipeline;
renderer->computePipelinesToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-29 22:52:24 +00:00
}
2021-01-06 01:02:36 +00:00
static void VULKAN_QueueDestroyGraphicsPipeline(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_GraphicsPipeline *graphicsPipeline
2020-12-17 01:23:49 +00:00
) {
2020-12-29 00:28:14 +00:00
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanGraphicsPipeline *vulkanGraphicsPipeline = (VulkanGraphicsPipeline*) graphicsPipeline;
SDL_LockMutex(renderer->disposeLock);
EXPAND_ARRAY_IF_NEEDED(
renderer->graphicsPipelinesToDestroy,
VulkanGraphicsPipeline*,
2020-12-29 00:42:51 +00:00
renderer->graphicsPipelinesToDestroyCount + 1,
2020-12-29 00:28:14 +00:00
renderer->graphicsPipelinesToDestroyCapacity,
renderer->graphicsPipelinesToDestroyCapacity * 2
)
renderer->graphicsPipelinesToDestroy[renderer->graphicsPipelinesToDestroyCount] = vulkanGraphicsPipeline;
renderer->graphicsPipelinesToDestroyCount += 1;
SDL_UnlockMutex(renderer->disposeLock);
2020-12-17 01:23:49 +00:00
}
static void VULKAN_BeginRenderPass(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_RenderPass *renderPass,
Refresh_Framebuffer *framebuffer,
Refresh_Rect renderArea,
Refresh_Color *pColorClearValues,
2020-12-20 08:05:12 +00:00
uint32_t colorClearCount,
2021-01-05 23:00:51 +00:00
Refresh_DepthStencilValue *depthStencilClearValue
2020-12-17 01:23:49 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2020-12-23 07:17:09 +00:00
VulkanFramebuffer *vulkanFramebuffer = (VulkanFramebuffer*) framebuffer;
2021-01-02 21:31:17 +00:00
2020-12-20 08:05:12 +00:00
VkClearValue *clearValues;
uint32_t i;
2020-12-23 07:17:09 +00:00
uint32_t clearCount = colorClearCount;
VkImageAspectFlags depthAspectFlags;
/* Layout transitions */
for (i = 0; i < vulkanFramebuffer->colorTargetCount; i += 1)
{
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-23 07:17:09 +00:00
RESOURCE_ACCESS_COLOR_ATTACHMENT_READ_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
vulkanFramebuffer->colorTargets[i]->layer,
1,
0,
1,
0,
vulkanFramebuffer->colorTargets[i]->texture->image,
&vulkanFramebuffer->colorTargets[i]->texture->resourceAccessType
);
}
2020-12-20 08:05:12 +00:00
if (depthStencilClearValue != NULL)
{
2020-12-23 07:17:09 +00:00
depthAspectFlags = VK_IMAGE_ASPECT_DEPTH_BIT;
if (DepthFormatContainsStencil(
vulkanFramebuffer->depthStencilTarget->texture->format
)) {
depthAspectFlags |= VK_IMAGE_ASPECT_STENCIL_BIT;
}
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-23 07:17:09 +00:00
RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_WRITE,
depthAspectFlags,
0,
1,
0,
1,
0,
vulkanFramebuffer->depthStencilTarget->texture->image,
&vulkanFramebuffer->depthStencilTarget->texture->resourceAccessType
);
clearCount += 1;
2020-12-20 08:05:12 +00:00
}
2020-12-23 07:17:09 +00:00
/* Set clear values */
clearValues = SDL_stack_alloc(VkClearValue, clearCount);
2020-12-20 08:05:12 +00:00
for (i = 0; i < colorClearCount; i += 1)
{
2020-12-23 05:53:39 +00:00
clearValues[i].color.float32[0] = pColorClearValues[i].r / 255.0f;
clearValues[i].color.float32[1] = pColorClearValues[i].g / 255.0f;
clearValues[i].color.float32[2] = pColorClearValues[i].b / 255.0f;
clearValues[i].color.float32[3] = pColorClearValues[i].a / 255.0f;
2020-12-20 08:05:12 +00:00
}
if (depthStencilClearValue != NULL)
{
clearValues[colorClearCount].depthStencil.depth =
depthStencilClearValue->depth;
clearValues[colorClearCount].depthStencil.stencil =
depthStencilClearValue->stencil;
}
VkRenderPassBeginInfo renderPassBeginInfo;
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.pNext = NULL;
renderPassBeginInfo.renderPass = (VkRenderPass) renderPass;
2020-12-23 07:17:09 +00:00
renderPassBeginInfo.framebuffer = vulkanFramebuffer->framebuffer;
2020-12-20 08:05:12 +00:00
renderPassBeginInfo.renderArea.extent.width = renderArea.w;
renderPassBeginInfo.renderArea.extent.height = renderArea.h;
renderPassBeginInfo.renderArea.offset.x = renderArea.x;
renderPassBeginInfo.renderArea.offset.y = renderArea.y;
renderPassBeginInfo.pClearValues = clearValues;
2020-12-23 07:17:09 +00:00
renderPassBeginInfo.clearValueCount = clearCount;
2020-12-20 08:05:12 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdBeginRenderPass(
vulkanCommandBuffer->commandBuffer,
2020-12-20 08:05:12 +00:00
&renderPassBeginInfo,
VK_SUBPASS_CONTENTS_INLINE
2021-01-02 06:07:15 +00:00
);
2020-12-20 08:05:12 +00:00
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentFramebuffer = vulkanFramebuffer;
2020-12-20 08:05:12 +00:00
SDL_stack_free(clearValues);
2020-12-17 01:23:49 +00:00
}
static void VULKAN_EndRenderPass(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer
2020-12-17 01:23:49 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanTexture *currentTexture;
2021-01-02 21:31:17 +00:00
uint32_t i;
2020-12-20 08:05:53 +00:00
2021-01-02 06:07:15 +00:00
renderer->vkCmdEndRenderPass(
vulkanCommandBuffer->commandBuffer
);
2020-12-23 07:17:09 +00:00
2021-01-02 06:07:15 +00:00
for (i = 0; i < vulkanCommandBuffer->currentFramebuffer->colorTargetCount; i += 1)
{
2021-01-02 06:07:15 +00:00
currentTexture = vulkanCommandBuffer->currentFramebuffer->colorTargets[i]->texture;
if (currentTexture->usageFlags & REFRESH_TEXTUREUSAGE_SAMPLER_BIT)
{
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
RESOURCE_ACCESS_ANY_SHADER_READ_SAMPLED_IMAGE,
VK_IMAGE_ASPECT_COLOR_BIT,
0,
currentTexture->layerCount,
0,
currentTexture->levelCount,
0,
currentTexture->image,
&currentTexture->resourceAccessType
);
}
}
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline = NULL;
vulkanCommandBuffer->currentFramebuffer = NULL;
2020-12-17 01:23:49 +00:00
}
static void VULKAN_BindGraphicsPipeline(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_GraphicsPipeline *graphicsPipeline
2020-12-17 01:23:49 +00:00
) {
2020-12-20 07:17:55 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2020-12-20 07:17:55 +00:00
VulkanGraphicsPipeline* pipeline = (VulkanGraphicsPipeline*) graphicsPipeline;
2020-12-31 04:39:47 +00:00
/* bind dummy sets */
2020-12-27 23:20:59 +00:00
if (pipeline->pipelineLayout->vertexSamplerDescriptorSetCache == NULL)
{
pipeline->vertexSamplerDescriptorSet = renderer->emptyVertexSamplerDescriptorSet;
}
2020-12-27 23:20:59 +00:00
if (pipeline->pipelineLayout->fragmentSamplerDescriptorSetCache == NULL)
{
pipeline->fragmentSamplerDescriptorSet = renderer->emptyFragmentSamplerDescriptorSet;
}
2021-01-02 06:07:15 +00:00
renderer->vkCmdBindPipeline(
vulkanCommandBuffer->commandBuffer,
2020-12-20 07:17:55 +00:00
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline->pipeline
2021-01-02 06:07:15 +00:00
);
2020-12-23 06:56:26 +00:00
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentGraphicsPipeline = pipeline;
2020-12-17 01:23:49 +00:00
}
2020-12-21 23:44:43 +00:00
static void VULKAN_INTERNAL_MarkAsBound(
VulkanRenderer* renderer,
VulkanBuffer* buf
) {
2020-12-23 06:05:01 +00:00
VulkanSubBuffer *subbuf = buf->subBuffers[buf->currentSubBufferIndex];
subbuf->bound = renderer->frameIndex;
2020-12-21 23:44:43 +00:00
/* Don't rebind a bound buffer */
if (buf->bound) return;
buf->bound = 1;
2021-01-02 06:07:15 +00:00
SDL_LockMutex(renderer->boundBufferLock);
2020-12-21 23:44:43 +00:00
if (renderer->buffersInUseCount == renderer->buffersInUseCapacity)
{
renderer->buffersInUseCapacity *= 2;
renderer->buffersInUse = SDL_realloc(
renderer->buffersInUse,
sizeof(VulkanBuffer*) * renderer->buffersInUseCapacity
);
}
renderer->buffersInUse[renderer->buffersInUseCount] = buf;
renderer->buffersInUseCount += 1;
2021-01-02 06:07:15 +00:00
SDL_UnlockMutex(renderer->boundBufferLock);
2020-12-21 23:44:43 +00:00
}
2020-12-20 07:31:55 +00:00
static void VULKAN_BindVertexBuffers(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
2020-12-20 07:31:55 +00:00
uint32_t firstBinding,
uint32_t bindingCount,
2021-01-05 23:00:51 +00:00
Refresh_Buffer **pBuffers,
2020-12-20 07:31:55 +00:00
uint64_t *pOffsets
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2021-01-02 21:31:17 +00:00
VkBuffer *buffers = SDL_stack_alloc(VkBuffer, bindingCount);
2020-12-20 07:31:55 +00:00
VulkanBuffer* currentBuffer;
2020-12-21 20:37:54 +00:00
uint32_t i;
2020-12-20 07:31:55 +00:00
2020-12-21 20:37:54 +00:00
for (i = 0; i < bindingCount; i += 1)
2020-12-20 07:31:55 +00:00
{
currentBuffer = (VulkanBuffer*) pBuffers[i];
2020-12-23 07:46:37 +00:00
buffers[i] = currentBuffer->subBuffers[currentBuffer->currentSubBufferIndex]->buffer;
2020-12-21 23:44:43 +00:00
VULKAN_INTERNAL_MarkAsBound(renderer, currentBuffer);
2020-12-20 07:31:55 +00:00
}
2021-01-02 06:07:15 +00:00
renderer->vkCmdBindVertexBuffers(
vulkanCommandBuffer->commandBuffer,
2020-12-20 07:31:55 +00:00
firstBinding,
bindingCount,
buffers,
pOffsets
2021-01-02 06:07:15 +00:00
);
2020-12-20 07:31:55 +00:00
SDL_stack_free(buffers);
}
static void VULKAN_BindIndexBuffer(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_Buffer *buffer,
2020-12-20 07:31:55 +00:00
uint64_t offset,
2021-01-05 23:00:51 +00:00
Refresh_IndexElementSize indexElementSize
2020-12-20 07:31:55 +00:00
) {
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2020-12-20 07:31:55 +00:00
VulkanBuffer* vulkanBuffer = (VulkanBuffer*) buffer;
2020-12-21 23:44:43 +00:00
VULKAN_INTERNAL_MarkAsBound(renderer, vulkanBuffer);
2021-01-02 06:07:15 +00:00
renderer->vkCmdBindIndexBuffer(
vulkanCommandBuffer->commandBuffer,
2020-12-21 23:44:43 +00:00
vulkanBuffer->subBuffers[renderer->frameIndex]->buffer,
2020-12-20 07:31:55 +00:00
offset,
RefreshToVK_IndexType[indexElementSize]
2021-01-02 06:07:15 +00:00
);
2020-12-20 07:31:55 +00:00
}
2020-12-30 01:31:39 +00:00
static void VULKAN_BindComputePipeline(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_ComputePipeline *computePipeline
2020-12-30 01:31:39 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
2020-12-31 00:47:13 +00:00
VulkanComputePipeline *vulkanComputePipeline = (VulkanComputePipeline*) computePipeline;
2020-12-31 04:39:47 +00:00
/* bind dummy sets */
if (vulkanComputePipeline->pipelineLayout->bufferDescriptorSetCache == NULL)
{
vulkanComputePipeline->bufferDescriptorSet = renderer->emptyComputeBufferDescriptorSet;
}
if (vulkanComputePipeline->pipelineLayout->imageDescriptorSetCache == NULL)
{
vulkanComputePipeline->imageDescriptorSet = renderer->emptyComputeImageDescriptorSet;
}
2020-12-31 00:47:13 +00:00
renderer->vkCmdBindPipeline(
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->commandBuffer,
2020-12-31 00:47:13 +00:00
VK_PIPELINE_BIND_POINT_COMPUTE,
vulkanComputePipeline->pipeline
);
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->currentComputePipeline = vulkanComputePipeline;
2020-12-30 01:31:39 +00:00
}
static void VULKAN_BindComputeBuffers(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_Buffer **pBuffers
2020-12-30 01:31:39 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanComputePipeline *computePipeline = vulkanCommandBuffer->currentComputePipeline;
2021-01-02 21:31:17 +00:00
2020-12-31 00:47:13 +00:00
VulkanBuffer *currentBuffer;
BufferDescriptorSetData bufferDescriptorSetData;
uint32_t i;
if (computePipeline->pipelineLayout->bufferDescriptorSetCache == NULL)
{
return;
}
for (i = 0; i < computePipeline->pipelineLayout->bufferDescriptorSetCache->bindingCount; i += 1)
{
currentBuffer = (VulkanBuffer*) pBuffers[i];
2020-12-31 06:28:37 +00:00
2020-12-31 00:47:13 +00:00
bufferDescriptorSetData.descriptorBufferInfo[i].buffer = currentBuffer->subBuffers[currentBuffer->currentSubBufferIndex]->buffer;
bufferDescriptorSetData.descriptorBufferInfo[i].offset = 0;
bufferDescriptorSetData.descriptorBufferInfo[i].range = currentBuffer->subBuffers[currentBuffer->currentSubBufferIndex]->size;
2020-12-31 06:28:37 +00:00
VULKAN_INTERNAL_MarkAsBound(renderer, currentBuffer);
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->boundComputeBuffers[i] = currentBuffer;
2020-12-31 00:47:13 +00:00
}
2021-01-02 06:07:15 +00:00
vulkanCommandBuffer->boundComputeBufferCount = computePipeline->pipelineLayout->bufferDescriptorSetCache->bindingCount;
2020-12-31 06:28:37 +00:00
2020-12-31 00:47:13 +00:00
computePipeline->bufferDescriptorSet =
VULKAN_INTERNAL_FetchBufferDescriptorSet(
renderer,
computePipeline->pipelineLayout->bufferDescriptorSetCache,
&bufferDescriptorSetData
);
2020-12-30 01:31:39 +00:00
}
static void VULKAN_BindComputeTextures(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_Texture **pTextures
2020-12-30 01:31:39 +00:00
) {
2021-01-02 21:31:17 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanComputePipeline *computePipeline = vulkanCommandBuffer->currentComputePipeline;
2021-01-02 21:31:17 +00:00
2020-12-31 00:47:13 +00:00
VulkanTexture *currentTexture;
ImageDescriptorSetData imageDescriptorSetData;
uint32_t i;
if (computePipeline->pipelineLayout->imageDescriptorSetCache == NULL)
{
return;
}
for (i = 0; i < computePipeline->pipelineLayout->imageDescriptorSetCache->bindingCount; i += 1)
{
currentTexture = (VulkanTexture*) pTextures[i];
imageDescriptorSetData.descriptorImageInfo[i].imageView = currentTexture->view;
imageDescriptorSetData.descriptorImageInfo[i].sampler = VK_NULL_HANDLE;
imageDescriptorSetData.descriptorImageInfo[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
computePipeline->imageDescriptorSet =
VULKAN_INTERNAL_FetchImageDescriptorSet(
renderer,
computePipeline->pipelineLayout->imageDescriptorSetCache,
&imageDescriptorSetData
);
2020-12-30 01:31:39 +00:00
}
2021-01-03 02:02:20 +00:00
static void VULKAN_INTERNAL_AllocateCommandBuffers(
2021-01-02 06:07:15 +00:00
VulkanRenderer *renderer,
2021-01-03 02:02:20 +00:00
VulkanCommandPool *vulkanCommandPool,
uint32_t allocateCount
2021-01-02 06:07:15 +00:00
) {
VkCommandBufferAllocateInfo allocateInfo;
VkResult vulkanResult;
2021-01-03 02:02:20 +00:00
uint32_t i;
VkCommandBuffer *commandBuffers = SDL_stack_alloc(VkCommandBuffer, allocateCount);
VulkanCommandBuffer *currentVulkanCommandBuffer;
2021-01-02 06:07:15 +00:00
2021-01-03 02:02:20 +00:00
vulkanCommandPool->inactiveCommandBufferCapacity += allocateCount;
vulkanCommandPool->inactiveCommandBuffers = SDL_realloc(
vulkanCommandPool->inactiveCommandBuffers,
sizeof(VulkanCommandBuffer*) *
vulkanCommandPool->inactiveCommandBufferCapacity
);
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.pNext = NULL;
allocateInfo.commandPool = vulkanCommandPool->commandPool;
allocateInfo.commandBufferCount = allocateCount;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
vulkanResult = renderer->vkAllocateCommandBuffers(
renderer->logicalDevice,
&allocateInfo,
commandBuffers
);
if (vulkanResult != VK_SUCCESS)
2021-01-02 06:07:15 +00:00
{
2021-01-03 02:02:20 +00:00
LogVulkanResult("vkAllocateCommandBuffers", vulkanResult);
SDL_stack_free(commandBuffers);
return;
}
2021-01-02 06:07:15 +00:00
2021-01-03 02:02:20 +00:00
for (i = 0; i < allocateCount; i += 1)
{
currentVulkanCommandBuffer = SDL_malloc(sizeof(VulkanCommandBuffer));
2021-01-03 03:03:25 +00:00
currentVulkanCommandBuffer->commandPool = vulkanCommandPool;
2021-01-03 02:02:20 +00:00
currentVulkanCommandBuffer->commandBuffer = commandBuffers[i];
vulkanCommandPool->inactiveCommandBuffers[
vulkanCommandPool->inactiveCommandBufferCount
] = currentVulkanCommandBuffer;
vulkanCommandPool->inactiveCommandBufferCount += 1;
}
2021-01-02 06:07:15 +00:00
2021-01-03 02:02:20 +00:00
SDL_stack_free(commandBuffers);
}
2021-01-02 06:07:15 +00:00
2021-01-03 02:02:20 +00:00
static VulkanCommandPool* VULKAN_INTERNAL_FetchCommandPool(
VulkanRenderer *renderer,
SDL_threadID threadID
) {
VulkanCommandPool *vulkanCommandPool;
VkCommandPoolCreateInfo commandPoolCreateInfo;
VkResult vulkanResult;
CommandPoolHash commandPoolHash;
2021-01-02 06:07:15 +00:00
2021-01-03 02:02:20 +00:00
commandPoolHash.threadID = threadID;
vulkanCommandPool = CommandPoolHashTable_Fetch(
&renderer->commandPoolHashTable,
commandPoolHash
);
if (vulkanCommandPool != NULL)
{
return vulkanCommandPool;
}
2021-01-03 03:03:25 +00:00
vulkanCommandPool = (VulkanCommandPool*) SDL_malloc(sizeof(VulkanCommandPool));
2021-01-03 02:02:20 +00:00
commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
commandPoolCreateInfo.pNext = NULL;
commandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
commandPoolCreateInfo.queueFamilyIndex = renderer->queueFamilyIndices.graphicsFamily;
vulkanResult = renderer->vkCreateCommandPool(
renderer->logicalDevice,
&commandPoolCreateInfo,
NULL,
&vulkanCommandPool->commandPool
);
2021-01-05 07:31:56 +00:00
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create command pool!");
2021-01-05 07:31:56 +00:00
LogVulkanResult("vkCreateCommandPool", vulkanResult);
return NULL;
}
2021-01-03 02:02:20 +00:00
vulkanCommandPool->threadID = threadID;
vulkanCommandPool->inactiveCommandBufferCapacity = 0;
vulkanCommandPool->inactiveCommandBufferCount = 0;
vulkanCommandPool->inactiveCommandBuffers = NULL;
VULKAN_INTERNAL_AllocateCommandBuffers(
renderer,
vulkanCommandPool,
2
);
CommandPoolHashTable_Insert(
&renderer->commandPoolHashTable,
commandPoolHash,
vulkanCommandPool
);
return vulkanCommandPool;
}
static VulkanCommandBuffer* VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(
VulkanRenderer *renderer,
SDL_threadID threadID
) {
VulkanCommandPool *commandPool =
VULKAN_INTERNAL_FetchCommandPool(renderer, threadID);
VulkanCommandBuffer *commandBuffer;
if (commandPool->inactiveCommandBufferCount == 0)
{
VULKAN_INTERNAL_AllocateCommandBuffers(
renderer,
commandPool,
commandPool->inactiveCommandBufferCapacity
);
2021-01-02 06:07:15 +00:00
}
2021-01-03 03:03:25 +00:00
commandBuffer = commandPool->inactiveCommandBuffers[commandPool->inactiveCommandBufferCount - 1];
2021-01-02 06:07:15 +00:00
commandPool->inactiveCommandBufferCount -= 1;
return commandBuffer;
}
2021-01-05 23:00:51 +00:00
static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer(
Refresh_Renderer *driverData,
2021-01-02 06:07:15 +00:00
uint8_t fixed
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
uint32_t i;
SDL_threadID threadID = SDL_ThreadID();
VulkanCommandBuffer *commandBuffer =
VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(renderer, threadID);
/* State tracking */
commandBuffer->currentComputePipeline = NULL;
commandBuffer->currentGraphicsPipeline = NULL;
commandBuffer->currentFramebuffer = NULL;
/* init bound compute buffer array */
for (i = 0; i < MAX_BUFFER_BINDINGS; i += 1)
{
commandBuffer->boundComputeBuffers[i] = NULL;
}
commandBuffer->boundComputeBufferCount = 0;
commandBuffer->fixed = fixed;
commandBuffer->submitted = 0;
VULKAN_INTERNAL_BeginCommandBuffer(renderer, commandBuffer);
2021-01-05 23:00:51 +00:00
return (Refresh_CommandBuffer*) commandBuffer;
2021-01-02 06:07:15 +00:00
}
static void VULKAN_QueuePresent(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
Refresh_CommandBuffer *commandBuffer,
Refresh_TextureSlice *textureSlice,
Refresh_Rect *destinationRectangle,
Refresh_Filter filter
2020-12-17 04:19:11 +00:00
) {
2020-12-21 23:44:43 +00:00
VkResult acquireResult;
uint32_t swapChainImageIndex;
2020-12-17 04:19:11 +00:00
2021-01-05 23:00:51 +00:00
Refresh_Rect dstRect;
2020-12-17 03:50:31 +00:00
VulkanRenderer* renderer = (VulkanRenderer*) driverData;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
VulkanTexture* vulkanTexture = (VulkanTexture*) textureSlice->texture;
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
if (renderer->headless)
2020-12-17 03:50:31 +00:00
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Cannot call QueuePresent in headless mode!");
2020-12-21 23:44:43 +00:00
return;
2020-12-17 03:50:31 +00:00
}
2020-12-21 23:44:43 +00:00
acquireResult = renderer->vkAcquireNextImageKHR(
renderer->logicalDevice,
renderer->swapChain,
UINT64_MAX,
renderer->imageAvailableSemaphore,
VK_NULL_HANDLE,
&swapChainImageIndex
);
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
if (acquireResult != VK_SUCCESS && acquireResult != VK_SUBOPTIMAL_KHR)
{
/* Failed to acquire swapchain image, mark that we need a new one */
renderer->needNewSwapChain = 1;
return;
2020-12-17 03:50:31 +00:00
}
2020-12-21 23:44:43 +00:00
renderer->shouldPresent = 1;
renderer->swapChainImageAcquired = 1;
renderer->currentSwapChainIndex = swapChainImageIndex;
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
if (destinationRectangle != NULL)
2020-12-17 03:50:31 +00:00
{
2020-12-21 23:44:43 +00:00
dstRect = *destinationRectangle;
}
else
{
dstRect.x = 0;
dstRect.y = 0;
dstRect.w = renderer->swapChainExtent.width;
dstRect.h = renderer->swapChainExtent.height;
}
2020-12-17 03:50:31 +00:00
2021-01-03 21:01:29 +00:00
/* Blit! */
2020-12-17 03:50:31 +00:00
2021-01-03 21:01:29 +00:00
VULKAN_INTERNAL_BlitImage(
2020-12-21 23:44:43 +00:00
renderer,
2021-01-02 21:31:17 +00:00
vulkanCommandBuffer->commandBuffer,
2021-01-03 22:37:02 +00:00
&textureSlice->rectangle,
textureSlice->depth,
2021-01-03 21:01:29 +00:00
textureSlice->layer,
textureSlice->level,
2020-12-21 23:44:43 +00:00
vulkanTexture->image,
2021-01-03 21:01:29 +00:00
&vulkanTexture->resourceAccessType,
vulkanTexture->resourceAccessType,
&dstRect,
2020-12-21 23:44:43 +00:00
0,
0,
2021-01-03 22:37:02 +00:00
0,
2020-12-21 23:44:43 +00:00
renderer->swapChainImages[swapChainImageIndex],
2021-01-03 21:01:29 +00:00
&renderer->swapChainResourceAccessTypes[swapChainImageIndex],
2020-12-21 23:44:43 +00:00
RESOURCE_ACCESS_PRESENT,
2021-01-03 21:01:29 +00:00
RefreshToVK_Filter[filter]
2020-12-21 23:44:43 +00:00
);
2020-12-17 03:50:31 +00:00
}
2020-12-31 07:02:12 +00:00
static void VULKAN_INTERNAL_DeactivateUnusedBufferDescriptorSets(
BufferDescriptorSetCache *bufferDescriptorSetCache
) {
int32_t i, j;
BufferDescriptorSetHashArray *arr;
for (i = bufferDescriptorSetCache->count - 1; i >= 0; i -= 1)
{
bufferDescriptorSetCache->elements[i].inactiveFrameCount += 1;
if (bufferDescriptorSetCache->elements[i].inactiveFrameCount + 1 > DESCRIPTOR_SET_DEACTIVATE_FRAMES)
{
arr = &bufferDescriptorSetCache->buckets[bufferDescriptorSetCache->elements[i].key % NUM_DESCRIPTOR_SET_HASH_BUCKETS];
/* remove index from bucket */
for (j = 0; j < arr->count; j += 1)
{
if (arr->elements[j] == i)
{
if (j < arr->count - 1)
{
arr->elements[j] = arr->elements[arr->count - 1];
}
arr->count -= 1;
break;
}
}
/* remove element from table and place in inactive sets */
bufferDescriptorSetCache->inactiveDescriptorSets[bufferDescriptorSetCache->inactiveDescriptorSetCount] = bufferDescriptorSetCache->elements[i].descriptorSet;
bufferDescriptorSetCache->inactiveDescriptorSetCount += 1;
/* move another descriptor set to fill the hole */
if (i < bufferDescriptorSetCache->count - 1)
{
bufferDescriptorSetCache->elements[i] = bufferDescriptorSetCache->elements[bufferDescriptorSetCache->count - 1];
/* update index in bucket */
arr = &bufferDescriptorSetCache->buckets[bufferDescriptorSetCache->elements[i].key % NUM_DESCRIPTOR_SET_HASH_BUCKETS];
for (j = 0; j < arr->count; j += 1)
{
if (arr->elements[j] == bufferDescriptorSetCache->count - 1)
{
arr->elements[j] = i;
break;
}
}
}
bufferDescriptorSetCache->count -= 1;
}
}
}
static void VULKAN_INTERNAL_DeactivateUnusedImageDescriptorSets(
2020-12-30 00:19:19 +00:00
ImageDescriptorSetCache *imageDescriptorSetCache
2020-12-27 23:34:15 +00:00
) {
int32_t i, j;
2020-12-30 00:19:19 +00:00
ImageDescriptorSetHashArray *arr;
2020-12-27 23:34:15 +00:00
2020-12-30 00:19:19 +00:00
for (i = imageDescriptorSetCache->count - 1; i >= 0; i -= 1)
2020-12-27 23:34:15 +00:00
{
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->elements[i].inactiveFrameCount += 1;
2020-12-27 23:34:15 +00:00
2020-12-30 00:19:19 +00:00
if (imageDescriptorSetCache->elements[i].inactiveFrameCount + 1 > DESCRIPTOR_SET_DEACTIVATE_FRAMES)
2020-12-27 23:34:15 +00:00
{
2020-12-30 00:19:19 +00:00
arr = &imageDescriptorSetCache->buckets[imageDescriptorSetCache->elements[i].key % NUM_DESCRIPTOR_SET_HASH_BUCKETS];
2020-12-27 23:34:15 +00:00
/* remove index from bucket */
for (j = 0; j < arr->count; j += 1)
{
if (arr->elements[j] == i)
{
if (j < arr->count - 1)
{
arr->elements[j] = arr->elements[arr->count - 1];
}
arr->count -= 1;
break;
}
}
/* remove element from table and place in inactive sets */
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->inactiveDescriptorSets[imageDescriptorSetCache->inactiveDescriptorSetCount] = imageDescriptorSetCache->elements[i].descriptorSet;
imageDescriptorSetCache->inactiveDescriptorSetCount += 1;
2020-12-27 23:34:15 +00:00
/* move another descriptor set to fill the hole */
2020-12-30 00:19:19 +00:00
if (i < imageDescriptorSetCache->count - 1)
2020-12-27 23:34:15 +00:00
{
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->elements[i] = imageDescriptorSetCache->elements[imageDescriptorSetCache->count - 1];
2020-12-27 23:34:15 +00:00
/* update index in bucket */
2020-12-30 00:19:19 +00:00
arr = &imageDescriptorSetCache->buckets[imageDescriptorSetCache->elements[i].key % NUM_DESCRIPTOR_SET_HASH_BUCKETS];
2020-12-27 23:34:15 +00:00
for (j = 0; j < arr->count; j += 1)
{
2020-12-30 00:19:19 +00:00
if (arr->elements[j] == imageDescriptorSetCache->count - 1)
2020-12-27 23:34:15 +00:00
{
arr->elements[j] = i;
break;
}
}
}
2020-12-30 00:19:19 +00:00
imageDescriptorSetCache->count -= 1;
2020-12-27 23:34:15 +00:00
}
}
}
static void VULKAN_INTERNAL_ResetDescriptorSetData(VulkanRenderer *renderer)
{
uint32_t i, j;
2020-12-31 07:02:12 +00:00
VulkanGraphicsPipelineLayout *graphicsPipelineLayout;
VulkanComputePipelineLayout *computePipelineLayout;
2020-12-27 23:34:15 +00:00
for (i = 0; i < NUM_PIPELINE_LAYOUT_BUCKETS; i += 1)
{
2020-12-29 22:52:24 +00:00
for (j = 0; j < renderer->graphicsPipelineLayoutHashTable.buckets[i].count; j += 1)
2020-12-27 23:34:15 +00:00
{
2020-12-31 07:02:12 +00:00
graphicsPipelineLayout = renderer->graphicsPipelineLayoutHashTable.buckets[i].elements[j].value;
if (graphicsPipelineLayout->vertexSamplerDescriptorSetCache != NULL)
{
VULKAN_INTERNAL_DeactivateUnusedImageDescriptorSets(
graphicsPipelineLayout->vertexSamplerDescriptorSetCache
);
}
if (graphicsPipelineLayout->fragmentSamplerDescriptorSetCache != NULL)
{
VULKAN_INTERNAL_DeactivateUnusedImageDescriptorSets(
graphicsPipelineLayout->fragmentSamplerDescriptorSetCache
);
}
}
for (j = 0; j < renderer->computePipelineLayoutHashTable.buckets[i].count; j += 1)
{
computePipelineLayout = renderer->computePipelineLayoutHashTable.buckets[i].elements[j].value;
2020-12-27 23:34:15 +00:00
2020-12-31 07:02:12 +00:00
if (computePipelineLayout->bufferDescriptorSetCache != NULL)
2020-12-27 23:34:15 +00:00
{
2020-12-31 07:02:12 +00:00
VULKAN_INTERNAL_DeactivateUnusedBufferDescriptorSets(
computePipelineLayout->bufferDescriptorSetCache
2020-12-27 23:34:15 +00:00
);
}
2020-12-31 07:02:12 +00:00
if (computePipelineLayout->imageDescriptorSetCache != NULL)
2020-12-27 23:34:15 +00:00
{
2020-12-31 07:02:12 +00:00
VULKAN_INTERNAL_DeactivateUnusedImageDescriptorSets(
computePipelineLayout->imageDescriptorSetCache
2020-12-27 23:34:15 +00:00
);
}
}
}
}
2021-01-02 06:07:15 +00:00
static void VULKAN_INTERNAL_ResetCommandBuffer(
VulkanRenderer *renderer,
VulkanCommandBuffer *commandBuffer
) {
VkResult vulkanResult;
VulkanCommandPool *commandPool = commandBuffer->commandPool;
vulkanResult = renderer->vkResetCommandBuffer(
2021-01-02 21:31:17 +00:00
commandBuffer->commandBuffer,
2021-01-02 06:07:15 +00:00
VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkResetCommandBuffer", vulkanResult);
}
commandBuffer->submitted = 0;
commandPool->inactiveCommandBuffers[
commandPool->inactiveCommandBufferCount
] = commandBuffer;
commandPool->inactiveCommandBufferCount += 1;
}
2020-12-21 23:44:43 +00:00
static void VULKAN_Submit(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData,
2021-01-03 22:57:46 +00:00
uint32_t commandBufferCount,
2021-01-05 23:00:51 +00:00
Refresh_CommandBuffer **pCommandBuffers
2020-12-17 03:50:31 +00:00
) {
2020-12-21 23:44:43 +00:00
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
2021-01-02 21:31:17 +00:00
VkSubmitInfo transferSubmitInfo, submitInfo;
2020-12-21 23:44:43 +00:00
VkResult vulkanResult, presentResult = VK_SUCCESS;
2021-01-02 06:07:15 +00:00
VulkanCommandBuffer *currentCommandBuffer;
VkCommandBuffer *commandBuffers;
2020-12-29 00:28:14 +00:00
uint32_t i;
2020-12-21 23:44:43 +00:00
uint8_t present;
2021-01-03 22:37:02 +00:00
VkPipelineStageFlags waitStages[2];
VkSemaphore waitSemaphores[2];
uint32_t waitSemaphoreCount = 0;
2020-12-21 23:44:43 +00:00
VkPresentInfoKHR presentInfo;
2021-01-02 21:31:17 +00:00
if (renderer->pendingTransfer)
{
2021-01-03 03:03:25 +00:00
VULKAN_INTERNAL_EndTransferCommandBuffer(renderer);
2021-01-02 21:31:17 +00:00
transferSubmitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
transferSubmitInfo.pNext = NULL;
transferSubmitInfo.commandBufferCount = 1;
transferSubmitInfo.pCommandBuffers = &renderer->transferCommandBuffers[renderer->frameIndex];
transferSubmitInfo.pWaitDstStageMask = NULL;
transferSubmitInfo.pWaitSemaphores = NULL;
transferSubmitInfo.waitSemaphoreCount = 0;
transferSubmitInfo.pSignalSemaphores = &renderer->transferFinishedSemaphore;
transferSubmitInfo.signalSemaphoreCount = 1;
waitSemaphores[waitSemaphoreCount] = renderer->transferFinishedSemaphore;
2021-01-03 22:37:02 +00:00
waitStages[waitSemaphoreCount] = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
waitSemaphoreCount += 1;
2021-01-02 21:31:17 +00:00
}
2020-12-21 23:44:43 +00:00
present = !renderer->headless && renderer->shouldPresent;
2021-01-02 06:07:15 +00:00
commandBuffers = SDL_stack_alloc(VkCommandBuffer, commandBufferCount);
2020-12-22 02:34:57 +00:00
2021-01-02 06:07:15 +00:00
for (i = 0; i < commandBufferCount; i += 1)
2020-12-22 02:34:57 +00:00
{
2021-01-02 06:07:15 +00:00
currentCommandBuffer = (VulkanCommandBuffer*)pCommandBuffers[i];
VULKAN_INTERNAL_EndCommandBuffer(renderer, currentCommandBuffer);
commandBuffers[i] = currentCommandBuffer->commandBuffer;
2020-12-22 02:34:57 +00:00
}
2020-12-21 23:44:43 +00:00
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pNext = NULL;
2021-01-02 21:31:17 +00:00
submitInfo.commandBufferCount = commandBufferCount;
submitInfo.pCommandBuffers = commandBuffers;
2020-12-21 23:44:43 +00:00
if (present)
2020-12-17 03:50:31 +00:00
{
waitSemaphores[waitSemaphoreCount] = renderer->imageAvailableSemaphore;
2021-01-03 22:37:02 +00:00
waitStages[waitSemaphoreCount] = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
waitSemaphoreCount += 1;
2021-01-03 22:37:02 +00:00
submitInfo.pWaitDstStageMask = waitStages;
2020-12-21 23:44:43 +00:00
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &renderer->renderFinishedSemaphore;
2020-12-17 03:50:31 +00:00
}
else
{
2021-01-14 01:37:54 +00:00
submitInfo.pWaitDstStageMask = waitStages;
2020-12-21 23:44:43 +00:00
submitInfo.signalSemaphoreCount = 0;
submitInfo.pSignalSemaphores = NULL;
2020-12-17 03:50:31 +00:00
}
submitInfo.waitSemaphoreCount = waitSemaphoreCount;
submitInfo.pWaitSemaphores = waitSemaphores;
2020-12-21 23:44:43 +00:00
/* Wait for the previous submission to complete */
vulkanResult = renderer->vkWaitForFences(
renderer->logicalDevice,
1,
&renderer->inFlightFence,
VK_TRUE,
UINT64_MAX
);
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkWaitForFences", vulkanResult);
return;
2020-12-17 03:50:31 +00:00
}
2020-12-29 03:44:34 +00:00
VULKAN_INTERNAL_PostWorkCleanup(renderer);
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
/* Reset the previously submitted command buffers */
for (i = 0; i < renderer->submittedCommandBufferCount; i += 1)
{
2021-01-02 06:07:15 +00:00
if (!renderer->submittedCommandBuffers[i]->fixed)
2020-12-21 23:44:43 +00:00
{
2021-01-02 06:07:15 +00:00
VULKAN_INTERNAL_ResetCommandBuffer(
renderer,
renderer->submittedCommandBuffers[i]
);
2020-12-21 23:44:43 +00:00
}
}
renderer->submittedCommandBufferCount = 0;
/* Prepare the command buffer fence for submission */
renderer->vkResetFences(
2020-12-17 03:50:31 +00:00
renderer->logicalDevice,
2020-12-21 23:44:43 +00:00
1,
&renderer->inFlightFence
2020-12-17 03:50:31 +00:00
);
2021-01-02 21:31:17 +00:00
if (renderer->pendingTransfer)
{
/* Submit any pending transfers */
vulkanResult = renderer->vkQueueSubmit(
renderer->transferQueue,
1,
&transferSubmitInfo,
2021-01-03 04:03:07 +00:00
NULL
2021-01-02 21:31:17 +00:00
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkQueueSubmit", vulkanResult);
return;
}
}
2020-12-21 23:44:43 +00:00
/* Submit the commands, finally. */
vulkanResult = renderer->vkQueueSubmit(
renderer->graphicsQueue,
1,
&submitInfo,
renderer->inFlightFence
2020-12-17 03:50:31 +00:00
);
2020-12-21 23:44:43 +00:00
if (vulkanResult != VK_SUCCESS)
2020-12-17 03:50:31 +00:00
{
2020-12-21 23:44:43 +00:00
LogVulkanResult("vkQueueSubmit", vulkanResult);
return;
2020-12-17 03:50:31 +00:00
}
2021-01-02 06:07:15 +00:00
if (renderer->submittedCommandBufferCount >= renderer->submittedCommandBufferCapacity)
2020-12-17 03:50:31 +00:00
{
2021-01-02 06:07:15 +00:00
renderer->submittedCommandBufferCapacity *= 2;
renderer->submittedCommandBuffers = SDL_realloc(
renderer->submittedCommandBuffers,
sizeof(VulkanCommandBuffer*) * renderer->submittedCommandBufferCapacity
);
2020-12-17 03:50:31 +00:00
}
2021-01-02 06:07:15 +00:00
/* Mark command buffers as submitted */
for (i = 0; i < commandBufferCount; i += 1)
{
((VulkanCommandBuffer*)pCommandBuffers[i])->submitted = 1;
2021-01-02 21:31:17 +00:00
renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount] = (VulkanCommandBuffer*) pCommandBuffers[i];
2021-01-02 06:07:15 +00:00
}
renderer->submittedCommandBufferCount = commandBufferCount;
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
/* Reset UBOs */
2021-01-02 06:07:15 +00:00
SDL_LockMutex(renderer->uniformBufferLock);
2020-12-21 23:44:43 +00:00
renderer->vertexUBOOffset = UBO_BUFFER_SIZE * renderer->frameIndex;
renderer->vertexUBOBlockIncrement = 0;
renderer->fragmentUBOOffset = UBO_BUFFER_SIZE * renderer->frameIndex;
renderer->fragmentUBOBlockIncrement = 0;
2021-01-02 06:07:15 +00:00
renderer->computeUBOOffset = UBO_BUFFER_SIZE * renderer->frameIndex;
renderer->computeUBOBlockIncrement = 0;
SDL_UnlockMutex(renderer->uniformBufferLock);
2020-12-21 23:44:43 +00:00
2020-12-27 23:34:15 +00:00
/* Reset descriptor set data */
VULKAN_INTERNAL_ResetDescriptorSetData(renderer);
2020-12-21 23:44:43 +00:00
/* Present, if applicable */
if (present)
2020-12-17 03:50:31 +00:00
{
2020-12-21 23:44:43 +00:00
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pNext = NULL;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = &renderer->renderFinishedSemaphore;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &renderer->swapChain;
presentInfo.pImageIndices = &renderer->currentSwapChainIndex;
presentInfo.pResults = NULL;
2020-12-17 03:50:31 +00:00
2020-12-21 23:44:43 +00:00
presentResult = renderer->vkQueuePresentKHR(
renderer->presentQueue,
&presentInfo
2020-12-17 03:50:31 +00:00
);
2021-01-05 07:31:56 +00:00
if (presentResult != VK_SUCCESS || renderer->needNewSwapChain)
2020-12-17 03:50:31 +00:00
{
2020-12-21 23:44:43 +00:00
VULKAN_INTERNAL_RecreateSwapchain(renderer);
2020-12-17 03:50:31 +00:00
}
}
2020-12-21 23:44:43 +00:00
renderer->swapChainImageAcquired = 0;
renderer->shouldPresent = 0;
2021-01-03 03:03:25 +00:00
renderer->pendingTransfer = 0;
renderer->textureStagingBufferOffset = 0;
2020-12-21 23:44:43 +00:00
2021-01-02 06:07:15 +00:00
SDL_stack_free(commandBuffers);
2020-12-17 03:50:31 +00:00
}
2021-01-03 05:07:51 +00:00
static void VULKAN_Wait(
2021-01-05 23:00:51 +00:00
Refresh_Renderer *driverData
2021-01-03 05:07:51 +00:00
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
renderer->vkWaitForFences(
renderer->logicalDevice,
1,
&renderer->inFlightFence,
VK_TRUE,
UINT64_MAX
);
}
2021-01-14 01:37:54 +00:00
/* External interop */
static void VULKAN_GetTextureHandles(
Refresh_Renderer* driverData,
Refresh_Texture* texture,
Refresh_TextureHandles *handles
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanTexture *vulkanTexture = (VulkanTexture*) texture;
handles->rendererType = REFRESH_RENDERER_TYPE_VULKAN;
handles->texture.vulkan.image = vulkanTexture->image;
handles->texture.vulkan.view = vulkanTexture->view;
}
2020-12-17 03:28:02 +00:00
/* Device instantiation */
static inline uint8_t VULKAN_INTERNAL_SupportsExtension(
const char *ext,
VkExtensionProperties *availableExtensions,
uint32_t numAvailableExtensions
) {
uint32_t i;
for (i = 0; i < numAvailableExtensions; i += 1)
{
if (SDL_strcmp(ext, availableExtensions[i].extensionName) == 0)
{
return 1;
}
}
return 0;
}
static uint8_t VULKAN_INTERNAL_CheckInstanceExtensions(
const char **requiredExtensions,
uint32_t requiredExtensionsLength,
uint8_t *supportsDebugUtils
) {
uint32_t extensionCount, i;
VkExtensionProperties *availableExtensions;
uint8_t allExtensionsSupported = 1;
vkEnumerateInstanceExtensionProperties(
NULL,
&extensionCount,
NULL
);
availableExtensions = SDL_stack_alloc(
VkExtensionProperties,
extensionCount
);
vkEnumerateInstanceExtensionProperties(
NULL,
&extensionCount,
availableExtensions
);
for (i = 0; i < requiredExtensionsLength; i += 1)
{
if (!VULKAN_INTERNAL_SupportsExtension(
requiredExtensions[i],
availableExtensions,
extensionCount
)) {
allExtensionsSupported = 0;
break;
}
}
/* This is optional, but nice to have! */
*supportsDebugUtils = VULKAN_INTERNAL_SupportsExtension(
VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
availableExtensions,
extensionCount
);
SDL_stack_free(availableExtensions);
return allExtensionsSupported;
}
static uint8_t VULKAN_INTERNAL_CheckValidationLayers(
const char** validationLayers,
uint32_t validationLayersLength
) {
uint32_t layerCount;
VkLayerProperties *availableLayers;
uint32_t i, j;
uint8_t layerFound = 0;
2020-12-17 03:28:02 +00:00
vkEnumerateInstanceLayerProperties(&layerCount, NULL);
availableLayers = SDL_stack_alloc(VkLayerProperties, layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers);
for (i = 0; i < validationLayersLength; i += 1)
{
layerFound = 0;
for (j = 0; j < layerCount; j += 1)
{
if (SDL_strcmp(validationLayers[i], availableLayers[j].layerName) == 0)
{
layerFound = 1;
break;
}
}
if (!layerFound)
{
break;
}
}
SDL_stack_free(availableLayers);
return layerFound;
}
static uint8_t VULKAN_INTERNAL_CreateInstance(
VulkanRenderer *renderer,
2020-12-17 02:12:20 +00:00
void *deviceWindowHandle
2020-12-17 03:28:02 +00:00
) {
VkResult vulkanResult;
VkApplicationInfo appInfo;
const char **instanceExtensionNames;
uint32_t instanceExtensionCount;
VkInstanceCreateInfo createInfo;
static const char *layerNames[] = { "VK_LAYER_KHRONOS_validation" };
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pNext = NULL;
appInfo.pApplicationName = NULL;
appInfo.applicationVersion = 0;
appInfo.pEngineName = "REFRESH";
appInfo.engineVersion = REFRESH_COMPILED_VERSION;
appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0);
if (!SDL_Vulkan_GetInstanceExtensions(
(SDL_Window*) deviceWindowHandle,
&instanceExtensionCount,
NULL
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"SDL_Vulkan_GetInstanceExtensions(): getExtensionCount: %s",
SDL_GetError()
);
return 0;
}
/* Extra space for the following extensions:
* VK_KHR_get_physical_device_properties2
* VK_EXT_debug_utils
*/
instanceExtensionNames = SDL_stack_alloc(
const char*,
instanceExtensionCount + 2
);
if (!SDL_Vulkan_GetInstanceExtensions(
(SDL_Window*) deviceWindowHandle,
&instanceExtensionCount,
instanceExtensionNames
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"SDL_Vulkan_GetInstanceExtensions(): %s",
SDL_GetError()
);
SDL_stack_free((char*) instanceExtensionNames);
return 0;
}
/* Core since 1.1 */
instanceExtensionNames[instanceExtensionCount++] =
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME;
if (!VULKAN_INTERNAL_CheckInstanceExtensions(
instanceExtensionNames,
instanceExtensionCount,
&renderer->supportsDebugUtils
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"Required Vulkan instance extensions not supported"
);
SDL_stack_free((char*) instanceExtensionNames);
return 0;
}
if (renderer->supportsDebugUtils)
{
/* Append the debug extension to the end */
instanceExtensionNames[instanceExtensionCount++] =
VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogWarn(
2020-12-17 03:28:02 +00:00
"%s is not supported!",
VK_EXT_DEBUG_UTILS_EXTENSION_NAME
);
}
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pNext = NULL;
createInfo.flags = 0;
createInfo.pApplicationInfo = &appInfo;
createInfo.ppEnabledLayerNames = layerNames;
createInfo.enabledExtensionCount = instanceExtensionCount;
createInfo.ppEnabledExtensionNames = instanceExtensionNames;
if (renderer->debugMode)
{
createInfo.enabledLayerCount = SDL_arraysize(layerNames);
if (!VULKAN_INTERNAL_CheckValidationLayers(
layerNames,
createInfo.enabledLayerCount
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogWarn("Validation layers not found, continuing without validation");
2020-12-17 03:28:02 +00:00
createInfo.enabledLayerCount = 0;
}
}
else
{
createInfo.enabledLayerCount = 0;
}
vulkanResult = vkCreateInstance(&createInfo, NULL, &renderer->instance);
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"vkCreateInstance failed: %s",
VkErrorMessages(vulkanResult)
);
SDL_stack_free((char*) instanceExtensionNames);
return 0;
}
SDL_stack_free((char*) instanceExtensionNames);
return 1;
}
static uint8_t VULKAN_INTERNAL_CheckDeviceExtensions(
VulkanRenderer *renderer,
VkPhysicalDevice physicalDevice,
const char** requiredExtensions,
uint32_t requiredExtensionsLength
) {
uint32_t extensionCount, i;
VkExtensionProperties *availableExtensions;
uint8_t allExtensionsSupported = 1;
renderer->vkEnumerateDeviceExtensionProperties(
physicalDevice,
NULL,
&extensionCount,
NULL
);
availableExtensions = SDL_stack_alloc(
VkExtensionProperties,
extensionCount
);
renderer->vkEnumerateDeviceExtensionProperties(
physicalDevice,
NULL,
&extensionCount,
availableExtensions
);
for (i = 0; i < requiredExtensionsLength; i += 1)
{
if (!VULKAN_INTERNAL_SupportsExtension(
requiredExtensions[i],
availableExtensions,
extensionCount
)) {
allExtensionsSupported = 0;
break;
}
}
SDL_stack_free(availableExtensions);
return allExtensionsSupported;
}
static uint8_t VULKAN_INTERNAL_IsDeviceSuitable(
VulkanRenderer *renderer,
VkPhysicalDevice physicalDevice,
const char** requiredExtensionNames,
uint32_t requiredExtensionNamesLength,
VkSurfaceKHR surface,
QueueFamilyIndices *queueFamilyIndices,
uint8_t *isIdeal
) {
uint32_t queueFamilyCount, i;
SwapChainSupportDetails swapChainSupportDetails;
VkQueueFamilyProperties *queueProps;
VkBool32 supportsPresent;
2021-01-03 03:03:25 +00:00
uint8_t querySuccess = 0;
2021-01-03 04:03:07 +00:00
uint8_t foundFamily = 0;
2021-01-03 03:03:25 +00:00
uint8_t foundSuitableDevice = 0;
2020-12-17 03:28:02 +00:00
VkPhysicalDeviceProperties deviceProperties;
queueFamilyIndices->graphicsFamily = UINT32_MAX;
queueFamilyIndices->presentFamily = UINT32_MAX;
2021-01-03 03:03:25 +00:00
queueFamilyIndices->computeFamily = UINT32_MAX;
2021-01-02 21:31:17 +00:00
queueFamilyIndices->transferFamily = UINT32_MAX;
2020-12-17 03:28:02 +00:00
*isIdeal = 0;
/* Note: If no dedicated device exists,
* one that supports our features would be fine
*/
if (!VULKAN_INTERNAL_CheckDeviceExtensions(
renderer,
physicalDevice,
requiredExtensionNames,
requiredExtensionNamesLength
)) {
return 0;
}
renderer->vkGetPhysicalDeviceQueueFamilyProperties(
physicalDevice,
&queueFamilyCount,
NULL
);
/* FIXME: Need better structure for checking vs storing support details */
querySuccess = VULKAN_INTERNAL_QuerySwapChainSupport(
renderer,
physicalDevice,
surface,
&swapChainSupportDetails
);
SDL_free(swapChainSupportDetails.formats);
SDL_free(swapChainSupportDetails.presentModes);
if ( querySuccess == 0 ||
swapChainSupportDetails.formatsLength == 0 ||
swapChainSupportDetails.presentModesLength == 0 )
{
return 0;
}
queueProps = (VkQueueFamilyProperties*) SDL_stack_alloc(
VkQueueFamilyProperties,
queueFamilyCount
);
renderer->vkGetPhysicalDeviceQueueFamilyProperties(
physicalDevice,
&queueFamilyCount,
queueProps
);
for (i = 0; i < queueFamilyCount; i += 1)
{
renderer->vkGetPhysicalDeviceSurfaceSupportKHR(
physicalDevice,
i,
surface,
&supportsPresent
);
2021-01-03 04:03:07 +00:00
if (!foundFamily)
2021-01-02 21:31:17 +00:00
{
if ( supportsPresent &&
2021-01-03 03:03:25 +00:00
(queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
2021-01-03 04:03:07 +00:00
(queueProps[i].queueFlags & VK_QUEUE_COMPUTE_BIT) &&
(queueProps[i].queueFlags & VK_QUEUE_TRANSFER_BIT) )
2021-01-02 21:31:17 +00:00
{
queueFamilyIndices->graphicsFamily = i;
queueFamilyIndices->presentFamily = i;
2021-01-03 03:03:25 +00:00
queueFamilyIndices->computeFamily = i;
2021-01-02 21:31:17 +00:00
queueFamilyIndices->transferFamily = i;
2021-01-03 04:03:07 +00:00
foundFamily = 1;
2021-01-02 21:31:17 +00:00
}
}
2021-01-03 04:03:07 +00:00
if (foundFamily)
2020-12-17 03:28:02 +00:00
{
foundSuitableDevice = 1;
break;
}
}
SDL_stack_free(queueProps);
if (foundSuitableDevice)
{
/* We'd really like a discrete GPU, but it's OK either way! */
renderer->vkGetPhysicalDeviceProperties(
physicalDevice,
&deviceProperties
);
if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
{
*isIdeal = 1;
}
return 1;
}
/* This device is useless for us, next! */
return 0;
}
2021-01-14 01:37:54 +00:00
static VULKAN_INTERNAL_GetPhysicalDeviceProperties(
VulkanRenderer *renderer
) {
renderer->physicalDeviceDriverProperties.sType =
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR;
renderer->physicalDeviceDriverProperties.pNext = NULL;
renderer->physicalDeviceProperties.sType =
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
renderer->physicalDeviceProperties.pNext =
&renderer->physicalDeviceDriverProperties;
renderer->vkGetPhysicalDeviceProperties2KHR(
renderer->physicalDevice,
&renderer->physicalDeviceProperties
);
}
2020-12-17 03:28:02 +00:00
static uint8_t VULKAN_INTERNAL_DeterminePhysicalDevice(
VulkanRenderer *renderer,
const char **deviceExtensionNames,
uint32_t deviceExtensionCount
) {
VkResult vulkanResult;
VkPhysicalDevice *physicalDevices;
uint32_t physicalDeviceCount, i, suitableIndex;
VkPhysicalDevice physicalDevice;
QueueFamilyIndices queueFamilyIndices;
uint8_t isIdeal;
vulkanResult = renderer->vkEnumeratePhysicalDevices(
renderer->instance,
&physicalDeviceCount,
NULL
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"vkEnumeratePhysicalDevices failed: %s",
VkErrorMessages(vulkanResult)
);
return 0;
}
if (physicalDeviceCount == 0)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to find any GPUs with Vulkan support");
2020-12-17 03:28:02 +00:00
return 0;
}
physicalDevices = SDL_stack_alloc(VkPhysicalDevice, physicalDeviceCount);
vulkanResult = renderer->vkEnumeratePhysicalDevices(
renderer->instance,
&physicalDeviceCount,
physicalDevices
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"vkEnumeratePhysicalDevices failed: %s",
VkErrorMessages(vulkanResult)
);
SDL_stack_free(physicalDevices);
return 0;
}
/* Any suitable device will do, but we'd like the best */
suitableIndex = -1;
for (i = 0; i < physicalDeviceCount; i += 1)
{
if (VULKAN_INTERNAL_IsDeviceSuitable(
renderer,
physicalDevices[i],
deviceExtensionNames,
deviceExtensionCount,
renderer->surface,
&queueFamilyIndices,
&isIdeal
)) {
suitableIndex = i;
if (isIdeal)
{
/* This is the one we want! */
break;
}
}
}
if (suitableIndex != -1)
{
physicalDevice = physicalDevices[suitableIndex];
}
else
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("No suitable physical devices found");
2020-12-17 03:28:02 +00:00
SDL_stack_free(physicalDevices);
return 0;
}
renderer->physicalDevice = physicalDevice;
renderer->queueFamilyIndices = queueFamilyIndices;
2021-01-14 01:37:54 +00:00
VULKAN_INTERNAL_GetPhysicalDeviceProperties(renderer);
2020-12-17 03:28:02 +00:00
SDL_stack_free(physicalDevices);
return 1;
}
static uint8_t VULKAN_INTERNAL_CreateLogicalDevice(
VulkanRenderer *renderer,
const char **deviceExtensionNames,
uint32_t deviceExtensionCount
) {
VkResult vulkanResult;
2020-12-17 04:04:47 +00:00
2020-12-17 03:28:02 +00:00
VkDeviceCreateInfo deviceCreateInfo;
VkPhysicalDeviceFeatures deviceFeatures;
VkDeviceQueueCreateInfo queueCreateInfos[2];
2020-12-17 03:28:02 +00:00
VkDeviceQueueCreateInfo queueCreateInfoGraphics;
VkDeviceQueueCreateInfo queueCreateInfoPresent;
int32_t queueInfoCount = 1;
2020-12-17 03:28:02 +00:00
float queuePriority = 1.0f;
queueCreateInfoGraphics.sType =
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfoGraphics.pNext = NULL;
queueCreateInfoGraphics.flags = 0;
queueCreateInfoGraphics.queueFamilyIndex =
renderer->queueFamilyIndices.graphicsFamily;
queueCreateInfoGraphics.queueCount = 1;
queueCreateInfoGraphics.pQueuePriorities = &queuePriority;
queueCreateInfos[0] = queueCreateInfoGraphics;
if (renderer->queueFamilyIndices.presentFamily != renderer->queueFamilyIndices.graphicsFamily)
{
queueCreateInfoPresent.sType =
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfoPresent.pNext = NULL;
queueCreateInfoPresent.flags = 0;
queueCreateInfoPresent.queueFamilyIndex =
renderer->queueFamilyIndices.presentFamily;
queueCreateInfoPresent.queueCount = 1;
queueCreateInfoPresent.pQueuePriorities = &queuePriority;
2021-01-02 21:31:17 +00:00
queueCreateInfos[queueInfoCount] = queueCreateInfoPresent;
2020-12-17 03:28:02 +00:00
queueInfoCount += 1;
}
/* specifying used device features */
SDL_zero(deviceFeatures);
deviceFeatures.occlusionQueryPrecise = VK_TRUE;
deviceFeatures.fillModeNonSolid = VK_TRUE;
/* creating the logical device */
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceCreateInfo.pNext = NULL;
deviceCreateInfo.flags = 0;
deviceCreateInfo.queueCreateInfoCount = queueInfoCount;
deviceCreateInfo.pQueueCreateInfos = queueCreateInfos;
deviceCreateInfo.enabledLayerCount = 0;
deviceCreateInfo.ppEnabledLayerNames = NULL;
deviceCreateInfo.enabledExtensionCount = deviceExtensionCount;
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
vulkanResult = renderer->vkCreateDevice(
renderer->physicalDevice,
&deviceCreateInfo,
NULL,
&renderer->logicalDevice
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError(
2020-12-17 03:28:02 +00:00
"vkCreateDevice failed: %s",
VkErrorMessages(vulkanResult)
);
return 0;
}
/* Load vkDevice entry points */
#define VULKAN_DEVICE_FUNCTION(ext, ret, func, params) \
renderer->func = (vkfntype_##func) \
renderer->vkGetDeviceProcAddr( \
renderer->logicalDevice, \
#func \
);
#include "Refresh_Driver_Vulkan_vkfuncs.h"
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.graphicsFamily,
0,
&renderer->graphicsQueue
);
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.presentFamily,
0,
&renderer->presentQueue
);
2021-01-03 03:03:25 +00:00
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.computeFamily,
0,
&renderer->computeQueue
);
2021-01-02 21:31:17 +00:00
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.transferFamily,
0,
&renderer->transferQueue
);
2020-12-17 03:28:02 +00:00
return 1;
}
2021-01-14 01:37:54 +00:00
static void VULKAN_INTERNAL_LoadEntryPoints(
VulkanRenderer *renderer
) {
/* Load Vulkan entry points */
if (SDL_Vulkan_LoadLibrary(NULL) < 0)
{
Refresh_LogWarn("Vulkan: SDL_Vulkan_LoadLibrary failed!");
return;
2021-01-14 01:37:54 +00:00
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();
#pragma GCC diagnostic pop
if (vkGetInstanceProcAddr == NULL)
{
Refresh_LogWarn(
"SDL_Vulkan_GetVkGetInstanceProcAddr(): %s",
SDL_GetError()
);
return;
2021-01-14 01:37:54 +00:00
}
#define VULKAN_GLOBAL_FUNCTION(name) \
name = (PFN_##name) vkGetInstanceProcAddr(VK_NULL_HANDLE, #name); \
if (name == NULL) \
{ \
Refresh_LogWarn("vkGetInstanceProcAddr(VK_NULL_HANDLE, \"" #name "\") failed"); \
return; \
2021-01-14 01:37:54 +00:00
}
#include "Refresh_Driver_Vulkan_vkfuncs.h"
}
/* Expects a partially initialized VulkanRenderer */
static Refresh_Device* VULKAN_INTERNAL_CreateDevice(
VulkanRenderer *renderer
2020-12-17 01:23:49 +00:00
) {
2021-01-05 23:00:51 +00:00
Refresh_Device *result;
2020-12-17 01:23:49 +00:00
2020-12-17 04:04:47 +00:00
VkResult vulkanResult;
2020-12-18 22:35:33 +00:00
uint32_t i;
2020-12-17 04:04:47 +00:00
/* Variables: Create fence and semaphores */
VkFenceCreateInfo fenceInfo;
VkSemaphoreCreateInfo semaphoreInfo;
2021-01-02 21:31:17 +00:00
/* Variables: Transfer command buffer */
VkCommandPoolCreateInfo transferCommandPoolCreateInfo;
VkCommandBufferAllocateInfo transferCommandBufferAllocateInfo;
2020-12-29 22:52:24 +00:00
/* Variables: Descriptor set layouts */
2020-12-17 19:40:49 +00:00
VkDescriptorSetLayoutCreateInfo setLayoutCreateInfo;
VkDescriptorSetLayoutBinding vertexParamLayoutBinding;
VkDescriptorSetLayoutBinding fragmentParamLayoutBinding;
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayoutBinding computeParamLayoutBinding;
2020-12-17 19:40:49 +00:00
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayoutBinding emptyVertexSamplerLayoutBinding;
VkDescriptorSetLayoutBinding emptyFragmentSamplerLayoutBinding;
2020-12-29 22:52:24 +00:00
VkDescriptorSetLayoutBinding emptyComputeBufferDescriptorSetLayoutBinding;
2020-12-31 04:39:47 +00:00
VkDescriptorSetLayoutBinding emptyComputeImageDescriptorSetLayoutBinding;
2020-12-29 22:52:24 +00:00
2020-12-20 09:29:15 +00:00
/* Variables: UBO Creation */
VkDescriptorPoolCreateInfo defaultDescriptorPoolInfo;
2020-12-31 04:39:47 +00:00
VkDescriptorPoolSize poolSizes[4];
2020-12-28 22:07:13 +00:00
VkDescriptorSetAllocateInfo descriptorAllocateInfo;
2020-12-20 09:29:15 +00:00
2021-01-05 23:00:51 +00:00
result = (Refresh_Device*) SDL_malloc(sizeof(Refresh_Device));
2020-12-17 01:23:49 +00:00
ASSIGN_DRIVER(VULKAN)
2021-01-05 23:00:51 +00:00
result->driverData = (Refresh_Renderer*) renderer;
2020-12-17 01:23:49 +00:00
2020-12-17 03:50:31 +00:00
/*
* Create initial swapchain
*/
if (!renderer->headless)
{
if (VULKAN_INTERNAL_CreateSwapchain(renderer) != CREATE_SWAPCHAIN_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create swap chain");
2020-12-17 03:50:31 +00:00
return NULL;
}
}
2020-12-21 23:44:43 +00:00
renderer->needNewSwapChain = 0;
renderer->shouldPresent = 0;
renderer->swapChainImageAcquired = 0;
2020-12-17 04:04:47 +00:00
/*
* Create fence and semaphores
*/
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.pNext = NULL;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
semaphoreInfo.pNext = NULL;
semaphoreInfo.flags = 0;
2021-01-02 21:31:17 +00:00
vulkanResult = renderer->vkCreateSemaphore(
renderer->logicalDevice,
&semaphoreInfo,
NULL,
&renderer->transferFinishedSemaphore
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateSemaphore", vulkanResult);
return NULL;
}
2020-12-17 04:04:47 +00:00
vulkanResult = renderer->vkCreateSemaphore(
renderer->logicalDevice,
&semaphoreInfo,
NULL,
&renderer->imageAvailableSemaphore
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-02 21:31:17 +00:00
LogVulkanResult("vkCreateSemaphore", vulkanResult);
2020-12-17 04:04:47 +00:00
return NULL;
}
vulkanResult = renderer->vkCreateSemaphore(
renderer->logicalDevice,
&semaphoreInfo,
NULL,
&renderer->renderFinishedSemaphore
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateSemaphore", vulkanResult);
return NULL;
}
vulkanResult = renderer->vkCreateFence(
renderer->logicalDevice,
&fenceInfo,
NULL,
&renderer->inFlightFence
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-02 21:31:17 +00:00
LogVulkanResult("vkCreateFence", vulkanResult);
2020-12-17 04:04:47 +00:00
return NULL;
}
2020-12-22 01:59:08 +00:00
/* Threading */
renderer->allocatorLock = SDL_CreateMutex();
2020-12-28 23:11:05 +00:00
renderer->disposeLock = SDL_CreateMutex();
2021-01-02 06:07:15 +00:00
renderer->uniformBufferLock = SDL_CreateMutex();
renderer->descriptorSetLock = SDL_CreateMutex();
renderer->boundBufferLock = SDL_CreateMutex();
2021-01-03 21:17:51 +00:00
renderer->stagingLock = SDL_CreateMutex();
2020-12-22 01:59:08 +00:00
2021-01-02 21:31:17 +00:00
/* Transfer buffer */
transferCommandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
transferCommandPoolCreateInfo.pNext = NULL;
transferCommandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
transferCommandPoolCreateInfo.queueFamilyIndex = renderer->queueFamilyIndices.transferFamily;
vulkanResult = renderer->vkCreateCommandPool(
renderer->logicalDevice,
&transferCommandPoolCreateInfo,
NULL,
&renderer->transferCommandPool
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkCreateCommandPool", vulkanResult);
return NULL;
}
transferCommandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
transferCommandBufferAllocateInfo.pNext = NULL;
transferCommandBufferAllocateInfo.commandBufferCount = 2;
transferCommandBufferAllocateInfo.commandPool = renderer->transferCommandPool;
transferCommandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
vulkanResult = renderer->vkAllocateCommandBuffers(
renderer->logicalDevice,
&transferCommandBufferAllocateInfo,
renderer->transferCommandBuffers
);
if (vulkanResult != VK_SUCCESS)
{
LogVulkanResult("vkAllocateCommandBuffers", vulkanResult);
return NULL;
}
renderer->pendingTransfer = 0;
2020-12-17 04:04:47 +00:00
/*
2021-01-02 06:07:15 +00:00
* Create submitted command buffer list
2020-12-17 04:04:47 +00:00
*/
2021-01-02 06:07:15 +00:00
renderer->submittedCommandBufferCapacity = 16;
2020-12-17 04:04:47 +00:00
renderer->submittedCommandBufferCount = 0;
2021-01-02 06:07:15 +00:00
renderer->submittedCommandBuffers = SDL_malloc(sizeof(VulkanCommandBuffer*) * renderer->submittedCommandBufferCapacity);
2020-12-17 04:04:47 +00:00
2020-12-22 01:59:08 +00:00
/* Memory Allocator */
renderer->memoryAllocator = (VulkanMemoryAllocator*) SDL_malloc(
sizeof(VulkanMemoryAllocator)
);
for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1)
{
renderer->memoryAllocator->subAllocators[i].nextAllocationSize = STARTING_ALLOCATION_SIZE;
renderer->memoryAllocator->subAllocators[i].allocations = NULL;
renderer->memoryAllocator->subAllocators[i].allocationCount = 0;
renderer->memoryAllocator->subAllocators[i].sortedFreeRegions = SDL_malloc(
sizeof(VulkanMemoryFreeRegion*) * 4
);
renderer->memoryAllocator->subAllocators[i].sortedFreeRegionCount = 0;
renderer->memoryAllocator->subAllocators[i].sortedFreeRegionCapacity = 4;
}
2020-12-23 21:11:09 +00:00
/* UBO Data */
renderer->vertexUBO = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
UBO_ACTUAL_SIZE,
RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
2020-12-29 00:28:14 +00:00
1,
2020-12-23 21:11:09 +00:00
renderer->vertexUBO
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create vertex UBO!");
2020-12-23 21:11:09 +00:00
return NULL;
}
renderer->fragmentUBO = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
UBO_ACTUAL_SIZE,
RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
2020-12-29 00:28:14 +00:00
1,
2020-12-23 21:11:09 +00:00
renderer->fragmentUBO
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create fragment UBO!");
2020-12-23 21:11:09 +00:00
return NULL;
}
2020-12-31 04:39:47 +00:00
renderer->computeUBO = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
UBO_ACTUAL_SIZE,
RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1,
renderer->computeUBO
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create compute UBO!");
2020-12-31 04:39:47 +00:00
return NULL;
}
2020-12-23 21:11:09 +00:00
renderer->minUBOAlignment = renderer->physicalDeviceProperties.properties.limits.minUniformBufferOffsetAlignment;
renderer->vertexUBOOffset = 0;
renderer->vertexUBOBlockIncrement = 0;
renderer->fragmentUBOOffset = 0;
renderer->fragmentUBOBlockIncrement = 0;
2020-12-31 04:39:47 +00:00
renderer->computeUBOOffset = 0;
renderer->computeUBOBlockIncrement = 0;
2020-12-23 21:11:09 +00:00
2020-12-17 19:40:49 +00:00
/* Set up UBO layouts */
emptyVertexSamplerLayoutBinding.binding = 0;
emptyVertexSamplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
emptyVertexSamplerLayoutBinding.descriptorCount = 0;
emptyVertexSamplerLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
emptyVertexSamplerLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
setLayoutCreateInfo.pNext = NULL;
setLayoutCreateInfo.flags = 0;
setLayoutCreateInfo.bindingCount = 1;
setLayoutCreateInfo.pBindings = &emptyVertexSamplerLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->emptyVertexSamplerLayout
);
emptyFragmentSamplerLayoutBinding.binding = 0;
emptyFragmentSamplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
emptyFragmentSamplerLayoutBinding.descriptorCount = 0;
emptyFragmentSamplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
emptyFragmentSamplerLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.pBindings = &emptyFragmentSamplerLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->emptyFragmentSamplerLayout
);
2020-12-29 22:52:24 +00:00
emptyComputeBufferDescriptorSetLayoutBinding.binding = 0;
emptyComputeBufferDescriptorSetLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
emptyComputeBufferDescriptorSetLayoutBinding.descriptorCount = 0;
emptyComputeBufferDescriptorSetLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
emptyComputeBufferDescriptorSetLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.pBindings = &emptyComputeBufferDescriptorSetLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->emptyComputeBufferDescriptorSetLayout
);
2020-12-31 04:39:47 +00:00
emptyComputeImageDescriptorSetLayoutBinding.binding = 0;
emptyComputeImageDescriptorSetLayoutBinding.descriptorCount = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
emptyComputeImageDescriptorSetLayoutBinding.descriptorCount = 0;
emptyComputeImageDescriptorSetLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
emptyComputeImageDescriptorSetLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.pBindings = &emptyComputeImageDescriptorSetLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->emptyComputeImageDescriptorSetLayout
);
2020-12-17 19:40:49 +00:00
vertexParamLayoutBinding.binding = 0;
vertexParamLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
vertexParamLayoutBinding.descriptorCount = 1;
vertexParamLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
vertexParamLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.bindingCount = 1;
setLayoutCreateInfo.pBindings = &vertexParamLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->vertexParamLayout
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create vertex UBO layout!");
2020-12-17 19:40:49 +00:00
return NULL;
}
fragmentParamLayoutBinding.binding = 0;
fragmentParamLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
fragmentParamLayoutBinding.descriptorCount = 1;
fragmentParamLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
fragmentParamLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.bindingCount = 1;
setLayoutCreateInfo.pBindings = &fragmentParamLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->fragmentParamLayout
);
if (vulkanResult != VK_SUCCESS)
{
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create fragment UBO layout!");
2020-12-17 19:40:49 +00:00
return NULL;
}
2020-12-31 04:39:47 +00:00
computeParamLayoutBinding.binding = 0;
computeParamLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
computeParamLayoutBinding.descriptorCount = 1;
computeParamLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
computeParamLayoutBinding.pImmutableSamplers = NULL;
setLayoutCreateInfo.bindingCount = 1;
setLayoutCreateInfo.pBindings = &computeParamLayoutBinding;
vulkanResult = renderer->vkCreateDescriptorSetLayout(
renderer->logicalDevice,
&setLayoutCreateInfo,
NULL,
&renderer->computeParamLayout
);
/* Default Descriptors */
2020-12-20 09:29:15 +00:00
2020-12-23 21:11:09 +00:00
/* default empty sampler descriptor sets */
poolSizes[0].descriptorCount = 2;
poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
2020-12-23 21:11:09 +00:00
/* UBO descriptor sets */
poolSizes[1].descriptorCount = UBO_POOL_SIZE;
poolSizes[1].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
2020-12-20 09:29:15 +00:00
2020-12-29 22:52:24 +00:00
poolSizes[2].descriptorCount = 1;
poolSizes[2].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
2020-12-31 04:39:47 +00:00
poolSizes[3].descriptorCount = 1;
poolSizes[3].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
defaultDescriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
defaultDescriptorPoolInfo.pNext = NULL;
2020-12-29 00:28:14 +00:00
defaultDescriptorPoolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
2020-12-31 04:39:47 +00:00
defaultDescriptorPoolInfo.maxSets = UBO_POOL_SIZE + 2 + 1 + 1;
defaultDescriptorPoolInfo.poolSizeCount = 4;
defaultDescriptorPoolInfo.pPoolSizes = poolSizes;
2020-12-20 09:29:15 +00:00
renderer->vkCreateDescriptorPool(
renderer->logicalDevice,
&defaultDescriptorPoolInfo,
2020-12-20 09:29:15 +00:00
NULL,
&renderer->defaultDescriptorPool
);
2020-12-28 22:07:13 +00:00
descriptorAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptorAllocateInfo.pNext = NULL;
descriptorAllocateInfo.descriptorPool = renderer->defaultDescriptorPool;
descriptorAllocateInfo.descriptorSetCount = 1;
descriptorAllocateInfo.pSetLayouts = &renderer->emptyVertexSamplerLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
2020-12-28 22:07:13 +00:00
&descriptorAllocateInfo,
&renderer->emptyVertexSamplerDescriptorSet
);
2020-12-28 22:07:13 +00:00
descriptorAllocateInfo.pSetLayouts = &renderer->emptyFragmentSamplerLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
2020-12-28 22:07:13 +00:00
&descriptorAllocateInfo,
&renderer->emptyFragmentSamplerDescriptorSet
2020-12-20 09:29:15 +00:00
);
2020-12-29 22:52:24 +00:00
descriptorAllocateInfo.pSetLayouts = &renderer->emptyComputeBufferDescriptorSetLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&descriptorAllocateInfo,
&renderer->emptyComputeBufferDescriptorSet
);
2020-12-31 04:39:47 +00:00
descriptorAllocateInfo.pSetLayouts = &renderer->emptyComputeImageDescriptorSetLayout;
renderer->vkAllocateDescriptorSets(
renderer->logicalDevice,
&descriptorAllocateInfo,
&renderer->emptyComputeImageDescriptorSet
);
2020-12-21 23:44:43 +00:00
/* Initialize buffer space */
renderer->buffersInUseCapacity = 32;
2020-12-22 01:59:08 +00:00
renderer->buffersInUseCount = 0;
2020-12-21 23:44:43 +00:00
renderer->buffersInUse = (VulkanBuffer**)SDL_malloc(
sizeof(VulkanBuffer*) * renderer->buffersInUseCapacity
);
renderer->submittedBufferCapacity = 32;
renderer->submittedBufferCount = 0;
renderer->submittedBuffers = (VulkanBuffer**)SDL_malloc(
sizeof(VulkanBuffer*) * renderer->submittedBufferCapacity
);
2020-12-19 01:03:26 +00:00
/* Staging Buffer */
renderer->textureStagingBuffer = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
TEXTURE_STAGING_SIZE,
RESOURCE_ACCESS_MEMORY_TRANSFER_READ_WRITE,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
2020-12-19 04:08:07 +00:00
1,
2020-12-19 01:03:26 +00:00
renderer->textureStagingBuffer
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create texture staging buffer!");
2020-12-19 01:03:26 +00:00
return NULL;
}
renderer->textureStagingBufferOffset = 0;
2020-12-28 22:07:13 +00:00
/* Dummy Uniform Buffers */
renderer->dummyVertexUniformBuffer = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
16,
RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1,
renderer->dummyVertexUniformBuffer
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create dummy vertex uniform buffer!");
2020-12-28 22:07:13 +00:00
return NULL;
}
renderer->dummyFragmentUniformBuffer = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
16,
RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1,
renderer->dummyFragmentUniformBuffer
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Failed to create dummy fragment uniform buffer!");
2020-12-28 22:07:13 +00:00
return NULL;
}
2020-12-31 04:39:47 +00:00
renderer->dummyComputeUniformBuffer = (VulkanBuffer*) SDL_malloc(sizeof(VulkanBuffer));
if (!VULKAN_INTERNAL_CreateBuffer(
renderer,
16,
RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1,
renderer->dummyComputeUniformBuffer
)) {
2021-01-05 23:00:51 +00:00
Refresh_LogError("Fialed to create dummy compute uniform buffer!");
2020-12-31 04:39:47 +00:00
return NULL;
}
/* Initialize caches */
2021-01-03 02:02:20 +00:00
for (i = 0; i < NUM_COMMAND_POOL_BUCKETS; i += 1)
{
renderer->commandPoolHashTable.buckets[i].elements = NULL;
renderer->commandPoolHashTable.buckets[i].count = 0;
renderer->commandPoolHashTable.buckets[i].capacity = 0;
}
for (i = 0; i < NUM_PIPELINE_LAYOUT_BUCKETS; i += 1)
{
2020-12-29 22:52:24 +00:00
renderer->graphicsPipelineLayoutHashTable.buckets[i].elements = NULL;
renderer->graphicsPipelineLayoutHashTable.buckets[i].count = 0;
renderer->graphicsPipelineLayoutHashTable.buckets[i].capacity = 0;
}
for (i = 0; i < NUM_PIPELINE_LAYOUT_BUCKETS; i += 1)
{
renderer->computePipelineLayoutHashTable.buckets[i].elements = NULL;
renderer->computePipelineLayoutHashTable.buckets[i].count = 0;
renderer->computePipelineLayoutHashTable.buckets[i].capacity = 0;
}
for (i = 0; i < NUM_DESCRIPTOR_SET_LAYOUT_BUCKETS; i += 1)
{
2020-12-29 23:05:26 +00:00
renderer->descriptorSetLayoutHashTable.buckets[i].elements = NULL;
renderer->descriptorSetLayoutHashTable.buckets[i].count = 0;
renderer->descriptorSetLayoutHashTable.buckets[i].capacity = 0;
2020-12-29 22:52:24 +00:00
}
2020-12-28 23:11:05 +00:00
/* Deferred destroy storage */
2020-12-29 00:28:14 +00:00
renderer->colorTargetsToDestroyCapacity = 16;
renderer->colorTargetsToDestroyCount = 0;
renderer->colorTargetsToDestroy = (VulkanColorTarget**) SDL_malloc(
sizeof(VulkanColorTarget*) *
renderer->colorTargetsToDestroyCapacity
);
renderer->submittedColorTargetsToDestroyCapacity = 16;
renderer->submittedColorTargetsToDestroyCount = 0;
renderer->submittedColorTargetsToDestroy = (VulkanColorTarget**) SDL_malloc(
sizeof(VulkanColorTarget*) *
renderer->submittedColorTargetsToDestroyCapacity
);
2020-12-29 04:09:31 +00:00
renderer->depthStencilTargetsToDestroyCapacity = 16;
renderer->depthStencilTargetsToDestroyCount = 0;
renderer->depthStencilTargetsToDestroy = (VulkanDepthStencilTarget**) SDL_malloc(
sizeof(VulkanDepthStencilTarget*) *
renderer->depthStencilTargetsToDestroyCapacity
);
renderer->submittedDepthStencilTargetsToDestroyCapacity = 16;
renderer->submittedDepthStencilTargetsToDestroyCount = 0;
renderer->submittedDepthStencilTargetsToDestroy = (VulkanDepthStencilTarget**) SDL_malloc(
sizeof(VulkanDepthStencilTarget*) *
renderer->submittedDepthStencilTargetsToDestroyCapacity
);
2020-12-28 23:11:05 +00:00
renderer->texturesToDestroyCapacity = 16;
renderer->texturesToDestroyCount = 0;
renderer->texturesToDestroy = (VulkanTexture**)SDL_malloc(
sizeof(VulkanTexture*) *
renderer->texturesToDestroyCapacity
);
renderer->submittedTexturesToDestroyCapacity = 16;
renderer->submittedTexturesToDestroyCount = 0;
renderer->submittedTexturesToDestroy = (VulkanTexture**)SDL_malloc(
sizeof(VulkanTexture*) *
renderer->submittedTexturesToDestroyCapacity
);
2020-12-29 00:28:14 +00:00
renderer->buffersToDestroyCapacity = 16;
renderer->buffersToDestroyCount = 0;
renderer->buffersToDestroy = (VulkanBuffer**) SDL_malloc(
sizeof(VulkanBuffer*) *
renderer->buffersToDestroyCapacity
);
renderer->submittedBuffersToDestroyCapacity = 16;
renderer->submittedBuffersToDestroyCount = 0;
renderer->submittedBuffersToDestroy = (VulkanBuffer**) SDL_malloc(
sizeof(VulkanBuffer*) *
renderer->submittedBuffersToDestroyCapacity
);
renderer->graphicsPipelinesToDestroyCapacity = 16;
renderer->graphicsPipelinesToDestroyCount = 0;
renderer->graphicsPipelinesToDestroy = (VulkanGraphicsPipeline**) SDL_malloc(
sizeof(VulkanGraphicsPipeline*) *
renderer->graphicsPipelinesToDestroyCapacity
);
renderer->submittedGraphicsPipelinesToDestroyCapacity = 16;
renderer->submittedGraphicsPipelinesToDestroyCount = 0;
renderer->submittedGraphicsPipelinesToDestroy = (VulkanGraphicsPipeline**) SDL_malloc(
sizeof(VulkanGraphicsPipeline*) *
renderer->submittedGraphicsPipelinesToDestroyCapacity
);
2020-12-31 07:02:12 +00:00
renderer->computePipelinesToDestroyCapacity = 16;
renderer->computePipelinesToDestroyCount = 0;
renderer->computePipelinesToDestroy = (VulkanComputePipeline**) SDL_malloc(
sizeof(VulkanComputePipeline*) *
renderer->computePipelinesToDestroyCapacity
);
renderer->submittedComputePipelinesToDestroyCapacity = 16;
renderer->submittedComputePipelinesToDestroyCount = 0;
renderer->submittedComputePipelinesToDestroy = (VulkanComputePipeline**) SDL_malloc(
sizeof(VulkanComputePipeline*) *
renderer->submittedComputePipelinesToDestroyCapacity
);
2020-12-29 00:42:51 +00:00
renderer->shaderModulesToDestroyCapacity = 16;
renderer->shaderModulesToDestroyCount = 0;
renderer->shaderModulesToDestroy = (VkShaderModule*) SDL_malloc(
sizeof(VkShaderModule) *
renderer->shaderModulesToDestroyCapacity
);
renderer->submittedShaderModulesToDestroyCapacity = 16;
renderer->submittedShaderModulesToDestroyCount = 0;
renderer->submittedShaderModulesToDestroy = (VkShaderModule*) SDL_malloc(
sizeof(VkShaderModule) *
renderer->submittedShaderModulesToDestroyCapacity
);
2020-12-29 00:56:49 +00:00
renderer->samplersToDestroyCapacity = 16;
renderer->samplersToDestroyCount = 0;
renderer->samplersToDestroy = (VkSampler*) SDL_malloc(
sizeof(VkSampler) *
renderer->samplersToDestroyCapacity
);
renderer->submittedSamplersToDestroyCapacity = 16;
renderer->submittedSamplersToDestroyCount = 0;
renderer->submittedSamplersToDestroy = (VkSampler*) SDL_malloc(
sizeof(VkSampler) *
renderer->submittedSamplersToDestroyCapacity
);
2020-12-29 01:35:18 +00:00
renderer->framebuffersToDestroyCapacity = 16;
renderer->framebuffersToDestroyCount = 0;
renderer->framebuffersToDestroy = (VulkanFramebuffer**) SDL_malloc(
sizeof(VulkanFramebuffer*) *
renderer->framebuffersToDestroyCapacity
);
renderer->submittedFramebuffersToDestroyCapacity = 16;
renderer->submittedFramebuffersToDestroyCount = 0;
renderer->submittedFramebuffersToDestroy = (VulkanFramebuffer**) SDL_malloc(
sizeof(VulkanFramebuffer*) *
renderer->submittedFramebuffersToDestroyCapacity
);
2020-12-29 03:44:34 +00:00
renderer->renderPassesToDestroyCapacity = 16;
renderer->renderPassesToDestroyCount = 0;
renderer->renderPassesToDestroy = (VkRenderPass*) SDL_malloc(
sizeof(VkRenderPass) *
renderer->renderPassesToDestroyCapacity
);
renderer->submittedRenderPassesToDestroyCapacity = 16;
renderer->submittedRenderPassesToDestroyCount = 0;
renderer->submittedRenderPassesToDestroy = (VkRenderPass*) SDL_malloc(
sizeof(VkRenderPass) *
renderer->submittedRenderPassesToDestroyCapacity
);
2020-12-17 01:23:49 +00:00
return result;
}
2021-01-14 01:37:54 +00:00
static Refresh_Device* VULKAN_CreateDevice(
Refresh_PresentationParameters *presentationParameters,
uint8_t debugMode
) {
VulkanRenderer *renderer = (VulkanRenderer*) SDL_malloc(sizeof(VulkanRenderer));
VULKAN_INTERNAL_LoadEntryPoints(renderer);
/* Create the VkInstance */
if (!VULKAN_INTERNAL_CreateInstance(renderer, presentationParameters->deviceWindowHandle))
{
Refresh_LogError("Error creating vulkan instance");
return NULL;
}
renderer->deviceWindowHandle = presentationParameters->deviceWindowHandle;
renderer->presentMode = presentationParameters->presentMode;
renderer->debugMode = debugMode;
renderer->headless = presentationParameters->deviceWindowHandle == NULL;
renderer->usesExternalDevice = 0;
/*
* Create the WSI vkSurface
*/
if (!SDL_Vulkan_CreateSurface(
(SDL_Window*)renderer->deviceWindowHandle,
renderer->instance,
&renderer->surface
)) {
Refresh_LogError(
"SDL_Vulkan_CreateSurface failed: %s",
SDL_GetError()
);
return NULL;
}
/*
* Get vkInstance entry points
*/
#define VULKAN_INSTANCE_FUNCTION(ext, ret, func, params) \
renderer->func = (vkfntype_##func) vkGetInstanceProcAddr(renderer->instance, #func);
#include "Refresh_Driver_Vulkan_vkfuncs.h"
/*
* Choose/Create vkDevice
*/
if (SDL_strcmp(SDL_GetPlatform(), "Stadia") != 0)
{
deviceExtensionCount -= 1;
}
if (!VULKAN_INTERNAL_DeterminePhysicalDevice(
renderer,
deviceExtensionNames,
deviceExtensionCount
)) {
Refresh_LogError("Failed to determine a suitable physical device");
return NULL;
}
Refresh_LogInfo("Refresh Driver: Vulkan");
Refresh_LogInfo(
"Vulkan Device: %s",
renderer->physicalDeviceProperties.properties.deviceName
);
Refresh_LogInfo(
"Vulkan Driver: %s %s",
renderer->physicalDeviceDriverProperties.driverName,
renderer->physicalDeviceDriverProperties.driverInfo
);
Refresh_LogInfo(
"Vulkan Conformance: %u.%u.%u",
renderer->physicalDeviceDriverProperties.conformanceVersion.major,
renderer->physicalDeviceDriverProperties.conformanceVersion.minor,
renderer->physicalDeviceDriverProperties.conformanceVersion.patch
);
Refresh_LogWarn(
"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
"! Refresh Vulkan is still in development! !\n"
"! The API is unstable and subject to change! !\n"
"! You have been warned! !\n"
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
);
if (!VULKAN_INTERNAL_CreateLogicalDevice(
renderer,
deviceExtensionNames,
deviceExtensionCount
)) {
Refresh_LogError("Failed to create logical device");
return NULL;
}
return VULKAN_INTERNAL_CreateDevice(renderer);
}
static Refresh_Device* VULKAN_CreateDeviceUsingExternal(
Refresh_SysRenderer *sysRenderer,
uint8_t debugMode
) {
VulkanRenderer* renderer = (VulkanRenderer*)SDL_malloc(sizeof(VulkanRenderer));
renderer->instance = sysRenderer->renderer.vulkan.instance;
renderer->physicalDevice = sysRenderer->renderer.vulkan.physicalDevice;
renderer->logicalDevice = sysRenderer->renderer.vulkan.logicalDevice;
renderer->queueFamilyIndices.computeFamily = sysRenderer->renderer.vulkan.queueFamilyIndex;
renderer->queueFamilyIndices.graphicsFamily = sysRenderer->renderer.vulkan.queueFamilyIndex;
renderer->queueFamilyIndices.presentFamily = sysRenderer->renderer.vulkan.queueFamilyIndex;
renderer->queueFamilyIndices.transferFamily = sysRenderer->renderer.vulkan.queueFamilyIndex;
renderer->deviceWindowHandle = NULL;
renderer->presentMode = 0;
renderer->debugMode = debugMode;
renderer->headless = 1;
renderer->usesExternalDevice = 1;
VULKAN_INTERNAL_LoadEntryPoints(renderer);
/*
* Get vkInstance entry points
*/
#define VULKAN_INSTANCE_FUNCTION(ext, ret, func, params) \
renderer->func = (vkfntype_##func) vkGetInstanceProcAddr(renderer->instance, #func);
#include "Refresh_Driver_Vulkan_vkfuncs.h"
/* Load vkDevice entry points */
#define VULKAN_DEVICE_FUNCTION(ext, ret, func, params) \
renderer->func = (vkfntype_##func) \
renderer->vkGetDeviceProcAddr( \
renderer->logicalDevice, \
#func \
);
#include "Refresh_Driver_Vulkan_vkfuncs.h"
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.graphicsFamily,
0,
&renderer->graphicsQueue
);
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.presentFamily,
0,
&renderer->presentQueue
);
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.computeFamily,
0,
&renderer->computeQueue
);
renderer->vkGetDeviceQueue(
renderer->logicalDevice,
renderer->queueFamilyIndices.transferFamily,
0,
&renderer->transferQueue
);
VULKAN_INTERNAL_GetPhysicalDeviceProperties(renderer);
return VULKAN_INTERNAL_CreateDevice(renderer);
}
2021-01-05 23:00:51 +00:00
Refresh_Driver VulkanDriver = {
2020-12-17 00:27:14 +00:00
"Vulkan",
2021-01-14 01:37:54 +00:00
VULKAN_CreateDevice,
VULKAN_CreateDeviceUsingExternal
2020-12-17 00:27:14 +00:00
};
#endif //REFRESH_DRIVER_VULKAN