VariableVisibility -> ShaderType

Review URL: http://codereview.appspot.com/6344103/


git-svn-id: http://skia.googlecode.com/svn/trunk@4534 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/effects/GrConvolutionEffect.cpp b/src/gpu/effects/GrConvolutionEffect.cpp
index 349c867..42dc2b4 100644
--- a/src/gpu/effects/GrConvolutionEffect.cpp
+++ b/src/gpu/effects/GrConvolutionEffect.cpp
@@ -60,12 +60,11 @@
 
 void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* state,
                                            int stage) {
-    fImageIncrementVar = &state->addUniform(
-        GrGLShaderBuilder::kBoth_VariableLifetime,
-        kVec2f_GrSLType, "uImageIncrement", stage);
-    fKernelVar = &state->addUniform(
-        GrGLShaderBuilder::kFragment_VariableLifetime,
-        kFloat_GrSLType, "uKernel", stage, this->width());
+    fImageIncrementVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
+                                            GrGLShaderBuilder::kVertex_ShaderType,
+                                            kVec2f_GrSLType, "uImageIncrement", stage);
+    fKernelVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                                    kFloat_GrSLType, "uKernel", stage, this->width());
 
     fImageIncrementLocation = kUseUniform;
     fKernelLocation = kUseUniform;
diff --git a/src/gpu/effects/GrGradientEffects.cpp b/src/gpu/effects/GrGradientEffects.cpp
index 9d75965..6045956 100644
--- a/src/gpu/effects/GrGradientEffects.cpp
+++ b/src/gpu/effects/GrGradientEffects.cpp
@@ -142,12 +142,10 @@
     // 2 copies of uniform array, 1 for each of vertex & fragment shader,
     // to work around Xoom bug. Doesn't seem to cause performance decrease
     // in test apps, but need to keep an eye on it.
-    fVSParamVar = &state->addUniform(
-        GrGLShaderBuilder::kVertex_VariableLifetime,
-        kFloat_GrSLType, "uRadial2VSParams", stage, 6);
-    fFSParamVar = &state->addUniform(
-        GrGLShaderBuilder::kFragment_VariableLifetime,
-        kFloat_GrSLType, "uRadial2FSParams", stage, 6);
+    fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
+                                     kFloat_GrSLType, "uRadial2VSParams", stage, 6);
+    fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                                     kFloat_GrSLType, "uRadial2FSParams", stage, 6);
 
     fVSParamLocation = GrGLProgramStage::kUseUniform;
     fFSParamLocation = GrGLProgramStage::kUseUniform;
@@ -399,12 +397,10 @@
     // 2 copies of uniform array, 1 for each of vertex & fragment shader,
     // to work around Xoom bug. Doesn't seem to cause performance decrease
     // in test apps, but need to keep an eye on it.
-    fVSParamVar = &state->addUniform(
-        GrGLShaderBuilder::kVertex_VariableLifetime,
-        kFloat_GrSLType, "uConical2VSParams", stage, 6);
-    fFSParamVar = &state->addUniform(
-        GrGLShaderBuilder::kFragment_VariableLifetime,
-        kFloat_GrSLType, "uConical2FSParams", stage, 6);
+    fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
+                                     kFloat_GrSLType, "uConical2VSParams", stage, 6);
+    fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                                     kFloat_GrSLType, "uConical2FSParams", stage, 6);
 
     fVSParamLocation = GrGLProgramStage::kUseUniform;
     fFSParamLocation = GrGLProgramStage::kUseUniform;
diff --git a/src/gpu/effects/GrMorphologyEffect.cpp b/src/gpu/effects/GrMorphologyEffect.cpp
index 40c1300..43e8702 100644
--- a/src/gpu/effects/GrMorphologyEffect.cpp
+++ b/src/gpu/effects/GrMorphologyEffect.cpp
@@ -57,9 +57,9 @@
 }
 
 void GrGLMorphologyEffect::setupVariables(GrGLShaderBuilder* state, int stage) {
-    fImageIncrementVar = &state->addUniform(
-        GrGLShaderBuilder::kBoth_VariableLifetime,
-        kVec2f_GrSLType, "uImageIncrement", stage);
+    fImageIncrementVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
+                                            GrGLShaderBuilder::kVertex_ShaderType,
+                                            kVec2f_GrSLType, "uImageIncrement", stage);
 }
 
 void GrGLMorphologyEffect::emitVS(GrGLShaderBuilder* state,
diff --git a/src/gpu/gl/GrGLProgram.cpp b/src/gpu/gl/GrGLProgram.cpp
index 2105cda..d673aca 100644
--- a/src/gpu/gl/GrGLProgram.cpp
+++ b/src/gpu/gl/GrGLProgram.cpp
@@ -362,7 +362,7 @@
             *inColor = fsName;
             } break;
         case GrGLProgram::ProgramDesc::kUniform_ColorInput:
