Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed Jan 1, 2024
1 parent 0108d78 commit a5fd8a2
Show file tree
Hide file tree
Showing 40 changed files with 567 additions and 578 deletions.
1 change: 0 additions & 1 deletion crates/renderling-shader/src/.#skybox.rs

This file was deleted.

65 changes: 52 additions & 13 deletions crates/renderling-shader/src/convolution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Convolution shaders.
//!
//! These shaders convolve various functions to produce cached maps.
use crabslab::{Id, Slab, SlabItem};
use glam::{UVec2, Vec2, Vec3, Vec4, Vec4Swizzles};
use spirv_std::{
image::{Cubemap, Image2d},
Expand All @@ -11,7 +12,7 @@ use spirv_std::{
#[cfg(target_arch = "spirv")]
use spirv_std::num_traits::Float;

use crate::{pbr, stage::GpuConstants, IsVector};
use crate::{pbr, IsVector};

fn radical_inverse_vdc(mut bits: u32) -> f32 {
bits = (bits << 16u32) | (bits >> 16u32);
Expand Down Expand Up @@ -148,26 +149,35 @@ pub fn integrate_brdf_doesnt_work(mut n_dot_v: f32, roughness: f32) -> Vec2 {
Vec2::new(a, b)
}

/// Expects a slab to contain a [`crate::stage::Camera`] followed by a `f32`
/// roughness value.
// TODO: merge this with the standard pass-thru cubemap vertex shader.
#[spirv(vertex)]
pub fn vertex_prefilter_environment_cubemap(
#[spirv(uniform, descriptor_set = 0, binding = 0)] constants: &GpuConstants,
in_pos: Vec3,
#[spirv(vertex_index)] vertex_id: u32,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
out_pos: &mut Vec3,
#[spirv(position)] gl_pos: &mut Vec4,
) {
let in_pos = crate::math::CUBE[vertex_id as usize];
let camera = slab.read::<crate::stage::Camera>(0u32.into());
*out_pos = in_pos;
*gl_pos = constants.camera_projection * constants.camera_view * in_pos.extend(1.0);
*gl_pos = camera.projection * camera.view * in_pos.extend(1.0);
}

/// Lambertian prefilter.
///
/// Expects a slab to contain a [`crate::stage::Camera`] followed by a `f32`
/// roughness value.
#[spirv(fragment)]
pub fn fragment_prefilter_environment_cubemap(
#[spirv(uniform, descriptor_set = 0, binding = 1)] roughness: &f32,
#[spirv(descriptor_set = 0, binding = 2)] environment_cubemap: &Cubemap,
#[spirv(descriptor_set = 0, binding = 3)] sampler: &Sampler,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
#[spirv(descriptor_set = 0, binding = 1)] environment_cubemap: &Cubemap,
#[spirv(descriptor_set = 0, binding = 2)] sampler: &Sampler,
in_pos: Vec3,
frag_color: &mut Vec4,
) {
let roughness = slab.read(Id::<f32>::from(crate::stage::Camera::slab_size()));
let mut n = in_pos.alt_norm_or_zero();
// `wgpu` and vulkan's y coords are flipped from opengl
n.y *= -1.0;
Expand All @@ -181,12 +191,12 @@ pub fn fragment_prefilter_environment_cubemap(

for i in 0..SAMPLE_COUNT {
let xi = hammersley(i, SAMPLE_COUNT);
let h = importance_sample_ggx(xi, n, *roughness);
let h = importance_sample_ggx(xi, n, roughness);
let l = (2.0 * v.dot(h) * h - v).alt_norm_or_zero();

let n_dot_l = n.dot(l).max(0.0);
if n_dot_l > 0.0 {
let mip_level = if *roughness == 0.0 {
let mip_level = if roughness == 0.0 {
0.0
} else {
calc_lod(n_dot_l)
Expand Down Expand Up @@ -273,15 +283,44 @@ pub fn fragment_bloom(
*frag_color = result.extend(1.0);
}

#[repr(C)]
#[derive(Clone, Copy)]
struct Vert {
pos: [f32; 3],
uv: [f32; 2],
}

/// A screen-space quad.
const BRDF_VERTS: [Vert; 6] = {
let bl = Vert {
pos: [-1.0, -1.0, 0.0],
uv: [0.0, 1.0],
};
let br = Vert {
pos: [1.0, -1.0, 0.0],
uv: [1.0, 1.0],
};
let tl = Vert {
pos: [-1.0, 1.0, 0.0],
uv: [0.0, 0.0],
};
let tr = Vert {
pos: [1.0, 1.0, 0.0],
uv: [1.0, 0.0],
};

[bl, br, tr, bl, tr, tl]
};

#[spirv(vertex)]
pub fn vertex_brdf_lut_convolution(
in_pos: glam::Vec3,
in_uv: glam::Vec2,
#[spirv(vertex_index)] vertex_id: u32,
out_uv: &mut glam::Vec2,
#[spirv(position)] gl_pos: &mut glam::Vec4,
) {
*out_uv = in_uv;
*gl_pos = in_pos.extend(1.0);
let Vert { pos, uv } = BRDF_VERTS[vertex_id as usize];
*out_uv = Vec2::from(uv);
*gl_pos = Vec3::from(pos).extend(1.0);
}

#[spirv(fragment)]
Expand Down
12 changes: 6 additions & 6 deletions crates/renderling-shader/src/skybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ pub fn fragment_cubemap(
*out_color = env_color.extend(1.0);
}

/// Passes the singular `Vec3` position attribute to the fragment shader
/// unchanged, while transforming `gl_pos` by the camera projection*view;
/// Draws a cubemap.
///
/// Expects there to be a [`Camera`] in the slab at index 0.
///
/// Used to create a cubemap from an equirectangular image as well as cubemap
/// convolutions.
#[spirv(vertex)]
pub fn vertex_position_passthru(
pub fn vertex_cubemap(
#[spirv(vertex_index)] vertex_index: u32,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
in_pos: Vec3,
local_pos: &mut Vec3,
#[spirv(position)] gl_pos: &mut Vec4,
) {
let camera = slab.read(Id::<Camera>::new(0));
*local_pos = in_pos;
*gl_pos = camera.projection * camera.view * in_pos.extend(1.0);
let pos = crate::math::CUBE[vertex_index as usize];
*local_pos = pos;
*gl_pos = camera.projection * camera.view * pos.extend(1.0);
}

/// Colors a skybox using an equirectangular texture.
Expand Down
3 changes: 2 additions & 1 deletion crates/renderling-shader/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! This is mostly for rendering text.
use crabslab::SlabItem;
use glam::{Mat4, UVec2, Vec2, Vec4};
use spirv_std::{image::Image2d, spirv, Sampler};

Expand Down Expand Up @@ -43,7 +44,7 @@ impl UiVertex {
}

#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, SlabItem)]
pub struct UiConstants {
pub canvas_size: UVec2,
pub camera_translation: Vec2,
Expand Down
Loading

0 comments on commit a5fd8a2

Please sign in to comment.