Skip to content

Commit 9ca7717

Browse files
committed
gl_rendertarget_phase_flip: Render the scene up-side down and flip it as a last step.
Allows for easier compatibility with DirectX.
1 parent dccc7ae commit 9ca7717

File tree

9 files changed

+72
-16
lines changed

9 files changed

+72
-16
lines changed

src/Layers/xrRender/HW.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CHW
5959
public:
6060
CHW* pDevice;
6161
CHW* pContext;
62+
CHW* m_pSwapChain;
6263
GLuint pBaseRT;
6364
GLuint pBaseZB;
6465
GLuint pPP;
@@ -137,6 +138,7 @@ class CHW
137138
// TODO: OGL: Implement this into a compatibility layer?
138139
void ClearRenderTargetView(GLuint pRenderTargetView, const FLOAT ColorRGBA[4]);
139140
void ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil);
141+
HRESULT Present(UINT SyncInterval, UINT Flags);
140142
#endif // USE_OGL
141143

142144

src/Layers/xrRender/r__dsgraph_build.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,9 +1077,7 @@ void D3DXRenderBase::End()
10771077
RCache.OnFrameEnd();
10781078
Memory.dbg_check();
10791079
DoAsyncScreenshot();
1080-
#if defined(USE_OGL)
1081-
SwapBuffers(HW.m_hDC);
1082-
#elif defined(USE_DX10) || defined(USE_DX11)
1080+
#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL)
10831081
HW.m_pSwapChain->Present(0, 0);
10841082
#else
10851083
CHK_DX(HW.pDevice->EndScene());

src/Layers/xrRenderGL/glHW.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ CHW::CHW() :
3838
m_hRC(NULL),
3939
pDevice(this),
4040
pContext(this),
41+
m_pSwapChain(this),
4142
m_move_window(true),
4243
pBaseRT(0),
4344
pBaseZB(0),
@@ -132,7 +133,7 @@ void CHW::CreateDevice( HWND hWnd, bool move_window )
132133
// Clip control ensures compatibility with D3D device coordinates.
133134
// TODO: OGL: Also use GL_UPPER_LEFT to match D3D.
134135
// TODO: OGL: Fix these differences in the blenders/shaders.
135-
CHK_GL(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE));
136+
CHK_GL(glClipControl(GL_UPPER_LEFT, GL_ZERO_TO_ONE));
136137

137138
// Create render target and depth-stencil views here
138139
UpdateViews();
@@ -346,11 +347,12 @@ void CHW::UpdateViews()
346347
// Create the clear framebuffer
347348
glGenFramebuffers(1, &pCFB);
348349

349-
// Create a render target view
350-
// We reserve a texture name to represent GL_BACK
350+
// Create a color render target
351351
glGenTextures(1, &HW.pBaseRT);
352+
CHK_GL(glBindTexture(GL_TEXTURE_2D, HW.pBaseRT));
353+
CHK_GL(glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, psCurrentVidMode[0], psCurrentVidMode[1]));
352354

353-
// Create Depth/stencil buffer
355+
// Create depth/stencil buffer
354356
glGenTextures(1, &HW.pBaseZB);
355357
CHK_GL(glBindTexture(GL_TEXTURE_2D, HW.pBaseZB));
356358
CHK_GL(glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, psCurrentVidMode[0], psCurrentVidMode[1]));
@@ -372,6 +374,8 @@ void CHW::ClearRenderTargetView(GLuint pRenderTargetView, const FLOAT ColorRGBA[
372374
glClearColor(ColorRGBA[0], ColorRGBA[1], ColorRGBA[2], ColorRGBA[3]);
373375
CHK_GL(glClear(GL_COLOR_BUFFER_BIT));
374376
glPopAttrib();
377+
378+
RCache.set_FB(pFB);
375379
}
376380

377381
void CHW::ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil)
@@ -403,5 +407,13 @@ void CHW::ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT
403407
}
404408
CHK_GL(glClear((ClearBufferMask)mask));
405409
glPopAttrib();
410+
411+
RCache.set_FB(pFB);
412+
}
413+
414+
HRESULT CHW::Present(UINT SyncInterval, UINT Flags)
415+
{
416+
RImplementation.Target->phase_flip();
417+
return SwapBuffers(m_hDC) ? S_OK : E_FAIL;
406418
}
407419
#endif

