joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 15 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 16 | void GrGLSLGeometryProcessor::emitCode(EmitArgs& args) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 17 | GrGPArgs gpArgs; |
| 18 | this->onEmitCode(args, &gpArgs); |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 19 | |
Chris Dalton | 5a2f962 | 2019-12-27 14:56:38 -0700 | [diff] [blame] | 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 | |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 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. |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 29 | SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() || |
| 30 | kFloat3_GrSLType == gpArgs.fPositionVar.getType()); |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 31 | vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName, |
| 32 | gpArgs.fPositionVar.getType()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 33 | if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) { |
| 34 | args.fVaryingHandler->setNoPerspective(); |
| 35 | } |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 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. |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 40 | // The subclass must call setNoPerspective on the varying handler, if applicable. |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 41 | vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 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; |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 55 | } |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 56 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 57 | } |
| 58 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 59 | void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 60 | GrGLSLVaryingHandler* varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 61 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 62 | const GrShaderVar& localCoordsVar, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 63 | const SkMatrix& localMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 64 | FPCoordTransformHandler* handler) { |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 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 | }; |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 82 | |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 83 | GrShaderVar transformVar; |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 84 | for (int i = 0; *handler; ++*handler, ++i) { |
| 85 | auto [coordTransform, fp] = handler->get(); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 86 | // Add uniform for coord transform matrix. |
| 87 | const char* matrixName; |
| 88 | if (!fp.isSampledWithExplicitCoords() || !coordTransform.isNoOp()) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 89 | SkString strUniName; |
| 90 | strUniName.printf("CoordTransformMatrix_%d", i); |
Brian Salomon | 2fade72 | 2020-03-20 12:35:32 -0400 | [diff] [blame] | 91 | auto flag = fp.isSampledWithExplicitCoords() ? kFragment_GrShaderFlag |
| 92 | : kVertex_GrShaderFlag; |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 93 | auto& uni = fInstalledTransforms.push_back(); |
Brian Salomon | 14ed885 | 2020-03-24 10:43:38 -0400 | [diff] [blame] | 94 | if (fp.isSampledWithExplicitCoords() && coordTransform.matrix().isScaleTranslate()) { |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 95 | uni.fType = kFloat4_GrSLType; |
| 96 | } else { |
| 97 | uni.fType = kFloat3x3_GrSLType; |
| 98 | } |
| 99 | uni.fHandle = |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 100 | uniformHandler->addUniform(&fp, flag, uni.fType, strUniName.c_str(), |
| 101 | &matrixName); |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 102 | transformVar = uniformHandler->getUniformVariable(uni.fHandle); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 103 | } else { |
| 104 | // Install a coord transform that will be skipped. |
| 105 | fInstalledTransforms.push_back(); |
| 106 | handler->omitCoordsForCurrCoordTransform(); |
| 107 | continue; |
| 108 | } |
| 109 | |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 110 | GrShaderVar fsVar; |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 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); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 115 | if (localMatrix.hasPerspective() || coordTransform.matrix().hasPerspective() || |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 116 | localCoordLength == 3) { |
| 117 | v = GrGLSLVarying(kFloat3_GrSLType); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 118 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 119 | SkString strVaryingName; |
| 120 | strVaryingName.printf("TransformedCoords_%d", i); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 121 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 122 | |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 123 | SkASSERT(fInstalledTransforms.back().fType == kFloat3x3_GrSLType); |
Brian Salomon | 706851d | 2020-02-20 14:18:00 -0500 | [diff] [blame] | 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()); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 129 | } |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 130 | fsVar = GrShaderVar(SkString(v.fsIn()), v.type(), GrShaderVar::TypeModifier::In); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 131 | } |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 132 | handler->specifyCoordsForCurrCoordTransform(transformVar, fsVar); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 136 | void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix, |
| 137 | const GrGLSLProgramDataManager& pdman, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 138 | const CoordTransformRange& transformRange) { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 139 | int i = 0; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 140 | for (auto [transform, fp] : transformRange) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 141 | if (fInstalledTransforms[i].fHandle.isValid()) { |
| 142 | SkMatrix m; |
Brian Salomon | b10a662 | 2020-02-20 13:36:01 -0500 | [diff] [blame] | 143 | if (fp.isSampledWithExplicitCoords()) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 144 | m = GetTransformMatrix(transform, SkMatrix::I()); |
Brian Salomon | b10a662 | 2020-02-20 13:36:01 -0500 | [diff] [blame] | 145 | } else { |
| 146 | m = GetTransformMatrix(transform, localMatrix); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 147 | } |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 148 | if (!SkMatrixPriv::CheapEqual(fInstalledTransforms[i].fCurrentValue, m)) { |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 149 | if (fInstalledTransforms[i].fType == kFloat4_GrSLType) { |
| 150 | float values[4] = {m.getScaleX(), m.getTranslateX(), |
| 151 | m.getScaleY(), m.getTranslateY()}; |
Brian Salomon | 14ed885 | 2020-03-24 10:43:38 -0400 | [diff] [blame] | 152 | SkASSERT(m.isScaleTranslate()); |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 153 | pdman.set4fv(fInstalledTransforms[i].fHandle.toIndex(), 1, values); |
| 154 | } else { |
Brian Salomon | 14ed885 | 2020-03-24 10:43:38 -0400 | [diff] [blame] | 155 | SkASSERT(!m.isScaleTranslate() || !fp.isSampledWithExplicitCoords()); |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 156 | SkASSERT(fInstalledTransforms[i].fType == kFloat3x3_GrSLType); |
| 157 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 158 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 159 | fInstalledTransforms[i].fCurrentValue = m; |
| 160 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 161 | } |
| 162 | ++i; |
| 163 | } |
| 164 | SkASSERT(i == fInstalledTransforms.count()); |
| 165 | } |
| 166 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 167 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 168 | GrGPArgs* gpArgs, |
| 169 | const char* posName) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 170 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 171 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 174 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 175 | GrGLSLUniformHandler* uniformHandler, |
| 176 | GrGPArgs* gpArgs, |
| 177 | const char* posName, |
| 178 | const SkMatrix& mat, |
| 179 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 180 | if (mat.isIdentity()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 181 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 182 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 183 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 184 | const char* viewMatrixName; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 185 | *viewMatrixUniform = uniformHandler->addUniform(nullptr, |
| 186 | kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 187 | kFloat3x3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 188 | "uViewM", |
| 189 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 190 | if (!mat.hasPerspective()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 191 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 192 | vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 193 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 194 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 195 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3"); |
| 196 | vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 197 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 198 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 199 | } |
| 200 | } |