group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
ParticleRenderer.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
7#include "ParticlePool.hpp"
8#include "ParticleTypes.hpp"
9
10#include <SDL3/SDL.h>
11
12#include <cstdint>
13#include <glm/glm.hpp>
14#include <vector>
15
20{
21public:
27 bool init(SDL_GPUDevice* dev, SDL_GPUTextureFormat colorFmt, SDL_GPUShaderFormat shaderFmt);
28
30 void quit();
31
32 // Upload staging arrays to GPU (must be before render pass)
33
38 void uploadBillboards(SDL_GPUCommandBuffer* cmd, const BillboardParticle* data, uint32_t count);
39
44 void uploadTracers(SDL_GPUCommandBuffer* cmd, const TracerParticle* data, uint32_t count);
45
50 void uploadRibbon(SDL_GPUCommandBuffer* cmd, const RibbonVertex* data, uint32_t count);
51
56 void uploadHitscan(SDL_GPUCommandBuffer* cmd, const HitscanBeam* data, uint32_t count);
57
62 void uploadArcs(SDL_GPUCommandBuffer* cmd, const ArcVertex* data, uint32_t count);
63
68 void uploadSmoke(SDL_GPUCommandBuffer* cmd, const SmokeParticle* data, uint32_t count);
69
74 void uploadDecals(SDL_GPUCommandBuffer* cmd, const DecalInstance* data, uint32_t count);
75
80 void uploadSdfWorld(SDL_GPUCommandBuffer* cmd, const SdfGlyphGPU* data, uint32_t count);
81
86 void uploadSdfHud(SDL_GPUCommandBuffer* cmd, const SdfGlyphGPU* data, uint32_t count);
87
88 // Draw calls (inside render pass)
89
95 void drawAll(SDL_GPURenderPass* pass, SDL_GPUCommandBuffer* cmd, float screenW, float screenH);
96
97 // Smoke noise texture (created at init)
98 [[nodiscard]] SDL_GPUTexture* smokeNoiseTex() const { return smokeNoise_; }
99 [[nodiscard]] SDL_GPUSampler* smokeSampler() const { return smokeSampler_; }
100
103 void setSdfAtlas(SDL_GPUTexture* tex, SDL_GPUSampler* samp)
104 {
105 sdfAtlasTex_ = tex;
106 sdfAtlasSamp_ = samp;
107 }
108
109private:
110 SDL_GPUDevice* device_ = nullptr;
111 SDL_GPUShaderFormat shaderFmt_ = SDL_GPU_SHADERFORMAT_INVALID;
112 SDL_GPUTextureFormat colorFmt_ = SDL_GPU_TEXTUREFORMAT_INVALID;
113
114 // Pipelines
115 SDL_GPUGraphicsPipeline* billboardPipeline_ = nullptr;
116 SDL_GPUGraphicsPipeline* tracerPipeline_ = nullptr;
117 SDL_GPUGraphicsPipeline* ribbonPipeline_ = nullptr;
118 SDL_GPUGraphicsPipeline* hitscanPipeline_ = nullptr;
119 SDL_GPUGraphicsPipeline* arcPipeline_ = nullptr;
120 SDL_GPUGraphicsPipeline* smokePipeline_ = nullptr;
121 SDL_GPUGraphicsPipeline* decalPipeline_ = nullptr;
122 SDL_GPUGraphicsPipeline* sdfWorldPipeline_ = nullptr;
123 SDL_GPUGraphicsPipeline* sdfHudPipeline_ = nullptr;
124
125 // Shared index buffer for instanced quad draws {0,1,2,2,3,0} x N
126 SDL_GPUBuffer* quadIndexBuf_ = nullptr;
127 static constexpr uint32_t k_maxQuadInstances = 4096;
128
129 // GPU buffers
130 GpuParticleBuffer billboardBuf_; // storage
131 GpuParticleBuffer tracerBuf_; // storage
132 GpuParticleBuffer ribbonBuf_; // vertex
133 GpuParticleBuffer hitscanBuf_; // storage
134 GpuParticleBuffer arcBuf_; // vertex
135 GpuParticleBuffer smokeBuf_; // storage
136 GpuParticleBuffer decalBuf_; // storage
137 GpuParticleBuffer sdfWorldBuf_; // storage
138 GpuParticleBuffer sdfHudBuf_; // storage
139
140 // Smoke noise texture
141 SDL_GPUTexture* smokeNoise_ = nullptr;
142 SDL_GPUSampler* smokeSampler_ = nullptr;
143
144 // Bullet hole / decal atlas (R8G8B8A8, procedural)
145 SDL_GPUTexture* decalTex_ = nullptr;
146 SDL_GPUSampler* decalSamp_ = nullptr;
147
148 // SDF atlas (registered from outside after SdfAtlas::init)
149 SDL_GPUTexture* sdfAtlasTex_ = nullptr;
150 SDL_GPUSampler* sdfAtlasSamp_ = nullptr;
151
152 // Internal helpers
155 [[nodiscard]] bool buildPipelines();
167 [[nodiscard]] SDL_GPUGraphicsPipeline*
168 makeStoragePipeline(const char* vertName,
169 const char* fragName,
170 uint32_t storageBufs,
171 uint32_t samplers,
172 SDL_GPUColorTargetBlendState blend,
173 bool depthTest,
174 bool depthWrite,
175 bool depthBias,
176 SDL_GPUPrimitiveType prim = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST);
177
187 [[nodiscard]] SDL_GPUGraphicsPipeline*
188 makeVertexPipeline(const char* vertName,
189 const char* fragName,
190 SDL_GPUVertexInputState vertexInput,
191 SDL_GPUColorTargetBlendState blend,
192 bool depthTest,
193 bool depthWrite,
194 SDL_GPUPrimitiveType prim = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST);
195
197 void buildQuadIndexBuffer();
198
200 void buildSmokeNoise();
201
203 void buildDecalTexture();
204
206 static SDL_GPUColorTargetBlendState additiveBlend();
207
209 static SDL_GPUColorTargetBlendState premulAlphaBlend();
210
212 static SDL_GPUColorTargetBlendState alphaBlend();
213};
GPU storage/vertex buffer paired with a CPU transfer buffer for particle uploads.
Fixed-capacity particle pool with O(1) swap-remove.
GPU-uploadable particle structs for all effect categories.
Manages a GPU storage buffer + CPU-side transfer buffer pair for particle data.
Definition GpuParticleBuffer.hpp:26
Owns all particle GPU pipelines and per-category GPU buffers.
Definition ParticleRenderer.hpp:20
void setSdfAtlas(SDL_GPUTexture *tex, SDL_GPUSampler *samp)
Register the SDF glyph atlas so drawAll() can bind it before drawing text.
Definition ParticleRenderer.hpp:103
bool init(SDL_GPUDevice *dev, SDL_GPUTextureFormat colorFmt, SDL_GPUShaderFormat shaderFmt)
Initialise GPU pipelines, buffers, and procedural textures.
Definition ParticleRenderer.cpp:463
void quit()
Release all GPU pipelines, buffers, and textures.
Definition ParticleRenderer.cpp:579
void uploadSmoke(SDL_GPUCommandBuffer *cmd, const SmokeParticle *data, uint32_t count)
Upload smoke particle data to the GPU.
Definition ParticleRenderer.cpp:662
void uploadHitscan(SDL_GPUCommandBuffer *cmd, const HitscanBeam *data, uint32_t count)
Upload hitscan beam data to the GPU.
Definition ParticleRenderer.cpp:652
void uploadRibbon(SDL_GPUCommandBuffer *cmd, const RibbonVertex *data, uint32_t count)
Upload ribbon vertex data to the GPU.
Definition ParticleRenderer.cpp:647
void uploadDecals(SDL_GPUCommandBuffer *cmd, const DecalInstance *data, uint32_t count)
Upload decal instance data to the GPU.
Definition ParticleRenderer.cpp:667
SDL_GPUSampler * smokeSampler() const
Definition ParticleRenderer.hpp:99
void uploadArcs(SDL_GPUCommandBuffer *cmd, const ArcVertex *data, uint32_t count)
Upload lightning arc vertex data to the GPU.
Definition ParticleRenderer.cpp:657
void uploadSdfHud(SDL_GPUCommandBuffer *cmd, const SdfGlyphGPU *data, uint32_t count)
Upload HUD SDF glyph data to the GPU.
Definition ParticleRenderer.cpp:677
void uploadTracers(SDL_GPUCommandBuffer *cmd, const TracerParticle *data, uint32_t count)
Upload tracer particle data to the GPU.
Definition ParticleRenderer.cpp:642
SDL_GPUTexture * smokeNoiseTex() const
Definition ParticleRenderer.hpp:98
void uploadBillboards(SDL_GPUCommandBuffer *cmd, const BillboardParticle *data, uint32_t count)
Upload billboard particle data to the GPU.
Definition ParticleRenderer.cpp:637
void uploadSdfWorld(SDL_GPUCommandBuffer *cmd, const SdfGlyphGPU *data, uint32_t count)
Upload world-space SDF glyph data to the GPU.
Definition ParticleRenderer.cpp:672
void drawAll(SDL_GPURenderPass *pass, SDL_GPUCommandBuffer *cmd, float screenW, float screenH)
Issue all particle draw calls in the correct blend/depth order.
Definition ParticleRenderer.cpp:699
Lightning arc vertex (pre-expanded triangle strip, uploaded as flat stream).
Definition ParticleTypes.hpp:58
Single billboard particle (sparks, impact flash, shockwave ring).
Definition ParticleTypes.hpp:12
World-space decal instance (bullet hole, scorch mark).
Definition ParticleTypes.hpp:80
Hitscan energy beam (main glowing quad).
Definition ParticleTypes.hpp:46
Ribbon vertex (pre-expanded on CPU, uploaded as flat vertex stream).
Definition ParticleTypes.hpp:37
SDF glyph quad (world-space or screen-space HUD).
Definition ParticleTypes.hpp:94
Smoke / fire billboard (uses noise texture for volumetric look).
Definition ParticleTypes.hpp:67
Oriented capsule streak for fast-bullet tracers (R301 style).
Definition ParticleTypes.hpp:23