| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrGLEffectMatrix.h" |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 9 | #include "GrDrawEffect.h" |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 10 | #include "GrTexture.h" |
| 11 | |
| 12 | GrGLEffect::EffectKey GrGLEffectMatrix::GenKey(const SkMatrix& effectMatrix, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 13 | const GrDrawEffect& drawEffect, |
| 14 | CoordsType coordsType, |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 15 | const GrTexture* texture) { |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 16 | EffectKey key = 0; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 17 | SkMatrix::TypeMask type0 = effectMatrix.getType(); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 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 when the local coords |
| 24 | // are not actually determined by positions. |
| 25 | key |= kPositionCoords_Flag; |
| 26 | } |
| 27 | type1 = SkMatrix::kIdentity_Mask; |
| 28 | } |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 29 | |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 30 | int combinedTypes = type0 | type1; |
| 31 | |
| senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 32 | bool reverseY = (NULL != texture) && kBottomLeft_GrSurfaceOrigin == texture->origin(); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 33 | |
| 34 | if (SkMatrix::kPerspective_Mask & combinedTypes) { |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 35 | key |= kGeneral_MatrixType; |
| 36 | } else if (((SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask) & combinedTypes) || reverseY) { |
| 37 | key |= kNoPersp_MatrixType; |
| 38 | } else if (SkMatrix::kTranslate_Mask & combinedTypes) { |
| 39 | key |= kTrans_MatrixType; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 40 | } else { |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 41 | key |= kIdentity_MatrixType; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 42 | } |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 43 | return key; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | GrSLType GrGLEffectMatrix::emitCode(GrGLShaderBuilder* builder, |
| 47 | EffectKey key, |
| commit-bot@chromium.org | 7ab7ca4 | 2013-08-28 15:59:13 +0000 | [diff] [blame] | 48 | SkString* fsCoordName, |
| 49 | SkString* vsCoordName, |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 50 | const char* suffix) { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 51 | GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(); |
| 52 | SkASSERT(NULL != vertexBuilder); |
| 53 | |
| tfarina@chromium.org | 4855231 | 2013-03-26 21:48:58 +0000 | [diff] [blame] | 54 | GrSLType varyingType = kVoid_GrSLType; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 55 | const char* uniName; |
| 56 | key &= kKeyMask; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 57 | switch (key & kMatrixTypeKeyMask) { |
| 58 | case kIdentity_MatrixType: |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 59 | fUniType = kVoid_GrSLType; |
| robertphillips@google.com | 115fbf6 | 2013-09-10 17:37:03 +0000 | [diff] [blame] | 60 | uniName = NULL; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 61 | varyingType = kVec2f_GrSLType; |
| 62 | break; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 63 | case kTrans_MatrixType: |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 64 | fUniType = kVec2f_GrSLType; |
| 65 | uniName = "StageTranslate"; |
| 66 | varyingType = kVec2f_GrSLType; |
| 67 | break; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 68 | case kNoPersp_MatrixType: |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 69 | fUniType = kMat33f_GrSLType; |
| 70 | uniName = "StageMatrix"; |
| 71 | varyingType = kVec2f_GrSLType; |
| 72 | break; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 73 | case kGeneral_MatrixType: |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 74 | fUniType = kMat33f_GrSLType; |
| 75 | uniName = "StageMatrix"; |
| 76 | varyingType = kVec3f_GrSLType; |
| 77 | break; |
| 78 | default: |
| 79 | GrCrash("Unexpected key."); |
| 80 | } |
| 81 | SkString suffixedUniName; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 82 | if (kVoid_GrSLType != fUniType) { |
| robertphillips@google.com | 115fbf6 | 2013-09-10 17:37:03 +0000 | [diff] [blame] | 83 | if (NULL != suffix) { |
| 84 | suffixedUniName.append(uniName); |
| 85 | suffixedUniName.append(suffix); |
| 86 | uniName = suffixedUniName.c_str(); |
| 87 | } |
| commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 88 | fUni = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 89 | fUniType, |
| 90 | uniName, |
| 91 | &uniName); |
| 92 | } |
| 93 | |
| bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 94 | const char* varyingName = "MatrixCoord"; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 95 | SkString suffixedVaryingName; |
| 96 | if (NULL != suffix) { |
| 97 | suffixedVaryingName.append(varyingName); |
| 98 | suffixedVaryingName.append(suffix); |
| 99 | varyingName = suffixedVaryingName.c_str(); |
| 100 | } |
| 101 | const char* vsVaryingName; |
| 102 | const char* fsVaryingName; |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 103 | vertexBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 104 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 105 | const GrGLShaderVar* coords; |
| 106 | switch (fCoordsType) { |
| 107 | case GrEffect::kLocal_CoordsType: |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 108 | SkASSERT(!(kPositionCoords_Flag & key)); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 109 | coords = &vertexBuilder->localCoordsAttribute(); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 110 | break; |
| 111 | case GrEffect::kPosition_CoordsType: |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 112 | SkASSERT((kPositionCoords_Flag & key) || !vertexBuilder->hasExplicitLocalCoords()); |
| 113 | coords = &vertexBuilder->positionAttribute(); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | coords = NULL; // prevents warning |
| 117 | GrCrash("Unexpected coords type."); |
| 118 | } |
| 119 | // varying = matrix * coords (logically) |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 120 | switch (fUniType) { |
| 121 | case kVoid_GrSLType: |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 122 | SkASSERT(kVec2f_GrSLType == varyingType); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 123 | vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords->c_str()); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 124 | break; |
| 125 | case kVec2f_GrSLType: |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 126 | SkASSERT(kVec2f_GrSLType == varyingType); |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 127 | vertexBuilder->vsCodeAppendf("\t%s = %s + %s;\n", |
| 128 | vsVaryingName, uniName, coords->c_str()); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 129 | break; |
| 130 | case kMat33f_GrSLType: { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 131 | SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 132 | if (kVec2f_GrSLType == varyingType) { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 133 | vertexBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", |
| 134 | vsVaryingName, uniName, coords->c_str()); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 135 | } else { |
| commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 136 | vertexBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", |
| 137 | vsVaryingName, uniName, coords->c_str()); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 138 | } |
| 139 | break; |
| 140 | } |
| 141 | default: |
| 142 | GrCrash("Unexpected uniform type."); |
| 143 | } |
| 144 | if (NULL != vsCoordName) { |
| 145 | *vsCoordName = vsVaryingName; |
| 146 | } |
| 147 | if (NULL != fsCoordName) { |
| 148 | *fsCoordName = fsVaryingName; |
| 149 | } |
| 150 | return varyingType; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * This is similar to emitCode except that it performs perspective division in the FS if the |
| 155 | * texture coordinates have a w coordinate. The fsCoordName always refers to a vec2f. |
| 156 | */ |
| 157 | void GrGLEffectMatrix::emitCodeMakeFSCoords2D(GrGLShaderBuilder* builder, |
| 158 | EffectKey key, |
| commit-bot@chromium.org | 7ab7ca4 | 2013-08-28 15:59:13 +0000 | [diff] [blame] | 159 | SkString* fsCoordName, |
| 160 | SkString* vsVaryingName, |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 161 | GrSLType* vsVaryingType, |
| 162 | const char* suffix) { |
| commit-bot@chromium.org | 7ab7ca4 | 2013-08-28 15:59:13 +0000 | [diff] [blame] | 163 | SkString fsVaryingName; |
| skia.committer@gmail.com | 760f2d9 | 2012-11-02 02:01:24 +0000 | [diff] [blame] | 164 | |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 165 | GrSLType varyingType = this->emitCode(builder, |
| 166 | key, |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 167 | &fsVaryingName, |
| 168 | vsVaryingName, |
| 169 | suffix); |
| 170 | if (kVec3f_GrSLType == varyingType) { |
| 171 | |
| 172 | const char* coordName = "coords2D"; |
| 173 | SkString suffixedCoordName; |
| 174 | if (NULL != suffix) { |
| 175 | suffixedCoordName.append(coordName); |
| 176 | suffixedCoordName.append(suffix); |
| 177 | coordName = suffixedCoordName.c_str(); |
| 178 | } |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 179 | builder->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| commit-bot@chromium.org | 7ab7ca4 | 2013-08-28 15:59:13 +0000 | [diff] [blame] | 180 | coordName, fsVaryingName.c_str(), fsVaryingName.c_str()); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 181 | if (NULL != fsCoordName) { |
| 182 | *fsCoordName = coordName; |
| 183 | } |
| 184 | } else if(NULL != fsCoordName) { |
| 185 | *fsCoordName = fsVaryingName; |
| 186 | } |
| 187 | if (NULL != vsVaryingType) { |
| 188 | *vsVaryingType = varyingType; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void GrGLEffectMatrix::setData(const GrGLUniformManager& uniformManager, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 193 | const SkMatrix& matrix, |
| 194 | const GrDrawEffect& drawEffect, |
| 195 | const GrTexture* texture) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 196 | SkASSERT(fUni.isValid() != (kVoid_GrSLType == fUniType)); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 197 | const SkMatrix& coordChangeMatrix = GrEffect::kLocal_CoordsType == fCoordsType ? |
| 198 | drawEffect.getCoordChangeMatrix() : |
| 199 | SkMatrix::I(); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 200 | switch (fUniType) { |
| 201 | case kVoid_GrSLType: |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 202 | SkASSERT(matrix.isIdentity()); |
| 203 | SkASSERT(coordChangeMatrix.isIdentity()); |
| 204 | SkASSERT(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->origin()); |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 205 | return; |
| 206 | case kVec2f_GrSLType: { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 207 | SkASSERT(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChangeMatrix.getType())); |
| 208 | SkASSERT(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->origin()); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 209 | SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[SkMatrix::kMTransX]; |
| 210 | SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[SkMatrix::kMTransY]; |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 211 | if (fPrevMatrix.get(SkMatrix::kMTransX) != tx || |
| 212 | fPrevMatrix.get(SkMatrix::kMTransY) != ty) { |
| 213 | uniformManager.set2f(fUni, tx, ty); |
| 214 | fPrevMatrix.set(SkMatrix::kMTransX, tx); |
| 215 | fPrevMatrix.set(SkMatrix::kMTransY, ty); |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | case kMat33f_GrSLType: { |
| 220 | SkMatrix combined; |
| 221 | combined.setConcat(matrix, coordChangeMatrix); |
| senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 222 | if (NULL != texture && kBottomLeft_GrSurfaceOrigin == texture->origin()) { |
| bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 223 | // combined.postScale(1,-1); |
| 224 | // combined.postTranslate(0,1); |
| 225 | combined.set(SkMatrix::kMSkewY, |
| 226 | combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]); |
| 227 | combined.set(SkMatrix::kMScaleY, |
| 228 | combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]); |
| 229 | combined.set(SkMatrix::kMTransY, |
| 230 | combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]); |
| 231 | } |
| 232 | if (!fPrevMatrix.cheapEqualTo(combined)) { |
| 233 | uniformManager.setSkMatrix(fUni, combined); |
| 234 | fPrevMatrix = combined; |
| 235 | } |
| 236 | break; |
| 237 | } |
| 238 | default: |
| 239 | GrCrash("Unexpected uniform type."); |
| 240 | } |
| 241 | } |