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 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 70 | int i = 0; |
| 71 | while (const GrCoordTransform* coordTransform = handler->nextCoordTransform()) { |
| 72 | SkString strUniName; |
| 73 | strUniName.printf("CoordTransformMatrix_%d", i); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 74 | const char* uniName; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 75 | fInstalledTransforms.push_back().fHandle = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 76 | kFloat3x3_GrSLType, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 77 | strUniName.c_str(), |
| 78 | &uniName).toIndex(); |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 79 | GrSLType varyingType = kFloat2_GrSLType; |
Michael Ludwig | 01ecf0f | 2018-11-01 11:07:14 -0400 | [diff] [blame] | 80 | if (localMatrix.hasPerspective() || coordTransform->getMatrix().hasPerspective() |
| 81 | || threeComponentLocalCoords) { |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 82 | varyingType = kFloat3_GrSLType; |
| 83 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 84 | SkString strVaryingName; |
| 85 | strVaryingName.printf("TransformedCoords_%d", i); |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 86 | GrGLSLVarying v(varyingType); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 87 | if (coordTransform->computeInVertexShader()) { |
| 88 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 89 | |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 90 | if (kFloat2_GrSLType == varyingType) { |
| 91 | vb->codeAppendf("%s = (%s * %s).xy;", v.vsOut(), uniName, localCoords.c_str()); |
| 92 | } else { |
| 93 | vb->codeAppendf("%s = %s * %s;", v.vsOut(), uniName, localCoords.c_str()); |
| 94 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 95 | } |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 96 | handler->specifyCoordsForCurrCoordTransform(SkString(uniName), |
| 97 | fInstalledTransforms.back().fHandle, |
| 98 | GrShaderVar(SkString(v.fsIn()), varyingType)); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 99 | ++i; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 103 | void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix, |
| 104 | const GrGLSLProgramDataManager& pdman, |
| 105 | FPCoordTransformIter* transformIter) { |
| 106 | int i = 0; |
| 107 | while (const GrCoordTransform* coordTransform = transformIter->next()) { |
| 108 | const SkMatrix& m = GetTransformMatrix(localMatrix, *coordTransform); |
| 109 | if (!fInstalledTransforms[i].fCurrentValue.cheapEqualTo(m)) { |
| 110 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 111 | fInstalledTransforms[i].fCurrentValue = m; |
| 112 | } |
| 113 | ++i; |
| 114 | } |
| 115 | SkASSERT(i == fInstalledTransforms.count()); |
| 116 | } |
| 117 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 118 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 119 | GrGPArgs* gpArgs, |
| 120 | const char* posName) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 121 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 122 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 125 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 126 | GrGLSLUniformHandler* uniformHandler, |
| 127 | GrGPArgs* gpArgs, |
| 128 | const char* posName, |
| 129 | const SkMatrix& mat, |
| 130 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 131 | if (mat.isIdentity()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 132 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 133 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 134 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 135 | const char* viewMatrixName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 136 | *viewMatrixUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 137 | kFloat3x3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 138 | "uViewM", |
| 139 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 140 | if (!mat.hasPerspective()) { |
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 * float3(%s, 1)).xy;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 143 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 144 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 145 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3"); |
| 146 | vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 147 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 148 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 149 | } |
| 150 | } |