diff --git a/src/Layers/xrRender/HW.h b/src/Layers/xrRender/HW.h index 1a9b54cff99..a130b721815 100644 --- a/src/Layers/xrRender/HW.h +++ b/src/Layers/xrRender/HW.h @@ -58,7 +58,10 @@ class CHW #if defined(USE_OGL) public: CHW* pDevice; + GLuint pBaseRT; GLuint pBaseZB; + GLuint pFB; + GLuint pPP; CHWCaps Caps; @@ -128,6 +131,12 @@ class CHW virtual void OnAppActivate(); virtual void OnAppDeactivate(); #endif +#ifdef USE_OGL + // 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); +#endif // USE_OGL + private: bool m_move_window; diff --git a/src/Layers/xrRender/R_Backend.cpp b/src/Layers/xrRender/R_Backend.cpp index 55f3f444279..7c4aed3f65b 100644 --- a/src/Layers/xrRender/R_Backend.cpp +++ b/src/Layers/xrRender/R_Backend.cpp @@ -161,10 +161,6 @@ void CBackend::OnDeviceCreate () #if defined(USE_DX10) || defined(USE_DX11) //CreateConstantBuffers(); #endif // USE_DX10 -#ifdef USE_OGL - // Create the program pipeline used for rendering with shaders - glGenProgramPipelines(1, &pp); -#endif // USE_OGL CreateQuadIB (); @@ -193,9 +189,6 @@ void CBackend::OnDeviceDestroy() #if defined(USE_DX10) || defined(USE_DX11) //DestroyConstantBuffers(); #endif // USE_DX10 -#ifdef USE_OGL - glDeleteProgramPipelines(1, &pp); -#endif // USE_OGL } #if defined(USE_DX10) || defined(USE_DX11) diff --git a/src/Layers/xrRender/R_Backend.h b/src/Layers/xrRender/R_Backend.h index b56472febf7..b5c08999d8c 100644 --- a/src/Layers/xrRender/R_Backend.h +++ b/src/Layers/xrRender/R_Backend.h @@ -93,7 +93,6 @@ class ECORE_API CBackend private: // Render-targets #ifdef USE_OGL - GLuint pFB; GLuint pRT[4]; GLuint pZB; #else @@ -123,7 +122,6 @@ class ECORE_API CBackend // Shaders/State #ifdef USE_OGL SState* state; - GLuint pp; GLuint ps; GLuint vs; #else @@ -253,10 +251,8 @@ class ECORE_API CBackend IC const Fmatrix& get_xform_project (); #ifdef USE_OGL - IC void set_FB (GLuint FB=0); IC void set_RT (GLuint RT, u32 ID=0); IC void set_ZB (GLuint ZB); - IC GLuint get_FB (); IC GLuint get_RT (u32 ID=0); IC GLuint get_ZB (); #else diff --git a/src/Layers/xrRender/xrD3DDefs.h b/src/Layers/xrRender/xrD3DDefs.h index 6b76ab8edfa..593c7793a62 100644 --- a/src/Layers/xrRender/xrD3DDefs.h +++ b/src/Layers/xrRender/xrD3DDefs.h @@ -3,11 +3,18 @@ #pragma once #if defined(USE_OGL) -using namespace gl; -class glState; + // TODO: Get rid of D3D types. #include +using namespace gl; +class glState; + +typedef enum D3D_CLEAR_FLAG { + D3D_CLEAR_DEPTH = 0x1L, + D3D_CLEAR_STENCIL = 0x2L +} D3D_CLEAR_FLAG; + #define DX10_ONLY(expr) do {} while (0) #elif defined(USE_DX11) || defined(USE_DX10) diff --git a/src/Layers/xrRenderGL/glHW.cpp b/src/Layers/xrRenderGL/glHW.cpp index 47a25994bda..107ef49aa45 100644 --- a/src/Layers/xrRenderGL/glHW.cpp +++ b/src/Layers/xrRenderGL/glHW.cpp @@ -38,7 +38,10 @@ CHW::CHW() : m_hRC(NULL), pDevice(this), m_move_window(true), - pBaseZB(0) + pBaseRT(0), + pBaseZB(0), + pPP(0), + pFB(0) { } @@ -129,6 +132,9 @@ void CHW::CreateDevice( HWND hWnd, bool move_window ) // TODO: Fix these differences in the blenders/shaders. CHK_GL(glClipControl(GL_UPPER_LEFT, GL_ZERO_TO_ONE)); + // Create render target and depth-stencil views here + UpdateViews(); + #ifndef _EDITOR updateWindowProps (m_hWnd); fill_vid_mode_list (this); @@ -168,7 +174,11 @@ void CHW::Reset (HWND hwnd) { BOOL bWindowed = !psDeviceFlags.is (rsFullscreen); - CHK_GL(glDeleteTextures(1, &HW.pBaseZB)); + CHK_GL(glDeleteProgramPipelines(1, &pPP)); + CHK_GL(glDeleteFramebuffers(1, &pFB)); + + CHK_GL(glDeleteTextures(1, &pBaseRT)); + CHK_GL(glDeleteTextures(1, &pBaseZB)); UpdateViews(); @@ -322,9 +332,57 @@ void fill_vid_mode_list() void CHW::UpdateViews() { - // Create an staging depth buffer used for post-processing + // Create the program pipeline used for rendering with shaders + glGenProgramPipelines(1, &pPP); + CHK_GL(glBindProgramPipeline(pPP)); + + // Create the framebuffer + glGenFramebuffers(1, &pFB); + CHK_GL(glBindFramebuffer(GL_FRAMEBUFFER, pFB)); + + // Create a render target view + // We reserve a texture name to represent GL_BACK + glGenTextures(1, &HW.pBaseRT); + + // 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, Device.dwWidth, Device.dwHeight)); } + + +void CHW::ClearRenderTargetView(GLuint pRenderTargetView, const FLOAT ColorRGBA[4]) +{ + // TODO: OGL: Bind the RT to a clear frame buffer. + glPushAttrib(GL_COLOR_BUFFER_BIT); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glClearColor(ColorRGBA[0], ColorRGBA[1], ColorRGBA[2], ColorRGBA[3]); + CHK_GL(glClear(GL_COLOR_BUFFER_BIT)); + glPopAttrib(); +} + +void CHW::ClearDepthStencilView(GLuint pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil) +{ + // TODO: OGL: Bind the DS to a clear frame buffer. + u32 mask = 0; + if (ClearFlags & D3D_CLEAR_DEPTH) + mask |= (u32)GL_DEPTH_BUFFER_BIT; + if (ClearFlags & D3D_CLEAR_STENCIL) + mask |= (u32)GL_STENCIL_BUFFER_BIT; + + + glPushAttrib((AttribMask)mask); + if (ClearFlags & D3DCLEAR_ZBUFFER) + { + glDepthMask(GL_TRUE); + glClearDepthf(Depth); + } + if (ClearFlags & D3DCLEAR_STENCIL) + { + glStencilMask(~0); + glClearStencil(Stencil); + } + CHK_GL(glClear((ClearBufferMask)mask)); + glPopAttrib(); +} #endif diff --git a/src/Layers/xrRenderGL/glR_Backend_Runtime.h b/src/Layers/xrRenderGL/glR_Backend_Runtime.h index 06268d287c9..248855207f1 100644 --- a/src/Layers/xrRenderGL/glR_Backend_Runtime.h +++ b/src/Layers/xrRenderGL/glR_Backend_Runtime.h @@ -4,11 +4,6 @@ #include "glStateUtils.h" -IC GLuint CBackend::get_FB() -{ - return pFB; -} - IC void CBackend::set_xform(u32 ID, const Fmatrix& M) { stat.xforms++; @@ -16,19 +11,6 @@ IC void CBackend::set_xform(u32 ID, const Fmatrix& M) //VERIFY(!"Implement CBackend::set_xform"); } -IC void CBackend::set_FB(GLuint FB) -{ - if (FB != pFB) - { - PGO(Msg("PGO:set_FB")); - pFB = FB; - CHK_GL(glBindFramebuffer(GL_FRAMEBUFFER, pFB)); - - // Clear cached attachments - pRT[0] = pRT[1] = pRT[2] = pRT[3] = pZB = NULL; - } -} - IC void CBackend::set_RT(GLuint RT, u32 ID) { if (RT != pRT[ID]) @@ -76,7 +58,7 @@ ICF void CBackend::set_PS(GLuint _ps, LPCSTR _n) PGO(Msg("PGO:Pshader:%d,%s", _ps, _n ? _n : name)); stat.ps++; ps = _ps; - CHK_GL(glUseProgramStages(pp, GL_FRAGMENT_SHADER_BIT, ps)); + CHK_GL(glUseProgramStages(HW.pPP, GL_FRAGMENT_SHADER_BIT, ps)); #ifdef DEBUG ps_name = _n; #endif @@ -92,7 +74,7 @@ ICF void CBackend::set_VS(GLuint _vs, LPCSTR _n) PGO(Msg("PGO:Vshader:%d,%s", _vs, _n ? _n : name)); stat.vs++; vs = _vs; - CHK_GL(glUseProgramStages(pp, GL_VERTEX_SHADER_BIT, vs)); + CHK_GL(glUseProgramStages(HW.pPP, GL_VERTEX_SHADER_BIT, vs)); #ifdef DEBUG vs_name = _n; #endif @@ -179,7 +161,6 @@ ICF void CBackend::Render(D3DPRIMITIVETYPE T, u32 baseV, u32 startV, u32 countV, stat.verts += countV; stat.polys += PC; constants.flush(); - CHK_GL(glBindProgramPipeline(pp)); CHK_GL(glDrawElementsBaseVertex(Topology, iIndexCount, GL_UNSIGNED_SHORT, (void*)(startI * sizeof(GLushort)), baseV)); PGO(Msg("PGO:DIP:%dv/%df", countV, PC)); } @@ -193,7 +174,6 @@ ICF void CBackend::Render(D3DPRIMITIVETYPE T, u32 startV, u32 PC) stat.verts += iIndexCount; stat.polys += PC; constants.flush(); - CHK_GL(glBindProgramPipeline(pp)); CHK_GL(glDrawArrays(Topology, startV, iIndexCount)); PGO(Msg("PGO:DIP:%dv/%df", iIndexCount, PC)); }