summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngine/include/RenderPassBase.hpp
blob: 36f743da8a8db456e96de0cb63dc6fef067dfb40 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 *  Copyright 2019-2021 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

#pragma once

/// \file
/// Implementation of the Diligent::RenderPassBase template class

#include <vector>
#include <utility>

#include "RenderPass.h"
#include "DeviceObjectBase.hpp"
#include "RenderDeviceBase.hpp"

namespace Diligent
{

void ValidateRenderPassDesc(const RenderPassDesc& Desc) noexcept(false);

template <typename RenderDeviceImplType>
void _CorrectAttachmentState(RESOURCE_STATE& State) {}

template <>
inline void _CorrectAttachmentState<class RenderDeviceVkImpl>(RESOURCE_STATE& State)
{
    if (State == RESOURCE_STATE_RESOLVE_DEST)
    {
        // In Vulkan resolve attachments must be in VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL state.
        // It is important to correct the state because outside of render pass RESOURCE_STATE_RESOLVE_DEST maps
        // to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL.
        State = RESOURCE_STATE_RENDER_TARGET;
    }
}

/// Template class implementing base functionality of the render pass object.

/// \tparam EngineImplTraits - Engine implementation type traits.
template <typename EngineImplTraits>
class RenderPassBase : public DeviceObjectBase<typename EngineImplTraits::RenderPassInterface, typename EngineImplTraits::RenderDeviceImplType, RenderPassDesc>
{
public:
    // Base interface this class inherits (e.g. IRenderPassVk)
    using BaseInterface = typename EngineImplTraits::RenderPassInterface;

    // Render device implementation type (RenderDeviceD3D12Impl, RenderDeviceVkImpl, etc.).
    using RenderDeviceImplType = typename EngineImplTraits::RenderDeviceImplType;

    using TDeviceObjectBase = DeviceObjectBase<BaseInterface, RenderDeviceImplType, RenderPassDesc>;

