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 | |
| 20 | GrGLSLVertexBuilder* vBuilder = args.fVertBuilder; |
| 21 | if (!args.fGP.willUseGeoShader()) { |
| 22 | // 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] | 23 | SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() || |
| 24 | kFloat3_GrSLType == gpArgs.fPositionVar.getType()); |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 25 | vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName, |
| 26 | gpArgs.fPositionVar.getType()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 27 | if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) { |
| 28 | args.fVaryingHandler->setNoPerspective(); |
| 29 | } |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 30 | } else { |
| 31 | // Since we have a geometry shader, leave the vertex position in Skia device space for now. |
| 32 | // The geometry Shader will operate in device space, and then convert the final positions to |
| 33 | // normalized hardware window coordinates under the hood, once everything else has finished. |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 34 | // The subclass must call setNoPerspective on the varying handler, if applicable. |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 35 | vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 36 | switch (gpArgs.fPositionVar.getType()) { |
| 37 | case kFloat_GrSLType: |
| 38 | vBuilder->codeAppend(", 0"); // fallthru. |
| 39 | case kFloat2_GrSLType: |
| 40 | vBuilder->codeAppend(", 0"); // fallthru. |
| 41 | case kFloat3_GrSLType: |
| 42 | vBuilder->codeAppend(", 1"); // fallthru. |
| 43 | case kFloat4_GrSLType: |
| 44 | vBuilder->codeAppend(");"); |
| 45 | break; |
| 46 | default: |
| 47 | SK_ABORT("Invalid position var type"); |
| 48 | break; |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 49 | } |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 50 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 51 | } |
| 52 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 53 | void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 54 | GrGLSLVaryingHandler* varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 55 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 56 | const GrShaderVar& localCoordsVar, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 57 | const SkMatrix& localMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 58 | FPCoordTransformHandler* handler) { |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 59 | SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType())); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 60 | SkASSERT(2 == GrSLTypeVecLength(localCoordsVar.getType()) || |
| 61 | 3 == GrSLTypeVecLength(localCoordsVar.getType())); |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 62 | |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 63 | bool threeComponentLocalCoords = 3 == GrSLTypeVecLength(localCoordsVar.getType()); |
| 64 | SkString localCoords; |
| 65 | if (threeComponentLocalCoords) { |
| 66 | localCoords = localCoordsVar.getName(); |
| 67 | } else { |
| 68 | localCoords.printf("float3(%s, 1)", localCoordsVar.c_str()); |
| 69 | } |
Brian Salomon | 7d8b397 | 2019-11-26 22:34:44 -0500 | [diff] [blame] | 70 | for (int i = 0; *handler; ++*handler, ++i) { |
| 71 | auto [coordTransform, fp] = handler->get(); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 72 | if (coordTransform.isNoOp() && !fp.coordTransformsApplyToLocalCoords()) { |
| 73 | handler->omitCoordsForCurrCoordTransform(); |
| 74 | fInstalledTransforms.push_back(); |
| 75 | } else { |
| 76 | SkString strUniName; |
| 77 | strUniName.printf("CoordTransformMatrix_%d", i); |
| 78 | const char* uniName; |
| 79 | fInstalledTransforms.push_back().fHandle = uniformHandler |
| 80 | ->addUniform(kVertex_GrShaderFlag, |
| 81 | kFloat3x3_GrSLType, |
| 82 | strUniName.c_str(), |
| 83 | &uniName) |
| 84 | .toIndex(); |
| 85 | GrSLType varyingType = kFloat2_GrSLType; |
| 86 | if (localMatrix.hasPerspective() || coordTransform.matrix().hasPerspective() || |
| 87 | threeComponentLocalCoords) { |
| 88 | varyingType = kFloat3_GrSLType; |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 89 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 90 | SkString strVaryingName; |
| 91 | strVaryingName.printf("TransformedCoords_%d", i); |
| 92 | GrGLSLVarying v(varyingType); |
| 93 | if (fp.coordTransformsApplyToLocalCoords()) { |
| 94 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
| 95 | |
| 96 | if (kFloat2_GrSLType == varyingType) { |
| 97 | vb->codeAppendf("%s = (%s * %s).xy;", v.vsOut(), uniName, localCoords.c_str()); |
| 98 | } else { |
| 99 | vb->codeAppendf("%s = %s * %s;", v.vsOut(), uniName, localCoords.c_str()); |
| 100 | } |
| 101 | } |
| 102 | handler->specifyCoordsForCurrCoordTransform( |
| 103 | SkString(uniName), |
| 104 | fInstalledTransforms.back().fHandle, |
| 105 | GrShaderVar(SkString(v.fsIn()), varyingType)); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 110 | void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix, |
| 111 | const GrGLSLProgramDataManager& pdman, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 112 | const CoordTransformRange& transformRange) { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 113 | int i = 0; |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 114 | for (auto [transform, fp] : transformRange) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 115 | if (fInstalledTransforms[i].fHandle.isValid()) { |
| 116 | SkMatrix m; |
| 117 | if (fp.coordTransformsApplyToLocalCoords()) { |
| 118 | m = GetTransformMatrix(transform, localMatrix); |
| 119 | } else { |
| 120 | m = GetTransformMatrix(transform, SkMatrix::I()); |
| 121 | } |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame^] | 122 | if (!SkMatrixPriv::CheapEqual(fInstalledTransforms[i].fCurrentValue, m)) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 123 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 124 | fInstalledTransforms[i].fCurrentValue = m; |
| 125 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 126 | } |
| 127 | ++i; |
| 128 | } |
| 129 | SkASSERT(i == fInstalledTransforms.count()); |
| 130 | } |
| 131 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 132 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 133 | GrGPArgs* gpArgs, |
| 134 | const char* posName) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 135 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 136 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 139 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 140 | GrGLSLUniformHandler* uniformHandler, |
| 141 | GrGPArgs* gpArgs, |
| 142 | const char* posName, |
| 143 | const SkMatrix& mat, |
| 144 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 145 | if (mat.isIdentity()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 146 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 147 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 148 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 149 | const char* viewMatrixName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 150 | *viewMatrixUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 151 | kFloat3x3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 152 | "uViewM", |
| 153 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 154 | if (!mat.hasPerspective()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 155 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 156 | vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 157 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 158 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 159 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3"); |
| 160 | vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 161 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 162 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 163 | } |
| 164 | } |