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