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