Skip to content

Commit 24ec0a6

Browse files
Reworked swap chain and context initialization on iOS
1 parent 12606a7 commit 24ec0a6

File tree

7 files changed

+150
-62
lines changed

7 files changed

+150
-62
lines changed

Graphics/GraphicsEngineOpenGL/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ set(INCLUDE
2020
include/SamplerGLImpl.h
2121
include/ShaderGLImpl.h
2222
include/ShaderResourceBindingGLImpl.h
23-
include/SwapChainGLImpl.h
2423
include/TexRegionRender.h
2524
include/Texture1D_OGL.h
2625
include/Texture1DArray_OGL.h
@@ -67,7 +66,6 @@ set(SOURCE
6766
src/SamplerGLImpl.cpp
6867
src/ShaderGLImpl.cpp
6968
src/ShaderResourceBindingGLImpl.cpp
70-
src/SwapChainGLImpl.cpp
7169
src/TexRegionRender.cpp
7270
src/Texture1D_OGL.cpp
7371
src/Texture1DArray_OGL.cpp
@@ -83,23 +81,31 @@ set(SOURCE
8381

8482
if(PLATFORM_WIN32)
8583
list(APPEND SOURCE src/GLContextWindows.cpp)
84+
list(APPEND SOURCE src/SwapChainGLImpl.cpp)
8685
list(APPEND INCLUDE include/GLContextWindows.h)
86+
list(APPEND INCLUDE include/SwapChainGLImpl.h)
8787
elseif(PLATFORM_ANDROID)
8888
list(APPEND SOURCE src/GLContextAndroid.cpp)
8989
list(APPEND SOURCE src/RenderDeviceGLESImpl.cpp)
9090
list(APPEND SOURCE src/GLStubsAndroid.cpp)
91+
list(APPEND SOURCE src/SwapChainGLImpl.cpp)
9192

9293
list(APPEND INCLUDE include/GLContextAndroid.h)
9394
list(APPEND INCLUDE include/GLStubsAndroid.h)
9495
list(APPEND INCLUDE include/RenderDeviceGLESImpl.h)
96+
list(APPEND INCLUDE include/SwapChainGLImpl.h)
9597

9698
list(APPEND INTERFACE interface/RenderDeviceGLES.h)
9799
elseif(PLATFORM_LINUX)
98100
list(APPEND SOURCE src/GLContextLinux.cpp)
101+
list(APPEND SOURCE src/SwapChainGLImpl.cpp)
99102
list(APPEND INCLUDE include/GLContextLinux.h)
103+
list(APPEND INCLUDE include/SwapChainGLImpl.h)
100104
elseif(PLATFORM_MACOS)
101105
list(APPEND SOURCE src/GLContextMacOS.mm)
102106
list(APPEND INCLUDE include/GLContextMacOS.h)
107+
list(APPEND SOURCE src/SwapChainGLImpl.cpp)
108+
list(APPEND INCLUDE include/SwapChainGLImpl.h)
103109
elseif(PLATFORM_IOS)
104110
list(APPEND SOURCE src/GLContextIOS.mm)
105111
list(APPEND INCLUDE include/GLContextIOS.h)

Graphics/GraphicsEngineOpenGL/include/GLContextIOS.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,15 @@
2323

2424
#pragma once
2525

26-
#include "GLObjectWrapper.h"
27-
2826
namespace Diligent
2927
{
30-
struct ContextInitInfo
31-
{
32-
SwapChainDesc SwapChainAttribs;
33-
void *pNativeWndHandle = nullptr;
34-
};
35-
3628
class GLContext
3729
{
3830
public:
3931
typedef void* NativeGLContextType; // EAGLContext*
4032

41-
GLContext(const ContextInitInfo &Info, struct DeviceCaps &DeviceCaps);
42-
void SwapBuffers();
43-
44-
const SwapChainDesc& GetSwapChainDesc()const{ return m_SwapChainAttribs; }
33+
GLContext(const struct EngineGLAttribs &InitAttribs, struct DeviceCaps &DeviceCaps);
4534

4635
NativeGLContextType GetCurrentNativeGLContext();
47-
48-
private:
49-
SwapChainDesc m_SwapChainAttribs;
50-
GLObjectWrappers::GLRenderBufferObj m_ColorRenderBuffer;
51-
GLObjectWrappers::GLRenderBufferObj m_DepthRenderBuffer;
52-
GLObjectWrappers::GLFrameBufferObj m_FBO;
5336
};
5437
}

Graphics/GraphicsEngineOpenGL/include/SwapChainGLIOS.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,43 @@
2323

2424
#pragma once
2525

26-
#include "SwapChainGLImpl.h"
26+
#include "EngineGLAttribs.h"
27+
#include "SwapChainGL.h"
28+
#include "SwapChainBase.h"
29+
#include "GLObjectWrapper.h"
2730

2831
namespace Diligent
2932
{
3033

3134
class IMemoryAllocator;
3235
/// Implementation of the Diligent::ISwapChainGL interface on IOS
33-
class SwapChainGLIOS final : public SwapChainGLImpl
36+
class SwapChainGLIOS final : public SwapChainBase<ISwapChainGL>
3437
{
3538
public:
39+
typedef SwapChainBase<ISwapChainGL> TSwapChainBase;
40+
3641
SwapChainGLIOS(IReferenceCounters *pRefCounters,
37-
const SwapChainDesc& SwapChainDesc,
38-
class RenderDeviceGLImpl* pRenderDeviceGL,
39-
class DeviceContextGLImpl* pImmediateContextGL);
42+
const EngineGLAttribs &InitAttribs,
43+
const SwapChainDesc& SwapChainDesc,
44+
class RenderDeviceGLImpl* pRenderDeviceGL,
45+
class DeviceContextGLImpl* pImmediateContextGL);
46+
SwapChainGLIOS();
4047

41-
virtual void Present();
42-
43-
virtual void Resize( Uint32 NewWidth, Uint32 NewHeight );
48+
virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final;
49+
50+
virtual void Present()override final;
51+
52+
virtual void Resize( Uint32 NewWidth, Uint32 NewHeight )override final;
53+
54+
virtual GLuint GetDefaultFBO()const override final;
55+
56+
private:
57+
void InitRenderBuffers(bool InitFromDrawable, Uint32 &Width, Uint32 &Height);
58+
59+
GLObjectWrappers::GLRenderBufferObj m_ColorRenderBuffer;
60+
GLObjectWrappers::GLRenderBufferObj m_DepthRenderBuffer;
61+
GLObjectWrappers::GLFrameBufferObj m_DefaultFBO;
62+
void *m_CALayer;
4463
};
4564

4665
}

Graphics/GraphicsEngineOpenGL/include/SwapChainGLImpl.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#pragma once
2525

26+
#include "EngineGLAttribs.h"
2627
#include "SwapChainGL.h"
2728
#include "SwapChainBase.h"
2829
#include "GLObjectWrapper.h"
@@ -32,7 +33,7 @@ namespace Diligent
3233

3334
class IMemoryAllocator;
3435
/// Implementation of the Diligent::ISwapChainGL interface
35-
class SwapChainGLImpl : public SwapChainBase<ISwapChainGL>
36+
class SwapChainGLImpl final : public SwapChainBase<ISwapChainGL>
3637
{
3738
public:
3839
typedef SwapChainBase<ISwapChainGL> TSwapChainBase;
@@ -44,13 +45,13 @@ class SwapChainGLImpl : public SwapChainBase<ISwapChainGL>
4445
class DeviceContextGLImpl* pImmediateContextGL);
4546
~SwapChainGLImpl();
4647

47-
virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override;
48+
virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final;
4849

49-
virtual void Present()override;
50+
virtual void Present()override final;
5051

51-
virtual void Resize( Uint32 NewWidth, Uint32 NewHeight )override;
52+
virtual void Resize( Uint32 NewWidth, Uint32 NewHeight )override final;
5253

53-
virtual GLuint GetDefaultFBO()const override { return 0; }
54+
virtual GLuint GetDefaultFBO()const override final{ return 0; }
5455
};
5556

5657
}

