blob: f69385f23901c3b997b619057417b344b909983f [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;
14
tomhudson@google.com086e5352011-12-08 14:44:10 +000015// Limited set of GLSL versions we build shaders for. Caller should round
16// down the GLSL version to one of these enums.
17enum GrGLSLGeneration {
18 /**
19 * Desktop GLSL 1.10 and ES2 shading lang (based on desktop GLSL 1.20)
20 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000021 k110_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000022 /**
23 * Desktop GLSL 1.30
24 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000025 k130_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000026 /**
27 * Dekstop GLSL 1.50
28 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000029 k150_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000030};
31
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000032/**
tomhudson@google.com168e6342012-04-18 17:49:20 +000033 * Types of shader-language-specific boxed variables we can create.
34 * (Currently only GrGLShaderVars, but should be applicable to other shader
35 * langauges.)
36 */
37enum GrSLType {
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000038 kVoid_GrSLType,
tomhudson@google.com168e6342012-04-18 17:49:20 +000039 kFloat_GrSLType,
40 kVec2f_GrSLType,
41 kVec3f_GrSLType,
42 kVec4f_GrSLType,
43 kMat33f_GrSLType,
44 kMat44f_GrSLType,
45 kSampler2D_GrSLType
46};
47
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000048namespace {
49inline 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.com168e6342012-04-18 17:49:20 +000065/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000066 * Gets the most recent GLSL Generation compatible with the OpenGL context.
67 */
68GrGLSLGeneration 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 */
75const char* GrGetGLSLVersionDecl(GrGLBinding binding,
76 GrGLSLGeneration v);
77
78/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000079 * 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.comfbfcd562012-08-23 18:09:54 +000084 * * The variable has to be bound as the color output
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000085 * (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.com168e6342012-04-18 17:49:20 +000093bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000094 const char* nameIfDeclared,
95 GrGLShaderVar* var);
tomhudson@google.com086e5352011-12-08 14:44:10 +000096
tomhudson@google.com168e6342012-04-18 17:49:20 +000097/** Convert a count of 1..n floats into the corresponding type enum,
98 e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
99GrSLType GrSLFloatVectorType(int count);
100
101/** Return the GLSL swizzle operator for a homogenous component of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000102 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000103const char* GrGLSLVectorHomogCoord(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000104const char* GrGLSLVectorHomogCoord(GrSLType type);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000105
106/** Return the GLSL swizzle operator for a nonhomogenous components of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000107 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000108const char* GrGLSLVectorNonhomogCoords(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000109const char* GrGLSLVectorNonhomogCoords(GrSLType type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000110#endif