glslang icon indicating copy to clipboard operation
glslang copied to clipboard

Allowed types for specialization constants

Open maxime-modulopi opened this issue 8 months ago • 7 comments

The GLSL extension GL_KHR_vulkan_glsl says that specialization constants can only be used with scalar int, float or bool.

On the other hand, glslang compiles shaders with specialization constants whose type is any unsigned/signed integer variant of any size, or double.

Which types are actually allowed?

maxime-modulopi avatar May 31 '25 13:05 maxime-modulopi

The additional permissiveness are due to SPIR-V. The spec allows types other than basic scalar types (including composite types and various number formats). Check section 1.9, "Specialization" and section 2.12, "Specialization" in https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.pdf.

dnovillo avatar Aug 26 '25 18:08 dnovillo

Yes it is allowed in SPIR-V, my question was if it was also officially allowed in GLSL or if I'm relying on glslang's UB, given that glslang is the reference compiler for GLSL and the GLSL extension does not seem to allow it.

maxime-modulopi avatar Aug 27 '25 13:08 maxime-modulopi

Well, yes. In as much as glslang emits SPIR-V, it will follow similar guidelines. Whether we should have different behaviours depending on target may be interesting to analyze.


From: maxime-modulopi @.> Sent: Wednesday, August 27, 2025 9:28:31 AM To: KhronosGroup/glslang @.> Cc: Diego Novillo @.>; Comment @.> Subject: Re: [KhronosGroup/glslang] Allowed types for specialization constants (Issue #3964)

[https://avatars.githubusercontent.com/u/144250665?s=20&v=4]maxime-modulopi left a comment (KhronosGroup/glslang#3964)https://github.com/KhronosGroup/glslang/issues/3964#issuecomment-3228214809

Yes it is allowed in SPIR-V, my question was if it was also officially allowed in GLSL or if I'm relying on glslang's UB, given that glslang is the reference compiler for GLSL and the GLSL extension does not seem to allow it.

— Reply to this email directly, view it on GitHubhttps://github.com/KhronosGroup/glslang/issues/3964#issuecomment-3228214809, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABXVCZ43TFBZ2ST3QP6NSXL3PWW77AVCNFSM6AAAAAB6J4ZM5GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTEMRYGIYTIOBQHE. You are receiving this because you commented.Message ID: @.***>

dnovillo avatar Aug 27 '25 14:08 dnovillo

By "depending on target" you mean GLSL targeting GL drivers vs SPIR-V, right? If so, is it something that could be documented in the extension spec?

maxime-modulopi avatar Aug 27 '25 17:08 maxime-modulopi

NVM, I forgot the extension is Vulkan-only.

maxime-modulopi avatar Aug 27 '25 17:08 maxime-modulopi

There's also specialization constants in ARB_gl_spirv but it says

The constant_id can only be applied to a scalar int, a scalar float or a scalar bool.

arcady-lunarg avatar Aug 27 '25 18:08 arcady-lunarg

The "Specialization Constants" section actually has the same text in both extensions (with the exception that ARB_gl_spirv does not have this fixup).

Given that the main implementation of GL_KHR_vulkan_glsl (glslang) seems to already support all byte sizes, it may be fine to adjust the wording to explicitly allow that?

ARB_gl_spirv however serves both as a GLSL extension and GL extension. While the GLSL part only allows int, float and bool similarly to GL_KHR_vulkan_glsl, it is unclear if the GL part supports specifying non-32-bit values in the pConstantValue field of the SpecializeShaderARB function if SPIR-V with non-32-bit specialization constants is provided.

maxime-modulopi avatar Aug 27 '25 21:08 maxime-modulopi