group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
HudRenderer.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include "HudTypes.hpp"
8
9#include <SDL3/SDL.h>
10
11#include <array>
12#include <span>
13
14class SdfAtlas;
15class HudSvgAtlas;
16
23{
24public:
32 bool init(SDL_GPUDevice* device,
33 SDL_GPUShaderFormat shaderFormat,
34 const SdfAtlas& sdfAtlas,
35 const HudSvgAtlas* svgAtlas,
36 uint32_t screenW,
37 uint32_t screenH);
38
40 void quit();
41
43 void resize(uint32_t newW, uint32_t newH);
44
52 void render(std::span<const HudVertex> vertices, std::span<const std::array<float, 6>> clipRects);
53
55 [[nodiscard]] SDL_GPUTexture* getOutputTexture() const { return offscreenTarget_; }
56
58 [[nodiscard]] uint32_t width() const { return width_; }
59
61 [[nodiscard]] uint32_t height() const { return height_; }
62
63private:
64 SDL_GPUDevice* device_ = nullptr;
65 SDL_GPUShaderFormat shaderFormat_ = SDL_GPU_SHADERFORMAT_INVALID;
66
67 // Offscreen targets — render into the multisample target, auto-resolve
68 // into the 1× sampleable target at end-of-pass. The 4× MSAA gives the
69 // entire HUD pass free geometry-edge AA on every diagonal icon stroke,
70 // chevron, ring, and rotated rect, which were previously hard-aliased.
71 SDL_GPUTexture* msaaTarget_ = nullptr;
72 SDL_GPUTexture* offscreenTarget_ = nullptr;
73 uint32_t width_ = 0;
74 uint32_t height_ = 0;
75 static constexpr SDL_GPUSampleCount k_sampleCount = SDL_GPU_SAMPLECOUNT_4;
76
77 // Pipeline
78 SDL_GPUGraphicsPipeline* pipeline_ = nullptr;
79 SDL_GPUGraphicsPipeline* erasePipeline_ = nullptr;
80
81 // Samplers (bound to fragment set 1)
82 SDL_GPUTexture* sdfAtlasTex_ = nullptr;
83 SDL_GPUSampler* sdfAtlasSamp_ = nullptr;
84 const HudSvgAtlas* svgAtlas_ = nullptr;
85 SDL_GPUTexture* iconAtlasTex_ = nullptr;
86 SDL_GPUSampler* iconAtlasSamp_ = nullptr;
87
88 // Dynamic vertex buffer (recreated if capacity exceeded)
89 SDL_GPUBuffer* vertexBuffer_ = nullptr;
90 SDL_GPUTransferBuffer* transferBuffer_ = nullptr;
91 uint32_t vertexCapacity_ = 0;
92
93 // Uniform buffer data
95 {
96 float screenW;
97 float screenH;
98 };
99
100 bool createOffscreenTarget(uint32_t w, uint32_t h);
101 bool createPipeline();
102 bool ensureVertexBuffer(uint32_t requiredVertices);
103
105 SDL_GPUShader*
106 loadShader(const char* name, SDL_GPUShaderStage stage, uint32_t samplerCount, uint32_t uniformBufferCount);
107};
Shared types for the HUD system.
Renders batched HUD geometry to an offscreen RGBA8 texture.
Definition HudRenderer.hpp:23
uint32_t vertexCapacity_
Current buffer capacity in vertices.
Definition HudRenderer.hpp:91
SDL_GPUSampler * iconAtlasSamp_
Owning.
Definition HudRenderer.hpp:86
uint32_t width() const
Current target width.
Definition HudRenderer.hpp:58
SDL_GPUSampler * sdfAtlasSamp_
Non-owning: from SdfAtlas.
Definition HudRenderer.hpp:83
bool init(SDL_GPUDevice *device, SDL_GPUShaderFormat shaderFormat, const SdfAtlas &sdfAtlas, const HudSvgAtlas *svgAtlas, uint32_t screenW, uint32_t screenH)
Initialise GPU resources.
Definition HudRenderer.cpp:17
SDL_GPUGraphicsPipeline * pipeline_
Definition HudRenderer.hpp:78
SDL_GPUShader * loadShader(const char *name, SDL_GPUShaderStage stage, uint32_t samplerCount, uint32_t uniformBufferCount)
Load a compiled shader from the shaders/ directory.
Definition HudRenderer.cpp:436
bool ensureVertexBuffer(uint32_t requiredVertices)
Definition HudRenderer.cpp:404
SDL_GPUTexture * getOutputTexture() const
The offscreen texture to be blitted by the renderer.
Definition HudRenderer.hpp:55
SDL_GPUTexture * offscreenTarget_
1×, COLOR_TARGET | SAMPLER (resolve dst).
Definition HudRenderer.hpp:72
SDL_GPUTexture * sdfAtlasTex_
Non-owning: from SdfAtlas.
Definition HudRenderer.hpp:82
SDL_GPUTexture * iconAtlasTex_
Owning: 1x1 white fallback until real atlas.
Definition HudRenderer.hpp:85
SDL_GPUBuffer * vertexBuffer_
Definition HudRenderer.hpp:89
uint32_t width_
Definition HudRenderer.hpp:73
SDL_GPUDevice * device_
Definition HudRenderer.hpp:64
static constexpr SDL_GPUSampleCount k_sampleCount
Definition HudRenderer.hpp:75
SDL_GPUTransferBuffer * transferBuffer_
Definition HudRenderer.hpp:90
void render(std::span< const HudVertex > vertices, std::span< const std::array< float, 6 > > clipRects)
Upload vertex data, execute the HUD render pass, and submit.
Definition HudRenderer.cpp:130
void quit()
Release all GPU resources.
Definition HudRenderer.cpp:89
SDL_GPUShaderFormat shaderFormat_
Definition HudRenderer.hpp:65
uint32_t height_
Definition HudRenderer.hpp:74
bool createPipeline()
Definition HudRenderer.cpp:305
bool createOffscreenTarget(uint32_t w, uint32_t h)
Definition HudRenderer.cpp:258
const HudSvgAtlas * svgAtlas_
Non-owning: LunaSVG HUD sprite atlas.
Definition HudRenderer.hpp:84
SDL_GPUTexture * msaaTarget_
4× multisampled, COLOR_TARGET only.
Definition HudRenderer.hpp:71
SDL_GPUGraphicsPipeline * erasePipeline_
Definition HudRenderer.hpp:79
void resize(uint32_t newW, uint32_t newH)
Recreate the offscreen target on window resize.
Definition HudRenderer.cpp:117
uint32_t height() const
Current target height.
Definition HudRenderer.hpp:61
Definition HudSvgAtlas.hpp:26
Loads a TTF font, bakes an SDF glyph atlas, and uploads it to the GPU.
Definition SdfAtlas.hpp:18
Definition HudRenderer.hpp:95
float screenH
Definition HudRenderer.hpp:97
float screenW
Definition HudRenderer.hpp:96