summaryrefslogtreecommitdiffstats
path: root/third_party/rust/metal/src/indirect_encoder.rs
blob: e434d348f207cd7379dd17d11c3199dd7c7d2133 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use super::*;

bitflags! {
    /// See <https://developer.apple.com/documentation/metal/mtlindirectcommandtype/>
    #[allow(non_upper_case_globals)]
    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
    pub struct MTLIndirectCommandType: NSUInteger {
        const Draw                      = 1 << 0;
        const DrawIndexed               = 1 << 1;
        const DrawPatches               = 1 << 2;
        const DrawIndexedPatches        = 1 << 3;
        const ConcurrentDispatch        = 1 << 4;
        const ConcurrentDispatchThreads = 1 << 5;
    }
}

/// See <https://developer.apple.com/documentation/metal/mtlindirectcommandbufferdescriptor/>
pub enum MTLIndirectCommandBufferDescriptor {}

foreign_obj_type! {
    type CType = MTLIndirectCommandBufferDescriptor;
    pub struct IndirectCommandBufferDescriptor;
}

impl IndirectCommandBufferDescriptor {
    pub fn new() -> Self {
        let class = class!(MTLIndirectCommandBufferDescriptor);
        unsafe { msg_send![class, new] }
    }
}

impl IndirectCommandBufferDescriptorRef {
    pub fn command_types(&self) -> MTLIndirectCommandType {
        unsafe { msg_send![self, commandTypes] }
    }

    pub fn set_command_types(&self, types: MTLIndirectCommandType) {
        unsafe { msg_send![self, setCommandTypes: types] }
    }

    pub fn inherit_buffers(&self) -> bool {
        unsafe { msg_send_bool![self, inheritBuffers] }
    }

    pub fn set_inherit_buffers(&self, inherit: bool) {
        unsafe { msg_send![self, setInheritBuffers: inherit] }
    }

    pub fn inherit_pipeline_state(&self) -> bool {
        unsafe { msg_send_bool![self, inheritPipelineState] }
    }

    pub fn set_inherit_pipeline_state(&self, inherit: bool) {
        unsafe { msg_send![self, setInheritPipelineState: inherit] }
    }

    pub fn max_vertex_buffer_bind_count(&self) -> NSUInteger {
        unsafe { msg_send![self, maxVertexBufferBindCount] }
    }

    pub fn set_max_vertex_buffer_bind_count(&self, count: NSUInteger) {
        unsafe { msg_send![self, setMaxVertexBufferBindCount: count] }
    }

    pub fn max_fragment_buffer_bind_count(&self) -> NSUInteger {
        unsafe { msg_send![self, maxFragmentBufferBindCount] }
    }

    pub fn set_max_fragment_buffer_bind_count(&self, count: NSUInteger) {
        unsafe { msg_send![self, setMaxFragmentBufferBindCount: count] }
    }

    pub fn max_kernel_buffer_bind_count(&self) -> NSUInteger {
        unsafe { msg_send![self, maxKernelBufferBindCount] }
    }

    pub fn set_max_kernel_buffer_bind_count(&self, count: NSUInteger) {
        unsafe { msg_send![self, setMaxKernelBufferBindCount: count] }
    }
}

/// See <https://developer.apple.com/documentation/metal/mtlindirectcommandbuffer/>
pub enum MTLIndirectCommandBuffer {}

foreign_obj_type! {
    type CType = MTLIndirectCommandBuffer;
    pub struct IndirectCommandBuffer;
    type ParentType = Resource;
}

impl IndirectCommandBufferRef {
    pub fn size(&self) -> NSUInteger {
        unsafe { msg_send![self, size] }
    }

    pub fn indirect_render_command_at_index(&self, index: NSUInteger) -> &IndirectRenderCommandRef {
        unsafe { msg_send![self, indirectRenderCommandAtIndex: index] }
    }

    pub fn indirect_compute_command_at_index(
        &self,
        index: NSUInteger,
    ) -> &IndirectComputeCommandRef {
        unsafe { msg_send![self, indirectComputeCommandAtIndex: index] }
    }

    pub fn reset_with_range(&self, range: crate::NSRange) {
        unsafe { msg_send![self, resetWithRange: range] }
    }
}

/// See <https://developer.apple.com/documentation/metal/mtlindirectrendercommand/>
pub enum MTLIndirectRenderCommand {}

foreign_obj_type! {
    type CType = MTLIndirectRenderCommand;
    pub struct IndirectRenderCommand;
}

impl IndirectRenderCommandRef {
    pub fn set_render_pipeline_state(&self, pipeline_state: &RenderPipelineStateRef) {
        unsafe { msg_send![self, setRenderPipelineState: pipeline_state] }
    }

