Skip to content

Commit a210091

Browse files
authored
[Metal] Support SV_TargetN. (shader-slang#4390)
* [Metal] Support SV_TargetN. * Fix.
1 parent 2cc9690 commit a210091

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "slang-ir-util.h"
66
#include "slang-ir-clone.h"
77
#include "slang-ir-specialize-address-space.h"
8+
#include "slang-parameter-binding.h"
89
#include "slang-ir-legalize-varying-params.h"
910

1011
namespace Slang
@@ -237,13 +238,15 @@ namespace Slang
237238
return builder.getVectorType(builder.getBasicType(BaseType::UInt), builder.getIntValue(builder.getIntType(), 3));
238239
}
239240

240-
MetalSystemValueInfo getSystemValueInfo(IRBuilder& builder, String semanticName, UInt attrIndex)
241+
MetalSystemValueInfo getSystemValueInfo(IRBuilder& builder, String inSemanticName)
241242
{
242-
SLANG_UNUSED(attrIndex);
243+
inSemanticName = inSemanticName.toLower();
243244

244-
MetalSystemValueInfo result = {};
245+
UnownedStringSlice semanticName;
246+
UnownedStringSlice semanticIndex;
247+
splitNameAndIndex(inSemanticName.getUnownedSlice(), semanticName, semanticIndex);
245248

246-
semanticName = semanticName.toLower();
249+
MetalSystemValueInfo result = {};
247250

248251
if (semanticName == "sv_position")
249252
{
@@ -374,9 +377,12 @@ namespace Slang
374377
result.requiredType = builder.getBasicType(BaseType::UInt);
375378
result.altRequiredType = builder.getBasicType(BaseType::UInt16);
376379
}
377-
else if (semanticName == "sv_target")
380+
else if (semanticName.startsWith("sv_target"))
378381
{
379-
result.metalSystemValueName = (StringBuilder() << "color(" << String(attrIndex) << ")").produceString();
382+
383+
result.metalSystemValueName = (StringBuilder() << "color("
384+
<< (semanticIndex.getLength() != 0 ? semanticIndex : toSlice("0"))
385+
<< ")").produceString();
380386
}
381387
else
382388
{
@@ -404,7 +410,7 @@ namespace Slang
404410
{
405411
if (semanticDecor->getSemanticName().startsWithCaseInsensitive(toSlice("sv_")))
406412
{
407-
auto sysValInfo = getSystemValueInfo(builder, semanticDecor->getSemanticName(), semanticDecor->getSemanticIndex());
413+
auto sysValInfo = getSystemValueInfo(builder, semanticDecor->getSemanticName());
408414
if (sysValInfo.isUnsupported || sysValInfo.isSpecial)
409415
{
410416
reportUnsupportedSystemAttribute(sink, field, semanticDecor->getSemanticName());
@@ -670,9 +676,8 @@ namespace Slang
670676

671677
auto param = workItem.param;
672678
auto semanticName = workItem.attrName;
673-
auto sysAttrIndex = workItem.attrIndex;
674679

675-
auto info = getSystemValueInfo(builder, semanticName, sysAttrIndex);
680+
auto info = getSystemValueInfo(builder, semanticName);
676681
if (info.isSpecial)
677682
{
678683
if (semanticName == "sv_innercoverage")

source/slang/slang-parameter-binding.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,7 @@ static bool isDigit(char c)
480480
return (c >= '0') && (c <= '9');
481481
}
482482

483-
/// Given a string that specifies a name and index (e.g., `COLOR0`),
484-
/// split it into slices for the name part and the index part.
485-
static void splitNameAndIndex(
483+
void splitNameAndIndex(
486484
UnownedStringSlice const& text,
487485
UnownedStringSlice& outName,
488486
UnownedStringSlice& outDigits)

source/slang/slang-parameter-binding.h

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ void generateParameterBindings(
2929
TargetRequest* targetReq,
3030
DiagnosticSink* sink);
3131

32+
/// Given a string that specifies a name and index (e.g., `COLOR0`),
33+
/// split it into slices for the name part and the index part.
34+
///
35+
void splitNameAndIndex(
36+
UnownedStringSlice const& text,
37+
UnownedStringSlice& outName,
38+
UnownedStringSlice& outDigits);
39+
3240
}
3341

3442
#endif

tests/metal/sv_target.slang

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target metal
2+
//TEST:SIMPLE(filecheck=CHECK-ASM): -target metallib
3+
4+
struct Output
5+
{
6+
float4 Diffuse : SV_Target0;
7+
float4 Normal : SV_Target1;
8+
float4 Material : SV_Target2;
9+
}
10+
11+
// CHECK-ASM: define {{.*}} @fragMain
12+
// CHECK: color(0)
13+
// CHECK: color(1)
14+
// CHECK: color(2)
15+
16+
[shader("fragment")]
17+
Output fragMain()
18+
{
19+
return { float4(1), float4(2), float4(3) };
20+
}

0 commit comments

Comments
 (0)