Split RenderPolyScene into two to allow rendering from multiple viewpoints

This commit is contained in:
Magnus Norddahl 2016-11-24 05:51:37 +01:00
parent 2316658dfc
commit 47aaf781c6
9 changed files with 364 additions and 255 deletions

View File

@ -1061,6 +1061,7 @@ set( NOT_COMPILED_SOURCE_FILES
set( FASTMATH_PCH_SOURCES
r_swrenderer.cpp
r_poly.cpp
r_poly_portal.cpp
r_poly_cull.cpp
r_poly_decal.cpp
r_poly_particle.cpp

View File

@ -28,21 +28,26 @@
#include "r_poly.h"
#include "gl/data/gl_data.h"
CVAR(Bool, r_debug_cull, 0, 0)
EXTERN_CVAR(Int, screenblocks)
void InitGLRMapinfoData();
/////////////////////////////////////////////////////////////////////////////
RenderPolyScene *RenderPolyScene::Instance()
{
static RenderPolyScene scene;
return &scene;
}
void RenderPolyScene::Render()
{
ClearBuffers();
SetSceneViewport();
SetupPerspectiveMatrix();
Cull.CullScene(WorldToClip);
RenderSectors();
skydome.Render(WorldToClip);
RenderTranslucent();
MainPortal.SetViewpoint(WorldToClip, 0);
MainPortal.Render();
Skydome.Render(WorldToClip);
MainPortal.RenderTranslucent();
PlayerSprites.Render();
DrawerCommandQueue::WaitForWorkers();
@ -56,13 +61,8 @@ void RenderPolyScene::RenderRemainingPlayerSprites()
void RenderPolyScene::ClearBuffers()
{
PolyVertexBuffer::Clear();
SectorSpriteRanges.clear();
SectorSpriteRanges.resize(numsectors);
SortedSprites.clear();
TranslucentObjects.clear();
PolyStencilBuffer::Instance()->Clear(RenderTarget->GetWidth(), RenderTarget->GetHeight(), 0);
PolySubsectorGBuffer::Instance()->Resize(RenderTarget->GetPitch(), RenderTarget->GetHeight());
NextSubsectorDepth = 0;
}
void RenderPolyScene::SetSceneViewport()
@ -116,179 +116,3 @@ void RenderPolyScene::SetupPerspectiveMatrix()
WorldToClip = TriMatrix::perspective(fovy, ratio, 5.0f, 65535.0f) * worldToView;
}
void RenderPolyScene::RenderSectors()
{
if (r_debug_cull)
{
for (auto it = Cull.PvsSectors.rbegin(); it != Cull.PvsSectors.rend(); ++it)
RenderSubsector(*it);
}
else
{
for (auto it = Cull.PvsSectors.begin(); it != Cull.PvsSectors.end(); ++it)
RenderSubsector(*it);
}
}
void RenderPolyScene::RenderSubsector(subsector_t *sub)
{
sector_t *frontsector = sub->sector;
frontsector->MoreFlags |= SECF_DRAWN;
uint32_t subsectorDepth = NextSubsectorDepth++;
if (sub->sector->CenterFloor() != sub->sector->CenterCeiling())
{
RenderPolyPlane::RenderPlanes(WorldToClip, sub, subsectorDepth, Cull.MaxCeilingHeight, Cull.MinFloorHeight);
}
for (uint32_t i = 0; i < sub->numlines; i++)
{
seg_t *line = &sub->firstline[i];
if (line->sidedef == nullptr || !(line->sidedef->Flags & WALLF_POLYOBJ))
{
RenderLine(sub, line, frontsector, subsectorDepth);
}
}
bool mainBSP = ((unsigned int)(sub - subsectors) < (unsigned int)numsubsectors);
if (mainBSP)
{
int subsectorIndex = (int)(sub - subsectors);
for (int i = ParticlesInSubsec[subsectorIndex]; i != NO_PARTICLE; i = Particles[i].snext)
{
particle_t *particle = Particles + i;
TranslucentObjects.push_back({ particle, sub, subsectorDepth });
}
}
SpriteRange sprites = GetSpritesForSector(sub->sector);
for (int i = 0; i < sprites.Count; i++)
{
AActor *thing = SortedSprites[sprites.Start + i].Thing;
TranslucentObjects.push_back({ thing, sub, subsectorDepth });
}
TranslucentObjects.insert(TranslucentObjects.end(), SubsectorTranslucentWalls.begin(), SubsectorTranslucentWalls.end());
SubsectorTranslucentWalls.clear();
}
SpriteRange RenderPolyScene::GetSpritesForSector(sector_t *sector)
{
if (SectorSpriteRanges.size() < sector->sectornum || sector->sectornum < 0)
return SpriteRange();
auto &range = SectorSpriteRanges[sector->sectornum];
if (range.Start == -1)
{
range.Start = (int)SortedSprites.size();
range.Count = 0;
for (AActor *thing = sector->thinglist; thing != nullptr; thing = thing->snext)
{
SortedSprites.push_back({ thing, (thing->Pos() - ViewPos).LengthSquared() });
range.Count++;
}
std::stable_sort(SortedSprites.begin() + range.Start, SortedSprites.begin() + range.Start + range.Count);
}
return range;
}
void RenderPolyScene::RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth)
{
// Reject lines not facing viewer
DVector2 pt1 = line->v1->fPos() - ViewPos;
DVector2 pt2 = line->v2->fPos() - ViewPos;
if (pt1.Y * (pt1.X - pt2.X) + pt1.X * (pt2.Y - pt1.Y) >= 0)
return;
// Cull wall if not visible
int sx1, sx2;
bool hasSegmentRange = Cull.GetSegmentRangeForLine(line->v1->fX(), line->v1->fY(), line->v2->fX(), line->v2->fY(), sx1, sx2);
if (!hasSegmentRange || Cull.IsSegmentCulled(sx1, sx2))
return;
// Tell automap we saw this
if (!r_dontmaplines && line->linedef)
{
line->linedef->flags |= ML_MAPPED;
sub->flags |= SSECF_DRAWN;
}
// Render 3D floor sides
if (line->backsector && frontsector->e && line->backsector->e->XFloor.ffloors.Size())
{
for (unsigned int i = 0; i < line->backsector->e->XFloor.ffloors.Size(); i++)
{
F3DFloor *fakeFloor = line->backsector->e->XFloor.ffloors[i];
if (!(fakeFloor->flags & FF_EXISTS)) continue;
if (!(fakeFloor->flags & FF_RENDERPLANES)) continue;
if (!fakeFloor->model) continue;
RenderPolyWall::Render3DFloorLine(WorldToClip, line, frontsector, subsectorDepth, fakeFloor, SubsectorTranslucentWalls);
}
}
// Render wall, and update culling info if its an occlusion blocker
if (RenderPolyWall::RenderLine(WorldToClip, line, frontsector, subsectorDepth, SubsectorTranslucentWalls))
{
if (hasSegmentRange)
Cull.MarkSegmentCulled(sx1, sx2);
}
}
void RenderPolyScene::RenderTranslucent()
{
for (auto it = TranslucentObjects.rbegin(); it != TranslucentObjects.rend(); ++it)
{
auto &obj = *it;
if (obj.particle)
{
RenderPolyParticle spr;
spr.Render(WorldToClip, obj.particle, obj.sub, obj.subsectorDepth);
}
else if (!obj.thing)
{
obj.wall.Render(WorldToClip);
}
else if ((obj.thing->renderflags & RF_SPRITETYPEMASK) == RF_WALLSPRITE)
{
RenderPolyWallSprite wallspr;
wallspr.Render(WorldToClip, obj.thing, obj.sub, obj.subsectorDepth);
}
else
{
RenderPolySprite spr;
spr.Render(WorldToClip, obj.thing, obj.sub, obj.subsectorDepth);
}
}
}
RenderPolyScene *RenderPolyScene::Instance()
{
static RenderPolyScene scene;
return &scene;
}
/////////////////////////////////////////////////////////////////////////////
namespace
{
int NextBufferVertex = 0;
}
TriVertex *PolyVertexBuffer::GetVertices(int count)
{
enum { VertexBufferSize = 64 * 1024 };
static TriVertex Vertex[VertexBufferSize];
if (NextBufferVertex + count > VertexBufferSize)
return nullptr;
TriVertex *v = Vertex + NextBufferVertex;
NextBufferVertex += count;
return v;
}
void PolyVertexBuffer::Clear()
{
NextBufferVertex = 0;
}

View File

@ -29,51 +29,9 @@
#include "doomdata.h"
#include "r_utility.h"
#include "r_main.h"
#include "r_poly_triangle.h"
#include "r_poly_intersection.h"
#include "r_poly_wall.h"
#include "r_poly_sprite.h"
#include "r_poly_wallsprite.h"
#include "r_poly_portal.h"
#include "r_poly_playersprite.h"
#include "r_poly_particle.h"
#include "r_poly_plane.h"
#include "r_poly_sky.h"
#include "r_poly_cull.h"
// Used for sorting things by distance to the camera
class PolySortedSprite
{
public:
PolySortedSprite(AActor *thing, double distanceSquared) : Thing(thing), DistanceSquared(distanceSquared) { }
bool operator<(const PolySortedSprite &other) const { return DistanceSquared < other.DistanceSquared; }
AActor *Thing;
double DistanceSquared;
};
class PolyTranslucentObject
{
public:
PolyTranslucentObject(particle_t *particle, subsector_t *sub, uint32_t subsectorDepth) : particle(particle), sub(sub), subsectorDepth(subsectorDepth) { }
PolyTranslucentObject(AActor *thing, subsector_t *sub, uint32_t subsectorDepth) : thing(thing), sub(sub), subsectorDepth(subsectorDepth) { }
PolyTranslucentObject(RenderPolyWall wall) : wall(wall) { }
particle_t *particle = nullptr;
AActor *thing = nullptr;
subsector_t *sub = nullptr;
uint32_t subsectorDepth = 0;
RenderPolyWall wall;
};
class SpriteRange
{
public:
SpriteRange() = default;
SpriteRange(int start, int count) : Start(start), Count(count) { }
int Start = -1;
int Count = 0;
};
// Renders a scene
class RenderPolyScene
@ -82,36 +40,15 @@ public:
void Render();
void RenderRemainingPlayerSprites();
static const uint32_t SkySubsectorDepth = 0x7fffffff;
static RenderPolyScene *Instance();
private:
void ClearBuffers();
void SetSceneViewport();
void SetupPerspectiveMatrix();
void RenderSectors();
void RenderSubsector(subsector_t *sub);
void RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth);
void RenderTranslucent();
SpriteRange GetSpritesForSector(sector_t *sector);
TriMatrix WorldToClip;
PolyCull Cull;
uint32_t NextSubsectorDepth = 0;
std::vector<SpriteRange> SectorSpriteRanges;
std::vector<PolySortedSprite> SortedSprites;
std::vector<PolyTranslucentObject> TranslucentObjects;
std::vector<PolyTranslucentObject> SubsectorTranslucentWalls;
PolySkyDome skydome;
RenderPolyPortal MainPortal;
PolySkyDome Skydome;
RenderPolyPlayerSprites PlayerSprites;
};
class PolyVertexBuffer
{
public:
static TriVertex *GetVertices(int count);
static void Clear();
};

