tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 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 | |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 11 | #include "gl/GrGLInterface.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 13 | class GrGLShaderVar; |
| 14 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 15 | // Limited set of GLSL versions we build shaders for. Caller should round |
| 16 | // down the GLSL version to one of these enums. |
| 17 | enum GrGLSLGeneration { |
| 18 | /** |
| 19 | * Desktop GLSL 1.10 and ES2 shading lang (based on desktop GLSL 1.20) |
| 20 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 21 | k110_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Desktop GLSL 1.30 |
| 24 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 25 | k130_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Dekstop GLSL 1.50 |
| 28 | */ |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 29 | k150_GrGLSLGeneration, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 32 | /** |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 33 | * Types of shader-language-specific boxed variables we can create. |
| 34 | * (Currently only GrGLShaderVars, but should be applicable to other shader |
| 35 | * langauges.) |
| 36 | */ |
| 37 | enum GrSLType { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 38 | kVoid_GrSLType, |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 39 | kFloat_GrSLType, |
| 40 | kVec2f_GrSLType, |
| 41 | kVec3f_GrSLType, |
| 42 | kVec4f_GrSLType, |
| 43 | kMat33f_GrSLType, |
| 44 | kMat44f_GrSLType, |
| 45 | kSampler2D_GrSLType |
| 46 | }; |
| 47 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame^] | 48 | namespace { |
| 49 | inline int GrSLTypeToVecLength(GrSLType type) { |
| 50 | static const int kVecLengths[] = { |
| 51 | 0, // kVoid_GrSLType |
| 52 | 1, // kFloat_GrSLType |
| 53 | 2, // kVec2f_GrSLType |
| 54 | 3, // kVec3f_GrSLType |
| 55 | 4, // kVec4f_GrSLType |
| 56 | 1, // kMat33f_GrSLType |
| 57 | 1, // kMat44f_GrSLType |
| 58 | 1, // kSampler2D_GrSLType |
| 59 | }; |
| 60 | GrAssert((size_t) type < GR_ARRAY_COUNT(kVecLengths)); |
| 61 | return kVecLengths[type]; |
| 62 | } |
| 63 | } |
| 64 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 65 | /** |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 66 | * Gets the most recent GLSL Generation compatible with the OpenGL context. |
| 67 | */ |
| 68 | GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, |
| 69 | const GrGLInterface* gl); |
| 70 | |
| 71 | /** |
| 72 | * Returns a string to include at the begining of a shader to declare the GLSL |
| 73 | * version. |
| 74 | */ |
| 75 | const char* GrGetGLSLVersionDecl(GrGLBinding binding, |
| 76 | GrGLSLGeneration v); |
| 77 | |
| 78 | /** |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 79 | * Depending on the GLSL version being emitted there may be an assumed output |
| 80 | * variable from the fragment shader for the color. Otherwise, the shader must |
| 81 | * declare an output variable for the color. If this function returns true: |
| 82 | * * Parameter var's name will be set to nameIfDeclared |
| 83 | * * The variable must be declared in the fragment shader |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | * * The variable has to be bound as the color output |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 85 | * (using glBindFragDataLocation) |
| 86 | * If the function returns false: |
| 87 | * * Parameter var's name will be set to the GLSL built-in color output name. |
| 88 | * * Do not declare the variable in the shader. |
| 89 | * * Do not use glBindFragDataLocation to bind the variable |
| 90 | * In either case var is initialized to represent the color output in the |
| 91 | * shader. |
| 92 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 93 | bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 94 | const char* nameIfDeclared, |
| 95 | GrGLShaderVar* var); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 96 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 97 | /** Convert a count of 1..n floats into the corresponding type enum, |
| 98 | e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */ |
| 99 | GrSLType GrSLFloatVectorType(int count); |
| 100 | |
| 101 | /** Return the GLSL swizzle operator for a homogenous component of a vector |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame^] | 102 | with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 103 | const char* GrGLSLVectorHomogCoord(int count); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame^] | 104 | const char* GrGLSLVectorHomogCoord(GrSLType type); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 105 | |
| 106 | /** Return the GLSL swizzle operator for a nonhomogenous components of a vector |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame^] | 107 | with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 108 | const char* GrGLSLVectorNonhomogCoords(int count); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame^] | 109 | const char* GrGLSLVectorNonhomogCoords(GrSLType type); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 110 | #endif |