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) | |
jvanverth1
2016/08/15 16:37:17
const SkShadowType&
vjiaoblack
2016/08/15 17:43:31
Done.
| |
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 , fShadowType(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 fShadowType; | |
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, | |
jvanverth1
2016/08/15 16:37:17
const SkShadowType&
vjiaoblack
2016/08/15 17:43:31
Done.
| |
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 |
140 this->registerChildProcessor(std::move(povDepth)); | 145 fShadowType = sType; |
146 | |
147 this->registerChildProcessor(std::move(povDepth)); | |
141 this->registerChildProcessor(std::move(diffuse)); | 148 this->registerChildProcessor(std::move(diffuse)); |
142 this->initClassID<ShadowFP>(); | 149 this->initClassID<ShadowFP>(); |
143 } | 150 } |
144 | 151 |
145 class GLSLShadowFP : public GrGLSLFragmentProcessor { | 152 class GLSLShadowFP : public GrGLSLFragmentProcessor { |
146 public: | 153 public: |
147 GLSLShadowFP() { } | 154 GLSLShadowFP() { } |
148 | 155 |
149 void emitCode(EmitArgs& args) override { | 156 void emitCode(EmitArgs& args) override { |
150 | 157 |
151 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 158 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 159 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
153 | 160 |
154 // add uniforms | 161 // add uniforms |
155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | 162 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; |
156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); | 163 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); |
157 | 164 |
165 int blurAlgorithm = args.fFp.cast<ShadowFP>().fShadowType.fBlurAlgor ithm; | |
166 | |
158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 167 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 168 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
160 | 169 |
161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] | 170 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] |
162 = {nullptr}; | 171 = {nullptr}; |
163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] | 172 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] |
164 = {nullptr}; | 173 = {nullptr}; |
165 | 174 |
166 SkString lightDirUniNameBase("lightDir"); | 175 SkString lightDirUniNameBase("lightDir"); |
167 SkString lightColorUniNameBase("lightColor"); | 176 SkString lightColorUniNameBase("lightColor"); |
(...skipping 28 matching lines...) Expand all Loading... | |
196 kDefault_GrSLPrecision, | 205 kDefault_GrSLPrecision, |
197 depthMapWidthUniNameStr.c_str (), | 206 depthMapWidthUniNameStr.c_str (), |
198 &depthMapWidthUniName[i]); | 207 &depthMapWidthUniName[i]); |
199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 208 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
200 kInt_GrSLType, | 209 kInt_GrSLType, |
201 kDefault_GrSLPrecision, | 210 kDefault_GrSLPrecision, |
202 depthMapHeightUniNameStr.c_st r(), | 211 depthMapHeightUniNameStr.c_st r(), |
203 &depthMapHeightUniName[i]); | 212 &depthMapHeightUniName[i]); |
204 } | 213 } |
205 | 214 |
215 const char* shBiasUniName = nullptr; | |
216 const char* minVarianceUniName = nullptr; | |
217 | |
218 fBiasingConstantUni = uniformHandler->addUniform(kFragment_GrShaderF lag, | |
219 kFloat_GrSLType, | |
220 kDefault_GrSLPrecis ion, | |
221 "shadowBias", &shBi asUniName); | |
222 fMinVarianceUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | |
223 kFloat_GrSLType, | |
224 kDefault_GrSLPrecision, | |
225 "minVariance", &minVari anceUniName); | |
206 | 226 |
207 const char* widthUniName = nullptr; | 227 const char* widthUniName = nullptr; |
208 const char* heightUniName = nullptr; | 228 const char* heightUniName = nullptr; |
209 | 229 |
210 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 230 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
211 kInt_GrSLType, | 231 kInt_GrSLType, |
212 kDefault_GrSLPrecision, | 232 kDefault_GrSLPrecision, |
213 "width", &widthUniName); | 233 "width", &widthUniName); |
214 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 234 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
215 kInt_GrSLType, | 235 kInt_GrSLType, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 offset.c_str(), lightDirUniName[i]); | 267 offset.c_str(), lightDirUniName[i]); |
248 | 268 |
249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", | 269 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", |
250 scaleVec.c_str(), | 270 scaleVec.c_str(), |
251 widthUniName, heightUniName, | 271 widthUniName, heightUniName, |
252 depthMapWidthUniName[i], depthMapHeight UniName[i]); | 272 depthMapWidthUniName[i], depthMapHeight UniName[i]); |
253 | 273 |
254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", | 274 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", |
255 scaleOffsetVec.c_str(), scaleVec.c_str( )); | 275 scaleOffsetVec.c_str(), scaleVec.c_str( )); |
256 | 276 |
257 | |
258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " | 277 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " |
259 "vec2(%s.x, 0 - %s.y)) " | 278 "vec2(%s.x, 0 - %s.y)) " |
260 " * %s + vec2(0,1) * %s;\n", | 279 " * %s + vec2(0,1) * %s;\n", |
261 | |
262 povCoord.c_str(), offset.c_str(), offse t.c_str(), | 280 povCoord.c_str(), offset.c_str(), offse t.c_str(), |
263 scaleVec.c_str(), scaleOffsetVec.c_str( )); | 281 scaleVec.c_str(), scaleOffsetVec.c_str( )); |
264 | 282 |
265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | 283 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], |
266 povCoord.c_str(), | 284 povCoord.c_str(), |
267 kVec2f_GrSLType); | 285 kVec2f_GrSLType); |
286 | |
287 | |
268 } | 288 } |
269 | 289 |
270 const char* ambientColorUniName = nullptr; | 290 const char* ambientColorUniName = nullptr; |
271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | 291 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , |
272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | 292 kVec3f_GrSLType, kDefa ult_GrSLPrecision, |
273 "AmbientColor", &ambie ntColorUniName); | 293 "AmbientColor", &ambie ntColorUniName); |
274 | 294 |
275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); | 295 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); |
276 | 296 |
277 // Essentially, | 297 // all the normal vectors point straight up |
278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) | |
279 SkString totalLightColor("totalLightColor"); | 298 SkString totalLightColor("totalLightColor"); |
280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); | 299 fragBuilder->codeAppendf("vec3 %s = vec3(0,0,0);", totalLightColor.c _str()); |
300 | |
301 SkString lightProb("lightProbability"); | |
302 fragBuilder->codeAppendf("float %s;", lightProb.c_str()); | |
303 fragBuilder->codeAppendf("float variance;"); | |
304 fragBuilder->codeAppendf("float d;"); | |
281 | 305 |
282 for (int i = 0; i < numLights; i++) { | 306 for (int i = 0; i < numLights; i++) { |
283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | 307 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str()); |
308 | |
309 // 1/512 is less than half a pixel; imperceptible | |
310 fragBuilder->codeAppendf("if (%s.b <= %s.b + 1/512) {", | |
284 povDepth.c_str(), depthMaps[i].c_str()) ; | 311 povDepth.c_str(), depthMaps[i].c_str()) ; |
285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s | 312 if (blurAlgorithm == SkShadowType::kVariance_BlurAlgorithm) { |
286 fragBuilder->codeAppendf("%s += %s.z * %s;", | 313 fragBuilder->codeAppendf("vec2 moments = vec2(%s.b * 255, % s.g * 255 * 256 );", |
314 depthMaps[i].c_str(), depthMaps[i]. c_str()); | |
315 | |
316 // variance biasing lessens light bleeding | |
317 fragBuilder->codeAppendf("variance = max(moments.y - (moment s.x * moments.x)," | |
318 "%s);", minVarianceUniName) ; | |
319 | |
320 fragBuilder->codeAppendf("d = (%s.b * 255) - moments.x;", po vDepth.c_str()); | |
321 fragBuilder->codeAppendf("%s = (variance / (variance + d * d ));", | |
322 lightProb.c_str()); | |
323 | |
324 SkString clamp("clamp"); | |
325 clamp.appendf("%d", i); | |
326 | |
327 // choosing between light artifacts or correct shape shadows | |
328 // linstep | |
329 fragBuilder->codeAppendf("float %s = clamp((%s - %s) / (1 - %s), 0, 1);", | |
330 clamp.c_str(), lightProb.c_str(), | |
331 shBiasUniName, shBiasUniName); | |
332 | |
333 fragBuilder->codeAppendf("%s = %s;", | |
334 lightProb.c_str(), clamp.c_str()); | |
335 } else { | |
336 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | |
337 povDepth.c_str(), depthMaps[i].c_st r()); | |
338 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str()); | |
339 fragBuilder->codeAppendf("} else { %s = 0; }", lightProb.c_s tr()); | |
340 } | |
341 | |
342 // VSM: The curved shadows near plane edges are mostly light ble eding. | |
343 fragBuilder->codeAppendf("}"); | |
344 | |
345 fragBuilder->codeAppendf("%s += dot(vec3(0,0,1), %s) * %s * %s;" , | |
287 totalLightColor.c_str(), | 346 totalLightColor.c_str(), |
288 lightDirUniName[i], | 347 lightDirUniName[i], |
289 lightColorUniName[i]); | 348 lightColorUniName[i], |
290 fragBuilder->codeAppendf("}"); | 349 lightProb.c_str()); |
291 } | 350 } |
292 | 351 |
293 fragBuilder->codeAppendf("%s += %s;", | 352 fragBuilder->codeAppendf("%s += %s;", totalLightColor.c_str(), ambie ntColorUniName); |
294 totalLightColor.c_str(), | |
295 ambientColorUniName); | |
296 | 353 |
297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", | 354 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", |
298 totalLightColor.c_str()); | 355 totalLightColor.c_str()); |
299 | 356 |
300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); | 357 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); |
301 } | 358 } |
302 | 359 |
303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 360 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
304 GrProcessorKeyBuilder* b) { | 361 GrProcessorKeyBuilder* b) { |
305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 362 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
306 b->add32(shadowFP.fNumDirLights); | 363 b->add32(shadowFP.fNumDirLights); |
364 b->add32(shadowFP.fShadowType.fBlurAlgorithm); | |
307 } | 365 } |
308 | 366 |
309 protected: | 367 protected: |
310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { | 368 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
311 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); | 369 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); |
312 | 370 |
313 fNumDirLights = shadowFP.numLights(); | 371 for (int i = 0; i < shadowFP.fNumDirLights; i++) { |
314 | |
315 for (int i = 0; i < fNumDirLights; i++) { | |
316 const SkVector3& lightDir = shadowFP.lightDir(i); | 372 const SkVector3& lightDir = shadowFP.lightDir(i); |
317 if (lightDir != fLightDir[i]) { | 373 if (lightDir != fLightDir[i]) { |
318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | 374 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); |
319 fLightDir[i] = lightDir; | 375 fLightDir[i] = lightDir; |
320 } | 376 } |
321 const SkColor3f& lightColor = shadowFP.lightColor(i); | 377 const SkColor3f& lightColor = shadowFP.lightColor(i); |
322 if (lightColor != fLightColor[i]) { | 378 if (lightColor != fLightColor[i]) { |
323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | 379 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); |
324 fLightColor[i] = lightColor; | 380 fLightColor[i] = lightColor; |
325 } | 381 } |
326 | 382 |
327 int depthMapWidth = shadowFP.depthMapWidth(i); | 383 int depthMapWidth = shadowFP.depthMapWidth(i); |
328 if (depthMapWidth != fDepthMapWidth[i]) { | 384 if (depthMapWidth != fDepthMapWidth[i]) { |
329 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); | 385 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); |
330 fDepthMapWidth[i] = depthMapWidth; | 386 fDepthMapWidth[i] = depthMapWidth; |
331 } | 387 } |
332 int depthMapHeight = shadowFP.depthMapHeight(i); | 388 int depthMapHeight = shadowFP.depthMapHeight(i); |
333 if (depthMapHeight != fDepthMapHeight[i]) { | 389 if (depthMapHeight != fDepthMapHeight[i]) { |
334 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); | 390 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); |
335 fDepthMapHeight[i] = depthMapHeight; | 391 fDepthMapHeight[i] = depthMapHeight; |
336 } | 392 } |
337 } | 393 } |
338 | 394 |
395 SkScalar biasingConstant = shadowFP.shadowType().fBiasingConstant; | |
396 if (biasingConstant != fBiasingConstant) { | |
397 pdman.set1f(fBiasingConstantUni, biasingConstant); | |
398 fBiasingConstant = biasingConstant; | |
399 } | |
400 | |
401 SkScalar minVariance = shadowFP.shadowType().fMinVariance; | |
402 if (minVariance != fMinVariance) { | |
403 pdman.set1f(fMinVarianceUni, minVariance); | |
404 fMinVariance = minVariance; | |
405 } | |
406 | |
339 int width = shadowFP.width(); | 407 int width = shadowFP.width(); |
340 if (width != fWidth) { | 408 if (width != fWidth) { |
341 pdman.set1i(fWidthUni, width); | 409 pdman.set1i(fWidthUni, width); |
342 fWidth = width; | 410 fWidth = width; |
343 } | 411 } |
344 int height = shadowFP.height(); | 412 int height = shadowFP.height(); |
345 if (height != fHeight) { | 413 if (height != fHeight) { |
346 pdman.set1i(fHeightUni, height); | 414 pdman.set1i(fHeightUni, height); |
347 fHeight = height; | 415 fHeight = height; |
348 } | 416 } |
(...skipping 20 matching lines...) Expand all Loading... | |
369 | 437 |
370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 438 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
371 GrGLSLProgramDataManager::UniformHandle | 439 GrGLSLProgramDataManager::UniformHandle |
372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; | 440 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; |
373 | 441 |
374 int fWidth; | 442 int fWidth; |
375 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 443 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
376 int fHeight; | 444 int fHeight; |
377 GrGLSLProgramDataManager::UniformHandle fHeightUni; | 445 GrGLSLProgramDataManager::UniformHandle fHeightUni; |
378 | 446 |
447 SkScalar fBiasingConstant; | |
448 GrGLSLProgramDataManager::UniformHandle fBiasingConstantUni; | |
449 SkScalar fMinVariance; | |
450 GrGLSLProgramDataManager::UniformHandle fMinVarianceUni; | |
451 | |
379 SkColor3f fAmbientColor; | 452 SkColor3f fAmbientColor; |
380 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | 453 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
381 | |
382 int fNumDirLights; | |
383 }; | 454 }; |
384 | 455 |
385 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { | 456 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
386 GLSLShadowFP::GenKey(*this, caps, b); | 457 GLSLShadowFP::GenKey(*this, caps, b); |
387 } | 458 } |
388 | 459 |
389 const char* name() const override { return "shadowFP"; } | 460 const char* name() const override { return "shadowFP"; } |
390 | 461 |
391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 462 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
392 inout->mulByUnknownFourComponents(); | 463 inout->mulByUnknownFourComponents(); |
(...skipping 13 matching lines...) Expand all Loading... | |
406 SkASSERT(i < fNumDirLights); | 477 SkASSERT(i < fNumDirLights); |
407 return fDepthMapWidth[i]; | 478 return fDepthMapWidth[i]; |
408 } | 479 } |
409 int depthMapHeight(int i) const { | 480 int depthMapHeight(int i) const { |
410 SkASSERT(i < fNumDirLights); | 481 SkASSERT(i < fNumDirLights); |
411 return fDepthMapHeight[i]; | 482 return fDepthMapHeight[i]; |
412 } | 483 } |
413 int width() const {return fWidth; } | 484 int width() const {return fWidth; } |
414 int height() const {return fHeight; } | 485 int height() const {return fHeight; } |
415 | 486 |
487 SkShadowType shadowType() const {return fShadowType; } | |
jvanverth1
2016/08/15 16:37:17
return const SkShadowType&
vjiaoblack
2016/08/15 17:43:31
Done.
| |
488 | |
416 private: | 489 private: |
417 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } | 490 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } |
418 | 491 |
419 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 492 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
420 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 493 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { | 494 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { |
422 return false; | 495 return false; |
423 } | 496 } |
424 | 497 |
425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { | 498 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { |
(...skipping 21 matching lines...) Expand all Loading... | |
447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 520 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; | 521 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; |
449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; | 522 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; |
450 | 523 |
451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 524 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 525 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
453 | 526 |
454 int fHeight; | 527 int fHeight; |
455 int fWidth; | 528 int fWidth; |
456 | 529 |
530 SkShadowType fShadowType; | |
531 | |
457 SkColor3f fAmbientColor; | 532 SkColor3f fAmbientColor; |
458 }; | 533 }; |
459 | 534 |
460 //////////////////////////////////////////////////////////////////////////// | 535 //////////////////////////////////////////////////////////////////////////// |
461 | 536 |
462 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const { | 537 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const { |
463 | 538 |
464 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor (fpargs); | 539 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor (fpargs); |
465 | 540 |
466 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f pargs); | 541 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f pargs); |
467 | 542 |
468 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP), | 543 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP), |
469 std::move(diffuse FP), | 544 std::move(diffuse FP), |
470 std::move(fLights ), | 545 std::move(fLights ), |
471 fDiffuseWidth, fD iffuseHeight, | 546 fDiffuseWidth, fD iffuseHeight, |
472 fpargs.fContext); | 547 fShadowType, fpar gs.fContext); |
473 return shadowfp; | 548 return shadowfp; |
474 } | 549 } |
475 | 550 |
476 | 551 |
477 #endif | 552 #endif |
478 | 553 |
479 //////////////////////////////////////////////////////////////////////////// | 554 //////////////////////////////////////////////////////////////////////////// |
480 | 555 |
481 bool SkShadowShaderImpl::isOpaque() const { | 556 bool SkShadowShaderImpl::isOpaque() const { |
482 return fDiffuseShader->isOpaque(); | 557 return fDiffuseShader->isOpaque(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
619 | 694 |
620 SkLights::Light light = SkLights::Light(color, dir); | 695 SkLights::Light light = SkLights::Light(color, dir); |
621 light.setShadowMap(depthMap); | 696 light.setShadowMap(depthMap); |
622 | 697 |
623 builder.add(light); | 698 builder.add(light); |
624 } | 699 } |
625 } | 700 } |
626 | 701 |
627 sk_sp<SkLights> lights(builder.finish()); | 702 sk_sp<SkLights> lights(builder.finish()); |
628 | 703 |
704 SkShadowType sType; | |
705 sType.fMinVariance = buf.readScalar(); | |
706 sType.fBiasingConstant = buf.readScalar(); | |
707 sType.fBlurAlgorithm = (SkShadowType::BlurAlgorithm) buf.readInt(); | |
708 sType.fShadowRadius = buf.readScalar(); | |
709 | |
629 int diffuseWidth = buf.readInt(); | 710 int diffuseWidth = buf.readInt(); |
630 int diffuseHeight = buf.readInt(); | 711 int diffuseHeight = buf.readInt(); |
631 | 712 |
632 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); | 713 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); |
633 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); | 714 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); |
634 | 715 |
635 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 716 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
636 std::move(diffuseShader), | 717 std::move(diffuseShader), |
637 std::move(lights), | 718 std::move(lights), |
638 diffuseWidth, diffuseHeight); | 719 diffuseWidth, diffuseHeight, |
720 sType); | |
639 } | 721 } |
640 | 722 |
641 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { | 723 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { |
642 this->INHERITED::flatten(buf); | 724 this->INHERITED::flatten(buf); |
643 | 725 |
644 buf.writeInt(fLights->numLights()); | 726 buf.writeInt(fLights->numLights()); |
645 | 727 |
646 for (int l = 0; l < fLights->numLights(); ++l) { | 728 for (int l = 0; l < fLights->numLights(); ++l) { |
647 const SkLights::Light& light = fLights->light(l); | 729 const SkLights::Light& light = fLights->light(l); |
648 | 730 |
649 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | 731 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
650 | 732 |
651 buf.writeBool(isAmbient); | 733 buf.writeBool(isAmbient); |
652 buf.writeScalarArray(&light.color().fX, 3); | 734 buf.writeScalarArray(&light.color().fX, 3); |
653 if (!isAmbient) { | 735 if (!isAmbient) { |
654 buf.writeScalarArray(&light.dir().fX, 3); | 736 buf.writeScalarArray(&light.dir().fX, 3); |
655 } | 737 } |
656 | 738 |
657 buf.writeImage(light.getShadowMap()); | 739 buf.writeImage(light.getShadowMap()); |
658 } | 740 } |
659 | 741 |
742 buf.writeScalar(fShadowType.fMinVariance); | |
743 buf.writeScalar(fShadowType.fBiasingConstant); | |
744 buf.writeInt(fShadowType.fBlurAlgorithm); | |
745 buf.writeScalar(fShadowType.fShadowRadius); | |
746 | |
660 buf.writeInt(fDiffuseWidth); | 747 buf.writeInt(fDiffuseWidth); |
661 buf.writeInt(fDiffuseHeight); | 748 buf.writeInt(fDiffuseHeight); |
662 | 749 |
663 buf.writeFlattenable(fPovDepthShader.get()); | 750 buf.writeFlattenable(fPovDepthShader.get()); |
664 buf.writeFlattenable(fDiffuseShader.get()); | 751 buf.writeFlattenable(fDiffuseShader.get()); |
665 } | 752 } |
666 | 753 |
667 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { | 754 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { |
668 return sizeof(ShadowShaderContext); | 755 return sizeof(ShadowShaderContext); |
669 } | 756 } |
(...skipping 25 matching lines...) Expand all Loading... | |
695 | 782 |
696 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext, | 783 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext, |
697 heapAllocated); | 784 heapAllocated); |
698 } | 785 } |
699 | 786 |
700 /////////////////////////////////////////////////////////////////////////////// | 787 /////////////////////////////////////////////////////////////////////////////// |
701 | 788 |
702 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, | 789 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, |
703 sk_sp<SkShader> diffuseShader, | 790 sk_sp<SkShader> diffuseShader, |
704 sk_sp<SkLights> lights, | 791 sk_sp<SkLights> lights, |
705 int diffuseWidth, int diffuseHeight) { | 792 int diffuseWidth, int diffuseHeight, |
793 SkShadowType sType) { | |
jvanverth1
2016/08/15 16:37:17
const SkShadowType&
vjiaoblack
2016/08/15 17:43:31
Done.
| |
706 if (!povDepthShader || !diffuseShader) { | 794 if (!povDepthShader || !diffuseShader) { |
707 // TODO: Use paint's color in absence of a diffuseShader | 795 // TODO: Use paint's color in absence of a diffuseShader |
708 // TODO: Use a default implementation of normalSource instead | 796 // TODO: Use a default implementation of normalSource instead |
709 return nullptr; | 797 return nullptr; |
710 } | 798 } |
711 | 799 |
712 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 800 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
713 std::move(diffuseShader), | 801 std::move(diffuseShader), |
714 std::move(lights), | 802 std::move(lights), |
715 diffuseWidth, diffuseHeight); | 803 diffuseWidth, diffuseHeight, |
804 sType); | |
716 } | 805 } |
717 | 806 |
718 /////////////////////////////////////////////////////////////////////////////// | 807 /////////////////////////////////////////////////////////////////////////////// |
719 | 808 |
720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 809 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 810 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 811 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
723 | 812 |
724 /////////////////////////////////////////////////////////////////////////////// | 813 /////////////////////////////////////////////////////////////////////////////// |
725 | 814 |
726 #endif | 815 #endif |
OLD | NEW |