Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Side by Side Diff: src/core/SkShadowShader.cpp

Issue 2224163005: Made shadows blurry (thru implementing variance mapping) (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: rebased off master again! Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "SkCanvas.h"
robertphillips 2016/08/23 17:19:40 rm comment code
vjiaoblack 2016/08/23 18:01:22 Done.
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"
13 12
14 //////////////////////////////////////////////////////////////////////////// 13 ////////////////////////////////////////////////////////////////////////////
15 #ifdef SK_EXPERIMENTAL_SHADOWING 14 #ifdef SK_EXPERIMENTAL_SHADOWING
16 15
17 16
18 /** \class SkShadowShaderImpl 17 /** \class SkShadowShaderImpl
19 This subclass of shader applies shadowing 18 This subclass of shader applies shadowing
20 */ 19 */
21 class SkShadowShaderImpl : public SkShader { 20 class SkShadowShaderImpl : public SkShader {
22 public: 21 public:
23 /** Create a new shadowing shader that shadows 22 /** Create a new shadowing shader that shadows
24 @param to do to do 23 @param to do to do
25 */ 24 */
26 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, 25 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader,
27 sk_sp<SkShader> diffuseShader, 26 sk_sp<SkShader> diffuseShader,
28 sk_sp<SkLights> lights, 27 sk_sp<SkLights> lights,
29 int diffuseWidth, int diffuseHeight) 28 int diffuseWidth, int diffuseHeight,
29 const SkShadowParams& params)
30 : fPovDepthShader(std::move(povDepthShader)) 30 : fPovDepthShader(std::move(povDepthShader))
31 , fDiffuseShader(std::move(diffuseShader)) 31 , fDiffuseShader(std::move(diffuseShader))
32 , fLights(std::move(lights)) 32 , fLights(std::move(lights))
33 , fDiffuseWidth(diffuseWidth) 33 , fDiffuseWidth(diffuseWidth)
34 , fDiffuseHeight(diffuseHeight) { } 34 , fDiffuseHeight(diffuseHeight)
35 , fShadowParams(params) { }
35 36
36 bool isOpaque() const override; 37 bool isOpaque() const override;
37 38
38 #if SK_SUPPORT_GPU 39 #if SK_SUPPORT_GPU
39 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de; 40 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de;
40 #endif 41 #endif
41 42
42 class ShadowShaderContext : public SkShader::Context { 43 class ShadowShaderContext : public SkShader::Context {
43 public: 44 public:
44 // The context takes ownership of the states. It will call their destruc tors 45 // The context takes ownership of the states. It will call their destruc tors
(...skipping 28 matching lines...) Expand all
73 Context* onCreateContext(const ContextRec&, void*) const override; 74 Context* onCreateContext(const ContextRec&, void*) const override;
74 75
75 private: 76 private:
76 sk_sp<SkShader> fPovDepthShader; 77 sk_sp<SkShader> fPovDepthShader;
77 sk_sp<SkShader> fDiffuseShader; 78 sk_sp<SkShader> fDiffuseShader;
78 sk_sp<SkLights> fLights; 79 sk_sp<SkLights> fLights;
79 80
80 int fDiffuseWidth; 81 int fDiffuseWidth;
81 int fDiffuseHeight; 82 int fDiffuseHeight;
82 83
84 SkShadowParams fShadowParams;
85
83 friend class SkShadowShader; 86 friend class SkShadowShader;
84 87
85 typedef SkShader INHERITED; 88 typedef SkShader INHERITED;
86 }; 89 };
87 90
88 //////////////////////////////////////////////////////////////////////////// 91 ////////////////////////////////////////////////////////////////////////////
89 92
90 #if SK_SUPPORT_GPU 93 #if SK_SUPPORT_GPU
91 94
92 #include "GrCoordTransform.h" 95 #include "GrCoordTransform.h"
93 #include "GrFragmentProcessor.h" 96 #include "GrFragmentProcessor.h"
94 #include "GrInvariantOutput.h" 97 #include "GrInvariantOutput.h"
95 #include "glsl/GrGLSLFragmentProcessor.h" 98 #include "glsl/GrGLSLFragmentProcessor.h"
96 #include "glsl/GrGLSLFragmentShaderBuilder.h" 99 #include "glsl/GrGLSLFragmentShaderBuilder.h"
97 #include "SkGr.h" 100 #include "SkGr.h"
98 #include "SkGrPriv.h" 101 #include "SkGrPriv.h"
99 #include "SkSpecialImage.h" 102 #include "SkSpecialImage.h"
100 #include "SkImage_Base.h" 103 #include "SkImage_Base.h"
101 #include "GrContext.h" 104 #include "GrContext.h"
102 105
103 class ShadowFP : public GrFragmentProcessor { 106 class ShadowFP : public GrFragmentProcessor {
104 public: 107 public:
105 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, 108 ShadowFP(sk_sp<GrFragmentProcessor> povDepth,
106 sk_sp<GrFragmentProcessor> diffuse, 109 sk_sp<GrFragmentProcessor> diffuse,
107 sk_sp<SkLights> lights, 110 sk_sp<SkLights> lights,
108 int diffuseWidth, int diffuseHeight, 111 int diffuseWidth, int diffuseHeight,
112 const SkShadowParams& params,
109 GrContext* context) { 113 GrContext* context) {
110 114
111 // fuse all ambient lights into a single one 115 // fuse all ambient lights into a single one
112 fAmbientColor.set(0.0f, 0.0f, 0.0f); 116 fAmbientColor.set(0.0f, 0.0f, 0.0f);
113 117
114 fNumDirLights = 0; // refers to directional lights. 118 fNumDirLights = 0; // refers to directional lights.
115 for (int i = 0; i < lights->numLights(); ++i) { 119 for (int i = 0; i < lights->numLights(); ++i) {
116 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { 120 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) {
117 fAmbientColor += lights->light(i).color(); 121 fAmbientColor += lights->light(i).color();
118 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { 122 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) {
(...skipping 11 matching lines...) Expand all
130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); 134 fDepthMapHeight[fNumDirLights] = shadowMap->height();
131 fDepthMapWidth[fNumDirLights] = shadowMap->width(); 135 fDepthMapWidth[fNumDirLights] = shadowMap->width();
132 136
133 fNumDirLights++; 137 fNumDirLights++;
134 } 138 }
135 } 139 }
136 140
137 fWidth = diffuseWidth; 141 fWidth = diffuseWidth;
138 fHeight = diffuseHeight; 142 fHeight = diffuseHeight;
139 143
140 this->registerChildProcessor(std::move(povDepth)); 144 fShadowParams = params;
145
146 this->registerChildProcessor(std::move(povDepth));
141 this->registerChildProcessor(std::move(diffuse)); 147 this->registerChildProcessor(std::move(diffuse));
142 this->initClassID<ShadowFP>(); 148 this->initClassID<ShadowFP>();
143 } 149 }
144 150
145 class GLSLShadowFP : public GrGLSLFragmentProcessor { 151 class GLSLShadowFP : public GrGLSLFragmentProcessor {
146 public: 152 public:
147 GLSLShadowFP() { } 153 GLSLShadowFP() { }
148 154
149 void emitCode(EmitArgs& args) override { 155 void emitCode(EmitArgs& args) override {
150 156
151 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; 157 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 158 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
153 159
154 // add uniforms 160 // add uniforms
155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; 161 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights;
156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); 162 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights);
157 163
164 int blurAlgorithm = args.fFp.cast<ShadowFP>().fShadowParams.fType;
165
158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; 166 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr};
159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; 167 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr};
160 168
161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] 169 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s]
162 = {nullptr}; 170 = {nullptr};
163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] 171 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts]
164 = {nullptr}; 172 = {nullptr};
165 173
166 SkString lightDirUniNameBase("lightDir"); 174 SkString lightDirUniNameBase("lightDir");
167 SkString lightColorUniNameBase("lightColor"); 175 SkString lightColorUniNameBase("lightColor");
(...skipping 28 matching lines...) Expand all
196 kDefault_GrSLPrecision, 204 kDefault_GrSLPrecision,
197 depthMapWidthUniNameStr.c_str (), 205 depthMapWidthUniNameStr.c_str (),
198 &depthMapWidthUniName[i]); 206 &depthMapWidthUniName[i]);
199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, 207 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag,
200 kInt_GrSLType, 208 kInt_GrSLType,
201 kDefault_GrSLPrecision, 209 kDefault_GrSLPrecision,
202 depthMapHeightUniNameStr.c_st r(), 210 depthMapHeightUniNameStr.c_st r(),
203 &depthMapHeightUniName[i]); 211 &depthMapHeightUniName[i]);
204 } 212 }
205 213
214 const char* shBiasUniName = nullptr;
215 const char* minVarianceUniName = nullptr;
216
217 fBiasingConstantUni = uniformHandler->addUniform(kFragment_GrShaderF lag,
218 kFloat_GrSLType,
219 kDefault_GrSLPrecis ion,
220 "shadowBias", &shBi asUniName);
221 fMinVarianceUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
222 kFloat_GrSLType,
223 kDefault_GrSLPrecision,
224 "minVariance", &minVari anceUniName);
206 225
207 const char* widthUniName = nullptr; 226 const char* widthUniName = nullptr;
208 const char* heightUniName = nullptr; 227 const char* heightUniName = nullptr;
209 228
210 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, 229 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
211 kInt_GrSLType, 230 kInt_GrSLType,
212 kDefault_GrSLPrecision, 231 kDefault_GrSLPrecision,
213 "width", &widthUniName); 232 "width", &widthUniName);
214 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, 233 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
215 kInt_GrSLType, 234 kInt_GrSLType,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 offset.c_str(), lightDirUniName[i]); 266 offset.c_str(), lightDirUniName[i]);
248 267
249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", 268 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n",
250 scaleVec.c_str(), 269 scaleVec.c_str(),
251 widthUniName, heightUniName, 270 widthUniName, heightUniName,
252 depthMapWidthUniName[i], depthMapHeight UniName[i]); 271 depthMapWidthUniName[i], depthMapHeight UniName[i]);
253 272
254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", 273 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n",
255 scaleOffsetVec.c_str(), scaleVec.c_str( )); 274 scaleOffsetVec.c_str(), scaleVec.c_str( ));
256 275
257
258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " 276 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + "
259 "vec2(%s.x, 0 - %s.y)) " 277 "vec2(%s.x, 0 - %s.y)) "
260 " * %s + vec2(0,1) * %s;\n", 278 " * %s + vec2(0,1) * %s;\n",
261
262 povCoord.c_str(), offset.c_str(), offse t.c_str(), 279 povCoord.c_str(), offset.c_str(), offse t.c_str(),
263 scaleVec.c_str(), scaleOffsetVec.c_str( )); 280 scaleVec.c_str(), scaleOffsetVec.c_str( ));
264 281
265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], 282 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i],
266 povCoord.c_str(), 283 povCoord.c_str(),
267 kVec2f_GrSLType); 284 kVec2f_GrSLType);
285
286
268 } 287 }
269 288
270 const char* ambientColorUniName = nullptr; 289 const char* ambientColorUniName = nullptr;
271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , 290 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag ,
272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, 291 kVec3f_GrSLType, kDefa ult_GrSLPrecision,
273 "AmbientColor", &ambie ntColorUniName); 292 "AmbientColor", &ambie ntColorUniName);
274 293
275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); 294 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str());
276 295
robertphillips 2016/08/23 17:19:40 This comment seems misplaced
vjiaoblack 2016/08/23 18:01:22 Done.
277 // Essentially, 296 // all the normal vectors point straight up
278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L)))
279 SkString totalLightColor("totalLightColor"); 297 SkString totalLightColor("totalLightColor");
280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); 298 fragBuilder->codeAppendf("vec3 %s = vec3(0,0,0);", totalLightColor.c _str());
299
300 fragBuilder->codeAppendf("float lightProbability;");
301 fragBuilder->codeAppendf("float variance;");
302 fragBuilder->codeAppendf("float d;");
281 303
282 for (int i = 0; i < numLights; i++) { 304 for (int i = 0; i < numLights; i++) {
283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", 305 fragBuilder->codeAppendf("lightProbability = 1;");
306
307 // 1/512 is less than half a pixel; imperceptible
308 fragBuilder->codeAppendf("if (%s.b <= %s.b + 1/512) {",
284 povDepth.c_str(), depthMaps[i].c_str()) ; 309 povDepth.c_str(), depthMaps[i].c_str()) ;
285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s 310 if (blurAlgorithm == SkShadowParams::kVariance_BlurAlgorithm) {
286 fragBuilder->codeAppendf("%s += %s.z * %s;", 311 fragBuilder->codeAppendf("vec2 moments = vec2(%s.b * 255, % s.g * 255 * 256 );",
312 depthMaps[i].c_str(), depthMaps[i]. c_str());
313
314 // variance biasing lessens light bleeding
315 fragBuilder->codeAppendf("variance = max(moments.y - (moment s.x * moments.x),"
316 "%s);", minVarianceUniName) ;
317
318 fragBuilder->codeAppendf("d = (%s.b * 255) - moments.x;", po vDepth.c_str());
319 fragBuilder->codeAppendf("lightProbability = "
320 "(variance / (variance + d * d));") ;
321
322 SkString clamp("clamp");
323 clamp.appendf("%d", i);
324
325 // choosing between light artifacts or correct shape shadows
326 // linstep
327 fragBuilder->codeAppendf("float %s = clamp((lightProbability - %s) /"
328 "(1 - %s), 0, 1);" ,
329 clamp.c_str(), shBiasUniName, shBia sUniName);
330
331 fragBuilder->codeAppendf("lightProbability = %s;", clamp.c_s tr());
332 } else {
333 fragBuilder->codeAppendf("if (%s.b >= %s.b) {",
334 povDepth.c_str(), depthMaps[i].c_st r());
335 fragBuilder->codeAppendf("lightProbability = 1;");
336 fragBuilder->codeAppendf("} else { lightProbability = 0; }") ;
337 }
338
339 // VSM: The curved shadows near plane edges are mostly light ble eding.
340 fragBuilder->codeAppendf("}");
341
342 fragBuilder->codeAppendf("%s += dot(vec3(0,0,1), %s) * %s * ligh tProbability;",
287 totalLightColor.c_str(), 343 totalLightColor.c_str(),
288 lightDirUniName[i], 344 lightDirUniName[i],
289 lightColorUniName[i]); 345 lightColorUniName[i]);
290 fragBuilder->codeAppendf("}");
291 } 346 }
292 347
293 fragBuilder->codeAppendf("%s += %s;", 348 fragBuilder->codeAppendf("%s += %s;", totalLightColor.c_str(), ambie ntColorUniName);
294 totalLightColor.c_str(),
295 ambientColorUniName);
296 349
297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", 350 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);",
298 totalLightColor.c_str()); 351 totalLightColor.c_str());
299 352
300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); 353 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or);
301 } 354 }
302 355
303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, 356 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
304 GrProcessorKeyBuilder* b) { 357 GrProcessorKeyBuilder* b) {
305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); 358 const ShadowFP& shadowFP = proc.cast<ShadowFP>();
306 b->add32(shadowFP.fNumDirLights); 359 b->add32(shadowFP.fNumDirLights);
360 b->add32(shadowFP.fShadowParams.fType);
307 } 361 }
308 362
309 protected: 363 protected:
310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { 364 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
311 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); 365 const ShadowFP &shadowFP = proc.cast<ShadowFP>();
312 366
313 fNumDirLights = shadowFP.numLights(); 367 for (int i = 0; i < shadowFP.fNumDirLights; i++) {
314
315 for (int i = 0; i < fNumDirLights; i++) {
316 const SkVector3& lightDir = shadowFP.lightDir(i); 368 const SkVector3& lightDir = shadowFP.lightDir(i);
317 if (lightDir != fLightDir[i]) { 369 if (lightDir != fLightDir[i]) {
318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); 370 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX);
319 fLightDir[i] = lightDir; 371 fLightDir[i] = lightDir;
320 } 372 }
321 const SkColor3f& lightColor = shadowFP.lightColor(i); 373 const SkColor3f& lightColor = shadowFP.lightColor(i);
322 if (lightColor != fLightColor[i]) { 374 if (lightColor != fLightColor[i]) {
323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); 375 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX);
324 fLightColor[i] = lightColor; 376 fLightColor[i] = lightColor;
325 } 377 }
326 378
327 int depthMapWidth = shadowFP.depthMapWidth(i); 379 int depthMapWidth = shadowFP.depthMapWidth(i);
328 if (depthMapWidth != fDepthMapWidth[i]) { 380 if (depthMapWidth != fDepthMapWidth[i]) {
329 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); 381 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth);
330 fDepthMapWidth[i] = depthMapWidth; 382 fDepthMapWidth[i] = depthMapWidth;
331 } 383 }
332 int depthMapHeight = shadowFP.depthMapHeight(i); 384 int depthMapHeight = shadowFP.depthMapHeight(i);
333 if (depthMapHeight != fDepthMapHeight[i]) { 385 if (depthMapHeight != fDepthMapHeight[i]) {
334 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); 386 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight);
335 fDepthMapHeight[i] = depthMapHeight; 387 fDepthMapHeight[i] = depthMapHeight;
336 } 388 }
337 } 389 }
338 390
391 SkScalar biasingConstant = shadowFP.shadowParams().fBiasingConstant;
392 if (biasingConstant != fBiasingConstant) {
393 pdman.set1f(fBiasingConstantUni, biasingConstant);
394 fBiasingConstant = biasingConstant;
395 }
396
397 SkScalar minVariance = shadowFP.shadowParams().fMinVariance;
398 if (minVariance != fMinVariance) {
399 pdman.set1f(fMinVarianceUni, minVariance);
400 fMinVariance = minVariance;
401 }
402
339 int width = shadowFP.width(); 403 int width = shadowFP.width();
340 if (width != fWidth) { 404 if (width != fWidth) {
341 pdman.set1i(fWidthUni, width); 405 pdman.set1i(fWidthUni, width);
342 fWidth = width; 406 fWidth = width;
343 } 407 }
344 int height = shadowFP.height(); 408 int height = shadowFP.height();
345 if (height != fHeight) { 409 if (height != fHeight) {
346 pdman.set1i(fHeightUni, height); 410 pdman.set1i(fHeightUni, height);
347 fHeight = height; 411 fHeight = height;
348 } 412 }
(...skipping 20 matching lines...) Expand all
369 433
370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; 434 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights];
371 GrGLSLProgramDataManager::UniformHandle 435 GrGLSLProgramDataManager::UniformHandle
372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; 436 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights];
373 437
374 int fWidth; 438 int fWidth;
375 GrGLSLProgramDataManager::UniformHandle fWidthUni; 439 GrGLSLProgramDataManager::UniformHandle fWidthUni;
376 int fHeight; 440 int fHeight;
377 GrGLSLProgramDataManager::UniformHandle fHeightUni; 441 GrGLSLProgramDataManager::UniformHandle fHeightUni;
378 442
443 SkScalar fBiasingConstant;
444 GrGLSLProgramDataManager::UniformHandle fBiasingConstantUni;
445 SkScalar fMinVariance;
446 GrGLSLProgramDataManager::UniformHandle fMinVarianceUni;
447
379 SkColor3f fAmbientColor; 448 SkColor3f fAmbientColor;
380 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; 449 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni;
381
382 int fNumDirLights;
383 }; 450 };
384 451
385 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { 452 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
386 GLSLShadowFP::GenKey(*this, caps, b); 453 GLSLShadowFP::GenKey(*this, caps, b);
387 } 454 }
388 455
389 const char* name() const override { return "shadowFP"; } 456 const char* name() const override { return "shadowFP"; }
390 457
391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { 458 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
392 inout->mulByUnknownFourComponents(); 459 inout->mulByUnknownFourComponents();
(...skipping 13 matching lines...) Expand all
406 SkASSERT(i < fNumDirLights); 473 SkASSERT(i < fNumDirLights);
407 return fDepthMapWidth[i]; 474 return fDepthMapWidth[i];
408 } 475 }
409 int depthMapHeight(int i) const { 476 int depthMapHeight(int i) const {
410 SkASSERT(i < fNumDirLights); 477 SkASSERT(i < fNumDirLights);
411 return fDepthMapHeight[i]; 478 return fDepthMapHeight[i];
412 } 479 }
413 int width() const {return fWidth; } 480 int width() const {return fWidth; }
414 int height() const {return fHeight; } 481 int height() const {return fHeight; }
415 482
483 const SkShadowParams& shadowParams() const {return fShadowParams; }
484
416 private: 485 private:
417 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } 486 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; }
418 487
419 bool onIsEqual(const GrFragmentProcessor& proc) const override { 488 bool onIsEqual(const GrFragmentProcessor& proc) const override {
420 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); 489 const ShadowFP& shadowFP = proc.cast<ShadowFP>();
421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { 490 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) {
422 return false; 491 return false;
423 } 492 }
424 493
425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { 494 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) {
(...skipping 20 matching lines...) Expand all
446 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; 515 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights];
447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; 516 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights];
448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; 517 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights];
449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; 518 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights];
450 519
451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; 520 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights];
452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; 521 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights];
453 522
454 int fHeight; 523 int fHeight;
455 int fWidth; 524 int fWidth;
456 525
robertphillips 2016/08/23 17:19:40 line up
vjiaoblack 2016/08/23 18:01:22 Done.
526 SkShadowParams fShadowParams;
527
457 SkColor3f fAmbientColor; 528 SkColor3f fAmbientColor;
458 }; 529 };
459 530
460 //////////////////////////////////////////////////////////////////////////// 531 ////////////////////////////////////////////////////////////////////////////
461 532
462 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const { 533 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const {
463 534
464 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor (fpargs); 535 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor (fpargs);
465 536
466 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f pargs); 537 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f pargs);
467 538
468 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP), 539 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP),
469 std::move(diffuse FP), 540 std::move(diffuse FP),
470 std::move(fLights ), 541 std::move(fLights ),
471 fDiffuseWidth, fD iffuseHeight, 542 fDiffuseWidth, fD iffuseHeight,
472 fpargs.fContext); 543 fShadowParams, fp args.fContext);
473 return shadowfp; 544 return shadowfp;
474 } 545 }
475 546
476 547
477 #endif 548 #endif
478 549
479 //////////////////////////////////////////////////////////////////////////// 550 ////////////////////////////////////////////////////////////////////////////
480 551
481 bool SkShadowShaderImpl::isOpaque() const { 552 bool SkShadowShaderImpl::isOpaque() const {
482 return fDiffuseShader->isOpaque(); 553 return fDiffuseShader->isOpaque();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 #endif 658 #endif
588 659
589 sk_sp<SkFlattenable> SkShadowShaderImpl::CreateProc(SkReadBuffer& buf) { 660 sk_sp<SkFlattenable> SkShadowShaderImpl::CreateProc(SkReadBuffer& buf) {
590 661
591 // Discarding SkShader flattenable params 662 // Discarding SkShader flattenable params
592 bool hasLocalMatrix = buf.readBool(); 663 bool hasLocalMatrix = buf.readBool();
593 SkAssertResult(!hasLocalMatrix); 664 SkAssertResult(!hasLocalMatrix);
594 665
595 sk_sp<SkLights> lights = SkLights::MakeFromBuffer(buf); 666 sk_sp<SkLights> lights = SkLights::MakeFromBuffer(buf);
596 667
668 SkShadowParams params;
669 params.fMinVariance = buf.readScalar();
670 params.fBiasingConstant = buf.readScalar();
671 params.fType = (SkShadowParams::ShadowType) buf.readInt();
672 params.fShadowRadius = buf.readScalar();
673
597 int diffuseWidth = buf.readInt(); 674 int diffuseWidth = buf.readInt();
598 int diffuseHeight = buf.readInt(); 675 int diffuseHeight = buf.readInt();
599 676
600 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); 677 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>());
601 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); 678 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>());
602 679
603 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), 680 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader),
604 std::move(diffuseShader), 681 std::move(diffuseShader),
605 std::move(lights), 682 std::move(lights),
606 diffuseWidth, diffuseHeight); 683 diffuseWidth, diffuseHeight,
684 params);
607 } 685 }
608 686
609 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { 687 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const {
610 this->INHERITED::flatten(buf); 688 this->INHERITED::flatten(buf);
611 689
612 fLights->flatten(buf); 690 fLights->flatten(buf);
613 691
692 buf.writeScalar(fShadowParams.fMinVariance);
693 buf.writeScalar(fShadowParams.fBiasingConstant);
694 buf.writeInt(fShadowParams.fType);
695 buf.writeScalar(fShadowParams.fShadowRadius);
696
614 buf.writeInt(fDiffuseWidth); 697 buf.writeInt(fDiffuseWidth);
615 buf.writeInt(fDiffuseHeight); 698 buf.writeInt(fDiffuseHeight);
616 699
617 buf.writeFlattenable(fPovDepthShader.get()); 700 buf.writeFlattenable(fPovDepthShader.get());
618 buf.writeFlattenable(fDiffuseShader.get()); 701 buf.writeFlattenable(fDiffuseShader.get());
619 } 702 }
620 703
621 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { 704 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const {
622 return sizeof(ShadowShaderContext); 705 return sizeof(ShadowShaderContext);
623 } 706 }
(...skipping 25 matching lines...) Expand all
649 732
650 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext, 733 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext,
651 heapAllocated); 734 heapAllocated);
652 } 735 }
653 736
654 /////////////////////////////////////////////////////////////////////////////// 737 ///////////////////////////////////////////////////////////////////////////////
655 738
656 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, 739 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader,
657 sk_sp<SkShader> diffuseShader, 740 sk_sp<SkShader> diffuseShader,
658 sk_sp<SkLights> lights, 741 sk_sp<SkLights> lights,
659 int diffuseWidth, int diffuseHeight) { 742 int diffuseWidth, int diffuseHeight,
743 const SkShadowParams& params) {
660 if (!povDepthShader || !diffuseShader) { 744 if (!povDepthShader || !diffuseShader) {
661 // TODO: Use paint's color in absence of a diffuseShader 745 // TODO: Use paint's color in absence of a diffuseShader
662 // TODO: Use a default implementation of normalSource instead 746 // TODO: Use a default implementation of normalSource instead
663 return nullptr; 747 return nullptr;
664 } 748 }
665 749
666 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), 750 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader),
667 std::move(diffuseShader), 751 std::move(diffuseShader),
668 std::move(lights), 752 std::move(lights),
669 diffuseWidth, diffuseHeight); 753 diffuseWidth, diffuseHeight,
754 params);
670 } 755 }
671 756
672 /////////////////////////////////////////////////////////////////////////////// 757 ///////////////////////////////////////////////////////////////////////////////
673 758
674 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) 759 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader)
675 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) 760 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl)
676 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 761 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
677 762
678 /////////////////////////////////////////////////////////////////////////////// 763 ///////////////////////////////////////////////////////////////////////////////
679 764
680 #endif 765 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698