View File

@ -26,6 +26,7 @@
#include "sbar.h"
#include "r_data/r_translate.h"
#include "r_poly_plane.h"
#include "r_poly_portal.h"
#include "r_poly.h"
#include "r_sky.h" // for skyflatnum
@ -200,7 +201,7 @@ void RenderPolyPlane::Render(const TriMatrix &worldToClip, subsector_t *sub, uin
if (fixedlightlev >= 0 || fixedcolormap)
uniforms.light = 256;
uniforms.flags = 0;
uniforms.subsectorDepth = isSky ? RenderPolyScene::SkySubsectorDepth : subsectorDepth;
uniforms.subsectorDepth = isSky ? RenderPolyPortal::SkySubsectorDepth : subsectorDepth;
TriVertex *vertices = PolyVertexBuffer::GetVertices(sub->numlines);
if (!vertices)

210
src/r_poly_portal.cpp Normal file
View File

@ -0,0 +1,210 @@
/*
** Polygon Doom software renderer
** Copyright (c) 2016 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#include <stdlib.h>
#include "templates.h"
#include "doomdef.h"
#include "sbar.h"
#include "r_data/r_translate.h"
#include "r_poly_portal.h"
#include "gl/data/gl_data.h"
CVAR(Bool, r_debug_cull, 0, 0)
/////////////////////////////////////////////////////////////////////////////
void RenderPolyPortal::SetViewpoint(const TriMatrix &worldToClip, uint32_t stencilValue)
{
WorldToClip = worldToClip;
StencilValue = stencilValue;
}
void RenderPolyPortal::Render()
{
ClearBuffers();
Cull.CullScene(WorldToClip);
RenderSectors();
for (auto &portal : Portals)
portal->Render();
}
void RenderPolyPortal::ClearBuffers()
{
SectorSpriteRanges.clear();
SectorSpriteRanges.resize(numsectors);
SortedSprites.clear();
TranslucentObjects.clear();
Portals.clear();
NextSubsectorDepth = 0;
}
void RenderPolyPortal::RenderSectors()
{
if (r_debug_cull)
{
for (auto it = Cull.PvsSectors.rbegin(); it != Cull.PvsSectors.rend(); ++it)
RenderSubsector(*it);
}
else
{
for (auto it = Cull.PvsSectors.begin(); it != Cull.PvsSectors.end(); ++it)
RenderSubsector(*it);
}
}
void RenderPolyPortal::RenderSubsector(subsector_t *sub)
{
sector_t *frontsector = sub->sector;
frontsector->MoreFlags |= SECF_DRAWN;
uint32_t subsectorDepth = NextSubsectorDepth++;
if (sub->sector->CenterFloor() != sub->sector->CenterCeiling())
{
RenderPolyPlane::RenderPlanes(WorldToClip, sub, subsectorDepth, Cull.MaxCeilingHeight, Cull.MinFloorHeight);
FSectorPortal *ceilingPortal = frontsector->ValidatePortal(sector_t::ceiling);
FSectorPortal *floorPortal = frontsector->ValidatePortal(sector_t::floor);
}
for (uint32_t i = 0; i < sub->numlines; i++)
{
seg_t *line = &sub->firstline[i];
if (line->sidedef == nullptr || !(line->sidedef->Flags & WALLF_POLYOBJ))
{
RenderLine(sub, line, frontsector, subsectorDepth);
}
}
bool mainBSP = ((unsigned int)(sub - subsectors) < (unsigned int)numsubsectors);
if (mainBSP)
{
int subsectorIndex = (int)(sub - subsectors);
for (int i = ParticlesInSubsec[subsectorIndex]; i != NO_PARTICLE; i = Particles[i].snext)
{
particle_t *particle = Particles + i;
TranslucentObjects.push_back({ particle, sub, subsectorDepth });
}
}
SpriteRange sprites = GetSpritesForSector(sub->sector);
for (int i = 0; i < sprites.Count; i++)
{
AActor *thing = SortedSprites[sprites.Start + i].Thing;
TranslucentObjects.push_back({ thing, sub, subsectorDepth });
}
TranslucentObjects.insert(TranslucentObjects.end(), SubsectorTranslucentWalls.begin(), SubsectorTranslucentWalls.end());
SubsectorTranslucentWalls.clear();
}
SpriteRange RenderPolyPortal::GetSpritesForSector(sector_t *sector)
{
if (SectorSpriteRanges.size() < sector->sectornum || sector->sectornum < 0)
return SpriteRange();
auto &range = SectorSpriteRanges[sector->sectornum];
if (range.Start == -1)
{
range.Start = (int)SortedSprites.size();
range.Count = 0;
for (AActor *thing = sector->thinglist; thing != nullptr; thing = thing->snext)
{
SortedSprites.push_back({ thing, (thing->Pos() - ViewPos).LengthSquared() });
range.Count++;
}
std::stable_sort(SortedSprites.begin() + range.Start, SortedSprites.begin() + range.Start + range.Count);
}
return range;
}
void RenderPolyPortal::RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth)
{
// Reject lines not facing viewer
DVector2 pt1 = line->v1->fPos() - ViewPos;
DVector2 pt2 = line->v2->fPos() - ViewPos;
if (pt1.Y * (pt1.X - pt2.X) + pt1.X * (pt2.Y - pt1.Y) >= 0)
return;
// Cull wall if not visible
int sx1, sx2;
bool hasSegmentRange = Cull.GetSegmentRangeForLine(line->v1->fX(), line->v1->fY(), line->v2->fX(), line->v2->fY(), sx1, sx2);
if (!hasSegmentRange || Cull.IsSegmentCulled(sx1, sx2))
return;
// Tell automap we saw this
if (!r_dontmaplines && line->linedef)
{
line->linedef->flags |= ML_MAPPED;
sub->flags |= SSECF_DRAWN;
}
// Render 3D floor sides
if (line->backsector && frontsector->e && line->backsector->e->XFloor.ffloors.Size())
{
for (unsigned int i = 0; i < line->backsector->e->XFloor.ffloors.Size(); i++)
{
F3DFloor *fakeFloor = line->backsector->e->XFloor.ffloors[i];
if (!(fakeFloor->flags & FF_EXISTS)) continue;
if (!(fakeFloor->flags & FF_RENDERPLANES)) continue;
if (!fakeFloor->model) continue;
RenderPolyWall::Render3DFloorLine(WorldToClip, line, frontsector, subsectorDepth, fakeFloor, SubsectorTranslucentWalls);
}
}
// Render wall, and update culling info if its an occlusion blocker
if (RenderPolyWall::RenderLine(WorldToClip, line, frontsector, subsectorDepth, SubsectorTranslucentWalls))
{
if (hasSegmentRange)
Cull.MarkSegmentCulled(sx1, sx2);
}
}
void RenderPolyPortal::RenderTranslucent()
{
for (auto it = Portals.rbegin(); it != Portals.rend(); ++it)
(*it)->RenderTranslucent();
for (auto it = TranslucentObjects.rbegin(); it != TranslucentObjects.rend(); ++it)
{
auto &obj = *it;
if (obj.particle)
{
RenderPolyParticle spr;
spr.Render(WorldToClip, obj.particle, obj.sub, obj.subsectorDepth);
}
else if (!obj.thing)
{
obj.wall.Render(WorldToClip);
}
else if ((obj.thing->renderflags & RF_SPRITETYPEMASK) == RF_WALLSPRITE)
{
RenderPolyWallSprite wallspr;
wallspr.Render(WorldToClip, obj.thing, obj.sub, obj.subsectorDepth);
}
else
{
RenderPolySprite spr;
spr.Render(WorldToClip, obj.thing, obj.sub, obj.subsectorDepth);
}
}
}

105
src/r_poly_portal.h Normal file
View File

@ -0,0 +1,105 @@
/*
** Polygon Doom software renderer
** Copyright (c) 2016 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#pragma once
#include <vector>
#include <memory>
#include <algorithm>
#include <functional>
#include "doomdata.h"
#include "r_utility.h"
#include "r_main.h"
#include "r_poly_triangle.h"
#include "r_poly_intersection.h"
#include "r_poly_wall.h"
#include "r_poly_sprite.h"
#include "r_poly_wallsprite.h"
#include "r_poly_playersprite.h"
#include "r_poly_particle.h"
#include "r_poly_plane.h"
#include "r_poly_cull.h"
// Used for sorting things by distance to the camera
class PolySortedSprite
{
public:
PolySortedSprite(AActor *thing, double distanceSquared) : Thing(thing), DistanceSquared(distanceSquared) { }
bool operator<(const PolySortedSprite &other) const { return DistanceSquared < other.DistanceSquared; }
AActor *Thing;
double DistanceSquared;
};
class PolyTranslucentObject
{
public:
PolyTranslucentObject(particle_t *particle, subsector_t *sub, uint32_t subsectorDepth) : particle(particle), sub(sub), subsectorDepth(subsectorDepth) { }
PolyTranslucentObject(AActor *thing, subsector_t *sub, uint32_t subsectorDepth) : thing(thing), sub(sub), subsectorDepth(subsectorDepth) { }
PolyTranslucentObject(RenderPolyWall wall) : wall(wall) { }
particle_t *particle = nullptr;
AActor *thing = nullptr;
subsector_t *sub = nullptr;
uint32_t subsectorDepth = 0;
RenderPolyWall wall;
};
class SpriteRange
{
public:
SpriteRange() = default;
SpriteRange(int start, int count) : Start(start), Count(count) { }
int Start = -1;
int Count = 0;
};
// Renders everything from a specific viewpoint
class RenderPolyPortal
{
public:
void SetViewpoint(const TriMatrix &worldToClip, uint32_t stencilValue);
void Render();
void RenderTranslucent();
static const uint32_t SkySubsectorDepth = 0x7fffffff;
private:
void ClearBuffers();
void RenderSectors();
void RenderSubsector(subsector_t *sub);
void RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth);
SpriteRange GetSpritesForSector(sector_t *sector);
TriMatrix WorldToClip;
uint32_t StencilValue = 0;
PolyCull Cull;
uint32_t NextSubsectorDepth = 0;
std::vector<SpriteRange> SectorSpriteRanges;
std::vector<PolySortedSprite> SortedSprites;
std::vector<PolyTranslucentObject> TranslucentObjects;
std::vector<PolyTranslucentObject> SubsectorTranslucentWalls;
std::vector<std::unique_ptr<RenderPolyPortal>> Portals;
};

View File

@ -25,7 +25,7 @@
#include "sbar.h"
#include "r_data/r_translate.h"
#include "r_poly_sky.h"
#include "r_poly.h"
#include "r_poly_portal.h"
#include "r_sky.h" // for skyflatnum
PolySkyDome::PolySkyDome()
@ -53,7 +53,7 @@ void PolySkyDome::Render(const TriMatrix &worldToClip)
TriUniforms uniforms;
uniforms.light = 256;
uniforms.flags = 0;
uniforms.subsectorDepth = RenderPolyScene::SkySubsectorDepth;
uniforms.subsectorDepth = RenderPolyPortal::SkySubsectorDepth;
int rc = mRows + 1;

View File

@ -523,3 +523,27 @@ TriVertex TriMatrix::operator*(TriVertex v) const
v.w = vw;
return v;
}
/////////////////////////////////////////////////////////////////////////////
namespace
{
int NextBufferVertex = 0;
}
TriVertex *PolyVertexBuffer::GetVertices(int count)
{
enum { VertexBufferSize = 256 * 1024 };
static TriVertex Vertex[VertexBufferSize];
if (NextBufferVertex + count > VertexBufferSize)
return nullptr;
TriVertex *v = Vertex + NextBufferVertex;
NextBufferVertex += count;
return v;
}
void PolyVertexBuffer::Clear()
{
NextBufferVertex = 0;
}

View File

@ -220,3 +220,10 @@ private:
TriDrawVariant variant;
TriBlendMode blendmode;
};
class PolyVertexBuffer
{
public:
static TriVertex *GetVertices(int count);
static void Clear();
};