gpu/mtl_res.rs (Apple)#
Source: lib/gpu/mtl_res.rs
mtl_res -- Metal resources (AP6, docs/APPLE.md): buffers, textures and views, samplers, shader
modules (SPIR-V translated to MSL by shader::msl), bind group and pipeline layouts, bind
groups, compute and render pipelines.
Buffers: MemoryKind::Device private storage, Upload and Readback shared (Apple GPUs have one
memory; the kinds keep the API's rules: only Upload / Readback are mapped). Textures: private,
views are MTLTexture views (newTextureViewWithPixelFormat:textureType:levels:slices:).
Shader modules: ShaderModule::from_spirv translates every entry point with the module's default
constants and compiles the MSL once (MTLCompileOptions without fast math): a misuse or a gap of
the translation shows at once, as on Vulkan; a pipeline with constants (or a point list)
translates and compiles its own. Bindings are flat: each pipeline keeps, per stage, which
(group, binding) goes to which buffer / texture / sampler slot (shader::msl), and the command
encoder sets them before each dispatch or draw (mtl_cmd.rs). Bindless and timestamps are not
built yet (level Basis; Features says so).
struct Buffer(pub(crate) Arc<BufInner>)GPU memory for data: vertices, indices, storage, uniforms, indirect arguments
impl Buffer
fn size(&self) -> u64fn usage(&self) -> BufferUsagefn memory(&self) -> MemoryKindfn write<T: Pod>(&self, offset: u64, data: &[T]) -> Result<()>copy data into a mapped buffer (Upload / Readback) at offset; the GPU sees it in the next submission -- do not write what a pending submission still readsfn read<T: Pod>(&self, offset: u64, out: &mut [T]) -> Result<()>copy a mapped buffer's contents at offset into out (wait for the submission that wrote them)
impl Device
fn create_buffer(&self, desc: &BufferDesc) -> Result<Buffer>struct TextureView(pub(crate) Arc<ViewInner>)a range of a texture's mips and layers as shaders and render passes see it
impl TextureView
fn format(&self) -> Formatfn size(&self) -> (u32, u32)width and height of the view's first mipfn usage(&self) -> TextureUsagefn same(&self, other: &TextureView) -> boolthe same view (it or a clone of it): a cache's keystruct Texturean image on the GPU: 2D, 2D array, cube (array) or 3D, with mips
impl Texture
fn view(&self) -> &TextureViewfn width(&self) -> u32fn height(&self) -> u32fn depth(&self) -> u32fn layers(&self) -> u32fn mips(&self) -> u32fn format(&self) -> Formatfn usage(&self) -> TextureUsagefn create_view(&self, desc: &ViewDesc) -> Result<TextureView>a view of some mips / layers (counts 0: to the end)
impl Device
fn create_texture(&self, desc: &TextureDesc) -> Result<Texture>struct Sampler(pub(crate) Arc<SamplerInner>)how textures are filtered and addressed
impl Device
fn create_sampler(&self, desc: &SamplerDesc) -> Result<Sampler>struct ShaderModule(pub(crate) Arc<ShaderInner>)compiled shader code (SPIR-V, translated to MSL)
impl ShaderModule
fn from_spirv(device: &Device, code: &[u32]) -> Result<ShaderModule>a module from SPIR-V words (1.0 ..= 1.6), translated to MSL and compiledfn from_wgsl(_device: &Device, _source: &str) -> Result<ShaderModule>(the browser) WGSL text: Metal takes SPIR-V like Vulkanfn entry_points(&self) -> Vec<(u32, &str)>the entry points: (model as SPIR-V numbers it: 0 vertex, 4 fragment, 5 compute, name)fn constants(&self) -> Vec<(u32, shader::reflect::Kind, f64)>the pipeline-overridable constants the module declares: (id, kind, default value)struct BindGroupLayout(pub(crate) Arc<BglInner>)the bindings of one bind group (@group)struct PipelineLayout(pub(crate) Arc<PlInner>)the bind group layouts and push constants of pipelinesstruct BindGroup(pub(crate) Arc<BgInner>)resources bound for shaders
impl Device
fn create_bind_group_layout(&self, bindings: &[Binding]) -> Result<BindGroupLayout>fn create_pipeline_layout(&self, desc: &PipelineLayoutDesc) -> Result<PipelineLayout>fn create_bind_group(&self, desc: &BindGroupDesc) -> Result<BindGroup>fn create_bindless(&self, desc: &BindlessDesc) -> Result<Bindless>(level Voll) a Bindless table: per binding a Metal 3 argument buffer (TableInner), written by the settersstruct Bindlessa large table of textures, samplers and buffers that shaders index (level Voll): binding 0 sampled textures, 1 samplers, 2 storage buffers, 3 storage textures, each a Metal 3 argument buffer. A slot may be changed while command buffers that use the table are pending, as long as those do not use the slot (as on Vulkan)
impl Bindless
fn group(&self) -> &BindGroupfn layout(&self) -> &BindGroupLayoutfn counts(&self) -> [u32; 4]fn set_texture(&self, slot: u32, view: &TextureView) -> Result<()>fn set_sampler(&self, slot: u32, s: &Sampler) -> Result<()>fn set_buffer(&self, slot: u32, buf: &Buffer, offset: u64, size: u64) -> Result<()>fn set_storage_texture(&self, slot: u32, view: &TextureView) -> Result<()>struct ComputePipeline(pub(crate) Arc<PipeInner>)a compute shader with its layoutstruct RenderPipeline(pub(crate) Arc<PipeInner>)vertex + fragment shaders with their fixed state and target formats
impl Device
fn create_compute_pipeline(&self, desc: &ComputePipelineDesc) -> Result<ComputePipeline>fn create_compute_pipeline_sized(&self, desc: &ComputePipelineDesc, size: u32) -> Result<ComputePipeline>create_compute_pipeline with a required subgroup size: Metal's SIMD groups have one size on Apple's GPUs (32), so only 0 (the driver's choice) and that size work; on an Intel Mac's GPU the driver picks it per pipeline (min and max of the limits differ), so only 0fn create_render_pipeline(&self, desc: &RenderPipelineDesc) -> Result<RenderPipeline>struct TimestampsGPU timestamps (not on Metal yet: Features::timestamps is false)
impl Device
fn create_timestamps(&self, _count: u32) -> Result<Timestamps>
impl Timestamps
fn count(&self) -> u32fn read_ticks(&self, _first: u32, _n: u32) -> Result<Vec<u64>>fn read_ms(&self, _first: u32, _n: u32) -> Result<Vec<f64>>