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