blob: cc8bc003a70c01d8a3f3cd7ae3363eedfeed2ebc [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
tomhudson@google.com5440f062012-06-01 15:55:50 +000032 // TODO: needs a better name
33 enum SamplerMode {
34 kDefault_SamplerMode,
35 kProj_SamplerMode,
36 kExplicitDivide_SamplerMode // must do an explicit divide
37 };
38
39 /** Determines whether we should use texture2D() or texture2Dproj(),
40 and if an explicit divide is required for the sample coordinates,
41 creates the new variable and emits the code to initialize it. */
42 void setupTextureAccess(SamplerMode samplerMode, int stageNum);
tomhudson@google.com52598142012-05-24 17:44:30 +000043
44 /** texture2D(samplerName, coordName), with projection
45 if necessary; if coordName is not specified,
46 uses fSampleCoords. */
47 void emitTextureLookup(const char* samplerName,
48 const char* coordName = NULL);
49
50 /** sets outColor to results of texture lookup, with
51 swizzle, and/or modulate as necessary */
52 void emitDefaultFetch(const char* outColor,
53 const char* samplerName);
54
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000055 /* TODO: can't arbitrarily OR together enum components, so
56 VariableLifetime will need to be reworked if we add
57 Geometry shaders. */
58 enum VariableLifetime {
59 kVertex_VariableLifetime = 1,
60 kFragment_VariableLifetime = 2,
61 kBoth_VariableLifetime = 3
62 };
63
64 /** Add a uniform variable to the current program, accessed
65 in vertex, fragment, or both stages. If stageNum is
66 specified, it is appended to the name to guarantee uniqueness;
67 if count is specified, the uniform is an array.
68 */
69 const GrGLShaderVar& addUniform(VariableLifetime lifetime,
70 GrSLType type,
71 const char* name,
72 int stageNum = -1,
73 int count = GrGLShaderVar::kNonArray);
74
tomhudson@google.com23cb2292012-05-30 18:26:03 +000075 /** Add a varying variable to the current program to pass
76 values between vertex and fragment shaders.
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 const char** vsOutName = NULL,
82 const char** fsInName = NULL);
83
84 /** Add a varying variable to the current program to pass
85 values between vertex and fragment shaders;
86 stageNum is appended to the name to guarantee uniqueness.
87 If the last two parameters are non-NULL, they are filled
88 in with the name generated. */
89 void addVarying(GrSLType type,
90 const char* name,
91 int stageNum,
92 const char** vsOutName = NULL,
93 const char** fsInName = NULL);
94
tomhudson@google.com52598142012-05-24 17:44:30 +000095
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000096 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
97 VarArray fVSUnis;
98 VarArray fVSAttrs;
99 VarArray fVSOutputs;
100 VarArray fGSInputs;
101 VarArray fGSOutputs;
102 VarArray fFSInputs;
103 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
104 VarArray fFSUnis;
105 VarArray fFSOutputs;
106 GrStringBuilder fFSFunctions;
107 GrStringBuilder fVSCode;
108 GrStringBuilder fGSCode;
109 GrStringBuilder fFSCode;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000110 bool fUsesGS;
111
tomhudson@google.com52598142012-05-24 17:44:30 +0000112 /// Per-stage settings - only valid while we're inside
113 /// GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000114 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000115
tomhudson@google.com52598142012-05-24 17:44:30 +0000116 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000117 static const int fCoordDims = 2;
118
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000119 /// True if fSampleCoords is an expression; false if it's a bare
120 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000121 bool fComplexCoord;
122 GrStringBuilder fSampleCoords;
123
124 GrStringBuilder fSwizzle;
125 GrStringBuilder fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000126
tomhudson@google.com5440f062012-06-01 15:55:50 +0000127 GrStringBuilder fTexFunc;
128
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000129 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000130
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000131};
132
133#endif