Debian Codegraph @

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

raw
1 // Copyright 2016 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 PROGRAM: &str = r"
12 #include <metal_stdlib>
13
14 using namespace metal;
15
16 typedef struct {
17 float2 position;
18 float3 color;
19 } vertex_t;
20
21 struct ColorInOut {
22 float4 position [[position]];
23 float4 color;
24 };
25
26 vertex ColorInOut vs(device vertex_t* vertex_array [[ buffer(0) ]],
27 unsigned int vid [[ vertex_id ]])
28 {
29 ColorInOut out;
30
31 out.position = float4(float2(vertex_array[vid].position), 0.0, 1.0);
32 out.color = float4(float3(vertex_array[vid].color), 1.0);
33
34 return out;
35 }
36
37 fragment float4 ps(ColorInOut in [[stage_in]])
38 {
39 return in.color;
40 };
41 ";
42
43 fn main() {
44 autoreleasepool(|| {
45 let device = Device::system_default().expect("no device found");
46
47 let options = CompileOptions::new();
48 let library = device.new_library_with_source(PROGRAM, &options).unwrap();
49 let (vs, ps) = (
50 library.get_function("vs", None).unwrap(),
51 library.get_function("ps", None).unwrap(),
52 );
53
54 let vertex_desc = VertexDescriptor::new();
55
56 let desc = RenderPipelineDescriptor::new();
57 desc.set_vertex_function(Some(&vs));
58 desc.set_fragment_function(Some(&ps));
59 desc.set_vertex_descriptor(Some(vertex_desc));
60
61 println!("{:?}", desc);
62
63 let reflect_options = MTLPipelineOption::ArgumentInfo | MTLPipelineOption::BufferTypeInfo;
64 let (_, reflection) = device
65 .new_render_pipeline_state_with_reflection(&desc, reflect_options)
66 .unwrap();
67
68 println!("Vertex arguments: ");
69 let vertex_arguments = reflection.vertex_arguments();
70 for index in 0..vertex_arguments.count() {
71 let argument = vertex_arguments.object_at(index).unwrap();
72 println!("{:?}", argument);
73 }
74 });
75 }