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 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 8 | #include "GrGLSLGeometryProcessor.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 10 | #include "GrCoordTransform.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLVarying.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 14 | #include "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); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 87 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 88 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 89 | handler->specifyCoordsForCurrCoordTransform(SkString(v.fsIn()), varyingType); |
| 90 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 91 | if (kFloat2_GrSLType == varyingType) { |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 92 | vb->codeAppendf("%s = (%s * %s).xy;", v.vsOut(), uniName, localCoords.c_str()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 93 | } else { |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 94 | vb->codeAppendf("%s = %s * %s;", v.vsOut(), uniName, localCoords.c_str()); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 95 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 96 | ++i; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 100 | void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix, |
| 101 | const GrGLSLProgramDataManager& pdman, |
| 102 | FPCoordTransformIter* transformIter) { |
| 103 | int i = 0; |
| 104 | while (const GrCoordTransform* coordTransform = transformIter->next()) { |
| 105 | const SkMatrix& m = GetTransformMatrix(localMatrix, *coordTransform); |
| 106 | if (!fInstalledTransforms[i].fCurrentValue.cheapEqualTo(m)) { |
| 107 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 108 | fInstalledTransforms[i].fCurrentValue = m; |
| 109 | } |
| 110 | ++i; |
| 111 | } |
| 112 | SkASSERT(i == fInstalledTransforms.count()); |
| 113 | } |
| 114 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 115 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 116 | GrGPArgs* gpArgs, |
| 117 | const char* posName) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 118 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 119 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 122 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 123 | GrGLSLUniformHandler* uniformHandler, |
| 124 | GrGPArgs* gpArgs, |
| 125 | const char* posName, |
| 126 | const SkMatrix& mat, |
| 127 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 128 | if (mat.isIdentity()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 129 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 130 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 131 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 132 | const char* viewMatrixName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 133 | *viewMatrixUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 134 | kFloat3x3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 135 | "uViewM", |
| 136 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 137 | if (!mat.hasPerspective()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 138 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 139 | vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 140 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 141 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 142 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3"); |
| 143 | vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 144 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 145 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 146 | } |
| 147 | } |