TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
sdlgpu_device.h
Go to the documentation of this file.
1#pragma once
2
3// Inside the SDL GPU backend: the device, and what its graphics context and window contexts
4// share. SDL GPU has no buffer addresses: a DeviceAddress here is an entry in a table of
5// buffers (high 32 bits) and a byte offset into it (low 32 bits). It binds 16 images a draw:
6// each draw is cut where it would need more (ImageBatcher). Copies are recorded at the start of
7// a window frame, before its render pass, as SDL wants them outside it. What's released is
8// freed by SDL once the GPU is done with it.
9
15
16#include <SDL3/SDL_gpu.h>
17#include <SDL3/SDL_log.h>
18
19#include <array>
20#include <functional>
21#include <memory>
22#include <mutex>
23#include <optional>
24#include <span>
25#include <string>
26#include <unordered_map>
27#include <unordered_set>
28#include <vector>
29
30namespace tachyon
31{
32 class StyleManager;
33 class GradientManager;
34}
35
37{
39 inline constexpr SDL_GPUTextureFormat kDrawFormat = SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM;
40 inline constexpr SDL_GPUTextureFormat kDepthFormat = SDL_GPU_TEXTUREFORMAT_D32_FLOAT;
42 inline DeviceAddress MakeAddress(uint32_t entry, uint32_t offset) { return (DeviceAddress(entry) << 32) | offset; }
43 inline uint32_t AddressEntry(DeviceAddress address) { return uint32_t(address >> 32); }
44 inline uint32_t AddressOffset(DeviceAddress address) { return uint32_t(address); }
45
47 class SDLGPUImage final : public Image
48 {
49 public:
50 SDL_GPUTexture* texture = nullptr;
54 std::optional<ImageFormat> sourceFormat;
55 };
56
58 class SDLGPUImageSet final : public ImageSet
59 {
60 public:
61 void SetCapacity(uint32_t capacity) { m_capacity = capacity; }
62 void Clear() override;
63 ImageIndex Add(Image* image) override;
64 void Remove(ImageIndex index) override;
68 void SetImage(ImageIndex index, SDLGPUImage* image);
70 const SDLGPUImage* At(uint32_t slot) const { return slot < m_images.size() ? m_images[slot] : nullptr; }
71
72 private:
73 ImageIndex Allocate();
74
75 uint32_t m_capacity = 4096;
76 std::vector<SDLGPUImage*> m_images;
77 std::vector<uint32_t> m_free;
78 };
79
82 {
83 float worldColumns[16];
85 uint32_t firstVertex;
86 uint32_t pad[2];
87 uint32_t primitiveOffsets[8];
88 uint32_t imageSlots[16];
89 };
90 static_assert(sizeof(DrawUniforms) == 176);
91
93 {
95 std::string driverAsked;
96 bool lowPower = false;
98 uint32_t imageSlots = 4096;
99
100 SDL_GPUDevice* device = nullptr;
101 SDL_GPUDevice* externalDevice = nullptr;
102 SDL_GPUShaderFormat shaderFormat = SDL_GPU_SHADERFORMAT_INVALID;
103 std::string driver;
104 std::string deviceName;
105
107 SDL_LogOutputFunction previousLog = nullptr;
108 void* previousLogData = nullptr;
109 bool logHooked = false;
110
111 // ---- Buffers a DeviceAddress names --------------------------------------------
112 std::vector<SDL_GPUBuffer*> buffers { nullptr };
113 std::vector<uint32_t> freeBuffers;
114 uint32_t AddBuffer(SDL_GPUBuffer* buffer);
115 void RemoveBuffer(uint32_t entry);
116 SDL_GPUBuffer* Buffer(uint32_t entry) const { return entry < buffers.size() ? buffers[entry] : nullptr; }
117
120 SDL_GPUBuffer* CreateBuffer(SDL_GPUBufferUsageFlags usage, const void* data, uint32_t bytes);
122 void ReleaseBuffer(SDL_GPUBuffer*& buffer);
123
124 // ---- Copies made at the next window frame's start -----------------------------
128 {
129 SDLGPUImage* image = nullptr;
130 SDL_GPUTransferBuffer* staging = nullptr;
131 uint32_t firstRow = 0;
132 uint32_t rows = 0;
133 };
135 {
136 SDL_GPUBuffer* buffer = nullptr;
137 SDL_GPUTransferBuffer* staging = nullptr;
138 uint32_t bytes = 0;
139 };
140 std::mutex copyMutex;
141 std::vector<TextureCopy> textureCopies;
142 std::vector<BufferCopy> bufferCopies;
145 std::vector<SDLGPUImage*> releasedImages;
148 {
149 std::vector<TextureCopy> textures;
150 std::vector<BufferCopy> buffers;
151 std::vector<SDLGPUImage*> released;
154 void RecordCopies(SDL_GPUCopyPass* pass);
155
156 // ---- Resident buffers (RHI::UploadResidentBuffers) ----------------------------
157 struct Resident
158 {
159 SDL_GPUBuffer* buffer = nullptr;
160 uint32_t entry = 0;
161 bool ready = false;
162 };
163 std::unordered_map<RHI::ResidentBufferId, Resident> residents;
166 std::vector<RHI::ResidentBufferId> pendingResidents;
168 RHI::ResidentBufferId CreateResident(std::span<const RHI::UploadRequest> requests, std::span<DeviceAddress> addresses);
171
172 // ---- Images -------------------------------------------------------------------
174 std::unordered_set<SDLGPUImage*> liveImages;
176 std::array<SDL_GPUSampler*, 3> samplers {};
177 SDL_GPUTexture* blankImage = nullptr;
178 SDL_GPUSampler* SamplerFor(tachyon::Sampler sampler) const;
179
180 // ---- What every draw uses -----------------------------------------------------
181 std::array<SDL_GPUGraphicsPipeline*, kPrimitiveKindCount> pipelineObjects {};
183 SDL_GPUBuffer* blankBuffer = nullptr;
184 SDL_GPUBuffer* quadIndices = nullptr;
185 uint32_t quadCapacity = 0;
187 bool EnsureQuadIndices(uint32_t quads);
188
189 // ---- The frame's styles (PrepareFrame) ------------------------------------------
190 SDL_GPUBuffer* boxStyles = nullptr;
191 SDL_GPUBuffer* colorStyles = nullptr;
192 std::vector<ImageIndex> gradientPages;
194 float elapsedTime = 0.0f;
197
198 // ---- Scenes the app draws (RenderScene) ----------------------------------------
199 static constexpr SDL_GPUTextureFormat kSceneColorFormat = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
200 static constexpr uint64_t kSceneKeepFrames = 4;
201
204 struct Scene
205 {
206 SDLGPUImage* color = nullptr;
207 SDLGPUImage* depth = nullptr;
210 uint64_t lastFrame = 0;
211 };
213 {
217 std::function<void(const ScenePayload& scene)> record;
218 };
219 std::unordered_map<ID, Scene> scenes;
220 std::vector<SceneRequest> sceneRequests;
221 uint64_t sceneFrame = 0;
222
225 void RecordScenes(SDL_GPUCommandBuffer* cmd, ID window);
228 SDLGPUImage* CreateTargetImage(uint2 size, SDL_GPUTextureFormat format, SDL_GPUTextureUsageFlags usage, Sampler sampler);
229
230 // ---- A host's textures (RegisterExternalImage, SetWindowImageTarget) -----------
232 std::unordered_map<uint32_t, std::unique_ptr<SDLGPUImage>> externalImages;
234 {
235 SDL_GPUTexture* texture = nullptr;
237 };
238 std::unordered_map<ID, ImageTarget> imageTargets;
239 };
240}
The atlas that the colors of color styles are baked into: pages of 2D textures, one row per gradient,...
Definition gui_gradient_manager.h:65
Definition gui_image_batcher.h:24
The images the shaders can sample, each at an ImageIndex. Implemented by the backend.
Definition gui_image.h:106
An image made by GraphicsContext::CreateImage; the backend defines its contents.
Definition gui_image.h:102
What GraphicsContext::RenderScene's record gets: the backend's recording state, as the struct of the ...
Definition gui_graphics_api.h:41
Bakes serializable Styles into deduplicated runtime layout/visual styles; a StyleRef holds indices in...
Definition gui_style_manager.h:39
SDL_Window *(*)(OSWindowHandle window) WindowResolver
The SDL window of a window backend's window (OSWindowHandle): SDLWindowBackend sets it.
Definition sdlgpu_graphics_context.h:58
The images the shaders sample, by ImageIndex (1-based; the shaders' slot is one less).
Definition sdlgpu_device.h:59
void Remove(ImageIndex index) override
Frees the index for reuse; the image itself stays.
void Clear() override
Removes every image, freeing all indices.
void SetImage(ImageIndex index, SDLGPUImage *image)
Points the slot at another image.
ImageIndex Add(Image *image) override
Adds an image (not owned) and returns its index.
ImageIndex ReserveIndex()
A slot with no image yet, for one that comes later (a text atlas page).
const SDLGPUImage * At(uint32_t slot) const
The image in a 0-based slot, or null.
Definition sdlgpu_device.h:70
void SetCapacity(uint32_t capacity)
Definition sdlgpu_device.h:61
An image (GraphicsContext::CreateImage): a texture sampled by the shaders.
Definition sdlgpu_device.h:48
SDL_GPUTexture * texture
Definition sdlgpu_device.h:50
Sampler sampler
Definition sdlgpu_device.h:51
std::optional< ImageFormat > sourceFormat
Made by CreateImage from pixels of this format: UpdateImageRows can write into it.
Definition sdlgpu_device.h:54
uint2 size
Definition sdlgpu_device.h:52
uint64_t ResidentBufferId
GPU storage kept across frames (UploadResidentBuffers); 0 is none.
Definition gui_rhi.h:36
Sampler
How an image is filtered and addressed when sampled.
Definition gui_image.h:89
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:99
std::array< RHI::GpuPipeline, kPrimitiveKindCount > PipelineSet
Definition gui_primitive_renderer.h:31
uint64_t DeviceAddress
Shader-visible buffer address.
Definition gui_render_primitives.h:206
ResidentState
Where a resident buffer is.
Definition gui_rhi.h:40
@ NearestRepeat
Definition gui_image.h:92
@ Invalid
Definition gui_image.h:99
Definition sdlgpu_device.h:37
uint32_t AddressEntry(DeviceAddress address)
Definition sdlgpu_device.h:43
constexpr SDL_GPUTextureFormat kDrawFormat
The format windows draw in, whatever their swapchain's: the blit to it converts.
Definition sdlgpu_device.h:39
DeviceAddress MakeAddress(uint32_t entry, uint32_t offset)
What a DeviceAddress holds: the buffer table's entry, and the byte offset into it.
Definition sdlgpu_device.h:42
uint32_t AddressOffset(DeviceAddress address)
Definition sdlgpu_device.h:44
constexpr SDL_GPUTextureFormat kDepthFormat
Definition sdlgpu_device.h:40
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
Definition gui_base_types.h:394
Where SDL GPU's messages go.
Definition sdlgpu_graphics_context.h:49
The uniforms both stages of every draw get, as all_common.slang declares them.
Definition sdlgpu_device.h:82
uint32_t pad[2]
Definition sdlgpu_device.h:86
uint32_t imageSlots[16]
the image slot each bound image is; ~0 for none
Definition sdlgpu_device.h:88
float elapsedTime
Definition sdlgpu_device.h:84
uint32_t firstVertex
Definition sdlgpu_device.h:85
uint32_t primitiveOffsets[8]
bytes into each slot's buffer
Definition sdlgpu_device.h:87
float worldColumns[16]
Definition sdlgpu_device.h:83
SDL_GPUBuffer * buffer
Definition sdlgpu_device.h:136
uint32_t bytes
Definition sdlgpu_device.h:138
SDL_GPUTransferBuffer * staging
Definition sdlgpu_device.h:137
RecordCopies' working lists, kept between frames for their storage.
Definition sdlgpu_device.h:148
std::vector< BufferCopy > buffers
Definition sdlgpu_device.h:150
std::vector< SDLGPUImage * > released
Definition sdlgpu_device.h:151
std::vector< TextureCopy > textures
Definition sdlgpu_device.h:149
SDL_GPUTexture * texture
Definition sdlgpu_device.h:235
uint32_t entry
Definition sdlgpu_device.h:160
SDL_GPUBuffer * buffer
Definition sdlgpu_device.h:159
std::function< void(const ScenePayload &scene)> record
Definition sdlgpu_device.h:217
What RenderScene keeps for a key: its color texture (in the image set) and depth, both in liveImages.
Definition sdlgpu_device.h:205
uint64_t lastFrame
the frame it was last asked for
Definition sdlgpu_device.h:210
uint2 extent
Definition sdlgpu_device.h:209
ImageIndex index
Definition sdlgpu_device.h:208
SDLGPUImage * color
Definition sdlgpu_device.h:206
SDLGPUImage * depth
Definition sdlgpu_device.h:207
A whole image's pixels, or rows rows from firstRow written into one already up (UpdateImageRows); 0 r...
Definition sdlgpu_device.h:128
SDLGPUImage * image
Definition sdlgpu_device.h:129
SDL_GPUTransferBuffer * staging
Definition sdlgpu_device.h:130
uint32_t firstRow
Definition sdlgpu_device.h:131
uint32_t rows
Definition sdlgpu_device.h:132
SDL_GPUDevice * device
Definition sdlgpu_device.h:100
SDL_GPUTexture * blankImage
bound where a draw has fewer images
Definition sdlgpu_device.h:177
std::vector< uint32_t > freeBuffers
Definition sdlgpu_device.h:113
SDL_GPUShaderFormat shaderFormat
the one the GUI's shaders are taken in
Definition sdlgpu_device.h:102
DebugConfig debug
Definition sdlgpu_device.h:94
float elapsedTime
Definition sdlgpu_device.h:194
void ReleaseBuffer(SDL_GPUBuffer *&buffer)
Lets go of a buffer: its pending copy, if any, isn't made.
bool EnsureQuadIndices(uint32_t quads)
Grows the quads' index buffer to hold quads (copied with the window frame's copies).
PipelineSet pipelines
pipeline: the object; layout: the kind
Definition sdlgpu_device.h:182
std::vector< ImageIndex > gradientPages
each gradient page's index in the image set
Definition sdlgpu_device.h:192
RHI::ResidentState ResidentState(RHI::ResidentBufferId id) const
RHI::ResidentBufferId CreateResident(std::span< const RHI::UploadRequest > requests, std::span< DeviceAddress > addresses)
SDL_GPUBuffer * CreateBuffer(SDL_GPUBufferUsageFlags usage, const void *data, uint32_t bytes)
A GPU buffer of usage and a transfer buffer holding data, copied at the next window frame's start.
StyleManager * lastStyles
PrepareFrame's, for a live resize's redraw.
Definition sdlgpu_device.h:195
std::vector< TextureCopy > textureCopies
Definition sdlgpu_device.h:141
SDL_GPUBuffer * blankBuffer
bound to the storage slots a draw doesn't use
Definition sdlgpu_device.h:183
std::vector< SceneRequest > sceneRequests
Definition sdlgpu_device.h:220
struct tachyon::sdlgpu::SDLGPUGraphicsContext::Impl::CopyScratch copyScratch
std::array< SDL_GPUGraphicsPipeline *, kPrimitiveKindCount > pipelineObjects
Definition sdlgpu_device.h:181
void * previousLogData
Definition sdlgpu_device.h:108
GradientManager * lastGradients
Definition sdlgpu_device.h:196
SDL_GPUSampler * SamplerFor(tachyon::Sampler sampler) const
std::mutex copyMutex
images are made and let go on any thread
Definition sdlgpu_device.h:140
std::unordered_map< ID, Scene > scenes
Definition sdlgpu_device.h:219
std::vector< SDL_GPUBuffer * > buffers
entry 0 is none
Definition sdlgpu_device.h:112
static constexpr SDL_GPUTextureFormat kSceneColorFormat
Definition sdlgpu_device.h:199
bool lowPower
Definition sdlgpu_device.h:96
std::string driverAsked
SetDriver; empty, SDL's choice.
Definition sdlgpu_device.h:95
std::unordered_map< ID, ImageTarget > imageTargets
Definition sdlgpu_device.h:238
SDL_GPUDevice * externalDevice
the host's (SetExternalDevice): not destroyed at Cleanup
Definition sdlgpu_device.h:101
void RecordScenes(SDL_GPUCommandBuffer *cmd, ID window)
Clears and records the scenes asked for in window this frame, before its UI's render pass and outside...
void ReleaseResident(RHI::ResidentBufferId id)
ImageBatcher batcher
Definition sdlgpu_device.h:193
SDLGPUImage * CreateTargetImage(uint2 size, SDL_GPUTextureFormat format, SDL_GPUTextureUsageFlags usage, Sampler sampler)
A texture the GPU draws into (a scene's), in liveImages so DestroyImageAtEnd frees it; null on failur...
std::vector< BufferCopy > bufferCopies
Definition sdlgpu_device.h:142
SDL_GPUBuffer * Buffer(uint32_t entry) const
Definition sdlgpu_device.h:116
SDL_GPUBuffer * colorStyles
Definition sdlgpu_device.h:191
WindowResolver resolveWindow
Definition sdlgpu_device.h:97
uint32_t AddBuffer(SDL_GPUBuffer *buffer)
uint64_t sceneFrame
Definition sdlgpu_device.h:221
std::array< SDL_GPUSampler *, 3 > samplers
nearest repeat, linear repeat, linear clamp
Definition sdlgpu_device.h:176
std::vector< SDLGPUImage * > releasedImages
Images let go of (DestroyImageAtEnd, on any thread), freed by RecordCopies on the frame's thread afte...
Definition sdlgpu_device.h:145
SDL_GPUBuffer * boxStyles
Definition sdlgpu_device.h:190
std::unordered_map< uint32_t, std::unique_ptr< SDLGPUImage > > externalImages
By image slot index: the host's texture, which the GUI never releases.
Definition sdlgpu_device.h:232
SDLGPUImageSet imageSet
Definition sdlgpu_device.h:175
uint32_t imageSlots
Definition sdlgpu_device.h:98
RHI::ResidentBufferId nextResident
Definition sdlgpu_device.h:167
std::unordered_map< RHI::ResidentBufferId, Resident > residents
Definition sdlgpu_device.h:163
SDL_GPUBuffer * quadIndices
p * 4 + { 0, 1, 3, 0, 3, 2 } for each quad p
Definition sdlgpu_device.h:184
std::string deviceName
Definition sdlgpu_device.h:104
SDL_LogOutputFunction previousLog
SDL's log output while the debug callback takes SDL GPU's messages.
Definition sdlgpu_device.h:107
static constexpr uint64_t kSceneKeepFrames
a scene not asked for this long goes
Definition sdlgpu_device.h:200
std::unordered_set< SDLGPUImage * > liveImages
Every image made and not let go for a frame: Cleanup frees what's left.
Definition sdlgpu_device.h:174
std::vector< RHI::ResidentBufferId > pendingResidents
Made and not copied yet, oldest first; one let go of meanwhile is no longer in residents and is passe...
Definition sdlgpu_device.h:166
std::string driver
the one in use
Definition sdlgpu_device.h:103
uint32_t quadCapacity
Definition sdlgpu_device.h:185
bool logHooked
Definition sdlgpu_device.h:109
void RecordCopies(SDL_GPUCopyPass *pass)
Records the queued copies; resident buffers they make are Ready after.
Definition gui_base_types.h:74