TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
sdlgpu_window_context.h
Go to the documentation of this file.
1#pragma once
2
3// Inside the SDL GPU backend: an OS window's rendering. The window is claimed for the device,
4// which makes its swapchain. A frame draws into a texture of the window's own and blits it to
5// the swapchain's, so the last frame can be read back after it's shown; submitting the frame
6// presents it. Its copies (its primitives, and what the device has queued) come first, as SDL
7// wants them outside the render pass.
8// A layered window (a transparent one, on Windows) isn't claimed, as SDL GPU makes no swapchain
9// for a transparent window: its frame is cleared see-through, read back, and shown with
10// UpdateLayeredWindow. A render-to-image window has no OS window at all: its frames are only
11// read back. The window's scenes (RenderScene) are recorded after the copies, before its UI.
12
17
18#include <memory>
19#include <optional>
20#include <span>
21#include <vector>
22
23namespace tachyon::sdlgpu
24{
25 class SDLGPUWindowContext final : public IWindowContext
26 {
27 public:
31 static SDLGPUWindowContext* Create(SDLGPUGraphicsContext& graphics, SDL_Window* window, void* nativeWindow,
32 OSWindowHandle osHandle, ID id, int2 size, WindowFlags flags, OnGuiFunc onGui);
34
35 ID GetID() const override { return m_id; }
36 OSWindowHandle GetWindowHandle() const override { return m_osHandle; }
37 bool IsTransparent() const override;
38
41 void RenderWindowFrame(GraphicsContext* graphicsContext, IntRect rect, bool resizing) override;
42 void RecordWindowFrame(GraphicsContext* graphicsContext, IntRect rect) override;
45 std::optional<double> PresentWindowFrame() override;
46 bool ReadPixels(std::span<uint8_t> bgra) override;
47
48 private:
49 SDLGPUWindowContext(SDLGPUGraphicsContext& graphics, SDL_Window* window, void* nativeWindow, OSWindowHandle osHandle, ID id,
50 WindowFlags flags, OnGuiFunc onGui);
51 void Record(IntRect rect, bool resizing);
53 bool EnsureTargets(uint32_t width, uint32_t height);
55 bool StageArena();
56
57 SDLGPUGraphicsContext& m_graphics;
59 SDL_Window* m_window;
60 OSWindowHandle m_osHandle;
61 ID m_id;
62 WindowFlags m_flags;
63 OnGuiFunc m_onGui;
66 std::shared_ptr<bool> m_alive = std::make_shared<bool>(true);
67 void* m_nativeWindow;
68 bool m_toImage;
69 bool m_layered;
70 bool m_claimed = false;
71
74 std::vector<uint8_t> m_layeredPixels;
75 uint2 m_layeredSize{ 0, 0 };
76 bool m_layeredPending = false;
77
78 SDL_GPUTexture* m_drawTexture = nullptr;
79 SDL_GPUTexture* m_depthTexture = nullptr;
80 uint32_t m_width = 0, m_height = 0;
84 std::vector<SDL_GPUFence*> m_fences;
85 static constexpr size_t kFramesInFlight = 2;
87 void ReleaseSignaledFences();
88 bool m_drawn = false;
89 std::optional<double> m_acquireSeconds;
90
93 std::vector<std::byte> m_staging;
94 SDL_GPUBuffer* m_arena = nullptr;
95 SDL_GPUTransferBuffer* m_arenaUpload = nullptr;
96 uint32_t m_arenaSize = 0;
97 uint32_t m_arenaEntry = 0;
99 SDLGPUCommandList m_commands{ m_device, m_staging };
100
102 SDL_GPUTransferBuffer* m_download = nullptr;
103 uint32_t m_downloadSize = 0;
104 };
105}
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:53
A window's rendering state: made by GraphicsContext::CreateWindowContext for each OS window,...
Definition gui_window_backend.h:72
Definition sdlgpu_rhi.h:20
Definition sdlgpu_graphics_context.h:55
void RecordWindowFrame(GraphicsContext *graphicsContext, IntRect rect) override
Records and submits a frame without presenting it; PresentWindowFrame presents.
OSWindowHandle GetWindowHandle() const override
The window it was created for.
Definition sdlgpu_window_context.h:36
std::optional< double > PresentWindowFrame() override
The frame was presented when it was submitted: the seconds the swapchain's image took to come (the vs...
bool ReadPixels(std::span< uint8_t > bgra) override
The last rendered frame, BGRA8 with tightly packed rows; bgra holds width x height x 4 bytes of the w...
static SDLGPUWindowContext * Create(SDLGPUGraphicsContext &graphics, SDL_Window *window, void *nativeWindow, OSWindowHandle osHandle, ID id, int2 size, WindowFlags flags, OnGuiFunc onGui)
Null, with the reason logged, when the window can't be claimed.
bool IsTransparent() const override
Whether the window has WindowFlags::Transparent.
ID GetID() const override
The GUI window's ID; the backend passes it with the window's input.
Definition sdlgpu_window_context.h:35
void RenderWindowFrame(GraphicsContext *graphicsContext, IntRect rect, bool resizing) override
resizing: a live resize's redraw, which lays the window out again (its onGui) before it's drawn at th...
std::function< void()> OnGuiFunc
Definition gui_base_types.h:13
WindowFlags
How an OS window is made and behaves (IWindowBackend::CreateWindow); flags combine with |.
Definition gui_window_flags.h:14
void * OSWindowHandle
An OS window, opaque; what it points to is up to the window backend.
Definition gui_window_backend.h:37
Definition sdlgpu_device.h:37
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
Definition gui_base_types.h:406
Integer 2D vector.
Definition gui_base_types.h:18
Definition gui_base_types.h:74