- clean separation of vertex creation from map data and the buffer object.

This was yet another object with too broad scope, the vertex creation has been offloaded into out-of-class functions now.
This commit is contained in:
Christoph Oelckers 2020-04-26 12:12:07 +02:00
parent cf41a0b1fb
commit c5dca89e66
9 changed files with 279 additions and 294 deletions

View file

@ -958,7 +958,6 @@ set (PCH_SOURCES
rendering/hwrenderer/hw_entrypoint.cpp
rendering/hwrenderer/data/hw_vertexbuilder.cpp
rendering/hwrenderer/data/flatvertices.cpp
rendering/hwrenderer/data/hw_viewpointbuffer.cpp
rendering/hwrenderer/dynlights/hw_aabbtree.cpp
rendering/hwrenderer/dynlights/hw_shadowmap.cpp
rendering/hwrenderer/dynlights/hw_lightbuffer.cpp
@ -1143,11 +1142,11 @@ set (PCH_SOURCES
common/objects/dobjgc.cpp
common/objects/dobjtype.cpp
common/rendering/r_videoscale.cpp
common/rendering/hwrenderer/data/hw_viewpointbuffer.cpp
common/rendering/hwrenderer/data/hw_cvars.cpp
common/rendering/hwrenderer/data/hw_vrmodes.cpp
common/rendering/hwrenderer/postprocessing/hw_postprocess.cpp
common/rendering/hwrenderer/postprocessing/hw_postprocess_cvars.cpp
common/rendering/gl_load/gl_interface.cpp
common/scripting/core/dictionary.cpp
common/scripting/core/dynarrays.cpp

View file

@ -81,6 +81,7 @@
#include "xlat/xlat.h"
#include "vm.h"
#include "texturemanager.h"
#include "hwrenderer/data/hw_vertexbuilder.h"
enum
{
@ -3223,7 +3224,7 @@ void MapLoader::LoadLevel(MapData *map, const char *lumpname, int position)
InitRenderInfo(); // create hardware independent renderer resources for the level. This must be done BEFORE the PolyObj Spawn!!!
Level->ClearDynamic3DFloorData(); // CreateVBO must be run on the plain 3D floor data.
screen->mVertexData->CreateVBO(Level->sectors);
CreateVBO(screen->mVertexData, Level->sectors);
for (auto &sec : Level->sectors)
{

View file

@ -32,16 +32,12 @@
**
*/
#include "doomtype.h"
#include "p_local.h"
#include "r_state.h"
#include "c_cvars.h"
#include "g_levellocals.h"
#include "flatvertices.h"
#include "v_video.h"
#include "cmdlib.h"
#include "printf.h"
#include "hwrenderer/data/buffers.h"
#include "hw_renderstate.h"
//==========================================================================
//
@ -94,7 +90,7 @@ FFlatVertexBuffer::FFlatVertexBuffer(int width, int height)
};
mVertexBuffer->SetFormat(1, 2, sizeof(FFlatVertex), format);
mIndex = mCurIndex = 0;
mIndex = mCurIndex = NUM_RESERVED;
mNumReserved = NUM_RESERVED;
Copy(0, NUM_RESERVED);
}
@ -128,238 +124,6 @@ void FFlatVertexBuffer::OutputResized(int width, int height)
Copy(4, 4);
}
//==========================================================================
//
// Initialize a single vertex
//
//==========================================================================
void FFlatVertex::SetFlatVertex(vertex_t *vt, const secplane_t & plane)
{
x = (float)vt->fX();
y = (float)vt->fY();
z = (float)plane.ZatPoint(vt);
u = (float)vt->fX()/64.f;
v = -(float)vt->fY()/64.f;
}
//==========================================================================
//
// Find a 3D floor
//
//==========================================================================
static F3DFloor *Find3DFloor(sector_t *target, sector_t *model)
{
for(unsigned i=0; i<target->e->XFloor.ffloors.Size(); i++)
{
F3DFloor *ffloor = target->e->XFloor.ffloors[i];
if (ffloor->model == model && !(ffloor->flags & FF_THISINSIDE)) return ffloor;
}
return NULL;
}
//==========================================================================
//
// Creates the vertices for one plane in one subsector
//
//==========================================================================
int FFlatVertexBuffer::CreateIndexedSectorVertices(sector_t *sec, const secplane_t &plane, int floor, VertexContainer &verts)
{
unsigned vi = vbo_shadowdata.Reserve(verts.vertices.Size());
float diff;
// Create the actual vertices.
if (sec->transdoor && floor) diff = -1.f;
else diff = 0.f;
for (unsigned i = 0; i < verts.vertices.Size(); i++)
{
vbo_shadowdata[vi + i].SetFlatVertex(verts.vertices[i].vertex, plane);
vbo_shadowdata[vi + i].z += diff;
}
unsigned rt = ibo_data.Reserve(verts.indices.Size());
for (unsigned i = 0; i < verts.indices.Size(); i++)
{
ibo_data[rt + i] = vi + verts.indices[i];
}
return (int)rt;
}
//==========================================================================
//
//
//
//==========================================================================
int FFlatVertexBuffer::CreateIndexedVertices(int h, sector_t *sec, const secplane_t &plane, int floor, VertexContainers &verts)
{
sec->vboindex[h] = vbo_shadowdata.Size();
// First calculate the vertices for the sector itself
sec->vboheight[h] = sec->GetPlaneTexZ(h);
sec->ibocount = verts[sec->Index()].indices.Size();
sec->iboindex[h] = CreateIndexedSectorVertices(sec, plane, floor, verts[sec->Index()]);
// Next are all sectors using this one as heightsec
TArray<sector_t *> &fakes = sec->e->FakeFloor.Sectors;
for (unsigned g = 0; g < fakes.Size(); g++)
{
sector_t *fsec = fakes[g];
fsec->iboindex[2 + h] = CreateIndexedSectorVertices(fsec, plane, false, verts[fsec->Index()]);
}
// and finally all attached 3D floors
TArray<sector_t *> &xf = sec->e->XFloor.attached;
for (unsigned g = 0; g < xf.Size(); g++)
{
sector_t *fsec = xf[g];
F3DFloor *ffloor = Find3DFloor(fsec, sec);
if (ffloor != NULL && ffloor->flags & FF_RENDERPLANES)
{
bool dotop = (ffloor->top.model == sec) && (ffloor->top.isceiling == h);
bool dobottom = (ffloor->bottom.model == sec) && (ffloor->bottom.isceiling == h);
if (dotop || dobottom)
{
auto ndx = CreateIndexedSectorVertices(fsec, plane, false, verts[fsec->Index()]);
if (dotop) ffloor->top.vindex = ndx;
if (dobottom) ffloor->bottom.vindex = ndx;
}
}
}
sec->vbocount[h] = vbo_shadowdata.Size() - sec->vboindex[h];
return sec->iboindex[h];
}
//==========================================================================
//
//
//
//==========================================================================
void FFlatVertexBuffer::CreateIndexedFlatVertices(TArray<sector_t> &sectors)
{
auto verts = BuildVertices(sectors);
int i = 0;
/*
for (auto &vert : verts)
{
Printf(PRINT_LOG, "Sector %d\n", i);
Printf(PRINT_LOG, "%d vertices, %d indices\n", vert.vertices.Size(), vert.indices.Size());
int j = 0;
for (auto &v : vert.vertices)
{
Printf(PRINT_LOG, " %d: (%2.3f, %2.3f)\n", j++, v.vertex->fX(), v.vertex->fY());
}
for (unsigned i=0;i<vert.indices.Size();i+=3)
{
Printf(PRINT_LOG, " %d, %d, %d\n", vert.indices[i], vert.indices[i + 1], vert.indices[i + 2]);
}
i++;
}
*/
for (int h = sector_t::floor; h <= sector_t::ceiling; h++)
{
for (auto &sec : sectors)
{
CreateIndexedVertices(h, &sec, sec.GetSecPlane(h), h == sector_t::floor, verts);
}
}
// We need to do a final check for Vavoom water and FF_FIX sectors.
// No new vertices are needed here. The planes come from the actual sector
for (auto &sec : sectors)
{
for (auto ff : sec.e->XFloor.ffloors)
{
if (ff->top.model == &sec)
{
ff->top.vindex = sec.iboindex[ff->top.isceiling];
}
if (ff->bottom.model == &sec)
{
ff->bottom.vindex = sec.iboindex[ff->top.isceiling];
}
}
}
}
//==========================================================================
//
//
//
//==========================================================================
void FFlatVertexBuffer::UpdatePlaneVertices(sector_t *sec, int plane)
{
int startvt = sec->vboindex[plane];
int countvt = sec->vbocount[plane];
secplane_t &splane = sec->GetSecPlane(plane);
FFlatVertex *vt = &vbo_shadowdata[startvt];
FFlatVertex *mapvt = GetBuffer(startvt);
for(int i=0; i<countvt; i++, vt++, mapvt++)
{
vt->z = (float)splane.ZatPoint(vt->x, vt->y);
if (plane == sector_t::floor && sec->transdoor) vt->z -= 1;
mapvt->z = vt->z;
}
}
//==========================================================================
//
//
//
//==========================================================================
void FFlatVertexBuffer::CreateVertices(TArray<sector_t> &sectors)
{
vbo_shadowdata.Resize(NUM_RESERVED);
CreateIndexedFlatVertices(sectors);
}
//==========================================================================
//
//
//
//==========================================================================
void FFlatVertexBuffer::CheckPlanes(sector_t *sector)
{
if (sector->GetPlaneTexZ(sector_t::ceiling) != sector->vboheight[sector_t::ceiling])
{
UpdatePlaneVertices(sector, sector_t::ceiling);
sector->vboheight[sector_t::ceiling] = sector->GetPlaneTexZ(sector_t::ceiling);
}
if (sector->GetPlaneTexZ(sector_t::floor) != sector->vboheight[sector_t::floor])
{
UpdatePlaneVertices(sector, sector_t::floor);
sector->vboheight[sector_t::floor] = sector->GetPlaneTexZ(sector_t::floor);
}
}
//==========================================================================
//
// checks the validity of all planes attached to this sector
// and updates them if possible.
//
//==========================================================================
void FFlatVertexBuffer::CheckUpdate(sector_t *sector)
{
CheckPlanes(sector);
sector_t *hs = sector->GetHeightSec();
if (hs != NULL) CheckPlanes(hs);
for (unsigned i = 0; i < sector->e->XFloor.ffloors.Size(); i++)
CheckPlanes(sector->e->XFloor.ffloors[i]->model);
}
//==========================================================================
//
//
@ -392,17 +156,3 @@ void FFlatVertexBuffer::Copy(int start, int count)
Unmap();
}
//==========================================================================
//
//
//
//==========================================================================
void FFlatVertexBuffer::CreateVBO(TArray<sector_t> &sectors)
{
vbo_shadowdata.Resize(mNumReserved);
FFlatVertexBuffer::CreateVertices(sectors);
mCurIndex = mIndex = vbo_shadowdata.Size();
Copy(0, mIndex);
mIndexBuffer->SetData(ibo_data.Size() * sizeof(uint32_t), &ibo_data[0]);
}