src/Layers/xrRenderPC_GL/gl_rendertarget.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ void CRenderTarget::u_setrt(const ref_rt& _1, const ref_rt& _2, const ref_rt& _3
2121
dwHeight = _1->dwHeight;
2222
GLuint cnt = 0;
2323
GLenum buffers[3] = { GL_NONE };
24-
RCache.set_FB(HW.pFB);
2524
if (_1)
2625
{
2726
buffers[cnt++] = GL_COLOR_ATTACHMENT0;
@@ -51,7 +50,6 @@ void CRenderTarget::u_setrt(const ref_rt& _1, const ref_rt& _2, GLuint zb)
5150
dwHeight = _1->dwHeight;
5251
GLuint cnt = 0;
5352
GLenum buffers[2] = { GL_NONE };
54-
RCache.set_FB(HW.pFB);
5553
if (_1)
5654
{
5755
buffers[cnt++] = GL_COLOR_ATTACHMENT0;
@@ -75,13 +73,6 @@ void CRenderTarget::u_setrt(u32 W, u32 H, GLuint _1, GLuint _2, GLuint _3, GLuin
7573
dwHeight = H;
7674
GLuint cnt = 0;
7775
GLenum buffers[3] = { GL_NONE };
78-
if (_1 == HW.pBaseRT)
79-
{
80-
RCache.set_FB();
81-
return;
82-
}
83-
84-
RCache.set_FB(HW.pFB);
8576
if (_1)
8677
{
8778
buffers[cnt++] = GL_COLOR_ATTACHMENT0;
@@ -844,6 +835,12 @@ CRenderTarget::CRenderTarget ()
844835
s_menu.create ("distort");
845836
g_menu.create (FVF::F_TL,RCache.Vertex.Buffer(),RCache.QuadIB);
846837

838+
// Flip
839+
t_base = RImplementation.Resources->_CreateTexture (r2_base);
840+
t_base->surface_set (GL_TEXTURE_2D, HW.pBaseRT);
841+
s_flip.create ("effects\\screen_set", r2_base);
842+
g_flip.create (FVF::F_TL, RCache.Vertex.Buffer(), RCache.QuadIB);
843+
847844
//
848845
dwWidth = Device.dwWidth;
849846
dwHeight = Device.dwHeight;

src/Layers/xrRenderPC_GL/gl_rendertarget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class CRenderTarget : public IRender_Target
106106
ref_texture t_noise [TEX_jitter_count];
107107
GLuint t_noise_surf_mipped;
108108
ref_texture t_noise_mipped;
109+
110+
ref_texture t_base;
109111
private:
110112
// OCCq
111113

@@ -192,6 +194,8 @@ class CRenderTarget : public IRender_Target
192194
ref_geom g_postprocess;
193195
ref_shader s_menu;
194196
ref_geom g_menu;
197+
ref_shader s_flip;
198+
ref_geom g_flip;
195199
private:
196200
float im_noise_time;
197201
u32 im_noise_shift_w;
@@ -290,6 +294,7 @@ class CRenderTarget : public IRender_Target
290294
void phase_combine ();
291295
void phase_combine_volumetric();
292296
void phase_pp ();
297+
void phase_flip ();
293298

294299
virtual void set_blur (float f) { param_blur=f; }
295300
virtual void set_gray (float f) { param_gray=f; }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "stdafx.h"
2+
3+
void CRenderTarget::phase_flip ()
4+
{
5+
PIX_EVENT(phase_flip);
6+
7+
// Switch to backbuffer
8+
RCache.set_FB();
9+
10+
// common calc for quad-rendering
11+
u32 Offset;
12+
u32 C = color_rgba (255,255,255,255);
13+
float _w = float (Device.dwWidth);
14+
float _h = float (Device.dwHeight);
15+
Fvector2 p0,p1;
16+
p0.set (.5f/_w, .5f/_h);
17+
p1.set ((_w+.5f)/_w, (_h+.5f)/_h );
18+
float d_Z = EPS_S, d_W = 1.f;
19+
20+
// Fill vertex buffer
21+
FVF::TL* pv = (FVF::TL*) RCache.Vertex.Lock (4,g_flip->vb_stride,Offset);
22+
pv->set (EPS, float(_h+EPS), d_Z, d_W, C, p0.x, p0.y); pv++;
23+
pv->set (EPS, EPS, d_Z, d_W, C, p0.x, p1.y); pv++;
24+
pv->set (float(_w+EPS), float(_h+EPS), d_Z, d_W, C, p1.x, p0.y); pv++;
25+
pv->set (float(_w+EPS), EPS, d_Z, d_W, C, p1.x, p1.y); pv++;
26+
RCache.Vertex.Unlock (4,g_flip->vb_stride);
27+
28+
// Draw
29+
RCache.set_Element (s_flip->E[0] );
30+
RCache.set_Geometry (g_flip );
31+
32+
RCache.Render (D3DPT_TRIANGLELIST,Offset,0,4,0,2);
33+
34+
// Switch to framebuffer
35+
RCache.set_FB(HW.pFB);
36+
}

src/Layers/xrRenderPC_GL/r2_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#define r2_jitter_mipped "$user$jitter_mipped" // --- dither
5050
#define r2_sunmask "sunmask"
5151

52+
#define r2_base "$user$base"
53+
5254
#define JITTER(a) r2_jitter #a
5355

5456
const float SMAP_near_plane = .1f ;

src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@
268268
<ClCompile Include="gl_rendertarget_phase_accumulator.cpp" />
269269
<ClCompile Include="gl_rendertarget_phase_bloom.cpp" />
270270
<ClCompile Include="gl_rendertarget_phase_combine.cpp" />
271+
<ClCompile Include="gl_rendertarget_phase_flip.cpp" />
271272
<ClCompile Include="gl_rendertarget_phase_luminance.cpp" />
272273
<ClCompile Include="gl_rendertarget_phase_occq.cpp" />
273274
<ClCompile Include="gl_rendertarget_phase_PP.cpp" />

src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@
681681
<ClCompile Include="..\xrRenderGL\MSAA\glMSAABlender.cpp">
682682
<Filter>Interface implementations\MSAARender</Filter>
683683
</ClCompile>
684+
<ClCompile Include="gl_rendertarget_phase_flip.cpp">
685+
<Filter>Core_Target</Filter>
686+
</ClCompile>
684687
</ItemGroup>
685688
<ItemGroup>
686689
<ClInclude Include="stdafx.h">

0 commit comments

Comments
 (0)