bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [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 | |
| 8 | #include "GrCoordTransform.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 9 | #include "GrCaps.h" |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 10 | #include "GrContext.h" |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 11 | #include "GrGpu.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 12 | #include "GrTextureProxy.h" |
| 13 | |
| 14 | static GrSLPrecision compute_precision(const GrShaderCaps* caps, |
| 15 | int width, int height, |
| 16 | GrSamplerParams::FilterMode filter) { |
| 17 | // Always start at kDefault. Then if precisions differ we see if the precision needs to be |
| 18 | // increased. Our rule is that we want at least 4 subpixel values in the representation for |
| 19 | // coords between 0 to 1 when bi- or tri-lerping and 1 value when nearest filtering. Note that |
| 20 | // this still might not be enough when drawing with repeat or mirror-repeat modes but that case |
| 21 | // can be arbitrarily bad. |
| 22 | int subPixelThresh = filter > GrSamplerParams::kNone_FilterMode ? 4 : 1; |
| 23 | GrSLPrecision precision = kDefault_GrSLPrecision; |
| 24 | if (caps) { |
| 25 | if (caps->floatPrecisionVaries()) { |
| 26 | int maxD = SkTMax(width, height); |
| 27 | const GrShaderCaps::PrecisionInfo* info; |
| 28 | info = &caps->getFloatShaderPrecisionInfo(kFragment_GrShaderType, precision); |
| 29 | do { |
| 30 | SkASSERT(info->supported()); |
| 31 | // Make sure there is at least 2 bits of subpixel precision in the range of |
| 32 | // texture coords from 0.5 to 1.0. |
| 33 | if ((2 << info->fBits) / maxD > subPixelThresh) { |
| 34 | break; |
| 35 | } |
| 36 | if (kHigh_GrSLPrecision == precision) { |
| 37 | break; |
| 38 | } |
| 39 | GrSLPrecision nextP = static_cast<GrSLPrecision>(precision + 1); |
| 40 | info = &caps->getFloatShaderPrecisionInfo(kFragment_GrShaderType, nextP); |
| 41 | if (!info->supported()) { |
| 42 | break; |
| 43 | } |
| 44 | precision = nextP; |
| 45 | } while (true); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return precision; |
| 50 | } |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 51 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 52 | void GrCoordTransform::reset(const SkMatrix& m, const GrTexture* texture, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 53 | GrSamplerParams::FilterMode filter, bool normalize) { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 54 | SkASSERT(texture); |
| 55 | SkASSERT(!fInProcessor); |
| 56 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 57 | fMatrix = m; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 58 | fTexture = texture; |
| 59 | fNormalize = normalize; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 60 | fReverseY = kBottomLeft_GrSurfaceOrigin == texture->origin(); |
| 61 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 62 | if (texture->getContext()) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 63 | fPrecision = compute_precision(texture->getContext()->caps()->shaderCaps(), |
| 64 | texture->width(), texture->height(), filter); |
| 65 | } else { |
| 66 | fPrecision = kDefault_GrSLPrecision; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 70 | void GrCoordTransform::reset(GrContext* context, const SkMatrix& m, |
| 71 | GrTextureProxy* proxy, |
| 72 | GrSamplerParams::FilterMode filter, bool normalize) { |
| 73 | SkASSERT(proxy); |
| 74 | SkASSERT(!fInProcessor); |
| 75 | |
| 76 | fMatrix = m; |
| 77 | // MDB TODO: just GrCaps is needed for this method |
| 78 | // MDB TODO: once all the coord transforms take a proxy just store it here and |
| 79 | // instantiate later |
| 80 | fTexture = proxy->instantiate(context->textureProvider()); |
| 81 | fNormalize = normalize; |
| 82 | fReverseY = kBottomLeft_GrSurfaceOrigin == proxy->origin(); |
| 83 | |
| 84 | const GrCaps* caps = context->caps(); |
| 85 | fPrecision = compute_precision(caps->shaderCaps(), |
| 86 | proxy->worstCaseWidth(*caps), |
| 87 | proxy->worstCaseHeight(*caps), filter); |
| 88 | } |
| 89 | |