blob: 119263729fcb414884a54b86cd2d3dd52cac6de9 [file] [log] [blame]
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001/*
2 * Copyright 2012 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 GrGLShaderBuilder_DEFINED
9#define GrGLShaderBuilder_DEFINED
10
11#include "GrAllocator.h"
12#include "gl/GrGLShaderVar.h"
13#include "gl/GrGLSL.h"
14
15typedef GrTAllocator<GrGLShaderVar> VarArray;
16
17/**
18 Containts all the incremental state of a shader as it is being built,
19 as well as helpers to manipulate that state.
20 TODO: migrate CompileShaders() here?
21*/
22
23class GrGLShaderBuilder {
24
25public:
26
27 GrGLShaderBuilder();
28
tomhudson@google.com52598142012-05-24 17:44:30 +000029 void computeSwizzle(uint32_t configFlags);
30 void computeModulate(const char* fsInColor);
31
32 void emitTextureSetup();
33
34 /** texture2D(samplerName, coordName), with projection
35 if necessary; if coordName is not specified,
36 uses fSampleCoords. */
37 void emitTextureLookup(const char* samplerName,
38 const char* coordName = NULL);
39
40 /** sets outColor to results of texture lookup, with
41 swizzle, and/or modulate as necessary */
42 void emitDefaultFetch(const char* outColor,
43 const char* samplerName);
44
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000045 /* TODO: can't arbitrarily OR together enum components, so
46 VariableLifetime will need to be reworked if we add
47 Geometry shaders. */
48 enum VariableLifetime {
49 kVertex_VariableLifetime = 1,
50 kFragment_VariableLifetime = 2,
51 kBoth_VariableLifetime = 3
52 };
53
54 /** Add a uniform variable to the current program, accessed
55 in vertex, fragment, or both stages. If stageNum is
56 specified, it is appended to the name to guarantee uniqueness;
57 if count is specified, the uniform is an array.
58 */
59 const GrGLShaderVar& addUniform(VariableLifetime lifetime,
60 GrSLType type,
61 const char* name,
62 int stageNum = -1,
63 int count = GrGLShaderVar::kNonArray);
64
tomhudson@google.com23cb2292012-05-30 18:26:03 +000065 /** Add a varying variable to the current program to pass
66 values between vertex and fragment shaders.
67 If the last two parameters are non-NULL, they are filled
68 in with the name generated. */
69 void addVarying(GrSLType type,
70 const char* name,
71 const char** vsOutName = NULL,
72 const char** fsInName = NULL);
73
74 /** Add a varying variable to the current program to pass
75 values between vertex and fragment shaders;
76 stageNum is appended to the name to guarantee uniqueness.
77 If the last two parameters are non-NULL, they are filled
78 in with the name generated. */
79 void addVarying(GrSLType type,
80 const char* name,
81 int stageNum,
82 const char** vsOutName = NULL,
83 const char** fsInName = NULL);
84
tomhudson@google.com52598142012-05-24 17:44:30 +000085 // TODO: needs a better name
86 enum SamplerMode {
87 kDefault_SamplerMode,
88 kProj_SamplerMode,
89 kExplicitDivide_SamplerMode // must do an explicit divide
90 };
91
92 // TODO: computing this requires information about fetch mode
93 // && coord mapping, as well as StageDesc::fOptFlags - proably
94 // need to set up default value and have some custom stages
95 // override as necessary?
96 void setSamplerMode(SamplerMode samplerMode) {
97 fSamplerMode = samplerMode;
98 }
99
100
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000101 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
102 VarArray fVSUnis;
103 VarArray fVSAttrs;
104 VarArray fVSOutputs;
105 VarArray fGSInputs;
106 VarArray fGSOutputs;
107 VarArray fFSInputs;
108 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
109 VarArray fFSUnis;
110 VarArray fFSOutputs;
111 GrStringBuilder fFSFunctions;
112 GrStringBuilder fVSCode;
113 GrStringBuilder fGSCode;
114 GrStringBuilder fFSCode;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000115 bool fUsesGS;
116
tomhudson@google.com52598142012-05-24 17:44:30 +0000117 /// Per-stage settings - only valid while we're inside
118 /// GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000119 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000120
tomhudson@google.com52598142012-05-24 17:44:30 +0000121 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000122 static const int fCoordDims = 2;
123
tomhudson@google.com52598142012-05-24 17:44:30 +0000124protected:
125
126 SamplerMode fSamplerMode;
127
128public:
129
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000130 /// True if fSampleCoords is an expression; false if it's a bare
131 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000132 bool fComplexCoord;
133 GrStringBuilder fSampleCoords;
134
135 GrStringBuilder fSwizzle;
136 GrStringBuilder fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000137
138 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000139
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000140};
141
142#endif