Added DSL sk_SampleCoord var

Change-Id: I539a288765243b2b9d994240c7917b036590f49d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/370916
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/sksl/dsl/DSLVar.cpp b/src/sksl/dsl/DSLVar.cpp
index ff670a5..3471475 100644
--- a/src/sksl/dsl/DSLVar.cpp
+++ b/src/sksl/dsl/DSLVar.cpp
@@ -22,6 +22,24 @@
 
 DSLVar::DSLVar(const char* name)
     : fName(name) {
+#if SK_SUPPORT_GPU && !defined(SKSL_STANDALONE)
+    if (!strcmp(name, "sk_SampleCoord")) {
+        fName = DSLWriter::CurrentEmitArgs()->fSampleCoord;
+        // The actual sk_SampleCoord variable hasn't been created by GrGLSLFPFragmentBuilder yet, so
+        // if we attempt to look it up in the symbol table we'll get null. As we are currently
+        // converting all DSL code into strings rather than nodes, all we really need is a
+        // correctly-named variable with the right type, so we just create a placeholder for it.
+        // TODO(skia/11330): we'll need to fix this when switching over to nodes.
+        fVar = DSLWriter::SymbolTable()->takeOwnershipOfIRNode(std::make_unique<SkSL::Variable>(
+                                  /*offset=*/-1,
+                                  DSLWriter::IRGenerator().fModifiers->addToPool(SkSL::Modifiers()),
+                                  fName,
+                                  DSLWriter::Context().fTypes.fFloat2.get(),
+                                  /*builtin=*/true,
+                                  SkSL::VariableStorage::kGlobal));
+        return;
+    }
+#endif
     const SkSL::Symbol* result = (*DSLWriter::SymbolTable())[fName];
     SkASSERTF(result, "could not find '%s' in symbol table", fName);
     fVar = &result->as<SkSL::Variable>();
diff --git a/src/sksl/dsl/DSLVar.h b/src/sksl/dsl/DSLVar.h
index 72ec317..ed7bbf5 100644
--- a/src/sksl/dsl/DSLVar.h
+++ b/src/sksl/dsl/DSLVar.h
@@ -119,6 +119,8 @@
     const SkSL::Variable* fVar = nullptr;
     const char* fName;
 
+    friend DSLVar sk_SampleCoord();
+
     friend class DSLCore;
     friend class DSLExpression;
     friend class DSLWriter;
diff --git a/src/sksl/dsl/priv/DSLFPs.cpp b/src/sksl/dsl/priv/DSLFPs.cpp
index 018266d..d1c65d1 100644
--- a/src/sksl/dsl/priv/DSLFPs.cpp
+++ b/src/sksl/dsl/priv/DSLFPs.cpp
@@ -25,6 +25,10 @@
     DSLWriter::EndFragmentProcessor();
 }
 
+DSLVar sk_SampleCoord() {
+    return DSLVar("sk_SampleCoord");
+}
+
 DSLExpression SampleChild(int index, DSLExpression coords) {
     std::unique_ptr<SkSL::Expression> coordsExpr = coords.release();
     SkString code = DSLWriter::CurrentProcessor()->invokeChild(index, *DSLWriter::CurrentEmitArgs(),
diff --git a/src/sksl/dsl/priv/DSLFPs.h b/src/sksl/dsl/priv/DSLFPs.h
index 9127045a..5135d07 100644
--- a/src/sksl/dsl/priv/DSLFPs.h
+++ b/src/sksl/dsl/priv/DSLFPs.h
@@ -23,6 +23,8 @@
 
 void EndFragmentProcessor();
 
+DSLVar sk_SampleCoord();
+
 DSLExpression SampleChild(int index, DSLExpression coords = DSLExpression());
 
 GrGLSLUniformHandler::UniformHandle VarUniformHandle(const DSLVar& var);