Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metal] Support SV_TargetN. #4390

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) };
}
Loading