blob: 79cccc727801489fd054f42d768c738506b28f80 [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
29 void appendVarying(GrSLType type,
30 const char* name,
31 const char** vsOutName = NULL,
32 const char** fsInName = NULL);
33
34 void appendVarying(GrSLType type,
35 const char* name,
36 int stageNum,
37 const char** vsOutName = NULL,
38 const char** fsInName = NULL);
39
tomhudson@google.com52598142012-05-24 17:44:30 +000040 void computeSwizzle(uint32_t configFlags);
41 void computeModulate(const char* fsInColor);
42
43 void emitTextureSetup();
44
45 /** texture2D(samplerName, coordName), with projection
46 if necessary; if coordName is not specified,
47 uses fSampleCoords. */
48 void emitTextureLookup(const char* samplerName,
49 const char* coordName = NULL);
50
51 /** sets outColor to results of texture lookup, with
52 swizzle, and/or modulate as necessary */
53 void emitDefaultFetch(const char* outColor,
54 const char* samplerName);
55
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000056 /* TODO: can't arbitrarily OR together enum components, so
57 VariableLifetime will need to be reworked if we add
58 Geometry shaders. */
59 enum VariableLifetime {
60 kVertex_VariableLifetime = 1,
61 kFragment_VariableLifetime = 2,
62 kBoth_VariableLifetime = 3
63 };
64
65 /** Add a uniform variable to the current program, accessed
66 in vertex, fragment, or both stages. If stageNum is
67 specified, it is appended to the name to guarantee uniqueness;
68 if count is specified, the uniform is an array.
69 */
70 const GrGLShaderVar& addUniform(VariableLifetime lifetime,
71 GrSLType type,
72 const char* name,
73 int stageNum = -1,
74 int count = GrGLShaderVar::kNonArray);
75
tomhudson@google.com52598142012-05-24 17:44:30 +000076 // TODO: needs a better name
77 enum SamplerMode {
78 kDefault_SamplerMode,
79 kProj_SamplerMode,
80 kExplicitDivide_SamplerMode // must do an explicit divide
81 };
82
83 // TODO: computing this requires information about fetch mode
84 // && coord mapping, as well as StageDesc::fOptFlags - proably
85 // need to set up default value and have some custom stages
86 // override as necessary?
87 void setSamplerMode(SamplerMode samplerMode) {
88 fSamplerMode = samplerMode;
89 }
90
91
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000092 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
93 VarArray fVSUnis;
94 VarArray fVSAttrs;
95 VarArray fVSOutputs;
96 VarArray fGSInputs;
97 VarArray fGSOutputs;
98 VarArray fFSInputs;
99 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
100 VarArray fFSUnis;
101 VarArray fFSOutputs;
102 GrStringBuilder fFSFunctions;
103 GrStringBuilder fVSCode;
104 GrStringBuilder fGSCode;
105 GrStringBuilder fFSCode;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000106 bool fUsesGS;
107
tomhudson@google.com52598142012-05-24 17:44:30 +0000108 /// Per-stage settings - only valid while we're inside
109 /// GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000110 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000111
tomhudson@google.com52598142012-05-24 17:44:30 +0000112 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000113 static const int fCoordDims = 2;
114
tomhudson@google.com52598142012-05-24 17:44:30 +0000115protected:
116
117 SamplerMode fSamplerMode;
118
119public:
120
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000121 /// True if fSampleCoords is an expression; false if it's a bare
122 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000123 bool fComplexCoord;
124 GrStringBuilder fSampleCoords;
125
126 GrStringBuilder fSwizzle;
127 GrStringBuilder fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000128
129 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000130
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000131};
132
133#endif