GrGLSL.h revision d686ac77c2c485c4a3302eda9c1de597a6f8c568
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
13class GrGLShaderVar;
14class SkString;
15
16// Limited set of GLSL versions we build shaders for. Caller should round
17// down the GLSL version to one of these enums.
18enum GrGLSLGeneration {
19    /**
20     * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
21     */
22    k110_GrGLSLGeneration,
23    /**
24     * Desktop GLSL 1.30
25     */
26    k130_GrGLSLGeneration,
27    /**
28     * Desktop GLSL 1.40
29     */
30    k140_GrGLSLGeneration,
31    /**
32     * Desktop GLSL 1.50
33     */
34    k150_GrGLSLGeneration,
35};
36
37/**
38 * Types of shader-language-specific boxed variables we can create.
39 * (Currently only GrGLShaderVars, but should be applicable to other shader
40 * languages.)
41 */
42enum GrSLType {
43    kVoid_GrSLType,
44    kFloat_GrSLType,
45    kVec2f_GrSLType,
46    kVec3f_GrSLType,
47    kVec4f_GrSLType,
48    kMat33f_GrSLType,
49    kMat44f_GrSLType,
50    kSampler2D_GrSLType
51};
52
53enum GrSLConstantVec {
54    kZeros_GrSLConstantVec,
55    kOnes_GrSLConstantVec,
56    kNone_GrSLConstantVec,
57};
58
59namespace {
60static inline int GrSLTypeToVecLength(GrSLType type) {
61    static const int kVecLengths[] = {
62        0, // kVoid_GrSLType
63        1, // kFloat_GrSLType
64        2, // kVec2f_GrSLType
65        3, // kVec3f_GrSLType
66        4, // kVec4f_GrSLType
67        1, // kMat33f_GrSLType
68        1, // kMat44f_GrSLType
69        1, // kSampler2D_GrSLType
70    };
71    GrAssert((size_t) type < GR_ARRAY_COUNT(kVecLengths));
72    return kVecLengths[type];
73}
74
75static inline const char* GrGLSLOnesVecf(int count) {
76    static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
77                                     "vec3(1,1,1)", "vec4(1,1,1,1)"};
78    GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
79    return kONESVEC[count];
80}
81
82static inline const char* GrGLSLZerosVecf(int count) {
83    static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
84                                      "vec3(0,0,0)", "vec4(0,0,0,0)"};
85    GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
86    return kZEROSVEC[count];
87}
88}
89
90/**
91 * Gets the most recent GLSL Generation compatible with the OpenGL context.
92 */
93GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
94                                     const GrGLInterface* gl);
95
96/**
97 * Returns a string to include at the beginning of a shader to declare the GLSL
98 * version.
99 */
100const char* GrGetGLSLVersionDecl(GrGLBinding binding,
101                                 GrGLSLGeneration v);
102
103/**
104 * Depending on the GLSL version being emitted there may be an assumed output
105 * variable from the fragment shader for the color. Otherwise, the shader must
106 * declare an output variable for the color. If this function returns true:
107 *    * Parameter var's name will be set to nameIfDeclared
108 *    * The variable must be declared in the fragment shader
109 *    * The variable has to be bound as the color output
110 *      (using glBindFragDataLocation)
111 *    If the function returns false:
112 *    * Parameter var's name will be set to the GLSL built-in color output name.
113 *    * Do not declare the variable in the shader.
114 *    * Do not use glBindFragDataLocation to bind the variable
115 * In either case var is initialized to represent the color output in the
116 * shader.
117 */
118bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
119                             const char* nameIfDeclared,
120                             GrGLShaderVar* var);
121
122/** Convert a count of 1..n floats into the corresponding type enum,
123    e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
124GrSLType GrSLFloatVectorType(int count);
125
126/** Return the GLSL swizzle operator for a homogenous component of a vector
127    with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
128const char* GrGLSLVectorHomogCoord(int count);
129const char* GrGLSLVectorHomogCoord(GrSLType type);
130
131/** Return the GLSL swizzle operator for a nonhomogenous components of a vector
132    with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
133const char* GrGLSLVectorNonhomogCoords(int count);
134const char* GrGLSLVectorNonhomogCoords(GrSLType type);
135
136/**
137  * Produces a string that is the result of modulating two inputs. The inputs must be vec4 or
138  * float. The result is always a vec4. The inputs may be expressions, not just identifier names.
139  * Either can be NULL or "" in which case the default params control whether vec4(1,1,1,1) or
140  * vec4(0,0,0,0) is assumed. It is an error to pass kNone for default<i> if in<i> is NULL or "".
141  * Note that when if function determines that the result is a zeros or ones vec then any expression
142  * represented by in0 or in1 will not be emitted. The return value indicates whether a zeros, ones
143  * or neither was appended.
144  */
145GrSLConstantVec GrGLSLModulate4f(SkString* outAppend,
146                                 const char* in0,
147                                 const char* in1,
148                                 GrSLConstantVec default0 = kOnes_GrSLConstantVec,
149                                 GrSLConstantVec default1 = kOnes_GrSLConstantVec);
150
151/**
152 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
153 * mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an
154 * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs.
155 * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert
156 * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is
157 * kZeros, otherwise kNone.
158 */
159GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
160                                 int tabCnt,
161                                 const char* vec4VarName,
162                                 const char* mulFactor,
163                                 GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
164
165/**
166  * Produces a string that is the result of adding two inputs. The inputs must be vec4 or float.
167  * The result is always a vec4. The inputs may be expressions, not just identifier names. Either
168  * can be NULL or "" in which case if the default is kZeros then vec4(0,0,0,0) is assumed. It is an
169  * error to pass kOnes for either default or to pass kNone for default<i> if in<i> is NULL or "".
170  * Note that if the function determines that the result is a zeros vec any expression represented
171  * by in0 or in1 will not be emitted. The return value indicates whether a zeros vec was appended
172  * or not.
173  */
174GrSLConstantVec GrGLSLAdd4f(SkString* outAppend,
175                            const char* in0,
176                            const char* in1,
177                            GrSLConstantVec default0 = kZeros_GrSLConstantVec,
178                            GrSLConstantVec default1 = kZeros_GrSLConstantVec);
179
180#endif
181