Skip to content

ShaderLab: #define macro not expanded after dot (swizzle position) #2962

@GuoLei1990

Description

@GuoLei1990

Description

ShaderLab compiler does not expand #define macros used after . (dot) in swizzle positions.

Reproduction

#define OCCLUSION_CHANNEL r
#define ROUGHNESS_CHANNEL g
#define METALLIC_CHANNEL b

vec4 pbrRes = texture2D(pbrMap, uv);
pbr.x *= pbrRes.OCCLUSION_CHANNEL;  // Expected: pbrRes.r, actual: compile error
pbr.y *= pbrRes.ROUGHNESS_CHANNEL;
pbr.z *= pbrRes.METALLIC_CHANNEL;

Error

CompilationError: Unexpected token OCCLUSION_CHANNEL
|··· pbr.x *= pbrRes.OCCLUSION_CHANNEL; ^^^^^^^^^^^^^^^^^

Expected

Standard GLSL preprocessors expand macros before parsing, so pbrRes.OCCLUSION_CHANNEL should be expanded to pbrRes.r and compile normally. This is a common pattern in Cocos Creator shaders.

Workaround

Inline the swizzle directly: pbrRes.r instead of pbrRes.OCCLUSION_CHANNEL.

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