Skip to content

Commit

Permalink
cargo fmt on shader \o/
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper-Bekkers committed Oct 19, 2020
1 parent 413a49a commit de95ae3
Showing 1 changed file with 54 additions and 60 deletions.
114 changes: 54 additions & 60 deletions examples/example-shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#![feature(core_intrinsics)]
#![allow(dead_code)]

use core::panic::PanicInfo;
use spirv_std::{f32x4, Input, Output};
use core::ops::Mul;
use core::ops::MulAssign;
use core::ops::Add;
use core::ops::AddAssign;
use core::ops::Div;
use core::ops::Add;
use core::ops::Mul;
use core::ops::MulAssign;
use core::ops::Neg;
use core::ops::Sub;
use core::panic::PanicInfo;
use spirv_std::{f32x4, Input, Output};

/// A 3-dimensional vector without SIMD support.
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
Expand Down Expand Up @@ -42,7 +42,6 @@ impl Vec3 {
#[inline]
pub const fn one() -> Self {
Self::new(1.0, 1.0, 1.0)

}

/// Creates a `Vec3` with values `[x: 1.0, y: 0.0, z: 0.0]`.
Expand Down Expand Up @@ -71,7 +70,11 @@ impl Vec3 {

pub fn pow(&self, factor: f32) -> Self {
unsafe {
Self(core::intrinsics::powf32(self.0, factor), core::intrinsics::powf32(self.1, factor), core::intrinsics::powf32(self.2, factor))
Self(
core::intrinsics::powf32(self.0, factor),
core::intrinsics::powf32(self.1, factor),
core::intrinsics::powf32(self.2, factor),
)
}
}
}
Expand All @@ -84,7 +87,6 @@ impl Mul<f32> for Vec3 {
}
}


