Chromium Code Reviews| 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(); | |
| 122 } else { | |
| 123 fLightDir[fNumDirLights] = lights->light(i).dir(); | |
| 124 } | |
| 125 fIsPointLight[fNumDirLights] = | |
| 126 lights->light(i).type() == SkLights::Light::kPoint_Light Type; | |
| 127 | |
| 121 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh adowMap()); | 128 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh adowMap()); |
| 122 | 129 |
| 123 // gets deleted when the ShadowFP is destroyed, and frees the Gr Texture* | 130 // gets deleted when the ShadowFP is destroyed, and frees the Gr Texture* |
| 124 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR ef(context, | 131 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR ef(context, |
| 125 GrTextureParams::Clam pNoFilter(), | 132 GrTextureParams::Clam pNoFilter(), |
| 126 SkSourceGammaTreatmen t::kIgnore)); | 133 SkSourceGammaTreatmen t::kIgnore)); |
| 127 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get ()); | 134 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get ()); |
| 128 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); | 135 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); |
| 129 | 136 |
| 130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); | 137 fDepthMapHeight[fNumDirLights] = shadowMap->height(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 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 bool isPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
| 166 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
| 167 isPointLight[i] = args.fFp.cast<ShadowFP>().fIsPointLight[i]; | |
| 168 } | |
| 169 | |
| 158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 170 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
| 171 const char* isPointLightUniName[SkShadowShader::kMaxNonAmbientLights ] = {nullptr}; | |
| 159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 172 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
| 160 | 173 |
| 161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] | 174 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] = {nullptr}; |
| 162 = {nullptr}; | 175 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] = {nullptr}; |
| 163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] | |
| 164 = {nullptr}; | |
| 165 | 176 |
| 166 SkString lightDirUniNameBase("lightDir"); | 177 SkString lightDirUniNameBase("lightDir"); |
| 167 SkString lightColorUniNameBase("lightColor"); | 178 SkString lightColorUniNameBase("lightColor"); |
| 179 SkString isPointLightUniNameBase("isPointLight"); | |
| 168 | 180 |
| 169 SkString depthMapWidthUniNameBase("dmapWidth"); | 181 SkString depthMapWidthUniNameBase("dmapWidth"); |
| 170 SkString depthMapHeightUniNameBase("dmapHeight"); | 182 SkString depthMapHeightUniNameBase("dmapHeight"); |
| 171 | 183 |
| 172 for (int i = 0; i < numLights; i++) { | 184 for (int i = 0; i < numLights; i++) { |
| 173 SkString lightDirUniNameStr(lightDirUniNameBase); | 185 SkString lightDirUniNameStr(lightDirUniNameBase); |
| 174 lightDirUniNameStr.appendf("%d", i); | 186 lightDirUniNameStr.appendf("%d", i); |
| 175 SkString lightColorUniNameStr(lightColorUniNameBase); | 187 SkString lightColorUniNameStr(lightColorUniNameBase); |
| 176 lightColorUniNameStr.appendf("%d", i); | 188 lightColorUniNameStr.appendf("%d", i); |
| 189 SkString isPointLightUniNameStr(isPointLightUniNameBase); | |
| 190 isPointLightUniNameStr.appendf("%d", i); | |
| 177 | 191 |
| 178 SkString depthMapWidthUniNameStr(depthMapWidthUniNameBase); | 192 SkString depthMapWidthUniNameStr(depthMapWidthUniNameBase); |
| 179 depthMapWidthUniNameStr.appendf("%d", i); | 193 depthMapWidthUniNameStr.appendf("%d", i); |
| 180 SkString depthMapHeightUniNameStr(depthMapHeightUniNameBase); | 194 SkString depthMapHeightUniNameStr(depthMapHeightUniNameBase); |
| 181 depthMapHeightUniNameStr.appendf("%d", i); | 195 depthMapHeightUniNameStr.appendf("%d", i); |
| 182 | 196 |
| 183 fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderF lag, | 197 fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderF lag, |
| 184 kVec3f_GrSLType, | 198 kVec3f_GrSLType, |
| 185 kDefault_GrSLPrecis ion, | 199 kDefault_GrSLPrecis ion, |
| 186 lightDirUniNameStr. c_str(), | 200 lightDirUniNameStr. c_str(), |
| 187 &lightDirUniName[i] ); | 201 &lightDirUniName[i] ); |
| 202 fIsPointLightUni[i] = uniformHandler->addUniform(kFragment_GrSha derFlag, | |
| 203 kFloat_GrSLType , | |
|
jvanverth1
2016/08/16 01:17:25
we have kBool_GrSLType
vjiaoblack
2016/08/16 20:33:40
Done.
| |
| 204 kDefault_GrSLPr ecision, | |
| 205 isPointLightUni NameStr.c_str(), | |
| 206 &isPointLightUn iName[i]); | |
| 188 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, | 207 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, |
| 189 kVec3f_GrSLType, | 208 kVec3f_GrSLType, |
| 190 kDefault_GrSLPrec ision, | 209 kDefault_GrSLPrec ision, |
| 191 lightColorUniName Str.c_str(), | 210 lightColorUniName Str.c_str(), |
| 192 &lightColorUniNam e[i]); | 211 &lightColorUniNam e[i]); |
| 193 | 212 |
| 194 fDepthMapWidthUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 213 fDepthMapWidthUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
| 195 kInt_GrSLType, | 214 kInt_GrSLType, |
| 196 kDefault_GrSLPrecision, | 215 kDefault_GrSLPrecision, |
| 197 depthMapWidthUniNameStr.c_str (), | 216 depthMapWidthUniNameStr.c_str (), |
| 198 &depthMapWidthUniName[i]); | 217 &depthMapWidthUniName[i]); |
| 199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 218 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
| 200 kInt_GrSLType, | 219 kInt_GrSLType, |
| 201 kDefault_GrSLPrecision, | 220 kDefault_GrSLPrecision, |
| 202 depthMapHeightUniNameStr.c_st r(), | 221 depthMapHeightUniNameStr.c_st r(), |
| 203 &depthMapHeightUniName[i]); | 222 &depthMapHeightUniName[i]); |
| 204 } | 223 } |
| 205 | 224 |
| 206 | |
| 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, |
| 216 kDefault_GrSLPrecision, | 234 kDefault_GrSLPrecision, |
| 217 "height", &heightUniName); | 235 "height", &heightUniName); |
| 218 | 236 |
| 219 | |
| 220 SkString povDepth("povDepth"); | 237 SkString povDepth("povDepth"); |
| 221 this->emitChild(0, nullptr, &povDepth, args); | 238 this->emitChild(0, nullptr, &povDepth, args); |
| 222 | 239 |
| 223 SkString diffuseColor("inDiffuseColor"); | 240 SkString diffuseColor("inDiffuseColor"); |
| 224 this->emitChild(1, nullptr, &diffuseColor, args); | 241 this->emitChild(1, nullptr, &diffuseColor, args); |
| 225 | 242 |
| 226 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; | 243 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; |
| 227 | 244 |
| 245 fragBuilder->codeAppendf("vec3 worldCor = vec3(vMatrixCoord_0_1_Stag e0 * " | |
| 246 "vec2(%s, %s), %s.b * 256); \n", | |
|
jvanverth1
2016/08/16 01:17:25
Why 256? And create a constant for this magic numb
vjiaoblack
2016/08/16 20:33:40
Done.
| |
| 247 widthUniName, heightUniName, povDepth.c_str ()); | |
| 248 | |
| 228 for (int i = 0; i < numLights; i++) { | 249 for (int i = 0; i < numLights; i++) { |
| 229 SkString povCoord("povCoord"); | 250 SkString povCoord("povCoord"); |
| 230 povCoord.appendf("%d", i); | 251 povCoord.appendf("%d", i); |
| 231 | 252 |
| 232 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. | 253 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. |
| 233 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s pace, | 254 // 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 | 255 // and the / 400 brings it back to a sampler coordinate, 0 - 1 |
| 235 // The 400 comes from the shadowmaps GM. | 256 // The 400 comes from the shadowmaps GM. |
| 236 // TODO use real shadowmaps size | 257 // TODO use real shadowmaps size |
| 237 SkString offset("offset"); | 258 SkString offset("offset"); |
| 238 offset.appendf("%d", i); | 259 offset.appendf("%d", i); |
| 239 | 260 |
| 240 SkString scaleVec("scaleVec"); | 261 SkString scaleVec("scaleVec"); |
| 241 scaleVec.appendf("%d", i); | 262 scaleVec.appendf("%d", i); |
| 242 | 263 |
| 243 SkString scaleOffsetVec("scaleOffsetVec"); | 264 SkString scaleOffsetVec("scaleOffsetVec"); |
| 244 scaleOffsetVec.appendf("%d", i); | 265 scaleOffsetVec.appendf("%d", i); |
| 245 | 266 |
| 246 fragBuilder->codeAppendf("vec2 %s = vec2(%s) * povDepth.b * 255 / 400;\n", | 267 fragBuilder->codeAppendf("vec2 %s;", offset.c_str()); |
| 247 offset.c_str(), lightDirUniName[i]); | |
| 248 | 268 |
| 269 if (isPointLight[i]) { | |
| 270 fragBuilder->codeAppendf("vec3 fragToLight%d = %s - worldCor ;\n", | |
| 271 i, lightDirUniName[i]); | |
| 272 fragBuilder->codeAppendf("float dist%d = length(fragToLight% d);" | |
|
jvanverth1
2016/08/16 01:17:25
Do you use dist? If not, why not use normalize()?
vjiaoblack
2016/08/16 20:33:40
Done.
| |
| 273 "fragToLight%d = fragToLight%d / di st%d;", | |
| 274 i, i, i, i, i); | |
| 275 fragBuilder->codeAppendf("%s = -vec2(%s.x - worldCor.x, worl dCor.y - %s.y)*" | |
| 276 "(povDepth.b) / 400;\n", | |
| 277 offset.c_str(), lightDirUniName[i], | |
| 278 lightDirUniName[i]); | |
| 279 } else { | |
| 280 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * 256 / 400;\n", | |
| 281 offset.c_str(), lightDirUniName[i]) ; | |
| 282 } | |
| 249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", | 283 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", |
| 250 scaleVec.c_str(), | 284 scaleVec.c_str(), |
| 251 widthUniName, heightUniName, | 285 widthUniName, heightUniName, |
| 252 depthMapWidthUniName[i], depthMapHeight UniName[i]); | 286 depthMapWidthUniName[i], depthMapHeight UniName[i]); |
| 253 | 287 |
| 254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", | 288 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", |
| 255 scaleOffsetVec.c_str(), scaleVec.c_str( )); | 289 scaleOffsetVec.c_str(), scaleVec.c_str( )); |
| 256 | 290 |
| 257 | |
| 258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " | 291 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " |
| 259 "vec2(%s.x, 0 - %s.y)) " | 292 "vec2(%s.x, 0 - %s.y)) " |
| 260 " * %s + vec2(0,1) * %s;\n", | 293 " * %s + vec2(0,1) * %s;\n", |
| 261 | 294 povCoord.c_str(), offset.c_str(), o ffset.c_str(), |
| 262 povCoord.c_str(), offset.c_str(), offse t.c_str(), | 295 scaleVec.c_str(), scaleOffsetVec.c_ str()); |
| 263 scaleVec.c_str(), scaleOffsetVec.c_str( )); | |
| 264 | 296 |
| 265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | 297 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], |
| 266 povCoord.c_str(), | 298 povCoord.c_str(), |
| 267 kVec2f_GrSLType); | 299 kVec2f_GrSLType); |
| 268 } | 300 } |
| 269 | 301 |
| 270 const char* ambientColorUniName = nullptr; | 302 const char* ambientColorUniName = nullptr; |
| 271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | 303 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , |
| 272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | 304 kVec3f_GrSLType, kDefa ult_GrSLPrecision, |
| 273 "AmbientColor", &ambie ntColorUniName); | 305 "AmbientColor", &ambie ntColorUniName); |
| 274 | 306 |
| 275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); | 307 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); |
| 276 | 308 |
| 277 // Essentially, | |
| 278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) | 309 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) |
| 279 SkString totalLightColor("totalLightColor"); | 310 SkString totalLightColor("totalLightColor"); |
| 280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); | 311 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); |
| 281 | 312 |
| 282 for (int i = 0; i < numLights; i++) { | 313 for (int i = 0; i < numLights; i++) { |
| 283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | 314 if (!isPointLight[i]) { |
| 284 povDepth.c_str(), depthMaps[i].c_str()) ; | 315 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", |
| 285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s | 316 povDepth.c_str(), depthMaps[i].c_st r()); |
| 286 fragBuilder->codeAppendf("%s += %s.z * %s;", | 317 // Note that dot(vec3(0,0,1), %s) == %s.z * %s |
| 287 totalLightColor.c_str(), | 318 fragBuilder->codeAppendf("%s += %s.z * %s;", |
| 288 lightDirUniName[i], | 319 totalLightColor.c_str(), |
| 289 lightColorUniName[i]); | 320 lightDirUniName[i], |
| 290 fragBuilder->codeAppendf("}"); | 321 lightColorUniName[i]); |
| 322 fragBuilder->codeAppendf("}"); | |
| 323 } else { | |
| 324 // fragToLight%d.z is equal to the fragToLight dot the surfa ce normal. | |
| 325 fragBuilder->codeAppendf("%s += fragToLight%d.z * %s / pow(( (dist%d / 1024.0) + 1.0), 2);", | |
|
jvanverth1
2016/08/16 01:17:25
Ah, I see you use dist here. Using pow to do a squ
vjiaoblack
2016/08/16 20:33:40
Done.
| |
| 326 totalLightColor.c_str(), i, | |
| 327 lightColorUniName[i], i); | |
| 328 } | |
| 291 } | 329 } |
| 292 | 330 |
| 293 fragBuilder->codeAppendf("%s += %s;", | 331 fragBuilder->codeAppendf("%s += %s;", |
| 294 totalLightColor.c_str(), | 332 totalLightColor.c_str(), |
| 295 ambientColorUniName); | 333 ambientColorUniName); |
| 296 | 334 |
| 297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", | 335 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", |
| 298 totalLightColor.c_str()); | 336 totalLightColor.c_str()); |
| 299 | 337 |
| 300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); | 338 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); |
| 301 } | 339 } |
| 302 | 340 |
| 303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 341 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 304 GrProcessorKeyBuilder* b) { | 342 GrProcessorKeyBuilder* b) { |
| 305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 343 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
| 306 b->add32(shadowFP.fNumDirLights); | 344 b->add32(shadowFP.fNumDirLights); |
| 345 int isPL = 0; | |
| 346 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
| 347 isPL = isPL | ((shadowFP.fIsPointLight[i] ? 1 : 0) << i); | |
| 348 } | |
| 349 b->add32(isPL); | |
| 307 } | 350 } |
| 308 | 351 |
| 309 protected: | 352 protected: |
| 310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { | 353 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
| 311 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); | 354 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); |
| 312 | 355 |
| 313 fNumDirLights = shadowFP.numLights(); | 356 fNumDirLights = shadowFP.numLights(); |
| 314 | 357 |
| 315 for (int i = 0; i < fNumDirLights; i++) { | 358 for (int i = 0; i < fNumDirLights; i++) { |
| 316 const SkVector3& lightDir = shadowFP.lightDir(i); | 359 const SkVector3& lightDir = shadowFP.lightDir(i); |
| 360 | |
| 361 bool isPoint = shadowFP.isPointLight(i); | |
| 362 SkScalar isPointf = isPoint ? 1.0f : 0.0f; | |
| 363 pdman.set1f(fIsPointLightUni[i], isPointf); | |
| 364 fIsPointLight[i] = isPoint; | |
| 365 | |
| 317 if (lightDir != fLightDir[i]) { | 366 if (lightDir != fLightDir[i]) { |
| 318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | 367 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); |
| 319 fLightDir[i] = lightDir; | 368 fLightDir[i] = lightDir; |
| 320 } | 369 } |
| 321 const SkColor3f& lightColor = shadowFP.lightColor(i); | 370 const SkColor3f& lightColor = shadowFP.lightColor(i); |
| 322 if (lightColor != fLightColor[i]) { | 371 if (lightColor != fLightColor[i]) { |
| 323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | 372 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); |
| 324 fLightColor[i] = lightColor; | 373 fLightColor[i] = lightColor; |
| 325 } | 374 } |
| 326 | 375 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 348 } | 397 } |
| 349 | 398 |
| 350 const SkColor3f& ambientColor = shadowFP.ambientColor(); | 399 const SkColor3f& ambientColor = shadowFP.ambientColor(); |
| 351 if (ambientColor != fAmbientColor) { | 400 if (ambientColor != fAmbientColor) { |
| 352 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 401 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
| 353 fAmbientColor = ambientColor; | 402 fAmbientColor = ambientColor; |
| 354 } | 403 } |
| 355 } | 404 } |
| 356 | 405 |
| 357 private: | 406 private: |
| 407 bool fIsPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
| 408 GrGLSLProgramDataManager::UniformHandle | |
| 409 fIsPointLightUni[SkShadowShader::kMaxNonAmbientLights]; | |
| 410 | |
| 358 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; | 411 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
| 359 GrGLSLProgramDataManager::UniformHandle | 412 GrGLSLProgramDataManager::UniformHandle |
| 360 fLightDirUni[SkShadowShader::kMaxNonAmbientLights]; | 413 fLightDirUni[SkShadowShader::kMaxNonAmbientLights]; |
| 361 | 414 |
| 362 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 415 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 363 GrGLSLProgramDataManager::UniformHandle | 416 GrGLSLProgramDataManager::UniformHandle |
| 364 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; | 417 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; |
| 365 | 418 |
| 366 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 419 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
| 367 GrGLSLProgramDataManager::UniformHandle | 420 GrGLSLProgramDataManager::UniformHandle |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 386 GLSLShadowFP::GenKey(*this, caps, b); | 439 GLSLShadowFP::GenKey(*this, caps, b); |
| 387 } | 440 } |
| 388 | 441 |
| 389 const char* name() const override { return "shadowFP"; } | 442 const char* name() const override { return "shadowFP"; } |
| 390 | 443 |
| 391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 444 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 392 inout->mulByUnknownFourComponents(); | 445 inout->mulByUnknownFourComponents(); |
| 393 } | 446 } |
| 394 int32_t numLights() const { return fNumDirLights; } | 447 int32_t numLights() const { return fNumDirLights; } |
| 395 const SkColor3f& ambientColor() const { return fAmbientColor; } | 448 const SkColor3f& ambientColor() const { return fAmbientColor; } |
| 449 const bool& isPointLight(int i) const { | |
| 450 SkASSERT(i < fNumDirLights); | |
| 451 return fIsPointLight[i]; | |
| 452 } | |
| 396 const SkVector3& lightDir(int i) const { | 453 const SkVector3& lightDir(int i) const { |
| 397 SkASSERT(i < fNumDirLights); | 454 SkASSERT(i < fNumDirLights); |
| 398 return fLightDir[i]; | 455 return fLightDir[i]; |
| 399 } | 456 } |
| 400 const SkVector3& lightColor(int i) const { | 457 const SkVector3& lightColor(int i) const { |
| 401 SkASSERT(i < fNumDirLights); | 458 SkASSERT(i < fNumDirLights); |
| 402 return fLightColor[i]; | 459 return fLightColor[i]; |
| 403 } | 460 } |
| 404 | 461 |
| 405 int depthMapWidth(int i) const { | 462 int depthMapWidth(int i) const { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { | 478 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { |
| 422 return false; | 479 return false; |
| 423 } | 480 } |
| 424 | 481 |
| 425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { | 482 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { |
| 426 return false; | 483 return false; |
| 427 } | 484 } |
| 428 | 485 |
| 429 for (int i = 0; i < fNumDirLights; i++) { | 486 for (int i = 0; i < fNumDirLights; i++) { |
| 430 if (fLightDir[i] != shadowFP.fLightDir[i] || | 487 if (fLightDir[i] != shadowFP.fLightDir[i] || |
| 431 fLightColor[i] != shadowFP.fLightColor[i]) { | 488 fLightColor[i] != shadowFP.fLightColor[i] || |
| 489 fIsPointLight[i] != shadowFP.fIsPointLight[i]) { | |
| 432 return false; | 490 return false; |
| 433 } | 491 } |
| 434 | 492 |
| 435 if (fDepthMapWidth[i] != shadowFP.fDepthMapWidth[i] || | 493 if (fDepthMapWidth[i] != shadowFP.fDepthMapWidth[i] || |
| 436 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { | 494 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { |
| 437 return false; | 495 return false; |
| 438 } | 496 } |
| 439 } | 497 } |
| 440 | 498 |
| 441 return true; | 499 return true; |
| 442 } | 500 } |
| 443 | 501 |
| 444 int fNumDirLights; | 502 int fNumDirLights; |
| 445 | 503 |
| 504 bool fIsPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
| 446 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; | 505 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
| 447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 506 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; | 507 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; |
| 449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; | 508 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; |
| 450 | 509 |
| 451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 510 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
| 452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 511 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
| 453 | 512 |
| 454 int fHeight; | 513 int fHeight; |
| 455 int fWidth; | 514 int fWidth; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 554 | 613 |
| 555 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 614 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) | 615 // 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) { | 616 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| 558 const SkLights::Light& light = lightShader.fLights->light(l); | 617 const SkLights::Light& light = lightShader.fLights->light(l); |
| 559 | 618 |
| 560 if (SkLights::Light::kAmbient_LightType == light.type()) { | 619 if (SkLights::Light::kAmbient_LightType == light.type()) { |
| 561 accum.fX += light.color().fX * SkColorGetR(diffColor); | 620 accum.fX += light.color().fX * SkColorGetR(diffColor); |
| 562 accum.fY += light.color().fY * SkColorGetG(diffColor); | 621 accum.fY += light.color().fY * SkColorGetG(diffColor); |
| 563 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | 622 accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
| 623 } else if (SkLights::Light::kDirectional_LightType == light.type ()) { | |
| 624 // scaling by fZ accounts for lighting direction | |
| 625 accum.fX += light.color().makeScale(light.dir().fZ).fX * | |
| 626 SkColorGetR(diffColor); | |
| 627 accum.fY += light.color().makeScale(light.dir().fZ).fY * | |
| 628 SkColorGetG(diffColor); | |
| 629 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * | |
| 630 SkColorGetB(diffColor); | |
| 564 } else { | 631 } else { |
| 565 // scaling by fZ accounts for lighting direction | 632 // TODO: do point lights for raster, currently treated like ambient |
| 566 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | 633 accum.fX += light.color().fX * SkColorGetR(diffColor); |
| 567 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | 634 accum.fY += light.color().fY * SkColorGetG(diffColor); |
| 568 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | 635 accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
| 569 } | 636 } |
| 570 } | 637 } |
| 571 | 638 |
| 572 result[i] = convert(accum, SkColorGetA(diffColor)); | 639 result[i] = convert(accum, SkColorGetA(diffColor)); |
| 573 } | 640 } |
| 574 | 641 |
| 575 result += n; | 642 result += n; |
| 576 x += n; | 643 x += n; |
| 577 count -= n; | 644 count -= n; |
| 578 } while (count > 0); | 645 } while (count > 0); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 | 738 |
| 672 /////////////////////////////////////////////////////////////////////////////// | 739 /////////////////////////////////////////////////////////////////////////////// |
| 673 | 740 |
| 674 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 741 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 675 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 742 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 676 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 743 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 677 | 744 |
| 678 /////////////////////////////////////////////////////////////////////////////// | 745 /////////////////////////////////////////////////////////////////////////////// |
| 679 | 746 |
| 680 #endif | 747 #endif |
| OLD | NEW |