dotnet / Silk.NET

The high-speed OpenGL, OpenCL, OpenAL, OpenXR, GLFW, SDL, Vulkan, Assimp, WebGPU, and DirectX bindings library your mother warned you about.
https://dotnet.github.io/Silk.NET
MIT License
3.89k stars 378 forks source link

GetUniformLocation() returns -1 on Nvidia #2153

Closed bamfbamf closed 2 months ago

bamfbamf commented 2 months ago

Summary

Querying location of uniforms returns -1 on laptop Nvidia RTX 3050 dedicated GPU, while returns proper location on laptop Intel iGPU.

Steps to reproduce

Comments

All my extension methods for my shader first query location with GetUniformLocation() and use its returned value to set uniform methods (Uniform1() etc.) location value. They will error out if location is not found (-1). And this works perfectly fine on Intel iGPU, locations are found and returned/set properly. While I force windows to use RTX, then GetUniformLocation fails with -1. Just for those two uniforms.

I tried to pack those uniforms into vec2 and set it, but this also fails. There are other float uniforms there, like gridOpacity etc. and for them GetUniformLocation() return proper locations on NVidia too.

So I made method that doesn't query location and I set NEAR_PLANE & FAR_PLANE location directly, just like layout(location = 3) and layout(location = 4) points to. And this works. Changing value of uniforms affects the shader. So the uniforms are working properly. GetUniformLocation() simply fails to get their location on NVidia.

bamfbamf commented 2 months ago

So as example, my extension methods for setting uniforms are as follows:


public enum OGLShaderUniformRequirementPolicy
{
  /// <summary>
  /// Uniform is always required. Error out if missing.
  /// </summary>
  REQUIRED,
  /// <summary>
  /// Uniform can be skipped if missing. Don't error out.
  /// </summary>
  OPTIONAL
}

public static (int Ok, ExecutionErrorReport Err) GetUniformLocation(this OGLShader me, string uniform_name, OGLShaderUniformRequirementPolicy requirement_policy)
{
    try
    {
       // Context is just a property to GL variable stored in OGLShader.
        var location = me.Context.GetUniformLocation(me.ID, uniform_name);
        var errCode = me.Context.GetError();
        if (errCode != GLEnum.NoError)
            return (location, new() { ErrorMessage = $"*** GetUniformLocation() failed with error: {errCode}", ErrorType = ExecutionErrorType.Exit });

        if (requirement_policy == OGLShaderUniformRequirementPolicy.OPTIONAL) return (location, new());
        else
        {
            return location.Equals((int)GLEnum.InvalidIndex)
                ? (location, new() { ErrorMessage = $"*** {uniform_name} uniform not found on shader {me.ID}", ErrorType = ExecutionErrorType.Exit })
                : (location, new());
        }
    }
    catch (Exception e)
    { return ((int)GLEnum.InvalidIndex, new() { ErrorMessage = e.Message, ErrorType = ExecutionErrorType.Exception }); }
}

public unsafe static ExecutionErrorReport SetUniform(this OGLShader me, string uniform_name, float value)
{
    try
    {
        var (location, err) = me.GetUniformLocation(uniform_name, OGLShaderUniformRequirementPolicy.REQUIRED);
        if (err.ErrorType != ExecutionErrorType.None) return err;

        me.Context.Uniform1(location, value);
        var errCode = me.Context.GetError();

        return errCode != GLEnum.NoError
            ? new() { ErrorMessage = $"*** Uniform1() failed with error: {errCode}", ErrorType = ExecutionErrorType.Exit }
            : new();
    }
    catch (Exception e)
    { return new() { ErrorMessage = e.Message, ErrorType = ExecutionErrorType.Exception }; }
}

public unsafe static ExecutionErrorReport SetNearPlane(this OGLShader me, float value)
    => me.SetUniform(OGLShaderUniformNames.NEAR_PLANE, value);

public unsafe static ExecutionErrorReport SetFarPlane(this OGLShader me, float value)
    => me.SetUniform(OGLShaderUniformNames.FAR_PLANE, value);

public unsafe static ExecutionErrorReport SetClippingPlanes(this OGLShader me, float near, float far)
{
    var err = me.SetNearPlane(near);
    if (err.ErrorType != ExecutionErrorType.None) return err;

    return me.SetFarPlane(far);
}

For now all methods use OGLShaderUniformRequirementPolicy::REQUIRED, so they error out if location is -1. It doesn't matter what combination I use, just GetUniformLocation(), SetUniform() or one of the tailored methods for setting planes, they all return -1 on NVidia.

And this fails only for those two uniforms, NEAR_PLANE & FAR_PLANE. But setting those uniforms directly, without querying their locations, and passing layout(location = 3) and layout(location = 4) with this method:

public unsafe static ExecutionErrorReport SetUniform(this OGLShader me, int uniform_location, float value)
{
    try
    {
        me.Context.Uniform1(uniform_location, value);
        var errCode = me.Context.GetError();

        return errCode != GLEnum.NoError
            ? new() { ErrorMessage = $"*** Uniform1() failed with error: {errCode}", ErrorType = ExecutionErrorType.Exit }
            : new();
    }
    catch (Exception e)
    { return new() { ErrorMessage = e.Message, ErrorType = ExecutionErrorType.Exception }; }
}

will work and uniforms are changing in shader.

qian-o commented 2 months ago

The pipeline layout of OGL should be fixed in the code rather than obtained through querying. This approach is also in line with the modern programming style of graphic libraries.

bamfbamf commented 2 months ago

I'm building engine at this point, so not many standarized things there yet. Just adding viewport gizmos.

