yum-mirror/slang

Making it easier to work with shaders

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

Ellie HermaszewskaMove switch statement bodies to their own lines (#5493)b118451e3

master
2.4 KiB85 linesraw
1// vk-query.cpp
2#include "vk-query.h"
3
4#include "vk-util.h"
5
6namespace gfx
7{
8
9using namespace Slang;
10
11namespace vk
12{
13Result QueryPoolImpl::init(const IQueryPool::Desc& desc, DeviceImpl* device)
14{
15    m_device = device;
16    m_pool = VK_NULL_HANDLE;
17    VkQueryPoolCreateInfo createInfo = {};
18    createInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
19    createInfo.queryCount = (uint32_t)desc.count;
20    switch (desc.type)
21    {
22    case QueryType::Timestamp:
23        createInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
24        break;
25    case QueryType::AccelerationStructureCompactedSize:
26        createInfo.queryType = VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR;
27        break;
28    case QueryType::AccelerationStructureSerializedSize:
29        createInfo.queryType = VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SERIALIZATION_SIZE_KHR;
30        break;
31    case QueryType::AccelerationStructureCurrentSize:
32        // Vulkan does not support CurrentSize query, will not create actual pools here.
33        return SLANG_OK;
34    default:
35        return SLANG_E_INVALID_ARG;
36    }
37    SLANG_VK_RETURN_ON_FAIL(
38        m_device->m_api.vkCreateQueryPool(m_device->m_api.m_device, &createInfo, nullptr, &m_pool));
39    return SLANG_OK;
40}
41
42QueryPoolImpl::~QueryPoolImpl()
43{
44    m_device->m_api.vkDestroyQueryPool(m_device->m_api.m_device, m_pool, nullptr);
45}
46
47Result QueryPoolImpl::getResult(GfxIndex index, GfxCount count, uint64_t* data)
48{
49    if (!m_pool)
50    {
51        // Vulkan does not support CurrentSize query, return 0 here.
52        for (SlangInt i = 0; i < count; i++)
53            data[i] = 0;
54        return SLANG_OK;
55    }
56
57    SLANG_VK_RETURN_ON_FAIL(m_device->m_api.vkGetQueryPoolResults(
58        m_device->m_api.m_device,
59        m_pool,
60        (uint32_t)index,
61        (uint32_t)count,
62        sizeof(uint64_t) * count,
63        data,
64        sizeof(uint64_t),
65        VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT));
66    return SLANG_OK;
67}
68
69void _writeTimestamp(
70    VulkanApi* api,
71    VkCommandBuffer vkCmdBuffer,
72    IQueryPool* queryPool,
73    SlangInt index)
74{
75    auto queryPoolImpl = static_cast<QueryPoolImpl*>(queryPool);
76    api->vkCmdResetQueryPool(vkCmdBuffer, queryPoolImpl->m_pool, (uint32_t)index, 1);
77    api->vkCmdWriteTimestamp(
78        vkCmdBuffer,
79        VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
80        queryPoolImpl->m_pool,
81        (uint32_t)index);
82}
83
84} // namespace vk
85} // namespace gfx