Skip to content

Commit

Permalink
Support interpolation modifiers for WGSL (shader-slang#5641)
Browse files Browse the repository at this point in the history
* wgsl: Support interpolation modifiers

* Move struct key decorations to flattened structs.
** This includes interpolation mode decorations, which must be in the flattened struct.
* Emit interpolation attribute.
* Enable tests/render/nointerpolation.hlsl for WGSL, as a result.

This closes shader-slang#5625.

* Add new expected output for 'nointerpolation' test
  • Loading branch information
aleino-nv authored Nov 25, 2024
1 parent 044b52c commit d282701
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
49 changes: 49 additions & 0 deletions source/slang/slang-emit-wgsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1506,4 +1506,53 @@ void WGSLSourceEmitter::emitIntrinsicCallExprImpl(
inOuterPrec);
}

void WGSLSourceEmitter::emitInterpolationModifiersImpl(
IRInst* varInst,
IRType* /* valueType */,
IRVarLayout* /* layout */)
{
char const* interpolationType = nullptr;
char const* interpolationSampling = nullptr;
for (auto dd : varInst->getDecorations())
{
if (dd->getOp() != kIROp_InterpolationModeDecoration)
continue;
auto decoration = (IRInterpolationModeDecoration*)dd;
IRInterpolationMode mode = decoration->getMode();
switch (mode)
{
case IRInterpolationMode::NoInterpolation:
interpolationType = "flat";
break;
case IRInterpolationMode::NoPerspective:
case IRInterpolationMode::Linear:
interpolationType = "linear";
break;
case IRInterpolationMode::Sample:
interpolationSampling = "sample";
break;
case IRInterpolationMode::Centroid:
interpolationSampling = "centroid";
break;
}
}

if (interpolationType)
{
m_writer->emit("@interpolate(");
m_writer->emit(interpolationType);
if (interpolationSampling)
{
m_writer->emit(", ");
m_writer->emit(interpolationSampling);
}
m_writer->emit(") ");
}

// TODO: Check the following:
// "User-defined vertex outputs and fragment inputs of scalar or vector
// integer type must always be specified with interpolation type flat."
// https://www.w3.org/TR/WGSL/#interpolation
}

} // namespace Slang
4 changes: 4 additions & 0 deletions source/slang/slang-emit-wgsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
virtual void emitStructFieldAttributes(IRStructType* structType, IRStructField* field)
SLANG_OVERRIDE;
virtual void emitCallArg(IRInst* inst) SLANG_OVERRIDE;
virtual void emitInterpolationModifiersImpl(
IRInst* varInst,
IRType* valueType,
IRVarLayout* layout) SLANG_OVERRIDE;

virtual void emitIntrinsicCallExprImpl(
IRCall* inst,
Expand Down
7 changes: 5 additions & 2 deletions source/slang/slang-ir-wgsl-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,10 @@ struct LegalizeWGSLEntryPointContext
// 2. If IRStructType:
// 2a. Recurse this function with 'decorations that carry semantic info' from parent.
// 3. If not IRStructType:
// 3a. Emit 'newField' equal to 'oldField', add 'decorations which carry semantic info'.
// 3a. Emit 'newField' with 'newKey' equal to 'oldField' and 'oldKey', respectively,
// where 'oldKey' is the key corresponding to 'oldField'.
// Add 'decorations which carry semantic info' to 'newField', and move all decorations
// of 'oldKey' to 'newKey'.
// 3b. Store a mapping from 'oldField' to 'newField' in 'mapFieldToField'. This info is
// needed to copy between types.
for (auto oldField : src->getFields())
Expand Down Expand Up @@ -674,7 +677,7 @@ struct LegalizeWGSLEntryPointContext

// step 3a
auto newKey = builder.createStructKey();
copyNameHintAndDebugDecorations(newKey, oldKey);
oldKey->transferDecorationsTo(newKey);

auto newField = builder.createStructField(dst, newKey, oldField->getFieldType());
copyNameHintAndDebugDecorations(newField, oldField);
Expand Down
1 change: 0 additions & 1 deletion tests/expected-failure-github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ tests/language-feature/generics/variadic-0.slang.4 syn (wgpu)
tests/language-feature/shader-params/interface-shader-param-ordinary.slang.4 syn (wgpu)
tests/language-feature/swizzles/matrix-swizzle-write-array.slang.3 syn (wgpu)
tests/language-feature/swizzles/matrix-swizzle-write-swizzle.slang.3 syn (wgpu)
tests/render/nointerpolation.hlsl (wgpu)
2 changes: 0 additions & 2 deletions tests/render/nointerpolation.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TEST(smoke):COMPARE_HLSL_RENDER:
// WGSL: nointerpolate doesn't work #5625
//DISABLE_TEST(smoke):COMPARE_HLSL_RENDER: -wgpu
// TODO: Investigate Metal failure
//DISABLE_TEST(smoke):COMPARE_HLSL_RENDER: -mtl

Expand Down
5 changes: 5 additions & 0 deletions tests/render/nointerpolation.hlsl.3.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
result code = 0
standard error = {
}
standard output = {
}
Binary file added tests/render/nointerpolation.hlsl.3.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d282701

Please sign in to comment.