lib/sys/src/probe/apple/abi/metal.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const sys = @import("sys");
 2 
 3 const dispatch = sys.apple.dispatch;
 4 const foundation = sys.apple.foundation;
 5 const metal = sys.apple.metal;
 6 const objc = sys.apple.objc;
 7 
 8 export fn sys_metal_abi_dispatch_threadgroups(
 9     encoder: metal.ComputeCommandEncoder,
10     threadgroups: metal.Size,
11     threads_per_threadgroup: metal.Size,
12 ) callconv(.c) void {
13     metal.dispatchThreadgroups(encoder, threadgroups, threads_per_threadgroup);
14 }
15 
16 export fn sys_metal_abi_copied_data(bytes: [*]const u8, len: usize) callconv(.c) ?dispatch.Data {
17     return dispatch.copiedData(bytes[0..len]);
18 }
19 
20 export fn sys_metal_abi_library_source(
21     device: metal.Device,
22     source: foundation.String,
23     out_error: *?foundation.Error,
24 ) callconv(.c) ?metal.Library {
25     return metal.newLibraryWithSource(device, source, null, out_error);
26 }
27 
28 export fn sys_metal_abi_objects(
29     device: metal.Device,
30     queue: metal.CommandQueue,
31     command_buffer: metal.CommandBuffer,
32     encoder: metal.ComputeCommandEncoder,
33     event: metal.SharedEvent,
34     buffer: metal.Buffer,
35     library: metal.Library,
36     name: foundation.String,
37     pipeline: metal.ComputePipelineState,
38     data: dispatch.Data,
39     out_error: *?foundation.Error,
40 ) callconv(.c) void {
41     const default_device = metal.systemDefaultDevice();
42     const command_queue = metal.newCommandQueue(device);
43     const shared_event = metal.newSharedEvent(device);
44     const device_buffer = metal.newBuffer(device, 64);
45     const data_library = metal.newLibraryWithData(device, data, out_error);
46     const function = metal.newFunction(library, name);
47     const compiled_pipeline = if (function) |value|
48         metal.newComputePipelineState(device, value, out_error)
49     else
50         null;
51     const queued_buffer = metal.commandBuffer(queue);
52     const compute_encoder = metal.computeCommandEncoder(command_buffer);
53     metal.setSignaledValue(event, 0);
54     _ = metal.signaledValue(event);
55     metal.encodeSignalEvent(command_buffer, event, 1);
56     metal.encodeWaitForEvent(command_buffer, event, 1);
57     metal.setComputePipelineState(encoder, pipeline);
58     metal.setBuffer(encoder, buffer, 0, 0);
59     var scalar: u32 = 1;
60     metal.setBytes(encoder, &scalar, @sizeOf(u32), 1);
61     metal.endEncoding(encoder);
62     metal.commit(command_buffer);
63     metal.waitUntilCompleted(command_buffer);
64     _ = metal.commandBufferStatus(command_buffer);
65     _ = metal.commandBufferError(command_buffer);
66     _ = metal.bufferContents(buffer);
67     if (default_device) |value| objc.release(value);
68     if (command_queue) |value| objc.release(value);
69     if (shared_event) |value| objc.release(value);
70     if (device_buffer) |value| objc.release(value);
71     if (data_library) |value| objc.release(value);
72     if (function) |value| objc.release(value);
73     if (compiled_pipeline) |value| objc.release(value);
74     _ = compute_encoder;
75     _ = queued_buffer;
76 }