KhronosGroup / GLSL

GLSL Shading Language Specification and Extensions
Other
337 stars 98 forks source link

Variables of opaque types should be allowed #235

Open JuanDiegoMontoya opened 6 months ago

JuanDiegoMontoya commented 6 months ago

It's currently illegal to have a variable (local or global) with an opaque type, e.g., sampler2D or image2D. Being able to do so would have a number of advantages, particularly for bindless renderers where all resources may reside in array descriptors. The main one would be the ability to make type-safe aliases of resources.

One way to deal with this issue is to use macros that refer to specific indices of a descriptor array:

#define SAMPLER_NEAREST       s_bigSamplerArray[0]
#define SAMPLER_LINEAR_MIP    s_bigSamplerArray[1]
#define SAMPLER_LINEAR_SHADOW s_bigSamplerArray[2]
...
color = texture(sampler2D(fooTexture, SAMPLER_LINEAR_MIP), uv);

But macros have several issues:

Being able to make a variable that refers to an object of an opaque type would generally make it easier to do logic on resources without having to add a layer of "indirection" (e.g., an if-else chain or switch statement in a bindful renderer).

While it's mostly a quality-of-life feature, it seems odd for it to be missing in the first place.

P.S. HLSL allows declaring locals and return values of opaque types, but they must be guaranteed to map to one unique resource (so no conditionally assigning them).

Related issues: