-
Notifications
You must be signed in to change notification settings - Fork 30
/
lib.rs
195 lines (160 loc) · 6.57 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Ported to Rust from <https://github.com/Tw1ddle/Sky-Shader/blob/master/src/shaders/glsl/sky.fragment>
#![cfg_attr(target_arch = "spirv", no_std)]
// HACK(eddyb) can't easily see warnings otherwise from `spirv-builder` builds.
#![deny(warnings)]
use core::f32::consts::PI;
use glam::{vec2, vec3, Vec2, Vec3, Vec4};
use shared::*;
use spirv_std::spirv;
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
// we tie #[no_std] above to the same condition, so it's fine.
#[cfg(target_arch = "spirv")]
use spirv_std::num_traits::Float;
const DEPOLARIZATION_FACTOR: f32 = 0.035;
const MIE_COEFFICIENT: f32 = 0.005;
const MIE_DIRECTIONAL_G: f32 = 0.8;
const MIE_K_COEFFICIENT: Vec3 = vec3(0.686, 0.678, 0.666);
const MIE_V: f32 = 4.0;
const MIE_ZENITH_LENGTH: f32 = 1.25e3;
const NUM_MOLECULES: f32 = 2.542e25f32;
const PRIMARIES: Vec3 = vec3(6.8e-7f32, 5.5e-7f32, 4.5e-7f32);
const RAYLEIGH: f32 = 1.0;
const RAYLEIGH_ZENITH_LENGTH: f32 = 8.4e3;
const REFRACTIVE_INDEX: f32 = 1.0003;
const SUN_ANGULAR_DIAMETER_DEGREES: f32 = 0.0093333;
const SUN_INTENSITY_FACTOR: f32 = 1000.0;
const SUN_INTENSITY_FALLOFF_STEEPNESS: f32 = 1.5;
const TURBIDITY: f32 = 2.0;
pub fn tonemap(col: Vec3) -> Vec3 {
// see https://www.desmos.com/calculator/0eo9pzo1at
const A: f32 = 2.35;
const B: f32 = 2.8826666;
const C: f32 = 789.7459;
const D: f32 = 0.935;
let z = pow(col, A);
z / (pow(z, D) * B + Vec3::splat(C))
}
fn total_rayleigh(lambda: Vec3) -> Vec3 {
(8.0 * PI.powf(3.0)
* (REFRACTIVE_INDEX.powf(2.0) - 1.0).powf(2.0)
* (6.0 + 3.0 * DEPOLARIZATION_FACTOR))
/ (3.0 * NUM_MOLECULES * pow(lambda, 4.0) * (6.0 - 7.0 * DEPOLARIZATION_FACTOR))
}
fn total_mie(lambda: Vec3, k: Vec3, t: f32) -> Vec3 {
let c = 0.2 * t * 10e-18;
0.434 * c * PI * pow((2.0 * PI) / lambda, MIE_V - 2.0) * k
}
fn rayleigh_phase(cos_theta: f32) -> f32 {
(3.0 / (16.0 * PI)) * (1.0 + cos_theta.powf(2.0))
}
fn henyey_greenstein_phase(cos_theta: f32, g: f32) -> f32 {
(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))
}
fn sun_intensity(zenith_angle_cos: f32) -> f32 {
let cutoff_angle = PI / 1.95; // Earth shadow hack
SUN_INTENSITY_FACTOR
* 0.0f32.max(
1.0 - (-((cutoff_angle - acos_approx(zenith_angle_cos))
/ SUN_INTENSITY_FALLOFF_STEEPNESS))
.exp(),
)
}
fn sky(dir: Vec3, sun_position: Vec3, sun_intensity_extra_spec_const_factor: u32) -> Vec3 {
let up = vec3(0.0, 1.0, 0.0);
let sunfade = 1.0 - (1.0 - saturate(sun_position.y / 450000.0).exp());
let rayleigh_coefficient = RAYLEIGH - (1.0 * (1.0 - sunfade));
let beta_r = total_rayleigh(PRIMARIES) * rayleigh_coefficient;
// Mie coefficient
let beta_m = total_mie(PRIMARIES, MIE_K_COEFFICIENT, TURBIDITY) * MIE_COEFFICIENT;
// Optical length, cutoff angle at 90 to avoid singularity
let zenith_angle = acos_approx(up.dot(dir).max(0.0));
let denom = (zenith_angle).cos() + 0.15 * (93.885 - ((zenith_angle * 180.0) / PI)).powf(-1.253);
let s_r = RAYLEIGH_ZENITH_LENGTH / denom;
let s_m = MIE_ZENITH_LENGTH / denom;
// Combined extinction factor
let fex = exp(-(beta_r * s_r + beta_m * s_m));
// In-scattering
let sun_direction = sun_position.normalize();
let cos_theta = dir.dot(sun_direction);
let beta_r_theta = beta_r * rayleigh_phase(cos_theta * 0.5 + 0.5);
let beta_m_theta = beta_m * henyey_greenstein_phase(cos_theta, MIE_DIRECTIONAL_G);
let sun_e = sun_intensity(sun_direction.dot(up))
// HACK(eddyb) this acts like an integration test for specialization constants,
// but the correct value is only obtained when this is a noop (multiplies by `1`).
* (sun_intensity_extra_spec_const_factor as f32 / 100.0);
let mut lin = pow(
sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * (Vec3::splat(1.0) - fex),
1.5,
);
lin *= Vec3::splat(1.0).lerp(
pow(
sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * fex,
0.5,
),
saturate((1.0 - up.dot(sun_direction)).powf(5.0)),
);
// Composition + solar disc
let sun_angular_diameter_cos = SUN_ANGULAR_DIAMETER_DEGREES.cos();
let sundisk = smoothstep(
sun_angular_diameter_cos,
sun_angular_diameter_cos + 0.00002,
cos_theta,
);
let mut l0 = 0.1 * fex;
l0 += sun_e * 19000.0 * fex * sundisk;
lin + l0
}
fn get_ray_dir(uv: Vec2, pos: Vec3, look_at_pos: Vec3) -> Vec3 {
let forward = (look_at_pos - pos).normalize();
let right = vec3(0.0, 1.0, 0.0).cross(forward).normalize();
let up = forward.cross(right);
(forward + uv.x * right + uv.y * up).normalize()
}
pub fn fs(
constants: &ShaderConstants,
frag_coord: Vec2,
sun_intensity_extra_spec_const_factor: u32,
) -> Vec4 {
let mut uv = (frag_coord - 0.5 * vec2(constants.width as f32, constants.height as f32))
/ constants.height as f32;
uv.y = -uv.y;
// hard-code information because we can't bind buffers at the moment
let eye_pos = vec3(0.0, 0.0997, 0.2);
let sun_pos = vec3(0.0, 75.0, -1000.0);
let dir = get_ray_dir(uv, eye_pos, sun_pos);
// evaluate Preetham sky model
let color = sky(dir, sun_pos, sun_intensity_extra_spec_const_factor);
// Tonemapping
let color = color.max(Vec3::splat(0.0)).min(Vec3::splat(1024.0));
tonemap(color).extend(1.0)
}
#[spirv(fragment)]
pub fn main_fs(
#[spirv(frag_coord)] in_frag_coord: Vec4,
#[spirv(push_constant)] constants: &ShaderConstants,
output: &mut Vec4,
// NOTE(eddyb) this acts like an integration test for specialization constants.
#[spirv(spec_constant(id = 0x5007, default = 100))] sun_intensity_extra_spec_const_factor: u32,
) {
let frag_coord = vec2(in_frag_coord.x, in_frag_coord.y);
*output = fs(constants, frag_coord, sun_intensity_extra_spec_const_factor);
}
#[spirv(vertex)]
pub fn main_vs(#[spirv(vertex_index)] vert_idx: i32, #[spirv(position)] builtin_pos: &mut Vec4) {
// Create a "full screen triangle" by mapping the vertex index.
// ported from https://www.saschawillems.de/blog/2016/08/13/vulkan-tutorial-on-rendering-a-fullscreen-quad-without-buffers/
let uv = vec2(((vert_idx << 1) & 2) as f32, (vert_idx & 2) as f32);
let pos = 2.0 * uv - Vec2::ONE;
*builtin_pos = pos.extend(0.0).extend(1.0);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tonemap() {
assert_eq!(
tonemap(vec3(1_f32, 1_f32, 1_f32)),
vec3(0.001261625, 0.001261625, 0.001261625)
);
}
}