blob: 9299f718112fe4a4993c0e0d042524a6ec677e7c [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
Brian Salomon7eabfe82019-12-02 14:20:20 -050010#include "src/gpu/GrFragmentProcessor.h"
joshualitt8072caa2015-02-12 14:20:52 -080011
12/**
Michael Ludwige88320b2020-06-24 09:04:56 -040013 * We specialize the vertex or fragment coord transform code for these matrix types, and where
14 * the transform code is applied.
joshualitt8072caa2015-02-12 14:20:52 -080015 */
Michael Ludwige88320b2020-06-24 09:04:56 -040016enum SampleFlag {
Brian Osman1298bc42020-06-30 13:39:35 -040017 kExplicitlySampled_Flag = 0b00001, // GrFP::isSampledWithExplicitCoords()
Michael Ludwige88320b2020-06-24 09:04:56 -040018
Brian Osman1298bc42020-06-30 13:39:35 -040019 kNone_SampleMatrix_Flag = 0b00100, // GrFP::sampleUsage()::hasMatrix() == false
20 kUniform_SampleMatrix_Flag = 0b01000, // GrFP::sampleUsage()::hasUniformMatrix()
21 kVariable_SampleMatrix_Flag = 0b01100, // GrFP::sampleUsage()::hasVariableMatrix()
Michael Ludwige88320b2020-06-24 09:04:56 -040022
23 // Currently, sample(matrix) only specializes on no-perspective or general.
24 // FIXME add new flags as more matrix types are supported.
Brian Osman1298bc42020-06-30 13:39:35 -040025 kPersp_Matrix_Flag = 0b10000, // GrFP::sampleUsage()::fHasPerspective
joshualitt8072caa2015-02-12 14:20:52 -080026};
27
Brian Salomone782f842018-07-31 13:53:11 -040028GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
29
30const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
31 SkASSERT(i >= 0 && i < this->numTextureSamplers());
32 return this->onTextureSampler(i);
33}
Brian Salomon92be2f72018-06-19 14:33:47 -040034
Brian Salomon7eabfe82019-12-02 14:20:20 -050035uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const {
Michael Ludwige7e25ac2020-06-26 12:53:03 -040036 // This is highly coupled with the code in GrGLSLGeometryProcessor::collectTransforms().
Michael Ludwige88320b2020-06-24 09:04:56 -040037
38 uint32_t key = 0;
39 if (fp.isSampledWithExplicitCoords()) {
40 key |= kExplicitlySampled_Flag;
joshualitt8072caa2015-02-12 14:20:52 -080041 }
Michael Ludwige88320b2020-06-24 09:04:56 -040042
Brian Osman1298bc42020-06-30 13:39:35 -040043 switch(fp.sampleUsage().fKind) {
44 case SkSL::SampleUsage::Kind::kNone:
Michael Ludwige88320b2020-06-24 09:04:56 -040045 key |= kNone_SampleMatrix_Flag;
46 break;
Brian Osman1298bc42020-06-30 13:39:35 -040047 case SkSL::SampleUsage::Kind::kUniform:
48 key |= kUniform_SampleMatrix_Flag;
Michael Ludwige88320b2020-06-24 09:04:56 -040049 break;
Brian Osman1298bc42020-06-30 13:39:35 -040050 case SkSL::SampleUsage::Kind::kVariable:
Michael Ludwige88320b2020-06-24 09:04:56 -040051 key |= kVariable_SampleMatrix_Flag;
52 break;
53 }
Brian Osman1298bc42020-06-30 13:39:35 -040054 if (fp.sampleUsage().fHasPerspective) {
Michael Ludwige88320b2020-06-24 09:04:56 -040055 key |= kPersp_Matrix_Flag;
56 }
57
58 return key;
joshualitt8072caa2015-02-12 14:20:52 -080059}
Brian Salomone782f842018-07-31 13:53:11 -040060
61///////////////////////////////////////////////////////////////////////////////////////////////////
62
Brian Salomon7eae3e02018-08-07 14:02:38 +000063static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
64 GrSamplerState::Filter requestedFilter) {
65 if (GrTextureTypeHasRestrictedSampling(type)) {
Brian Salomona3b02f52020-07-15 16:02:01 -040066 return std::min(requestedFilter, GrSamplerState::Filter::kLinear);
Brian Salomon7eae3e02018-08-07 14:02:38 +000067 }
68 return requestedFilter;
Brian Salomonaf874832018-08-03 11:53:40 -040069}
70
Brian Salomonccb61422020-01-09 10:46:36 -050071GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040072 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050073 const GrSwizzle& swizzle) {
74 this->reset(samplerState, backendFormat, swizzle);
Brian Salomon7eae3e02018-08-07 14:02:38 +000075}
76
Brian Salomonccb61422020-01-09 10:46:36 -050077void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState,
Robert Phillipsf272bea2019-10-17 08:56:16 -040078 const GrBackendFormat& backendFormat,
Robert Phillips323471e2019-11-11 11:33:37 -050079 const GrSwizzle& swizzle) {
Brian Salomone782f842018-07-31 13:53:11 -040080 fSamplerState = samplerState;
Robert Phillipsf272bea2019-10-17 08:56:16 -040081 fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
82 fBackendFormat = backendFormat;
Greg Daniel2c3398d2019-06-19 11:58:01 -040083 fSwizzle = swizzle;
Brian Salomon67529b22019-08-13 15:31:04 -040084 fIsInitialized = true;
Brian Salomone782f842018-07-31 13:53:11 -040085}