shader-slang / slang

Making it easier to work with shaders
MIT License
2.06k stars 177 forks source link

SPIR-V `SV_InstanceId` support in pixel shader #4368

Closed ArielG-NV closed 3 months ago

ArielG-NV commented 3 months ago

fixes: #3087

Problem:

  1. SV_InstanceID (HLSL) is allowed as an output semantic with HLSL, it is also allowed for input into pixel shader. We need to account for multiple entry points in 1 file using both of these scenarios at once.
  2. SPIRV does not allow SV_InstanceID equivalent as a builtin, SV_InstanceID must be passed as a regular Flat Input from vertex shader.

Solution: Slang needs to treat these SV objects as non built-in's. As a result:

  1. Slang needs to allocate for vertex output and fragment Input binding slots for all SV_InstanceID uses (if the target is SPIRV). This allows Slang to prepare an open slot to bind these SV objects to.
  2. Slang needs to not emit built in modifier for these not actual built in variables (under the specific circumstances described).

Note: we do not need to add Flat ourselves to any fragment Input variables since the parameter being transformed is a uint. This means to avoid validation errors Flat must be added anyways, and Slang implements this behavior for us.

ArielG-NV commented 3 months ago

Also make sure you are testing that the keyword Flat is present in the emitted Spirv-asm

I added to the test, this is a good suggestion (it is a VK validation error to not have Flat)

But I think the variable needs to be marked as Flat. I don't see anywhere in your change to make it Flat.

We do not need to add Flat ourselves to any fragment Input variables since the parameter being transformed is a uint. This means to avoid validation errors Flat must be added anyways, and Slang implements this behavior for us. This is behavior which Slang implements is used in more than 1 instance, so it is likely correct to be using this behavior.