Skip to content

Commit a3e384a

Browse files
committed
fixed problems with the skybox after slabbing
1 parent 745ae0c commit a3e384a

27 files changed

+69
-93
lines changed

crates/renderling-shader/src/convolution.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use spirv_std::{
1212
#[cfg(target_arch = "spirv")]
1313
use spirv_std::num_traits::Float;
1414

15-
use crate::{pbr, IsVector};
15+
use crate::{pbr, stage::Camera, IsVector};
1616

1717
fn radical_inverse_vdc(mut bits: u32) -> f32 {
1818
bits = (bits << 16u32) | (bits >> 16u32);
@@ -149,40 +149,46 @@ pub fn integrate_brdf_doesnt_work(mut n_dot_v: f32, roughness: f32) -> Vec2 {
149149
Vec2::new(a, b)
150150
}
151151

152-
/// Expects a slab to contain a [`crate::stage::Camera`] followed by a `f32`
152+
/// Used by [`vertex_prefilter_environment_cubemap`] to read the camera and
153+
/// roughness values from the slab.
154+
#[derive(Default, SlabItem)]
155+
pub struct VertexPrefilterEnvironmentCubemapIds {
156+
pub camera: Id<Camera>,
157+
pub roughness: Id<f32>,
158+
}
159+
160+
/// Uses the `instance_index` as the [`Id`] of a [`PrefilterEnvironmentIds`].
153161
/// roughness value.
154-
// TODO: merge this with the standard pass-thru cubemap vertex shader.
155162
#[spirv(vertex)]
156163
pub fn vertex_prefilter_environment_cubemap(
164+
#[spirv(instance_index)] instance_index: u32,
157165
#[spirv(vertex_index)] vertex_id: u32,
158166
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
159167
out_pos: &mut Vec3,
168+
out_roughness: &mut f32,
160169
#[spirv(position)] gl_pos: &mut Vec4,
161170
) {
162171
let in_pos = crate::math::CUBE[vertex_id as usize];
163-
let camera = slab.read::<crate::stage::Camera>(0u32.into());
172+
let VertexPrefilterEnvironmentCubemapIds { camera, roughness } =
173+
slab.read(Id::new(instance_index));
174+
let camera = slab.read(camera);
175+
*out_roughness = slab.read(roughness);
164176
*out_pos = in_pos;
165177
*gl_pos = camera.projection * camera.view * in_pos.extend(1.0);
166178
}
167179

168180
/// Lambertian prefilter.
169-
///
170-
/// Expects a slab to contain a [`crate::stage::Camera`] followed by a `f32`
171-
/// roughness value.
172181
#[spirv(fragment)]
173182
pub fn fragment_prefilter_environment_cubemap(
174-
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
175183
#[spirv(descriptor_set = 0, binding = 1)] environment_cubemap: &Cubemap,
176184
#[spirv(descriptor_set = 0, binding = 2)] sampler: &Sampler,
177185
in_pos: Vec3,
186+
in_roughness: f32,
178187
frag_color: &mut Vec4,
179188
) {
180-
let roughness = slab.read(Id::<f32>::from(crate::stage::Camera::slab_size()));
181189
let mut n = in_pos.alt_norm_or_zero();
182190
// `wgpu` and vulkan's y coords are flipped from opengl
183191
n.y *= -1.0;
184-
// These moves are redundant but the names have connections to the PBR
185-
// equations.
186192
let r = n;
187193
let v = r;
188194

@@ -191,12 +197,12 @@ pub fn fragment_prefilter_environment_cubemap(
191197

192198
for i in 0..SAMPLE_COUNT {
193199
let xi = hammersley(i, SAMPLE_COUNT);
194-
let h = importance_sample_ggx(xi, n, roughness);
200+
let h = importance_sample_ggx(xi, n, in_roughness);
195201
let l = (2.0 * v.dot(h) * h - v).alt_norm_or_zero();
196202

197203
let n_dot_l = n.dot(l).max(0.0);
198204
if n_dot_l > 0.0 {
199-
let mip_level = if roughness == 0.0 {
205+
let mip_level = if in_roughness == 0.0 {
200206
0.0
201207
} else {
202208
calc_lod(n_dot_l)

crates/renderling-shader/src/skybox.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,19 @@ pub fn fragment_cubemap(
5656

5757
/// Draws a cubemap.
5858
///
59-
/// Expects there to be a [`Camera`] in the slab at index 0.
59+
/// Uses the `instance_index` as the [`Id`] for a [`Camera`].
6060
///
6161
/// Used to create a cubemap from an equirectangular image as well as cubemap
6262
/// convolutions.
6363
#[spirv(vertex)]
6464
pub fn vertex_cubemap(
65+
#[spirv(instance_index)] camera_index: u32,
6566
#[spirv(vertex_index)] vertex_index: u32,
6667
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] slab: &[u32],
6768
local_pos: &mut Vec3,
6869
#[spirv(position)] gl_pos: &mut Vec4,
6970
) {
70-
let camera = slab.read(Id::<Camera>::new(0));
71+
let camera = slab.read(Id::<Camera>::new(camera_index));
7172
let pos = crate::math::CUBE[vertex_index as usize];
7273
*local_pos = pos;
7374
*gl_pos = camera.projection * camera.view * pos.extend(1.0);
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)