    /// \param pRefCounters      - Reference counters object that controls the lifetime of this render pass.
    /// \param pDevice           - Pointer to the device.
    /// \param Desc              - Render pass description.
    /// \param bIsDeviceInternal - Flag indicating if the RenderPass is an internal device object and
    ///							   must not keep a strong reference to the device.
    RenderPassBase(IReferenceCounters*   pRefCounters,
                   RenderDeviceImplType* pDevice,
                   const RenderPassDesc& Desc,
                   bool                  bIsDeviceInternal = false) :
        TDeviceObjectBase{pRefCounters, pDevice, Desc, bIsDeviceInternal}
    {
        ValidateRenderPassDesc(this->m_Desc);

        if (Desc.AttachmentCount != 0)
        {
            auto* pAttachments =
                ALLOCATE(GetRawAllocator(), "Memory for RenderPassAttachmentDesc array", RenderPassAttachmentDesc, Desc.AttachmentCount);
            this->m_Desc.pAttachments = pAttachments;
            for (Uint32 i = 0; i < Desc.AttachmentCount; ++i)
            {
                pAttachments[i] = Desc.pAttachments[i];
                _CorrectAttachmentState<RenderDeviceImplType>(pAttachments[i].FinalState);
            }
        }

        Uint32 TotalAttachmentReferencesCount = 0;
        Uint32 TotalPreserveAttachmentsCount  = 0;
        CountSubpassAttachmentReferences(Desc, TotalAttachmentReferencesCount, TotalPreserveAttachmentsCount);
        if (TotalAttachmentReferencesCount != 0)
        {
            m_pAttachmentReferences = ALLOCATE(GetRawAllocator(), "Memory for subpass attachment references array", AttachmentReference, TotalAttachmentReferencesCount);
        }
        if (TotalPreserveAttachmentsCount != 0)
        {
            m_pPreserveAttachments = ALLOCATE(GetRawAllocator(), "Memory for subpass preserve attachments array", Uint32, TotalPreserveAttachmentsCount);
        }

        m_AttachmentStates.resize(Desc.AttachmentCount * Desc.SubpassCount);
        m_AttachmentFirstLastUse.resize(Desc.AttachmentCount, std::pair<Uint32, Uint32>{ATTACHMENT_UNUSED, 0});

        auto* pCurrAttachmentRef      = m_pAttachmentReferences;
        auto* pCurrPreserveAttachment = m_pPreserveAttachments;
        VERIFY(Desc.SubpassCount != 0, "Render pass must have at least one subpass");
        auto* pSubpasses =
            ALLOCATE(GetRawAllocator(), "Memory for SubpassDesc array", SubpassDesc, Desc.SubpassCount);
        this->m_Desc.pSubpasses = pSubpasses;
        for (Uint32 subpass = 0; subpass < Desc.SubpassCount; ++subpass)
        {
            for (Uint32 att = 0; att < Desc.AttachmentCount; ++att)
            {
                SetAttachmentState(subpass, att, subpass > 0 ? GetAttachmentState(subpass - 1, att) : Desc.pAttachments[subpass].InitialState);
            }

            const auto& SrcSubpass = Desc.pSubpasses[subpass];
            auto&       DstSubpass = pSubpasses[subpass];

            auto UpdateAttachmentStateAndFirstUseSubpass = [this, subpass](const AttachmentReference& AttRef) //
            {
                if (AttRef.AttachmentIndex != ATTACHMENT_UNUSED)
                {
                    SetAttachmentState(subpass, AttRef.AttachmentIndex, AttRef.State);
                    auto& FirstLastUse = m_AttachmentFirstLastUse[AttRef.AttachmentIndex];
                    if (FirstLastUse.first == ATTACHMENT_UNUSED)
                        FirstLastUse.first = subpass;
                    FirstLastUse.second = subpass;
                }
            };

            DstSubpass = SrcSubpass;
            if (SrcSubpass.InputAttachmentCount != 0)
            {
                DstSubpass.pInputAttachments = pCurrAttachmentRef;
                for (Uint32 input_attachment = 0; input_attachment < SrcSubpass.InputAttachmentCount; ++input_attachment, ++pCurrAttachmentRef)
                {
                    *pCurrAttachmentRef = SrcSubpass.pInputAttachments[input_attachment];
                    UpdateAttachmentStateAndFirstUseSubpass(*pCurrAttachmentRef);
                }
            }
            else
                DstSubpass.pInputAttachments = nullptr;

            if (SrcSubpass.RenderTargetAttachmentCount != 0)
            {
                DstSubpass.pRenderTargetAttachments = pCurrAttachmentRef;
                for (Uint32 rt_attachment = 0; rt_attachment < SrcSubpass.RenderTargetAttachmentCount; ++rt_attachment, ++pCurrAttachmentRef)
                {
                    *pCurrAttachmentRef = SrcSubpass.pRenderTargetAttachments[rt_attachment];
                    UpdateAttachmentStateAndFirstUseSubpass(*pCurrAttachmentRef);
                }

                if (DstSubpass.pResolveAttachments)
                {
                    DstSubpass.pResolveAttachments = pCurrAttachmentRef;
                    for (Uint32 rslv_attachment = 0; rslv_attachment < SrcSubpass.RenderTargetAttachmentCount; ++rslv_attachment, ++pCurrAttachmentRef)
                    {
                        *pCurrAttachmentRef = SrcSubpass.pResolveAttachments[rslv_attachment];
                        _CorrectAttachmentState<RenderDeviceImplType>(pCurrAttachmentRef->State);
                        UpdateAttachmentStateAndFirstUseSubpass(*pCurrAttachmentRef);
                    }
                }
            }
            else
            {
                DstSubpass.pRenderTargetAttachments = nullptr;
                DstSubpass.pResolveAttachments      = nullptr;
            }

            if (SrcSubpass.pDepthStencilAttachment != nullptr)
            {
                DstSubpass.pDepthStencilAttachment = pCurrAttachmentRef;
                *(pCurrAttachmentRef++)            = *SrcSubpass.pDepthStencilAttachment;
                UpdateAttachmentStateAndFirstUseSubpass(*SrcSubpass.pDepthStencilAttachment);
            }

            if (SrcSubpass.PreserveAttachmentCount != 0)
            {
                DstSubpass.pPreserveAttachments = pCurrPreserveAttachment;
                for (Uint32 prsv_attachment = 0; prsv_attachment < SrcSubpass.PreserveAttachmentCount; ++prsv_attachment)
                    *(pCurrPreserveAttachment++) = SrcSubpass.pPreserveAttachments[prsv_attachment];
            }
            else
                DstSubpass.pPreserveAttachments = nullptr;
        }
        VERIFY_EXPR(pCurrAttachmentRef - m_pAttachmentReferences == static_cast<ptrdiff_t>(TotalAttachmentReferencesCount));
        VERIFY_EXPR(pCurrPreserveAttachment - m_pPreserveAttachments == static_cast<ptrdiff_t>(TotalPreserveAttachmentsCount));

        if (Desc.DependencyCount != 0)
        {
            auto* pDependencies =
                ALLOCATE(GetRawAllocator(), "Memory for SubpassDependencyDesc array", SubpassDependencyDesc, Desc.DependencyCount);
            this->m_Desc.pDependencies = pDependencies;
            for (Uint32 i = 0; i < Desc.DependencyCount; ++i)
            {
                pDependencies[i] = Desc.pDependencies[i];
            }
        }
    }

