Skip to content

Commit 353fa96

Browse files
fu5hakhyperia
andauthored
Rename pow to powf, remove saturate from MathExt, and update to latest glam (EmbarkStudios#248)
* remove saturate from spirv_std MathExt and rename pow to powf * update to latest glam * update glam again and add `std` feature to `spirv-std` to support cpu runner * re-remove spirv-tools-sys submodule * fixup! Merge branch 'main' into powf-saturate * Use libm instead of MathExt * Fix lint for unused import when compiling with std Co-authored-by: khyperia <[email protected]>
1 parent 62da29e commit 353fa96

File tree

10 files changed

+70
-149
lines changed

10 files changed

+70
-149
lines changed

Cargo.lock

+11-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spirv-std/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ repository = "https://github.com/EmbarkStudios/rust-gpu"
88

99
[dependencies]
1010
# https://github.com/bitshifter/glam-rs/pull/85
11-
glam = { git = "https://github.com/EmbarkStudios/glam-rs", rev = "c9561e4dfd55fa5a9d6838cae3c9e90c8edafaf9" }
11+
glam = { git = "https://github.com/EmbarkStudios/glam-rs", rev = "ea8fe2766ca27f481656d3507cc129537e262182", default-features = false, features = ["libm"] }
12+
num-traits = { version = "0.2.14", default-features = false }
13+
14+
[features]
15+
default = []
16+
std = ["glam/std"]

crates/spirv-std/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
nonstandard_style
3838
)]
3939

40-
mod math_ext;
41-
pub use math_ext::MathExt;
42-
4340
pub use glam;
41+
pub use num_traits;
4442

