Diligent Engine  v.2.4.g
FramebufferBase.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include "Framebuffer.h"
34 #include "DeviceObjectBase.hpp"
35 #include "RenderDeviceBase.hpp"
36 #include "TextureView.h"
37 #include "GraphicsAccessories.hpp"
38 
39 namespace Diligent
40 {
41 
42 void ValidateFramebufferDesc(const FramebufferDesc& Desc) noexcept(false);
43 
45 
47 template <typename EngineImplTraits>
48 class FramebufferBase : public DeviceObjectBase<typename EngineImplTraits::FramebufferInterface, typename EngineImplTraits::RenderDeviceImplType, FramebufferDesc>
49 {
50 public:
51  // Base interface that this class inherits (e.g. IFramebufferVk).
52  using BaseInterface = typename EngineImplTraits::FramebufferInterface;
53 
54  // Render device implementation type (RenderDeviceD3D12Impl, RenderDeviceVkImpl, etc.).
55  using RenderDeviceImplType = typename EngineImplTraits::RenderDeviceImplType;
56 
58 
65  RenderDeviceImplType* pDevice,
66  const FramebufferDesc& Desc,
67  bool bIsDeviceInternal = false) :
68  TDeviceObjectBase{pRefCounters, pDevice, Desc, bIsDeviceInternal},
69  m_pRenderPass{Desc.pRenderPass}
70  {
72 
73  if (this->m_Desc.Width == 0 || this->m_Desc.Height == 0 || this->m_Desc.NumArraySlices == 0)
74  {
75  for (Uint32 i = 0; i < this->m_Desc.AttachmentCount; ++i)
76  {
77  auto* const pAttachment = Desc.ppAttachments[i];
78  if (pAttachment == nullptr)
79  continue;
80 
81  const auto& ViewDesc = pAttachment->GetDesc();
82  const auto& TexDesc = pAttachment->GetTexture()->GetDesc();
83 
84  auto MipLevelProps = GetMipLevelProperties(TexDesc, ViewDesc.MostDetailedMip);
85  if (this->m_Desc.Width == 0)
86  this->m_Desc.Width = MipLevelProps.LogicalWidth;
87  if (this->m_Desc.Height == 0)
88  this->m_Desc.Height = MipLevelProps.LogicalHeight;
89  if (this->m_Desc.NumArraySlices == 0)
90  this->m_Desc.NumArraySlices = ViewDesc.NumArraySlices;
91  }
92  }
93 
94  // It is legal for a subpass to use no color or depth/stencil attachments, either because it has no attachment
95  // references or because all of them are VK_ATTACHMENT_UNUSED. This kind of subpass can use shader side effects
96  // such as image stores and atomics to produce an output. In this case, the subpass continues to use the width,
97  // height, and layers of the framebuffer to define the dimensions of the rendering area.
98  if (this->m_Desc.Width == 0)
99  LOG_ERROR_AND_THROW("The framebuffer width is zero and can't be automatically determined as there are no non-null attachments");
100  if (this->m_Desc.Height == 0)
101  LOG_ERROR_AND_THROW("The framebuffer height is zero and can't be automatically determined as there are no non-null attachments");
102  if (this->m_Desc.NumArraySlices == 0)
103  LOG_ERROR_AND_THROW("The framebuffer array slice count is zero and can't be automatically determined as there are no non-null attachments");
104 
105  if (this->m_Desc.AttachmentCount > 0)
106  {
107  m_ppAttachments =
108  ALLOCATE(GetRawAllocator(), "Memory for framebuffer attachment array", ITextureView*, this->m_Desc.AttachmentCount);
109  this->m_Desc.ppAttachments = m_ppAttachments;
110  for (Uint32 i = 0; i < this->m_Desc.AttachmentCount; ++i)
111  {
112  if (Desc.ppAttachments[i] == nullptr)
113  continue;
114 
115  m_ppAttachments[i] = Desc.ppAttachments[i];
116  m_ppAttachments[i]->AddRef();
117  }
118  }
119 
120  Desc.pRenderPass->AddRef();
121  }
122 
124  {
125  if (this->m_Desc.AttachmentCount > 0)
126  {
127  VERIFY_EXPR(m_ppAttachments != nullptr);
128  for (Uint32 i = 0; i < this->m_Desc.AttachmentCount; ++i)
129  {
130  m_ppAttachments[i]->Release();
131  }
132  GetRawAllocator().Free(m_ppAttachments);
133  }
134  this->m_Desc.pRenderPass->Release();
135  }
136 
138 
139 private:
140  RefCntAutoPtr<IRenderPass> m_pRenderPass;
141 
142  ITextureView** m_ppAttachments = nullptr;
143 };
144 
145 } // namespace Diligent
Diligent::FramebufferBase< EngineGLImplTraits >::RenderDeviceImplType
typename EngineGLImplTraits ::RenderDeviceImplType RenderDeviceImplType
Definition: FramebufferBase.hpp:55
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
GraphicsAccessories.hpp
TextureView.h
Diligent::FramebufferDesc::AttachmentCount
Uint32 AttachmentCount
The number of attachments.
Definition: Framebuffer.h:52
Diligent::IObject::Release
virtual ReferenceCounterValueType Release()=0
Decrements the number of strong references by 1 and destroys the object when the counter reaches zero...
LOG_ERROR_AND_THROW
#define LOG_ERROR_AND_THROW(...)
Definition: Errors.hpp:101
DeviceObjectBase.hpp
Diligent::FramebufferBase::~FramebufferBase
~FramebufferBase()
Definition: FramebufferBase.hpp:123
Diligent::DeviceObjectBase< EngineImplTraits::FramebufferInterface, EngineImplTraits::RenderDeviceImplType, FramebufferDesc >::m_Desc
FramebufferDesc m_Desc
Object description.
Definition: DeviceObjectBase.hpp:182
Diligent::ITextureView
Texture view interface.
Definition: TextureView.h:202
Diligent::FramebufferBase::TDeviceObjectBase
DeviceObjectBase< BaseInterface, RenderDeviceImplType, FramebufferDesc > TDeviceObjectBase
Definition: FramebufferBase.hpp:57
Diligent::FramebufferBase
Template class implementing base functionality of the framebuffer object.
Definition: FramebufferBase.hpp:48
RenderDeviceBase.hpp
Diligent::IObject::AddRef
virtual ReferenceCounterValueType AddRef()=0
Increments the number of strong references by 1.
Diligent::GetRawAllocator
IMemoryAllocator & GetRawAllocator()
Returns raw memory allocator.
Definition: EngineMemory.cpp:51
IMPLEMENT_QUERY_INTERFACE_IN_PLACE
#define IMPLEMENT_QUERY_INTERFACE_IN_PLACE(InterfaceID, ParentClassName)
Definition: ObjectBase.hpp:59
Diligent::RefCntAutoPtr
Template class that implements reference counting.
Definition: RefCntAutoPtr.hpp:73
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::FramebufferDesc::NumArraySlices
Uint32 NumArraySlices
The number of array slices in the framebuffer.
Definition: Framebuffer.h:64
Diligent::FramebufferDesc::Width
Uint32 Width
Width of the framebuffer.
Definition: Framebuffer.h:58
Diligent::FramebufferDesc
struct FramebufferDesc FramebufferDesc
Definition: Framebuffer.h:66
Diligent::GetMipLevelProperties
MipLevelProperties GetMipLevelProperties(const TextureDesc &TexDesc, Uint32 MipLevel)
Definition: GraphicsAccessories.cpp:1339
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
Diligent::FramebufferDesc::Height
Uint32 Height
Height of the framebuffer.
Definition: Framebuffer.h:61
Diligent::FramebufferDesc
Framebuffer description.
Definition: Framebuffer.h:46
Diligent::FramebufferBase::FramebufferBase
FramebufferBase(IReferenceCounters *pRefCounters, RenderDeviceImplType *pDevice, const FramebufferDesc &Desc, bool bIsDeviceInternal=false)
Definition: FramebufferBase.hpp:64
Diligent::ValidateFramebufferDesc
void ValidateFramebufferDesc(const FramebufferDesc &Desc) noexcept(false)
Definition: FramebufferBase.cpp:34
Framebuffer.h
Diligent::FramebufferDesc::ppAttachments
ITextureView *const * ppAttachments
Pointer to the array of attachments.
Definition: Framebuffer.h:55
ALLOCATE
#define ALLOCATE(Allocator, Desc, Type, Count)
Definition: EngineMemory.h:47
Diligent::FramebufferDesc::pRenderPass
IRenderPass * pRenderPass
Render pass that the framebuffer will be compatible with.
Definition: Framebuffer.h:49
Diligent::IMemoryAllocator::Free
virtual void Free(void *Ptr)=0
Releases memory.
Diligent::FramebufferBase< EngineGLImplTraits >::BaseInterface
typename EngineGLImplTraits ::FramebufferInterface BaseInterface
Definition: FramebufferBase.hpp:52
Diligent::DeviceObjectBase
Template class implementing base functionality of the device object.
Definition: DeviceObjectBase.hpp:45
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37