OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapProcState.h" | 8 #include "SkBitmapProcState.h" |
9 #include "SkColor.h" | 9 #include "SkColor.h" |
10 #include "SkEmptyShader.h" | 10 #include "SkEmptyShader.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 @param diffuse the diffuse bitmap | 45 @param diffuse the diffuse bitmap |
46 @param normal the normal map | 46 @param normal the normal map |
47 @param lights the lights applied to the normal map | 47 @param lights the lights applied to the normal map |
48 @param invNormRotation rotation applied to the normal map's normals | 48 @param invNormRotation rotation applied to the normal map's normals |
49 @param diffLocalM the local matrix for the diffuse coordinates | 49 @param diffLocalM the local matrix for the diffuse coordinates |
50 @param normLocalM the local matrix for the normal coordinates | 50 @param normLocalM the local matrix for the normal coordinates |
51 */ | 51 */ |
52 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, | 52 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, |
53 const sk_sp<SkLights> lights, | 53 const sk_sp<SkLights> lights, |
54 const SkVector& invNormRotation, | 54 const SkVector& invNormRotation, |
55 const SkMatrix* diffLocalM, const SkMatrix* normLocalM) | 55 const SkMatrix* diffLocalM, const SkMatrix* normLocalM, |
56 sk_sp<const SkLightingShader::NormalSource> normalSourc e) | |
egdaniel
2016/06/10 14:23:28
putting a const inside of an sk_sp is generally a
dvonbeck
2016/06/10 15:22:29
Done.
| |
56 : INHERITED(diffLocalM) | 57 : INHERITED(diffLocalM) |
57 , fDiffuseMap(diffuse) | 58 , fDiffuseMap(diffuse) |
58 , fNormalMap(normal) | 59 , fNormalMap(normal) |
59 , fLights(std::move(lights)) | 60 , fLights(std::move(lights)) |
60 , fInvNormRotation(invNormRotation) { | 61 , fInvNormRotation(invNormRotation) { |
61 | 62 |
62 if (normLocalM) { | 63 if (normLocalM) { |
63 fNormLocalMatrix = *normLocalM; | 64 fNormLocalMatrix = *normLocalM; |
64 } else { | 65 } else { |
65 fNormLocalMatrix.reset(); | 66 fNormLocalMatrix.reset(); |
66 } | 67 } |
67 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf e. | 68 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf e. |
68 (void)fNormLocalMatrix.getType(); | 69 (void)fNormLocalMatrix.getType(); |
69 | 70 |
71 fNormalSource = normalSource; | |
egdaniel
2016/06/10 14:23:28
use std::move(
dvonbeck
2016/06/10 15:22:29
Done.
| |
70 } | 72 } |
71 | 73 |
72 bool isOpaque() const override; | 74 bool isOpaque() const override; |
73 | 75 |
74 #if SK_SUPPORT_GPU | 76 #if SK_SUPPORT_GPU |
75 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, | 77 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, |
76 const SkMatrix& viewM, | 78 const SkMatrix& viewM, |
77 const SkMatrix* localMatrix, | 79 const SkMatrix* localMatrix, |
78 SkFilterQuality, | 80 SkFilterQuality, |
79 SkSourceGammaTreatment) const override; | 81 SkSourceGammaTreatment) const override; |
(...skipping 30 matching lines...) Expand all Loading... | |
110 | 112 |
111 private: | 113 private: |
112 SkBitmap fDiffuseMap; | 114 SkBitmap fDiffuseMap; |
113 SkBitmap fNormalMap; | 115 SkBitmap fNormalMap; |
114 | 116 |
115 sk_sp<SkLights> fLights; | 117 sk_sp<SkLights> fLights; |
116 | 118 |
117 SkMatrix fNormLocalMatrix; | 119 SkMatrix fNormLocalMatrix; |
118 SkVector fInvNormRotation; | 120 SkVector fInvNormRotation; |
119 | 121 |
122 sk_sp<const SkLightingShader::NormalSource> fNormalSource; | |
egdaniel
2016/06/10 14:23:27
again const sk_sp is bad.
dvonbeck
2016/06/10 15:22:29
Done.
| |
123 | |
120 friend class SkLightingShader; | 124 friend class SkLightingShader; |
121 | 125 |
122 typedef SkShader INHERITED; | 126 typedef SkShader INHERITED; |
123 }; | 127 }; |
124 | 128 |
125 //////////////////////////////////////////////////////////////////////////// | 129 //////////////////////////////////////////////////////////////////////////// |
126 | 130 |
127 #if SK_SUPPORT_GPU | 131 #if SK_SUPPORT_GPU |
128 | 132 |
129 #include "GrCoordTransform.h" | 133 #include "GrCoordTransform.h" |
130 #include "GrFragmentProcessor.h" | 134 #include "GrFragmentProcessor.h" |
131 #include "GrInvariantOutput.h" | 135 #include "GrInvariantOutput.h" |
132 #include "GrTextureAccess.h" | 136 #include "GrTextureAccess.h" |
133 #include "glsl/GrGLSLFragmentProcessor.h" | 137 #include "glsl/GrGLSLFragmentProcessor.h" |
134 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 138 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
135 #include "glsl/GrGLSLProgramDataManager.h" | 139 #include "glsl/GrGLSLProgramDataManager.h" |
136 #include "glsl/GrGLSLUniformHandler.h" | 140 #include "glsl/GrGLSLUniformHandler.h" |
137 #include "SkGr.h" | 141 #include "SkGr.h" |
138 #include "SkGrPriv.h" | 142 #include "SkGrPriv.h" |
139 | 143 |
140 class LightingFP : public GrFragmentProcessor { | 144 class LightingFP : public GrFragmentProcessor { |
141 public: | 145 public: |
142 LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& diffMatrix , | 146 LightingFP(GrTexture* diffuse, const SkMatrix& diffMatrix, const GrTexturePa rams& diffParams, |
143 const SkMatrix& normMatrix, const GrTextureParams& diffParams, | 147 sk_sp<SkLights> lights, sk_sp<GrFragmentProcessor> normalFP) |
144 const GrTextureParams& normParams, sk_sp<SkLights> lights, | |
145 const SkVector& invNormRotation) | |
146 : fDiffDeviceTransform(kLocal_GrCoordSet, diffMatrix, diffuse, diffParam s.filterMode()) | 148 : fDiffDeviceTransform(kLocal_GrCoordSet, diffMatrix, diffuse, diffParam s.filterMode()) |
147 , fNormDeviceTransform(kLocal_GrCoordSet, normMatrix, normal, normParams .filterMode()) | 149 , fDiffuseTextureAccess(diffuse, diffParams) { |
148 , fDiffuseTextureAccess(diffuse, diffParams) | |
149 , fNormalTextureAccess(normal, normParams) | |
150 , fInvNormRotation(invNormRotation) { | |
151 this->addCoordTransform(&fDiffDeviceTransform); | 150 this->addCoordTransform(&fDiffDeviceTransform); |
152 this->addCoordTransform(&fNormDeviceTransform); | |
153 this->addTextureAccess(&fDiffuseTextureAccess); | 151 this->addTextureAccess(&fDiffuseTextureAccess); |
154 this->addTextureAccess(&fNormalTextureAccess); | |
155 | 152 |
156 // fuse all ambient lights into a single one | 153 // fuse all ambient lights into a single one |
157 fAmbientColor.set(0.0f, 0.0f, 0.0f); | 154 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
158 for (int i = 0; i < lights->numLights(); ++i) { | 155 for (int i = 0; i < lights->numLights(); ++i) { |
159 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { | 156 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { |
160 fAmbientColor += lights->light(i).color(); | 157 fAmbientColor += lights->light(i).color(); |
161 } else { | 158 } else { |
162 // TODO: handle more than one of these | 159 // TODO: handle more than one of these |
163 fLightColor = lights->light(i).color(); | 160 fLightColor = lights->light(i).color(); |
164 fLightDir = lights->light(i).dir(); | 161 fLightDir = lights->light(i).dir(); |
165 } | 162 } |
166 } | 163 } |
167 | 164 |
165 this->registerChildProcessor(normalFP); | |
egdaniel
2016/06/10 14:23:27
use std::move(noramlFP) here
dvonbeck
2016/06/10 15:22:29
Done.
| |
168 this->initClassID<LightingFP>(); | 166 this->initClassID<LightingFP>(); |
169 } | 167 } |
170 | 168 |
171 class LightingGLFP : public GrGLSLFragmentProcessor { | 169 class LightingGLFP : public GrGLSLFragmentProcessor { |
172 public: | 170 public: |
173 LightingGLFP() { | 171 LightingGLFP() { |
174 fLightDir.fX = 10000.0f; | 172 fLightDir.fX = 10000.0f; |
175 fLightColor.fX = 0.0f; | 173 fLightColor.fX = 0.0f; |
176 fAmbientColor.fX = 0.0f; | 174 fAmbientColor.fX = 0.0f; |
177 fInvNormRotation.set(0.0f, 0.0f); | |
178 } | 175 } |
179 | 176 |
180 void emitCode(EmitArgs& args) override { | 177 void emitCode(EmitArgs& args) override { |
181 | 178 |
182 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 179 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
183 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 180 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
184 | 181 |
185 // add uniforms | 182 // add uniforms |
186 const char* lightDirUniName = nullptr; | 183 const char* lightDirUniName = nullptr; |
187 fLightDirUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 184 fLightDirUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
188 kVec3f_GrSLType, kDefault_ GrSLPrecision, | 185 kVec3f_GrSLType, kDefault_ GrSLPrecision, |
189 "LightDir", &lightDirUniNa me); | 186 "LightDir", &lightDirUniNa me); |
190 | 187 |
191 const char* lightColorUniName = nullptr; | 188 const char* lightColorUniName = nullptr; |
192 fLightColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 189 fLightColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
193 kVec3f_GrSLType, kDefaul t_GrSLPrecision, | 190 kVec3f_GrSLType, kDefaul t_GrSLPrecision, |
194 "LightColor", &lightColo rUniName); | 191 "LightColor", &lightColo rUniName); |
195 | 192 |
196 const char* ambientColorUniName = nullptr; | 193 const char* ambientColorUniName = nullptr; |
197 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | 194 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , |
198 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | 195 kVec3f_GrSLType, kDefa ult_GrSLPrecision, |
199 "AmbientColor", &ambie ntColorUniName); | 196 "AmbientColor", &ambie ntColorUniName); |
200 | 197 |
201 const char* xformUniName = nullptr; | |
202 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | |
203 kVec2f_GrSLType, kDefault_GrS LPrecision, | |
204 "Xform", &xformUniName); | |
205 | |
206 fragBuilder->codeAppend("vec4 diffuseColor = "); | 198 fragBuilder->codeAppend("vec4 diffuseColor = "); |
207 fragBuilder->appendTextureLookupAndModulate(args.fInputColor, args.f TexSamplers[0], | 199 fragBuilder->appendTextureLookupAndModulate(args.fInputColor, args.f TexSamplers[0], |
208 args.fCoords[0].c_str(), | 200 args.fCoords[0].c_str(), |
209 args.fCoords[0].getType()); | 201 args.fCoords[0].getType()); |
210 fragBuilder->codeAppend(";"); | 202 fragBuilder->codeAppend(";"); |
211 | 203 |
212 fragBuilder->codeAppend("vec4 normalColor = "); | 204 SkString dstNormalName("dstNormal"); |
213 fragBuilder->appendTextureLookup(args.fTexSamplers[1], | 205 this->emitChild(0, nullptr, &dstNormalName, args); |
214 args.fCoords[1].c_str(), | |
215 args.fCoords[1].getType()); | |
216 fragBuilder->codeAppend(";"); | |
217 | 206 |
218 fragBuilder->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);" ); | 207 fragBuilder->codeAppendf("vec3 normal = %s.xyz;", dstNormalName.c_st r()); |
219 | |
220 fragBuilder->codeAppendf( | |
221 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0. 0, 0.0, 0.0, 1.0);", | |
222 xformUniName, xformUniName, xformUniName, xform UniName); | |
223 | |
224 // TODO: inverse map the light direction vectors in the vertex shade r rather than | |
225 // transforming all the normals here! | |
226 fragBuilder->codeAppend("normal = normalize(m*normal);"); | |
227 | |
228 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0, 1.0);", | 208 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0, 1.0);", |
229 lightDirUniName); | 209 lightDirUniName); |
230 // diffuse light | 210 // diffuse light |
231 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightColorUniName); | 211 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightColorUniName); |
232 // ambient light | 212 // ambient light |
233 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); | 213 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); |
234 fragBuilder->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", a rgs.fOutputColor); | 214 fragBuilder->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", a rgs.fOutputColor); |
235 } | 215 } |
236 | 216 |
237 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 217 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
(...skipping 17 matching lines...) Expand all Loading... | |
255 if (lightColor != fLightColor) { | 235 if (lightColor != fLightColor) { |
256 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); | 236 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); |
257 fLightColor = lightColor; | 237 fLightColor = lightColor; |
258 } | 238 } |
259 | 239 |
260 const SkColor3f& ambientColor = lightingFP.ambientColor(); | 240 const SkColor3f& ambientColor = lightingFP.ambientColor(); |
261 if (ambientColor != fAmbientColor) { | 241 if (ambientColor != fAmbientColor) { |
262 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 242 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
263 fAmbientColor = ambientColor; | 243 fAmbientColor = ambientColor; |
264 } | 244 } |
265 | |
266 const SkVector& invNormRotation = lightingFP.invNormRotation(); | |
267 if (invNormRotation != fInvNormRotation) { | |
268 pdman.set2fv(fXformUni, 1, &invNormRotation.fX); | |
269 fInvNormRotation = invNormRotation; | |
270 } | |
271 } | 245 } |
272 | 246 |
273 private: | 247 private: |
274 SkVector3 fLightDir; | 248 SkVector3 fLightDir; |
275 GrGLSLProgramDataManager::UniformHandle fLightDirUni; | 249 GrGLSLProgramDataManager::UniformHandle fLightDirUni; |
276 | 250 |
277 SkColor3f fLightColor; | 251 SkColor3f fLightColor; |
278 GrGLSLProgramDataManager::UniformHandle fLightColorUni; | 252 GrGLSLProgramDataManager::UniformHandle fLightColorUni; |
279 | 253 |
280 SkColor3f fAmbientColor; | 254 SkColor3f fAmbientColor; |
281 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | 255 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
282 | |
283 SkVector fInvNormRotation; | |
284 GrGLSLProgramDataManager::UniformHandle fXformUni; | |
285 }; | 256 }; |
286 | 257 |
287 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { | 258 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
288 LightingGLFP::GenKey(*this, caps, b); | 259 LightingGLFP::GenKey(*this, caps, b); |
289 } | 260 } |
290 | 261 |
291 const char* name() const override { return "LightingFP"; } | 262 const char* name() const override { return "LightingFP"; } |
292 | 263 |
293 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 264 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
294 inout->mulByUnknownFourComponents(); | 265 inout->mulByUnknownFourComponents(); |
295 } | 266 } |
296 | 267 |
297 const SkVector3& lightDir() const { return fLightDir; } | 268 const SkVector3& lightDir() const { return fLightDir; } |
298 const SkColor3f& lightColor() const { return fLightColor; } | 269 const SkColor3f& lightColor() const { return fLightColor; } |
299 const SkColor3f& ambientColor() const { return fAmbientColor; } | 270 const SkColor3f& ambientColor() const { return fAmbientColor; } |
300 const SkVector& invNormRotation() const { return fInvNormRotation; } | |
301 | 271 |
302 private: | 272 private: |
303 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new LightingGLFP; } | 273 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new LightingGLFP; } |
304 | 274 |
305 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 275 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
276 // TODO add comparison of NormalFPs | |
egdaniel
2016/06/10 14:23:27
The base class of isEquals takes care of calling c
dvonbeck
2016/06/10 15:22:29
Done.
| |
306 const LightingFP& lightingFP = proc.cast<LightingFP>(); | 277 const LightingFP& lightingFP = proc.cast<LightingFP>(); |
307 return fDiffDeviceTransform == lightingFP.fDiffDeviceTransform && | 278 return fDiffDeviceTransform == lightingFP.fDiffDeviceTransform && |
308 fNormDeviceTransform == lightingFP.fNormDeviceTransform && | |
309 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && | 279 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && |
310 fNormalTextureAccess == lightingFP.fNormalTextureAccess && | |
311 fLightDir == lightingFP.fLightDir && | 280 fLightDir == lightingFP.fLightDir && |
312 fLightColor == lightingFP.fLightColor && | 281 fLightColor == lightingFP.fLightColor && |
313 fAmbientColor == lightingFP.fAmbientColor && | 282 fAmbientColor == lightingFP.fAmbientColor; |
314 fInvNormRotation == lightingFP.fInvNormRotation; | |
315 } | 283 } |
316 | 284 |
317 GrCoordTransform fDiffDeviceTransform; | 285 GrCoordTransform fDiffDeviceTransform; |
318 GrCoordTransform fNormDeviceTransform; | |
319 GrTextureAccess fDiffuseTextureAccess; | 286 GrTextureAccess fDiffuseTextureAccess; |
320 GrTextureAccess fNormalTextureAccess; | |
321 SkVector3 fLightDir; | 287 SkVector3 fLightDir; |
322 SkColor3f fLightColor; | 288 SkColor3f fLightColor; |
323 SkColor3f fAmbientColor; | 289 SkColor3f fAmbientColor; |
324 | |
325 SkVector fInvNormRotation; | |
326 }; | 290 }; |
327 | 291 |
328 //////////////////////////////////////////////////////////////////////////// | 292 //////////////////////////////////////////////////////////////////////////// |
329 | 293 |
330 static bool make_mat(const SkBitmap& bm, | 294 static bool make_mat(const SkBitmap& bm, |
331 const SkMatrix& localMatrix1, | 295 const SkMatrix& localMatrix1, |
332 const SkMatrix* localMatrix2, | 296 const SkMatrix* localMatrix2, |
333 SkMatrix* result) { | 297 SkMatrix* result) { |
334 | 298 |
335 result->setIDiv(bm.width(), bm.height()); | 299 result->setIDiv(bm.width(), bm.height()); |
(...skipping 17 matching lines...) Expand all Loading... | |
353 sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor( | 317 sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor( |
354 GrContext* context, | 318 GrContext* context, |
355 const SkMatrix& viewM, | 319 const SkMatrix& viewM, |
356 const SkMatrix* localMatrix , | 320 const SkMatrix* localMatrix , |
357 SkFilterQuality filterQuali ty, | 321 SkFilterQuality filterQuali ty, |
358 SkSourceGammaTreatment gamm aTreatment) const { | 322 SkSourceGammaTreatment gamm aTreatment) const { |
359 // we assume diffuse and normal maps have same width and height | 323 // we assume diffuse and normal maps have same width and height |
360 // TODO: support different sizes | 324 // TODO: support different sizes |
361 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | 325 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && |
362 fDiffuseMap.height() == fNormalMap.height()); | 326 fDiffuseMap.height() == fNormalMap.height()); |
363 SkMatrix diffM, normM; | 327 SkMatrix diffM; |
364 | 328 |
365 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { | 329 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { |
366 return nullptr; | 330 return nullptr; |
367 } | 331 } |
368 | 332 |
369 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) { | |
370 return nullptr; | |
371 } | |
372 | |
373 bool doBicubic; | 333 bool doBicubic; |
374 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode ( | 334 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode ( |
375 SkTMin(filterQuality, kMedium_SkFilterQu ality), | 335 SkTMin(filterQuality, kMedium_SkFilterQu ality), |
376 viewM, | 336 viewM, |
377 this->getLocalMatrix(), | 337 this->getLocalMatrix(), |
378 &doBicubic); | 338 &doBicubic); |
379 SkASSERT(!doBicubic); | 339 SkASSERT(!doBicubic); |
380 | 340 |
381 GrTextureParams::FilterMode normFilterMode = GrSkFilterQualityToGrFilterMode ( | |
382 SkTMin(filterQuality, kMedium_SkFilterQu ality), | |
383 viewM, | |
384 fNormLocalMatrix, | |
385 &doBicubic); | |
386 SkASSERT(!doBicubic); | |
387 | |
388 // TODO: support other tile modes | 341 // TODO: support other tile modes |
389 GrTextureParams diffParams(kClamp_TileMode, diffFilterMode); | 342 GrTextureParams diffParams(kClamp_TileMode, diffFilterMode); |
390 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, | 343 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, |
391 fDiffuseMap, diffParams, | 344 fDiffuseMap, diffParams, |
392 gammaTreatme nt)); | 345 gammaTreatme nt)); |
393 if (!diffuseTexture) { | 346 if (!diffuseTexture) { |
394 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit map to texture."); | 347 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit map to texture."); |
395 return nullptr; | 348 return nullptr; |
396 } | 349 } |
397 | 350 |
398 GrTextureParams normParams(kClamp_TileMode, normFilterMode); | 351 /* TODO is this correct memory handling? FPs were changed to use sk_sp so it is different from |
egdaniel
2016/06/10 14:23:28
This should still be fine still
dvonbeck
2016/06/10 15:22:29
Done.
| |
399 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, | 352 * last patch |
400 fNormalMap, n ormParams, | 353 */ |
401 gammaTreatmen t)); | 354 sk_sp<GrFragmentProcessor> normalFP( |
402 if (!normalTexture) { | 355 fNormalSource->asFragmentProcessor(context, viewM, localMatrix, filt erQuality, |
403 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit map to texture."); | 356 gammaTreatment)); |
404 return nullptr; | 357 sk_sp<GrFragmentProcessor> inner ( |
405 } | 358 new LightingFP(diffuseTexture, diffM, diffParams, fLights, normalFP) ); |
egdaniel
2016/06/10 14:23:27
I believe you want to use std::move here around no
dvonbeck
2016/06/10 15:22:29
Done.
| |
406 | 359 |
407 sk_sp<GrFragmentProcessor> inner ( | |
408 new LightingFP(diffuseTexture, normalTexture, diffM, normM, diffParams, normParams, fLights, | |
409 fInvNormRotation)); | |
410 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); | 360 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); |
411 } | 361 } |
412 | 362 |
413 #endif | 363 #endif |
414 | 364 |
415 //////////////////////////////////////////////////////////////////////////// | 365 //////////////////////////////////////////////////////////////////////////// |
416 | 366 |
417 bool SkLightingShaderImpl::isOpaque() const { | 367 bool SkLightingShaderImpl::isOpaque() const { |
418 return fDiffuseMap.isOpaque(); | 368 return fDiffuseMap.isOpaque(); |
419 } | 369 } |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
608 } | 558 } |
609 } | 559 } |
610 | 560 |
611 sk_sp<SkLights> lights(builder.finish()); | 561 sk_sp<SkLights> lights(builder.finish()); |
612 | 562 |
613 SkVector invNormRotation = {1,0}; | 563 SkVector invNormRotation = {1,0}; |
614 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) { | 564 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) { |
615 invNormRotation = buf.readPoint(); | 565 invNormRotation = buf.readPoint(); |
616 } | 566 } |
617 | 567 |
568 sk_sp<SkLightingShader::NormalSource> normalSource( | |
569 static_cast<SkLightingShader::NormalSource*>(buf.readFlattenable(kNo rmalSource_Type))); | |
570 | |
618 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), invNormRotation, | 571 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), invNormRotation, |
619 &diffLocalM, &normLocalM); | 572 &diffLocalM, &normLocalM, normalSour ce); |
egdaniel
2016/06/10 14:23:27
wrap normalSouce in std::move
dvonbeck
2016/06/10 15:22:29
Done.
| |
620 } | 573 } |
621 | 574 |
622 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { | 575 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { |
623 this->INHERITED::flatten(buf); | 576 this->INHERITED::flatten(buf); |
624 | 577 |
625 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); | 578 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); |
626 buf.writeBool(hasNormLocalM); | 579 buf.writeBool(hasNormLocalM); |
627 if (hasNormLocalM) { | 580 if (hasNormLocalM) { |
628 buf.writeMatrix(fNormLocalMatrix); | 581 buf.writeMatrix(fNormLocalMatrix); |
629 } | 582 } |
630 | 583 |
631 buf.writeBitmap(fDiffuseMap); | 584 buf.writeBitmap(fDiffuseMap); |
632 buf.writeBitmap(fNormalMap); | 585 buf.writeBitmap(fNormalMap); |
633 | 586 |
634 buf.writeInt(fLights->numLights()); | 587 buf.writeInt(fLights->numLights()); |
635 for (int l = 0; l < fLights->numLights(); ++l) { | 588 for (int l = 0; l < fLights->numLights(); ++l) { |
636 const SkLights::Light& light = fLights->light(l); | 589 const SkLights::Light& light = fLights->light(l); |
637 | 590 |
638 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | 591 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
639 | 592 |
640 buf.writeBool(isAmbient); | 593 buf.writeBool(isAmbient); |
641 buf.writeScalarArray(&light.color().fX, 3); | 594 buf.writeScalarArray(&light.color().fX, 3); |
642 if (!isAmbient) { | 595 if (!isAmbient) { |
643 buf.writeScalarArray(&light.dir().fX, 3); | 596 buf.writeScalarArray(&light.dir().fX, 3); |
644 } | 597 } |
645 } | 598 } |
646 buf.writePoint(fInvNormRotation); | 599 buf.writePoint(fInvNormRotation); |
600 | |
601 buf.writeFlattenable(fNormalSource.get()); | |
647 } | 602 } |
648 | 603 |
649 bool SkLightingShaderImpl::computeNormTotalInverse(const ContextRec& rec, | 604 bool SkLightingShaderImpl::computeNormTotalInverse(const ContextRec& rec, |
650 SkMatrix* normTotalInverse) c onst { | 605 SkMatrix* normTotalInverse) c onst { |
651 SkMatrix total; | 606 SkMatrix total; |
652 total.setConcat(*rec.fMatrix, fNormLocalMatrix); | 607 total.setConcat(*rec.fMatrix, fNormLocalMatrix); |
653 | 608 |
654 const SkMatrix* m = &total; | 609 const SkMatrix* m = &total; |
655 if (rec.fLocalMatrix) { | 610 if (rec.fLocalMatrix) { |
656 total.setConcat(*m, *rec.fLocalMatrix); | 611 total.setConcat(*m, *rec.fLocalMatrix); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
714 const SkVector& invNormRotation, | 669 const SkVector& invNormRotation, |
715 const SkMatrix* diffLocalM, const SkMatri x* normLocalM) { | 670 const SkMatrix* diffLocalM, const SkMatri x* normLocalM) { |
716 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || | 671 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || |
717 normal.isNull() || bitmap_is_too_big(normal) || | 672 normal.isNull() || bitmap_is_too_big(normal) || |
718 diffuse.width() != normal.width() || | 673 diffuse.width() != normal.width() || |
719 diffuse.height() != normal.height()) { | 674 diffuse.height() != normal.height()) { |
720 return nullptr; | 675 return nullptr; |
721 } | 676 } |
722 | 677 |
723 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); | 678 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); |
724 | 679 sk_sp<SkLightingShader::NormalSource> normalSource = |
680 SkLightingShader::NormalMapSource::Make(normal, invNormRotation, nor mLocalM); | |
725 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), | 681 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), |
726 invNormRotation, diffLocalM, normLoc alM); | 682 invNormRotation, diffLocalM, normLoc alM, normalSource); |
727 } | 683 } |
728 | 684 |
729 /////////////////////////////////////////////////////////////////////////////// | 685 /////////////////////////////////////////////////////////////////////////////// |
730 | 686 |
731 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 687 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
732 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 688 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
733 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 689 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
734 | 690 |
735 /////////////////////////////////////////////////////////////////////////////// | 691 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |