[HLSL] Implement `.sample[index][pos]` operator for MS textures
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## References
- https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2dms
- Existing `mips` implementation: `clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp` (`addMipsMember`, `addMipsType`, `addMipsSliceType`)
## Description
Add the `.sample[index][pos]` double-subscript operator for `Texture2DMS` and `Texture2DMSArray`. This provides `texture.sample[sampleIndex][coord]` syntax for reading individual samples, mirroring the existing `texture.mips[level][coord]` pattern.
The implementation follows the same two-level nested type structure as `mips`:
```
class sample_slice_type {
__handle;
__sampleIndex;
T operator[](uint2 coord); // performs resource load with sample index
};
class sample_type {
__handle;
sample_slice_type operator[](uint index); // captures sample index
};
sample_type sample; // public member on the texture
```
In DXC, `sample[index][pos]` lowers to DXIL `TextureLoad` (op 66) — the same op as `Load` and `mips[][]`. The first argument (`MipLevelOrSampleCount`) is overloaded: for MS textures it's the sample index.
## Tasks
- [ ] **Add `addSampleSliceType`** in `clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp`
- Mirrors `addMipsSliceType`
- Nested `sample_slice_type` holds handle + `__sampleIndex` (uint)
- `operator[](uint2 coord)` calls `__builtin_hlsl_resource_load_ms` with handle, coord, and stored sample index
- For `Texture2DMS`: coord is `uint2`; for `Texture2DMSArray`: coord is `uint3` (x, y, arrayIndex)
- [ ] **Add `addSampleType`** in `clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp`
- Mirrors `addMipsType`
- Nested `sample_type` holds handle
- `operator[](uint index)` returns `sample_slice_type` with handle and sample index captured
- [ ] **Add `addSampleMember`** in `clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp`
- Mirrors `addMipsMember`
- Adds `sample_type sample` public member to the texture record
- [ ] **Wire into MS texture setup** in `clang/lib/Sema/HLSLExternalSemaSource.cpp`
- Call `addSampleMember(Dim)` when setting up `Texture2DMS` and `Texture2DMSArray`
- [ ] **Write tests**
- CodeGen (`clang/test/CodeGenHLSL/resources/`): `Texture2DMS-Sample-Subscript.hlsl`, `Texture2DMSArray-Sample-Subscript.hlsl`
- Sema (`clang/test/SemaHLSL/Resources/`): `Texture2DMS-sample-errors.hlsl`
## Notes
- Unlike mips (which concatenates coord+level into one vector for `__builtin_hlsl_resource_load_level`), the sample subscript passes coord and sampleIndex as separate arguments to `__builtin_hlsl_resource_load_ms`.
Contributor guide
Assessment
This issue has not been assessed yet.