blob: 9378c41a293cab58b66a9595a837e90dcb8868b3 [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/**
joshualitt8072caa2015-02-12 14:20:52 -080014 * We specialize the vertex code for each of these matrix types.
15 */
16enum MatrixType {
Brian Salomon7eabfe82019-12-02 14:20:20 -050017 kNone_MatrixType = 0,
18 kNoPersp_MatrixType = 1,
19 kGeneral_MatrixType = 2,
joshualitt8072caa2015-02-12 14:20:52 -080020};
21
Brian Salomone782f842018-07-31 13:53:11 -040022GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
23
24const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
25 SkASSERT(i >= 0 && i < this->numTextureSamplers());
26 return this->onTextureSampler(i);
27}
Brian Salomon92be2f72018-06-19 14:33:47 -040028
Brian Salomon7eabfe82019-12-02 14:20:20 -050029uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const {
30 // This is highly coupled with the code in GrGLSLGeometryProcessor::emitTransforms().
31 SkASSERT(fp.numCoordTransforms() * 2 <= 32);
joshualitt8072caa2015-02-12 14:20:52 -080032 uint32_t totalKey = 0;
Brian Salomon7eabfe82019-12-02 14:20:20 -050033 for (int t = 0; t < fp.numCoordTransforms(); ++t) {
joshualitt8072caa2015-02-12 14:20:52 -080034 uint32_t key = 0;
Brian Salomon7eabfe82019-12-02 14:20:20 -050035 const GrCoordTransform& coordTransform = fp.coordTransform(t);
Brian Salomonb10a6622020-02-20 13:36:01 -050036 if (fp.isSampledWithExplicitCoords() && coordTransform.isNoOp()) {
Brian Salomon7eabfe82019-12-02 14:20:20 -050037 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;
joshualitt8072caa2015-02-12 14:20:52 -080043 } else {
Brian Salomon7eabfe82019-12-02 14:20:20 -050044 key = kNoPersp_MatrixType;
joshualitt8072caa2015-02-12 14:20:52 -080045 }
Brian Salomon7eabfe82019-12-02 14:20:20 -050046 key <<= 2*t;
joshualitt8072caa2015-02-12 14:20:52 -080047 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
48 totalKey |= key;
49 }
50 return totalKey;
51}
Brian Salomone782f842018-07-31 13:53:11 -040052
53///////////////////////////////////////////////////////////////////////////////////////////////////
54
Brian Salomon7eae3e02018-08-07 14:02:38 +000055static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
56 GrSamplerState::Filter requestedFilter) {
57 if (GrTextureTypeHasRestrictedSampling(type)) {
Brian Osman788b9162020-02-07 10:36:46 -050058 return std::min(requestedFilter, GrSamplerState::Filter::kBilerp);
Brian Salomon7eae3e02018-08-07 14:02:38 +000059 }
60 return requestedFilter;
Brian Salomonaf874832018-08-03 11:53:40 -040061}
62
Brian Salomonccb61422020-01-09 10:46:36 -050063GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040064 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050065 const GrSwizzle& swizzle) {
66 this->reset(samplerState, backendFormat, swizzle);
Brian Salomon7eae3e02018-08-07 14:02:38 +000067}
68
Brian Salomonccb61422020-01-09 10:46:36 -050069void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040070 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050071 const GrSwizzle& swizzle) {
Brian Salomone782f842018-07-31 13:53:11 -040072 fSamplerState = samplerState;
Robert Phillipsf272bea2019-10-17 08:56:16 -040073 fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
74 fBackendFormat = backendFormat;
Greg Daniel2c3398d2019-06-19 11:58:01 -040075 fSwizzle = swizzle;
Brian Salomon67529b22019-08-13 15:31:04 -040076 fIsInitialized = true;
Brian Salomone782f842018-07-31 13:53:11 -040077}