8
8
use core:: f32:: consts:: PI ;
9
9
use shared:: * ;
10
10
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 ;
12
17
13
18
const DEPOLARIZATION_FACTOR : f32 = 0.035 ;
14
19
const MIE_COEFFICIENT : f32 = 0.005 ;
@@ -26,9 +31,20 @@ const SUN_INTENSITY_FACTOR: f32 = 1000.0;
26
31
const SUN_INTENSITY_FALLOFF_STEEPNESS : f32 = 1.5 ;
27
32
const TURBIDITY : f32 = 2.0 ;
28
33
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
+
29
45
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 )
32
48
* ( 6.0 + 3.0 * DEPOLARIZATION_FACTOR ) )
33
49
/ ( 3.0 * NUM_MOLECULES * pow ( lambda, 4.0 ) * ( 6.0 - 7.0 * DEPOLARIZATION_FACTOR ) )
34
50
}
@@ -39,11 +55,11 @@ fn total_mie(lambda: Vec3, k: Vec3, t: f32) -> Vec3 {
39
55
}
40
56
41
57
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 ) )
43
59
}
44
60
45
61
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 ) )
47
63
}
48
64
49
65
fn sun_intensity ( zenith_angle_cos : f32 ) -> f32 {
@@ -58,7 +74,7 @@ fn sun_intensity(zenith_angle_cos: f32) -> f32 {
58
74
59
75
fn sky ( dir : Vec3 , sun_position : Vec3 ) -> Vec3 {
60
76
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 ( ) ) ;
62
78
let rayleigh_coefficient = RAYLEIGH - ( 1.0 * ( 1.0 - sunfade) ) ;
63
79
let beta_r = total_rayleigh ( PRIMARIES ) * rayleigh_coefficient;
64
80
@@ -67,7 +83,7 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
67
83
68
84
// Optical length, cutoff angle at 90 to avoid singularity
69
85
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 ) ;
71
87
72
88
let s_r = RAYLEIGH_ZENITH_LENGTH / denom;
73
89
let s_m = MIE_ZENITH_LENGTH / denom;
@@ -92,7 +108,7 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
92
108
sun_e * ( ( beta_r_theta + beta_m_theta) / ( beta_r + beta_m) ) * fex,
93
109
0.5 ,
94
110
) ,
95
- ( ( 1.0 - up. dot ( sun_direction) ) . pow ( 5.0 ) ) . saturate ( ) ,
111
+ saturate ( ( 1.0 - up. dot ( sun_direction) ) . powf ( 5.0 ) ) ,
96
112
) ;
97
113
98
114
// Composition + solar disc
@@ -108,10 +124,17 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
108
124
lin + l0
109
125
}
110
126
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
+
111
134
pub fn fs ( constants : & ShaderConstants , frag_coord : Vec2 ) -> Vec4 {
112
135
let mut uv = ( frag_coord - 0.5 * Vec2 :: new ( constants. width as f32 , constants. height as f32 ) )
113
136
/ constants. height as f32 ;
114
- uv. set_y ( -uv. y ( ) ) ;
137
+ uv. y = -uv. y ;
115
138
116
139
// hard-code information because we can't bind buffers at the moment
117
140
let eye_pos = Vec3 :: new ( 0.0 , 0.0997 , 0.2 ) ;
@@ -136,7 +159,7 @@ pub fn main_fs(
136
159
) {
137
160
let constants = constants. load ( ) ;
138
161
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 ) ;
140
163
let color = fs ( & constants, frag_coord) ;
141
164
output. store ( color) ;
142
165
}
0 commit comments