-            segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
+            segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
                                  kVec4f_GrSLType, COL_UNI_NAME);
             programData->fUniLocations.fColorUni = kUseUniform;
             *inColor = COL_UNI_NAME;
@@ -398,7 +398,7 @@
 void genUniformCoverage(GrGLShaderBuilder* segments,
                         GrGLProgram::CachedData* programData,
                         SkString* inOutCoverage) {
-    segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
+    segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
                          kVec4f_GrSLType, COV_UNI_NAME);
     programData->fUniLocations.fCoverageUni = kUseUniform;
     if (inOutCoverage->size()) {
@@ -529,7 +529,7 @@
         segments.fFSOutputs.push_back(colorOutput);
     }
 
-    segments.addUniform(GrGLShaderBuilder::kVertex_VariableLifetime,
+    segments.addUniform(GrGLShaderBuilder::kVertex_ShaderType,
                         kMat33f_GrSLType, VIEW_MATRIX_NAME);
     programData->fUniLocations.fViewMatrixUni = kUseUniform;
 
@@ -635,7 +635,7 @@
         }
     }
     if (needColorFilterUniform) {
-        segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
+        segments.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
                             kVec4f_GrSLType, COL_FILTER_UNI_NAME);
         programData->fUniLocations.fColorFilterUni = kUseUniform;
     }
@@ -655,9 +655,9 @@
         inColor = "filteredColor";
     }
     if (applyColorMatrix) {
-        segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
+        segments.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
                             kMat44f_GrSLType, COL_MATRIX_UNI_NAME);
-        segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
+        segments.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
                             kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME);
         programData->fUniLocations.fColorMatrixUni = kUseUniform;
         programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
@@ -1170,9 +1170,8 @@
     programData->fColorFilterColor = GrColor_ILLEGAL;
 }
 
-//============================================================================
+///////////////////////////////////////////////////////////////////////////////
 // Stage code generation
-//============================================================================
 
 void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
                                int stageNum,
@@ -1190,17 +1189,16 @@
 
     /// Vertex Shader Stuff
 
-    // decide whether we need a matrix to transform texture coords
-    // and whether the varying needs a perspective coord.
+    // decide whether we need a matrix to transform texture coords and whether the varying needs a
+    // perspective coord.
     const char* matName = NULL;
     if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
         segments->fVaryingDims = segments->fCoordDims;
     } else {
         SkString texMatName;
         tex_matrix_name(stageNum, &texMatName);
-        const GrGLShaderVar* mat = &segments->addUniform(
-            GrGLShaderBuilder::kVertex_VariableLifetime, kMat33f_GrSLType,
-            texMatName.c_str());
+        const GrGLShaderVar* mat = &segments->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
+                                                         kMat33f_GrSLType, texMatName.c_str());
         // Can't use texMatName.c_str() because it's on the stack!
         matName = mat->getName().c_str();
         locations->fTextureMatrixUni = kUseUniform;
@@ -1220,9 +1218,8 @@
 
     SkString samplerName;
     sampler_name(stageNum, &samplerName);
-    // const GrGLShaderVar* sampler = &
-        segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
-        kSampler2D_GrSLType, samplerName.c_str());
+    segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                         kSampler2D_GrSLType, samplerName.c_str());
     locations->fSamplerUni = kUseUniform;
 
     const char *varyingVSName, *varyingFSName;
@@ -1242,8 +1239,6 @@
                                   vector_all_coords(segments->fVaryingDims));
     }
 
