aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/builders/GrGLVertexShaderBuilder.cpp
blob: 7af5ce9843af40507f694b314b3b7d3db50671af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrGLVertexShaderBuilder.h"
#include "GrGLProgramBuilder.h"
#include "GrGLShaderStringBuilder.h"
#include "../GrGpuGL.h"

#define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X)
#define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(), R, X)

static const char* color_attribute_name() { return "inColor"; }
static const char* coverage_attribute_name() { return "inCoverage"; }

GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program)
    : INHERITED(program)
    , fPositionVar(NULL)
    , fLocalCoordsVar(NULL)
    , fRtAdjustName(NULL)
    , fEffectAttribOffset(0) {
}

void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) {
    fOutputs.push_back();
    fOutputs.back().setType(v->fType);
    fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier);
    fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name);
    v->fVsOut = fOutputs.back().getName().c_str();
}

void GrGLVertexBuilder::setupUniformViewMatrix() {
    fProgramBuilder->fUniformHandles.fViewMatrixUni =
            fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility,
                                        kMat33f_GrSLType,
                                        this->uViewM());
}

void GrGLVertexBuilder::setupPositionAndLocalCoords() {
    // Setup position
    this->codeAppendf("vec3 %s;", this->glPosition());

    // setup position and local coords attribute
    fPositionVar = &fInputs.push_back();
    fPositionVar->set(kVec2f_GrSLType,
                      GrGLShaderVar::kAttribute_TypeModifier,
                      this->inPosition());
    if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) {
        fLocalCoordsVar = &fInputs.push_back();
        fLocalCoordsVar->set(kVec2f_GrSLType,
                             GrGLShaderVar::kAttribute_TypeModifier,
                             "inLocalCoords");
    } else {
        fLocalCoordsVar = fPositionVar;
    }
    fEffectAttribOffset = fInputs.count();
}

void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLExpr1* out) {
    GrGLVertToFrag v(kFloat_GrSLType);
    fProgramBuilder->addVarying(inName, &v);
    SkString name(inName);
    name.prepend("in");
    this->addAttribute(GrShaderVar(name.c_str(),
                                   kFloat_GrSLType,
                                   GrShaderVar::kAttribute_TypeModifier));
    this->codeAppendf("%s = %s;", v.vsOut(), name.c_str());
    *out = v.fsIn();
    fEffectAttribOffset++;
}

void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLExpr4* out) {
    GrGLVertToFrag v(kVec4f_GrSLType);
    fProgramBuilder->addVarying(inName, &v);
    SkString name(inName);
    name.prepend("in");
    this->addAttribute(GrShaderVar(name.c_str(),
                                   kVec4f_GrSLType,
                                   GrShaderVar::kAttribute_TypeModifier));
    this->codeAppendf("%s = %s;", v.vsOut(), name.c_str());
    *out = v.fsIn();
    fEffectAttribOffset++;
}

void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) {
    const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs();
    int numAttributes = vars.count();
    for (int a = 0; a < numAttributes; ++a) {
        this->addAttribute(vars[a]);
    }
}

void GrGLVertexBuilder::transformToNormalizedDeviceSpace() {
    // setup RT Uniform
    fProgramBuilder->fUniformHandles.fRTAdjustmentUni =
            fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility,
                                        kVec4f_GrSLType,
                                        fProgramBuilder->rtAdjustment(),
                                        &fRtAdjustName);
    // Wire transforms
    SkTArray<GrGLProgramBuilder::TransformVarying, true>& transVs = fProgramBuilder->fCoordVaryings;
    int transformCount = transVs.count();
    for (int i = 0; i < transformCount; i++) {
        const char* coords = transVs[i].fSourceCoords.c_str();

        // varying = matrix * coords (logically)
        const GrGLVarying& v = transVs[i].fV;
        if (kVec2f_GrSLType == v.fType) {
            this->codeAppendf("%s = (%s * vec3(%s, 1)).xy;", v.fVsOut, transVs[i].fUniName.c_str(),
                              coords);
        } else {
            this->codeAppendf("%s = %s * vec3(%s, 1);", v.fVsOut, transVs[i].fUniName.c_str(),
                              coords);
        }
    }

    // Transform from Skia's device coords to GL's normalized device coords.
    this->codeAppendf("gl_Position = vec4(dot(%s.xz, %s.xy), dot(%s.yz, %s.zw), 0, %s.z);",
                      this->glPosition(), fRtAdjustName, this->glPosition(), fRtAdjustName,
                      this->glPosition());
}

void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) {
    // Bind the attrib locations to same values for all shaders
    const GrProgramDesc::KeyHeader& header = fProgramBuilder->header();
    SkASSERT(-1 != header.fPositionAttributeIndex);
    GL_CALL(BindAttribLocation(programID,
                               header.fPositionAttributeIndex,
                               fPositionVar->c_str()));
    if (-1 != header.fLocalCoordAttributeIndex) {
        GL_CALL(BindAttribLocation(programID,
                                   header.fLocalCoordAttributeIndex,
                                   fLocalCoordsVar->c_str()));
    }
    if (-1 != header.fColorAttributeIndex) {
        GL_CALL(BindAttribLocation(programID,
                                   header.fColorAttributeIndex,
                                   color_attribute_name()));
    }
    if (-1 != header.fCoverageAttributeIndex) {
        GL_CALL(BindAttribLocation(programID,
                                   header.fCoverageAttributeIndex,
                                   coverage_attribute_name()));
    }

    const GrOptDrawState& optState = fProgramBuilder->optState();
    const GrVertexAttrib* vaPtr = optState.getVertexAttribs();
    const int vaCount = optState.getVertexAttribCount();

    // We start binding attributes after builtins
    int i = fEffectAttribOffset;
    for (int index = 0; index < vaCount; index++) {
        if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) {
            continue;
        }
        SkASSERT(index != header.fPositionAttributeIndex &&
                 index != header.fLocalCoordAttributeIndex &&
                 index != header.fColorAttributeIndex &&
                 index != header.fCoverageAttributeIndex);
        // We should never find another effect attribute if we have bound everything
        SkASSERT(i < fInputs.count());
        GL_CALL(BindAttribLocation(programID, index, fInputs[i].c_str()));
        i++;
    }
    // Make sure we bound everything
    SkASSERT(fInputs.count() == i);
}

bool GrGLVertexBuilder::compileAndAttachShaders(GrGLuint programId,
        SkTDArray<GrGLuint>* shaderIds) const {
    GrGpuGL* gpu = fProgramBuilder->gpu();
    const GrGLContext& glCtx = gpu->glContext();
    const GrGLContextInfo& ctxInfo = gpu->ctxInfo();
    SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo));
    fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility, &vertShaderSrc);
    this->appendDecls(fInputs, &vertShaderSrc);
    this->appendDecls(fOutputs, &vertShaderSrc);
    vertShaderSrc.append("void main() {");
    vertShaderSrc.append(fCode);
    vertShaderSrc.append("}\n");
    GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId,
                                                       GR_GL_VERTEX_SHADER, vertShaderSrc,
                                                       gpu->gpuStats());
    if (!vertShaderId) {
        return false;
    }
    *shaderIds->append() = vertShaderId;
    return true;
}

bool GrGLVertexBuilder::addAttribute(const GrShaderVar& var) {
    SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier());
    for (int i = 0; i < fInputs.count(); ++i) {
        const GrGLShaderVar& attr = fInputs[i];
        // if attribute already added, don't add it again
        if (attr.getName().equals(var.getName())) {
            return false;
        }
    }
    fInputs.push_back(var);
    return true;
}