Overte C++ Documentation
Sampler.h
1 //
2 // Sampler.h
3 // libraries/shared/src
4 //
5 // Created by HifiExperiments on 8/21/25
6 // Copyright 2025 Overte e.V.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_Sampler_h
13 #define hifi_Sampler_h
14 
15 #include <glm/glm.hpp>
16 
17 #include "RegisteredMetaTypes.h"
18 #include "SerDes.h"
19 
20 enum class ComparisonFunction {
21  NEVER = 0,
22  LESS,
23  EQUAL,
24  LESS_EQUAL,
25  GREATER,
26  NOT_EQUAL,
27  GREATER_EQUAL,
28  ALWAYS,
29 
30  NUM_COMPARISON_FUNCS,
31 };
32 
33 class Sampler {
34 public:
35 
36  enum Filter {
37  FILTER_MIN_MAG_POINT, // top mip only
38  FILTER_MIN_POINT_MAG_LINEAR, // top mip only
39  FILTER_MIN_LINEAR_MAG_POINT, // top mip only
40  FILTER_MIN_MAG_LINEAR, // top mip only
41 
42  FILTER_MIN_MAG_MIP_POINT,
43  FILTER_MIN_MAG_POINT_MIP_LINEAR,
44  FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT,
45  FILTER_MIN_POINT_MAG_MIP_LINEAR,
46  FILTER_MIN_LINEAR_MAG_MIP_POINT,
47  FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR,
48  FILTER_MIN_MAG_LINEAR_MIP_POINT,
49  FILTER_MIN_MAG_MIP_LINEAR,
50  FILTER_ANISOTROPIC,
51 
52  NUM_FILTERS,
53  };
54 
55  enum WrapMode {
56  WRAP_REPEAT = 0,
57  WRAP_MIRROR,
58  WRAP_CLAMP,
59  WRAP_BORDER,
60  WRAP_MIRROR_ONCE,
61 
62  NUM_WRAP_MODES
63  };
64 
65  static const uint8_t MAX_MIP_LEVEL = 0xFF;
66 
67  class Desc {
68  public:
69  glm::vec4 _borderColor{ 1.0f };
70  uint32_t _maxAnisotropy = 16;
71 
72  uint8_t _filter = FILTER_MIN_MAG_MIP_LINEAR;
73  uint8_t _comparisonFunc = (uint8_t)ComparisonFunction::ALWAYS;
74 
75  uint8_t _wrapModeU = WRAP_REPEAT;
76  uint8_t _wrapModeV = WRAP_REPEAT;
77  uint8_t _wrapModeW = WRAP_REPEAT;
78 
79  uint8_t _mipOffset = 0;
80  uint8_t _minMip = 0;
81  uint8_t _maxMip = MAX_MIP_LEVEL;
82 
83  Desc() {}
84  Desc(const Filter filter, const WrapMode wrap = WRAP_REPEAT) : _filter(filter), _wrapModeU(wrap), _wrapModeV(wrap), _wrapModeW(wrap) {}
85 
86  bool operator==(const Desc& other) const {
87  return _borderColor == other._borderColor &&
88  _maxAnisotropy == other._maxAnisotropy &&
89  _filter == other._filter &&
90  _comparisonFunc == other._comparisonFunc &&
91  _wrapModeU == other._wrapModeU &&
92  _wrapModeV == other._wrapModeV &&
93  _wrapModeW == other._wrapModeW &&
94  _mipOffset == other._mipOffset &&
95  _minMip == other._minMip &&
96  _maxMip == other._maxMip;
97  }
98  };
99  static_assert(sizeof(Desc) == 28, "Sampler::Desc size doesn't match.");
100 
101  Sampler() {}
102  Sampler(const Filter filter, const WrapMode wrap = WRAP_REPEAT) : _desc(filter, wrap) {}
103  Sampler(const Desc& desc) : _desc(desc) {}
104  ~Sampler() {}
105 
106  const glm::vec4& getBorderColor() const { return _desc._borderColor; }
107 
108  uint32_t getMaxAnisotropy() const { return _desc._maxAnisotropy; }
109 
110  WrapMode getWrapModeU() const { return WrapMode(_desc._wrapModeU); }
111  WrapMode getWrapModeV() const { return WrapMode(_desc._wrapModeV); }
112  WrapMode getWrapModeW() const { return WrapMode(_desc._wrapModeW); }
113 
114  Filter getFilter() const { return Filter(_desc._filter); }
115  ComparisonFunction getComparisonFunction() const { return ComparisonFunction(_desc._comparisonFunc); }
116  bool doComparison() const { return getComparisonFunction() != ComparisonFunction::ALWAYS; }
117 
118  uint8_t getMipOffset() const { return _desc._mipOffset; }
119  uint8_t getMinMip() const { return _desc._minMip; }
120  uint8_t getMaxMip() const { return _desc._maxMip; }
121 
122  const Desc& getDesc() const { return _desc; }
123 
124  bool operator==(const Sampler& other) const {
125  return _desc == other._desc;
126  }
127  bool operator!=(const Sampler& other) const {
128  return !(*this == other);
129  }
130 
131  static Sampler parseSampler(const QJsonObject& object);
132 
133 protected:
134  Desc _desc;
135 };
136 
137 inline DataSerializer& operator<<(DataSerializer& ser, const Sampler::Desc& d) {
138  DataSerializer::SizeTracker tracker(ser);
139  ser << d._borderColor;
140  ser << d._maxAnisotropy;
141  ser << d._filter;
142  ser << d._comparisonFunc;
143  ser << d._wrapModeU;
144  ser << d._wrapModeV;
145  ser << d._wrapModeW;
146  ser << d._mipOffset;
147  ser << d._minMip;
148  ser << d._maxMip;
149  return ser;
150 }
151 
152 inline DataDeserializer& operator>>(DataDeserializer& dsr, Sampler::Desc& d) {
153  DataDeserializer::SizeTracker tracker(dsr);
154  dsr >> d._borderColor;
155  dsr >> d._maxAnisotropy;
156  dsr >> d._filter;
157  dsr >> d._comparisonFunc;
158  dsr >> d._wrapModeU;
159  dsr >> d._wrapModeV;
160  dsr >> d._wrapModeW;
161  dsr >> d._mipOffset;
162  dsr >> d._minMip;
163  dsr >> d._maxMip;
164  return dsr;
165 }
166 
167 namespace std {
168  template<> struct hash<Sampler> {
169  size_t operator()(const Sampler& sampler) const noexcept {
170  size_t result = 0;
171  const auto& desc = sampler.getDesc();
172  hash_combine(result, desc._comparisonFunc, desc._filter, desc._maxAnisotropy, desc._maxMip, desc._minMip, desc._wrapModeU, desc._wrapModeV, desc._wrapModeW);
173  return result;
174  }
175  };
176 }
177 
178 QDebug& operator<<(QDebug& dbg, const Sampler& s);
179 
180 QString wrapModeToString(Sampler::WrapMode mode);
181 Sampler::WrapMode wrapModeFromString(const QString& string);
182 
183 #endif // hifi_Sampler_h
RAII tracker of advance position.
Definition: SerDes.h:594
Data deserializer.
Definition: SerDes.h:573
RAII tracker of advance position.
Definition: SerDes.h:82
Data serializer.
Definition: SerDes.h:61