| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "GrGLEffectMatrix.h" | 8 #include "GrGLEffectMatrix.h" |
| 9 #include "GrDrawEffect.h" |
| 9 #include "GrTexture.h" | 10 #include "GrTexture.h" |
| 10 | 11 |
| 11 GrGLEffect::EffectKey GrGLEffectMatrix::GenKey(const SkMatrix& effectMatrix, | 12 GrGLEffect::EffectKey GrGLEffectMatrix::GenKey(const SkMatrix& effectMatrix, |
| 12 const SkMatrix& coordChangeMatrix
, | 13 const GrDrawEffect& drawEffect, |
| 14 CoordsType coordsType, |
| 13 const GrTexture* texture) { | 15 const GrTexture* texture) { |
| 16 EffectKey key = 0; |
| 14 SkMatrix::TypeMask type0 = effectMatrix.getType(); | 17 SkMatrix::TypeMask type0 = effectMatrix.getType(); |
| 15 SkMatrix::TypeMask type1 = coordChangeMatrix.getType(); | 18 SkMatrix::TypeMask type1; |
| 19 if (GrEffect::kLocal_CoordsType == coordsType) { |
| 20 type1 = drawEffect.getCoordChangeMatrix().getType(); |
| 21 } else { |
| 22 if (drawEffect.programHasExplicitLocalCoords()) { |
| 23 // We only make the key indicate that device coords are referenced w
hen the local coords |
| 24 // are not actually determined by positions. |
| 25 key |= kPositionCoords_Key; |
| 26 } |
| 27 type1 = SkMatrix::kIdentity_Mask; |
| 28 } |
| 16 | 29 |
| 17 static const int kNonTransMask = SkMatrix::kAffine_Mask | | 30 static const int kNonTransMask = SkMatrix::kAffine_Mask | |
| 18 SkMatrix::kScale_Mask | | 31 SkMatrix::kScale_Mask | |
| 19 SkMatrix::kPerspective_Mask; | 32 SkMatrix::kPerspective_Mask; |
| 20 int combinedTypes = type0 | type1; | 33 int combinedTypes = type0 | type1; |
| 21 | 34 |
| 22 bool reverseY = (NULL != texture) && kBottomLeft_GrSurfaceOrigin == texture-
>origin(); | 35 bool reverseY = (NULL != texture) && kBottomLeft_GrSurfaceOrigin == texture-
>origin(); |
| 23 | 36 |
| 24 if (SkMatrix::kPerspective_Mask & combinedTypes) { | 37 if (SkMatrix::kPerspective_Mask & combinedTypes) { |
| 25 return kGeneral_Key; | 38 key |= kGeneral_Key; |
| 26 } else if ((kNonTransMask & combinedTypes) || reverseY) { | 39 } else if ((kNonTransMask & combinedTypes) || reverseY) { |
| 27 return kNoPersp_Key; | 40 key |= kNoPersp_Key; |
| 28 } else if (kTrans_Key & combinedTypes) { | 41 } else if (kTrans_Key & combinedTypes) { |
| 29 return kTrans_Key; | 42 key |= kTrans_Key; |
| 30 } else { | 43 } else { |
| 31 GrAssert(effectMatrix.isIdentity() && coordChangeMatrix.isIdentity()); | 44 key |= kIdentity_Key; |
| 32 return kIdentity_Key; | |
| 33 } | 45 } |
| 46 return key; |
| 34 } | 47 } |
| 35 | 48 |
| 36 GrSLType GrGLEffectMatrix::emitCode(GrGLShaderBuilder* builder, | 49 GrSLType GrGLEffectMatrix::emitCode(GrGLShaderBuilder* builder, |
| 37 EffectKey key, | 50 EffectKey key, |
| 38 const char* vertexCoords, | |
| 39 const char** fsCoordName, | 51 const char** fsCoordName, |
| 40 const char** vsCoordName, | 52 const char** vsCoordName, |
| 41 const char* suffix) { | 53 const char* suffix) { |
| 42 GrSLType varyingType; | 54 GrSLType varyingType; |
| 43 const char* uniName; | 55 const char* uniName; |
| 44 key &= kKeyMask; | 56 key &= kKeyMask; |
| 45 switch (key) { | 57 switch (key & kMatrixTypeMask) { |
| 46 case kIdentity_Key: | 58 case kIdentity_Key: |
| 47 fUniType = kVoid_GrSLType; | 59 fUniType = kVoid_GrSLType; |
| 48 varyingType = kVec2f_GrSLType; | 60 varyingType = kVec2f_GrSLType; |
| 49 break; | 61 break; |
| 50 case kTrans_Key: | 62 case kTrans_Key: |
| 51 fUniType = kVec2f_GrSLType; | 63 fUniType = kVec2f_GrSLType; |
| 52 uniName = "StageTranslate"; | 64 uniName = "StageTranslate"; |
| 53 varyingType = kVec2f_GrSLType; | 65 varyingType = kVec2f_GrSLType; |
| 54 break; | 66 break; |
| 55 case kNoPersp_Key: | 67 case kNoPersp_Key: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 82 SkString suffixedVaryingName; | 94 SkString suffixedVaryingName; |
| 83 if (NULL != suffix) { | 95 if (NULL != suffix) { |
| 84 suffixedVaryingName.append(varyingName); | 96 suffixedVaryingName.append(varyingName); |
| 85 suffixedVaryingName.append(suffix); | 97 suffixedVaryingName.append(suffix); |
| 86 varyingName = suffixedVaryingName.c_str(); | 98 varyingName = suffixedVaryingName.c_str(); |
| 87 } | 99 } |
| 88 const char* vsVaryingName; | 100 const char* vsVaryingName; |
| 89 const char* fsVaryingName; | 101 const char* fsVaryingName; |
| 90 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName
); | 102 builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName
); |
| 91 | 103 |
| 92 // varying = matrix * vertex-coords (logically) | 104 const GrGLShaderVar* coords; |
| 105 switch (fCoordsType) { |
| 106 case GrEffect::kLocal_CoordsType: |
| 107 GrAssert(!(kPositionCoords_Key & key)); |
| 108 coords = &builder->localCoordsAttribute(); |
| 109 break; |
| 110 case GrEffect::kPosition_CoordsType: |
| 111 GrAssert((kPositionCoords_Key & key) || !builder->hasExplicitLocalCo
ords()); |
| 112 coords = &builder->positionAttribute(); |
| 113 break; |
| 114 default: |
| 115 GrCrash("Unexpected coords type."); |
| 116 } |
| 117 // varying = matrix * coords (logically) |
| 93 switch (fUniType) { | 118 switch (fUniType) { |
| 94 case kVoid_GrSLType: | 119 case kVoid_GrSLType: |
| 95 GrAssert(kVec2f_GrSLType == varyingType); | 120 GrAssert(kVec2f_GrSLType == varyingType); |
| 96 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, vertexCoords); | 121 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords->c_str(
)); |
| 97 break; | 122 break; |
| 98 case kVec2f_GrSLType: | 123 case kVec2f_GrSLType: |
| 99 GrAssert(kVec2f_GrSLType == varyingType); | 124 GrAssert(kVec2f_GrSLType == varyingType); |
| 100 builder->vsCodeAppendf("\t%s = %s + %s;\n", vsVaryingName, uniName,
vertexCoords); | 125 builder->vsCodeAppendf("\t%s = %s + %s;\n", |
| 126 vsVaryingName, uniName, coords->c_str()); |
| 101 break; | 127 break; |
| 102 case kMat33f_GrSLType: { | 128 case kMat33f_GrSLType: { |
| 103 GrAssert(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyin
gType); | 129 GrAssert(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyin
gType); |
| 104 if (kVec2f_GrSLType == varyingType) { | 130 if (kVec2f_GrSLType == varyingType) { |
| 105 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", | 131 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", |
| 106 vsVaryingName, uniName, vertexCoords); | 132 vsVaryingName, uniName, coords->c_str()); |
| 107 } else { | 133 } else { |
| 108 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", | 134 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", |
| 109 vsVaryingName, uniName, vertexCoords); | 135 vsVaryingName, uniName, coords->c_str()); |
| 110 } | 136 } |
| 111 break; | 137 break; |
| 112 } | 138 } |
| 113 default: | 139 default: |
| 114 GrCrash("Unexpected uniform type."); | 140 GrCrash("Unexpected uniform type."); |
| 115 } | 141 } |
| 116 if (NULL != vsCoordName) { | 142 if (NULL != vsCoordName) { |
| 117 *vsCoordName = vsVaryingName; | 143 *vsCoordName = vsVaryingName; |
| 118 } | 144 } |
| 119 if (NULL != fsCoordName) { | 145 if (NULL != fsCoordName) { |
| 120 *fsCoordName = fsVaryingName; | 146 *fsCoordName = fsVaryingName; |
| 121 } | 147 } |
| 122 return varyingType; | 148 return varyingType; |
| 123 } | 149 } |
| 124 | 150 |
| 125 /** | 151 /** |
| 126 * This is similar to emitCode except that it performs perspective division i
n the FS if the | 152 * This is similar to emitCode except that it performs perspective division i
n the FS if the |
| 127 * texture coordinates have a w coordinate. The fsCoordName always refers to
a vec2f. | 153 * texture coordinates have a w coordinate. The fsCoordName always refers to
a vec2f. |
| 128 */ | 154 */ |
| 129 void GrGLEffectMatrix::emitCodeMakeFSCoords2D(GrGLShaderBuilder* builder, | 155 void GrGLEffectMatrix::emitCodeMakeFSCoords2D(GrGLShaderBuilder* builder, |
| 130 EffectKey key, | 156 EffectKey key, |
| 131 const char* vertexCoords, | |
| 132 const char** fsCoordName, | 157 const char** fsCoordName, |
| 133 const char** vsVaryingName, | 158 const char** vsVaryingName, |
| 134 GrSLType* vsVaryingType, | 159 GrSLType* vsVaryingType, |
| 135 const char* suffix) { | 160 const char* suffix) { |
| 136 const char* fsVaryingName; | 161 const char* fsVaryingName; |
| 137 | 162 |
| 138 GrSLType varyingType = this->emitCode(builder, | 163 GrSLType varyingType = this->emitCode(builder, |
| 139 key, | 164 key, |
| 140 vertexCoords, | |
| 141 &fsVaryingName, | 165 &fsVaryingName, |
| 142 vsVaryingName, | 166 vsVaryingName, |
| 143 suffix); | 167 suffix); |
| 144 if (kVec3f_GrSLType == varyingType) { | 168 if (kVec3f_GrSLType == varyingType) { |
| 145 | 169 |
| 146 const char* coordName = "coords2D"; | 170 const char* coordName = "coords2D"; |
| 147 SkString suffixedCoordName; | 171 SkString suffixedCoordName; |
| 148 if (NULL != suffix) { | 172 if (NULL != suffix) { |
| 149 suffixedCoordName.append(coordName); | 173 suffixedCoordName.append(coordName); |
| 150 suffixedCoordName.append(suffix); | 174 suffixedCoordName.append(suffix); |
| 151 coordName = suffixedCoordName.c_str(); | 175 coordName = suffixedCoordName.c_str(); |
| 152 } | 176 } |
| 153 builder->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", | 177 builder->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 154 coordName, fsVaryingName, fsVaryingName); | 178 coordName, fsVaryingName, fsVaryingName); |
| 155 if (NULL != fsCoordName) { | 179 if (NULL != fsCoordName) { |
| 156 *fsCoordName = coordName; | 180 *fsCoordName = coordName; |
| 157 } | 181 } |
| 158 } else if(NULL != fsCoordName) { | 182 } else if(NULL != fsCoordName) { |
| 159 *fsCoordName = fsVaryingName; | 183 *fsCoordName = fsVaryingName; |
| 160 } | 184 } |
| 161 if (NULL != vsVaryingType) { | 185 if (NULL != vsVaryingType) { |
| 162 *vsVaryingType = varyingType; | 186 *vsVaryingType = varyingType; |
| 163 } | 187 } |
| 164 } | 188 } |
| 165 | 189 |
| 166 void GrGLEffectMatrix::setData(const GrGLUniformManager& uniformManager, | 190 void GrGLEffectMatrix::setData(const GrGLUniformManager& uniformManager, |
| 167 const SkMatrix& matrix, | 191 const SkMatrix& matrix, |
| 168 const SkMatrix& coordChangeMatrix, | 192 const GrDrawEffect& drawEffect, |
| 169 const GrTexture* texture) { | 193 const GrTexture* texture) { |
| 170 GrAssert((GrGLUniformManager::kInvalidUniformHandle == fUni) == | 194 GrAssert((GrGLUniformManager::kInvalidUniformHandle == fUni) == |
| 171 (kVoid_GrSLType == fUniType)); | 195 (kVoid_GrSLType == fUniType)); |
| 196 const SkMatrix& coordChangeMatrix = GrEffect::kLocal_CoordsType == fCoordsTy
pe ? |
| 197 drawEffect.getCoordChangeMatrix() : |
| 198 SkMatrix::I(); |
| 172 switch (fUniType) { | 199 switch (fUniType) { |
| 173 case kVoid_GrSLType: | 200 case kVoid_GrSLType: |
| 174 GrAssert(matrix.isIdentity()); | 201 GrAssert(matrix.isIdentity()); |
| 175 GrAssert(coordChangeMatrix.isIdentity()); | 202 GrAssert(coordChangeMatrix.isIdentity()); |
| 176 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->ori
gin()); | 203 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->ori
gin()); |
| 177 return; | 204 return; |
| 178 case kVec2f_GrSLType: { | 205 case kVec2f_GrSLType: { |
| 179 GrAssert(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChang
eMatrix.getType())); | 206 GrAssert(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChang
eMatrix.getType())); |
| 180 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->ori
gin()); | 207 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->ori
gin()); |
| 181 SkScalar tx = matrix[SkMatrix::kMTransX] + coordChangeMatrix[SkMatri
x::kMTransX]; | 208 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[SkMat
rix::kMTransX]; |
| 182 SkScalar ty = matrix[SkMatrix::kMTransY] + coordChangeMatrix[SkMatri
x::kMTransY]; | 209 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[SkMat
rix::kMTransY]; |
| 183 if (fPrevMatrix.get(SkMatrix::kMTransX) != tx || | 210 if (fPrevMatrix.get(SkMatrix::kMTransX) != tx || |
| 184 fPrevMatrix.get(SkMatrix::kMTransY) != ty) { | 211 fPrevMatrix.get(SkMatrix::kMTransY) != ty) { |
| 185 uniformManager.set2f(fUni, tx, ty); | 212 uniformManager.set2f(fUni, tx, ty); |
| 186 fPrevMatrix.set(SkMatrix::kMTransX, tx); | 213 fPrevMatrix.set(SkMatrix::kMTransX, tx); |
| 187 fPrevMatrix.set(SkMatrix::kMTransY, ty); | 214 fPrevMatrix.set(SkMatrix::kMTransY, ty); |
| 188 } | 215 } |
| 189 break; | 216 break; |
| 190 } | 217 } |
| 191 case kMat33f_GrSLType: { | 218 case kMat33f_GrSLType: { |
| 192 SkMatrix combined; | 219 SkMatrix combined; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 204 if (!fPrevMatrix.cheapEqualTo(combined)) { | 231 if (!fPrevMatrix.cheapEqualTo(combined)) { |
| 205 uniformManager.setSkMatrix(fUni, combined); | 232 uniformManager.setSkMatrix(fUni, combined); |
| 206 fPrevMatrix = combined; | 233 fPrevMatrix = combined; |
| 207 } | 234 } |
| 208 break; | 235 break; |
| 209 } | 236 } |
| 210 default: | 237 default: |
| 211 GrCrash("Unexpected uniform type."); | 238 GrCrash("Unexpected uniform type."); |
| 212 } | 239 } |
| 213 } | 240 } |
| OLD | NEW |