blob: b2c75c0d2963cdb9520513c9eb8a051cd4568aed [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/**
Brian Salomon8d1dcd72020-03-20 21:06:41 -040014 * We specialize the vertex or fragment coord transform code for these matrix types.
15 * Some specializations are only applied when the coord transform is applied in the fragment
16 * shader.
joshualitt8072caa2015-02-12 14:20:52 -080017 */
18enum MatrixType {
Brian Salomon8d1dcd72020-03-20 21:06:41 -040019 kNone_MatrixType = 0, // Used only in FS for explicitly sampled FPs
20 kScaleTranslate_MatrixType = 1, // Used only in FS for explicitly sampled FPs
21 kNoPersp_MatrixType = 2,
22 kGeneral_MatrixType = 3,
joshualitt8072caa2015-02-12 14:20:52 -080023};
24
Brian Salomone782f842018-07-31 13:53:11 -040025GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
26
27const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
28 SkASSERT(i >= 0 && i < this->numTextureSamplers());
29 return this->onTextureSampler(i);
30}
Brian Salomon92be2f72018-06-19 14:33:47 -040031
Brian Salomon7eabfe82019-12-02 14:20:20 -050032uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const {
33 // This is highly coupled with the code in GrGLSLGeometryProcessor::emitTransforms().
34 SkASSERT(fp.numCoordTransforms() * 2 <= 32);
joshualitt8072caa2015-02-12 14:20:52 -080035 uint32_t totalKey = 0;
Brian Salomon7eabfe82019-12-02 14:20:20 -050036 for (int t = 0; t < fp.numCoordTransforms(); ++t) {
joshualitt8072caa2015-02-12 14:20:52 -080037 uint32_t key = 0;
Brian Salomon7eabfe82019-12-02 14:20:20 -050038 const GrCoordTransform& coordTransform = fp.coordTransform(t);
Brian Salomonb10a6622020-02-20 13:36:01 -050039 if (fp.isSampledWithExplicitCoords() && coordTransform.isNoOp()) {
Brian Salomon7eabfe82019-12-02 14:20:20 -050040 key = kNone_MatrixType;
Brian Salomon8d1dcd72020-03-20 21:06:41 -040041 } else if (fp.isSampledWithExplicitCoords() && coordTransform.matrix().isScaleTranslate()) {
42 key = kScaleTranslate_MatrixType;
43 } else if (!coordTransform.matrix().hasPerspective()) {
44 key = kNoPersp_MatrixType;
45 } else {
Brian Salomon7eabfe82019-12-02 14:20:20 -050046 // Note that we can also have homogeneous varyings as a result of a GP local matrix or
47 // homogeneous local coords generated by GP. We're relying on the GP to include any
48 // variability in those in its key.
49 key = kGeneral_MatrixType;
joshualitt8072caa2015-02-12 14:20:52 -080050 }
Brian Salomon7eabfe82019-12-02 14:20:20 -050051 key <<= 2*t;
joshualitt8072caa2015-02-12 14:20:52 -080052 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
53 totalKey |= key;
54 }
55 return totalKey;
56}
Brian Salomone782f842018-07-31 13:53:11 -040057
58///////////////////////////////////////////////////////////////////////////////////////////////////
59
Brian Salomon7eae3e02018-08-07 14:02:38 +000060static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
61 GrSamplerState::Filter requestedFilter) {
62 if (GrTextureTypeHasRestrictedSampling(type)) {
Brian Osman788b9162020-02-07 10:36:46 -050063 return std::min(requestedFilter, GrSamplerState::Filter::kBilerp);
Brian Salomon7eae3e02018-08-07 14:02:38 +000064 }
65 return requestedFilter;
Brian Salomonaf874832018-08-03 11:53:40 -040066}
67
Brian Salomonccb61422020-01-09 10:46:36 -050068GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040069 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050070 const GrSwizzle& swizzle) {
71 this->reset(samplerState, backendFormat, swizzle);
Brian Salomon7eae3e02018-08-07 14:02:38 +000072}
73
Brian Salomonccb61422020-01-09 10:46:36 -050074void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040075 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050076 const GrSwizzle& swizzle) {
Brian Salomone782f842018-07-31 13:53:11 -040077 fSamplerState = samplerState;
Robert Phillipsf272bea2019-10-17 08:56:16 -040078 fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
79 fBackendFormat = backendFormat;
Greg Daniel2c3398d2019-06-19 11:58:01 -040080 fSwizzle = swizzle;
Brian Salomon67529b22019-08-13 15:31:04 -040081 fIsInitialized = true;
Brian Salomone782f842018-07-31 13:53:11 -040082}