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;
15
22{
23public:
31 bool init(SDL_GPUDevice* device,
32 SDL_GPUShaderFormat shaderFormat,
33 const SdfAtlas& sdfAtlas,
34 uint32_t screenW,
35 uint32_t screenH);
36
38 void quit();
39
41 void resize(uint32_t newW, uint32_t newH);
42
50 void render(std::span<const HudVertex> vertices, std::span<const std::array<float, 6>> clipRects);
51
53 [[nodiscard]] SDL_GPUTexture* getOutputTexture() const { return offscreenTarget_; }
54
56 [[nodiscard]] uint32_t width() const { return width_; }
57
59 [[nodiscard]] uint32_t height() const { return height_; }
60
61private:
62 SDL_GPUDevice* device_ = nullptr;
63 SDL_GPUShaderFormat shaderFormat_ = SDL_GPU_SHADERFORMAT_INVALID;
64
65 // Offscreen target
66 SDL_GPUTexture* offscreenTarget_ = nullptr;
67 uint32_t width_ = 0;
68 uint32_t height_ = 0;
69
70 // Pipeline
71 SDL_GPUGraphicsPipeline* pipeline_ = nullptr;
72
73 // Samplers (bound to fragment set 1)
74 SDL_GPUTexture* sdfAtlasTex_ = nullptr;
75 SDL_GPUSampler* sdfAtlasSamp_ = nullptr;
76 SDL_GPUTexture* iconAtlasTex_ = nullptr;
77 SDL_GPUSampler* iconAtlasSamp_ = nullptr;
78
79 // Dynamic vertex buffer (recreated if capacity exceeded)
80 SDL_GPUBuffer* vertexBuffer_ = nullptr;
81 SDL_GPUTransferBuffer* transferBuffer_ = nullptr;
82 uint32_t vertexCapacity_ = 0;
83
84 // Uniform buffer data
86 {
87 float screenW;
88 float screenH;
89 };
90
91 bool createOffscreenTarget(uint32_t w, uint32_t h);
92 bool createPipeline();
93 bool ensureVertexBuffer(uint32_t requiredVertices);
94
96 SDL_GPUShader*
97 loadShader(const char* name, SDL_GPUShaderStage stage, uint32_t samplerCount, uint32_t uniformBufferCount);
98};
Shared types for the HUD system.
Renders batched HUD geometry to an offscreen RGBA8 texture.
Definition HudRenderer.hpp:22
uint32_t vertexCapacity_
Current buffer capacity in vertices.
Definition HudRenderer.hpp:82
SDL_GPUSampler * iconAtlasSamp_
Owning.
Definition HudRenderer.hpp:77
uint32_t width() const
Current target width.
Definition HudRenderer.hpp:56
SDL_GPUSampler * sdfAtlasSamp_
Non-owning: from SdfAtlas.
Definition HudRenderer.hpp:75
SDL_GPUGraphicsPipeline * pipeline_
Definition HudRenderer.hpp:71
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:341
bool ensureVertexBuffer(uint32_t requiredVertices)
Definition HudRenderer.cpp:309
bool init(SDL_GPUDevice *device, SDL_GPUShaderFormat shaderFormat, const SdfAtlas &sdfAtlas, uint32_t screenW, uint32_t screenH)
Initialise GPU resources.
Definition HudRenderer.cpp:16
SDL_GPUTexture * getOutputTexture() const
The offscreen texture to be blitted by the renderer.
Definition HudRenderer.hpp:53
SDL_GPUTexture * offscreenTarget_
Definition HudRenderer.hpp:66
SDL_GPUTexture * sdfAtlasTex_
Non-owning: from SdfAtlas.
Definition HudRenderer.hpp:74
SDL_GPUTexture * iconAtlasTex_
Owning: 1x1 white fallback until real atlas.
Definition HudRenderer.hpp:76
SDL_GPUBuffer * vertexBuffer_
Definition HudRenderer.hpp:80
uint32_t width_
Definition HudRenderer.hpp:67
SDL_GPUDevice * device_
Definition HudRenderer.hpp:62
SDL_GPUTransferBuffer * transferBuffer_
Definition HudRenderer.hpp:81
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:120
void quit()
Release all GPU resources.
Definition HudRenderer.cpp:86
SDL_GPUShaderFormat shaderFormat_
Definition HudRenderer.hpp:63
uint32_t height_
Definition HudRenderer.hpp:68
bool createPipeline()
Definition HudRenderer.cpp:230
bool createOffscreenTarget(uint32_t w, uint32_t h)
Definition HudRenderer.cpp:210
void resize(uint32_t newW, uint32_t newH)
Recreate the offscreen target on window resize.
Definition HudRenderer.cpp:109
uint32_t height() const
Current target height.
Definition HudRenderer.hpp:59
Loads a TTF font, bakes an SDF glyph atlas, and uploads it to the GPU.
Definition SdfAtlas.hpp:18
Definition HudRenderer.hpp:86
float screenH
Definition HudRenderer.hpp:88
float screenW
Definition HudRenderer.hpp:87