This commit is contained in:
Christoph Oelckers 2018-11-05 15:30:50 +01:00
parent d7db00d92e
commit 950ed07ae6
4 changed files with 228 additions and 208 deletions

View file

@ -35,6 +35,13 @@
#include "hwrenderer/data/buffers.h" #include "hwrenderer/data/buffers.h"
#include "hwrenderer/scene/hw_renderstate.h" #include "hwrenderer/scene/hw_renderstate.h"
namespace VertexBuilder
{
TArray<VertexContainer> BuildVertices();
}
using VertexContainers = TArray<VertexContainer>;
//========================================================================== //==========================================================================
// //
@ -158,54 +165,22 @@ static F3DFloor *Find3DFloor(sector_t *target, sector_t *model)
// //
//========================================================================== //==========================================================================
int FFlatVertexBuffer::CreateIndexedSubsectorVertices(subsector_t *sub, const secplane_t &plane, int floor, int vi, FFlatVertexBuffer::FIndexGenerationInfo &gen) int FFlatVertexBuffer::CreateIndexedSectorVertices(sector_t *sec, const secplane_t &plane, int floor, VertexContainer &verts)
{ {
if (sub->numlines < 3) return -1; int vi = vbo_shadowdata.Reserve(verts.vertices.Size());
int idx = ibo_data.Reserve((sub->numlines - 2) * 3);
int idxc = idx;
int firstndx = gen.GetIndex(sub->firstline[0].v1);
int secondndx = gen.GetIndex(sub->firstline[1].v1);
for (unsigned int k = 2; k<sub->numlines; k++)
{
auto ndx = gen.GetIndex(sub->firstline[k].v1);
ibo_data[idx++] = vi + firstndx;
ibo_data[idx++] = vi + secondndx;
ibo_data[idx++] = vi + ndx;
secondndx = ndx;
}
return idx;
}
//==========================================================================
//
// Creates the vertices for one plane in one subsector
//
//==========================================================================
int FFlatVertexBuffer::CreateIndexedSectorVertices(sector_t *sec, const secplane_t &plane, int floor, FFlatVertexBuffer::FIndexGenerationInfo &gen)
{
int rt = ibo_data.Size();
int vi = vbo_shadowdata.Reserve(gen.vertices.Size());
float diff; float diff;
// Create the actual vertices. // Create the actual vertices.
if (sec->transdoor && floor) diff = -1.f; if (sec->transdoor && floor) diff = -1.f;
else diff = 0.f; else diff = 0.f;
for (unsigned i = 0; i < gen.vertices.Size(); i++) for (unsigned i = 0; i < verts.vertices.Size(); i++)
{ {
vbo_shadowdata[vi + i].SetFlatVertex(gen.vertices[i], plane); vbo_shadowdata[vi + i].SetFlatVertex(verts.vertices[i].vertex, plane);
vbo_shadowdata[vi + i].z += diff; vbo_shadowdata[vi + i].z += diff;
} }
// Create the indices for the subsectors int rt = ibo_data.size();
for (int j = 0; j<sec->subsectorcount; j++) ibo_data.Append(verts.indices);
{
subsector_t *sub = sec->subsectors[j];
CreateIndexedSubsectorVertices(sub, plane, floor, vi, gen);
}
sec->ibocount = ibo_data.Size() - rt;
return rt; return rt;
} }
@ -215,19 +190,20 @@ int FFlatVertexBuffer::CreateIndexedSectorVertices(sector_t *sec, const secplane
// //
//========================================================================== //==========================================================================
int FFlatVertexBuffer::CreateIndexedVertices(int h, sector_t *sec, const secplane_t &plane, int floor, TArray<FFlatVertexBuffer::FIndexGenerationInfo> &gen) int FFlatVertexBuffer::CreateIndexedVertices(int h, sector_t *sec, const secplane_t &plane, int floor, VertexContainers &verts)
{ {
sec->vboindex[h] = vbo_shadowdata.Size(); sec->vboindex[h] = vbo_shadowdata.Size();
// First calculate the vertices for the sector itself // First calculate the vertices for the sector itself
sec->vboheight[h] = sec->GetPlaneTexZ(h); sec->vboheight[h] = sec->GetPlaneTexZ(h);
sec->iboindex[h] = CreateIndexedSectorVertices(sec, plane, floor, gen[sec->Index()]); sec->ibocount = verts.indices.Size();
sec->iboindex[h] = CreateIndexedSectorVertices(sec, plane, floor, verts[sec->Index()]);
// Next are all sectors using this one as heightsec // Next are all sectors using this one as heightsec
TArray<sector_t *> &fakes = sec->e->FakeFloor.Sectors; TArray<sector_t *> &fakes = sec->e->FakeFloor.Sectors;
for (unsigned g = 0; g < fakes.Size(); g++) for (unsigned g = 0; g < fakes.Size(); g++)
{ {
sector_t *fsec = fakes[g]; sector_t *fsec = fakes[g];
fsec->iboindex[2 + h] = CreateIndexedSectorVertices(fsec, plane, false, gen[fsec->Index()]); fsec->iboindex[2 + h] = CreateIndexedSectorVertices(fsec, plane, false, verts[fsec->Index()]);
} }
// and finally all attached 3D floors // and finally all attached 3D floors
@ -244,7 +220,7 @@ int FFlatVertexBuffer::CreateIndexedVertices(int h, sector_t *sec, const secplan
if (dotop || dobottom) if (dotop || dobottom)
{ {
auto ndx = CreateIndexedSectorVertices(fsec, plane, false, gen[fsec->Index()]); auto ndx = CreateIndexedSectorVertices(fsec, plane, false, verts[fsec->Index()]);
if (dotop) ffloor->top.vindex = ndx; if (dotop) ffloor->top.vindex = ndx;
if (dobottom) ffloor->bottom.vindex = ndx; if (dobottom) ffloor->bottom.vindex = ndx;
} }
@ -263,26 +239,13 @@ int FFlatVertexBuffer::CreateIndexedVertices(int h, sector_t *sec, const secplan
void FFlatVertexBuffer::CreateIndexedFlatVertices() void FFlatVertexBuffer::CreateIndexedFlatVertices()
{ {
TArray<FIndexGenerationInfo> gen; auto verts = VertexBuilder::BuildVertices();
gen.Resize(level.sectors.Size());
// This must be generated up front so that the following code knows how many vertices a sector contains.
for (unsigned i = 0; i < level.sectors.Size(); i++)
{
for (int j = 0; j < level.sectors[i].subsectorcount; j++)
{
auto sub = level.sectors[i].subsectors[j];
for (unsigned k = 0; k < sub->numlines; k++)
{
auto vert = sub->firstline[k].v1;
gen[i].AddVertex(vert);
}
}
}
for (int h = sector_t::floor; h <= sector_t::ceiling; h++) for (int h = sector_t::floor; h <= sector_t::ceiling; h++)
{ {
for (auto &sec : level.sectors) for (auto &sec : level.sectors)
{ {
CreateIndexedVertices(h, &sec, sec.GetSecPlane(h), h == sector_t::floor, gen); CreateIndexedVertices(h, &sec, sec.GetSecPlane(h), h == sector_t::floor, verts);
} }
} }

View file

@ -62,31 +62,6 @@ class FFlatVertexBuffer
static const unsigned int BUFFER_SIZE = 2000000; static const unsigned int BUFFER_SIZE = 2000000;
static const unsigned int BUFFER_SIZE_TO_USE = 1999500; static const unsigned int BUFFER_SIZE_TO_USE = 1999500;
// Temporary data for creating an indexed buffer
struct FIndexGenerationInfo
{
TArray<vertex_t *> vertices;
TMap<vertex_t*, uint32_t> vertexmap;
uint32_t AddVertex(vertex_t *vert)
{
auto check = vertexmap.CheckKey(vert);
if (check != nullptr) return *check;
auto index = vertices.Push(vert);
vertexmap[vert] = index;
return index;
}
uint32_t GetIndex(vertex_t *vert)
{
auto check = vertexmap.CheckKey(vert);
if (check != nullptr) return *check;
return ~0;
}
};
public: public:
enum enum
{ {
@ -140,9 +115,9 @@ public:
} }
private: private:
int CreateIndexedSubsectorVertices(subsector_t *sub, const secplane_t &plane, int floor, int vi, FIndexGenerationInfo &gen); int CreateIndexedSectionVertices(subsector_t *sub, const secplane_t &plane, int floor, VertexContainer &cont);
int CreateIndexedSectorVertices(sector_t *sec, const secplane_t &plane, int floor, FIndexGenerationInfo &gen); 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, TArray<FIndexGenerationInfo> &gen); int CreateIndexedVertices(int h, sector_t *sec, const secplane_t &plane, int floor, VertexContainers &cont);
void CreateIndexedFlatVertices(); void CreateIndexedFlatVertices();
void UpdatePlaneVertices(sector_t *sec, int plane); void UpdatePlaneVertices(sector_t *sec, int plane);

View file

@ -40,7 +40,6 @@ using DoublePoint = std::pair<DVector2, DVector2>;
template<> struct THashTraits<DoublePoint> template<> struct THashTraits<DoublePoint>
{ {
// Use all bits when hashing doubles instead of converting them to ints.
hash_t Hash(const DoublePoint &key) hash_t Hash(const DoublePoint &key)
{ {
return (hash_t)SuperFastHash((const char*)(const void*)&key, sizeof(key)); return (hash_t)SuperFastHash((const char*)(const void*)&key, sizeof(key));
@ -48,6 +47,15 @@ template<> struct THashTraits<DoublePoint>
int Compare(const DoublePoint &left, const DoublePoint &right) { return left != right; } int Compare(const DoublePoint &left, const DoublePoint &right) { return left != right; }
}; };
template<> struct THashTraits<FSectionVertex>
{
hash_t Hash(const FSectionVertex &key)
{
return (int)(((intptr_t)key.vertex) >> 4) ^ (key.qualifier << 16);
}
int Compare(const FSectionVertex &left, const FSectionVertex &right) { return left.vertex != right.vertex && left.qualifier != right.qualifier; }
};
struct WorkSectionLine struct WorkSectionLine
{ {
@ -613,112 +621,6 @@ public:
} }
//=============================================================================
//
//
//
//=============================================================================
// Temporary data for creating an indexed buffer
struct VertexIndexGenerationInfo
{
TArray<vertex_t *> vertices;
TMap<vertex_t*, uint32_t> vertexmap;
TArray<uint32_t> indices;
uint32_t AddVertex(vertex_t *vert)
{
auto check = vertexmap.CheckKey(vert);
if (check != nullptr) return *check;
auto index = vertices.Push(vert);
vertexmap[vert] = index;
return index;
}
uint32_t GetIndex(vertex_t *vert)
{
auto check = vertexmap.CheckKey(vert);
if (check != nullptr) return *check;
return ~0u;
}
uint32_t AddIndexForVertex(vertex_t *vert)
{
return indices.Push(GetIndex(vert));
}
uint32_t AddIndex(uint32_t indx)
{
return indices.Push(indx);
}
};
//=============================================================================
//
//
//
//=============================================================================
void CreateIndexedSubsectorVertices(subsector_t *sub, VertexIndexGenerationInfo &gen)
{
if (sub->numlines < 3) return;
uint32_t startindex = gen.indices.Size();
if ((sub->flags & SSECF_HOLE) && sub->numlines > 3)
{
// Hole filling "subsectors" are not necessarily convex so they require real triangulation.
// These things are extremely rare so performance is secondary here.
using Point = std::pair<double, double>;
std::vector<std::vector<Point>> polygon;
std::vector<Point> *curPoly;
for (unsigned i = 0; i < sub->numlines; i++)
{
polygon.resize(1);
curPoly = &polygon.back();
curPoly->push_back({ sub->firstline[i].v1->fX(), sub->firstline[i].v1->fY() });
}
auto indices = mapbox::earcut(polygon);
for (auto vti : indices)
{
gen.AddIndexForVertex(sub->firstline[vti].v1);
}
}
else
{
int firstndx = gen.GetIndex(sub->firstline[0].v1);
int secondndx = gen.GetIndex(sub->firstline[1].v1);
for (unsigned int k = 2; k < sub->numlines; k++)
{
gen.AddIndex(firstndx);
gen.AddIndex(secondndx);
auto ndx = gen.GetIndex(sub->firstline[k].v1);
gen.AddIndex(ndx);
secondndx = ndx;
}
}
}
void CreateVerticesForSection(FSectionContainer &output, FSection &section)
{
VertexIndexGenerationInfo gen;
for (auto sub : section.subsectors)
{
CreateIndexedSubsectorVertices(sub, gen);
}
section.vertexindex = output.allVertices.Size();
section.vertexcount = gen.vertices.Size();
section.indexindex = output.allVertexIndices.Size();
section.indexcount = gen.indices.Size();
output.allVertices.Append(gen.vertices);
output.allVertexIndices.Append(gen.indices);
}
//============================================================================= //=============================================================================
// //
@ -815,7 +717,7 @@ public:
output.sectionForSubsectorPtr[ssi] = curgroup; output.sectionForSubsectorPtr[ssi] = curgroup;
} }
numsubsectors += group.subsectors.Size(); numsubsectors += group.subsectors.Size();
CreateVerticesForSection(output, dest); CreateVerticesForSection(output, dest, true);
curgroup++; curgroup++;
} }
} }
@ -893,3 +795,185 @@ CCMD(printsections)
{ {
PrintSections(level.sections); PrintSections(level.sections);
} }
//=============================================================================
//
// One sector's vertex data.
//
//=============================================================================
struct VertexContainer
{
TArray<FSectionVertex> vertices;
TMap<FSectionVertex *, uint32_t> vertexmap;
bool perSubsector = false;
TArray<uint32_t> indices;
uint32_t AddVertex(FSectionVertex *vert)
{
auto check = vertexmap.CheckKey(vert);
if (check != nullptr) return *check;
auto index = vertices.Push(*vert);
vertexmap[vert] = index;
return index;
}
uint32_t AddVertex(vertex_t *vert, int qualifier)
{
FSectionVertex vertx = { vert, qualifier};
return AddVertex(&vertx);
}
uint32_t GetIndex(FSectionVertex *vert)
{
auto check = vertexmap.CheckKey(vert);
if (check != nullptr) return *check;
return ~0u;
}
uint32_t GetIndex(vertex_t *vert, int qualifier)
{
FSectionVertex vertx = { vert, qualifier};
return GetIndex(&vertx);
}
uint32_t AddIndexForVertex(FSectionVertex *vert)
{
return indices.Push(GetIndex(vert));
}
uint32_t AddIndexForVertex(vertex_t *vert, int qualifier)
{
return indices.Push(GetIndex(vert, qualifier));
}
uint32_t AddIndex(uint32_t indx)
{
return indices.Push(indx);
}
};
//=============================================================================
//
// Creates vertex meshes for sector planes
//
//=============================================================================
namespace VertexBuilder
{
//=============================================================================
//
//
//
//=============================================================================
static void CreateVerticesForSubsector(subsector_t *sub, VertexContainer &gen, int qualifier)
{
if (sub->numlines < 3) return;
uint32_t startindex = gen.indices.Size();
if ((sub->flags & SSECF_HOLE) && sub->numlines > 3)
{
// Hole filling "subsectors" are not necessarily convex so they require real triangulation.
// These things are extremely rare so performance is secondary here.
using Point = std::pair<double, double>;
std::vector<std::vector<Point>> polygon;
std::vector<Point> *curPoly;
for (unsigned i = 0; i < sub->numlines; i++)
{
polygon.resize(1);
curPoly = &polygon.back();
curPoly->push_back({ sub->firstline[i].v1->fX(), sub->firstline[i].v1->fY() });
}
auto indices = mapbox::earcut(polygon);
for (auto vti : indices)
{
gen.AddIndexForVertex(sub->firstline[vti].v1, qualifier);
}
}
else
{
int firstndx = gen.GetIndex(sub->firstline[0].v1, qualifier);
int secondndx = gen.GetIndex(sub->firstline[1].v1, qualifier);
for (unsigned int k = 2; k < sub->numlines; k++)
{
gen.AddIndex(firstndx);
gen.AddIndex(secondndx);
auto ndx = gen.GetIndex(sub->firstline[k].v1, qualifier);
gen.AddIndex(ndx);
secondndx = ndx;
}
}
}
//=============================================================================
//
//
//
//=============================================================================
static void TriangulateSection(FSection &sect, VertexContainer &gen, int qualifier)
{
if (sect.segments.Size() < 3) return;
// todo
}
//=============================================================================
//
//
//
//=============================================================================
static void CreateVerticesForSection(FSection &section, VertexContainer &gen, bool useSubsectors)
{
section.vertexindex = gen.indices.Size();
if (useSubsectors)
{
for (auto sub : section.subsectors)
{
CreateVerticesForSubsector(sub, gen, -1);
}
}
else
{
TriangulateSection(section, gen, -1);
}
section.vertexcount = gen.indices.Size() - section.vertexindex;
}
//==========================================================================
//
// Creates the vertices for one plane in one subsector
//
//==========================================================================
static void CreateVerticesForSector(sector_t *sec, VertexContainer gen)
{
auto sections = level.sections.SectionsForSector(sec);
for (auto &section :sections)
{
CreateVerticesForSection( section, gen, true);
}
}
TArray<VertexContainer> BuildVertices()
{
TArray<VertexContainer> verticesPerSector(level.sectors.Size(), true);
for (unsigned i=0; i<level.sectors.Size(); i++)
{
CreateVerticesForSector(&level.sectors[i], verticesPerSector[i]);
}
}
};

View file

@ -76,6 +76,12 @@ struct FSectionLine
side_t *sidedef; side_t *sidedef;
}; };
struct FSectionVertex
{
vertex_t *vertex; // index into vertex array
int qualifier; // some index to prevent vertices in different groups from being merged together.
};
struct FSection struct FSection
{ {
// tbd: Do we need a list of subsectors here? Ideally the subsectors should not be used anywhere anymore except for finding out where a location is. // tbd: Do we need a list of subsectors here? Ideally the subsectors should not be used anywhere anymore except for finding out where a location is.
@ -85,10 +91,8 @@ struct FSection
sector_t *sector; sector_t *sector;
FLightNode *lighthead; // Light nodes (blended and additive) FLightNode *lighthead; // Light nodes (blended and additive)
BoundingRect bounds; BoundingRect bounds;
int vertexindex; int vertexindex; // This is relative to the start of the entire sector's vertex plane data because it needs to be used with different sources.
int vertexcount; // index and length of this section's entry in the allVertices array int vertexcount;
int indexindex;
int indexcount; // index and length of this section's entry in the allVertices array
int validcount; int validcount;
short mapsection; short mapsection;
char hacked; // 1: is part of a render hack char hacked; // 1: is part of a render hack
@ -99,8 +103,6 @@ class FSectionContainer
public: public:
TArray<FSectionLine> allLines; TArray<FSectionLine> allLines;
TArray<FSection> allSections; TArray<FSection> allSections;
TArray<vertex_t*> allVertices;
TArray<uint32_t> allVertexIndices;
TArray<side_t *> allSides; TArray<side_t *> allSides;
TArray<subsector_t *> allSubsectors; TArray<subsector_t *> allSubsectors;
TArray<int> allIndices; TArray<int> allIndices;
@ -159,8 +161,6 @@ public:
allLines.Clear(); allLines.Clear();
allSections.Clear(); allSections.Clear();
allIndices.Clear(); allIndices.Clear();
allVertexIndices.Clear();
allVertices.Clear();
allSides.Clear(); allSides.Clear();
allSubsectors.Clear(); allSubsectors.Clear();
} }
@ -170,8 +170,6 @@ public:
allLines.ShrinkToFit(); allLines.ShrinkToFit();
allSections.ShrinkToFit(); allSections.ShrinkToFit();
allIndices.ShrinkToFit(); allIndices.ShrinkToFit();
allVertexIndices.ShrinkToFit();
allVertices.ShrinkToFit();
allSides.ShrinkToFit(); allSides.ShrinkToFit();
allSubsectors.ShrinkToFit(); allSubsectors.ShrinkToFit();
} }