Skip to content

Commit

Permalink
Add runtime shader toggles in the ParticleGeneratorHandler just for f…
Browse files Browse the repository at this point in the history
…un and testing
  • Loading branch information
lhog committed Aug 26, 2024
1 parent 184a4f3 commit f00362d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main()
indirect[HIST_SSBO_INDRCT_Z] = 1u;
}
{
indirect[DRAW_SSBO_INDSC] = 3u * numElems;
indirect[DRAW_SSBO_INDSC] = 6u * numQuads;
indirect[DRAW_SSBO_INSTC] = 1u;
indirect[DRAW_SSBO_FIRSI] = 0u;
indirect[DRAW_SSBO_BASEV] = 0u;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void main()
float dist = distance(cameraPos, quadCenter);
#endif

dist = clamp((distance - cameraNearFar.x) / (cameraNearFar.y - cameraNearFar.x), 0.0, 1.0);
dist = clamp((dist - cameraNearFar.x) / (cameraNearFar.y - cameraNearFar.x), 0.0, 1.0);

uint key = uint(0xFFFFFFu * dist); // save in 24 bit depth format
key |= uint(drawOrder << 24u); // add drawOrder as MSB of UINT
Expand Down
93 changes: 88 additions & 5 deletions rts/Game/UnsyncedGameCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#include "Rendering/Env/IWater.h"
#include "Rendering/Env/GrassDrawer.h"
#include "Rendering/Env/Particles/ProjectileDrawer.h"
#include "Rendering/Env/Particles/Generators/ParticleGeneratorHandler.h"
#include "Rendering/Fonts/glFont.h"
#include "Rendering/Map/InfoTexture/IInfoTextureHandler.h"
#include "Rendering/Map/InfoTexture/Modern/Path.h"
Expand Down Expand Up @@ -2833,11 +2834,89 @@ class GroundDecalsActionExecutor : public IUnsyncedActionExecutor {
}
};

