Skip to content

Weird behaviour when reusing the compile function #57

@geoffreytools

Description

@geoffreytools

Hey,

I am using glsl_transpiler with great effect to unit test my shaders and something puzzles me.

After reading the readme I got this impression that I could reuse the function compile that is returned by GLSL, like so:

var compile = GLSL({
	uniform: function (name) {
		return `uniforms.${name}`
	},
	attribute: function (name) {
		return `attributes.${name}`
	}
})

const a = compile(something);
const b = compile(somethingElse);

However when I do this I realise the behaviour is inconsistent: sometimes my glsl functions are suffixed with their types (for example vec2 getPosition(float index, float length) gets renamed to getPosition_float_float in the emitted Javascript) and sometimes they are not (they are not overloaded, at least not the ones I am currently testing). Whether they are renamed or not depends on evaluation order: the first compilation has no suffixes and subsequent compilations have some, at least from what I have seen.

As a workaround I simply call GLSL each time I want to compile, then my glsl functions are never renamed, but I don't know if this is the intended behaviour.

I usually run the same shader with different static variables (to be used in loops for example) which I inject with a simple template and find/replace logic, so the same functions are compiled multiple times:

// with the aforementioned workaround
export const compile = (shaderStr, map = {}) => GLSL({
    uniform: name => `uniforms.${name}`,
    attribute: name => `attributes.${name}`
})(replaceMap(shaderStr, map))

const replaceMap = (str, map) => {
    return str.replace(
        new RegExp(Object.keys(map).join("|"), "gi"),
        function(match) {return String(map[match]);}
    );
}

Hope this is useful to you. Even without reusing the compile function I don't have to complain about performance, so this is not a pressing issue but it's a little weird.

tchuss

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions