blob: 2be07b19d9f76e86bd30d1a5e09188b1d0db6915 [file] [log] [blame]
joshualitt8072caa2015-02-12 14:20:52 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrPrimitiveProcessor.h"
joshualitt8072caa2015-02-12 14:20:52 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrCoordTransform.h"
Brian Salomon7eabfe82019-12-02 14:20:20 -050011#include "src/gpu/GrFragmentProcessor.h"
joshualitt8072caa2015-02-12 14:20:52 -080012
13/**
Michael Ludwige88320b2020-06-24 09:04:56 -040014 * We specialize the vertex or fragment coord transform code for these matrix types, and where
15 * the transform code is applied.
joshualitt8072caa2015-02-12 14:20:52 -080016 */
Michael Ludwige88320b2020-06-24 09:04:56 -040017enum 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
joshualitt8072caa2015-02-12 14:20:52 -080035};
36
Brian Salomone782f842018-07-31 13:53:11 -040037GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
38
39const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
40 SkASSERT(i >= 0 && i < this->numTextureSamplers());
41 return this->onTextureSampler(i);
42}
Brian Salomon92be2f72018-06-19 14:33:47 -040043
Brian Salomon7eabfe82019-12-02 14:20:20 -050044uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const {
45 // This is highly coupled with the code in GrGLSLGeometryProcessor::emitTransforms().
Michael Ludwige88320b2020-06-24 09:04:56 -040046 // 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;
joshualitt8072caa2015-02-12 14:20:52 -080052 }
Michael Ludwige88320b2020-06-24 09:04:56 -040053 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;
joshualitt8072caa2015-02-12 14:20:52 -080087}
Brian Salomone782f842018-07-31 13:53:11 -040088
89///////////////////////////////////////////////////////////////////////////////////////////////////
90
Brian Salomon7eae3e02018-08-07 14:02:38 +000091static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
92 GrSamplerState::Filter requestedFilter) {
93 if (GrTextureTypeHasRestrictedSampling(type)) {
Brian Osman788b9162020-02-07 10:36:46 -050094 return std::min(requestedFilter, GrSamplerState::Filter::kBilerp);
Brian Salomon7eae3e02018-08-07 14:02:38 +000095 }
96 return requestedFilter;
Brian Salomonaf874832018-08-03 11:53:40 -040097}
98
Brian Salomonccb61422020-01-09 10:46:36 -050099GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400100 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -0500101 const GrSwizzle& swizzle) {
102 this->reset(samplerState, backendFormat, swizzle);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000103}
104
Brian Salomonccb61422020-01-09 10:46:36 -0500105void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400106 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -0500107 const GrSwizzle& swizzle) {
Brian Salomone782f842018-07-31 13:53:11 -0400108 fSamplerState = samplerState;
Robert Phillipsf272bea2019-10-17 08:56:16 -0400109 fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
110 fBackendFormat = backendFormat;
Greg Daniel2c3398d2019-06-19 11:58:01 -0400111 fSwizzle = swizzle;
Brian Salomon67529b22019-08-13 15:31:04 -0400112 fIsInitialized = true;
Brian Salomone782f842018-07-31 13:53:11 -0400113}