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 | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLProcessorTypes.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 13 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLVarying.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 16 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 17 | void GrGLSLGeometryProcessor::emitCode(EmitArgs& args) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 18 | GrGLSLVertexBuilder* vBuilder = args.fVertBuilder; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 19 | GrGPArgs gpArgs; |
| 20 | this->onEmitCode(args, &gpArgs); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 21 | vBuilder->transformToNormalizedDeviceSpace(gpArgs.fPositionVar); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 22 | } |
| 23 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 24 | void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 25 | GrGLSLVaryingHandler* varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 26 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 27 | const GrShaderVar& posVar, |
| 28 | const char* localCoords, |
| 29 | const SkMatrix& localMatrix, |
| 30 | const TransformsIn& tin, |
| 31 | TransformsOut* tout) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 32 | tout->push_back_n(tin.count()); |
| 33 | fInstalledTransforms.push_back_n(tin.count()); |
| 34 | for (int i = 0; i < tin.count(); i++) { |
| 35 | const ProcCoords& coordTransforms = tin[i]; |
| 36 | fInstalledTransforms[i].push_back_n(coordTransforms.count()); |
| 37 | for (int t = 0; t < coordTransforms.count(); t++) { |
| 38 | SkString strUniName("StageMatrix"); |
| 39 | strUniName.appendf("_%i_%i", i, t); |
| 40 | GrSLType varyingType; |
| 41 | |
| 42 | GrCoordSet coordType = coordTransforms[t]->sourceCoords(); |
| 43 | uint32_t type = coordTransforms[t]->getMatrix().getType(); |
| 44 | if (kLocal_GrCoordSet == coordType) { |
| 45 | type |= localMatrix.getType(); |
| 46 | } |
| 47 | varyingType = SkToBool(SkMatrix::kPerspective_Mask & type) ? kVec3f_GrSLType : |
| 48 | kVec2f_GrSLType; |
| 49 | GrSLPrecision precision = coordTransforms[t]->precision(); |
| 50 | |
| 51 | const char* uniName; |
| 52 | fInstalledTransforms[i][t].fHandle = |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 53 | uniformHandler->addUniform(GrGLSLUniformHandler::kVertex_Visibility, |
| 54 | kMat33f_GrSLType, precision, |
| 55 | strUniName.c_str(), |
| 56 | &uniName).toIndex(); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 57 | |
| 58 | SkString strVaryingName("MatrixCoord"); |
| 59 | strVaryingName.appendf("_%i_%i", i, t); |
| 60 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 61 | GrGLSLVertToFrag v(varyingType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 62 | varyingHandler->addVarying(strVaryingName.c_str(), &v, precision); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 63 | |
| 64 | SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType); |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 65 | SkNEW_APPEND_TO_TARRAY(&(*tout)[i], GrGLSLTransformedCoords, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 66 | (SkString(v.fsIn()), varyingType)); |
| 67 | |
| 68 | // varying = matrix * coords (logically) |
| 69 | if (kDevice_GrCoordSet == coordType) { |
| 70 | if (kVec2f_GrSLType == varyingType) { |
| 71 | if (kVec2f_GrSLType == posVar.getType()) { |
| 72 | vb->codeAppendf("%s = (%s * vec3(%s, 1)).xy;", |
| 73 | v.vsOut(), uniName, posVar.c_str()); |
| 74 | } else { |
| 75 | // The brackets here are just to scope the temp variable |
| 76 | vb->codeAppendf("{ vec3 temp = %s * %s;", uniName, posVar.c_str()); |
| 77 | vb->codeAppendf("%s = vec2(temp.x/temp.z, temp.y/temp.z); }", v.vsOut()); |
| 78 | } |
| 79 | } else { |
| 80 | if (kVec2f_GrSLType == posVar.getType()) { |
| 81 | vb->codeAppendf("%s = %s * vec3(%s, 1);", |
| 82 | v.vsOut(), uniName, posVar.c_str()); |
| 83 | } else { |
| 84 | vb->codeAppendf("%s = %s * %s;", v.vsOut(), uniName, posVar.c_str()); |
| 85 | } |
| 86 | } |
| 87 | } else { |
| 88 | if (kVec2f_GrSLType == varyingType) { |
| 89 | vb->codeAppendf("%s = (%s * vec3(%s, 1)).xy;", v.vsOut(), uniName, localCoords); |
| 90 | } else { |
| 91 | vb->codeAppendf("%s = %s * vec3(%s, 1);", v.vsOut(), uniName, localCoords); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 98 | void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 99 | GrGLSLVaryingHandler* varyingHandler, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 100 | const char* localCoords, |
| 101 | const TransformsIn& tin, |
| 102 | TransformsOut* tout) { |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 103 | tout->push_back_n(tin.count()); |
| 104 | for (int i = 0; i < tin.count(); i++) { |
| 105 | const ProcCoords& coordTransforms = tin[i]; |
| 106 | for (int t = 0; t < coordTransforms.count(); t++) { |
| 107 | GrSLType varyingType = kVec2f_GrSLType; |
| 108 | |
| 109 | // Device coords aren't supported |
| 110 | SkASSERT(kDevice_GrCoordSet != coordTransforms[t]->sourceCoords()); |
| 111 | GrSLPrecision precision = coordTransforms[t]->precision(); |
| 112 | |
| 113 | SkString strVaryingName("MatrixCoord"); |
| 114 | strVaryingName.appendf("_%i_%i", i, t); |
| 115 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 116 | GrGLSLVertToFrag v(varyingType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 117 | varyingHandler->addVarying(strVaryingName.c_str(), &v, precision); |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 118 | vb->codeAppendf("%s = %s;", v.vsOut(), localCoords); |
| 119 | |
| 120 | SkNEW_APPEND_TO_TARRAY(&(*tout)[i], |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 121 | GrGLSLTransformedCoords, |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 122 | (SkString(v.fsIn()), varyingType)); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 127 | void GrGLSLGeometryProcessor::setupPosition(GrGLSLVertexBuilder* vertBuilder, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 128 | GrGPArgs* gpArgs, |
| 129 | const char* posName) { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 130 | gpArgs->fPositionVar.set(kVec2f_GrSLType, "pos2"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 131 | vertBuilder->codeAppendf("vec2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 134 | void GrGLSLGeometryProcessor::setupPosition(GrGLSLVertexBuilder* vertBuilder, |
| 135 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 136 | GrGPArgs* gpArgs, |
| 137 | const char* posName, |
| 138 | const SkMatrix& mat, |
| 139 | UniformHandle* viewMatrixUniform) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 140 | if (mat.isIdentity()) { |
| 141 | gpArgs->fPositionVar.set(kVec2f_GrSLType, "pos2"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 142 | vertBuilder->codeAppendf("vec2 %s = %s;", gpArgs->fPositionVar.c_str(), posName); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 143 | } else { |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 144 | const char* viewMatrixName; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame^] | 145 | *viewMatrixUniform = uniformHandler->addUniform(GrGLSLUniformHandler::kVertex_Visibility, |
| 146 | kMat33f_GrSLType, kHigh_GrSLPrecision, |
| 147 | "uViewM", |
| 148 | &viewMatrixName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 149 | if (!mat.hasPerspective()) { |
| 150 | gpArgs->fPositionVar.set(kVec2f_GrSLType, "pos2"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 151 | vertBuilder->codeAppendf("vec2 %s = vec2(%s * vec3(%s, 1));", |
| 152 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 153 | } else { |
| 154 | gpArgs->fPositionVar.set(kVec3f_GrSLType, "pos3"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 155 | vertBuilder->codeAppendf("vec3 %s = %s * vec3(%s, 1);", |
| 156 | gpArgs->fPositionVar.c_str(), viewMatrixName, posName); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 157 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 158 | } |
| 159 | } |