Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "glsl/GrGLSLVarying.h" | 8 #include "glsl/GrGLSLVarying.h" |
| 9 | 9 |
| 10 #include "glsl/GrGLSLProgramBuilder.h" | 10 #include "glsl/GrGLSLProgramBuilder.h" |
| 11 | 11 |
| 12 void GrGLSLVaryingHandler::setNoPerspective() { | |
| 13 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); | |
| 14 if (!caps.noperspectiveInterpolationSupport()) { | |
| 15 return; | |
| 16 } | |
| 17 if (fNoPerspective) { | |
| 18 return; // This method has already been called previously. | |
| 19 } | |
| 20 if (const char* extension = caps.noperspectiveInterpolationExtensionString() ) { | |
| 21 int bit = 1 << GrGLSLFragmentShaderBuilder::kNoPerspectiveInterpolation_ GLSLPrivateFeature; | |
| 22 fProgramBuilder->fVS.addFeature(bit, extension); | |
| 23 fProgramBuilder->fGS.addFeature(bit, extension); | |
| 24 fProgramBuilder->fFS.addFeature(bit, extension); | |
| 25 } | |
| 26 for (int i = 0; i < fVaryingShaderVars.count(); ++i) { | |
| 27 if (!fVaryingShaderVars[i].fIsFlat) { | |
| 28 fVaryingShaderVars[i].addModifier("noperspective"); | |
|
egdaniel
2016/02/10 14:23:44
I recently added a finalize method to GrGLSLVaryin
Chris Dalton
2016/02/10 15:29:30
Ok that sounds great. onFinalize does sound like a
| |
| 29 } | |
| 30 } | |
| 31 fNoPerspective = true; | |
| 32 } | |
| 33 | |
| 12 void GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::At tribute* input, | 34 void GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::At tribute* input, |
| 13 const char* output) { | 35 const char* output, GrSLPreci sion precision) { |
| 14 GrSLType type = GrVertexAttribTypeToSLType(input->fType); | 36 GrSLType type = GrVertexAttribTypeToSLType(input->fType); |
| 15 GrGLSLVertToFrag v(type); | 37 GrGLSLVertToFrag v(type); |
| 16 this->addVarying(input->fName, &v); | 38 this->addVarying(input->fName, &v, precision); |
| 39 this->writePassThroughAttribute(input, output, v); | |
| 40 } | |
| 41 | |
| 42 void GrGLSLVaryingHandler::addFlatPassThroughAttribute(const GrGeometryProcessor ::Attribute* input, | |
| 43 const char* output, | |
| 44 GrSLPrecision precision) { | |
| 45 GrSLType type = GrVertexAttribTypeToSLType(input->fType); | |
| 46 GrGLSLVertToFrag v(type); | |
| 47 this->addFlatVarying(input->fName, &v, precision); | |
| 48 this->writePassThroughAttribute(input, output, v); | |
| 49 } | |
| 50 | |
| 51 void GrGLSLVaryingHandler::writePassThroughAttribute(const GrGeometryProcessor:: Attribute* input, | |
| 52 const char* output, const G rGLSLVarying& v) { | |
| 17 fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName); | 53 fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName); |
| 18 | 54 |
| 19 if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) { | 55 if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) { |
| 20 fProgramBuilder->fGS.codeAppendf("%s = %s[0];", v.gsOut(), v.gsIn()); | 56 fProgramBuilder->fGS.codeAppendf("%s = %s[0];", v.gsOut(), v.gsIn()); |
| 21 } | 57 } |
| 22 | 58 |
| 23 fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn()); | 59 fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn()); |
| 24 } | 60 } |
| 25 | 61 |
| 26 void GrGLSLVaryingHandler::addVarying(const char* name, | 62 void GrGLSLVaryingHandler::internalAddVarying(const char* name, |
| 27 GrGLSLVarying* varying, | 63 GrGLSLVarying* varying, |
| 28 GrSLPrecision precision) { | 64 GrSLPrecision precision, |
| 65 bool flat) { | |
| 29 SkASSERT(varying); | 66 SkASSERT(varying); |
| 67 VaryingShaderVars& v = fVaryingShaderVars.push_back(); | |
| 68 | |
| 30 if (varying->vsVarying()) { | 69 if (varying->vsVarying()) { |
| 31 this->addVertexVarying(name, precision, varying); | 70 v.fVertexOutput.setType(varying->fType); |
| 32 } | 71 v.fVertexOutput.setTypeModifier(GrGLSLShaderVar::kVaryingOut_TypeModifie r); |
| 33 if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) { | 72 v.fVertexOutput.setPrecision(precision); |
| 34 this->addGeomVarying(name, precision, varying); | 73 fProgramBuilder->nameVariable(v.fVertexOutput.accessName(), 'v', name); |
| 35 } | 74 varying->fVsOut = v.fVertexOutput.getName().c_str(); |
| 36 if (varying->fsVarying()) { | |
| 37 this->addFragVarying(precision, varying); | |
| 38 } | |
| 39 } | |
| 40 | 75 |
| 41 void GrGLSLVaryingHandler::addVertexVarying(const char* name, | 76 if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) { |
| 42 GrSLPrecision precision, | 77 v.fGeomInput.setType(varying->fType); |
| 43 GrGLSLVarying* v) { | 78 v.fGeomInput.setTypeModifier(GrGLSLShaderVar::kVaryingIn_TypeModifie r); |
| 44 fVertexOutputs.push_back(); | 79 v.fGeomInput.setPrecision(precision); |
| 45 fVertexOutputs.back().setType(v->fType); | 80 v.fGeomInput.setUnsizedArray(); |
| 46 fVertexOutputs.back().setTypeModifier(GrGLSLShaderVar::kVaryingOut_TypeModif ier); | 81 *v.fGeomInput.accessName() = varying->fVsOut; |
| 47 fVertexOutputs.back().setPrecision(precision); | 82 varying->fGsIn = varying->fVsOut; |
| 48 fProgramBuilder->nameVariable(fVertexOutputs.back().accessName(), 'v', name) ; | 83 } |
| 49 v->fVsOut = fVertexOutputs.back().getName().c_str(); | |
| 50 } | |
| 51 void GrGLSLVaryingHandler::addGeomVarying(const char* name, | |
| 52 GrSLPrecision precision, | |
| 53 GrGLSLVarying* v) { | |
| 54 // if we have a GS take each varying in as an array | |
| 55 // and output as non-array. | |
| 56 if (v->vsVarying()) { | |
| 57 fGeomInputs.push_back(); | |
| 58 fGeomInputs.back().setType(v->fType); | |
| 59 fGeomInputs.back().setTypeModifier(GrGLSLShaderVar::kVaryingIn_TypeModif ier); | |
| 60 fGeomInputs.back().setPrecision(precision); | |
| 61 fGeomInputs.back().setUnsizedArray(); | |
| 62 *fGeomInputs.back().accessName() = v->fVsOut; | |
| 63 v->fGsIn = v->fVsOut; | |
| 64 } | 84 } |
| 65 | 85 |
| 66 if (v->fsVarying()) { | 86 if (varying->fsVarying()) { |
| 67 fGeomOutputs.push_back(); | 87 if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) { |
| 68 fGeomOutputs.back().setType(v->fType); | 88 v.fGeomOutput.setType(varying->fType); |
| 69 fGeomOutputs.back().setTypeModifier(GrGLSLShaderVar::kVaryingOut_TypeMod ifier); | 89 v.fGeomOutput.setTypeModifier(GrGLSLShaderVar::kVaryingOut_TypeModif ier); |
| 70 fGeomOutputs.back().setPrecision(precision); | 90 v.fGeomOutput.setPrecision(precision); |
| 71 fProgramBuilder->nameVariable(fGeomOutputs.back().accessName(), 'g', nam e); | 91 fProgramBuilder->nameVariable(v.fGeomOutput.accessName(), 'g', name) ; |
| 72 v->fGsOut = fGeomOutputs.back().getName().c_str(); | 92 varying->fFsIn = varying->fGsOut = v.fGeomOutput.getName().c_str(); |
| 93 } else { | |
| 94 varying->fFsIn = varying->fVsOut; | |
| 95 } | |
| 96 | |
| 97 v.fFragInput.set(varying->fType, | |
| 98 GrGLSLShaderVar::kVaryingIn_TypeModifier, | |
| 99 varying->fFsIn, | |
| 100 precision); | |
| 73 } | 101 } |
| 74 } | |
| 75 | 102 |
| 76 void GrGLSLVaryingHandler::addFragVarying(GrSLPrecision precision, GrGLSLVarying * v) { | 103 v.fIsFlat = flat; |
| 77 v->fFsIn = v->fGsOut ? v->fGsOut : v->fVsOut; | 104 if (v.fIsFlat) { |
| 78 fFragInputs.push_back().set(v->fType, | 105 v.addModifier("flat"); |
| 79 GrGLSLShaderVar::kVaryingIn_TypeModifier, | 106 } else if (fNoPerspective) { |
| 80 v->fFsIn, | 107 v.addModifier("noperspective"); |
| 81 precision); | 108 } |
| 82 } | 109 } |
| 83 | 110 |
| 84 void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) { | 111 void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) { |
| 85 int vaCount = gp.numAttribs(); | 112 int vaCount = gp.numAttribs(); |
| 86 for (int i = 0; i < vaCount; i++) { | 113 for (int i = 0; i < vaCount; i++) { |
| 87 const GrGeometryProcessor::Attribute& attr = gp.getAttrib(i); | 114 const GrGeometryProcessor::Attribute& attr = gp.getAttrib(i); |
| 88 this->addAttribute(GrShaderVar(attr.fName, | 115 this->addAttribute(GrShaderVar(attr.fName, |
| 89 GrVertexAttribTypeToSLType(attr.fType), | 116 GrVertexAttribTypeToSLType(attr.fType), |
| 90 GrShaderVar::kAttribute_TypeModifier, | 117 GrShaderVar::kAttribute_TypeModifier, |
| 91 GrShaderVar::kNonArray, | 118 GrShaderVar::kNonArray, |
| 92 attr.fPrecision)); | 119 attr.fPrecision)); |
| 93 } | 120 } |
| 94 } | 121 } |
| 95 | 122 |
| 96 void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) { | 123 void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) { |
| 97 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier()); | 124 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier()); |
| 98 for (int j = 0; j < fVertexInputs.count(); ++j) { | 125 for (int j = 0; j < fVertexInputs.count(); ++j) { |
| 99 const GrGLSLShaderVar& attr = fVertexInputs[j]; | 126 const GrGLSLShaderVar& attr = fVertexInputs[j]; |
| 100 // if attribute already added, don't add it again | 127 // if attribute already added, don't add it again |
| 101 if (attr.getName().equals(var.getName())) { | 128 if (attr.getName().equals(var.getName())) { |
| 102 return; | 129 return; |
| 103 } | 130 } |
| 104 } | 131 } |
| 105 fVertexInputs.push_back(var); | 132 fVertexInputs.push_back(var); |
| 106 } | 133 } |
| 107 | 134 |
| 108 void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) cons t { | 135 void GrGLSLVaryingHandler::appendDecls(const ShaderVarList& vars, SkString* out) const { |
| 109 for (int i = 0; i < vars.count(); ++i) { | 136 for (int i = 0; i < vars.count(); ++i) { |
| 110 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); | 137 vars[i].appendDecl(fProgramBuilder->glslCaps(), out); |
| 111 out->append(";"); | 138 out->append(";"); |
| 112 } | 139 } |
| 113 } | 140 } |
| 114 | 141 |
| 115 void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* output Decls) const { | 142 void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* output Decls) const { |
| 116 this->appendDecls(fVertexInputs, inputDecls); | 143 this->appendDecls(fVertexInputs, inputDecls); |
| 117 this->appendDecls(fVertexOutputs, outputDecls); | 144 for (int i = 0; i < fVaryingShaderVars.count(); ++i) { |
| 145 fVaryingShaderVars[i].fVertexOutput.appendDecl(fProgramBuilder->glslCaps (), outputDecls); | |
| 146 outputDecls->append(";"); | |
| 147 } | |
| 118 } | 148 } |
| 119 | 149 |
| 120 void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDe cls) const { | 150 void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDe cls) const { |
| 121 this->appendDecls(fGeomInputs, inputDecls); | 151 for (int i = 0; i < fVaryingShaderVars.count(); ++i) { |
| 122 this->appendDecls(fGeomOutputs, outputDecls); | 152 fVaryingShaderVars[i].fGeomInput.appendDecl(fProgramBuilder->glslCaps(), outputDecls); |
| 153 outputDecls->append(";"); | |
| 154 fVaryingShaderVars[i].fGeomOutput.appendDecl(fProgramBuilder->glslCaps() , outputDecls); | |
| 155 outputDecls->append(";"); | |
| 156 } | |
| 123 } | 157 } |
| 124 | 158 |
| 125 void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDe cls) const { | 159 void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDe cls) const { |
| 126 // We should not have any outputs in the fragment shader when using version 1.10 | 160 // We should not have any outputs in the fragment shader when using version 1.10 |
| 127 SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation() || | 161 SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation() || |
| 128 fFragOutputs.empty()); | 162 fFragOutputs.empty()); |
| 129 this->appendDecls(fFragInputs, inputDecls); | 163 for (int i = 0; i < fVaryingShaderVars.count(); ++i) { |
| 164 fVaryingShaderVars[i].fFragInput.appendDecl(fProgramBuilder->glslCaps(), outputDecls); | |
| 165 outputDecls->append(";"); | |
| 166 } | |
| 130 this->appendDecls(fFragOutputs, outputDecls); | 167 this->appendDecls(fFragOutputs, outputDecls); |
| 131 } | 168 } |
| 132 | 169 |
| OLD | NEW |