blob: 523b37e72ee907d6adcb7c5ff5fc6ca72d059c96 [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.comc7818882013-03-20 19:19:53 +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.comc7818882013-03-20 19:19:53 +000013 const GrDrawEffect& drawEffect,
14 CoordsType coordsType,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000015 const GrTexture* texture) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000016 EffectKey key = 0;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000017 SkMatrix::TypeMask type0 = effectMatrix.getType();
bsalomon@google.comc7818882013-03-20 19:19:53 +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.comc7818882013-03-20 19:19:53 +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.comc7818882013-03-20 19:19:53 +000041 key |= kIdentity_MatrixType;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000042 }
bsalomon@google.comc7818882013-03-20 19:19:53 +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) {
tfarina@chromium.org48552312013-03-26 21:48:58 +000051 GrSLType varyingType = kVoid_GrSLType;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000052 const char* uniName;
53 key &= kKeyMask;
bsalomon@google.comc7818882013-03-20 19:19:53 +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.comc7818882013-03-20 19:19:53 +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.comc7818882013-03-20 19:19:53 +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.comc7818882013-03-20 19:19:53 +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
bsalomon@google.com504976e2013-05-09 13:45:02 +000090 const char* varyingName = "MatrixCoord";
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000091 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.comc7818882013-03-20 19:19:53 +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 coords = NULL; // prevents warning
113 GrCrash("Unexpected coords type.");
114 }
115 // varying = matrix * coords (logically)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000116 switch (fUniType) {
117 case kVoid_GrSLType:
118 GrAssert(kVec2f_GrSLType == varyingType);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000119 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000120 break;
121 case kVec2f_GrSLType:
122 GrAssert(kVec2f_GrSLType == varyingType);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000123 builder->vsCodeAppendf("\t%s = %s + %s;\n",
124 vsVaryingName, uniName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000125 break;
126 case kMat33f_GrSLType: {
127 GrAssert(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType);
128 if (kVec2f_GrSLType == varyingType) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000129 builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n",
bsalomon@google.comc7818882013-03-20 19:19:53 +0000130 vsVaryingName, uniName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000131 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000132 builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n",
bsalomon@google.comc7818882013-03-20 19:19:53 +0000133 vsVaryingName, uniName, coords->c_str());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000134 }
135 break;
136 }
137 default:
138 GrCrash("Unexpected uniform type.");
139 }
140 if (NULL != vsCoordName) {
141 *vsCoordName = vsVaryingName;
142 }
143 if (NULL != fsCoordName) {
144 *fsCoordName = fsVaryingName;
145 }
146 return varyingType;
147}
148
149/**
150 * This is similar to emitCode except that it performs perspective division in the FS if the
151 * texture coordinates have a w coordinate. The fsCoordName always refers to a vec2f.
152 */
153void GrGLEffectMatrix::emitCodeMakeFSCoords2D(GrGLShaderBuilder* builder,
154 EffectKey key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000155 const char** fsCoordName,
156 const char** vsVaryingName,
157 GrSLType* vsVaryingType,
158 const char* suffix) {
159 const char* fsVaryingName;
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +0000160
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000161 GrSLType varyingType = this->emitCode(builder,
162 key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000163 &fsVaryingName,
164 vsVaryingName,
165 suffix);
166 if (kVec3f_GrSLType == varyingType) {
167
168 const char* coordName = "coords2D";
169 SkString suffixedCoordName;
170 if (NULL != suffix) {
171 suffixedCoordName.append(coordName);
172 suffixedCoordName.append(suffix);
173 coordName = suffixedCoordName.c_str();
174 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000175 builder->fsCodeAppendf("\tvec2 %s = %s.xy / %s.z;",
176 coordName, fsVaryingName, fsVaryingName);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000177 if (NULL != fsCoordName) {
178 *fsCoordName = coordName;
179 }
180 } else if(NULL != fsCoordName) {
181 *fsCoordName = fsVaryingName;
182 }
183 if (NULL != vsVaryingType) {
184 *vsVaryingType = varyingType;
185 }
186}
187
188void GrGLEffectMatrix::setData(const GrGLUniformManager& uniformManager,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000189 const SkMatrix& matrix,
190 const GrDrawEffect& drawEffect,
191 const GrTexture* texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000192 GrAssert((GrGLUniformManager::kInvalidUniformHandle == fUni) ==
bsalomon@google.comc7818882013-03-20 19:19:53 +0000193 (kVoid_GrSLType == fUniType));
194 const SkMatrix& coordChangeMatrix = GrEffect::kLocal_CoordsType == fCoordsType ?
195 drawEffect.getCoordChangeMatrix() :
196 SkMatrix::I();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000197 switch (fUniType) {
198 case kVoid_GrSLType:
199 GrAssert(matrix.isIdentity());
200 GrAssert(coordChangeMatrix.isIdentity());
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000201 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->origin());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000202 return;
203 case kVec2f_GrSLType: {
204 GrAssert(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChangeMatrix.getType()));
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000205 GrAssert(NULL == texture || kTopLeft_GrSurfaceOrigin == texture->origin());
bsalomon@google.comc7818882013-03-20 19:19:53 +0000206 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[SkMatrix::kMTransX];
207 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[SkMatrix::kMTransY];
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000208 if (fPrevMatrix.get(SkMatrix::kMTransX) != tx ||
209 fPrevMatrix.get(SkMatrix::kMTransY) != ty) {
210 uniformManager.set2f(fUni, tx, ty);
211 fPrevMatrix.set(SkMatrix::kMTransX, tx);
212 fPrevMatrix.set(SkMatrix::kMTransY, ty);
213 }
214 break;
215 }
216 case kMat33f_GrSLType: {
217 SkMatrix combined;
218 combined.setConcat(matrix, coordChangeMatrix);
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000219 if (NULL != texture && kBottomLeft_GrSurfaceOrigin == texture->origin()) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000220 // combined.postScale(1,-1);
221 // combined.postTranslate(0,1);
222 combined.set(SkMatrix::kMSkewY,
223 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]);
224 combined.set(SkMatrix::kMScaleY,
225 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]);
226 combined.set(SkMatrix::kMTransY,
227 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]);
228 }
229 if (!fPrevMatrix.cheapEqualTo(combined)) {
230 uniformManager.setSkMatrix(fUni, combined);
231 fPrevMatrix = combined;
232 }
233 break;
234 }
235 default:
236 GrCrash("Unexpected uniform type.");
237 }
238}