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