Debian Codegraph @

third_party/rust/metal/examples/bindless/main.rs

raw
1 // Copyright 2017 GFX developers
2 //
3 // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 // http://opensource.org/licenses/MIT>, at your option. This file may not be
6 // copied, modified, or distributed except according to those terms.
7
8 use metal::*;
9 use objc::rc::autoreleasepool;
10
11 const BINDLESS_TEXTURE_COUNT: NSUInteger = 100_000; // ~25Mb
12
13 /// This example demonstrates:
14 /// - How to create a heap
15 /// - How to allocate textures from heap.
16 /// - How to create bindless resources via Metal's argument buffers.
17 /// - How to bind argument buffer to render encoder
18 fn main() {
19 autoreleasepool(|| {
20 let device = Device::system_default().expect("no device found");
21
22 /*
23
24 MSL
25
26 struct Textures {
27 texture2d<float> texture;
28 };
29 struct BindlessTextures {
30 device Textures *textures;
31 };
32
33 */
34
35 // Tier 2 argument buffers are supported by macOS devices with a discrete GPU and by the A13 GPU.
36 // The maximum per-app resources available at any given time are:
37 // - 500,000 buffers or textures
38 // - 2048 unique samplers
39 let tier = device.argument_buffers_support();
40 println!("Argument buffer support: {:?}", tier);
41 assert_eq!(MTLArgumentBuffersTier::Tier2, tier);
42
43 let texture_descriptor = TextureDescriptor::new();
44 texture_descriptor.set_width(1);
45 texture_descriptor.set_height(1);
46 texture_descriptor.set_depth(1);
47 texture_descriptor.set_texture_type(MTLTextureType::D2);
48 texture_descriptor.set_pixel_format(MTLPixelFormat::R8Uint);
49 texture_descriptor.set_storage_mode(MTLStorageMode::Private); // GPU only.
50 println!("Texture descriptor: {:?}", texture_descriptor);
51
52 // Determine the size required for the heap for the given descriptor
53 let size_and_align = device.heap_texture_size_and_align(&texture_descriptor);
54
55 // Align the size so that more resources will fit in the heap after this texture
56 // See https://developer.apple.com/documentation/metal/buffers/using_argument_buffers_with_resource_heaps
57 let texture_size =
58 (size_and_align.size & (size_and_align.align - 1)) + size_and_align.align;
59 let heap_size = texture_size * BINDLESS_TEXTURE_COUNT;
60
61 let heap_descriptor = HeapDescriptor::new();
62 heap_descriptor.set_storage_mode(texture_descriptor.storage_mode()); // Must be compatible
63 heap_descriptor.set_size(heap_size);
64 println!("Heap descriptor: {:?}", heap_descriptor);
65
66 let heap = device.new_heap(&heap_descriptor);
67 println!("Heap: {:?}", heap);
68
69 // Allocate textures from heap
70 let textures = (0..BINDLESS_TEXTURE_COUNT)
71 .map(|i| {
72 heap.new_texture(&texture_descriptor)
73 .unwrap_or_else(|| panic!("Failed to allocate texture {}", i))
74 })
75 .collect::<Vec<_>>();
76
77 // Crate argument encoder that knows how to encode single texture
78 let descriptor = ArgumentDescriptor::new();
79 descriptor.set_index(0);
80 descriptor.set_data_type(MTLDataType::Texture);
81 descriptor.set_texture_type(MTLTextureType::D2);
82 descriptor.set_access(MTLArgumentAccess::ReadOnly);
83 println!("Argument descriptor: {:?}", descriptor);
84
85 let encoder = device.new_argument_encoder(Array::from_slice(&[descriptor]));
86 println!("Encoder: {:?}", encoder);
87
88 // Determinate argument buffer size to allocate.
89 // Size needed to encode one texture * total number of bindless textures.
90 let argument_buffer_size = encoder.encoded_length() * BINDLESS_TEXTURE_COUNT;
91 let argument_buffer = device.new_buffer(argument_buffer_size, MTLResourceOptions::empty());
92
93 // Encode textures to the argument buffer.
94 textures.iter().enumerate().for_each(|(index, texture)| {
95 // Offset encoder to a proper texture slot
96 let offset = index as NSUInteger * encoder.encoded_length();
97 encoder.set_argument_buffer(&argument_buffer, offset);
98 encoder.set_texture(0, texture);
99 });
100
101 // How to use bindless argument buffer when drawing
102
103 let queue = device.new_command_queue();
104 let command_buffer = queue.new_command_buffer();
105
106 let render_pass_descriptor = RenderPassDescriptor::new();
107 let encoder = command_buffer.new_render_command_encoder(render_pass_descriptor);
108
109 // Bind argument buffer.
110 encoder.set_fragment_buffer(0, Some(&argument_buffer), 0);
111 // Make sure all textures are available to the pass.
112 encoder.use_heap_at(&heap, MTLRenderStages::Fragment);
113
114 // Bind material buffer at index 1
115 // Draw
116
117 /*
118
119 // Now instead of binding individual textures each draw call,
120 // you can just bind material information instead:
121
122 MSL
123
124 struct Material {
125 int diffuse_texture_index;
126 int normal_texture_index;
127 // ...
128 }
129
130 fragment float4 pixel(
131 VertexOut v [[stage_in]],
132 constant const BindlessTextures * textures [[buffer(0)]],
133 constant Material * material [[buffer(1)]]
134 ) {
135 if (material->base_color_texture_index != -1) {
136 textures[material->diffuse_texture_index].texture.sampler(...)
137 }
138 if (material->normal_texture_index != -1) {
139 ...
140 }
141 ...
142 }
143
144 */
145
146 encoder.end_encoding();
147 command_buffer.commit();
148 });
149 }