framework update 4/29/2020

This commit is contained in:
Christoph Kubisch 2020-04-29 13:59:03 +02:00
parent 21fc655237
commit 60103dd1ce
62 changed files with 2931 additions and 2743 deletions

View file

@ -117,13 +117,13 @@ Replace the definition of buffers and textures and include the right allocator.
~~~~ C++
#if defined(ALLOC_DEDICATED)
#include "nvvkpp/allocator_dedicated_vkpp.hpp"
using nvvkBuffer = nvvkpp::BufferDedicated;
using nvvkTexture = nvvkpp::TextureDedicated;
#include "nvvk/allocator_dedicated_vk.hpp"
using nvvkBuffer = nvvk::BufferDedicated;
using nvvkTexture = nvvk::TextureDedicated;
#elif defined(ALLOC_DMA)
#include "nvvkpp/allocator_dma_vkpp.hpp"
using nvvkBuffer = nvvkpp::BufferDma;
using nvvkTexture = nvvkpp::TextureDma;
#include "nvvk/allocator_dma_vk.hpp"
using nvvkBuffer = nvvk::BufferDma;
using nvvkTexture = nvvk::TextureDma;
#endif
~~~~
@ -131,10 +131,11 @@ And do the same for the allocator
~~~~ C++
#if defined(ALLOC_DEDICATED)
nvvkpp::AllocatorDedicated m_alloc; // Allocator for buffer, images, acceleration structures
nvvk::AllocatorDedicated m_alloc; // Allocator for buffer, images, acceleration structures
#elif defined(ALLOC_DMA)
nvvkpp::AllocatorDma m_alloc; // Allocator for buffer, images, acceleration structures
nvvk::DeviceMemoryAllocator m_dmaAllocator;
nvvk::AllocatorDma m_alloc; // Allocator for buffer, images, acceleration structures
nvvk::DeviceMemoryAllocator m_memAllocator;
nvvk::StagingMemoryManagerDma m_staging;
#endif
~~~~
@ -148,34 +149,24 @@ DMA needs to be initialized, which will be done in the `setup()` function:
#if defined(ALLOC_DEDICATED)
m_alloc.init(device, physicalDevice);
#elif defined(ALLOC_DMA)
m_dmaAllocator.init(device, physicalDevice);
m_alloc.init(device, &m_dmaAllocator);
m_memAllocator.init(device, physicalDevice);
m_memAllocator.setAllocateFlags(VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR, true);
m_staging.init(m_memAllocator);
m_alloc.init(device, m_memAllocator, m_staging);
#endif
~~~~
When using DMA, memory buffer mapping is done through the DMA interface (instead of the VKDevice). Therefore, change the lines at the end of `updateUniformBuffer()` to
When using DMA, memory buffer mapping is done through the DMA interface (instead of the VKDevice).
Therefore, change the lines at the end of `updateUniformBuffer()` to use the common allocator interface.
~~~~ C++
#if defined(ALLOC_DEDICATED)
void* data = m_device.mapMemory(m_cameraMat.allocation, 0, sizeof(CameraMatrices));
memcpy(data, &ubo, sizeof(ubo));
m_device.unmapMemory(m_cameraMat.allocation);
#elif defined(ALLOC_DMA)
void* data = m_dmaAllocator.map(m_cameraMat.allocation);
memcpy(data, &ubo, sizeof(ubo));
m_dmaAllocator.unmap(m_cameraMat.allocation);
#endif
void* data = m_alloc.map(m_cameraMat);
memcpy(data, &ubo, sizeof(ubo));
m_alloc.unmap(m_cameraMat);
~~~~
The RaytracerBuilder was made to allow various allocators, but we still need to pass the right one in its setup function. Change the last line of `initRayTracing()` to
The RaytracerBuilder was made to allow various allocators, therefore nothing to change in the call to `m_rtBuilder.setup()`
~~~~ C++
#if defined(ALLOC_DEDICATED)
m_rtBuilder.setup(m_device, m_physicalDevice, m_graphicsQueueIndex);
#elif defined(ALLOC_DMA)
m_rtBuilder.setup(m_device, m_dmaAllocator, m_graphicsQueueIndex);
#endif
~~~~
## Destruction
@ -204,7 +195,7 @@ We can also modify the code to use the [Vulkan Memory Allocator](https://github.
Download [vk_mem_alloc.h](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/blob/master/src/vk_mem_alloc.h) from GitHub and add this to the `shared_sources` folder.
There is already a variation of the allocator for VMA, which is located under [nvpro-samples](https://github.com/nvpro-samples/shared_sources/tree/master/nvvkpp). This allocator has the same simple interface as the `AllocatorDedicated` class in `allocator_dedicated_vkpp.hpp`, but will use VMA for memory management.
There is already a variation of the allocator for VMA, which is located under [nvpro-samples](https://github.com/nvpro-samples/shared_sources/tree/master/nvvk). This allocator has the same simple interface as the `AllocatorDedicated` class in `allocator_dedicated_vkpp.hpp`, but will use VMA for memory management.
VMA might use dedicated memory, which we do, so you need to add the following extension to the
creation of the context in `main.cpp`.
@ -223,15 +214,16 @@ Follow the changes done before and add the following
~~~~ C++
#elif defined(ALLOC_VMA)
#include "nvvkpp/allocator_vma_vkpp.hpp"
using nvvkBuffer = nvvkpp::BufferVma;
using nvvkTexture = nvvkpp::TextureVma;
#include "nvvk/allocator_vma_vk.hpp"
using nvvkBuffer = nvvk::BufferVma;
using nvvkTexture = nvvk::TextureVma;
~~~~
~~~~ C++
#elif defined(ALLOC_VMA)
nvvkpp::AllocatorVma m_alloc; // Allocator for buffer, images, acceleration structures
VmaAllocator m_vmaAllocator;
nvvk::AllocatorVma m_alloc; // Allocator for buffer, images, acceleration structures
nvvk::StagingMemoryManagerVma m_staging;
VmaAllocator m_memAllocator;
~~~~
@ -247,21 +239,13 @@ In `setup()`
~~~~ C++
#elif defined(ALLOC_VMA)
VmaAllocatorCreateInfo allocatorInfo = {};
allocatorInfo.instance = instance;
allocatorInfo.physicalDevice = physicalDevice;
allocatorInfo.device = device;
allocatorInfo.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
vmaCreateAllocator(&allocatorInfo, &m_vmaAllocator);
m_alloc.init(device, m_vmaAllocator);
~~~~
In `updateUniformBuffer()`
~~~~ C++
#elif defined(ALLOC_VMA)
void* data;
vmaMapMemory(m_vmaAllocator, m_cameraMat.allocation, &data);
memcpy(data, &ubo, sizeof(ubo));
vmaUnmapMemory(m_vmaAllocator, m_cameraMat.allocation);
vmaCreateAllocator(&allocatorInfo, &m_memAllocator);
m_staging.init(device, physicalDevice, m_memAllocator);
m_alloc.init(device, m_memAllocator, m_staging);
~~~~
In `destroyResources()`
@ -271,25 +255,6 @@ In `destroyResources()`
vmaDestroyAllocator(m_vmaAllocator);
~~~~
In `initRayTracing()`
~~~~ C++
#elif defined(ALLOC_VMA)
m_rtBuilder.setup(m_device, m_vmaAllocator, m_graphicsQueueIndex);
~~~~
Additionally, VMA has its own usage flags, so since `VMA_MEMORY_USAGE_CPU_TO_GPU` maps to `vkMP::eHostVisible` and `vkMP::eHostCoherent`, change the call to `m_alloc.createBuffer` in `HelloVulkan::createUniformBuffer()` to
~~~~ C++
m_cameraMat = m_alloc.createBuffer(sizeof(CameraMatrices), vkBU::eUniformBuffer,
#if defined(ALLOC_DEDICATED) || defined(ALLOC_DMA)
vkMP::eHostVisible | vkMP::eHostCoherent
#elif defined(ALLOC_VMA)
VMA_MEMORY_USAGE_CPU_TO_GPU
#endif
);
~~~~
# Final Code