TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
metal_graphics_context.h
Go to the documentation of this file.
1#pragma once
2
3// The Metal graphics backend, on macOS (TACHYON_METAL): the UI in OS and render-to-image
4// windows, and the app's scenes (RenderScene). This header is plain C++: the Metal objects it
5// names are `void*` here, an `id<MTL...>` to Objective-C++ (bridge them with
6// `(__bridge id<MTLDevice>)pointer`).
7
9
10#include <memory>
11#include <string>
12
13namespace tachyon::metal
14{
17
24 {
26
27 void* commandBuffer = nullptr;
28 void* encoder = nullptr;
29 void* colorTexture = nullptr;
30 void* depthTexture = nullptr;
31 uint32_t colorFormat = 0;
32 uint32_t depthFormat = 0;
34 };
35
39 {
40 void* device = nullptr;
41 void* queue = nullptr;
42 };
43
45 {
46 public:
49
52 static bool CheckDeviceSupport(void* device, std::string* problems = nullptr);
55 void SetExternalDevice(const ExternalDevice& external);
56
57 GraphicsAPI GetAPI() const override { return GraphicsAPI::Metal; }
58 std::string_view GetDeviceName() const override;
59 bool DeviceLost() const override;
60 void SetGpuPreference(std::string_view preference) override;
63 void SetImageSlotCount(uint32_t slots) override;
64 uint32_t GetImageSlotCount() const override;
65
69 Result<void> Initialize(IWindowBackend* windowBackend) override;
70 void Cleanup() override;
72 void BeginFrame() override;
73
78 Image* CreateImage(void* data, uint2 size, ImageFormat format, Sampler sampler, bool mipmapped = false) override;
79 bool UpdateImageRows(Image* image, uint32_t firstRow, uint32_t rowCount, const void* data) override;
80 void DestroyImageAtEnd(Image* img, DeviceScope scope, const std::source_location& loc = std::source_location::current()) override;
81
83
86 IWindowContext* CreateWindowContext(void* nativeWindowHandle, OSWindowHandle osHandle, ID id, int2 size,
87 WindowFlags windowFlags, OnGuiFunc onGui) override;
88 void DestroyWindowContext(IWindowContext* context) override;
89
90 ImageSet* GetImageSet() const override;
91 ImageIndex RegisterImage(Image* image) override;
92 void UnregisterImage(ImageIndex index) override;
93
95 void PrepareFrame(StyleManager& styleManager, GradientManager& gradientManager, float elapsedTimeSecs) override;
98
102 ImageIndex RenderScene(ID key, ID window, uint2 size, color4f clear,
103 std::function<void(const ScenePayload& scene)> record) override;
104
105 // ---- A host's own textures, on its device (SetExternalDevice) -----------------------
106
111 ImageIndex RegisterExternalImage(void* texture, Sampler sampler);
114 void UpdateExternalImage(ImageIndex index, void* texture, Sampler sampler);
115
117 void WaitIdle();
118
119 struct Impl;
121 Impl& Internal() { return *m_impl; }
122
123 private:
124 std::unique_ptr<Impl> m_impl;
125 };
126
127}
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
Makes and manages the OS windows and delivers their input; GUIApplication::SetWindowBackend sets one,...
Definition gui_window_backend.h:112
A window's rendering state: made by GraphicsContext::CreateWindowContext for each OS window,...
Definition gui_window_backend.h:72
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
A value, or the Error saying why there is none.
Definition gui_result.h:36
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
Definition gui_text_atlas_manager.h:17
ImageSet * GetImageSet() const override
The images the shaders sample, by ImageIndex.
uint32_t GetImageSlotCount() const override
How many images can be registered at once; 0 when the backend doesn't say.
void SetExternalDevice(const ExternalDevice &external)
Render on the host's device and queue instead of making them.
std::string_view GetDeviceName() const override
The GPU it renders with, as the driver names it ("NVIDIA GeForce RTX 4070"); empty before Initialize ...
Result< void > Initialize(IWindowBackend *windowBackend) override
Picks a GPU of the Metal 3 family (the system's default unless the app or TACHYON_GPU asks for anothe...
IWindowContext * CreateWindowContext(void *nativeWindowHandle, OSWindowHandle osHandle, ID id, int2 size, WindowFlags windowFlags, OnGuiFunc onGui) override
nativeWindowHandle is the window's NSWindow: the context adds a view with a CAMetalLayer to it.
void BeginFrame() override
Frees what the GPU is done with, and resets the renderer.
ImageIndex RegisterImage(Image *image) override
Adds the image to the image set; elements draw it by the returned index.
void UnregisterImage(ImageIndex index) override
Frees the index for reuse; the image stays (DestroyImageAtEnd destroys it).
void SetGpuPreference(std::string_view preference) override
Which GPU to render with, when there's more than one: a kind ("integrated", "discrete"),...
bool UpdateImageRows(Image *image, uint32_t firstRow, uint32_t rowCount, const void *data) override
Writes rowCount rows from firstRow into an image CreateImage made with pixels (not mipmapped): tightl...
void DestroyImageAtEnd(Image *img, DeviceScope scope, const std::source_location &loc=std::source_location::current()) override
Destroys the image and deletes the object once the GPU is done with it: for DeviceScope::Frame once i...
void SetImageSlotCount(uint32_t slots) override
4096 by default, 500,000 at most (an argument buffer's textures).
static bool CheckDeviceSupport(void *device, std::string *problems=nullptr)
Whether device (an id<MTLDevice>) runs the GUI's shaders; what it lacks, one line each,...
Image * CreateImage(void *data, uint2 size, ImageFormat format, Sampler sampler, bool mipmapped=false) override
R8G8B8 is widened to R8G8B8A8 (Metal has no 3-byte format), and R8 and R8G8 in sRGB too (macOS has no...
bool DeviceLost() const override
True once the GPU device is lost; RecreateDevice then makes a new one.
ImageIndex RegisterExternalImage(void *texture, Sampler sampler)
A texture the host renders (an id<MTLTexture>, a 3D viewport) in the image set, for the Image control...
Impl & Internal()
Library internal: the device, its queue, and what the window contexts draw with.
Definition metal_graphics_context.h:121
void PrepareFrame(StyleManager &styleManager, GradientManager &gradientManager, float elapsedTimeSecs) override
The gradient pages into the image set, and the styles uploaded where they changed.
void Cleanup() override
Waits for the GPU and destroys what the backend made, the renderer included.
TextAtlasManager * CreateTextAtlasManager() override
A new text atlas manager for the backend. The caller owns it and keeps it past Cleanup.
void DestroyWindowContext(IWindowContext *context) override
Waits for the context's GPU work and destroys it now; its native window must still exist.
void RePrepareFrame()
PrepareFrame again with its last arguments: a live resize's redraw changes styles.
void UpdateExternalImage(ImageIndex index, void *texture, Sampler sampler)
Points an existing index at another texture, for example after a resize.
ImageIndex RenderScene(ID key, ID window, uint2 size, color4f clear, std::function< void(const ScenePayload &scene)> record) override
The scene's color texture (MTLPixelFormatRGBA8Unorm, sampled with Sampler::LinearClampToEdge) and dep...
GraphicsAPI GetAPI() const override
The API it presents with; window backends make their windows for it.
Definition metal_graphics_context.h:57
void WaitIdle()
Waits until the GPU has done all work committed so far.
Sampler
How an image is filtered and addressed when sampled.
Definition gui_image.h:89
ImageFormat
Pixel formats for GraphicsContext::CreateImage, 8 bits per channel.
Definition gui_image.h:75
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:99
DeviceScope
When a deferred GPU resource destruction runs: Frame once the GPU has finished the frames that may us...
Definition gui_graphics_context.h:44
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
GraphicsAPI
The graphics API a backend presents with; window backends create their windows to match (SDL: SDL_WIN...
Definition gui_graphics_api.h:14
@ Metal
Metal, on macOS.
Definition gui_graphics_api.h:19
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
Definition gui_base_types.h:394
Integer 2D vector.
Definition gui_base_types.h:18
A host's device and queue, for the GUI to render on (SetExternalDevice) instead of making its own.
Definition metal_graphics_context.h:39
void * queue
id<MTLCommandQueue> of device: every GUI frame is committed to it
Definition metal_graphics_context.h:41
void * device
id<MTLDevice>, of the Metal 3 GPU family
Definition metal_graphics_context.h:40
Definition metal_device.h:134
What GraphicsContext::RenderScene's record gets on Metal (ScenePayload::As): the window frame's comma...
Definition metal_graphics_context.h:24
void * encoder
id<MTLRenderCommandEncoder>
Definition metal_graphics_context.h:28
void * commandBuffer
id<MTLCommandBuffer>
Definition metal_graphics_context.h:27
uint32_t colorFormat
MTLPixelFormat: MTLPixelFormatRGBA8Unorm.
Definition metal_graphics_context.h:31
uint32_t depthFormat
MTLPixelFormat: MTLPixelFormatDepth32Float.
Definition metal_graphics_context.h:32
uint2 extent
Definition metal_graphics_context.h:33
static constexpr GraphicsAPI kAPI
Definition metal_graphics_context.h:25
void * depthTexture
id<MTLTexture>
Definition metal_graphics_context.h:30
void * colorTexture
id<MTLTexture>
Definition metal_graphics_context.h:29
Definition gui_base_types.h:74