Skip to content

Commit

Permalink
[Metal] Support SV_TargetN. (#4390)
Browse files Browse the repository at this point in the history
* [Metal] Support SV_TargetN.

* Fix.
  • Loading branch information
csyonghe committed Jun 14, 2024
1 parent 2cc9690 commit a210091
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
23 changes: 14 additions & 9 deletions source/slang/slang-ir-metal-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "slang-ir-util.h"
#include "slang-ir-clone.h"
#include "slang-ir-specialize-address-space.h"
#include "slang-parameter-binding.h"
#include "slang-ir-legalize-varying-params.h"

namespace Slang
Expand Down Expand Up @@ -237,13 +238,15 @@ namespace Slang
return builder.getVectorType(builder.getBasicType(BaseType::UInt), builder.getIntValue(builder.getIntType(), 3));
}

MetalSystemValueInfo getSystemValueInfo(IRBuilder& builder, String semanticName, UInt attrIndex)
MetalSystemValueInfo getSystemValueInfo(IRBuilder& builder, String inSemanticName)
{
SLANG_UNUSED(attrIndex);
inSemanticName = inSemanticName.toLower();

MetalSystemValueInfo result = {};
UnownedStringSlice semanticName;
UnownedStringSlice semanticIndex;
splitNameAndIndex(inSemanticName.getUnownedSlice(), semanticName, semanticIndex);

semanticName = semanticName.toLower();
MetalSystemValueInfo result = {};

if (semanticName == "sv_position")
{
Expand Down Expand Up @@ -374,9 +377,12 @@ namespace Slang
result.requiredType = builder.getBasicType(BaseType::UInt);
result.altRequiredType = builder.getBasicType(BaseType::UInt16);
}
else if (semanticName == "sv_target")
else if (semanticName.startsWith("sv_target"))
{
result.metalSystemValueName = (StringBuilder() << "color(" << String(attrIndex) << ")").produceString();

result.metalSystemValueName = (StringBuilder() << "color("
<< (semanticIndex.getLength() != 0 ? semanticIndex : toSlice("0"))
<< ")").produceString();
}
else
{
Expand Down Expand Up @@ -404,7 +410,7 @@ namespace Slang
{
if (semanticDecor->getSemanticName().startsWithCaseInsensitive(toSlice("sv_")))
{
auto sysValInfo = getSystemValueInfo(builder, semanticDecor->getSemanticName(), semanticDecor->getSemanticIndex());
auto sysValInfo = getSystemValueInfo(builder, semanticDecor->getSemanticName());
if (sysValInfo.isUnsupported || sysValInfo.isSpecial)
{
reportUnsupportedSystemAttribute(sink, field, semanticDecor->getSemanticName());
Expand Down Expand Up @@ -670,9 +676,8 @@ namespace Slang

auto param = workItem.param;
auto semanticName = workItem.attrName;
auto sysAttrIndex = workItem.attrIndex;

auto info = getSystemValueInfo(builder, semanticName, sysAttrIndex);
auto info = getSystemValueInfo(builder, semanticName);
if (info.isSpecial)
{
if (semanticName == "sv_innercoverage")
Expand Down
4 changes: 1 addition & 3 deletions source/slang/slang-parameter-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,7 @@ static bool isDigit(char c)
return (c >= '0') && (c <= '9');
}

/// Given a string that specifies a name and index (e.g., `COLOR0`),
/// split it into slices for the name part and the index part.
static void splitNameAndIndex(
void splitNameAndIndex(
UnownedStringSlice const& text,
UnownedStringSlice& outName,
UnownedStringSlice& outDigits)
Expand Down
8 changes: 8 additions & 0 deletions source/slang/slang-parameter-binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ void generateParameterBindings(
TargetRequest* targetReq,
DiagnosticSink* sink);

/// Given a string that specifies a name and index (e.g., `COLOR0`),
/// split it into slices for the name part and the index part.
///
void splitNameAndIndex(
UnownedStringSlice const& text,
UnownedStringSlice& outName,
UnownedStringSlice& outDigits);

}

#endif
20 changes: 20 additions & 0 deletions tests/metal/sv_target.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//TEST:SIMPLE(filecheck=CHECK): -target metal
//TEST:SIMPLE(filecheck=CHECK-ASM): -target metallib

struct Output
{
float4 Diffuse : SV_Target0;
float4 Normal : SV_Target1;
float4 Material : SV_Target2;
}

// CHECK-ASM: define {{.*}} @fragMain
// CHECK: color(0)
// CHECK: color(1)
// CHECK: color(2)

[shader("fragment")]
Output fragMain()
{
return { float4(1), float4(2), float4(3) };
}

0 comments on commit a210091

Please sign in to comment.