    ~RenderPassBase()
    {
        auto& RawAllocator = GetRawAllocator();
        if (this->m_Desc.pAttachments != nullptr)
            RawAllocator.Free(const_cast<RenderPassAttachmentDesc*>(this->m_Desc.pAttachments));
        if (this->m_Desc.pSubpasses != nullptr)
            RawAllocator.Free(const_cast<SubpassDesc*>(this->m_Desc.pSubpasses));
        if (this->m_Desc.pDependencies != nullptr)
            RawAllocator.Free(const_cast<SubpassDependencyDesc*>(this->m_Desc.pDependencies));
        if (m_pAttachmentReferences != nullptr)
            RawAllocator.Free(m_pAttachmentReferences);
        if (m_pPreserveAttachments != nullptr)
            RawAllocator.Free(m_pPreserveAttachments);
    }

    IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_RenderPass, TDeviceObjectBase)

    RESOURCE_STATE GetAttachmentState(Uint32 Subpass, Uint32 Attachment) const
    {
        VERIFY_EXPR(Attachment < this->m_Desc.AttachmentCount);
        VERIFY_EXPR(Subpass < this->m_Desc.SubpassCount);
        return m_AttachmentStates[this->m_Desc.AttachmentCount * Subpass + Attachment];
    }

    std::pair<Uint32, Uint32> GetAttachmentFirstLastUse(Uint32 Attachment) const
    {
        return m_AttachmentFirstLastUse[Attachment];
    }

protected:
    static void CountSubpassAttachmentReferences(const RenderPassDesc& Desc,
                                                 Uint32&               TotalAttachmentReferencesCount,
                                                 Uint32&               TotalPreserveAttachmentsCount)
    {
        TotalAttachmentReferencesCount = 0;
        TotalPreserveAttachmentsCount  = 0;
        for (Uint32 i = 0; i < Desc.SubpassCount; ++i)
        {
            const auto& Subpass = Desc.pSubpasses[i];
            TotalAttachmentReferencesCount += Subpass.InputAttachmentCount;
            TotalAttachmentReferencesCount += Subpass.RenderTargetAttachmentCount;
            if (Subpass.pResolveAttachments != nullptr)
                TotalAttachmentReferencesCount += Subpass.RenderTargetAttachmentCount;
            if (Subpass.pDepthStencilAttachment != nullptr)
                TotalAttachmentReferencesCount += 1;
            TotalPreserveAttachmentsCount += Subpass.PreserveAttachmentCount;
        }
    }

private:
    void SetAttachmentState(Uint32 Subpass, Uint32 Attachment, RESOURCE_STATE State)
    {
        VERIFY_EXPR(Attachment < this->m_Desc.AttachmentCount);
        VERIFY_EXPR(Subpass < this->m_Desc.SubpassCount);
        m_AttachmentStates[this->m_Desc.AttachmentCount * Subpass + Attachment] = State;
    }

    AttachmentReference* m_pAttachmentReferences = nullptr;
    Uint32*              m_pPreserveAttachments  = nullptr;

    // Attachment states during each subpass
    std::vector<RESOURCE_STATE> m_AttachmentStates;

    // The index of the subpass where the attachment is first used
    std::vector<std::pair<Uint32, Uint32>> m_AttachmentFirstLastUse;
};

} // namespace Diligent