mirror of
https://github.com/EmbarkStudios/rust-gpu.git
synced 2024-11-22 06:45:13 +00:00
Moved some functions to a shared crate (#255)
* Moved some functions to a shared crate * fmt! * fixed the cpu
This commit is contained in:
parent
6353505e9e
commit
2dd8dba3b0
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -615,6 +615,7 @@ dependencies = [
|
||||
"ash-molten",
|
||||
"ash-window",
|
||||
"cfg-if 1.0.0",
|
||||
"shared",
|
||||
"spirv-builder",
|
||||
"structopt",
|
||||
"winit",
|
||||
@ -626,6 +627,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"minifb",
|
||||
"rayon",
|
||||
"shared",
|
||||
"sky-shader",
|
||||
"spirv-std",
|
||||
]
|
||||
@ -640,6 +642,7 @@ dependencies = [
|
||||
"console_log",
|
||||
"futures",
|
||||
"ndk-glue",
|
||||
"shared",
|
||||
"spirv-builder",
|
||||
"strum",
|
||||
"wasm-bindgen-futures",
|
||||
@ -2100,6 +2103,13 @@ dependencies = [
|
||||
"loom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shared"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"spirv-std",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simplest-shader"
|
||||
version = "0.1.0"
|
||||
@ -2111,6 +2121,7 @@ dependencies = [
|
||||
name = "sky-shader"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"shared",
|
||||
"spirv-std",
|
||||
]
|
||||
|
||||
|
@ -13,6 +13,7 @@ use-installed-tools = ["spirv-builder/use-installed-tools"]
|
||||
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
|
||||
|
||||
[dependencies]
|
||||
shared = { path = "../../shaders/shared" }
|
||||
ash = "0.31"
|
||||
ash-window = "0.5"
|
||||
cfg-if = "1.0.0"
|
||||
|
@ -10,6 +10,7 @@ use ash::extensions::khr::{Surface, Swapchain};
|
||||
use ash::util::*;
|
||||
use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0};
|
||||
use ash::{vk, Device, Instance};
|
||||
use shared::ShaderConstants;
|
||||
use std::borrow::Cow;
|
||||
use std::default::Default;
|
||||
use std::ffi::{CStr, CString};
|
||||
@ -33,13 +34,6 @@ macro_rules! offset_of {
|
||||
}};
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
struct ShaderConstants {
|
||||
width: u32,
|
||||
height: u32,
|
||||
time: f32,
|
||||
}
|
||||
|
||||
unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
|
||||
::std::slice::from_raw_parts((p as *const T) as *const u8, ::std::mem::size_of::<T>())
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ publish = false
|
||||
[dependencies]
|
||||
minifb = "0.19.1"
|
||||
# bring in the shader as natively compiled code
|
||||
shared = { path = "../../shaders/shared" }
|
||||
sky-shader = { path = "../../shaders/sky-shader" }
|
||||
spirv-std = { path = "../../../crates/spirv-std" }
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
use minifb::{Key, Window, WindowOptions};
|
||||
use rayon::prelude::*;
|
||||
use shared::ShaderConstants;
|
||||
use spirv_std::glam::{vec2, Vec2, Vec4};
|
||||
use std::time::Instant;
|
||||
|
||||
@ -35,7 +36,7 @@ fn main() {
|
||||
)
|
||||
.expect("Window creation failed");
|
||||
|
||||
let push_constants = shader_module::ShaderConstants {
|
||||
let push_constants = ShaderConstants {
|
||||
width: WIDTH as u32,
|
||||
height: HEIGHT as u32,
|
||||
time: 0f32,
|
||||
|
@ -17,6 +17,7 @@ use-compiled-tools = ["spirv-builder/use-compiled-tools"]
|
||||
|
||||
[dependencies]
|
||||
cfg-if = "1.0.0"
|
||||
shared = { path = "../../shaders/shared" }
|
||||
futures = { version = "0.3", default-features = false, features = ["std", "executor"] }
|
||||
wgpu = "0.6.0"
|
||||
winit = { version = "0.23", features = ["web-sys"] }
|
||||
|
@ -1,4 +1,5 @@
|
||||
use super::{shader_module, Options, ShaderConstants};
|
||||
use super::{shader_module, Options};
|
||||
use shared::ShaderConstants;
|
||||
use winit::{
|
||||
event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
|
@ -11,13 +11,6 @@ pub enum RustGPUShader {
|
||||
Compute,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct ShaderConstants {
|
||||
width: u32,
|
||||
height: u32,
|
||||
time: f32,
|
||||
}
|
||||
|
||||
fn shader_module(shader: RustGPUShader) -> wgpu::ShaderModuleSource<'static> {
|
||||
match shader {
|
||||
RustGPUShader::Simplest => wgpu::include_spirv!(env!("simplest_shader.spv")),
|
||||
|
10
examples/shaders/shared/Cargo.toml
Normal file
10
examples/shaders/shared/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "shared"
|
||||
version = "0.1.0"
|
||||
authors = ["Embark <opensource@embark-studios.com>"]
|
||||
edition = "2018"
|
||||
license = "MIT OR Apache-2.0"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
spirv-std = { path = "../../../crates/spirv-std" }
|
66
examples/shaders/shared/src/lib.rs
Normal file
66
examples/shaders/shared/src/lib.rs
Normal file
@ -0,0 +1,66 @@
|
||||
//! Ported to Rust from https://github.com/Tw1ddle/Sky-Shader/blob/master/src/shaders/glsl/sky.fragment
|
||||
|
||||
#![cfg_attr(target_arch = "spirv", no_std)]
|
||||
#![feature(lang_items)]
|
||||
#![feature(register_attr)]
|
||||
#![register_attr(spirv)]
|
||||
|
||||
use core::f32::consts::PI;
|
||||
use spirv_std::glam::{Vec2, Vec3};
|
||||
use spirv_std::MathExt;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct ShaderConstants {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub time: f32,
|
||||
}
|
||||
|
||||
// TODO: add this to glam? Rust std has it on f32/f64
|
||||
pub fn pow(v: Vec3, power: f32) -> Vec3 {
|
||||
Vec3::new(v.x().pow(power), v.y().pow(power), v.z().pow(power))
|
||||
}
|
||||
|
||||
// TODO: add this to glam? Rust std has it on f32/f64
|
||||
pub fn exp(v: Vec3) -> Vec3 {
|
||||
Vec3::new(v.x().exp(), v.y().exp(), v.z().exp())
|
||||
}
|
||||
|
||||
/// Based on: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
|
||||
pub fn acos_approx(v: f32) -> f32 {
|
||||
let x = v.abs();
|
||||
let mut res = -0.155972 * x + 1.56467; // p(x)
|
||||
res *= (1.0f32 - x).sqrt();
|
||||
|
||||
if v >= 0.0 {
|
||||
res
|
||||
} else {
|
||||
PI - res
|
||||
}
|
||||
}
|
||||
|
||||
/// renamed because of cross-compilation issues with spirv-cross/ moltenvk
|
||||
pub fn my_smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
|
||||
// Scale, bias and saturate x to 0..1 range
|
||||
let x = ((x - edge0) / (edge1 - edge0)).saturate();
|
||||
// Evaluate polynomial
|
||||
x * x * (3.0 - 2.0 * x)
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn get_ray_dir(uv: Vec2, pos: Vec3, look_at_pos: Vec3) -> Vec3 {
|
||||
let forward = (look_at_pos - pos).normalize();
|
||||
let right = Vec3::new(0.0, 1.0, 0.0).cross(forward).normalize();
|
||||
let up = forward.cross(right);
|
||||
(forward + uv.x() * right + uv.y() * up).normalize()
|
||||
}
|
@ -10,4 +10,5 @@ publish = false
|
||||
crate-type = ["dylib"]
|
||||
|
||||
[dependencies]
|
||||
shared = { path = "../../shaders/shared" }
|
||||
spirv-std = { path = "../../../crates/spirv-std" }
|
||||
|
@ -6,6 +6,7 @@
|
||||
#![register_attr(spirv)]
|
||||
|
||||
use core::f32::consts::PI;
|
||||
use shared::*;
|
||||
use spirv_std::glam::{const_vec3, Vec2, Vec3, Vec4};
|
||||
use spirv_std::{Input, MathExt, Output, PushConstant};
|
||||
|
||||
@ -25,44 +26,6 @@ const SUN_INTENSITY_FACTOR: f32 = 1000.0;
|
||||
const SUN_INTENSITY_FALLOFF_STEEPNESS: f32 = 1.5;
|
||||
const TURBIDITY: f32 = 2.0;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct ShaderConstants {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub time: f32,
|
||||
}
|
||||
|
||||
// TODO: add this to glam? Rust std has it on f32/f64
|
||||
fn pow(v: Vec3, power: f32) -> Vec3 {
|
||||
Vec3::new(v.x().pow(power), v.y().pow(power), v.z().pow(power))
|
||||
}
|
||||
|
||||
// TODO: add this to glam? Rust std has it on f32/f64
|
||||
fn exp(v: Vec3) -> Vec3 {
|
||||
Vec3::new(v.x().exp(), v.y().exp(), v.z().exp())
|
||||
}
|
||||
|
||||
/// Based on: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/
|
||||
fn acos_approx(v: f32) -> f32 {
|
||||
let x = v.abs();
|
||||
let mut res = -0.155972 * x + 1.56467; // p(x)
|
||||
res *= (1.0f32 - x).sqrt();
|
||||
|
||||
if v >= 0.0 {
|
||||
res
|
||||
} else {
|
||||
PI - res
|
||||
}
|
||||
}
|
||||
|
||||
/// renamed because of cross-compilation issues with spirv-cross/ moltenvk
|
||||
fn my_smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
|
||||
// Scale, bias and saturate x to 0..1 range
|
||||
let x = ((x - edge0) / (edge1 - edge0)).saturate();
|
||||
// Evaluate polynomial
|
||||
x * x * (3.0 - 2.0 * x)
|
||||
}
|
||||
|
||||
fn total_rayleigh(lambda: Vec3) -> Vec3 {
|
||||
(8.0 * PI.pow(3.0)
|
||||
* (REFRACTIVE_INDEX.pow(2.0) - 1.0).pow(2.0)
|
||||
@ -93,17 +56,6 @@ fn sun_intensity(zenith_angle_cos: f32) -> f32 {
|
||||
)
|
||||
}
|
||||
|
||||
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 sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
|
||||
let up = Vec3::new(0.0, 1.0, 0.0);
|
||||
let sunfade = 1.0 - (1.0 - (sun_position.y() / 450000.0).exp()).saturate();
|
||||
@ -156,13 +108,6 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 {
|
||||
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::new(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) -> Vec4 {
|
||||
let mut uv = (frag_coord - 0.5 * Vec2::new(constants.width as f32, constants.height as f32))
|
||||
/ constants.height as f32;
|
||||
|
Loading…
Reference in New Issue
Block a user