1 | /* |
2 | * Copyright 2015 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 | #ifndef GrSamplerState_DEFINED |
9 | #define GrSamplerState_DEFINED |
10 | |
11 | #include "include/gpu/GrTypes.h" |
12 | #include <limits> |
13 | |
14 | /** |
15 | * Represents the filtering and tile modes used to access a texture. |
16 | */ |
17 | class GrSamplerState { |
18 | public: |
19 | enum class Filter : uint8_t { kNearest, kLinear, kLast = kLinear }; |
20 | enum class MipmapMode : uint8_t { kNone, kNearest, kLinear, kLast = kLinear }; |
21 | |
22 | enum class WrapMode : uint8_t { |
23 | kClamp, |
24 | kRepeat, |
25 | kMirrorRepeat, |
26 | kClampToBorder, |
27 | kLast = kClampToBorder |
28 | }; |
29 | |
30 | static constexpr int kFilterCount = static_cast<int>(Filter::kLast) + 1; |
31 | static constexpr int kWrapModeCount = static_cast<int>(WrapMode::kLast) + 1; |
32 | |
33 | constexpr GrSamplerState() = default; |
34 | |
35 | constexpr GrSamplerState(WrapMode wrapXAndY, Filter filter, MipmapMode mm = MipmapMode::kNone) |
36 | : fWrapModes{wrapXAndY, wrapXAndY}, fFilter(filter), fMipmapMode(mm) {} |
37 | |
38 | constexpr GrSamplerState(WrapMode wrapX, |
39 | WrapMode wrapY, |
40 | Filter filter, |
41 | MipmapMode mm = MipmapMode::kNone) |
42 | : fWrapModes{wrapX, wrapY}, fFilter(filter), fMipmapMode(mm) {} |
43 | |
44 | constexpr GrSamplerState(const WrapMode wrapModes[2], |
45 | Filter filter, |
46 | MipmapMode mm = MipmapMode::kNone) |
47 | : fWrapModes{wrapModes[0], wrapModes[1]}, fFilter(filter), fMipmapMode(mm) {} |
48 | |
49 | constexpr /*explicit*/ GrSamplerState(Filter filter) : fFilter(filter) {} |
50 | constexpr GrSamplerState(Filter filter, MipmapMode mm) : fFilter(filter), fMipmapMode(mm) {} |
51 | |
52 | constexpr GrSamplerState(const GrSamplerState&) = default; |
53 | |
54 | constexpr GrSamplerState& operator=(const GrSamplerState&) = default; |
55 | |
56 | constexpr WrapMode wrapModeX() const { return fWrapModes[0]; } |
57 | |
58 | constexpr WrapMode wrapModeY() const { return fWrapModes[1]; } |
59 | |
60 | constexpr bool isRepeated() const { |
61 | return fWrapModes[0] == WrapMode::kRepeat || fWrapModes[0] == WrapMode::kMirrorRepeat || |
62 | fWrapModes[1] == WrapMode::kRepeat || fWrapModes[1] == WrapMode::kMirrorRepeat; |
63 | } |
64 | |
65 | constexpr Filter filter() const { return fFilter; } |
66 | |
67 | constexpr MipmapMode mipmapMode() const { return fMipmapMode; } |
68 | |
69 | constexpr GrMipmapped mipmapped() const { |
70 | return GrMipmapped(fMipmapMode != MipmapMode::kNone); |
71 | } |
72 | |
73 | constexpr void setFilterMode(Filter filterMode) { fFilter = filterMode; } |
74 | |
75 | constexpr void setMipmapMode(MipmapMode mm) { fMipmapMode = mm; } |
76 | |
77 | constexpr void setWrapModeX(const WrapMode wrap) { fWrapModes[0] = wrap; } |
78 | |
79 | constexpr void setWrapModeY(const WrapMode wrap) { fWrapModes[1] = wrap; } |
80 | |
81 | constexpr bool operator==(GrSamplerState that) const { |
82 | return fWrapModes[0] == that.fWrapModes[0] && fWrapModes[1] == that.fWrapModes[1] && |
83 | fFilter == that.fFilter && fMipmapMode == that.fMipmapMode; |
84 | } |
85 | |
86 | constexpr bool operator!=(const GrSamplerState& that) const { return !(*this == that); } |
87 | |
88 | /** |
89 | * Turn the sampler state into an integer from a tightly packed range for use as an index |
90 | * (or key) |
91 | */ |
92 | constexpr uint8_t asIndex() const { |
93 | constexpr int kNumWraps = static_cast<int>(WrapMode::kLast) + 1; |
94 | constexpr int kNumFilters = static_cast<int>(Filter::kLast ) + 1; |
95 | int result = static_cast<int>(fWrapModes[0])*1 |
96 | + static_cast<int>(fWrapModes[1])*kNumWraps |
97 | + static_cast<int>(fFilter) *kNumWraps*kNumWraps |
98 | + static_cast<int>(fMipmapMode) *kNumWraps*kNumWraps*kNumFilters; |
99 | SkASSERT(result <= kNumUniqueSamplers); |
100 | return static_cast<uint8_t>(result); |
101 | } |
102 | |
103 | static constexpr int kNumUniqueSamplers = (static_cast<int>(WrapMode::kLast ) + 1) |
104 | * (static_cast<int>(WrapMode::kLast ) + 1) |
105 | * (static_cast<int>(Filter::kLast ) + 1) |
106 | * (static_cast<int>(MipmapMode::kLast) + 1); |
107 | private: |
108 | WrapMode fWrapModes[2] = {WrapMode::kClamp, WrapMode::kClamp}; |
109 | Filter fFilter = GrSamplerState::Filter::kNearest; |
110 | MipmapMode fMipmapMode = GrSamplerState::MipmapMode::kNone; |
111 | }; |
112 | |
113 | static_assert(GrSamplerState::kNumUniqueSamplers <= |
114 | std::numeric_limits<decltype(GrSamplerState{}.asIndex())>::max()); |
115 | |
116 | #endif |
117 | |