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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 class SkLightingShaderImpl : public SkShader { | 42 class SkLightingShaderImpl : public SkShader { |
43 public: | 43 public: |
44 /** Create a new lighting shader that uses the provided normal map and | 44 /** Create a new lighting shader that uses the provided normal map and |
45 lights to light the diffuse bitmap. | 45 lights to light the diffuse bitmap. |
46 @param diffuseShader the shader that provides the diffuse colors | 46 @param diffuseShader the shader that provides the diffuse colors |
47 @param normalSource the source of normals for lighting computation | 47 @param normalSource the source of normals for lighting computation |
48 @param lights the lights applied to the geometry | 48 @param lights the lights applied to the geometry |
49 */ | 49 */ |
50 SkLightingShaderImpl(sk_sp<SkShader> diffuseShader, | 50 SkLightingShaderImpl(sk_sp<SkShader> diffuseShader, |
51 sk_sp<SkNormalSource> normalSource, | 51 sk_sp<SkNormalSource> normalSource, |
52 sk_sp<SkLights> lights) | 52 const sk_sp<SkLights> lights) |
53 : fDiffuseShader(std::move(diffuseShader)) | 53 : fDiffuseShader(std::move(diffuseShader)) |
54 , fNormalSource(std::move(normalSource)) | 54 , fNormalSource(std::move(normalSource)) |
55 , fLights(std::move(lights)) {} | 55 , fLights(std::move(lights)) {} |
56 | 56 |
57 bool isOpaque() const override; | 57 bool isOpaque() const override; |
58 | 58 |
59 #if SK_SUPPORT_GPU | 59 #if SK_SUPPORT_GPU |
60 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, | 60 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, |
61 const SkMatrix& viewM, | 61 const SkMatrix& viewM, |
62 const SkMatrix* localMatrix, | 62 const SkMatrix* localMatrix, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 127 |
128 // fuse all ambient lights into a single one | 128 // fuse all ambient lights into a single one |
129 fAmbientColor.set(0.0f, 0.0f, 0.0f); | 129 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
130 for (int i = 0; i < lights->numLights(); ++i) { | 130 for (int i = 0; i < lights->numLights(); ++i) { |
131 if (SkLights::Light::kAmbient_LightType == lights->light(i).type())
{ | 131 if (SkLights::Light::kAmbient_LightType == lights->light(i).type())
{ |
132 fAmbientColor += lights->light(i).color(); | 132 fAmbientColor += lights->light(i).color(); |
133 } else { | 133 } else { |
134 // TODO: handle more than one of these | 134 // TODO: handle more than one of these |
135 fLightColor = lights->light(i).color(); | 135 fLightColor = lights->light(i).color(); |
136 fLightDir = lights->light(i).dir(); | 136 fLightDir = lights->light(i).dir(); |
137 // TODO get the handle to the shadow map if there is one | |
138 } | 137 } |
139 } | 138 } |
140 | 139 |
141 this->registerChildProcessor(std::move(normalFP)); | 140 this->registerChildProcessor(std::move(normalFP)); |
142 this->initClassID<LightingFP>(); | 141 this->initClassID<LightingFP>(); |
143 } | 142 } |
144 | 143 |
145 class GLSLLightingFP : public GrGLSLFragmentProcessor { | 144 class GLSLLightingFP : public GrGLSLFragmentProcessor { |
146 public: | 145 public: |
147 GLSLLightingFP() { | 146 GLSLLightingFP() { |
(...skipping 22 matching lines...) Expand all Loading... |
170 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag
, | 169 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag
, |
171 kVec3f_GrSLType, kDefa
ult_GrSLPrecision, | 170 kVec3f_GrSLType, kDefa
ult_GrSLPrecision, |
172 "AmbientColor", &ambie
ntColorUniName); | 171 "AmbientColor", &ambie
ntColorUniName); |
173 | 172 |
174 fragBuilder->codeAppendf("vec4 diffuseColor = %s;", args.fInputColor
); | 173 fragBuilder->codeAppendf("vec4 diffuseColor = %s;", args.fInputColor
); |
175 | 174 |
176 SkString dstNormalName("dstNormal"); | 175 SkString dstNormalName("dstNormal"); |
177 this->emitChild(0, nullptr, &dstNormalName, args); | 176 this->emitChild(0, nullptr, &dstNormalName, args); |
178 | 177 |
179 fragBuilder->codeAppendf("vec3 normal = %s.xyz;", dstNormalName.c_st
r()); | 178 fragBuilder->codeAppendf("vec3 normal = %s.xyz;", dstNormalName.c_st
r()); |
180 | |
181 // TODO: make this a loop and modulate the contribution from each li
ght | |
182 // based on the shadow map | |
183 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);", |
184 lightDirUniName); | 180 lightDirUniName); |
185 // diffuse light | 181 // diffuse light |
186 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); | 182 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); |
187 // ambient light | 183 // ambient light |
188 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); | 184 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); |
189 fragBuilder->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", a
rgs.fOutputColor); | 185 fragBuilder->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", a
rgs.fOutputColor); |
190 } | 186 } |
191 | 187 |
192 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 188 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 std::move(lights)); | 501 std::move(lights)); |
506 } | 502 } |
507 | 503 |
508 /////////////////////////////////////////////////////////////////////////////// | 504 /////////////////////////////////////////////////////////////////////////////// |
509 | 505 |
510 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 506 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
511 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 507 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
512 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 508 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
513 | 509 |
514 /////////////////////////////////////////////////////////////////////////////// | 510 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |