Change SkSL main() from (float x, float y) to (float2 p)

Change-Id: Id046199edd63535ef07e1dfa65fbc7c0f8cefd00
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/269371
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/gm/runtimefunctions.cpp b/gm/runtimefunctions.cpp
index 495e591..000ad77 100644
--- a/gm/runtimefunctions.cpp
+++ b/gm/runtimefunctions.cpp
@@ -25,8 +25,8 @@
         return half4(half3(value), raw.a);
     }
 
-    void main(float x, float y, inout half4 color) {
-        color = blackAndWhite(half4(scale(x), scale(y), gColor.b, 1));
+    void main(float2 p, inout half4 color) {
+        color = blackAndWhite(half4(scale(p.x), scale(p.y), gColor.b, 1));
     }
 )";
 
diff --git a/gm/runtimeshader.cpp b/gm/runtimeshader.cpp
index c37350f..b84d310 100644
--- a/gm/runtimeshader.cpp
+++ b/gm/runtimeshader.cpp
@@ -16,8 +16,8 @@
 const char* gProg = R"(
     uniform half4 gColor;
 
-    void main(float x, float y, inout half4 color) {
-        color = half4(half(x)*(1.0/255), half(y)*(1.0/255), gColor.b, 1);
+    void main(float2 p, inout half4 color) {
+        color = half4(half2(p)*(1.0/255), gColor.b, 1);
     }
 )";
 
diff --git a/modules/canvaskit/canvaskit/extra.html b/modules/canvaskit/canvaskit/extra.html
index 2bc7f9b..8a70da0 100644
--- a/modules/canvaskit/canvaskit/extra.html
+++ b/modules/canvaskit/canvaskit/extra.html
@@ -377,8 +377,8 @@
 
   uniform half4 gColor;
 
