1+ #![ cfg_attr( target_arch = "spirv" , no_std) ]
2+ #![ allow( clippy:: missing_safety_doc) ]
3+
4+ use spirv_std:: { spirv, glam:: { mat3, vec3, vec4, Mat4 , Vec2 , Vec3 , Vec4 } , Image , num_traits:: Float } ;
5+ use spirv_std:: image:: SampledImage ;
6+
7+ #[ repr( C ) ]
8+ #[ derive( Copy , Clone ) ]
9+ pub struct UboScene {
10+ pub projection : Mat4 ,
11+ pub view : Mat4 ,
12+ pub light_pos : Vec4 ,
13+ pub view_pos : Vec4 ,
14+ }
15+
16+ #[ repr( C ) ]
17+ #[ derive( Copy , Clone ) ]
18+ pub struct PushConsts {
19+ pub model : Mat4 ,
20+ }
21+
22+ #[ spirv( vertex) ]
23+ pub fn main_vs (
24+ in_pos : Vec3 ,
25+ in_normal : Vec3 ,
26+ in_uv : Vec2 ,
27+ in_color : Vec3 ,
28+ #[ spirv( uniform, descriptor_set = 0 , binding = 0 ) ] ubo_scene : & UboScene ,
29+ #[ spirv( push_constant) ] push_consts : & PushConsts ,
30+ #[ spirv( position) ] out_position : & mut Vec4 ,
31+ out_normal : & mut Vec3 ,
32+ out_color : & mut Vec3 ,
33+ out_uv : & mut Vec2 ,
34+ out_view_vec : & mut Vec3 ,
35+ out_light_vec : & mut Vec3 ,
36+ ) {
37+ * out_normal = in_normal;
38+ * out_color = in_color;
39+ * out_uv = in_uv;
40+ * out_position = ubo_scene. projection * ubo_scene. view * push_consts. model * vec4 ( in_pos. x , in_pos. y , in_pos. z , 1.0 ) ;
41+
42+ let pos = ubo_scene. view * push_consts. model * vec4 ( in_pos. x , in_pos. y , in_pos. z , 1.0 ) ;
43+ let view_mat3 = mat3 (
44+ ubo_scene. view . x_axis . truncate ( ) ,
45+ ubo_scene. view . y_axis . truncate ( ) ,
46+ ubo_scene. view . z_axis . truncate ( ) ,
47+ ) ;
48+ * out_normal = view_mat3 * in_normal;
49+ * out_light_vec = ubo_scene. light_pos . truncate ( ) - pos. truncate ( ) ;
50+ * out_view_vec = ubo_scene. view_pos . truncate ( ) - pos. truncate ( ) ;
51+ }
52+
53+ #[ spirv( fragment) ]
54+ pub fn main_fs (
55+ in_normal : Vec3 ,
56+ in_color : Vec3 ,
57+ in_uv : Vec2 ,
58+ in_view_vec : Vec3 ,
59+ in_light_vec : Vec3 ,
60+ #[ spirv( descriptor_set = 1 , binding = 0 ) ] sampler_color_map : & SampledImage < Image ! ( 2 D , type =f32 , sampled) > ,
61+ out_frag_color : & mut Vec4 ,
62+ ) {
63+ let color = sampler_color_map. sample ( in_uv) * vec4 ( in_color. x , in_color. y , in_color. z , 1.0 ) ;
64+
65+ let n = in_normal. normalize ( ) ;
66+ let l = in_light_vec. normalize ( ) ;
67+ let v = in_view_vec. normalize ( ) ;
68+ let r = l. reflect ( n) ;
69+ let diffuse = n. dot ( l) . max ( 0.15 ) * in_color;
70+ let specular = v. dot ( r) . max ( 0.0 ) . powf ( 16.0 ) * vec3 ( 0.75 , 0.75 , 0.75 ) ;
71+ * out_frag_color = vec4 ( diffuse. x * color. x + specular. x , diffuse. y * color. y + specular. y , diffuse. z * color. z + specular. z , 1.0 ) ;
72+ }
0 commit comments