Compare commits

..

28 Commits

Author SHA1 Message Date
Caleb Cornett f91c18e85c Fixed uniform offsets, removed a bunch of code from cdefines.h 2023-11-05 19:20:37 -06:00
Caleb Cornett ff0f30a986 Uniform buffer infrastructure, still missing offset support 2023-11-05 14:17:23 -06:00
Caleb Cornett bf4b640937 Fix texture conversion array 2023-10-26 23:03:05 -05:00
Caleb Cornett 01aff2e5f1 Fix command buffer memory leak 2023-10-26 21:39:15 -05:00
Caleb Cornett 3966614681 CreateTexture, SetTextureData, BindIndexBuffer, BindFragmentSamplers, QueueDestroyTexture, DrawIndexedPrimitives, DrawInstancedPrimitives 2023-10-26 21:25:03 -05:00
Caleb Cornett 21c5ded803 Fix redundant error code printing 2023-10-26 20:37:27 -05:00
Caleb Cornett 5ba865489b QueueDestroySampler 2023-10-12 22:12:55 -05:00
Caleb Cornett 595c15a087 Add a border color conversion function 2023-10-12 22:11:55 -05:00
Caleb Cornett 50730bd52d CreateSampler 2023-10-12 22:07:46 -05:00
Caleb Cornett e3748391fe CreateBuffer, QueueDestroyBuffer, SetBufferData, BindVertexBuffers 2023-10-12 21:31:04 -05:00
Caleb Cornett 5c6d32e21a Initial work on DestroyDevice 2023-10-12 08:42:32 -05:00
Caleb Cornett 0555023100 Implement SetViewport and SetScissor 2023-10-12 08:18:53 -05:00
Caleb Cornett 3c22699dbb Style cleanups + added FIXME note for shader compilation 2023-10-12 08:18:39 -05:00
Caleb Cornett 509bd278c7 shaderc now flips Y values in the vertex shader 2023-10-10 21:51:34 -05:00
Caleb Cornett e17f86724a DrawPrimitives, BindGraphicsPipeline, misc cleanup 2023-10-10 21:45:32 -05:00
Caleb Cornett c327c3547a Implement CreateShaderModule 2023-10-10 21:32:12 -05:00
Caleb Cornett e7b2542bc0 Pipeline creation copied over from old work + shaderc update 2023-10-10 21:27:36 -05:00
Caleb Cornett e48941eb48 Fix for command pool crash, fix for hanging on Wait 2023-10-08 16:16:11 -05:00
Caleb Cornett 4fecc829bf Reworked Wait and Submit logic to use D3D11 Query objects as fences 2023-10-08 16:05:33 -05:00
Caleb Cornett ff41afb04a Stub out D3D11_Wait, fix crash when unclaiming windows, fix backend order 2023-10-08 15:14:43 -05:00
Caleb Cornett cb467c7b1a UnclaimWindow, swapchain destruction, fixed template signature 2023-10-08 15:14:43 -05:00
Caleb Cornett d5a3438547 Implemented BeginRenderPass and Submit, ClearScreen works now! 2023-10-08 15:14:43 -05:00
Caleb Cornett 287d3a7a6e More swapchain setup 2023-10-08 15:14:43 -05:00
Caleb Cornett 433c70eb8c Apply swapchainDesc fix from FNA3D, add cdefines.h to CMakeLists 2023-10-08 15:14:43 -05:00
Caleb Cornett 8c98889f46 Implemented AcquireCommandBuffer (from old D3D11 work) 2023-10-08 15:14:43 -05:00
Caleb Cornett 0e5da8a2ea Implement ClaimWindow, mostly copied from Vulkan and FNA3D D3D11 2023-10-08 15:14:43 -05:00
Caleb Cornett 8b999aca93 Remove old stuff, flesh out PrepareDriver 2023-10-08 15:14:43 -05:00
Caleb Cornett 2498e2d7c4 Groundwork for D3D11. Yoinked most of this from the prior work, but updated the ABI 2023-10-08 15:13:08 -05:00
4 changed files with 179 additions and 237 deletions

View File

