| 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 "src/gpu/GrPrimitiveProcessor.h" |
| 9 | |
| 10 | #include "src/gpu/GrFragmentProcessor.h" |
| 11 | |
| 12 | /** |
| 13 | * We specialize the vertex or fragment coord transform code for these matrix types, and where |
| 14 | * the transform code is applied. |
| 15 | */ |
| 16 | enum SampleFlag { |
| 17 | kExplicitlySampled_Flag = 0b00001, // GrFP::isSampledWithExplicitCoords() |
| 18 | |
| 19 | kNone_SampleMatrix_Flag = 0b00100, // GrFP::sampleUsage()::hasMatrix() == false |
| 20 | kUniform_SampleMatrix_Flag = 0b01000, // GrFP::sampleUsage()::hasUniformMatrix() |
| 21 | kVariable_SampleMatrix_Flag = 0b01100, // GrFP::sampleUsage()::hasVariableMatrix() |
| 22 | |
| 23 | // Currently, sample(matrix) only specializes on no-perspective or general. |
| 24 | // FIXME add new flags as more matrix types are supported. |
| 25 | kPersp_Matrix_Flag = 0b10000, // GrFP::sampleUsage()::fHasPerspective |
| 26 | }; |
| 27 | |
| 28 | GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {} |
| 29 | |
| 30 | const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const { |
| 31 | SkASSERT(i >= 0 && i < this->numTextureSamplers()); |
| 32 | return this->onTextureSampler(i); |
| 33 | } |
| 34 | |
| 35 | uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const { |
| 36 | // This is highly coupled with the code in GrGLSLGeometryProcessor::collectTransforms(). |
| 37 | |
| 38 | uint32_t key = 0; |
| 39 | if (fp.isSampledWithExplicitCoords()) { |
| 40 | key |= kExplicitlySampled_Flag; |
| 41 | } |
| 42 | |
| 43 | switch(fp.sampleUsage().fKind) { |
| 44 | case SkSL::SampleUsage::Kind::kNone: |
| 45 | key |= kNone_SampleMatrix_Flag; |
| 46 | break; |
| 47 | case SkSL::SampleUsage::Kind::kUniform: |
| 48 | key |= kUniform_SampleMatrix_Flag; |
| 49 | break; |
| 50 | case SkSL::SampleUsage::Kind::kVariable: |
| 51 | key |= kVariable_SampleMatrix_Flag; |
| 52 | break; |
| 53 | } |
| 54 | if (fp.sampleUsage().fHasPerspective) { |
| 55 | key |= kPersp_Matrix_Flag; |
| 56 | } |
| 57 | |
| 58 | return key; |
| 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 62 | |
| 63 | static inline GrSamplerState::Filter clamp_filter(GrTextureType type, |
| 64 | GrSamplerState::Filter requestedFilter) { |
| 65 | if (GrTextureTypeHasRestrictedSampling(type)) { |
| 66 | return std::min(requestedFilter, GrSamplerState::Filter::kLinear); |
| 67 | } |
| 68 | return requestedFilter; |
| 69 | } |
| 70 | |
| 71 | GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState, |
| 72 | const GrBackendFormat& backendFormat, |
| 73 | const GrSwizzle& swizzle) { |
| 74 | this->reset(samplerState, backendFormat, swizzle); |
| 75 | } |
| 76 | |
| 77 | void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState, |
| 78 | const GrBackendFormat& backendFormat, |
| 79 | const GrSwizzle& swizzle) { |
| 80 | fSamplerState = samplerState; |
| 81 | fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter())); |
| 82 | fBackendFormat = backendFormat; |
| 83 | fSwizzle = swizzle; |
| 84 | fIsInitialized = true; |
| 85 | } |
| 86 | |