Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

c# - OpenGL throws InvalidOperation error after glDrawElements when one specific texture unit is not set - why?

My scene contains one sun light and one spot light. The sun always has a shadow map attached. The spot light may have a shadow map but the default for it is: no shadow map attached.

When uploading mesh data for a draw call there is this section in my code that causes trouble on AMD cards. I was not able to reproduce this problem on Intel or nVidia cards.

// Upload depth texture from sun (shadow mapping)
GL.ActiveTexture(TextureUnit.Texture10);
GL.BindTexture(TextureTarget.Texture2D, Sun.TextureShadowMap);
GL.Uniform1(mUniform_TextureShadowMap, 10);
GL.Uniform1(mUniform_BiasCoefficient, Sun.ShadowMapBias);

// optionally upload depth texture for second light:
// (lightShadow is the index in an array of light sources that is >= 0 if a shadow map is attached)
if (lightShadow >= 0) 
{
    GL.ActiveTexture(TextureUnit.Texture11);
    GL.BindTexture(TextureTarget.Texture2D, Engine.TextureShadowMap2);
    GL.Uniform1(mUniform_TextureShadowMap2, 11);
    GL.Uniform1(mUniform_ShadowLightPosition, lightShadow);
    GL.Uniform1(mUniform_BiasCoefficient2, GetLightObjects().ElementAt(lightShadow).ShadowMapBias);
}
else
{
    // if the index is < 0, the fragment shader does no texture lookup
    GL.Uniform1(mUniform_ShadowLightPosition, -1);
}

// After that, some other textures with unit numbers 0-5 
// (albedo, normal, roughness, etc.) may be uploaded as well

So, texture unit 11 may or may be not used. The passing of the uniforms themselves does not create an error. The "InvalidOperation" error comes up when the actual draw call glDrawElements() happens.

However, if I change the optional shadow map upload like so...

...
else
{
    // if the index is < 0, the fragment shader does no texture lookup
    GL.Uniform1(mUniform_ShadowLightPosition, -1);

    GL.ActiveTexture(TextureUnit.Texture11);
    GL.BindTexture(TextureTarget.Texture2D, Engine.TextureShadowMap2);
    GL.Uniform1(mUniform_TextureShadowMap2, 11);
    GL.Uniform1(mUniform_BiasCoefficient2, GetLightObjects().ElementAt(lightShadow).ShadowMapBias);
}

...the scene renders perfectly fine. But I do not want to upload the second shadow map (which is always empty then) if the light has no real shadow map attached.

I think it may have something to do with the way I upload my textures to the shaders.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...