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 "GrShaderCaps.h"
9 #include "glsl/GrGLSLVarying.h"
10 #include "glsl/GrGLSLProgramBuilder.h"
11
addPassThroughAttribute(const GrGeometryProcessor::Attribute * input,const char * output,GrSLPrecision precision)12 void GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::Attribute* input,
13 const char* output, GrSLPrecision precision) {
14 GrSLType type = GrVertexAttribTypeToSLType(input->fType);
15 GrGLSLVertToFrag v(type);
16 this->addVarying(input->fName, &v, precision);
17 this->writePassThroughAttribute(input, output, v);
18 }
19
addFlatPassThroughAttribute(const GrGeometryProcessor::Attribute * input,const char * output,GrSLPrecision precision)20 void GrGLSLVaryingHandler::addFlatPassThroughAttribute(const GrGeometryProcessor::Attribute* input,
21 const char* output,
22 GrSLPrecision precision) {
23 GrSLType type = GrVertexAttribTypeToSLType(input->fType);
24 GrGLSLVertToFrag v(type);
25 this->addFlatVarying(input->fName, &v, precision);
26 this->writePassThroughAttribute(input, output, v);
27 }
28
writePassThroughAttribute(const GrGeometryProcessor::Attribute * input,const char * output,const GrGLSLVarying & v)29 void GrGLSLVaryingHandler::writePassThroughAttribute(const GrGeometryProcessor::Attribute* input,
30 const char* output, const GrGLSLVarying& v) {
31 SkASSERT(!fProgramBuilder->primitiveProcessor().willUseGeoShader());
32 fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName);
33 fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn());
34 }
35
internalAddVarying(const char * name,GrGLSLVarying * varying,GrSLPrecision precision,bool flat)36 void GrGLSLVaryingHandler::internalAddVarying(const char* name,
37 GrGLSLVarying* varying,
38 GrSLPrecision precision,
39 bool flat) {
40 bool willUseGeoShader = fProgramBuilder->primitiveProcessor().willUseGeoShader();
41 VaryingInfo& v = fVaryings.push_back();
42
43 SkASSERT(varying);
44 v.fType = varying->fType;
45 v.fPrecision = (kDefault_GrSLPrecision == precision) ? kMedium_GrSLPrecision : precision;
46 v.fIsFlat = flat;
47 fProgramBuilder->nameVariable(&v.fVsOut, 'v', name);
48 v.fVisibility = kNone_GrShaderFlags;
49 if (varying->vsVarying()) {
50 varying->fVsOut = v.fVsOut.c_str();
51 v.fVisibility |= kVertex_GrShaderFlag;
52 }
53 if (willUseGeoShader) {
54 fProgramBuilder->nameVariable(&v.fGsOut, 'g', name);
55 varying->fGsIn = v.fVsOut.c_str();
56 varying->fGsOut = v.fGsOut.c_str();
57 v.fVisibility |= kGeometry_GrShaderFlag;
58 }
59 if (varying->fsVarying()) {
60 varying->fFsIn = (willUseGeoShader ? v.fGsOut : v.fVsOut).c_str();
61 v.fVisibility |= kFragment_GrShaderFlag;
62 }
63 }
64
emitAttributes(const GrGeometryProcessor & gp)65 void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) {
66 int vaCount = gp.numAttribs();
67 for (int i = 0; i < vaCount; i++) {
68 const GrGeometryProcessor::Attribute& attr = gp.getAttrib(i);
69 this->addAttribute(GrShaderVar(attr.fName,
70 GrVertexAttribTypeToSLType(attr.fType),
71 GrShaderVar::kIn_TypeModifier,
72 GrShaderVar::kNonArray,
73 attr.fPrecision));
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 v.fPrecision, nullptr, modifier);
112 if (v.fVisibility & kGeometry_GrShaderFlag) {
113 fGeomInputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kUnsizedArray,
114 GrShaderVar::kIn_TypeModifier, v.fPrecision, nullptr,
115 modifier);
116 }
117 }
118 if (v.fVisibility & kFragment_GrShaderFlag) {
119 const char* fsIn = v.fVsOut.c_str();
120 if (v.fVisibility & kGeometry_GrShaderFlag) {
121 fGeomOutputs.push_back().set(v.fType, v.fGsOut, GrShaderVar::kOut_TypeModifier,
122 v.fPrecision, nullptr, modifier);
123 fsIn = v.fGsOut.c_str();
124 }
125 fFragInputs.push_back().set(v.fType, fsIn, GrShaderVar::kIn_TypeModifier, v.fPrecision,
126 nullptr, modifier);
127 }
128 }
129 this->onFinalize();
130 }
131
appendDecls(const VarArray & vars,SkString * out) const132 void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const {
133 for (int i = 0; i < vars.count(); ++i) {
134 vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
135 out->append(";");
136 }
137 }
138
getVertexDecls(SkString * inputDecls,SkString * outputDecls) const139 void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const {
140 this->appendDecls(fVertexInputs, inputDecls);
141 this->appendDecls(fVertexOutputs, outputDecls);
142 }
143
getGeomDecls(SkString * inputDecls,SkString * outputDecls) const144 void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDecls) const {
145 this->appendDecls(fGeomInputs, inputDecls);
146 this->appendDecls(fGeomOutputs, outputDecls);
147 }
148
getFragDecls(SkString * inputDecls,SkString * outputDecls) const149 void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDecls) const {
150 // We should not have any outputs in the fragment shader when using version 1.10
151 SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->shaderCaps()->generation() ||
152 fFragOutputs.empty());
153 this->appendDecls(fFragInputs, inputDecls);
154 this->appendDecls(fFragOutputs, outputDecls);
155 }
156