TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
tachyon::metal::MetalGraphicsContext Class Reference

#include <gui_core/graphics/metal/metal_graphics_context.h>

Inheritance diagram for tachyon::metal::MetalGraphicsContext:

Classes

struct  Impl

Public Member Functions

 MetalGraphicsContext ()
 ~MetalGraphicsContext () override
void SetExternalDevice (const ExternalDevice &external)
 Render on the host's device and queue instead of making them.
GraphicsAPI GetAPI () const override
 The API it presents with; window backends make their windows for it.
std::string_view GetDeviceName () const override
 The GPU it renders with, as the driver names it ("NVIDIA GeForce RTX 4070"); empty before Initialize and for a backend without a device.
bool DeviceLost () const override
 True once the GPU device is lost; RecreateDevice then makes a new one.
void SetGpuPreference (std::string_view preference) override
 Which GPU to render with, when there's more than one: a kind ("integrated", "discrete"), the GPU's number in the log's list of detected GPUs, or part of its name ("radeon"); see ChooseGpu.
void SetImageSlotCount (uint32_t slots) override
 4096 by default, 500,000 at most (an argument buffer's textures).
uint32_t GetImageSlotCount () const override
 How many images can be registered at once; 0 when the backend doesn't say.
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 another), and makes its queue and the GUI's pipelines from the built-in shaders.
void Cleanup () override
 Waits for the GPU and destroys what the backend made, the renderer included.
void BeginFrame () override
 Frees what the GPU is done with, and resets the renderer.
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 sRGB format of fewer channels).
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): tightly packed in its format, the image's width each.
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 it has finished the frames that may have drawn with it, however many frames draw nothing meanwhile; for DeviceScope::Application at Cleanup.
TextAtlasManager * CreateTextAtlasManager () override
 A new text atlas manager for the backend. The caller owns it and keeps it past Cleanup.
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 DestroyWindowContext (IWindowContext *context) override
 Waits for the context's GPU work and destroys it now; its native window must still exist.
ImageSet * GetImageSet () const override
 The images the shaders sample, by ImageIndex.
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 PrepareFrame (StyleManager &styleManager, GradientManager &gradientManager, float elapsedTimeSecs) override
 The gradient pages into the image set, and the styles uploaded where they changed.
void RePrepareFrame ()
 PrepareFrame again with its last arguments: a live resize's redraw changes styles.
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 depth (MTLPixelFormatDepth32Float), drawn by record in window's next frame before its UI.
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 or a primitive to sample with sampler.
void UpdateExternalImage (ImageIndex index, void *texture, Sampler sampler)
 Points an existing index at another texture, for example after a resize.
void WaitIdle ()
 Waits until the GPU has done all work committed so far.
Impl & Internal ()
 Library internal: the device, its queue, and what the window contexts draw with.
Public Member Functions inherited from tachyon::NullGraphicsContext
 NullGraphicsContext ()
 ~NullGraphicsContext () override
GraphicsAPI GetAPI () const override
 The API it presents with; window backends make their windows for it.
void SetAllocationHooks (const AllocationHooks &) override
 Host memory functions for the graphics API's allocations. Call before Initialize.
Result< void > Initialize (IWindowBackend *windowBackend) override
 Brings the device up; GUIApplication::Initialize calls it once, before any window context.
void Cleanup () override
 Waits for the GPU and destroys what the backend made, the renderer included.
void BeginFrame () override
 Starts a frame's UI build: resets the renderer for new primitives (Vulkan: also frees what frame-scoped deletions held long enough).
void EndFrame () override
 Ends the UI build, before the uploads and the window contexts' recording.
Image * CreateImage (void *data, uint2 size, ImageFormat format, Sampler sampler, bool mipmapped=false) override
 Makes an image from size.x x size.y tightly packed pixels of format (Vulkan: data null for a cleared image); mipmapped generates its mip levels.
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 it has finished the frames that may have drawn with it, however many frames draw nothing meanwhile; for DeviceScope::Application at Cleanup.
TextAtlasManager * CreateTextAtlasManager () override
 A new text atlas manager for the backend. The caller owns it and keeps it past Cleanup.
IWindowContext * CreateWindowContext (void *nativeWindowHandle, OSWindowHandle osHandle, ID id, int2 size, WindowFlags windowFlags, OnGuiFunc onGui) override
 Makes the context that renders an OS window: nativeWindowHandle comes from IWindowBackend::GetNativeHandle, size is in pixels and onGui builds the window's UI.
void DestroyWindowContext (IWindowContext *context) override
 Waits for the context's GPU work and destroys it now; its native window must still exist.
ImageSet * GetImageSet () const override
 The images the shaders sample, by ImageIndex.
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 PrepareFrame (StyleManager &styleManager, GradientManager &gradientManager, float) override
 Nothing to upload: takes the style and gradient changes, as a GPU backend's upload does.
uint64_t RecordedFrames (ID window) const
 How many frames the window was recorded in (RecordWindowFrame), for tests.
