OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2014 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 "gl/GrGLProgramDesc.h" | |
12 #include "gl/GrGLProgramEffects.h" | |
13 #include "gl/GrGLSL.h" | |
14 #include "gl/GrGLProgramDataManager.h" | |
15 #include "GrAllocator.h" | |
16 #include "GrBackendEffectFactory.h" | |
17 #include "GrColor.h" | |
18 #include "GrEffect.h" | |
19 #include "SkTypes.h" | |
20 | |
21 #include <stdarg.h> | |
22 | |
23 class GrGLContextInfo; | |
24 class GrEffectStage; | |
25 class GrGLProgramDesc; | |
26 class GrGLProgramBuilder; | |
27 class GrGLFullProgramBuilder; | |
28 | |
29 /** | |
30 base class for all shaders builders | |
31 */ | |
32 class GrGLShaderBuilder { | |
33 public: | |
34 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; | |
35 typedef GrGLProgramEffects::TextureSampler TextureSampler; | |
36 GrGLShaderBuilder(GrGLProgramBuilder* program); | |
37 | |
38 void addInput(GrGLShaderVar i) { fInputs.push_back(i); } | |
39 void addOutput(GrGLShaderVar i) { fOutputs.push_back(i); } | |
40 | |
41 /* | |
42 * We put texture lookups in the base class because it is TECHNICALLY possib le to do texture | |
43 * lookups in any kind of shader. However, for the time being using these c alls on non-fragment | |
44 * shaders will result in a shader compilation error as texture sampler unif orms are only | |
45 * visible to the fragment shader. It would not be hard to change this beha vior, if someone | |
46 * actually wants to do texture lookups in a non-fragment shader | |
47 * | |
48 * TODO if append texture lookup is used on a non-fragment shader, sampler u niforms should be | |
49 * made visible to that shaders | |
50 */ | |
51 /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or | |
52 Vec3f. The latter is interpreted as projective texture coords. The vec l ength and swizzle | |
53 order of the result depends on the GrTextureAccess associated with the T extureSampler. */ | |
54 void appendTextureLookup(SkString* out, | |
55 const TextureSampler&, | |
56 const char* coordName, | |
57 GrSLType coordType = kVec2f_GrSLType) const; | |
58 | |
59 /** Version of above that appends the result to the fragment shader code ins tead.*/ | |
60 void appendTextureLookup(const TextureSampler&, | |
61 const char* coordName, | |
62 GrSLType coordType = kVec2f_GrSLType); | |
63 | |
64 | |
65 /** Does the work of appendTextureLookup and modulates the result by modulat ion. The result is | |
66 always a vec4. modulation and the swizzle specified by TextureSampler mu st both be vec4 or | |
67 float. If modulation is "" or NULL it this function acts as though appen dTextureLookup were | |
68 called. */ | |
69 void appendTextureLookupAndModulate(const char* modulation, | |
70 const TextureSampler&, | |
71 const char* coordName, | |
72 GrSLType coordType = kVec2f_GrSLType); | |
73 | |
74 /** If texture swizzling is available using tex parameters then it is prefer red over mangling | |
75 the generated shader code. This potentially allows greater reuse of cach ed shaders. */ | |
76 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa ps& caps); | |
77 | |
78 /** | |
79 * Called by GrGLEffects to add code to one of the shaders. | |
80 */ | |
81 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { | |
82 va_list args; | |
83 va_start(args, format); | |
84 fCode.appendVAList(format, args); | |
85 va_end(args); | |
86 } | |
87 | |
88 void codeAppend(const char* str) { fCode.append(str); } | |
89 | |
90 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { | |
91 va_list args; | |
92 va_start(args, format); | |
93 fCode.prependVAList(format, args); | |
94 va_end(args); | |
95 } | |
96 | |
97 /** Emits a helper function outside of main() in the fragment shader. */ | |
98 void emitFunction(GrSLType returnType, | |
99 const char* name, | |
100 int argCnt, | |
101 const GrGLShaderVar* args, | |
102 const char* body, | |
103 SkString* outName); | |
104 | |
105 /* | |
106 * Get parent builder for adding uniforms | |
107 */ | |
108 GrGLProgramBuilder* getProgramBuilder() { return fProgram; } | |
109 | |
110 /** | |
111 * Helper for begining and ending a block in the fragment code. | |
112 */ | |
113 class ShaderBlock { | |
114 public: | |
115 ShaderBlock(GrGLShaderBuilder* builder) : fBuilder(builder) { | |
116 SkASSERT(NULL != builder); | |
117 fBuilder->codeAppend("{"); | |
118 } | |
119 | |
120 ~ShaderBlock() { | |
121 fBuilder->codeAppend("}"); | |
122 } | |
123 private: | |
124 GrGLShaderBuilder* fBuilder; | |
125 }; | |
126 protected: | |
127 | |
128 /* | |
129 * this super low level function is just for use internally to builders | |
130 */ | |
131 void appendTextureLookup(const char* samplerName, | |
132 const char* coordName, | |
133 uint32_t configComponentMask, | |
134 const char* swizzle); | |
135 | |
136 /* | |
137 * A general function which enables an extension in a shader if the feature bit is not present | |
138 */ | |
139 void addFeature(uint32_t featureBit, const char* extensionName); | |
140 | |
141 typedef GrTAllocator<GrGLShaderVar> VarArray; | |
142 | |
143 GrGLProgramBuilder* fProgram; | |
144 | |
145 SkString fCode; | |
146 SkString fFunctions; | |
147 SkString fExtensions; | |
148 | |
149 VarArray fInputs; | |
150 VarArray fOutputs; | |
151 uint32_t fFeaturesAddedMask; | |
152 }; | |
153 | |
154 | |
155 /* | |
156 * Full Shader builder is the base class for shaders which are only accessible t hrough full program | |
157 * builder, ie vertex, geometry, and later TCU / TES. Using this base class, th ey can access the | |
158 * full program builder functionality through the full program pointer | |
159 */ | |
160 class GrGLFullShaderBuilder : public GrGLShaderBuilder { | |
161 public: | |
162 GrGLFullShaderBuilder(GrGLFullProgramBuilder* program); | |
163 | |
164 GrGLFullProgramBuilder* fullProgram() { return fFullProgram; } | |
bsalomon
2014/08/21 20:21:01
How about getFullProgramBuilder()? It's more a bu
| |
165 protected: | |
166 GrGLFullProgramBuilder* fFullProgram; | |
167 private: | |
168 typedef GrGLShaderBuilder INHERITED; | |
169 }; | |
170 #endif | |
OLD | NEW |