    pub fn set_vertex_buffer(
        &self,
        index: NSUInteger,
        buffer: Option<&BufferRef>,
        offset: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                setVertexBuffer: buffer
                offset: offset
                atIndex: index
            ]
        }
    }

    pub fn set_fragment_buffer(
        &self,
        index: NSUInteger,
        buffer: Option<&BufferRef>,
        offset: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                setFragmentBuffer:buffer
                offset:offset
                atIndex:index
            ]
        }
    }

    pub fn draw_primitives(
        &self,
        primitive_type: MTLPrimitiveType,
        vertex_start: NSUInteger,
        vertex_count: NSUInteger,
        instance_count: NSUInteger,
        base_instance: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                drawPrimitives: primitive_type
                vertexStart: vertex_start
                vertexCount: vertex_count
                instanceCount: instance_count
                baseInstance: base_instance
            ]
        }
    }

    pub fn draw_indexed_primitives(
        &self,
        primitive_type: MTLPrimitiveType,
        index_count: NSUInteger,
        index_type: MTLIndexType,
        index_buffer: &BufferRef,
        index_buffer_offset: NSUInteger,
        instance_count: NSUInteger,
        base_vertex: NSUInteger,
        base_instance: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                drawIndexedPrimitives: primitive_type
                indexCount: index_count
                indexType: index_type
                indexBuffer: index_buffer
                indexBufferOffset: index_buffer_offset
                instanceCount: instance_count
                baseVertex: base_vertex
                baseInstance: base_instance
            ]
        }
    }

    pub fn draw_patches(
        &self,
        number_of_patch_control_points: NSUInteger,
        patch_start: NSUInteger,
        patch_count: NSUInteger,
        patch_index_buffer: &BufferRef,
        patch_index_buffer_offset: NSUInteger,
        instance_count: NSUInteger,
        base_instance: NSUInteger,
        tesselation_factor_buffer: &BufferRef,
        tesselation_factor_buffer_offset: NSUInteger,
        tesselation_factor_buffer_instance_stride: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                drawPatches: number_of_patch_control_points
                patchStart: patch_start
                patchCount: patch_count
                patchIndexBuffer: patch_index_buffer
                patchIndexBufferOffset: patch_index_buffer_offset
                instanceCount: instance_count
                baseInstance: base_instance
                tessellationFactorBuffer: tesselation_factor_buffer
                tessellationFactorBufferOffset: tesselation_factor_buffer_offset
                tessellationFactorBufferInstanceStride: tesselation_factor_buffer_instance_stride
            ]
        }
    }

    pub fn draw_indexed_patches(
        &self,
        number_of_patch_control_points: NSUInteger,
        patch_start: NSUInteger,
        patch_count: NSUInteger,
        patch_index_buffer: &BufferRef,
        patch_index_buffer_offset: NSUInteger,
        control_point_index_buffer: &BufferRef,
        control_point_index_buffer_offset: NSUInteger,
        instance_count: NSUInteger,
        base_instance: NSUInteger,
        tesselation_factor_buffer: &BufferRef,
        tesselation_factor_buffer_offset: NSUInteger,
        tesselation_factor_buffer_instance_stride: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                drawIndexedPatches: number_of_patch_control_points
                patchStart: patch_start
                patchCount: patch_count
                patchIndexBuffer: patch_index_buffer
                patchIndexBufferOffset: patch_index_buffer_offset
                controlPointIndexBuffer: control_point_index_buffer
                controlPointIndexBufferOffset: control_point_index_buffer_offset
                instanceCount: instance_count
                baseInstance: base_instance
                tessellationFactorBuffer: tesselation_factor_buffer
                tessellationFactorBufferOffset: tesselation_factor_buffer_offset
                tessellationFactorBufferInstanceStride: tesselation_factor_buffer_instance_stride
            ]
        }
    }

    pub fn reset(&self) {
        unsafe { msg_send![self, reset] }
    }
}

/// See <https://developer.apple.com/documentation/metal/mtlindirectcomputecommand/>
pub enum MTLIndirectComputeCommand {}

foreign_obj_type! {
    type CType = MTLIndirectComputeCommand;
    pub struct IndirectComputeCommand;
}

impl IndirectComputeCommandRef {
    pub fn set_compute_pipeline_state(&self, state: &ComputePipelineStateRef) {
        unsafe { msg_send![self, setComputePipelineState: state] }
    }

    pub fn set_kernel_buffer(
        &self,
        index: NSUInteger,
        buffer: Option<&BufferRef>,
        offset: NSUInteger,
    ) {
        unsafe {
            msg_send![self,
                setKernelBuffer: buffer
                offset: offset
                atIndex: index
            ]
        }
    }

    pub fn set_threadgroup_memory_length(&self, index: NSUInteger, length: NSUInteger) {
        unsafe {
            msg_send![self,
                setThreadgroupMemoryLength: length
                atIndex: index
            ]
        }
    }

    pub fn set_stage_in_region(&self, region: MTLRegion) {
        unsafe { msg_send![self, setStageInRegion: region] }
    }

    pub fn set_barrier(&self) {
        unsafe { msg_send![self, setBarrier] }
    }

    pub fn clear_barrier(&self) {
        unsafe { msg_send![self, clearBarrier] }
    }

    pub fn concurrent_dispatch_threadgroups(
        &self,
        thread_groups_per_grid: MTLSize,
        threads_per_threadgroup: MTLSize,
    ) {
        unsafe {
            msg_send![self,
                concurrentDispatchThreadgroups: thread_groups_per_grid
                threadsPerThreadgroup: threads_per_threadgroup
            ]
        }
    }

    pub fn concurrent_dispatch_threads(
        &self,
        thread_groups_per_grid: MTLSize,
        threads_per_threadgroup: MTLSize,
    ) {
        unsafe {
            msg_send![self,
                concurrentDispatchThreads: thread_groups_per_grid
                threadsPerThreadgroup: threads_per_threadgroup
            ]
        }
    }

    pub fn reset(&self) {
        unsafe { msg_send![self, reset] }
    }
}