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