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