Skip to content

Commit

Permalink
Improve preprocessor to allow for define value injection (#7309)
Browse files Browse the repository at this point in the history
* Improve preprocessor to allow for define value injection

* lint

* using it for the skybox shader

---------

Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Jan 24, 2025
1 parent a354950 commit fb1ecb2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/core/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class Preprocessor {
}
});

// extract defines with name starting with __INJECT_
const injectDefines = new Map();
defines.forEach((value, key) => {
if (key.startsWith('__INJECT_')) {
injectDefines.set(key, value);
}
});

// strip comments again after the includes have been resolved
source = this.stripComments(source);

Expand All @@ -111,6 +119,9 @@ class Preprocessor {
// process array sizes
source = this.processArraySize(source, intDefines);

// inject defines
source = this.injectDefines(source, injectDefines);

return source;
}

Expand All @@ -131,6 +142,28 @@ class Preprocessor {
return source;
}

static injectDefines(source, injectDefines) {

if (source !== null && injectDefines.size > 0) {

// replace all instances of the injected defines with the value itself
const lines = source.split('\n');
injectDefines.forEach((value, key) => {
const regex = new RegExp(`\\b${key}\\b`, 'g');
for (let i = 0; i < lines.length; i++) {

// replace them on lines that do not contain a preprocessor directive (the define itself for example)
if (!lines[i].includes('#')) {
lines[i] = lines[i].replace(regex, value);
}
}
});
source = lines.join('\n');
}

return source;
}

static RemoveEmptyLines(source) {

if (source !== null) {
Expand Down
4 changes: 2 additions & 2 deletions src/scene/shader-lib/chunks/skybox/frag/skybox.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export default /* glsl */`
#endif
dir.x *= -1.0;
vec3 linear = SKYBOX_DECODE_FNC(textureCube(texture_cubeMap, dir));
vec3 linear = __INJECT_SKYBOX_DECODE_FNC(textureCube(texture_cubeMap, dir));
#else // env-atlas
vec3 dir = vViewDir * vec3(-1.0, 1.0, 1.0);
vec2 uv = toSphericalUv(normalize(dir));
vec3 linear = SKYBOX_DECODE_FNC(texture2D(texture_envAtlas, mapRoughnessUv(uv, mipLevel)));
vec3 linear = __INJECT_SKYBOX_DECODE_FNC(texture2D(texture_envAtlas, mapRoughnessUv(uv, mipLevel)));
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/scene/skybox/sky-mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SkyMesh {
});

// defines
material.setDefine('SKYBOX_DECODE_FNC', ChunkUtils.decodeFunc(texture.encoding));
material.setDefine('__INJECT_SKYBOX_DECODE_FNC', ChunkUtils.decodeFunc(texture.encoding));
if (type !== SKYTYPE_INFINITE) material.setDefine('SKYMESH', '');
if (texture.cubemap) material.setDefine('SKY_CUBEMAP', '');

Expand Down
13 changes: 13 additions & 0 deletions test/core/preprocessor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('Preprocessor', function () {

const srcData = `
#define __INJECT_COUNT 2
#define __INJECT_STRING hello
#define FEATURE1
#define FEATURE2
Expand Down Expand Up @@ -95,6 +97,9 @@ describe('Preprocessor', function () {
#if NAME != hello
CMP5
#endif
TESTINJECTION __INJECT_COUNT
INJECTSTRING __INJECT_STRING(x)
`;

it('returns false for MORPH_A', function () {
Expand Down Expand Up @@ -201,4 +206,12 @@ describe('Preprocessor', function () {
expect(Preprocessor.run(srcData, includes, { stripDefines: true }).includes('#')).to.equal(false);
});

it('returns true for working integer injection', function () {
expect(Preprocessor.run(srcData, includes).includes('TESTINJECTION 2')).to.equal(true);
});

it('returns true for working string injection', function () {
expect(Preprocessor.run(srcData, includes).includes('INJECTSTRING hello(x)')).to.equal(true);
});

});

0 comments on commit fb1ecb2

Please sign in to comment.