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" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 | 110 |
111 // fuse all ambient lights into a single one | 111 // fuse all ambient lights into a single one |
112 fAmbientColor.set(0.0f, 0.0f, 0.0f); | 112 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
113 | 113 |
114 fNumDirLights = 0; // refers to directional lights. | 114 fNumDirLights = 0; // refers to directional lights. |
115 for (int i = 0; i < lights->numLights(); ++i) { | 115 for (int i = 0; i < lights->numLights(); ++i) { |
116 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { | 116 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { |
117 fAmbientColor += lights->light(i).color(); | 117 fAmbientColor += lights->light(i).color(); |
118 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { | 118 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { |
119 fLightColor[fNumDirLights] = lights->light(i).color(); | 119 fLightColor[fNumDirLights] = lights->light(i).color(); |
120 fLightDir[fNumDirLights] = lights->light(i).dir(); | 120 if (lights->light(i).type() == SkLights::Light::kPoint_LightType ) { |
121 fLightDir[fNumDirLights] = lights->light(i).pos(); | |
jvanverth1
2016/08/17 19:59:02
fLightDirOrPos?
vjiaoblack
2016/08/18 14:52:19
Done.
| |
122 fLightIntensity[fNumDirLights] = lights->light(i).intensity( ); | |
123 } else { | |
124 fLightDir[fNumDirLights] = lights->light(i).dir(); | |
125 fLightIntensity[fNumDirLights] = 0.0f; | |
126 } | |
127 fIsPointLight[fNumDirLights] = | |
128 lights->light(i).type() == SkLights::Light::kPoint_Light Type; | |
129 | |
121 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh adowMap()); | 130 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh adowMap()); |
122 | 131 |
123 // gets deleted when the ShadowFP is destroyed, and frees the Gr Texture* | 132 // gets deleted when the ShadowFP is destroyed, and frees the Gr Texture* |
124 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR ef(context, | 133 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR ef(context, |
125 GrTextureParams::Clam pNoFilter(), | 134 GrTextureParams::Clam pNoFilter(), |
126 SkSourceGammaTreatmen t::kIgnore)); | 135 SkSourceGammaTreatmen t::kIgnore)); |
127 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get ()); | 136 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get ()); |
128 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); | 137 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); |
129 | 138 |
130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); | 139 fDepthMapHeight[fNumDirLights] = shadowMap->height(); |
(...skipping 17 matching lines...) Expand all Loading... | |
148 | 157 |
149 void emitCode(EmitArgs& args) override { | 158 void emitCode(EmitArgs& args) override { |
150 | 159 |
151 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 160 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 161 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
153 | 162 |
154 // add uniforms | 163 // add uniforms |
155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | 164 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; |
156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); | 165 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); |
157 | 166 |
167 bool isPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
jvanverth1
2016/08/17 19:59:02
I'm wondering if it might be worthwhile to change
vjiaoblack
2016/08/18 14:52:19
Done.
| |
168 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
169 isPointLight[i] = args.fFp.cast<ShadowFP>().fIsPointLight[i]; | |
170 } | |
171 | |
158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 172 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
173 const char* isPointLightUniName[SkShadowShader::kMaxNonAmbientLights ] = {nullptr}; | |
159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 174 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
175 const char* lightIntensityUniName[SkShadowShader::kMaxNonAmbientLigh ts] = {nullptr}; | |
160 | 176 |
161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] | 177 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] = {nullptr}; |
162 = {nullptr}; | 178 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] = {nullptr}; |
163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] | |
164 = {nullptr}; | |
165 | 179 |
166 SkString lightDirUniNameBase("lightDir"); | 180 SkString lightDirUniNameBase("lightDir"); |
167 SkString lightColorUniNameBase("lightColor"); | 181 SkString lightColorUniNameBase("lightColor"); |
182 SkString lightIntensityUniNameBase("lightIntensity"); | |
183 SkString isPointLightUniNameBase("isPointLight"); | |
168 | 184 |
169 SkString depthMapWidthUniNameBase("dmapWidth"); | 185 SkString depthMapWidthUniNameBase("dmapWidth"); |
170 SkString depthMapHeightUniNameBase("dmapHeight"); | 186 SkString depthMapHeightUniNameBase("dmapHeight"); |
171 | 187 |
172 for (int i = 0; i < numLights; i++) { | 188 for (int i = 0; i < numLights; i++) { |
173 SkString lightDirUniNameStr(lightDirUniNameBase); | 189 SkString lightDirUniNameStr(lightDirUniNameBase); |
174 lightDirUniNameStr.appendf("%d", i); | 190 lightDirUniNameStr.appendf("%d", i); |
175 SkString lightColorUniNameStr(lightColorUniNameBase); | 191 SkString lightColorUniNameStr(lightColorUniNameBase); |
176 lightColorUniNameStr.appendf("%d", i); | 192 lightColorUniNameStr.appendf("%d", i); |
193 SkString lightIntensityUniNameStr(lightIntensityUniNameBase); | |
194 lightIntensityUniNameStr.appendf("%d", i); | |
195 SkString isPointLightUniNameStr(isPointLightUniNameBase); | |
196 isPointLightUniNameStr.appendf("%d", i); | |
177 | 197 |
178 SkString depthMapWidthUniNameStr(depthMapWidthUniNameBase); | 198 SkString depthMapWidthUniNameStr(depthMapWidthUniNameBase); |
179 depthMapWidthUniNameStr.appendf("%d", i); | 199 depthMapWidthUniNameStr.appendf("%d", i); |
180 SkString depthMapHeightUniNameStr(depthMapHeightUniNameBase); | 200 SkString depthMapHeightUniNameStr(depthMapHeightUniNameBase); |
181 depthMapHeightUniNameStr.appendf("%d", i); | 201 depthMapHeightUniNameStr.appendf("%d", i); |
182 | 202 |
183 fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderF lag, | 203 fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderF lag, |
184 kVec3f_GrSLType, | 204 kVec3f_GrSLType, |
185 kDefault_GrSLPrecis ion, | 205 kDefault_GrSLPrecis ion, |
186 lightDirUniNameStr. c_str(), | 206 lightDirUniNameStr. c_str(), |
187 &lightDirUniName[i] ); | 207 &lightDirUniName[i] ); |
208 fIsPointLightUni[i] = uniformHandler->addUniform(kFragment_GrSha derFlag, | |
209 kFloat_GrSLType , | |
210 kDefault_GrSLPr ecision, | |
211 isPointLightUni NameStr.c_str(), | |
212 &isPointLightUn iName[i]); | |
188 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, | 213 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, |
189 kVec3f_GrSLType, | 214 kVec3f_GrSLType, |
190 kDefault_GrSLPrec ision, | 215 kDefault_GrSLPrec ision, |
191 lightColorUniName Str.c_str(), | 216 lightColorUniName Str.c_str(), |
192 &lightColorUniNam e[i]); | 217 &lightColorUniNam e[i]); |
218 fLightIntensityUni[i] = | |
219 uniformHandler->addUniform(kFragment_GrShaderFlag, | |
220 kFloat_GrSLType, | |
221 kDefault_GrSLPrecision, | |
222 lightIntensityUniNameStr.c_st r(), | |
223 &lightIntensityUniName[i]); | |
193 | 224 |
194 fDepthMapWidthUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 225 fDepthMapWidthUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
195 kInt_GrSLType, | 226 kInt_GrSLType, |
196 kDefault_GrSLPrecision, | 227 kDefault_GrSLPrecision, |
197 depthMapWidthUniNameStr.c_str (), | 228 depthMapWidthUniNameStr.c_str (), |
198 &depthMapWidthUniName[i]); | 229 &depthMapWidthUniName[i]); |
199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 230 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
200 kInt_GrSLType, | 231 kInt_GrSLType, |
201 kDefault_GrSLPrecision, | 232 kDefault_GrSLPrecision, |
202 depthMapHeightUniNameStr.c_st r(), | 233 depthMapHeightUniNameStr.c_st r(), |
203 &depthMapHeightUniName[i]); | 234 &depthMapHeightUniName[i]); |
204 } | 235 } |
205 | 236 |
206 | |
207 const char* widthUniName = nullptr; | 237 const char* widthUniName = nullptr; |
208 const char* heightUniName = nullptr; | 238 const char* heightUniName = nullptr; |
209 | 239 |
210 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 240 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
211 kInt_GrSLType, | 241 kInt_GrSLType, |
212 kDefault_GrSLPrecision, | 242 kDefault_GrSLPrecision, |
213 "width", &widthUniName); | 243 "width", &widthUniName); |
214 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 244 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
215 kInt_GrSLType, | 245 kInt_GrSLType, |
216 kDefault_GrSLPrecision, | 246 kDefault_GrSLPrecision, |
217 "height", &heightUniName); | 247 "height", &heightUniName); |
218 | 248 |
219 | |
220 SkString povDepth("povDepth"); | 249 SkString povDepth("povDepth"); |
221 this->emitChild(0, nullptr, &povDepth, args); | 250 this->emitChild(0, nullptr, &povDepth, args); |
222 | 251 |
223 SkString diffuseColor("inDiffuseColor"); | 252 SkString diffuseColor("inDiffuseColor"); |
224 this->emitChild(1, nullptr, &diffuseColor, args); | 253 this->emitChild(1, nullptr, &diffuseColor, args); |
225 | 254 |
226 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; | 255 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; |
227 | 256 |
257 // Multiply by 255 to transform from sampler coordinates to world | |
258 // coordinates (since 1 channel is 0xFF) | |
259 fragBuilder->codeAppendf("vec3 worldCor = vec3(vMatrixCoord_0_1_Stag e0 * " | |
260 "vec2(%s, %s), %s.b * 255); \n", | |
261 widthUniName, heightUniName, povDepth.c_str ()); | |
262 | |
228 for (int i = 0; i < numLights; i++) { | 263 for (int i = 0; i < numLights; i++) { |
229 SkString povCoord("povCoord"); | 264 SkString povCoord("povCoord"); |
230 povCoord.appendf("%d", i); | 265 povCoord.appendf("%d", i); |
231 | 266 |
232 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. | 267 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. |
233 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s pace, | 268 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s pace, |
234 // and the / 400 brings it back to a sampler coordinate, 0 - 1 | 269 // and the / vec2(width, height) brings it back to a sampler coo rdinate |
235 // The 400 comes from the shadowmaps GM. | |
236 // TODO use real shadowmaps size | |
237 SkString offset("offset"); | 270 SkString offset("offset"); |
238 offset.appendf("%d", i); | 271 offset.appendf("%d", i); |
239 | 272 |
240 SkString scaleVec("scaleVec"); | 273 SkString scaleVec("scaleVec"); |
241 scaleVec.appendf("%d", i); | 274 scaleVec.appendf("%d", i); |
242 | 275 |
243 SkString scaleOffsetVec("scaleOffsetVec"); | 276 SkString scaleOffsetVec("scaleOffsetVec"); |
244 scaleOffsetVec.appendf("%d", i); | 277 scaleOffsetVec.appendf("%d", i); |
245 | 278 |
246 fragBuilder->codeAppendf("vec2 %s = vec2(%s) * povDepth.b * 255 / 400;\n", | 279 fragBuilder->codeAppendf("vec2 %s;", offset.c_str()); |
247 offset.c_str(), lightDirUniName[i]); | |
248 | 280 |
281 if (isPointLight[i]) { | |
282 fragBuilder->codeAppendf("vec3 fragToLight%d = %s - worldCor ;\n", | |
283 i, lightDirUniName[i]); | |
284 fragBuilder->codeAppendf("float distsq%d = dot(fragToLight%d , fragToLight%d);" | |
285 "fragToLight%d = normalize(fragToLi ght%d);", | |
286 i, i, i, i, i); | |
287 fragBuilder->codeAppendf("%s = -vec2(%s.x - worldCor.x, worl dCor.y - %s.y)*" | |
288 "(povDepth.b) / vec2(%s, %s) ;\n", | |
jvanverth1
2016/08/17 19:59:02
I don't like these divides, but can live with it f
vjiaoblack
2016/08/18 14:52:19
Done.
| |
289 offset.c_str(), lightDirUniName[i], | |
290 lightDirUniName[i], widthUniName, h eightUniName); | |
291 } else { | |
292 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * 255 / vec2(%s, %s);\n", | |
293 offset.c_str(), lightDirUniName[i], | |
294 widthUniName, heightUniName); | |
295 } | |
249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", | 296 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", |
250 scaleVec.c_str(), | 297 scaleVec.c_str(), |
251 widthUniName, heightUniName, | 298 widthUniName, heightUniName, |
252 depthMapWidthUniName[i], depthMapHeight UniName[i]); | 299 depthMapWidthUniName[i], depthMapHeight UniName[i]); |
253 | 300 |
254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", | 301 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", |
255 scaleOffsetVec.c_str(), scaleVec.c_str( )); | 302 scaleOffsetVec.c_str(), scaleVec.c_str( )); |
256 | 303 |
257 | |
258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " | 304 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " |
259 "vec2(%s.x, 0 - %s.y)) " | 305 "vec2(%s.x, 0 - %s.y)) " |
jvanverth1
2016/08/17 19:59:02
Indent
vjiaoblack
2016/08/18 14:52:19
Done.
| |
260 " * %s + vec2(0,1) * %s;\n", | 306 " * %s + vec2(0,1) * %s;\n", |
261 | 307 povCoord.c_str(), offset.c_str(), o ffset.c_str(), |
262 povCoord.c_str(), offset.c_str(), offse t.c_str(), | 308 scaleVec.c_str(), scaleOffsetVec.c_ str()); |
263 scaleVec.c_str(), scaleOffsetVec.c_str( )); | |
264 | 309 |
265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | 310 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], |
266 povCoord.c_str(), | 311 povCoord.c_str(), |
267 kVec2f_GrSLType); | 312 kVec2f_GrSLType); |
268 } | 313 } |
269 | 314 |
270 const char* ambientColorUniName = nullptr; | 315 const char* ambientColorUniName = nullptr; |
271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | 316 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , |
272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | 317 kVec3f_GrSLType, kDefa ult_GrSLPrecision, |
273 "AmbientColor", &ambie ntColorUniName); | 318 "AmbientColor", &ambie ntColorUniName); |
274 | 319 |
275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); | 320 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); |
276 | 321 |
277 // Essentially, | |
278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) | 322 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) |
279 SkString totalLightColor("totalLightColor"); | 323 SkString totalLightColor("totalLightColor"); |
280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); | 324 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); |
281 | 325 |
282 for (int i = 0; i < numLights; i++) { | 326 for (int i = 0; i < numLights; i++) { |
283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | 327 if (!isPointLight[i]) { |
284 povDepth.c_str(), depthMaps[i].c_str()) ; | 328 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", |
285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s | 329 povDepth.c_str(), depthMaps[i].c_st r()); |
286 fragBuilder->codeAppendf("%s += %s.z * %s;", | 330 // Note that dot(vec3(0,0,1), %s) == %s.z * %s |
287 totalLightColor.c_str(), | 331 fragBuilder->codeAppendf("%s += %s.z * %s;", |
288 lightDirUniName[i], | 332 totalLightColor.c_str(), |
289 lightColorUniName[i]); | 333 lightDirUniName[i], |
290 fragBuilder->codeAppendf("}"); | 334 lightColorUniName[i]); |
335 fragBuilder->codeAppendf("}"); | |
336 } else { | |
337 // fragToLight%d.z is equal to the fragToLight dot the surfa ce normal. | |
338 fragBuilder->codeAppendf("%s += fragToLight%d.z * %s / (1 + distsq%d / (%s * %s));", | |
jvanverth1
2016/08/17 19:59:02
Greater than 100 char line.
vjiaoblack
2016/08/18 14:52:19
Done.
| |
339 totalLightColor.c_str(), i, | |
340 lightColorUniName[i], i, | |
341 lightIntensityUniName[i], | |
342 lightIntensityUniName[i]); | |
343 } | |
291 } | 344 } |
292 | 345 |
293 fragBuilder->codeAppendf("%s += %s;", | 346 fragBuilder->codeAppendf("%s += %s;", |
294 totalLightColor.c_str(), | 347 totalLightColor.c_str(), |
295 ambientColorUniName); | 348 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 int isPL = 0; | |
361 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
362 isPL = isPL | ((shadowFP.fIsPointLight[i] ? 1 : 0) << i); | |
363 } | |
364 b->add32(isPL); | |
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 fNumDirLights = shadowFP.numLights(); |
314 | 372 |
315 for (int i = 0; i < fNumDirLights; i++) { | 373 for (int i = 0; i < fNumDirLights; i++) { |
374 bool isPoint = shadowFP.isPointLight(i); | |
375 SkScalar isPointf = isPoint ? 1.0f : 0.0f; | |
376 pdman.set1f(fIsPointLightUni[i], isPointf); | |
377 fIsPointLight[i] = isPoint; | |
378 | |
316 const SkVector3& lightDir = shadowFP.lightDir(i); | 379 const SkVector3& lightDir = shadowFP.lightDir(i); |
317 if (lightDir != fLightDir[i]) { | 380 if (lightDir != fLightDir[i]) { |
318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | 381 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); |
319 fLightDir[i] = lightDir; | 382 fLightDir[i] = lightDir; |
320 } | 383 } |
384 | |
321 const SkColor3f& lightColor = shadowFP.lightColor(i); | 385 const SkColor3f& lightColor = shadowFP.lightColor(i); |
322 if (lightColor != fLightColor[i]) { | 386 if (lightColor != fLightColor[i]) { |
323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | 387 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); |
324 fLightColor[i] = lightColor; | 388 fLightColor[i] = lightColor; |
325 } | 389 } |
326 | 390 |
391 SkScalar lightIntensity = shadowFP.lightIntensity(i); | |
392 if (lightIntensity != fLightIntensity[i]) { | |
393 pdman.set1f(fLightIntensityUni[i], lightIntensity); | |
394 fLightIntensity[i] = lightIntensity; | |
395 } | |
396 | |
327 int depthMapWidth = shadowFP.depthMapWidth(i); | 397 int depthMapWidth = shadowFP.depthMapWidth(i); |
328 if (depthMapWidth != fDepthMapWidth[i]) { | 398 if (depthMapWidth != fDepthMapWidth[i]) { |
329 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); | 399 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); |
330 fDepthMapWidth[i] = depthMapWidth; | 400 fDepthMapWidth[i] = depthMapWidth; |
331 } | 401 } |
332 int depthMapHeight = shadowFP.depthMapHeight(i); | 402 int depthMapHeight = shadowFP.depthMapHeight(i); |
333 if (depthMapHeight != fDepthMapHeight[i]) { | 403 if (depthMapHeight != fDepthMapHeight[i]) { |
334 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); | 404 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); |
335 fDepthMapHeight[i] = depthMapHeight; | 405 fDepthMapHeight[i] = depthMapHeight; |
336 } | 406 } |
(...skipping 11 matching lines...) Expand all Loading... | |
348 } | 418 } |
349 | 419 |
350 const SkColor3f& ambientColor = shadowFP.ambientColor(); | 420 const SkColor3f& ambientColor = shadowFP.ambientColor(); |
351 if (ambientColor != fAmbientColor) { | 421 if (ambientColor != fAmbientColor) { |
352 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 422 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
353 fAmbientColor = ambientColor; | 423 fAmbientColor = ambientColor; |
354 } | 424 } |
355 } | 425 } |
356 | 426 |
357 private: | 427 private: |
428 bool fIsPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
429 GrGLSLProgramDataManager::UniformHandle | |
430 fIsPointLightUni[SkShadowShader::kMaxNonAmbientLights]; | |
431 | |
358 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; | 432 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
359 GrGLSLProgramDataManager::UniformHandle | 433 GrGLSLProgramDataManager::UniformHandle |
360 fLightDirUni[SkShadowShader::kMaxNonAmbientLights]; | 434 fLightDirUni[SkShadowShader::kMaxNonAmbientLights]; |
361 | 435 |
362 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 436 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
363 GrGLSLProgramDataManager::UniformHandle | 437 GrGLSLProgramDataManager::UniformHandle |
364 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; | 438 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; |
365 | 439 |
440 SkScalar fLightIntensity[SkShadowShader::kMaxNonAmbientLights]; | |
441 GrGLSLProgramDataManager::UniformHandle | |
442 fLightIntensityUni[SkShadowShader::kMaxNonAmbientLights]; | |
443 | |
366 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 444 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
367 GrGLSLProgramDataManager::UniformHandle | 445 GrGLSLProgramDataManager::UniformHandle |
368 fDepthMapWidthUni[SkShadowShader::kMaxNonAmbientLights]; | 446 fDepthMapWidthUni[SkShadowShader::kMaxNonAmbientLights]; |
369 | 447 |
370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 448 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
371 GrGLSLProgramDataManager::UniformHandle | 449 GrGLSLProgramDataManager::UniformHandle |
372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; | 450 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; |
373 | 451 |
374 int fWidth; | 452 int fWidth; |
375 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 453 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
(...skipping 10 matching lines...) Expand all Loading... | |
386 GLSLShadowFP::GenKey(*this, caps, b); | 464 GLSLShadowFP::GenKey(*this, caps, b); |
387 } | 465 } |
388 | 466 |
389 const char* name() const override { return "shadowFP"; } | 467 const char* name() const override { return "shadowFP"; } |
390 | 468 |
391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 469 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
392 inout->mulByUnknownFourComponents(); | 470 inout->mulByUnknownFourComponents(); |
393 } | 471 } |
394 int32_t numLights() const { return fNumDirLights; } | 472 int32_t numLights() const { return fNumDirLights; } |
395 const SkColor3f& ambientColor() const { return fAmbientColor; } | 473 const SkColor3f& ambientColor() const { return fAmbientColor; } |
474 const bool& isPointLight(int i) const { | |
475 SkASSERT(i < fNumDirLights); | |
476 return fIsPointLight[i]; | |
477 } | |
396 const SkVector3& lightDir(int i) const { | 478 const SkVector3& lightDir(int i) const { |
397 SkASSERT(i < fNumDirLights); | 479 SkASSERT(i < fNumDirLights); |
398 return fLightDir[i]; | 480 return fLightDir[i]; |
399 } | 481 } |
400 const SkVector3& lightColor(int i) const { | 482 const SkVector3& lightColor(int i) const { |
401 SkASSERT(i < fNumDirLights); | 483 SkASSERT(i < fNumDirLights); |
402 return fLightColor[i]; | 484 return fLightColor[i]; |
403 } | 485 } |
486 SkScalar lightIntensity(int i) const { | |
487 SkASSERT(i < fNumDirLights); | |
488 return fLightIntensity[i]; | |
489 } | |
404 | 490 |
405 int depthMapWidth(int i) const { | 491 int depthMapWidth(int i) const { |
406 SkASSERT(i < fNumDirLights); | 492 SkASSERT(i < fNumDirLights); |
407 return fDepthMapWidth[i]; | 493 return fDepthMapWidth[i]; |
408 } | 494 } |
409 int depthMapHeight(int i) const { | 495 int depthMapHeight(int i) const { |
410 SkASSERT(i < fNumDirLights); | 496 SkASSERT(i < fNumDirLights); |
411 return fDepthMapHeight[i]; | 497 return fDepthMapHeight[i]; |
412 } | 498 } |
413 int width() const {return fWidth; } | 499 int width() const {return fWidth; } |
414 int height() const {return fHeight; } | 500 int height() const {return fHeight; } |
415 | 501 |
416 private: | 502 private: |
417 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } | 503 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } |
418 | 504 |
419 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 505 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
420 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 506 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { | 507 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { |
422 return false; | 508 return false; |
423 } | 509 } |
424 | 510 |
425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { | 511 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { |
426 return false; | 512 return false; |
427 } | 513 } |
428 | 514 |
429 for (int i = 0; i < fNumDirLights; i++) { | 515 for (int i = 0; i < fNumDirLights; i++) { |
430 if (fLightDir[i] != shadowFP.fLightDir[i] || | 516 if (fLightDir[i] != shadowFP.fLightDir[i] || |
431 fLightColor[i] != shadowFP.fLightColor[i]) { | 517 fLightColor[i] != shadowFP.fLightColor[i] || |
518 fLightIntensity[i] != shadowFP.fLightIntensity[i] || | |
519 fIsPointLight[i] != shadowFP.fIsPointLight[i]) { | |
432 return false; | 520 return false; |
433 } | 521 } |
434 | 522 |
435 if (fDepthMapWidth[i] != shadowFP.fDepthMapWidth[i] || | 523 if (fDepthMapWidth[i] != shadowFP.fDepthMapWidth[i] || |
436 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { | 524 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { |
437 return false; | 525 return false; |
438 } | 526 } |
439 } | 527 } |
440 | 528 |
441 return true; | 529 return true; |
442 } | 530 } |
443 | 531 |
444 int fNumDirLights; | 532 int fNumDirLights; |
445 | 533 |
534 bool fIsPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
446 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; | 535 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 536 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
537 SkScalar fLightIntensity[SkShadowShader::kMaxNonAmbientLights]; | |
448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; | 538 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; |
449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; | 539 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; |
450 | 540 |
451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 541 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 542 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
453 | 543 |
454 int fHeight; | 544 int fHeight; |
455 int fWidth; | 545 int fWidth; |
456 | 546 |
457 SkColor3f fAmbientColor; | 547 SkColor3f fAmbientColor; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
554 | 644 |
555 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 645 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
556 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | 646 // This is all done in linear unpremul color space (each component 0 ..255.0f though) |
557 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | 647 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
558 const SkLights::Light& light = lightShader.fLights->light(l); | 648 const SkLights::Light& light = lightShader.fLights->light(l); |
559 | 649 |
560 if (SkLights::Light::kAmbient_LightType == light.type()) { | 650 if (SkLights::Light::kAmbient_LightType == light.type()) { |
561 accum.fX += light.color().fX * SkColorGetR(diffColor); | 651 accum.fX += light.color().fX * SkColorGetR(diffColor); |
562 accum.fY += light.color().fY * SkColorGetG(diffColor); | 652 accum.fY += light.color().fY * SkColorGetG(diffColor); |
563 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | 653 accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
654 } else if (SkLights::Light::kDirectional_LightType == light.type ()) { | |
655 // scaling by fZ accounts for lighting direction | |
656 accum.fX += light.color().makeScale(light.dir().fZ).fX * | |
657 SkColorGetR(diffColor); | |
658 accum.fY += light.color().makeScale(light.dir().fZ).fY * | |
659 SkColorGetG(diffColor); | |
660 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * | |
661 SkColorGetB(diffColor); | |
564 } else { | 662 } else { |
565 // scaling by fZ accounts for lighting direction | 663 // TODO: do point lights for raster, currently treated like ambient |
566 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | 664 accum.fX += light.color().fX * SkColorGetR(diffColor); |
567 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | 665 accum.fY += light.color().fY * SkColorGetG(diffColor); |
568 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | 666 accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
569 } | 667 } |
570 } | 668 } |
571 | 669 |
572 result[i] = convert(accum, SkColorGetA(diffColor)); | 670 result[i] = convert(accum, SkColorGetA(diffColor)); |
573 } | 671 } |
574 | 672 |
575 result += n; | 673 result += n; |
576 x += n; | 674 x += n; |
577 count -= n; | 675 count -= n; |
578 } while (count > 0); | 676 } while (count > 0); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
671 | 769 |
672 /////////////////////////////////////////////////////////////////////////////// | 770 /////////////////////////////////////////////////////////////////////////////// |
673 | 771 |
674 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 772 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
675 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 773 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
676 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 774 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
677 | 775 |
678 /////////////////////////////////////////////////////////////////////////////// | 776 /////////////////////////////////////////////////////////////////////////////// |
679 | 777 |
680 #endif | 778 #endif |
OLD | NEW |