Skip to content

[HLSL] Codegen for resource array element access #145424

Open
@hekota

Description

@hekota

Add support for fixed-size resource arrays to ArraySubscriptExpr AST node handling in Clang codegen. If the array indexed by ArraySubscriptExpr is a resource array declared at global scope and the expected result is a single resource, the code for array element access should be generated as a resource class constructor call with the appropriate indexing and binding values.

For example in this simple shader:

RWBuffer<float> A[4] : register(u10);
RWStructuredBuffer<float> Out;

[numthreads(4,1,1)]
void main() {
  Out[0] = A[2][1];
}

The code generated for the A[2][1] expression should be equivalent RWBuffer<float>(10, 0, 4, 2, "A")[1] - that is, a constructor call followed by a subscript operator invoked on the resource class. Note that the constructor arguments represent the resource binding register 10, space 0, size of the array 4, and index in the resource array 2. The array indices can be constant values or dynamic.

This will also require changes in Sema. While the resource arrays will not be initialized by Sema, we must ensure that the constructor for the specific resource template class is instantiated by Sema and its definition is emitted, so that Clang codegen can call it.

This includes multi-dimensional global arrays as well:

RWBuffer<float> B[4][4] : register(u2);
RWBuffer<float> C[2][2][5] : register(u10, space1);

RWStructuredBuffer<float> Out;

[numthreads(4,1,1)]
void main() {
  Out[0] = B[3][2][0] + C[1][0][3][0];
}

Support for unbounded resource arrays will be added in #145427.

Design proposal: llvm/wg-hlsl#206

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Support

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions