summaryrefslogtreecommitdiffstats
path: root/tools/gfx/metal/metal-render-pass.cpp
blob: 2b8cf61d235126c8ae965e5648aa302cb0d84d11 (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
// metal-render-pass.cpp
#include "metal-render-pass.h"

// #include "metal-helper-functions.h"

namespace gfx
{

using namespace Slang;

namespace metal
{

IRenderPassLayout* RenderPassLayoutImpl::getInterface(const Guid& guid)
{
    if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_IRenderPassLayout)
        return static_cast<IRenderPassLayout*>(this);
    return nullptr;
}

static inline MTL::LoadAction translateLoadOp(IRenderPassLayout::TargetLoadOp loadOp)
{
    switch (loadOp)
    {
    case IRenderPassLayout::TargetLoadOp::Load:
        return MTL::LoadActionLoad;
    case IRenderPassLayout::TargetLoadOp::Clear:
        return MTL::LoadActionClear;
    case IRenderPassLayout::TargetLoadOp::DontCare:
        return MTL::LoadActionDontCare;
    default:
        return MTL::LoadAction(0);
    }
}

static inline MTL::StoreAction translateStoreOp(IRenderPassLayout::TargetStoreOp storeOp)
{
    switch (storeOp)
    {
    case IRenderPassLayout::TargetStoreOp::Store:
        return MTL::StoreActionStore;
    case IRenderPassLayout::TargetStoreOp::DontCare:
        return MTL::StoreActionDontCare;
    default:
        return MTL::StoreAction(0);
    }
}

Result RenderPassLayoutImpl::init(DeviceImpl* device, const IRenderPassLayout::Desc& desc)
{
    m_device = device;

    FramebufferLayoutImpl* framebufferLayout =
        static_cast<FramebufferLayoutImpl*>(desc.framebufferLayout);
    assert(framebufferLayout);

    // Initialize render pass descriptor, filling in attachment metadata, but leaving texture data
    // unbound.
    m_renderPassDesc = NS::TransferPtr(MTL::RenderPassDescriptor::alloc()->init());

    m_renderPassDesc->setRenderTargetArrayLength(desc.renderTargetCount);
    for (GfxIndex i = 0; i < desc.renderTargetCount; ++i)
    {
        MTL::RenderPassColorAttachmentDescriptor* colorAttachment =
            m_renderPassDesc->colorAttachments()->object(i);
        colorAttachment->setLoadAction(translateLoadOp(desc.renderTargetAccess[i].loadOp));
        colorAttachment->setStoreAction(translateStoreOp(desc.renderTargetAccess[i].storeOp));
    }

    m_renderPassDesc->depthAttachment()->setLoadAction(
        translateLoadOp(desc.depthStencilAccess->loadOp));
    m_renderPassDesc->depthAttachment()->setStoreAction(
        translateStoreOp(desc.depthStencilAccess->storeOp));

    m_renderPassDesc->stencilAttachment()->setLoadAction(
        translateLoadOp(desc.depthStencilAccess->loadOp));
    m_renderPassDesc->stencilAttachment()->setStoreAction(
        translateStoreOp(desc.depthStencilAccess->storeOp));

    return SLANG_OK;
}

} // namespace metal
} // namespace gfx