Skip to content

Commit 19b5a0b

Browse files
mvaligurskyMartin Valigursky
andauthored
Small refactor of internal reproject code / shader (#7429)
Co-authored-by: Martin Valigursky <[email protected]>
1 parent 98d1238 commit 19b5a0b

File tree

5 files changed

+55
-48
lines changed

5 files changed

+55
-48
lines changed

src/scene/graphics/reproject-texture.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -366,19 +366,6 @@ const generateGGXSamplesTex = (device, numSamples, specularPower, sourceTotalPix
366366
});
367367
};
368368

369-
const vsCode = `
370-
attribute vec2 vertex_position;
371-
372-
uniform vec4 uvMod;
373-
374-
varying vec2 vUv0;
375-
376-
void main(void) {
377-
gl_Position = vec4(vertex_position, 0.5, 1.0);
378-
vUv0 = getImageEffectUV((vertex_position.xy * 0.5 + 0.5) * uvMod.xy + uvMod.zw);
379-
}
380-
`;
381-
382369
/**
383370
* This function reprojects textures between cubemap, equirectangular and octahedral formats. The
384371
* function can read and write textures with pixel data in RGBE, RGBM, linear and sRGB formats.
@@ -441,22 +428,29 @@ function reprojectTexture(source, target, options = {}) {
441428
let shader = getProgramLibrary(device).getCachedShader(shaderKey);
442429
if (!shader) {
443430
const defines = `
444-
#define PROCESS_FUNC ${processFunc}
445431
${prefilterSamples ? '#define USE_SAMPLES_TEX' : ''}
446432
${source.cubemap ? '#define CUBEMAP_SOURCE' : ''}
447-
#define DECODE_FUNC ${decodeFunc}
448-
#define ENCODE_FUNC ${encodeFunc}
449-
#define SOURCE_FUNC ${sourceFunc}
450-
#define TARGET_FUNC ${targetFunc}
451-
#define NUM_SAMPLES ${numSamples}
452-
#define NUM_SAMPLES_SQRT ${Math.round(Math.sqrt(numSamples)).toFixed(1)}
433+
#define {PROCESS_FUNC} ${processFunc}
434+
#define {DECODE_FUNC} ${decodeFunc}
435+
#define {ENCODE_FUNC} ${encodeFunc}
436+
#define {SOURCE_FUNC} ${sourceFunc}
437+
#define {TARGET_FUNC} ${targetFunc}
438+
#define {NUM_SAMPLES} ${numSamples}
439+
#define {NUM_SAMPLES_SQRT} ${Math.round(Math.sqrt(numSamples)).toFixed(1)}
453440
`;
454441

442+
const includes = new Map();
443+
includes.set('decodePS', shaderChunks.decodePS);
444+
includes.set('encodePS', shaderChunks.encodePS);
445+
455446
shader = createShaderFromCode(
456447
device,
457-
vsCode,
448+
shaderChunks.reprojectVS,
458449
`${defines}\n${shaderChunks.reprojectPS}`,
459-
shaderKey
450+
shaderKey,
451+
undefined, {
452+
fragmentIncludes: includes
453+
}
460454
);
461455
}
462456

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ import immediateLineVS from './internal/vert/immediateLine.js';
155155
// import refractionCubePS from './lit/frag/refractionCube.js';
156156
// import refractionDynamicPS from './lit/frag/refractionDynamic.js';
157157
// import reprojectPS from './common/frag/reproject.js';
158+
// import reprojectVS from './common/vert/reproject.js';
158159
// import sampleCatmullRomPS from './common/frag/sampleCatmullRom.js';
159160
// import screenDepthPS from './common/frag/screenDepth.js';
160161
// import shadowCascadesPS from './lit/frag/shadowCascades.js';
@@ -364,6 +365,7 @@ const shaderChunksWGSL = {
364365
// refractionCubePS,
365366
// refractionDynamicPS,
366367
// reprojectPS,
368+
// reprojectVS,
367369
// sampleCatmullRomPS,
368370
// screenDepthPS,
369371
// shadowCascadesPS,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ import reflectionSheenPS from './lit/frag/reflectionSheen.js';
155155
import refractionCubePS from './lit/frag/refractionCube.js';
156156
import refractionDynamicPS from './lit/frag/refractionDynamic.js';
157157
import reprojectPS from './common/frag/reproject.js';
158+
import reprojectVS from './common/vert/reproject.js';
158159
import sampleCatmullRomPS from './common/frag/sampleCatmullRom.js';
159160
import screenDepthPS from './common/frag/screenDepth.js';
160161
import shadowCascadesPS from './lit/frag/lighting/shadowCascades.js';
@@ -363,6 +364,7 @@ const shaderChunks = {
363364
refractionCubePS,
364365
refractionDynamicPS,
365366
reprojectPS,
367+
reprojectVS,
366368
sampleCatmullRomPS,
367369
screenDepthPS,
368370
shadowCascadesPS,

src/scene/shader-lib/chunks/common/frag/reproject.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import decode from './decode.js';
2-
import encode from './encode.js';
3-
4-
export default /* glsl */`
51
// This shader requires the following #DEFINEs:
62
//
73
// PROCESS_FUNC - must be one of reproject, prefilter
@@ -13,6 +9,7 @@ export default /* glsl */`
139
// When filtering:
1410
// NUM_SAMPLES - number of samples
1511
// NUM_SAMPLES_SQRT - sqrt of number of samples
12+
export default /* glsl */`
1613
1714
varying vec2 vUv0;
1815
@@ -45,8 +42,8 @@ float saturate(float x) {
4542
return clamp(x, 0.0, 1.0);
4643
}
4744
48-
${decode}
49-
${encode}
45+
#include "decodePS"
46+
#include "encodePS"
5047
5148
//-- supported projections
5249
@@ -116,8 +113,8 @@ vec2 octEncode(in vec3 v) {
116113
}
117114
118115
vec4 sampleCubemap(vec2 sph) {
119-
return sampleCubemap(fromSpherical(sph));
120-
}
116+
return sampleCubemap(fromSpherical(sph));
117+
}
121118
122119
vec4 sampleCubemap(vec3 dir, float mipLevel) {
123120
return textureCubeLod(sourceCube, modifySeams(dir, 1.0), mipLevel);
@@ -204,24 +201,24 @@ mat3 matrixFromVectorSlow(vec3 n) {
204201
}
205202
206203
vec4 reproject() {
207-
if (NUM_SAMPLES <= 1) {
204+
if ({NUM_SAMPLES} <= 1) {
208205
// single sample
209-
return ENCODE_FUNC(DECODE_FUNC(SOURCE_FUNC(TARGET_FUNC())));
206+
return {ENCODE_FUNC}({DECODE_FUNC}({SOURCE_FUNC}({TARGET_FUNC}())));
210207
} else {
211208
// multi sample
212-
vec3 t = TARGET_FUNC();
209+
vec3 t = {TARGET_FUNC}();
213210
vec3 tu = dFdx(t);
214211
vec3 tv = dFdy(t);
215212
216213
vec3 result = vec3(0.0);
217-
for (float u = 0.0; u < NUM_SAMPLES_SQRT; ++u) {
218-
for (float v = 0.0; v < NUM_SAMPLES_SQRT; ++v) {
219-
result += DECODE_FUNC(SOURCE_FUNC(normalize(t +
220-
tu * (u / NUM_SAMPLES_SQRT - 0.5) +
221-
tv * (v / NUM_SAMPLES_SQRT - 0.5))));
214+
for (float u = 0.0; u < {NUM_SAMPLES_SQRT}; ++u) {
215+
for (float v = 0.0; v < {NUM_SAMPLES_SQRT}; ++v) {
216+
result += {DECODE_FUNC}({SOURCE_FUNC}(normalize(t +
217+
tu * (u / {NUM_SAMPLES_SQRT} - 0.5) +
218+
tv * (v / {NUM_SAMPLES_SQRT} - 0.5))));
222219
}
223220
}
224-
return ENCODE_FUNC(result / (NUM_SAMPLES_SQRT * NUM_SAMPLES_SQRT));
221+
return {ENCODE_FUNC}(result / ({NUM_SAMPLES_SQRT} * {NUM_SAMPLES_SQRT}));
225222
}
226223
}
227224
@@ -245,42 +242,42 @@ vec4 unpackFloat = vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 16581375.0);
245242
// convolve an environment given pre-generated samples
246243
vec4 prefilterSamples() {
247244
// construct vector space given target direction
248-
mat3 vecSpace = matrixFromVectorSlow(TARGET_FUNC());
245+
mat3 vecSpace = matrixFromVectorSlow({TARGET_FUNC}());
249246
250247
vec3 L;
251248
float mipLevel;
252249
253250
vec3 result = vec3(0.0);
254251
float totalWeight = 0.0;
255-
for (int i = 0; i < NUM_SAMPLES; ++i) {
252+
for (int i = 0; i < {NUM_SAMPLES}; ++i) {
256253
unpackSample(i, L, mipLevel);
257-
result += DECODE_FUNC(SOURCE_FUNC(vecSpace * L, mipLevel)) * L.z;
254+
result += {DECODE_FUNC}({SOURCE_FUNC}(vecSpace * L, mipLevel)) * L.z;
258255
totalWeight += L.z;
259256
}
260257
261-
return ENCODE_FUNC(result / totalWeight);
258+
return {ENCODE_FUNC}(result / totalWeight);
262259
}
263260
264261
// unweighted version of prefilterSamples
265262
vec4 prefilterSamplesUnweighted() {
266263
// construct vector space given target direction
267-
mat3 vecSpace = matrixFromVectorSlow(TARGET_FUNC());
264+
mat3 vecSpace = matrixFromVectorSlow({TARGET_FUNC}());
268265
269266
vec3 L;
270267
float mipLevel;
271268
272269
vec3 result = vec3(0.0);
273270
float totalWeight = 0.0;
274-
for (int i = 0; i < NUM_SAMPLES; ++i) {
271+
for (int i = 0; i < {NUM_SAMPLES}; ++i) {
275272
unpackSample(i, L, mipLevel);
276-
result += DECODE_FUNC(SOURCE_FUNC(vecSpace * L, mipLevel));
273+
result += {DECODE_FUNC}({SOURCE_FUNC}(vecSpace * L, mipLevel));
277274
}
278275
279-
return ENCODE_FUNC(result / float(NUM_SAMPLES));
276+
return {ENCODE_FUNC}(result / float({NUM_SAMPLES}));
280277
}
281278
#endif
282279
283280
void main(void) {
284-
gl_FragColor = PROCESS_FUNC();
281+
gl_FragColor = {PROCESS_FUNC}();
285282
}
286283
`;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default /* glsl */`
2+
attribute vec2 vertex_position;
3+
4+
uniform vec4 uvMod;
5+
6+
varying vec2 vUv0;
7+
8+
void main(void) {
9+
gl_Position = vec4(vertex_position, 0.5, 1.0);
10+
vUv0 = getImageEffectUV((vertex_position.xy * 0.5 + 0.5) * uvMod.xy + uvMod.zw);
11+
}
12+
`;

0 commit comments

Comments
 (0)