-  void main(float x, float y, inout half4 color) {
-      color = half4(half(x)*(1.0/255), half(y)*(1.0/255), gColor.b, 1);
+  void main(float2 p, inout half4 color) {
+      color = half4(half2(p)*(1.0/255), gColor.b, 1);
   }
 `;
     const surface = CanvasKit.MakeCanvasSurface('rtshader');
diff --git a/samplecode/Sample3D.cpp b/samplecode/Sample3D.cpp
index a7b755b..0f17540 100644
--- a/samplecode/Sample3D.cpp
+++ b/samplecode/Sample3D.cpp
@@ -393,8 +393,8 @@
                 return a > b ? a : b;
             }
 
-            void main(float x, float y, inout half4 color) {
-                float3 plane_pos = (localToWorld * float4(x, y, 0, 1)).xyz;
+            void main(float2 p, inout half4 color) {
+                float3 plane_pos = (localToWorld * float4(p, 0, 1)).xyz;
                 float3 plane_norm = normalize_((localToWorld * float4(0, 0, 1, 0)).xyz);
                 float3 light_dir = normalize_(lightPos - plane_pos);
                 float ambient = 0.5;
@@ -555,18 +555,18 @@
                 return n;
             }
 
-            void main(float x, float y, inout half4 color) {
-                float3 norm = convert_normal_sample(sample(normal_map, float2(x, y)));
+            void main(float2 p, inout half4 color) {
+                float3 norm = convert_normal_sample(sample(normal_map, p));
                 float3 plane_norm = normalize(localToWorld * float4(norm, 0)).xyz;
 
-                float3 plane_pos = (localToWorld * float4(x, y, 0, 1)).xyz;
+                float3 plane_pos = (localToWorld * float4(p, 0, 1)).xyz;
                 float3 light_dir = normalize(lightPos - plane_pos);
 
                 float ambient = 0.2;
                 float dp = dot(plane_norm, light_dir);
                 float scale = min(ambient + max(dp, 0), 1);
 
-                color = sample(color_map, float2(x, y)) * half4(float4(scale, scale, scale, 1));
+                color = sample(color_map, p) * half4(float4(scale, scale, scale, 1));
             }
         )";
         auto [effect, error] = SkRuntimeEffect::Make(SkString(code));
diff --git a/site/user/modules/canvaskit.md b/site/user/modules/canvaskit.md
index b41b57e..99612be 100644
--- a/site/user/modules/canvaskit.md
+++ b/site/user/modules/canvaskit.md
@@ -473,12 +473,11 @@
 uniform float4 in_colors0;
 uniform float4 in_colors1;
 
-void main(float x, float y, inout half4 color) {
-    float xx = x - in_center.x;
-    float yy = y - in_center.y;
-    float radius = sqrt(xx*xx + yy*yy);
+void main(float2 p, inout half4 color) {
+    float2 pp = p - in_center;
+    float radius = sqrt(dot(pp, pp));
     radius = sqrt(radius);
-    float angle = atan(yy / xx);
+    float angle = atan(pp.y / pp.x);
     float t = (angle + 3.1415926/2) / (3.1415926);
     t += radius * rad_scale;
     t = fract(t);
diff --git a/src/effects/imagefilters/SkArithmeticImageFilter.cpp b/src/effects/imagefilters/SkArithmeticImageFilter.cpp
index 2148215..e91b572 100644
--- a/src/effects/imagefilters/SkArithmeticImageFilter.cpp
+++ b/src/effects/imagefilters/SkArithmeticImageFilter.cpp
@@ -36,8 +36,8 @@
 in bool enforcePMColor;
 in fragmentProcessor child;
 
-void main(float x, float y, inout half4 color) {
-    half4 dst = sample(child, float2(x, y));
+void main(float2 p, inout half4 color) {
+    half4 dst = sample(child, p);
     color = saturate(half(k.x) * color * dst + half(k.y) * color + half(k.z) * dst + half(k.w));
     @if (enforcePMColor) {
         color.rgb = min(color.rgb, color.a);
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 8216270..c6fb7b7 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -50,7 +50,7 @@
 // This controls the range of values added to color channels
 in int rangeType;
 
-void main(float x, float y, inout half4 color) {
+void main(float2 p, inout half4 color) {
     half value;
     half range;
     @switch (rangeType) {
@@ -67,8 +67,8 @@
     }
     @if (sk_Caps.integerSupport) {
         // This ordered-dither code is lifted from the cpu backend.
-        uint x = uint(x);
-        uint y = uint(y);
+        uint x = uint(p.x);
+        uint y = uint(p.y);
         uint m = (y & 1) << 5 | (x & 1) << 4 |
                  (y & 2) << 2 | (x & 2) << 1 |
                  (y & 4) >> 1 | (x & 4) >> 2;
@@ -76,7 +76,7 @@
     } else {
         // Simulate the integer effect used above using step/mod. For speed, simulates a 4x4
         // dither pattern rather than an 8x8 one.
-        half4 modValues = mod(half4(half(x), half(y), half(x), half(y)), half4(2.0, 2.0, 4.0, 4.0));
+        half4 modValues = mod(half4(half(p.x), half(p.y), half(p.x), half(p.y)), half4(2.0, 2.0, 4.0, 4.0));
         half4 stepValues = step(modValues, half4(1.0, 1.0, 2.0, 2.0));
         value = dot(stepValues, half4(8.0 / 16.0, 4.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0)) - 15.0 / 32.0;
     }
diff --git a/src/gpu/effects/GrSkSLFP.cpp b/src/gpu/effects/GrSkSLFP.cpp
index 48d3b79..bd5a7cd 100644
--- a/src/gpu/effects/GrSkSLFP.cpp
+++ b/src/gpu/effects/GrSkSLFP.cpp
@@ -44,13 +44,8 @@
                             case SkSL::Compiler::FormatArg::Kind::kOutput:
                                 result += args.fOutputColor;
                                 break;
-                            case SkSL::Compiler::FormatArg::Kind::kCoordX:
+                            case SkSL::Compiler::FormatArg::Kind::kCoords:
                                 result += coordsName;
-                                result += ".x";
-                                break;
-                            case SkSL::Compiler::FormatArg::Kind::kCoordY:
-                                result += coordsName;
-                                result += ".y";
                                 break;
                             case SkSL::Compiler::FormatArg::Kind::kUniform:
                                 result += args.fUniformHandler->getUniformCStr(
diff --git a/src/opts/SkRasterPipeline_opts.h b/src/opts/SkRasterPipeline_opts.h
index 0f75d54..9ff871f 100644
--- a/src/opts/SkRasterPipeline_opts.h
+++ b/src/opts/SkRasterPipeline_opts.h
@@ -2701,7 +2701,7 @@
     load4(c->read_from,0, &r,&g,&b,&a);
 }
 
-// shader:      void main(float x, float y, inout half4 color)
+// shader:      void main(float2 p, inout half4 color)
 // colorfilter: void main(inout half4 color)
 STAGE(interpreter, SkRasterPipeline_InterpreterCtx* c) {
     // If N is less than the interpreter's VecWidth, then we are doing more work than necessary in
diff --git a/src/sksl/SkSLCompiler.h b/src/sksl/SkSLCompiler.h
index cf657e5..7eaba35 100644
--- a/src/sksl/SkSLCompiler.h
+++ b/src/sksl/SkSLCompiler.h
@@ -32,8 +32,7 @@
 #define SK_TEXTURESAMPLERS_BUILTIN     10006
 #define SK_OUT_BUILTIN                 10007
 #define SK_LASTFRAGCOLOR_BUILTIN       10008
-#define SK_MAIN_X_BUILTIN              10009
-#define SK_MAIN_Y_BUILTIN              10010
+#define SK_MAIN_COORDS_BUILTIN         10009
 #define SK_WIDTH_BUILTIN               10011
 #define SK_HEIGHT_BUILTIN              10012
 #define SK_FRAGCOORD_BUILTIN              15
@@ -77,8 +76,7 @@
         enum class Kind {
             kInput,
             kOutput,
-            kCoordX,
-            kCoordY,
+            kCoords,
             kUniform,
             kChildProcessor,
             kFunctionName
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index 8d9d42d..fc21c65 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -773,13 +773,11 @@
             case Program::kPipelineStage_Kind: {
                 bool valid;
                 switch (parameters.size()) {
-                    case 3:
-                        valid = parameters[0]->fType == *fContext.fFloat_Type &&
+                    case 2:
+                        valid = parameters[0]->fType == *fContext.fFloat2_Type &&
                                 parameters[0]->fModifiers.fFlags == 0 &&
-                                parameters[1]->fType == *fContext.fFloat_Type &&
-                                parameters[1]->fModifiers.fFlags == 0 &&
-                                parameters[2]->fType == *fContext.fHalf4_Type &&
-                                parameters[2]->fModifiers.fFlags == (Modifiers::kIn_Flag |
+                                parameters[1]->fType == *fContext.fHalf4_Type &&
+                                parameters[1]->fModifiers.fFlags == (Modifiers::kIn_Flag |
                                                                      Modifiers::kOut_Flag);
                         break;
                     case 1:
@@ -791,8 +789,8 @@
                         valid = false;
                 }
                 if (!valid) {
-                    fErrors.error(f.fOffset, "pipeline stage 'main' must be declared main(float, "
-                                             "float, inout half4) or main(inout half4)");
+                    fErrors.error(f.fOffset, "pipeline stage 'main' must be declared main(float2, "
+                                             "inout half4) or main(inout half4)");
                     return;
                 }
                 break;
@@ -878,10 +876,9 @@
         std::shared_ptr<SymbolTable> old = fSymbolTable;
         AutoSymbolTable table(this);
         if (fd.fName == "main" && fKind == Program::kPipelineStage_Kind) {
-            if (parameters.size() == 3) {
-                parameters[0]->fModifiers.fLayout.fBuiltin = SK_MAIN_X_BUILTIN;
-                parameters[1]->fModifiers.fLayout.fBuiltin = SK_MAIN_Y_BUILTIN;
-                parameters[2]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN;
+            if (parameters.size() == 2) {
+                parameters[0]->fModifiers.fLayout.fBuiltin = SK_MAIN_COORDS_BUILTIN;
+                parameters[1]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN;
             } else {
                 SkASSERT(parameters.size() == 1);
                 parameters[0]->fModifiers.fLayout.fBuiltin = SK_OUTCOLOR_BUILTIN;
diff --git a/src/sksl/SkSLPipelineStageCodeGenerator.cpp b/src/sksl/SkSLPipelineStageCodeGenerator.cpp
index 907009d..86cf790 100644
--- a/src/sksl/SkSLPipelineStageCodeGenerator.cpp
+++ b/src/sksl/SkSLPipelineStageCodeGenerator.cpp
@@ -132,13 +132,9 @@
             this->write("%s");
             fArgs->fFormatArgs.push_back(Compiler::FormatArg(Compiler::FormatArg::Kind::kOutput));
             break;
-        case SK_MAIN_X_BUILTIN:
+        case SK_MAIN_COORDS_BUILTIN:
             this->write("%s");
-            fArgs->fFormatArgs.push_back(Compiler::FormatArg(Compiler::FormatArg::Kind::kCoordX));
-            break;
-        case SK_MAIN_Y_BUILTIN:
-            this->write("%s");
-            fArgs->fFormatArgs.push_back(Compiler::FormatArg(Compiler::FormatArg::Kind::kCoordY));
+            fArgs->fFormatArgs.push_back(Compiler::FormatArg(Compiler::FormatArg::Kind::kCoords));
             break;
         default:
             if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag) {
diff --git a/tests/SkRuntimeEffectTest.cpp b/tests/SkRuntimeEffectTest.cpp
index 6982cdc..9bfaf8b 100644
--- a/tests/SkRuntimeEffectTest.cpp
+++ b/tests/SkRuntimeEffectTest.cpp
@@ -16,7 +16,7 @@
 
 DEF_TEST(SkRuntimeEffectInvalidInputs, r) {
     auto test = [r](const char* hdr, const char* expected) {
-        SkString src = SkStringPrintf("%s void main(float x, float y, inout half4 color) {}", hdr);
+        SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) {}", hdr);
         auto[effect, errorText] = SkRuntimeEffect::Make(src);
         REPORTER_ASSERT(r, !effect);
         REPORTER_ASSERT(r, errorText.contains(expected),
@@ -55,7 +55,7 @@
 class TestEffect {
 public:
     TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) {
-        SkString src = SkStringPrintf("%s void main(float x, float y, inout half4 color) { %s }",
+        SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
                                       hdr, body);
         auto[effect, errorText] = SkRuntimeEffect::Make(src);
         if (!effect) {
@@ -139,7 +139,7 @@
     }
     REPORTER_ASSERT(r, surface);
 
-    TestEffect xy(r, "", "color = half4(half(x - 0.5), half(y - 0.5), 0, 1);");
+    TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);");
     xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
 
     using float4 = std::array<float, 4>;
diff --git a/tools/viewer/SkSLSlide.cpp b/tools/viewer/SkSLSlide.cpp
index 01619f8..7335669 100644
--- a/tools/viewer/SkSLSlide.cpp
+++ b/tools/viewer/SkSLSlide.cpp
@@ -39,8 +39,8 @@
 
         "in fragmentProcessor fp;\n"
         "\n"
-        "void main(float x, float y, inout half4 color) {\n"
-        "    color = sample(fp, float2(x, y));\n"
+        "void main(float2 p, inout half4 color) {\n"
+        "    color = sample(fp, p);\n"
         "}\n";
 }