blob: f1106286277408229f532727fda9f9839f5940b4 [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"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014#include "gl/GrGLUniformManager.h"
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000015
bsalomon@google.comad5e9372012-07-11 18:11:27 +000016class GrGLContextInfo;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000017
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000018/**
bsalomon@google.comeb715c82012-07-11 15:03:31 +000019 Contains all the incremental state of a shader as it is being built,as well as helpers to
20 manipulate that state.
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000021*/
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000022class GrGLShaderBuilder {
23
24public:
bsalomon@google.com032b2212012-07-16 13:36:18 +000025
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
tomhudson@google.com5440f062012-06-01 15:55:50 +000037 // TODO: needs a better name
38 enum SamplerMode {
39 kDefault_SamplerMode,
40 kProj_SamplerMode,
41 kExplicitDivide_SamplerMode // must do an explicit divide
42 };
43
bsalomon@google.comeb715c82012-07-11 15:03:31 +000044 /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide
45 is required for the sample coordinates, creates the new variable and emits the code to
46 initialize it. */
tomhudson@google.com5440f062012-06-01 15:55:50 +000047 void setupTextureAccess(SamplerMode samplerMode, int stageNum);
tomhudson@google.com52598142012-05-24 17:44:30 +000048
bsalomon@google.comeb715c82012-07-11 15:03:31 +000049 /** texture2D(samplerName, coordName), with projection if necessary; if coordName is not
50 specified, uses fSampleCoords. */
tomhudson@google.com52598142012-05-24 17:44:30 +000051 void emitTextureLookup(const char* samplerName,
52 const char* coordName = NULL);
53
bsalomon@google.comeb715c82012-07-11 15:03:31 +000054 /** sets outColor to results of texture lookup, with swizzle, and/or modulate as necessary */
tomhudson@google.com52598142012-05-24 17:44:30 +000055 void emitDefaultFetch(const char* outColor,
56 const char* samplerName);
57
bsalomon@google.comeb715c82012-07-11 15:03:31 +000058 /** Add a uniform variable to the current program, that has visibilty in one or more shaders.
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000059 visibility is a bitfield of ShaderType values indicating from which shaders the uniform
60 should be accessible. At least one bit must be set. Geometry shader uniforms are not
61 supported at this time. The actual uniform name will be mangled. If outName is not NULL then
62 it will refer to the final uniform name after return. Use the addUniformArray variant to add
63 an array of uniforms.
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000064 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000065 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility,
66 GrSLType type,
67 const char* name,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000068 const char** outName = NULL) {
69 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
70 }
71 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility,
72 GrSLType type,
73 const char* name,
74 int arrayCount,
75 const char** outName = NULL);
bsalomon@google.com032b2212012-07-16 13:36:18 +000076
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000077 const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const;
bsalomon@google.com032b2212012-07-16 13:36:18 +000078
79 /**
80 * Shorcut for getUniformVariable(u).c_str()
81 */
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000082 const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const {
bsalomon@google.com032b2212012-07-16 13:36:18 +000083 return this->getUniformVariable(u).c_str();
84 }
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000085
bsalomon@google.comeb715c82012-07-11 15:03:31 +000086 /** Add a varying variable to the current program to pass values between vertex and fragment
87 shaders. If the last two parameters are non-NULL, they are filled in with the name
88 generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +000089 void addVarying(GrSLType type,
90 const char* name,
91 const char** vsOutName = NULL,
92 const char** fsInName = NULL);
93
bsalomon@google.comad5e9372012-07-11 18:11:27 +000094 /** Called after building is complete to get the final shader string. */
95 void getShader(ShaderType, SkString*) const;
96
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000097 /**
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000098 * TODO: Make this do all the compiling, linking, etc. Hide from the custom stages
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000099 */
100 void finished(GrGLuint programID);
101
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000102 /**
103 * Sets the current stage (used to make variable names unique).
104 * TODO: Hide from the custom stages
105 */
106 void setCurrentStage(int stage) { fCurrentStage = stage; }
107 void setNonStage() { fCurrentStage = kNonStageIdx; }
108
bsalomon@google.com032b2212012-07-16 13:36:18 +0000109private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000110
bsalomon@google.com032b2212012-07-16 13:36:18 +0000111 typedef GrTAllocator<GrGLShaderVar> VarArray;
112
bsalomon@google.com032b2212012-07-16 13:36:18 +0000113 void appendDecls(const VarArray&, SkString*) const;
114 void appendUniformDecls(ShaderType, SkString*) const;
115
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000116 typedef GrGLUniformManager::BuilderUniform BuilderUniform;
117 GrGLUniformManager::BuilderUniformArray fUniforms;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000118
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000119 // TODO: Everything below here private.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000120public:
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000121
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000122 SkString fHeader; // VS+FS, GLSL version, etc
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000123 VarArray fVSAttrs;
124 VarArray fVSOutputs;
125 VarArray fGSInputs;
126 VarArray fGSOutputs;
127 VarArray fFSInputs;
128 SkString fGSHeader; // layout qualifiers specific to GS
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000129 VarArray fFSOutputs;
130 SkString fFSFunctions;
131 SkString fVSCode;
132 SkString fGSCode;
133 SkString fFSCode;
134 bool fUsesGS;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000135
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000136 /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000137 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000138
tomhudson@google.com52598142012-05-24 17:44:30 +0000139 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000140 static const int fCoordDims = 2;
141
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000142 /// True if fSampleCoords is an expression; false if it's a bare
143 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000144 bool fComplexCoord;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000145 SkString fSampleCoords;
tomhudson@google.com52598142012-05-24 17:44:30 +0000146
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000147 SkString fSwizzle;
148 SkString fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000149
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000150 SkString fTexFunc;
tomhudson@google.com5440f062012-06-01 15:55:50 +0000151
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000152 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000153
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000154private:
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000155 enum {
156 kNonStageIdx = -1,
157 };
158
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000159 const GrGLContextInfo& fContext;
160 GrGLUniformManager& fUniformManager;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000161 int fCurrentStage;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000162};
163
164#endif