yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
3.0 KiB105 linesraw
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{
17    m_fenceIndex++;
18    if (m_fenceIndex >= m_fences.getCount())
19    {
20        m_fences.setCount(m_fenceIndex + 1);
21        VkFenceCreateInfo fenceCreateInfo = {};
22        fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
23        fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
24        m_device->m_api.vkCreateFence(
25            m_device->m_api.m_device,
26            &fenceCreateInfo,
27            nullptr,
28            &m_fences[m_fenceIndex]);
29    }
30}
31
32Result TransientResourceHeapImpl::init(const ITransientResourceHeap::Desc& desc, DeviceImpl* device)
33{
34    Super::init(
35        desc,
36        (uint32_t)device->m_api.m_deviceProperties.limits.minUniformBufferOffsetAlignment,
37        device);
38
39    m_descSetAllocator.m_api = &device->m_api;
40
41    VkCommandPoolCreateInfo poolCreateInfo = {};
42    poolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
43    poolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
44    poolCreateInfo.queueFamilyIndex =
45        device->getQueueFamilyIndex(ICommandQueue::QueueType::Graphics);
46    device->m_api
47        .vkCreateCommandPool(device->m_api.m_device, &poolCreateInfo, nullptr, &m_commandPool);
48
49    advanceFence();
50    return SLANG_OK;
51}
52
53TransientResourceHeapImpl::~TransientResourceHeapImpl()
54{
55    m_commandBufferPool = decltype(m_commandBufferPool)();
56    m_device->m_api.vkDestroyCommandPool(m_device->m_api.m_device, m_commandPool, nullptr);
57    for (auto fence : m_fences)
58    {
59        m_device->m_api.vkDestroyFence(m_device->m_api.m_device, fence, nullptr);
60    }
61    m_descSetAllocator.close();
62}
63
64Result TransientResourceHeapImpl::createCommandBuffer(ICommandBuffer** outCmdBuffer)
65{
66    if (m_commandBufferAllocId < (uint32_t)m_commandBufferPool.getCount())
67    {
68        auto result = m_commandBufferPool[m_commandBufferAllocId];
69        result->m_transientHeap.establishStrongReference();
70        result->beginCommandBuffer();
71        m_commandBufferAllocId++;
72        returnComPtr(outCmdBuffer, result);
73        return SLANG_OK;
74    }
75
76    RefPtr<CommandBufferImpl> commandBuffer = new CommandBufferImpl();
77    SLANG_RETURN_ON_FAIL(commandBuffer->init(m_device, m_commandPool, this));
78    m_commandBufferPool.add(commandBuffer);
79    m_commandBufferAllocId++;
80    returnComPtr(outCmdBuffer, commandBuffer);
81    return SLANG_OK;
82}
83
84Result TransientResourceHeapImpl::synchronizeAndReset()
85{
86    m_commandBufferAllocId = 0;
87    auto& api = m_device->m_api;
88    if (api.vkWaitForFences(
89            api.m_device,
90            (uint32_t)m_fences.getCount(),
91            m_fences.getBuffer(),
92            1,
93            UINT64_MAX) != VK_SUCCESS)
94    {
95        return SLANG_FAIL;
96    }
97    api.vkResetCommandPool(api.m_device, m_commandPool, 0);
98    m_descSetAllocator.reset();
99    m_fenceIndex = 0;
100    Super::reset();
101    return SLANG_OK;
102}
103
104} // namespace vk
105} // namespace gfx