Skip to content

Commit

Permalink
handwritten bit pack/unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Elabajaba committed Jun 6, 2023
1 parent 61a73f9 commit 70915a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 9 additions & 1 deletion crates/bevy_pbr/src/ssao/gtao.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ fn calculate_neighboring_depth_differences(pixel_coordinates: vec2<i32>) -> f32
edge_info = saturate((1.0 + bias) - edge_info / scale); // Apply the bias and scale, and invert edge_info so that small values become large, and vice versa

// Pack the edge info into the texture
let edge_info_packed = vec4<u32>(pack4x8unorm(edge_info), 0u, 0u, 0u);
let edge_info_packed = vec4<u32>(mypack4x8unorm(edge_info), 0u, 0u, 0u);
textureStore(depth_differences, pixel_coordinates, edge_info_packed);

return depth_center;
}

// TODO: Remove this once https://github.com/gfx-rs/naga/pull/2353 lands
fn mypack4x8unorm(e: vec4<f32>) -> u32 {
return u32(clamp(e.x, 0.0, 1.0) * 255.0 + 0.5) |
u32(clamp(e.y, 0.0, 1.0) * 255.0 + 0.5) << 8u |
u32(clamp(e.z, 0.0, 1.0) * 255.0 + 0.5) << 16u |
u32(clamp(e.w, 0.0, 1.0) * 255.0 + 0.5) << 24u;
}

fn load_normal_view_space(uv: vec2<f32>) -> vec3<f32> {
var world_normal = textureSampleLevel(normals, point_clamp_sampler, uv, 0.0).xyz;
world_normal = (world_normal * 2.0) - 1.0;
Expand Down
18 changes: 13 additions & 5 deletions crates/bevy_pbr/src/ssao/spatial_denoise.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ fn spatial_denoise(@builtin(global_invocation_id) global_id: vec3<u32>) {
let visibility2 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(0i, 2i));
let visibility3 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(2i, 2i));

let left_edges = unpack4x8unorm(edges0.x);
let right_edges = unpack4x8unorm(edges1.x);
let top_edges = unpack4x8unorm(edges0.z);
let bottom_edges = unpack4x8unorm(edges2.w);
var center_edges = unpack4x8unorm(edges0.y);
let left_edges = myunpack4x8unorm(edges0.x);
let right_edges = myunpack4x8unorm(edges1.x);
let top_edges = myunpack4x8unorm(edges0.z);
let bottom_edges = myunpack4x8unorm(edges2.w);
var center_edges = myunpack4x8unorm(edges0.y);
center_edges *= vec4<f32>(left_edges.y, right_edges.x, top_edges.w, bottom_edges.z);

let center_weight = 1.2;
Expand Down Expand Up @@ -82,3 +82,11 @@ fn spatial_denoise(@builtin(global_invocation_id) global_id: vec3<u32>) {

textureStore(ambient_occlusion, pixel_coordinates, vec4<f32>(denoised_visibility, 0.0, 0.0, 0.0));
}

// TODO: Remove this once https://github.com/gfx-rs/naga/pull/2353 lands in Bevy
fn myunpack4x8unorm(e: u32) -> vec4<f32> {
return vec4<f32>(clamp(f32(e & 0xFFu) / 255.0, 0.0, 1.0),
clamp(f32((e >> 8u) & 0xFFu) / 255.0, 0.0, 1.0),
clamp(f32((e >> 16u) & 0xFFu) / 255.0, 0.0, 1.0),
clamp(f32((e >> 24u) & 0xFFu) / 255.0, 0.0, 1.0));
}

0 comments on commit 70915a3

Please sign in to comment.