1
+ #![ no_std]
2
+
3
+ use spirv_std:: glam:: { Mat3 , Mat4 , Vec3 , Vec4 , Vec4Swizzles } ;
4
+ use spirv_std:: num_traits:: Float ;
5
+ use spirv_std:: spirv;
6
+
7
+ #[ repr( C ) ]
8
+ #[ derive( Copy , Clone ) ]
9
+ pub struct UBO {
10
+ pub projection : Mat4 ,
11
+ pub modelview : Mat4 ,
12
+ pub light_pos : Vec4 ,
13
+ }
14
+
15
+ #[ repr( C ) ]
16
+ #[ derive( Copy , Clone ) ]
17
+ pub struct PushConsts {
18
+ pub obj_pos : Vec3 ,
19
+ }
20
+
21
+ #[ spirv( vertex) ]
22
+ pub fn main_vs (
23
+ in_pos : Vec3 ,
24
+ in_normal : Vec3 ,
25
+ in_color : Vec3 ,
26
+ #[ spirv( uniform, descriptor_set = 0 , binding = 0 ) ] ubo : & UBO ,
27
+ #[ spirv( push_constant) ] push_consts : & PushConsts ,
28
+ #[ spirv( position) ] out_position : & mut Vec4 ,
29
+ out_normal : & mut Vec3 ,
30
+ out_color : & mut Vec3 ,
31
+ out_view_vec : & mut Vec3 ,
32
+ out_light_vec : & mut Vec3 ,
33
+ ) {
34
+ * out_normal = in_normal;
35
+ * out_color = in_color;
36
+
37
+ let _loc_pos = ( ubo. modelview * Vec4 :: from ( ( in_pos, 1.0 ) ) ) . xyz ( ) ;
38
+ let world_pos = ( ubo. modelview * Vec4 :: from ( ( in_pos + push_consts. obj_pos , 1.0 ) ) ) . xyz ( ) ;
39
+ * out_position = ubo. projection * Vec4 :: from ( ( world_pos, 1.0 ) ) ;
40
+
41
+ let pos = ubo. modelview * Vec4 :: from ( ( world_pos, 1.0 ) ) ;
42
+ * out_normal = Mat3 :: from_mat4 ( ubo. modelview ) * in_normal;
43
+ * out_light_vec = ubo. light_pos . xyz ( ) - pos. xyz ( ) ;
44
+ * out_view_vec = -pos. xyz ( ) ;
45
+ }
46
+
47
+ #[ spirv( tessellation_control( output_vertices = 3 ) ) ]
48
+ pub fn main_tcs (
49
+ #[ spirv( invocation_id) ] invocation_id : u32 ,
50
+ #[ spirv( position) ] in_position : [ Vec4 ; 3 ] ,
51
+ in_normal : [ Vec3 ; 3 ] ,
52
+ in_color : [ Vec3 ; 3 ] ,
53
+ in_view_vec : [ Vec3 ; 3 ] ,
54
+ in_light_vec : [ Vec3 ; 3 ] ,
55
+ #[ spirv( position) ] out_position : & mut [ Vec4 ; 3 ] ,
56
+ out_normal : & mut [ Vec3 ; 3 ] ,
57
+ out_color : & mut [ Vec3 ; 3 ] ,
58
+ out_view_vec : & mut [ Vec3 ; 3 ] ,
59
+ out_light_vec : & mut [ Vec3 ; 3 ] ,
60
+ #[ spirv( tess_level_inner) ] tess_level_inner : & mut [ f32 ; 2 ] ,
61
+ #[ spirv( tess_level_outer) ] tess_level_outer : & mut [ f32 ; 4 ] ,
62
+ ) {
63
+ if invocation_id == 0 {
64
+ tess_level_inner[ 0 ] = 2.0 ;
65
+ tess_level_outer[ 0 ] = 1.0 ;
66
+ tess_level_outer[ 1 ] = 1.0 ;
67
+ tess_level_outer[ 2 ] = 1.0 ;
68
+ }
69
+
70
+ let idx = invocation_id as usize ;
71
+ out_position[ idx] = in_position[ idx] ;
72
+ out_normal[ idx] = in_normal[ idx] ;
73
+ out_color[ idx] = in_color[ idx] ;
74
+ out_view_vec[ idx] = in_view_vec[ idx] ;
75
+ out_light_vec[ idx] = in_light_vec[ idx] ;
76
+ }
77
+
78
+ #[ spirv( tessellation_evaluation( triangles) ) ]
79
+ pub fn main_tes (
80
+ #[ spirv( tess_coord) ] tess_coord : Vec3 ,
81
+ #[ spirv( position) ] in_position : [ Vec4 ; 3 ] ,
82
+ in_normal : [ Vec3 ; 3 ] ,
83
+ in_color : [ Vec3 ; 3 ] ,
84
+ in_view_vec : [ Vec3 ; 3 ] ,
85
+ in_light_vec : [ Vec3 ; 3 ] ,
86
+ #[ spirv( position) ] out_position : & mut Vec4 ,
87
+ out_normal : & mut Vec3 ,
88
+ out_color : & mut Vec3 ,
89
+ out_view_vec : & mut Vec3 ,
90
+ out_light_vec : & mut Vec3 ,
91
+ ) {
92
+ * out_position = tess_coord. x * in_position[ 2 ] +
93
+ tess_coord. y * in_position[ 1 ] +
94
+ tess_coord. z * in_position[ 0 ] ;
95
+ * out_normal = tess_coord. x * in_normal[ 2 ] +
96
+ tess_coord. y * in_normal[ 1 ] +
97
+ tess_coord. z * in_normal[ 0 ] ;
98
+ * out_view_vec = tess_coord. x * in_view_vec[ 2 ] +
99
+ tess_coord. y * in_view_vec[ 1 ] +
100
+ tess_coord. z * in_view_vec[ 0 ] ;
101
+ * out_light_vec = tess_coord. x * in_light_vec[ 2 ] +
102
+ tess_coord. y * in_light_vec[ 1 ] +
103
+ tess_coord. z * in_light_vec[ 0 ] ;
104
+ * out_color = in_color[ 0 ] ;
105
+ }
106
+
107
+ #[ spirv( fragment) ]
108
+ pub fn main_fs (
109
+ in_normal : Vec3 ,
110
+ in_color : Vec3 ,
111
+ in_view_vec : Vec3 ,
112
+ in_light_vec : Vec3 ,
113
+ out_frag_color : & mut Vec4 ,
114
+ ) {
115
+ let n = in_normal. normalize ( ) ;
116
+ let l = in_light_vec. normalize ( ) ;
117
+ let v = in_view_vec. normalize ( ) ;
118
+ let r = ( -l) . reflect ( n) ;
119
+ let diffuse = n. dot ( l) . max ( 0.0 ) * in_color;
120
+ let specular = r. dot ( v) . max ( 0.0 ) . powi ( 8 ) * Vec3 :: splat ( 0.75 ) ;
121
+ * out_frag_color = Vec4 :: from ( ( diffuse + specular, 0.5 ) ) ;
122
+ }
0 commit comments