blob: 66de5abdcc3e6cad3a4eca2ca037b2eccdf17912 [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 {
24
25public:
bsalomon@google.comeb715c82012-07-11 15:03:31 +000026 enum ShaderType {
27 kVertex_ShaderType = 0x1,
28 kGeometry_ShaderType = 0x2,
29 kFragment_ShaderType = 0x4,
30 };
31
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000032 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&);
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000033
tomhudson@google.com52598142012-05-24 17:44:30 +000034 void computeSwizzle(uint32_t configFlags);
35 void computeModulate(const char* fsInColor);
36
bsalomon@google.comeb715c82012-07-11 15:03:31 +000037 /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide
38 is required for the sample coordinates, creates the new variable and emits the code to
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000039 initialize it. This should only be called by GrGLProgram.*/
40 void setupTextureAccess(const char* varyingFSName, GrSLType varyingType);
tomhudson@google.com52598142012-05-24 17:44:30 +000041
bsalomon@google.comeb715c82012-07-11 15:03:31 +000042 /** texture2D(samplerName, coordName), with projection if necessary; if coordName is not
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000043 specified, uses fSampleCoords. coordType must either be Vec2f or Vec3f. The latter is
44 interpreted as projective texture coords. */
tomhudson@google.com52598142012-05-24 17:44:30 +000045 void emitTextureLookup(const char* samplerName,
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000046 const char* coordName = NULL,
47 GrSLType coordType = kVec2f_GrSLType);
tomhudson@google.com52598142012-05-24 17:44:30 +000048
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000049 /** sets outColor to results of texture lookup, with swizzle, and/or modulate as necessary. If
50 coordName is NULL then it as if defaultTexCoordsName() was passed. coordType must be either
51 kVec2f or kVec3f. */
52 void emitTextureLookupAndModulate(const char* outColor,
53 const char* samplerName,
54 const char* coordName = NULL,
55 GrSLType coordType = kVec2f_GrSLType);
56
57 /** Gets the name of the default texture coords which are always kVec2f */
58 const char* defaultTexCoordsName() const { return fDefaultTexCoordsName.c_str(); }
59
60 /* Returns true if the texture matrix from which the default texture coords are computed has
61 perspective. */
62 bool defaultTextureMatrixIsPerspective() const {
63 return fTexCoordVaryingType == kVec3f_GrSLType;
64 }
tomhudson@google.com52598142012-05-24 17:44:30 +000065
twiz@google.coma5e65ec2012-08-02 15:15:16 +000066 /** Emits a texture lookup to the shader code with the form:
67 texture2D{Proj}(samplerName, coordName).swizzle
68 The routine selects the type of texturing based on samplerMode.
69 The generated swizzle state is built based on the format of the texture and the requested
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000070 swizzle access pattern. coordType must either be Vec2f or Vec3f. The latter is interpreted
71 as projective texture coords.*/
72 void emitCustomTextureLookup(const GrTextureAccess& textureAccess,
twiz@google.coma5e65ec2012-08-02 15:15:16 +000073 const char* samplerName,
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000074 const char* coordName,
75 GrSLType coordType = kVec2f_GrSLType);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000076
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000077 /** Emits a helper function outside of main(). Currently ShaderType must be
78 kFragment_ShaderType. */
79 void emitFunction(ShaderType shader,
80 GrSLType returnType,
81 const char* name,
82 int argCnt,
83 const GrGLShaderVar* args,
84 const char* body,
85 SkString* outName);
86
twiz@google.coma5e65ec2012-08-02 15:15:16 +000087 /** Generates a StageKey for the shader code based on the texture access parameters and the
88 capabilities of the GL context. This is useful for keying the shader programs that may
89 have multiple representations, based on the type/format of textures used. */
90 static GrCustomStage::StageKey KeyForTextureAccess(const GrTextureAccess& access,
91 const GrGLCaps& caps);
92
bsalomon@google.comeb715c82012-07-11 15:03:31 +000093 /** Add a uniform variable to the current program, that has visibilty in one or more shaders.
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000094 visibility is a bitfield of ShaderType values indicating from which shaders the uniform
95 should be accessible. At least one bit must be set. Geometry shader uniforms are not
96 supported at this time. The actual uniform name will be mangled. If outName is not NULL then
97 it will refer to the final uniform name after return. Use the addUniformArray variant to add
98 an array of uniforms.
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000099 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000100 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility,
101 GrSLType type,
102 const char* name,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000103 const char** outName = NULL) {
104 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
105 }
106 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility,
107 GrSLType type,
108 const char* name,
109 int arrayCount,
110 const char** outName = NULL);
bsalomon@google.com032b2212012-07-16 13:36:18 +0000111
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000112 const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000113
114 /**
115 * Shorcut for getUniformVariable(u).c_str()
116 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000117 const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000118 return this->getUniformVariable(u).c_str();
119 }
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000120
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000121 /** Add a varying variable to the current program to pass values between vertex and fragment
122 shaders. If the last two parameters are non-NULL, they are filled in with the name
123 generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000124 void addVarying(GrSLType type,
125 const char* name,
126 const char** vsOutName = NULL,
127 const char** fsInName = NULL);
128
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000129 /** Called after building is complete to get the final shader string. */
130 void getShader(ShaderType, SkString*) const;
131
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000132 /**
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000133 * TODO: Make this do all the compiling, linking, etc. Hide from the custom stages
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000134 */
135 void finished(GrGLuint programID);
136
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000137 /**
138 * Sets the current stage (used to make variable names unique).
139 * TODO: Hide from the custom stages
140 */
141 void setCurrentStage(int stage) { fCurrentStage = stage; }
142 void setNonStage() { fCurrentStage = kNonStageIdx; }
143
bsalomon@google.com032b2212012-07-16 13:36:18 +0000144private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000145
bsalomon@google.com032b2212012-07-16 13:36:18 +0000146 typedef GrTAllocator<GrGLShaderVar> VarArray;
147
bsalomon@google.com032b2212012-07-16 13:36:18 +0000148 void appendDecls(const VarArray&, SkString*) const;
149 void appendUniformDecls(ShaderType, SkString*) const;
150
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000151 typedef GrGLUniformManager::BuilderUniform BuilderUniform;
152 GrGLUniformManager::BuilderUniformArray fUniforms;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000153
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000154 // TODO: Everything below here private.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000155public:
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000156
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000157 SkString fHeader; // VS+FS, GLSL version, etc
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000158 VarArray fVSAttrs;
159 VarArray fVSOutputs;
160 VarArray fGSInputs;
161 VarArray fGSOutputs;
162 VarArray fFSInputs;
163 SkString fGSHeader; // layout qualifiers specific to GS
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000164 VarArray fFSOutputs;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000165 SkString fVSCode;
166 SkString fGSCode;
167 SkString fFSCode;
168 bool fUsesGS;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000169
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000170 /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000171 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000172
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000173 SkString fSwizzle;
174 SkString fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000175
176 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000177
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000178private:
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000179 enum {
180 kNonStageIdx = -1,
181 };
182
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000183 const GrGLContextInfo& fContext;
184 GrGLUniformManager& fUniformManager;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000185 int fCurrentStage;
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000186 SkString fFSFunctions;
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000187
188 /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
189 //@{
190 GrSLType fTexCoordVaryingType; // the type, either Vec2f or Vec3f, of the coords passed
191 // as a varying from the VS to the FS.
192 SkString fDefaultTexCoordsName; // the name of the default 2D coords value.
193 //@}
194
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000195};
196
197#endif