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/GrCoordTransform.h"
11 #include "src/gpu/GrFragmentProcessor.h"
12
13 /**
14 * We specialize the vertex code for each of these matrix types.
15 */
16 enum MatrixType {
17 kNone_MatrixType = 0,
18 kNoPersp_MatrixType = 1,
19 kGeneral_MatrixType = 2,
20 };
21
GrPrimitiveProcessor(ClassID classID)22 GrPrimitiveProcessor::GrPrimitiveProcessor(ClassID classID) : GrProcessor(classID) {}
23
textureSampler(int i) const24 const GrPrimitiveProcessor::TextureSampler& GrPrimitiveProcessor::textureSampler(int i) const {
25 SkASSERT(i >= 0 && i < this->numTextureSamplers());
26 return this->onTextureSampler(i);
27 }
28
computeCoordTransformsKey(const GrFragmentProcessor & fp) const29 uint32_t GrPrimitiveProcessor::computeCoordTransformsKey(const GrFragmentProcessor& fp) const {
30 // This is highly coupled with the code in GrGLSLGeometryProcessor::emitTransforms().
31 SkASSERT(fp.numCoordTransforms() * 2 <= 32);
32 uint32_t totalKey = 0;
33 for (int t = 0; t < fp.numCoordTransforms(); ++t) {
34 uint32_t key = 0;
35 const GrCoordTransform& coordTransform = fp.coordTransform(t);
36 if (fp.isSampledWithExplicitCoords() && coordTransform.isNoOp()) {
37 key = kNone_MatrixType;
38 } else if (coordTransform.matrix().hasPerspective()) {
39 // Note that we can also have homogeneous varyings as a result of a GP local matrix or
40 // homogeneous local coords generated by GP. We're relying on the GP to include any
41 // variability in those in its key.
42 key = kGeneral_MatrixType;
43 } else {
44 key = kNoPersp_MatrixType;
45 }
46 key <<= 2*t;
47 SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap
48 totalKey |= key;
49 }
50 return totalKey;
51 }
52
53 ///////////////////////////////////////////////////////////////////////////////////////////////////
54
clamp_filter(GrTextureType type,GrSamplerState::Filter requestedFilter)55 static inline GrSamplerState::Filter clamp_filter(GrTextureType type,
56 GrSamplerState::Filter requestedFilter) {
57 if (GrTextureTypeHasRestrictedSampling(type)) {
58 return std::min(requestedFilter, GrSamplerState::Filter::kBilerp);
59 }
60 return requestedFilter;
61 }
62
TextureSampler(GrSamplerState samplerState,const GrBackendFormat & backendFormat,const GrSwizzle & swizzle)63 GrPrimitiveProcessor::TextureSampler::TextureSampler(GrSamplerState samplerState,
64 const GrBackendFormat& backendFormat,
65 const GrSwizzle& swizzle) {
66 this->reset(samplerState, backendFormat, swizzle);
67 }
68
reset(GrSamplerState samplerState,const GrBackendFormat & backendFormat,const GrSwizzle & swizzle)69 void GrPrimitiveProcessor::TextureSampler::reset(GrSamplerState samplerState,
70 const GrBackendFormat& backendFormat,
71 const GrSwizzle& swizzle) {
72 fSamplerState = samplerState;
73 fSamplerState.setFilterMode(clamp_filter(backendFormat.textureType(), samplerState.filter()));
74 fBackendFormat = backendFormat;
75 fSwizzle = swizzle;
76 fIsInitialized = true;
77 }
78