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 
addPassThroughAttribute(const GrGeometryProcessor::Attribute & input,const char * output,Interpolation interpolation)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 
use_flat_interpolation(GrGLSLVaryingHandler::Interpolation interpolation,const GrShaderCaps & shaderCaps)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 
addVarying(const char * name,GrGLSLVarying * varying,Interpolation interpolation)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 
emitAttributes(const GrGeometryProcessor & gp)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 
addAttribute(const GrShaderVar & var)77 void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) {
78     SkASSERT(GrShaderVar::kIn_TypeModifier == var.getTypeModifier());
79     for (int j = 0; j < fVertexInputs.count(); ++j) {
80         const GrShaderVar& attr = fVertexInputs[j];
81         // if attribute already added, don't add it again
82         if (attr.getName().equals(var.getName())) {
83             return;
84         }
85     }
86     fVertexInputs.push_back(var);
87 }
88 
setNoPerspective()89 void GrGLSLVaryingHandler::setNoPerspective() {
90     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
91     if (!caps.noperspectiveInterpolationSupport()) {
92         return;
93     }
94     if (const char* extension = caps.noperspectiveInterpolationExtensionString()) {
95         int bit = 1 << GrGLSLFragmentBuilder::kNoPerspectiveInterpolation_GLSLPrivateFeature;
96         fProgramBuilder->fVS.addFeature(bit, extension);
97         if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) {
98             fProgramBuilder->fGS.addFeature(bit, extension);
99         }
100         fProgramBuilder->fFS.addFeature(bit, extension);
101     }
102     fDefaultInterpolationModifier = "noperspective";
103 }
104 
finalize()105 void GrGLSLVaryingHandler::finalize() {
106     for (int i = 0; i < fVaryings.count(); ++i) {
107         const VaryingInfo& v = this->fVaryings[i];
108         const char* modifier = v.fIsFlat ? "flat" : fDefaultInterpolationModifier;
109         if (v.fVisibility & kVertex_GrShaderFlag) {
110             fVertexOutputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kOut_TypeModifier,
111                                            nullptr, modifier);
112             if (v.fVisibility & kGeometry_GrShaderFlag) {
113                 fGeomInputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kUnsizedArray,
114                                             GrShaderVar::kIn_TypeModifier, nullptr, modifier);
115             }
116         }
117         if (v.fVisibility & kFragment_GrShaderFlag) {
118             const char* fsIn = v.fVsOut.c_str();
119             if (v.fVisibility & kGeometry_GrShaderFlag) {
120                 fGeomOutputs.push_back().set(v.fType, v.fGsOut, GrShaderVar::kOut_TypeModifier,
121                                              nullptr, modifier);
122                 fsIn = v.fGsOut.c_str();
123             }
124             fFragInputs.push_back().set(v.fType, fsIn, GrShaderVar::kIn_TypeModifier, nullptr,
125                                         modifier);
126         }
127     }
128     this->onFinalize();
129 }
130 
appendDecls(const VarArray & vars,SkString * out) const131 void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const {
132     for (int i = 0; i < vars.count(); ++i) {
133         vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
134         out->append(";");
135     }
136 }
137 
getVertexDecls(SkString * inputDecls,SkString * outputDecls) const138 void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const {
139     this->appendDecls(fVertexInputs, inputDecls);
140     this->appendDecls(fVertexOutputs, outputDecls);
141 }
142 
getGeomDecls(SkString * inputDecls,SkString * outputDecls) const143 void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDecls) const {
144     this->appendDecls(fGeomInputs, inputDecls);
145     this->appendDecls(fGeomOutputs, outputDecls);
146 }
147 
getFragDecls(SkString * inputDecls,SkString * outputDecls) const148 void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDecls) const {
149     // We should not have any outputs in the fragment shader when using version 1.10
150     SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->shaderCaps()->generation() ||
151              fFragOutputs.empty());
152     this->appendDecls(fFragInputs, inputDecls);
153     this->appendDecls(fFragOutputs, outputDecls);
154 }
155