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
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
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:472
GpuParticleBuffer smokeBuf_
Definition ParticleRenderer.hpp:135
SDL_GPUSampler * smokeSampler_
Definition ParticleRenderer.hpp:142
GpuParticleBuffer decalBuf_
Definition ParticleRenderer.hpp:136
void buildQuadIndexBuffer()
Build the shared quad index buffer ({0,1,2,2,3,0} x N).
Definition ParticleRenderer.cpp:183
SDL_GPUGraphicsPipeline * hitscanPipeline_
Definition ParticleRenderer.hpp:118
SDL_GPUGraphicsPipeline * arcPipeline_
Definition ParticleRenderer.hpp:119
void quit()
Release all GPU pipelines, buffers, and textures.
Definition ParticleRenderer.cpp:588
GpuParticleBuffer tracerBuf_
Definition ParticleRenderer.hpp:131
SDL_GPUSampler * decalSamp_
Definition ParticleRenderer.hpp:146
SDL_GPUBuffer * quadIndexBuf_
Definition ParticleRenderer.hpp:126
SDL_GPUGraphicsPipeline * tracerPipeline_
Definition ParticleRenderer.hpp:116
void uploadSmoke(SDL_GPUCommandBuffer *cmd, const SmokeParticle *data, uint32_t count)
Upload smoke particle data to the GPU.
Definition ParticleRenderer.cpp:671
GpuParticleBuffer hitscanBuf_
Definition ParticleRenderer.hpp:133
SDL_GPUTextureFormat colorFmt_
Definition ParticleRenderer.hpp:112
void uploadHitscan(SDL_GPUCommandBuffer *cmd, const HitscanBeam *data, uint32_t count)
Upload hitscan beam data to the GPU.
Definition ParticleRenderer.cpp:661
SDL_GPUGraphicsPipeline * sdfHudPipeline_
Definition ParticleRenderer.hpp:123
SDL_GPUGraphicsPipeline * makeStoragePipeline(const char *vertName, const char *fragName, uint32_t storageBufs, uint32_t samplers, SDL_GPUColorTargetBlendState blend, bool depthTest, bool depthWrite, bool depthBias, SDL_GPUPrimitiveType prim=SDL_GPU_PRIMITIVETYPE_TRIANGLELIST)
Create a pipeline that reads particle data from a storage buffer.
Definition ParticleRenderer.cpp:66
static SDL_GPUColorTargetBlendState premulAlphaBlend()
Return a pre-multiplied alpha blend state (src*1 + dst*(1-srcAlpha)).
Definition ParticleRenderer.cpp:36
GpuParticleBuffer sdfHudBuf_
Definition ParticleRenderer.hpp:138
bool buildPipelines()
Create all graphics pipelines for each particle category.
Definition ParticleRenderer.cpp:496
void buildSmokeNoise()
Generate and upload the procedural smoke noise texture.
Definition ParticleRenderer.cpp:237
SDL_GPUGraphicsPipeline * sdfWorldPipeline_
Definition ParticleRenderer.hpp:122
SDL_GPUGraphicsPipeline * makeVertexPipeline(const char *vertName, const char *fragName, SDL_GPUVertexInputState vertexInput, SDL_GPUColorTargetBlendState blend, bool depthTest, bool depthWrite, SDL_GPUPrimitiveType prim=SDL_GPU_PRIMITIVETYPE_TRIANGLELIST)
Create a pipeline that reads particle data from a vertex buffer.
Definition ParticleRenderer.cpp:127
void uploadRibbon(SDL_GPUCommandBuffer *cmd, const RibbonVertex *data, uint32_t count)
Upload ribbon vertex data to the GPU.
Definition ParticleRenderer.cpp:656
static constexpr uint32_t k_maxQuadInstances
Definition ParticleRenderer.hpp:127
void uploadDecals(SDL_GPUCommandBuffer *cmd, const DecalInstance *data, uint32_t count)
Upload decal instance data to the GPU.
Definition ParticleRenderer.cpp:676
GpuParticleBuffer arcBuf_
Definition ParticleRenderer.hpp:134
SDL_GPUGraphicsPipeline * smokePipeline_
Definition ParticleRenderer.hpp:120
static SDL_GPUColorTargetBlendState additiveBlend()
Return an additive blend state (src*alpha + dst*1).
Definition ParticleRenderer.cpp:20
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:666
SDL_GPUGraphicsPipeline * decalPipeline_
Definition ParticleRenderer.hpp:121
SDL_GPUTexture * decalTex_
Definition ParticleRenderer.hpp:145
void uploadSdfHud(SDL_GPUCommandBuffer *cmd, const SdfGlyphGPU *data, uint32_t count)
Upload HUD SDF glyph data to the GPU.
Definition ParticleRenderer.cpp:686
SDL_GPUGraphicsPipeline * ribbonPipeline_
Definition ParticleRenderer.hpp:117
SDL_GPUGraphicsPipeline * billboardPipeline_
Definition ParticleRenderer.hpp:115
void uploadTracers(SDL_GPUCommandBuffer *cmd, const TracerParticle *data, uint32_t count)
Upload tracer particle data to the GPU.
Definition ParticleRenderer.cpp:651
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:646
SDL_GPUTexture * sdfAtlasTex_
Definition ParticleRenderer.hpp:149
SDL_GPUDevice * device_
Definition ParticleRenderer.hpp:110
GpuParticleBuffer ribbonBuf_
Definition ParticleRenderer.hpp:132
void uploadSdfWorld(SDL_GPUCommandBuffer *cmd, const SdfGlyphGPU *data, uint32_t count)
Upload world-space SDF glyph data to the GPU.
Definition ParticleRenderer.cpp:681
void buildDecalTexture()
Generate and upload the procedural bullet-hole decal texture.
Definition ParticleRenderer.cpp:331
GpuParticleBuffer sdfWorldBuf_
Definition ParticleRenderer.hpp:137
GpuParticleBuffer billboardBuf_
Definition ParticleRenderer.hpp:130
SDL_GPUShaderFormat shaderFmt_
Definition ParticleRenderer.hpp:111
SDL_GPUTexture * smokeNoise_
Definition ParticleRenderer.hpp:141
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:708
SDL_GPUSampler * sdfAtlasSamp_
Definition ParticleRenderer.hpp:150
static SDL_GPUColorTargetBlendState alphaBlend()
Return a standard alpha blend state (src*alpha + dst*(1-srcAlpha)).
Definition ParticleRenderer.cpp:50
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