blob: c0d3d5e596ce06d4c55a01601ecd282b96233d12 [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 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000020 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
tomhudson@google.com086e5352011-12-08 14:44:10 +000021 */
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 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000028 * Desktop GLSL 1.40
29 */
30 k140_GrGLSLGeneration,
31 /**
32 * Desktop GLSL 1.50
tomhudson@google.com086e5352011-12-08 14:44:10 +000033 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000034 k150_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000035};
36
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000037/**
tomhudson@google.com168e6342012-04-18 17:49:20 +000038 * Types of shader-language-specific boxed variables we can create.
39 * (Currently only GrGLShaderVars, but should be applicable to other shader
bsalomon@google.com281c7262012-10-23 14:31:30 +000040 * languages.)
tomhudson@google.com168e6342012-04-18 17:49:20 +000041 */
42enum GrSLType {
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000043 kVoid_GrSLType,
tomhudson@google.com168e6342012-04-18 17:49:20 +000044 kFloat_GrSLType,
45 kVec2f_GrSLType,
46 kVec3f_GrSLType,
47 kVec4f_GrSLType,
48 kMat33f_GrSLType,
49 kMat44f_GrSLType,
50 kSampler2D_GrSLType
51};
52
bsalomon@google.com4af0af62012-08-29 12:59:57 +000053enum GrSLConstantVec {
54 kZeros_GrSLConstantVec,
55 kOnes_GrSLConstantVec,
56 kNone_GrSLConstantVec,
57};
58
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000059namespace {
60inline 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}
bsalomon@google.com4af0af62012-08-29 12:59:57 +000074
75const 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
82const 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}
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000088}
89
tomhudson@google.com168e6342012-04-18 17:49:20 +000090/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000091 * Gets the most recent GLSL Generation compatible with the OpenGL context.
92 */
93GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
94 const GrGLInterface* gl);
95
96/**
bsalomon@google.com281c7262012-10-23 14:31:30 +000097 * Returns a string to include at the beginning of a shader to declare the GLSL
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000098 * version.
99 */
100const char* GrGetGLSLVersionDecl(GrGLBinding binding,
101 GrGLSLGeneration v);
102
103/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000104 * 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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109 * * The variable has to be bound as the color output
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000110 * (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 */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000118bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000119 const char* nameIfDeclared,
120 GrGLShaderVar* var);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000121
tomhudson@google.com168e6342012-04-18 17:49:20 +0000122/** 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
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000127 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000128const char* GrGLSLVectorHomogCoord(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000129const char* GrGLSLVectorHomogCoord(GrSLType type);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000130
131/** Return the GLSL swizzle operator for a nonhomogenous components of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000132 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000133const char* GrGLSLVectorNonhomogCoords(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000134const char* GrGLSLVectorNonhomogCoords(GrSLType type);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000135
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
bsalomon@google.com281c7262012-10-23 14:31:30 +0000143 * or neither was appended.
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000144 */
145GrSLConstantVec GrGLSLModulate4f(SkString* outAppend,
146 const char* in0,
147 const char* in1,
148 GrSLConstantVec default0 = kOnes_GrSLConstantVec,
149 GrSLConstantVec default1 = kOnes_GrSLConstantVec);
150
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000151/**
152 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
bsalomon@google.com281c7262012-10-23 14:31:30 +0000153 * mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000154 * 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
bsalomon@google.comd698f772012-10-25 13:22:00 +0000156 * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000157 * kZeros, otherwise kNone.
158 */
159GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
160 int tabCnt,
161 const char* vec4VarName,
162 const char* mulFactor,
163 GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000164
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
tomhudson@google.com086e5352011-12-08 14:44:10 +0000180#endif