Public Member Functions inherited from tachyon::GraphicsContext
bool TakeContentChanged ()
 True when something drawn changed since the last call without a draw changing: an image registered or unregistered (a slot reused shows other pixels), or a scene rendered.
 GraphicsContext ()=default
virtual ~GraphicsContext ()
virtual Result< void > RecreateDevice (IWindowBackend *)
 Makes a new device after DeviceLost, and everything the backend owns on it (the window contexts are the caller's to make again).
virtual float GetLastFrameBuildSeconds () const
 Wall-clock seconds of CPU work in the last frame: from just after one present returns to just before the next, so without the vsync wait.

Static Public Member Functions

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, in problems.

Additional Inherited Members

Public Attributes inherited from tachyon::GraphicsContext
GUIRenderer * guiRenderer = nullptr
 Where the UI records its draw primitives.
Protected Member Functions inherited from tachyon::GraphicsContext
void MarkContentChanged ()
 For backends: see TakeContentChanged.

Constructor & Destructor Documentation

◆ MetalGraphicsContext()

tachyon::metal::MetalGraphicsContext::MetalGraphicsContext ( )

◆ ~MetalGraphicsContext()

tachyon::metal::MetalGraphicsContext::~MetalGraphicsContext ( )
override

Member Function Documentation

◆ CheckDeviceSupport()

bool tachyon::metal::MetalGraphicsContext::CheckDeviceSupport ( void * device,
std::string * problems = nullptr )
static

Whether device (an id<MTLDevice>) runs the GUI's shaders; what it lacks, one line each, in problems.

It needs the Metal 3 GPU family, for its bindless images.

◆ SetExternalDevice()

void tachyon::metal::MetalGraphicsContext::SetExternalDevice ( const ExternalDevice & external)

Render on the host's device and queue instead of making them.

Call before Initialize, which fails when they can't be used.

◆ GetAPI()

GraphicsAPI tachyon::metal::MetalGraphicsContext::GetAPI ( ) const
inlineoverridevirtual

The API it presents with; window backends make their windows for it.

Implements tachyon::GraphicsContext.

◆ GetDeviceName()

std::string_view tachyon::metal::MetalGraphicsContext::GetDeviceName ( ) const
overridevirtual

The GPU it renders with, as the driver names it ("NVIDIA GeForce RTX 4070"); empty before Initialize and for a backend without a device.

Reimplemented from tachyon::GraphicsContext.

◆ DeviceLost()

bool tachyon::metal::MetalGraphicsContext::DeviceLost ( ) const
overridevirtual

True once the GPU device is lost; RecreateDevice then makes a new one.

Reimplemented from tachyon::GraphicsContext.

◆ SetGpuPreference()

void tachyon::metal::MetalGraphicsContext::SetGpuPreference ( std::string_view preference)
overridevirtual

Which GPU to render with, when there's more than one: a kind ("integrated", "discrete"), the GPU's number in the log's list of detected GPUs, or part of its name ("radeon"); see ChooseGpu.

Empty, the default, leaves it to the backend (a discrete GPU first). The TACHYON_GPU environment variable takes its place when set. Call before Initialize. The default does nothing.

Reimplemented from tachyon::GraphicsContext.

◆ SetImageSlotCount()

void tachyon::metal::MetalGraphicsContext::SetImageSlotCount ( uint32_t slots)
overridevirtual

4096 by default, 500,000 at most (an argument buffer's textures).

Call before Initialize.

Reimplemented from tachyon::GraphicsContext.

◆ GetImageSlotCount()

uint32_t tachyon::metal::MetalGraphicsContext::GetImageSlotCount ( ) const
overridevirtual

How many images can be registered at once; 0 when the backend doesn't say.

Reimplemented from tachyon::GraphicsContext.

◆ Initialize()

Result< void > tachyon::metal::MetalGraphicsContext::Initialize ( IWindowBackend * windowBackend)
overridevirtual

Picks a GPU of the Metal 3 family (the system's default unless the app or TACHYON_GPU asks for another), and makes its queue and the GUI's pipelines from the built-in shaders.

Implements tachyon::GraphicsContext.

◆ Cleanup()

void tachyon::metal::MetalGraphicsContext::Cleanup ( )
overridevirtual

Waits for the GPU and destroys what the backend made, the renderer included.

The window contexts are destroyed before it.

Implements tachyon::GraphicsContext.

◆ BeginFrame()

void tachyon::metal::MetalGraphicsContext::BeginFrame ( )
overridevirtual

Frees what the GPU is done with, and resets the renderer.

Implements tachyon::GraphicsContext.

◆ CreateImage()

Image * tachyon::metal::MetalGraphicsContext::CreateImage ( void * data,
uint2 size,
ImageFormat format,
Sampler sampler,
bool mipmapped = false )
overridevirtual

R8G8B8 is widened to R8G8B8A8 (Metal has no 3-byte format), and R8 and R8G8 in sRGB too (macOS has no sRGB format of fewer channels).

mipmapped makes the base level only: the samplers read nothing else, as on the other APIs. The pixels go to the GPU with the next window frame.

Implements tachyon::GraphicsContext.

◆ UpdateImageRows()

bool tachyon::metal::MetalGraphicsContext::UpdateImageRows ( Image * image,
uint32_t firstRow,
uint32_t rowCount,
const void * data )
overridevirtual

Writes rowCount rows from firstRow into an image CreateImage made with pixels (not mipmapped): tightly packed in its format, the image's width each.

As CreateImage's upload, it doesn't wait for the GPU and goes with the next frame, ahead of its draws; the frames already sent draw what they had. False when the backend can't, and the caller makes the image again instead (the default).

Reimplemented from tachyon::GraphicsContext.

◆ DestroyImageAtEnd()

void tachyon::metal::MetalGraphicsContext::DestroyImageAtEnd ( Image * img,
DeviceScope scope,
const std::source_location & loc = std::source_location::current() )
overridevirtual

Destroys the image and deletes the object once the GPU is done with it: for DeviceScope::Frame once it has finished the frames that may have drawn with it, however many frames draw nothing meanwhile; for DeviceScope::Application at Cleanup.

Unregister it first (UnregisterImage): no frame may draw with it after this. Any thread. loc is for diagnostics.

Implements tachyon::GraphicsContext.

◆ CreateTextAtlasManager()

TextAtlasManager * tachyon::metal::MetalGraphicsContext::CreateTextAtlasManager ( )
overridevirtual

A new text atlas manager for the backend. The caller owns it and keeps it past Cleanup.

Implements tachyon::GraphicsContext.

◆ CreateWindowContext()

IWindowContext * tachyon::metal::MetalGraphicsContext::CreateWindowContext ( void * nativeWindowHandle,
OSWindowHandle osHandle,
ID id,
int2 size,
WindowFlags windowFlags,
OnGuiFunc onGui )
overridevirtual

nativeWindowHandle is the window's NSWindow: the context adds a view with a CAMetalLayer to it.

A render-to-image window's frames are read back.

Implements tachyon::GraphicsContext.

◆ DestroyWindowContext()

void tachyon::metal::MetalGraphicsContext::DestroyWindowContext ( IWindowContext * context)
overridevirtual

Waits for the context's GPU work and destroys it now; its native window must still exist.

Implements tachyon::GraphicsContext.

◆ GetImageSet()

ImageSet * tachyon::metal::MetalGraphicsContext::GetImageSet ( ) const
overridevirtual

The images the shaders sample, by ImageIndex.

Implements tachyon::GraphicsContext.

◆ RegisterImage()

ImageIndex tachyon::metal::MetalGraphicsContext::RegisterImage ( Image * image)
overridevirtual

Adds the image to the image set; elements draw it by the returned index.

Implements tachyon::GraphicsContext.

◆ UnregisterImage()

void tachyon::metal::MetalGraphicsContext::UnregisterImage ( ImageIndex index)
overridevirtual

Frees the index for reuse; the image stays (DestroyImageAtEnd destroys it).

Implements tachyon::GraphicsContext.

◆ PrepareFrame()

void tachyon::metal::MetalGraphicsContext::PrepareFrame ( StyleManager & styleManager,
GradientManager & gradientManager,
float elapsedTimeSecs )
overridevirtual

The gradient pages into the image set, and the styles uploaded where they changed.

Implements tachyon::GraphicsContext.

◆ RePrepareFrame()

void tachyon::metal::MetalGraphicsContext::RePrepareFrame ( )

PrepareFrame again with its last arguments: a live resize's redraw changes styles.

◆ RenderScene()

ImageIndex tachyon::metal::MetalGraphicsContext::RenderScene ( ID key,
ID window,
uint2 size,
color4f clear,
std::function< void(const ScenePayload &scene)> record )
overridevirtual

The scene's color texture (MTLPixelFormatRGBA8Unorm, sampled with Sampler::LinearClampToEdge) and depth (MTLPixelFormatDepth32Float), drawn by record in window's next frame before its UI.

Reimplemented from tachyon::GraphicsContext.

◆ RegisterExternalImage()

ImageIndex tachyon::metal::MetalGraphicsContext::RegisterExternalImage ( void * texture,
Sampler sampler )

A texture the host renders (an id<MTLTexture>, a 3D viewport) in the image set, for the Image control or a primitive to sample with sampler.

The texture stays the host's and must outlive every GUI frame that samples it; Metal orders the host's work on the same queue before the GUI's frame. Call it while the UI is built (OnGUI).

◆ UpdateExternalImage()

void tachyon::metal::MetalGraphicsContext::UpdateExternalImage ( ImageIndex index,
void * texture,
Sampler sampler )

Points an existing index at another texture, for example after a resize.

No GUI frame in flight may still sample the old one (WaitIdle).

◆ WaitIdle()

void tachyon::metal::MetalGraphicsContext::WaitIdle ( )

Waits until the GPU has done all work committed so far.

◆ Internal()

Impl & tachyon::metal::MetalGraphicsContext::Internal ( )
inline

Library internal: the device, its queue, and what the window contexts draw with.


The documentation for this class was generated from the following file: