Skip to content

Commit 152a476

Browse files
committed
Use a prelude.
1 parent 768b7e8 commit 152a476

34 files changed

+190
-441
lines changed

shaders/src/lib.rs

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,8 @@
11
#![cfg_attr(target_arch = "spirv", no_std)]
22

3-
use shared::*;
4-
use spirv_std::{
5-
glam::{vec2, vec3, vec4, Vec2, Vec3, Vec4},
6-
spirv,
7-
};
8-
9-
pub mod a_lot_of_spheres;
10-
pub mod a_question_of_time;
11-
pub mod apollonian;
12-
pub mod atmosphere_system_test;
13-
pub mod bubble_buckey_balls;
14-
pub mod clouds;
15-
pub mod constants;
16-
pub mod filtering_procedurals;
17-
pub mod flappy_bird;
18-
pub mod galaxy_of_universes;
19-
pub mod geodesic_tiling;
20-
pub mod heart;
21-
pub mod loading_repeating_circles;
22-
pub mod luminescence;
23-
pub mod mandelbrot_smooth;
24-
pub mod miracle_snowflakes;
25-
pub mod morphing;
26-
pub mod moving_square;
27-
pub mod on_off_spikes;
28-
pub mod phantom_star;
29-
pub mod playing_marble;
30-
pub mod protean_clouds;
31-
pub mod raymarching_primitives;
32-
pub mod seascape;
33-
pub mod skyline;
34-
pub mod soft_shadow_variation;
35-
pub mod tileable_water_caustic;
36-
pub mod tokyo;
37-
pub mod two_tweets;
38-
pub mod voxel_pac_man;
39-
40-
pub trait SampleCube: Copy {
41-
fn sample_cube(self, p: Vec3) -> Vec4;
42-
}
43-
44-
#[derive(Copy, Clone)]
45-
struct ConstantColor {
46-
color: Vec4,
47-
}
48-
49-
impl SampleCube for ConstantColor {
50-
fn sample_cube(self, _: Vec3) -> Vec4 {
51-
self.color
52-
}
53-
}
54-
55-
#[derive(Copy, Clone)]
56-
struct RgbCube {
57-
alpha: f32,
58-
intensity: f32,
59-
}
60-
61-
impl SampleCube for RgbCube {
62-
fn sample_cube(self, p: Vec3) -> Vec4 {
63-
(p.abs() * self.intensity).extend(self.alpha)
64-
}
65-
}
66-
67-
pub struct ShaderInput {
68-
resolution: Vec3,
69-
time: f32,
70-
frag_coord: Vec2,
71-
mouse: Vec4,
72-
}
73-
74-
pub struct ShaderResult {
75-
color: Vec4,
76-
}
77-
78-
pub struct ShaderDefinition {
79-
pub name: &'static str,
80-
}
81-
82-
macro_rules! match_index {
83-
($e:expr; $($result:expr),* $(,)?) => ({
84-
let mut i = 0..;
85-
match $e { e => {
86-
$(if e == i.next().unwrap() { $result } else)*
87-
{ unreachable!() }
88-
}}
89-
})
90-
}
91-
92-
macro_rules! render_shader_macro {
93-
($num_shaders:expr, $($shader_name:ident),* $(,)?) => {
94-
#[inline(always)]
95-
pub fn render_shader(shader_index: u32, shader_input: &ShaderInput, shader_output: &mut ShaderResult) {
96-
match_index!(shader_index; $(
97-
$shader_name::shader_fn(shader_input, shader_output),
98-
)*)
99-
}
100-
101-
pub const SHADER_DEFINITIONS: [ShaderDefinition; $num_shaders] = [
102-
$(
103-
$shader_name::SHADER_DEFINITION,
104-
)*
105-
];
106-
};
107-
}
108-
109-
render_shader_macro!(
110-
29,
111-
loading_repeating_circles,
112-
two_tweets,
113-
heart,
114-
clouds,
115-
mandelbrot_smooth,
116-
protean_clouds,
117-
tileable_water_caustic,
118-
apollonian,
119-
phantom_star,
120-
seascape,
121-
playing_marble,
122-
a_lot_of_spheres,
123-
a_question_of_time,
124-
galaxy_of_universes,
125-
atmosphere_system_test,
126-
soft_shadow_variation,
127-
miracle_snowflakes,
128-
morphing,
129-
bubble_buckey_balls,
130-
raymarching_primitives,
131-
moving_square,
132-
skyline,
133-
filtering_procedurals,
134-
geodesic_tiling,
135-
flappy_bird,
136-
tokyo,
137-
on_off_spikes,
138-
luminescence,
139-
voxel_pac_man,
140-
);
3+
pub mod shader_prelude;
4+
use shader_prelude::*;
5+
pub mod shaders;
1416

1427
#[inline(always)]
1438
pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {
@@ -174,7 +39,7 @@ pub fn fs(constants: &ShaderConstants, mut frag_coord: Vec2) -> Vec4 {
17439
mouse,
17540
};
17641
let mut shader_output = &mut ShaderResult { color: Vec4::ZERO };
177-
render_shader(constants.shader_to_show, &shader_input, &mut shader_output);
42+
shaders::render_shader(constants.shader_to_show, &shader_input, &mut shader_output);
17843
let color = shader_output.color;
17944
Vec3::powf(color.truncate(), 2.2).extend(color.w)
18045
}

shaders/src/prelude.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