View file

@ -1,31 +1,9 @@
//
//---------------------------------------------------------------------------
//
// Copyright(C) 2005-2016 Christoph Oelckers
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
//
//--------------------------------------------------------------------------
//
#ifndef _HW__VERTEXBUFFER_H
#define _HW__VERTEXBUFFER_H
#include "tarray.h"
#include "hwrenderer/data/buffers.h"
#include "hw_vertexbuilder.h"
#include <atomic>
#include <mutex>
@ -38,7 +16,6 @@ struct FFlatVertex
float x, z, y; // world position
float u, v; // texture coordinates
void SetFlatVertex(vertex_t *vt, const secplane_t &plane);
void Set(float xx, float zz, float yy, float uu, float vv)
{
x = xx;
@ -47,10 +24,25 @@ struct FFlatVertex
u = uu;
v = vv;
}
void SetVertex(float _x, float _y, float _z = 0)
{
x = _x;
z = _y;
y = _z;
}
void SetTexCoord(float _u = 0, float _v = 0)
{
u = _u;
v = _v;
}
};
class FFlatVertexBuffer
{
public:
TArray<FFlatVertex> vbo_shadowdata;
TArray<uint32_t> ibo_data;
@ -63,7 +55,7 @@ class FFlatVertexBuffer
static const unsigned int BUFFER_SIZE = 2000000;
static const unsigned int BUFFER_SIZE_TO_USE = 1999500;
static const unsigned int BUFFER_SIZE_TO_USE = BUFFER_SIZE-500;
public:
enum
@ -86,7 +78,6 @@ public:
return std::make_pair(mVertexBuffer, mIndexBuffer);
}
void CreateVBO(TArray<sector_t> &sectors);
void Copy(int start, int count);
FFlatVertex *GetBuffer(int index) const
@ -117,19 +108,6 @@ public:
mVertexBuffer->Unmap();
}
private:
int CreateIndexedSectionVertices(subsector_t *sub, const secplane_t &plane, int floor, VertexContainer &cont);
int CreateIndexedSectorVertices(sector_t *sec, const secplane_t &plane, int floor, VertexContainer &cont);
int CreateIndexedVertices(int h, sector_t *sec, const secplane_t &plane, int floor, VertexContainers &cont);
void CreateIndexedFlatVertices(TArray<sector_t> &sectors);
void UpdatePlaneVertices(sector_t *sec, int plane);
protected:
void CreateVertices(TArray<sector_t> &sectors);
void CheckPlanes(sector_t *sector);
public:
void CheckUpdate(sector_t *sector);
};
#endif

