KhronosGroup / KhronosGroup/GLSL

Variables of opaque types should be allowed

Open
#235 0 comments 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
458
Forks
114
Avg merge
4m
Merged PRs (30d)
1

Description

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:
```glsl
#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:
- Inability to assign to them at runtime
- Infinite scope (unless explicitly `#undef`'d at some point)
- Can cause confusing error messages or surprising bugs due to being simple text replacement

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:
- https://github.com/KhronosGroup/GLSL/issues/57

Contributor guide

Open the contributing guide

Research direction

No source files or tests are named in the issue. Read the opaque-type rules and related issue #57 first; done means the language specification permits local and global variables of opaque types while preserving the stated resource-uniqueness constraints.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.