| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkNormalMapSource.h" | 8 #include "SkNormalMapSource.h" |
| 9 | 9 |
| 10 #include "SkLightingShader.h" | 10 #include "SkLightingShader.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 fragBuilder->codeAppend( " float scalingFactorSquared = " | 66 fragBuilder->codeAppend( " float scalingFactorSquared = " |
| 67 "( (transformed.x * transformed
.x) " | 67 "( (transformed.x * transformed
.x) " |
| 68 "+ (transformed.y * transform
ed.y) )" | 68 "+ (transformed.y * transform
ed.y) )" |
| 69 "/(1.0 - (normal.z * normal.z))
;"); | 69 "/(1.0 - (normal.z * normal.z))
;"); |
| 70 fragBuilder->codeAppendf(" %s = vec4(transformed*inversesqrt(scal
ingFactorSquared)," | 70 fragBuilder->codeAppendf(" %s = vec4(transformed*inversesqrt(scal
ingFactorSquared)," |
| 71 "normal.z, 0.0);", | 71 "normal.z, 0.0);", |
| 72 args.fOutputColor); | 72 args.fOutputColor); |
| 73 fragBuilder->codeAppend( "}"); | 73 fragBuilder->codeAppend( "}"); |
| 74 } | 74 } |
| 75 | 75 |
| 76 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 76 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKey
Builder* b) { |
| 77 GrProcessorKeyBuilder* b) { | |
| 78 b->add32(0x0); | 77 b->add32(0x0); |
| 79 } | 78 } |
| 80 | 79 |
| 81 protected: | 80 protected: |
| 82 void setNormalData(const GrGLSLProgramDataManager& pdman, | 81 void setNormalData(const GrGLSLProgramDataManager& pdman, |
| 83 const GrProcessor& proc) override { | 82 const GrProcessor& proc) override { |
| 84 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); | 83 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); |
| 85 | 84 |
| 86 const SkMatrix& invCTM = normalMapFP.invCTM(); | 85 const SkMatrix& invCTM = normalMapFP.invCTM(); |
| 87 fColumnMajorInvCTM22[0] = invCTM.get(SkMatrix::kMScaleX); | 86 fColumnMajorInvCTM22[0] = invCTM.get(SkMatrix::kMScaleX); |
| 88 fColumnMajorInvCTM22[1] = invCTM.get(SkMatrix::kMSkewY); | 87 fColumnMajorInvCTM22[1] = invCTM.get(SkMatrix::kMSkewY); |
| 89 fColumnMajorInvCTM22[2] = invCTM.get(SkMatrix::kMSkewX); | 88 fColumnMajorInvCTM22[2] = invCTM.get(SkMatrix::kMSkewX); |
| 90 fColumnMajorInvCTM22[3] = invCTM.get(SkMatrix::kMScaleY); | 89 fColumnMajorInvCTM22[3] = invCTM.get(SkMatrix::kMScaleY); |
| 91 pdman.setMatrix2f(fXformUni, fColumnMajorInvCTM22); | 90 pdman.setMatrix2f(fXformUni, fColumnMajorInvCTM22); |
| 92 } | 91 } |
| 93 | 92 |
| 94 private: | 93 private: |
| 94 // Upper-right 2x2 corner of the inverse of the CTM in column-major form |
| 95 float fColumnMajorInvCTM22[4]; | 95 float fColumnMajorInvCTM22[4]; |
| 96 GrGLSLProgramDataManager::UniformHandle fXformUni; | 96 GrGLSLProgramDataManager::UniformHandle fXformUni; |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { | 99 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { |
| 100 GLSLNormalMapFP::GenKey(*this, caps, b); | 100 GLSLNormalMapFP::GenKey(*this, caps, b); |
| 101 } | 101 } |
| 102 | 102 |
| 103 const char* name() const override { return "NormalMapFP"; } | 103 const char* name() const override { return "NormalMapFP"; } |
| 104 | 104 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 127 } | 127 } |
| 128 | 128 |
| 129 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); | 129 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); |
| 130 } | 130 } |
| 131 | 131 |
| 132 #endif // SK_SUPPORT_GPU | 132 #endif // SK_SUPPORT_GPU |
| 133 | 133 |
| 134 //////////////////////////////////////////////////////////////////////////// | 134 //////////////////////////////////////////////////////////////////////////// |
| 135 | 135 |
| 136 SkNormalMapSourceImpl::Provider::Provider(const SkNormalMapSourceImpl& source, | 136 SkNormalMapSourceImpl::Provider::Provider(const SkNormalMapSourceImpl& source, |
| 137 SkShader::Context* mapContext, | 137 SkShader::Context* mapContext, |
| 138 SkPaint* overridePaint) | 138 SkPaint* overridePaint) |
| 139 : fSource(source) | 139 : fSource(source) |
| 140 , fMapContext(mapContext) | 140 , fMapContext(mapContext) |
| 141 , fOverridePaint(overridePaint) {} | 141 , fOverridePaint(overridePaint) {} |
| 142 | 142 |
| 143 SkNormalMapSourceImpl::Provider::~Provider() { | 143 SkNormalMapSourceImpl::Provider::~Provider() { |
| 144 fMapContext->~Context(); | 144 fMapContext->~Context(); |
| 145 fOverridePaint->~SkPaint(); | 145 fOverridePaint->~SkPaint(); |
| 146 } | 146 } |
| 147 | 147 |
| 148 SkNormalSource::Provider* SkNormalMapSourceImpl::asProvider( | 148 SkNormalSource::Provider* SkNormalMapSourceImpl::asProvider(const SkShader::Cont
extRec &rec, |
| 149 const SkShader::ContextRec &rec, void *storage) const { | 149 void *storage) const
{ |
| 150 SkMatrix normTotalInv; | 150 SkMatrix normTotalInv; |
| 151 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { | 151 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { |
| 152 return nullptr; | 152 return nullptr; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Overriding paint's alpha because we need the normal map's RGB channels to
be unpremul'd | 155 // Overriding paint's alpha because we need the normal map's RGB channels to
be unpremul'd |
| 156 void* paintStorage = (char*)storage + sizeof(Provider); | 156 void* paintStorage = (char*)storage + sizeof(Provider); |
| 157 SkPaint* overridePaint = new (paintStorage) SkPaint(*(rec.fPaint)); | 157 SkPaint* overridePaint = new (paintStorage) SkPaint(*(rec.fPaint)); |
| 158 overridePaint->setAlpha(0xFF); | 158 overridePaint->setAlpha(0xFF); |
| 159 SkShader::ContextRec overrideRec(*overridePaint, *(rec.fMatrix), rec.fLocalM
atrix, | 159 SkShader::ContextRec overrideRec(*overridePaint, *(rec.fMatrix), rec.fLocalM
atrix, |
| 160 rec.fPreferredDstType); | 160 rec.fPreferredDstType); |
| 161 | 161 |
| 162 void* mapContextStorage = (char*) paintStorage + sizeof(SkPaint); | 162 void* mapContextStorage = (char*) paintStorage + sizeof(SkPaint); |
| 163 SkShader::Context* context = fMapShader->createContext(overrideRec, mapConte
xtStorage); | 163 SkShader::Context* context = fMapShader->createContext(overrideRec, mapConte
xtStorage); |
| 164 if (!context) { | 164 if (!context) { |
| 165 return nullptr; | 165 return nullptr; |
| 166 } | 166 } |
| 167 | 167 |
| 168 return new (storage) Provider(*this, context, overridePaint); | 168 return new (storage) Provider(*this, context, overridePaint); |
| 169 } | 169 } |
| 170 | 170 |
| 171 size_t SkNormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) cons
t { | 171 size_t SkNormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) cons
t { |
| 172 return sizeof(Provider) + sizeof(SkPaint) + fMapShader->contextSize(rec); | 172 return sizeof(Provider) + sizeof(SkPaint) + fMapShader->contextSize(rec); |
| 173 } | 173 } |
| 174 | 174 |
| 175 bool SkNormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec&
rec, | 175 bool SkNormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec&
rec, |
| 176 SkMatrix* normTotalInverse) co
nst { | 176 SkMatrix* normTotalInverse)
const { |
| 177 SkMatrix total; | 177 SkMatrix total; |
| 178 total.setConcat(*rec.fMatrix, fMapShader->getLocalMatrix()); | 178 total.setConcat(*rec.fMatrix, fMapShader->getLocalMatrix()); |
| 179 | 179 |
| 180 const SkMatrix* m = &total; | 180 const SkMatrix* m = &total; |
| 181 if (rec.fLocalMatrix) { | 181 if (rec.fLocalMatrix) { |
| 182 total.setConcat(*m, *rec.fLocalMatrix); | 182 total.setConcat(*m, *rec.fLocalMatrix); |
| 183 m = &total; | 183 m = &total; |
| 184 } | 184 } |
| 185 return m->invert(normTotalInverse); | 185 return m->invert(normTotalInverse); |
| 186 } | 186 } |
| 187 | 187 |
| 188 #define BUFFER_MAX 16 | 188 #define BUFFER_MAX 16 |
| 189 void SkNormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output
[], | 189 void SkNormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output
[], |
| 190 int count) const { | 190 int count) const { |
| 191 SkPMColor tmpNormalColors[BUFFER_MAX]; | 191 SkPMColor tmpNormalColors[BUFFER_MAX]; |
| 192 | 192 |
| 193 do { | 193 do { |
| 194 int n = SkTMin(count, BUFFER_MAX); | 194 int n = SkTMin(count, BUFFER_MAX); |
| 195 | 195 |
| 196 fMapContext->shadeSpan(x, y, tmpNormalColors, n); | 196 fMapContext->shadeSpan(x, y, tmpNormalColors, n); |
| 197 | 197 |
| 198 for (int i = 0; i < n; i++) { | 198 for (int i = 0; i < n; i++) { |
| 199 SkPoint3 tempNorm; | 199 SkPoint3 tempNorm; |
| 200 | 200 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 258 |
| 259 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con
st SkMatrix& ctm) { | 259 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con
st SkMatrix& ctm) { |
| 260 SkMatrix invCTM; | 260 SkMatrix invCTM; |
| 261 | 261 |
| 262 if (!ctm.invert(&invCTM) || !map) { | 262 if (!ctm.invert(&invCTM) || !map) { |
| 263 return nullptr; | 263 return nullptr; |
| 264 } | 264 } |
| 265 | 265 |
| 266 return sk_make_sp<SkNormalMapSourceImpl>(std::move(map), invCTM); | 266 return sk_make_sp<SkNormalMapSourceImpl>(std::move(map), invCTM); |
| 267 } | 267 } |
| OLD | NEW |