Skip to content

Commit d282701

Browse files
authored
Support interpolation modifiers for WGSL (shader-slang#5641)
* 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
1 parent 044b52c commit d282701

7 files changed

+63
-5
lines changed

source/slang/slang-emit-wgsl.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -1506,4 +1506,53 @@ void WGSLSourceEmitter::emitIntrinsicCallExprImpl(
15061506
inOuterPrec);
15071507
}
15081508

1509+
void WGSLSourceEmitter::emitInterpolationModifiersImpl(
1510+
IRInst* varInst,
1511+
IRType* /* valueType */,
1512+
IRVarLayout* /* layout */)
1513+
{
1514+
char const* interpolationType = nullptr;
1515+
char const* interpolationSampling = nullptr;
1516+
for (auto dd : varInst->getDecorations())
1517+
{
1518+
if (dd->getOp() != kIROp_InterpolationModeDecoration)
1519+
continue;
1520+
auto decoration = (IRInterpolationModeDecoration*)dd;
1521+
IRInterpolationMode mode = decoration->getMode();
1522+
switch (mode)
1523+
{
1524+
case IRInterpolationMode::NoInterpolation:
1525+
interpolationType = "flat";
1526+
break;
1527+
case IRInterpolationMode::NoPerspective:
1528+
case IRInterpolationMode::Linear:
1529+
interpolationType = "linear";
1530+
break;
1531+
case IRInterpolationMode::Sample:
1532+
interpolationSampling = "sample";
1533+
break;
1534+
case IRInterpolationMode::Centroid:
1535+
interpolationSampling = "centroid";
1536+
break;
1537+
}
1538+
}
1539+
1540+
if (interpolationType)
1541+
{
1542+
m_writer->emit("@interpolate(");
1543+
m_writer->emit(interpolationType);
1544+
if (interpolationSampling)
1545+
{
1546+
m_writer->emit(", ");
1547+
m_writer->emit(interpolationSampling);
1548+
}
1549+
m_writer->emit(") ");
1550+
}
1551+
1552+
// TODO: Check the following:
1553+
// "User-defined vertex outputs and fragment inputs of scalar or vector
1554+
// integer type must always be specified with interpolation type flat."
1555+
// https://www.w3.org/TR/WGSL/#interpolation
1556+
}
1557+
15091558
} // namespace Slang

source/slang/slang-emit-wgsl.h

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
4242
virtual void emitStructFieldAttributes(IRStructType* structType, IRStructField* field)
4343
SLANG_OVERRIDE;
4444
virtual void emitCallArg(IRInst* inst) SLANG_OVERRIDE;
45+
virtual void emitInterpolationModifiersImpl(
46+
IRInst* varInst,
47+
IRType* valueType,
48+
IRVarLayout* layout) SLANG_OVERRIDE;
4549

4650
virtual void emitIntrinsicCallExprImpl(
4751
IRCall* inst,

source/slang/slang-ir-wgsl-legalize.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,10 @@ struct LegalizeWGSLEntryPointContext
629629
// 2. If IRStructType:
630630
// 2a. Recurse this function with 'decorations that carry semantic info' from parent.
631631
// 3. If not IRStructType:
632-
// 3a. Emit 'newField' equal to 'oldField', add 'decorations which carry semantic info'.
632+
// 3a. Emit 'newField' with 'newKey' equal to 'oldField' and 'oldKey', respectively,
633+
// where 'oldKey' is the key corresponding to 'oldField'.
634+
// Add 'decorations which carry semantic info' to 'newField', and move all decorations
635+
// of 'oldKey' to 'newKey'.
633636
// 3b. Store a mapping from 'oldField' to 'newField' in 'mapFieldToField'. This info is
634637
// needed to copy between types.
635638
for (auto oldField : src->getFields())
@@ -674,7 +677,7 @@ struct LegalizeWGSLEntryPointContext
674677

675678
// step 3a
676679
auto newKey = builder.createStructKey();
677-
copyNameHintAndDebugDecorations(newKey, oldKey);
680+
oldKey->transferDecorationsTo(newKey);
678681

679682
auto newField = builder.createStructField(dst, newKey, oldField->getFieldType());
680683
copyNameHintAndDebugDecorations(newField, oldField);

tests/expected-failure-github.txt

-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ tests/language-feature/generics/variadic-0.slang.4 syn (wgpu)
2020
tests/language-feature/shader-params/interface-shader-param-ordinary.slang.4 syn (wgpu)
2121
tests/language-feature/swizzles/matrix-swizzle-write-array.slang.3 syn (wgpu)
2222
tests/language-feature/swizzles/matrix-swizzle-write-swizzle.slang.3 syn (wgpu)
23-
tests/render/nointerpolation.hlsl (wgpu)

tests/render/nointerpolation.hlsl

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
//TEST(smoke):COMPARE_HLSL_RENDER:
2-
// WGSL: nointerpolate doesn't work #5625
3-
//DISABLE_TEST(smoke):COMPARE_HLSL_RENDER: -wgpu
42
// TODO: Investigate Metal failure
53
//DISABLE_TEST(smoke):COMPARE_HLSL_RENDER: -mtl
64

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
result code = 0
2+
standard error = {
3+
}
4+
standard output = {
5+
}
31.7 KB
Loading

0 commit comments

Comments
 (0)