mirror of
https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator
synced 2024-11-05 04:10:06 +00:00
Submitting version 2.0.0-alpha.1 to separate branch.
This commit is contained in:
parent
0a41b12c10
commit
819860e1f0
@ -90,6 +90,7 @@ static VkDescriptorSet g_hDescriptorSet; // Automatically destroyed with m_Descr
|
|||||||
static VkSampler g_hSampler;
|
static VkSampler g_hSampler;
|
||||||
static VkFormat g_DepthFormat;
|
static VkFormat g_DepthFormat;
|
||||||
static VkImage g_hDepthImage;
|
static VkImage g_hDepthImage;
|
||||||
|
static VmaAllocation g_hDepthImageAlloc;
|
||||||
static VkImageView g_hDepthImageView;
|
static VkImageView g_hDepthImageView;
|
||||||
|
|
||||||
static VkSurfaceCapabilitiesKHR g_SurfaceCapabilities;
|
static VkSurfaceCapabilitiesKHR g_SurfaceCapabilities;
|
||||||
@ -111,11 +112,14 @@ static VkRenderPass g_hRenderPass;
|
|||||||
static VkPipeline g_hPipeline;
|
static VkPipeline g_hPipeline;
|
||||||
|
|
||||||
static VkBuffer g_hVertexBuffer;
|
static VkBuffer g_hVertexBuffer;
|
||||||
|
static VmaAllocation g_hVertexBufferAlloc;
|
||||||
static VkBuffer g_hIndexBuffer;
|
static VkBuffer g_hIndexBuffer;
|
||||||
|
static VmaAllocation g_hIndexBufferAlloc;
|
||||||
static uint32_t g_VertexCount;
|
static uint32_t g_VertexCount;
|
||||||
static uint32_t g_IndexCount;
|
static uint32_t g_IndexCount;
|
||||||
|
|
||||||
static VkImage g_hTextureImage;
|
static VkImage g_hTextureImage;
|
||||||
|
static VmaAllocation g_hTextureImageAlloc;
|
||||||
static VkImageView g_hTextureImageView;
|
static VkImageView g_hTextureImageView;
|
||||||
|
|
||||||
static void BeginSingleTimeCommands()
|
static void BeginSingleTimeCommands()
|
||||||
@ -287,20 +291,24 @@ static void CreateMesh()
|
|||||||
vbInfo.size = vertexBufferSize;
|
vbInfo.size = vertexBufferSize;
|
||||||
vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||||
vbInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
vbInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
VmaMemoryRequirements vbMemReq = {};
|
|
||||||
vbMemReq.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
|
||||||
VkMappedMemoryRange stagingVertexBufferMem;
|
|
||||||
VkBuffer stagingVertexBuffer = VK_NULL_HANDLE;
|
|
||||||
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbMemReq, &stagingVertexBuffer, &stagingVertexBufferMem, nullptr) );
|
|
||||||
|
|
||||||
void* pVbData = nullptr;
|
VmaMemoryRequirements vbMemReq = {};
|
||||||
ERR_GUARD_VULKAN( vmaMapMemory(g_hAllocator, &stagingVertexBufferMem, &pVbData) );
|
vbMemReq.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
||||||
memcpy(pVbData, vertices, vertexBufferSize);
|
vbMemReq.flags = VMA_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT;
|
||||||
vmaUnmapMemory(g_hAllocator, &stagingVertexBufferMem);
|
|
||||||
|
VkBuffer stagingVertexBuffer = VK_NULL_HANDLE;
|
||||||
|
VmaAllocation stagingVertexBufferAlloc = VK_NULL_HANDLE;
|
||||||
|
VmaAllocationInfo stagingVertexBufferAllocInfo = {};
|
||||||
|
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbMemReq, &stagingVertexBuffer, &stagingVertexBufferAlloc, &stagingVertexBufferAllocInfo) );
|
||||||
|
|
||||||
|
memcpy(stagingVertexBufferAllocInfo.pMappedData, vertices, vertexBufferSize);
|
||||||
|
|
||||||
|
// No need to flush stagingVertexBuffer memory because CPU_ONLY memory is always HOST_COHERENT.
|
||||||
|
|
||||||
vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
||||||
vbMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
vbMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||||
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbMemReq, &g_hVertexBuffer, nullptr, nullptr) );
|
vbMemReq.flags = 0;
|
||||||
|
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbMemReq, &g_hVertexBuffer, &g_hVertexBufferAlloc, nullptr) );
|
||||||
|
|
||||||
// Create index buffer
|
// Create index buffer
|
||||||
|
|
||||||
@ -308,20 +316,24 @@ static void CreateMesh()
|
|||||||
ibInfo.size = indexBufferSize;
|
ibInfo.size = indexBufferSize;
|
||||||
ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||||
ibInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
ibInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
VmaMemoryRequirements ibMemReq = {};
|
VmaMemoryRequirements ibMemReq = {};
|
||||||
ibMemReq.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
ibMemReq.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
||||||
VkMappedMemoryRange stagingIndexBufferMem;
|
ibMemReq.flags = VMA_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT;
|
||||||
|
|
||||||
VkBuffer stagingIndexBuffer = VK_NULL_HANDLE;
|
VkBuffer stagingIndexBuffer = VK_NULL_HANDLE;
|
||||||
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibMemReq, &stagingIndexBuffer, &stagingIndexBufferMem, nullptr) );
|
VmaAllocation stagingIndexBufferAlloc = VK_NULL_HANDLE;
|
||||||
|
VmaAllocationInfo stagingIndexBufferAllocInfo = {};
|
||||||
|
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibMemReq, &stagingIndexBuffer, &stagingIndexBufferAlloc, &stagingIndexBufferAllocInfo) );
|
||||||
|
|
||||||
void* pIbData = nullptr;
|
memcpy(stagingIndexBufferAllocInfo.pMappedData, indices, indexBufferSize);
|
||||||
ERR_GUARD_VULKAN( vmaMapMemory(g_hAllocator, &stagingIndexBufferMem, &pIbData) );
|
|
||||||
memcpy(pIbData, indices, indexBufferSize);
|
// No need to flush stagingIndexBuffer memory because CPU_ONLY memory is always HOST_COHERENT.
|
||||||
vmaUnmapMemory(g_hAllocator, &stagingIndexBufferMem);
|
|
||||||
|
|
||||||
ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
||||||
ibMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
ibMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||||
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibMemReq, &g_hIndexBuffer, nullptr, nullptr) );
|
ibMemReq.flags = 0;
|
||||||
|
ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibMemReq, &g_hIndexBuffer, &g_hIndexBufferAlloc, nullptr) );
|
||||||
|
|
||||||
// Copy buffers
|
// Copy buffers
|
||||||
|
|
||||||
@ -341,8 +353,8 @@ static void CreateMesh()
|
|||||||
|
|
||||||
EndSingleTimeCommands();
|
EndSingleTimeCommands();
|
||||||
|
|
||||||
vmaDestroyBuffer(g_hAllocator, stagingIndexBuffer);
|
vmaDestroyBuffer(g_hAllocator, stagingIndexBuffer, stagingIndexBufferAlloc);
|
||||||
vmaDestroyBuffer(g_hAllocator, stagingVertexBuffer);
|
vmaDestroyBuffer(g_hAllocator, stagingVertexBuffer, stagingVertexBufferAlloc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void CopyImage(VkImage srcImage, VkImage dstImage, uint32_t width, uint32_t height, uint32_t mipLevel)
|
static void CopyImage(VkImage srcImage, VkImage dstImage, uint32_t width, uint32_t height, uint32_t mipLevel)
|
||||||
@ -468,14 +480,15 @@ static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
|||||||
stagingImageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
stagingImageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
stagingImageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
stagingImageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
stagingImageInfo.flags = 0;
|
stagingImageInfo.flags = 0;
|
||||||
|
|
||||||
VmaMemoryRequirements stagingImageMemReq = {};
|
VmaMemoryRequirements stagingImageMemReq = {};
|
||||||
stagingImageMemReq.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
stagingImageMemReq.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
||||||
|
stagingImageMemReq.flags = VMA_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT;
|
||||||
|
|
||||||
VkImage stagingImage = VK_NULL_HANDLE;
|
VkImage stagingImage = VK_NULL_HANDLE;
|
||||||
VkMappedMemoryRange stagingImageMem;
|
VmaAllocation stagingImageAlloc = VK_NULL_HANDLE;
|
||||||
ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &stagingImageInfo, &stagingImageMemReq, &stagingImage, &stagingImageMem, nullptr) );
|
VmaAllocationInfo stagingImageAllocInfo = {};
|
||||||
|
ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &stagingImageInfo, &stagingImageMemReq, &stagingImage, &stagingImageAlloc, &stagingImageAllocInfo) );
|
||||||
char* pImageData = nullptr;
|
|
||||||
ERR_GUARD_VULKAN( vmaMapMemory(g_hAllocator, &stagingImageMem, (void**)&pImageData) );
|
|
||||||
|
|
||||||
VkImageSubresource imageSubresource = {};
|
VkImageSubresource imageSubresource = {};
|
||||||
imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
@ -485,7 +498,7 @@ static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
|||||||
VkSubresourceLayout imageLayout = {};
|
VkSubresourceLayout imageLayout = {};
|
||||||
vkGetImageSubresourceLayout(g_hDevice, stagingImage, &imageSubresource, &imageLayout);
|
vkGetImageSubresourceLayout(g_hDevice, stagingImage, &imageSubresource, &imageLayout);
|
||||||
|
|
||||||
char* const pMipLevelData = pImageData + imageLayout.offset;
|
char* const pMipLevelData = (char*)stagingImageAllocInfo.pMappedData + imageLayout.offset;
|
||||||
uint8_t* pRowData = (uint8_t*)pMipLevelData;
|
uint8_t* pRowData = (uint8_t*)pMipLevelData;
|
||||||
for(uint32_t y = 0; y < sizeY; ++y)
|
for(uint32_t y = 0; y < sizeY; ++y)
|
||||||
{
|
{
|
||||||
@ -502,7 +515,7 @@ static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
|||||||
pRowData += imageLayout.rowPitch;
|
pRowData += imageLayout.rowPitch;
|
||||||
}
|
}
|
||||||
|
|
||||||
vmaUnmapMemory(g_hAllocator, &stagingImageMem);
|
// No need to flush stagingImage memory because CPU_ONLY memory is always HOST_COHERENT.
|
||||||
|
|
||||||
VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
||||||
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||||
@ -520,7 +533,7 @@ static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
|||||||
imageInfo.flags = 0;
|
imageInfo.flags = 0;
|
||||||
VmaMemoryRequirements imageMemReq = {};
|
VmaMemoryRequirements imageMemReq = {};
|
||||||
imageMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
imageMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||||
ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &imageInfo, &imageMemReq, &g_hTextureImage, nullptr, nullptr) );
|
ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &imageInfo, &imageMemReq, &g_hTextureImage, &g_hTextureImageAlloc, nullptr) );
|
||||||
|
|
||||||
TransitionImageLayout(
|
TransitionImageLayout(
|
||||||
stagingImage,
|
stagingImage,
|
||||||
@ -542,7 +555,7 @@ static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
|||||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
|
||||||
vmaDestroyImage(g_hAllocator, stagingImage);
|
vmaDestroyImage(g_hAllocator, stagingImage, stagingImageAlloc);
|
||||||
|
|
||||||
// Create ImageView
|
// Create ImageView
|
||||||
|
|
||||||
@ -756,7 +769,7 @@ static void CreateSwapchain()
|
|||||||
VmaMemoryRequirements depthImageMemReq = {};
|
VmaMemoryRequirements depthImageMemReq = {};
|
||||||
depthImageMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
depthImageMemReq.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||||
|
|
||||||
ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &depthImageInfo, &depthImageMemReq, &g_hDepthImage, nullptr, nullptr) );
|
ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &depthImageInfo, &depthImageMemReq, &g_hDepthImage, &g_hDepthImageAlloc, nullptr) );
|
||||||
|
|
||||||
VkImageViewCreateInfo depthImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
VkImageViewCreateInfo depthImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
||||||
depthImageViewInfo.image = g_hDepthImage;
|
depthImageViewInfo.image = g_hDepthImage;
|
||||||
@ -1084,7 +1097,7 @@ static void DestroySwapchain(bool destroyActualSwapchain)
|
|||||||
}
|
}
|
||||||
if(g_hDepthImage != VK_NULL_HANDLE)
|
if(g_hDepthImage != VK_NULL_HANDLE)
|
||||||
{
|
{
|
||||||
vmaDestroyImage(g_hAllocator, g_hDepthImage);
|
vmaDestroyImage(g_hAllocator, g_hDepthImage, g_hDepthImageAlloc);
|
||||||
g_hDepthImage = VK_NULL_HANDLE;
|
g_hDepthImage = VK_NULL_HANDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1391,18 +1404,18 @@ static void FinalizeApplication()
|
|||||||
}
|
}
|
||||||
if(g_hTextureImage != VK_NULL_HANDLE)
|
if(g_hTextureImage != VK_NULL_HANDLE)
|
||||||
{
|
{
|
||||||
vmaDestroyImage(g_hAllocator, g_hTextureImage);
|
vmaDestroyImage(g_hAllocator, g_hTextureImage, g_hTextureImageAlloc);
|
||||||
g_hTextureImage = VK_NULL_HANDLE;
|
g_hTextureImage = VK_NULL_HANDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(g_hIndexBuffer != VK_NULL_HANDLE)
|
if(g_hIndexBuffer != VK_NULL_HANDLE)
|
||||||
{
|
{
|
||||||
vmaDestroyBuffer(g_hAllocator, g_hIndexBuffer);
|
vmaDestroyBuffer(g_hAllocator, g_hIndexBuffer, g_hIndexBufferAlloc);
|
||||||
g_hIndexBuffer = VK_NULL_HANDLE;
|
g_hIndexBuffer = VK_NULL_HANDLE;
|
||||||
}
|
}
|
||||||
if(g_hVertexBuffer != VK_NULL_HANDLE)
|
if(g_hVertexBuffer != VK_NULL_HANDLE)
|
||||||
{
|
{
|
||||||
vmaDestroyBuffer(g_hAllocator, g_hVertexBuffer);
|
vmaDestroyBuffer(g_hAllocator, g_hVertexBuffer, g_hVertexBufferAlloc);
|
||||||
g_hVertexBuffer = VK_NULL_HANDLE;
|
g_hVertexBuffer = VK_NULL_HANDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2191
src/vk_mem_alloc.h
2191
src/vk_mem_alloc.h
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user