Metal Backend
The Metal backend is an Apple-specific backend implemented using the Metal graphics API. A Metal GraphicsDevice can be created with a pointer to an NSWindow. That window will be used to create the device's main swapchain.
API Concept Map
Veldrid Concept | Metal Concept | Notes |
---|---|---|
GraphicsDevice | MTLDevice, MTLCommandQueue | |
CommandList | MTLCommandBuffer, MTLRenderCommandEncoder, MTLBlitCommandEncoder, MTLComputeCommandEncoder | |
DeviceBuffer | MTLBuffer | |
BufferUsage | None | MTLBuffers are not specialized -- any buffer can be used for any operation. |
Texture | MTLTexture | |
TextureUsage | MTLTextureUsage | |
TextureView | MTLTexture | Similar to OpenGL, there is no dedicated TextureView object. "Subset" views can be generated by creating a new MTLTexture from an existing one. |
Sampler | MTLSamplerState | |
Pipeline | MTLRenderPipelineState, MTLComputePipelineState | |
Blend State | MTLRenderPipelineColorAttachmentDescriptor | |
Depth Stencil State | MTLDepthStencilState | |
Rasterizer State | MTLRenderCommandEncoder functions: setCullMode , setFrontFacing |
|
PrimitiveTopology | MTLPrimitiveType | |
Vertex Layouts | MTLVertexDescriptor | |
Shader | MTLLibrary, MTLFunction | |
ShaderSetDescription | None | |
Framebuffer | MTLRenderPassDescriptor | |
ResourceLayout | None | The Metal backend uses the information from a ResourceLayout to determine which buffer, texture, and sampler slots to bind resources to. |
ResourceSet | None | The Metal backend simply uses the individual resources contained in a ResourceSet to bind to appropriate shader stages. The slots are determined using information from the associated ResourceLayout. |
Swapchain | CAMetalLayer, CAMetalDrawable |
Notes
Metal is a high-performance, modern graphics API developed by Apple. It is well-supported and well-implemented on newer devices, and as such it should be the preferred backend on those systems. Unlike OpenGL, it supports modern functionality like Compute shaders, and is also receiving continued investment and updates from Apple.
Metal Interop
Metal is provided as an Objective-C API and is therefore much more difficult to interoperate with from .NET. The Veldrid.MetalBindings assembly contains a minimal set of helpers and function imports that are needed to communicate with Metal. It is possible that a non-negligible amount of overhead is caused by the complexity of this Objective-C interop layer, but it is unavoidable.
Staging Textures
As with the Vulkan backend, Metal staging Textures are implemented using an MTLBuffer, for simplicity.