| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1 | /* |
| 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.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 14 | #include "gl/GrGLUniformManager.h" |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 15 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 16 | class GrGLContextInfo; |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 17 | |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 18 | /** |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 19 | Contains all the incremental state of a shader as it is being built,as well as helpers to |
| 20 | manipulate that state. |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 21 | */ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 22 | class GrGLShaderBuilder { |
| 23 | |
| 24 | public: |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 25 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 26 | enum ShaderType { |
| 27 | kVertex_ShaderType = 0x1, |
| 28 | kGeometry_ShaderType = 0x2, |
| 29 | kFragment_ShaderType = 0x4, |
| 30 | }; |
| 31 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 32 | GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&); |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 33 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 34 | void computeSwizzle(uint32_t configFlags); |
| 35 | void computeModulate(const char* fsInColor); |
| 36 | |
| tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 37 | // 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.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 44 | /** 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.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 47 | void setupTextureAccess(SamplerMode samplerMode, int stageNum); |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 48 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 49 | /** texture2D(samplerName, coordName), with projection if necessary; if coordName is not |
| 50 | specified, uses fSampleCoords. */ |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 51 | void emitTextureLookup(const char* samplerName, |
| 52 | const char* coordName = NULL); |
| 53 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 54 | /** sets outColor to results of texture lookup, with swizzle, and/or modulate as necessary */ |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 55 | void emitDefaultFetch(const char* outColor, |
| 56 | const char* samplerName); |
| 57 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 58 | /** Add a uniform variable to the current program, that has visibilty in one or more shaders. |
| 59 | If stageNum is specified, it is appended to the name to guarantee uniqueness; if count is |
| 60 | specified, the uniform is an array. visibility is a bitfield of ShaderType values indicating |
| 61 | from which shaders the uniform should be accessible. At least one bit must be set. Geometry |
| 62 | shader uniforms are not supported at this time. |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 63 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 64 | GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
| 65 | GrSLType type, |
| 66 | const char* name, |
| 67 | int stageNum = -1, |
| 68 | int count = GrGLShaderVar::kNonArray); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 69 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 70 | const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) const; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Shorcut for getUniformVariable(u).c_str() |
| 74 | */ |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 75 | const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 76 | return this->getUniformVariable(u).c_str(); |
| 77 | } |
| tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 78 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 79 | /** Add a varying variable to the current program to pass values between vertex and fragment |
| 80 | shaders. If the last two parameters are non-NULL, they are filled in with the name |
| 81 | generated. */ |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 82 | void addVarying(GrSLType type, |
| 83 | const char* name, |
| 84 | const char** vsOutName = NULL, |
| 85 | const char** fsInName = NULL); |
| 86 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 87 | /** Add a varying variable to the current program to pass values between vertex and fragment |
| 88 | shaders; stageNum is appended to the name to guarantee uniqueness. If the last two |
| 89 | parameters are non-NULL, they are filled in with the name generated. */ |
| tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 90 | void addVarying(GrSLType type, |
| 91 | const char* name, |
| 92 | int stageNum, |
| 93 | const char** vsOutName = NULL, |
| 94 | const char** fsInName = NULL); |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 95 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 96 | /** Called after building is complete to get the final shader string. */ |
| 97 | void getShader(ShaderType, SkString*) const; |
| 98 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 99 | /** |
| 100 | * TODO: Make this do all the compiling, linking, etc. Hide this from the custom stages |
| 101 | */ |
| 102 | void finished(GrGLuint programID); |
| 103 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 104 | private: |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 105 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 106 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 107 | |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 108 | void appendDecls(const VarArray&, SkString*) const; |
| 109 | void appendUniformDecls(ShaderType, SkString*) const; |
| 110 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 111 | typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
| 112 | GrGLUniformManager::BuilderUniformArray fUniforms; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 113 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 114 | // TODO: Everything below here private. |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 115 | public: |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 116 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 117 | SkString fHeader; // VS+FS, GLSL version, etc |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 118 | VarArray fVSAttrs; |
| 119 | VarArray fVSOutputs; |
| 120 | VarArray fGSInputs; |
| 121 | VarArray fGSOutputs; |
| 122 | VarArray fFSInputs; |
| 123 | SkString fGSHeader; // layout qualifiers specific to GS |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 124 | VarArray fFSOutputs; |
| 125 | SkString fFSFunctions; |
| 126 | SkString fVSCode; |
| 127 | SkString fGSCode; |
| 128 | SkString fFSCode; |
| 129 | bool fUsesGS; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 130 | |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 131 | /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode(). |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 132 | //@{ |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 133 | |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 134 | int fVaryingDims; |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 135 | static const int fCoordDims = 2; |
| 136 | |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 137 | /// True if fSampleCoords is an expression; false if it's a bare |
| 138 | /// variable name |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 139 | bool fComplexCoord; |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 140 | SkString fSampleCoords; |
| tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 141 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 142 | SkString fSwizzle; |
| 143 | SkString fModulate; |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 144 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 145 | SkString fTexFunc; |
| tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 146 | |
| tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 147 | //@} |
| tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 148 | |
| bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 149 | private: |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 150 | const GrGLContextInfo& fContext; |
| 151 | GrGLUniformManager& fUniformManager; |
| tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | #endif |