blob: e02d53d00a2c9badb92308d714e31ced064459fe [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"
twiz@google.coma5e65ec2012-08-02 15:15:16 +000012#include "GrCustomStage.h"
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000013#include "gl/GrGLShaderVar.h"
14#include "gl/GrGLSL.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000015#include "gl/GrGLUniformManager.h"
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000016
bsalomon@google.comad5e9372012-07-11 18:11:27 +000017class GrGLContextInfo;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000018
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000019/**
bsalomon@google.comeb715c82012-07-11 15:03:31 +000020 Contains all the incremental state of a shader as it is being built,as well as helpers to
21 manipulate that state.
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000022*/
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000023class GrGLShaderBuilder {
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000024public:
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000025 /**
26 * Used by GrGLProgramStages to add texture reads to their shader code.
27 */
28 class TextureSampler {
29 public:
30 TextureSampler()
31 : fTextureAccess(NULL)
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000032 , fSamplerUniform(GrGLUniformManager::kInvalidUniformHandle) {}
33
34 TextureSampler(const TextureSampler& other) { *this = other; }
35
36 TextureSampler& operator= (const TextureSampler& other) {
37 GrAssert(NULL == fTextureAccess);
38 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform);
39
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000040 fTextureAccess = other.fTextureAccess;
41 fSamplerUniform = other.fSamplerUniform;
42 return *this;
43 }
44
45 const GrTextureAccess* textureAccess() const { return fTextureAccess; }
46
47 private:
48 void init(GrGLShaderBuilder* builder, const GrTextureAccess* access) {
49 GrAssert(NULL == fTextureAccess);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000050 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform);
51
52 GrAssert(NULL != builder);
53 GrAssert(NULL != access);
54 fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
55 kSampler2D_GrSLType,
56 "Sampler");
57 GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUniform);
58
59 fTextureAccess = access;
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000060 }
61
62 const GrTextureAccess* fTextureAccess;
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000063 GrGLUniformManager::UniformHandle fSamplerUniform;
64
65 friend class GrGLShaderBuilder; // to access fSamplerUniform
66 friend class GrGLProgram; // to construct these and access fSamplerUniform.
67 };
68
69 typedef SkTArray<TextureSampler> TextureSamplerArray;
70
bsalomon@google.comeb715c82012-07-11 15:03:31 +000071 enum ShaderType {
72 kVertex_ShaderType = 0x1,
73 kGeometry_ShaderType = 0x2,
74 kFragment_ShaderType = 0x4,
75 };
76
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000077 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&);
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000078
bsalomon@google.comeb715c82012-07-11 15:03:31 +000079 /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide
80 is required for the sample coordinates, creates the new variable and emits the code to
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000081 initialize it. This should only be called by GrGLProgram.*/
82 void setupTextureAccess(const char* varyingFSName, GrSLType varyingType);
tomhudson@google.com52598142012-05-24 17:44:30 +000083
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000084 /** Appends a texture sample with projection if necessary; if coordName is not
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000085 specified, uses fSampleCoords. coordType must either be Vec2f or Vec3f. The latter is
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000086 interpreted as projective texture coords. The vec length and swizzle order of the result
87 depends on the GrTextureAccess associated with the TextureSampler. */
bsalomon@google.com868a8e72012-08-30 19:11:34 +000088 void appendTextureLookup(SkString* out,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000089 const TextureSampler&,
bsalomon@google.com868a8e72012-08-30 19:11:34 +000090 const char* coordName = NULL,
91 GrSLType coordType = kVec2f_GrSLType) const;
tomhudson@google.com52598142012-05-24 17:44:30 +000092
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000093 /** Does the work of appendTextureLookup and modulates the result by modulation. The result is
94 always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or
95 float. If modulation is "" or NULL it this function acts as though appendTextureLookup were
96 called. */
bsalomon@google.com868a8e72012-08-30 19:11:34 +000097 void appendTextureLookupAndModulate(SkString* out,
98 const char* modulation,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000099 const TextureSampler&,
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000100 const char* coordName = NULL,
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +0000101 GrSLType coordType = kVec2f_GrSLType) const;
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000102
103 /** Gets the name of the default texture coords which are always kVec2f */
104 const char* defaultTexCoordsName() const { return fDefaultTexCoordsName.c_str(); }
105
106 /* Returns true if the texture matrix from which the default texture coords are computed has
107 perspective. */
108 bool defaultTextureMatrixIsPerspective() const {
109 return fTexCoordVaryingType == kVec3f_GrSLType;
110 }
tomhudson@google.com52598142012-05-24 17:44:30 +0000111
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000112 /** Emits a helper function outside of main(). Currently ShaderType must be
113 kFragment_ShaderType. */
114 void emitFunction(ShaderType shader,
115 GrSLType returnType,
116 const char* name,
117 int argCnt,
118 const GrGLShaderVar* args,
119 const char* body,
120 SkString* outName);
121
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000122 /** Generates a StageKey for the shader code based on the texture access parameters and the
123 capabilities of the GL context. This is useful for keying the shader programs that may
124 have multiple representations, based on the type/format of textures used. */
125 static GrCustomStage::StageKey KeyForTextureAccess(const GrTextureAccess& access,
126 const GrGLCaps& caps);
127
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000128 /** Add a uniform variable to the current program, that has visibilty in one or more shaders.
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000129 visibility is a bitfield of ShaderType values indicating from which shaders the uniform
130 should be accessible. At least one bit must be set. Geometry shader uniforms are not
131 supported at this time. The actual uniform name will be mangled. If outName is not NULL then
132 it will refer to the final uniform name after return. Use the addUniformArray variant to add
133 an array of uniforms.
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000134 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000135 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility,
136 GrSLType type,
137 const char* name,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000138 const char** outName = NULL) {
139 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
140 }
141 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility,
142 GrSLType type,
143 const char* name,
144 int arrayCount,
145 const char** outName = NULL);
bsalomon@google.com032b2212012-07-16 13:36:18 +0000146
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000147 const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000148
149 /**
150 * Shorcut for getUniformVariable(u).c_str()
151 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000152 const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000153 return this->getUniformVariable(u).c_str();
154 }
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000155
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000156 /** Add a varying variable to the current program to pass values between vertex and fragment
157 shaders. If the last two parameters are non-NULL, they are filled in with the name
158 generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000159 void addVarying(GrSLType type,
160 const char* name,
161 const char** vsOutName = NULL,
162 const char** fsInName = NULL);
163
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000164 /** Called after building is complete to get the final shader string. */
165 void getShader(ShaderType, SkString*) const;
166
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000167 /**
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000168 * TODO: Make this do all the compiling, linking, etc. Hide from the custom stages
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000169 */
170 void finished(GrGLuint programID);
171
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000172 /**
173 * Sets the current stage (used to make variable names unique).
174 * TODO: Hide from the custom stages
175 */
176 void setCurrentStage(int stage) { fCurrentStage = stage; }
177 void setNonStage() { fCurrentStage = kNonStageIdx; }
178
bsalomon@google.com032b2212012-07-16 13:36:18 +0000179private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000180
bsalomon@google.com032b2212012-07-16 13:36:18 +0000181 typedef GrTAllocator<GrGLShaderVar> VarArray;
182
bsalomon@google.com032b2212012-07-16 13:36:18 +0000183 void appendDecls(const VarArray&, SkString*) const;
184 void appendUniformDecls(ShaderType, SkString*) const;
185
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000186 typedef GrGLUniformManager::BuilderUniform BuilderUniform;
187 GrGLUniformManager::BuilderUniformArray fUniforms;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000188
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000189 // TODO: Everything below here private.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000190public:
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000191
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000192 SkString fHeader; // VS+FS, GLSL version, etc
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000193 VarArray fVSAttrs;
194 VarArray fVSOutputs;
195 VarArray fGSInputs;
196 VarArray fGSOutputs;
197 VarArray fFSInputs;
198 SkString fGSHeader; // layout qualifiers specific to GS
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000199 VarArray fFSOutputs;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000200 SkString fVSCode;
201 SkString fGSCode;
202 SkString fFSCode;
203 bool fUsesGS;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000204
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000205private:
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000206 enum {
207 kNonStageIdx = -1,
208 };
209
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000210 const GrGLContextInfo& fContext;
211 GrGLUniformManager& fUniformManager;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000212 int fCurrentStage;
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000213 SkString fFSFunctions;
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000214
215 /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
216 //@{
217 GrSLType fTexCoordVaryingType; // the type, either Vec2f or Vec3f, of the coords passed
218 // as a varying from the VS to the FS.
219 SkString fDefaultTexCoordsName; // the name of the default 2D coords value.
220 //@}
221
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000222};
223
224#endif