Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in generation of symbols for ShaderGen Type Globals #1608

Closed
kwokcb opened this issue Nov 29, 2023 · 4 comments
Closed

Regression in generation of symbols for ShaderGen Type Globals #1608

kwokcb opened this issue Nov 29, 2023 · 4 comments

Comments

@kwokcb
Copy link
Contributor

kwokcb commented Nov 29, 2023

Issue

The logic to generate globals appears to have "broken" sometime between Nov 15 and 29,2203 -- at least for Python libraries but perhaps for shared libraries in general.

This shows up for example in this code for GLSL uniforms.
The code perform pointers comparisons which can now fail when running outside the test suite.

int GlslProgram::mapTypeToOpenGLType(const TypeDesc* type)
{
    if (type == Type::INTEGER)
        return GL_INT;
    else if (type == Type::BOOLEAN) <-- can fail
        return GL_BOOL;
    else if (type == Type::FLOAT) <-- can fail
        return GL_FLOAT;
    else if (type->isFloat2())
        return GL_FLOAT_VEC2;
    else if (type->isFloat3())
        return GL_FLOAT_VEC3;
    else if (type->isFloat4())
        return GL_FLOAT_VEC4;
    else if (type == Type::MATRIX33) <-- can fail
        return GL_FLOAT_MAT3;
    else if (type == Type::MATRIX44) <-- can fail
        return GL_FLOAT_MAT4;
    else if (type == Type::FILENAME)
    {
        // A "filename" is not indicative of type, so just return a 2d sampler.
        return GL_SAMPLER_2D;
    }

    return GlslProgram::Input::INVALID_OPENGL_TYPE;
}

You get these errors when trying to render from Python:

Errors: "GLSL uniform parsing error
Vertex shader uniform block type mismatch [PrivateUniforms]. Name: "u_worldMatrix". Type: "matrix44". Semantic: "". Value: "<none>". Unit: "<none>". Colorspace: "<none>". GLType: -1
Vertex shader uniform block type mismatch [PrivateUniforms]. Name: "u_viewProjectionMatrix". Type: "matrix44". Semantic: "". Value: "<none>". Unit: "<none>". Colorspace: "<none>". GLType: -1"

The root issue appears that globals defined for Type's get duplicated per Python module. This can be checked by adding debugging statements to Type constructor here: https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/source/MaterialXGenShader/TypeDesc.cpp#L42

This does not appear to be cause by this module dependency change: #1595
so it is still unknown why this regression has occurred.

The simplest test is to add debugging prints to the global declaration initializations and having a Python file which does the following:

import MaterialX.PyMaterialXGenShader as mx_gen_shader
import MaterialX.PyMaterialXGenGlsl as mx_gen_glsl

Globals appear to be initialized twice -- once for each module.

@kwokcb kwokcb changed the title glsl uniform type checking fails on poitner comparison glsl uniform type checking fails on pointer comparison Nov 29, 2023
@kwokcb
Copy link
Contributor Author

kwokcb commented Nov 29, 2023

This is a workaround for the compare, but there could be issue elsewhere for global pointer compares:

int GlslProgram::mapTypeToOpenGLType(const TypeDesc* type)
{
    if (type == Type::INTEGER)
        return GL_INT;
    else if (type->getName() == Type::BOOLEAN->getName())
        return GL_BOOL;
    else if (type->getName() == Type::FLOAT->getName())
        return GL_FLOAT;
    else if (type->isFloat2())
        return GL_FLOAT_VEC2;
    else if (type->isFloat3())
        return GL_FLOAT_VEC3;
    else if (type->isFloat4())
        return GL_FLOAT_VEC4;
    else if (type->getName() == Type::MATRIX33->getName())
        return GL_FLOAT_MAT3;
    else if (type->getName() == Type::MATRIX44->getName())
    {
        return GL_FLOAT_MAT4;
    } 
    else if (type == Type::FILENAME)
    {
        // A "filename" is not indicative of type, so just return a 2d sampler.
        return GL_SAMPLER_2D;
    }

    return GlslProgram::Input::INVALID_OPENGL_TYPE;
}

@kwokcb kwokcb changed the title glsl uniform type checking fails on pointer comparison Regression in generation of symbols for ShaderGen Statics Jan 18, 2024
@kwokcb kwokcb changed the title Regression in generation of symbols for ShaderGen Statics Regression in generation of symbols for ShaderGen Type Globals Jan 18, 2024
@jstone-lucasfilm
Copy link
Member

Thanks for writing this up, @kwokcb, and it sounds like a key issue to investigate and/or fix before we wrap up MaterialX 1.38.9.

@niklasharrysson
Copy link
Contributor

As we discussed offline the source of this issue seems to be the use of global pointers to identify the registered TypeDesc instances. It's not safe to use the pointers as unique identifiers when used over module boundaries. At least not in a Python context.

In hindsight this is not a good design in general, and we should rewamp the TypeDesc class in a future release when API breakage is ok. The short term fix will be to change the TypeDesc compare operations to use the type name strings instead.

@jstone-lucasfilm
Copy link
Member

I'll mark this as resolved, based on your fix in #1665, with the note that @niklasharrysson is planning a more thorough refactoring of the TypeDesc system in MaterialX 1.39.

jstone-lucasfilm pushed a commit that referenced this issue Mar 1, 2024
This change list implements a redesign of the TypeDesc class used for data type descriptions in MaterialX.

The main purpose of this change is to solve issue #1608. But the new design also gives performance improvements to the use of this class.

Since the TypeDesc class is used frequently many files are touched by this change and there are some API breakage. Function wrappers with [[deprecated]] are added to minimize API breakage, but it's not possible for everything that changed. For example, the TypeDesc instances are now stored and accessed by value rather then by pointer, requiring a change to the access syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants