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