diff --git a/source/blood/src/view.cpp b/source/blood/src/view.cpp index 8f696ddb0..27307c729 100644 --- a/source/blood/src/view.cpp +++ b/source/blood/src/view.cpp @@ -3242,7 +3242,6 @@ void viewDrawScreen(bool sceneonly) } r enderSetTarget(4079, 128, 128); renderSetAspect(65536, 78643); - renderBeginScene(); int vd8 = pOther->pSprite->x; int vd4 = pOther->pSprite->y; int vd0 = pOther->zView; @@ -3308,7 +3307,6 @@ void viewDrawScreen(bool sceneonly) memcpy(gotpic+510, bakMirrorGotpic, 2); viewProcessSprites(vd8, vd4, vd0, v50, gInterpolate); renderDrawMasks(); - renderFinishScene(); renderRestoreTarget(); #endif } @@ -3350,7 +3348,6 @@ void viewDrawScreen(bool sceneonly) } nSprite = nextspritestat[nSprite]; } - renderBeginScene(); g_visibility = (int32_t)(ClipLow(gVisibility - 32 * gView->visibility - unk, 0) * (numplayers > 1 ? 1.f : r_ambientlightrecip)); cA = (cA + interpolateangfix16(fix16_from_int(deliriumTurnO), fix16_from_int(deliriumTurn), gInterpolate)) & 0x7ffffff; int vfc, vf8; @@ -3405,7 +3402,6 @@ void viewDrawScreen(bool sceneonly) sub_557C4(cX, cY, gInterpolate); renderDrawMasks(); gView->pSprite->cstat = bakCstat; - renderFinishScene(); if ((v78 || bDelirium) && !sceneonly) { diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index e2ad79057..acbddaf61 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -2618,6 +2618,7 @@ killsprite: sortsprites(0, spritesortcnt); sortsprites(spritesortcnt, numSprites); + renderBeginScene(); #ifdef USE_OPENGL if (videoGetRenderMode() == REND_POLYMOST) @@ -2809,17 +2810,9 @@ killsprite: } debugmask_add(maskwall[maskwallcnt], thewall[maskwall[maskwallcnt]]); -#ifdef USE_OPENGL - if (videoGetRenderMode() == REND_POLYMOST) - GLInterface.SetClamp(0); -#endif renderDrawMaskedWall(maskwallcnt); } -#ifdef USE_OPENGL - if (videoGetRenderMode() == REND_POLYMOST) - GLInterface.SetClamp(1+2); -#endif while (spritesortcnt) { --spritesortcnt; @@ -2830,14 +2823,10 @@ killsprite: tspriteptr[spritesortcnt] = NULL; } } -#ifdef USE_OPENGL - if (videoGetRenderMode() == REND_POLYMOST) - { - GLInterface.SetDepthMask(true); - GLInterface.SetClamp(0); - GLInterface.SetDepthBias(0, 0); - } -#endif + renderFinishScene(); + GLInterface.SetDepthMask(true); + GLInterface.SetClamp(0); + GLInterface.SetDepthBias(0, 0); } diff --git a/source/build/src/polymost.cpp b/source/build/src/polymost.cpp index 2e24c3557..834343ee8 100644 --- a/source/build/src/polymost.cpp +++ b/source/build/src/polymost.cpp @@ -208,7 +208,7 @@ static void resizeglcheck(void) m[2][2] = (farclip + nearclip) / (farclip - nearclip); m[2][3] = 1.f; m[3][2] = -(2.f * farclip * nearclip) / (farclip - nearclip); - GLInterface.SetMatrix(Matrix_Projection, &m[0][0]); + renderSetProjectionMatrix(&m[0][0]); GLInterface.SetIdentityMatrix(Matrix_Model); } @@ -283,7 +283,7 @@ static void polymost_updaterotmat(void) 0.f, 0.f, 0.f, 1.f, }; multiplyMatrix4f(matrix, tiltmatrix); - GLInterface.SetMatrix(Matrix_View, matrix); + renderSetViewMatrix(matrix); } static void polymost_flatskyrender(vec2f_t const* const dpxy, int32_t const n, int32_t method, const vec2_16_t& tilesiz); @@ -3279,6 +3279,8 @@ void polymost_drawrooms() grhalfxdown10x = grhalfxdown10; + renderBeginScene(); + if (inpreparemirror) { // see engine.c: INPREPAREMIRROR_NO_BUNCHES @@ -3295,6 +3297,7 @@ void polymost_drawrooms() } } + while (numbunches > 0) { Bmemset(ptempbuf,0,numbunches+3); ptempbuf[0] = 1; @@ -3327,6 +3330,7 @@ void polymost_drawrooms() bunchfirst[closest] = bunchfirst[numbunches]; bunchlast[closest] = bunchlast[numbunches]; } + renderFinishScene(); GLInterface.SetDepthFunc(Depth_LessEqual); } @@ -3601,6 +3605,7 @@ void polymost_prepareMirror(int32_t dax, int32_t day, int32_t daz, fix16_t daang polymost_updaterotmat(); grhalfxdown10x = grhalfxdown10; + renderBeginScene(); //POGO: write the mirror region to the stencil buffer to allow showing mirrors & skyboxes at the same time GLInterface.EnableStencilWrite(1); GLInterface.EnableAlphaTest(false); @@ -3608,6 +3613,7 @@ void polymost_prepareMirror(int32_t dax, int32_t day, int32_t daz, fix16_t daang polymost_drawmaskwallinternal(mirrorWall); GLInterface.EnableAlphaTest(true); GLInterface.EnableDepthTest(true); + renderFinishScene(); //POGO: render only to the mirror region GLInterface.EnableStencilTest(1); diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index f05f0a7dc..b9a4b1638 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -679,7 +679,6 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) "other values are reserved.\n"); #endif - renderBeginScene(); #ifdef LEGACY_ROR G_SE40(smoothRatio); #endif @@ -692,7 +691,6 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) yax_drawrooms(G_DoSpriteAnimations, pSprite->sectnum, 0, smoothRatio); G_DoSpriteAnimations(pSprite->x, pSprite->y, pSprite->z - ZOFFSET6, fix16_to_int(CAMERA(q16ang)), smoothRatio); renderDrawMasks(); - renderFinishScene(); } } else @@ -844,7 +842,6 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) Printf(TEXTCOLOR_RED "ERROR: EVENT_DISPLAYROOMS return value must be 0 or 1, " "other values are reserved.\n"); #endif - renderBeginScene(); G_HandleMirror(CAMERA(pos.x), CAMERA(pos.y), CAMERA(pos.z), CAMERA(q16ang), CAMERA(q16horiz), smoothRatio); G_ClearGotMirror(); @@ -874,7 +871,6 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) #endif renderDrawMasks(); #endif - renderFinishScene(); } } diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index 0200ad024..7d3a98b57 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -1218,7 +1218,6 @@ LUNATIC_EXTERN void G_ShowView(vec3_t vec, fix16_t a, fix16_t horiz, int sect, i renderSetAspect(viewingRange, yxAspect); int const smoothratio = calc_smoothratio(totalclock, ototalclock); G_DoInterpolations(smoothratio); - renderBeginScene(); if (!display_mirror) G_HandleMirror(vec.x, vec.y, vec.z, a, horiz, smoothratio); #ifdef POLYMER @@ -1233,7 +1232,6 @@ LUNATIC_EXTERN void G_ShowView(vec3_t vec, fix16_t a, fix16_t horiz, int sect, i G_DoSpriteAnimations(vec.x, vec.y, vec.z, fix16_to_int(a), smoothratio); display_mirror = 0; renderDrawMasks(); - renderFinishScene(); G_RestoreInterpolations(); G_UpdateScreenArea(); renderSetAspect(viewingRange, yxAspect); diff --git a/source/duke3d/src/sector.cpp b/source/duke3d/src/sector.cpp index 1855eb155..3c90156bc 100644 --- a/source/duke3d/src/sector.cpp +++ b/source/duke3d/src/sector.cpp @@ -401,7 +401,6 @@ static void G_SetupCamTile(int spriteNum, int tileNum, int smoothRatio) Printf(TEXTCOLOR_RED "ERROR: EVENT_DISPLAYROOMSCAMERATILE return value must be 0 or 1, " "other values are reserved.\n"); #endif - renderBeginScene(); yax_preparedrawrooms(); drawrooms(camera.x, camera.y, camera.z, SA(spriteNum), 100 + sprite[spriteNum].shade, SECT(spriteNum)); @@ -411,8 +410,7 @@ static void G_SetupCamTile(int spriteNum, int tileNum, int smoothRatio) G_DoSpriteAnimations(camera.x, camera.y, camera.z, SA(spriteNum), smoothRatio); display_mirror = saveMirror; renderDrawMasks(); - renderFinishScene(); - + finishTileSetup: renderRestoreTarget(); } diff --git a/source/exhumed/src/view.cpp b/source/exhumed/src/view.cpp index 53273ff53..32fe19ac4 100644 --- a/source/exhumed/src/view.cpp +++ b/source/exhumed/src/view.cpp @@ -494,11 +494,9 @@ void DrawView(int smoothRatio, bool sceneonly) } } - renderBeginScene(); renderDrawRoomsQ16(nCamerax, nCameray, viewz, nCameraa, nCamerapan, nSector); analyzesprites(); renderDrawMasks(); - renderFinishScene(); if (HavePLURemap()) { diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index e1b58b435..35f627fab 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -53,8 +53,10 @@ #include "build.h" #include "v_draw.h" #include "v_font.h" +#include "hw_viewpointuniforms.h" F2DDrawer twodpsp; +static int BufferLock = 0; float shadediv[MAXPALOOKUPS]; @@ -187,13 +189,13 @@ static GLint primtypes[] ={ GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_FAN, void GLInstance::Draw(EDrawType type, size_t start, size_t count) { + assert (BufferLock > 0); applyMapFog(); renderState.vindex = start; renderState.vcount = count; renderState.primtype = type; rendercommands.Push(renderState); clearMapFog(); - SetIdentityMatrix(Matrix_Texture); renderState.StateFlags &= ~(STF_CLEARCOLOR | STF_CLEARDEPTH | STF_VIEWPORTSET | STF_SCISSORSET); } @@ -232,12 +234,17 @@ void GLInstance::DoDraw() int GLInstance::SetMatrix(int num, const VSMatrix *mat) { int r = renderState.matrixIndex[num]; - if (num == Matrix_Projection) mProjectionM5 = mat->get()[5]; renderState.matrixIndex[num] = matrixArray.Size(); matrixArray.Push(*mat); return r; } +void GLInstance::SetIdentityMatrix(int num) +{ + renderState.matrixIndex[num] = 0; +} + + void GLInstance::ReadPixels(int xdim, int ydim, uint8_t* buffer) { glReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, buffer); @@ -492,10 +499,6 @@ void PolymostRenderState::Apply(PolymostShader* shader, GLState &oldState) shader->TintModulate.Set(hictint); shader->TintOverlay.Set(hictint_overlay); shader->FullscreenTint.Set(fullscreenTint); - if (matrixIndex[Matrix_View] != -1) - shader->RotMatrix.Set(matrixArray[matrixIndex[Matrix_View]].get()); - if (matrixIndex[Matrix_Projection] != -1) - shader->ProjectionMatrix.Set(matrixArray[matrixIndex[Matrix_Projection]].get()); if (matrixIndex[Matrix_Model] != -1) shader->ModelMatrix.Set(matrixArray[matrixIndex[Matrix_Model]].get()); if (matrixIndex[Matrix_Texture] != -1) @@ -569,12 +572,32 @@ void WriteSavePic(FileWriter* file, int width, int height) } -static int BufferLock = 0; +static HWViewpointUniforms vp; + +void renderSetProjectionMatrix(const float* p) +{ + if (p) + { + vp.mProjectionMatrix.loadMatrix(p); + GLInterface.mProjectionM5 = p[5]; + } + else vp.mProjectionMatrix.loadIdentity(); +} + +void renderSetViewMatrix(const float* p) +{ + if (p) vp.mViewMatrix.loadMatrix(p); + else vp.mViewMatrix.loadIdentity(); +} void renderBeginScene() { if (videoGetRenderMode() < REND_POLYMOST) return; - assert(BufferLock >= 0); + assert(BufferLock == 0); + + GLInterface.polymostShader->ProjectionMatrix.Set(vp.mProjectionMatrix.get()); + GLInterface.polymostShader->RotMatrix.Set(vp.mViewMatrix.get()); + if (BufferLock++ == 0) { screen->mVertexData->Map(); @@ -584,7 +607,7 @@ void renderBeginScene() void renderFinishScene() { if (videoGetRenderMode() < REND_POLYMOST) return; - assert(BufferLock > 0); + assert(BufferLock == 1); if (--BufferLock == 0) { screen->mVertexData->Unmap(); diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h index 2babbe41a..871af6393 100644 --- a/source/glbackend/glbackend.h +++ b/source/glbackend/glbackend.h @@ -117,13 +117,13 @@ class GLInstance // Cached GL state. GLState lastState; - float mProjectionM5 = 1.0f; // needed by ssao PolymostRenderState renderState; FShader* activeShader; - PolymostShader* polymostShader; public: + float mProjectionM5 = 1.0f; // needed by ssao + PolymostShader* polymostShader; glinfo_t glinfo; void Init(int y); @@ -158,12 +158,7 @@ public: { return SetMatrix(num, reinterpret_cast(mat)); } - int SetIdentityMatrix(int num) - { - auto r = renderState.matrixIndex[num]; - renderState.matrixIndex[num] = 0; - return r; - } + void SetIdentityMatrix(int num); void RestoreMatrix(int num, int index) { renderState.matrixIndex[num] = index; @@ -504,6 +499,8 @@ public: extern GLInstance GLInterface; +void renderSetProjectionMatrix(const float* p); +void renderSetViewMatrix(const float* p); void renderBeginScene(); void renderFinishScene(); void DrawRateStuff(); diff --git a/source/glbackend/hw_draw2d.cpp b/source/glbackend/hw_draw2d.cpp index 0422ed278..4b9a9cf4f 100644 --- a/source/glbackend/hw_draw2d.cpp +++ b/source/glbackend/hw_draw2d.cpp @@ -102,10 +102,15 @@ void polymost_dorotatesprite(int32_t sx, int32_t sy, int32_t z, int16_t a, int16 void GLInstance::Draw2D(F2DDrawer *drawer) { VSMatrix mat(0); - SetIdentityMatrix(Matrix_View); - SetIdentityMatrix(Matrix_Model); + renderSetViewMatrix(nullptr); mat.ortho(0, xdim, ydim, 0, -1, 1); - SetMatrix(Matrix_Projection, mat.get()); + renderSetProjectionMatrix(mat.get()); + SetIdentityMatrix(Matrix_Model); + + // Temporary hack to set the matrices. + renderBeginScene(); + renderFinishScene(); + SetViewport(0, 0, xdim, ydim); EnableDepthTest(false); EnableMultisampling(false); @@ -216,7 +221,6 @@ void GLInstance::Draw2D(F2DDrawer *drawer) //drawer->mIsFirstPass = false; EnableBlend(true); EnableMultisampling(true); - SetIdentityMatrix(Matrix_Projection); matrixArray.Resize(1); renderState.Apply(polymostShader, lastState); // actually set the desired state before returning. } @@ -227,15 +231,14 @@ extern PalEntry palfadergb; void DrawFullscreenBlends() { - GLInterface.SetIdentityMatrix(Matrix_Projection); GLInterface.SetIdentityMatrix(Matrix_Model); - GLInterface.SetIdentityMatrix(Matrix_View); GLInterface.EnableDepthTest(false); GLInterface.EnableAlphaTest(false); GLInterface.EnableBlend(true); GLInterface.UseColorOnly(true); + renderBeginScene(); if (palfadergb.a > 0) { // Todo: reroute to the 2D drawer @@ -257,7 +260,7 @@ void DrawFullscreenBlends() GLInterface.SetColorub(255, 255, 255, 255); GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]); } - GLInterface.DoDraw(); + renderFinishScene(); GLInterface.UseColorOnly(false); } diff --git a/source/glbackend/pm_renderstate.h b/source/glbackend/pm_renderstate.h index e15899c52..88d4d5307 100644 --- a/source/glbackend/pm_renderstate.h +++ b/source/glbackend/pm_renderstate.h @@ -9,8 +9,6 @@ class FMaterial; enum EMatrixType { - Matrix_View, - Matrix_Projection, Matrix_Model, Matrix_Texture, // These are the only ones being used. @@ -70,7 +68,7 @@ struct PolymostRenderState float AlphaThreshold = 0.5f; bool AlphaTest = true; float Color[4] = { 1,1,1,1 }; - short matrixIndex[NUMMATRICES] = { 0,0,0,0 }; + short matrixIndex[NUMMATRICES] = { 0,0 }; PalEntry fullscreenTint = 0xffffff, hictint = 0xffffff, hictint_overlay = 0xffffff; int hictint_flags = -1; FDepthBiasState mBias{ }; diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 0d664e445..f800558ef 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -1133,7 +1133,6 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) CAMERA(q16horiz) = fix16_clamp(CAMERA(q16horiz), F16(HORIZ_MIN), F16(HORIZ_MAX)); - renderBeginScene(); G_HandleMirror(CAMERA(pos.x), CAMERA(pos.y), CAMERA(pos.z), CAMERA(q16ang), CAMERA(q16horiz), smoothRatio); #ifdef LEGACY_ROR if (!RR) @@ -1263,7 +1262,6 @@ void G_DrawRooms(int32_t playerNum, int32_t smoothRatio) #endif renderDrawMasks(); #endif - renderFinishScene(); } G_RestoreInterpolations(); diff --git a/source/rr/src/sector.cpp b/source/rr/src/sector.cpp index a28da0769..9574e1ad2 100644 --- a/source/rr/src/sector.cpp +++ b/source/rr/src/sector.cpp @@ -507,7 +507,6 @@ static void G_SetupCamTile(int spriteNum, int tileNum, int smoothRatio) int const saveMirror = display_mirror; renderSetTarget(tileNum, tilesiz[tileNum].y, tilesiz[tileNum].x); - renderBeginScene(); yax_preparedrawrooms(); drawrooms(camera.x, camera.y, camera.z, SA(spriteNum), 100 + sprite[spriteNum].shade, SECT(spriteNum)); @@ -517,7 +516,6 @@ static void G_SetupCamTile(int spriteNum, int tileNum, int smoothRatio) G_DoSpriteAnimations(camera.x, camera.y, camera.z, SA(spriteNum), smoothRatio); display_mirror = saveMirror; renderDrawMasks(); - renderFinishScene(); renderRestoreTarget(); } diff --git a/source/sw/src/draw.cpp b/source/sw/src/draw.cpp index fee29cd1e..09fb0dcb8 100644 --- a/source/sw/src/draw.cpp +++ b/source/sw/src/draw.cpp @@ -2174,7 +2174,6 @@ drawscreen(PLAYERp pp) JS_DrawCameras(pp, tx, ty, tz); } - renderBeginScene(); OverlapDraw = TRUE; DrawOverlapRoom(tx, ty, tz, tq16ang, tq16horiz, tsectnum); OverlapDraw = FALSE; @@ -2194,7 +2193,6 @@ drawscreen(PLAYERp pp) analyzesprites(tx, ty, tz, FALSE); post_analyzesprites(); renderDrawMasks(); - renderFinishScene(); if (r_usenewaspect) { diff --git a/source/sw/src/jsector.cpp b/source/sw/src/jsector.cpp index a8255ed2f..55dd1af2f 100644 --- a/source/sw/src/jsector.cpp +++ b/source/sw/src/jsector.cpp @@ -478,12 +478,10 @@ void drawroomstotile(int daposx, int daposy, int daposz, TileFiles.MakeCanvas(tilenume, tilesiz[tilenume].x, tilesiz[tilenume].y); renderSetTarget(tilenume, tilesiz[tilenume].x, tilesiz[tilenume].y); - renderBeginScene(); renderDrawRoomsQ16(daposx, daposy, daposz, daq16ang, daq16horiz, dacursectnum); analyzesprites(daposx, daposy, daposz, FALSE); renderDrawMasks(); - renderFinishScene(); renderRestoreTarget(); }