From 2e06ccfec62efd142fad9de5730ac04d9bf15d26 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 18 Jan 2020 13:23:01 +0100 Subject: [PATCH] - draw the screen overlays using the vertex buffer. --- source/build/src/animvpx.cpp | 2 +- source/build/src/glsurface.cpp | 2 +- source/build/src/mdsprite.cpp | 2 +- source/build/src/polymost.cpp | 2 +- source/build/src/voxmodel.cpp | 4 +-- source/glbackend/glbackend.cpp | 56 ++++++++++++++++++++++++++-------- source/glbackend/glbackend.h | 10 ++---- source/glbackend/hw_draw2d.cpp | 24 ++++----------- 8 files changed, 58 insertions(+), 44 deletions(-) diff --git a/source/build/src/animvpx.cpp b/source/build/src/animvpx.cpp index 353437093..bce748dde 100644 --- a/source/build/src/animvpx.cpp +++ b/source/build/src/animvpx.cpp @@ -437,7 +437,7 @@ int32_t animvpx_render_frame(animvpx_codec_ctx *codec, double animvpx_aspect) vt[3].SetTexCoord(1.0,1.0); vt[3].SetVertex(x, -y, 0.0); - GLInterface.Draw(DT_TRIANGLE_FAN, data.first, 4); + GLInterface.DrawIm(DT_TRIANGLE_FAN, data.first, 4); t = timerGetTicks()-t; codec->sumtimes[2] += t; diff --git a/source/build/src/glsurface.cpp b/source/build/src/glsurface.cpp index d396eb7b2..d9be935f4 100644 --- a/source/build/src/glsurface.cpp +++ b/source/build/src/glsurface.cpp @@ -83,5 +83,5 @@ void glsurface_blitBuffer() vt[1].Set(-1.0f, -1.0f, 0.0f, 0.0f, 1.0f); //bottom-left vt[2].Set(1.0f, 1.0f, 0.0f, 1.0f, 0.0f); //top-right vt[3].Set(1.0f, -1.0f, 0.0f, 1.0f, 1.0f); //bottom-right - GLInterface.Draw(DT_TRIANGLE_STRIP, data.first, 4); + GLInterface.DrawIm(DT_TRIANGLE_STRIP, data.first, 4); } diff --git a/source/build/src/mdsprite.cpp b/source/build/src/mdsprite.cpp index b9abbd106..4d123d47d 100644 --- a/source/build/src/mdsprite.cpp +++ b/source/build/src/mdsprite.cpp @@ -1470,7 +1470,7 @@ static void md3draw_handle_triangles(const md3surf_t *s, uint16_t *indexhandle, vt->SetVertex(vertlist[k].x, vertlist[k].y); } } - GLInterface.Draw(DT_TRIANGLES, data.first, s->numtris *3); + GLInterface.DrawIm(DT_TRIANGLES, data.first, s->numtris *3); #ifndef USE_GLEXT UNREFERENCED_PARAMETER(texunits); diff --git a/source/build/src/polymost.cpp b/source/build/src/polymost.cpp index 9b42e38de..da1111e86 100644 --- a/source/build/src/polymost.cpp +++ b/source/build/src/polymost.cpp @@ -599,7 +599,7 @@ static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32 r * (1.f / 1024.f)); } - GLInterface.Draw(DT_TRIANGLE_FAN, data.first, npoints); + GLInterface.DrawIm(DT_TRIANGLE_FAN, data.first, npoints); GLInterface.SetTinting(0, 0, PalEntry(255, 255, 255)); GLInterface.UseDetailMapping(false); diff --git a/source/build/src/voxmodel.cpp b/source/build/src/voxmodel.cpp index e844a7e40..9862e1396 100644 --- a/source/build/src/voxmodel.cpp +++ b/source/build/src/voxmodel.cpp @@ -1152,7 +1152,7 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr) f = 1 /*clut[fi++]*/; if (qdone > 0) { - GLInterface.Draw(DT_TRIANGLES, qstart, qdone * 6); + GLInterface.DrawIm(DT_TRIANGLES, qstart, qdone * 6); qstart += qdone * 6; qdone = 0; } @@ -1182,7 +1182,7 @@ int32_t polymost_voxdraw(voxmodel_t *m, tspriteptr_t const tspr) qdone++; } - GLInterface.Draw(DT_TRIANGLES, qstart, qdone * 6); + GLInterface.DrawIm(DT_TRIANGLES, qstart, qdone * 6); GLInterface.SetClamp(prevClamp); //------------ GLInterface.SetCull(Cull_None); diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index 3a3b91192..728a2e1c9 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -47,6 +47,7 @@ #include "gl_interface.h" #include "v_2ddrawer.h" #include "v_video.h" +#include "flatvertices.h" #include "gl_renderer.h" float shadediv[MAXPALOOKUPS]; @@ -174,6 +175,7 @@ void GLInstance::InitGLState(int fogmode, int multisample) screen->BeginFrame(); bool useSSAO = (gl_ssao != 0); OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(useSSAO); + ClearBufferState(); } void GLInstance::Deinit() @@ -212,6 +214,17 @@ void GLInstance::ResetFrame() } +void GLInstance::ClearBufferState() +{ + auto buffer = (screen->mVertexData->GetBufferObjects().first); + + SetVertexBuffer(buffer, 0, 0); + SetIndexBuffer(nullptr); + // Invalidate the pointers as well to make sure that if another buffer with the same address is used it actually gets bound. + LastVertexBuffer = (IVertexBuffer*)~intptr_t(0); + LastIndexBuffer = (IIndexBuffer*)~intptr_t(0); +} + std::pair GLInstance::AllocVertices(size_t num) { @@ -227,6 +240,36 @@ static GLint primtypes[] = GL_LINES }; +void GLInstance::DrawIm(EDrawType type, size_t start, size_t count) +{ + // Todo: Based on the current tinting flags and the texture type (indexed texture and APPLYOVERPALSWAP not set) this may have to reset the palette for the draw call / texture creation. + bool applied = false; + + if (activeShader == polymostShader) + { + glVertexAttrib4fv(2, renderState.Color); + if (renderState.Color[3] != 1.f) renderState.Flags &= ~RF_Brightmapping; // The way the colormaps are set up means that brightmaps cannot be used on translucent content at all. + renderState.Apply(polymostShader, lastState); + } + glBegin(primtypes[type]); + auto p = &Buffer[start]; + for (size_t i = 0; i < count; i++, p++) + { + glVertexAttrib2f(1, p->u, p->v); + glVertexAttrib3f(0, p->x, p->y, p->z); + } + glEnd(); + + if (MatrixChange) + { + if (MatrixChange & 1) SetIdentityMatrix(Matrix_Texture); + if (MatrixChange & 2) SetIdentityMatrix(Matrix_Detail); + MatrixChange = 0; + } + matrixArray.Resize(1); +} + + void GLInstance::Draw(EDrawType type, size_t start, size_t count) { // Todo: Based on the current tinting flags and the texture type (indexed texture and APPLYOVERPALSWAP not set) this may have to reset the palette for the draw call / texture creation. @@ -258,18 +301,7 @@ void GLInstance::Draw(EDrawType type, size_t start, size_t count) LastIndexBuffer = renderState.IndexBuffer; } } - if (!LastVertexBuffer) - { - glBegin(primtypes[type]); - auto p = &Buffer[start]; - for (size_t i = 0; i < count; i++, p++) - { - glVertexAttrib2f(1, p->u, p->v); - glVertexAttrib3f(0, p->x, p->y, p->z); - } - glEnd(); - } - else if (type != DT_LINES) + if (type != DT_LINES && renderState.IndexBuffer) { glDrawElements(primtypes[type], count, GL_UNSIGNED_INT, (void*)(intptr_t)(start * sizeof(uint32_t))); } diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h index 6446d0a79..1b668e7cd 100644 --- a/source/glbackend/glbackend.h +++ b/source/glbackend/glbackend.h @@ -228,6 +228,7 @@ public: GLInstance(); std::pair AllocVertices(size_t num); + void DrawIm(EDrawType type, size_t start, size_t count); void Draw(EDrawType type, size_t start, size_t count); FHardwareTexture* NewTexture(); @@ -246,14 +247,7 @@ public: { renderState.IndexBuffer = vb; } - void ClearBufferState() - { - SetVertexBuffer(nullptr, 0, 0); - SetIndexBuffer(nullptr); - // Invalidate the pointers as well to make sure that if another buffer with the same address is used it actually gets bound. - LastVertexBuffer = (IVertexBuffer*)~intptr_t(0); - LastIndexBuffer = (IIndexBuffer*)~intptr_t(0); - } + void ClearBufferState(); float GetProjectionM5() { return mProjectionM5; } void SetMatrix(int num, const VSMatrix *mat ); diff --git a/source/glbackend/hw_draw2d.cpp b/source/glbackend/hw_draw2d.cpp index e4ec15f20..3ee8b424c 100644 --- a/source/glbackend/hw_draw2d.cpp +++ b/source/glbackend/hw_draw2d.cpp @@ -40,6 +40,7 @@ #include "glbackend.h" #include "v_draw.h" #include "palette.h" +#include "flatvertices.h" extern int16_t numshades; //=========================================================================== @@ -244,12 +245,7 @@ void fullscreen_tint_gl(PalEntry pe) GLInterface.UseColorOnly(true); - auto data = GLInterface.AllocVertices(3); - auto vt = data.second; - vt[0].Set(-2.5f, 1.f); - vt[1].Set(2.5f, 1.f); - vt[2].Set(.0f, -2.5f); - GLInterface.Draw(DT_TRIANGLES, data.first, 3); + GLInterface.Draw(DT_TRIANGLE_STRIP, FFlatVertexBuffer::PRESENT_INDEX, 4); GLInterface.UseColorOnly(false); } @@ -268,20 +264,12 @@ void fullscreen_tint_gl_blood(int tint_blood_r, int tint_blood_g, int tint_blood GLInterface.UseColorOnly(true); GLInterface.SetColorub(max(tint_blood_r, 0), max(tint_blood_g, 0), max(tint_blood_b, 0), 255); - auto data = GLInterface.AllocVertices(3); - auto vt = data.second; - vt[0].Set(-2.5f, 1.f); - vt[1].Set(2.5f, 1.f); - vt[2].Set(.0f, -2.5f); - GLInterface.Draw(DT_TRIANGLES, data.first, 3); + GLInterface.Draw(DT_TRIANGLE_STRIP, FFlatVertexBuffer::PRESENT_INDEX, 4); + GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Subtract]); GLInterface.SetColorub(max(-tint_blood_r, 0), max(-tint_blood_g, 0), max(-tint_blood_b, 0), 255); - data = GLInterface.AllocVertices(3); - vt = data.second; - vt[0].Set(-2.5f, 1.f); - vt[1].Set(2.5f, 1.f); - vt[2].Set(.0f, -2.5f); - GLInterface.Draw(DT_TRIANGLES, data.first, 3); + GLInterface.Draw(DT_TRIANGLE_STRIP, FFlatVertexBuffer::PRESENT_INDEX, 4); + GLInterface.SetColorub(255, 255, 255, 255); GLInterface.SetRenderStyle(LegacyRenderStyles[STYLE_Translucent]); GLInterface.UseColorOnly(false);