From f3cc3314362d6414590cb147f096272cc857de41 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 14 Jun 2014 15:00:10 +0200 Subject: [PATCH] - allow vertex buffers for all GL versions. At least on NVidia this is horrendously slow without GL_ARB_buffer_storage but we'll need this code for some benchmarking. --- src/gl/data/gl_vertexbuffer.cpp | 52 ++++++++++++++++----------------- src/gl/data/gl_vertexbuffer.h | 15 +++++++++- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/gl/data/gl_vertexbuffer.cpp b/src/gl/data/gl_vertexbuffer.cpp index f7b4bdbf29..3e70fbfeb3 100644 --- a/src/gl/data/gl_vertexbuffer.cpp +++ b/src/gl/data/gl_vertexbuffer.cpp @@ -45,21 +45,24 @@ #include "c_cvars.h" #include "gl/system/gl_interface.h" #include "gl/renderer/gl_renderer.h" +#include "gl/renderer/gl_renderstate.h" #include "gl/data/gl_data.h" #include "gl/data/gl_vertexbuffer.h" -CUSTOM_CVAR(Int, gl_usevbo, -1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL) +CUSTOM_CVAR(Int, gl_usevbo, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL) { - if (self < -1 || self > 1 || !(gl.flags & RFL_BUFFER_STORAGE)) + if (self < -1 || self > 1 /* || !(gl.flags & RFL_BUFFER_STORAGE)*/) { self = 0; } + /* else if (self == -1) { if (!(gl.flags & RFL_BUFFER_STORAGE)) self = 0; else self = 1; } + */ } //========================================================================== @@ -92,16 +95,19 @@ FVertexBuffer::~FVertexBuffer() FFlatVertexBuffer::FFlatVertexBuffer() : FVertexBuffer() { + unsigned int bytesize = BUFFER_SIZE * sizeof(FFlatVertex); + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); if (gl.flags & RFL_BUFFER_STORAGE) { - unsigned int bytesize = BUFFER_SIZE * sizeof(FFlatVertex); - glBindBuffer(GL_ARRAY_BUFFER, vbo_id); glBufferStorage(GL_ARRAY_BUFFER, bytesize, NULL, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); map = (FFlatVertex*)glMapBufferRange(GL_ARRAY_BUFFER, 0, bytesize, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); } else { - map = NULL; + glBufferData(GL_ARRAY_BUFFER, bytesize, NULL, GL_STREAM_DRAW); + vbo_shadowdata.Reserve(BUFFER_SIZE); + vbo_shadowdata.Clear(); + map = &vbo_shadowdata[0]; } mIndex = mCurIndex = 0; } @@ -274,7 +280,7 @@ void FFlatVertexBuffer::UpdatePlaneVertices(sector_t *sec, int plane) secplane_t &splane = sec->GetSecPlane(plane); FFlatVertex *vt = &vbo_shadowdata[startvt]; FFlatVertex *mapvt = &map[startvt]; - for(int i=0; iz = splane.ZatPoint(vt->x, vt->y); if (plane == sector_t::floor && sec->transdoor) vt->z -= 1; @@ -291,21 +297,16 @@ void FFlatVertexBuffer::UpdatePlaneVertices(sector_t *sec, int plane) void FFlatVertexBuffer::CreateVBO() { vbo_shadowdata.Clear(); + CreateFlatVBO(); + mCurIndex = mIndex = vbo_shadowdata.Size(); if (gl.flags & RFL_BUFFER_STORAGE) { - CreateFlatVBO(); memcpy(map, &vbo_shadowdata[0], vbo_shadowdata.Size() * sizeof(FFlatVertex)); - mCurIndex = mIndex = vbo_shadowdata.Size(); } - else if (sectors) + else { - // set all VBO info to invalid values so that we can save some checks in the rendering code - for(int i=0;ix); - glTexCoordPointer(2,GL_FLOAT, sizeof(FFlatVertex), &VTO->u); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); - } + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glVertexPointer(3, GL_FLOAT, sizeof(FFlatVertex), &VTO->x); + glTexCoordPointer(2, GL_FLOAT, sizeof(FFlatVertex), &VTO->u); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); } //========================================================================== @@ -337,7 +335,7 @@ void FFlatVertexBuffer::BindVBO() void FFlatVertexBuffer::CheckPlanes(sector_t *sector) { - if (gl.flags & RFL_BUFFER_STORAGE) + //if (gl.flags & RFL_BUFFER_STORAGE) { if (sector->GetPlaneTexZ(sector_t::ceiling) != sector->vboheight[sector_t::ceiling]) { diff --git a/src/gl/data/gl_vertexbuffer.h b/src/gl/data/gl_vertexbuffer.h index d7aeab9a27..80cadb4c4b 100644 --- a/src/gl/data/gl_vertexbuffer.h +++ b/src/gl/data/gl_vertexbuffer.h @@ -2,6 +2,7 @@ #define __VERTEXBUFFER_H #include "tarray.h" +#include "gl/system/gl_interface.h" struct vertex_t; struct secplane_t; @@ -60,6 +61,8 @@ public: void BindVBO(); void CheckUpdate(sector_t *sector); + bool SetBufferMode(bool hw); + FFlatVertex *GetBuffer() { return &map[mCurIndex]; @@ -78,6 +81,16 @@ public: { unsigned int offset; unsigned int count = GetCount(newptr, &offset); + if (!(gl.flags & RFL_BUFFER_STORAGE)) + { + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + void *p = glMapBufferRange(GL_ARRAY_BUFFER, offset * sizeof(FFlatVertex), count * sizeof(FFlatVertex), GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT); + if (p != NULL) + { + memcpy(p, &vbo_shadowdata[offset], count * sizeof(FFlatVertex)); + glUnmapBuffer(GL_ARRAY_BUFFER); + } + } glDrawArrays(primtype, offset, count); } #endif @@ -135,7 +148,7 @@ private: void SkyVertex(int r, int c, bool yflip); void CreateSkyHemisphere(int hemi); void CreateDome(); - void RenderRow(int prim, int row, bool color); + void RenderRow(int prim, int row); public: