Diligent Engine  v.2.4.g
SwapChainGLBase.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 
30 #include "SwapChainBase.hpp"
31 #include "TextureViewGLImpl.hpp"
32 #include "RenderDeviceGLImpl.hpp"
33 
34 namespace Diligent
35 {
36 
38 template <class BaseInterface>
39 class SwapChainGLBase : public SwapChainBase<BaseInterface>
40 {
41 public:
43 
45  IRenderDevice* pDevice,
46  IDeviceContext* pDeviceContext,
47  const SwapChainDesc& SCDesc) :
48  TSwapChainBase{pRefCounters, pDevice, pDeviceContext, SCDesc}
49  {}
50 
51 
54 
57 
58 protected:
59  bool Resize(Uint32 NewWidth, Uint32 NewHeight, SURFACE_TRANSFORM NewPreTransform, Int32 /*To be different from virtual function*/)
60  {
61  if (NewPreTransform != SURFACE_TRANSFORM_OPTIMAL &&
62  NewPreTransform != SURFACE_TRANSFORM_IDENTITY)
63  {
65  " is not an allowed pretransform because OpenGL swap chains only support identity transform. "
66  "Use SURFACE_TRANSFORM_OPTIMAL (recommended) or SURFACE_TRANSFORM_IDENTITY.");
67  }
68  NewPreTransform = SURFACE_TRANSFORM_OPTIMAL;
69 
70  if (TSwapChainBase::Resize(NewWidth, NewHeight, NewPreTransform))
71  {
73  {
74  if (auto pDeviceContext = this->m_wpDeviceContext.Lock())
75  {
76  auto* pImmediateCtxGL = pDeviceContext.template RawPtr<DeviceContextGLImpl>();
77  // Unbind the back buffer to be consistent with other backends
78  auto* pCurrentBackBuffer = ValidatedCast<TextureBaseGL>(m_pRenderTargetView->GetTexture());
79  auto RenderTargetsReset = pImmediateCtxGL->UnbindTextureFromFramebuffer(pCurrentBackBuffer, false);
80  if (RenderTargetsReset)
81  {
82  LOG_INFO_MESSAGE_ONCE("Resizing the swap chain requires back and depth-stencil buffers to be unbound from the device context. "
83  "An application should use SetRenderTargets() to restore them.");
84  }
85  }
86  }
87 
88  CreateDummyBuffers(this->m_pRenderDevice.template RawPtr<RenderDeviceGLImpl>());
89  return true;
90  }
91 
92  return false;
93  }
94 
95  void CreateDummyBuffers(RenderDeviceGLImpl* pRenderDeviceGL)
96  {
97  TextureDesc ColorBuffDesc;
98  ColorBuffDesc.Type = RESOURCE_DIM_TEX_2D;
99  ColorBuffDesc.Name = "Main color buffer stub";
100  ColorBuffDesc.Width = this->m_SwapChainDesc.Width;
101  ColorBuffDesc.Height = this->m_SwapChainDesc.Height;
102  ColorBuffDesc.Format = this->m_SwapChainDesc.ColorBufferFormat;
103  ColorBuffDesc.BindFlags = BIND_RENDER_TARGET;
104  RefCntAutoPtr<ITexture> pDummyColorBuffer;
105  pRenderDeviceGL->CreateDummyTexture(ColorBuffDesc, RESOURCE_STATE_RENDER_TARGET, &pDummyColorBuffer);
106  m_pRenderTargetView = ValidatedCast<TextureViewGLImpl>(pDummyColorBuffer->GetDefaultView(TEXTURE_VIEW_RENDER_TARGET));
107 
108  TextureDesc DepthBuffDesc = ColorBuffDesc;
109  DepthBuffDesc.Name = "Main depth buffer stub";
110  DepthBuffDesc.Format = this->m_SwapChainDesc.DepthBufferFormat;
111  DepthBuffDesc.BindFlags = BIND_DEPTH_STENCIL;
112  RefCntAutoPtr<ITexture> pDummyDepthBuffer;
113  pRenderDeviceGL->CreateDummyTexture(DepthBuffDesc, RESOURCE_STATE_DEPTH_WRITE, &pDummyDepthBuffer);
114  m_pDepthStencilView = ValidatedCast<TextureViewGLImpl>(pDummyDepthBuffer->GetDefaultView(TEXTURE_VIEW_DEPTH_STENCIL));
115  }
116 
119 };
120 
121 } // namespace Diligent
Diligent::GetSurfaceTransformString
const char * GetSurfaceTransformString(SURFACE_TRANSFORM SrfTransform)
Definition: GraphicsAccessories.cpp:1119
Diligent::TEXTURE_VIEW_RENDER_TARGET
@ TEXTURE_VIEW_RENDER_TARGET
A texture view will define a render target view that will be used as the target for rendering operati...
Definition: GraphicsTypes.h:285
RenderDeviceGLImpl.hpp
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
Diligent::TextureDesc::Format
TEXTURE_FORMAT Format
Texture format, see Diligent::TEXTURE_FORMAT.
Definition: Texture.h:68
Diligent::SwapChainGLBase::SwapChainGLBase
SwapChainGLBase(IReferenceCounters *pRefCounters, IRenderDevice *pDevice, IDeviceContext *pDeviceContext, const SwapChainDesc &SCDesc)
Definition: SwapChainGLBase.hpp:44
Diligent::SwapChainGLBase::Resize
bool Resize(Uint32 NewWidth, Uint32 NewHeight, SURFACE_TRANSFORM NewPreTransform, Int32)
Definition: SwapChainGLBase.hpp:59
Diligent::SwapChainDesc::Width
Uint32 Width
The swap chain width. Default value is 0.
Definition: GraphicsTypes.h:1350
SwapChainBase.hpp
Diligent::SwapChainGLBase
Base implementation of a swap chain for OpenGL.
Definition: SwapChainGLBase.hpp:39
Diligent::SwapChainDesc::ColorBufferFormat
TEXTURE_FORMAT ColorBufferFormat
Back buffer format. Default value is Diligent::TEX_FORMAT_RGBA8_UNORM_SRGB.
Definition: GraphicsTypes.h:1356
Diligent::SwapChainGLBase::GetDepthBufferDSV
virtual ITextureView * GetDepthBufferDSV() override final
Implementation of ISwapChain::GetDepthBufferDSV() in OpenGL backend.
Definition: SwapChainGLBase.hpp:56
Diligent::ITextureView
Texture view interface.
Definition: TextureView.h:202
Diligent::SwapChainDesc::DepthBufferFormat
TEXTURE_FORMAT DepthBufferFormat
Depth buffer format. Default value is Diligent::TEX_FORMAT_D32_FLOAT. Use Diligent::TEX_FORMAT_UNKNOW...
Definition: GraphicsTypes.h:1360
Diligent::SURFACE_TRANSFORM_IDENTITY
@ SURFACE_TRANSFORM_IDENTITY
The image content is presented without being transformed.
Definition: GraphicsTypes.h:1321
Diligent::SwapChainBase::m_wpDeviceContext
RefCntWeakPtr< IDeviceContext > m_wpDeviceContext
Weak references to the immediate device context. The context holds the strong reference to the swap c...
Definition: SwapChainBase.hpp:119
Diligent::Int32
int32_t Int32
32-bit signed integer
Definition: BasicTypes.h:46
Diligent::SURFACE_TRANSFORM_OPTIMAL
@ SURFACE_TRANSFORM_OPTIMAL
Uset the most optimal surface transform.
Definition: GraphicsTypes.h:1318
Diligent::IRenderDevice
Render device interface.
Definition: RenderDevice.h:75
Diligent::RenderDeviceGLImpl::CreateDummyTexture
virtual void CreateDummyTexture(const TextureDesc &TexDesc, RESOURCE_STATE InitialState, ITexture **ppTexture) override final
Implementation of IRenderDeviceGL::CreateDummyTexture().
Definition: RenderDeviceGLImpl.cpp:691
Diligent::SURFACE_TRANSFORM
SURFACE_TRANSFORM
The transform applied to the image content prior to presentation.
Definition: GraphicsTypes.h:1315
Diligent::TextureDesc
Texture description.
Definition: Texture.h:47
Diligent::TextureDesc::Width
Uint32 Width
Texture width, in pixels.
Definition: Texture.h:53
Diligent::RefCntAutoPtr
Template class that implements reference counting.
Definition: RefCntAutoPtr.hpp:73
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::SwapChainGLBase::GetCurrentBackBufferRTV
virtual ITextureView * GetCurrentBackBufferRTV() override final
Implementation of ISwapChain::GetCurrentBackBufferRTV() in OpenGL backend.
Definition: SwapChainGLBase.hpp:53
Diligent::RESOURCE_STATE_RENDER_TARGET
@ RESOURCE_STATE_RENDER_TARGET
The resource is accessed as a render target.
Definition: GraphicsTypes.h:2832
Diligent::SwapChainBase
Base implementation of the swap chain.
Definition: SwapChainBase.hpp:51
Diligent::SwapChainDesc
Swap chain description.
Definition: GraphicsTypes.h:1347
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::SwapChainBase::Resize
bool Resize(Uint32 NewWidth, Uint32 NewHeight, SURFACE_TRANSFORM NewPreTransform, Int32 Dummy=0)
Definition: SwapChainBase.hpp:97
Diligent::SwapChainGLBase::CreateDummyBuffers
void CreateDummyBuffers(RenderDeviceGLImpl *pRenderDeviceGL)
Definition: SwapChainGLBase.hpp:95
Diligent::RenderDeviceGLImpl
Render device implementation in OpenGL backend.
Definition: RenderDeviceGLImpl.hpp:45
Diligent::SwapChainBase::m_pRenderDevice
RefCntAutoPtr< IRenderDevice > m_pRenderDevice
Strong reference to the render device.
Definition: SwapChainBase.hpp:115
Diligent::RESOURCE_DIM_TEX_2D
@ RESOURCE_DIM_TEX_2D
Two-dimensional texture.
Definition: GraphicsTypes.h:262
Diligent::SwapChainBase::m_SwapChainDesc
SwapChainDesc m_SwapChainDesc
Swap chain description.
Definition: SwapChainBase.hpp:122
Diligent::SwapChainGLBase::m_pDepthStencilView
RefCntAutoPtr< TextureViewGLImpl > m_pDepthStencilView
Definition: SwapChainGLBase.hpp:118
Diligent::TextureDesc::BindFlags
BIND_FLAGS BindFlags
Bind flags, see Diligent::BIND_FLAGS for details. The following bind flags are allowed: Diligent::BI...
Definition: Texture.h:86
LOG_WARNING_MESSAGE
#define LOG_WARNING_MESSAGE(...)
Definition: Errors.hpp:123
Diligent::BIND_DEPTH_STENCIL
@ BIND_DEPTH_STENCIL
A texture can be bound as a depth-stencil target.
Definition: GraphicsTypes.h:126
Diligent::IDeviceContext
Device context interface.
Definition: DeviceContext.h:1460
Diligent::TEXTURE_VIEW_DEPTH_STENCIL
@ TEXTURE_VIEW_DEPTH_STENCIL
A texture view will define a depth stencil view that will be used as the target for rendering operati...
Definition: GraphicsTypes.h:289
LOG_INFO_MESSAGE_ONCE
#define LOG_INFO_MESSAGE_ONCE(...)
Definition: Errors.hpp:141
TextureViewGLImpl.hpp
Diligent::BIND_RENDER_TARGET
@ BIND_RENDER_TARGET
A texture can be bound as a render target.
Definition: GraphicsTypes.h:125
Diligent::SwapChainDesc::Height
Uint32 Height
The swap chain height. Default value is 0.
Definition: GraphicsTypes.h:1353
Diligent::TextureDesc::Type
RESOURCE_DIMENSION Type
Texture type. See Diligent::RESOURCE_DIMENSION for details.
Definition: Texture.h:50
Diligent::SwapChainGLBase::m_pRenderTargetView
RefCntAutoPtr< TextureViewGLImpl > m_pRenderTargetView
Definition: SwapChainGLBase.hpp:117
Diligent::RESOURCE_STATE_DEPTH_WRITE
@ RESOURCE_STATE_DEPTH_WRITE
The resource is used in a writable depth-stencil view or in clear operation.
Definition: GraphicsTypes.h:2838
Diligent::TextureDesc::Height
Uint32 Height
Texture height, in pixels.
Definition: Texture.h:56
Diligent::DeviceObjectAttribs::Name
const Char * Name
Object name.
Definition: GraphicsTypes.h:1199
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37