Skip to content

Commit a33c6af

Browse files
committed
Update rust shaders to work with latest upstream main
1 parent 14fb52b commit a33c6af

File tree

6 files changed

+74
-33
lines changed

6 files changed

+74
-33
lines changed
296 Bytes
Binary file not shown.

shaders/rust/computeparticles/particle/src/lib.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#![cfg_attr(target_arch = "spirv", no_std)]
22
#![allow(clippy::missing_safety_doc)]
33

4-
use spirv_std::{spirv, glam::{vec2, vec4, Vec2, Vec4, UVec3}, Image, num_traits::Float};
54
use spirv_std::image::SampledImage;
5+
use spirv_std::{
6+
glam::{vec2, vec4, UVec3, Vec2, Vec4},
7+
num_traits::Float,
8+
spirv, Image,
9+
};
610

711
#[repr(C)]
812
#[derive(Copy, Clone)]
@@ -39,17 +43,19 @@ fn repulsion(pos: Vec2, attract_pos: Vec2) -> Vec2 {
3943
#[spirv(compute(threads(256, 1, 1)))]
4044
pub fn main_cs(
4145
#[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,
4449
) {
4550
let index = global_id.x;
4651
if index >= ubo.particle_count as u32 {
4752
return;
4853
}
4954

5055
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;
5359

5460
let dest_pos = vec2(ubo.dest_x, ubo.dest_y);
5561

@@ -62,13 +68,13 @@ pub fn main_cs(
6268
if pos.x < -1.0 || pos.x > 1.0 || pos.y < -1.0 || pos.y > 1.0 {
6369
vel = (-vel * 0.1) + attraction(pos, dest_pos) * 12.0;
6470
} else {
65-
particles[idx].pos = pos;
71+
particles_out[idx].pos = pos;
6672
}
6773

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;
7278
}
7379
}
7480

@@ -92,11 +98,22 @@ pub fn main_fs(
9298
_in_color: Vec4,
9399
in_gradient_pos: f32,
94100
#[spirv(point_coord)] point_coord: Vec2,
95-
#[spirv(descriptor_set = 0, binding = 0)] sampler_color_map: &SampledImage<Image!(2D, type=f32, sampled)>,
96-
#[spirv(descriptor_set = 0, binding = 1)] sampler_gradient_ramp: &SampledImage<Image!(2D, type=f32, sampled)>,
101+
#[spirv(descriptor_set = 0, binding = 0)] sampler_color_map: &SampledImage<
102+
Image!(2D, type=f32, sampled),
103+
>,
104+
#[spirv(descriptor_set = 0, binding = 1)] sampler_gradient_ramp: &SampledImage<
105+
Image!(2D, type=f32, sampled),
106+
>,
97107
out_frag_color: &mut Vec4,
98108
) {
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();
100112
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+
}

shaders/rust/offscreen/mirror/src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#![cfg_attr(target_arch = "spirv", no_std)]
22
#![allow(clippy::missing_safety_doc)]
33

4-
use spirv_std::{spirv, glam::{vec2, vec4, Mat4, Vec3, Vec4}, Image};
54
use spirv_std::image::SampledImage;
5+
use spirv_std::{
6+
glam::{vec2, vec4, Mat4, Vec3, Vec4},
7+
spirv, Image,
8+
};
69

710
#[repr(C)]
811
#[derive(Copy, Clone)]
@@ -28,22 +31,29 @@ pub fn main_vs(
2831
pub fn main_fs(
2932
in_pos: Vec4,
3033
#[spirv(front_facing)] front_facing: bool,
31-
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<Image!(2D, type=f32, sampled)>,
34+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<
35+
Image!(2D, type=f32, sampled),
36+
>,
3237
out_frag_color: &mut Vec4,
3338
) {
34-
let tmp = vec4(1.0 / in_pos.w, 1.0 / in_pos.w, 1.0 / in_pos.w, 1.0 / in_pos.w);
39+
let tmp = vec4(
40+
1.0 / in_pos.w,
41+
1.0 / in_pos.w,
42+
1.0 / in_pos.w,
43+
1.0 / in_pos.w,
44+
);
3545
let proj_coord = in_pos * tmp;
36-
46+
3747
// Scale and bias
3848
let proj_coord = proj_coord + vec4(1.0, 1.0, 1.0, 1.0);
3949
let proj_coord = proj_coord * vec4(0.5, 0.5, 0.5, 0.5);
40-
50+
4151
// Slow single pass blur
4252
// For demonstration purposes only
4353
const BLUR_SIZE: f32 = 1.0 / 512.0;
44-
54+
4555
*out_frag_color = vec4(0.0, 0.0, 0.0, 1.0);
46-
56+
4757
if front_facing {
4858
// Only render mirrored scene on front facing (upper) side of mirror surface
4959
let mut reflection = vec4(0.0, 0.0, 0.0, 0.0);
@@ -56,4 +66,4 @@ pub fn main_fs(
5666
}
5767
*out_frag_color = *out_frag_color + reflection;
5868
}
59-
}
69+
}

shaders/rust/offscreen/phong.vert.spv

-20 Bytes
Binary file not shown.

shaders/rust/offscreen/phong/src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#![cfg_attr(target_arch = "spirv", no_std)]
22
#![allow(clippy::missing_safety_doc)]
33

4-
use spirv_std::{spirv, glam::{vec4, Mat4, Vec3, Vec4}, num_traits::Float};
4+
use spirv_std::{
5+
glam::{vec4, Mat4, Vec3, Vec4},
6+
num_traits::Float,
7+
spirv,
8+
};
59

610
#[repr(C)]
711
#[derive(Copy, Clone)]
@@ -31,9 +35,9 @@ pub fn main_vs(
3135
let eye_pos = ubo.view * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
3236
*out_eye_pos = eye_pos.truncate();
3337
*out_light_vec = (ubo.light_pos.truncate() - *out_eye_pos).normalize();
34-
38+
3539
// Clip against reflection plane
36-
let clip_plane = vec4(0.0, -1.0, 0.0, 0.0);
40+
let clip_plane = vec4(0.0, 0.0, 0.0, 0.0);
3741
clip_distance[0] = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0).dot(clip_plane);
3842
}
3943

