| 1 | /* | 
|---|
| 2 | * Copyright 2015 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 |  | 
|---|
| 8 | #include "src/gpu/GrShaderCaps.h" | 
|---|
| 9 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" | 
|---|
| 10 | #include "src/gpu/glsl/GrGLSLVarying.h" | 
|---|
| 11 |  | 
|---|
| 12 | void GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::Attribute& input, | 
|---|
| 13 | const char* output, | 
|---|
| 14 | Interpolation interpolation) { | 
|---|
| 15 | SkASSERT(input.isInitialized()); | 
|---|
| 16 | SkASSERT(!fProgramBuilder->primitiveProcessor().willUseGeoShader()); | 
|---|
| 17 | GrGLSLVarying v(input.gpuType()); | 
|---|
| 18 | this->addVarying(input.name(), &v, interpolation); | 
|---|
| 19 | fProgramBuilder->fVS.codeAppendf( "%s = %s;", v.vsOut(), input.name()); | 
|---|
| 20 | fProgramBuilder->fFS.codeAppendf( "%s = %s;", output, v.fsIn()); | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | static bool use_flat_interpolation(GrGLSLVaryingHandler::Interpolation interpolation, | 
|---|
| 24 | const GrShaderCaps& shaderCaps) { | 
|---|
| 25 | switch (interpolation) { | 
|---|
| 26 | using Interpolation = GrGLSLVaryingHandler::Interpolation; | 
|---|
| 27 | case Interpolation::kInterpolated: | 
|---|
| 28 | return false; | 
|---|
| 29 | case Interpolation::kCanBeFlat: | 
|---|
| 30 | SkASSERT(!shaderCaps.preferFlatInterpolation() || | 
|---|
| 31 | shaderCaps.flatInterpolationSupport()); | 
|---|
| 32 | return shaderCaps.preferFlatInterpolation(); | 
|---|
| 33 | case Interpolation::kMustBeFlat: | 
|---|
| 34 | SkASSERT(shaderCaps.flatInterpolationSupport()); | 
|---|
| 35 | return true; | 
|---|
| 36 | } | 
|---|
| 37 | SK_ABORT( "Invalid interpolation"); | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | void GrGLSLVaryingHandler::addVarying(const char* name, GrGLSLVarying* varying, | 
|---|
| 41 | Interpolation interpolation) { | 
|---|
| 42 | SkASSERT(GrSLTypeIsFloatType(varying->type()) || Interpolation::kMustBeFlat == interpolation); | 
|---|
| 43 | bool willUseGeoShader = fProgramBuilder->primitiveProcessor().willUseGeoShader(); | 
|---|
| 44 | VaryingInfo& v = fVaryings.push_back(); | 
|---|
| 45 |  | 
|---|
| 46 | SkASSERT(varying); | 
|---|
| 47 | SkASSERT(kVoid_GrSLType != varying->fType); | 
|---|
| 48 | v.fType = varying->fType; | 
|---|
| 49 | v.fIsFlat = use_flat_interpolation(interpolation, *fProgramBuilder->shaderCaps()); | 
|---|
| 50 | fProgramBuilder->nameVariable(&v.fVsOut, 'v', name); | 
|---|
| 51 | v.fVisibility = kNone_GrShaderFlags; | 
|---|
| 52 | if (varying->isInVertexShader()) { | 
|---|
| 53 | varying->fVsOut = v.fVsOut.c_str(); | 
|---|
| 54 | v.fVisibility |= kVertex_GrShaderFlag; | 
|---|
| 55 | } | 
|---|
| 56 | if (willUseGeoShader) { | 
|---|
| 57 | fProgramBuilder->nameVariable(&v.fGsOut, 'g', name); | 
|---|
| 58 | varying->fGsIn = v.fVsOut.c_str(); | 
|---|
| 59 | varying->fGsOut = v.fGsOut.c_str(); | 
|---|
| 60 | v.fVisibility |= kGeometry_GrShaderFlag; | 
|---|
| 61 | } | 
|---|
| 62 | if (varying->isInFragmentShader()) { | 
|---|
| 63 | varying->fFsIn = (willUseGeoShader ? v.fGsOut : v.fVsOut).c_str(); | 
|---|
| 64 | v.fVisibility |= kFragment_GrShaderFlag; | 
|---|
| 65 | } | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) { | 
|---|
| 69 | for (const auto& attr : gp.vertexAttributes()) { | 
|---|
| 70 | this->addAttribute(attr.asShaderVar()); | 
|---|
| 71 | } | 
|---|
| 72 | for (const auto& attr : gp.instanceAttributes()) { | 
|---|
| 73 | this->addAttribute(attr.asShaderVar()); | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) { | 
|---|
| 78 | SkASSERT(GrShaderVar::TypeModifier::In == var.getTypeModifier()); | 
|---|
| 79 | for (const GrShaderVar& attr : fVertexInputs.items()) { | 
|---|
| 80 | // if attribute already added, don't add it again | 
|---|
| 81 | if (attr.getName().equals(var.getName())) { | 
|---|
| 82 | return; | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | fVertexInputs.push_back(var); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void GrGLSLVaryingHandler::setNoPerspective() { | 
|---|
| 89 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); | 
|---|
| 90 | if (!caps.noperspectiveInterpolationSupport()) { | 
|---|
| 91 | return; | 
|---|
| 92 | } | 
|---|
| 93 | if (const char* extension = caps.noperspectiveInterpolationExtensionString()) { | 
|---|
| 94 | int bit = 1 << GrGLSLShaderBuilder::kNoPerspectiveInterpolation_GLSLPrivateFeature; | 
|---|
| 95 | fProgramBuilder->fVS.addFeature(bit, extension); | 
|---|
| 96 | if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) { | 
|---|
| 97 | fProgramBuilder->fGS.addFeature(bit, extension); | 
|---|
| 98 | } | 
|---|
| 99 | fProgramBuilder->fFS.addFeature(bit, extension); | 
|---|
| 100 | } | 
|---|
| 101 | fDefaultInterpolationModifier = "noperspective"; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | void GrGLSLVaryingHandler::finalize() { | 
|---|
| 105 | for (const VaryingInfo& v : fVaryings.items()) { | 
|---|
| 106 | const char* modifier = v.fIsFlat ? "flat": fDefaultInterpolationModifier; | 
|---|
| 107 | if (v.fVisibility & kVertex_GrShaderFlag) { | 
|---|
| 108 | fVertexOutputs.emplace_back(v.fVsOut, v.fType, GrShaderVar::TypeModifier::Out, | 
|---|
| 109 | GrShaderVar::kNonArray, SkString(), SkString(modifier)); | 
|---|
| 110 | if (v.fVisibility & kGeometry_GrShaderFlag) { | 
|---|
| 111 | fGeomInputs.emplace_back(v.fVsOut, v.fType, GrShaderVar::TypeModifier::In, | 
|---|
| 112 | GrShaderVar::kUnsizedArray, SkString(), SkString(modifier)); | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 | if (v.fVisibility & kFragment_GrShaderFlag) { | 
|---|
| 116 | const char* fsIn = v.fVsOut.c_str(); | 
|---|
| 117 | if (v.fVisibility & kGeometry_GrShaderFlag) { | 
|---|
| 118 | fGeomOutputs.emplace_back(v.fGsOut, v.fType, GrShaderVar::TypeModifier::Out, | 
|---|
| 119 | GrShaderVar::kNonArray, SkString(), SkString(modifier)); | 
|---|
| 120 | fsIn = v.fGsOut.c_str(); | 
|---|
| 121 | } | 
|---|
| 122 | fFragInputs.emplace_back(SkString(fsIn), v.fType, GrShaderVar::TypeModifier::In, | 
|---|
| 123 | GrShaderVar::kNonArray, SkString(), SkString(modifier)); | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 | this->onFinalize(); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const { | 
|---|
| 130 | for (const GrShaderVar& varying : vars.items()) { | 
|---|
| 131 | varying.appendDecl(fProgramBuilder->shaderCaps(), out); | 
|---|
| 132 | out->append( ";"); | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const { | 
|---|
| 137 | this->appendDecls(fVertexInputs, inputDecls); | 
|---|
| 138 | this->appendDecls(fVertexOutputs, outputDecls); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDecls) const { | 
|---|
| 142 | this->appendDecls(fGeomInputs, inputDecls); | 
|---|
| 143 | this->appendDecls(fGeomOutputs, outputDecls); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDecls) const { | 
|---|
| 147 | // We should not have any outputs in the fragment shader when using version 1.10 | 
|---|
| 148 | SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->shaderCaps()->generation() || | 
|---|
| 149 | fFragOutputs.empty()); | 
|---|
| 150 | this->appendDecls(fFragInputs, inputDecls); | 
|---|
| 151 | this->appendDecls(fFragOutputs, outputDecls); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|