OLD | NEW |
| (Empty) |
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 "GrGLCoordTransform.h" | |
9 #include "GrDrawEffect.h" | |
10 #include "GrTexture.h" | |
11 #include "GrGLShaderBuilder.h" | |
12 | |
13 GrGLCoordTransform::EffectKey GrGLCoordTransform::GenKey(const GrDrawEffect& dra
wEffect, | |
14 int transformIdx) { | |
15 const GrCoordTransform& transform = (*drawEffect.effect())->coordTransform(t
ransformIdx); | |
16 EffectKey key = 0; | |
17 SkMatrix::TypeMask type0 = transform.getMatrix().getType(); | |
18 SkMatrix::TypeMask type1; | |
19 if (kLocal_GrCoordSet == transform.sourceCoords()) { | |
20 type1 = drawEffect.getCoordChangeMatrix().getType(); | |
21 } else { | |
22 if (drawEffect.programHasExplicitLocalCoords()) { | |
23 // We only make the key indicate that device coords are referenced w
hen the local coords | |
24 // are not actually determined by positions. Otherwise the local coo
rds var and position | |
25 // var are identical. | |
26 key |= kPositionCoords_Flag; | |
27 } | |
28 type1 = SkMatrix::kIdentity_Mask; | |
29 } | |
30 | |
31 int combinedTypes = type0 | type1; | |
32 | |
33 bool reverseY = transform.reverseY(); | |
34 | |
35 if (SkMatrix::kPerspective_Mask & combinedTypes) { | |
36 key |= kGeneral_MatrixType; | |
37 } else if (((SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask) & combinedTypes
) || reverseY) { | |
38 key |= kNoPersp_MatrixType; | |
39 } else if (SkMatrix::kTranslate_Mask & combinedTypes) { | |
40 key |= kTrans_MatrixType; | |
41 } else { | |
42 key |= kIdentity_MatrixType; | |
43 } | |
44 return key; | |
45 } | |
46 | |
47 void GrGLCoordTransform::emitCode(GrGLShaderBuilder* builder, | |
48 EffectKey key, | |
49 TransformedCoords* transformedCoords, | |
50 int suffix) { | |
51 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(
); | |
52 SkASSERT(NULL != vertexBuilder); | |
53 | |
54 GrSLType varyingType = kVoid_GrSLType; | |
55 const char* uniName; | |
56 switch (key & kMatrixTypeKeyMask) { | |
57 case kIdentity_MatrixType: | |
58 fUniType = kVoid_GrSLType; | |
59 uniName = NULL; | |
60 varyingType = kVec2f_GrSLType; | |
61 break; | |
62 case kTrans_MatrixType: | |
63 fUniType = kVec2f_GrSLType; | |
64 uniName = "StageTranslate"; | |
65 varyingType = kVec2f_GrSLType; | |
66 break; | |
67 case kNoPersp_MatrixType: | |
68 fUniType = kMat33f_GrSLType; | |
69 uniName = "StageMatrix"; | |
70 varyingType = kVec2f_GrSLType; | |
71 break; | |
72 case kGeneral_MatrixType: | |
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 (kVoid_GrSLType != fUniType) { | |
82 if (0 != suffix) { | |
83 suffixedUniName.append(uniName); | |
84 suffixedUniName.appendf("_%i", suffix); | |
85 uniName = suffixedUniName.c_str(); | |
86 } | |
87 fUni = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility, | |
88 fUniType, | |
89 uniName, | |
90 &uniName); | |
91 } | |
92 | |
93 const char* varyingName = "MatrixCoord"; | |
94 SkString suffixedVaryingName; | |
95 if (0 != suffix) { | |
96 suffixedVaryingName.append(varyingName); | |
97 suffixedVaryingName.appendf("_%i", suffix); | |
98 varyingName = suffixedVaryingName.c_str(); | |
99 } | |
100 const char* vsVaryingName; | |
101 const char* fsVaryingName; | |
102 vertexBuilder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryi
ngName); | |
103 | |
104 const GrGLShaderVar& coords = (kPositionCoords_Flag & key) ? | |
105 vertexBuilder->positionAttribute() : | |
106 vertexBuilder->localCoordsAttribute(); | |
107 // varying = matrix * coords (logically) | |
108 switch (fUniType) { | |
109 case kVoid_GrSLType: | |
110 SkASSERT(kVec2f_GrSLType == varyingType); | |
111 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords.c
_str()); | |
112 break; | |
113 case kVec2f_GrSLType: | |
114 SkASSERT(kVec2f_GrSLType == varyingType); | |
115 vertexBuilder->vsCodeAppendf("\t%s = %s + %s;\n", | |
116 vsVaryingName, uniName, coords.c_str())
; | |
117 break; | |
118 case kMat33f_GrSLType: { | |
119 SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyin
gType); | |
120 if (kVec2f_GrSLType == varyingType) { | |
121 vertexBuilder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", | |
122 vsVaryingName, uniName, coords.c_st
r()); | |
123 } else { | |
124 vertexBuilder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", | |
125 vsVaryingName, uniName, coords.c_st
r()); | |
126 } | |
127 break; | |
128 } | |
129 default: | |
130 GrCrash("Unexpected uniform type."); | |
131 } | |
132 SkASSERT(NULL != transformedCoords); | |
133 transformedCoords->fName = fsVaryingName; | |
134 transformedCoords->fType = varyingType; | |
135 transformedCoords->fVSName = vsVaryingName; | |
136 } | |
137 | |
138 void GrGLCoordTransform::setData(const GrGLUniformManager& uniformManager, | |
139 const GrDrawEffect& drawEffect, | |
140 int transformIdx) { | |
141 SkASSERT(fUni.isValid() != (kVoid_GrSLType == fUniType)); | |
142 const GrCoordTransform& transform = (*drawEffect.effect())->coordTransform(t
ransformIdx); | |
143 const SkMatrix& matrix = transform.getMatrix(); | |
144 const SkMatrix& coordChangeMatrix = kLocal_GrCoordSet == transform.sourceCoo
rds() ? | |
145 drawEffect.getCoordChangeMatrix() : | |
146 SkMatrix::I(); | |
147 switch (fUniType) { | |
148 case kVoid_GrSLType: | |
149 SkASSERT(matrix.isIdentity()); | |
150 SkASSERT(coordChangeMatrix.isIdentity()); | |
151 SkASSERT(!transform.reverseY()); | |
152 return; | |
153 case kVec2f_GrSLType: { | |
154 SkASSERT(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChang
eMatrix.getType())); | |
155 SkASSERT(!transform.reverseY()); | |
156 SkScalar tx = matrix[SkMatrix::kMTransX] + (coordChangeMatrix)[SkMat
rix::kMTransX]; | |
157 SkScalar ty = matrix[SkMatrix::kMTransY] + (coordChangeMatrix)[SkMat
rix::kMTransY]; | |
158 if (fPrevMatrix.get(SkMatrix::kMTransX) != tx || | |
159 fPrevMatrix.get(SkMatrix::kMTransY) != ty) { | |
160 uniformManager.set2f(fUni, tx, ty); | |
161 fPrevMatrix.set(SkMatrix::kMTransX, tx); | |
162 fPrevMatrix.set(SkMatrix::kMTransY, ty); | |
163 } | |
164 break; | |
165 } | |
166 case kMat33f_GrSLType: { | |
167 SkMatrix combined; | |
168 combined.setConcat(matrix, coordChangeMatrix); | |
169 if (transform.reverseY()) { | |
170 // combined.postScale(1,-1); | |
171 // combined.postTranslate(0,1); | |
172 combined.set(SkMatrix::kMSkewY, | |
173 combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]); | |
174 combined.set(SkMatrix::kMScaleY, | |
175 combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY])
; | |
176 combined.set(SkMatrix::kMTransY, | |
177 combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY])
; | |
178 } | |
179 if (!fPrevMatrix.cheapEqualTo(combined)) { | |
180 uniformManager.setSkMatrix(fUni, combined); | |
181 fPrevMatrix = combined; | |
182 } | |
183 break; | |
184 } | |
185 default: | |
186 GrCrash("Unexpected uniform type."); | |
187 } | |
188 } | |
OLD | NEW |