Skip to content

Commit

Permalink
Merge pull request #787 from pbdot/ec-partition-rendering
Browse files Browse the repository at this point in the history
Sokol: Add render unit locking and isolate rendering from BSP traversal
  • Loading branch information
dashodanger authored Jan 2, 2025
2 parents 243927d + 98cfd4d commit 97dc512
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 19 deletions.
2 changes: 2 additions & 0 deletions source_files/edge/r_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void RenderBackend::SoftInit(void)

render_state->Hint(GL_FOG_HINT, GL_NICEST);
render_state->Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

LockRenderUnits(false);
}

void RenderBackend::Init()
Expand Down
11 changes: 11 additions & 0 deletions source_files/edge/r_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class RenderBackend

virtual RenderLayer GetRenderLayer() = 0;

void LockRenderUnits(bool locked)
{
units_locked_ = locked;
}

bool RenderUnitsLocked()
{
return units_locked_;
}

virtual void Resize(int32_t width, int32_t height) = 0;

virtual void Shutdown() = 0;
Expand All @@ -85,6 +95,7 @@ class RenderBackend

int32_t max_texture_size_ = 0;
int64_t frame_number_;
bool units_locked_ = false;

std::vector<FrameFinishedCallback> on_frame_finished_;
};
Expand Down
16 changes: 8 additions & 8 deletions source_files/edge/r_bsp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -411,31 +411,31 @@ static void BSPWalkSeg(DrawSubsector *dsub, Seg *seg)
{
if (f_fh < b_fh)
{
RenderSkyWall(seg, f_fh, b_fh);
QueueSkyWall(seg, f_fh, b_fh);
}
}

if (EDGE_IMAGE_IS_SKY(*f_ceil))
{
if (f_ch < fsector->sky_height && (!bsector || !EDGE_IMAGE_IS_SKY(*b_ceil) || b_fh >= f_ch))
{
RenderSkyWall(seg, f_ch, fsector->sky_height);
QueueSkyWall(seg, f_ch, fsector->sky_height);
}
else if (bsector && EDGE_IMAGE_IS_SKY(*b_ceil))
{
float max_f = HMM_MAX(f_fh, b_fh);

if (b_ch <= max_f && max_f < fsector->sky_height)
{
RenderSkyWall(seg, max_f, fsector->sky_height);
QueueSkyWall(seg, max_f, fsector->sky_height);
}
}
}
// -AJA- 2004/08/29: Emulate Sky-Flooding TRICK
else if (!debug_hall_of_mirrors.d_ && bsector && EDGE_IMAGE_IS_SKY(*b_ceil) && seg->sidedef->top.image == nullptr &&
b_ch < f_ch)
{
RenderSkyWall(seg, b_ch, f_ch);
QueueSkyWall(seg, b_ch, f_ch);
}
}

Expand Down Expand Up @@ -660,12 +660,12 @@ static void BSPWalkSubsector(int num)
{
if (EDGE_IMAGE_IS_SKY(sub->sector->floor) && view_z > sub->sector->interpolated_floor_height)
{
RenderSkyPlane(sub, sub->sector->interpolated_floor_height);
QueueSkyPlane(sub, sub->sector->interpolated_floor_height);
}

if (EDGE_IMAGE_IS_SKY(sub->sector->ceiling) && view_z < sub->sector->sky_height)
{
RenderSkyPlane(sub, sub->sector->sky_height);
QueueSkyPlane(sub, sub->sector->sky_height);
}
}

Expand Down Expand Up @@ -703,11 +703,11 @@ static void BSPWalkSubsector(int num)
}
if (EDGE_IMAGE_IS_SKY(*floor_s) && view_z > floor_h)
{
RenderSkyPlane(sub, floor_h);
QueueSkyPlane(sub, floor_h);
}
if (EDGE_IMAGE_IS_SKY(*ceil_s) && view_z < sub->sector->sky_height)
{
RenderSkyPlane(sub, sub->sector->sky_height);
QueueSkyPlane(sub, sub->sector->sky_height);
}
}
// -AJA- 2004/04/22: emulate the Deep-Water TRICK
Expand Down
86 changes: 79 additions & 7 deletions source_files/edge/r_render.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