class DistSortParticlesActionExecutor : public IUnsyncedActionExecutor {
public:
DistSortParticlesActionExecutor() : IUnsyncedActionExecutor(
"DistSortParticles",
"Enable/Disable sorting drawn particles by camera distance"
) {
}

bool Execute(const UnsyncedAction& action) const final {
const auto& args = action.GetArgs();

const char* fmt = "ParticleGeneratorHandler distance-sorting %s";
const char* strs[] = { "disabled", "enabled" };

auto& pgh = ParticleGeneratorHandler::GetInstance();

class DistSortProjectilesActionExecutor: public IUnsyncedActionExecutor {
if (!args.empty()) {
LOG(fmt, strs[pgh.EnableSorting(StringToInt(args))]);
}
else {
LOG(fmt, strs[pgh.ToggleSorting()]);
}

return true;
}
};
class TriangleSortParticlesActionExecutor : public IUnsyncedActionExecutor {
public:
DistSortProjectilesActionExecutor(): IUnsyncedActionExecutor(
TriangleSortParticlesActionExecutor() : IUnsyncedActionExecutor(
"TriangleSortParticles",
"Enable/Disable sorting particles by triangles vs quads"
) {
}

bool Execute(const UnsyncedAction& action) const final {
const auto& args = action.GetArgs();

const char* fmt = "ParticleGeneratorHandler sorting type is %s";
const char* strs[] = { "triangles", "quads" };

auto& pgh = ParticleGeneratorHandler::GetInstance();

if (!args.empty()) {
LOG(fmt, strs[pgh.EnableTriangles(StringToInt(args))]);
}
else {
LOG(fmt, strs[pgh.ToggleTriangles()]);
}

return true;
}
};
class ProjDistSortParticlesActionExecutor : public IUnsyncedActionExecutor {
public:
ProjDistSortParticlesActionExecutor(): IUnsyncedActionExecutor(
"ProjDistSortParticles",
"Enable/Disable the use of projected distance for particles sorting"
) {
}

bool Execute(const UnsyncedAction& action) const final {
const auto& args = action.GetArgs();

const char* fmt = "ParticleGeneratorHandler distance type is %s";
const char* strs[] = { "projected", "euclidean" };

auto& pgh = ParticleGeneratorHandler::GetInstance();

if (!args.empty()) {
LOG(fmt, strs[pgh.EnableProjDistance(StringToInt(args))]);
}
else {
LOG(fmt, strs[pgh.ToggleProjDistance()]);
}

return true;
}
};


class DistSortProjectilesActionExecutor : public IUnsyncedActionExecutor {
public:
DistSortProjectilesActionExecutor() : IUnsyncedActionExecutor(
"DistSortProjectiles",
"Enable/Disable sorting drawn projectiles by camera distance"
) {
Expand All @@ -2847,18 +2926,18 @@ class DistSortProjectilesActionExecutor: public IUnsyncedActionExecutor {
const auto& args = action.GetArgs();

const char* fmt = "ProjectileDrawer distance-sorting %s";
const char* strs[] = {"disabled", "enabled"};
const char* strs[] = { "disabled", "enabled" };

if (!args.empty()) {
LOG(fmt, strs[projectileDrawer->EnableSorting(StringToInt(args))]);
} else {
}
else {
LOG(fmt, strs[projectileDrawer->ToggleSorting()]);
}

return true;
}
};

class ParticleSoftenActionExecutor : public IUnsyncedActionExecutor {
public:
ParticleSoftenActionExecutor() : IUnsyncedActionExecutor(
Expand Down Expand Up @@ -4023,6 +4102,10 @@ void UnsyncedGameCommands::AddDefaultActionExecutors()
AddActionExecutor(AllocActionExecutor<MiniMapActionExecutor>());
AddActionExecutor(AllocActionExecutor<GroundDecalsActionExecutor>());

AddActionExecutor(AllocActionExecutor<DistSortParticlesActionExecutor>());
AddActionExecutor(AllocActionExecutor<TriangleSortParticlesActionExecutor>());
AddActionExecutor(AllocActionExecutor<ProjDistSortParticlesActionExecutor>());

AddActionExecutor(AllocActionExecutor<DistSortProjectilesActionExecutor>());
AddActionExecutor(AllocActionExecutor<ParticleSoftenActionExecutor>());
AddActionExecutor(AllocActionExecutor<ParticleDrawOrderActionExecutor>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ void ParticleGeneratorHandler::GenerateAll()
{
auto bindingToken1 = cntrVBO.BindBufferRangeScoped(IndirectBufferIndices::SIZE_SSBO_BINDING_IDX);
auto bindingToken2 = indrVBO.BindBufferRangeScoped(IndirectBufferIndices::INDR_SSBO_BINDING_IDX);

indirParamsShader->SetFlag("PROCESS_TRIANGLES", PROCESS_TRIANGLES);
auto shaderToken = indirParamsShader->EnableScoped();
indirParamsShader->SetUniform("sortElemsPerThread", sortElemsPerThread);

Expand Down Expand Up @@ -393,6 +395,8 @@ void ParticleGeneratorHandler::GenerateAll()
auto bindingToken3 = keysInVBO.BindBufferRangeScoped(KeyValStorageBindings::KEYO_SSBO_BINDING_IDX);
auto bindingToken4 = valsInVBO.BindBufferRangeScoped(KeyValStorageBindings::VALO_SSBO_BINDING_IDX);

keyValShader->SetFlag("USE_PROJECTED_DISTANCE", USE_PROJECTED_DISTANCE);
keyValShader->SetFlag("PROCESS_TRIANGLES", PROCESS_TRIANGLES);
auto enToken = keyValShader->EnableScoped();
const auto& camPos = camera->GetPos();
const auto& camFwd = camera->GetForward();
Expand Down Expand Up @@ -421,8 +425,9 @@ void ParticleGeneratorHandler::GenerateAll()
#endif
}
// radix sorting
std::array<VBO*, 2> outBufs;
{
std::array<VBO*, 2> outBufs = { &keysInVBO, &valsInVBO };

if (SORT_PARTICLES) {
auto bindingToken1 = histVBO.BindBufferRangeScoped(RadixSortStorageBindings::HIST_SSBO_BINDING_IDX);
auto bindingToken2 = cntrVBO.BindBufferRangeScoped(RadixSortStorageBindings::SIZE_SSBO_BINDING_IDX);

Expand Down Expand Up @@ -476,6 +481,7 @@ void ParticleGeneratorHandler::GenerateAll()
auto bindingToken2 = indcVBO.BindBufferRangeScoped(IndicesProducerStorageBindings::IDCS_SSBO_BINDING_IDX);
auto bindingToken3 = cntrVBO.BindBufferRangeScoped(IndicesProducerStorageBindings::SIZE_SSBO_BINDING_IDX);

indcsProdShader->SetFlag("PROCESS_TRIANGLES", PROCESS_TRIANGLES);
auto enToken = indcsProdShader->EnableScoped();

glDispatchComputeIndirect(static_cast<GLintptr>(IndirectBufferIndices::KVAL_SSBO_INDRCT_X * sizeof(int32_t)));
Expand Down
12 changes: 10 additions & 2 deletions rts/Rendering/Env/Particles/Generators/ParticleGeneratorHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class ParticleGeneratorHandler {
static ParticleGeneratorHandler instance;
return instance;
}

bool EnableSorting(bool b) { return (SORT_PARTICLES = b); }
bool ToggleSorting( ) { return (SORT_PARTICLES != SORT_PARTICLES); }
bool EnableTriangles(bool b) { return (PROCESS_TRIANGLES = b); }
bool ToggleTriangles( ) { return (PROCESS_TRIANGLES != PROCESS_TRIANGLES); }
bool EnableProjDistance(bool b) { return (USE_PROJECTED_DISTANCE = b); }
bool ToggleProjDistance( ) { return (USE_PROJECTED_DISTANCE != USE_PROJECTED_DISTANCE); }
private:
void ReallocateBuffersPre();
void ReallocateBuffersPost();
Expand Down Expand Up @@ -129,6 +136,7 @@ class ParticleGeneratorHandler {
static constexpr int32_t RADIX_BIN_BIT_SIZE = 8;
static constexpr int32_t HIST_BIN_SIZE = (1 << RADIX_BIN_BIT_SIZE);

static constexpr bool PROCESS_TRIANGLES = true;
static constexpr bool USE_PROJECTED_DISTANCE = true;
bool SORT_PARTICLES = true;
bool PROCESS_TRIANGLES = true;
bool USE_PROJECTED_DISTANCE = true;
};

0 comments on commit f00362d

Please sign in to comment.