Isaac Woods
Isaac Woods

Reputation: 1163

Syntax error in GLSL - unexpected NEW_IDENTIFIER

I have been writing a shading language that is meant to simplify writing GLSL. Because of this, I can't really rely on line numbers given by the GLSL compiler. I therefore can't find what this error is trying to say. What does it mean first of all, and then how can I fix it?

Error code:

0:8(18): error: syntax error, unexpected NEW_IDENTIFIER, expecting '{'

I believe that this just means there is a syntax error with the generated GLSL, but I can't find one. What does this error message actually mean?

-- EDIT --

Sorry for not providing code, I wanted to know what the error meant so I could provide the relevant areas of code.

Generated vertex shader:

#version 120

attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;
attribute vec3 tangent;

varying vec2 fragTexCoords;
varying vec3 worldPos;
varying vec4 shadowMapCoords;
varying mat3 tbnMatrix;

uniform mat4 modelMatrix;
uniform mat4 MVPMatrix;
uniform mat4 lightMatrix;

void main() {
    gl_Position = MVPMatrix * vec4(position, 1);
    fragTexCoords = texCoord;
    shadowMapCoords = lightMatrix * vec4(position, 1);
    worldPos = (modelMatrix * vec4(position, 1)).xyz;

    vec3 n = normalize((modelMatrix * vec4(normal, 0)).xyz);
    vec3 t = normalize((modelMatrix * vec4(tangent, 0)).xyz);
    t = normalize(t - dot(t, n) * n);

    vec3 biTangent = cross(t, n);
    tbnMatrix = mat3(t, biTangent, n);
}

And the generated fragment shader:

#version 120

varying vec2 fragTexCoords;
varying vec3 worldPos;
varying vec4 shadowMapCoords;
varying mat3 tbnMatrix;

uniform Material material;
uniform Light light;
uniform vec3 eyePos;

uniform sampler2D shadowMap;
uniform float shadowVarianceMin;
uniform float shadowLightBleedReduction;

bool inRange(float value) {
    return value >= 0 && value <= 1;
}

float linStep(float low, float high, float v) {
    return clamp((v - low) / (high - low), 0, 1);
}

vec2 calcParallaxTexCoords(sampler2D dispMap, mat3 tbnMatrix, vec3 directionToEye, vec2 texCoords, float scale, float bias) {
    return texCoords.xy + (directionToEye * tbnMatrix).xy * (texture2D(dispMap, texCoords.xy).r * scale + bias);
}

float sampleShadowMap(sampler2D shadowMap, vec2 coords, float compare) {
    return step(compare, texture2D(shadowMap, coords.xy).r);
}

float sampleVarianceShadowMap(sampler2D shadowMap, vec2 coords, float compare, float varianceMin, float lightBleedReduction) {
    vec2 moments = texture2D(shadowMap, coords.xy).xy;

    float p = step(compare, moments.x);
    float variance = max(moments.y - moments.x * moments.x, varianceMin);

    float d = compare - moments.x;
    float pMax = linStep(lightBleedReduction, 1, variance / (variance + d * d));

    return min(max(p, pMax), 1);
}

struct Light {
    vec3 color;
    float intensity;
    vec3 direction;
};

struct Material {
    float roughness;
    float fresnelReflectance;

    sampler2D diffuseTexture;
    sampler2D normalMap;
    sampler2D dispMap;

    float dispMapScale;
    float dispMapBias;
};

vec3 calcLight(Light light, Material material, vec3 surfaceNormal, vec3 eyePos, vec3 diffuseColor, vec3 specularColor) {
    vec3 halfVector     = normalize(light.direction + eyePos);
    float NdotL         = clamp(dot(surfaceNormal, light.direction),    0.0, 1.0);
    float NdotH         = clamp(dot(surfaceNormal, halfVector),         0.0, 1.0);
    float NdotV         = clamp(dot(surfaceNormal, eyePos),             0.0, 1.0);
    float VdotH         = clamp(dot(eyePos, halfVector),                0.0, 1.0);
    float rSq           = material.roughness * material.roughness;

    // ----- Fresnel term -----
    float fresnel = pow(1 - VdotH, 5.0);
    fresnel *= 1 - material.fresnelReflectance;
    fresnel += material.fresnelReflectance;

    // ----- Geometric attenuation term -----
    float geoNum = 2.0 * NdotV;
    float geoB   = (geoNum * NdotV) / VdotH;
    float geoC   = (geoNum * NdotL) / VdotH;
    float geo    = min(1.0, min(geoB, geoC));

    // ----- Roughness term -----
    float roughnessA = rSq * pow(NdotH, 4.0);
    float roughnessB = (NdotH * NdotH - 1.0) / (rSq * NdotH * NdotH);
    float roughness = 1.0 / roughnessA * exp(roughnessB);

    // ----- Cook-Torrance calculation -----
    vec3 specular = vec3(fresnel * roughness * geo) / (NdotV * NdotL);

    return max(0.0, NdotL) * (specularColor * specular + diffuseColor);
}

float calcShadowAmount(sampler2D shadowMap, vec4 initialShadowMapCoords) {
    vec3 shadowMapCoords = (initialShadowMapCoords.xyz / initialShadowMapCoords.w);

    if (inRange(shadowMapCoords.x) && inRange(shadowMapCoords.y) && inRange(shadowMapCoords.z)) {
        return sampleVarianceShadowMap(shadowMap, shadowMapCoords.xy, shadowMapCoords.z, shadowVarianceMin, shadowLightBleedReduction);
    } else {
        return 1.0;
    }
}

float calcBasicShadowAmount(sampler2D shadowMap, vec4 initialShadowMapCoords) {
    vec3 shadowMapCoords = (initialShadowMapCoords.xyz / initialShadowMapCoords.w);

    return sampleShadowMap(shadowMap, shadowMapCoords.xy, shadowMapCoords.z);
}

void main() {
    vec3 directionToEye = normalize(eyePos - worldPos);
    vec2 texCoords = calcParallaxTexCoords(dispMap, tbnMatrix, directionToEye, fragTexCoords, dispMapScale, dispMapBias);
    vec3 normal = normalize(tbnMatrix * (255.0 / 128.0 * texture2D(normalMap, texCoords).xyz - 1));

    gl_FragColor = calcLight(light, material, normal, eyePos, texture2D(diffuse, texCoords), vec3(1, 1, 1));
}

Upvotes: 1

Views: 8873

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43389

GLSL error reporting conventions are vendor-specific and thus difficult to parse. However, in this case 0:8(18): error: syntax error, unexpected NEW_IDENTIFIER, expecting '{' refers to line 8, character 18.

If you look at your fragment shader, on line 8 you have the following:

uniform Material material;
                 ^ char. 18

The problem here is that you have not yet defined the structure of Material.

You will need to move these structs to precede your uniforms:

struct Light {
    vec3 color;
    float intensity;
    vec3 direction;
};

struct Material {
    float roughness;
    float fresnelReflectance;

    sampler2D diffuseTexture;
    sampler2D normalMap;
    sampler2D dispMap;

    float dispMapScale;
    float dispMapBias;
};

Upvotes: 4

Related Questions