Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / ganesh / glsl / GrGLSLVarying.cpp
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/ganesh/GrShaderCaps.h"
9 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
10 #include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
11
12 void GrGLSLVaryingHandler::addPassThroughAttribute(const GrShaderVar& vsVar,
13                                                    const char* output,
14                                                    Interpolation interpolation) {
15     SkASSERT(vsVar.getType() != SkSLType::kVoid);
16     GrGLSLVarying v(vsVar.getType());
17     this->addVarying(vsVar.c_str(), &v, interpolation);
18     fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), vsVar.c_str());
19     fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn());
20 }
21
22 static bool use_flat_interpolation(GrGLSLVaryingHandler::Interpolation interpolation,
23                                    const GrShaderCaps& shaderCaps) {
24     switch (interpolation) {
25         using Interpolation = GrGLSLVaryingHandler::Interpolation;
26         case Interpolation::kInterpolated:
27             return false;
28         case Interpolation::kCanBeFlat:
29             SkASSERT(!shaderCaps.preferFlatInterpolation() ||
30                      shaderCaps.flatInterpolationSupport());
31             return shaderCaps.preferFlatInterpolation();
32         case Interpolation::kMustBeFlat:
33             SkASSERT(shaderCaps.flatInterpolationSupport());
34             return true;
35     }
36     SK_ABORT("Invalid interpolation");
37 }
38
39 void GrGLSLVaryingHandler::addVarying(const char* name, GrGLSLVarying* varying,
40                                       Interpolation interpolation) {
41     SkASSERT(SkSLTypeIsFloatType(varying->type()) || Interpolation::kMustBeFlat == interpolation);
42     VaryingInfo& v = fVaryings.push_back();
43
44     SkASSERT(varying);
45     SkASSERT(SkSLType::kVoid != varying->fType);
46     v.fType = varying->fType;
47     v.fIsFlat = use_flat_interpolation(interpolation, *fProgramBuilder->shaderCaps());
48     v.fVsOut = fProgramBuilder->nameVariable('v', name);
49     v.fVisibility = kNone_GrShaderFlags;
50     if (varying->isInVertexShader()) {
51         varying->fVsOut = v.fVsOut.c_str();
52         v.fVisibility |= kVertex_GrShaderFlag;
53     }
54     if (varying->isInFragmentShader()) {
55         varying->fFsIn = v.fVsOut.c_str();
56         v.fVisibility |= kFragment_GrShaderFlag;
57     }
58 }
59
60 void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) {
61     for (auto attr : gp.vertexAttributes()) {
62         this->addAttribute(attr.asShaderVar());
63     }
64     for (auto attr : gp.instanceAttributes()) {
65         this->addAttribute(attr.asShaderVar());
66     }
67 }
68
69 void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) {
70     SkASSERT(GrShaderVar::TypeModifier::In == var.getTypeModifier());
71     for (const GrShaderVar& attr : fVertexInputs.items()) {
72         // if attribute already added, don't add it again
73         if (attr.getName().equals(var.getName())) {
74             return;
75         }
76     }
77     fVertexInputs.push_back(var);
78 }
79
80 void GrGLSLVaryingHandler::setNoPerspective() {
81     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
82     if (!caps.noperspectiveInterpolationSupport()) {
83         return;
84     }
85     if (const char* extension = caps.noperspectiveInterpolationExtensionString()) {
86         int bit = 1 << GrGLSLShaderBuilder::kNoPerspectiveInterpolation_GLSLPrivateFeature;
87         fProgramBuilder->fVS.addFeature(bit, extension);
88         fProgramBuilder->fFS.addFeature(bit, extension);
89     }
90     fDefaultInterpolationModifier = "noperspective";
91 }
92
93 void GrGLSLVaryingHandler::finalize() {
94     for (const VaryingInfo& v : fVaryings.items()) {
95         const char* modifier = v.fIsFlat ? "flat" : fDefaultInterpolationModifier;
96         if (v.fVisibility & kVertex_GrShaderFlag) {
97             fVertexOutputs.emplace_back(v.fVsOut, v.fType, GrShaderVar::TypeModifier::Out,
98                                         GrShaderVar::kNonArray, SkString(), SkString(modifier));
99         }
100         if (v.fVisibility & kFragment_GrShaderFlag) {
101             const char* fsIn = v.fVsOut.c_str();
102             fFragInputs.emplace_back(SkString(fsIn), v.fType, GrShaderVar::TypeModifier::In,
103                                      GrShaderVar::kNonArray, SkString(), SkString(modifier));
104         }
105     }
106     this->onFinalize();
107 }
108
109 void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const {
110     for (const GrShaderVar& varying : vars.items()) {
111         varying.appendDecl(fProgramBuilder->shaderCaps(), out);
112         out->append(";");
113     }
114 }
115
116 void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const {
117     this->appendDecls(fVertexInputs, inputDecls);
118     this->appendDecls(fVertexOutputs, outputDecls);
119 }
120
121 void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDecls) const {
122     // We should not have any outputs in the fragment shader when using version 1.10
123     SkASSERT(SkSL::GLSLGeneration::k110 != fProgramBuilder->shaderCaps()->generation() ||
124              fFragOutputs.empty());
125     this->appendDecls(fFragInputs, inputDecls);
126     this->appendDecls(fFragOutputs, outputDecls);
127 }