extern ConsoleVariable draw_culling;

extern MapObject *view_camera_map_object;
extern MapObject *view_camera_map_object;
extern ConsoleVariable debug_hall_of_mirrors;

extern float sprite_skew;
Expand All @@ -69,14 +69,14 @@ extern ViewHeightZone view_height_zone;
extern Subsector *current_subsector;
extern Seg *current_seg;

extern unsigned int root_node;
extern unsigned int root_node;
extern std::list<DrawSubsector *> draw_subsector_list;

EDGE_DEFINE_CONSOLE_VARIABLE(force_flat_lighting, "0", kConsoleVariableFlagArchive)

bool solid_mode;
int detail_level = 1;
int use_dynamic_lights = 0;
int detail_level = 1;
int use_dynamic_lights = 0;

float view_x_slope;
float view_y_slope;
Expand All @@ -100,6 +100,26 @@ static bool thick_liquid = false;
static float wave_now; // value for doing wave table lookups
static float plane_z_bob; // for floor/ceiling bob DDFSECT stuff

// Sky
enum kSkyQueueType
{
kSkyQueueWall = 0,
kSkyQueuePlane = 1
};

struct SkyQueueItem
{
kSkyQueueType type_;

Seg *wallSeg_;
Subsector *planeSubsector_;

float height1_;
float height2_;
};

static std::vector<SkyQueueItem> queued_skies_;

