blob: ca384f0a84344d2d9048bf0b7ba9b1c95e4c88ab [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 {
Michael Ludwige7e25ac2020-06-26 12:53:03 -040018 kExplicitlySampled_Flag = 0b00001, // GrFP::isSampledWithExplicitCoords()
Michael Ludwige88320b2020-06-24 09:04:56 -040019
Michael Ludwige7e25ac2020-06-26 12:53:03 -040020 kLegacyCoordTransform_Flag = 0b00010, // !GrFP::coordTransform(i)::isNoOp()
Michael Ludwige88320b2020-06-24 09:04:56 -040021
Michael Ludwige7e25ac2020-06-26 12:53:03 -040022 kNone_SampleMatrix_Flag = 0b00100, // GrFP::sampleMatrix()::isNoOp()
23 kConstUniform_SampleMatrix_Flag = 0b01000, // GrFP::sampleMatrix()::isConstUniform()
24 kVariable_SampleMatrix_Flag = 0b01100, // GrFP::sampleMatrix()::isVariable()
Michael Ludwige88320b2020-06-24 09:04:56 -040025
26 // Currently, sample(matrix) only specializes on no-perspective or general.
27 // FIXME add new flags as more matrix types are supported.
Michael Ludwige7e25ac2020-06-26 12:53:03 -040028 kPersp_Matrix_Flag = 0b10000, // GrFP::sampleMatrix()::fHasPerspective
joshualitt8072caa2015-02-12 14:20:52 -080029};
30
Brian Salomone782f842018-07-31 13:53:11 -040031GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
32
33const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
34 SkASSERT(i >= 0 && i < this->numTextureSamplers());
35 return this->onTextureSampler(i);
36}
Brian Salomon92be2f72018-06-19 14:33:47 -040037
Brian Salomon7eabfe82019-12-02 14:20:20 -050038uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const {
Michael Ludwige7e25ac2020-06-26 12:53:03 -040039 // 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 Ludwige88320b2020-06-24 09:04:56 -040044
45 uint32_t key = 0;
46 if (fp.isSampledWithExplicitCoords()) {
47 key |= kExplicitlySampled_Flag;
joshualitt8072caa2015-02-12 14:20:52 -080048 }
Michael Ludwige88320b2020-06-24 09:04:56 -040049
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;
joshualitt8072caa2015-02-12 14:20:52 -080066}
Brian Salomone782f842018-07-31 13:53:11 -040067
68///////////////////////////////////////////////////////////////////////////////////////////////////
69
Brian Salomon7eae3e02018-08-07 14:02:38 +000070static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
71 GrSamplerState::Filter requestedFilter) {
72 if (GrTextureTypeHasRestrictedSampling(type)) {
Brian Osman788b9162020-02-07 10:36:46 -050073 return std::min(requestedFilter, GrSamplerState::Filter::kBilerp);
Brian Salomon7eae3e02018-08-07 14:02:38 +000074 }
75 return requestedFilter;
Brian Salomonaf874832018-08-03 11:53:40 -040076}
77
Brian Salomonccb61422020-01-09 10:46:36 -050078GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040079 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050080 const GrSwizzle& swizzle) {
81 this->reset(samplerState, backendFormat, swizzle);
Brian Salomon7eae3e02018-08-07 14:02:38 +000082}
83
Brian Salomonccb61422020-01-09 10:46:36 -050084void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040085 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050086 const GrSwizzle& swizzle) {
Brian Salomone782f842018-07-31 13:53:11 -040087 fSamplerState = samplerState;
Robert Phillipsf272bea2019-10-17 08:56:16 -040088 fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
89 fBackendFormat = backendFormat;
Greg Daniel2c3398d2019-06-19 11:58:01 -040090 fSwizzle = swizzle;
Brian Salomon67529b22019-08-13 15:31:04 -040091 fIsInitialized = true;
Brian Salomone782f842018-07-31 13:53:11 -040092}