Panda3D
samplerState.h
1 // Filename: samplerState.h
2 // Created by: rdb (09Dec14)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef SAMPLERSTATE_H
16 #define SAMPLERSTATE_H
17 
18 #include "pandabase.h"
19 
20 #include "typedObject.h"
21 #include "namable.h"
22 #include "luse.h"
23 #include "numeric_types.h"
24 #include "bamReader.h"
25 #include "config_gobj.h"
26 
27 class FactoryParams;
30 class SamplerContext;
31 
32 ////////////////////////////////////////////////////////////////////
33 // Class : SamplerState
34 // Description : Represents a set of settings that indicate how a
35 // texture is sampled. This can be used to sample the
36 // same texture using different settings in different
37 // places.
38 ////////////////////////////////////////////////////////////////////
39 class EXPCL_PANDA_GOBJ SamplerState {
40 PUBLISHED:
41  enum FilterType {
42  // Mag Filter and Min Filter
43 
44  // Point sample the pixel
45  FT_nearest,
46 
47  // Bilinear filtering of four neighboring pixels
48  FT_linear,
49 
50  // Min Filter Only
51 
52  // Point sample the pixel from the nearest mipmap level
53  FT_nearest_mipmap_nearest,
54 
55  // Bilinear filter the pixel from the nearest mipmap level
56  FT_linear_mipmap_nearest,
57 
58  // Point sample the pixel from two mipmap levels, and linearly blend
59  FT_nearest_mipmap_linear,
60 
61  // A.k.a. trilinear filtering: Bilinear filter the pixel from
62  // two mipmap levels, and linearly blend the results.
63  FT_linear_mipmap_linear,
64 
65  // The OpenGL ARB_shadow extension can be thought of as a kind of filtering.
66  FT_shadow,
67 
68  // Default is usually linear, but it depends on format.
69  // This was added at the end of the list to avoid bumping TXO version #.
70  FT_default,
71 
72  // Returned by string_filter_type() for an invalid match.
73  FT_invalid
74  };
75 
76  enum WrapMode {
77  WM_clamp, // coords that would be outside [0-1] are clamped to 0 or 1
78  WM_repeat,
79  WM_mirror,
80  WM_mirror_once, // mirror once, then clamp
81  WM_border_color, // coords outside [0-1] use explicit border color
82  // Returned by string_wrap_mode() for an invalid match.
83  WM_invalid
84  };
85 
86  INLINE SamplerState();
87  INLINE static const SamplerState &get_default();
88 
89  INLINE void set_wrap_u(WrapMode wrap);
90  INLINE void set_wrap_v(WrapMode wrap);
91  INLINE void set_wrap_w(WrapMode wrap);
92  INLINE void set_minfilter(FilterType filter);
93  INLINE void set_magfilter(FilterType filter);
94  INLINE void set_anisotropic_degree(int anisotropic_degree);
95  INLINE void set_border_color(const LColor &color);
96  INLINE void set_min_lod(PN_stdfloat min_lod);
97  INLINE void set_max_lod(PN_stdfloat max_lod);
98  INLINE void set_lod_bias(PN_stdfloat lod_bias);
99 
100  INLINE WrapMode get_wrap_u() const;
101  INLINE WrapMode get_wrap_v() const;
102  INLINE WrapMode get_wrap_w() const;
103  INLINE FilterType get_minfilter() const;
104  INLINE FilterType get_magfilter() const;
105  FilterType get_effective_minfilter() const;
106  FilterType get_effective_magfilter() const;
107  INLINE int get_anisotropic_degree() const;
108  INLINE int get_effective_anisotropic_degree() const;
109  INLINE const LColor &get_border_color() const;
110  INLINE PN_stdfloat get_min_lod() const;
111  INLINE PN_stdfloat get_max_lod() const;
112  INLINE PN_stdfloat get_lod_bias() const;
113 
114  INLINE bool uses_mipmaps() const;
115  INLINE static bool is_mipmap(FilterType type);
116 
117  static string format_filter_type(FilterType ft);
118  static FilterType string_filter_type(const string &str);
119 
120  static string format_wrap_mode(WrapMode wm);
121  static WrapMode string_wrap_mode(const string &str);
122 
123  INLINE bool operator == (const SamplerState &other) const;
124  INLINE bool operator != (const SamplerState &other) const;
125  INLINE bool operator < (const SamplerState &other) const;
126 
127  void prepare(PreparedGraphicsObjects *prepared_objects) const;
128  bool is_prepared(PreparedGraphicsObjects *prepared_objects) const;
129  void release(PreparedGraphicsObjects *prepared_objects) const;
130 
131  SamplerContext *prepare_now(PreparedGraphicsObjects *prepared_objects,
132  GraphicsStateGuardianBase *gsg) const;
133 
134 public:
135  int compare_to(const SamplerState &other) const;
136 
137  void output(ostream &out) const;
138  void write(ostream &out, int indent) const;
139 
140 private:
141  LColor _border_color;
142  PN_stdfloat _min_lod;
143  PN_stdfloat _max_lod;
144  PN_stdfloat _lod_bias;
145 
146  // These are packed in a way that this class conveniently fits in
147  // 32 bytes; feel free to change the packing as necessary when
148  // more enum values are added.
149  FilterType _minfilter : 4;
150  FilterType _magfilter : 4;
151  WrapMode _wrap_u : 4;
152  WrapMode _wrap_v : 4;
153  WrapMode _wrap_w : 4;
154  int _anisotropic_degree : 12;
155 
156  static SamplerState _default;
157 
158 public:
159  void write_datagram(Datagram &destination) const;
160  void read_datagram(DatagramIterator &source, BamReader *manager);
161 
162 public:
163  static TypeHandle get_class_type() {
164  return _type_handle;
165  }
166  static void init_type() {
168  register_type(_type_handle, "SamplerState",
169  TypedObject::get_class_type());
170  }
171  /*virtual TypeHandle get_type() const {
172  return get_class_type();
173  }
174  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}*/
175 
176 private:
177 
178  static TypeHandle _type_handle;
179 };
180 
181 extern EXPCL_PANDA_GOBJ ConfigVariableEnum<SamplerState::FilterType> texture_minfilter;
182 extern EXPCL_PANDA_GOBJ ConfigVariableEnum<SamplerState::FilterType> texture_magfilter;
183 extern EXPCL_PANDA_GOBJ ConfigVariableInt texture_anisotropic_degree;
184 
185 INLINE ostream &operator << (ostream &out, const SamplerState &m) {
186  m.output(out);
187  return out;
188 }
189 
190 INLINE ostream &operator << (ostream &out, SamplerState::FilterType ft) {
191  return out << SamplerState::format_filter_type(ft);
192 }
193 
194 INLINE istream &operator >> (istream &in, SamplerState::FilterType &ft) {
195  string word;
196  in >> word;
198  return in;
199 }
200 
201 INLINE ostream &operator << (ostream &out, SamplerState::WrapMode wm) {
202  return out << SamplerState::format_wrap_mode(wm);
203 }
204 
205 INLINE istream &operator >> (istream &in, SamplerState::WrapMode &wm) {
206  string word;
207  in >> word;
209  return in;
210 }
211 
212 #include "samplerState.I"
213 
214 #endif
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:52
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
static string format_wrap_mode(WrapMode wm)
Returns the indicated WrapMode converted to a string word.
static string format_filter_type(FilterType ft)
Returns the indicated FilterType converted to a string word.
A table of objects that are saved within the graphics context for reference by handle later...
static FilterType string_filter_type(const string &str)
Returns the FilterType value associated with the given string representation, or FT_invalid if the st...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This class specializes ConfigVariable as an enumerated type.
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:39
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
This is a special class object that holds a handle to the sampler state object given by the graphics ...
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
This is a convenience class to specialize ConfigVariable as an integer type.
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
static WrapMode string_wrap_mode(const string &str)
Returns the WrapMode value associated with the given string representation, or WM_invalid if the stri...