Skip to content

Transpiler: Add GLSL preprocessor directives and macro support - #34206

Merged
sunag merged 2 commits into
mrdoob:devfrom
sunag:dev-transpiler-defines
Aug 10, 2026
Merged

sunag merged 2 commits into
mrdoob:devfrom
sunag:dev-transpiler-defines

Conversation

@sunag

@sunag sunag commented Aug 10, 2026 •

Copy link
Copy Markdown
Collaborator

Related issue: N/A

Description

This PR adds support for GLSL preprocessor directives to GLSLDecoder. Previously, preprocessor directives (such as #define, #ifdef, #version 300 es, or #include) were tokenized as regular # operator tokens, causing GLSLDecoder to throw syntax errors or generate invalid AST nodes.

Example

#version 300 es
#extension GL_OES_standard_derivatives : enable

/*
 * Perlin noise
 * https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
 */

#define PI 3.141592653589793
#define USE_SMOOTH

float rand(vec2 c){
	return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

float noise(vec2 p, float freq ){
	float unit = 1./freq;
	vec2 ij = floor(p/unit);
	vec2 xy = mod(p,unit)/unit;
#ifdef USE_SMOOTH
	xy = .5*(1.-cos(PI*xy));
#else
	xy = 3.*xy*xy-2.*xy*xy*xy;
#endif
	float a = rand((ij+vec2(0.,0.)));
	float b = rand((ij+vec2(1.,0.)));
	float c = rand((ij+vec2(0.,1.)));
	float d = rand((ij+vec2(1.,1.)));
	float x1 = mix(a, b, xy.x);
	float x2 = mix(c, d, xy.x);
	return mix(x1, x2, xy.y);
}

float pNoise(vec2 p, int res){
	float persistence = .5;
	float n = 0.;
	float normK = 0.;
	float f = 4.;
	float amp = 1.;
	int iCount = 0;
	for (int i = 0; i<50; i++){
		n+=amp*noise(p, f);
		f*=2.;
		normK+=amp;
		amp*=persistence;
		if (iCount == res) break;
		iCount++;
	}
	float nf = n/normK;
	return nf*nf*nf*nf;
}

Macro

#define SQR(x) ((x) * (x))
#define MULT(a, b) ((a) * (b))
#define CLAMP01(x) clamp((x), 0.0, 1.0)

float test(float v) {
	float a = SQR(v);
	float b = MULT(v + 1.0, 2.0);
	float c = CLAMP01(b);
	return a + b + c;
}

@sunag sunag added this to the r186 milestone Aug 10, 2026
@sunag
sunag marked this pull request as ready for review August 10, 2026 15:45
@sunag sunag changed the title Transpiler: Add GLSL preprocessor directives support Transpiler: Add GLSL preprocessor directives and macro support Aug 10, 2026
@sunag
sunag merged commit 3b7d836 into mrdoob:dev Aug 10, 2026
8 checks passed
@sunag
sunag deleted the dev-transpiler-defines branch August 10, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant