blob: 10b13370f42be129e7fb1655ce8ce7a8c9d0208a [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
Brian Salomone782f842018-07-31 13:53:11 -040077void GrPrimitiveProcessor::addPendingIOs() const {
78 for (int i = 0; i < fTextureSamplerCnt; ++i) {
79 this->textureSampler(i).programProxy()->markPendingIO();
80 }
81}
82
83void GrPrimitiveProcessor::removeRefs() const {
84 for (int i = 0; i < fTextureSamplerCnt; ++i) {
85 this->textureSampler(i).programProxy()->removeRef();
86 }
87}
88
89void GrPrimitiveProcessor::pendingIOComplete() const {
90 for (int i = 0; i < fTextureSamplerCnt; ++i) {
91 this->textureSampler(i).programProxy()->pendingIOComplete();
92 }
93}
94
95bool GrPrimitiveProcessor::instantiate(GrResourceProvider* resourceProvider) const {
96 for (int i = 0; i < fTextureSamplerCnt; ++i) {
97 if (!this->textureSampler(i).instantiate(resourceProvider)) {
98 return false;
99 }
100 }
101 return true;
102}
103
joshualitt8072caa2015-02-12 14:20:52 -0800104uint32_t
wangyixa7f4c432015-08-20 07:25:02 -0700105GrPrimitiveProcessor::getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
106 int numCoords) const {
joshualitt8072caa2015-02-12 14:20:52 -0800107 uint32_t totalKey = 0;
wangyixa7f4c432015-08-20 07:25:02 -0700108 for (int t = 0; t < numCoords; ++t) {
joshualitt8072caa2015-02-12 14:20:52 -0800109 uint32_t key = 0;
110 const GrCoordTransform* coordTransform = coords[t];
111 if (coordTransform->getMatrix().hasPerspective()) {
112 key |= kGeneral_MatrixType;
113 } else {
114 key |= kNoPersp_MatrixType;
115 }
Brian Salomon969bdef2018-06-06 17:16:05 -0400116 key <<= t;
joshualitt8072caa2015-02-12 14:20:52 -0800117 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
118 totalKey |= key;
119 }
120 return totalKey;
121}
Brian Salomone782f842018-07-31 13:53:11 -0400122
123///////////////////////////////////////////////////////////////////////////////////////////////////
124
125GrPrimitiveProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
126 const GrSamplerState& samplerState,
127 GrShaderFlags visibility) {
128 this->reset(std::move(proxy), samplerState, visibility);
129}
130
131GrPrimitiveProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
132 GrSamplerState::Filter filterMode,
133 GrSamplerState::WrapMode wrapXAndY,
134 GrShaderFlags visibility) {
135 this->reset(std::move(proxy), filterMode, wrapXAndY, visibility);
136}
137
138void GrPrimitiveProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
139 const GrSamplerState& samplerState,
140 GrShaderFlags visibility) {
141 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
142 fSamplerState = samplerState;
143 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
144 fVisibility = visibility;
145}
146
147void GrPrimitiveProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
148 GrSamplerState::Filter filterMode,
149 GrSamplerState::WrapMode wrapXAndY,
150 GrShaderFlags visibility) {
151 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
152 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
153 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
154 fVisibility = visibility;
155}