joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 1 | /* |
| 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 | /** |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 13 | * We specialize the vertex code for each of these matrix types. |
| 14 | */ |
| 15 | enum MatrixType { |
| 16 | kNoPersp_MatrixType = 0, |
| 17 | kGeneral_MatrixType = 1, |
| 18 | }; |
| 19 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame^] | 20 | GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {} |
| 21 | |
| 22 | const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const { |
| 23 | SkASSERT(i >= 0 && i < this->numTextureSamplers()); |
| 24 | return this->onTextureSampler(i); |
| 25 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 26 | |
| 27 | const 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 | |
| 34 | const 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 |
| 42 | size_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 | |
| 50 | size_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 | |
| 58 | size_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 | |
| 67 | size_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 Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame^] | 77 | void GrPrimitiveProcessor::addPendingIOs() const { |
| 78 | for (int i = 0; i < fTextureSamplerCnt; ++i) { |
| 79 | this->textureSampler(i).programProxy()->markPendingIO(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void GrPrimitiveProcessor::removeRefs() const { |
| 84 | for (int i = 0; i < fTextureSamplerCnt; ++i) { |
| 85 | this->textureSampler(i).programProxy()->removeRef(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void GrPrimitiveProcessor::pendingIOComplete() const { |
| 90 | for (int i = 0; i < fTextureSamplerCnt; ++i) { |
| 91 | this->textureSampler(i).programProxy()->pendingIOComplete(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | bool 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 104 | uint32_t |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 105 | GrPrimitiveProcessor::getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 106 | int numCoords) const { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 107 | uint32_t totalKey = 0; |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 108 | for (int t = 0; t < numCoords; ++t) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 109 | 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 Salomon | 969bdef | 2018-06-06 17:16:05 -0400 | [diff] [blame] | 116 | key <<= t; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 117 | SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap |
| 118 | totalKey |= key; |
| 119 | } |
| 120 | return totalKey; |
| 121 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame^] | 122 | |
| 123 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | GrPrimitiveProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy, |
| 126 | const GrSamplerState& samplerState, |
| 127 | GrShaderFlags visibility) { |
| 128 | this->reset(std::move(proxy), samplerState, visibility); |
| 129 | } |
| 130 | |
| 131 | GrPrimitiveProcessor::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 | |
| 138 | void 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 | |
| 147 | void 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 | } |