12#import <Metal/Metal.h>
28#include <unordered_map>
29#include <unordered_set>
42 inline constexpr MTLPixelFormat
kDrawFormat = MTLPixelFormatBGRA8Unorm;
43 inline constexpr MTLPixelFormat
kDepthFormat = MTLPixelFormatDepth32Float;
81 explicit operator bool()
const {
return buffer != nil; }
91 MTLPixelFormat
format = MTLPixelFormatInvalid;
107 void Create(uint32_t capacity) { m_capacity = capacity; }
119 uint32_t
SlotsInUse()
const {
return uint32_t(m_images.size()); }
120 const std::vector<MetalImage*>&
Images()
const {
return m_images; }
122 const std::vector<uint32_t>&
SamplerSlots()
const {
return m_samplers; }
127 uint32_t m_capacity = 0;
128 std::vector<MetalImage*> m_images;
129 std::vector<uint32_t> m_samplers;
130 std::vector<uint32_t> m_free;
149 std::deque<std::pair<uint64_t, id<MTLCommandBuffer>>>
inFlight;
156 uint64_t
Commit(id<MTLCommandBuffer> commandBuffer);
167 std::unordered_map<uint32_t, id<MTLBuffer>>
buffers;
213 std::unordered_map<RHI::ResidentBufferId, Buffer>
residents;
217 std::span<DeviceAddress> addresses);
238 std::array<RHI::GpuPipeline, kPrimitiveKindCount>
pipelines {};
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
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
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
uint64_t ResidentBufferId
GPU storage kept across frames (UploadResidentBuffers); 0 is none.
Definition gui_rhi.h:36
Sampler
How an image is filtered and addressed when sampled.
Definition gui_image.h:89
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:99
uint64_t DeviceAddress
Shader-visible buffer address.
Definition gui_render_primitives.h:206
ResidentState
Where a resident buffer is.
Definition gui_rhi.h:40
@ NearestRepeat
Definition gui_image.h:92
@ Invalid
Definition gui_image.h:99
Definition gui_logging.h:24
Definition gui_render_primitives.h:257
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
Definition gui_base_types.h:394
Definition metal_device.h:184
std::unique_ptr< MetalImage > image
Definition metal_device.h:186
id< MTLResource > resource
Definition metal_device.h:187
uint64_t afterValue
Definition metal_device.h:185
A whole image's pixels, or with partial a band of rows from firstRow written into an image already up...
Definition metal_device.h:196
uint32_t firstRow
Definition metal_device.h:200
uint32_t rowCount
Definition metal_device.h:201
id< MTLBuffer > staging
Definition metal_device.h:198
MetalImage * image
Definition metal_device.h:197
NSUInteger rowPitch
Definition metal_device.h:199
Definition metal_device.h:267
ID key
Definition metal_device.h:268
std::function< void(const ScenePayload &scene)> record
Definition metal_device.h:271
ID window
Definition metal_device.h:269
color4f clear
Definition metal_device.h:270
What RenderScene keeps for a key: its color texture (in the image set) and depth, both in liveImages.
Definition metal_device.h:259
uint64_t lastFrame
the frame it was last asked for
Definition metal_device.h:264
uint2 extent
Definition metal_device.h:263
ImageIndex index
Definition metal_device.h:262
MetalImage * color
Definition metal_device.h:260
MetalImage * depth
Definition metal_device.h:261
Definition metal_device.h:134
void Wait(uint64_t value)
Waits on the CPU until the commit counted value (and every one before) is done.
id< MTLDevice > device
Definition metal_device.h:140
RHI::ResidentBufferId CreateResidentBuffer(std::span< const RHI::UploadRequest > requests, std::span< DeviceAddress > addresses)
uint32_t nextBufferHandle
Definition metal_device.h:168
MetalImageSet imageSet
Definition metal_device.h:227
id< MTLBuffer > quadIndices
p * 4 + { 0, 1, 3, 0, 3, 2 } for each quad p.
Definition metal_device.h:243
void FlushDeletions()
Frees what the GPU is done with.
GradientManager * lastGradients
Definition metal_device.h:234
bool EnsureQuadIndices(uint32_t quads)
Grows the quads' index buffer to hold quads; false when it can't.
std::vector< ImageUpload > uploadScratch
Definition metal_device.h:205
float lastTime
Definition metal_device.h:235
void RecordPendingUploads(id< MTLCommandBuffer > commandBuffer)
Records the queued image copies into commandBuffer, ahead of its draws.
uint64_t Completed()
The highest value all commits up to are done.
uint64_t Commit(id< MTLCommandBuffer > commandBuffer)
Commits commandBuffer and counts it; its value.
std::string deviceName
Definition metal_device.h:137
std::pair< id< MTLBuffer >, NSUInteger > Resolve(DeviceAddress address) const
The buffer and offset an address names; nil for 0 or a buffer let go of.
uint64_t completed
Definition metal_device.h:150
std::deque< std::pair< uint64_t, id< MTLCommandBuffer > > > inFlight
Definition metal_device.h:149
std::vector< ImageUpload > imageUploads
Definition metal_device.h:204
std::unordered_map< RHI::ResidentBufferId, Buffer > residents
Shared memory: written at once and never again, so ready for the draws recorded from now on.
Definition metal_device.h:213
GPUDrawPushConstants pushConstants
the styles' addresses and the time, from PrepareFrame
Definition metal_device.h:247
std::string gpuPreference
Definition metal_device.h:136
std::mutex inFlightMutex
The command buffers committed and not seen complete, oldest first.
Definition metal_device.h:148
std::unordered_map< ID, Scene > scenes
Definition metal_device.h:273
std::vector< id< MTLRenderPipelineState > > pipelineStates
By PrimitiveKind; GpuPipeline::pipeline is the id<MTLRenderPipelineState>, unretained.
Definition metal_device.h:237
uint32_t imageSlots
asked for with SetImageSlotCount
Definition metal_device.h:138
std::vector< ImageIndex > gradientPages
each gradient page's index in the image set
Definition metal_device.h:232
ExternalDevice external
the host's device and queue, when it gave them
Definition metal_device.h:135
id< MTLDepthStencilState > depthWrite
GREATER_EQUAL, writes (occluders).
Definition metal_device.h:240
MetalImage * CreateTargetImage(uint2 size, MTLPixelFormat format, MTLTextureUsage usage, Sampler sampler)
A texture the GPU draws into (a scene's), in liveImages so DestroyImageAtEnd frees it; null on failur...
std::mutex uploadMutex
images are made on any thread
Definition metal_device.h:203
StyleManager * lastStyles
PrepareFrame's, for a live resize's redraw.
Definition metal_device.h:233
std::unordered_map< uint32_t, id< MTLBuffer > > buffers
Every buffer the shaders read, by its handle (Buffer::handle); handles are never handed out twice.
Definition metal_device.h:167
void RecordScenes(id< MTLCommandBuffer > commandBuffer, ID window)
Records the scenes asked for in window this frame, before its UI.
std::array< RHI::GpuPipeline, kPrimitiveKindCount > pipelines
Definition metal_device.h:238
RHI::ResidentBufferId boxStyles
The box styles (a resident buffer), made again when they change; 0: none.
Definition metal_device.h:229
std::vector< Garbage > garbage
Definition metal_device.h:190
void WaitIdle()
Waits until everything committed so far is done.
std::mutex garbageMutex
images are let go on any thread
Definition metal_device.h:189
static constexpr uint64_t kSceneKeepFrames
a scene not asked for this long goes
Definition metal_device.h:254
void DestroyAfterGpu(std::unique_ptr< MetalImage > image, uint64_t afterValue)
What's let go is freed once the GPU is past afterValue: the next commit (submitted + 1) for anything ...
uint64_t sceneFrame
Definition metal_device.h:275
static constexpr MTLPixelFormat kSceneColorFormat
Definition metal_device.h:253
std::atomic< bool > removed
A command buffer failed in a way that loses the device: told once, and the GUI is told through Device...
Definition metal_device.h:153
void ReleaseBuffer(Buffer &buffer)
Its handle given back: draws recorded with it keep the buffer (Metal holds it).
std::atomic< uint64_t > submitted
Every commit counts the next value: submitted is the last one.
Definition metal_device.h:146
uint32_t quadCapacity
Definition metal_device.h:244
std::unordered_set< MetalImage * > liveImages
Every image made and not let go for a frame (DeviceScope::Frame): those handed over for the applicati...
Definition metal_device.h:224
id< MTLCommandQueue > queue
Definition metal_device.h:141
void ReleaseResident(RHI::ResidentBufferId id)
std::vector< SceneRequest > sceneRequests
Definition metal_device.h:274
RHI::ResidentBufferId colorStyles
The color styles, with their gradient pages' slots, likewise.
Definition metal_device.h:231
void DestroyAfterGpu(id< MTLResource > resource, uint64_t afterValue)
std::unordered_map< uint32_t, std::unique_ptr< MetalImage > > externalImages
By image index: what the image set points at for a host's texture.
Definition metal_device.h:278
std::array< id< MTLSamplerState >, 3 > samplers
SamplerSlot's order.
Definition metal_device.h:241
RHI::ResidentBufferId nextResident
Definition metal_device.h:214
id< MTLBuffer > emptyBuffer
Bound where a draw's buffer address is 0, so every buffer the shaders declare is bound.
Definition metal_device.h:246
id< MTLDepthStencilState > depthTest
GREATER_EQUAL, no writes.
Definition metal_device.h:239
Buffer CreateBuffer(size_t size)
A shared buffer the shaders can read, with a handle. Empty on failure.
RHI::ResidentState ResidentState(RHI::ResidentBufferId id) const
Definition gui_base_types.h:74