diff --git a/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js b/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js index 81a35e0bd88..1d76ca6b4f7 100644 --- a/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js +++ b/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js @@ -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'; @@ -250,7 +250,7 @@ const shaderChunksWGSL = { envProcPS, falloffInvSquaredPS, falloffLinearPS, - // floatAsUintPS, + floatAsUintPS, fogPS, fresnelSchlickPS, fullscreenQuadVS, diff --git a/src/scene/shader-lib/chunks-wgsl/common/frag/float-as-uint.js b/src/scene/shader-lib/chunks-wgsl/common/frag/float-as-uint.js new file mode 100644 index 00000000000..ad877f63dff --- /dev/null +++ b/src/scene/shader-lib/chunks-wgsl/common/frag/float-as-uint.js @@ -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(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(value * 255.0); + let intBits: u32 = + (rgba_u32.r << 24u) | + (rgba_u32.g << 16u) | + (rgba_u32.b << 8u) | + rgba_u32.a; + return bitcast(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 +`; diff --git a/src/scene/shader-lib/chunks-wgsl/lit/vert/litMain.js b/src/scene/shader-lib/chunks-wgsl/lit/vert/litMain.js index 2c00013700c..041b6f0ddff 100644 --- a/src/scene/shader-lib/chunks-wgsl/lit/vert/litMain.js +++ b/src/scene/shader-lib/chunks-wgsl/lit/vert/litMain.js @@ -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