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 | /** |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 14 | * We specialize the vertex or fragment coord transform code for these matrix types, and where |
| 15 | * the transform code is applied. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 16 | */ |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 17 | enum SampleFlag { |
| 18 | kExplicitlySampled_Flag = 0b0000001, // GrFP::isSampledWithExplicitCoords() |
| 19 | |
| 20 | kLegacyCoordTransform_Flag = 0b0000010, // !GrFP::coordTransform(i)::isNoOp() |
| 21 | |
| 22 | kNone_SampleMatrix_Flag = 0b0000100, // GrFP::sampleMatrix()::isNoOp() |
| 23 | kConstUniform_SampleMatrix_Flag = 0b0001000, // GrFP::sampleMatrix()::isConstUniform() |
| 24 | kVariable_SampleMatrix_Flag = 0b0001100, // GrFP::sampleMatrix()::isVariable() |
| 25 | |
| 26 | // Legacy coord transforms specialize on identity, S+T, no-perspective, and general matrix types |
| 27 | // FIXME these (and kLegacyCoordTransform) can be removed once all FPs no longer use them |
| 28 | kLCT_ScaleTranslate_Matrix_Flag = 0b0010000, // GrFP::coordTransform(i)::isScaleTranslate() |
| 29 | kLCT_NoPersp_Matrix_Flag = 0b0100000, // !GrFP::coordTransform(i)::hasPerspective() |
| 30 | kLCT_General_Matrix_Flag = 0b0110000, // any other matrix type |
| 31 | |
| 32 | // Currently, sample(matrix) only specializes on no-perspective or general. |
| 33 | // FIXME add new flags as more matrix types are supported. |
| 34 | kPersp_Matrix_Flag = 0b1000000, // GrFP::sampleMatrix()::fHasPerspective |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 37 | GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {} |
| 38 | |
| 39 | const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const { |
| 40 | SkASSERT(i >= 0 && i < this->numTextureSamplers()); |
| 41 | return this->onTextureSampler(i); |
| 42 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 43 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 44 | uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const { |
| 45 | // This is highly coupled with the code in GrGLSLGeometryProcessor::emitTransforms(). |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 46 | // At this point, all effects either don't use legacy coord transforms, or only use 1. |
| 47 | SkASSERT(fp.numCoordTransforms() <= 1); |
| 48 | |
| 49 | uint32_t key = 0; |
| 50 | if (fp.isSampledWithExplicitCoords()) { |
| 51 | key |= kExplicitlySampled_Flag; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 52 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 53 | if (fp.numCoordTransforms() > 0) { |
| 54 | const GrCoordTransform& coordTransform = fp.coordTransform(0); |
| 55 | if (!coordTransform.isNoOp()) { |
| 56 | // A true identity matrix shouldn't result in a coord transform; proxy normalization |
| 57 | // and flipping will eventually present as a scale+translate matrix. |
| 58 | SkASSERT(!coordTransform.matrix().isIdentity() || coordTransform.normalize() || |
| 59 | coordTransform.reverseY()); |
| 60 | key |= kLegacyCoordTransform_Flag; |
| 61 | if (coordTransform.matrix().isScaleTranslate()) { |
| 62 | key |= kLCT_ScaleTranslate_Matrix_Flag; |
| 63 | } else if (!coordTransform.matrix().hasPerspective()) { |
| 64 | key |= kLCT_NoPersp_Matrix_Flag; |
| 65 | } else { |
| 66 | key |= kLCT_General_Matrix_Flag; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | switch(fp.sampleMatrix().fKind) { |
| 72 | case SkSL::SampleMatrix::Kind::kNone: |
| 73 | key |= kNone_SampleMatrix_Flag; |
| 74 | break; |
| 75 | case SkSL::SampleMatrix::Kind::kConstantOrUniform: |
| 76 | key |= kConstUniform_SampleMatrix_Flag; |
| 77 | break; |
| 78 | case SkSL::SampleMatrix::Kind::kVariable: |
| 79 | key |= kVariable_SampleMatrix_Flag; |
| 80 | break; |
| 81 | } |
| 82 | if (fp.sampleMatrix().fHasPerspective) { |
| 83 | key |= kPersp_Matrix_Flag; |
| 84 | } |
| 85 | |
| 86 | return key; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 87 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 90 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 91 | static inline GrSamplerState::Filter clamp_filter(GrTextureType type, |
| 92 | GrSamplerState::Filter requestedFilter) { |
| 93 | if (GrTextureTypeHasRestrictedSampling(type)) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 94 | return std::min(requestedFilter, GrSamplerState::Filter::kBilerp); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 95 | } |
| 96 | return requestedFilter; |
Brian Salomon | af87483 | 2018-08-03 11:53:40 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 99 | GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 100 | const GrBackendFormat& backendFormat, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 101 | const GrSwizzle& swizzle) { |
| 102 | this->reset(samplerState, backendFormat, swizzle); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 105 | void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 106 | const GrBackendFormat& backendFormat, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 107 | const GrSwizzle& swizzle) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 108 | fSamplerState = samplerState; |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 109 | fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter())); |
| 110 | fBackendFormat = backendFormat; |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 111 | fSwizzle = swizzle; |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 112 | fIsInitialized = true; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 113 | } |