-    // GrGLShaderVar* kernel = NULL;
-    // const char* imageIncrementName = NULL;
     if (NULL != customStage) {
         segments->fVSCode.appendf("\t{ // stage %d %s\n",
                                   stageNum, customStage->name());
@@ -1275,10 +1270,8 @@
     if (desc.fOptFlags & StageDesc::kCustomTextureDomain_OptFlagBit) {
         SkString texDomainName;
         tex_domain_name(stageNum, &texDomainName);
-        // const GrGLShaderVar* texDomain = &
-            segments->addUniform(
-                GrGLShaderBuilder::kFragment_VariableLifetime,
-                kVec4f_GrSLType, texDomainName.c_str());
+        segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
+                             kVec4f_GrSLType, texDomainName.c_str());
         SkString coordVar("clampCoord");
         segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
                                   float_vector_type_str(segments->fCoordDims),
diff --git a/src/gpu/gl/GrGLShaderBuilder.cpp b/src/gpu/gl/GrGLShaderBuilder.cpp
index 4074c4a..220aed7 100644
--- a/src/gpu/gl/GrGLShaderBuilder.cpp
+++ b/src/gpu/gl/GrGLShaderBuilder.cpp
@@ -8,15 +8,11 @@
 #include "gl/GrGLShaderBuilder.h"
 #include "gl/GrGLProgram.h"
 
-namespace {
-
 // number of each input/output type in a single allocation block
-static const int sVarsPerBlock = 8;
+static const int kVarsPerBlock = 8;
 
 // except FS outputs where we expect 2 at most.
-static const int sMaxFSOutputs = 2;
-
-}
+static const int kMaxFSOutputs = 2;
 
 // Architectural assumption: always 2-d input coords.
 // Likely to become non-constant and non-static, perhaps even
@@ -24,14 +20,14 @@
 //const int GrGLShaderBuilder::fCoordDims = 2;
 
 GrGLShaderBuilder::GrGLShaderBuilder()
-    : fVSUnis(sVarsPerBlock)
-    , fVSAttrs(sVarsPerBlock)
-    , fVSOutputs(sVarsPerBlock)
-    , fGSInputs(sVarsPerBlock)
-    , fGSOutputs(sVarsPerBlock)
-    , fFSInputs(sVarsPerBlock)
-    , fFSUnis(sVarsPerBlock)
-    , fFSOutputs(sMaxFSOutputs)
+    : fVSUnis(kVarsPerBlock)
+    , fVSAttrs(kVarsPerBlock)
+    , fVSOutputs(kVarsPerBlock)
+    , fGSInputs(kVarsPerBlock)
+    , fGSOutputs(kVarsPerBlock)
+    , fFSInputs(kVarsPerBlock)
+    , fFSUnis(kVarsPerBlock)
+    , fFSOutputs(kMaxFSOutputs)
     , fUsesGS(false)
     , fVaryingDims(0)
     , fComplexCoord(false) {
@@ -116,18 +112,21 @@
     fFSCode.appendf("%s%s;\n", fSwizzle.c_str(), fModulate.c_str());
 }
 
-const GrGLShaderVar& GrGLShaderBuilder::addUniform(VariableLifetime lifetime,
-                                             GrSLType type,
-                                             const char* name,
-                                             int stageNum,
-                                             int count) {
+const GrGLShaderVar& GrGLShaderBuilder::addUniform(uint32_t visibility,
+                                                   GrSLType type,
+                                                   const char* name,
+                                                   int stageNum,
+                                                   int count) {
     GrAssert(name && strlen(name));
+    static const uint32_t kVisibilityMask = kVertex_ShaderType | kFragment_ShaderType;
+    GrAssert(0 == (~kVisibilityMask & visibility));
+    GrAssert(0 != visibility);
 
     GrGLShaderVar* var = NULL;
-    if (kVertex_VariableLifetime & lifetime) {
+    if (kVertex_ShaderType & visibility) {
         var = &fVSUnis.push_back();
     } else {
-        GrAssert(kFragment_VariableLifetime & lifetime);
+        GrAssert(kFragment_ShaderType & visibility);
         var = &fFSUnis.push_back();
     }
     var->setType(type);
@@ -138,8 +137,7 @@
     }
     var->setArrayCount(count);
 
-    if ((kVertex_VariableLifetime |
-        kFragment_VariableLifetime) == lifetime) {
+    if ((kVertex_ShaderType | kFragment_ShaderType) == visibility) {
         fFSUnis.push_back(*var);
         // If it's shared between VS and FS, VS must override
         // default highp and specify mediump.
diff --git a/src/gpu/gl/GrGLShaderBuilder.h b/src/gpu/gl/GrGLShaderBuilder.h
index 002a20a..bf308f3 100644
--- a/src/gpu/gl/GrGLShaderBuilder.h
+++ b/src/gpu/gl/GrGLShaderBuilder.h
@@ -15,8 +15,8 @@
 typedef GrTAllocator<GrGLShaderVar> VarArray;
 
 /**
-  Containts all the incremental state of a shader as it is being built,
-  as well as helpers to manipulate that state.
+  Contains all the incremental state of a shader as it is being built,as well as helpers to
+  manipulate that state.
   TODO: migrate CompileShaders() here?
 */
 
@@ -24,6 +24,12 @@
 
 public:
 
+    enum ShaderType {
+        kVertex_ShaderType   = 0x1,
+        kGeometry_ShaderType = 0x2,
+        kFragment_ShaderType = 0x4,
+    };
+
     GrGLShaderBuilder();
 
     void computeSwizzle(uint32_t configFlags);
@@ -36,62 +42,52 @@
         kExplicitDivide_SamplerMode  // must do an explicit divide
     };
 
-    /** Determines whether we should use texture2D() or texture2Dproj(),
-        and if an explicit divide is required for the sample coordinates,
-        creates the new variable and emits the code to initialize it. */
+    /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide
+        is required for the sample coordinates, creates the new variable and emits the code to
+        initialize it. */
     void setupTextureAccess(SamplerMode samplerMode, int stageNum);
 
-    /** texture2D(samplerName, coordName), with projection
-        if necessary; if coordName is not specified,
-        uses fSampleCoords. */
+    /** texture2D(samplerName, coordName), with projection if necessary; if coordName is not
+        specified, uses fSampleCoords. */
     void emitTextureLookup(const char* samplerName,
                            const char* coordName = NULL);
 
-    /** sets outColor to results of texture lookup, with
-        swizzle, and/or modulate as necessary */
+    /** sets outColor to results of texture lookup, with swizzle, and/or modulate as necessary */
     void emitDefaultFetch(const char* outColor,
                           const char* samplerName);
 
-    /* TODO: can't arbitrarily OR together enum components, so
-       VariableLifetime will need to be reworked if we add
-       Geometry shaders. */
-    enum VariableLifetime {
-        kVertex_VariableLifetime = 1,
-        kFragment_VariableLifetime = 2,
-        kBoth_VariableLifetime = 3
-    };
 
-    /** Add a uniform variable to the current program, accessed
-       in vertex, fragment, or both stages. If stageNum is
-       specified, it is appended to the name to guarantee uniqueness;
-       if count is specified, the uniform is an array.
+    /** Add a uniform variable to the current program, that has visibilty in one or more shaders.
+        If stageNum is specified, it is appended to the name to guarantee uniqueness; if count is
+        specified, the uniform is an array. visibility is a bitfield of ShaderType values indicating
+        from which shaders the uniform should be accessible. At least one bit must be set. Geometry
+        shader uniforms are not supported at this time.
     */
-    const GrGLShaderVar& addUniform(VariableLifetime lifetime,
-        GrSLType type,
-        const char* name,
-        int stageNum = -1,
-        int count = GrGLShaderVar::kNonArray);
+    const GrGLShaderVar& addUniform(uint32_t visibility,
+                                    GrSLType type,
+                                    const char* name,
+                                    int stageNum = -1,
+                                    int count = GrGLShaderVar::kNonArray);
 
-    /** Add a varying variable to the current program to pass
-        values between vertex and fragment shaders.
-        If the last two parameters are non-NULL, they are filled
-        in with the name generated. */
+    /** Add a varying variable to the current program to pass values between vertex and fragment
+        shaders. If the last two parameters are non-NULL, they are filled in with the name
+        generated. */
     void addVarying(GrSLType type,
                     const char* name,
                     const char** vsOutName = NULL,
                     const char** fsInName = NULL);
 
-    /** Add a varying variable to the current program to pass
-        values between vertex and fragment shaders;
-        stageNum is appended to the name to guarantee uniqueness.
-        If the last two parameters are non-NULL, they are filled
-        in with the name generated. */
+    /** Add a varying variable to the current program to pass values between vertex and fragment
+        shaders; stageNum is appended to the name to guarantee uniqueness. If the last two
+        parameters are non-NULL, they are filled in with the name generated. */
     void addVarying(GrSLType type,
                     const char* name,
                     int stageNum,
                     const char** vsOutName = NULL,
                     const char** fsInName = NULL);
-    
+
+    // TODO: Everything below here private.
+
     SkString    fHeader; // VS+FS, GLSL version, etc
     VarArray    fVSUnis;
     VarArray    fVSAttrs;
@@ -108,8 +104,7 @@
     SkString    fFSCode;
     bool        fUsesGS;
 
-    /// Per-stage settings - only valid while we're inside
-    /// GrGLProgram::genStageCode().
+    /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
     //@{
 
     int              fVaryingDims;