From 35adfe8abf1367380956a6e4d72f724298fae674 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Mar 2020 12:33:22 -0400 Subject: [PATCH 1/5] CElementGen: Alphabetize forward declarations Makes them nicer to visually scan over. --- Runtime/Particle/CElementGen.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Runtime/Particle/CElementGen.hpp b/Runtime/Particle/CElementGen.hpp index 8319d11e9..2c308dcea 100644 --- a/Runtime/Particle/CElementGen.hpp +++ b/Runtime/Particle/CElementGen.hpp @@ -19,13 +19,13 @@ #include namespace urde { -class CWarp; -class CLight; -class IGenDescription; -class CGenDescription; -class CParticleSwoosh; -class CParticleElectric; class CActorLights; +class CGenDescription; +class CLight; +class CParticleElectric; +class CParticleSwoosh; +class CWarp; +class IGenDescription; class CElementGen : public CParticleGen { static u16 g_GlobalSeed; From 9db07b037a389d5f8dbf796c15e70bd74268eecb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Mar 2020 12:37:15 -0400 Subject: [PATCH 2/5] CElementGen: Make use of std::array where applicable --- .../Graphics/Shaders/CElementGenShaders.cpp | 12 +++++----- Runtime/Particle/CElementGen.cpp | 24 ++++++++++++------- Runtime/Particle/CElementGen.hpp | 20 ++++++++-------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Runtime/Graphics/Shaders/CElementGenShaders.cpp b/Runtime/Graphics/Shaders/CElementGenShaders.cpp index 03e825184..155b6fc10 100644 --- a/Runtime/Graphics/Shaders/CElementGenShaders.cpp +++ b/Runtime/Graphics/Shaders/CElementGenShaders.cpp @@ -249,28 +249,28 @@ void CElementGenShaders::BuildShaderDataBinding(boo::IGraphicsDataFactory::Conte const std::array, 1> uniforms{gen.m_uniformBuf.get()}; if (regPipeline != nullptr) { - for (size_t i = 0; i < std::size(gen.m_normalDataBind); ++i) { + for (size_t i = 0; i < gen.m_normalDataBind.size(); ++i) { gen.m_normalDataBind[i] = ctx.newShaderDataBinding((*regPipeline)[i], nullptr, gen.m_instBuf.get(), nullptr, uniforms.size(), uniforms.data(), nullptr, texCount, textures.data(), nullptr, nullptr); } } if (regPipelineSub != nullptr) { - for (size_t i = 0; i < std::size(gen.m_normalSubDataBind); ++i) { + for (size_t i = 0; i < gen.m_normalSubDataBind.size(); ++i) { gen.m_normalSubDataBind[i] = ctx.newShaderDataBinding((*regPipelineSub)[i], nullptr, gen.m_instBuf.get(), nullptr, uniforms.size(), uniforms.data(), nullptr, texCount, textures.data(), nullptr, nullptr); } } if (redToAlphaPipeline != nullptr) { - for (size_t i = 0; i < std::size(gen.m_redToAlphaDataBind); ++i) { + for (size_t i = 0; i < gen.m_redToAlphaDataBind.size(); ++i) { gen.m_redToAlphaDataBind[i] = ctx.newShaderDataBinding((*redToAlphaPipeline)[i], nullptr, gen.m_instBuf.get(), nullptr, uniforms.size(), uniforms.data(), nullptr, texCount, textures.data(), nullptr, nullptr); } } if (redToAlphaPipelineSub != nullptr) { - for (size_t i = 0; i < std::size(gen.m_redToAlphaSubDataBind); ++i) { + for (size_t i = 0; i < gen.m_redToAlphaSubDataBind.size(); ++i) { gen.m_redToAlphaSubDataBind[i] = ctx.newShaderDataBinding( (*redToAlphaPipelineSub)[i], nullptr, gen.m_instBuf.get(), nullptr, uniforms.size(), uniforms.data(), nullptr, texCount, textures.data(), nullptr, nullptr); @@ -283,14 +283,14 @@ void CElementGenShaders::BuildShaderDataBinding(boo::IGraphicsDataFactory::Conte texCount = std::min(texCount, 1); if (regPipelinePmus != nullptr) { - for (size_t i = 0; i < std::size(gen.m_normalDataBindPmus); ++i) { + for (size_t i = 0; i < gen.m_normalDataBindPmus.size(); ++i) { gen.m_normalDataBindPmus[i] = ctx.newShaderDataBinding((*regPipelinePmus)[i], nullptr, gen.m_instBufPmus.get(), nullptr, uniforms.size(), uniforms.data(), nullptr, texCount, textures.data(), nullptr, nullptr); } } if (redToAlphaPipelinePmus != nullptr) { - for (size_t i = 0; i < std::size(gen.m_redToAlphaDataBindPmus); ++i) { + for (size_t i = 0; i < gen.m_redToAlphaDataBindPmus.size(); ++i) { gen.m_redToAlphaDataBindPmus[i] = ctx.newShaderDataBinding( (*redToAlphaPipelinePmus)[i], nullptr, gen.m_instBufPmus.get(), nullptr, uniforms.size(), uniforms.data(), nullptr, texCount, textures.data(), nullptr, nullptr); diff --git a/Runtime/Particle/CElementGen.cpp b/Runtime/Particle/CElementGen.cpp index 0e187f5d7..22c170fd8 100644 --- a/Runtime/Particle/CElementGen.cpp +++ b/Runtime/Particle/CElementGen.cpp @@ -16,7 +16,15 @@ #define MAX_GLOBAL_PARTICLES 2560 namespace urde { -static logvisor::Module Log("urde::CElementGen"); +namespace { +logvisor::Module Log("urde::CElementGen"); + +constexpr std::array ShadClsSizes{ + sizeof(SParticleInstanceTex), + sizeof(SParticleInstanceIndTex), + sizeof(SParticleInstanceNoTex), +}; +} // Anonymous namespace u16 CElementGen::g_GlobalSeed = 99; bool CElementGen::g_subtractBlend = false; @@ -45,9 +53,6 @@ void CElementGen::Initialize() { void CElementGen::Shutdown() { CElementGenShaders::Shutdown(); } -static const size_t ShadClsSizes[] = {sizeof(SParticleInstanceTex), sizeof(SParticleInstanceIndTex), - sizeof(SParticleInstanceNoTex)}; - CElementGen::CElementGen(const TToken& gen, EModelOrientationType orientType, EOptionalSystemFlags flags) : x1c_genDesc(gen), x2c_orientType(orientType), x27c_randState(x94_randomSeed) { @@ -212,11 +217,11 @@ CElementGen::CElementGen(const TToken& gen, EModelOrientationTy CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) { if (!x26c_31_LINE) { - m_instBuf = ctx.newDynamicBuffer(boo::BufferUse::Vertex, ShadClsSizes[int(m_shaderClass)], maxInsts); + m_instBuf = ctx.newDynamicBuffer(boo::BufferUse::Vertex, ShadClsSizes[size_t(m_shaderClass)], maxInsts); m_uniformBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(SParticleUniforms), 1); } if (desc->x45_24_x31_26_PMUS) { - m_instBufPmus = ctx.newDynamicBuffer(boo::BufferUse::Vertex, ShadClsSizes[int(m_shaderClass)], maxInsts); + m_instBufPmus = ctx.newDynamicBuffer(boo::BufferUse::Vertex, ShadClsSizes[size_t(m_shaderClass)], maxInsts); m_uniformBufPmus = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(SParticleUniforms), 1); } CElementGenShaders::BuildShaderDataBinding(ctx, *this); @@ -366,7 +371,7 @@ void CElementGen::UpdateAdvanceAccessParameters(u32 activeParticleCount, u32 par adv8->GetValue(particleFrame, arr[7]); } -bool CElementGen::UpdateVelocitySource(u32 idx, u32 particleFrame, CParticle& particle) { +bool CElementGen::UpdateVelocitySource(size_t idx, u32 particleFrame, CParticle& particle) { bool err; if (x278_hasVMD[idx]) { zeus::CVector3f localVel = x208_orientationInverse * particle.x1c_vel; @@ -433,9 +438,10 @@ void CElementGen::UpdateExistingParticles() { ++x25c_activeParticleCount; - for (int i = 0; i < 4; ++i) { - if (!x280_VELSources[i]) + for (size_t i = 0; i < x280_VELSources.size(); ++i) { + if (!x280_VELSources[i]) { break; + } UpdateVelocitySource(i, particleFrame, particle); } diff --git a/Runtime/Particle/CElementGen.hpp b/Runtime/Particle/CElementGen.hpp index 2c308dcea..f8d2ef3db 100644 --- a/Runtime/Particle/CElementGen.hpp +++ b/Runtime/Particle/CElementGen.hpp @@ -68,7 +68,7 @@ private: int m_maxMAXP = 256; u16 x94_randomSeed = g_GlobalSeed; float x98_generatorRate = 1.f; - float x9c_externalVars[16] = {}; + std::array x9c_externalVars{}; zeus::CVector3f xdc_translation; zeus::CVector3f xe8_globalTranslation; @@ -110,9 +110,9 @@ private: int x270_MBSP = 0; int m_maxMBSP = 0; ERglLightBits x274_backupLightActive = ERglLightBits::None; - bool x278_hasVMD[4] = {}; + std::array x278_hasVMD{}; CRandom16 x27c_randState; - CModVectorElement* x280_VELSources[4] = {}; + std::array x280_VELSources{}; std::vector> x290_activePartChildren; int x2a0_CSSD = 0; @@ -148,15 +148,15 @@ public: EOptionalSystemFlags flags = EOptionalSystemFlags::One); ~CElementGen() override; - boo::ObjToken m_normalDataBind[2]; - boo::ObjToken m_normalSubDataBind[2]; - boo::ObjToken m_redToAlphaDataBind[2]; - boo::ObjToken m_redToAlphaSubDataBind[2]; + std::array, 2> m_normalDataBind; + std::array, 2> m_normalSubDataBind; + std::array, 2> m_redToAlphaDataBind; + std::array, 2> m_redToAlphaSubDataBind; boo::ObjToken m_instBuf; boo::ObjToken m_uniformBuf; - boo::ObjToken m_normalDataBindPmus[2]; - boo::ObjToken m_redToAlphaDataBindPmus[2]; + std::array, 2> m_normalDataBindPmus; + std::array, 2> m_redToAlphaDataBindPmus; boo::ObjToken m_instBufPmus; boo::ObjToken m_uniformBufPmus; @@ -171,7 +171,7 @@ public: static void Shutdown(); void UpdateAdvanceAccessParameters(u32 activeParticleCount, u32 particleFrame); - bool UpdateVelocitySource(u32 idx, u32 particleFrame, CParticle& particle); + bool UpdateVelocitySource(size_t idx, u32 particleFrame, CParticle& particle); void UpdateExistingParticles(); void CreateNewParticles(int); void UpdatePSTranslationAndOrientation(); From a8486cf47983c2f92e44923be7a343c37b8d591a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Mar 2020 12:54:59 -0400 Subject: [PATCH 3/5] CElementGen: Resolve sign-conversion warnings in loops Prevents implicit truncation to int from occurring. --- Runtime/Particle/CElementGen.cpp | 52 +++++++++++++++++--------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/Runtime/Particle/CElementGen.cpp b/Runtime/Particle/CElementGen.cpp index 22c170fd8..45a9b880c 100644 --- a/Runtime/Particle/CElementGen.cpp +++ b/Runtime/Particle/CElementGen.cpp @@ -925,7 +925,7 @@ void CElementGen::RenderModels(const CActorLights* actorLights) { zeus::CVector3f pmopVec; auto matrixIt = x50_parentMatrices.begin(); - for (int i = 0; i < x30_particles.size(); ++i) { + for (size_t i = 0; i < x30_particles.size(); ++i) { CParticle& particle = x30_particles[i]; g_currentParticle = &particle; @@ -1124,7 +1124,7 @@ void CElementGen::RenderLines() { m_lineRenderer->Reset(); - for (int i = 0; i < x30_particles.size(); ++i) { + for (size_t i = 0; i < x30_particles.size(); ++i) { CParticle& particle = x30_particles[i]; g_currentParticle = &particle; @@ -1227,9 +1227,9 @@ void CElementGen::RenderParticles() { if (desc->x44_28_x30_28_SORT) { sortItems.reserve(x30_particles.size()); - for (int i = 0; i < x30_particles.size(); ++i) { + for (size_t i = 0; i < x30_particles.size(); ++i) { const CParticle& particle = x30_particles[i]; - sortItems.emplace_back(i); + sortItems.emplace_back(s16(i)); CParticleListItem& sortItem = sortItems.back(); sortItem.x4_viewPoint = systemCameraMatrix * ((particle.x4_pos - particle.x10_prevPos) * x80_timeDeltaScale + particle.x10_prevPos); @@ -1303,18 +1303,19 @@ void CElementGen::RenderParticles() { } if (!x26c_29_ORNT) { - for (int i = 0; i < x30_particles.size(); ++i) { - int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : i; + for (size_t i = 0; i < x30_particles.size(); ++i) { + const int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : int(i); CParticle& particle = x30_particles[partIdx]; g_currentParticle = &particle; - int partFrame = x74_curFrame - particle.x28_startFrame - 1; + const int partFrame = x74_curFrame - particle.x28_startFrame - 1; zeus::CVector3f viewPoint; - if (desc->x44_28_x30_28_SORT) + if (desc->x44_28_x30_28_SORT) { viewPoint = sortItems[i].x4_viewPoint; - else + } else { viewPoint = systemCameraMatrix * ((particle.x4_pos - particle.x10_prevPos) * x80_timeDeltaScale + particle.x10_prevPos); + } if (!constUVs) { CParticleGlobals::instance()->SetParticleLifetime(particle.x0_endFrame - particle.x28_startFrame); @@ -1322,7 +1323,7 @@ void CElementGen::RenderParticles() { texr->GetValueUV(partFrame, uvs); } - float size = 0.5f * particle.x2c_lineLengthOrSize; + const float size = 0.5f * particle.x2c_lineLengthOrSize; if (0.f == particle.x30_lineWidthOrRota) { switch (m_shaderClass) { case CElementGenShaders::EShaderClass::Tex: { @@ -1392,15 +1393,15 @@ void CElementGen::RenderParticles() { } } } else { - for (int i = 0; i < x30_particles.size(); ++i) { - int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : i; + for (size_t i = 0; i < x30_particles.size(); ++i) { + const int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : int(i); CParticle& particle = x30_particles[partIdx]; g_currentParticle = &particle; - int partFrame = x74_curFrame - particle.x28_startFrame - 1; + const int partFrame = x74_curFrame - particle.x28_startFrame - 1; zeus::CVector3f viewPoint = ((particle.x4_pos - particle.x10_prevPos) * x80_timeDeltaScale + particle.x10_prevPos); - float width = !desc->x50_x3c_ROTA ? 1.f : particle.x30_lineWidthOrRota; + const float width = !desc->x50_x3c_ROTA ? 1.f : particle.x30_lineWidthOrRota; zeus::CVector3f dir; if (particle.x1c_vel.canBeNormalized()) { dir = particle.x1c_vel.normalized(); @@ -1495,13 +1496,13 @@ void CElementGen::RenderParticles() { Log.report(logvisor::Fatal, fmt("unexpected particle shader class")); break; } - float mbspFac = 1.f / float(mbspVal); - for (int i = 0; i < x30_particles.size(); ++i) { - int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : i; + const float mbspFac = 1.f / float(mbspVal); + for (size_t i = 0; i < x30_particles.size(); ++i) { + const int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : int(i); CParticle& particle = x30_particles[partIdx]; g_currentParticle = &particle; - int partFrame = x74_curFrame - particle.x28_startFrame - 1; + const int partFrame = x74_curFrame - particle.x28_startFrame - 1; if (!constUVs) { CParticleGlobals::instance()->SetParticleLifetime(particle.x0_endFrame - particle.x28_startFrame); @@ -1652,9 +1653,9 @@ void CElementGen::RenderParticlesIndirectTexture() { if (desc->x44_28_x30_28_SORT) { sortItems.reserve(x30_particles.size()); - for (int i = 0; i < x30_particles.size(); ++i) { + for (size_t i = 0; i < x30_particles.size(); ++i) { const CParticle& particle = x30_particles[i]; - sortItems.emplace_back(i); + sortItems.emplace_back(s16(i)); CParticleListItem& sortItem = sortItems.back(); sortItem.x4_viewPoint = systemCameraMatrix * ((particle.x4_pos - particle.x10_prevPos) * x80_timeDeltaScale + particle.x10_prevPos); @@ -1671,18 +1672,19 @@ void CElementGen::RenderParticlesIndirectTexture() { if (!x30_particles.empty()) CGraphics::SetShaderDataBinding(m_normalDataBind[g_Renderer->IsThermalVisorHotPass()]); - for (int i = 0; i < x30_particles.size(); ++i) { - int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : i; + for (size_t i = 0; i < x30_particles.size(); ++i) { + const int partIdx = desc->x44_28_x30_28_SORT ? sortItems[i].x0_partIdx : int(i); CParticle& particle = x30_particles[partIdx]; g_currentParticle = &particle; - int thisPartFrame = x74_curFrame - particle.x28_startFrame; + const int thisPartFrame = x74_curFrame - particle.x28_startFrame; zeus::CVector3f viewPoint; - if (desc->x44_28_x30_28_SORT) + if (desc->x44_28_x30_28_SORT) { viewPoint = sortItems[i].x4_viewPoint; - else + } else { viewPoint = systemCameraMatrix * ((particle.x4_pos - particle.x10_prevPos) * x80_timeDeltaScale + particle.x10_prevPos); + } if (!constTexr) { CTexture* tex = texr->GetValueTexture(thisPartFrame).GetObj(); From af2a82b5a76249ec9eb1896c1bc719856d899076 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Mar 2020 13:03:40 -0400 Subject: [PATCH 4/5] CElementGen: Make GetSystemCount() a const member function This doesn't modify internal member state, so this can be made a const member function. --- Runtime/Particle/CElementGen.cpp | 11 ++++++----- Runtime/Particle/CElementGen.hpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Runtime/Particle/CElementGen.cpp b/Runtime/Particle/CElementGen.cpp index 45a9b880c..9516fbdf2 100644 --- a/Runtime/Particle/CElementGen.cpp +++ b/Runtime/Particle/CElementGen.cpp @@ -786,16 +786,17 @@ void CElementGen::BuildParticleSystemBounds() { x2f0_systemBounds.accumulateBounds(aabb); } -u32 CElementGen::GetSystemCount() { +u32 CElementGen::GetSystemCount() const { u32 ret = 0; for (const std::unique_ptr& child : x290_activePartChildren) { - if (child->Get4CharId() == FOURCC('PART')) - ret += static_cast(*child).GetSystemCount(); - else + if (child->Get4CharId() == FOURCC('PART')) { + ret += static_cast(*child).GetSystemCount(); + } else { ret += 1; + } } - return (ret + (x25c_activeParticleCount != 0)); + return ret + (x25c_activeParticleCount != 0); } void CElementGen::Render(const CActorLights* actorLights) { diff --git a/Runtime/Particle/CElementGen.hpp b/Runtime/Particle/CElementGen.hpp index f8d2ef3db..2a6595e6d 100644 --- a/Runtime/Particle/CElementGen.hpp +++ b/Runtime/Particle/CElementGen.hpp @@ -180,7 +180,7 @@ public: void UpdateLightParameters(); void BuildParticleSystemBounds(); u32 GetEmitterTime() const { return x74_curFrame; } - u32 GetSystemCount(); + u32 GetSystemCount() const; u32 GetCumulativeParticleCount() const { return x260_cumulativeParticles; } u32 GetParticleCountAllInternal() const; u32 GetParticleCountAll() const { return x264_recursiveParticleCount; } From 15dce1683e399a9e29a02034b4dac8e9775a02cd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Mar 2020 13:07:51 -0400 Subject: [PATCH 5/5] CElementGen: std::move TToken instance in constructor Allows the parameter to be moved into via calling code. --- Runtime/Particle/CElementGen.cpp | 5 ++--- Runtime/Particle/CElementGen.hpp | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Runtime/Particle/CElementGen.cpp b/Runtime/Particle/CElementGen.cpp index 9516fbdf2..e1d614970 100644 --- a/Runtime/Particle/CElementGen.cpp +++ b/Runtime/Particle/CElementGen.cpp @@ -53,9 +53,8 @@ void CElementGen::Initialize() { void CElementGen::Shutdown() { CElementGenShaders::Shutdown(); } -CElementGen::CElementGen(const TToken& gen, EModelOrientationType orientType, - EOptionalSystemFlags flags) -: x1c_genDesc(gen), x2c_orientType(orientType), x27c_randState(x94_randomSeed) { +CElementGen::CElementGen(TToken gen, EModelOrientationType orientType, EOptionalSystemFlags flags) +: x1c_genDesc(std::move(gen)), x2c_orientType(orientType), x27c_randState(x94_randomSeed) { CGenDescription* desc = x1c_genDesc.GetObj(); x28_loadedGenDesc = desc; diff --git a/Runtime/Particle/CElementGen.hpp b/Runtime/Particle/CElementGen.hpp index 2a6595e6d..893211428 100644 --- a/Runtime/Particle/CElementGen.hpp +++ b/Runtime/Particle/CElementGen.hpp @@ -144,8 +144,8 @@ private: void AccumulateBounds(const zeus::CVector3f& pos, float size); public: - CElementGen(const TToken& gen, EModelOrientationType orientType = EModelOrientationType::Normal, - EOptionalSystemFlags flags = EOptionalSystemFlags::One); + explicit CElementGen(TToken gen, EModelOrientationType orientType = EModelOrientationType::Normal, + EOptionalSystemFlags flags = EOptionalSystemFlags::One); ~CElementGen() override; std::array, 2> m_normalDataBind;