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"
|
2013-04-18 19:36:09 +00:00
|
|
|
#include "GrColor.h"
|
2013-03-12 12:26:08 +00:00
|
|
|
#include "GrTypesPriv.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-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
|
|
|
|
};
|
2013-04-22 20:26:44 +00:00
|
|
|
GR_STATIC_ASSERT(kGrSLTypeCount == GR_ARRAY_COUNT(kVecLengths));
|
2012-08-28 18:20:18 +00:00
|
|
|
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)"};
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
|
2012-08-29 12:59:57 +00:00
|
|
|
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)"};
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
|
2012-08-29 12:59:57 +00:00
|
|
|
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);
|
2013-04-18 19:36:09 +00:00
|
|
|
/**
|
|
|
|
* Converts a GrSLType to a string containing the name of the equivalent GLSL type.
|
|
|
|
*/
|
|
|
|
static const char* GrGLSLTypeString(GrSLType t) {
|
|
|
|
switch (t) {
|
|
|
|
case kVoid_GrSLType:
|
|
|
|
return "void";
|
|
|
|
case kFloat_GrSLType:
|
|
|
|
return "float";
|
|
|
|
case kVec2f_GrSLType:
|
|
|
|
return "vec2";
|
|
|
|
case kVec3f_GrSLType:
|
|
|
|
return "vec3";
|
|
|
|
case kVec4f_GrSLType:
|
|
|
|
return "vec4";
|
|
|
|
case kMat33f_GrSLType:
|
|
|
|
return "mat3";
|
|
|
|
case kMat44f_GrSLType:
|
|
|
|
return "mat4";
|
|
|
|
case kSampler2D_GrSLType:
|
|
|
|
return "sampler2D";
|
|
|
|
default:
|
|
|
|
GrCrash("Unknown shader var type.");
|
|
|
|
return ""; // suppress warning
|
|
|
|
}
|
|
|
|
}
|
2012-02-10 15:56:06 +00:00
|
|
|
|
2013-04-18 19:36:09 +00:00
|
|
|
/** Return the type enum for a vector of floats of length n (1..4),
|
|
|
|
e.g. 1 -> "float", 2 -> "vec2", ... */
|
|
|
|
static inline const char* GrGLSLFloatVectorTypeString(int n) {
|
|
|
|
return GrGLSLTypeString(GrSLFloatVectorType(n));
|
|
|
|
}
|
2012-04-18 17:49:20 +00:00
|
|
|
|
|
|
|
/** 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
|
|
|
|
|
|
|
/**
|
2013-04-18 19:36:09 +00:00
|
|
|
* Produces a string that is the result of modulating two inputs. The inputs must be vecN or
|
|
|
|
* float. The result is always a vecN. The inputs may be expressions, not just identifier names.
|
|
|
|
* Either can be NULL or "" in which case the default params control whether a vector of ones or
|
|
|
|
* zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
|
|
|
|
* function determines that the result is a zeros or ones vec then any expression represented by
|
|
|
|
* or in1 will not be emitted (side effects won't occur). The return value indicates whether a
|
|
|
|
* known zeros or ones vector resulted. The output can be suppressed when known vector is produced
|
|
|
|
* by passing true for omitIfConstVec.
|
2012-08-29 12:59:57 +00:00
|
|
|
*/
|
2013-04-18 19:36:09 +00:00
|
|
|
template <int N>
|
|
|
|
GrSLConstantVec GrGLSLModulatef(SkString* outAppend,
|
|
|
|
const char* in0,
|
|
|
|
const char* in1,
|
|
|
|
GrSLConstantVec default0 = kOnes_GrSLConstantVec,
|
|
|
|
GrSLConstantVec default1 = kOnes_GrSLConstantVec,
|
|
|
|
bool omitIfConstVec = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces a string that is the result of adding two inputs. The inputs must be vecN or
|
|
|
|
* float. The result is always a vecN. The inputs may be expressions, not just identifier names.
|
|
|
|
* Either can be NULL or "" in which case the default params control whether a vector of ones or
|
|
|
|
* zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
|
|
|
|
* function determines that the result is a zeros or ones vec then any expression represented by
|
|
|
|
* or in1 will not be emitted (side effects won't occur). The return value indicates whether a
|
|
|
|
* known zeros or ones vector resulted. The output can be suppressed when known vector is produced
|
|
|
|
* by passing true for omitIfConstVec.
|
|
|
|
*/
|
|
|
|
template <int N>
|
|
|
|
GrSLConstantVec GrGLSLAddf(SkString* outAppend,
|
|
|
|
const char* in0,
|
|
|
|
const char* in1,
|
|
|
|
GrSLConstantVec default0 = kZeros_GrSLConstantVec,
|
|
|
|
GrSLConstantVec default1 = kZeros_GrSLConstantVec,
|
|
|
|
bool omitIfConstVec = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces a string that is the result of subtracting two inputs. The inputs must be vecN or
|
|
|
|
* float. The result is always a vecN. The inputs may be expressions, not just identifier names.
|
|
|
|
* Either can be NULL or "" in which case the default params control whether a vector of ones or
|
|
|
|
* zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
|
|
|
|
* function determines that the result is a zeros or ones vec then any expression represented by
|
|
|
|
* or in1 will not be emitted (side effects won't occur). The return value indicates whether a
|
|
|
|
* known zeros or ones vector resulted. The output can be suppressed when known vector is produced
|
|
|
|
* by passing true for omitIfConstVec.
|
|
|
|
*/
|
|
|
|
template <int N>
|
|
|
|
GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
|
|
|
|
const char* in0,
|
|
|
|
const char* in1,
|
|
|
|
GrSLConstantVec default0 = kZeros_GrSLConstantVec,
|
|
|
|
GrSLConstantVec default1 = kZeros_GrSLConstantVec,
|
|
|
|
bool omitIfConstVec = false);
|
2012-08-29 12:59:57 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2013-04-18 19:36:09 +00:00
|
|
|
* Given an expression that evaluates to a GLSL vec4, extract a component. If expr is NULL or ""
|
|
|
|
* the value of defaultExpr is used. It is an error to pass an empty expr and have set defaultExpr
|
|
|
|
* to kNone. The return value indicates whether the value is known to be 0 or 1. If omitIfConst is
|
|
|
|
* set then nothing is appended when the return is not kNone.
|
|
|
|
*/
|
|
|
|
GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend,
|
|
|
|
const char* expr,
|
|
|
|
GrColorComponentFlags component,
|
|
|
|
GrSLConstantVec defaultExpr = kNone_GrSLConstantVec,
|
|
|
|
bool omitIfConst = false);
|
|
|
|
|
|
|
|
#include "GrGLSL_impl.h"
|
2012-08-29 12:59:57 +00:00
|
|
|
|
2012-02-10 15:56:06 +00:00
|
|
|
#endif
|