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 | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 65 | SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType())); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 66 | SkASSERT(2 == GrSLTypeVecLength(localCoordsVar.getType()) || |
| 67 | 3 == GrSLTypeVecLength(localCoordsVar.getType())); |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 68 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 69 | bool threeComponentLocalCoords = 3 == GrSLTypeVecLength(localCoordsVar.getType()); |
| 70 | SkString localCoords; |
| 71 | if (threeComponentLocalCoords) { |
| 72 | localCoords = localCoordsVar.getName(); |
| 73 | } else { |
| 74 | localCoords.printf("float3(%s, 1)", localCoordsVar.c_str()); |
| 75 | } |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 76 | for (int i = 0; *handler; ++*handler, ++i) { |
| 77 | auto [coordTransform, fp] = handler->get(); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 78 | if (coordTransform.isNoOp() && !fp.coordTransformsApplyToLocalCoords()) { |
| 79 | handler->omitCoordsForCurrCoordTransform(); |
| 80 | fInstalledTransforms.push_back(); |
| 81 | } else { |
| 82 | SkString strUniName; |
| 83 | strUniName.printf("CoordTransformMatrix_%d", i); |
| 84 | const char* uniName; |
| 85 | fInstalledTransforms.push_back().fHandle = uniformHandler |
| 86 | ->addUniform(kVertex_GrShaderFlag, |
| 87 | kFloat3x3_GrSLType, |
| 88 | strUniName.c_str(), |
| 89 | &uniName) |
| 90 | .toIndex(); |
| 91 | GrSLType varyingType = kFloat2_GrSLType; |
| 92 | if (localMatrix.hasPerspective() || coordTransform.matrix().hasPerspective() || |
| 93 | threeComponentLocalCoords) { |
| 94 | varyingType = kFloat3_GrSLType; |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 95 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 96 | SkString strVaryingName; |
| 97 | strVaryingName.printf("TransformedCoords_%d", i); |
| 98 | GrGLSLVarying v(varyingType); |
| 99 | if (fp.coordTransformsApplyToLocalCoords()) { |
| 100 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
| 101 | |
| 102 | if (kFloat2_GrSLType == varyingType) { |
| 103 | vb->codeAppendf("%s = (%s * %s).xy;", v.vsOut(), uniName, localCoords.c_str()); |
| 104 | } else { |
| 105 | vb->codeAppendf("%s = %s * %s;", v.vsOut(), uniName, localCoords.c_str()); |
| 106 | } |
| 107 | } |
| 108 | handler->specifyCoordsForCurrCoordTransform( |
| 109 | SkString(uniName), |
| 110 | fInstalledTransforms.back().fHandle, |
| 111 | GrShaderVar(SkString(v.fsIn()), varyingType)); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 116 | void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix, |
| 117 | const GrGLSLProgramDataManager& pdman, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 118 | const CoordTransformRange& transformRange) { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 119 | int i = 0; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 120 | for (auto [transform, fp] : transformRange) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 121 | if (fInstalledTransforms[i].fHandle.isValid()) { |
| 122 | SkMatrix m; |
| 123 | if (fp.coordTransformsApplyToLocalCoords()) { |
| 124 | m = GetTransformMatrix(transform, localMatrix); |
| 125 | } else { |
| 126 | m = GetTransformMatrix(transform, SkMatrix::I()); |
| 127 | } |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 128 | if (!SkMatrixPriv::CheapEqual(fInstalledTransforms[i].fCurrentValue, m)) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 129 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 130 | fInstalledTransforms[i].fCurrentValue = m; |
| 131 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 132 | } |
| 133 | ++i; |
| 134 | } |
| 135 | SkASSERT(i == fInstalledTransforms.count()); |
| 136 | } |
| 137 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 138 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 139 | GrGPArgs* gpArgs, |
| 140 | const char* posName) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 141 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 142 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 145 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 146 | GrGLSLUniformHandler* uniformHandler, |
| 147 | GrGPArgs* gpArgs, |
| 148 | const char* posName, |
| 149 | const SkMatrix& mat, |
| 150 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 151 | if (mat.isIdentity()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 152 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 153 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 154 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 155 | const char* viewMatrixName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 156 | *viewMatrixUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 157 | kFloat3x3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 158 | "uViewM", |
| 159 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 160 | if (!mat.hasPerspective()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 161 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 162 | vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 163 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 164 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 165 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3"); |
| 166 | vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 167 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 168 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 169 | } |
| 170 | } |