yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
3.5 KiB90 linesraw
1// vk-render-pass.cpp
2#include "vk-render-pass.h"
3
4#include "vk-helper-functions.h"
5
6namespace gfx
7{
8
9using namespace Slang;
10
11namespace vk
12{
13
14IRenderPassLayout* RenderPassLayoutImpl::getInterface(const Guid& guid)
15{
16    if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_IRenderPassLayout)
17        return static_cast<IRenderPassLayout*>(this);
18    return nullptr;
19}
20
21RenderPassLayoutImpl::~RenderPassLayoutImpl()
22{
23    m_renderer->m_api.vkDestroyRenderPass(m_renderer->m_api.m_device, m_renderPass, nullptr);
24}
25
26Result RenderPassLayoutImpl::init(DeviceImpl* renderer, const IRenderPassLayout::Desc& desc)
27{
28    m_renderer = renderer;
29
30    // Create render pass using load/storeOp and layouts info from `desc`.
31    auto framebufferLayout = static_cast<FramebufferLayoutImpl*>(desc.framebufferLayout);
32    assert(desc.renderTargetCount == framebufferLayout->m_renderTargetCount);
33
34    // We need extra space if we have depth buffer
35    Array<VkAttachmentDescription, kMaxTargets> targetDescs;
36    targetDescs = framebufferLayout->m_targetDescs;
37    for (GfxIndex i = 0; i < desc.renderTargetCount; ++i)
38    {
39        VkAttachmentDescription& dst = targetDescs[i];
40        auto access = desc.renderTargetAccess[i];
41        // Fill in loadOp/storeOp and layout from desc.
42        dst.loadOp = translateLoadOp(access.loadOp);
43        dst.storeOp = translateStoreOp(access.storeOp);
44        dst.stencilLoadOp = translateLoadOp(access.stencilLoadOp);
45        dst.stencilStoreOp = translateStoreOp(access.stencilStoreOp);
46        dst.initialLayout = VulkanUtil::mapResourceStateToLayout(access.initialState);
47        dst.finalLayout = VulkanUtil::mapResourceStateToLayout(access.finalState);
48    }
49
50    if (framebufferLayout->m_hasDepthStencilTarget)
51    {
52        VkAttachmentDescription& dst = targetDescs[desc.renderTargetCount];
53        auto access = *desc.depthStencilAccess;
54        dst.loadOp = translateLoadOp(access.loadOp);
55        dst.storeOp = translateStoreOp(access.storeOp);
56        dst.stencilLoadOp = translateLoadOp(access.stencilLoadOp);
57        dst.stencilStoreOp = translateStoreOp(access.stencilStoreOp);
58        dst.initialLayout = VulkanUtil::mapResourceStateToLayout(access.initialState);
59        dst.finalLayout = VulkanUtil::mapResourceStateToLayout(access.finalState);
60    }
61
62    VkSubpassDescription subpassDesc = {};
63    subpassDesc.flags = 0;
64    subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
65    subpassDesc.inputAttachmentCount = 0u;
66    subpassDesc.pInputAttachments = nullptr;
67    subpassDesc.colorAttachmentCount = desc.renderTargetCount;
68    subpassDesc.pColorAttachments = framebufferLayout->m_colorReferences.getBuffer();
69    subpassDesc.pResolveAttachments = nullptr;
70    subpassDesc.pDepthStencilAttachment =
71        framebufferLayout->m_hasDepthStencilTarget ? &framebufferLayout->m_depthReference : nullptr;
72    subpassDesc.preserveAttachmentCount = 0u;
73    subpassDesc.pPreserveAttachments = nullptr;
74
75    VkRenderPassCreateInfo renderPassCreateInfo = {};
76    renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
77    renderPassCreateInfo.attachmentCount = (uint32_t)targetDescs.getCount();
78    renderPassCreateInfo.pAttachments = targetDescs.getBuffer();
79    renderPassCreateInfo.subpassCount = 1;
80    renderPassCreateInfo.pSubpasses = &subpassDesc;
81    SLANG_VK_RETURN_ON_FAIL(m_renderer->m_api.vkCreateRenderPass(
82        m_renderer->m_api.m_device,
83        &renderPassCreateInfo,
84        nullptr,
85        &m_renderPass));
86    return SLANG_OK;
87}
88
89} // namespace vk
90} // namespace gfx