OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 | 8 |
9 #include "SkLights.h" | 9 #include "SkLights.h" |
10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
11 #include "SkShadowShader.h" | 11 #include "SkShadowShader.h" |
12 #include "SkPoint3.h" | 12 #include "SkPoint3.h" |
13 | 13 |
14 //////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////// |
15 #ifdef SK_EXPERIMENTAL_SHADOWING | 15 #ifdef SK_EXPERIMENTAL_SHADOWING |
16 | 16 |
17 | 17 |
18 /** \class SkShadowShaderImpl | 18 /** \class SkShadowShaderImpl |
19 This subclass of shader applies shadowing | 19 This subclass of shader applies shadowing |
20 */ | 20 */ |
21 class SkShadowShaderImpl : public SkShader { | 21 class SkShadowShaderImpl : public SkShader { |
22 public: | 22 public: |
23 /** Create a new shadowing shader that shadows | 23 /** Create a new shadowing shader that shadows |
24 @param to do to do | 24 @param to do to do |
25 */ | 25 */ |
26 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, | 26 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, |
27 sk_sp<SkShader> diffuseShader, | 27 sk_sp<SkShader> diffuseShader, |
28 sk_sp<SkLights> lights, | 28 sk_sp<SkLights> lights, |
29 int diffuseWidth, int diffuseHeight) | 29 int diffuseWidth, int diffuseHeight, |
30 SkShadowType sType) | |
30 : fPovDepthShader(std::move(povDepthShader)) | 31 : fPovDepthShader(std::move(povDepthShader)) |
31 , fDiffuseShader(std::move(diffuseShader)) | 32 , fDiffuseShader(std::move(diffuseShader)) |
32 , fLights(std::move(lights)) | 33 , fLights(std::move(lights)) |
33 , fDiffuseWidth(diffuseWidth) | 34 , fDiffuseWidth(diffuseWidth) |
34 , fDiffuseHeight(diffuseHeight) { } | 35 , fDiffuseHeight(diffuseHeight) |
36 , fShType(sType) { } | |
35 | 37 |
36 bool isOpaque() const override; | 38 bool isOpaque() const override; |
37 | 39 |
38 #if SK_SUPPORT_GPU | 40 #if SK_SUPPORT_GPU |
39 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de; | 41 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de; |
40 #endif | 42 #endif |
41 | 43 |
42 class ShadowShaderContext : public SkShader::Context { | 44 class ShadowShaderContext : public SkShader::Context { |
43 public: | 45 public: |
44 // The context takes ownership of the states. It will call their destruc tors | 46 // The context takes ownership of the states. It will call their destruc tors |
(...skipping 28 matching lines...) Expand all Loading... | |
73 Context* onCreateContext(const ContextRec&, void*) const override; | 75 Context* onCreateContext(const ContextRec&, void*) const override; |
74 | 76 |
75 private: | 77 private: |
76 sk_sp<SkShader> fPovDepthShader; | 78 sk_sp<SkShader> fPovDepthShader; |
77 sk_sp<SkShader> fDiffuseShader; | 79 sk_sp<SkShader> fDiffuseShader; |
78 sk_sp<SkLights> fLights; | 80 sk_sp<SkLights> fLights; |
79 | 81 |
80 int fDiffuseWidth; | 82 int fDiffuseWidth; |
81 int fDiffuseHeight; | 83 int fDiffuseHeight; |
82 | 84 |
85 SkShadowType fShType; | |
86 | |
83 friend class SkShadowShader; | 87 friend class SkShadowShader; |
84 | 88 |
85 typedef SkShader INHERITED; | 89 typedef SkShader INHERITED; |
86 }; | 90 }; |
87 | 91 |
88 //////////////////////////////////////////////////////////////////////////// | 92 //////////////////////////////////////////////////////////////////////////// |
89 | 93 |
90 #if SK_SUPPORT_GPU | 94 #if SK_SUPPORT_GPU |
91 | 95 |
92 #include "GrCoordTransform.h" | 96 #include "GrCoordTransform.h" |
93 #include "GrFragmentProcessor.h" | 97 #include "GrFragmentProcessor.h" |
94 #include "GrInvariantOutput.h" | 98 #include "GrInvariantOutput.h" |
95 #include "glsl/GrGLSLFragmentProcessor.h" | 99 #include "glsl/GrGLSLFragmentProcessor.h" |
96 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 100 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
97 #include "SkGr.h" | 101 #include "SkGr.h" |
98 #include "SkGrPriv.h" | 102 #include "SkGrPriv.h" |
99 #include "SkSpecialImage.h" | 103 #include "SkSpecialImage.h" |
100 #include "SkImage_Base.h" | 104 #include "SkImage_Base.h" |
101 #include "GrContext.h" | 105 #include "GrContext.h" |
102 | 106 |
103 class ShadowFP : public GrFragmentProcessor { | 107 class ShadowFP : public GrFragmentProcessor { |
104 public: | 108 public: |
105 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, | 109 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, |
106 sk_sp<GrFragmentProcessor> diffuse, | 110 sk_sp<GrFragmentProcessor> diffuse, |
107 sk_sp<SkLights> lights, | 111 sk_sp<SkLights> lights, |
108 int diffuseWidth, int diffuseHeight, | 112 int diffuseWidth, int diffuseHeight, |
113 SkShadowType sType, | |
109 GrContext* context) { | 114 GrContext* context) { |
110 | 115 |
111 // fuse all ambient lights into a single one | 116 // fuse all ambient lights into a single one |
112 fAmbientColor.set(0.0f, 0.0f, 0.0f); | 117 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
113 | 118 |
114 fNumDirLights = 0; // refers to directional lights. | 119 fNumDirLights = 0; // refers to directional lights. |
115 for (int i = 0; i < lights->numLights(); ++i) { | 120 for (int i = 0; i < lights->numLights(); ++i) { |
116 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { | 121 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { |
117 fAmbientColor += lights->light(i).color(); | 122 fAmbientColor += lights->light(i).color(); |
118 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { | 123 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { |
(...skipping 11 matching lines...) Expand all Loading... | |
130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); | 135 fDepthMapHeight[fNumDirLights] = shadowMap->height(); |
131 fDepthMapWidth[fNumDirLights] = shadowMap->width(); | 136 fDepthMapWidth[fNumDirLights] = shadowMap->width(); |
132 | 137 |
133 fNumDirLights++; | 138 fNumDirLights++; |
134 } | 139 } |
135 } | 140 } |
136 | 141 |
137 fWidth = diffuseWidth; | 142 fWidth = diffuseWidth; |
138 fHeight = diffuseHeight; | 143 fHeight = diffuseHeight; |
139 | 144 |
145 fShType = sType; | |
146 | |
147 | |
148 // printf("init: %d\n", sType.fBlurAlgorithm); | |
jvanverth1
2016/08/12 17:39:09
Remove this or put in #ifdef block (e.g. #ifdef DU
vjiaoblack
2016/08/12 19:07:42
Thanks! Done.
| |
149 | |
140 this->registerChildProcessor(std::move(povDepth)); | 150 this->registerChildProcessor(std::move(povDepth)); |
141 this->registerChildProcessor(std::move(diffuse)); | 151 this->registerChildProcessor(std::move(diffuse)); |
142 this->initClassID<ShadowFP>(); | 152 this->initClassID<ShadowFP>(); |
143 } | 153 } |
144 | 154 |
145 class GLSLShadowFP : public GrGLSLFragmentProcessor { | 155 class GLSLShadowFP : public GrGLSLFragmentProcessor { |
146 public: | 156 public: |
147 GLSLShadowFP() { } | 157 GLSLShadowFP() { } |
148 | 158 |
149 void emitCode(EmitArgs& args) override { | 159 void emitCode(EmitArgs& args) override { |
150 | 160 |
151 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 161 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 162 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
153 | 163 |
154 // add uniforms | 164 // add uniforms |
155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | 165 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; |
156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); | 166 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); |
157 | 167 |
168 int blurAlgorithm = args.fFp.cast<ShadowFP>().fShType.fBlurAlgorithm ; | |
169 | |
158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 170 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 171 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
160 | 172 |
161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] | 173 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] |
162 = {nullptr}; | 174 = {nullptr}; |
163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] | 175 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] |
164 = {nullptr}; | 176 = {nullptr}; |
165 | 177 |
166 SkString lightDirUniNameBase("lightDir"); | 178 SkString lightDirUniNameBase("lightDir"); |
167 SkString lightColorUniNameBase("lightColor"); | 179 SkString lightColorUniNameBase("lightColor"); |
(...skipping 28 matching lines...) Expand all Loading... | |
196 kDefault_GrSLPrecision, | 208 kDefault_GrSLPrecision, |
197 depthMapWidthUniNameStr.c_str (), | 209 depthMapWidthUniNameStr.c_str (), |
198 &depthMapWidthUniName[i]); | 210 &depthMapWidthUniName[i]); |
199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 211 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
200 kInt_GrSLType, | 212 kInt_GrSLType, |
201 kDefault_GrSLPrecision, | 213 kDefault_GrSLPrecision, |
202 depthMapHeightUniNameStr.c_st r(), | 214 depthMapHeightUniNameStr.c_st r(), |
203 &depthMapHeightUniName[i]); | 215 &depthMapHeightUniName[i]); |
204 } | 216 } |
205 | 217 |
218 const char* shBiasUniName = nullptr; | |
219 const char* minVarianceUniName = nullptr; | |
220 | |
221 fBiasingConstantUni = uniformHandler->addUniform(kFragment_GrShaderF lag, | |
222 kFloat_GrSLType, | |
223 kDefault_GrSLPrecis ion, | |
224 "shadowBias", &shBi asUniName); | |
225 fMinVarianceUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | |
226 kFloat_GrSLType, | |
227 kDefault_GrSLPrecision, | |
228 "minVariance", &minVari anceUniName); | |
206 | 229 |
207 const char* widthUniName = nullptr; | 230 const char* widthUniName = nullptr; |
208 const char* heightUniName = nullptr; | 231 const char* heightUniName = nullptr; |
209 | 232 |
210 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 233 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
211 kInt_GrSLType, | 234 kInt_GrSLType, |
212 kDefault_GrSLPrecision, | 235 kDefault_GrSLPrecision, |
213 "width", &widthUniName); | 236 "width", &widthUniName); |
214 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 237 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
215 kInt_GrSLType, | 238 kInt_GrSLType, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 offset.c_str(), lightDirUniName[i]); | 270 offset.c_str(), lightDirUniName[i]); |
248 | 271 |
249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", | 272 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", |
250 scaleVec.c_str(), | 273 scaleVec.c_str(), |
251 widthUniName, heightUniName, | 274 widthUniName, heightUniName, |
252 depthMapWidthUniName[i], depthMapHeight UniName[i]); | 275 depthMapWidthUniName[i], depthMapHeight UniName[i]); |
253 | 276 |
254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", | 277 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", |
255 scaleOffsetVec.c_str(), scaleVec.c_str( )); | 278 scaleOffsetVec.c_str(), scaleVec.c_str( )); |
256 | 279 |
257 | |
258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " | 280 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " |
259 "vec2(%s.x, 0 - %s.y)) " | 281 "vec2(%s.x, 0 - %s.y)) " |
260 " * %s + vec2(0,1) * %s;\n", | 282 " * %s + vec2(0,1) * %s;\n", |
261 | |
262 povCoord.c_str(), offset.c_str(), offse t.c_str(), | 283 povCoord.c_str(), offset.c_str(), offse t.c_str(), |
263 scaleVec.c_str(), scaleOffsetVec.c_str( )); | 284 scaleVec.c_str(), scaleOffsetVec.c_str( )); |
264 | 285 |
265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | 286 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], |
266 povCoord.c_str(), | 287 povCoord.c_str(), |
267 kVec2f_GrSLType); | 288 kVec2f_GrSLType); |
289 | |
290 | |
268 } | 291 } |
269 | 292 |
270 const char* ambientColorUniName = nullptr; | 293 const char* ambientColorUniName = nullptr; |
271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | 294 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , |
272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | 295 kVec3f_GrSLType, kDefa ult_GrSLPrecision, |
273 "AmbientColor", &ambie ntColorUniName); | 296 "AmbientColor", &ambie ntColorUniName); |
274 | 297 |
275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); | 298 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); |
276 | 299 |
277 // Essentially, | 300 // all the normal vectors point straight up |
278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) | |
279 SkString totalLightColor("totalLightColor"); | 301 SkString totalLightColor("totalLightColor"); |
280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); | 302 fragBuilder->codeAppendf("vec3 %s = vec3(0,0,0);", totalLightColor.c _str()); |
303 | |
304 SkString lightProb("lightProbability"); | |
305 fragBuilder->codeAppendf("float %s;", lightProb.c_str()); | |
306 fragBuilder->codeAppendf("float variance;"); | |
307 fragBuilder->codeAppendf("float d;"); | |
281 | 308 |
282 for (int i = 0; i < numLights; i++) { | 309 for (int i = 0; i < numLights; i++) { |
283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | 310 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str()); |
311 | |
312 // 1/512 is less than half a pixel; imperceptible | |
313 fragBuilder->codeAppendf("if (%s.b <= %s.b + 1/512) {", | |
284 povDepth.c_str(), depthMaps[i].c_str()) ; | 314 povDepth.c_str(), depthMaps[i].c_str()) ; |
285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s | 315 if (blurAlgorithm == SkShadowType::kVariance_BlurAlgorithm) { |
286 fragBuilder->codeAppendf("%s += %s.z * %s;", | 316 fragBuilder->codeAppendf("vec2 moments = vec2(%s.b * 255, % s.g * 255 * 256 );", |
317 depthMaps[i].c_str(), depthMaps[i]. c_str()); | |
318 | |
319 // variance biasing lessens light bleeding | |
320 fragBuilder->codeAppendf("variance = max(moments.y - (moment s.x * moments.x)," | |
321 "%s);", minVarianceUniName) ; | |
322 | |
323 fragBuilder->codeAppendf("d = (%s.b * 255) - moments.x;", po vDepth.c_str()); | |
324 fragBuilder->codeAppendf("%s = (variance / (variance + d * d ));", | |
325 lightProb.c_str()); | |
326 | |
327 SkString clamp("clamp"); | |
328 clamp.appendf("%d", i); | |
329 | |
330 // choosing between light artifacts or correct shape shadows | |
331 // linstep | |
332 fragBuilder->codeAppendf("float %s = clamp((%s - %s) / (1 - %s), 0, 1);", | |
333 clamp.c_str(), lightProb.c_str(), | |
334 shBiasUniName, shBiasUniName); | |
335 | |
336 // different bias types | |
337 // sqrt pulls in and makes an edge | |
338 // pow2 pushes out | |
339 // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s) ;", | |
340 // lightProb.c_str(), clamp.c_str(), clamp.c_str(), clamp.c_str()); | |
jvanverth1
2016/08/12 17:39:09
Line length > 100? And are these commented out lin
vjiaoblack
2016/08/12 19:07:42
I would like to keep them as comments to document
jvanverth1
2016/08/12 19:44:10
Then I would block them out with an #ifdef, just l
| |
341 // fragBuilder->codeAppendf("%s = 6 * pow(%s, 5) - 15 * pow( %s, 4) + 10 * pow(%s, 3);", | |
342 // lightProb.c_str(), clamp.c_str(), clamp.c_str(), clamp.c_str()); | |
343 | |
344 fragBuilder->codeAppendf("%s = %s;", | |
345 lightProb.c_str(), clamp.c_str()); | |
346 } else { | |
347 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | |
348 povDepth.c_str(), depthMaps[i].c_st r()); | |
349 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str()); | |
350 fragBuilder->codeAppendf("} else { %s = 0; }", lightProb.c_s tr()); | |
351 } | |
352 | |
353 // VSM: The curved shadows near plane edges are mostly light ble eding. | |
354 fragBuilder->codeAppendf("}"); | |
355 | |
356 fragBuilder->codeAppendf("%s += dot(vec3(0,0,1), %s) * %s * %s;" , | |
287 totalLightColor.c_str(), | 357 totalLightColor.c_str(), |
288 lightDirUniName[i], | 358 lightDirUniName[i], |
289 lightColorUniName[i]); | 359 lightColorUniName[i], |
290 fragBuilder->codeAppendf("}"); | 360 lightProb.c_str()); |
291 } | 361 } |
292 | 362 |
293 fragBuilder->codeAppendf("%s += %s;", | 363 fragBuilder->codeAppendf("%s += %s;", totalLightColor.c_str(), ambie ntColorUniName); |
294 totalLightColor.c_str(), | |
295 ambientColorUniName); | |
296 | 364 |
297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", | 365 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", |
298 totalLightColor.c_str()); | 366 totalLightColor.c_str()); |
299 | 367 |
300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); | 368 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); |
301 } | 369 } |
302 | 370 |
303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 371 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
304 GrProcessorKeyBuilder* b) { | 372 GrProcessorKeyBuilder* b) { |
305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 373 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
306 b->add32(shadowFP.fNumDirLights); | 374 b->add32(shadowFP.fNumDirLights); |
375 b->add32(shadowFP.fShType.fBlurAlgorithm); | |
307 } | 376 } |
308 | 377 |
309 protected: | 378 protected: |
310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { | 379 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
311 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); | 380 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); |
312 | 381 |
313 fNumDirLights = shadowFP.numLights(); | 382 for (int i = 0; i < shadowFP.fNumDirLights; i++) { |
314 | |
315 for (int i = 0; i < fNumDirLights; i++) { | |
316 const SkVector3& lightDir = shadowFP.lightDir(i); | 383 const SkVector3& lightDir = shadowFP.lightDir(i); |
317 if (lightDir != fLightDir[i]) { | 384 if (lightDir != fLightDir[i]) { |
318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | 385 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); |
319 fLightDir[i] = lightDir; | 386 fLightDir[i] = lightDir; |
320 } | 387 } |
321 const SkColor3f& lightColor = shadowFP.lightColor(i); | 388 const SkColor3f& lightColor = shadowFP.lightColor(i); |
322 if (lightColor != fLightColor[i]) { | 389 if (lightColor != fLightColor[i]) { |
323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | 390 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); |
324 fLightColor[i] = lightColor; | 391 fLightColor[i] = lightColor; |
325 } | 392 } |
326 | 393 |
327 int depthMapWidth = shadowFP.depthMapWidth(i); | 394 int depthMapWidth = shadowFP.depthMapWidth(i); |
328 if (depthMapWidth != fDepthMapWidth[i]) { | 395 if (depthMapWidth != fDepthMapWidth[i]) { |
329 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); | 396 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); |
330 fDepthMapWidth[i] = depthMapWidth; | 397 fDepthMapWidth[i] = depthMapWidth; |
331 } | 398 } |
332 int depthMapHeight = shadowFP.depthMapHeight(i); | 399 int depthMapHeight = shadowFP.depthMapHeight(i); |
333 if (depthMapHeight != fDepthMapHeight[i]) { | 400 if (depthMapHeight != fDepthMapHeight[i]) { |
334 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); | 401 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); |
335 fDepthMapHeight[i] = depthMapHeight; | 402 fDepthMapHeight[i] = depthMapHeight; |
336 } | 403 } |
337 } | 404 } |
338 | 405 |
406 SkScalar biasingConstant = shadowFP.shadowType().fBiasingConstant; | |
407 if (biasingConstant != fBiasingConstant) { | |
408 pdman.set1f(fBiasingConstantUni, biasingConstant); | |
409 fBiasingConstant = biasingConstant; | |
410 } | |
411 | |
412 SkScalar minVariance = shadowFP.shadowType().fMinVariance; | |
413 if (minVariance != fMinVariance) { | |
414 pdman.set1f(fMinVarianceUni, minVariance); | |
415 fMinVariance = minVariance; | |
416 } | |
417 | |
339 int width = shadowFP.width(); | 418 int width = shadowFP.width(); |
340 if (width != fWidth) { | 419 if (width != fWidth) { |
341 pdman.set1i(fWidthUni, width); | 420 pdman.set1i(fWidthUni, width); |
342 fWidth = width; | 421 fWidth = width; |
343 } | 422 } |
344 int height = shadowFP.height(); | 423 int height = shadowFP.height(); |
345 if (height != fHeight) { | 424 if (height != fHeight) { |
346 pdman.set1i(fHeightUni, height); | 425 pdman.set1i(fHeightUni, height); |
347 fHeight = height; | 426 fHeight = height; |
348 } | 427 } |
(...skipping 20 matching lines...) Expand all Loading... | |
369 | 448 |
370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 449 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
371 GrGLSLProgramDataManager::UniformHandle | 450 GrGLSLProgramDataManager::UniformHandle |
372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; | 451 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; |
373 | 452 |
374 int fWidth; | 453 int fWidth; |
375 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 454 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
376 int fHeight; | 455 int fHeight; |
377 GrGLSLProgramDataManager::UniformHandle fHeightUni; | 456 GrGLSLProgramDataManager::UniformHandle fHeightUni; |
378 | 457 |
458 SkScalar fBiasingConstant; | |
459 GrGLSLProgramDataManager::UniformHandle fBiasingConstantUni; | |
460 SkScalar fMinVariance; | |
461 GrGLSLProgramDataManager::UniformHandle fMinVarianceUni; | |
462 | |
379 SkColor3f fAmbientColor; | 463 SkColor3f fAmbientColor; |
380 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | 464 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
381 | |
382 int fNumDirLights; | |
383 }; | 465 }; |
384 | 466 |
385 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { | 467 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
386 GLSLShadowFP::GenKey(*this, caps, b); | 468 GLSLShadowFP::GenKey(*this, caps, b); |
387 } | 469 } |
388 | 470 |
389 const char* name() const override { return "shadowFP"; } | 471 const char* name() const override { return "shadowFP"; } |
390 | 472 |
391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 473 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
392 inout->mulByUnknownFourComponents(); | 474 inout->mulByUnknownFourComponents(); |
(...skipping 13 matching lines...) Expand all Loading... | |
406 SkASSERT(i < fNumDirLights); | 488 SkASSERT(i < fNumDirLights); |
407 return fDepthMapWidth[i]; | 489 return fDepthMapWidth[i]; |
408 } | 490 } |
409 int depthMapHeight(int i) const { | 491 int depthMapHeight(int i) const { |
410 SkASSERT(i < fNumDirLights); | 492 SkASSERT(i < fNumDirLights); |
411 return fDepthMapHeight[i]; | 493 return fDepthMapHeight[i]; |
412 } | 494 } |
413 int width() const {return fWidth; } | 495 int width() const {return fWidth; } |
414 int height() const {return fHeight; } | 496 int height() const {return fHeight; } |
415 | 497 |
498 SkShadowType shadowType() const {return fShType; } | |
499 | |
416 private: | 500 private: |
417 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } | 501 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } |
418 | 502 |
419 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 503 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
420 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 504 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { | 505 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { |
422 return false; | 506 return false; |
423 } | 507 } |
424 | 508 |
425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { | 509 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { |
(...skipping 21 matching lines...) Expand all Loading... | |
447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 531 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; | 532 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; |
449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; | 533 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; |
450 | 534 |
451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 535 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 536 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
453 | 537 |
454 int fHeight; | 538 int fHeight; |
455 int fWidth; | 539 int fWidth; |
456 | 540 |
541 SkShadowType fShType; | |
542 | |
457 SkColor3f fAmbientColor; | 543 SkColor3f fAmbientColor; |
458 }; | 544 }; |
459 | 545 |
460 //////////////////////////////////////////////////////////////////////////// | 546 //////////////////////////////////////////////////////////////////////////// |
461 | 547 |
462 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const { | 548 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const { |
463 | 549 |
464 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor (fpargs); | 550 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor (fpargs); |
465 | 551 |
466 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f pargs); | 552 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f pargs); |
467 | 553 |
468 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP), | 554 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP), |
469 std::move(diffuse FP), | 555 std::move(diffuse FP), |
470 std::move(fLights ), | 556 std::move(fLights ), |
471 fDiffuseWidth, fD iffuseHeight, | 557 fDiffuseWidth, fD iffuseHeight, |
472 fpargs.fContext); | 558 fShType, fpargs.f Context); |
473 return shadowfp; | 559 return shadowfp; |
474 } | 560 } |
475 | 561 |
476 | 562 |
477 #endif | 563 #endif |
478 | 564 |
479 //////////////////////////////////////////////////////////////////////////// | 565 //////////////////////////////////////////////////////////////////////////// |
480 | 566 |
481 bool SkShadowShaderImpl::isOpaque() const { | 567 bool SkShadowShaderImpl::isOpaque() const { |
482 return fDiffuseShader->isOpaque(); | 568 return fDiffuseShader->isOpaque(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
619 | 705 |
620 SkLights::Light light = SkLights::Light(color, dir); | 706 SkLights::Light light = SkLights::Light(color, dir); |
621 light.setShadowMap(depthMap); | 707 light.setShadowMap(depthMap); |
622 | 708 |
623 builder.add(light); | 709 builder.add(light); |
624 } | 710 } |
625 } | 711 } |
626 | 712 |
627 sk_sp<SkLights> lights(builder.finish()); | 713 sk_sp<SkLights> lights(builder.finish()); |
628 | 714 |
715 SkShadowType sType; | |
716 sType.fMinVariance = buf.readScalar(); | |
717 sType.fBiasingConstant = buf.readScalar(); | |
718 sType.fBlurAlgorithm = (SkShadowType::BlurAlgorithm) buf.readInt(); | |
719 sType.fShadowRadius = buf.readScalar(); | |
720 | |
629 int diffuseWidth = buf.readInt(); | 721 int diffuseWidth = buf.readInt(); |
630 int diffuseHeight = buf.readInt(); | 722 int diffuseHeight = buf.readInt(); |
631 | 723 |
632 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); | 724 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); |
633 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); | 725 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); |
634 | 726 |
635 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 727 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
636 std::move(diffuseShader), | 728 std::move(diffuseShader), |
637 std::move(lights), | 729 std::move(lights), |
638 diffuseWidth, diffuseHeight); | 730 diffuseWidth, diffuseHeight, |
731 sType); | |
639 } | 732 } |
640 | 733 |
641 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { | 734 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { |
642 this->INHERITED::flatten(buf); | 735 this->INHERITED::flatten(buf); |
643 | 736 |
644 buf.writeInt(fLights->numLights()); | 737 buf.writeInt(fLights->numLights()); |
645 | 738 |
646 for (int l = 0; l < fLights->numLights(); ++l) { | 739 for (int l = 0; l < fLights->numLights(); ++l) { |
647 const SkLights::Light& light = fLights->light(l); | 740 const SkLights::Light& light = fLights->light(l); |
648 | 741 |
649 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | 742 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
650 | 743 |
651 buf.writeBool(isAmbient); | 744 buf.writeBool(isAmbient); |
652 buf.writeScalarArray(&light.color().fX, 3); | 745 buf.writeScalarArray(&light.color().fX, 3); |
653 if (!isAmbient) { | 746 if (!isAmbient) { |
654 buf.writeScalarArray(&light.dir().fX, 3); | 747 buf.writeScalarArray(&light.dir().fX, 3); |
655 } | 748 } |
656 | 749 |
657 buf.writeImage(light.getShadowMap()); | 750 buf.writeImage(light.getShadowMap()); |
658 } | 751 } |
659 | 752 |
753 buf.writeScalar(fShType.fMinVariance); | |
754 buf.writeScalar(fShType.fBiasingConstant); | |
755 buf.writeInt(fShType.fBlurAlgorithm); | |
756 buf.writeScalar(fShType.fShadowRadius); | |
757 | |
660 buf.writeInt(fDiffuseWidth); | 758 buf.writeInt(fDiffuseWidth); |
661 buf.writeInt(fDiffuseHeight); | 759 buf.writeInt(fDiffuseHeight); |
662 | 760 |
663 buf.writeFlattenable(fPovDepthShader.get()); | 761 buf.writeFlattenable(fPovDepthShader.get()); |
664 buf.writeFlattenable(fDiffuseShader.get()); | 762 buf.writeFlattenable(fDiffuseShader.get()); |
665 } | 763 } |
666 | 764 |
667 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { | 765 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { |
668 return sizeof(ShadowShaderContext); | 766 return sizeof(ShadowShaderContext); |
669 } | 767 } |
(...skipping 25 matching lines...) Expand all Loading... | |
695 | 793 |
696 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext, | 794 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext, |
697 heapAllocated); | 795 heapAllocated); |
698 } | 796 } |
699 | 797 |
700 /////////////////////////////////////////////////////////////////////////////// | 798 /////////////////////////////////////////////////////////////////////////////// |
701 | 799 |
702 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, | 800 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, |
703 sk_sp<SkShader> diffuseShader, | 801 sk_sp<SkShader> diffuseShader, |
704 sk_sp<SkLights> lights, | 802 sk_sp<SkLights> lights, |
705 int diffuseWidth, int diffuseHeight) { | 803 int diffuseWidth, int diffuseHeight, |
804 SkShadowType sType) { | |
706 if (!povDepthShader || !diffuseShader) { | 805 if (!povDepthShader || !diffuseShader) { |
707 // TODO: Use paint's color in absence of a diffuseShader | 806 // TODO: Use paint's color in absence of a diffuseShader |
708 // TODO: Use a default implementation of normalSource instead | 807 // TODO: Use a default implementation of normalSource instead |
709 return nullptr; | 808 return nullptr; |
710 } | 809 } |
711 | 810 |
712 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 811 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
713 std::move(diffuseShader), | 812 std::move(diffuseShader), |
714 std::move(lights), | 813 std::move(lights), |
715 diffuseWidth, diffuseHeight); | 814 diffuseWidth, diffuseHeight, |
815 sType); | |
716 } | 816 } |
717 | 817 |
718 /////////////////////////////////////////////////////////////////////////////// | 818 /////////////////////////////////////////////////////////////////////////////// |
719 | 819 |
720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 820 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 821 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 822 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
723 | 823 |
724 /////////////////////////////////////////////////////////////////////////////// | 824 /////////////////////////////////////////////////////////////////////////////// |
725 | 825 |
726 #endif | 826 #endif |
OLD | NEW |