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