OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapProcShader.h" | 8 #include "SkBitmapProcShader.h" |
9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 this->emitChild(0, nullptr, &dstNormalName, args); | 173 this->emitChild(0, nullptr, &dstNormalName, args); |
174 | 174 |
175 fragBuilder->codeAppendf("vec3 normal = %s.xyz;", dstNormalName.c_st
r()); | 175 fragBuilder->codeAppendf("vec3 normal = %s.xyz;", dstNormalName.c_st
r()); |
176 | 176 |
177 // TODO: make this a loop and modulate the contribution from each li
ght | 177 // TODO: make this a loop and modulate the contribution from each li
ght |
178 // based on the shadow map | 178 // based on the shadow map |
179 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0,
1.0);", | 179 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0,
1.0);", |
180 lightDirUniName); | 180 lightDirUniName); |
181 // diffuse light | 181 // diffuse light |
182 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); | 182 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); |
183 // ambient light (multiplied by input color's alpha because we're wo
rking in premul'd | 183 // ambient light |
184 // space) | 184 fragBuilder->codeAppendf("result += %s * diffuseColor.rgb;", ambient
ColorUniName); |
185 fragBuilder->codeAppendf("result += diffuseColor.a * %s;", ambientCo
lorUniName); | |
186 | 185 |
187 // Clamping to alpha (equivalent to an unpremul'd clamp to 1.0) | 186 // Clamping to alpha (equivalent to an unpremul'd clamp to 1.0) |
188 fragBuilder->codeAppendf("%s = vec4(clamp(result.rgb, 0.0, diffuseCo
lor.a), " | 187 fragBuilder->codeAppendf("%s = vec4(clamp(result.rgb, 0.0, diffuseCo
lor.a), " |
189 "diffuseColor.a);", args.fOutputC
olor); | 188 "diffuseColor.a);", args.fOutputC
olor); |
190 } | 189 } |
191 | 190 |
192 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 191 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
193 GrProcessorKeyBuilder* b) { | 192 GrProcessorKeyBuilder* b) { |
194 // const LightingFP& lightingFP = proc.cast<LightingFP>(); | 193 // const LightingFP& lightingFP = proc.cast<LightingFP>(); |
195 // only one shader generated currently | 194 // only one shader generated currently |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 for (int i = 0; i < n; ++i) { | 371 for (int i = 0; i < n; ++i) { |
373 if (fDiffuseContext) { | 372 if (fDiffuseContext) { |
374 diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | 373 diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); |
375 } | 374 } |
376 | 375 |
377 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 376 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
378 // This is all done in linear unpremul color space (each component 0
..255.0f though) | 377 // This is all done in linear unpremul color space (each component 0
..255.0f though) |
379 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | 378 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
380 const SkLights::Light& light = lightShader.fLights->light(l); | 379 const SkLights::Light& light = lightShader.fLights->light(l); |
381 | 380 |
382 if (SkLights::Light::kAmbient_LightType == light.type()) { | 381 SkScalar illuminanceScalingFactor = 1.0f; |
383 accum += light.color().makeScale(255.0f); | 382 |
384 } else { | 383 if (SkLights::Light::kDirectional_LightType == light.type()) { |
385 SkScalar NdotL = normals[i].dot(light.dir()); | 384 illuminanceScalingFactor = normals[i].dot(light.dir()); |
386 if (NdotL < 0.0f) { | 385 if (illuminanceScalingFactor < 0.0f) { |
387 NdotL = 0.0f; | 386 illuminanceScalingFactor = 0.0f; |
388 } | 387 } |
| 388 } |
389 | 389 |
390 accum.fX += light.color().fX * SkColorGetR(diffColor) * Ndot
L; | 390 accum.fX += light.color().fX * SkColorGetR(diffColor) * illumina
nceScalingFactor; |
391 accum.fY += light.color().fY * SkColorGetG(diffColor) * Ndot
L; | 391 accum.fY += light.color().fY * SkColorGetG(diffColor) * illumina
nceScalingFactor; |
392 accum.fZ += light.color().fZ * SkColorGetB(diffColor) * Ndot
L; | 392 accum.fZ += light.color().fZ * SkColorGetB(diffColor) * illumina
nceScalingFactor; |
393 } | |
394 } | 393 } |
395 | 394 |
396 // convert() premultiplies the accumulate color with alpha | 395 // convert() premultiplies the accumulate color with alpha |
397 result[i] = convert(accum, SkColorGetA(diffColor)); | 396 result[i] = convert(accum, SkColorGetA(diffColor)); |
398 } | 397 } |
399 | 398 |
400 result += n; | 399 result += n; |
401 x += n; | 400 x += n; |
402 count -= n; | 401 count -= n; |
403 } while (count > 0); | 402 } while (count > 0); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 std::move(lights)); | 524 std::move(lights)); |
526 } | 525 } |
527 | 526 |
528 /////////////////////////////////////////////////////////////////////////////// | 527 /////////////////////////////////////////////////////////////////////////////// |
529 | 528 |
530 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 529 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
531 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 530 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
532 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 531 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
533 | 532 |
534 /////////////////////////////////////////////////////////////////////////////// | 533 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |