Skip to content

Commit a5fd8a2

Browse files
committed
WIP
1 parent 0108d78 commit a5fd8a2

40 files changed

+567
-578
lines changed

crates/renderling-shader/src/.#skybox.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/renderling-shader/src/convolution.rs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Convolution shaders.
22
//!
33
//! These shaders convolve various functions to produce cached maps.
4+
use crabslab::{Id, Slab, SlabItem};
45
use glam::{UVec2, Vec2, Vec3, Vec4, Vec4Swizzles};
56
use spirv_std::{
67
image::{Cubemap, Image2d},
@@ -11,7 +12,7 @@ use spirv_std::{
1112
#[cfg(target_arch = "spirv")]
1213
use spirv_std::num_traits::Float;
1314

14-
use crate::{pbr, stage::GpuConstants, IsVector};
15+
use crate::{pbr, IsVector};
1516

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

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

162168
/// Lambertian prefilter.
169+
///
170+
/// Expects a slab to contain a [`crate::stage::Camera`] followed by a `f32`
171+
/// roughness value.
163172
#[spirv(fragment)]
164173
pub fn fragment_prefilter_environment_cubemap(
165-
#[spirv(uniform, descriptor_set = 0, binding = 1)] roughness: &f32,
166-
#[spirv(descriptor_set = 0, binding = 2)] environment_cubemap: &Cubemap,
167-
#[spirv(descriptor_set = 0, binding = 3)] sampler: &Sampler,
174+
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
175+
#[spirv(descriptor_set = 0, binding = 1)] environment_cubemap: &Cubemap,
176+
#[spirv(descriptor_set = 0, binding = 2)] sampler: &Sampler,
168177
in_pos: Vec3,
169178
frag_color: &mut Vec4,
170179
) {
180+
let roughness = slab.read(Id::<f32>::from(crate::stage::Camera::slab_size()));
171181
let mut n = in_pos.alt_norm_or_zero();
172182
// `wgpu` and vulkan's y coords are flipped from opengl
173183
n.y *= -1.0;
@@ -181,12 +191,12 @@ pub fn fragment_prefilter_environment_cubemap(
181191

182192
for i in 0..SAMPLE_COUNT {
183193
let xi = hammersley(i, SAMPLE_COUNT);
184-
let h = importance_sample_ggx(xi, n, *roughness);
194+
let h = importance_sample_ggx(xi, n, roughness);
185195
let l = (2.0 * v.dot(h) * h - v).alt_norm_or_zero();
186196

187197
let n_dot_l = n.dot(l).max(0.0);
188198
if n_dot_l > 0.0 {
189-
let mip_level = if *roughness == 0.0 {
199+
let mip_level = if roughness == 0.0 {
190200
0.0
191201
} else {
192202
calc_lod(n_dot_l)
@@ -273,15 +283,44 @@ pub fn fragment_bloom(
273283
*frag_color = result.extend(1.0);
274284
}
275285

286+
#[repr(C)]
287+
#[derive(Clone, Copy)]
288+
struct Vert {
289+
pos: [f32; 3],
290+
uv: [f32; 2],
291+
}
292+
293+
/// A screen-space quad.
294+
const BRDF_VERTS: [Vert; 6] = {
295+
let bl = Vert {
296+
pos: [-1.0, -1.0, 0.0],
297+
uv: [0.0, 1.0],
298+
};
299+
let br = Vert {
300+
pos: [1.0, -1.0, 0.0],
301+
uv: [1.0, 1.0],
302+
};
303+
let tl = Vert {
304+
pos: [-1.0, 1.0, 0.0],
305+
uv: [0.0, 0.0],
306+
};
307+
let tr = Vert {
308+
pos: [1.0, 1.0, 0.0],
309+
uv: [1.0, 0.0],
310+
};
311+
312+
[bl, br, tr, bl, tr, tl]
313+
};
314+
276315
#[spirv(vertex)]
277316
pub fn vertex_brdf_lut_convolution(
278-
in_pos: glam::Vec3,
279-
in_uv: glam::Vec2,
317+
#[spirv(vertex_index)] vertex_id: u32,
280318
out_uv: &mut glam::Vec2,
281319
#[spirv(position)] gl_pos: &mut glam::Vec4,
282320
) {
283-
*out_uv = in_uv;
284-
*gl_pos = in_pos.extend(1.0);
321+
let Vert { pos, uv } = BRDF_VERTS[vertex_id as usize];
322+
*out_uv = Vec2::from(uv);
323+
*gl_pos = Vec3::from(pos).extend(1.0);
285324
}
286325

287326
#[spirv(fragment)]

crates/renderling-shader/src/skybox.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ pub fn fragment_cubemap(
5454
*out_color = env_color.extend(1.0);
5555
}
5656

57-
/// Passes the singular `Vec3` position attribute to the fragment shader
58-
/// unchanged, while transforming `gl_pos` by the camera projection*view;
57+
/// Draws a cubemap.
5958
///
6059
/// Expects there to be a [`Camera`] in the slab at index 0.
6160
///
6261
/// Used to create a cubemap from an equirectangular image as well as cubemap
6362
/// convolutions.
6463
#[spirv(vertex)]
65-
pub fn vertex_position_passthru(
64+
pub fn vertex_cubemap(
65+
#[spirv(vertex_index)] vertex_index: u32,
6666
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
67-
in_pos: Vec3,
6867
local_pos: &mut Vec3,
6968
#[spirv(position)] gl_pos: &mut Vec4,
7069
) {
7170
let camera = slab.read(Id::<Camera>::new(0));
72-
*local_pos = in_pos;
73-
*gl_pos = camera.projection * camera.view * in_pos.extend(1.0);
71+
let pos = crate::math::CUBE[vertex_index as usize];
72+
*local_pos = pos;
73+
*gl_pos = camera.projection * camera.view * pos.extend(1.0);
7474
}
7575

7676
/// Colors a skybox using an equirectangular texture.

crates/renderling-shader/src/ui.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! This is mostly for rendering text.
44
5+
use crabslab::SlabItem;
56
use glam::{Mat4, UVec2, Vec2, Vec4};
67
use spirv_std::{image::Image2d, spirv, Sampler};
78

@@ -43,7 +44,7 @@ impl UiVertex {
4344
}
4445

4546
#[repr(C)]
46-
#[derive(Clone, Copy)]
47+
#[derive(Clone, Copy, SlabItem)]
4748
pub struct UiConstants {
4849
pub canvas_size: UVec2,
4950
pub camera_translation: Vec2,

0 commit comments

Comments
 (0)