joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 8 | #include "GrGLSLVertexShaderBuilder.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 9 | #include "glsl/GrGLSLProgramBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 10 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLVarying.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 12 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 13 | GrGLSLVertexBuilder::GrGLSLVertexBuilder(GrGLSLProgramBuilder* program) |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 14 | : INHERITED(program) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 15 | , fRtAdjustName(nullptr) { |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 16 | } |
| 17 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 18 | void GrGLSLVertexBuilder::transformToNormalizedDeviceSpace(const GrShaderVar& posVar) { |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 19 | SkASSERT(!fRtAdjustName); |
| 20 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 21 | // setup RT Uniform |
joel.liang | 8e03b8a | 2015-12-07 10:33:00 -0800 | [diff] [blame] | 22 | fProgramBuilder->addRTAdjustmentUniform(kHigh_GrSLPrecision, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 23 | fProgramBuilder->rtAdjustment(), |
| 24 | &fRtAdjustName); |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 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);" |
robertphillips | ef4ba3d | 2015-09-17 11:21:06 -0700 | [diff] [blame] | 34 | "gl_Position = vec4(_posTmp.x * %s.x + %s.y," |
| 35 | "_posTmp.y * %s.z + %s.w, 0, 1);}", |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 36 | fRtAdjustName, fRtAdjustName, fRtAdjustName, fRtAdjustName); |
| 37 | } else if (kVec3f_GrSLType == posVar.getType()) { |
robertphillips | ef4ba3d | 2015-09-17 11:21:06 -0700 | [diff] [blame] | 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()); |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 42 | } else { |
| 43 | SkASSERT(kVec2f_GrSLType == posVar.getType()); |
robertphillips | 7f14c9b | 2015-01-30 14:44:22 -0800 | [diff] [blame] | 44 | this->codeAppendf("gl_Position = vec4(%s.x * %s.x + %s.y, %s.y * %s.z + %s.w, 0, 1);", |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 45 | posVar.c_str(), fRtAdjustName, fRtAdjustName, |
| 46 | posVar.c_str(), fRtAdjustName, fRtAdjustName); |
| 47 | } |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 48 | // We could have the GrGeometryProcessor do this, but its just easier to have it performed |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame^] | 49 | // here. If we ever need to set variable pointsize, then we can reinvestigate. |
| 50 | if (this->getProgramBuilder()->desc().header().fHasPointSize) { |
| 51 | this->codeAppend("gl_PointSize = 1.0;"); |
| 52 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 53 | } |
| 54 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 55 | void GrGLSLVertexBuilder::onFinalize() { |
| 56 | fProgramBuilder->varyingHandler()->getVertexDecls(&this->inputs(), &this->outputs()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 57 | } |