Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import envAtlasPS from './common/frag/envAtlas.js';
import envProcPS from './common/frag/envProc.js';
import falloffInvSquaredPS from './lit/frag/falloffInvSquared.js';
import falloffLinearPS from './lit/frag/falloffLinear.js';
// import floatAsUintPS from './common/frag/float-as-uint.js';
import floatAsUintPS from './common/frag/float-as-uint.js';
import fogPS from './common/frag/fog.js';
import fresnelSchlickPS from './lit/frag/fresnelSchlick.js';
import fullscreenQuadVS from './common/vert/fullscreenQuad.js';
Expand Down Expand Up @@ -250,7 +250,7 @@ const shaderChunksWGSL = {
envProcPS,
falloffInvSquaredPS,
falloffLinearPS,
// floatAsUintPS,
floatAsUintPS,
fogPS,
fresnelSchlickPS,
fullscreenQuadVS,
Expand Down
42 changes: 42 additions & 0 deletions src/scene/shader-lib/chunks-wgsl/common/frag/float-as-uint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Chunk that allows us to store all 32bits of float in a single RGBA8 texture without any loss of
// precision. The float value is encoded to RGBA8 and decoded back to float. Used as a fallback
// for platforms that do not support float textures but need to render to a float texture (without
// filtering)
export default /* wgsl */`

#ifndef FLOAT_AS_UINT
#define FLOAT_AS_UINT

// encode float value to RGBA8 representation (0.0-1.0 range)
fn float2uint(value: f32) -> vec4f {
let intBits = bitcast<u32>(value);
return vec4f(
f32((intBits >> 24u) & 0xffu),
f32((intBits >> 16u) & 0xffu),
f32((intBits >> 8u) & 0xffu),
f32(intBits & 0xffu)
) / 255.0;
}

// decode RGBA8 value to float
fn uint2float(value: vec4f) -> f32 {
let rgba_u32 = vec4<u32>(value * 255.0);
let intBits: u32 =
(rgba_u32.r << 24u) |
(rgba_u32.g << 16u) |
(rgba_u32.b << 8u) |
rgba_u32.a;
return bitcast<f32>(intBits);
}

// store a single float value in vec4, assuming either RGBA8 or float renderable texture
fn float2vec4(value: f32) -> vec4f {
#if defined(CAPS_TEXTURE_FLOAT_RENDERABLE)
return vec4f(value, 1.0, 1.0, 1.0);
#else
return float2uint(value);
#endif
}

#endif // FLOAT_AS_UINT
`;
2 changes: 1 addition & 1 deletion src/scene/shader-lib/chunks-wgsl/lit/vert/litMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn vertexMain(input : VertexInput) -> VertexOutput {

#ifdef LINEAR_DEPTH
// linear depth from the worldPosition, see getLinearDepth
output.vLinearDepth = -(matrix_view * vec4f(vPositionW, 1.0)).z;
output.vLinearDepth = -(uniform.matrix_view * vec4f(output.vPositionW, 1.0)).z;
#endif

#ifdef MSDF
Expand Down