Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
| 10 #include "SkColor.h" | 10 #include "SkColor.h" |
| 11 #include "SkEmptyShader.h" | 11 #include "SkEmptyShader.h" |
| 12 #include "SkErrorInternals.h" | 12 #include "SkErrorInternals.h" |
| 13 #include "SkLightingShader.h" | 13 #include "SkLightingShader.h" |
| 14 #include "SkMathPriv.h" | 14 #include "SkMathPriv.h" |
| 15 #include "SkPoint3.h" | |
| 15 #include "SkReadBuffer.h" | 16 #include "SkReadBuffer.h" |
| 16 #include "SkWriteBuffer.h" | 17 #include "SkWriteBuffer.h" |
| 17 | 18 |
| 18 //////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////// |
| 19 | 20 |
| 20 /* | 21 /* |
| 21 SkLightingShader TODOs: | 22 SkLightingShader TODOs: |
| 22 support other than clamp mode | 23 support other than clamp mode |
| 23 allow 'diffuse' & 'normal' to be of different dimensions? | 24 allow 'diffuse' & 'normal' to be of different dimensions? |
| 24 support different light types | 25 support different light types |
| 25 support multiple lights | 26 support multiple lights |
| 26 enforce normal map is 4 channel | 27 enforce normal map is 4 channel |
| 27 use SkImages instead if SkBitmaps | 28 use SkImages instead if SkBitmaps |
| 28 | 29 |
| 29 To Test: | 30 To Test: |
| 30 non-opaque diffuse textures | 31 non-opaque diffuse textures |
| 31 A8 diffuse textures | 32 A8 diffuse textures |
| 32 down & upsampled draws | 33 down & upsampled draws |
| 33 */ | 34 */ |
| 34 | 35 |
| 35 | 36 |
| 36 | 37 |
| 37 /** \class SkLightingShaderImpl | 38 /** \class SkLightingShaderImpl |
| 38 This subclass of shader applies lighting. | 39 This subclass of shader applies lighting. |
| 39 */ | 40 */ |
| 40 class SK_API SkLightingShaderImpl : public SkShader { | 41 class SK_API SkLightingShaderImpl : public SkShader { |
| 41 public: | 42 public: |
| 42 | 43 |
| 43 /** Create a new lighting shader that use the provided normal map, light | 44 /** Create a new lighting shader that uses the provided normal map and |
| 44 and ambient color to light the diffuse bitmap. | 45 lights to light the diffuse bitmap. |
| 45 @param diffuse the diffuse bitmap | 46 @param diffuse the diffuse bitmap |
| 46 @param normal the normal map | 47 @param normal the normal map |
| 47 @param light the light applied to the normal map | 48 @param lights the lights applied to the normal map |
| 48 @param ambient the linear (unpremul) ambient light color | 49 @param invNormRotation rotation applied to the normal map's normals |
| 50 @param diffLocalM the local matrix for the diffuse coordinates | |
| 51 @param normLocalM the local matrix for the normal coordinates | |
| 49 */ | 52 */ |
| 50 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, | 53 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, |
| 51 const SkLightingShader::Light& light, | 54 const SkLightingShader::Lights* lights, |
| 52 const SkColor3f& ambient, const SkMatrix* localMatrix) | 55 const SkVector& invNormRotation, |
| 53 : INHERITED(localMatrix) | 56 const SkMatrix* diffLocalM, const SkMatrix* normLocalM) |
| 57 : INHERITED(diffLocalM) | |
| 54 , fDiffuseMap(diffuse) | 58 , fDiffuseMap(diffuse) |
| 55 , fNormalMap(normal) | 59 , fNormalMap(normal) |
| 56 , fLight(light) | 60 , fLights(SkRef(lights)) |
| 57 , fAmbientColor(ambient) { | 61 , fInvNormRotation(invNormRotation) { |
| 58 if (!fLight.fDirection.normalize()) { | 62 |
| 59 fLight.fDirection = SkPoint3::Make(0.0f, 0.0f, 1.0f); | 63 if (normLocalM) { |
| 64 fNormLocalMatrix = *normLocalM; | |
| 65 } else { | |
| 66 fNormLocalMatrix.reset(); | |
| 60 } | 67 } |
| 68 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf e. | |
| 69 (void)fNormLocalMatrix.getType(); | |
| 70 | |
| 61 } | 71 } |
| 62 | 72 |
| 63 bool isOpaque() const override; | 73 bool isOpaque() const override; |
| 64 | 74 |
| 65 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v iewM, | 75 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v iewM, |
| 66 const SkMatrix* localMatrix, GrColor* color, | 76 const SkMatrix* localMatrix, GrColor* color, |
| 67 GrProcessorDataManager*, GrFragmentProcessor** fp) const override; | 77 GrProcessorDataManager*, GrFragmentProcessor** fp) const override; |
| 68 | 78 |
| 69 size_t contextSize() const override; | 79 size_t contextSize() const override; |
| 70 | 80 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 87 | 97 |
| 88 typedef SkShader::Context INHERITED; | 98 typedef SkShader::Context INHERITED; |
| 89 }; | 99 }; |
| 90 | 100 |
| 91 SK_TO_STRING_OVERRIDE() | 101 SK_TO_STRING_OVERRIDE() |
| 92 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) | 102 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) |
| 93 | 103 |
| 94 protected: | 104 protected: |
| 95 void flatten(SkWriteBuffer&) const override; | 105 void flatten(SkWriteBuffer&) const override; |
| 96 Context* onCreateContext(const ContextRec&, void*) const override; | 106 Context* onCreateContext(const ContextRec&, void*) const override; |
| 107 bool computeNormTotalInverse(const ContextRec& rec, SkMatrix* normTotalInver se) const; | |
| 97 | 108 |
| 98 private: | 109 private: |
| 99 SkBitmap fDiffuseMap; | 110 SkBitmap fDiffuseMap; |
| 100 SkBitmap fNormalMap; | 111 SkBitmap fNormalMap; |
| 101 SkLightingShader::Light fLight; | 112 |
| 102 SkColor3f fAmbientColor; // linear (unpremul) color. Range is 0..1/channel. | 113 SkAutoTUnref<const SkLightingShader::Lights> fLights; |
| 114 | |
| 115 SkMatrix fNormLocalMatrix; | |
| 116 SkVector fInvNormRotation; | |
| 103 | 117 |
| 104 friend class SkLightingShader; | 118 friend class SkLightingShader; |
| 105 | 119 |
| 106 typedef SkShader INHERITED; | 120 typedef SkShader INHERITED; |
| 107 }; | 121 }; |
| 108 | 122 |
| 109 //////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////// |
| 110 | 124 |
| 111 #if SK_SUPPORT_GPU | 125 #if SK_SUPPORT_GPU |
| 112 | 126 |
| 113 #include "GrCoordTransform.h" | 127 #include "GrCoordTransform.h" |
| 114 #include "GrFragmentProcessor.h" | 128 #include "GrFragmentProcessor.h" |
| 115 #include "GrTextureAccess.h" | 129 #include "GrTextureAccess.h" |
| 116 #include "gl/GrGLProcessor.h" | 130 #include "gl/GrGLProcessor.h" |
| 117 #include "gl/builders/GrGLProgramBuilder.h" | 131 #include "gl/builders/GrGLProgramBuilder.h" |
| 118 #include "SkGr.h" | 132 #include "SkGr.h" |
| 119 | 133 |
| 120 class LightingFP : public GrFragmentProcessor { | 134 class LightingFP : public GrFragmentProcessor { |
| 121 public: | 135 public: |
| 122 LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& matrix, | 136 LightingFP(GrProcessorDataManager* pdm, GrTexture* diffuse, GrTexture* norma l, |
| 123 const SkVector3& lightDir, const SkColor3f& lightColor, | 137 const SkMatrix& diffMatrix, const SkMatrix& normMatrix, |
| 124 const SkColor3f& ambientColor) | 138 const GrTextureParams& diffParams, const GrTextureParams& normPar ams, |
| 125 : fDeviceTransform(kDevice_GrCoordSet, matrix) | 139 const SkLightingShader::Lights* lights, const SkVector& invNormRo tation) |
| 126 , fDiffuseTextureAccess(diffuse) | 140 : fDiffDeviceTransform(kLocal_GrCoordSet, diffMatrix, diffuse, diffParam s.filterMode()) |
| 127 , fNormalTextureAccess(normal) | 141 , fNormDeviceTransform(kLocal_GrCoordSet, normMatrix, normal, normParams .filterMode()) |
| 128 , fLightDir(lightDir) | 142 , fDiffuseTextureAccess(diffuse, diffParams) |
| 129 , fLightColor(lightColor) | 143 , fNormalTextureAccess(normal, normParams) |
| 130 , fAmbientColor(ambientColor) { | 144 , fInvNormRotation(invNormRotation) { |
| 131 this->addCoordTransform(&fDeviceTransform); | 145 this->addCoordTransform(&fDiffDeviceTransform); |
| 146 this->addCoordTransform(&fNormDeviceTransform); | |
| 132 this->addTextureAccess(&fDiffuseTextureAccess); | 147 this->addTextureAccess(&fDiffuseTextureAccess); |
| 133 this->addTextureAccess(&fNormalTextureAccess); | 148 this->addTextureAccess(&fNormalTextureAccess); |
| 134 | 149 |
| 150 // fuse all ambient lights into a single one | |
| 151 fAmbientColor.set(0.0f, 0.0f, 0.0f); | |
| 152 for (int i = 0; i < lights->numLights(); ++i) { | |
| 153 if (SkLight::kAmbient_LightType == lights->light(i).type()) { | |
| 154 fAmbientColor += lights->light(i).color(); | |
| 155 } else { | |
| 156 // TODO: handle more than one of these | |
| 157 fLightColor = lights->light(i).color(); | |
| 158 fLightDir = lights->light(i).dir(); | |
| 159 } | |
| 160 } | |
| 161 | |
| 135 this->initClassID<LightingFP>(); | 162 this->initClassID<LightingFP>(); |
| 136 } | 163 } |
| 137 | 164 |
| 138 class LightingGLFP : public GrGLFragmentProcessor { | 165 class LightingGLFP : public GrGLFragmentProcessor { |
| 139 public: | 166 public: |
| 140 LightingGLFP() { | 167 LightingGLFP() { |
| 141 fLightDir.fX = 10000.0f; | 168 fLightDir.fX = 10000.0f; |
| 142 fLightColor.fX = 0.0f; | 169 fLightColor.fX = 0.0f; |
| 143 fAmbientColor.fX = 0.0f; | 170 fAmbientColor.fX = 0.0f; |
| 171 fInvNormRotation.fX = 0.0f; | |
| 144 } | 172 } |
| 145 | 173 |
| 146 void emitCode(EmitArgs& args) override { | 174 void emitCode(EmitArgs& args) override { |
| 147 | 175 |
| 148 GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder() ; | 176 GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder() ; |
| 149 | 177 |
| 150 // add uniforms | 178 // add uniforms |
| 151 const char* lightDirUniName = NULL; | 179 const char* lightDirUniName = NULL; |
| 152 fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragme nt_Visibility, | 180 fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragme nt_Visibility, |
| 153 kVec3f_GrSLType, kDefault_G rSLPrecision, | 181 kVec3f_GrSLType, kDefault_G rSLPrecision, |
| 154 "LightDir", &lightDirUniNam e); | 182 "LightDir", &lightDirUniNam e); |
| 155 | 183 |
| 156 const char* lightColorUniName = NULL; | 184 const char* lightColorUniName = NULL; |
| 157 fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFrag ment_Visibility, | 185 fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFrag ment_Visibility, |
| 158 kVec3f_GrSLType, kDefault _GrSLPrecision, | 186 kVec3f_GrSLType, kDefault _GrSLPrecision, |
| 159 "LightColor", &lightColor UniName); | 187 "LightColor", &lightColor UniName); |
| 160 | 188 |
| 161 const char* ambientColorUniName = NULL; | 189 const char* ambientColorUniName = NULL; |
| 162 fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFr agment_Visibility, | 190 fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFr agment_Visibility, |
| 163 kVec3f_GrSLType, kDefau lt_GrSLPrecision, | 191 kVec3f_GrSLType, kDefau lt_GrSLPrecision, |
| 164 "AmbientColor", &ambien tColorUniName); | 192 "AmbientColor", &ambien tColorUniName); |
| 165 | 193 |
| 194 const char* xformUniName = NULL; | |
| 195 fXformUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_ Visibility, | |
| 196 kVec2f_GrSLType, kDefault_GrSL Precision, | |
| 197 "Xform", &xformUniName); | |
| 198 | |
| 166 fpb->codeAppend("vec4 diffuseColor = "); | 199 fpb->codeAppend("vec4 diffuseColor = "); |
| 167 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers [0], | 200 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers [0], |
| 168 args.fCoords[0].c_str(), | 201 args.fCoords[0].c_str(), |
| 169 args.fCoords[0].getType()); | 202 args.fCoords[0].getType()); |
| 170 fpb->codeAppend(";"); | 203 fpb->codeAppend(";"); |
| 171 | 204 |
| 172 fpb->codeAppend("vec4 normalColor = "); | 205 fpb->codeAppend("vec4 normalColor = "); |
| 173 fpb->appendTextureLookup(args.fSamplers[1], | 206 fpb->appendTextureLookup(args.fSamplers[1], |
| 174 args.fCoords[0].c_str(), | 207 args.fCoords[1].c_str(), |
| 175 args.fCoords[0].getType()); | 208 args.fCoords[1].getType()); |
| 176 fpb->codeAppend(";"); | 209 fpb->codeAppend(";"); |
| 177 | 210 |
| 178 fpb->codeAppend("vec3 normal = normalize(normalColor.rgb - vec3(0.5) );"); | 211 fpb->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"); |
| 179 fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName); | 212 |
| 180 fpb->codeAppend("float NdotL = dot(normal, lightDir);"); | 213 fpb->codeAppendf("mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.0, 0 .0, 0.0, 1.0);", |
| 214 xformUniName, xformUniName, xformUniName, xformUniN ame); | |
| 215 | |
| 216 fpb->codeAppend("normal = normalize(m*normal);"); | |
|
jvanverth1
2015/08/19 13:57:48
It's more efficient to multiply the light directio
robertphillips
2015/08/19 16:17:57
I've added a TODO.
| |
| 217 | |
| 218 fpb->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0, 1.0);", lightDirUniName); | |
| 181 // diffuse light | 219 // diffuse light |
| 182 fpb->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightCo lorUniName); | 220 fpb->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightCo lorUniName); |
| 183 // ambient light | 221 // ambient light |
| 184 fpb->codeAppendf("result += %s;", ambientColorUniName); | 222 fpb->codeAppendf("result += %s;", ambientColorUniName); |
| 185 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut putColor); | 223 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut putColor); |
| 186 } | 224 } |
| 187 | 225 |
| 188 void setData(const GrGLProgramDataManager& pdman, const GrProcessor& pro c) override { | 226 void setData(const GrGLProgramDataManager& pdman, const GrProcessor& pro c) override { |
| 189 const LightingFP& lightingFP = proc.cast<LightingFP>(); | 227 const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 190 | 228 |
| 191 const SkVector3& lightDir = lightingFP.lightDir(); | 229 const SkVector3& lightDir = lightingFP.lightDir(); |
| 192 if (lightDir != fLightDir) { | 230 if (lightDir != fLightDir) { |
| 193 pdman.set3fv(fLightDirUni, 1, &lightDir.fX); | 231 pdman.set3fv(fLightDirUni, 1, &lightDir.fX); |
| 194 fLightDir = lightDir; | 232 fLightDir = lightDir; |
| 195 } | 233 } |
| 196 | 234 |
| 197 const SkColor3f& lightColor = lightingFP.lightColor(); | 235 const SkColor3f& lightColor = lightingFP.lightColor(); |
| 198 if (lightColor != fLightColor) { | 236 if (lightColor != fLightColor) { |
| 199 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); | 237 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); |
| 200 fLightColor = lightColor; | 238 fLightColor = lightColor; |
| 201 } | 239 } |
| 202 | 240 |
| 203 const SkColor3f& ambientColor = lightingFP.ambientColor(); | 241 const SkColor3f& ambientColor = lightingFP.ambientColor(); |
| 204 if (ambientColor != fAmbientColor) { | 242 if (ambientColor != fAmbientColor) { |
| 205 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 243 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
| 206 fAmbientColor = ambientColor; | 244 fAmbientColor = ambientColor; |
| 207 } | 245 } |
| 246 | |
| 247 const SkVector& invNormRotation = lightingFP.invNormRotation(); | |
| 248 if (invNormRotation != fInvNormRotation) { | |
| 249 pdman.set2fv(fXformUni, 1, &invNormRotation.fX); | |
| 250 fInvNormRotation = invNormRotation; | |
| 251 } | |
| 208 } | 252 } |
| 209 | 253 |
| 210 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 254 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 211 GrProcessorKeyBuilder* b) { | 255 GrProcessorKeyBuilder* b) { |
| 212 // const LightingFP& lightingFP = proc.cast<LightingFP>(); | 256 // const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 213 // only one shader generated currently | 257 // only one shader generated currently |
| 214 b->add32(0x0); | 258 b->add32(0x0); |
| 215 } | 259 } |
| 216 | 260 |
| 217 private: | 261 private: |
| 218 SkVector3 fLightDir; | 262 SkVector3 fLightDir; |
| 219 GrGLProgramDataManager::UniformHandle fLightDirUni; | 263 GrGLProgramDataManager::UniformHandle fLightDirUni; |
| 220 | 264 |
| 221 SkColor3f fLightColor; | 265 SkColor3f fLightColor; |
| 222 GrGLProgramDataManager::UniformHandle fLightColorUni; | 266 GrGLProgramDataManager::UniformHandle fLightColorUni; |
| 223 | 267 |
| 224 SkColor3f fAmbientColor; | 268 SkColor3f fAmbientColor; |
| 225 GrGLProgramDataManager::UniformHandle fAmbientColorUni; | 269 GrGLProgramDataManager::UniformHandle fAmbientColorUni; |
| 270 | |
| 271 SkVector fInvNormRotation; | |
| 272 GrGLProgramDataManager::UniformHandle fXformUni; | |
| 226 }; | 273 }; |
| 227 | 274 |
| 228 GrGLFragmentProcessor* createGLInstance() const override { return SkNEW(Ligh tingGLFP); } | 275 GrGLFragmentProcessor* createGLInstance() const override { return SkNEW(Ligh tingGLFP); } |
| 229 | 276 |
| 230 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c onst override { | 277 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c onst override { |
| 231 LightingGLFP::GenKey(*this, caps, b); | 278 LightingGLFP::GenKey(*this, caps, b); |
| 232 } | 279 } |
| 233 | 280 |
| 234 const char* name() const override { return "LightingFP"; } | 281 const char* name() const override { return "LightingFP"; } |
| 235 | 282 |
| 236 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 283 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 237 inout->mulByUnknownFourComponents(); | 284 inout->mulByUnknownFourComponents(); |
| 238 } | 285 } |
| 239 | 286 |
| 240 const SkVector3& lightDir() const { return fLightDir; } | 287 const SkVector3& lightDir() const { return fLightDir; } |
| 241 const SkColor3f& lightColor() const { return fLightColor; } | 288 const SkColor3f& lightColor() const { return fLightColor; } |
| 242 const SkColor3f& ambientColor() const { return fAmbientColor; } | 289 const SkColor3f& ambientColor() const { return fAmbientColor; } |
| 290 const SkVector& invNormRotation() const { return fInvNormRotation; } | |
| 243 | 291 |
| 244 private: | 292 private: |
| 245 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 293 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
| 246 const LightingFP& lightingFP = proc.cast<LightingFP>(); | 294 const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 247 return fDeviceTransform == lightingFP.fDeviceTransform && | 295 return fDiffDeviceTransform == lightingFP.fDiffDeviceTransform && |
| 296 fNormDeviceTransform == lightingFP.fNormDeviceTransform && | |
| 248 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && | 297 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && |
| 249 fNormalTextureAccess == lightingFP.fNormalTextureAccess && | 298 fNormalTextureAccess == lightingFP.fNormalTextureAccess && |
| 250 fLightDir == lightingFP.fLightDir && | 299 fLightDir == lightingFP.fLightDir && |
| 251 fLightColor == lightingFP.fLightColor && | 300 fLightColor == lightingFP.fLightColor && |
| 252 fAmbientColor == lightingFP.fAmbientColor; | 301 fAmbientColor == lightingFP.fAmbientColor && |
| 302 fInvNormRotation == lightingFP.fInvNormRotation; | |
| 253 } | 303 } |
| 254 | 304 |
| 255 GrCoordTransform fDeviceTransform; | 305 GrCoordTransform fDiffDeviceTransform; |
| 306 GrCoordTransform fNormDeviceTransform; | |
| 256 GrTextureAccess fDiffuseTextureAccess; | 307 GrTextureAccess fDiffuseTextureAccess; |
| 257 GrTextureAccess fNormalTextureAccess; | 308 GrTextureAccess fNormalTextureAccess; |
| 258 SkVector3 fLightDir; | 309 SkVector3 fLightDir; |
| 259 SkColor3f fLightColor; | 310 SkColor3f fLightColor; |
| 260 SkColor3f fAmbientColor; | 311 SkColor3f fAmbientColor; |
| 312 | |
| 313 SkVector fInvNormRotation; | |
| 261 }; | 314 }; |
| 262 | 315 |
| 263 //////////////////////////////////////////////////////////////////////////// | 316 //////////////////////////////////////////////////////////////////////////// |
| 264 | 317 |
| 265 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint & paint, | 318 static bool make_mat(const SkBitmap& bm, |
| 266 const SkMatrix& viewM, const SkMa trix* localMatrix, | 319 const SkMatrix& localMatrix1, |
| 267 GrColor* color, GrProcessorDataMa nager*, | 320 const SkMatrix* localMatrix2, |
| 268 GrFragmentProcessor** fp) const { | 321 SkMatrix* result) { |
| 269 // we assume diffuse and normal maps have same width and height | 322 |
| 270 // TODO: support different sizes | 323 result->setIDiv(bm.width(), bm.height()); |
| 271 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | |
| 272 fDiffuseMap.height() == fNormalMap.height()); | |
| 273 SkMatrix matrix; | |
| 274 matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height()); | |
| 275 | 324 |
| 276 SkMatrix lmInverse; | 325 SkMatrix lmInverse; |
| 277 if (!this->getLocalMatrix().invert(&lmInverse)) { | 326 if (!localMatrix1.invert(&lmInverse)) { |
| 278 return false; | 327 return false; |
| 279 } | 328 } |
| 280 if (localMatrix) { | 329 if (localMatrix2) { |
| 281 SkMatrix inv; | 330 SkMatrix inv; |
| 282 if (!localMatrix->invert(&inv)) { | 331 if (!localMatrix2->invert(&inv)) { |
| 283 return false; | 332 return false; |
| 284 } | 333 } |
| 285 lmInverse.postConcat(inv); | 334 lmInverse.postConcat(inv); |
| 286 } | 335 } |
| 287 matrix.preConcat(lmInverse); | 336 result->preConcat(lmInverse); |
| 288 | 337 |
| 338 return true; | |
| 339 } | |
| 340 | |
| 341 static GrTextureParams::FilterMode pick_filter_level(SkFilterQuality filterIn, | |
| 342 const SkMatrix& viewM, | |
| 343 const SkMatrix& localM) { | |
| 289 // Must set wrap and filter on the sampler before requesting a texture. In t wo places below | 344 // Must set wrap and filter on the sampler before requesting a texture. In t wo places below |
| 290 // we check the matrix scale factors to determine how to interpret the filte r quality setting. | 345 // we check the matrix scale factors to determine how to interpret the filte r quality setting. |
| 291 // This completely ignores the complexity of the drawVertices case where exp licit local coords | 346 // This completely ignores the complexity of the drawVertices case where exp licit local coords |
| 292 // are provided by the caller. | 347 // are provided by the caller. |
| 293 GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_Fil terMode; | 348 GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_Fil terMode; |
| 294 switch (paint.getFilterQuality()) { | 349 switch (filterIn) { |
| 295 case kNone_SkFilterQuality: | 350 case kNone_SkFilterQuality: |
| 296 textureFilterMode = GrTextureParams::kNone_FilterMode; | 351 textureFilterMode = GrTextureParams::kNone_FilterMode; |
| 297 break; | 352 break; |
| 298 case kLow_SkFilterQuality: | 353 case kLow_SkFilterQuality: |
| 299 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | 354 textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 300 break; | 355 break; |
| 301 case kMedium_SkFilterQuality:{ | 356 case kMedium_SkFilterQuality:{ |
| 302 SkMatrix matrix; | 357 SkMatrix matrix; |
| 303 matrix.setConcat(viewM, this->getLocalMatrix()); | 358 matrix.setConcat(viewM, localM); |
| 304 if (matrix.getMinScale() < SK_Scalar1) { | 359 if (matrix.getMinScale() < SK_Scalar1) { |
| 305 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | 360 textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 306 } else { | 361 } else { |
| 307 // Don't trigger MIP level generation unnecessarily. | 362 // Don't trigger MIP level generation unnecessarily. |
| 308 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | 363 textureFilterMode = GrTextureParams::kBilerp_FilterMode; |
| 309 } | 364 } |
| 310 break; | 365 break; |
| 311 } | 366 } |
| 312 case kHigh_SkFilterQuality: | 367 case kHigh_SkFilterQuality: |
| 313 default: | 368 default: |
| 314 SkErrorInternals::SetError(kInvalidPaint_SkError, | 369 SkErrorInternals::SetError(kInvalidPaint_SkError, |
| 315 "Sorry, I don't understand the filtering " | 370 "Sorry, I don't understand the filtering " |
| 316 "mode you asked for. Falling back to " | 371 "mode you asked for. Falling back to " |
| 317 "MIPMaps."); | 372 "MIPMaps."); |
| 318 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | 373 textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
| 319 break; | 374 break; |
| 320 | 375 |
| 321 } | 376 } |
| 322 | 377 |
| 378 return textureFilterMode; | |
| 379 } | |
| 380 | |
| 381 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint & paint, | |
| 382 const SkMatrix& viewM, const SkMa trix* localMatrix, | |
| 383 GrColor* color, GrProcessorDataMa nager* pdm, | |
| 384 GrFragmentProcessor** fp) const { | |
| 385 // we assume diffuse and normal maps have same width and height | |
| 386 // TODO: support different sizes | |
| 387 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | |
| 388 fDiffuseMap.height() == fNormalMap.height()); | |
| 389 SkMatrix diffM, normM; | |
| 390 | |
| 391 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { | |
| 392 return false; | |
| 393 } | |
| 394 | |
| 395 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) { | |
| 396 return false; | |
| 397 } | |
| 398 | |
| 399 GrTextureParams::FilterMode diffFilterMode = pick_filter_level(paint.getFilt erQuality(), | |
| 400 viewM, | |
| 401 this->getLoca lMatrix()); | |
| 402 | |
| 403 GrTextureParams::FilterMode normFilterMode = pick_filter_level(paint.getFilt erQuality(), | |
| 404 viewM, | |
| 405 fNormLocalMat rix); | |
| 406 | |
| 323 // TODO: support other tile modes | 407 // TODO: support other tile modes |
| 324 GrTextureParams params(kClamp_TileMode, textureFilterMode); | 408 GrTextureParams diffParams(kClamp_TileMode, diffFilterMode); |
| 325 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDi ffuseMap, ¶ms)); | 409 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, |
| 410 fDiffuseMap, &diffParams)); | |
| 326 if (!diffuseTexture) { | 411 if (!diffuseTexture) { |
| 327 SkErrorInternals::SetError(kInternalError_SkError, | 412 SkErrorInternals::SetError(kInternalError_SkError, |
| 328 "Couldn't convert bitmap to texture."); | 413 "Couldn't convert bitmap to texture."); |
| 329 return false; | 414 return false; |
| 330 } | 415 } |
| 331 | 416 |
| 332 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNor malMap, ¶ms)); | 417 GrTextureParams normParams(kClamp_TileMode, normFilterMode); |
| 418 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, | |
| 419 fNormalMap, & normParams)); | |
| 333 if (!normalTexture) { | 420 if (!normalTexture) { |
| 334 SkErrorInternals::SetError(kInternalError_SkError, | 421 SkErrorInternals::SetError(kInternalError_SkError, |
| 335 "Couldn't convert bitmap to texture."); | 422 "Couldn't convert bitmap to texture."); |
| 336 return false; | 423 return false; |
| 337 } | 424 } |
| 338 | 425 |
| 339 *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix, | 426 |
| 340 fLight.fDirection, fLight.fColor, fAmbientColo r)); | 427 *fp = SkNEW_ARGS(LightingFP, (pdm, diffuseTexture, normalTexture, |
| 428 diffM, normM, diffParams, normParams, fLights, | |
| 429 fInvNormRotation)); | |
| 430 | |
| 341 *color = GrColorPackA4(paint.getAlpha()); | 431 *color = GrColorPackA4(paint.getAlpha()); |
| 342 return true; | 432 return true; |
| 343 } | 433 } |
| 344 #else | 434 #else |
| 345 | 435 |
| 346 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint & paint, | 436 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint & paint, |
| 347 const SkMatrix& viewM, const SkMa trix* localMatrix, | 437 const SkMatrix& viewM, const SkMa trix* localMatrix, |
| 348 GrColor* color, GrProcessorDataMa nager*, | 438 GrColor* color, GrProcessorDataMa nager*, |
| 349 GrFragmentProcessor** fp) const { | 439 GrFragmentProcessor** fp) const { |
| 350 SkDEBUGFAIL("Should not call in GPU-less build"); | 440 SkDEBUGFAIL("Should not call in GPU-less build"); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 fFlags = flags; | 473 fFlags = flags; |
| 384 } | 474 } |
| 385 | 475 |
| 386 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { | 476 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { |
| 387 // The bitmap proc states have been created outside of the context on memory that will be freed | 477 // The bitmap proc states have been created outside of the context on memory that will be freed |
| 388 // elsewhere. Call the destructors but leave the freeing of the memory to th e caller. | 478 // elsewhere. Call the destructors but leave the freeing of the memory to th e caller. |
| 389 fDiffuseState->~SkBitmapProcState(); | 479 fDiffuseState->~SkBitmapProcState(); |
| 390 fNormalState->~SkBitmapProcState(); | 480 fNormalState->~SkBitmapProcState(); |
| 391 } | 481 } |
| 392 | 482 |
| 393 static inline int light(SkScalar light, int diff, SkScalar NdotL, SkScalar ambie nt) { | 483 static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
| 394 SkScalar color = light * diff * NdotL + 255 * ambient; | 484 if (color.fX <= 0.0f) { |
| 395 if (color <= 0.0f) { | 485 color.fX = 0.0f; |
| 396 return 0; | 486 } else if (color.fX >= 255.0f) { |
| 397 } else if (color >= 255.0f) { | 487 color.fX = 255.0f; |
| 398 return 255; | 488 } |
| 399 } else { | 489 |
| 400 return (int) color; | 490 if (color.fY <= 0.0f) { |
| 401 } | 491 color.fY = 0.0f; |
| 492 } else if (color.fY >= 255.0f) { | |
| 493 color.fY = 255.0f; | |
| 494 } | |
| 495 | |
| 496 if (color.fZ <= 0.0f) { | |
| 497 color.fZ = 0.0f; | |
| 498 } else if (color.fZ >= 255.0f) { | |
| 499 color.fZ = 255.0f; | |
| 500 } | |
| 501 | |
| 502 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ) ; | |
| 402 } | 503 } |
| 403 | 504 |
| 404 // larger is better (fewer times we have to loop), but we shouldn't | 505 // larger is better (fewer times we have to loop), but we shouldn't |
| 405 // take up too much stack-space (each could here costs 16 bytes) | 506 // take up too much stack-space (each one here costs 16 bytes) |
| 406 #define TMP_COUNT 16 | 507 #define TMP_COUNT 16 |
| 407 | 508 |
| 408 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, | 509 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, |
| 409 SkPMColor result[], int count) { | 510 SkPMColor result[], int count) { |
| 410 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader Impl&>(fShader); | 511 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader Impl&>(fShader); |
| 411 | 512 |
| 412 SkPMColor tmpColor[TMP_COUNT], tmpColor2[TMP_COUNT]; | 513 uint32_t tmpColor[TMP_COUNT], tmpNormal[TMP_COUNT]; |
| 413 SkPMColor tmpNormal[TMP_COUNT], tmpNormal2[TMP_COUNT]; | 514 SkPMColor tmpColor2[2*TMP_COUNT], tmpNormal2[2*TMP_COUNT]; |
| 414 | 515 |
| 415 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); | 516 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); |
| 416 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32() ; | 517 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32() ; |
| 417 | 518 |
| 418 SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc(); | 519 SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc(); |
| 419 SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32( ); | 520 SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32( ); |
| 420 | 521 |
| 522 int diffMax = fDiffuseState->maxCountForBufferSize(sizeof(tmpColor[0]) * TMP _COUNT); | |
| 523 int normMax = fNormalState->maxCountForBufferSize(sizeof(tmpNormal[0]) * TMP _COUNT); | |
| 524 int max = SkTMin(diffMax, normMax); | |
| 525 | |
| 421 SkASSERT(fDiffuseState->fPixmap.addr()); | 526 SkASSERT(fDiffuseState->fPixmap.addr()); |
| 422 SkASSERT(fNormalState->fPixmap.addr()); | 527 SkASSERT(fNormalState->fPixmap.addr()); |
| 423 | 528 |
| 424 SkPoint3 norm; | 529 SkPoint3 norm, xformedNorm; |
| 425 SkScalar NdotL; | |
| 426 int r, g, b; | |
| 427 | 530 |
| 428 do { | 531 do { |
| 429 int n = count; | 532 int n = count; |
| 430 if (n > TMP_COUNT) { | 533 if (n > max) { |
| 431 n = TMP_COUNT; | 534 n = max; |
| 432 } | 535 } |
| 433 | 536 |
| 434 diffMProc(*fDiffuseState, tmpColor, n, x, y); | 537 diffMProc(*fDiffuseState, tmpColor, n, x, y); |
| 435 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); | 538 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); |
| 436 | 539 |
| 437 normalMProc(*fNormalState, tmpNormal, n, x, y); | 540 normalMProc(*fNormalState, tmpNormal, n, x, y); |
| 438 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); | 541 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); |
| 439 | 542 |
| 440 for (int i = 0; i < n; ++i) { | 543 for (int i = 0; i < n; ++i) { |
| 441 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | 544 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul |
| 442 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, | 545 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, |
| 443 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, | 546 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, |
| 444 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); | 547 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); |
| 445 norm.normalize(); | 548 norm.normalize(); |
| 446 | 549 |
| 550 xformedNorm.fX = lightShader.fInvNormRotation.fX * norm.fX + | |
| 551 lightShader.fInvNormRotation.fY * norm.fY; | |
| 552 xformedNorm.fY = lightShader.fInvNormRotation.fX * norm.fX - | |
| 553 lightShader.fInvNormRotation.fY * norm.fY; | |
| 554 xformedNorm.fZ = norm.fZ; | |
| 555 | |
| 447 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); | 556 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); |
| 448 NdotL = norm.dot(lightShader.fLight.fDirection); | |
| 449 | 557 |
| 450 // This is all done in linear unpremul color space | 558 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
| 451 r = light(lightShader.fLight.fColor.fX, SkColorGetR(diffColor), Ndot L, | 559 // This is all done in linear unpremul color space (each component 0 ..255.0f though) |
| 452 lightShader.fAmbientColor.fX); | 560 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| 453 g = light(lightShader.fLight.fColor.fY, SkColorGetG(diffColor), Ndot L, | 561 const SkLight& light = lightShader.fLights->light(l); |
| 454 lightShader.fAmbientColor.fY); | |
| 455 b = light(lightShader.fLight.fColor.fZ, SkColorGetB(diffColor), Ndot L, | |
| 456 lightShader.fAmbientColor.fZ); | |
| 457 | 562 |
| 458 result[i] = SkPreMultiplyARGB(SkColorGetA(diffColor), r, g, b); | 563 if (SkLight::kAmbient_LightType == light.type()) { |
| 564 accum += light.color().makeScale(255.0f); | |
| 565 } else { | |
| 566 SkScalar NdotL = xformedNorm.dot(light.dir()); | |
| 567 if (NdotL < 0.0f) { | |
| 568 NdotL = 0.0f; | |
| 569 } | |
| 570 | |
| 571 accum.fX += light.color().fX * SkColorGetR(diffColor) * Ndot L; | |
| 572 accum.fY += light.color().fY * SkColorGetG(diffColor) * Ndot L; | |
| 573 accum.fZ += light.color().fZ * SkColorGetB(diffColor) * Ndot L; | |
| 574 } | |
| 575 } | |
| 576 | |
| 577 result[i] = convert(accum, SkColorGetA(diffColor)); | |
| 459 } | 578 } |
| 460 | 579 |
| 461 result += n; | 580 result += n; |
| 462 x += n; | 581 x += n; |
| 463 count -= n; | 582 count -= n; |
| 464 } while (count > 0); | 583 } while (count > 0); |
| 465 } | 584 } |
| 466 | 585 |
| 467 //////////////////////////////////////////////////////////////////////////// | 586 //////////////////////////////////////////////////////////////////////////// |
| 468 | 587 |
| 469 #ifndef SK_IGNORE_TO_STRING | 588 #ifndef SK_IGNORE_TO_STRING |
| 470 void SkLightingShaderImpl::toString(SkString* str) const { | 589 void SkLightingShaderImpl::toString(SkString* str) const { |
| 471 str->appendf("LightingShader: ()"); | 590 str->appendf("LightingShader: ()"); |
| 472 } | 591 } |
| 473 #endif | 592 #endif |
| 474 | 593 |
| 475 SkFlattenable* SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { | 594 SkFlattenable* SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { |
| 476 SkMatrix localMatrix; | 595 SkMatrix diffLocalM; |
| 477 buf.readMatrix(&localMatrix); | 596 bool hasDiffLocalM = buf.readBool(); |
| 597 if (hasDiffLocalM) { | |
| 598 buf.readMatrix(&diffLocalM); | |
| 599 } else { | |
| 600 diffLocalM.reset(); | |
| 601 } | |
| 602 | |
| 603 SkMatrix normLocalM; | |
| 604 bool hasNormLocalM = buf.readBool(); | |
| 605 if (hasNormLocalM) { | |
| 606 buf.readMatrix(&normLocalM); | |
| 607 } else { | |
| 608 normLocalM.reset(); | |
| 609 } | |
| 478 | 610 |
| 479 SkBitmap diffuse; | 611 SkBitmap diffuse; |
| 480 if (!buf.readBitmap(&diffuse)) { | 612 if (!buf.readBitmap(&diffuse)) { |
| 481 return NULL; | 613 return NULL; |
| 482 } | 614 } |
| 483 diffuse.setImmutable(); | 615 diffuse.setImmutable(); |
| 484 | 616 |
| 485 SkBitmap normal; | 617 SkBitmap normal; |
| 486 if (!buf.readBitmap(&normal)) { | 618 if (!buf.readBitmap(&normal)) { |
| 487 return NULL; | 619 return NULL; |
| 488 } | 620 } |
| 489 normal.setImmutable(); | 621 normal.setImmutable(); |
| 490 | 622 |
| 491 SkLightingShader::Light light; | 623 int numLights = buf.readInt(); |
| 492 if (!buf.readScalarArray(&light.fDirection.fX, 3)) { | 624 |
| 493 return NULL; | 625 SkLightingShader::Lights::Builder builder; |
| 494 } | 626 |
| 495 if (!buf.readScalarArray(&light.fColor.fX, 3)) { | 627 for (int l = 0; l < numLights; ++l) { |
| 496 return NULL; | 628 bool isAmbient = buf.readBool(); |
| 629 | |
| 630 SkColor3f color; | |
| 631 if (!buf.readScalarArray(&color.fX, 3)) { | |
| 632 return NULL; | |
| 633 } | |
| 634 | |
| 635 if (isAmbient) { | |
| 636 builder.add(SkLight::SkLight(color)); | |
| 637 } else { | |
| 638 SkVector3 dir; | |
| 639 if (!buf.readScalarArray(&dir.fX, 3)) { | |
| 640 return NULL; | |
| 641 } | |
| 642 builder.add(SkLight::SkLight(color, dir)); | |
| 643 } | |
| 497 } | 644 } |
| 498 | 645 |
| 499 SkColor3f ambient; | 646 SkAutoTUnref<const SkLightingShader::Lights> lights(builder.finish()); |
| 500 if (!buf.readScalarArray(&ambient.fX, 3)) { | |
| 501 return NULL; | |
| 502 } | |
| 503 | 647 |
| 504 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, &l ocalMatrix)); | 648 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, lights, |
| 649 SkVector::Make(1.0f, 0.0f), | |
| 650 &diffLocalM, &normLocalM)); | |
| 505 } | 651 } |
| 506 | 652 |
| 507 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { | 653 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { |
| 508 buf.writeMatrix(this->getLocalMatrix()); | 654 this->INHERITED::flatten(buf); |
| 655 | |
| 656 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); | |
| 657 buf.writeBool(hasNormLocalM); | |
| 658 if (hasNormLocalM) { | |
| 659 buf.writeMatrix(fNormLocalMatrix); | |
| 660 } | |
| 509 | 661 |
| 510 buf.writeBitmap(fDiffuseMap); | 662 buf.writeBitmap(fDiffuseMap); |
| 511 buf.writeBitmap(fNormalMap); | 663 buf.writeBitmap(fNormalMap); |
| 512 buf.writeScalarArray(&fLight.fDirection.fX, 3); | 664 |
| 513 buf.writeScalarArray(&fLight.fColor.fX, 3); | 665 buf.writeInt(fLights->numLights()); |
| 514 buf.writeScalarArray(&fAmbientColor.fX, 3); | 666 for (int l = 0; l < fLights->numLights(); ++l) { |
| 667 const SkLight& light = fLights->light(l); | |
| 668 | |
| 669 bool isAmbient = SkLight::kAmbient_LightType == light.type(); | |
| 670 | |
| 671 buf.writeBool(isAmbient); | |
| 672 buf.writeScalarArray(&light.color().fX, 3); | |
| 673 if (!isAmbient) { | |
| 674 buf.writeScalarArray(&light.dir().fX, 3); | |
| 675 } | |
| 676 } | |
| 677 } | |
| 678 | |
| 679 bool SkLightingShaderImpl::computeNormTotalInverse(const ContextRec& rec, | |
| 680 SkMatrix* normTotalInverse) c onst { | |
| 681 SkMatrix total; | |
| 682 total.setConcat(*rec.fMatrix, fNormLocalMatrix); | |
| 683 | |
| 684 const SkMatrix* m = &total; | |
| 685 if (rec.fLocalMatrix) { | |
| 686 total.setConcat(*m, *rec.fLocalMatrix); | |
| 687 m = &total; | |
| 688 } | |
| 689 return m->invert(normTotalInverse); | |
| 515 } | 690 } |
| 516 | 691 |
| 517 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, | 692 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, |
| 518 void* storage) const { | 693 void* storage) const { |
| 519 | 694 |
| 520 SkMatrix totalInverse; | 695 SkMatrix diffTotalInv; |
| 521 // Do this first, so we know the matrix can be inverted. | 696 // computeTotalInverse was called in SkShader::createContext so we know it w ill succeed |
| 522 if (!this->computeTotalInverse(rec, &totalInverse)) { | 697 SkAssertResult(this->computeTotalInverse(rec, &diffTotalInv)); |
| 698 | |
| 699 SkMatrix normTotalInv; | |
| 700 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { | |
| 523 return NULL; | 701 return NULL; |
| 524 } | 702 } |
| 525 | 703 |
| 526 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); | 704 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); |
| 527 SkBitmapProcState* diffuseState = SkNEW_PLACEMENT(diffuseStateStorage, SkBit mapProcState); | 705 SkBitmapProcState* diffuseState = SkNEW_PLACEMENT(diffuseStateStorage, SkBit mapProcState); |
| 528 SkASSERT(diffuseState); | 706 SkASSERT(diffuseState); |
| 529 | 707 |
| 530 diffuseState->fTileModeX = SkShader::kClamp_TileMode; | 708 diffuseState->fTileModeX = SkShader::kClamp_TileMode; |
| 531 diffuseState->fTileModeY = SkShader::kClamp_TileMode; | 709 diffuseState->fTileModeY = SkShader::kClamp_TileMode; |
| 532 diffuseState->fOrigBitmap = fDiffuseMap; | 710 diffuseState->fOrigBitmap = fDiffuseMap; |
| 533 if (!diffuseState->chooseProcs(totalInverse, *rec.fPaint)) { | 711 if (!diffuseState->chooseProcs(diffTotalInv, *rec.fPaint)) { |
| 534 diffuseState->~SkBitmapProcState(); | 712 diffuseState->~SkBitmapProcState(); |
| 535 return NULL; | 713 return NULL; |
| 536 } | 714 } |
| 537 | 715 |
| 538 void* normalStateStorage = (char*)storage + sizeof(LightingShaderContext) + sizeof(SkBitmapProcState); | 716 void* normalStateStorage = (char*)storage + sizeof(LightingShaderContext) + sizeof(SkBitmapProcState); |
| 539 SkBitmapProcState* normalState = SkNEW_PLACEMENT(normalStateStorage, SkBitma pProcState); | 717 SkBitmapProcState* normalState = SkNEW_PLACEMENT(normalStateStorage, SkBitma pProcState); |
| 540 SkASSERT(normalState); | 718 SkASSERT(normalState); |
| 541 | 719 |
| 542 normalState->fTileModeX = SkShader::kClamp_TileMode; | 720 normalState->fTileModeX = SkShader::kClamp_TileMode; |
| 543 normalState->fTileModeY = SkShader::kClamp_TileMode; | 721 normalState->fTileModeY = SkShader::kClamp_TileMode; |
| 544 normalState->fOrigBitmap = fNormalMap; | 722 normalState->fOrigBitmap = fNormalMap; |
| 545 if (!normalState->chooseProcs(totalInverse, *rec.fPaint)) { | 723 if (!normalState->chooseProcs(normTotalInv, *rec.fPaint)) { |
| 546 diffuseState->~SkBitmapProcState(); | 724 diffuseState->~SkBitmapProcState(); |
| 547 normalState->~SkBitmapProcState(); | 725 normalState->~SkBitmapProcState(); |
| 548 return NULL; | 726 return NULL; |
| 549 } | 727 } |
| 550 | 728 |
| 551 return SkNEW_PLACEMENT_ARGS(storage, LightingShaderContext, (*this, rec, | 729 return SkNEW_PLACEMENT_ARGS(storage, LightingShaderContext, (*this, rec, |
| 552 diffuseState, n ormalState)); | 730 diffuseState, n ormalState)); |
| 553 } | 731 } |
| 554 | 732 |
| 555 /////////////////////////////////////////////////////////////////////////////// | 733 /////////////////////////////////////////////////////////////////////////////// |
| 556 | 734 |
| 557 static bool bitmap_is_too_big(const SkBitmap& bm) { | 735 static bool bitmap_is_too_big(const SkBitmap& bm) { |
| 558 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it | 736 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it |
| 559 // communicates between its matrix-proc and its sampler-proc. Until we can | 737 // communicates between its matrix-proc and its sampler-proc. Until we can |
| 560 // widen that, we have to reject bitmaps that are larger. | 738 // widen that, we have to reject bitmaps that are larger. |
| 561 // | 739 // |
| 562 static const int kMaxSize = 65535; | 740 static const int kMaxSize = 65535; |
| 563 | 741 |
| 564 return bm.width() > kMaxSize || bm.height() > kMaxSize; | 742 return bm.width() > kMaxSize || bm.height() > kMaxSize; |
| 565 } | 743 } |
| 566 | 744 |
| 567 SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& norm al, | 745 SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& norm al, |
| 568 const SkLightingShader::Light& light, | 746 const Lights* lights, |
| 569 const SkColor3f& ambient, | 747 const SkVector& invNormRotation, |
| 570 const SkMatrix* localMatrix) { | 748 const SkMatrix* diffLocalM, const SkMatrix* n ormLocalM) { |
| 571 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || | 749 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || |
| 572 normal.isNull() || bitmap_is_too_big(normal) || | 750 normal.isNull() || bitmap_is_too_big(normal) || |
| 573 diffuse.width() != normal.width() || | 751 diffuse.width() != normal.width() || |
| 574 diffuse.height() != normal.height()) { | 752 diffuse.height() != normal.height()) { |
| 575 return nullptr; | 753 return nullptr; |
| 576 } | 754 } |
| 577 | 755 |
| 578 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, lo calMatrix)); | 756 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); |
| 757 | |
| 758 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, lights, | |
| 759 invNormRotation, diffLocalM, normLo calM)); | |
| 579 } | 760 } |
| 580 | 761 |
| 581 /////////////////////////////////////////////////////////////////////////////// | 762 /////////////////////////////////////////////////////////////////////////////// |
| 582 | 763 |
| 583 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 764 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
| 584 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 765 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
| 585 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 766 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 586 | 767 |
| 587 /////////////////////////////////////////////////////////////////////////////// | 768 /////////////////////////////////////////////////////////////////////////////// |
| OLD | NEW |