yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// vk-transient-heap.cpp 2#include "vk-transient-heap.h" 3 4#include "vk-device.h" 5#include "vk-util.h" 6 7namespace gfx 8{ 9 10using namespace Slang ; 11 12namespace vk 13{ 14 15void TransientResourceHeapImpl ::advanceFence () 16{ 17m_fenceIndex ++ ; 18if (m_fenceIndex >=m_fences .getCount ()) 19 { 20m_fences .setCount (m_fenceIndex + 1 ); 21VkFenceCreateInfo fenceCreateInfo = {}; 22fenceCreateInfo .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO ; 23fenceCreateInfo .flags = VK_FENCE_CREATE_SIGNALED_BIT ; 24m_device -> m_api .vkCreateFence ( 25m_device -> m_api .m_device , 26& fenceCreateInfo , 27nullptr , 28& m_fences [m_fenceIndex ]); 29 } 30} 31 32Result TransientResourceHeapImpl ::init (const ITransientResourceHeap ::Desc & desc ,DeviceImpl * device ) 33{ 34Super ::init ( 35desc , 36 (uint32_t )device -> m_api .m_deviceProperties .limits .minUniformBufferOffsetAlignment , 37device ); 38 39m_descSetAllocator .m_api = & device -> m_api ; 40 41VkCommandPoolCreateInfo poolCreateInfo = {}; 42poolCreateInfo .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO ; 43poolCreateInfo .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT ; 44poolCreateInfo .queueFamilyIndex = 45device -> getQueueFamilyIndex (ICommandQueue ::QueueType ::Graphics ); 46device -> m_api 47 .vkCreateCommandPool (device -> m_api .m_device ,& poolCreateInfo ,nullptr ,& m_commandPool ); 48 49advanceFence (); 50return SLANG_OK ; 51} 52 53TransientResourceHeapImpl ::~TransientResourceHeapImpl () 54{ 55m_commandBufferPool = decltype(m_commandBufferPool )(); 56m_device -> m_api .vkDestroyCommandPool (m_device -> m_api .m_device ,m_commandPool ,nullptr ); 57for (auto fence :m_fences ) 58 { 59m_device -> m_api .vkDestroyFence (m_device -> m_api .m_device ,fence ,nullptr ); 60 } 61m_descSetAllocator .close (); 62} 63 64Result TransientResourceHeapImpl ::createCommandBuffer (ICommandBuffer ** outCmdBuffer ) 65{ 66if (m_commandBufferAllocId < (uint32_t )m_commandBufferPool .getCount ()) 67 { 68auto result = m_commandBufferPool [m_commandBufferAllocId ]; 69result -> m_transientHeap .establishStrongReference (); 70result -> beginCommandBuffer (); 71m_commandBufferAllocId ++ ; 72returnComPtr (outCmdBuffer ,result ); 73return SLANG_OK ; 74 } 75 76RefPtr < CommandBufferImpl > commandBuffer = new CommandBufferImpl (); 77SLANG_RETURN_ON_FAIL (commandBuffer -> init (m_device ,m_commandPool ,this )); 78m_commandBufferPool .add (commandBuffer ); 79m_commandBufferAllocId ++ ; 80returnComPtr (outCmdBuffer ,commandBuffer ); 81return SLANG_OK ; 82} 83 84Result TransientResourceHeapImpl ::synchronizeAndReset () 85{ 86m_commandBufferAllocId = 0 ; 87auto & api = m_device -> m_api ; 88if (api .vkWaitForFences ( 89api .m_device , 90 (uint32_t )m_fences .getCount (), 91m_fences .getBuffer (), 921 , 93UINT64_MAX )!= VK_SUCCESS ) 94 { 95return SLANG_FAIL ; 96 } 97api .vkResetCommandPool (api .m_device ,m_commandPool ,0 ); 98m_descSetAllocator .reset (); 99m_fenceIndex = 0 ; 100Super ::reset (); 101return SLANG_OK ; 102} 103 104}// namespace vk 105}// namespace gfx