Skip to content

Commit 6039c18

Browse files
committed
handwritten bit pack/unpacking
1 parent 36cd601 commit 6039c18

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

crates/bevy_pbr/src/ssao/gtao.wgsl

+9-1
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,20 @@ fn calculate_neighboring_depth_differences(pixel_coordinates: vec2<i32>) -> f32
5353
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
5454

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

5959
return depth_center;
6060
}
6161

62+
// TODO: Remove this once https://github.com/gfx-rs/naga/pull/2353 lands
63+
fn mypack4x8unorm(e: vec4<f32>) -> u32 {
64+
return u32(clamp(e.x, 0.0, 1.0) * 255.0 + 0.5) |
65+
u32(clamp(e.y, 0.0, 1.0) * 255.0 + 0.5) << 8u |
66+
u32(clamp(e.z, 0.0, 1.0) * 255.0 + 0.5) << 16u |
67+
u32(clamp(e.w, 0.0, 1.0) * 255.0 + 0.5) << 24u;
68+
}
69+
6270
fn load_normal_view_space(uv: vec2<f32>) -> vec3<f32> {
6371
var world_normal = textureSampleLevel(normals, point_clamp_sampler, uv, 0.0).xyz;
6472
world_normal = (world_normal * 2.0) - 1.0;

crates/bevy_pbr/src/ssao/spatial_denoise.wgsl

+13-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ fn spatial_denoise(@builtin(global_invocation_id) global_id: vec3<u32>) {
3131
let visibility2 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(0i, 2i));
3232
let visibility3 = textureGather(0, ambient_occlusion_noisy, point_clamp_sampler, uv, vec2<i32>(2i, 2i));
3333

34-
let left_edges = unpack4x8unorm(edges0.x);
35-
let right_edges = unpack4x8unorm(edges1.x);
36-
let top_edges = unpack4x8unorm(edges0.z);
37-
let bottom_edges = unpack4x8unorm(edges2.w);
38-
var center_edges = unpack4x8unorm(edges0.y);
34+
let left_edges = myunpack4x8unorm(edges0.x);
35+
let right_edges = myunpack4x8unorm(edges1.x);
36+
let top_edges = myunpack4x8unorm(edges0.z);
37+
let bottom_edges = myunpack4x8unorm(edges2.w);
38+
var center_edges = myunpack4x8unorm(edges0.y);
3939
center_edges *= vec4<f32>(left_edges.y, right_edges.x, top_edges.w, bottom_edges.z);
4040

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

8383
textureStore(ambient_occlusion, pixel_coordinates, vec4<f32>(denoised_visibility, 0.0, 0.0, 0.0));
8484
}
85+
86+
// TODO: Remove this once https://github.com/gfx-rs/naga/pull/2353 lands in Bevy
87+
fn myunpack4x8unorm(e: u32) -> vec4<f32> {
88+
return vec4<f32>(clamp(f32(e & 0xFFu) / 255.0, 0.0, 1.0),
89+
clamp(f32((e >> 8u) & 0xFFu) / 255.0, 0.0, 1.0),
90+
clamp(f32((e >> 16u) & 0xFFu) / 255.0, 0.0, 1.0),
91+
clamp(f32((e >> 24u) & 0xFFu) / 255.0, 0.0, 1.0));
92+
}

0 commit comments

Comments
 (0)