blob: aaca2403a991bdd7bb32bbb5e5528a5fd4ba8bbe [file] [log] [blame]
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001/*
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.comae81d5c2013-03-20 17:32:27 +00009#include "GrDrawEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000010#include "GrTexture.h"
11
12GrGLEffect::EffectKey GrGLEffectMatrix::GenKey(const SkMatrix& effectMatrix,
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000013 const GrDrawEffect& drawEffect,
14 CoordsType coordsType,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000015 const GrTexture* texture) {
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000016 EffectKey key = 0;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000017 SkMatrix::TypeMask type0 = effectMatrix.getType();
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000018 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.comd8b5fac2012-11-01 17:02:46 +000029
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000030 int combinedTypes = type0 | type1;
31
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +000032 bool reverseY = (NULL != texture) && kBottomLeft_GrSurfaceOrigin == texture->origin();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000033
34 if (SkMatrix::kPerspective_Mask & combinedTypes) {
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000035 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.comd8b5fac2012-11-01 17:02:46 +000040 } else {
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000041 key |= kIdentity_MatrixType;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000042 }
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000043 return key;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000044}
45
46GrSLType GrGLEffectMatrix::emitCode(GrGLShaderBuilder* builder,
47 EffectKey key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000048 const char** fsCoordName,
49 const char** vsCoordName,
50 const char* suffix) {
51 GrSLType varyingType;
52 const char* uniName;
53 key &= kKeyMask;
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000054 switch (key & kMatrixTypeKeyMask) {
55 case kIdentity_MatrixType:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000056 fUniType = kVoid_GrSLType;
57 varyingType = kVec2f_GrSLType;
58 break;
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000059 case kTrans_MatrixType:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000060 fUniType = kVec2f_GrSLType;
61 uniName = "StageTranslate";
62 varyingType = kVec2f_GrSLType;
63 break;
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000064 case kNoPersp_MatrixType:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000065 fUniType = kMat33f_GrSLType;
66 uniName = "StageMatrix";
67 varyingType = kVec2f_GrSLType;
68 break;
bsalomon@google.comae81d5c2013-03-20 17:32:27 +000069 case kGeneral_MatrixType:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000070 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.comae81d5c2013-03-20 17:32:27 +0000101 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.comd8b5fac2012-11-01 17:02:46 +0000115 switch (fUniType) {
116 case kVoid_GrSLType:
117 GrAssert(kVec2f_GrSLType == varyingType);
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000118 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000119 break;
120 case kVec2f_GrSLType:
121 GrAssert(kVec2f_GrSLType == varyingType);
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000122 builder->vsCodeAppendf("\t%s = %s + %s;\n",
123 vsVaryingName, uniName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000124 break;
125 case kMat33f_GrSLType: {
126 GrAssert(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType);
127 if (kVec2f_GrSLType == varyingType) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000128 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000129 vsVaryingName, uniName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000130 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000131 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000132 vsVaryingName, uniName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000133 }
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 */
152void GrGLEffectMatrix::emitCodeMakeFSCoords2D(GrGLShaderBuilder* builder,
153 EffectKey key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000154 const char** fsCoordName,
155 const char** vsVaryingName,
156 GrSLType* vsVaryingType,
157 const char* suffix) {
158 const char* fsVaryingName;
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +0000159
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000160 GrSLType varyingType = this->emitCode(builder,
161 key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000162 &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.comf910d3b2013-03-07 17:06:57 +0000174 builder->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;",
175 coordName, fsVaryingName, fsVaryingName);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000176 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
187void GrGLEffectMatrix::setData(const GrGLUniformManager& uniformManager,
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000188 const SkMatrix& matrix,
189 const GrDrawEffect& drawEffect,
190 const GrTexture* texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000191 GrAssert((GrGLUniformManager::kInvalidUniformHandle == fUni) ==
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000192 (kVoid_GrSLType == fUniType));
193 const SkMatrix& coordChangeMatrix = GrEffect::kLocal_CoordsType == fCoordsType ?
194 drawEffect.getCoordChangeMatrix() :
195 SkMatrix::I();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000196 switch (fUniType) {
197 case kVoid_GrSLType:
198 GrAssert(matrix.isIdentity());
199 GrAssert(coordChangeMatrix.isIdentity());
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000200 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->origin());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000201 return;
202 case kVec2f_GrSLType: {
203 GrAssert(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChangeMatrix.getType()));
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000204 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->origin());
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000205 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[SkMatrix::kMTransX];
206 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[SkMatrix::kMTransY];
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000207 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.orgef5dbe12013-01-28 16:42:38 +0000218 if (NULL != texture && kBottomLeft_GrSurfaceOrigin == texture->origin()) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000219 // 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}