So what, GetUniformLocation() is deprecated in Core and it can return wrong value?

qian-o commented 2 months ago

I'm building engine at this point, so not many standarized things there yet. Just adding viewport gizmos.

So what, GetUniformLocation() is deprecated in Core and it can return wrong value?

I have encountered similar issues with shaders compiled using OGL 4.6 + SPV on Intel and AMD GPUs, where querying would indeed return -1.

Perksey commented 2 months ago

Have you tried to check the output of something like this:

gl.GetProgram(program, GLEnum.ActiveUniforms, out var activeUniforms);
for (var i = 0; i < activeUniforms; i++)
{
    var name = GetActiveUniform(program, (uint)i, out var size, out var type);
    Console.WriteLine($"Uniform {i} - Name: {name} - Size: {size} - Type: {type}");
}
bamfbamf commented 2 months ago

On Intel iGPU prints:

Uniform 0 - Name: PROJECTION - Size: 1 - Type: FloatMat4
Uniform 1 - Name: VIEW - Size: 1 - Type: FloatMat4
Uniform 2 - Name: FAR_PLANE - Size: 1 - Type: Float
Uniform 3 - Name: NEAR_PLANE - Size: 1 - Type: Float
Uniform 4 - Name: VIEW_POS - Size: 1 - Type: FloatVec3
Uniform 5 - Name: gridAxis - Size: 1 - Type: Int
Uniform 6 - Name: gridCellSize - Size: 1 - Type: Float
Uniform 7 - Name: gridOpacity - Size: 1 - Type: Float
Uniform 8 - Name: gl_DepthRange.near - Size: 1 - Type: Float
Uniform 9 - Name: gl_DepthRange.far - Size: 1 - Type: Float
Uniform 10 - Name: gl_DepthRange.diff - Size: 1 - Type: Float

If I force RTX 3050, it prints:

Uniform 0 - Name: PROJECTION - Size: 1 - Type: FloatMat4
Uniform 1 - Name: VIEW - Size: 1 - Type: FloatMat4
Uniform 2 - Name: VIEW_POS - Size: 1 - Type: FloatVec3
Uniform 3 - Name: gridAxis - Size: 1 - Type: Int
Uniform 4 - Name: gridCellSize - Size: 1 - Type: Float
Uniform 5 - Name: gridOpacity - Size: 1 - Type: Float
Uniform 6 - Name: gl_DepthRange.diff - Size: 1 - Type: Float
Uniform 7 - Name: gl_DepthRange.near - Size: 1 - Type: Float
Perksey commented 2 months ago

Interesting... Perhaps try adjusting the previous code:

    Console.WriteLine($"Uniform {i} - Name: {name} - Size: {size} - Type: {type} - Location: {gl.GetUniformLocation(program, name)}");

To see whether there's a noticeable gap in the location indices.

Also make sure you're checking the compile/link status, and retrieving the info logs in any case. There may be more info therein.

Note you can also use BindAttribLocation before calling LinkProgram to set an attribute location. Really the one in the shader text should be respected and will take precedence over the one specified in BindAttribLocation, but I'm wondering whether this would force it to pop up.

bamfbamf commented 2 months ago

Intel iGPU:

Uniform 0 - Name: PROJECTION - Size: 1 - Type: FloatMat4 - Location: 1
Uniform 1 - Name: VIEW - Size: 1 - Type: FloatMat4 - Location: 0
Uniform 2 - Name: FAR_PLANE - Size: 1 - Type: Float - Location: 4
Uniform 3 - Name: NEAR_PLANE - Size: 1 - Type: Float - Location: 3
Uniform 4 - Name: VIEW_POS - Size: 1 - Type: FloatVec3 - Location: 2
Uniform 5 - Name: gridAxis - Size: 1 - Type: Int - Location: 5
Uniform 6 - Name: gridCellSize - Size: 1 - Type: Float - Location: 6
Uniform 7 - Name: gridOpacity - Size: 1 - Type: Float - Location: 7
Uniform 8 - Name: gl_DepthRange.near - Size: 1 - Type: Float - Location: -1
Uniform 9 - Name: gl_DepthRange.far - Size: 1 - Type: Float - Location: -1
Uniform 10 - Name: gl_DepthRange.diff - Size: 1 - Type: Float - Location: -1

RTX 3050:

Uniform 0 - Name: PROJECTION - Size: 1 - Type: FloatMat4 - Location: 1
Uniform 1 - Name: VIEW - Size: 1 - Type: FloatMat4 - Location: 0
Uniform 2 - Name: VIEW_POS - Size: 1 - Type: FloatVec3 - Location: 2
Uniform 3 - Name: gridAxis - Size: 1 - Type: Int - Location: 5
Uniform 4 - Name: gridCellSize - Size: 1 - Type: Float - Location: 6
Uniform 5 - Name: gridOpacity - Size: 1 - Type: Float - Location: 7
Uniform 6 - Name: gl_DepthRange.diff - Size: 1 - Type: Float - Location: -1
Uniform 7 - Name: gl_DepthRange.near - Size: 1 - Type: Float - Location: -1

As for the BindAttribLocation(), it is for attribs, not for uniforms. So it did nothing.

Perksey commented 2 months ago

Ah yeah my bad, forgot about that. It seems like 3 and 4 are being kept free. Have you tried with other names just in case it's a weird driver quirk?

Assuming that there was no extra info in the link/compile logs, I also recommend using RenderDoc to see if you can see what RenderDoc recognises the shader as.

Failing all of that, I recommend reporting this to NVIDIA and finding a workaround e.g. using a uniform block or SSBO instead. There is ample evidence here that this is not a Silk.NET issue.