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