blob: 6d2b1e9418cc2098574e61c548903940446a993b [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
8#include "GrPrimitiveProcessor.h"
9
10#include "GrCoordTransform.h"
11
12/**
joshualitt8072caa2015-02-12 14:20:52 -080013 * We specialize the vertex code for each of these matrix types.
14 */
15enum MatrixType {
16 kNoPersp_MatrixType = 0,
17 kGeneral_MatrixType = 1,
18};
19
Brian Salomone782f842018-07-31 13:53:11 -040020GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
21
22const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
23 SkASSERT(i >= 0 && i < this->numTextureSamplers());
24 return this->onTextureSampler(i);
25}
Brian Salomon92be2f72018-06-19 14:33:47 -040026
27const GrPrimitiveProcessor::Attribute& GrPrimitiveProcessor::vertexAttribute(int i) const {
28 SkASSERT(i >= 0 && i < this->numVertexAttributes());
29 const auto& result = this->onVertexAttribute(i);
30 SkASSERT(result.isInitialized());
31 return result;
32}
33
34const GrPrimitiveProcessor::Attribute& GrPrimitiveProcessor::instanceAttribute(int i) const {
35 SkASSERT(i >= 0 && i < this->numInstanceAttributes());
36 const auto& result = this->onInstanceAttribute(i);
37 SkASSERT(result.isInitialized());
38 return result;
39}
40
41#ifdef SK_DEBUG
42size_t GrPrimitiveProcessor::debugOnly_vertexStride() const {
43 size_t stride = 0;
44 for (int i = 0; i < fVertexAttributeCnt; ++i) {
45 stride += this->vertexAttribute(i).sizeAlign4();
46 }
47 return stride;
48}
49
50size_t GrPrimitiveProcessor::debugOnly_instanceStride() const {
51 size_t stride = 0;
52 for (int i = 0; i < fInstanceAttributeCnt; ++i) {
53 stride += this->instanceAttribute(i).sizeAlign4();
54 }
55 return stride;
56}
57
58size_t GrPrimitiveProcessor::debugOnly_vertexAttributeOffset(int i) const {
59 SkASSERT(i >= 0 && i < fVertexAttributeCnt);
60 size_t offset = 0;
61 for (int j = 0; j < i; ++j) {
62 offset += this->vertexAttribute(j).sizeAlign4();
63 }
64 return offset;
65}
66
67size_t GrPrimitiveProcessor::debugOnly_instanceAttributeOffset(int i) const {
68 SkASSERT(i >= 0 && i < fInstanceAttributeCnt);
69 size_t offset = 0;
70 for (int j = 0; j < i; ++j) {
71 offset += this->instanceAttribute(j).sizeAlign4();
72 }
73 return offset;
74}
75#endif
76
joshualitt8072caa2015-02-12 14:20:52 -080077uint32_t
wangyixa7f4c432015-08-20 07:25:02 -070078GrPrimitiveProcessor::getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
79 int numCoords) const {
joshualitt8072caa2015-02-12 14:20:52 -080080 uint32_t totalKey = 0;
wangyixa7f4c432015-08-20 07:25:02 -070081 for (int t = 0; t < numCoords; ++t) {
joshualitt8072caa2015-02-12 14:20:52 -080082 uint32_t key = 0;
83 const GrCoordTransform* coordTransform = coords[t];
84 if (coordTransform->getMatrix().hasPerspective()) {
85 key |= kGeneral_MatrixType;
86 } else {
87 key |= kNoPersp_MatrixType;
88 }
Brian Salomon969bdef2018-06-06 17:16:05 -040089 key <<= t;
joshualitt8072caa2015-02-12 14:20:52 -080090 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
91 totalKey |= key;
92 }
93 return totalKey;
94}
Brian Salomone782f842018-07-31 13:53:11 -040095
96///////////////////////////////////////////////////////////////////////////////////////////////////
97
Brian Salomon7eae3e02018-08-07 14:02:38 +000098static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
99 GrSamplerState::Filter requestedFilter) {
100 if (GrTextureTypeHasRestrictedSampling(type)) {
101 return SkTMin(requestedFilter, GrSamplerState::Filter::kBilerp);
102 }
103 return requestedFilter;
Brian Salomonaf874832018-08-03 11:53:40 -0400104}
105
Brian Salomon7eae3e02018-08-07 14:02:38 +0000106GrPrimitiveProcessor::TextureSampler::TextureSampler(GrTextureType textureType,
107 GrPixelConfig config,
108 const GrSamplerState& samplerState,
109 GrShaderFlags visibility) {
110 this->reset(textureType, config, samplerState, visibility);
111}
112
113GrPrimitiveProcessor::TextureSampler::TextureSampler(GrTextureType textureType,
114 GrPixelConfig config,
Brian Salomone782f842018-07-31 13:53:11 -0400115 GrSamplerState::Filter filterMode,
116 GrSamplerState::WrapMode wrapXAndY,
117 GrShaderFlags visibility) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000118 this->reset(textureType, config, filterMode, wrapXAndY, visibility);
Brian Salomone782f842018-07-31 13:53:11 -0400119}
120
Brian Salomon7eae3e02018-08-07 14:02:38 +0000121void GrPrimitiveProcessor::TextureSampler::reset(GrTextureType textureType,
122 GrPixelConfig config,
Brian Salomone782f842018-07-31 13:53:11 -0400123 const GrSamplerState& samplerState,
124 GrShaderFlags visibility) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000125 SkASSERT(kUnknown_GrPixelConfig != config);
Brian Salomone782f842018-07-31 13:53:11 -0400126 fSamplerState = samplerState;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000127 fSamplerState.setFilterMode(clamp_filter(textureType, samplerState.filter()));
128 fTextureType = textureType;
129 fConfig = config;
Brian Salomone782f842018-07-31 13:53:11 -0400130 fVisibility = visibility;
131}
132
Brian Salomon7eae3e02018-08-07 14:02:38 +0000133void GrPrimitiveProcessor::TextureSampler::reset(GrTextureType textureType,
134 GrPixelConfig config,
Brian Salomone782f842018-07-31 13:53:11 -0400135 GrSamplerState::Filter filterMode,
136 GrSamplerState::WrapMode wrapXAndY,
137 GrShaderFlags visibility) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000138 SkASSERT(kUnknown_GrPixelConfig != config);
139 filterMode = clamp_filter(textureType, filterMode);
Brian Salomone782f842018-07-31 13:53:11 -0400140 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000141 fTextureType = textureType;
142 fConfig = config;
Brian Salomone782f842018-07-31 13:53:11 -0400143 fVisibility = visibility;
144}