Skip to content

Commit

Permalink
gl_rendertarget_phase_flip: Render the scene up-side down and flip it…
Browse files Browse the repository at this point in the history
… as a last step.

Allows for easier compatibility with DirectX.
  • Loading branch information
CrossVR committed Feb 28, 2016
1 parent dccc7ae commit 9ca7717
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/Layers/xrRender/HW.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class CHW
public:
CHW* pDevice;
CHW* pContext;
CHW* m_pSwapChain;
GLuint pBaseRT;
GLuint pBaseZB;
GLuint pPP;
Expand Down Expand Up @@ -137,6 +138,7 @@ class CHW
// TODO: OGL: Implement this into a compatibility layer?
void ClearRenderTargetView(GLuint pRenderTargetView, const FLOAT ColorRGBA[4]);
void ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil);
HRESULT Present(UINT SyncInterval, UINT Flags);
#endif // USE_OGL


Expand Down
4 changes: 1 addition & 3 deletions src/Layers/xrRender/r__dsgraph_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,7 @@ void D3DXRenderBase::End()
RCache.OnFrameEnd();
Memory.dbg_check();
DoAsyncScreenshot();
#if defined(USE_OGL)
SwapBuffers(HW.m_hDC);
#elif defined(USE_DX10) || defined(USE_DX11)
#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL)
HW.m_pSwapChain->Present(0, 0);
#else
CHK_DX(HW.pDevice->EndScene());
Expand Down
20 changes: 16 additions & 4 deletions src/Layers/xrRenderGL/glHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CHW::CHW() :
m_hRC(NULL),
pDevice(this),
pContext(this),
m_pSwapChain(this),
m_move_window(true),
pBaseRT(0),
pBaseZB(0),
Expand Down Expand Up @@ -132,7 +133,7 @@ void CHW::CreateDevice( HWND hWnd, bool move_window )
// Clip control ensures compatibility with D3D device coordinates.
// TODO: OGL: Also use GL_UPPER_LEFT to match D3D.
// TODO: OGL: Fix these differences in the blenders/shaders.
CHK_GL(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE));
CHK_GL(glClipControl(GL_UPPER_LEFT, GL_ZERO_TO_ONE));

// Create render target and depth-stencil views here
UpdateViews();
Expand Down Expand Up @@ -346,11 +347,12 @@ void CHW::UpdateViews()
// Create the clear framebuffer
glGenFramebuffers(1, &pCFB);

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

// Create Depth/stencil buffer
// Create depth/stencil buffer
glGenTextures(1, &HW.pBaseZB);
CHK_GL(glBindTexture(GL_TEXTURE_2D, HW.pBaseZB));
CHK_GL(glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, psCurrentVidMode[0], psCurrentVidMode[1]));
Expand All @@ -372,6 +374,8 @@ void CHW::ClearRenderTargetView(GLuint pRenderTargetView, const FLOAT ColorRGBA[
glClearColor(ColorRGBA[0], ColorRGBA[1], ColorRGBA[2], ColorRGBA[3]);
CHK_GL(glClear(GL_COLOR_BUFFER_BIT));
glPopAttrib();

RCache.set_FB(pFB);
}

void CHW::ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil)
Expand Down Expand Up @@ -403,5 +407,13 @@ void CHW::ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT
}
CHK_GL(glClear((ClearBufferMask)mask));
glPopAttrib();

RCache.set_FB(pFB);
}

