summaryrefslogtreecommitdiffstats
path: root/tools/gfx/vulkan/vk-shader-table.cpp
blob: 0b6488465fd988c7b52549695edc967837deca86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// vk-shader-table.cpp
#include "vk-shader-table.h"

#include "vk-device.h"
#include "vk-transient-heap.h"

#include "vk-helper-functions.h"

namespace gfx
{

using namespace Slang;

namespace vk
{

RefPtr<BufferResource> ShaderTableImpl::createDeviceBuffer(
    PipelineStateBase* pipeline,
    TransientResourceHeapBase* transientHeap,
    IResourceCommandEncoder* encoder)
{
    auto vkApi = m_device->m_api;
    auto rtProps = vkApi.m_rtProperties;
    uint32_t handleSize = rtProps.shaderGroupHandleSize;
    m_raygenTableSize = m_rayGenShaderCount * rtProps.shaderGroupBaseAlignment;
    m_missTableSize = (uint32_t)VulkanUtil::calcAligned(
        m_missShaderCount * handleSize, rtProps.shaderGroupBaseAlignment);
    m_hitTableSize = (uint32_t)VulkanUtil::calcAligned(
        m_hitGroupCount * handleSize, rtProps.shaderGroupBaseAlignment);
    m_callableTableSize = 0; // TODO: Are callable shaders needed?
    uint32_t tableSize = m_raygenTableSize + m_missTableSize + m_hitTableSize + m_callableTableSize;

    auto pipelineImpl = static_cast<RayTracingPipelineStateImpl*>(pipeline);
    ComPtr<IBufferResource> bufferResource;
    IBufferResource::Desc bufferDesc = {};
    bufferDesc.memoryType = MemoryType::DeviceLocal;
    bufferDesc.defaultState = ResourceState::General;
    bufferDesc.allowedStates =
        ResourceStateSet(ResourceState::General, ResourceState::CopyDestination);
    bufferDesc.type = IResource::Type::Buffer;
    bufferDesc.sizeInBytes = tableSize;
    static_cast<vk::DeviceImpl*>(m_device)->createBufferResourceImpl(
        bufferDesc,
        VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR,
        nullptr,
        bufferResource.writeRef());

    TransientResourceHeapImpl* transientHeapImpl =
        static_cast<TransientResourceHeapImpl*>(transientHeap);

    IBufferResource* stagingBuffer = nullptr;
    Offset stagingBufferOffset = 0;
    transientHeapImpl->allocateStagingBuffer(
        tableSize, stagingBuffer, stagingBufferOffset, MemoryType::Upload);

    assert(stagingBuffer);
    void* stagingPtr = nullptr;
    stagingBuffer->map(nullptr, &stagingPtr);

    List<uint8_t> handles;
    auto handleCount = pipelineImpl->shaderGroupCount;
    auto totalHandleSize = handleSize * handleCount;
    handles.setCount(totalHandleSize);
    auto result = vkApi.vkGetRayTracingShaderGroupHandlesKHR(
        m_device->m_device,
        pipelineImpl->m_pipeline,
        0,
        (uint32_t)handleCount,
        totalHandleSize,
        handles.getBuffer());

    uint8_t* stagingBufferPtr = (uint8_t*)stagingPtr + stagingBufferOffset;
    auto subTablePtr = stagingBufferPtr;
    Int shaderTableEntryCounter = 0;

    // Each loop calculates the copy source and destination locations by fetching the name
    // of the shader group from the list of shader group names and getting its corresponding
    // index in the buffer of handles.
    for (uint32_t i = 0; i < m_rayGenShaderCount; i++)
    {
        auto dstHandlePtr = subTablePtr + i * rtProps.shaderGroupBaseAlignment;
        auto shaderGroupName = m_shaderGroupNames[shaderTableEntryCounter++];
        auto shaderGroupIndexPtr =
            pipelineImpl->shaderGroupNameToIndex.tryGetValue(shaderGroupName);
        if (!shaderGroupIndexPtr)
            continue;

        auto shaderGroupIndex = *shaderGroupIndexPtr;
        auto srcHandlePtr = handles.getBuffer() + shaderGroupIndex * handleSize;
        memcpy(dstHandlePtr, srcHandlePtr, handleSize);
        memset(dstHandlePtr + handleSize, 0, rtProps.shaderGroupBaseAlignment - handleSize);
    }
    subTablePtr += m_raygenTableSize;

    for (uint32_t i = 0; i < m_missShaderCount; i++)
    {
        auto dstHandlePtr = subTablePtr + i * handleSize;
        auto shaderGroupName = m_shaderGroupNames[shaderTableEntryCounter++];
        auto shaderGroupIndexPtr =
            pipelineImpl->shaderGroupNameToIndex.tryGetValue(shaderGroupName);
        if (!shaderGroupIndexPtr)
            continue;

        auto shaderGroupIndex = *shaderGroupIndexPtr;
        auto srcHandlePtr = handles.getBuffer() + shaderGroupIndex * handleSize;
        memcpy(dstHandlePtr, srcHandlePtr, handleSize);
    }
    subTablePtr += m_missTableSize;

    for (uint32_t i = 0; i < m_hitGroupCount; i++)
    {
        auto dstHandlePtr = subTablePtr + i * handleSize;
        auto shaderGroupName = m_shaderGroupNames[shaderTableEntryCounter++];
        auto shaderGroupIndexPtr =
            pipelineImpl->shaderGroupNameToIndex.tryGetValue(shaderGroupName);
        if (!shaderGroupIndexPtr)
            continue;

        auto shaderGroupIndex = *shaderGroupIndexPtr;
        auto srcHandlePtr = handles.getBuffer() + shaderGroupIndex * handleSize;
        memcpy(dstHandlePtr, srcHandlePtr, handleSize);
    }
    subTablePtr += m_hitTableSize;

    // TODO: Callable shaders?

    stagingBuffer->unmap(nullptr);
    encoder->copyBuffer(bufferResource, 0, stagingBuffer, stagingBufferOffset, tableSize);
    encoder->bufferBarrier(
        1,
        bufferResource.readRef(),
        gfx::ResourceState::CopyDestination,
        gfx::ResourceState::ShaderResource);
    RefPtr<BufferResource> resultPtr = static_cast<BufferResource*>(bufferResource.get());
    return _Move(resultPtr);
}

} // namespace vk
} // namespace gfx