Debian Codegraph @

third_party/rust/metal/examples/window/shaders.metal

raw
1 #include <metal_stdlib>
2
3 using namespace metal;
4
5 typedef struct {
6 packed_float2 position;
7 packed_float3 color;
8 } vertex_t;
9
10 struct ColorInOut {
11 float4 position [[position]];
12 float4 color;
13 };
14 // vertex shader function
15 vertex ColorInOut triangle_vertex(const device vertex_t* vertex_array [[ buffer(0) ]],
16 unsigned int vid [[ vertex_id ]])
17 {
18 ColorInOut out;
19
20 auto device const &v = vertex_array[vid];
21 out.position = float4(v.position.x, v.position.y, 0.0, 1.0);
22 out.color = float4(v.color.x, v.color.y, v.color.z, 0.2);
23
24 return out;
25 }
26
27 // fragment shader function
28 fragment float4 triangle_fragment(ColorInOut in [[stage_in]])
29 {
30 return in.color;
31 };
32
33
34 struct Rect {
35 float x;
36 float y;
37 float w;
38 float h;
39 };
40
41 struct Color {
42 float r;
43 float g;
44 float b;
45 float a;
46 };
47
48 struct ClearRect {
49 Rect rect;
50 Color color;
51 };
52
53 float2 rect_vert(
54 Rect rect,
55 uint vid
56 ) {
57 float2 pos;
58
59 float left = rect.x;
60 float right = rect.x + rect.w;
61 float bottom = rect.y;
62 float top = rect.y + rect.h;
63
64 switch (vid) {
65 case 0:
66 pos = float2(right, top);
67 break;
68 case 1:
69 pos = float2(left, top);
70 break;
71 case 2:
72 pos = float2(right, bottom);
73 break;
74 case 3:
75 pos = float2(left, bottom);
76 break;
77 }
78 return pos;
79 }
80
81 vertex ColorInOut clear_rect_vertex(
82 const device ClearRect *clear_rect [[ buffer(0) ]],
83 unsigned int vid [[ vertex_id ]]
84 ) {
85 ColorInOut out;
86 float4 pos = float4(rect_vert(clear_rect->rect, vid), 0, 1);
87 auto col = clear_rect->color;
88
89 out.position = pos;
90 out.color = float4(col.r, col.g, col.b, col.a);
91 return out;
92 }
93
94 fragment float4 clear_rect_fragment(ColorInOut in [[stage_in]])
95 {
96 return in.color;
97 };