View file

@ -24,6 +24,7 @@
#include "g_levellocals.h"
#include "hw_vertexbuilder.h"
#include "flatvertices.h"
#include "earcut.hpp"
@ -146,3 +147,255 @@ TArray<VertexContainer> BuildVertices(TArray<sector_t> &sectors)
}
return verticesPerSector;
}
//==========================================================================
//
// Creates the vertices for one plane in one subsector
//
//==========================================================================
//==========================================================================
//
// Find a 3D floor
//
//==========================================================================
static F3DFloor* Find3DFloor(sector_t* target, sector_t* model)
{
for (unsigned i = 0; i < target->e->XFloor.ffloors.Size(); i++)
{
F3DFloor* ffloor = target->e->XFloor.ffloors[i];
if (ffloor->model == model && !(ffloor->flags & FF_THISINSIDE)) return ffloor;
}
return NULL;
}
//==========================================================================
//
// Initialize a single vertex
//
//==========================================================================
static void SetFlatVertex(FFlatVertex& ffv, vertex_t* vt, const secplane_t& plane)
{
ffv.x = (float)vt->fX();
ffv.y = (float)vt->fY();
ffv.z = (float)plane.ZatPoint(vt);
ffv.u = (float)vt->fX() / 64.f;
ffv.v = -(float)vt->fY() / 64.f;
}
static int CreateIndexedSectorVertices(FFlatVertexBuffer* fvb, sector_t* sec, const secplane_t& plane, int floor, VertexContainer& verts)
{
auto& vbo_shadowdata = fvb->vbo_shadowdata;
unsigned vi = vbo_shadowdata.Reserve(verts.vertices.Size());
float diff;
// Create the actual vertices.
if (sec->transdoor && floor) diff = -1.f;
else diff = 0.f;
for (unsigned i = 0; i < verts.vertices.Size(); i++)
{
SetFlatVertex(vbo_shadowdata[vi + i], verts.vertices[i].vertex, plane);
vbo_shadowdata[vi + i].z += diff;
}
auto& ibo_data = fvb->ibo_data;
unsigned rt = ibo_data.Reserve(verts.indices.Size());
for (unsigned i = 0; i < verts.indices.Size(); i++)
{
ibo_data[rt + i] = vi + verts.indices[i];
}
return (int)rt;
}
//==========================================================================
//
//
//
//==========================================================================
static int CreateIndexedVertices(FFlatVertexBuffer* fvb, int h, sector_t* sec, const secplane_t& plane, int floor, VertexContainers& verts)
{
auto& vbo_shadowdata = fvb->vbo_shadowdata;
sec->vboindex[h] = vbo_shadowdata.Size();
// First calculate the vertices for the sector itself
sec->vboheight[h] = sec->GetPlaneTexZ(h);
sec->ibocount = verts[sec->Index()].indices.Size();
sec->iboindex[h] = CreateIndexedSectorVertices(fvb, sec, plane, floor, verts[sec->Index()]);
// Next are all sectors using this one as heightsec
TArray<sector_t*>& fakes = sec->e->FakeFloor.Sectors;
for (unsigned g = 0; g < fakes.Size(); g++)
{
sector_t* fsec = fakes[g];
fsec->iboindex[2 + h] = CreateIndexedSectorVertices(fvb, fsec, plane, false, verts[fsec->Index()]);
}
// and finally all attached 3D floors
TArray<sector_t*>& xf = sec->e->XFloor.attached;
for (unsigned g = 0; g < xf.Size(); g++)
{
sector_t* fsec = xf[g];
F3DFloor* ffloor = Find3DFloor(fsec, sec);
if (ffloor != NULL && ffloor->flags & FF_RENDERPLANES)
{
bool dotop = (ffloor->top.model == sec) && (ffloor->top.isceiling == h);
bool dobottom = (ffloor->bottom.model == sec) && (ffloor->bottom.isceiling == h);
if (dotop || dobottom)
{
auto ndx = CreateIndexedSectorVertices(fvb, fsec, plane, false, verts[fsec->Index()]);
if (dotop) ffloor->top.vindex = ndx;
if (dobottom) ffloor->bottom.vindex = ndx;
}
}
}
sec->vbocount[h] = vbo_shadowdata.Size() - sec->vboindex[h];
return sec->iboindex[h];
}
//==========================================================================
//
//
//
//==========================================================================
static void CreateIndexedFlatVertices(FFlatVertexBuffer* fvb, TArray<sector_t>& sectors)
{
auto verts = BuildVertices(sectors);
int i = 0;
/*
for (auto &vert : verts)
{
Printf(PRINT_LOG, "Sector %d\n", i);
Printf(PRINT_LOG, "%d vertices, %d indices\n", vert.vertices.Size(), vert.indices.Size());
int j = 0;
for (auto &v : vert.vertices)
{
Printf(PRINT_LOG, " %d: (%2.3f, %2.3f)\n", j++, v.vertex->fX(), v.vertex->fY());
}
for (unsigned i=0;i<vert.indices.Size();i+=3)
{
Printf(PRINT_LOG, " %d, %d, %d\n", vert.indices[i], vert.indices[i + 1], vert.indices[i + 2]);
}
i++;
}
*/
for (int h = sector_t::floor; h <= sector_t::ceiling; h++)
{
for (auto& sec : sectors)
{
CreateIndexedVertices(fvb, h, &sec, sec.GetSecPlane(h), h == sector_t::floor, verts);
}
}
// We need to do a final check for Vavoom water and FF_FIX sectors.
// No new vertices are needed here. The planes come from the actual sector
for (auto& sec : sectors)
{
for (auto ff : sec.e->XFloor.ffloors)
{
if (ff->top.model == &sec)
{
ff->top.vindex = sec.iboindex[ff->top.isceiling];
}
if (ff->bottom.model == &sec)
{
ff->bottom.vindex = sec.iboindex[ff->top.isceiling];
}
}
}
}
//==========================================================================
//
//
//
//==========================================================================
static void UpdatePlaneVertices(FFlatVertexBuffer *fvb, sector_t* sec, int plane)
{
int startvt = sec->vboindex[plane];
int countvt = sec->vbocount[plane];
secplane_t& splane = sec->GetSecPlane(plane);
FFlatVertex* vt = &fvb->vbo_shadowdata[startvt];
FFlatVertex* mapvt = fvb->GetBuffer(startvt);
for (int i = 0; i < countvt; i++, vt++, mapvt++)
{
vt->z = (float)splane.ZatPoint(vt->x, vt->y);
if (plane == sector_t::floor && sec->transdoor) vt->z -= 1;
mapvt->z = vt->z;
}
}
//==========================================================================
//
//
//
//==========================================================================
static void CreateVertices(FFlatVertexBuffer* fvb, TArray<sector_t>& sectors)
{
fvb->vbo_shadowdata.Resize(FFlatVertexBuffer::NUM_RESERVED);
CreateIndexedFlatVertices(fvb, sectors);
}
//==========================================================================
//
//
//
//==========================================================================
static void CheckPlanes(FFlatVertexBuffer* fvb, sector_t* sector)
{
if (sector->GetPlaneTexZ(sector_t::ceiling) != sector->vboheight[sector_t::ceiling])
{
UpdatePlaneVertices(fvb, sector, sector_t::ceiling);
sector->vboheight[sector_t::ceiling] = sector->GetPlaneTexZ(sector_t::ceiling);
}
if (sector->GetPlaneTexZ(sector_t::floor) != sector->vboheight[sector_t::floor])
{
UpdatePlaneVertices(fvb, sector, sector_t::floor);
sector->vboheight[sector_t::floor] = sector->GetPlaneTexZ(sector_t::floor);
}
}
//==========================================================================
//
// checks the validity of all planes attached to this sector
// and updates them if possible.
//
//==========================================================================
void CheckUpdate(FFlatVertexBuffer* fvb, sector_t* sector)
{
CheckPlanes(fvb, sector);
sector_t* hs = sector->GetHeightSec();
if (hs != NULL) CheckPlanes(fvb, hs);
for (unsigned i = 0; i < sector->e->XFloor.ffloors.Size(); i++)
CheckPlanes(fvb, sector->e->XFloor.ffloors[i]->model);
}
//==========================================================================
//
//
//
//==========================================================================
void CreateVBO(FFlatVertexBuffer* fvb, TArray<sector_t>& sectors)
{
fvb->vbo_shadowdata.Resize(fvb->mNumReserved);
CreateVertices(fvb, sectors);
fvb->mCurIndex = fvb->mIndex = fvb->vbo_shadowdata.Size();
fvb->Copy(0, fvb->mIndex);
fvb->mIndexBuffer->SetData(fvb->ibo_data.Size() * sizeof(uint32_t), &fvb->ibo_data[0]);
}

View file

@ -67,4 +67,7 @@ using VertexContainers = TArray<VertexContainer>;
VertexContainers BuildVertices(TArray<sector_t> &sectors);
class FFlatVertexBuffer;
void CheckUpdate(FFlatVertexBuffer* fvb, sector_t* sector);
void CreateVBO(FFlatVertexBuffer* fvb, TArray<sector_t>& sectors);

View file

@ -41,6 +41,7 @@
#include "hwrenderer/scene/hw_portal.h"
#include "hwrenderer/utility/hw_clock.h"
#include "hwrenderer/data/flatvertices.h"
#include "hwrenderer/data/hw_vertexbuilder.h"
#ifdef ARCH_IA32
#include <immintrin.h>
@ -628,7 +629,7 @@ void HWDrawInfo::DoSubsector(subsector_t * sub)
if (sector->validcount != validcount)
{
screen->mVertexData->CheckUpdate(sector);
CheckUpdate(screen->mVertexData, sector);
}
// [RH] Add particles