DirectXShaderCompiler
DirectXShaderCompiler copied to clipboard
[SER] Const correctness with HitObjects and payload
Description
You can write code that modifies const HitObject and const payload. I would expect to get a compiler error instead.
Steps to Reproduce
RaytracingAccelerationStructure gScene : register(t0, space0); // acceleration structure
RWBuffer<float> output : register(u0, space0);
struct [raypayload] Payload {
float value : write(caller, closesthit, miss) : read(caller, closesthit, miss);
};
[shader("raygeneration")]
void BasicRayGen()
{
RayDesc rayInit;
rayInit.TMin = 0;
rayInit.TMax = 1;
rayInit.Origin = float3(1, 1, 1);
rayInit.Direction = float3(1, 1, 1);
const RayDesc ray = rayInit;
Payload pInit;
pInit.value = 0.0;
const Payload p = pInit;
// Anyhit shader could modify payload
const dx::HitObject obj = dx::HitObject::TraceRay(gScene, 0, 0xff, 0, 0, 0, ray, p);
// this modifies obj
obj.SetShaderTableIndex(42);
// Invoked shader could modifiy payload
dx::HitObject::Invoke(obj, p);
output[0] = p.value;
}
-T lib_6_9
See https://godbolt.org/z/fexT48588
Actual Behavior
This compiles without any issues / warning.
Environment
- DXC version: dxcompiler.dll: 1.9 - 1.8.2505.32 (https://github.com/microsoft/DirectXShaderCompiler/commit/b106a961d09221b3c5bdb37be45b679257da08b8); dxil.dll: 1.9(1.8.2505.32)
Sorry, spirv label was not intended, but I think I cannot remove it anymore
@simoll - can you have first look at this one as well please?
@damyanp yup
There is two issues at play here:
- Sema does not reject HLSL that feeds const values into
[in]outparams declared in gen_intrin_main.txt -> const payloads should be rejected during Sema as their Invoke param is declaredinout. Fixable in Sema with additional diagnosis. - SetShaderTableIndex modifies the object but Sema does not validate that the object is mutable.. would need a new intrinsic attribute (eg
constin gen_intrin_main.txt) and Sema validation.
The PR up for #7761 also resolves this issue.
bug3.hlsl:21:31: error: no matching function for call to 'TraceRay'
const dx::HitObject obj = dx::HitObject::TraceRay(gScene, 0, 0xff, 0, 0, 0, ray, p);
^~~~~~~~~~~~~~~~~~~~~~~
bug3.hlsl:25:5: error: no matching function for call to 'Invoke'
dx::HitObject::Invoke(obj, p);