Skip to content

Commit 90f5ea5

Browse files
mvaligurskyMartin Valigursky
andauthored
WGSL version of an internal floatAsUintPS shader chunk (#7631)
Co-authored-by: Martin Valigursky <[email protected]>
1 parent d3161ef commit 90f5ea5

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import envAtlasPS from './common/frag/envAtlas.js';
3838
import envProcPS from './common/frag/envProc.js';
3939
import falloffInvSquaredPS from './lit/frag/falloffInvSquared.js';
4040
import falloffLinearPS from './lit/frag/falloffLinear.js';
41-
// import floatAsUintPS from './common/frag/float-as-uint.js';
41+
import floatAsUintPS from './common/frag/float-as-uint.js';
4242
import fogPS from './common/frag/fog.js';
4343
import fresnelSchlickPS from './lit/frag/fresnelSchlick.js';
4444
import fullscreenQuadVS from './common/vert/fullscreenQuad.js';
@@ -250,7 +250,7 @@ const shaderChunksWGSL = {
250250
envProcPS,
251251
falloffInvSquaredPS,
252252
falloffLinearPS,
253-
// floatAsUintPS,
253+
floatAsUintPS,
254254
fogPS,
255255
fresnelSchlickPS,
256256
fullscreenQuadVS,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Chunk that allows us to store all 32bits of float in a single RGBA8 texture without any loss of
2+
// precision. The float value is encoded to RGBA8 and decoded back to float. Used as a fallback
3+
// for platforms that do not support float textures but need to render to a float texture (without
4+
// filtering)
5+
export default /* wgsl */`
6+
7+
#ifndef FLOAT_AS_UINT
8+
#define FLOAT_AS_UINT
9+
10+
// encode float value to RGBA8 representation (0.0-1.0 range)
11+
fn float2uint(value: f32) -> vec4f {
12+
let intBits = bitcast<u32>(value);
13+
return vec4f(
14+
f32((intBits >> 24u) & 0xffu),
15+
f32((intBits >> 16u) & 0xffu),
16+
f32((intBits >> 8u) & 0xffu),
17+
f32(intBits & 0xffu)
18+
) / 255.0;
19+
}
20+
21+
// decode RGBA8 value to float
22+
fn uint2float(value: vec4f) -> f32 {
23+
let rgba_u32 = vec4<u32>(value * 255.0);
24+
let intBits: u32 =
25+
(rgba_u32.r << 24u) |
26+
(rgba_u32.g << 16u) |
27+
(rgba_u32.b << 8u) |
28+
rgba_u32.a;
29+
return bitcast<f32>(intBits);
30+
}
31+
32+
// store a single float value in vec4, assuming either RGBA8 or float renderable texture
33+
fn float2vec4(value: f32) -> vec4f {
34+
#if defined(CAPS_TEXTURE_FLOAT_RENDERABLE)
35+
return vec4f(value, 1.0, 1.0, 1.0);
36+
#else
37+
return float2uint(value);
38+
#endif
39+
}
40+
41+
#endif // FLOAT_AS_UINT
42+
`;

src/scene/shader-lib/chunks-wgsl/lit/vert/litMain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn vertexMain(input : VertexInput) -> VertexOutput {
9999
100100
#ifdef LINEAR_DEPTH
101101
// linear depth from the worldPosition, see getLinearDepth
102-
output.vLinearDepth = -(matrix_view * vec4f(vPositionW, 1.0)).z;
102+
output.vLinearDepth = -(uniform.matrix_view * vec4f(output.vPositionW, 1.0)).z;
103103
#endif
104104
105105
#ifdef MSDF

0 commit comments

Comments
 (0)