joshualitt | 8072caa | 2015-02-12 14:20:52 -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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrPrimitiveProcessor.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrCoordTransform.h" |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrFragmentProcessor.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 12 | |
| 13 | /** |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 14 | * We specialize the vertex or fragment coord transform code for these matrix types. |
| 15 | * Some specializations are only applied when the coord transform is applied in the fragment |
| 16 | * shader. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 17 | */ |
| 18 | enum MatrixType { |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 19 | kNone_MatrixType = 0, // Used only in FS for explicitly sampled FPs |
| 20 | kScaleTranslate_MatrixType = 1, // Used only in FS for explicitly sampled FPs |
| 21 | kNoPersp_MatrixType = 2, |
| 22 | kGeneral_MatrixType = 3, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 25 | GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {} |
| 26 | |
| 27 | const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const { |
| 28 | SkASSERT(i >= 0 && i < this->numTextureSamplers()); |
| 29 | return this->onTextureSampler(i); |
| 30 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 31 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 32 | uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const { |
| 33 | // This is highly coupled with the code in GrGLSLGeometryProcessor::emitTransforms(). |
| 34 | SkASSERT(fp.numCoordTransforms() * 2 <= 32); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 35 | uint32_t totalKey = 0; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 36 | for (int t = 0; t < fp.numCoordTransforms(); ++t) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 37 | uint32_t key = 0; |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 38 | const GrCoordTransform& coordTransform = fp.coordTransform(t); |
Brian Salomon | b10a662 | 2020-02-20 13:36:01 -0500 | [diff] [blame] | 39 | if (fp.isSampledWithExplicitCoords() && coordTransform.isNoOp()) { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 40 | key = kNone_MatrixType; |
Brian Salomon | 8d1dcd7 | 2020-03-20 21:06:41 -0400 | [diff] [blame] | 41 | } else if (fp.isSampledWithExplicitCoords() && coordTransform.matrix().isScaleTranslate()) { |
| 42 | key = kScaleTranslate_MatrixType; |
| 43 | } else if (!coordTransform.matrix().hasPerspective()) { |
| 44 | key = kNoPersp_MatrixType; |
| 45 | } else { |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 46 | // Note that we can also have homogeneous varyings as a result of a GP local matrix or |
| 47 | // homogeneous local coords generated by GP. We're relying on the GP to include any |
| 48 | // variability in those in its key. |
| 49 | key = kGeneral_MatrixType; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 50 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 51 | key <<= 2*t; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 52 | SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap |
| 53 | totalKey |= key; |
| 54 | } |
| 55 | return totalKey; |
| 56 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 59 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 60 | static inline GrSamplerState::Filter clamp_filter(GrTextureType type, |
| 61 | GrSamplerState::Filter requestedFilter) { |
| 62 | if (GrTextureTypeHasRestrictedSampling(type)) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 63 | return std::min(requestedFilter, GrSamplerState::Filter::kBilerp); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 64 | } |
| 65 | return requestedFilter; |
Brian Salomon | af87483 | 2018-08-03 11:53:40 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 68 | GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 69 | const GrBackendFormat& backendFormat, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 70 | const GrSwizzle& swizzle) { |
| 71 | this->reset(samplerState, backendFormat, swizzle); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 74 | void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 75 | const GrBackendFormat& backendFormat, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 76 | const GrSwizzle& swizzle) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 77 | fSamplerState = samplerState; |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 78 | fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter())); |
| 79 | fBackendFormat = backendFormat; |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 80 | fSwizzle = swizzle; |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 81 | fIsInitialized = true; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 82 | } |