TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
metal_window_context.h
Go to the documentation of this file.
1#pragma once
2
3// Inside the Metal backend (Objective-C++ only): an OS window's rendering. Each frame draws into
4// a texture of the frame slot's and is copied into the CAMetalLayer's drawable when it's
5// presented, as the other backends do, so the last frame can be read back after it's shown. A
6// render-to-image window has no OS window: its frames are only read back.
7
12
13#include <array>
14#include <memory>
15#include <optional>
16#include <span>
17
18@class CAMetalLayer;
19@class NSView;
20
21namespace tachyon::metal
22{
23 class MetalWindowContext final : public IWindowContext
24 {
25 public:
28 static MetalWindowContext* Create(MetalGraphicsContext& graphics, void* window, OSWindowHandle osHandle, ID id,
29 int2 size, WindowFlags flags, OnGuiFunc onGui);
31
32 ID GetID() const override { return m_id; }
33 OSWindowHandle GetWindowHandle() const override { return m_osHandle; }
34 bool IsTransparent() const override;
35
38 void RenderWindowFrame(GraphicsContext* graphicsContext, IntRect rect, bool resizing) override;
39 void RecordWindowFrame(GraphicsContext* graphicsContext, IntRect rect) override;
40 std::optional<double> PresentWindowFrame() override;
41 bool ReadPixels(std::span<uint8_t> bgra) override;
42
43 private:
44 static constexpr uint32_t kFrames = 2;
45
46 struct Frame
47 {
48 id<MTLTexture> drawTexture = nil;
49 FrameArena arena;
50 uint64_t fenceValue = 0;
51 };
52
54 void Record(IntRect rect, bool resizing);
55 bool MakeSizedResources(uint32_t width, uint32_t height);
56 void Draw(Frame& frame, id<MTLCommandBuffer> commandBuffer);
58 double ClearAlpha() const { return IsTransparent() || m_toImage ? 0.0 : 1.0; }
59
60 MetalGraphicsContext& m_graphics;
61 MetalGraphicsContext::Impl& m_device;
62 OSWindowHandle m_osHandle;
63 ID m_id;
64 WindowFlags m_flags;
65 OnGuiFunc m_onGui;
66 bool m_toImage = false;
67
68 NSView* m_view = nil;
69 CAMetalLayer* m_layer = nil;
70 id<MTLTexture> m_depth = nil;
71 std::array<Frame, kFrames> m_frames;
72 uint32_t m_frameIndex = 0;
73 uint32_t m_width = 0, m_height = 0;
74 bool m_presentPending = false;
75 std::optional<uint32_t> m_lastFrame;
76
77 id<MTLBuffer> m_readback = nil;
80 std::shared_ptr<bool> m_alive = std::make_shared<bool>(true);
81 };
82}
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 metal_graphics_context.h:45
Definition metal_window_context.h:24
std::optional< double > PresentWindowFrame() override
May wait for vsync; nothing to do for a render-to-image window.
void RecordWindowFrame(GraphicsContext *graphicsContext, IntRect rect) override
Records and submits a frame without presenting it; PresentWindowFrame presents.
bool IsTransparent() const override
Whether the window has WindowFlags::Transparent.
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...
ID GetID() const override
The GUI window's ID; the backend passes it with the window's input.
Definition metal_window_context.h:32
static MetalWindowContext * Create(MetalGraphicsContext &graphics, void *window, OSWindowHandle osHandle, ID id, int2 size, WindowFlags flags, OnGuiFunc onGui)
window is the NSWindow, null for a render-to-image window.
OSWindowHandle GetWindowHandle() const override
The window it was created for.
Definition metal_window_context.h:33
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 metal_device.h:40
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
A frame slot's transient data: one buffer, written from the start each time the slot comes round,...
Definition metal_rhi.h:18