group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
SkinnedRenderer.hpp
Go to the documentation of this file.
1
26
27#pragma once
28
29#include "Camera.hpp" // FrustumPlanes
30#include "RendererTypes.hpp" // BoneInfluence, SkinnedInstance, RigMeshSource
31
32#include <SDL3/SDL.h>
33#include <SDL3/SDL_gpu.h>
34
35#include <glm/glm.hpp>
36#include <vector>
37
40{
41public:
42 SkinnedRenderer() = default;
43 ~SkinnedRenderer() = default;
44
49
54 void init(SDL_GPUDevice* device,
55 SDL_GPUTextureFormat& colorTarget,
56 const SDL_GPUShaderFormat& shaderFormat);
57
60 void setFrustumCullEnabled(bool enabled) { frustumCullEnabled_ = enabled; }
61
80 bool setRig(const std::vector<RigMeshSource>& meshes, int numJoints);
81
113 void setFrame(const std::vector<glm::mat4>& palette,
114 const std::vector<SkinnedInstance>& instances,
115 const FrustumPlanes& frustum);
116
125 void uploadFrame(SDL_GPUCommandBuffer* cmd, SDL_GPUCopyPass* copyPass);
126
133 void draw(SDL_GPURenderPass* renderPass, SDL_GPUCommandBuffer* cmd);
134
144 void drawDepth(SDL_GPURenderPass* renderPass, SDL_GPUCommandBuffer* cmd);
145
151 void drawChams(SDL_GPURenderPass* renderPass, SDL_GPUCommandBuffer* cmd);
152
157 void drawKillcamHighlight(SDL_GPURenderPass* renderPass, SDL_GPUCommandBuffer* cmd);
158
161 void shutdown();
162
163 // ─── Readback (optional, for debug UI) ───────────────────────────────────
164
166 [[nodiscard]] int numJoints() const { return numJoints_; }
167
169 [[nodiscard]] bool rigInstalled() const { return rigInstalled_; }
170
172 [[nodiscard]] size_t pendingInstanceCount() const { return frameInstances_.size(); }
173
174private:
183 {
184 SDL_GPUBuffer* vb = nullptr;
185 SDL_GPUBuffer* boneVb = nullptr;
186 SDL_GPUBuffer* ib = nullptr;
187 Uint32 indexCount = 0;
188 Uint32 vertexCount = 0;
189 };
190
191 struct SsboInfo
192 {
193 SDL_GPUBuffer* ssbo_{};
194 Uint32 capacityBytes_ = 0;
195 };
196
198 bool ensureSsbos(Uint32 paletteBytes, Uint32 instanceBytes);
199
205 static bool sphereInFrustum(const glm::vec3& center, float radius, const FrustumPlanes& planes);
206
207 bool createSkinningPipeline(SDL_GPUTextureFormat& colorTarget, const SDL_GPUShaderFormat& shaderFormat);
208
209 bool createSkinnedDepthPipeline(const SDL_GPUShaderFormat& shaderFormat);
210
212 bool createChamsPipeline();
213
216
217 // ─── Borrowed ────────────────────────────────────────────────────────────
218 SDL_GPUDevice* device_ = nullptr;
219
220 // ─── Owned: pipeline (graphics team to create) ───────────────────────────
225 SDL_GPUGraphicsPipeline* pipeline_ = nullptr;
226
229 SDL_GPUGraphicsPipeline* depthPipeline_ = nullptr;
230
233 SDL_GPUGraphicsPipeline* chamsPipeline_ = nullptr;
234
236 SDL_GPUGraphicsPipeline* killcamHighlightPipeline_ = nullptr;
237
239 SDL_GPUTextureFormat colorFormat_{};
240 SDL_GPUShaderFormat shaderFormat_{};
241
243 std::vector<Uint32> chamsIndices_;
244
246 std::vector<Uint32> killcamHighlightIndices_;
247
248 // ─── Owned: rig (set once via setRig) ────────────────────────────────────
249 bool rigInstalled_ = false;
250 int numJoints_ = 0;
251 std::vector<SkinnedMesh> skinnedMeshes_;
252 // Disabled by default: Game submits the skinned frame before drawFrame()
253 // updates the renderer camera, so culling here can use a stale frustum and
254 // make remote players flicker/fade at the edge of the previous frame's view.
255 bool frustumCullEnabled_ = false;
256
257 // Bind-pose bounding sphere (rig-local space), computed in `setRig` and used
258 // by `setFrame` to frustum-cull instances. The radius carries an animation
259 // margin so limbs swung out past the bind pose are not clipped early.
260 glm::vec3 rigBoundingCenter_{0.0f};
261 float rigBoundingRadius_ = 0.0f;
262
263 // ─── Owned: per-frame GPU resources (grow on demand) ─────────────────────
264 // SDL_GPUBuffer* palettesSsbo_ = nullptr; ///< STORAGE_READ, mat4[numInstances * numJoints].
265 // SDL_GPUBuffer* instancesSsbo_ = nullptr; ///< STORAGE_READ, SkinnedInstance[numInstances].
266 // Uint32 palettesCapacityBytes_ = 0;
267 // Uint32 instancesCapacityBytes_ = 0;
270
271 SDL_GPUTransferBuffer* paletteXfer_ = nullptr;
272 SDL_GPUTransferBuffer* instanceXfer_ = nullptr;
275
276 // ─── CPU staging (filled by setFrame, drained by uploadFrame) ────────────
277 //
278 // `frameInstances_` holds EVERY submitted character, partitioned so the
279 // on-screen ones come first:
280 // [0, visibleInstanceCount_) → inside the camera frustum
281 // [visibleInstanceCount_, size()) → off-screen, but still shadow casters
282 // The colour pass (`draw`) renders only the front slice; the shadow/depth
283 // pass (`drawDepth`) renders the whole buffer so off-screen players keep
284 // casting shadows. `framePalette_` is compacted in the same order.
285 std::vector<glm::mat4> framePalette_;
286 std::vector<SkinnedInstance> frameInstances_;
288 bool frameDirty_ = false;
289};
Camera class for the new renderer with combined view-projection matrix.
Shared data types exchanged between Game (producer) and NewRenderer (consumer).
std::vector< Uint32 > chamsIndices_
Indices into frameInstances_ of wallhack-revealed enemies.
Definition SkinnedRenderer.hpp:243
bool frameDirty_
Definition SkinnedRenderer.hpp:288
bool frustumCullEnabled_
Optional per-instance frustum cull in setFrame.
Definition SkinnedRenderer.hpp:255
SsboInfo instancesSsboInfo_
Definition SkinnedRenderer.hpp:269
SDL_GPUTransferBuffer * paletteXfer_
Definition SkinnedRenderer.hpp:271
int numJoints() const
Number of joints in the installed rig (0 if not installed).
Definition SkinnedRenderer.hpp:166
bool setRig(const std::vector< RigMeshSource > &meshes, int numJoints)
Install the shared character rig.
Definition SkinnedRenderer.cpp:97
SDL_GPUTransferBuffer * instanceXfer_
Definition SkinnedRenderer.hpp:272
void draw(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *cmd)
Issue the instanced draws for every visible skinned character.
Definition SkinnedRenderer.cpp:421
void shutdown()
Release all GPU resources owned by this subsystem.
Definition SkinnedRenderer.cpp:41
std::vector< glm::mat4 > framePalette_
Definition SkinnedRenderer.hpp:285
SkinnedRenderer & operator=(SkinnedRenderer &&)=delete
void init(SDL_GPUDevice *device, SDL_GPUTextureFormat &colorTarget, const SDL_GPUShaderFormat &shaderFormat)
Bind the SDL GPU device.
Definition SkinnedRenderer.cpp:22
std::vector< SkinnedInstance > frameInstances_
Definition SkinnedRenderer.hpp:286
bool ensureSsbos(Uint32 paletteBytes, Uint32 instanceBytes)
Grow palette/instance SSBOs (and their transfer buffers) to at least these byte sizes.
Definition SkinnedRenderer.cpp:339
void uploadFrame(SDL_GPUCommandBuffer *cmd, SDL_GPUCopyPass *copyPass)
Upload this frame's palette + instance buffers to the GPU.
Definition SkinnedRenderer.cpp:388
glm::vec3 rigBoundingCenter_
Definition SkinnedRenderer.hpp:260
bool rigInstalled_
Definition SkinnedRenderer.hpp:249
SDL_GPUTextureFormat colorFormat_
Cached formats (from init) needed to build the chams pipeline.
Definition SkinnedRenderer.hpp:239
SDL_GPUShaderFormat shaderFormat_
Definition SkinnedRenderer.hpp:240
void drawKillcamHighlight(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *cmd)
Draw the killcam killer full-body red highlight.
Definition SkinnedRenderer.cpp:537
Uint32 visibleInstanceCount_
Definition SkinnedRenderer.hpp:287
SDL_GPUGraphicsPipeline * depthPipeline_
Depth-only variant of the skinned pipeline, used to rasterise the rig into the shadow map so the play...
Definition SkinnedRenderer.hpp:229
SsboInfo palettesSsboInfo_
Definition SkinnedRenderer.hpp:268
SkinnedRenderer()=default
SkinnedRenderer & operator=(const SkinnedRenderer &)=delete
bool createChamsPipeline()
Create the wallhack chams pipeline.
Definition SkinnedRenderer.cpp:720
std::vector< SkinnedMesh > skinnedMeshes_
Definition SkinnedRenderer.hpp:251
SDL_GPUGraphicsPipeline * pipeline_
The skinned graphics pipeline.
Definition SkinnedRenderer.hpp:225
SkinnedRenderer(SkinnedRenderer &&)=delete
void drawChams(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *cmd)
Draw wallhack chams for flagged instances.
Definition SkinnedRenderer.cpp:500
std::vector< Uint32 > killcamHighlightIndices_
Indices into frameInstances_ of killcam-highlighted killers.
Definition SkinnedRenderer.hpp:246
bool createSkinningPipeline(SDL_GPUTextureFormat &colorTarget, const SDL_GPUShaderFormat &shaderFormat)
Definition SkinnedRenderer.cpp:610
~SkinnedRenderer()=default
size_t pendingInstanceCount() const
Number of instances pending render this frame (0 if no frame submitted).
Definition SkinnedRenderer.hpp:172
static bool sphereInFrustum(const glm::vec3 &center, float radius, const FrustumPlanes &planes)
Test whether a world-space bounding sphere intersects (or lies inside) the view frustum.
Definition SkinnedRenderer.cpp:236
Uint32 instanceXferCapacityBytes_
Definition SkinnedRenderer.hpp:274
float rigBoundingRadius_
Definition SkinnedRenderer.hpp:261
bool createSkinnedDepthPipeline(const SDL_GPUShaderFormat &shaderFormat)
Definition SkinnedRenderer.cpp:659
void setFrame(const std::vector< glm::mat4 > &palette, const std::vector< SkinnedInstance > &instances, const FrustumPlanes &frustum)
Push this frame's per-character bone palette + per-instance data and frustum-cull it down to the on-s...
Definition SkinnedRenderer.cpp:256
bool createKillcamHighlightPipeline()
Create the killcam full-body highlight pipeline.
Definition SkinnedRenderer.cpp:804
void drawDepth(SDL_GPURenderPass *renderPass, SDL_GPUCommandBuffer *cmd)
Issue the instanced draws for every visible skinned character into a depth-only shadow pass.
Definition SkinnedRenderer.cpp:571
SkinnedRenderer(const SkinnedRenderer &)=delete
SDL_GPUDevice * device_
Definition SkinnedRenderer.hpp:218
int numJoints_
Definition SkinnedRenderer.hpp:250
SDL_GPUGraphicsPipeline * chamsPipeline_
Wallhack chams pipeline: flat-red, GREATER depth test, no depth write — draws flagged players where t...
Definition SkinnedRenderer.hpp:233
void setFrustumCullEnabled(bool enabled)
Enable/disable per-instance frustum culling in setFrame.
Definition SkinnedRenderer.hpp:60
bool rigInstalled() const
True after a successful setRig.
Definition SkinnedRenderer.hpp:169
Uint32 paletteXferCapacityBytes_
Definition SkinnedRenderer.hpp:273
SDL_GPUGraphicsPipeline * killcamHighlightPipeline_
Killcam full-body red highlight pipeline.
Definition SkinnedRenderer.hpp:236
Definition Camera.hpp:11
One mesh of the installed skinned rig.
Definition SkinnedRenderer.hpp:183
SDL_GPUBuffer * vb
Definition SkinnedRenderer.hpp:184
SDL_GPUBuffer * ib
Definition SkinnedRenderer.hpp:186
Uint32 indexCount
Definition SkinnedRenderer.hpp:187
Uint32 vertexCount
Definition SkinnedRenderer.hpp:188
SDL_GPUBuffer * boneVb
Definition SkinnedRenderer.hpp:185
Definition SkinnedRenderer.hpp:192
Uint32 capacityBytes_
Definition SkinnedRenderer.hpp:194
SDL_GPUBuffer * ssbo_
Definition SkinnedRenderer.hpp:193