Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: src/core/SkNormalSource.cpp

Issue 2050773002: Refactoring of CPU NormalMap handling out into its own class (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-normals-gpu-cl
Patch Set: Updated Flattenable_type enum name Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "SkBitmapProcShader.h" 8 #include "SkBitmapProcShader.h"
9 #include "SkBitmapProcState.h"
9 #include "SkError.h" 10 #include "SkError.h"
10 #include "SkErrorInternals.h" 11 #include "SkErrorInternals.h"
11 #include "SkLightingShader.h" 12 #include "SkLightingShader.h"
13 #include "SkNormalSource.h"
12 #include "SkReadBuffer.h" 14 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h" 15 #include "SkWriteBuffer.h"
14 16
15 // Genretating vtable 17 // Genretating vtable
16 SkLightingShader::NormalSource::~NormalSource() {} 18 SkNormalSource::~SkNormalSource() {}
17 19
18 /////////////////////////////////////////////////////////////////////////////// 20 ///////////////////////////////////////////////////////////////////////////////
19 21
20 class NormalMapSourceImpl : public SkLightingShader::NormalSource { 22 class NormalMapSourceImpl : public SkNormalSource {
21 public: 23 public:
22 NormalMapSourceImpl(const SkBitmap &normal, const SkVector &invNormRotation, 24 NormalMapSourceImpl(sk_sp<SkShader> bitmapShader, const SkVector &invNormRot ation)
23 const SkMatrix *normLocalM) 25 : fBitmapShader(std::move(bitmapShader))
24 : fNormalMap(normal) 26 , fInvNormRotation(invNormRotation) {}
25 , fInvNormRotation(invNormRotation) {
26
27 if (normLocalM) {
28 fNormLocalMatrix = *normLocalM;
29 } else {
30 fNormLocalMatrix.reset();
31 }
32 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf e.
33 (void)fNormLocalMatrix.getType();
34 }
35 27
36 #if SK_SUPPORT_GPU 28 #if SK_SUPPORT_GPU
37 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, 29 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*,
38 const SkMatrix& viewM, 30 const SkMatrix& viewM,
39 const SkMatrix* localMatrix, 31 const SkMatrix* localMatrix,
40 SkFilterQuality, 32 SkFilterQuality,
41 SkSourceGammaTreatment) const override; 33 SkSourceGammaTreatment) const override;
42 #endif 34 #endif
43 35
36 SkNormalSource::Provider* asProvider(const SkShader::ContextRec& rec,
37 void* storage) const ov erride;
38
39 size_t providerSize(const SkShader::ContextRec& rec) const override;
44 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl) 40 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl)
45 41
46 protected: 42 protected:
47 void flatten(SkWriteBuffer& buf) const override; 43 void flatten(SkWriteBuffer& buf) const override;
48 44
45 bool computeNormTotalInverse(const SkShader::ContextRec& rec, SkMatrix* norm TotalInverse) const;
46
49 private: 47 private:
50 SkBitmap fNormalMap; 48 class Provider : public SkNormalSource::Provider {
51 SkMatrix fNormLocalMatrix; 49 public:
50 Provider(const NormalMapSourceImpl& source, SkShader::Context* fBitmapCo ntext);
51
52 virtual ~Provider();
53
54 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over ride;
55 private:
56 const NormalMapSourceImpl& fSource;
57 SkShader::Context* fBitmapContext;
58
59 typedef SkNormalSource::Provider INHERITED;
60 };
61
62 sk_sp<SkShader> fBitmapShader;
52 SkVector fInvNormRotation; 63 SkVector fInvNormRotation;
53 64
54 friend class SkLightingShader::NormalSource; 65 friend class SkNormalSource;
55 66
56 typedef SkLightingShader::NormalSource INHERITED; 67 typedef SkNormalSource INHERITED;
57 }; 68 };
58 69
59 //////////////////////////////////////////////////////////////////////////// 70 ////////////////////////////////////////////////////////////////////////////
60 71
61 #if SK_SUPPORT_GPU 72 #if SK_SUPPORT_GPU
62 73
63 #include "GrCoordTransform.h" 74 #include "GrCoordTransform.h"
64 #include "GrInvariantOutput.h" 75 #include "GrInvariantOutput.h"
65 #include "GrTextureParams.h" 76 #include "GrTextureParams.h"
66 #include "glsl/GrGLSLFragmentProcessor.h" 77 #include "glsl/GrGLSLFragmentProcessor.h"
67 #include "glsl/GrGLSLFragmentShaderBuilder.h" 78 #include "glsl/GrGLSLFragmentShaderBuilder.h"
68 #include "SkGr.h" 79 #include "SkGr.h"
69 80
70 class NormalMapFP : public GrFragmentProcessor { 81 class NormalMapFP : public GrFragmentProcessor {
71 public: 82 public:
72 NormalMapFP(GrTexture* normal, const SkMatrix& normMatrix, const GrTexturePa rams& normParams, 83 NormalMapFP(sk_sp<GrFragmentProcessor> bitmapFP, const SkVector& invNormRota tion)
73 const SkVector& invNormRotation) 84 : fInvNormRotation(invNormRotation) {
74 : fNormDeviceTransform(kLocal_GrCoordSet, normMatrix, normal, normParams .filterMode()) 85 this->registerChildProcessor(bitmapFP);
75 , fNormalTextureAccess(normal, normParams)
76 , fInvNormRotation(invNormRotation) {
77 this->addCoordTransform(&fNormDeviceTransform);
78 this->addTextureAccess(&fNormalTextureAccess);
79 86
80 this->initClassID<NormalMapFP>(); 87 this->initClassID<NormalMapFP>();
81 } 88 }
82 89
83 class GLSLNormalMapFP : public GrGLSLFragmentProcessor { 90 class GLSLNormalMapFP : public GrGLSLFragmentProcessor {
84 public: 91 public:
85 GLSLNormalMapFP() { 92 GLSLNormalMapFP() {
86 fInvNormRotation.set(0.0f, 0.0f); 93 fInvNormRotation.set(0.0f, 0.0f);
87 } 94 }
88 95
89 void emitCode(EmitArgs& args) override { 96 void emitCode(EmitArgs& args) override {
90 97
91 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; 98 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
92 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 99 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
93 100
94 // add uniform 101 // add uniform
95 const char* xformUniName = nullptr; 102 const char* xformUniName = nullptr;
96 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, 103 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
97 kVec2f_GrSLType, kDefault_GrS LPrecision, 104 kVec2f_GrSLType, kDefault_GrS LPrecision,
98 "Xform", &xformUniName); 105 "Xform", &xformUniName);
99 106
100 fragBuilder->codeAppend("vec4 normalColor = "); 107 SkString dstNormalColorName("dstNormalColor");
101 fragBuilder->appendTextureLookup(args.fTexSamplers[0], 108 this->emitChild(0, nullptr, &dstNormalColorName, args);
102 args.fCoords[0].c_str(), 109 fragBuilder->codeAppendf("vec3 normal = %s.rgb - vec3(0.5);",
103 args.fCoords[0].getType()); 110 dstNormalColorName.c_str());
104 fragBuilder->codeAppend(";");
105
106 fragBuilder->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);" );
107 111
108 // TODO: inverse map the light direction vectors in the vertex shade r rather than 112 // TODO: inverse map the light direction vectors in the vertex shade r rather than
109 // transforming all the normals here! 113 // transforming all the normals here!
110 fragBuilder->codeAppendf( 114 fragBuilder->codeAppendf(
111 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.0, 0.0, 0.0, 1.0);", 115 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.0, 0.0, 0.0, 1.0);",
112 xformUniName, xformUniName, xformUniName, xformUniName); 116 xformUniName, xformUniName, xformUniName, xformUniName);
113 117
114 fragBuilder->codeAppend("normal = normalize(m*normal);"); 118 fragBuilder->codeAppend("normal = normalize(m*normal);");
115 fragBuilder->codeAppendf("%s = vec4(normal, 0);", args.fOutputColor) ; 119 fragBuilder->codeAppendf("%s = vec4(normal, 0);", args.fOutputColor) ;
116 } 120 }
(...skipping 29 matching lines...) Expand all
146 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); 150 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput);
147 } 151 }
148 152
149 const SkVector& invNormRotation() const { return fInvNormRotation; } 153 const SkVector& invNormRotation() const { return fInvNormRotation; }
150 154
151 private: 155 private:
152 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLNormalMapFP; } 156 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLNormalMapFP; }
153 157
154 bool onIsEqual(const GrFragmentProcessor& proc) const override { 158 bool onIsEqual(const GrFragmentProcessor& proc) const override {
155 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); 159 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>();
156 return fNormDeviceTransform == normalMapFP.fNormDeviceTransform && 160 return fInvNormRotation == normalMapFP.fInvNormRotation;
157 fNormalTextureAccess == normalMapFP.fNormalTextureAccess &&
158 fInvNormRotation == normalMapFP.fInvNormRotation;
159 } 161 }
160 162
161 GrCoordTransform fNormDeviceTransform; 163 SkVector fInvNormRotation;
162 GrTextureAccess fNormalTextureAccess;
163 SkVector fInvNormRotation;
164 }; 164 };
165 165
166 // TODO same code at SkLightingShader.cpp. Refactor to common source!
167 static bool make_mat(const SkBitmap& bm,
168 const SkMatrix& localMatrix1,
169 const SkMatrix* localMatrix2,
170 SkMatrix* result) {
171
172 result->setIDiv(bm.width(), bm.height());
173
174 SkMatrix lmInverse;
175 if (!localMatrix1.invert(&lmInverse)) {
176 return false;
177 }
178 if (localMatrix2) {
179 SkMatrix inv;
180 if (!localMatrix2->invert(&inv)) {
181 return false;
182 }
183 lmInverse.postConcat(inv);
184 }
185 result->preConcat(lmInverse);
186
187 return true;
188 }
189
190 sk_sp<GrFragmentProcessor> NormalMapSourceImpl::asFragmentProcessor( 166 sk_sp<GrFragmentProcessor> NormalMapSourceImpl::asFragmentProcessor(
191 GrContext *context, 167 GrContext *context,
192 const SkMatrix &viewM, 168 const SkMatrix &viewM,
193 const SkMatrix *localMatrix , 169 const SkMatrix *localMatrix ,
194 SkFilterQuality filterQuali ty, 170 SkFilterQuality filterQuali ty,
195 SkSourceGammaTreatment gamm aTreatment) const { 171 SkSourceGammaTreatment gamm aTreatment) const {
196 172
197 // TODO Here, the old code was checking that diffuse map and normal map are same size, that 173 sk_sp<GrFragmentProcessor> bitmapFP = fBitmapShader->asFragmentProcessor(con text, viewM,
198 // will be addressed when diffuse maps are factored out of SkLightingSh ader in a future CL 174 localMatrix, filterQuality, gammaTreatment);
199 175
200 SkMatrix normM; 176 return sk_make_sp<NormalMapFP>(std::move(bitmapFP), fInvNormRotation);
201 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) {
202 return nullptr;
203 }
204
205 bool doBicubic;
206 GrTextureParams::FilterMode normFilterMode = GrSkFilterQualityToGrFilterMode (
207 SkTMin(filterQuality, kMedium_SkFilterQuality),
208 viewM,
209 fNormLocalMatrix,
210 &doBicubic);
211 SkASSERT(!doBicubic);
212
213 // TODO: support other tile modes
214 GrTextureParams normParams(SkShader::kClamp_TileMode, normFilterMode);
215 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context,
216 fNormalMap,
217 normParams,
218 gammaTreatmen t));
219 if (!normalTexture) {
220 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit map to texture.");
221 return nullptr;
222 }
223
224 return sk_make_sp<NormalMapFP>(normalTexture, normM, normParams, fInvNormRot ation);
225 } 177 }
226 178
227 #endif // SK_SUPPORT_GPU 179 #endif // SK_SUPPORT_GPU
228 180
229 //////////////////////////////////////////////////////////////////////////// 181 ////////////////////////////////////////////////////////////////////////////
230 182
183 NormalMapSourceImpl::Provider::Provider(const NormalMapSourceImpl& source,
184 SkShader::Context* bitmapContext)
185 : fSource(source)
186 , fBitmapContext(bitmapContext) {
187 }
188
189 NormalMapSourceImpl::Provider::~Provider() {
190 fBitmapContext->~Context();
191 }
192
193 SkNormalSource::Provider* NormalMapSourceImpl::asProvider(
194 const SkShader::ContextRec &rec, void *storage) const {
195 SkMatrix normTotalInv;
196 if (!this->computeNormTotalInverse(rec, &normTotalInv)) {
197 return nullptr;
198 }
199
200 void* bitmapContextStorage = (char*)storage + sizeof(Provider);
201 SkShader::Context* context = fBitmapShader->createContext(rec, bitmapContext Storage);
202 if (!context) {
203 return nullptr;
204 }
205
206 return new (storage) Provider(*this, context);
207 }
208
209 size_t NormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) const {
210 return sizeof(Provider) + fBitmapShader->contextSize(rec);
211 }
212
213 bool NormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec& re c,
214 SkMatrix* normTotalInverse) co nst {
215 SkMatrix total;
216 total.setConcat(*rec.fMatrix, fBitmapShader->getLocalMatrix());
217
218 const SkMatrix* m = &total;
219 if (rec.fLocalMatrix) {
220 total.setConcat(*m, *rec.fLocalMatrix);
221 m = &total;
222 }
223 return m->invert(normTotalInverse);
224 }
225
226 #define BUFFER_MAX 16
227 void NormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[] ,
228 int count) const {
229 SkPMColor tmpNormalColors[BUFFER_MAX];
230
231 do {
232 int n = SkTMin(count, BUFFER_MAX);
233
234 fBitmapContext->shadeSpan(x, y, tmpNormalColors, n);
235
236 for (int i = 0; i < n; i++) {
237 SkPoint3 tempNorm;
238
239 tempNorm.set(SkIntToScalar(SkGetPackedR32(tmpNormalColors[i])) - 127 .0f,
240 SkIntToScalar(SkGetPackedG32(tmpNormalColors[i])) - 127 .0f,
241 SkIntToScalar(SkGetPackedB32(tmpNormalColors[i])) - 127 .0f);
242 tempNorm.normalize();
243
244 output[i].fX = fSource.fInvNormRotation.fX * tempNorm.fX +
245 fSource.fInvNormRotation.fY * tempNorm.fY;
246 output[i].fY = -fSource.fInvNormRotation.fY * tempNorm.fX +
247 fSource.fInvNormRotation.fX * tempNorm.fY;
248 output[i].fZ = tempNorm.fZ;
249 }
250
251 output += n;
252 x += n;
253 count -= n;
254 } while (count > 0);
255 }
256
257 ////////////////////////////////////////////////////////////////////////////////
258
231 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { 259 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) {
232 260
233 SkMatrix normLocalM; 261 sk_sp<SkShader> bitmapShader = buf.readFlattenable<SkShader>();
234 bool hasNormLocalM = buf.readBool();
235 if (hasNormLocalM) {
236 buf.readMatrix(&normLocalM);
237 } else {
238 normLocalM.reset();
239 }
240
241 SkBitmap normal;
242 if (!buf.readBitmap(&normal)) {
243 return nullptr;
244 }
245 normal.setImmutable();
246 262
247 SkVector invNormRotation = {1,0}; 263 SkVector invNormRotation = {1,0};
248 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) { 264 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) {
249 invNormRotation = buf.readPoint(); 265 invNormRotation = buf.readPoint();
250 } 266 }
251 267
252 return sk_make_sp<NormalMapSourceImpl>(normal, invNormRotation, &normLocalM) ; 268 return sk_make_sp<NormalMapSourceImpl>(std::move(bitmapShader), invNormRotat ion);
253 } 269 }
254 270
255 void NormalMapSourceImpl::flatten(SkWriteBuffer& buf) const { 271 void NormalMapSourceImpl::flatten(SkWriteBuffer& buf) const {
256 this->INHERITED::flatten(buf); 272 this->INHERITED::flatten(buf);
257 273
258 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); 274 buf.writeFlattenable(fBitmapShader.get());
259 buf.writeBool(hasNormLocalM);
260 if (hasNormLocalM) {
261 buf.writeMatrix(fNormLocalMatrix);
262 }
263
264 buf.writeBitmap(fNormalMap);
265 buf.writePoint(fInvNormRotation); 275 buf.writePoint(fInvNormRotation);
266 } 276 }
267 277
268 //////////////////////////////////////////////////////////////////////////// 278 ////////////////////////////////////////////////////////////////////////////
269 279
270 sk_sp<SkLightingShader::NormalSource> SkLightingShader::NormalSource::MakeMap( 280 sk_sp<SkNormalSource> SkNormalSource::MakeMap(
271 const SkBitmap &normal, const SkVector &invNormRotation, const SkMatrix *normLocalM) { 281 const SkBitmap &normal, const SkVector &invNormRotation, const SkMatrix *normLocalM) {
272 282
273 // TODO not checking normal and diffuse maps to be same size, will be addres sed when diffuse
274 // maps are factored out of SkLightingShader in a future CL
275 if (normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal)) { 283 if (normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal)) {
276 return nullptr; 284 return nullptr;
277 } 285 }
278 286
279 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); 287 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1));
280 288
281 return sk_make_sp<NormalMapSourceImpl>(normal, invNormRotation, normLocalM); 289 // TODO: support other tile modes
290 sk_sp<SkShader> bitmapShader = SkMakeBitmapShader(normal, SkShader::kClamp_T ileMode,
291 SkShader::kClamp_TileMode, normLocalM, nullptr);
292
293 return sk_make_sp<NormalMapSourceImpl>(std::move(bitmapShader), invNormRotat ion);
282 } 294 }
283 295
284 //////////////////////////////////////////////////////////////////////////// 296 ////////////////////////////////////////////////////////////////////////////
285 297
286 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader::NormalSource) 298 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource)
287 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) 299 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl)
288 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 300 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
289 301
290 //////////////////////////////////////////////////////////////////////////// 302 ////////////////////////////////////////////////////////////////////////////
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698