blob: 2aded892d4f2f2c9ae0fd8a6b8f88c1eb4d12883 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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
egdaniel2d721d32015-11-11 13:06:05 -08008#include "GrGLSLVertexShaderBuilder.h"
egdaniel8dcdedc2015-11-11 06:27:20 -08009#include "glsl/GrGLSLProgramBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080010#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080011#include "glsl/GrGLSLVarying.h"
joshualitt30ba4362014-08-21 20:18:45 -070012
egdaniel2d721d32015-11-11 13:06:05 -080013GrGLSLVertexBuilder::GrGLSLVertexBuilder(GrGLSLProgramBuilder* program)
joshualittdb0d3ca2014-10-07 12:42:26 -070014 : INHERITED(program)
halcanary96fcdcc2015-08-27 07:41:13 -070015 , fRtAdjustName(nullptr) {
joshualittdb0d3ca2014-10-07 12:42:26 -070016}
17
egdaniel2d721d32015-11-11 13:06:05 -080018void GrGLSLVertexBuilder::transformToNormalizedDeviceSpace(const GrShaderVar& posVar) {
joshualittabb52a12015-01-13 15:02:10 -080019 SkASSERT(!fRtAdjustName);
20
joshualitt4973d9d2014-11-08 09:24:25 -080021 // setup RT Uniform
joel.liang8e03b8a2015-12-07 10:33:00 -080022 fProgramBuilder->addRTAdjustmentUniform(kHigh_GrSLPrecision,
egdaniel7ea439b2015-12-03 09:20:44 -080023 fProgramBuilder->rtAdjustment(),
24 &fRtAdjustName);
Ethan Nicholasde4d3012017-01-19 16:58:02 -050025 if (this->getProgramBuilder()->desc()->header().fSnapVerticesToPixelCenters) {
bsalomond79c5492015-04-27 10:07:04 -070026 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);"
robertphillipsef4ba3d2015-09-17 11:21:06 -070034 "gl_Position = vec4(_posTmp.x * %s.x + %s.y,"
35 "_posTmp.y * %s.z + %s.w, 0, 1);}",
bsalomond79c5492015-04-27 10:07:04 -070036 fRtAdjustName, fRtAdjustName, fRtAdjustName, fRtAdjustName);
37 } else if (kVec3f_GrSLType == posVar.getType()) {
robertphillipsef4ba3d2015-09-17 11:21:06 -070038 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());
robertphillips46d36f02015-01-18 08:14:14 -080042 } else {
43 SkASSERT(kVec2f_GrSLType == posVar.getType());
robertphillips7f14c9b2015-01-30 14:44:22 -080044 this->codeAppendf("gl_Position = vec4(%s.x * %s.x + %s.y, %s.y * %s.z + %s.w, 0, 1);",
robertphillips46d36f02015-01-18 08:14:14 -080045 posVar.c_str(), fRtAdjustName, fRtAdjustName,
46 posVar.c_str(), fRtAdjustName, fRtAdjustName);
47 }
bsalomond79c5492015-04-27 10:07:04 -070048 // We could have the GrGeometryProcessor do this, but its just easier to have it performed
bsalomon2eda5b32016-09-21 10:53:24 -070049 // here. If we ever need to set variable pointsize, then we can reinvestigate.
Ethan Nicholasde4d3012017-01-19 16:58:02 -050050 if (this->getProgramBuilder()->desc()->header().fHasPointSize) {
bsalomon2eda5b32016-09-21 10:53:24 -070051 this->codeAppend("gl_PointSize = 1.0;");
52 }
joshualitt47bb3822014-10-07 16:43:25 -070053}
54
egdaniel0eafe792015-11-20 14:01:22 -080055void GrGLSLVertexBuilder::onFinalize() {
56 fProgramBuilder->varyingHandler()->getVertexDecls(&this->inputs(), &this->outputs());
joshualitt30ba4362014-08-21 20:18:45 -070057}