2011-12-08 14:44:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrGLSL_DEFINED
|
|
|
|
#define GrGLSL_DEFINED
|
|
|
|
|
2012-02-14 15:11:59 +00:00
|
|
|
#include "gl/GrGLInterface.h"
|
2011-12-08 14:44:10 +00:00
|
|
|
|
2012-02-10 15:56:06 +00:00
|
|
|
class GrGLShaderVar;
|
2012-08-29 12:59:57 +00:00
|
|
|
class SkString;
|
2012-02-10 15:56:06 +00:00
|
|
|
|
2011-12-08 14:44:10 +00:00
|
|
|
// Limited set of GLSL versions we build shaders for. Caller should round
|
|
|
|
// down the GLSL version to one of these enums.
|
|
|
|
enum GrGLSLGeneration {
|
|
|
|
/**
|
2012-10-23 14:31:30 +00:00
|
|
|
* Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
|
2011-12-08 14:44:10 +00:00
|
|
|
*/
|
2012-02-10 15:56:06 +00:00
|
|
|
k110_GrGLSLGeneration,
|
2011-12-08 14:44:10 +00:00
|
|
|
/**
|
|
|
|
* Desktop GLSL 1.30
|
|
|
|
*/
|
2012-02-10 15:56:06 +00:00
|
|
|
k130_GrGLSLGeneration,
|
2011-12-08 14:44:10 +00:00
|
|
|
/**
|
2012-10-23 14:31:30 +00:00
|
|
|
* Desktop GLSL 1.40
|
|
|
|
*/
|
|
|
|
k140_GrGLSLGeneration,
|
|
|
|
/**
|
|
|
|
* Desktop GLSL 1.50
|
2011-12-08 14:44:10 +00:00
|
|
|
*/
|
2012-02-10 15:56:06 +00:00
|
|
|
k150_GrGLSLGeneration,
|
2011-12-08 14:44:10 +00:00
|
|
|
};
|
|
|
|
|
2012-04-18 17:49:20 +00:00
|
|
|
/**
|
|
|
|
* Types of shader-language-specific boxed variables we can create.
|
|
|
|
* (Currently only GrGLShaderVars, but should be applicable to other shader
|
2012-10-23 14:31:30 +00:00
|
|
|
* languages.)
|
2012-04-18 17:49:20 +00:00
|
|
|
*/
|
|
|
|
enum GrSLType {
|
2012-08-07 17:36:29 +00:00
|
|
|
kVoid_GrSLType,
|
2012-04-18 17:49:20 +00:00
|
|
|
kFloat_GrSLType,
|
|
|
|
kVec2f_GrSLType,
|
|
|
|
kVec3f_GrSLType,
|
|
|
|
kVec4f_GrSLType,
|
|
|
|
kMat33f_GrSLType,
|
|
|
|
kMat44f_GrSLType,
|
|
|
|
kSampler2D_GrSLType
|
|
|
|
};
|
|
|
|
|
2012-08-29 12:59:57 +00:00
|
|
|
enum GrSLConstantVec {
|
|
|
|
kZeros_GrSLConstantVec,
|
|
|
|
kOnes_GrSLConstantVec,
|
|
|
|
kNone_GrSLConstantVec,
|
|
|
|
};
|
|
|
|
|
2012-08-28 18:20:18 +00:00
|
|
|
namespace {
|
2013-01-07 16:47:43 +00:00
|
|
|
static inline int GrSLTypeToVecLength(GrSLType type) {
|
2012-08-28 18:20:18 +00:00
|
|
|
static const int kVecLengths[] = {
|
|
|
|
0, // kVoid_GrSLType
|
|
|
|
1, // kFloat_GrSLType
|
|
|
|
2, // kVec2f_GrSLType
|
|
|
|
3, // kVec3f_GrSLType
|
|
|
|
4, // kVec4f_GrSLType
|
|
|
|
1, // kMat33f_GrSLType
|
|
|
|
1, // kMat44f_GrSLType
|
|
|
|
1, // kSampler2D_GrSLType
|
|
|
|
};
|
|
|
|
GrAssert((size_t) type < GR_ARRAY_COUNT(kVecLengths));
|
|
|
|
return kVecLengths[type];
|
|
|
|
}
|
2012-08-29 12:59:57 +00:00
|
|
|
|
2013-01-07 16:47:43 +00:00
|
|
|
static inline const char* GrGLSLOnesVecf(int count) {
|
2012-08-29 12:59:57 +00:00
|
|
|
static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
|
|
|
|
"vec3(1,1,1)", "vec4(1,1,1,1)"};
|
|
|
|
GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
|
|
|
|
return kONESVEC[count];
|
|
|
|
}
|
|
|
|
|
2013-01-07 16:47:43 +00:00
|
|
|
static inline const char* GrGLSLZerosVecf(int count) {
|
2012-08-29 12:59:57 +00:00
|
|
|
static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
|
|
|
|
"vec3(0,0,0)", "vec4(0,0,0,0)"};
|
|
|
|
GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
|
|
|
|
return kZEROSVEC[count];
|
|
|
|
}
|
2012-08-28 18:20:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-10 15:56:06 +00:00
|
|
|
/**
|
|
|
|
* Gets the most recent GLSL Generation compatible with the OpenGL context.
|
|
|
|
*/
|
|
|
|
GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
|
|
|
|
const GrGLInterface* gl);
|
2011-12-08 14:44:10 +00:00
|
|
|
|
2012-02-10 15:56:06 +00:00
|
|
|
/**
|
2012-10-23 14:31:30 +00:00
|
|
|
* Returns a string to include at the beginning of a shader to declare the GLSL
|
2012-02-10 15:56:06 +00:00
|
|
|
* version.
|
|
|
|
*/
|
|
|
|
const char* GrGetGLSLVersionDecl(GrGLBinding binding,
|
|
|
|
GrGLSLGeneration v);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Depending on the GLSL version being emitted there may be an assumed output
|
|
|
|
* variable from the fragment shader for the color. Otherwise, the shader must
|
|
|
|
* declare an output variable for the color. If this function returns true:
|
|
|
|
* * Parameter var's name will be set to nameIfDeclared
|
|
|
|
* * The variable must be declared in the fragment shader
|
2012-08-23 18:09:54 +00:00
|
|
|
* * The variable has to be bound as the color output
|
2012-02-10 15:56:06 +00:00
|
|
|
* (using glBindFragDataLocation)
|
|
|
|
* If the function returns false:
|
|
|
|
* * Parameter var's name will be set to the GLSL built-in color output name.
|
|
|
|
* * Do not declare the variable in the shader.
|
|
|
|
* * Do not use glBindFragDataLocation to bind the variable
|
|
|
|
* In either case var is initialized to represent the color output in the
|
|
|
|
* shader.
|
|
|
|
*/
|
2012-04-18 17:49:20 +00:00
|
|
|
bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
|
2012-02-10 15:56:06 +00:00
|
|
|
const char* nameIfDeclared,
|
|
|
|
GrGLShaderVar* var);
|
|
|
|
|
2012-04-18 17:49:20 +00:00
|
|
|
/** Convert a count of 1..n floats into the corresponding type enum,
|
|
|
|
e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
|
|
|
|
GrSLType GrSLFloatVectorType(int count);
|
|
|
|
|
|
|
|
/** Return the GLSL swizzle operator for a homogenous component of a vector
|
2012-08-28 18:20:18 +00:00
|
|
|
with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
|
2012-04-18 17:49:20 +00:00
|
|
|
const char* GrGLSLVectorHomogCoord(int count);
|
2012-08-28 18:20:18 +00:00
|
|
|
const char* GrGLSLVectorHomogCoord(GrSLType type);
|
2012-04-18 17:49:20 +00:00
|
|
|
|
|
|
|
/** Return the GLSL swizzle operator for a nonhomogenous components of a vector
|
2012-08-28 18:20:18 +00:00
|
|
|
with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
|
2012-04-18 17:49:20 +00:00
|
|
|
const char* GrGLSLVectorNonhomogCoords(int count);
|
2012-08-28 18:20:18 +00:00
|
|
|
const char* GrGLSLVectorNonhomogCoords(GrSLType type);
|
2012-08-29 12:59:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces a string that is the result of modulating two inputs. The inputs must be vec4 or
|
|
|
|
* float. The result is always a vec4. The inputs may be expressions, not just identifier names.
|
|
|
|
* Either can be NULL or "" in which case the default params control whether vec4(1,1,1,1) or
|
|
|
|
* vec4(0,0,0,0) is assumed. It is an error to pass kNone for default<i> if in<i> is NULL or "".
|
|
|
|
* Note that when if function determines that the result is a zeros or ones vec then any expression
|
|
|
|
* represented by in0 or in1 will not be emitted. The return value indicates whether a zeros, ones
|
2012-10-23 14:31:30 +00:00
|
|
|
* or neither was appended.
|
2012-08-29 12:59:57 +00:00
|
|
|
*/
|
|
|
|
GrSLConstantVec GrGLSLModulate4f(SkString* outAppend,
|
|
|
|
const char* in0,
|
|
|
|
const char* in1,
|
|
|
|
GrSLConstantVec default0 = kOnes_GrSLConstantVec,
|
|
|
|
GrSLConstantVec default1 = kOnes_GrSLConstantVec);
|
|
|
|
|
2012-08-30 19:11:34 +00:00
|
|
|
/**
|
|
|
|
* Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
|
2012-10-23 14:31:30 +00:00
|
|
|
* mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an
|
2012-08-30 19:11:34 +00:00
|
|
|
* assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs.
|
|
|
|
* A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert
|
2012-10-25 13:22:00 +00:00
|
|
|
* tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is
|
2012-08-30 19:11:34 +00:00
|
|
|
* kZeros, otherwise kNone.
|
|
|
|
*/
|
|
|
|
GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
|
|
|
|
int tabCnt,
|
|
|
|
const char* vec4VarName,
|
|
|
|
const char* mulFactor,
|
|
|
|
GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
|
2012-08-29 12:59:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces a string that is the result of adding two inputs. The inputs must be vec4 or float.
|
|
|
|
* The result is always a vec4. The inputs may be expressions, not just identifier names. Either
|
|
|
|
* can be NULL or "" in which case if the default is kZeros then vec4(0,0,0,0) is assumed. It is an
|
|
|
|
* error to pass kOnes for either default or to pass kNone for default<i> if in<i> is NULL or "".
|
|
|
|
* Note that if the function determines that the result is a zeros vec any expression represented
|
|
|
|
* by in0 or in1 will not be emitted. The return value indicates whether a zeros vec was appended
|
|
|
|
* or not.
|
|
|
|
*/
|
|
|
|
GrSLConstantVec GrGLSLAdd4f(SkString* outAppend,
|
|
|
|
const char* in0,
|
|
|
|
const char* in1,
|
|
|
|
GrSLConstantVec default0 = kZeros_GrSLConstantVec,
|
|
|
|
GrSLConstantVec default1 = kZeros_GrSLConstantVec);
|
|
|
|
|
2012-02-10 15:56:06 +00:00
|
|
|
#endif
|