blob: 63d91a810fbfafdcd3234f9389de3be9726c2f02 [file] [log] [blame]
bsalomon17168df2014-12-09 09:00:49 -08001/*
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 "GrCoordTransform.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
bsalomon17168df2014-12-09 09:00:49 -080010#include "GrContext.h"
bsalomon17168df2014-12-09 09:00:49 -080011#include "GrGpu.h"
12
Brian Salomon2ebd0c82016-10-03 17:15:28 -040013void GrCoordTransform::reset(const SkMatrix& m, const GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -050014 GrSamplerParams::FilterMode filter) {
bsalomon17168df2014-12-09 09:00:49 -080015 SkASSERT(texture);
16 SkASSERT(!fInProcessor);
17
bsalomon17168df2014-12-09 09:00:49 -080018 fMatrix = m;
19 fReverseY = kBottomLeft_GrSurfaceOrigin == texture->origin();
20
21 // Always start at kDefault. Then if precisions differ we see if the precision needs to be
22 // increased. Our rule is that we want at least 4 subpixel values in the representation for
bsalomon9f876a32014-12-09 10:51:07 -080023 // coords between 0 to 1 when bi- or tri-lerping and 1 value when nearest filtering. Note that
24 // this still might not be enough when drawing with repeat or mirror-repeat modes but that case
25 // can be arbitrarily bad.
Brian Salomon514baff2016-11-17 15:17:07 -050026 int subPixelThresh = filter > GrSamplerParams::kNone_FilterMode ? 4 : 1;
bsalomonc0bd6482014-12-09 10:04:14 -080027 fPrecision = kDefault_GrSLPrecision;
bsalomon17168df2014-12-09 09:00:49 -080028 if (texture->getContext()) {
bsalomon76228632015-05-29 08:02:10 -070029 const GrShaderCaps* caps = texture->getContext()->caps()->shaderCaps();
bsalomon17168df2014-12-09 09:00:49 -080030 if (caps->floatPrecisionVaries()) {
31 int maxD = SkTMax(texture->width(), texture->height());
jvanverthe9c0fc62015-04-29 11:18:05 -070032 const GrShaderCaps::PrecisionInfo* info;
bsalomon17168df2014-12-09 09:00:49 -080033 info = &caps->getFloatShaderPrecisionInfo(kFragment_GrShaderType, fPrecision);
34 do {
35 SkASSERT(info->supported());
36 // Make sure there is at least 2 bits of subpixel precision in the range of
37 // texture coords from 0.5 to 1.0.
bsalomon9f876a32014-12-09 10:51:07 -080038 if ((2 << info->fBits) / maxD > subPixelThresh) {
bsalomon17168df2014-12-09 09:00:49 -080039 break;
40 }
bsalomonc0bd6482014-12-09 10:04:14 -080041 if (kHigh_GrSLPrecision == fPrecision) {
bsalomon17168df2014-12-09 09:00:49 -080042 break;
43 }
bsalomonc0bd6482014-12-09 10:04:14 -080044 GrSLPrecision nextP = static_cast<GrSLPrecision>(fPrecision + 1);
bsalomon17168df2014-12-09 09:00:49 -080045 info = &caps->getFloatShaderPrecisionInfo(kFragment_GrShaderType, nextP);
46 if (!info->supported()) {
47 break;
48 }
49 fPrecision = nextP;
50 } while (true);
51 }
52 }
53}
54
Brian Salomon2ebd0c82016-10-03 17:15:28 -040055void GrCoordTransform::reset(const SkMatrix& m, GrSLPrecision precision) {
bsalomon17168df2014-12-09 09:00:49 -080056 SkASSERT(!fInProcessor);
bsalomon17168df2014-12-09 09:00:49 -080057 fMatrix = m;
58 fReverseY = false;
59 fPrecision = precision;
60}