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