Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ParticleRenderer.h" 21 : 22 : #include "graphics/Camera.h" 23 : #include "graphics/Color.h" 24 : #include "graphics/ParticleEmitter.h" 25 : #include "graphics/ParticleEmitterType.h" 26 : #include "graphics/ShaderDefines.h" 27 : #include "graphics/ShaderManager.h" 28 : #include "graphics/ShaderTechnique.h" 29 : #include "graphics/ShaderTechniquePtr.h" 30 : #include "lib/debug.h" 31 : #include "maths/BoundingBoxAligned.h" 32 : #include "maths/Matrix3D.h" 33 : #include "maths/Vector3D.h" 34 : #include "ps/CStrIntern.h" 35 : #include "ps/CStrInternStatic.h" 36 : #include "ps/Profile.h" 37 : #include "renderer/DebugRenderer.h" 38 : #include "renderer/Renderer.h" 39 : #include "renderer/SceneRenderer.h" 40 : #include "renderer/backend/IDeviceCommandContext.h" 41 : #include "renderer/backend/IShaderProgram.h" 42 : 43 : #include <algorithm> 44 : #include <iterator> 45 : #include <utility> 46 : #include <vector> 47 : 48 10 : struct ParticleRendererInternals 49 : { 50 : int frameNumber; 51 : CShaderTechniquePtr techAdd; 52 : CShaderTechniquePtr techSubtract; 53 : CShaderTechniquePtr techOverlay; 54 : CShaderTechniquePtr techMultiply; 55 : CShaderTechniquePtr techWireframe; 56 : std::vector<CParticleEmitter*> emitters[CSceneRenderer::CULL_MAX]; 57 : }; 58 : 59 10 : ParticleRenderer::ParticleRenderer() 60 : { 61 10 : m = new ParticleRendererInternals(); 62 10 : m->frameNumber = 0; 63 10 : } 64 : 65 10 : ParticleRenderer::~ParticleRenderer() 66 : { 67 10 : delete m; 68 10 : } 69 : 70 0 : void ParticleRenderer::Submit(int cullGroup, CParticleEmitter* emitter) 71 : { 72 0 : m->emitters[cullGroup].push_back(emitter); 73 0 : } 74 : 75 0 : void ParticleRenderer::EndFrame() 76 : { 77 0 : for (std::vector<CParticleEmitter*>& cullGroupEmitters : m->emitters) 78 0 : cullGroupEmitters.clear(); 79 : // this should leave the capacity unchanged, which is okay since it 80 : // won't be very large or very variable 81 0 : } 82 : 83 : struct SortEmitterDistance 84 : { 85 0 : SortEmitterDistance(const CMatrix3D& m) : worldToCam(m) { } 86 : 87 : // TODO: if this is slow, we should pre-compute the distance for each emitter 88 : 89 0 : bool operator()(CParticleEmitter* const& a, CParticleEmitter* const& b) 90 : { 91 0 : CVector3D posa = a->GetPosition(); 92 0 : CVector3D posb = b->GetPosition(); 93 0 : if (posa == posb) 94 : return false; 95 0 : float dista = worldToCam.Transform(posa).LengthSquared(); 96 0 : float distb = worldToCam.Transform(posb).LengthSquared(); 97 0 : return distb < dista; 98 : } 99 : 100 : CMatrix3D worldToCam; 101 : }; 102 : 103 0 : void ParticleRenderer::PrepareForRendering(const CShaderDefines& context) 104 : { 105 0 : PROFILE3("prepare particles"); 106 : 107 : // Can't load the shader in the constructor because it's called before the 108 : // renderer initialisation is complete, so load it the first time through here 109 0 : if (!m->techWireframe) 110 : { 111 0 : m->techAdd = g_Renderer.GetShaderManager().LoadEffect(str_particle_add, context); 112 0 : m->techSubtract = g_Renderer.GetShaderManager().LoadEffect(str_particle_subtract, context); 113 0 : m->techOverlay = g_Renderer.GetShaderManager().LoadEffect(str_particle_overlay, context); 114 0 : m->techMultiply = g_Renderer.GetShaderManager().LoadEffect(str_particle_multiply, context); 115 0 : m->techWireframe = g_Renderer.GetShaderManager().LoadEffect(str_particle_wireframe, {}); 116 : } 117 : 118 0 : ++m->frameNumber; 119 : 120 0 : for (std::vector<CParticleEmitter*>& cullGroupEmitters : m->emitters) 121 : { 122 0 : PROFILE("update emitters"); 123 0 : for (CParticleEmitter* emitter : cullGroupEmitters) 124 : { 125 0 : emitter->UpdateArrayData(m->frameNumber); 126 0 : emitter->PrepareForRendering(); 127 : } 128 0 : } 129 : 130 0 : CMatrix3D worldToCamera; 131 0 : g_Renderer.GetSceneRenderer().GetViewCamera().GetOrientation().GetInverse(worldToCamera); 132 0 : for (std::vector<CParticleEmitter*>& cullGroupEmitters : m->emitters) 133 : { 134 : // Sort back-to-front by distance from camera 135 0 : PROFILE("sort emitters"); 136 0 : std::stable_sort(cullGroupEmitters.begin(), cullGroupEmitters.end(), SortEmitterDistance(worldToCamera)); 137 0 : } 138 : 139 : // TODO: should batch by texture here when possible, maybe 140 0 : } 141 : 142 0 : void ParticleRenderer::Upload( 143 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext) 144 : { 145 0 : for (std::vector<CParticleEmitter*>& cullGroupEmitters : m->emitters) 146 0 : for (CParticleEmitter* emitter : cullGroupEmitters) 147 0 : emitter->UploadData(deviceCommandContext); 148 0 : } 149 : 150 0 : void ParticleRenderer::RenderParticles( 151 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 152 : int cullGroup, bool wireframe) 153 : { 154 0 : CShaderTechnique* lastTech = nullptr; 155 0 : for (CParticleEmitter* emitter : m->emitters[cullGroup]) 156 : { 157 0 : CShaderTechnique* currentTech = nullptr; 158 0 : if (wireframe) 159 : { 160 0 : currentTech = m->techWireframe.get(); 161 : } 162 : else 163 : { 164 0 : switch (emitter->m_Type.m_BlendMode) 165 : { 166 0 : case CParticleEmitterType::BlendMode::ADD: currentTech = m->techAdd.get(); break; 167 0 : case CParticleEmitterType::BlendMode::SUBTRACT: currentTech = m->techSubtract.get(); break; 168 0 : case CParticleEmitterType::BlendMode::OVERLAY: currentTech = m->techOverlay.get(); break; 169 0 : case CParticleEmitterType::BlendMode::MULTIPLY: currentTech = m->techMultiply.get(); break; 170 : } 171 : } 172 0 : ENSURE(currentTech); 173 0 : if (lastTech != currentTech) 174 : { 175 0 : if (lastTech) 176 0 : deviceCommandContext->EndPass(); 177 0 : lastTech = currentTech; 178 0 : deviceCommandContext->SetGraphicsPipelineState(lastTech->GetGraphicsPipelineState()); 179 0 : deviceCommandContext->BeginPass(); 180 : 181 0 : Renderer::Backend::IShaderProgram* shader = lastTech->GetShader(); 182 0 : const CCamera& viewCamera{g_Renderer.GetSceneRenderer().GetViewCamera()}; 183 0 : const CMatrix3D transform{viewCamera.GetViewProjection()}; 184 0 : const CMatrix3D modelViewMatrix{viewCamera.GetOrientation().GetInverse()}; 185 : 186 0 : deviceCommandContext->SetUniform( 187 0 : shader->GetBindingSlot(str_transform), transform.AsFloatArray()); 188 0 : deviceCommandContext->SetUniform( 189 0 : shader->GetBindingSlot(str_modelViewMatrix), modelViewMatrix.AsFloatArray()); 190 0 : deviceCommandContext->SetUniform( 191 0 : shader->GetBindingSlot(str_cameraPos), 192 0 : viewCamera.GetOrientation().GetTranslation().AsFloatArray()); 193 : } 194 : 195 : 196 0 : const CMatrix3D rotationMatrix{emitter->GetRotation().ToMatrix()}; 197 0 : CMatrix3D spaceTransform; 198 0 : if (emitter->m_Type.m_UseLocalSpace) 199 : { 200 0 : spaceTransform = rotationMatrix; 201 0 : spaceTransform.Translate(emitter->GetPosition()); 202 : } 203 : else 204 0 : spaceTransform.SetIdentity(); 205 : 206 0 : Renderer::Backend::IShaderProgram* shader = lastTech->GetShader(); 207 0 : deviceCommandContext->SetUniform( 208 0 : shader->GetBindingSlot(str_spaceTransform), spaceTransform.AsFloatArray()); 209 0 : emitter->Bind(deviceCommandContext, lastTech->GetShader()); 210 0 : emitter->RenderArray(deviceCommandContext); 211 : } 212 : 213 0 : if (lastTech) 214 0 : deviceCommandContext->EndPass(); 215 0 : } 216 : 217 0 : void ParticleRenderer::RenderBounds(Renderer::Backend::IDeviceCommandContext& deviceCommandContext, int cullGroup) 218 : { 219 0 : for (const CParticleEmitter* emitter : m->emitters[cullGroup]) 220 : { 221 0 : const CBoundingBoxAligned bounds = 222 0 : emitter->m_Type.CalculateBounds(emitter->GetPosition(), emitter->GetParticleBounds()); 223 : 224 0 : g_Renderer.GetDebugRenderer().DrawBoundingBox(deviceCommandContext, bounds, CColor(0.0f, 1.0f, 0.0f, 1.0f), true); 225 : } 226 0 : }