coastline 0.0.4
Loading...
Searching...
No Matches
gpu.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <functional>
5#include <future>
6#include <memory>
7#include <optional>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12#include <SDL3/SDL.h>
13
14namespace coastline {
15
22
29
31enum class PostAntialias {
34};
35
44
46enum class HdrOutput {
50};
51
54 bool n64Effects = false;
55 bool n64WindowReflections = false;
56 bool wideMenus = false;
57 bool autoWideMenus = false;
58 float fieldOfViewScale = 1.0f;
59 float drawDistanceScale = 1.0f;
63 false;
64 int width = 1280;
65 int height = 720;
66 int frameWidth = 0;
67 int frameHeight = 0;
69 float renderScale = 1.0f;
75 bool fixedFrame = false;
77 bool lowestFrame = false;
80 float aspectRatio = 0.0f;
83 bool gpuDebug = false;
85 bool minimized = false;
86 bool fullscreen = false;
89 bool exclusive = false;
90 int refreshRate = 0;
93 SDL_GPUPresentMode presentMode = SDL_GPU_PRESENTMODE_VSYNC;
96 bool fifo = false;
97 int antialias = 1;
100 int anisotropy = 0;
103 bool alphaToCoverage = true;
106};
107
113public:
116 std::function<void(SDL_GPUCommandBuffer *, SDL_GPUTexture *, Uint32, Uint32)>;
124 static std::unique_ptr<GpuWindow> Create(const char *title, const DisplayOptions &options);
125
129 GpuWindow(const GpuWindow &) = delete;
131 GpuWindow &operator=(const GpuWindow &) = delete;
132
137 bool WideMenus() const;
138
140 float FieldOfViewScale() const {
141 return m_options.fieldOfViewScale;
142 }
143
145 float DrawDistanceScale() const {
146 return m_options.drawDistanceScale;
147 }
148
150 bool N64Effects() const {
151 return m_options.n64Effects;
152 }
153
155 bool N64WindowReflections() const {
156 return m_options.n64WindowReflections;
157 }
158
166 bool ApplyLiveOptions(const DisplayOptions &options);
174
176 SDL_Window *Window() const {
177 return m_window.get();
178 }
179
181 SDL_GPUDevice *Device() const {
182 return m_device.get();
183 }
184
189 Uint64 GpuMemoryBytes() const {
190 return m_gpuMemoryBytes;
191 }
192
195
197 const std::string &GpuPreference() const {
198 return m_options.gpuPreference;
199 }
200
202 bool NvmlAllowed() const;
203
205 float AspectRatio() const {
206 return m_options.aspectRatio;
207 }
208
214 void SetFramerateUnlocked(bool unlocked);
215
217 bool FramerateUnlocked() const {
218 return m_options.unlockedFramerate;
219 }
220
223 return m_presentationWait;
224 }
225
227 bool Fullscreen() const {
228 return (SDL_GetWindowFlags(m_window.get()) & SDL_WINDOW_FULLSCREEN) != 0;
229 }
230
238 bool SetFullscreen(bool fullscreen);
239
241 SDL_GPUPresentMode PresentMode() const {
242 return m_options.presentMode;
243 }
244
252 void SetPresentMode(SDL_GPUPresentMode mode);
253
258 Uint64 RefreshPeriod() const;
259
267 bool PresentClear(SDL_FColor color, const std::filesystem::path *capture = nullptr);
268
277 bool Present(SDL_GPUCommandBuffer *commands,
278 const std::filesystem::path *capture = nullptr,
279 const OutputOverlay &overlay = {});
280
282 SDL_GPUTexture *FrameTexture() const {
283 return m_frame;
284 }
285
287 static constexpr SDL_GPUTextureFormat FrameFormat() {
288 return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
289 }
290
292 Uint32 FrameWidth() const {
293 return m_frameWidth;
294 }
295
297 Uint32 FrameHeight() const {
298 return m_frameHeight;
299 }
300
307
309 int Antialias() const {
310 return m_antialias;
311 }
312
315 return m_options.textureFilter;
316 }
317
319 bool AlphaToCoverage() const {
320 return m_options.alphaToCoverage;
321 }
322
324 int Anisotropy() const {
325 return m_options.anisotropy;
326 }
327
328private:
329 friend SDL_GPUTexture *CreateGpuTexture(SDL_GPUDevice *device,
330 const SDL_GPUTextureCreateInfo *info);
331 friend SDL_GPUBuffer *CreateGpuBuffer(SDL_GPUDevice *device,
332 const SDL_GPUBufferCreateInfo *info);
333 friend SDL_GPUTransferBuffer *
334 CreateGpuTransferBuffer(SDL_GPUDevice *device, const SDL_GPUTransferBufferCreateInfo *info);
335 friend void ReleaseGpuTexture(SDL_GPUDevice *device, SDL_GPUTexture *texture);
336 friend void ReleaseGpuBuffer(SDL_GPUDevice *device, SDL_GPUBuffer *buffer);
337 friend void ReleaseGpuTransferBuffer(SDL_GPUDevice *device, SDL_GPUTransferBuffer *buffer);
338
339 static GpuWindow *ForDevice(SDL_GPUDevice *device);
340 void AddGpuMemory(const void *object, Uint64 bytes);
341 void RemoveGpuMemory(const void *object);
342
344 struct WindowDeleter {
346 void operator()(SDL_Window *window) const {
347 SDL_DestroyWindow(window);
348 }
349 };
351 struct DeviceDeleter {
353 void operator()(SDL_GPUDevice *device) const {
354 SDL_DestroyGPUDevice(device);
355 }
356 };
357
358 GpuWindow() = default;
359 bool SaveFrame(SDL_GPUCommandBuffer *commands,
360 SDL_GPUTexture *source,
361 Uint32 width,
362 Uint32 height,
363 const std::filesystem::path &path);
364 bool CreateFrame(Uint32 width, Uint32 height);
365 bool CreatePostPasses();
366 SDL_GPUTexture *AntialiasFrame(SDL_GPUCommandBuffer *commands);
367 bool CreatePresentationPass();
368 bool CreateHdrPass();
369 SDL_GPUTexture *FilterFrame(SDL_GPUCommandBuffer *commands,
370 SDL_GPUTexture *source,
371 Uint32 width,
372 Uint32 height);
373
374 std::unordered_map<const void *, Uint64> m_gpuMemoryObjects;
375 Uint64 m_gpuMemoryBytes = 0;
376 unsigned m_texturesSincePresent = 0;
377 std::unique_ptr<SDL_Window, WindowDeleter> m_window;
378 std::unique_ptr<SDL_GPUDevice, DeviceDeleter> m_device;
379 SDL_GPUTexture *m_frame = nullptr;
380 // The frame after the anti-aliasing pass, its pipeline, and the sampler it reads the frame by.
381 SDL_GPUTexture *m_antialiased = nullptr;
382 SDL_GPUGraphicsPipeline *m_antialiasPipeline = nullptr;
383 SDL_GPUSampler *m_postSampler = nullptr;
384 SDL_GPUTexture *m_filtered = nullptr;
385 SDL_GPUGraphicsPipeline *m_presentationPipeline = nullptr;
386 SDL_GPUSampler *m_presentationSampler = nullptr;
387 SDL_GPUGraphicsPipeline *m_hdrPipeline = nullptr;
388 SDL_GPUSampler *m_hdrSampler = nullptr;
389 Uint32 m_filteredWidth = 0;
390 Uint32 m_filteredHeight = 0;
391 SDL_GPUTexture *m_composite = nullptr;
392 Uint32 m_compositeWidth = 0;
393 Uint32 m_compositeHeight = 0;
394 Uint32 m_frameWidth = 0;
395 Uint32 m_frameHeight = 0;
396 DisplayOptions m_options;
397 Uint64 m_presentationWait = 0;
398 int m_antialias = 1;
399 bool m_exclusive = false;
400 int m_refreshRate = 0;
401 bool m_pacedToRefresh = false;
402 bool m_hdrActive = false;
403 // Pending PNG saves of captured frames, waited for before the window is destroyed.
404 std::vector<std::future<void>> m_frameSaves;
405};
406
414SDL_GPUTexture *CreateGpuTexture(SDL_GPUDevice *device, const SDL_GPUTextureCreateInfo *info);
415
423SDL_GPUBuffer *CreateGpuBuffer(SDL_GPUDevice *device, const SDL_GPUBufferCreateInfo *info);
424
432SDL_GPUTransferBuffer *CreateGpuTransferBuffer(SDL_GPUDevice *device,
433 const SDL_GPUTransferBufferCreateInfo *info);
434
441void ReleaseGpuTexture(SDL_GPUDevice *device, SDL_GPUTexture *texture);
442
449void ReleaseGpuBuffer(SDL_GPUDevice *device, SDL_GPUBuffer *buffer);
450
457void ReleaseGpuTransferBuffer(SDL_GPUDevice *device, SDL_GPUTransferBuffer *buffer);
458
459} // namespace coastline
bool N64WindowReflections() const
Definition gpu.h:155
Uint64 RefreshPeriod() const
friend void ReleaseGpuBuffer(SDL_GPUDevice *device, SDL_GPUBuffer *buffer)
SDL_ReleaseGPUBuffer(), subtracting its bytes from GpuWindow::GpuMemoryBytes().
Uint64 GpuMemoryBytes() const
Definition gpu.h:189
bool AlphaToCoverage() const
Definition gpu.h:319
int Antialias() const
Definition gpu.h:309
bool WideMenus() const
SDL_GPUPresentMode PresentMode() const
Definition gpu.h:241
static constexpr SDL_GPUTextureFormat FrameFormat()
Definition gpu.h:287
friend void ReleaseGpuTexture(SDL_GPUDevice *device, SDL_GPUTexture *texture)
SDL_ReleaseGPUTexture(), subtracting its bytes from GpuWindow::GpuMemoryBytes().
Uint32 FrameHeight() const
Definition gpu.h:297
bool ApplyLiveOptions(const DisplayOptions &options)
Apply camera, particle, frame-size, post-processing, and pacing settings between frames.
std::string DeviceName() const
bool NvmlAllowed() const
bool PresentClear(SDL_FColor color, const std::filesystem::path *capture=nullptr)
Clear the frame texture to color, present it, and save it when capture is given.
float DrawDistanceScale() const
Definition gpu.h:145
float FieldOfViewScale() const
Definition gpu.h:140
TextureFilter Filter() const
Definition gpu.h:314
static std::unique_ptr< GpuWindow > Create(const char *title, const DisplayOptions &options)
Create the window, a GPU device for it, and the frame texture.
SDL_GPUDevice * Device() const
Definition gpu.h:181
bool N64Effects() const
Definition gpu.h:150
bool Fullscreen() const
Definition gpu.h:227
int Anisotropy() const
Definition gpu.h:324
bool ApplyRendererOptions(const DisplayOptions &options)
Update sampling and texture filtering, then apply live settings.
SDL_Window * Window() const
Definition gpu.h:176
GpuWindow & operator=(const GpuWindow &)=delete
Prevent assignment of the window and GPU device.
friend SDL_GPUTexture * CreateGpuTexture(SDL_GPUDevice *device, const SDL_GPUTextureCreateInfo *info)
SDL_CreateGPUTexture(), recording bytes of every level and sample in GpuWindow::GpuMemoryBytes().
std::function< void(SDL_GPUCommandBuffer *, SDL_GPUTexture *, Uint32, Uint32)> OutputOverlay
Draw directly over the output texture after scaling and screen filters.
Definition gpu.h:115
void SetFramerateUnlocked(bool unlocked)
Set uncapped presentation while retaining the configured present mode.
friend SDL_GPUTransferBuffer * CreateGpuTransferBuffer(SDL_GPUDevice *device, const SDL_GPUTransferBufferCreateInfo *info)
SDL_CreateGPUTransferBuffer(), recording its size in GpuWindow::GpuMemoryBytes().
bool SetFullscreen(bool fullscreen)
Cover the display, at the desktop mode or the exclusive mode of the options, or return to a window.
~GpuWindow()
Release presentation resources and the window after all device-dependent users.
friend SDL_GPUBuffer * CreateGpuBuffer(SDL_GPUDevice *device, const SDL_GPUBufferCreateInfo *info)
SDL_CreateGPUBuffer(), recording its size in GpuWindow::GpuMemoryBytes().
const std::string & GpuPreference() const
Definition gpu.h:197
float AspectRatio() const
Definition gpu.h:205
void SetPresentMode(SDL_GPUPresentMode mode)
Present the frames with mode, with the other mode that does not wait and then vertical sync in the pl...
SDL_GPUTexture * FrameTexture() const
Definition gpu.h:282
void UpdateFrameSize()
Give the frame the size of the window in pixels times the render scale, or the size of the options,...
bool Present(SDL_GPUCommandBuffer *commands, const std::filesystem::path *capture=nullptr, const OutputOverlay &overlay={})
Scale the frame texture to the window and submit commands, which drew the frame.
GpuWindow(const GpuWindow &)=delete
Prevent copying the window and GPU device.
friend void ReleaseGpuTransferBuffer(SDL_GPUDevice *device, SDL_GPUTransferBuffer *buffer)
SDL_ReleaseGPUTransferBuffer(), subtracting its bytes from GpuWindow::GpuMemoryBytes().
Uint64 PresentationWaitNanoseconds() const
Definition gpu.h:222
Uint32 FrameWidth() const
Definition gpu.h:292
bool FramerateUnlocked() const
Definition gpu.h:217
Definition assetimport.h:11
ScaleFilter
How the frame is scaled to the window.
Definition gpu.h:17
@ EdgeAdaptive
Definition gpu.h:20
@ Linear
Definition gpu.h:18
@ Nearest
Definition gpu.h:19
SDL_GPUTexture * CreateGpuTexture(SDL_GPUDevice *device, const SDL_GPUTextureCreateInfo *info)
SDL_CreateGPUTexture(), recording bytes of every level and sample in GpuWindow::GpuMemoryBytes().
SDL_GPUTransferBuffer * CreateGpuTransferBuffer(SDL_GPUDevice *device, const SDL_GPUTransferBufferCreateInfo *info)
SDL_CreateGPUTransferBuffer(), recording its size in GpuWindow::GpuMemoryBytes().
ScreenShader
Display simulation applied after the internal frame is scaled to the window.
Definition gpu.h:37
@ Pvm
Definition gpu.h:40
@ None
Definition gpu.h:38
@ ConsumerCrt
Definition gpu.h:39
@ PcCrt
Definition gpu.h:42
@ ApertureGrille
Definition gpu.h:41
void ReleaseGpuTransferBuffer(SDL_GPUDevice *device, SDL_GPUTransferBuffer *buffer)
SDL_ReleaseGPUTransferBuffer(), subtracting its bytes from GpuWindow::GpuMemoryBytes().
PostAntialias
The anti-aliasing of the finished frame, before it is scaled to the window.
Definition gpu.h:31
@ None
Definition gpu.h:32
@ Fxaa
Definition gpu.h:33
TextureFilter
How the textures the game filters are filtered.
Definition gpu.h:24
@ N64
Definition gpu.h:25
@ Modern
Definition gpu.h:26
void ReleaseGpuTexture(SDL_GPUDevice *device, SDL_GPUTexture *texture)
SDL_ReleaseGPUTexture(), subtracting its bytes from GpuWindow::GpuMemoryBytes().
HdrOutput
Whether HDR output follows the window, is requested, or is disabled.
Definition gpu.h:46
@ Auto
Definition gpu.h:47
@ On
Definition gpu.h:48
@ Off
Definition gpu.h:49
SDL_GPUBuffer * CreateGpuBuffer(SDL_GPUDevice *device, const SDL_GPUBufferCreateInfo *info)
SDL_CreateGPUBuffer(), recording its size in GpuWindow::GpuMemoryBytes().
void ReleaseGpuBuffer(SDL_GPUDevice *device, SDL_GPUBuffer *buffer)
SDL_ReleaseGPUBuffer(), subtracting its bytes from GpuWindow::GpuMemoryBytes().
The options of the window and the GPU device.
Definition gpu.h:53
bool wideMenus
Definition gpu.h:56
bool gpuDebug
Whether the GPU device checks its use, with the validation layers of Vulkan when they are installed.
Definition gpu.h:83
std::string gpuPreference
Definition gpu.h:60
ScaleFilter scaleFilter
Definition gpu.h:70
bool fixedFrame
Whether the frame stays at the width and the height, for captures that are the same on every machine.
Definition gpu.h:75
bool minimized
Whether the window starts minimized, for runs that save their frames unattended.
Definition gpu.h:85
float renderScale
The pixels of the frame for each pixel of the window, when the frame follows the window.
Definition gpu.h:69
int frameWidth
Definition gpu.h:66
bool autoWideMenus
Definition gpu.h:57
SDL_GPUPresentMode presentMode
How frames are presented, with the other mode that does not wait and then vertical sync taking the pl...
Definition gpu.h:93
bool fifo
Whether vertical sync of a Wayland window waits for the vertical blank as on other systems,...
Definition gpu.h:96
ScreenShader screenShader
Definition gpu.h:72
float aspectRatio
The width over the height the frame keeps in a window and in the full screen, with bars around it,...
Definition gpu.h:80
bool lowestFrame
Whether the frame expands from 320x240 to the selected aspect ratio.
Definition gpu.h:77
bool exclusive
Whether the window covers the display in a mode of the width, the height, and the refresh rate,...
Definition gpu.h:89
int anisotropy
The most samples of anisotropic filtering of the textures of models, which are then mipmapped,...
Definition gpu.h:100
std::optional< std::string > driver
Definition gpu.h:105
int height
Definition gpu.h:65
bool fullscreen
Definition gpu.h:86
bool n64Effects
Definition gpu.h:54
PostAntialias postAntialias
Definition gpu.h:71
bool n64WindowReflections
Definition gpu.h:55
float drawDistanceScale
Definition gpu.h:59
int refreshRate
Definition gpu.h:90
bool unlockedFramerate
Definition gpu.h:62
TextureFilter textureFilter
Definition gpu.h:101
bool alphaToCoverage
Whether the alpha of the texels a model cuts covers the samples of a multisampled pixel.
Definition gpu.h:103
HdrOutput hdrOutput
Definition gpu.h:61
float fieldOfViewScale
Definition gpu.h:58
int width
Definition gpu.h:64
int frameHeight
Definition gpu.h:67
int antialias
Definition gpu.h:97