OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
| 8 #include "gl/builders/GrGLProgramBuilder.h" |
8 #include "GrAARectRenderer.h" | 9 #include "GrAARectRenderer.h" |
9 #include "GrGpu.h" | 10 #include "GrGpu.h" |
10 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLEffect.h" |
11 #include "gl/GrGLShaderBuilder.h" | |
12 #include "gl/GrGLVertexEffect.h" | 12 #include "gl/GrGLVertexEffect.h" |
13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendEffectFactory.h" |
14 #include "SkColorPriv.h" | 14 #include "SkColorPriv.h" |
15 #include "effects/GrVertexEffect.h" | 15 #include "effects/GrVertexEffect.h" |
16 | 16 |
17 /////////////////////////////////////////////////////////////////////////////// | 17 /////////////////////////////////////////////////////////////////////////////// |
18 class GrGLAlignedRectEffect; | 18 class GrGLAlignedRectEffect; |
19 | 19 |
20 // Axis Aligned special case | 20 // Axis Aligned special case |
21 class GrAlignedRectEffect : public GrVertexEffect { | 21 class GrAlignedRectEffect : public GrVertexEffect { |
(...skipping 15 matching lines...) Expand all Loading... |
37 | 37 |
38 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 38 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
39 return GrTBackendEffectFactory<GrAlignedRectEffect>::getInstance(); | 39 return GrTBackendEffectFactory<GrAlignedRectEffect>::getInstance(); |
40 } | 40 } |
41 | 41 |
42 class GLEffect : public GrGLVertexEffect { | 42 class GLEffect : public GrGLVertexEffect { |
43 public: | 43 public: |
44 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) | 44 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
45 : INHERITED (factory) {} | 45 : INHERITED (factory) {} |
46 | 46 |
47 virtual void emitCode(GrGLFullShaderBuilder* builder, | 47 virtual void emitCode(GrGLFullProgramBuilder* builder, |
48 const GrDrawEffect& drawEffect, | 48 const GrDrawEffect& drawEffect, |
49 const GrEffectKey& key, | 49 const GrEffectKey& key, |
50 const char* outputColor, | 50 const char* outputColor, |
51 const char* inputColor, | 51 const char* inputColor, |
52 const TransformedCoordsArray&, | 52 const TransformedCoordsArray&, |
53 const TextureSamplerArray& samplers) SK_OVERRIDE { | 53 const TextureSamplerArray& samplers) SK_OVERRIDE { |
54 // setup the varying for the Axis aligned rect effect | 54 // setup the varying for the Axis aligned rect effect |
55 // xy -> interpolated offset | 55 // xy -> interpolated offset |
56 // zw -> w/2+0.5, h/2+0.5 | 56 // zw -> w/2+0.5, h/2+0.5 |
57 const char *vsRectName, *fsRectName; | 57 const char *vsRectName, *fsRectName; |
58 builder->addVarying(kVec4f_GrSLType, "Rect", &vsRectName, &fsRectNam
e); | 58 builder->addVarying(kVec4f_GrSLType, "Rect", &vsRectName, &fsRectNam
e); |
| 59 |
| 60 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder
(); |
59 const SkString* attr0Name = | 61 const SkString* attr0Name = |
60 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0]); | 62 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi
ces()[0]); |
61 builder->vsCodeAppendf("\t%s = %s;\n", vsRectName, attr0Name->c_str(
)); | 63 vsBuilder->codeAppendf("\t%s = %s;\n", vsRectName, attr0Name->c_str(
)); |
62 | 64 |
| 65 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui
lder(); |
63 // TODO: compute all these offsets, spans, and scales in the VS | 66 // TODO: compute all these offsets, spans, and scales in the VS |
64 builder->fsCodeAppendf("\tfloat insetW = min(1.0, %s.z) - 0.5;\n", f
sRectName); | 67 fsBuilder->codeAppendf("\tfloat insetW = min(1.0, %s.z) - 0.5;\n", f
sRectName); |
65 builder->fsCodeAppendf("\tfloat insetH = min(1.0, %s.w) - 0.5;\n", f
sRectName); | 68 fsBuilder->codeAppendf("\tfloat insetH = min(1.0, %s.w) - 0.5;\n", f
sRectName); |
66 builder->fsCodeAppend("\tfloat outset = 0.5;\n"); | 69 fsBuilder->codeAppend("\tfloat outset = 0.5;\n"); |
67 // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0
). For rects | 70 // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0
). For rects |
68 // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a
0 .. 1 range. | 71 // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a
0 .. 1 range. |
69 builder->fsCodeAppend("\tfloat spanW = insetW + outset;\n"); | 72 fsBuilder->codeAppend("\tfloat spanW = insetW + outset;\n"); |
70 builder->fsCodeAppend("\tfloat spanH = insetH + outset;\n"); | 73 fsBuilder->codeAppend("\tfloat spanH = insetH + outset;\n"); |
71 // For rects < 1 pixel wide or tall, these scale factors are used to
cap the maximum | 74 // For rects < 1 pixel wide or tall, these scale factors are used to
cap the maximum |
72 // value of coverage that is used. In other words it is the coverage
that is | 75 // value of coverage that is used. In other words it is the coverage
that is |
73 // used in the interior of the rect after the ramp. | 76 // used in the interior of the rect after the ramp. |
74 builder->fsCodeAppend("\tfloat scaleW = min(1.0, 2.0*insetW/spanW);\
n"); | 77 fsBuilder->codeAppend("\tfloat scaleW = min(1.0, 2.0*insetW/spanW);\
n"); |
75 builder->fsCodeAppend("\tfloat scaleH = min(1.0, 2.0*insetH/spanH);\
n"); | 78 fsBuilder->codeAppend("\tfloat scaleH = min(1.0, 2.0*insetH/spanH);\
n"); |
76 | 79 |
77 // Compute the coverage for the rect's width | 80 // Compute the coverage for the rect's width |
78 builder->fsCodeAppendf( | 81 fsBuilder->codeAppendf( |
79 "\tfloat coverage = scaleW*clamp((%s.z-abs(%s.x))/spanW, 0.0, 1.
0);\n", fsRectName, | 82 "\tfloat coverage = scaleW*clamp((%s.z-abs(%s.x))/spanW, 0.0, 1.
0);\n", fsRectName, |
80 fsRectName); | 83 fsRectName); |
81 // Compute the coverage for the rect's height and merge with the wid
th | 84 // Compute the coverage for the rect's height and merge with the wid
th |
82 builder->fsCodeAppendf( | 85 fsBuilder->codeAppendf( |
83 "\tcoverage = coverage*scaleH*clamp((%s.w-abs(%s.y))/spanH, 0.0,
1.0);\n", | 86 "\tcoverage = coverage*scaleH*clamp((%s.w-abs(%s.y))/spanH, 0.0,
1.0);\n", |
84 fsRectName, fsRectName); | 87 fsRectName, fsRectName); |
85 | 88 |
86 | 89 |
87 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, | 90 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, |
88 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("cover
age")).c_str()); | 91 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("cover
age")).c_str()); |
89 } | 92 } |
90 | 93 |
91 static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuil
der*) {} | 94 static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuil
der*) {} |
92 | 95 |
93 virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEf
fect&) SK_OVERRIDE {} | 96 virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEf
fect&) SK_OVERRIDE {} |
94 | 97 |
95 private: | 98 private: |
96 typedef GrGLVertexEffect INHERITED; | 99 typedef GrGLVertexEffect INHERITED; |
97 }; | 100 }; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 156 |
154 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 157 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
155 return GrTBackendEffectFactory<GrRectEffect>::getInstance(); | 158 return GrTBackendEffectFactory<GrRectEffect>::getInstance(); |
156 } | 159 } |
157 | 160 |
158 class GLEffect : public GrGLVertexEffect { | 161 class GLEffect : public GrGLVertexEffect { |
159 public: | 162 public: |
160 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) | 163 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
161 : INHERITED (factory) {} | 164 : INHERITED (factory) {} |
162 | 165 |
163 virtual void emitCode(GrGLFullShaderBuilder* builder, | 166 virtual void emitCode(GrGLFullProgramBuilder* builder, |
164 const GrDrawEffect& drawEffect, | 167 const GrDrawEffect& drawEffect, |
165 const GrEffectKey& key, | 168 const GrEffectKey& key, |
166 const char* outputColor, | 169 const char* outputColor, |
167 const char* inputColor, | 170 const char* inputColor, |
168 const TransformedCoordsArray&, | 171 const TransformedCoordsArray&, |
169 const TextureSamplerArray& samplers) SK_OVERRIDE { | 172 const TextureSamplerArray& samplers) SK_OVERRIDE { |
170 // setup the varying for the center point and the unit vector | 173 // setup the varying for the center point and the unit vector |
171 // that points down the height of the rect | 174 // that points down the height of the rect |
172 const char *vsRectEdgeName, *fsRectEdgeName; | 175 const char *vsRectEdgeName, *fsRectEdgeName; |
173 builder->addVarying(kVec4f_GrSLType, "RectEdge", | 176 builder->addVarying(kVec4f_GrSLType, "RectEdge", |
174 &vsRectEdgeName, &fsRectEdgeName); | 177 &vsRectEdgeName, &fsRectEdgeName); |
| 178 |
| 179 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder
(); |
175 const SkString* attr0Name = | 180 const SkString* attr0Name = |
176 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0]); | 181 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi
ces()[0]); |
177 builder->vsCodeAppendf("\t%s = %s;\n", vsRectEdgeName, attr0Name->c_
str()); | 182 vsBuilder->codeAppendf("\t%s = %s;\n", vsRectEdgeName, attr0Name->c_
str()); |
178 | 183 |
179 // setup the varying for width/2+.5 and height/2+.5 | 184 // setup the varying for width/2+.5 and height/2+.5 |
180 const char *vsWidthHeightName, *fsWidthHeightName; | 185 const char *vsWidthHeightName, *fsWidthHeightName; |
181 builder->addVarying(kVec2f_GrSLType, "WidthHeight", | 186 builder->addVarying(kVec2f_GrSLType, "WidthHeight", |
182 &vsWidthHeightName, &fsWidthHeightName); | 187 &vsWidthHeightName, &fsWidthHeightName); |
183 const SkString* attr1Name = | 188 const SkString* attr1Name = |
184 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[1]); | 189 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi
ces()[1]); |
185 builder->vsCodeAppendf("\t%s = %s;\n", vsWidthHeightName, attr1Name-
>c_str()); | 190 vsBuilder->codeAppendf("\t%s = %s;\n", vsWidthHeightName, attr1Name-
>c_str()); |
186 | 191 |
| 192 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui
lder(); |
187 // TODO: compute all these offsets, spans, and scales in the VS | 193 // TODO: compute all these offsets, spans, and scales in the VS |
188 builder->fsCodeAppendf("\tfloat insetW = min(1.0, %s.x) - 0.5;\n", f
sWidthHeightName); | 194 fsBuilder->codeAppendf("\tfloat insetW = min(1.0, %s.x) - 0.5;\n", f
sWidthHeightName); |
189 builder->fsCodeAppendf("\tfloat insetH = min(1.0, %s.y) - 0.5;\n", f
sWidthHeightName); | 195 fsBuilder->codeAppendf("\tfloat insetH = min(1.0, %s.y) - 0.5;\n", f
sWidthHeightName); |
190 builder->fsCodeAppend("\tfloat outset = 0.5;\n"); | 196 fsBuilder->codeAppend("\tfloat outset = 0.5;\n"); |
191 // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0
). For rects | 197 // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0
). For rects |
192 // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a
0 .. 1 range. | 198 // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a
0 .. 1 range. |
193 builder->fsCodeAppend("\tfloat spanW = insetW + outset;\n"); | 199 fsBuilder->codeAppend("\tfloat spanW = insetW + outset;\n"); |
194 builder->fsCodeAppend("\tfloat spanH = insetH + outset;\n"); | 200 fsBuilder->codeAppend("\tfloat spanH = insetH + outset;\n"); |
195 // For rects < 1 pixel wide or tall, these scale factors are used to
cap the maximum | 201 // For rects < 1 pixel wide or tall, these scale factors are used to
cap the maximum |
196 // value of coverage that is used. In other words it is the coverage
that is | 202 // value of coverage that is used. In other words it is the coverage
that is |
197 // used in the interior of the rect after the ramp. | 203 // used in the interior of the rect after the ramp. |
198 builder->fsCodeAppend("\tfloat scaleW = min(1.0, 2.0*insetW/spanW);\
n"); | 204 fsBuilder->codeAppend("\tfloat scaleW = min(1.0, 2.0*insetW/spanW);\
n"); |
199 builder->fsCodeAppend("\tfloat scaleH = min(1.0, 2.0*insetH/spanH);\
n"); | 205 fsBuilder->codeAppend("\tfloat scaleH = min(1.0, 2.0*insetH/spanH);\
n"); |
200 | 206 |
201 // Compute the coverage for the rect's width | 207 // Compute the coverage for the rect's width |
202 builder->fsCodeAppendf("\tvec2 offset = %s.xy - %s.xy;\n", | 208 fsBuilder->codeAppendf("\tvec2 offset = %s.xy - %s.xy;\n", |
203 builder->fragmentPosition(), fsRectEdgeName); | 209 fsBuilder->fragmentPosition(), fsRectEdgeName
); |
204 builder->fsCodeAppendf("\tfloat perpDot = abs(offset.x * %s.w - offs
et.y * %s.z);\n", | 210 fsBuilder->codeAppendf("\tfloat perpDot = abs(offset.x * %s.w - offs
et.y * %s.z);\n", |
205 fsRectEdgeName, fsRectEdgeName); | 211 fsRectEdgeName, fsRectEdgeName); |
206 builder->fsCodeAppendf( | 212 fsBuilder->codeAppendf( |
207 "\tfloat coverage = scaleW*clamp((%s.x-perpDot)/spanW, 0.0, 1.0)
;\n", | 213 "\tfloat coverage = scaleW*clamp((%s.x-perpDot)/spanW, 0.0, 1.0)
;\n", |
208 fsWidthHeightName); | 214 fsWidthHeightName); |
209 | 215 |
210 // Compute the coverage for the rect's height and merge with the wid
th | 216 // Compute the coverage for the rect's height and merge with the wid
th |
211 builder->fsCodeAppendf("\tperpDot = abs(dot(offset, %s.zw));\n", | 217 fsBuilder->codeAppendf("\tperpDot = abs(dot(offset, %s.zw));\n", |
212 fsRectEdgeName); | 218 fsRectEdgeName); |
213 builder->fsCodeAppendf( | 219 fsBuilder->codeAppendf( |
214 "\tcoverage = coverage*scaleH*clamp((%s.y-perpDot)/spanH, 0.
0, 1.0);\n", | 220 "\tcoverage = coverage*scaleH*clamp((%s.y-perpDot)/spanH, 0.
0, 1.0);\n", |
215 fsWidthHeightName); | 221 fsWidthHeightName); |
216 | 222 |
217 | 223 |
218 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, | 224 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, |
219 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("cover
age")).c_str()); | 225 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("cover
age")).c_str()); |
220 } | 226 } |
221 | 227 |
222 static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuil
der*) {} | 228 static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuil
der*) {} |
223 | 229 |
224 virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEf
fect&) SK_OVERRIDE {} | 230 virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEf
fect&) SK_OVERRIDE {} |
225 | 231 |
226 private: | 232 private: |
227 typedef GrGLVertexEffect INHERITED; | 233 typedef GrGLVertexEffect INHERITED; |
228 }; | 234 }; |
(...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 // can't call mapRect for devInside since it calls sort | 949 // can't call mapRect for devInside since it calls sort |
944 combinedMatrix.mapPoints((SkPoint*)&devInside, (const SkPoint*)&rects[1], 2)
; | 950 combinedMatrix.mapPoints((SkPoint*)&devInside, (const SkPoint*)&rects[1], 2)
; |
945 | 951 |
946 if (devInside.isEmpty()) { | 952 if (devInside.isEmpty()) { |
947 this->fillAARect(gpu, target, devOutside, SkMatrix::I(), devOutside); | 953 this->fillAARect(gpu, target, devOutside, SkMatrix::I(), devOutside); |
948 return; | 954 return; |
949 } | 955 } |
950 | 956 |
951 this->geometryStrokeAARect(gpu, target, devOutside, devOutsideAssist, devIns
ide, true); | 957 this->geometryStrokeAARect(gpu, target, devOutside, devOutsideAssist, devIns
ide, true); |
952 } | 958 } |
OLD | NEW |