Graphics/GraphicsEngineOpenGL/src/GLContextIOS.mm

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,21 @@
2424
#include "pch.h"
2525

2626
#import <OpenGLES/EAGL.h>
27-
#import <OpenGLES/EAGLDrawable.h>
2827

2928
#include "GLContextIOS.h"
3029
#include "DeviceCaps.h"
3130
#include "GLTypeConversions.h"
31+
#include "EngineGLAttribs.h"
3232

3333
namespace Diligent
3434
{
35-
GLContext::GLContext( const ContextInitInfo &Info, DeviceCaps &DeviceCaps ) :
36-
m_SwapChainAttribs(Info.SwapChainAttribs),
37-
m_ColorRenderBuffer(false),
38-
m_DepthRenderBuffer(false),
39-
m_FBO(false)
35+
GLContext::GLContext( const EngineGLAttribs &Info, DeviceCaps &DeviceCaps )
4036
{
4137
if (GetCurrentNativeGLContext() == nullptr)
4238
{
4339
LOG_ERROR_AND_THROW("No current GL context found!");
4440
}
4541

46-
//Set dummy width and height until resize is called by the app
47-
if(m_SwapChainAttribs.Width == 0)
48-
m_SwapChainAttribs.Width = 1024;
49-
if(m_SwapChainAttribs.Height)
50-
m_SwapChainAttribs.Height = 768;
51-
5242
//Checking GL version
5343
const GLubyte *GLVersionString = glGetString( GL_VERSION );
5444
const GLubyte *GLRenderer = glGetString(GL_RENDERER);
@@ -96,11 +86,6 @@
9686
DeviceCaps.TexCaps.bTexture2DMSArraySupported = False;
9787
}
9888

99-
void GLContext::SwapBuffers()
100-
{
101-
LOG_ERROR("Swap buffers operation must be performed by the app on MacOS");
102-
}
103-
10489
GLContext::NativeGLContextType GLContext::GetCurrentNativeGLContext()
10590
{
10691
EAGLContext* CurrentCtx = [EAGLContext currentContext];

Graphics/GraphicsEngineOpenGL/src/RenderDeviceFactoryOpenGL.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,31 @@
2828
#include "RenderDeviceFactoryOpenGL.h"
2929
#include "RenderDeviceGLImpl.h"
3030
#include "DeviceContextGLImpl.h"
31-
#include "SwapChainGLImpl.h"
3231
#include "EngineMemory.h"
3332
#include "HLSL2GLSLConverterObject.h"
3433

34+
#ifdef PLATFORM_IOS
35+
# include "SwapChainGLIOS.h"
36+
#else
37+
# include "SwapChainGLImpl.h"
38+
#endif
39+
3540
#ifdef PLATFORM_ANDROID
36-
#include "RenderDeviceGLESImpl.h"
41+
# include "RenderDeviceGLESImpl.h"
3742
#endif
3843

3944
namespace Diligent
4045
{
4146

4247
#if defined(PLATFORM_WIN32) || defined(PLATFORM_UNIVERSAL_WINDOWS) || defined(PLATFORM_LINUX) || defined(PLATFORM_MACOS)
4348
typedef RenderDeviceGLImpl TRenderDeviceGLImpl;
49+
typedef SwapChainGLImpl TSwapChain;
4450
#elif defined(PLATFORM_ANDROID)
4551
typedef RenderDeviceGLESImpl TRenderDeviceGLImpl;
52+
typedef SwapChainGLImpl TSwapChain;
4653
#elif defined(PLATFORM_IOS)
4754
typedef RenderDeviceGLImpl TRenderDeviceGLImpl;
55+
typedef SwapChainGLIOS TSwapChain;
4856
#else
4957
# error Unsupported platform
5058
#endif
@@ -112,7 +120,7 @@ void EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(const EngineGLAttribs&
112120
pDeviceContextOpenGL->QueryInterface(IID_DeviceContext, reinterpret_cast<IObject**>(ppImmediateContext) );
113121
pRenderDeviceOpenGL->SetImmediateContext(pDeviceContextOpenGL);
114122

115-
SwapChainGLImpl *pSwapChainGL = NEW_RC_OBJ(RawMemAllocator, "SwapChainGLImpl instance", SwapChainGLImpl)(CreationAttribs, SCDesc, pRenderDeviceOpenGL, pDeviceContextOpenGL );
123+
TSwapChain *pSwapChainGL = NEW_RC_OBJ(RawMemAllocator, "SwapChainGLImpl instance", TSwapChain)(CreationAttribs, SCDesc, pRenderDeviceOpenGL, pDeviceContextOpenGL );
116124
pSwapChainGL->QueryInterface(IID_SwapChain, reinterpret_cast<IObject**>(ppSwapChain) );
117125

118126
pDeviceContextOpenGL->SetSwapChain(pSwapChainGL);

Graphics/GraphicsEngineOpenGL/src/SwapChainGLIOS.mm

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,119 @@
2222
*/
2323

2424
#include "pch.h"
25+
26+
#import <QuartzCore/QuartzCore.h>
27+
#import <OpenGLES/EAGL.h>
28+
#import <OpenGLES/EAGLDrawable.h>
29+
30+
#include "DeviceContextGLImpl.h"
31+
#include "RenderDeviceGLImpl.h"
2532
#include "SwapChainGLIOS.h"
33+
#include "EngineGLAttribs.h"
2634

2735
namespace Diligent
2836
{
2937
SwapChainGLIOS::SwapChainGLIOS(IReferenceCounters *pRefCounters,
30-
const SwapChainDesc& SCDesc,
31-
RenderDeviceGLImpl* pRenderDeviceGL,
32-
DeviceContextGLImpl* pImmediateContextGL) :
33-
SwapChainGLImpl( pRefCounters, SCDesc, pRenderDeviceGL, pImmediateContextGL )
38+
const EngineGLAttribs &InitAttribs,
39+
const SwapChainDesc& SCDesc,
40+
RenderDeviceGLImpl* pRenderDeviceGL,
41+
DeviceContextGLImpl* pImmediateContextGL) :
42+
TSwapChainBase( pRefCounters, pRenderDeviceGL, pImmediateContextGL, SCDesc),
43+
m_ColorRenderBuffer(false),
44+
m_DepthRenderBuffer(false),
45+
m_DefaultFBO(false)
3446
{
47+
m_CALayer = InitAttribs.pNativeWndHandle;
48+
InitRenderBuffers(true, m_SwapChainDesc.Width, m_SwapChainDesc.Height);
3549
}
3650

51+
IMPLEMENT_QUERY_INTERFACE( SwapChainGLIOS, IID_SwapChainGL, TSwapChainBase )
52+
3753
void SwapChainGLIOS::Present()
3854
{
55+
EAGLContext* context = [EAGLContext currentContext];
56+
glBindRenderbuffer(GL_RENDERBUFFER, m_ColorRenderBuffer);
57+
[context presentRenderbuffer:GL_RENDERBUFFER];
3958
//auto *pDeviceGL = ValidatedCast<RenderDeviceGLImpl>(m_pRenderDevice.RawPtr());
4059
//pDeviceGL->m_GLContext.SwapBuffers();
4160
}
4261

43-
void SwapChainGLIOS::Resize( Uint32 NewWidth, Uint32 NewHeight )
62+
void SwapChainGLIOS::InitRenderBuffers(bool InitFromDrawable, Uint32 &Width, Uint32 &Height)
4463
{
45-
if( NewWidth != 0 && NewHeight != 0 &&
46-
(m_SwapChainDesc.Width != NewWidth || m_SwapChainDesc.Height != NewHeight) )
64+
EAGLContext* context = [EAGLContext currentContext];
65+
66+
m_DefaultFBO.Release();
67+
m_DefaultFBO.Create();
68+
glBindFramebuffer(GL_FRAMEBUFFER, m_DefaultFBO);
69+
70+
m_ColorRenderBuffer.Release();
71+
m_ColorRenderBuffer.Create();
72+
glBindRenderbuffer(GL_RENDERBUFFER, m_ColorRenderBuffer);
73+
74+
if(InitFromDrawable)
75+
{
76+
// This call associates the storage for the current render buffer with the
77+
// EAGLDrawable (our CAEAGLLayer) allowing us to draw into a buffer that
78+
// will later be rendered to the screen wherever the layer is (which
79+
// corresponds with our view).
80+
id<EAGLDrawable> drawable = (__bridge id<EAGLDrawable>)m_CALayer;
81+
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:drawable];
82+
}
83+
else
84+
{
85+
CAEAGLLayer* layer = (__bridge CAEAGLLayer*)m_CALayer;
86+
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
87+
}
88+
89+
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_ColorRenderBuffer);
90+
91+
// Get the drawable buffer's width and height so we can create a depth buffer for the FBO
92+
GLint backingWidth;
93+
GLint backingHeight;
94+
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
95+
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
96+
Width = backingWidth;
97+
Height = backingHeight;
98+
99+
// Create a depth buffer to use with our drawable FBO
100+
m_DepthRenderBuffer.Release();
101+
m_DepthRenderBuffer.Create();
102+
glBindRenderbuffer(GL_RENDERBUFFER, m_DepthRenderBuffer);
103+
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
104+
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_DepthRenderBuffer);
105+
106+
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
47107
{
48-
108+
LOG_ERROR_AND_THROW("Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
49109
}
110+
}
111+
112+
void SwapChainGLIOS::Resize( Uint32 NewWidth, Uint32 NewHeight )
113+
{
114+
InitRenderBuffers(false, NewWidth, NewHeight);
50115

51-
SwapChainGLImpl::Resize(NewWidth, NewHeight);
116+
if( TSwapChainBase::Resize( NewWidth, NewHeight ) )
117+
{
118+
auto pDeviceContext = m_wpDeviceContext.Lock();
119+
VERIFY( pDeviceContext, "Immediate context has been released" );
120+
if( pDeviceContext )
121+
{
122+
auto *pImmediateCtxGL = ValidatedCast<DeviceContextGLImpl>( pDeviceContext.RawPtr() );
123+
bool bIsDefaultFBBound = pImmediateCtxGL->IsDefaultFBBound();
124+
125+
// To update the viewport is the only thing we need to do in OpenGL
126+
if( bIsDefaultFBBound )
127+
{
128+
// Update viewport
129+
pImmediateCtxGL->SetViewports( 1, nullptr, 0, 0 );
130+
}
131+
}
132+
}
52133
}
53134

135+
GLuint SwapChainGLIOS::GetDefaultFBO()const
136+
{
137+
return m_DefaultFBO;
138+
}
139+
54140
}

0 commit comments

Comments
 (0)