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 { |
Michael Ludwig | e7e25ac | 2020-06-26 12:53:03 -0400 | [diff] [blame] | 18 | kExplicitlySampled_Flag = 0b00001, // GrFP::isSampledWithExplicitCoords() |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 19 | |
Michael Ludwig | e7e25ac | 2020-06-26 12:53:03 -0400 | [diff] [blame] | 20 | kLegacyCoordTransform_Flag = 0b00010, // !GrFP::coordTransform(i)::isNoOp() |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 21 | |
Michael Ludwig | e7e25ac | 2020-06-26 12:53:03 -0400 | [diff] [blame] | 22 | kNone_SampleMatrix_Flag = 0b00100, // GrFP::sampleMatrix()::isNoOp() |
| 23 | kConstUniform_SampleMatrix_Flag = 0b01000, // GrFP::sampleMatrix()::isConstUniform() |
| 24 | kVariable_SampleMatrix_Flag = 0b01100, // GrFP::sampleMatrix()::isVariable() |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 25 | |
| 26 | // Currently, sample(matrix) only specializes on no-perspective or general. |
| 27 | // FIXME add new flags as more matrix types are supported. |
Michael Ludwig | e7e25ac | 2020-06-26 12:53:03 -0400 | [diff] [blame] | 28 | kPersp_Matrix_Flag = 0b10000, // GrFP::sampleMatrix()::fHasPerspective |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 29 | }; |
| 30 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 31 | GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {} |
| 32 | |
| 33 | const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const { |
| 34 | SkASSERT(i >= 0 && i < this->numTextureSamplers()); |
| 35 | return this->onTextureSampler(i); |
| 36 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 37 | |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 38 | uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const { |
Michael Ludwig | e7e25ac | 2020-06-26 12:53:03 -0400 | [diff] [blame] | 39 | // This is highly coupled with the code in GrGLSLGeometryProcessor::collectTransforms(). |
| 40 | // At this point, all effects do not use really coord transforms; they may implicitly report |
| 41 | // a noop coord transform but this does not impact the key. |
| 42 | SkASSERT(fp.numCoordTransforms() == 0 || |
| 43 | (fp.numCoordTransforms() == 1 && fp.coordTransform(0).isNoOp())); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 44 | |
| 45 | uint32_t key = 0; |
| 46 | if (fp.isSampledWithExplicitCoords()) { |
| 47 | key |= kExplicitlySampled_Flag; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 48 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 49 | |
| 50 | switch(fp.sampleMatrix().fKind) { |
| 51 | case SkSL::SampleMatrix::Kind::kNone: |
| 52 | key |= kNone_SampleMatrix_Flag; |
| 53 | break; |
| 54 | case SkSL::SampleMatrix::Kind::kConstantOrUniform: |
| 55 | key |= kConstUniform_SampleMatrix_Flag; |
| 56 | break; |
| 57 | case SkSL::SampleMatrix::Kind::kVariable: |
| 58 | key |= kVariable_SampleMatrix_Flag; |
| 59 | break; |
| 60 | } |
| 61 | if (fp.sampleMatrix().fHasPerspective) { |
| 62 | key |= kPersp_Matrix_Flag; |
| 63 | } |
| 64 | |
| 65 | return key; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 66 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 67 | |
| 68 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 69 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 70 | static inline GrSamplerState::Filter clamp_filter(GrTextureType type, |
| 71 | GrSamplerState::Filter requestedFilter) { |
| 72 | if (GrTextureTypeHasRestrictedSampling(type)) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 73 | return std::min(requestedFilter, GrSamplerState::Filter::kBilerp); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 74 | } |
| 75 | return requestedFilter; |
Brian Salomon | af87483 | 2018-08-03 11:53:40 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 78 | GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 79 | const GrBackendFormat& backendFormat, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 80 | const GrSwizzle& swizzle) { |
| 81 | this->reset(samplerState, backendFormat, swizzle); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 84 | void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 85 | const GrBackendFormat& backendFormat, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 86 | const GrSwizzle& swizzle) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 87 | fSamplerState = samplerState; |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 88 | fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter())); |
| 89 | fBackendFormat = backendFormat; |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 90 | fSwizzle = swizzle; |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 91 | fIsInitialized = true; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 92 | } |