HRESULT CHW::Present(UINT SyncInterval, UINT Flags)
{
RImplementation.Target->phase_flip();
return SwapBuffers(m_hDC) ? S_OK : E_FAIL;
}
#endif
15 changes: 6 additions & 9 deletions src/Layers/xrRenderPC_GL/gl_rendertarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void CRenderTarget::u_setrt(const ref_rt& _1, const ref_rt& _2, const ref_rt& _3
dwHeight = _1->dwHeight;
GLuint cnt = 0;
GLenum buffers[3] = { GL_NONE };
RCache.set_FB(HW.pFB);
if (_1)
{
buffers[cnt++] = GL_COLOR_ATTACHMENT0;
Expand Down Expand Up @@ -51,7 +50,6 @@ void CRenderTarget::u_setrt(const ref_rt& _1, const ref_rt& _2, GLuint zb)
dwHeight = _1->dwHeight;
GLuint cnt = 0;
GLenum buffers[2] = { GL_NONE };
RCache.set_FB(HW.pFB);
if (_1)
{
buffers[cnt++] = GL_COLOR_ATTACHMENT0;
Expand All @@ -75,13 +73,6 @@ void CRenderTarget::u_setrt(u32 W, u32 H, GLuint _1, GLuint _2, GLuint _3, GLuin
dwHeight = H;
GLuint cnt = 0;
GLenum buffers[3] = { GL_NONE };
if (_1 == HW.pBaseRT)
{
RCache.set_FB();
return;
}

RCache.set_FB(HW.pFB);
if (_1)
{
buffers[cnt++] = GL_COLOR_ATTACHMENT0;
Expand Down Expand Up @@ -844,6 +835,12 @@ CRenderTarget::CRenderTarget ()
s_menu.create ("distort");
g_menu.create (FVF::F_TL,RCache.Vertex.Buffer(),RCache.QuadIB);

// Flip
t_base = RImplementation.Resources->_CreateTexture (r2_base);
t_base->surface_set (GL_TEXTURE_2D, HW.pBaseRT);
s_flip.create ("effects\\screen_set", r2_base);
g_flip.create (FVF::F_TL, RCache.Vertex.Buffer(), RCache.QuadIB);

//
dwWidth = Device.dwWidth;
dwHeight = Device.dwHeight;
Expand Down
5 changes: 5 additions & 0 deletions src/Layers/xrRenderPC_GL/gl_rendertarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class CRenderTarget : public IRender_Target
ref_texture t_noise [TEX_jitter_count];
GLuint t_noise_surf_mipped;
ref_texture t_noise_mipped;

ref_texture t_base;
private:
// OCCq

Expand Down Expand Up @@ -192,6 +194,8 @@ class CRenderTarget : public IRender_Target
ref_geom g_postprocess;
ref_shader s_menu;
ref_geom g_menu;
ref_shader s_flip;
ref_geom g_flip;
private:
float im_noise_time;
u32 im_noise_shift_w;
Expand Down Expand Up @@ -290,6 +294,7 @@ class CRenderTarget : public IRender_Target
void phase_combine ();
void phase_combine_volumetric();
void phase_pp ();
void phase_flip ();

virtual void set_blur (float f) { param_blur=f; }
virtual void set_gray (float f) { param_gray=f; }
Expand Down
36 changes: 36 additions & 0 deletions src/Layers/xrRenderPC_GL/gl_rendertarget_phase_flip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "stdafx.h"

void CRenderTarget::phase_flip ()
{
PIX_EVENT(phase_flip);

// Switch to backbuffer
RCache.set_FB();

// common calc for quad-rendering
u32 Offset;
u32 C = color_rgba (255,255,255,255);
float _w = float (Device.dwWidth);
float _h = float (Device.dwHeight);
Fvector2 p0,p1;
p0.set (.5f/_w, .5f/_h);
p1.set ((_w+.5f)/_w, (_h+.5f)/_h );
float d_Z = EPS_S, d_W = 1.f;

// Fill vertex buffer
FVF::TL* pv = (FVF::TL*) RCache.Vertex.Lock (4,g_flip->vb_stride,Offset);
pv->set (EPS, float(_h+EPS), d_Z, d_W, C, p0.x, p0.y); pv++;
pv->set (EPS, EPS, d_Z, d_W, C, p0.x, p1.y); pv++;
pv->set (float(_w+EPS), float(_h+EPS), d_Z, d_W, C, p1.x, p0.y); pv++;
pv->set (float(_w+EPS), EPS, d_Z, d_W, C, p1.x, p1.y); pv++;
RCache.Vertex.Unlock (4,g_flip->vb_stride);

// Draw
RCache.set_Element (s_flip->E[0] );
RCache.set_Geometry (g_flip );

RCache.Render (D3DPT_TRIANGLELIST,Offset,0,4,0,2);

// Switch to framebuffer
RCache.set_FB(HW.pFB);
}
2 changes: 2 additions & 0 deletions src/Layers/xrRenderPC_GL/r2_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#define r2_jitter_mipped "$user$jitter_mipped" // --- dither
#define r2_sunmask "sunmask"

#define r2_base "$user$base"

#define JITTER(a) r2_jitter #a

const float SMAP_near_plane = .1f ;
Expand Down
1 change: 1 addition & 0 deletions src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
<ClCompile Include="gl_rendertarget_phase_accumulator.cpp" />
<ClCompile Include="gl_rendertarget_phase_bloom.cpp" />
<ClCompile Include="gl_rendertarget_phase_combine.cpp" />
<ClCompile Include="gl_rendertarget_phase_flip.cpp" />
<ClCompile Include="gl_rendertarget_phase_luminance.cpp" />
<ClCompile Include="gl_rendertarget_phase_occq.cpp" />
<ClCompile Include="gl_rendertarget_phase_PP.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@
<ClCompile Include="..\xrRenderGL\MSAA\glMSAABlender.cpp">
<Filter>Interface implementations\MSAARender</Filter>
</ClCompile>
<ClCompile Include="gl_rendertarget_phase_flip.cpp">
<Filter>Core_Target</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
Expand Down

0 comments on commit 9ca7717

Please sign in to comment.