blob: cbff27338279d163a9654cd83b71abb6b6743e42 [file] [log] [blame]
tomhudson@google.com086e5352011-12-08 14:44:10 +00001/*
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
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000011#include "gl/GrGLInterface.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000012
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000013class GrGLShaderVar;
bsalomon@google.com4af0af62012-08-29 12:59:57 +000014class SkString;
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000015
tomhudson@google.com086e5352011-12-08 14:44:10 +000016// 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 lang (based on desktop GLSL 1.20)
21 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000022 k110_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000023 /**
24 * Desktop GLSL 1.30
25 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000026 k130_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000027 /**
28 * Dekstop GLSL 1.50
29 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000030 k150_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000031};
32
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000033/**
tomhudson@google.com168e6342012-04-18 17:49:20 +000034 * Types of shader-language-specific boxed variables we can create.
35 * (Currently only GrGLShaderVars, but should be applicable to other shader
36 * langauges.)
37 */
38enum GrSLType {
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000039 kVoid_GrSLType,
tomhudson@google.com168e6342012-04-18 17:49:20 +000040 kFloat_GrSLType,
41 kVec2f_GrSLType,
42 kVec3f_GrSLType,
43 kVec4f_GrSLType,
44 kMat33f_GrSLType,
45 kMat44f_GrSLType,
46 kSampler2D_GrSLType
47};
48
bsalomon@google.com4af0af62012-08-29 12:59:57 +000049enum GrSLConstantVec {
50 kZeros_GrSLConstantVec,
51 kOnes_GrSLConstantVec,
52 kNone_GrSLConstantVec,
53};
54
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000055namespace {
56inline int GrSLTypeToVecLength(GrSLType type) {
57 static const int kVecLengths[] = {
58 0, // kVoid_GrSLType
59 1, // kFloat_GrSLType
60 2, // kVec2f_GrSLType
61 3, // kVec3f_GrSLType
62 4, // kVec4f_GrSLType
63 1, // kMat33f_GrSLType
64 1, // kMat44f_GrSLType
65 1, // kSampler2D_GrSLType
66 };
67 GrAssert((size_t) type < GR_ARRAY_COUNT(kVecLengths));
68 return kVecLengths[type];
69}
bsalomon@google.com4af0af62012-08-29 12:59:57 +000070
71const char* GrGLSLOnesVecf(int count) {
72 static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
73 "vec3(1,1,1)", "vec4(1,1,1,1)"};
74 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
75 return kONESVEC[count];
76}
77
78const char* GrGLSLZerosVecf(int count) {
79 static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
80 "vec3(0,0,0)", "vec4(0,0,0,0)"};
81 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
82 return kZEROSVEC[count];
83}
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000084}
85
tomhudson@google.com168e6342012-04-18 17:49:20 +000086/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000087 * Gets the most recent GLSL Generation compatible with the OpenGL context.
88 */
89GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
90 const GrGLInterface* gl);
91
92/**
93 * Returns a string to include at the begining of a shader to declare the GLSL
94 * version.
95 */
96const char* GrGetGLSLVersionDecl(GrGLBinding binding,
97 GrGLSLGeneration v);
98
99/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000100 * Depending on the GLSL version being emitted there may be an assumed output
101 * variable from the fragment shader for the color. Otherwise, the shader must
102 * declare an output variable for the color. If this function returns true:
103 * * Parameter var's name will be set to nameIfDeclared
104 * * The variable must be declared in the fragment shader
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105 * * The variable has to be bound as the color output
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000106 * (using glBindFragDataLocation)
107 * If the function returns false:
108 * * Parameter var's name will be set to the GLSL built-in color output name.
109 * * Do not declare the variable in the shader.
110 * * Do not use glBindFragDataLocation to bind the variable
111 * In either case var is initialized to represent the color output in the
112 * shader.
113 */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000114bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000115 const char* nameIfDeclared,
116 GrGLShaderVar* var);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000117
tomhudson@google.com168e6342012-04-18 17:49:20 +0000118/** Convert a count of 1..n floats into the corresponding type enum,
119 e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
120GrSLType GrSLFloatVectorType(int count);
121
122/** Return the GLSL swizzle operator for a homogenous component of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000123 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000124const char* GrGLSLVectorHomogCoord(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000125const char* GrGLSLVectorHomogCoord(GrSLType type);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000126
127/** Return the GLSL swizzle operator for a nonhomogenous components of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000128 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000129const char* GrGLSLVectorNonhomogCoords(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000130const char* GrGLSLVectorNonhomogCoords(GrSLType type);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000131
132/**
133 * Produces a string that is the result of modulating two inputs. The inputs must be vec4 or
134 * float. The result is always a vec4. The inputs may be expressions, not just identifier names.
135 * Either can be NULL or "" in which case the default params control whether vec4(1,1,1,1) or
136 * vec4(0,0,0,0) is assumed. It is an error to pass kNone for default<i> if in<i> is NULL or "".
137 * Note that when if function determines that the result is a zeros or ones vec then any expression
138 * represented by in0 or in1 will not be emitted. The return value indicates whether a zeros, ones
139 * or neither was appeneded.
140 */
141GrSLConstantVec GrGLSLModulate4f(SkString* outAppend,
142 const char* in0,
143 const char* in1,
144 GrSLConstantVec default0 = kOnes_GrSLConstantVec,
145 GrSLConstantVec default1 = kOnes_GrSLConstantVec);
146
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000147/**
148 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
149 * mulFactor may be either "" or NULL. In this case either nothing will be appened (kOnes) or an
150 * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs.
151 * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert
152 * tabs to custom stage-generated lines.) If a zeros vec is assigned then the return value is
153 * kZeros, otherwise kNone.
154 */
155GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
156 int tabCnt,
157 const char* vec4VarName,
158 const char* mulFactor,
159 GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000160
161/**
162 * Produces a string that is the result of adding two inputs. The inputs must be vec4 or float.
163 * The result is always a vec4. The inputs may be expressions, not just identifier names. Either
164 * can be NULL or "" in which case if the default is kZeros then vec4(0,0,0,0) is assumed. It is an
165 * error to pass kOnes for either default or to pass kNone for default<i> if in<i> is NULL or "".
166 * Note that if the function determines that the result is a zeros vec any expression represented
167 * by in0 or in1 will not be emitted. The return value indicates whether a zeros vec was appended
168 * or not.
169 */
170GrSLConstantVec GrGLSLAdd4f(SkString* outAppend,
171 const char* in0,
172 const char* in1,
173 GrSLConstantVec default0 = kZeros_GrSLConstantVec,
174 GrSLConstantVec default1 = kZeros_GrSLConstantVec);
175
tomhudson@google.com086e5352011-12-08 14:44:10 +0000176#endif