forked from Dynamitos/Seele
65 lines
9.1 KiB
Markdown
65 lines
9.1 KiB
Markdown
# Metal Backend (Metal::Graphics)
|
|||
|
|
|
||
|
|
> The Metal implementation: device setup, resource creation, and the feature set it lacks versus Vulkan.
|
||
|
|
|
||
|
|
This is a **navigation** doc: it tells you where things live, how data actually flows, and the sharp edges — so you can orient in minutes instead of hours. It complements the repository's [AGENTS.md](../../AGENTS.md) (map + rules) and the [cross-repo guide](../cross-repo.md) (engine ↔ demo contract). Everything below is grounded in the checked-out source at commit `d3ed573`.
|
||
|
|
|
||
|
|
## Key types & where they live
|
||
|
|
|
||
|
|
| Type | Defined in |
|
||
|
|
|------|------------|
|
||
|
|
| `Metal::Graphics` | `src/Engine/Graphics/Graphics.h` |
|
||
|
|
| `Metal::Window` | `src/Engine/Graphics/Metal/Window.h` |
|
||
|
|
| `Metal::Viewport` | `src/Editor/Window/ViewportControl.h` (search) |
|
||
|
|
| `Metal::RenderPass` | `src/Engine/Graphics/Metal/RenderPass.h` |
|
||
|
|
| `Metal::Buffer (all types)` | `git grep` in `src/` |
|
||
|
|
| `Metal::Texture (all types)` | `git grep` in `src/` |
|
||
|
|
| `Metal::Shader (all stages)` | `git grep` in `src/` |
|
||
|
|
| `Metal::DescriptorLayout/DescriptorSet/PipelineLayout` | `git grep` in `src/` |
|
||
|
|
| `Metal::GraphicsPipeline/ComputePipeline` | `git grep` in `src/` |
|
||
|
|
| `Metal::Command (RenderCommand/ComputeCommand)` | `git grep` in `src/` |
|
||
|
|
| `Metal::Query (Occlusion/Statistics/Timestamp)` | `git grep` in `src/` |
|
||
|
|
| `Metal::PipelineCache` | `src/Engine/Graphics/Metal/PipelineCache.h` |
|
||
|
|
| `Metal::Resources` | `src/Engine/Graphics/Metal/Resources.h` |
|
||
|
|
|
||
|
|
Paths above are resolved against the actual source tree (namespace ≈ directory in this codebase). A `(search)` marker means the file contains the name but may also define others — open it and grep. `git grep` is always a fallback. The full per-file map is in **Where to look** below.
|
||
|
|
|
||
|
|
## Data flow / lifecycle
|
||
|
|
|
||
|
|
Metal::Graphics (src/Engine/Graphics/Metal/Graphics.h + Graphics.mm, 5.8KB header + 8.3KB impl) implements Gfx::Graphics using the Metal.framework (Objective-C++). init(GraphicsInitializer) creates an MTLDevice (MTLCreateSystemDefaultDevice or from a specific identifier), an MTLCommandQueue (banded width from GraphicsInitializer), and a Metal::PipelineCache (MTLBinaryArchive-based). There is NO surface/swapchain management in Graphics.mm — that is in Window.mm (7.7KB) which creates an NSWindow (Cocoa) + CAMetalLayer + MTLView, manages the drawable pool, and handles the frame loop (drawable acquisition, command buffer commit, present). The Metal backend does NOT support ray tracing (rayTracingEnabled is always false — Metal RT is not implemented). It also does NOT support mesh shading (meshShadingEnabled is always false). All resource types mirror the Gfx::Graphics interface: Buffer.mm (8.9KB) implements MTLBuffer wrappers for all buffer types (shader/uniform/vertex/index) with MTLResourceStorageModeShared by default. Texture.mm (14.6KB) implements all MTLTexture types (2D/2DArray/3D/cube) with mipmap generation (blit encoder generateMipmaps), MTLTextureView, MTSamplerState. Shader.mm (1.9KB) creates MTLFunction objects from compiled MetalSL (the Slang compiler outputs MetalSL for the Metal target — see slang-compile.cpp target detection). Command.mm (16.5KB) implements MTLCommandBuffer + MTLRenderCommandEncoder + MTLComputeCommandEncoder wrappers: RenderCommand begins/ends render encoders, draws primitives (drawPrimitives, drawIndexedPrimitives), blits, resolves; ComputeCommand dispatches threadgroups (dispatchThreadgroups). Descriptor.mm (8.8KB) implements MTLBuffer/MTLTexture/MTLSamplerState binding into an MTLRenderPipelineState/MTLComputePipelineState (Metal has no descriptor sets — bindings are direct function arguments via the pipeline state's argument buffer, implemented here as an argument buffer + per-frame update). Pipeline.mm (906B) creates MTLRenderPipelineState (from MTLRenderPipelineDescriptor) and MTLComputePipelineState (from MTLComputePipelineDescriptor). Query.mm (2KB) implements MTLCommandBuffer counters (GPU timestamp via MTLCommandBuffer.GPUStartTime/GPUEndTime, occlusion via MTLResourceID + draw dispatch count). PipelineCache.mm (12.8KB) uses MTLBinaryArchive to cache compiled pipeline states to disk. Resources.mm (2KB) is a thin wrapper for shared Metal resource management. The Metal backend uses Objective-C++ (.mm files) throughout — all Metal API calls are Objective-C method invocations. The C++-side headers (Graphics.h, Buffer.h, Texture.h, etc.) define the C++ interface that matches Gfx::Graphics, and the .mm files implement them.
|
||
|
|
|
||
|
|
## Where to look
|
||
|
|
|
||
|
|
| Path | What's there |
|
||
|
|
|------|--------------|
|
||
|
|
| `src/Engine/Graphics/Metal/Graphics.h` | 5.8KB — Metal backend interface |
|
||
|
|
| `src/Engine/Graphics/Metal/Graphics.mm` | 8.3KB — MTLDevice + MTLCommandQueue creation, factory methods |
|
||
|
|
| `src/Engine/Graphics/Metal/Window.h + .mm` | Cocoa NSWindow + CAMetalLayer + MTLView, 7.7KB impl |
|
||
|
|
| `src/Engine/Graphics/Metal/Command.h + .mm` | MTLCommandBuffer + render/compute encoders, 16.5KB impl |
|
||
|
|
| `src/Engine/Graphics/Metal/Texture.h + .mm` | all MTLTexture types, 14.6KB impl |
|
||
|
|
| `src/Engine/Graphics/Metal/Buffer.h + .mm` | MTLBuffer wrappers, 8.9KB impl |
|
||
|
|
| `src/Engine/Graphics/Metal/Descriptor.h + .mm` | argument buffers for Metal pipeline binding, 8.8KB impl |
|
||
|
|
| `src/Engine/Graphics/Metal/Pipeline.h + .mm` | MTLRenderPipelineState + MTLComputePipelineState |
|
||
|
|
| `src/Engine/Graphics/Metal/Query.h + .mm` | GPU timestamps + occlusion counters |
|
||
|
|
| `src/Engine/Graphics/Metal/PipelineCache.h + .mm` | MTLBinaryArchive caching, 12.8KB impl |
|
||
|
|
| `src/Engine/Graphics/Metal/Shader.h + .mm` | MTLFunction from MetalSL |
|
||
|
|
| `src/Engine/Graphics/Metal/Resources.h + .mm` | shared resource management |
|
||
|
|
| `src/Engine/Graphics/Metal/Enums.h + .mm` | Metal-specific enum mappings, 32KB impl |
|
||
|
|
|
||
|
|
## Gotchas & sharp edges
|
||
|
|
|
||
|
|
- The Metal backend does NOT support ray tracing or mesh shading — both capability flags are hardcoded false. RayTracingPass and mesh-shading pipelines will never be used on Metal. The engine's RT path is Vulkan-only.
|
||
|
|
- Metal has no descriptor sets or pipeline layouts in the hardware sense. Metal::Descriptor.mm implements the Gfx::DescriptorLayout/DescriptorSet interface using MTLBuffer argument buffers — the 'descriptor set' is an MTLBuffer that holds raw GPU-visible data, and the pipeline state's MTLFunction constant binding references it. This is a software abstraction over Metal's argument buffer mechanism.
|
||
|
|
- All Metal source files are .mm (Objective-C++). You need Xcode command-line tools + the Metal.framework to build the Metal backend. The CMakeLists.txt in Metal/ configures the .mm files and links against Metal.framework + Foundation.framework.
|
||
|
|
- The MTLDevice is created once at init() and is shared by all resources. There is no per-window device — a single MTLDevice serves all windows in the process. If the device is lost (e.g. GPU reset), the entire engine must be re-initialized.
|
||
|
|
- MTLCommandQueue is created at init() with a banded width (from GraphicsInitializer). The command queue is long-lived and shared by all render/compute commands. Command buffers are created per-frame (or per-command) and destroyed after commit + waitCompleted. There is no command buffer pool — each MTLCommandBuffer is created fresh.
|
||
|
|
- The Window.mm file creates an NSWindow (Cocoa) on macOS. On Linux the Metal backend is NOT available (no Metal.framework on Linux). The Metal backend is macOS-only. Linux uses the Vulkan backend. Windows can use either (the build system selects based on CMake options).
|
||
|
|
- Mipmap generation on Metal uses MTLBlitCommandEncoder.generateMipmapsForTexture. The texture must have MTLResourceUsageBlit | MTLResourceUsageShaderRead enabled. The initial texture usage in Texture.mm is set based on the usage flags in TextureCreateInfo — if mipmap generation is requested but the usage flags are wrong, the blit will fail silently.
|
||
|
|
- The Metal backend's command encoder lifetime is tied to the MTLCommandBuffer. All render/compute encoders must be ended before the command buffer is committed. The Command.mm implementation tracks encoder lifetime and asserts if you try to commit with an open encoder.
|
||
|
|
- MTLBinaryArchive (PipelineCache.mm) caches compiled MTLRenderPipelineState and MTLComputePipelineState objects. The cache is keyed by a hash of the pipeline descriptor + shader function name. If the Metal driver changes (macOS update), the cache is invalidated automatically by the system (MTLBinaryArchive handles this).
|
||
|
|
- The Metal backend's Window.mm uses MTLView (a NSView subclass that integrates Metal rendering with Cocoa). The frame callback is driven by MTLView's updateDrawable callback (called by the system when the window needs a new frame). There is no manual frame loop — the system drives rendering at the display refresh rate.
|
||
|
|
- Metal's GPU timestamps (Query.mm) use MTLCommandBuffer.GPUStartTime/GPUEndTime (mach_time_t). Converting to nanoseconds requires converting the mach_time base (mach_timebase_info). The TimestampQuery implementation in Query.mm does this conversion. Occlusion queries use MTLCommandBuffer.dispatchThreadgroupCounts + a counter buffer (there is no hardware occlusion query on Metal).
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
*Generated from a full read of the checked-out source at commit `d3ed573`. If a path has moved, `git grep` for the type name — the engine is small enough to find anything in seconds.*
|