@ -9,7 +9,7 @@ option(BUILD_SHARED_LIBS "Build shared library" ON)
# Version
SET(LIB_MAJOR_VERSION "1")
SET(LIB_MINOR_VERSION "15")
SET(LIB_REVISION "4")
SET(LIB_REVISION "0")
SET(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_REVISION}")
# Build Type
@ -68,11 +68,6 @@ if(NOT MSVC)
set_property(TARGET Refresh PROPERTY COMPILE_FLAGS "-std=gnu99 -Wall -Wno-strict-aliasing -pedantic")
endif()
# Windows is silly and we need to manually include the Vulkan SDK
if(MSVC)
target_include_directories(Refresh PUBLIC $ENV{VULKAN_SDK}/include)
endif()
# Refresh folders as includes, for other targets to consume
target_include_directories(Refresh PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>

View File

@ -56,7 +56,7 @@ extern "C" {
#define REFRESH_MAJOR_VERSION 1
#define REFRESH_MINOR_VERSION 15
#define REFRESH_PATCH_VERSION 4
#define REFRESH_PATCH_VERSION 0
#define REFRESH_COMPILED_VERSION ( \
(REFRESH_MAJOR_VERSION * 100 * 100) + \

View File

@ -238,7 +238,7 @@ partial class Program
{
Process glslc = Process.Start(
"glslc",
$"\"{glslPath}\" -o \"{outputPath}\""
$"""{glslPath}"" -o ""{outputPath}"""
);
glslc.WaitForExit();
if (glslc.ExitCode != 0)
@ -254,7 +254,7 @@ partial class Program
{
Process spirvcross = Process.Start(
"spirv-cross",
$"\"{spirvPath}\" --hlsl --flip-vert-y --shader-model 50 --output \"{outputPath}\""
$"""{spirvPath}"" --hlsl --flip-vert-y --shader-model 50 --output ""{outputPath}"""
);
spirvcross.WaitForExit();
if (spirvcross.ExitCode != 0)

View File

@ -79,8 +79,7 @@ typedef struct VulkanExtensions
#define ALLOCATION_INCREMENT 16000000 /* 16MB */
#define TRANSFER_BUFFER_STARTING_SIZE 8000000 /* 8MB */
#define POOLED_TRANSFER_BUFFER_SIZE 16000000 /* 16MB */
#define UBO_BUFFER_SIZE 16777216 /* 16MB */
#define UBO_SECTION_SIZE 4096 /* 4KB */
#define UBO_BUFFER_SIZE 16000 /* 16KB */
#define DESCRIPTOR_POOL_STARTING_SIZE 128
#define DEFRAG_TIME 200
#define WINDOW_DATA "Refresh_VulkanWindowData"
@ -561,8 +560,8 @@ static const VulkanResourceAccessInfo AccessMap[RESOURCE_ACCESS_TYPES_COUNT] =
/* RESOURCE_ACCESS_ANY_SHADER_READ_SAMPLED_IMAGE */
{
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_ACCESS_SHADER_READ_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
},
@ -708,7 +707,6 @@ struct VulkanBuffer
VulkanResourceAccessType resourceAccessType;
VkBufferUsageFlags usage;
uint8_t requireHostVisible;
uint8_t preferDeviceLocal;
SDL_atomic_t referenceCount; /* Tracks command buffer usage */
@ -721,8 +719,8 @@ typedef struct VulkanUniformBufferPool VulkanUniformBufferPool;
typedef struct VulkanUniformBuffer
{
VulkanUniformBufferPool *pool;
VkDeviceSize poolOffset; /* memory offset relative to the pool buffer */
VkDeviceSize offset; /* based on uniform pushes */
VulkanBuffer *vulkanBuffer;
VkDeviceSize offset;
VkDescriptorSet descriptorSet;
} VulkanUniformBuffer;
@ -748,13 +746,10 @@ typedef struct VulkanUniformDescriptorPool
uint32_t availableDescriptorSetCount;
} VulkanUniformDescriptorPool;
/* This is actually just one buffer that we carve slices out of. */
struct VulkanUniformBufferPool
{
VulkanUniformBufferType type;
VulkanUniformDescriptorPool descriptorPool;
VulkanBuffer *buffer;
VkDeviceSize nextAvailableOffset;
SDL_mutex *lock;
VulkanUniformBuffer **availableBuffers;
@ -1781,8 +1776,6 @@ typedef struct VulkanRenderer
VkDescriptorSetLayout vertexUniformDescriptorSetLayout;
VkDescriptorSetLayout fragmentUniformDescriptorSetLayout;
VkDescriptorSetLayout computeUniformDescriptorSetLayout;
VulkanBuffer *dummyBuffer;
VulkanUniformBuffer *dummyVertexUniformBuffer;
VulkanUniformBuffer *dummyFragmentUniformBuffer;
VulkanUniformBuffer *dummyComputeUniformBuffer;
@ -1980,51 +1973,59 @@ static inline uint32_t VULKAN_INTERNAL_BytesPerPixel(VkFormat format)
{
switch (format)
{
case VK_FORMAT_R8_UNORM:
case VK_FORMAT_R8_UINT:
return 1;
case VK_FORMAT_R5G6B5_UNORM_PACK16:
case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
case VK_FORMAT_R16_SFLOAT:
case VK_FORMAT_R8G8_SNORM:
case VK_FORMAT_R8G8_UINT:
case VK_FORMAT_R16_UINT:
case VK_FORMAT_D16_UNORM:
return 2;
case VK_FORMAT_D16_UNORM_S8_UINT:
return 3;
case VK_FORMAT_R8G8B8A8_UNORM:
case VK_FORMAT_B8G8R8A8_UNORM:
case VK_FORMAT_R32_SFLOAT:
case VK_FORMAT_R16G16_UNORM:
case VK_FORMAT_R16G16_SFLOAT:
case VK_FORMAT_R8G8B8A8_SNORM:
case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
case VK_FORMAT_R8G8B8A8_UINT:
case VK_FORMAT_R16G16_UINT:
case VK_FORMAT_D32_SFLOAT:
return 4;
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return 5;
case VK_FORMAT_R16G16B16A16_SFLOAT:
case VK_FORMAT_R16G16B16A16_UNORM:
case VK_FORMAT_R32G32_SFLOAT:
case VK_FORMAT_R16G16B16A16_UINT:
case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
return 8;
case VK_FORMAT_R32G32B32A32_SFLOAT:
case VK_FORMAT_BC2_UNORM_BLOCK:
case VK_FORMAT_BC3_UNORM_BLOCK:
case VK_FORMAT_BC7_UNORM_BLOCK:
case VK_FORMAT_R16G16B16A16_UINT:
return 16;
case VK_FORMAT_R8G8B8A8_UNORM:
case VK_FORMAT_R8G8B8A8_SNORM:
case VK_FORMAT_B8G8R8A8_UNORM:
case VK_FORMAT_B8G8R8A8_SNORM:
case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
case VK_FORMAT_R16G16_UNORM:
case VK_FORMAT_R16G16_SFLOAT:
case VK_FORMAT_R32_SFLOAT:
case VK_FORMAT_D32_SFLOAT:
case VK_FORMAT_R8G8B8A8_UINT:
case VK_FORMAT_R16G16_UINT:
return 4;
case VK_FORMAT_R5G6B5_UNORM_PACK16:
case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
case VK_FORMAT_R8G8_SNORM:
case VK_FORMAT_R16_SFLOAT:
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_R8G8_UINT:
case VK_FORMAT_R16_UINT:
return 2;
case VK_FORMAT_R16G16B16A16_UNORM:
case VK_FORMAT_R32G32_SFLOAT:
case VK_FORMAT_R16G16B16A16_SFLOAT:
case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
case VK_FORMAT_R8_UINT:
return 8;
case VK_FORMAT_R8_UNORM:
return 1;
case VK_FORMAT_D16_UNORM_S8_UINT:
return 3;
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return 5;
default:
Refresh_LogError("Texture format not recognized!");
Refresh_LogError("Invalid texture format!");
return 0;
}
}
static inline uint32_t VULKAN_INTERNAL_TextureBlockSize(
static inline uint32_t VULKAN_INTERNAL_GetTextureBlockSize(
VkFormat format
) {
switch (format)
@ -2039,28 +2040,24 @@ static inline uint32_t VULKAN_INTERNAL_TextureBlockSize(
case VK_FORMAT_R5G6B5_UNORM_PACK16:
case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
case VK_FORMAT_R8G8_SNORM:
case VK_FORMAT_R8G8B8A8_SNORM:
case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
case VK_FORMAT_R16G16_UNORM:
case VK_FORMAT_R16G16B16A16_UNORM:
case VK_FORMAT_R8_UNORM:
case VK_FORMAT_R8G8_SNORM:
case VK_FORMAT_R8G8B8A8_SNORM:
case VK_FORMAT_R16_SFLOAT:
case VK_FORMAT_R16G16_SFLOAT:
case VK_FORMAT_R16G16B16A16_SFLOAT:
case VK_FORMAT_R32_SFLOAT:
case VK_FORMAT_R32G32_SFLOAT:
case VK_FORMAT_R32G32B32A32_SFLOAT:
case VK_FORMAT_R16_SFLOAT:
case VK_FORMAT_R16G16_SFLOAT:
case VK_FORMAT_R16G16B16A16_SFLOAT:
case VK_FORMAT_R8_UINT:
case VK_FORMAT_R8G8_UINT:
case VK_FORMAT_R8G8B8A8_UINT:
case VK_FORMAT_R16_UINT:
case VK_FORMAT_R16G16_UINT:
case VK_FORMAT_R16G16B16A16_UINT:
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_D32_SFLOAT:
case VK_FORMAT_D16_UNORM_S8_UINT:
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return 1;
default:
Refresh_LogError("Unrecognized texture format!");
@ -2073,8 +2070,17 @@ static inline VkDeviceSize VULKAN_INTERNAL_BytesPerImage(
uint32_t height,
VkFormat format
) {
uint32_t blockSize = VULKAN_INTERNAL_TextureBlockSize(format);
return (width * height * VULKAN_INTERNAL_BytesPerPixel(format)) / (blockSize * blockSize);
uint32_t blocksPerRow = width;
uint32_t blocksPerColumn = height;
uint32_t blockSize = VULKAN_INTERNAL_GetTextureBlockSize(format);
if (blockSize > 1)
{
blocksPerRow = (width + blockSize - 1) / blockSize;
blocksPerColumn = (height + blockSize - 1) / blockSize;
}
return blocksPerRow * blocksPerColumn * VULKAN_INTERNAL_BytesPerPixel(format);
}
static inline Refresh_SampleCount VULKAN_INTERNAL_GetMaxMultiSampleCount(
@ -2392,6 +2398,12 @@ static uint8_t VULKAN_INTERNAL_FindMemoryType(
}
}
Refresh_LogWarn(
"Failed to find memory type %X, required %X, ignored %X",
typeFilter,
requiredProperties,
ignoredProperties
);
return 0;
}
@ -2575,6 +2587,7 @@ static uint8_t VULKAN_INTERNAL_AllocateMemory(
SDL_free(allocation);
Refresh_LogWarn("vkAllocateMemory: %s", VkErrorMessages(result));
return 0;
}
@ -2808,6 +2821,7 @@ static uint8_t VULKAN_INTERNAL_BindResourceMemory(
SDL_UnlockMutex(renderer->allocatorLock);
/* Responsibility of the caller to handle being out of memory */
Refresh_LogWarn("Failed to allocate memory!");
return 2;
}
@ -2933,7 +2947,7 @@ static uint8_t VULKAN_INTERNAL_BindMemoryForImage(
{
memoryTypeIndex = 0;
requiredMemoryPropertyFlags = 0;
ignoredMemoryPropertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
ignoredMemoryPropertyFlags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
if (isRenderTarget)
{
@ -2979,33 +2993,23 @@ static uint8_t VULKAN_INTERNAL_BindMemoryForBuffer(
VulkanRenderer* renderer,
VkBuffer buffer,
VkDeviceSize size,
uint8_t requireHostVisible,
uint8_t preferDeviceLocal,
uint8_t dedicatedAllocation,
uint8_t isTransferBuffer,
VulkanMemoryUsedRegion** usedRegion
) {
uint8_t bindResult = 0;
uint32_t memoryTypeIndex = 0;
VkMemoryPropertyFlags requiredMemoryPropertyFlags = 0;
VkMemoryPropertyFlags ignoredMemoryPropertyFlags = 0;
VkMemoryPropertyFlags requiredMemoryPropertyFlags;
VkMemoryPropertyFlags ignoredMemoryPropertyFlags;
VkMemoryRequirements2KHR memoryRequirements =
{
VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR,
NULL
};
if (requireHostVisible)
{
requiredMemoryPropertyFlags =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
}
else
{
ignoredMemoryPropertyFlags =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
}
if (preferDeviceLocal)
{
@ -3026,7 +3030,7 @@ static uint8_t VULKAN_INTERNAL_BindMemoryForBuffer(
renderer,
memoryTypeIndex,
&memoryRequirements,
dedicatedAllocation,
isTransferBuffer,
size,
buffer,
VK_NULL_HANDLE,
@ -3047,17 +3051,9 @@ static uint8_t VULKAN_INTERNAL_BindMemoryForBuffer(
if (bindResult != 1 && preferDeviceLocal)
{
memoryTypeIndex = 0;
requiredMemoryPropertyFlags = 0;
if (requireHostVisible)
{
requiredMemoryPropertyFlags =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
}
ignoredMemoryPropertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
/* Follow-up for the warning logged by FindMemoryType */
if (!renderer->unifiedMemoryWarning)
@ -3078,7 +3074,7 @@ static uint8_t VULKAN_INTERNAL_BindMemoryForBuffer(
renderer,
memoryTypeIndex,
&memoryRequirements,
dedicatedAllocation,
isTransferBuffer,
size,
buffer,
VK_NULL_HANDLE,
@ -3610,8 +3606,6 @@ static void VULKAN_INTERNAL_DestroyCommandPool(
SDL_free(commandBuffer->transferBuffers);
SDL_free(commandBuffer->boundUniformBuffers);
SDL_free(commandBuffer->boundDescriptorSetDatas);
SDL_free(commandBuffer->boundComputeBuffers);
SDL_free(commandBuffer->boundComputeTextures);
SDL_free(commandBuffer->usedBuffers);
SDL_free(commandBuffer->usedTextures);
SDL_free(commandBuffer->usedSamplers);
@ -4115,9 +4109,8 @@ static VulkanBuffer* VULKAN_INTERNAL_CreateBuffer(
VkDeviceSize size,
VulkanResourceAccessType resourceAccessType,
VkBufferUsageFlags usage,
uint8_t requireHostVisible,
uint8_t preferDeviceLocal,
uint8_t dedicatedAllocation
uint8_t isTransferBuffer
) {
VulkanBuffer* buffer;
VkResult vulkanResult;
@ -4129,7 +4122,6 @@ static VulkanBuffer* VULKAN_INTERNAL_CreateBuffer(
buffer->size = size;
buffer->resourceAccessType = resourceAccessType;
buffer->usage = usage;
buffer->requireHostVisible = requireHostVisible;
buffer->preferDeviceLocal = preferDeviceLocal;
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
@ -4153,9 +4145,8 @@ static VulkanBuffer* VULKAN_INTERNAL_CreateBuffer(
renderer,
buffer->buffer,
buffer->size,
buffer->requireHostVisible,
buffer->preferDeviceLocal,
dedicatedAllocation,
isTransferBuffer,
&buffer->usedRegion
);
@ -4212,37 +4203,6 @@ static VulkanUniformBufferPool* VULKAN_INTERNAL_CreateUniformBufferPool(
VulkanUniformBufferType uniformBufferType
) {
VulkanUniformBufferPool* uniformBufferPool = SDL_malloc(sizeof(VulkanUniformBufferPool));
VulkanResourceAccessType resourceAccessType;
if (uniformBufferType == UNIFORM_BUFFER_VERTEX)
{
resourceAccessType = RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER;
}
else if (uniformBufferType == UNIFORM_BUFFER_FRAGMENT)
{
resourceAccessType = RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER;
}
else if (uniformBufferType == UNIFORM_BUFFER_COMPUTE)
{
resourceAccessType = RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER;
}
else
{
Refresh_LogError("Unrecognized uniform buffer type!");
return 0;
}
uniformBufferPool->buffer = VULKAN_INTERNAL_CreateBuffer(
renderer,
UBO_BUFFER_SIZE,
resourceAccessType,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1,
0,
1
);
uniformBufferPool->nextAvailableOffset = 0;
uniformBufferPool->type = uniformBufferType;
uniformBufferPool->lock = SDL_CreateMutex();
@ -4261,7 +4221,6 @@ static VulkanUniformBufferPool* VULKAN_INTERNAL_CreateUniformBufferPool(
}
static void VULKAN_INTERNAL_BindUniformBuffer(
VulkanRenderer *renderer,
VulkanCommandBuffer *commandBuffer,
VulkanUniformBuffer *uniformBuffer
) {
@ -4278,59 +4237,26 @@ static void VULKAN_INTERNAL_BindUniformBuffer(
commandBuffer->boundUniformBufferCount += 1;
}
/* Buffer indirection so we can cleanly defrag */
static VulkanBufferContainer* VULKAN_INTERNAL_CreateBufferContainer(
VulkanRenderer *renderer,
uint32_t sizeInBytes,
VulkanResourceAccessType resourceAccessType,
VkBufferUsageFlags usageFlags,
uint8_t dedicated
) {
VulkanBufferContainer* bufferContainer;
VulkanBuffer* buffer;
/* always set transfer bits so we can defrag */
usageFlags |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
buffer = VULKAN_INTERNAL_CreateBuffer(
renderer,
sizeInBytes,
resourceAccessType,
usageFlags,
0,
1,
dedicated
);
if (buffer == NULL)
{
Refresh_LogError("Failed to create buffer!");
return NULL;
}
bufferContainer = SDL_malloc(sizeof(VulkanBufferContainer));
bufferContainer->vulkanBuffer = buffer;
buffer->container = bufferContainer;
return (VulkanBufferContainer*) bufferContainer;
}
static uint8_t VULKAN_INTERNAL_CreateUniformBuffer(
VulkanRenderer *renderer,
VulkanUniformBufferPool *bufferPool
) {
VulkanResourceAccessType resourceAccessType;
VkDescriptorSetLayout descriptorSetLayout;
if (bufferPool->type == UNIFORM_BUFFER_VERTEX)
{
resourceAccessType = RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER;
descriptorSetLayout = renderer->vertexUniformDescriptorSetLayout;
}
else if (bufferPool->type == UNIFORM_BUFFER_FRAGMENT)
{
resourceAccessType = RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER;
descriptorSetLayout = renderer->fragmentUniformDescriptorSetLayout;
}
else if (bufferPool->type == UNIFORM_BUFFER_COMPUTE)
{
resourceAccessType = RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER;
descriptorSetLayout = renderer->computeUniformDescriptorSetLayout;
}
else
@ -4339,20 +4265,26 @@ static uint8_t VULKAN_INTERNAL_CreateUniformBuffer(
return 0;
}
VulkanUniformBuffer *uniformBuffer = SDL_malloc(sizeof(VulkanUniformBuffer));
uniformBuffer->pool = bufferPool;
uniformBuffer->poolOffset = bufferPool->nextAvailableOffset;
uniformBuffer->offset = 0;
VulkanUniformBuffer *buffer = SDL_malloc(sizeof(VulkanUniformBuffer));
buffer->pool = bufferPool;
bufferPool->nextAvailableOffset += VULKAN_INTERNAL_NextHighestAlignment(UBO_SECTION_SIZE, renderer->minUBOAlignment);
buffer->vulkanBuffer = VULKAN_INTERNAL_CreateBuffer(
renderer,
UBO_BUFFER_SIZE,
resourceAccessType,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
0,
0
);
if (bufferPool->nextAvailableOffset >= UBO_BUFFER_SIZE)
if (buffer->vulkanBuffer == NULL)
{
Refresh_LogError("Uniform buffer overflow!");
SDL_free(uniformBuffer);
Refresh_LogError("Failed to create buffer for uniform buffer!");
return 0;
}
buffer->offset = 0;
/* Allocate a descriptor set for the uniform buffer */
if (bufferPool->descriptorPool.availableDescriptorSetCount == 0)
@ -4362,7 +4294,6 @@ static uint8_t VULKAN_INTERNAL_CreateUniformBuffer(
&bufferPool->descriptorPool
)) {
Refresh_LogError("Failed to add uniform descriptor pool!");
SDL_free(uniformBuffer);
return 0;
}
}
@ -4372,7 +4303,7 @@ static uint8_t VULKAN_INTERNAL_CreateUniformBuffer(
bufferPool->descriptorPool.descriptorPools[bufferPool->descriptorPool.descriptorPoolCount - 1],
descriptorSetLayout,
1,
&uniformBuffer->descriptorSet
&buffer->descriptorSet
)) {
Refresh_LogError("Failed to allocate uniform descriptor set!");
return 0;
@ -4390,7 +4321,7 @@ static uint8_t VULKAN_INTERNAL_CreateUniformBuffer(
);
}
bufferPool->availableBuffers[bufferPool->availableBufferCount] = uniformBuffer;
bufferPool->availableBuffers[bufferPool->availableBufferCount] = buffer;
bufferPool->availableBufferCount += 1;
return 1;
@ -4400,20 +4331,24 @@ static VulkanUniformBuffer* VULKAN_INTERNAL_CreateDummyUniformBuffer(
VulkanRenderer *renderer,
VulkanUniformBufferType uniformBufferType
) {
VulkanResourceAccessType resourceAccessType;
VkDescriptorSetLayout descriptorSetLayout;
VkWriteDescriptorSet writeDescriptorSet;
VkDescriptorBufferInfo descriptorBufferInfo;
if (uniformBufferType == UNIFORM_BUFFER_VERTEX)
{
resourceAccessType = RESOURCE_ACCESS_VERTEX_SHADER_READ_UNIFORM_BUFFER;
descriptorSetLayout = renderer->vertexUniformDescriptorSetLayout;
}
else if (uniformBufferType == UNIFORM_BUFFER_FRAGMENT)
{
resourceAccessType = RESOURCE_ACCESS_FRAGMENT_SHADER_READ_UNIFORM_BUFFER;
descriptorSetLayout = renderer->fragmentUniformDescriptorSetLayout;
}
else if (uniformBufferType == UNIFORM_BUFFER_COMPUTE)
{
resourceAccessType = RESOURCE_ACCESS_COMPUTE_SHADER_READ_UNIFORM_BUFFER;
descriptorSetLayout = renderer->computeUniformDescriptorSetLayout;
}
else
@ -4422,9 +4357,17 @@ static VulkanUniformBuffer* VULKAN_INTERNAL_CreateDummyUniformBuffer(
return NULL;
}
VulkanUniformBuffer *uniformBuffer = SDL_malloc(sizeof(VulkanUniformBuffer));
uniformBuffer->poolOffset = 0;
uniformBuffer->offset = 0;
VulkanUniformBuffer *buffer = SDL_malloc(sizeof(VulkanUniformBuffer));
buffer->vulkanBuffer = VULKAN_INTERNAL_CreateBuffer(
renderer,
UBO_BUFFER_SIZE,
resourceAccessType,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
0,
0
);
buffer->offset = 0;
/* Allocate a descriptor set for the uniform buffer */
@ -4433,12 +4376,12 @@ static VulkanUniformBuffer* VULKAN_INTERNAL_CreateDummyUniformBuffer(
renderer->defaultDescriptorPool,
descriptorSetLayout,
1,
&uniformBuffer->descriptorSet
&buffer->descriptorSet
);
/* Update the descriptor set for the first and last time! */
descriptorBufferInfo.buffer = renderer->dummyBuffer->buffer;
descriptorBufferInfo.buffer = buffer->vulkanBuffer->buffer;
descriptorBufferInfo.offset = 0;
descriptorBufferInfo.range = VK_WHOLE_SIZE;
@ -4448,7 +4391,7 @@ static VulkanUniformBuffer* VULKAN_INTERNAL_CreateDummyUniformBuffer(
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
writeDescriptorSet.dstArrayElement = 0;
writeDescriptorSet.dstBinding = 0;
writeDescriptorSet.dstSet = uniformBuffer->descriptorSet;
writeDescriptorSet.dstSet = buffer->descriptorSet;
writeDescriptorSet.pBufferInfo = &descriptorBufferInfo;
writeDescriptorSet.pImageInfo = NULL;
writeDescriptorSet.pTexelBufferView = NULL;
@ -4461,9 +4404,9 @@ static VulkanUniformBuffer* VULKAN_INTERNAL_CreateDummyUniformBuffer(
NULL
);
uniformBuffer->pool = NULL; /* No pool because this is a dummy */
buffer->pool = NULL; /* No pool because this is a dummy */
return uniformBuffer;
return buffer;
}
static void VULKAN_INTERNAL_DestroyUniformBufferPool(
@ -4485,11 +4428,10 @@ static void VULKAN_INTERNAL_DestroyUniformBufferPool(
/* This is always destroyed after submissions, so all buffers are available */
for (i = 0; i < uniformBufferPool->availableBufferCount; i += 1)
{
VULKAN_INTERNAL_DestroyBuffer(renderer, uniformBufferPool->availableBuffers[i]->vulkanBuffer);
SDL_free(uniformBufferPool->availableBuffers[i]);
}
VULKAN_INTERNAL_DestroyBuffer(renderer, uniformBufferPool->buffer);
SDL_DestroyMutex(uniformBufferPool->lock);
SDL_free(uniformBufferPool->availableBuffers);
SDL_free(uniformBufferPool);
@ -4524,8 +4466,8 @@ static VulkanUniformBuffer* VULKAN_INTERNAL_AcquireUniformBufferFromPool(
/* Update the descriptor set with the correct range */
descriptorBufferInfo.buffer = uniformBuffer->pool->buffer->buffer;
descriptorBufferInfo.offset = uniformBuffer->poolOffset;
descriptorBufferInfo.buffer = uniformBuffer->vulkanBuffer->buffer;
descriptorBufferInfo.offset = 0;
descriptorBufferInfo.range = blockSize;
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
@ -5164,7 +5106,6 @@ static void VULKAN_INTERNAL_EndCommandBuffer(
commandBuffer->computeUniformBuffer != NULL
) {
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
commandBuffer,
commandBuffer->computeUniformBuffer
);
@ -5205,7 +5146,9 @@ static void VULKAN_DestroyDevice(
SDL_free(renderer->submittedCommandBuffers);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyBuffer);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyVertexUniformBuffer->vulkanBuffer);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyFragmentUniformBuffer->vulkanBuffer);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyComputeUniformBuffer->vulkanBuffer);
SDL_free(renderer->dummyVertexUniformBuffer);
SDL_free(renderer->dummyFragmentUniformBuffer);
@ -5428,7 +5371,6 @@ static void VULKAN_DestroyDevice(
SDL_free(renderer->computePipelinesToDestroy);
SDL_free(renderer->shaderModulesToDestroy);
SDL_free(renderer->samplersToDestroy);
SDL_free(renderer->framebuffersToDestroy);
SDL_DestroyMutex(renderer->allocatorLock);
SDL_DestroyMutex(renderer->disposeLock);
@ -7093,6 +7035,8 @@ static Refresh_Buffer* VULKAN_CreateBuffer(
Refresh_BufferUsageFlags usageFlags,
uint32_t sizeInBytes
) {
VulkanBufferContainer* bufferContainer;
VulkanBuffer* buffer;
VulkanResourceAccessType resourceAccessType;
VkBufferUsageFlags vulkanUsageFlags =
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
@ -7126,13 +7070,26 @@ static Refresh_Buffer* VULKAN_CreateBuffer(
resourceAccessType = RESOURCE_ACCESS_INDIRECT_BUFFER;
}
return (Refresh_Buffer*) VULKAN_INTERNAL_CreateBufferContainer(
(VulkanRenderer*) driverData,
buffer = VULKAN_INTERNAL_CreateBuffer(
(VulkanRenderer*)driverData,
sizeInBytes,
resourceAccessType,
vulkanUsageFlags,
1,
0
);
if (buffer == NULL)
{
Refresh_LogError("Failed to create buffer!");
return NULL;
}
bufferContainer = SDL_malloc(sizeof(VulkanBufferContainer));
bufferContainer->vulkanBuffer = buffer;
buffer->container = bufferContainer;
return (Refresh_Buffer*) bufferContainer;
}
/* Setters */
@ -7213,7 +7170,6 @@ static VulkanTransferBuffer* VULKAN_INTERNAL_AcquireTransferBuffer(
RESOURCE_ACCESS_TRANSFER_READ_WRITE,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
1,
1,
1
);
transferBuffer->fromPool = 0;
@ -7253,7 +7209,7 @@ static void VULKAN_SetTextureData(
VulkanTransferBuffer *transferBuffer;
VkBufferImageCopy imageCopy;
uint8_t *stagingBufferPointer;
uint32_t blockSize = VULKAN_INTERNAL_TextureBlockSize(vulkanTexture->format);
uint32_t blockSize = VULKAN_INTERNAL_GetTextureBlockSize(vulkanTexture->format);
uint32_t bufferRowLength;
uint32_t bufferImageHeight;
@ -7832,11 +7788,10 @@ static uint32_t VULKAN_PushVertexShaderUniforms(
if (
vulkanCommandBuffer->vertexUniformBuffer->offset +
graphicsPipeline->vertexUniformBlockSize >=
UBO_SECTION_SIZE
UBO_BUFFER_SIZE
) {
/* We're out of space in this buffer, bind the old one and acquire a new one */
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->vertexUniformBuffer
);
@ -7850,8 +7805,8 @@ static uint32_t VULKAN_PushVertexShaderUniforms(
offset = vulkanCommandBuffer->vertexUniformBuffer->offset;
VULKAN_INTERNAL_SetBufferData(
vulkanCommandBuffer->vertexUniformBuffer->pool->buffer,
vulkanCommandBuffer->vertexUniformBuffer->poolOffset + vulkanCommandBuffer->vertexUniformBuffer->offset,
vulkanCommandBuffer->vertexUniformBuffer->vulkanBuffer,
vulkanCommandBuffer->vertexUniformBuffer->offset,
data,
dataLengthInBytes
);
@ -7876,11 +7831,10 @@ static uint32_t VULKAN_PushFragmentShaderUniforms(
if (
vulkanCommandBuffer->fragmentUniformBuffer->offset +
graphicsPipeline->fragmentUniformBlockSize >=
UBO_SECTION_SIZE
UBO_BUFFER_SIZE
) {
/* We're out of space in this buffer, bind the old one and acquire a new one */
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->fragmentUniformBuffer
);
@ -7894,8 +7848,8 @@ static uint32_t VULKAN_PushFragmentShaderUniforms(
offset = vulkanCommandBuffer->fragmentUniformBuffer->offset;
VULKAN_INTERNAL_SetBufferData(
vulkanCommandBuffer->fragmentUniformBuffer->pool->buffer,
vulkanCommandBuffer->fragmentUniformBuffer->poolOffset + vulkanCommandBuffer->fragmentUniformBuffer->offset,
vulkanCommandBuffer->fragmentUniformBuffer->vulkanBuffer,
vulkanCommandBuffer->fragmentUniformBuffer->offset,
data,
dataLengthInBytes
);
@ -7919,11 +7873,10 @@ static uint32_t VULKAN_PushComputeShaderUniforms(
if (
vulkanCommandBuffer->computeUniformBuffer->offset +
computePipeline->uniformBlockSize >=
UBO_SECTION_SIZE
UBO_BUFFER_SIZE
) {
/* We're out of space in this buffer, bind the old one and acquire a new one */
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->computeUniformBuffer
);
@ -7937,8 +7890,8 @@ static uint32_t VULKAN_PushComputeShaderUniforms(
offset = vulkanCommandBuffer->computeUniformBuffer->offset;
VULKAN_INTERNAL_SetBufferData(
vulkanCommandBuffer->computeUniformBuffer->pool->buffer,
vulkanCommandBuffer->computeUniformBuffer->poolOffset + vulkanCommandBuffer->computeUniformBuffer->offset,
vulkanCommandBuffer->computeUniformBuffer->vulkanBuffer,
vulkanCommandBuffer->computeUniformBuffer->offset,
data,
dataLengthInBytes
);
@ -8863,7 +8816,7 @@ static void VULKAN_BeginRenderPass(
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
vulkanCommandBuffer->commandBuffer,
RESOURCE_ACCESS_COLOR_ATTACHMENT_READ_WRITE,
RESOURCE_ACCESS_COLOR_ATTACHMENT_WRITE,
VK_IMAGE_ASPECT_COLOR_BIT,
0,
texture->layerCount,
@ -8896,7 +8849,7 @@ static void VULKAN_BeginRenderPass(
VULKAN_INTERNAL_ImageMemoryBarrier(
renderer,
vulkanCommandBuffer->commandBuffer,
RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_WRITE,
RESOURCE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE,
depthAspectFlags,
0,
texture->layerCount,
@ -9018,7 +8971,6 @@ static void VULKAN_EndRenderPass(
vulkanCommandBuffer->vertexUniformBuffer != NULL
) {
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->vertexUniformBuffer
);
@ -9029,7 +8981,6 @@ static void VULKAN_EndRenderPass(
vulkanCommandBuffer->fragmentUniformBuffer != NULL
) {
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->fragmentUniformBuffer
);
@ -9115,7 +9066,6 @@ static void VULKAN_BindGraphicsPipeline(
vulkanCommandBuffer->vertexUniformBuffer != NULL
) {
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->vertexUniformBuffer
);
@ -9138,7 +9088,6 @@ static void VULKAN_BindGraphicsPipeline(
vulkanCommandBuffer->fragmentUniformBuffer != NULL
) {
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->fragmentUniformBuffer
);
@ -9270,7 +9219,6 @@ static void VULKAN_BindComputePipeline(
vulkanCommandBuffer->computeUniformBuffer != NULL
) {
VULKAN_INTERNAL_BindUniformBuffer(
renderer,
vulkanCommandBuffer,
vulkanCommandBuffer->computeUniformBuffer
);
@ -10566,6 +10514,7 @@ static uint8_t VULKAN_INTERNAL_DefragmentMemory(
VkBufferCopy bufferCopy;
VkImageCopy *imageCopyRegions;
VulkanCommandBuffer *commandBuffer;
VkCommandBufferBeginInfo beginInfo;
uint32_t i, level;
VulkanResourceAccessType copyResourceAccessType = RESOURCE_ACCESS_NONE;
VulkanResourceAccessType originalResourceAccessType;
@ -10575,6 +10524,11 @@ static uint8_t VULKAN_INTERNAL_DefragmentMemory(
renderer->needDefrag = 0;
renderer->defragInProgress = 1;
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.pNext = NULL;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
beginInfo.pInheritanceInfo = NULL;
commandBuffer = (VulkanCommandBuffer*) VULKAN_AcquireCommandBuffer((Refresh_Renderer *) renderer);
if (VULKAN_INTERNAL_FindAllocationToDefragment(
@ -10607,7 +10561,6 @@ static uint8_t VULKAN_INTERNAL_DefragmentMemory(
currentRegion->vulkanBuffer->size,
RESOURCE_ACCESS_NONE,
currentRegion->vulkanBuffer->usage,
currentRegion->vulkanBuffer->requireHostVisible,
currentRegion->vulkanBuffer->preferDeviceLocal,
0
);
@ -11580,9 +11533,6 @@ static uint8_t VULKAN_INTERNAL_CreateLogicalDevice(
static void VULKAN_INTERNAL_LoadEntryPoints(void)
{
/* Required for MoltenVK support */
SDL_setenv("MVK_CONFIG_FULL_IMAGE_VIEW_SWIZZLE", "1", 1);
/* Load Vulkan entry points */
if (SDL_Vulkan_LoadLibrary(NULL) < 0)
{
@ -11755,6 +11705,14 @@ static Refresh_Device* VULKAN_CreateDevice(
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
@ -12010,16 +11968,6 @@ static Refresh_Device* VULKAN_CreateDevice(
/* Dummy Uniform Buffers */
renderer->dummyBuffer = VULKAN_INTERNAL_CreateBuffer(
renderer,
16,
RESOURCE_ACCESS_GENERAL,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
0,
0,
1
);
renderer->dummyVertexUniformBuffer = VULKAN_INTERNAL_CreateDummyUniformBuffer(
renderer,
UNIFORM_BUFFER_VERTEX
@ -12133,7 +12081,6 @@ static Refresh_Device* VULKAN_CreateDevice(
RESOURCE_ACCESS_TRANSFER_READ_WRITE,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
1,
1,
1
);