Home | History | Annotate | Download | only in gl
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef GrGLSL_DEFINED
      9 #define GrGLSL_DEFINED
     10 
     11 #include "gl/GrGLInterface.h"
     12 #include "GrColor.h"
     13 #include "GrTypesPriv.h"
     14 
     15 class GrGLShaderVar;
     16 class SkString;
     17 
     18 // Limited set of GLSL versions we build shaders for. Caller should round
     19 // down the GLSL version to one of these enums.
     20 enum GrGLSLGeneration {
     21     /**
     22      * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
     23      */
     24     k110_GrGLSLGeneration,
     25     /**
     26      * Desktop GLSL 1.30
     27      */
     28     k130_GrGLSLGeneration,
     29     /**
     30      * Desktop GLSL 1.40
     31      */
     32     k140_GrGLSLGeneration,
     33     /**
     34      * Desktop GLSL 1.50
     35      */
     36     k150_GrGLSLGeneration,
     37 };
     38 
     39 enum GrSLConstantVec {
     40     kZeros_GrSLConstantVec,
     41     kOnes_GrSLConstantVec,
     42     kNone_GrSLConstantVec,
     43 };
     44 
     45 namespace {
     46 static inline int GrSLTypeToVecLength(GrSLType type) {
     47     static const int kVecLengths[] = {
     48         0, // kVoid_GrSLType
     49         1, // kFloat_GrSLType
     50         2, // kVec2f_GrSLType
     51         3, // kVec3f_GrSLType
     52         4, // kVec4f_GrSLType
     53         1, // kMat33f_GrSLType
     54         1, // kMat44f_GrSLType
     55         1, // kSampler2D_GrSLType
     56     };
     57     GR_STATIC_ASSERT(kGrSLTypeCount == GR_ARRAY_COUNT(kVecLengths));
     58     return kVecLengths[type];
     59 }
     60 
     61 static inline const char* GrGLSLOnesVecf(int count) {
     62     static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
     63                                      "vec3(1,1,1)", "vec4(1,1,1,1)"};
     64     GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
     65     return kONESVEC[count];
     66 }
     67 
     68 static inline const char* GrGLSLZerosVecf(int count) {
     69     static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
     70                                       "vec3(0,0,0)", "vec4(0,0,0,0)"};
     71     GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
     72     return kZEROSVEC[count];
     73 }
     74 }
     75 
     76 /**
     77  * Gets the most recent GLSL Generation compatible with the OpenGL context.
     78  */
     79 GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
     80                                      const GrGLInterface* gl);
     81 
     82 /**
     83  * Returns a string to include at the beginning of a shader to declare the GLSL
     84  * version.
     85  */
     86 const char* GrGetGLSLVersionDecl(GrGLBinding binding,
     87                                  GrGLSLGeneration v);
     88 
     89 /**
     90  * Depending on the GLSL version being emitted there may be an assumed output
     91  * variable from the fragment shader for the color. Otherwise, the shader must
     92  * declare an output variable for the color. If this function returns true:
     93  *    * Parameter var's name will be set to nameIfDeclared
     94  *    * The variable must be declared in the fragment shader
     95  *    * The variable has to be bound as the color output
     96  *      (using glBindFragDataLocation)
     97  *    If the function returns false:
     98  *    * Parameter var's name will be set to the GLSL built-in color output name.
     99  *    * Do not declare the variable in the shader.
    100  *    * Do not use glBindFragDataLocation to bind the variable
    101  * In either case var is initialized to represent the color output in the
    102  * shader.
    103  */
    104 bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
    105                              const char* nameIfDeclared,
    106                              GrGLShaderVar* var);
    107 /**
    108  * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
    109  */
    110 static const char* GrGLSLTypeString(GrSLType t) {
    111     switch (t) {
    112         case kVoid_GrSLType:
    113             return "void";
    114         case kFloat_GrSLType:
    115             return "float";
    116         case kVec2f_GrSLType:
    117             return "vec2";
    118         case kVec3f_GrSLType:
    119             return "vec3";
    120         case kVec4f_GrSLType:
    121             return "vec4";
    122         case kMat33f_GrSLType:
    123             return "mat3";
    124         case kMat44f_GrSLType:
    125             return "mat4";
    126         case kSampler2D_GrSLType:
    127             return "sampler2D";
    128         default:
    129             GrCrash("Unknown shader var type.");
    130             return ""; // suppress warning
    131     }
    132 }
    133 
    134 /** Return the type enum for a vector of floats of length n (1..4),
    135     e.g. 1 -> "float", 2 -> "vec2", ... */
    136 static inline const char* GrGLSLFloatVectorTypeString(int n) {
    137     return GrGLSLTypeString(GrSLFloatVectorType(n));
    138 }
    139 
    140 /** Return the GLSL swizzle operator for a homogenous component of a vector
    141     with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
    142 const char* GrGLSLVectorHomogCoord(int count);
    143 const char* GrGLSLVectorHomogCoord(GrSLType type);
    144 
    145 /** Return the GLSL swizzle operator for a nonhomogenous components of a vector
    146     with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
    147 const char* GrGLSLVectorNonhomogCoords(int count);
    148 const char* GrGLSLVectorNonhomogCoords(GrSLType type);
    149 
    150 /**
    151   * Produces a string that is the result of modulating two inputs. The inputs must be vecN or
    152   * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
    153   * Either can be NULL or "" in which case the default params control whether a vector of ones or
    154   * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
    155   * function determines that the result is a zeros or ones vec then any expression represented by
    156   * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
    157   * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
    158   * by passing true for omitIfConstVec.
    159   */
    160 template <int N>
    161 GrSLConstantVec GrGLSLModulatef(SkString* outAppend,
    162                                 const char* in0,
    163                                 const char* in1,
    164                                 GrSLConstantVec default0 = kOnes_GrSLConstantVec,
    165                                 GrSLConstantVec default1 = kOnes_GrSLConstantVec,
    166                                 bool omitIfConstVec = false);
    167 
    168 /**
    169  * Produces a string that is the result of adding two inputs. The inputs must be vecN or
    170  * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
    171  * Either can be NULL or "" in which case the default params control whether a vector of ones or
    172  * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
    173  * function determines that the result is a zeros or ones vec then any expression represented by
    174  * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
    175  * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
    176  * by passing true for omitIfConstVec.
    177  */
    178 template <int N>
    179 GrSLConstantVec GrGLSLAddf(SkString* outAppend,
    180                            const char* in0,
    181                            const char* in1,
    182                            GrSLConstantVec default0 = kZeros_GrSLConstantVec,
    183                            GrSLConstantVec default1 = kZeros_GrSLConstantVec,
    184                            bool omitIfConstVec = false);
    185 
    186 /**
    187  * Produces a string that is the result of subtracting two inputs. The inputs must be vecN or
    188  * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
    189  * Either can be NULL or "" in which case the default params control whether a vector of ones or
    190  * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
    191  * function determines that the result is a zeros or ones vec then any expression represented by
    192  * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
    193  * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
    194  * by passing true for omitIfConstVec.
    195  */
    196 template <int N>
    197 GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
    198                                 const char* in0,
    199                                 const char* in1,
    200                                 GrSLConstantVec default0 = kZeros_GrSLConstantVec,
    201                                 GrSLConstantVec default1 = kZeros_GrSLConstantVec,
    202                                 bool omitIfConstVec = false);
    203 
    204 /**
    205  * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
    206  * mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an
    207  * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs.
    208  * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert
    209  * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is
    210  * kZeros, otherwise kNone.
    211  */
    212 GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
    213                                  int tabCnt,
    214                                  const char* vec4VarName,
    215                                  const char* mulFactor,
    216                                  GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
    217 
    218 /**
    219  * Given an expression that evaluates to a GLSL vec4, extract a component. If expr is NULL or ""
    220  * the value of defaultExpr is used. It is an error to pass an empty expr and have set defaultExpr
    221  * to kNone. The return value indicates whether the value is known to be 0 or 1. If omitIfConst is
    222  * set then nothing is appended when the return is not kNone.
    223  */
    224 GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend,
    225                                      const char* expr,
    226                                      GrColorComponentFlags component,
    227                                      GrSLConstantVec defaultExpr = kNone_GrSLConstantVec,
    228                                      bool omitIfConst = false);
    229 
    230 #include "GrGLSL_impl.h"
    231 
    232 #endif
    233