blob: 002a20a6635e35024c42c4675ed2589ab84519cd [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);
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000094
95 SkString fHeader; // VS+FS, GLSL version, etc
96 VarArray fVSUnis;
97 VarArray fVSAttrs;
98 VarArray fVSOutputs;
99 VarArray fGSInputs;
100 VarArray fGSOutputs;
101 VarArray fFSInputs;
102 SkString fGSHeader; // layout qualifiers specific to GS
103 VarArray fFSUnis;
104 VarArray fFSOutputs;
105 SkString fFSFunctions;
106 SkString fVSCode;
107 SkString fGSCode;
108 SkString fFSCode;
109 bool fUsesGS;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000110
tomhudson@google.com52598142012-05-24 17:44:30 +0000111 /// Per-stage settings - only valid while we're inside
112 /// GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000113 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000114
tomhudson@google.com52598142012-05-24 17:44:30 +0000115 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000116 static const int fCoordDims = 2;
117
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000118 /// True if fSampleCoords is an expression; false if it's a bare
119 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000120 bool fComplexCoord;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000121 SkString fSampleCoords;
tomhudson@google.com52598142012-05-24 17:44:30 +0000122
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000123 SkString fSwizzle;
124 SkString fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000125
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000126 SkString fTexFunc;
tomhudson@google.com5440f062012-06-01 15:55:50 +0000127
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000128 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000129
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000130};
131
132#endif