Skip to content

Commit f0a206d

Browse files
committed
Add cvars to optimize or shuffle layers
There's plenty of optimization possible when drawing UI primitives where primitives related by texture or colour can be merged into composite GPU draw calls. This optimization can currently not be enabled wholesale as much of the Lua code has hardcoded assumptions about draw order within layers being preserved. In order to more clearly manifest such order dependencies there's also an entertaining option to completely shuffle the draw order of primitives within each layer. These new variables are `r_layerOptimize` and `r_layerShuffle`, both defaulting to 0 (off).
1 parent 685e023 commit f0a206d

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

engine/render/r_main.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <fmt/chrono.h>
1111
#include <map>
12+
#include <numeric>
13+
#include <random>
1214
#include <sstream>
1315
#include <vector>
1416

@@ -297,6 +299,9 @@ void Batch::Execute()
297299

298300
void r_layer_c::Render()
299301
{
302+
bool const optimize = renderer->r_layerOptimize->intVal == 1;
303+
bool const shuffle = renderer->r_layerShuffle->intVal == 1;
304+
300305
struct BatchKey {
301306
r_viewport_s viewport = { -1, -1, -1, -1 };
302307
int blendMode = -1;
@@ -356,7 +361,7 @@ void r_layer_c::Render()
356361
case r_layerCmd_s::QUAD: {
357362
Batch* batch{};
358363
auto I = batchIndices.find(currentKey);
359-
if (I != batchIndices.end()) {
364+
if (I != batchIndices.end() && optimize) {
360365
batch = &batches[I->second];
361366
}
362367
else {
@@ -392,9 +397,20 @@ void r_layer_c::Render()
392397

393398
std::optional<BatchKey> lastKey{};
394399
int const numBatches = batches.size();
400+
401+
std::vector<size_t> batchPermutation(numBatches);
402+
{
403+
static std::random_device rd;
404+
static std::mt19937 g(rd());
405+
std::iota(batchPermutation.begin(), batchPermutation.end(), 0);
406+
if (shuffle) {
407+
std::shuffle(batchPermutation.begin(), batchPermutation.end(), g);
408+
}
409+
}
410+
395411
for (int i = 0; i < numBatches; ++i) {
396-
auto& batch = batches[i];
397-
auto& key = batchKeys[i];
412+
auto& batch = batches[batchPermutation[i]];
413+
auto& key = batchKeys[batchPermutation[i]];
398414
if (!lastKey || lastKey->viewport.x != key.viewport.x || lastKey->viewport.y != key.viewport.y ||
399415
lastKey->viewport.width != key.viewport.width || lastKey->viewport.height != key.viewport.height)
400416
{
@@ -467,6 +483,8 @@ r_renderer_c::r_renderer_c(sys_IMain* sysHnd)
467483
r_compress = sys->con->Cvar_Add("r_compress", CV_ARCHIVE, "0");
468484
r_screenshotFormat = sys->con->Cvar_Add("r_screenshotFormat", CV_ARCHIVE, "jpg");
469485
r_layerDebug = sys->con->Cvar_Add("r_layerDebug", CV_ARCHIVE, "0");
486+
r_layerOptimize = sys->con->Cvar_Add("r_layerOptimize", CV_ARCHIVE | CV_CLAMP, "0", 0, 1);
487+
r_layerShuffle = sys->con->Cvar_Add("r_layerShuffle", CV_ARCHIVE | CV_CLAMP, "0", 0, 1);
470488

471489
Cmd_Add("screenshot", 0, "[<format>]", this, &r_renderer_c::C_Screenshot);
472490
}

engine/render/r_main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
105105
conVar_c* r_compress = nullptr;
106106
conVar_c* r_screenshotFormat = nullptr;
107107
conVar_c* r_layerDebug = nullptr;
108+
conVar_c* r_layerOptimize = nullptr;
109+
conVar_c* r_layerShuffle = nullptr;
108110

109111
r_shaderHnd_c* whiteImage = nullptr; // White image
110112

0 commit comments

Comments
 (0)