Allow Metal shaders to return half4 colors.

This allows us to write SkSL shaders which are valid both for use as
Runtime Effect, and for compilation with skslc targeting Metal.

Change-Id: I74e125d81865d4092e657a7d9948d2e72054bda5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/357777
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
John Stiles 2021-01-22 16:47:42 -05:00 committed by Skia Commit-Bot
parent 1089c0c59c
commit 97d18174d6
7 changed files with 36 additions and 2 deletions

View File

@ -189,6 +189,8 @@ sksl_metal_tests = [
"/sksl/metal/OpaqueTypeInStruct.sksl",
"/sksl/metal/OutParams.sksl",
"/sksl/metal/OutVarsRequireLocation.sksl",
"/sksl/metal/ReturnBadTypeFromMain.sksl",
"/sksl/metal/ReturnColorFromMain.sksl",
"/sksl/metal/SamplerGlobals.sksl",
"/sksl/metal/SwizzleHelper.sksl",
]

View File

@ -0,0 +1,3 @@
int3 main() {
return int3(1, 2, 3);
}

View File

@ -0,0 +1,3 @@
half4 main() {
return half4(1, 2, 3, 4);
}

View File

@ -1121,7 +1121,8 @@ void IRGenerator::convertFunction(const ASTNode& f) {
break;
default:
if (parameters.size()) {
this->errorReporter().error(f.fOffset, "shader 'main' must have zero parameters");
this->errorReporter().error(f.fOffset,
"shader 'main' must have zero parameters");
}
break;
}

View File

@ -1904,7 +1904,14 @@ void MetalCodeGenerator::writeReturnStatementFromMain() {
void MetalCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
if (fCurrentFunction && fCurrentFunction->name() == "main") {
if (r.expression()) {
fErrors.error(r.fOffset, "Metal does not support returning values from main()");
if (r.expression()->type() == *fContext.fTypes.fHalf4) {
this->write("_out.sk_FragColor = ");
this->writeExpression(*r.expression(), kTopLevel_Precedence);
this->writeLine(";");
} else {
fErrors.error(r.fOffset, "Metal does not support returning '" +
r.expression()->type().description() + "' from main()");
}
}
this->writeReturnStatementFromMain();
return;

View File

@ -0,0 +1,4 @@
### Compilation failed:
error: 2: Metal does not support returning 'int3' from main()
1 error

View File

@ -0,0 +1,14 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct Inputs {
};
struct Outputs {
float4 sk_FragColor [[color(0)]];
};
fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Outputs _out;
(void)_out;
_out.sk_FragColor = float4(1.0, 2.0, 3.0, 4.0);
return _out;
}