Skip to content

Commit

Permalink
pack as r32uint instead of rg16uint to be able to be storage-writable
Browse files Browse the repository at this point in the history
also move terrain shaders in terrain folder

still dont understand why having 2 16-bits channels is soooo different than 1 32-bits channel for texture capabilities smh
  • Loading branch information
Uriopass committed Dec 11, 2023
1 parent 6f7102b commit a200291
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
6 changes: 3 additions & 3 deletions assets/shaders/pbr/render.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "dither.wgsl"
#include "tonemap.wgsl"
#include "atmosphere.wgsl"
#include "../dither.wgsl"
#include "../tonemap.wgsl"
#include "../atmosphere.wgsl"

fn fresnelSchlick(cosTheta: f32, F0: vec3<f32>) -> vec3<f32> {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "render_params.wgsl"
#include "shadow.wgsl"
#include "pbr/render.wgsl"
#include "../render_params.wgsl"
#include "../shadow.wgsl"
#include "../pbr/render.wgsl"

struct FragmentOutput {
@location(0) out_color: vec4<f32>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "render_params.wgsl"
#include "../render_params.wgsl"
#include "unpack.wgsl"

struct Uniforms {
u_view_proj: mat4x4<f32>,
Expand Down Expand Up @@ -36,20 +37,6 @@ normal: vec3(self.cell_size * scale as f32, 0.0, hx - height)
.normalize(),
*/

const MAX_HEIGHT: f32 = 1024.0;
const MAX_DIFF: f32 = 32.0;

fn unpack_height(h: u32) -> f32 {
return ((f32(h) - 32768.0) / 32767.0 ) * MAX_HEIGHT;
}

fn unpack_diffs(v: u32) -> vec2<f32> {
let x = v & 0xFFu;
let y = (v & 0xFF00u) >> 8u;
return vec2<f32>((f32(x) - 128.0) / 127.0 * MAX_DIFF,
(f32(y) - 128.0) / 127.0 * MAX_DIFF);
}

@vertex
fn vert(@builtin(vertex_index) vid: u32,
@location(0) in_off: vec2<f32>,
Expand All @@ -75,19 +62,15 @@ fn vert(@builtin(vertex_index) vid: u32,

let tpos: vec2<i32> = in_position * i32(cdata.lod_pow2) + vec2<i32>(in_off * cdata.inv_cell_size);

let texLoad: vec2<u32> = textureLoad(t_terraindata, tpos, 0).rg;

let height: f32 = unpack_height(texLoad.r);
let diffs: vec2<f32> = unpack_diffs(texLoad.g);
let h_dx_dy: vec3<f32> = unpack(textureLoad(t_terraindata, tpos, 0).r, 1.0);

let world_pos: vec3<f32> = vec3(vec2<f32>(in_position * i32(cdata.lod_pow2)) * cdata.cell_size + in_off, height);
let world_pos: vec3<f32> = vec3(vec2<f32>(in_position * i32(cdata.lod_pow2)) * cdata.cell_size + in_off, h_dx_dy.x);
let clip_pos: vec4<f32> = global.u_view_proj * vec4(world_pos, 1.0);

//let dist_to_cam: f32 = length(params.cam_pos.xyz - vec3(pos.xy, 0.0));
//let transition_alpha: f32 = smoothstep(cdata.distance_lod_cutoff * 0.8, cdata.distance_lod_cutoff, dist_to_cam);

var out_normal: vec3<f32> = normalize(vec3(diffs.x, diffs.y, cdata.cell_size * 2.0)); // https://stackoverflow.com/questions/49640250/calculate-normals-from-heightmap

let position: vec4<f32> = global.u_view_proj * vec4(world_pos, 1.0);
var out_normal: vec3<f32> = normalize(vec3(h_dx_dy.yz, cdata.cell_size * 2.0)); // https://stackoverflow.com/questions/49640250/calculate-normals-from-heightmap

#ifdef DEBUG
var debug = 0.0;
Expand All @@ -104,5 +87,5 @@ fn vert(@builtin(vertex_index) vid: u32,
#ifdef DEBUG
debug,
#endif
position);
clip_pos);
}
18 changes: 18 additions & 0 deletions assets/shaders/terrain/unpack.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const MAX_HEIGHT: f32 = 1024.0;
const MAX_DIFF: f32 = 32.0;

fn unpack_height(h: u32) -> f32 {
return ((f32(h) - 32768.0) / 32767.0 ) * MAX_HEIGHT;
}

fn unpack_diffs(v: u32, lod_pow2: f32) -> vec2<f32> {
let x = (f32(v >> 8u) - 128.0) / 127.0 * (MAX_DIFF * lod_pow2);
let y = (f32(v & 0xFFu) - 128.0) / 127.0 * (MAX_DIFF * lod_pow2);
return vec2<f32>(x, y);
}

fn unpack(v: u32, lod_pow2: f32) -> vec3<f32> {
let h = unpack_height(v & 0xFFFFu);
let d = unpack_diffs(v >> 16u, lod_pow2);
return vec3(h, d);
}
27 changes: 17 additions & 10 deletions engine/src/drawables/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
w * CRESOLUTION as u32,
h * CRESOLUTION as u32,
1,
TextureFormat::Rg16Uint,
TextureFormat::R32Uint,
)
.with_usage(TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING)
.with_fixed_mipmaps(LOD as u32)
Expand Down Expand Up @@ -123,17 +123,24 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
get_left: impl Fn(usize) -> Option<f32>,
) -> bool {
fn pack(height: f32, diffx: f32, diffy: f32) -> [u8; 4] {
let a = ((height.clamp(-MAX_HEIGHT, MAX_HEIGHT) / MAX_HEIGHT * i16::MAX as f32
+ 32768.0) as u16)
.to_le_bytes();
let h_encoded = ((height.clamp(-MAX_HEIGHT, MAX_HEIGHT) / MAX_HEIGHT * i16::MAX as f32

Check warning on line 126 in engine/src/drawables/terrain.rs

View workflow job for this annotation

GitHub Actions / build

unnecessary parentheses around assigned value

Check warning on line 126 in engine/src/drawables/terrain.rs

View workflow job for this annotation

GitHub Actions / build

unnecessary parentheses around assigned value

Check warning on line 126 in engine/src/drawables/terrain.rs

View workflow job for this annotation

GitHub Actions / build

unnecessary parentheses around assigned value
+ 32768.0) as u16);

let dx_encoded: u8;
let dy_encoded: u8;

if height >= MAX_HEIGHT || height <= -MAX_HEIGHT {
return [a[0], a[1], 128, 128]; // normal is zero if we hit max height
dx_encoded = 128;
dy_encoded = 128; // normal is zero if we hit max height
} else {
dx_encoded =
(diffx.clamp(-MAX_DIFF, MAX_DIFF) / MAX_DIFF * i8::MAX as f32 + 128.0) as u8;
dy_encoded =
(diffy.clamp(-MAX_DIFF, MAX_DIFF) / MAX_DIFF * i8::MAX as f32 + 128.0) as u8;
}

let b = (diffx.clamp(-MAX_DIFF, MAX_DIFF) / MAX_DIFF * i8::MAX as f32 + 128.0) as u8;
let c = (diffy.clamp(-MAX_DIFF, MAX_DIFF) / MAX_DIFF * i8::MAX as f32 + 128.0) as u8;
[a[0], a[1], b, c]
let packed = (dx_encoded as u32) << 24 | (dy_encoded as u32) << 16 | h_encoded as u32;
packed.to_le_bytes()
}

let mut contents = Vec::with_capacity(CRESOLUTION * CRESOLUTION);
Expand Down Expand Up @@ -386,10 +393,10 @@ impl PipelineBuilder for TerrainPipeline {
.collect::<Vec<_>>(),
label: Some("terrain bindgroup layout"),
});
let vert = &mk_module("terrain.vert");
let vert = &mk_module("terrain/terrain.vert");

if !self.depth {
let frag = &mk_module("terrain.frag");
let frag = &mk_module("terrain/terrain.frag");

return gfx.color_pipeline(
"terrain",
Expand Down
6 changes: 3 additions & 3 deletions engine/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ fn replace_imports(base: &Path, src: &str, deps: &mut Vec<String>) -> String {
let mut p = base.to_path_buf();
p.pop();
p.push(loc);
let mut s = std::fs::read_to_string(p)
.unwrap_or_else(|_| panic!("could not find included file {loc}"));
s = replace_imports(base, &s, deps);
let mut s = std::fs::read_to_string(&p)
.unwrap_or_else(|_| panic!("could not find included file {loc} for {base:?}"));
s = replace_imports(&p, &s, deps);
return Cow::Owned(s);
}
Cow::Borrowed(x)
Expand Down

0 comments on commit a200291

Please sign in to comment.