1/*
2 * Copyright 2014 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 "src/gpu/glsl/GrGLSLGeometryProcessor.h"
9
10#include "src/gpu/GrCoordTransform.h"
11#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
12#include "src/gpu/glsl/GrGLSLUniformHandler.h"
13#include "src/gpu/glsl/GrGLSLVarying.h"
14#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
15
16void GrGLSLGeometryProcessor::emitCode(EmitArgs& args) {
17 GrGPArgs gpArgs;
18 this->onEmitCode(args, &gpArgs);
19
20 if (args.fGP.willUseTessellationShaders()) {
21 // Tessellation shaders are temporarily responsible for integrating their own code strings
22 // while we work out full support.
23 return;
24 }
25
26 GrGLSLVertexBuilder* vBuilder = args.fVertBuilder;
27 if (!args.fGP.willUseGeoShader()) {
28 // Emit the vertex position to the hardware in the normalized window coordinates it expects.
29 SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() ||
30 kFloat3_GrSLType == gpArgs.fPositionVar.getType());
31 vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName,
32 gpArgs.fPositionVar.getType());
33 if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) {
34 args.fVaryingHandler->setNoPerspective();
35 }
36 } else {
37 // Since we have a geometry shader, leave the vertex position in Skia device space for now.
38 // The geometry Shader will operate in device space, and then convert the final positions to
39 // normalized hardware window coordinates under the hood, once everything else has finished.
40 // The subclass must call setNoPerspective on the varying handler, if applicable.
41 vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str());
42 switch (gpArgs.fPositionVar.getType()) {
43 case kFloat_GrSLType:
44 vBuilder->codeAppend(", 0"); // fallthru.
45 case kFloat2_GrSLType:
46 vBuilder->codeAppend(", 0"); // fallthru.
47 case kFloat3_GrSLType:
48 vBuilder->codeAppend(", 1"); // fallthru.
49 case kFloat4_GrSLType:
50 vBuilder->codeAppend(");");
51 break;
52 default:
53 SK_ABORT("Invalid position var type");
54 break;
55 }
56 }
57}
58
59void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb,
60 GrGLSLVaryingHandler* varyingHandler,
61 GrGLSLUniformHandler* uniformHandler,
62 const GrShaderVar& localCoordsVar,
63 const SkMatrix& localMatrix,
64 FPCoordTransformHandler* handler) {
65 // We only require localCoordsVar to be valid if there is a coord transform that needs
66 // it. CTs on FPs called with explicit coords do not require a local coord.
67 auto getLocalCoords = [&localCoordsVar,
68 localCoords = SkString(),
69 localCoordLength = int()]() mutable {
70 if (localCoords.isEmpty()) {
71 localCoordLength = GrSLTypeVecLength(localCoordsVar.getType());
72 SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType()));
73 SkASSERT(localCoordLength == 2 || localCoordLength == 3);
74 if (localCoordLength == 3) {
75 localCoords = localCoordsVar.getName();
76 } else {
77 localCoords.printf("float3(%s, 1)", localCoordsVar.c_str());
78 }
79 }
80 return std::make_tuple(localCoords, localCoordLength);
81 };
82
83 GrShaderVar transformVar;
84 for (int i = 0; *handler; ++*handler, ++i) {
85 auto [coordTransform, fp] = handler->get();
86 // Add uniform for coord transform matrix.
87 const char* matrixName;
88 if (!fp.isSampledWithExplicitCoords() || !coordTransform.isNoOp()) {
89 SkString strUniName;
90 strUniName.printf("CoordTransformMatrix_%d", i);
91 auto flag = fp.isSampledWithExplicitCoords() ? kFragment_GrShaderFlag
92 : kVertex_GrShaderFlag;
93 auto& uni = fInstalledTransforms.push_back();
94 if (fp.isSampledWithExplicitCoords() && coordTransform.matrix().isScaleTranslate()) {
95 uni.fType = kFloat4_GrSLType;
96 } else {
97 uni.fType = kFloat3x3_GrSLType;
98 }
99 uni.fHandle =
100 uniformHandler->addUniform(&fp, flag, uni.fType, strUniName.c_str(),
101 &matrixName);
102 transformVar = uniformHandler->getUniformVariable(uni.fHandle);
103 } else {
104 // Install a coord transform that will be skipped.
105 fInstalledTransforms.push_back();
106 handler->omitCoordsForCurrCoordTransform();
107 continue;
108 }
109
110 GrShaderVar fsVar;
111 // Add varying if required and register varying and matrix uniform.
112 if (!fp.isSampledWithExplicitCoords()) {
113 auto [localCoordsStr, localCoordLength] = getLocalCoords();
114 GrGLSLVarying v(kFloat2_GrSLType);
115 if (localMatrix.hasPerspective() || coordTransform.matrix().hasPerspective() ||
116 localCoordLength == 3) {
117 v = GrGLSLVarying(kFloat3_GrSLType);
118 }
119 SkString strVaryingName;
120 strVaryingName.printf("TransformedCoords_%d", i);
121 varyingHandler->addVarying(strVaryingName.c_str(), &v);
122
123 SkASSERT(fInstalledTransforms.back().fType == kFloat3x3_GrSLType);
124 if (v.type() == kFloat2_GrSLType) {
125 vb->codeAppendf("%s = (%s * %s).xy;", v.vsOut(), matrixName,
126 localCoordsStr.c_str());
127 } else {
128 vb->codeAppendf("%s = %s * %s;", v.vsOut(), matrixName, localCoordsStr.c_str());
129 }
130 fsVar = GrShaderVar(SkString(v.fsIn()), v.type(), GrShaderVar::TypeModifier::In);
131 }
132 handler->specifyCoordsForCurrCoordTransform(transformVar, fsVar);
133 }
134}
135
136void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix,
137 const GrGLSLProgramDataManager& pdman,
138 const CoordTransformRange& transformRange) {
139 int i = 0;
140 for (auto [transform, fp] : transformRange) {
141 if (fInstalledTransforms[i].fHandle.isValid()) {
142 SkMatrix m;
143 if (fp.isSampledWithExplicitCoords()) {
144 m = GetTransformMatrix(transform, SkMatrix::I());
145 } else {
146 m = GetTransformMatrix(transform, localMatrix);
147 }
148 if (!SkMatrixPriv::CheapEqual(fInstalledTransforms[i].fCurrentValue, m)) {
149 if (fInstalledTransforms[i].fType == kFloat4_GrSLType) {
150 float values[4] = {m.getScaleX(), m.getTranslateX(),
151 m.getScaleY(), m.getTranslateY()};
152 SkASSERT(m.isScaleTranslate());
153 pdman.set4fv(fInstalledTransforms[i].fHandle.toIndex(), 1, values);
154 } else {
155 SkASSERT(!m.isScaleTranslate() || !fp.isSampledWithExplicitCoords());
156 SkASSERT(fInstalledTransforms[i].fType == kFloat3x3_GrSLType);
157 pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m);
158 }
159 fInstalledTransforms[i].fCurrentValue = m;
160 }
161 }
162 ++i;
163 }
164 SkASSERT(i == fInstalledTransforms.count());
165}
166
167void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder,
168 GrGPArgs* gpArgs,
169 const char* posName) {
170 gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2");
171 vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName);
172}
173
174void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder,
175 GrGLSLUniformHandler* uniformHandler,
176 GrGPArgs* gpArgs,
177 const char* posName,
178 const SkMatrix& mat,
179 UniformHandle* viewMatrixUniform) {
180 if (mat.isIdentity()) {
181 gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2");
182 vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName);
183 } else {
184 const char* viewMatrixName;
185 *viewMatrixUniform = uniformHandler->addUniform(nullptr,
186 kVertex_GrShaderFlag,
187 kFloat3x3_GrSLType,
188 "uViewM",
189 &viewMatrixName);
190 if (!mat.hasPerspective()) {
191 gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2");
192 vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;",
193 gpArgs->fPositionVar.c_str(), viewMatrixName, posName);
194 } else {
195 gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3");
196 vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);",
197 gpArgs->fPositionVar.c_str(), viewMatrixName, posName);
198 }
199 }
200}
201