GrGLSLVarying.cpp revision 0eafe79f42e3c675f3c504aed4a41abf511df2b7
10eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel/*
20eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel * Copyright 2015 Google Inc.
30eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel *
40eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel * Use of this source code is governed by a BSD-style license that can be
50eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel * found in the LICENSE file.
60eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel */
70eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
80eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel#include "glsl/GrGLSLVarying.h"
90eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
100eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel#include "glsl/GrGLSLProgramBuilder.h"
110eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
120eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::Attribute* input,
130eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                                   const char* output) {
140eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    GrSLType type = GrVertexAttribTypeToSLType(input->fType);
150eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    GrGLSLVertToFrag v(type);
160eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->addVarying(input->fName, &v);
170eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), input->fName);
180eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
190eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) {
200eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fProgramBuilder->fGS.codeAppendf("%s = %s[0];", v.gsOut(), v.gsIn());
210eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
220eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
230eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn());
240eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
250eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
260eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::addVarying(const char* name,
270eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                      GrGLSLVarying* varying,
280eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                      GrSLPrecision precision) {
290eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    SkASSERT(varying);
300eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    if (varying->vsVarying()) {
310eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        this->addVertexVarying(name, precision, varying);
320eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
330eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) {
340eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        this->addGeomVarying(name, precision, varying);
350eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
360eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    if (varying->fsVarying()) {
370eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        this->addFragVarying(precision, varying);
380eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
390eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
400eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
410eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::addVertexVarying(const char* name,
420eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                            GrSLPrecision precision,
430eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                            GrGLSLVarying* v) {
440eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fVertexOutputs.push_back();
450eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fVertexOutputs.back().setType(v->fType);
460eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fVertexOutputs.back().setTypeModifier(GrGLSLShaderVar::kVaryingOut_TypeModifier);
470eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fVertexOutputs.back().setPrecision(precision);
480eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fProgramBuilder->nameVariable(fVertexOutputs.back().accessName(), 'v', name);
490eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    v->fVsOut = fVertexOutputs.back().getName().c_str();
500eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
510eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::addGeomVarying(const char* name,
520eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                          GrSLPrecision precision,
530eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                          GrGLSLVarying* v) {
540eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    // if we have a GS take each varying in as an array
550eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    // and output as non-array.
560eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    if (v->vsVarying()) {
570eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomInputs.push_back();
580eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomInputs.back().setType(v->fType);
590eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomInputs.back().setTypeModifier(GrGLSLShaderVar::kVaryingIn_TypeModifier);
600eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomInputs.back().setPrecision(precision);
610eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomInputs.back().setUnsizedArray();
620eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        *fGeomInputs.back().accessName() = v->fVsOut;
630eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        v->fGsIn = v->fVsOut;
640eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
650eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
660eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    if (v->fsVarying()) {
670eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomOutputs.push_back();
680eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomOutputs.back().setType(v->fType);
690eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomOutputs.back().setTypeModifier(GrGLSLShaderVar::kVaryingOut_TypeModifier);
700eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fGeomOutputs.back().setPrecision(precision);
710eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        fProgramBuilder->nameVariable(fGeomOutputs.back().accessName(), 'g', name);
720eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        v->fGsOut = fGeomOutputs.back().getName().c_str();
730eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
740eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
750eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
760eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::addFragVarying(GrSLPrecision precision, GrGLSLVarying* v) {
770eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    v->fFsIn = v->fGsOut ? v->fGsOut : v->fVsOut;
780eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fFragInputs.push_back().set(v->fType,
790eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                GrGLSLShaderVar::kVaryingIn_TypeModifier,
800eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                v->fFsIn,
810eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                precision);
820eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
830eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
840eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) {
850eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    int vaCount = gp.numAttribs();
860eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    for (int i = 0; i < vaCount; i++) {
870eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        const GrGeometryProcessor::Attribute& attr = gp.getAttrib(i);
880eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        this->addAttribute(GrShaderVar(attr.fName,
890eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                       GrVertexAttribTypeToSLType(attr.fType),
900eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                       GrShaderVar::kAttribute_TypeModifier,
910eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                       GrShaderVar::kNonArray,
920eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel                                       attr.fPrecision));
930eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
940eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
950eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
960eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) {
970eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier());
980eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    for (int j = 0; j < fVertexInputs.count(); ++j) {
990eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        const GrGLSLShaderVar& attr = fVertexInputs[j];
1000eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        // if attribute already added, don't add it again
1010eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        if (attr.getName().equals(var.getName())) {
1020eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel            return;
1030eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        }
1040eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
1050eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    fVertexInputs.push_back(var);
1060eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
1070eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
1080eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const {
1090eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    for (int i = 0; i < vars.count(); ++i) {
1100eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        vars[i].appendDecl(fProgramBuilder->glslCaps(), out);
1110eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel        out->append(";");
1120eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    }
1130eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
1140eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
1150eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const {
1160eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->appendDecls(fVertexInputs, inputDecls);
1170eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->appendDecls(fVertexOutputs, outputDecls);
1180eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
1190eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
1200eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDecls) const {
1210eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->appendDecls(fGeomInputs, inputDecls);
1220eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->appendDecls(fGeomOutputs, outputDecls);
1230eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
1240eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
1250eafe79f42e3c675f3c504aed4a41abf511df2b7egdanielvoid GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDecls) const {
1260eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    // We should not have any outputs in the fragment shader when using version 1.10
1270eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->glslCaps()->generation() ||
1280eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel             fFragOutputs.empty());
1290eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->appendDecls(fFragInputs, inputDecls);
1300eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel    this->appendDecls(fFragOutputs, outputDecls);
1310eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel}
1320eafe79f42e3c675f3c504aed4a41abf511df2b7egdaniel
133