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

Invalid SPIR-V produced when gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT used in the same shader #2921

Closed
andfau-arm opened this issue Apr 4, 2022 · 6 comments

Comments

@andfau-arm
Copy link
Contributor

Consider the following intersection shader:

#version 460
#extension GL_EXT_ray_tracing : require

void main()
{
    mat4x3 foo = gl_ObjectToWorldEXT;
    mat3x4 bar = gl_ObjectToWorld3x4EXT;
}

There is no "3x4" version of the SPIR-V built-in, so glslang maps both of these built-ins to ObjectToWorldKHR and inserts a transpose.

However, if you use both of these in the same shader, you will get two variables decorated with the same BuiltIn:

                              Decorate 12(gl_ObjectToWorldEXT) BuiltIn ObjectToWorldKHR
                              Decorate 18(gl_ObjectToWorld3x4EXT) BuiltIn ObjectToWorldKHR
               2:             TypeVoid
               3:             TypeFunction 2
               6:             TypeFloat 32
               7:             TypeVector 6(float) 3
               8:             TypeMatrix 7(fvec3) 4
               9:             TypePointer Function 8
              11:             TypePointer Input 8
12(gl_ObjectToWorldEXT):     11(ptr) Variable Input
              14:             TypeVector 6(float) 4
              15:             TypeMatrix 14(fvec4) 3
              16:             TypePointer Function 15
18(gl_ObjectToWorld3x4EXT):     11(ptr) Variable Input
         4(main):           2 Function None 3
               5:             Label
         10(foo):      9(ptr) Variable Function
         17(bar):     16(ptr) Variable Function
              13:           8 Load 12(gl_ObjectToWorldEXT)
                              Store 10(foo) 13
              19:           8 Load 18(gl_ObjectToWorld3x4EXT)
              20:          15 Transpose 19

This is not permitted by the SPIR-V specification:

For all the declarations of all the global variables and constants statically referenced by the entry-point’s call tree, within any specific storage class it is invalid to decorate with a specific BuiltIn more than once.

The same issue exists for the pair of gl_WorldToObjectEXT and gl_WorldToObject3x4EXT.

This issue is similar to #2910 but is not as easy to fix because the built-ins are not perfect aliases.

@andfau-arm
Copy link
Contributor Author

Incidentally, spirv-val doesn't seem to catch this case. That could be a potential improvement to the validator.

@greg-lunarg
Copy link
Contributor

@dgkoch This code appears to be yours. Can you please fix?

@dgkoch
Copy link
Contributor

dgkoch commented Apr 5, 2022

@greg-lunarg I contributed the code, but it was written by someone else, who is likely not available at this point.
I don't think I know enough about the compiler internals to be able to fix this myself.

@greg-lunarg greg-lunarg self-assigned this Apr 5, 2022
@andfau-arm
Copy link
Contributor Author

@greg-lunarg If you haven't started on this yet: we (Arm) will try to write a patch at some point.

@greg-lunarg
Copy link
Contributor

OK. I will wait for your patch.

andfau-arm added a commit to andfau-arm/glslang that referenced this issue May 30, 2022
…osGroup#2921)

Fixes an issue where invalid SPIR-V was generated when
gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, or
gl_WorldToObjectEXT and gl_WorldToObject3x4EXT, were used in the same
shader. The SPIR-V specification requires that there be at most one
OpVariable decorated with a given BuiltIn value.

Change-Id: I94dc9ee3f7adf1acb29873d68056fdb2caf8cac3
andfau-arm added a commit to andfau-arm/glslang that referenced this issue May 30, 2022
…osGroup#2921)

Fixes an issue where invalid SPIR-V was generated when
gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, or
gl_WorldToObjectEXT and gl_WorldToObject3x4EXT, were used in the same
shader. The SPIR-V specification requires that there be at most one
OpVariable decorated with a given BuiltIn value.
@andfau-arm
Copy link
Contributor Author

Here is the patch: #2955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants