1
1
#![ cfg_attr( target_arch = "spirv" , no_std) ]
2
2
#![ allow( clippy:: missing_safety_doc) ]
3
3
4
- use spirv_std:: { spirv, glam:: { vec2, vec4, Vec2 , Vec4 , UVec3 } , Image , num_traits:: Float } ;
5
4
use spirv_std:: image:: SampledImage ;
5
+ use spirv_std:: {
6
+ glam:: { vec2, vec4, UVec3 , Vec2 , Vec4 } ,
7
+ num_traits:: Float ,
8
+ spirv, Image ,
9
+ } ;
6
10
7
11
#[ repr( C ) ]
8
12
#[ derive( Copy , Clone ) ]
@@ -39,17 +43,19 @@ fn repulsion(pos: Vec2, attract_pos: Vec2) -> Vec2 {
39
43
#[ spirv( compute( threads( 256 , 1 , 1 ) ) ) ]
40
44
pub fn main_cs (
41
45
#[ spirv( global_invocation_id) ] global_id : UVec3 ,
42
- #[ spirv( storage_buffer, descriptor_set = 0 , binding = 0 ) ] particles : & mut [ Particle ] ,
43
- #[ spirv( uniform, descriptor_set = 0 , binding = 1 ) ] ubo : & Ubo ,
46
+ #[ spirv( storage_buffer, descriptor_set = 0 , binding = 0 ) ] particles_in : & [ Particle ] ,
47
+ #[ spirv( storage_buffer, descriptor_set = 0 , binding = 1 ) ] particles_out : & mut [ Particle ] ,
48
+ #[ spirv( uniform, descriptor_set = 0 , binding = 2 ) ] ubo : & Ubo ,
44
49
) {
45
50
let index = global_id. x ;
46
51
if index >= ubo. particle_count as u32 {
47
52
return ;
48
53
}
49
54
50
55
let idx = index as usize ;
51
- let mut vel = particles[ idx] . vel ;
52
- let mut pos = particles[ idx] . pos ;
56
+ let mut vel = particles_in[ idx] . vel ;
57
+ let mut pos = particles_in[ idx] . pos ;
58
+ let g_pos = particles_in[ idx] . gradient_pos ;
53
59
54
60
let dest_pos = vec2 ( ubo. dest_x , ubo. dest_y ) ;
55
61
@@ -62,13 +68,13 @@ pub fn main_cs(
62
68
if pos. x < -1.0 || pos. x > 1.0 || pos. y < -1.0 || pos. y > 1.0 {
63
69
vel = ( -vel * 0.1 ) + attraction ( pos, dest_pos) * 12.0 ;
64
70
} else {
65
- particles [ idx] . pos = pos;
71
+ particles_out [ idx] . pos = pos;
66
72
}
67
73
68
- particles [ idx] . vel = vel;
69
- particles [ idx] . gradient_pos . x += 0.02 * ubo. delta_t ;
70
- if particles [ idx] . gradient_pos . x > 1.0 {
71
- particles [ idx] . gradient_pos . x -= 1.0 ;
74
+ particles_out [ idx] . vel = vel;
75
+ particles_out [ idx] . gradient_pos . x = g_pos . x + 0.02 * ubo. delta_t ;
76
+ if particles_out [ idx] . gradient_pos . x > 1.0 {
77
+ particles_out [ idx] . gradient_pos . x -= 1.0 ;
72
78
}
73
79
}
74
80
@@ -92,11 +98,22 @@ pub fn main_fs(
92
98
_in_color : Vec4 ,
93
99
in_gradient_pos : f32 ,
94
100
#[ spirv( point_coord) ] point_coord : Vec2 ,
95
- #[ spirv( descriptor_set = 0 , binding = 0 ) ] sampler_color_map : & SampledImage < Image ! ( 2 D , type =f32 , sampled) > ,
96
- #[ spirv( descriptor_set = 0 , binding = 1 ) ] sampler_gradient_ramp : & SampledImage < Image ! ( 2 D , type =f32 , sampled) > ,
101
+ #[ spirv( descriptor_set = 0 , binding = 0 ) ] sampler_color_map : & SampledImage <
102
+ Image ! ( 2 D , type =f32 , sampled) ,
103
+ > ,
104
+ #[ spirv( descriptor_set = 0 , binding = 1 ) ] sampler_gradient_ramp : & SampledImage <
105
+ Image ! ( 2 D , type =f32 , sampled) ,
106
+ > ,
97
107
out_frag_color : & mut Vec4 ,
98
108
) {
99
- let color = sampler_gradient_ramp. sample ( vec2 ( in_gradient_pos, 0.0 ) ) . truncate ( ) ;
109
+ let color = sampler_gradient_ramp
110
+ . sample ( vec2 ( in_gradient_pos, 0.0 ) )
111
+ . truncate ( ) ;
100
112
let tex_color = sampler_color_map. sample ( point_coord) . truncate ( ) ;
101
- * out_frag_color = vec4 ( tex_color. x * color. x , tex_color. y * color. y , tex_color. z * color. z , 1.0 ) ;
102
- }
113
+ * out_frag_color = vec4 (
114
+ tex_color. x * color. x ,
115
+ tex_color. y * color. y ,
116
+ tex_color. z * color. z ,
117
+ 1.0 ,
118
+ ) ;
119
+ }
0 commit comments