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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 77 | uint32_t |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 78 | GrPrimitiveProcessor::getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 79 | int numCoords) const { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 80 | uint32_t totalKey = 0; |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 81 | for (int t = 0; t < numCoords; ++t) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 82 | 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 Salomon | 969bdef | 2018-06-06 17:16:05 -0400 | [diff] [blame] | 89 | key <<= t; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 90 | SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap |
| 91 | totalKey |= key; |
| 92 | } |
| 93 | return totalKey; |
| 94 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 97 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 98 | static 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 Salomon | af87483 | 2018-08-03 11:53:40 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 106 | GrPrimitiveProcessor::TextureSampler::TextureSampler(GrTextureType textureType, |
| 107 | GrPixelConfig config, |
| 108 | const GrSamplerState& samplerState, |
| 109 | GrShaderFlags visibility) { |
| 110 | this->reset(textureType, config, samplerState, visibility); |
| 111 | } |
| 112 | |
| 113 | GrPrimitiveProcessor::TextureSampler::TextureSampler(GrTextureType textureType, |
| 114 | GrPixelConfig config, |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 115 | GrSamplerState::Filter filterMode, |
| 116 | GrSamplerState::WrapMode wrapXAndY, |
| 117 | GrShaderFlags visibility) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 118 | this->reset(textureType, config, filterMode, wrapXAndY, visibility); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 121 | void GrPrimitiveProcessor::TextureSampler::reset(GrTextureType textureType, |
| 122 | GrPixelConfig config, |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 123 | const GrSamplerState& samplerState, |
| 124 | GrShaderFlags visibility) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 125 | SkASSERT(kUnknown_GrPixelConfig != config); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 126 | fSamplerState = samplerState; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 127 | fSamplerState.setFilterMode(clamp_filter(textureType, samplerState.filter())); |
| 128 | fTextureType = textureType; |
| 129 | fConfig = config; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 130 | fVisibility = visibility; |
| 131 | } |
| 132 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 133 | void GrPrimitiveProcessor::TextureSampler::reset(GrTextureType textureType, |
| 134 | GrPixelConfig config, |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 135 | GrSamplerState::Filter filterMode, |
| 136 | GrSamplerState::WrapMode wrapXAndY, |
| 137 | GrShaderFlags visibility) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 138 | SkASSERT(kUnknown_GrPixelConfig != config); |
| 139 | filterMode = clamp_filter(textureType, filterMode); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 140 | fSamplerState = GrSamplerState(wrapXAndY, filterMode); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 141 | fTextureType = textureType; |
| 142 | fConfig = config; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 143 | fVisibility = visibility; |
| 144 | } |