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