shaders/src/shader_prelude.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pub use core::f32::consts::{FRAC_1_PI, FRAC_PI_2, PI};
2+
pub const TWO_PI: f32 = 2.0 * PI;
3+
pub const SQRT3: f32 = 1.7320508075688772;
4+
5+
pub use shared::*;
6+
pub use spirv_std::{
7+
arch::Derivative,
8+
glam::{
9+
mat2, mat3, vec2, vec3, vec4, Mat2, Mat3, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4,
10+
Vec4Swizzles,
11+
},
12+
spirv,
13+
};
14+
15+
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
16+
// we tie #[no_std] above to the same condition, so it's fine.
17+
#[cfg(target_arch = "spirv")]
18+
pub use spirv_std::num_traits::Float;
19+
20+
pub trait SampleCube: Copy {
21+
fn sample_cube(self, p: Vec3) -> Vec4;
22+
}
23+
24+
#[derive(Copy, Clone)]
25+
pub struct ConstantColor {
26+
pub color: Vec4,
27+
}
28+
29+
impl SampleCube for ConstantColor {
30+
fn sample_cube(self, _: Vec3) -> Vec4 {
31+
self.color
32+
}
33+
}
34+
35+
#[derive(Copy, Clone)]
36+
pub struct RgbCube {
37+
pub alpha: f32,
38+
pub intensity: f32,
39+
}
40+
41+
impl SampleCube for RgbCube {
42+
fn sample_cube(self, p: Vec3) -> Vec4 {
43+
(p.abs() * self.intensity).extend(self.alpha)
44+
}
45+
}
46+
47+
pub struct ShaderInput {
48+
pub resolution: Vec3,
49+
pub time: f32,
50+
pub frag_coord: Vec2,
51+
pub mouse: Vec4,
52+
}
53+
54+
pub struct ShaderResult {
55+
pub color: Vec4,
56+
}
57+
58+
pub struct ShaderDefinition {
59+
pub name: &'static str,
60+
}

shaders/src/shaders/a_lot_of_spheres.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
//! */
1212
//! ```
1313
14-
use shared::*;
15-
use spirv_std::glam::{mat2, vec2, vec3, Mat2, Vec2, Vec3, Vec3Swizzles, Vec4};
16-
17-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
18-
// we tie #[no_std] above to the same condition, so it's fine.
19-
#[cfg(target_arch = "spirv")]
20-
use spirv_std::num_traits::Float;
21-
22-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
14+
use crate::shader_prelude::*;
2315

2416
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
2517
name: "A Lot of Spheres",

shaders/src/shaders/a_question_of_time.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@
2626
//! */
2727
//! ```
2828
29-
use shared::*;
30-
use spirv_std::glam::{
31-
vec2, vec3, Mat2, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles,
32-
};
33-
34-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
35-
// we tie #[no_std] above to the same condition, so it's fine.
36-
#[cfg(target_arch = "spirv")]
37-
use spirv_std::num_traits::Float;
38-
39-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
29+
use crate::shader_prelude::*;
4030

4131
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
4232
name: "A Question of Time",

shaders/src/shaders/apollonian.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@
1010
//! // Coloring and fake occlusions are done by orbit trapping, as usual.
1111
//! ```
1212
13-
use shared::*;
14-
use spirv_std::glam::{vec2, vec3, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4};
15-
16-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
17-
// we tie #[no_std] above to the same condition, so it's fine.
18-
#[cfg(target_arch = "spirv")]
19-
use spirv_std::num_traits::Float;
20-
21-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
13+
use crate::shader_prelude::*;
2214

2315
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
2416
name: "Apollonian Fractal",

shaders/src/shaders/atmosphere_system_test.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@
1010
//! // ----------------------------------------------------------------------------
1111
//! ```
1212
13-
use spirv_std::glam::{vec2, vec3, Mat3, Vec2, Vec3, Vec3Swizzles, Vec4};
14-
15-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
16-
// we tie #[no_std] above to the same condition, so it's fine.
17-
#[cfg(target_arch = "spirv")]
18-
use spirv_std::num_traits::Float;
19-
20-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
13+
use crate::shader_prelude::*;
2114

2215
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
2316
name: "Atmosphere System Test",

shaders/src/shaders/bubble_buckey_balls.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@
88
//! // Tested on Nvidia GTX 780 Windows 7
99
//! ```
1010
11-
use core::f32::consts::{FRAC_1_PI, PI};
12-
13-
use crate::{constants::TWO_PI, ConstantColor, RgbCube, SampleCube};
14-
use shared::*;
15-
use spirv_std::glam::{vec2, vec3, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
16-
17-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
18-
// we tie #[no_std] above to the same condition, so it's fine.
19-
#[cfg(target_arch = "spirv")]
20-
use spirv_std::num_traits::Float;
21-
22-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
11+
use crate::shader_prelude::*;
2312

2413
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
2514
name: "Bubble Buckey Balls",

shaders/src/shaders/clouds.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Ported to Rust from <https://www.shadertoy.com/view/4tdSWr>
22
3-
use shared::*;
4-
use spirv_std::glam::{mat2, vec2, vec3, Mat2, Vec2, Vec3, Vec3Swizzles, Vec4};
5-
6-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
3+
use crate::shader_prelude::*;
74

85
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition { name: "Clouds" };
96

shaders/src/shaders/filtering_procedurals.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,7 @@
1919
//! */
2020
//! ```
2121
22-
use shared::*;
23-
use spirv_std::{
24-
arch::Derivative,
25-
glam::{vec2, vec3, vec4, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles},
26-
};
27-
28-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
29-
// we tie #[no_std] above to the same condition, so it's fine.
30-
#[cfg(target_arch = "spirv")]
31-
use spirv_std::num_traits::Float;
32-
33-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
22+
use crate::shader_prelude::*;
3423

3524
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
3625
name: "Filtering Procedurals",

0 commit comments

Comments
 (0)