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