static float Slope_GetHeight(SlopePlane *slope, float x, float y)
{
// FIXME: precompute (store in slope_plane_t)
Expand Down Expand Up @@ -1822,7 +1842,7 @@ static void DoWeaponModel(void)
// by the world geometry. NOTE: a tad expensive, but I don't
// know how any better way to prevent clipping -- the model
// needs the depth buffer for overlapping parts of itself.

render_state->Clear(GL_DEPTH_BUFFER_BIT);

solid_mode = false;
Expand Down Expand Up @@ -2023,9 +2043,29 @@ void RenderTrueBsp(void)
// needed for drawing the sky
BeginSky();

render_backend->LockRenderUnits(true);

// walk the bsp tree
BspWalkNode(root_node);

render_backend->LockRenderUnits(false);

for (size_t i = 0; i < queued_skies_.size(); i++)
{
SkyQueueItem *item = &queued_skies_[i];
switch (item->type_)
{
case kSkyQueuePlane:
RenderSkyPlane(item->planeSubsector_, item->height1_);
break;
case kSkyQueueWall:
RenderSkyWall(item->wallSeg_, item->height1_, item->height2_);
break;
}
}

queued_skies_.clear();

FinishSky();

RenderSubList(draw_subsector_list);
Expand Down Expand Up @@ -2068,7 +2108,7 @@ void RenderTrueBsp(void)
if (FlashFirst == true)
{
render_backend->SetRenderLayer(kRenderLayerWeapon);
render_backend->SetupMatrices3D();
render_backend->SetupMatrices3D();
render_state->Enable(GL_DEPTH_TEST);
DoWeaponModel();
render_state->Disable(GL_DEPTH_TEST);
Expand All @@ -2080,7 +2120,6 @@ void RenderTrueBsp(void)
#endif
}


void RenderView(int x, int y, int w, int h, MapObject *camera, bool full_height, float expand_w)
{
EDGE_ZoneScoped;
Expand All @@ -2103,6 +2142,39 @@ void RenderView(int x, int y, int w, int h, MapObject *camera, bool full_height,
RenderTrueBsp();
}

static void QueueSky(const SkyQueueItem &item)
{
if (!queued_skies_.size())
{
queued_skies_.reserve(4 * 1024);
}
else if (queued_skies_.size() == queued_skies_.capacity())
{
queued_skies_.reserve(queued_skies_.size() * 2);
}

queued_skies_.emplace_back(item);
}

void QueueSkyWall(Seg *seg, float h1, float h2)
{
SkyQueueItem item;
item.height1_ = h1;
item.height2_ = h2;
item.wallSeg_ = seg;
item.type_ = kSkyQueueWall;
QueueSky(item);
}

void QueueSkyPlane(Subsector *sub, float h)
{
SkyQueueItem item;
item.height1_ = h;
item.planeSubsector_ = sub;
item.type_ = kSkyQueuePlane;
QueueSky(item);
}

#ifdef _DISABLE_FLOODPLANES

static constexpr uint8_t kMaximumFloodVertices = 16;
Expand Down
11 changes: 7 additions & 4 deletions source_files/edge/r_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ void MirrorPush(DrawMirror *mir);
void MirrorPop();
void RenderMirror(DrawMirror *mir);
bool MirrorSegOnPortal(Seg *seg);
void MirrorPushSubsector(int32_t index, DrawSubsector* subsector);
void MirrorPushSubsector(int32_t index, DrawSubsector *subsector);

void MirrorTransform(int32_t index, float& x, float& y);
void MirrorTransform(int32_t index, float &x, float &y);
bool MirrorIsPortal(int32_t index);
Seg* MirrorSeg(int32_t index);
Seg *MirrorSeg(int32_t index);

int32_t MirrorTotalActive();

void RenderSubList(std::list<DrawSubsector *> &dsubs, bool for_mirror = false);

void BspWalkNode(unsigned int);
void BspWalkNode(unsigned int);

void QueueSkyWall(Seg *seg, float h1, float h2);
void QueueSkyPlane(Subsector *sub, float h);
27 changes: 27 additions & 0 deletions source_files/edge/render/gl/gl_units.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "i_defs_gl.h"
#include "im_data.h"
#include "m_argv.h"
#include "r_backend.h"
#include "r_colormap.h"
#include "r_gldefs.h"
#include "r_image.h"
Expand Down Expand Up @@ -104,6 +105,11 @@ RGBAColor culling_fog_color;
//
void StartUnitBatch(bool sort_em)
{
if (render_backend->RenderUnitsLocked())
{
FatalError("StartUnitBatch - Render units are locked");
}

current_render_vert = current_render_unit = 0;

batch_sort = sort_em;
Expand All @@ -118,6 +124,11 @@ void StartUnitBatch(bool sort_em)
//
void FinishUnitBatch(void)
{
if (render_backend->RenderUnitsLocked())
{
FatalError("FinishUnitBatch - Render units are locked");
}

RenderCurrentUnits();
}

Expand All @@ -134,6 +145,11 @@ void FinishUnitBatch(void)
RendererVertex *BeginRenderUnit(GLuint shape, int max_vert, GLuint env1, GLuint tex1, GLuint env2, GLuint tex2,
int pass, int blending, RGBAColor fog_color, float fog_density)
{
if (render_backend->RenderUnitsLocked())
{
FatalError("BeginRenderUnit - Render units are locked");
}

RendererUnit *unit;

EPI_ASSERT(max_vert > 0);
Expand Down Expand Up @@ -175,6 +191,12 @@ RendererVertex *BeginRenderUnit(GLuint shape, int max_vert, GLuint env1, GLuint
//
void EndRenderUnit(int actual_vert)
{

if (render_backend->RenderUnitsLocked())
{
FatalError("EndRenderUnit - Render units are locked");
}

RendererUnit *unit;

EPI_ASSERT(actual_vert >= 0);
Expand Down Expand Up @@ -225,6 +247,11 @@ void RenderCurrentUnits(void)
{
EDGE_ZoneScoped;

if (render_backend->RenderUnitsLocked())
{
FatalError("RenderCurrentUnits - Render units are locked");
}

if (current_render_unit == 0)
return;

Expand Down
Loading

0 comments on commit 97dc512

Please sign in to comment.