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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 public: | 104 public: |
| 105 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, | 105 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, |
| 106 sk_sp<GrFragmentProcessor> diffuse, | 106 sk_sp<GrFragmentProcessor> diffuse, |
| 107 sk_sp<SkLights> lights, | 107 sk_sp<SkLights> lights, |
| 108 int diffuseWidth, int diffuseHeight, | 108 int diffuseWidth, int diffuseHeight, |
| 109 GrContext* context) { | 109 GrContext* context) { |
| 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 fNumNonAmbLights = 0; // count of non-ambient 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 (fNumNonAmbLights < SkShadowShader::kMaxNonAmbientLights) { |
| 119 fLightColor[fNumDirLights] = lights->light(i).color(); | 119 fLightColor[fNumNonAmbLights] = lights->light(i).color(); |
| 120 fLightDir[fNumDirLights] = lights->light(i).dir(); | 120 if (lights->light(i).type() == SkLights::Light::kPoint_LightType ) { |
| 121 fLightDirOrPos[fNumNonAmbLights] = lights->light(i).pos(); | |
| 122 fLightIntensity[fNumNonAmbLights] = lights->light(i).intensi ty(); | |
| 123 } else { | |
| 124 fLightDirOrPos[fNumNonAmbLights] = lights->light(i).dir(); | |
| 125 fLightIntensity[fNumNonAmbLights] = 0.0f; | |
| 126 } | |
| 127 fIsPointLight[fNumNonAmbLights] = | |
| 128 SkLights::Light::kPoint_LightType == lights->light(i).t ype(); | |
| 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[fNumNonAmbLights] = sk_sp<GrTexture>(shadowMap->asTextu reRef(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[fNumNonAmbLights].reset(fTexture[fNumNonAmbLight s].get()); |
| 128 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); | 137 this->addTextureAccess(&fDepthMapAccess[fNumNonAmbLights]); |
| 129 | 138 |
| 130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); | 139 fDepthMapHeight[fNumNonAmbLights] = shadowMap->height(); |
| 131 fDepthMapWidth[fNumDirLights] = shadowMap->width(); | 140 fDepthMapWidth[fNumNonAmbLights] = shadowMap->width(); |
| 132 | 141 |
| 133 fNumDirLights++; | 142 fNumNonAmbLights++; |
| 134 } | 143 } |
| 135 } | 144 } |
| 136 | 145 |
| 137 fWidth = diffuseWidth; | 146 fWidth = diffuseWidth; |
| 138 fHeight = diffuseHeight; | 147 fHeight = diffuseHeight; |
| 139 | 148 |
| 140 this->registerChildProcessor(std::move(povDepth)); | 149 this->registerChildProcessor(std::move(povDepth)); |
| 141 this->registerChildProcessor(std::move(diffuse)); | 150 this->registerChildProcessor(std::move(diffuse)); |
| 142 this->initClassID<ShadowFP>(); | 151 this->initClassID<ShadowFP>(); |
| 143 } | 152 } |
| 144 | 153 |
| 145 class GLSLShadowFP : public GrGLSLFragmentProcessor { | 154 class GLSLShadowFP : public GrGLSLFragmentProcessor { |
| 146 public: | 155 public: |
| 147 GLSLShadowFP() { } | 156 GLSLShadowFP() { } |
| 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; |
| 162 const ShadowFP& shadowFP = args.fFp.cast<ShadowFP>(); | |
| 163 | |
| 164 SkASSERT(shadowFP.fNumNonAmbLights <= SkShadowShader::kMaxNonAmbient Lights); | |
| 153 | 165 |
| 154 // add uniforms | 166 // add uniforms |
| 155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | 167 const char* lightDirOrPosUniName[SkShadowShader::kMaxNonAmbientLight s] = {nullptr}; |
| 156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); | 168 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; |
| 169 const char* lightIntensityUniName[SkShadowShader::kMaxNonAmbientLigh ts] = {nullptr}; | |
| 157 | 170 |
| 158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 171 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] = {nullptr}; |
| 159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights] = {nullptr}; | 172 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] = {nullptr}; |
| 160 | 173 |
| 161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight s] | 174 for (int i = 0; i < shadowFP.fNumNonAmbLights; i++) { |
| 162 = {nullptr}; | 175 SkString lightDirOrPosUniNameStr("lightDir"); |
| 163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh ts] | 176 lightDirOrPosUniNameStr.appendf("%d", i); |
| 164 = {nullptr}; | 177 SkString lightColorUniNameStr("lightColor"); |
| 178 lightColorUniNameStr.appendf("%d", i); | |
| 179 SkString lightIntensityUniNameStr("lightIntensity"); | |
| 180 lightIntensityUniNameStr.appendf("%d", i); | |
| 165 | 181 |
| 166 SkString lightDirUniNameBase("lightDir"); | 182 SkString depthMapWidthUniNameStr("dmapWidth"); |
| 167 SkString lightColorUniNameBase("lightColor"); | |
| 168 | |
| 169 SkString depthMapWidthUniNameBase("dmapWidth"); | |
| 170 SkString depthMapHeightUniNameBase("dmapHeight"); | |
| 171 | |
| 172 for (int i = 0; i < numLights; i++) { | |
| 173 SkString lightDirUniNameStr(lightDirUniNameBase); | |
| 174 lightDirUniNameStr.appendf("%d", i); | |
| 175 SkString lightColorUniNameStr(lightColorUniNameBase); | |
| 176 lightColorUniNameStr.appendf("%d", i); | |
| 177 | |
| 178 SkString depthMapWidthUniNameStr(depthMapWidthUniNameBase); | |
| 179 depthMapWidthUniNameStr.appendf("%d", i); | 183 depthMapWidthUniNameStr.appendf("%d", i); |
| 180 SkString depthMapHeightUniNameStr(depthMapHeightUniNameBase); | 184 SkString depthMapHeightUniNameStr("dmapHeight"); |
| 181 depthMapHeightUniNameStr.appendf("%d", i); | 185 depthMapHeightUniNameStr.appendf("%d", i); |
| 182 | 186 |
| 183 fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderF lag, | 187 fLightDirOrPosUni[i] = uniformHandler->addUniform(kFragment_GrSh aderFlag, |
| 184 kVec3f_GrSLType, | 188 kVec3f_GrSLType, |
| 185 kDefault_GrSLPrecis ion, | 189 kDefault_GrSLPrecis ion, |
| 186 lightDirUniNameStr. c_str(), | 190 lightDirOrPosUniNam eStr.c_str(), |
| 187 &lightDirUniName[i] ); | 191 &lightDirOrPosUniNa me[i]); |
| 188 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, | 192 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, |
| 189 kVec3f_GrSLType, | 193 kVec3f_GrSLType, |
| 190 kDefault_GrSLPrec ision, | 194 kDefault_GrSLPrec ision, |
| 191 lightColorUniName Str.c_str(), | 195 lightColorUniName Str.c_str(), |
| 192 &lightColorUniNam e[i]); | 196 &lightColorUniNam e[i]); |
| 197 fLightIntensityUni[i] = | |
| 198 uniformHandler->addUniform(kFragment_GrShaderFlag, | |
| 199 kFloat_GrSLType, | |
| 200 kDefault_GrSLPrecision, | |
| 201 lightIntensityUniNameStr.c_st r(), | |
| 202 &lightIntensityUniName[i]); | |
| 193 | 203 |
| 194 fDepthMapWidthUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 204 fDepthMapWidthUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
| 195 kInt_GrSLType, | 205 kInt_GrSLType, |
| 196 kDefault_GrSLPrecision, | 206 kDefault_GrSLPrecision, |
| 197 depthMapWidthUniNameStr.c_str (), | 207 depthMapWidthUniNameStr.c_str (), |
| 198 &depthMapWidthUniName[i]); | 208 &depthMapWidthUniName[i]); |
| 199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, | 209 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS haderFlag, |
| 200 kInt_GrSLType, | 210 kInt_GrSLType, |
| 201 kDefault_GrSLPrecision, | 211 kDefault_GrSLPrecision, |
| 202 depthMapHeightUniNameStr.c_st r(), | 212 depthMapHeightUniNameStr.c_st r(), |
| 203 &depthMapHeightUniName[i]); | 213 &depthMapHeightUniName[i]); |
| 204 } | 214 } |
| 205 | 215 |
| 206 | |
| 207 const char* widthUniName = nullptr; | 216 const char* widthUniName = nullptr; |
| 208 const char* heightUniName = nullptr; | 217 const char* heightUniName = nullptr; |
| 209 | 218 |
| 210 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 219 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 211 kInt_GrSLType, | 220 kInt_GrSLType, |
| 212 kDefault_GrSLPrecision, | 221 kDefault_GrSLPrecision, |
| 213 "width", &widthUniName); | 222 "width", &widthUniName); |
| 214 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 223 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 215 kInt_GrSLType, | 224 kInt_GrSLType, |
| 216 kDefault_GrSLPrecision, | 225 kDefault_GrSLPrecision, |
| 217 "height", &heightUniName); | 226 "height", &heightUniName); |
| 218 | 227 |
| 219 | |
| 220 SkString povDepth("povDepth"); | 228 SkString povDepth("povDepth"); |
| 221 this->emitChild(0, nullptr, &povDepth, args); | 229 this->emitChild(0, nullptr, &povDepth, args); |
| 222 | 230 |
| 223 SkString diffuseColor("inDiffuseColor"); | 231 SkString diffuseColor("inDiffuseColor"); |
| 224 this->emitChild(1, nullptr, &diffuseColor, args); | 232 this->emitChild(1, nullptr, &diffuseColor, args); |
| 225 | 233 |
| 226 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; | 234 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; |
| 227 | 235 |
| 228 for (int i = 0; i < numLights; i++) { | 236 // Multiply by 255 to transform from sampler coordinates to world |
| 237 // coordinates (since 1 channel is 0xFF) | |
| 238 fragBuilder->codeAppendf("vec3 worldCor = vec3(vMatrixCoord_0_1_Stag e0 * " | |
|
robertphillips
2016/08/23 19:24:51
Don't need '\n' in shader code. The pretty printer
vjiaoblack
2016/08/23 20:11:27
I would like to make this change after I merge the
| |
| 239 "vec2(%s, %s), %s.b * 255); \n", | |
| 240 widthUniName, heightUniName, povDepth.c_str ()); | |
| 241 | |
| 242 for (int i = 0; i < shadowFP.fNumNonAmbLights; i++) { | |
| 229 SkString povCoord("povCoord"); | 243 SkString povCoord("povCoord"); |
| 230 povCoord.appendf("%d", i); | 244 povCoord.appendf("%d", i); |
| 231 | 245 |
| 232 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. | 246 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. |
| 233 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s pace, | 247 // 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 | 248 // 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"); | 249 SkString offset("offset"); |
| 238 offset.appendf("%d", i); | 250 offset.appendf("%d", i); |
| 239 | 251 |
| 240 SkString scaleVec("scaleVec"); | 252 SkString scaleVec("scaleVec"); |
| 241 scaleVec.appendf("%d", i); | 253 scaleVec.appendf("%d", i); |
| 242 | 254 |
| 243 SkString scaleOffsetVec("scaleOffsetVec"); | 255 SkString scaleOffsetVec("scaleOffsetVec"); |
| 244 scaleOffsetVec.appendf("%d", i); | 256 scaleOffsetVec.appendf("%d", i); |
| 245 | 257 |
| 246 fragBuilder->codeAppendf("vec2 %s = vec2(%s) * povDepth.b * 255 / 400;\n", | 258 fragBuilder->codeAppendf("vec2 %s;", offset.c_str()); |
| 247 offset.c_str(), lightDirUniName[i]); | |
| 248 | 259 |
| 260 if (shadowFP.fIsPointLight[i]) { | |
| 261 fragBuilder->codeAppendf("vec3 fragToLight%d = %s - worldCor ;\n", | |
| 262 i, lightDirOrPosUniName[i]); | |
| 263 fragBuilder->codeAppendf("float distsq%d = dot(fragToLight%d , fragToLight%d);" | |
| 264 "fragToLight%d = normalize(fragToLi ght%d);", | |
| 265 i, i, i, i, i); | |
| 266 fragBuilder->codeAppendf("%s = -vec2(%s.x - worldCor.x, worl dCor.y - %s.y)*" | |
| 267 "(povDepth.b) / vec2(%s, %s) ;\n", | |
| 268 offset.c_str(), lightDirOrPosUniNam e[i], | |
| 269 lightDirOrPosUniName[i], widthUniNa me, heightUniName); | |
| 270 } else { | |
| 271 fragBuilder->codeAppendf("%s = vec2(%s) * povDepth.b * 255 / vec2(%s, %s);\n", | |
| 272 offset.c_str(), lightDirOrPosUniNam e[i], | |
| 273 widthUniName, heightUniName); | |
| 274 } | |
| 249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", | 275 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", |
| 250 scaleVec.c_str(), | 276 scaleVec.c_str(), |
| 251 widthUniName, heightUniName, | 277 widthUniName, heightUniName, |
| 252 depthMapWidthUniName[i], depthMapHeight UniName[i]); | 278 depthMapWidthUniName[i], depthMapHeight UniName[i]); |
| 253 | 279 |
| 254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", | 280 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", |
| 255 scaleOffsetVec.c_str(), scaleVec.c_str( )); | 281 scaleOffsetVec.c_str(), scaleVec.c_str( )); |
| 256 | 282 |
| 257 | |
| 258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " | 283 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " |
| 259 "vec2(%s.x, 0 - %s.y)) " | 284 "vec2(%s.x, 0 - %s.y)) " |
| 260 " * %s + vec2(0,1) * %s;\n", | 285 " * %s + vec2(0,1) * %s;\n", |
| 261 | |
| 262 povCoord.c_str(), offset.c_str(), offse t.c_str(), | 286 povCoord.c_str(), offset.c_str(), offse t.c_str(), |
| 263 scaleVec.c_str(), scaleOffsetVec.c_str( )); | 287 scaleVec.c_str(), scaleOffsetVec.c_str( )); |
| 264 | 288 |
| 265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | 289 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], |
| 266 povCoord.c_str(), | 290 povCoord.c_str(), |
| 267 kVec2f_GrSLType); | 291 kVec2f_GrSLType); |
| 268 } | 292 } |
| 269 | 293 |
| 270 const char* ambientColorUniName = nullptr; | 294 const char* ambientColorUniName = nullptr; |
| 271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | 295 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , |
| 272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | 296 kVec3f_GrSLType, kDefa ult_GrSLPrecision, |
| 273 "AmbientColor", &ambie ntColorUniName); | 297 "AmbientColor", &ambie ntColorUniName); |
| 274 | 298 |
| 275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); | 299 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); |
| 276 | 300 |
| 277 // Essentially, | |
| 278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) | 301 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) |
| 279 SkString totalLightColor("totalLightColor"); | 302 SkString totalLightColor("totalLightColor"); |
| 280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); | 303 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); |
| 281 | 304 |
| 282 for (int i = 0; i < numLights; i++) { | 305 for (int i = 0; i < shadowFP.fNumNonAmbLights; i++) { |
| 283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | 306 if (!shadowFP.fIsPointLight[i]) { |
| 284 povDepth.c_str(), depthMaps[i].c_str()) ; | 307 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", |
| 285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s | 308 povDepth.c_str(), depthMaps[i].c_st r()); |
| 286 fragBuilder->codeAppendf("%s += %s.z * %s;", | 309 // Note that dot(vec3(0,0,1), %s) == %s.z * %s |
| 287 totalLightColor.c_str(), | 310 fragBuilder->codeAppendf("%s += %s.z * %s;", |
| 288 lightDirUniName[i], | 311 totalLightColor.c_str(), |
| 289 lightColorUniName[i]); | 312 lightDirOrPosUniName[i], |
| 290 fragBuilder->codeAppendf("}"); | 313 lightColorUniName[i]); |
| 314 fragBuilder->codeAppendf("}"); | |
| 315 } else { | |
| 316 // fragToLight%d.z is equal to the fragToLight dot the surfa ce normal. | |
| 317 fragBuilder->codeAppendf("%s += max(fragToLight%d.z, 0) * %s / " | |
| 318 "(1 + distsq%d / (%s * %s));" , | |
| 319 totalLightColor.c_str(), i, | |
| 320 lightColorUniName[i], i, | |
| 321 lightIntensityUniName[i], | |
| 322 lightIntensityUniName[i]); | |
| 323 } | |
| 291 } | 324 } |
| 292 | 325 |
| 293 fragBuilder->codeAppendf("%s += %s;", | 326 fragBuilder->codeAppendf("%s += %s;", |
| 294 totalLightColor.c_str(), | 327 totalLightColor.c_str(), |
| 295 ambientColorUniName); | 328 ambientColorUniName); |
| 296 | 329 |
| 297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", | 330 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", |
| 298 totalLightColor.c_str()); | 331 totalLightColor.c_str()); |
| 299 | 332 |
| 300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); | 333 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); |
| 301 } | 334 } |
| 302 | 335 |
| 303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 336 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 304 GrProcessorKeyBuilder* b) { | 337 GrProcessorKeyBuilder* b) { |
| 305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 338 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
| 306 b->add32(shadowFP.fNumDirLights); | 339 b->add32(shadowFP.fNumNonAmbLights); |
| 340 int isPL = 0; | |
| 341 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
| 342 isPL = isPL | ((shadowFP.fIsPointLight[i] ? 1 : 0) << i); | |
| 343 } | |
| 344 b->add32(isPL); | |
| 307 } | 345 } |
| 308 | 346 |
| 309 protected: | 347 protected: |
| 310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { | 348 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
| 311 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); | 349 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); |
| 312 | 350 |
| 313 fNumDirLights = shadowFP.numLights(); | 351 fNumNonAmbLights = shadowFP.numLights(); |
| 314 | 352 |
| 315 for (int i = 0; i < fNumDirLights; i++) { | 353 for (int i = 0; i < fNumNonAmbLights; i++) { |
| 316 const SkVector3& lightDir = shadowFP.lightDir(i); | 354 bool isPoint = shadowFP.isPointLight(i); |
| 317 if (lightDir != fLightDir[i]) { | 355 fIsPointLight[i] = isPoint; |
| 318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | 356 |
| 319 fLightDir[i] = lightDir; | 357 const SkVector3& lightDirOrPos = shadowFP.lightDirOrPos(i); |
| 358 if (lightDirOrPos != fLightDir[i]) { | |
| 359 pdman.set3fv(fLightDirOrPosUni[i], 1, &lightDirOrPos.fX); | |
|
robertphillips
2016/08/23 19:24:51
Shouldn't fLightDir be fLightDirOrPos ?
| |
| 360 fLightDir[i] = lightDirOrPos; | |
| 320 } | 361 } |
| 362 | |
| 321 const SkColor3f& lightColor = shadowFP.lightColor(i); | 363 const SkColor3f& lightColor = shadowFP.lightColor(i); |
| 322 if (lightColor != fLightColor[i]) { | 364 if (lightColor != fLightColor[i]) { |
| 323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | 365 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); |
| 324 fLightColor[i] = lightColor; | 366 fLightColor[i] = lightColor; |
| 325 } | 367 } |
| 326 | 368 |
| 369 SkScalar lightIntensity = shadowFP.lightIntensity(i); | |
| 370 if (lightIntensity != fLightIntensity[i]) { | |
| 371 pdman.set1f(fLightIntensityUni[i], lightIntensity); | |
| 372 fLightIntensity[i] = lightIntensity; | |
| 373 } | |
| 374 | |
| 327 int depthMapWidth = shadowFP.depthMapWidth(i); | 375 int depthMapWidth = shadowFP.depthMapWidth(i); |
| 328 if (depthMapWidth != fDepthMapWidth[i]) { | 376 if (depthMapWidth != fDepthMapWidth[i]) { |
| 329 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); | 377 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); |
| 330 fDepthMapWidth[i] = depthMapWidth; | 378 fDepthMapWidth[i] = depthMapWidth; |
| 331 } | 379 } |
| 332 int depthMapHeight = shadowFP.depthMapHeight(i); | 380 int depthMapHeight = shadowFP.depthMapHeight(i); |
| 333 if (depthMapHeight != fDepthMapHeight[i]) { | 381 if (depthMapHeight != fDepthMapHeight[i]) { |
| 334 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); | 382 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); |
| 335 fDepthMapHeight[i] = depthMapHeight; | 383 fDepthMapHeight[i] = depthMapHeight; |
| 336 } | 384 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 348 } | 396 } |
| 349 | 397 |
| 350 const SkColor3f& ambientColor = shadowFP.ambientColor(); | 398 const SkColor3f& ambientColor = shadowFP.ambientColor(); |
| 351 if (ambientColor != fAmbientColor) { | 399 if (ambientColor != fAmbientColor) { |
| 352 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 400 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
| 353 fAmbientColor = ambientColor; | 401 fAmbientColor = ambientColor; |
| 354 } | 402 } |
| 355 } | 403 } |
| 356 | 404 |
| 357 private: | 405 private: |
| 406 bool fIsPointLight[SkShadowShader::kMaxNonAmbientLights]; | |
| 407 | |
| 358 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; | 408 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
| 359 GrGLSLProgramDataManager::UniformHandle | 409 GrGLSLProgramDataManager::UniformHandle |
| 360 fLightDirUni[SkShadowShader::kMaxNonAmbientLights]; | 410 fLightDirOrPosUni[SkShadowShader::kMaxNonAmbientLights]; |
| 361 | 411 |
| 362 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 412 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 363 GrGLSLProgramDataManager::UniformHandle | 413 GrGLSLProgramDataManager::UniformHandle |
| 364 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; | 414 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; |
| 365 | 415 |
| 416 SkScalar fLightIntensity[SkShadowShader::kMaxNonAmbientLights]; | |
| 417 GrGLSLProgramDataManager::UniformHandle | |
| 418 fLightIntensityUni[SkShadowShader::kMaxNonAmbientLights]; | |
| 419 | |
| 366 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 420 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
| 367 GrGLSLProgramDataManager::UniformHandle | 421 GrGLSLProgramDataManager::UniformHandle |
| 368 fDepthMapWidthUni[SkShadowShader::kMaxNonAmbientLights]; | 422 fDepthMapWidthUni[SkShadowShader::kMaxNonAmbientLights]; |
| 369 | 423 |
| 370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 424 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
| 371 GrGLSLProgramDataManager::UniformHandle | 425 GrGLSLProgramDataManager::UniformHandle |
| 372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; | 426 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; |
| 373 | 427 |
| 374 int fWidth; | 428 int fWidth; |
| 375 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 429 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
| 376 int fHeight; | 430 int fHeight; |
| 377 GrGLSLProgramDataManager::UniformHandle fHeightUni; | 431 GrGLSLProgramDataManager::UniformHandle fHeightUni; |
| 378 | 432 |
| 379 SkColor3f fAmbientColor; | 433 SkColor3f fAmbientColor; |
| 380 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | 434 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
| 381 | 435 |
| 382 int fNumDirLights; | 436 int fNumNonAmbLights; |
| 383 }; | 437 }; |
| 384 | 438 |
| 385 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { | 439 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 386 GLSLShadowFP::GenKey(*this, caps, b); | 440 GLSLShadowFP::GenKey(*this, caps, b); |
| 387 } | 441 } |
| 388 | 442 |
| 389 const char* name() const override { return "shadowFP"; } | 443 const char* name() const override { return "shadowFP"; } |
| 390 | 444 |
| 391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 445 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 392 inout->mulByUnknownFourComponents(); | 446 inout->mulByUnknownFourComponents(); |
| 393 } | 447 } |
| 394 int32_t numLights() const { return fNumDirLights; } | 448 int32_t numLights() const { return fNumNonAmbLights; } |
| 395 const SkColor3f& ambientColor() const { return fAmbientColor; } | 449 const SkColor3f& ambientColor() const { return fAmbientColor; } |
| 396 const SkVector3& lightDir(int i) const { | 450 bool isPointLight(int i) const { |
| 397 SkASSERT(i < fNumDirLights); | 451 SkASSERT(i < fNumNonAmbLights); |
| 398 return fLightDir[i]; | 452 return fIsPointLight[i]; |
| 453 } | |
| 454 const SkVector3& lightDirOrPos(int i) const { | |
| 455 SkASSERT(i < fNumNonAmbLights); | |
| 456 return fLightDirOrPos[i]; | |
| 399 } | 457 } |
| 400 const SkVector3& lightColor(int i) const { | 458 const SkVector3& lightColor(int i) const { |
| 401 SkASSERT(i < fNumDirLights); | 459 SkASSERT(i < fNumNonAmbLights); |
| 402 return fLightColor[i]; | 460 return fLightColor[i]; |
| 403 } | 461 } |
| 462 SkScalar lightIntensity(int i) const { | |
| 463 SkASSERT(i < fNumNonAmbLights); | |
| 464 return fLightIntensity[i]; | |
| 465 } | |
| 404 | 466 |
| 405 int depthMapWidth(int i) const { | 467 int depthMapWidth(int i) const { |
| 406 SkASSERT(i < fNumDirLights); | 468 SkASSERT(i < fNumNonAmbLights); |
| 407 return fDepthMapWidth[i]; | 469 return fDepthMapWidth[i]; |
| 408 } | 470 } |
| 409 int depthMapHeight(int i) const { | 471 int depthMapHeight(int i) const { |
| 410 SkASSERT(i < fNumDirLights); | 472 SkASSERT(i < fNumNonAmbLights); |
| 411 return fDepthMapHeight[i]; | 473 return fDepthMapHeight[i]; |
| 412 } | 474 } |
| 413 int width() const {return fWidth; } | 475 int width() const {return fWidth; } |
| 414 int height() const {return fHeight; } | 476 int height() const {return fHeight; } |
| 415 | 477 |
| 416 private: | 478 private: |
| 417 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } | 479 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } |
| 418 | 480 |
| 419 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 481 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
| 420 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 482 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
| 421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { | 483 if (fAmbientColor != shadowFP.fAmbientColor || |
| 484 fNumNonAmbLights != shadowFP.fNumNonAmbLights) { | |
| 422 return false; | 485 return false; |
| 423 } | 486 } |
| 424 | 487 |
| 425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { | 488 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { |
| 426 return false; | 489 return false; |
| 427 } | 490 } |
| 428 | 491 |
| 429 for (int i = 0; i < fNumDirLights; i++) { | 492 for (int i = 0; i < fNumNonAmbLights; i++) { |
| 430 if (fLightDir[i] != shadowFP.fLightDir[i] || | 493 if (fLightDirOrPos[i] != shadowFP.fLightDirOrPos[i] || |
| 431 fLightColor[i] != shadowFP.fLightColor[i]) { | 494 fLightColor[i] != shadowFP.fLightColor[i] || |
| 495 fLightIntensity[i] != shadowFP.fLightIntensity[i] || | |
| 496 fIsPointLight[i] != shadowFP.fIsPointLight[i]) { | |
| 432 return false; | 497 return false; |
| 433 } | 498 } |
| 434 | 499 |
| 435 if (fDepthMapWidth[i] != shadowFP.fDepthMapWidth[i] || | 500 if (fDepthMapWidth[i] != shadowFP.fDepthMapWidth[i] || |
| 436 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { | 501 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { |
| 437 return false; | 502 return false; |
| 438 } | 503 } |
| 439 } | 504 } |
| 440 | 505 |
| 441 return true; | 506 return true; |
| 442 } | 507 } |
| 443 | 508 |
| 444 int fNumDirLights; | 509 int fNumNonAmbLights; |
| 445 | 510 |
| 446 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; | 511 bool fIsPointLight[SkShadowShader::kMaxNonAmbientLights]; |
| 512 SkVector3 fLightDirOrPos[SkShadowShader::kMaxNonAmbientLights]; | |
| 447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 513 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 514 SkScalar fLightIntensity[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 |
| 457 SkColor3f fAmbientColor; | 524 SkColor3f fAmbientColor; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 554 | 621 |
| 555 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 622 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) | 623 // 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) { | 624 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| 558 const SkLights::Light& light = lightShader.fLights->light(l); | 625 const SkLights::Light& light = lightShader.fLights->light(l); |
| 559 | 626 |
| 560 if (SkLights::Light::kAmbient_LightType == light.type()) { | 627 if (SkLights::Light::kAmbient_LightType == light.type()) { |
| 561 accum.fX += light.color().fX * SkColorGetR(diffColor); | 628 accum.fX += light.color().fX * SkColorGetR(diffColor); |
| 562 accum.fY += light.color().fY * SkColorGetG(diffColor); | 629 accum.fY += light.color().fY * SkColorGetG(diffColor); |
| 563 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | 630 accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
| 631 } else if (SkLights::Light::kDirectional_LightType == light.type ()) { | |
| 632 // scaling by fZ accounts for lighting direction | |
| 633 accum.fX += light.color().makeScale(light.dir().fZ).fX * | |
| 634 SkColorGetR(diffColor); | |
| 635 accum.fY += light.color().makeScale(light.dir().fZ).fY * | |
| 636 SkColorGetG(diffColor); | |
| 637 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * | |
| 638 SkColorGetB(diffColor); | |
| 564 } else { | 639 } else { |
| 565 // scaling by fZ accounts for lighting direction | 640 // TODO: do point lights for raster, currently treated like ambient |
| 566 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | 641 accum.fX += light.color().fX * SkColorGetR(diffColor); |
| 567 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | 642 accum.fY += light.color().fY * SkColorGetG(diffColor); |
| 568 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | 643 accum.fZ += light.color().fZ * SkColorGetB(diffColor); |
| 569 } | 644 } |
| 570 } | 645 } |
| 571 | 646 |
| 572 result[i] = convert(accum, SkColorGetA(diffColor)); | 647 result[i] = convert(accum, SkColorGetA(diffColor)); |
| 573 } | 648 } |
| 574 | 649 |
| 575 result += n; | 650 result += n; |
| 576 x += n; | 651 x += n; |
| 577 count -= n; | 652 count -= n; |
| 578 } while (count > 0); | 653 } while (count > 0); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 | 746 |
| 672 /////////////////////////////////////////////////////////////////////////////// | 747 /////////////////////////////////////////////////////////////////////////////// |
| 673 | 748 |
| 674 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 749 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 675 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 750 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 676 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 751 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 677 | 752 |
| 678 /////////////////////////////////////////////////////////////////////////////// | 753 /////////////////////////////////////////////////////////////////////////////// |
| 679 | 754 |
| 680 #endif | 755 #endif |
| OLD | NEW |