microsoft / hlsl-specs

HLSL Specifications
MIT License
116 stars 29 forks source link

Improve code generated for node record .Get() #256

Open tcorringham opened 2 months ago

tcorringham commented 2 months ago

In node shaders that uses an input record the .Get() access mechanism is a bit clumsy:

 Output[gid] = inputRecord.Get().data1[gid];

and it is tempting to use a local variable to write code like:

 const RecordType record = inputRecord.Get();
 Output[gid] = record.data1[gid];
 ...

In both cases any fields in the record that are not referenced are not loaded. In the first version, only the single array element indexed (by variable gid) is loaded. However, in the second version all elements of the array are loaded (unless the array index happens to have a known value). This can lead to unexpected and surprising runtime overheads, especially when converting existing code to use work graphs.

Describe the solution you'd like The proposed '->' operator would allow more natural code of the form:

Output[gid] = inputRecord->data1[gid];

Alternatively, where there are only invariant indices used (possibly where only a single value is used as an index), only those need be loaded - effectively deferring the generation of the loads until the usage is known just as if the Get() had been used as in the first example.

Describe alternatives you've considered The use of a macro is an alternative to the first example, but just as ugly.

Additional context Example on gobolt : https://godbolt.org/z/bM6nGh1eo

llvm-beanz commented 2 months ago

This adds new syntax to HLSL, so this is a language change and needs to be treated as such. Sending to hlsl-specs.

llvm-beanz commented 3 weeks ago

This feature is likely intwined with the larger feature to add reference support to HLSL (https://github.com/microsoft/hlsl-specs/blob/main/proposals/0006-reference-types.md).