Multi-string shaders

BUG=skia:

Committed: https://skia.googlesource.com/skia/+/1c3c2d83364ee228e0751df0e1b9c161c0ba8c1e

Review URL: https://codereview.chromium.org/929503002
diff --git a/src/gpu/gl/builders/GrGLShaderBuilder.cpp b/src/gpu/gl/builders/GrGLShaderBuilder.cpp
index 4887225..c1bc959 100644
--- a/src/gpu/gl/builders/GrGLShaderBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLShaderBuilder.cpp
@@ -7,7 +7,7 @@
 
 #include "GrGLShaderBuilder.h"
 #include "GrGLProgramBuilder.h"
-#include "GrGLProgramBuilder.h"
+#include "GrGLShaderStringBuilder.h"
 #include "../GrGLGpu.h"
 #include "../GrGLShaderVar.h"
 
@@ -59,7 +59,17 @@
     : fProgramBuilder(program)
     , fInputs(GrGLProgramBuilder::kVarsPerBlock)
     , fOutputs(GrGLProgramBuilder::kVarsPerBlock)
-    , fFeaturesAddedMask(0) {
+    , fFeaturesAddedMask(0)
+    , fCodeIndex(kCode)
+    , fFinalized(false) {
+    // We push back some dummy pointers which will later become our header
+    for (int i = 0; i <= kCode; i++) {
+        fShaderStrings.push_back();
+        fCompilerStrings.push_back(NULL);
+        fCompilerStringLengths.push_back(0);
+    }
+
+    this->main() = "void main() {";
 }
 
 void GrGLShaderBuilder::declAppend(const GrGLShaderVar& var) {
@@ -74,20 +84,20 @@
                                      const GrGLShaderVar* args,
                                      const char* body,
                                      SkString* outName) {
-    fFunctions.append(GrGLSLTypeString(returnType));
+    this->functions().append(GrGLSLTypeString(returnType));
     fProgramBuilder->nameVariable(outName, '\0', name);
-    fFunctions.appendf(" %s", outName->c_str());
-    fFunctions.append("(");
+    this->functions().appendf(" %s", outName->c_str());
+    this->functions().append("(");
     const GrGLContextInfo& ctxInfo = fProgramBuilder->gpu()->ctxInfo();
     for (int i = 0; i < argCnt; ++i) {
-        args[i].appendDecl(ctxInfo, &fFunctions);
+        args[i].appendDecl(ctxInfo, &this->functions());
         if (i < argCnt - 1) {
-            fFunctions.append(", ");
+            this->functions().append(", ");
         }
     }
-    fFunctions.append(") {\n");
-    fFunctions.append(body);
-    fFunctions.append("}\n\n");
+    this->functions().append(") {\n");
+    this->functions().append(body);
+    this->functions().append("}\n\n");
 }
 
 void GrGLShaderBuilder::appendTextureLookup(SkString* out,
@@ -106,7 +116,7 @@
 void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler,
                                             const char* coordName,
                                             GrSLType varyingType) {
-    this->appendTextureLookup(&fCode, sampler, coordName, varyingType);
+    this->appendTextureLookup(&this->code(), sampler, coordName, varyingType);
 }
 
 void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
@@ -137,8 +147,8 @@
 
 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
     if (!(featureBit & fFeaturesAddedMask)) {
-        fExtensions.appendf("#extension %s: require\n", extensionName);
-            fFeaturesAddedMask |= featureBit;
+        this->extensions().appendf("#extension %s: require\n", extensionName);
+        fFeaturesAddedMask |= featureBit;
     }
 }
 
@@ -153,7 +163,7 @@
                                             const char* coordName,
                                             uint32_t configComponentMask,
                                             const char* swizzle) {
-    append_texture_lookup(&fCode,
+    append_texture_lookup(&this->code(),
                           fProgramBuilder->gpu(),
                           samplerName,
                           coordName,
@@ -161,3 +171,34 @@
                           swizzle,
                           kVec2f_GrSLType);
 }
+
+bool
+GrGLShaderBuilder::finalize(GrGLuint programId, GrGLenum type, SkTDArray<GrGLuint>* shaderIds) {
+    SkASSERT(!fFinalized);
+    // append the 'footer' to code
+    this->code().append("}");
+
+    for (int i = 0; i <= fCodeIndex; i++) {
+        fCompilerStrings[i] = fShaderStrings[i].c_str();
+        fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
+    }
+
+    GrGLGpu* gpu = fProgramBuilder->gpu();
+    GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
+                                                   programId,
+                                                   type,
+                                                   fCompilerStrings.begin(),
+                                                   fCompilerStringLengths.begin(),
+                                                   fCompilerStrings.count(),
+                                                   gpu->stats());
+
+    fFinalized = true;
+
+    if (!shaderId) {
+        return false;
+    }
+
+    *shaderIds->append() = shaderId;
+
+    return true;
+}