added constant swizzle support for Metal

Bug: skia:
Change-Id: Ia7d81920ddc341aa40da6af534d846f3e805a109
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/192031
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
This commit is contained in:
Ethan Nicholas 2019-03-07 15:11:31 -05:00 committed by Skia Commit-Bot
parent 858feccb58
commit 5476f2e9b8
2 changed files with 36 additions and 1 deletions

View File

@ -577,10 +577,23 @@ void MetalCodeGenerator::writeFieldAccess(const FieldAccess& f) {
}
void MetalCodeGenerator::writeSwizzle(const Swizzle& swizzle) {
int last = swizzle.fComponents.back();
if (last == SKSL_SWIZZLE_0 || last == SKSL_SWIZZLE_1) {
this->writeType(swizzle.fType);
this->write("(");
}
this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
this->write(".");
for (int c : swizzle.fComponents) {
this->write(&("x\0y\0z\0w\0"[c * 2]));
if (c >= 0) {
this->write(&("x\0y\0z\0w\0"[c * 2]));
}
}
if (last == SKSL_SWIZZLE_0) {
this->write(", 0)");
}
else if (last == SKSL_SWIZZLE_1) {
this->write(", 1)");
}
}

View File

@ -109,3 +109,25 @@ DEF_TEST(SkSLMetalMatrices, r) {
" return *_out;\n"
"}\n");
}
DEF_TEST(SkSLMetalConstantSwizzle, r) {
test(r,
"void main() {"
"sk_FragColor = half4(0.5).rgb1;"
"}",
*SkSL::ShaderCapsFactory::Default(),
"#include <metal_stdlib>\n"
"#include <simd/simd.h>\n"
"using namespace metal;\n"
"struct Inputs {\n"
"};\n"
"struct Outputs {\n"
" float4 sk_FragColor [[color(0)]];\n"
"};\n"
"fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n"
" Outputs _outputStruct;\n"
" thread Outputs* _out = &_outputStruct;\n"
" _out->sk_FragColor = float4(float4(0.5).xyz, 1);\n"
" return *_out;\n"
"}\n");
}