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 | SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() || |
| 20 | kFloat3_GrSLType == gpArgs.fPositionVar.getType()); |
| 21 | |
| 22 | GrGLSLVertexBuilder* vBuilder = args.fVertBuilder; |
| 23 | if (!args.fGP.willUseGeoShader()) { |
| 24 | // Emit the vertex position to the hardware in the normalized window coordinates it expects. |
| 25 | vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName, |
| 26 | gpArgs.fPositionVar.getType()); |
| 27 | } else { |
| 28 | // Since we have a geometry shader, leave the vertex position in Skia device space for now. |
| 29 | // The geometry Shader will operate in device space, and then convert the final positions to |
| 30 | // normalized hardware window coordinates under the hood, once everything else has finished. |
| 31 | vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str()); |
| 32 | if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) { |
| 33 | vBuilder->codeAppend(", 0"); |
| 34 | } |
| 35 | vBuilder->codeAppend(", 1);"); |
| 36 | } |
| 37 | |
| 38 | if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) { |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 39 | args.fVaryingHandler->setNoPerspective(); |
| 40 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 41 | } |
| 42 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 43 | void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 44 | GrGLSLVaryingHandler* varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 45 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 46 | const GrShaderVar& localCoordsVar, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 47 | const SkMatrix& localMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 48 | FPCoordTransformHandler* handler) { |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 49 | SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType())); |
| 50 | SkASSERT(2 == GrSLTypeVecLength(localCoordsVar.getType())); |
| 51 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 52 | int i = 0; |
| 53 | while (const GrCoordTransform* coordTransform = handler->nextCoordTransform()) { |
| 54 | SkString strUniName; |
| 55 | strUniName.printf("CoordTransformMatrix_%d", i); |
| 56 | GrSLType varyingType; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 57 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 58 | uint32_t type = coordTransform->getMatrix().getType(); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 59 | type |= localMatrix.getType(); |
| 60 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 61 | varyingType = SkToBool(SkMatrix::kPerspective_Mask & type) ? kFloat3_GrSLType : |
| 62 | kFloat2_GrSLType; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 63 | const char* uniName; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 64 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 65 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 66 | fInstalledTransforms.push_back().fHandle = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 67 | kFloat3x3_GrSLType, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 68 | strUniName.c_str(), |
| 69 | &uniName).toIndex(); |
| 70 | SkString strVaryingName; |
| 71 | strVaryingName.printf("TransformedCoords_%d", i); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 72 | |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 73 | GrGLSLVarying v(varyingType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 74 | varyingHandler->addVarying(strVaryingName.c_str(), &v); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 75 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 76 | SkASSERT(kFloat2_GrSLType == varyingType || kFloat3_GrSLType == varyingType); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 77 | handler->specifyCoordsForCurrCoordTransform(SkString(v.fsIn()), varyingType); |
| 78 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 79 | if (kFloat2_GrSLType == varyingType) { |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 80 | vb->codeAppendf("%s = (%s * float3(%s, 1)).xy;", v.vsOut(), uniName, |
| 81 | localCoordsVar.c_str()); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 82 | } else { |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 83 | vb->codeAppendf("%s = %s * float3(%s, 1);", v.vsOut(), uniName, localCoordsVar.c_str()); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 84 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 85 | ++i; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 89 | void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix, |
| 90 | const GrGLSLProgramDataManager& pdman, |
| 91 | FPCoordTransformIter* transformIter) { |
| 92 | int i = 0; |
| 93 | while (const GrCoordTransform* coordTransform = transformIter->next()) { |
| 94 | const SkMatrix& m = GetTransformMatrix(localMatrix, *coordTransform); |
| 95 | if (!fInstalledTransforms[i].fCurrentValue.cheapEqualTo(m)) { |
| 96 | pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m); |
| 97 | fInstalledTransforms[i].fCurrentValue = m; |
| 98 | } |
| 99 | ++i; |
| 100 | } |
| 101 | SkASSERT(i == fInstalledTransforms.count()); |
| 102 | } |
| 103 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 104 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 105 | GrGPArgs* gpArgs, |
| 106 | const char* posName) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 107 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 108 | vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 111 | void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder, |
| 112 | GrGLSLUniformHandler* uniformHandler, |
| 113 | GrGPArgs* gpArgs, |
| 114 | const char* posName, |
| 115 | const SkMatrix& mat, |
| 116 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 117 | if (mat.isIdentity()) { |
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 | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 120 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 121 | const char* viewMatrixName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 122 | *viewMatrixUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 123 | kFloat3x3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 124 | "uViewM", |
| 125 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 126 | if (!mat.hasPerspective()) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 127 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2"); |
| 128 | vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 129 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 130 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 131 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3"); |
| 132 | vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 133 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 134 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 135 | } |
| 136 | } |