DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

[SER] Const correctness with HitObjects and payload

Open Hengoo opened this issue 1 year ago • 5 comments

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)

Hengoo avatar Sep 12 '25 15:09 Hengoo

Sorry, spirv label was not intended, but I think I cannot remove it anymore

Hengoo avatar Sep 12 '25 15:09 Hengoo

@simoll - can you have first look at this one as well please?

damyanp avatar Sep 12 '25 15:09 damyanp

@damyanp yup

simoll avatar Sep 15 '25 13:09 simoll

There is two issues at play here:

  1. Sema does not reject HLSL that feeds const values into [in]out params declared in gen_intrin_main.txt -> const payloads should be rejected during Sema as their Invoke param is declared inout. Fixable in Sema with additional diagnosis.
  2. SetShaderTableIndex modifies the object but Sema does not validate that the object is mutable.. would need a new intrinsic attribute (eg const in gen_intrin_main.txt) and Sema validation.

simoll avatar Sep 17 '25 11:09 simoll

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);

V-FEXrt avatar Oct 07 '25 23:10 V-FEXrt