Line data Source code
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 :
12 0 : void GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::Attribute* input,
13 : const char* output, GrSLPrecision precision) {
14 0 : GrSLType type = GrVertexAttribTypeToSLType(input->fType);
15 0 : GrGLSLVertToFrag v(type);
16 0 : this->addVarying(input->fName, &v, precision);
17 0 : this->writePassThroughAttribute(input, output, v);
18 0 : }
19 :
20 0 : void GrGLSLVaryingHandler::addFlatPassThroughAttribute(const GrGeometryProcessor::Attribute* input,
21 : const char* output,
22 : GrSLPrecision precision) {
23 0 : GrSLType type = GrVertexAttribTypeToSLType(input->fType);
24 0 : GrGLSLVertToFrag v(type);
25 0 : this->addFlatVarying(input->fName, &v, precision);
26 0 : this->writePassThroughAttribute(input, output, v);
27 0 : }
28 :
29 0 : void GrGLSLVaryingHandler::writePassThroughAttribute(const GrGeometryProcessor::Attribute* input,
30 : const char* output, const GrGLSLVarying& v) {
31 0 : SkASSERT(!fProgramBuilder->primitiveProcessor().willUseGeoShader());
32 0 : fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName);
33 0 : fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn());
34 0 : }
35 :
36 0 : void GrGLSLVaryingHandler::internalAddVarying(const char* name,
37 : GrGLSLVarying* varying,
38 : GrSLPrecision precision,
39 : bool flat) {
40 0 : bool willUseGeoShader = fProgramBuilder->primitiveProcessor().willUseGeoShader();
41 0 : VaryingInfo& v = fVaryings.push_back();
42 :
43 0 : SkASSERT(varying);
44 0 : v.fType = varying->fType;
45 0 : v.fPrecision = (kDefault_GrSLPrecision == precision) ? kMedium_GrSLPrecision : precision;
46 0 : v.fIsFlat = flat;
47 0 : fProgramBuilder->nameVariable(&v.fVsOut, 'v', name);
48 0 : v.fVisibility = kNone_GrShaderFlags;
49 0 : if (varying->vsVarying()) {
50 0 : varying->fVsOut = v.fVsOut.c_str();
51 0 : v.fVisibility |= kVertex_GrShaderFlag;
52 : }
53 0 : if (willUseGeoShader) {
54 0 : fProgramBuilder->nameVariable(&v.fGsOut, 'g', name);
55 0 : varying->fGsIn = v.fVsOut.c_str();
56 0 : varying->fGsOut = v.fGsOut.c_str();
57 0 : v.fVisibility |= kGeometry_GrShaderFlag;
58 : }
59 0 : if (varying->fsVarying()) {
60 0 : varying->fFsIn = (willUseGeoShader ? v.fGsOut : v.fVsOut).c_str();
61 0 : v.fVisibility |= kFragment_GrShaderFlag;
62 : }
63 0 : }
64 :
65 0 : void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) {
66 0 : int vaCount = gp.numAttribs();
67 0 : for (int i = 0; i < vaCount; i++) {
68 0 : const GrGeometryProcessor::Attribute& attr = gp.getAttrib(i);
69 0 : this->addAttribute(GrShaderVar(attr.fName,
70 0 : GrVertexAttribTypeToSLType(attr.fType),
71 : GrShaderVar::kIn_TypeModifier,
72 : GrShaderVar::kNonArray,
73 0 : attr.fPrecision));
74 : }
75 0 : }
76 :
77 0 : void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) {
78 0 : SkASSERT(GrShaderVar::kIn_TypeModifier == var.getTypeModifier());
79 0 : for (int j = 0; j < fVertexInputs.count(); ++j) {
80 0 : const GrShaderVar& attr = fVertexInputs[j];
81 : // if attribute already added, don't add it again
82 0 : if (attr.getName().equals(var.getName())) {
83 0 : return;
84 : }
85 : }
86 0 : fVertexInputs.push_back(var);
87 : }
88 :
89 0 : void GrGLSLVaryingHandler::setNoPerspective() {
90 0 : const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
91 0 : if (!caps.noperspectiveInterpolationSupport()) {
92 0 : return;
93 : }
94 0 : if (const char* extension = caps.noperspectiveInterpolationExtensionString()) {
95 0 : int bit = 1 << GrGLSLFragmentBuilder::kNoPerspectiveInterpolation_GLSLPrivateFeature;
96 0 : fProgramBuilder->fVS.addFeature(bit, extension);
97 0 : if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) {
98 0 : fProgramBuilder->fGS.addFeature(bit, extension);
99 : }
100 0 : fProgramBuilder->fFS.addFeature(bit, extension);
101 : }
102 0 : fDefaultInterpolationModifier = "noperspective";
103 : }
104 :
105 0 : void GrGLSLVaryingHandler::finalize() {
106 0 : for (int i = 0; i < fVaryings.count(); ++i) {
107 0 : const VaryingInfo& v = this->fVaryings[i];
108 0 : const char* modifier = v.fIsFlat ? "flat" : fDefaultInterpolationModifier;
109 0 : if (v.fVisibility & kVertex_GrShaderFlag) {
110 0 : fVertexOutputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kOut_TypeModifier,
111 0 : v.fPrecision, nullptr, modifier);
112 0 : if (v.fVisibility & kGeometry_GrShaderFlag) {
113 0 : fGeomInputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kUnsizedArray,
114 0 : GrShaderVar::kIn_TypeModifier, v.fPrecision, nullptr,
115 0 : modifier);
116 : }
117 : }
118 0 : if (v.fVisibility & kFragment_GrShaderFlag) {
119 0 : const char* fsIn = v.fVsOut.c_str();
120 0 : if (v.fVisibility & kGeometry_GrShaderFlag) {
121 0 : fGeomOutputs.push_back().set(v.fType, v.fGsOut, GrShaderVar::kOut_TypeModifier,
122 0 : v.fPrecision, nullptr, modifier);
123 0 : fsIn = v.fGsOut.c_str();
124 : }
125 0 : fFragInputs.push_back().set(v.fType, fsIn, GrShaderVar::kIn_TypeModifier, v.fPrecision,
126 0 : nullptr, modifier);
127 : }
128 : }
129 0 : this->onFinalize();
130 0 : }
131 :
132 0 : void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const {
133 0 : for (int i = 0; i < vars.count(); ++i) {
134 0 : vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
135 0 : out->append(";");
136 : }
137 0 : }
138 :
139 0 : void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const {
140 0 : this->appendDecls(fVertexInputs, inputDecls);
141 0 : this->appendDecls(fVertexOutputs, outputDecls);
142 0 : }
143 :
144 0 : void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDecls) const {
145 0 : this->appendDecls(fGeomInputs, inputDecls);
146 0 : this->appendDecls(fGeomOutputs, outputDecls);
147 0 : }
148 :
149 0 : 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 0 : SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->shaderCaps()->generation() ||
152 : fFragOutputs.empty());
153 0 : this->appendDecls(fFragInputs, inputDecls);
154 0 : this->appendDecls(fFragOutputs, outputDecls);
155 0 : }
|