1/*
2 * Copyright 2014 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 "GrGLSLVertexShaderBuilder.h"
9#include "glsl/GrGLSLProgramBuilder.h"
10#include "glsl/GrGLSLUniformHandler.h"
11#include "glsl/GrGLSLVarying.h"
12
13GrGLSLVertexBuilder::GrGLSLVertexBuilder(GrGLSLProgramBuilder* program)
14    : INHERITED(program)
15    , fRtAdjustName(nullptr) {
16}
17
18void GrGLSLVertexBuilder::transformToNormalizedDeviceSpace(const GrShaderVar& posVar) {
19    SkASSERT(!fRtAdjustName);
20
21    // setup RT Uniform
22    fProgramBuilder->addRTAdjustmentUniform(kHigh_GrSLPrecision,
23                                            fProgramBuilder->rtAdjustment(),
24                                            &fRtAdjustName);
25    if (this->getProgramBuilder()->desc().header().fSnapVerticesToPixelCenters) {
26        if (kVec3f_GrSLType == posVar.getType()) {
27            const char* p = posVar.c_str();
28            this->codeAppendf("{vec2 _posTmp = vec2(%s.x/%s.z, %s.y/%s.z);", p, p, p, p);
29        } else {
30            SkASSERT(kVec2f_GrSLType == posVar.getType());
31            this->codeAppendf("{vec2 _posTmp = %s;", posVar.c_str());
32        }
33        this->codeAppendf("_posTmp = floor(_posTmp) + vec2(0.5, 0.5);"
34                          "gl_Position = vec4(_posTmp.x * %s.x + %s.y,"
35                                             "_posTmp.y * %s.z + %s.w, 0, 1);}",
36                          fRtAdjustName, fRtAdjustName, fRtAdjustName, fRtAdjustName);
37    } else if (kVec3f_GrSLType == posVar.getType()) {
38        this->codeAppendf("gl_Position = vec4(dot(%s.xz, %s.xy), dot(%s.yz, %s.zw), 0, %s.z);",
39                          posVar.c_str(), fRtAdjustName,
40                          posVar.c_str(), fRtAdjustName,
41                          posVar.c_str());
42    } else {
43        SkASSERT(kVec2f_GrSLType == posVar.getType());
44        this->codeAppendf("gl_Position = vec4(%s.x * %s.x + %s.y, %s.y * %s.z + %s.w, 0, 1);",
45                          posVar.c_str(), fRtAdjustName, fRtAdjustName,
46                          posVar.c_str(), fRtAdjustName, fRtAdjustName);
47    }
48    // We could have the GrGeometryProcessor do this, but its just easier to have it performed
49    // here. If we ever need to set variable pointsize, then we can reinvestigate
50    this->codeAppend("gl_PointSize = 1.0;");
51}
52
53void GrGLSLVertexBuilder::onFinalize() {
54    fProgramBuilder->varyingHandler()->getVertexDecls(&this->inputs(), &this->outputs());
55}
56
57