4543
macro_rules! pointer_addrspace_write {
4644
(false) => {};

crates/spirv-std/src/math_ext.rs

-95
This file was deleted.

examples/runners/cpu/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ minifb = "0.19.1"
1111
# bring in the shader as natively compiled code
1212
shared = { path = "../../shaders/shared" }
1313
sky-shader = { path = "../../shaders/sky-shader" }
14-
spirv-std = { path = "../../../crates/spirv-std" }
14+
spirv-std = { path = "../../../crates/spirv-std", features = ["std"] }
1515

1616
# for parallelism, not really needed though
1717
rayon = "1.5"

examples/runners/cpu/src/main.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn srgb_oetf(x: f32) -> f32 {
1818
fn color_u32_from_vec4(v: Vec4) -> u32 {
1919
let convert = |f: f32| -> u32 { (f.min(1.0).max(0.0) * 255.0).round() as u32 };
2020

21-
convert(srgb_oetf(v.z()))
22-
| convert(srgb_oetf(v.y())) << 8
23-
| convert(srgb_oetf(v.x())) << 16
24-
| convert(v.w()) << 24
21+
convert(srgb_oetf(v.z))
22+
| convert(srgb_oetf(v.y)) << 8
23+
| convert(srgb_oetf(v.x)) << 16
24+
| convert(v.w) << 24
2525
}
2626

2727
fn main() {
@@ -55,8 +55,7 @@ fn main() {
5555
-((i / WIDTH) as f32 / HEIGHT as f32 * 2.0 - 1.0),
5656
);
5757

58-
let frag_coord = (vec2(screen_pos.x(), -screen_pos.y()) + Vec2::one())
59-
/ Vec2::splat(2.0)
58+
let frag_coord = (vec2(screen_pos.x, -screen_pos.y) + Vec2::one()) / Vec2::splat(2.0)
6059
* vec2(WIDTH as f32, HEIGHT as f32);
6160

6261
// evaluate the fragment shader for the specific pixel

examples/shaders/shared/src/lib.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#![register_attr(spirv)]
77

88
use core::f32::consts::PI;
9-
use spirv_std::glam::{Vec2, Vec3};
10-
use spirv_std::MathExt;
9+
use spirv_std::glam::Vec3;
10+
11+
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
12+
// we tie #[no_std] above to the same condition, so it's fine.
13+
#[cfg(target_arch = "spirv")]
14+
use spirv_std::num_traits::Float;
1115

1216
#[derive(Copy, Clone)]
1317
pub struct ShaderConstants {
@@ -16,14 +20,16 @@ pub struct ShaderConstants {
1620
pub time: f32,
1721
}
1822

19-
// TODO: add this to glam? Rust std has it on f32/f64
23+
pub fn saturate(x: f32) -> f32 {
24+
x.max(0.0).min(1.0)
25+
}
26+
2027
pub fn pow(v: Vec3, power: f32) -> Vec3 {
21-
Vec3::new(v.x().pow(power), v.y().pow(power), v.z().pow(power))
28+
Vec3::new(v.x.powf(power), v.y.powf(power), v.z.powf(power))
2229
}
2330

24-
// TODO: add this to glam? Rust std has it on f32/f64
2531
pub fn exp(v: Vec3) -> Vec3 {
26-
Vec3::new(v.x().exp(), v.y().exp(), v.z().exp())
32+
Vec3::new(v.x.exp(), v.y.exp(), v.z.exp())
2733
}
2834

2935
/// Based on: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
@@ -41,25 +47,7 @@ pub fn acos_approx(v: f32) -> f32 {
4147

4248
pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
4349
// Scale, bias and saturate x to 0..1 range
44-
let x = ((x - edge0) / (edge1 - edge0)).saturate();
50+
let x = saturate((x - edge0) / (edge1 - edge0));
4551
// Evaluate polynomial
4652
x * x * (3.0 - 2.0 * x)
4753
}
48-
49-
pub fn tonemap(col: Vec3) -> Vec3 {
50-
// see https://www.desmos.com/calculator/0eo9pzo1at
51-
const A: f32 = 2.35;
52-
const B: f32 = 2.8826666;
53-
const C: f32 = 789.7459;
54-
const D: f32 = 0.935;
55-
56-
let z = pow(col, A);
57-
z / (pow(z, D) * B + Vec3::splat(C))
58-
}
59-
60-
pub fn get_ray_dir(uv: Vec2, pos: Vec3, look_at_pos: Vec3) -> Vec3 {
61-
let forward = (look_at_pos - pos).normalize();
62-
let right = Vec3::new(0.0, 1.0, 0.0).cross(forward).normalize();
63-
let up = forward.cross(right);
64-
(forward + uv.x() * right + uv.y() * up).normalize()
65-
}

examples/shaders/sky-shader/src/lib.rs

+33-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
use core::f32::consts::PI;
99
use shared::*;
1010
use spirv_std::glam::{const_vec3, Vec2, Vec3, Vec4};
11-
use spirv_std::{Input, MathExt, Output, PushConstant};
11+
use spirv_std::{Input, Output, PushConstant};
12+
13+
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
14+
// we tie #[no_std] above to the same condition, so it's fine.
15+
#[cfg(target_arch = "spirv")]
16+
use spirv_std::num_traits::Float;
1217

1318
const DEPOLARIZATION_FACTOR: f32 = 0.035;
1419
const MIE_COEFFICIENT: f32 = 0.005;
@@ -26,9 +31,20 @@ const SUN_INTENSITY_FACTOR: f32 = 1000.0;
2631
const SUN_INTENSITY_FALLOFF_STEEPNESS: f32 = 1.5;
2732
const TURBIDITY: f32 = 2.0;
2833

34+
pub fn tonemap(col: Vec3) -> Vec3 {
35+
// see https://www.desmos.com/calculator/0eo9pzo1at
36+
const A: f32 = 2.35;
37+
const B: f32 = 2.8826666;
38+
const C: f32 = 789.7459;
39+
const D: f32 = 0.935;
40+
41+
let z = pow(col, A);
42+
z / (pow(z, D) * B + Vec3::splat(C))
43+
}
44+
2945
fn total_rayleigh(lambda: Vec3) -> Vec3 {
30-
(8.0 * PI.pow(3.0)
31-
* (REFRACTIVE_INDEX.pow(2.0) - 1.0).pow(2.0)
46+
(8.0 * PI.powf(3.0)
47+
* (REFRACTIVE_INDEX.powf(2.0) - 1.0).powf(2.0)
3248
* (6.0 + 3.0 * DEPOLARIZATION_FACTOR))
3349
/ (3.0 * NUM_MOLECULES * pow(lambda, 4.0) * (6.0 - 7.0 * DEPOLARIZATION_FACTOR))
3450
}
@@ -39,11 +55,11 @@ fn total_mie(lambda: Vec3, k: Vec3, t: f32) -> Vec3 {
3955
}
4056

4157
fn rayleigh_phase(cos_theta: f32) -> f32 {
42-
(3.0 / (16.0 * PI)) * (1.0 + cos_theta.pow(2.0))
58+
(3.0 / (16.0 * PI)) * (1.0 + cos_theta.powf(2.0))
4359
}
4460

4561
fn henyey_greenstein_phase(cos_theta: f32, g: f32) -> f32 {
46-
(1.0 / (4.0 * PI)) * ((1.0 - g.pow(2.0)) / (1.0 - 2.0 * g * cos_theta + g.pow(2.0)).pow(1.5))
62+
(1.0 / (4.0 * PI)) * ((1.0 - g.powf(2.0)) / (1.0 - 2.0 * g * cos_theta + g.powf(2.0)).powf(1.5))
4763
}
4864

4965
fn sun_intensity(zenith_angle_cos: f32) -> f32 {
@@ -58,7 +74,7 @@ fn sun_intensity(zenith_angle_cos: f32) -> f32 {
5874

5975
fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
6076
let up = Vec3::new(0.0, 1.0, 0.0);
61-
let sunfade = 1.0 - (1.0 - (sun_position.y() / 450000.0).exp()).saturate();
77+
let sunfade = 1.0 - (1.0 - saturate(sun_position.y / 450000.0).exp());
6278
let rayleigh_coefficient = RAYLEIGH - (1.0 * (1.0 - sunfade));
6379
let beta_r = total_rayleigh(PRIMARIES) * rayleigh_coefficient;
6480

@@ -67,7 +83,7 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
6783

6884
// Optical length, cutoff angle at 90 to avoid singularity
6985
let zenith_angle = acos_approx(up.dot(dir).max(0.0));
70-
let denom = (zenith_angle).cos() + 0.15 * (93.885 - ((zenith_angle * 180.0) / PI)).pow(-1.253);
86+
let denom = (zenith_angle).cos() + 0.15 * (93.885 - ((zenith_angle * 180.0) / PI)).powf(-1.253);
7187

7288
let s_r = RAYLEIGH_ZENITH_LENGTH / denom;
7389
let s_m = MIE_ZENITH_LENGTH / denom;
@@ -92,7 +108,7 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
92108
sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * fex,
93109
0.5,
94110
),
95-
((1.0 - up.dot(sun_direction)).pow(5.0)).saturate(),
111+
saturate((1.0 - up.dot(sun_direction)).powf(5.0)),
96112
);
97113

98114
// Composition + solar disc
@@ -108,10 +124,17 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
108124
lin + l0
109125
}
110126

127+
fn get_ray_dir(uv: Vec2, pos: Vec3, look_at_pos: Vec3) -> Vec3 {
128+
let forward = (look_at_pos - pos).normalize();
129+
let right = Vec3::new(0.0, 1.0, 0.0).cross(forward).normalize();
130+
let up = forward.cross(right);
131+
(forward + uv.x * right + uv.y * up).normalize()
132+
}
133+
111134
pub fn fs(constants: &ShaderConstants, frag_coord: Vec2) -> Vec4 {
112135
let mut uv = (frag_coord - 0.5 * Vec2::new(constants.width as f32, constants.height as f32))
113136
/ constants.height as f32;
114-
uv.set_y(-uv.y());
137+
uv.y = -uv.y;
115138

116139
// hard-code information because we can't bind buffers at the moment
117140
let eye_pos = Vec3::new(0.0, 0.0997, 0.2);
@@ -136,7 +159,7 @@ pub fn main_fs(
136159
) {
137160
let constants = constants.load();
138161

139-
let frag_coord = Vec2::new(in_frag_coord.load().x(), in_frag_coord.load().y());
162+
let frag_coord = Vec2::new(in_frag_coord.load().x, in_frag_coord.load().y);
140163
let color = fs(&constants, frag_coord);
141164
output.store(color);
142165
}

setup.bat

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
setlocal
22

33
rustup toolchain install nightly-2020-11-15 --component rust-src rustc-dev llvm-tools-preview
4-
5-
git submodule init
6-
git submodule update

setup.sh

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
#!/usr/bin/env bash
22

33
rustup toolchain install nightly-2020-11-15 --component rust-src rustc-dev llvm-tools-preview
4-
5-
git submodule init
6-
git submodule update

0 commit comments

Comments
 (0)