impl Add<f32> for Vec3 {
type Output = Self;
#[inline]
Expand Down Expand Up @@ -121,11 +123,11 @@ impl Sub<Vec3> for f32 {
type Output = Vec3;
#[inline]
fn sub(self, other: Vec3) -> Vec3 {
Vec3(self - other.0, self- other.1, self - other.2)
Vec3(self - other.0, self - other.1, self - other.2)
}
}

fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32{
fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
// Scale, bias and saturate x to 0..1 range
let x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
// Evaluate polynomial
Expand All @@ -142,7 +144,7 @@ impl Mul<Vec3> for Vec3 {

impl MulAssign<Vec3> for Vec3 {
#[inline]
fn mul_assign(&mut self, other: Vec3){
fn mul_assign(&mut self, other: Vec3) {
self.0 *= other.0;
self.1 *= other.1;
self.2 *= other.2;
Expand All @@ -151,7 +153,7 @@ impl MulAssign<Vec3> for Vec3 {

impl AddAssign<Vec3> for Vec3 {
#[inline]
fn add_assign(&mut self, other: Vec3){
fn add_assign(&mut self, other: Vec3) {
self.0 += other.0;
self.1 += other.1;
self.2 += other.2;
Expand Down Expand Up @@ -194,44 +196,36 @@ const DEPOLARIZATION_FACTOR: f32 = 0.035;
const LUMINANCE: f32 = 1.0;
const MIE_COEFFICIENT: f32 = 0.005;
const MIE_DIRECTIONAL_G: f32 = 0.8;
const MIE_K_COEFFICIENT :Vec3=Vec3::new(0.686, 0.678, 0.666);
const MIE_K_COEFFICIENT: Vec3 = Vec3::new(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 NUM_MOLECULES: f32 = 2.542e25f32;
const PRIMARIES: Vec3 = Vec3::new(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 TONEMAP_WEIGHTING : Vec3 = Vec3::splat(9.50);
const TONEMAP_WEIGHTING: Vec3 = Vec3::splat(9.50);
const TURBIDITY: f32 = 2.0;

use core::f32::consts::PI;

fn pow(base: f32, factor: f32) -> f32 {
unsafe {
core::intrinsics::powf32(base, factor)
}
unsafe { core::intrinsics::powf32(base, factor) }
}

fn sqrt(f: f32) -> f32 {
unsafe {
core::intrinsics::sqrtf32(f)
}
unsafe { core::intrinsics::sqrtf32(f) }
}

fn log2(f: f32) -> f32 {
unsafe {
core::intrinsics::log2f32(f)
}
unsafe { core::intrinsics::log2f32(f) }
}

fn abs(f: f32) -> f32 {
unsafe {
core::intrinsics::fabsf32(f)
}
unsafe { core::intrinsics::fabsf32(f) }
}

fn acos(v: f32) -> f32 {
Expand All @@ -246,59 +240,55 @@ fn acos(v: f32) -> f32 {
}

fn cos(n: f32) -> f32 {
unsafe {
core::intrinsics::cosf32(n)
}
unsafe { core::intrinsics::cosf32(n) }
}

fn normalize(v: Vec3) -> Vec3 {
let len = 1.0 / sqrt(dot(v, v));
Vec3::new(
v.0 * len,
v.1 * len,
v.2 * len
)
Vec3::new(v.0 * len, v.1 * len, v.2 * len)
}

fn dot(a: Vec3, b: Vec3) -> f32 {
a.0 * b.0 + a.1 * b.1 + a.2 * b.2
}

fn total_rayleigh(lambda: Vec3) -> Vec3
{
(8.0 * pow(PI, 3.0) * pow(pow(REFRACTIVE_INDEX, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * DEPOLARIZATION_FACTOR)) / (3.0 * NUM_MOLECULES * lambda.pow(4.0) * (6.0 - 7.0 * DEPOLARIZATION_FACTOR))
fn total_rayleigh(lambda: Vec3) -> Vec3 {
(8.0 * pow(PI, 3.0)
* pow(pow(REFRACTIVE_INDEX, 2.0) - 1.0, 2.0)
* (6.0 + 3.0 * DEPOLARIZATION_FACTOR))
/ (3.0 * NUM_MOLECULES * lambda.pow(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 * ((2.0 * PI) / lambda).pow(MIE_V - 2.0) * k
fn total_mie(lambda: Vec3, k: Vec3, t: f32) -> Vec3 {
let c = 0.2 * t * 10e-18;
0.434 * c * PI * ((2.0 * PI) / lambda).pow(MIE_V - 2.0) * k
}

fn rayleigh_phase(cos_theta: f32)-> f32
{
(3.0 / (16.0 * PI)) * (1.0 + pow(cos_theta, 2.0))
fn rayleigh_phase(cos_theta: f32) -> f32 {
(3.0 / (16.0 * PI)) * (1.0 + pow(cos_theta, 2.0))
}

fn henyey_greenstein_phase(cos_theta: f32, g: f32) -> f32 {
(1.0 / (4.0 * PI)) * ((1.0 - pow(g, 2.0)) / pow(1.0 - 2.0 * g * cos_theta + pow(g, 2.0), 1.5))
(1.0 / (4.0 * PI)) * ((1.0 - pow(g, 2.0)) / pow(1.0 - 2.0 * g * cos_theta + pow(g, 2.0), 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 - exp(-((cutoff_angle - acos(zenith_angle_cos)) / SUN_INTENSITY_FALLOFF_STEEPNESS)))
let cutoff_angle = PI / 1.95; // Earth shadow hack
SUN_INTENSITY_FACTOR
* 0.0f32.max(
1.0 - exp(-((cutoff_angle - acos(zenith_angle_cos)) / SUN_INTENSITY_FALLOFF_STEEPNESS)),
)
}

fn uncharted2_tonemap(w: Vec3)->Vec3
{
fn uncharted2_tonemap(w: Vec3) -> Vec3 {
const A: f32 = 0.15; // Shoulder strength
const B: f32 = 0.50; // Linear strength
const C: f32 = 0.10; // Linear angle
const D: f32 = 0.20; // Toe strength
const E: f32 = 0.02; // Toe numerator
const F: f32 = 0.30; // Toe denominator

((w * (A * w + C * B) + D * E) / (w * (A * w + B) + D * F)) - E / F
((w * (A * w + C * B) + D * E) / (w * (A * w + B) + D * F)) - E / F
}

fn clamp(a: f32, b: f32, c: f32) -> f32 {
Expand All @@ -310,11 +300,7 @@ fn exp(a: f32) -> f32 {
}

fn exp_v3(a: Vec3) -> Vec3 {
Vec3::new(
exp(a.0),
exp(a.1),
exp(a.2)
)
Vec3::new(exp(a.0), exp(a.1), exp(a.2))
}

fn lerp(a: Vec3, b: Vec3, c: f32) -> Vec3 {
Expand Down Expand Up @@ -344,15 +330,23 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
let cos_theta = dot(dir, 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(dot(sun_direction, up));
let mut lin = (sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * (1.0 - fex)).pow(1.5);
lin *= lerp(Vec3::splat(1.0), (sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * fex).pow(0.5), clamp(pow(1.0 - dot(up, sun_direction), 5.0), 0.0, 1.0));
let mut lin =
(sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * (1.0 - fex)).pow(1.5);
lin *= lerp(
Vec3::splat(1.0),
(sun_e * ((beta_r_theta + beta_m_theta) / (beta_r + beta_m)) * fex).pow(0.5),
clamp(pow(1.0 - dot(up, sun_direction), 5.0), 0.0, 1.0),
);

// Composition + solar disc
let sun_angular_diameter_cos = cos(SUN_ANGULAR_DIAMETER_DEGREES);
let sundisk = smoothstep(sun_angular_diameter_cos, sun_angular_diameter_cos + 0.00002, cos_theta);
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;
let mut tex_color = lin + l0;
Expand Down

0 comments on commit de95ae3

Please sign in to comment.