Restore support for multisample locations


Bug: skia:
Change-Id: I971455867e54d431cc1094fca041f773f78748ee
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/196218
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp
index 361221a..891e973 100644
--- a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp
@@ -67,17 +67,8 @@
 }
 
 GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
-    : GrGLSLFragmentBuilder(program)
-    , fSetupFragPosition(false)
-    , fHasCustomColorOutput(false)
-    , fCustomColorOutputIndex(-1)
-    , fHasSecondaryOutput(false)
-    , fHasInitializedSampleMask(false)
-    , fForceHighPrecision(false) {
+        : GrGLSLFragmentBuilder(program) {
     fSubstageIndices.push_back(0);
-#ifdef SK_DEBUG
-    fHasReadDstColor = false;
-#endif
 }
 
 SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) {
@@ -93,6 +84,13 @@
     return coords2D;
 }
 
+const char* GrGLSLFragmentShaderBuilder::sampleOffsets() {
+    SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures());
+    SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations);
+    SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations);
+    return "_sampleOffsets";
+}
+
 void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage(const char* mask, Scope scope) {
     const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
     if (!shaderCaps.sampleVariablesSupport()) {
@@ -116,7 +114,7 @@
 }
 
 const char* GrGLSLFragmentShaderBuilder::dstColor() {
-    SkDEBUGCODE(fHasReadDstColor = true;)
+    SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;)
 
     const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
     if (shaderCaps->fbFetchSupport()) {
@@ -207,7 +205,7 @@
 }
 
 GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
-    SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
+    SkASSERT(fProgramBuilder->header().hasSurfaceOriginKey());
     return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey-1);
 
     GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin);
@@ -215,6 +213,24 @@
 }
 
 void GrGLSLFragmentShaderBuilder::onFinalize() {
+    SkASSERT(fProgramBuilder->header().processorFeatures()
+                     == fUsedProcessorFeaturesAllStages_DebugOnly);
+
+    if (CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures()) {
+        this->definitions().append("const float2 _sampleOffsets[] = float2[](");
+        const GrPipeline& pipeline = fProgramBuilder->pipeline();
+        const SkTArray<SkPoint>& sampleLocations =
+                fProgramBuilder->renderTarget()->renderTargetPriv().getSampleLocations(pipeline);
+        for (int i = 0; i < sampleLocations.count(); ++i) {
+            SkPoint offset = sampleLocations[i] - SkPoint::Make(.5f, .5f);
+            if (kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
+                offset.fY = -offset.fY;
+            }
+            this->definitions().appendf("float2(%f, %f)", offset.x(), offset.y());
+            this->definitions().append((i + 1 != sampleLocations.count()) ? ", " : ");");
+        }
+    }
+
     fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
 }