@@ -47,16 +51,21 @@ pub fn main_fs(
4751
) {
4852
let eye = (-in_eye_pos).normalize();
4953
let reflected = (-in_light_vec).reflect(in_normal).normalize();
50-
54+
5155
let i_ambient = vec4(0.1, 0.1, 0.1, 1.0);
52-
let i_diffuse = vec4(in_normal.dot(in_light_vec).max(0.0), in_normal.dot(in_light_vec).max(0.0), in_normal.dot(in_light_vec).max(0.0), in_normal.dot(in_light_vec).max(0.0));
56+
let i_diffuse = vec4(
57+
in_normal.dot(in_light_vec).max(0.0),
58+
in_normal.dot(in_light_vec).max(0.0),
59+
in_normal.dot(in_light_vec).max(0.0),
60+
in_normal.dot(in_light_vec).max(0.0),
61+
);
5362
let specular = 0.75;
5463
let mut i_specular = vec4(0.0, 0.0, 0.0, 0.0);
5564
if in_eye_pos.dot(in_normal) < 0.0 {
5665
let spec_factor = reflected.dot(eye).max(0.0).powf(16.0) * specular;
5766
i_specular = vec4(0.5, 0.5, 0.5, 1.0) * spec_factor;
5867
}
59-
68+
6069
let color_vec4 = vec4(in_color.x, in_color.y, in_color.z, 1.0);
6170
*out_frag_color = (i_ambient + i_diffuse) * color_vec4 + i_specular;
62-
}
71+
}

shaders/rust/offscreen/quad/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#![cfg_attr(target_arch = "spirv", no_std)]
22
#![allow(clippy::missing_safety_doc)]
33

4-
use spirv_std::{spirv, glam::{vec2, vec4, Vec2, Vec4}, Image};
54
use spirv_std::image::SampledImage;
5+
use spirv_std::{
6+
glam::{vec2, vec4, Vec2, Vec4},
7+
spirv, Image,
8+
};
69

710
#[spirv(vertex)]
811
pub fn main_vs(
@@ -19,8 +22,10 @@ pub fn main_vs(
1922
#[spirv(fragment)]
2023
pub fn main_fs(
2124
in_uv: Vec2,
22-
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<Image!(2D, type=f32, sampled)>,
25+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<
26+
Image!(2D, type=f32, sampled),
27+
>,
2328
out_frag_color: &mut Vec4,
2429
) {
2530
*out_frag_color = sampler_color.sample(in_uv);
26-
}
31+
}

0 commit comments

Comments
 (0)