]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.cpp
Add an abstraction for queries
[libs/gl.git] / source / core / sampler.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_sampler_objects.h>
3 #include <msp/gl/extensions/arb_shadow.h>
4 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/gl/extensions/khr_debug.h>
7 #include <msp/strings/format.h>
8 #include "error.h"
9 #include "sampler.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Sampler::Sampler():
17         min_filter(NEAREST_MIPMAP_LINEAR),
18         mag_filter(LINEAR),
19         max_anisotropy(1.0f),
20         wrap_s(REPEAT),
21         wrap_t(REPEAT),
22         wrap_r(REPEAT),
23         border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
24         compare(false),
25         cmp_func(LEQUAL),
26         dirty_params(0)
27 {
28         Require _req(ARB_sampler_objects);
29         if(ARB_direct_state_access)
30                 glCreateSamplers(1, &id);
31         else
32                 glGenSamplers(1, &id);
33 }
34
35 void Sampler::update() const
36 {
37         if(dirty_params&MIN_FILTER)
38                 glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, get_gl_filter(min_filter));
39         if(dirty_params&MAG_FILTER)
40                 glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, get_gl_filter(mag_filter));
41         if(dirty_params&MAX_ANISOTROPY)
42                 glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
43         if(dirty_params&WRAP_S)
44                 glSamplerParameteri(id, GL_TEXTURE_WRAP_S, get_gl_wrap(wrap_s));
45         if(dirty_params&WRAP_T)
46                 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, get_gl_wrap(wrap_t));
47         if(dirty_params&WRAP_R)
48                 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, get_gl_wrap(wrap_r));
49         if(dirty_params&BORDER_COLOR)
50                 glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r);
51         if(dirty_params&COMPARE)
52         {
53                 glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
54                 if(compare)
55                         glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(cmp_func));
56         }
57
58         dirty_params = 0;
59 }
60
61 void Sampler::set_min_filter(TextureFilter f)
62 {
63         min_filter = f;
64         dirty_params |= MIN_FILTER;
65 }
66
67 void Sampler::set_mag_filter(TextureFilter f)
68 {
69         mag_filter = f;
70         dirty_params |= MAG_FILTER;
71 }
72
73 void Sampler::set_filter(TextureFilter f)
74 {
75         set_min_filter(f);
76         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
77 }
78
79 void Sampler::set_max_anisotropy(float a)
80 {
81         if(a<1.0f)
82                 throw invalid_argument("Sampler::set_max_anisotropy");
83         else if(a>1.0f)
84                 static Require _req(EXT_texture_filter_anisotropic);
85         max_anisotropy = a;
86         if(EXT_texture_filter_anisotropic)
87                 dirty_params |= MAX_ANISOTROPY;
88 }
89
90 void Sampler::set_wrap(TextureWrap w)
91 {
92         set_wrap_s(w);
93         set_wrap_t(w);
94         if(EXT_texture3D)
95                 set_wrap_r(w);
96 }
97
98 void Sampler::set_wrap_s(TextureWrap w)
99 {
100         wrap_s = w;
101         dirty_params |= WRAP_S;
102 }
103
104 void Sampler::set_wrap_t(TextureWrap w)
105 {
106         wrap_t = w;
107         dirty_params |= WRAP_T;
108 }
109
110 void Sampler::set_wrap_r(TextureWrap w)
111 {
112         static Require _req(EXT_texture3D);
113         wrap_r = w;
114         dirty_params |= WRAP_R;
115 }
116
117 void Sampler::set_border_color(const Color &c)
118 {
119         border_color = c;
120         dirty_params |= BORDER_COLOR;
121 }
122
123 void Sampler::disable_compare()
124 {
125         compare = false;
126         dirty_params |= COMPARE;
127 }
128
129 void Sampler::set_compare(Predicate f)
130 {
131         static Require _req(ARB_shadow);
132         compare = true;
133         cmp_func = f;
134         dirty_params |= COMPARE;
135 }
136
137 void Sampler::set_debug_name(const string &name)
138 {
139 #ifdef DEBUG
140         if(id && KHR_debug)
141                 glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
142 #else
143         (void)name;
144 #endif
145 }
146
147
148 Sampler::Loader::Loader(Sampler &s):
149         DataFile::ObjectLoader<Sampler>(s)
150 {
151         add("border_color", &Loader::border_color);
152         add("compare", &Loader::compare);
153         add("filter", &Loader::filter);
154         add("mag_filter", &Loader::mag_filter);
155         add("max_anisotropy", &Loader::max_anisotropy);
156         add("min_filter", &Loader::min_filter);
157         add("wrap", &Loader::wrap);
158         add("wrap_r", &Loader::wrap_r);
159         add("wrap_s", &Loader::wrap_s);
160         add("wrap_t", &Loader::wrap_t);
161 }
162
163 void Sampler::Loader::border_color(float r, float g, float b, float a)
164 {
165         obj.set_border_color(Color(r, g, b, a));
166 }
167
168 void Sampler::Loader::compare(Predicate c)
169 {
170         obj.set_compare(c);
171 }
172
173 void Sampler::Loader::filter(TextureFilter f)
174 {
175         obj.set_filter(f);
176 }
177
178 void Sampler::Loader::mag_filter(TextureFilter f)
179 {
180         obj.set_mag_filter(f);
181 }
182
183 void Sampler::Loader::max_anisotropy(float a)
184 {
185         obj.set_max_anisotropy(a);
186 }
187
188 void Sampler::Loader::min_filter(TextureFilter f)
189 {
190         obj.set_min_filter(f);
191 }
192
193 void Sampler::Loader::wrap(TextureWrap w)
194 {
195         obj.set_wrap(w);
196 }
197
198 void Sampler::Loader::wrap_r(TextureWrap w)
199 {
200         obj.set_wrap_r(w);
201 }
202
203 void Sampler::Loader::wrap_s(TextureWrap w)
204 {
205         obj.set_wrap_s(w);
206 }
207
208 void Sampler::Loader::wrap_t(TextureWrap w)
209 {
210         obj.set_wrap_t(w);
211 }
212
213
214 bool is_mipmapped(TextureFilter filter)
215 {
216         return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
217                 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
218 }
219
220 unsigned get_gl_filter(TextureFilter filter)
221 {
222         switch(filter)
223         {
224         case NEAREST: return GL_NEAREST;
225         case LINEAR: return GL_LINEAR;
226         case NEAREST_MIPMAP_NEAREST: return GL_NEAREST_MIPMAP_NEAREST;
227         case NEAREST_MIPMAP_LINEAR: return GL_NEAREST_MIPMAP_LINEAR;
228         case LINEAR_MIPMAP_NEAREST: return GL_LINEAR_MIPMAP_NEAREST;
229         case LINEAR_MIPMAP_LINEAR: return GL_LINEAR_MIPMAP_LINEAR;
230         default: throw invalid_argument("get_gl_filter");
231         }
232 }
233
234 unsigned get_gl_wrap(TextureWrap wrap)
235 {
236         switch(wrap)
237         {
238         case REPEAT: return GL_REPEAT;
239         case CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
240         case CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
241         case MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
242         default: throw invalid_argument("get_gl_wrap");
243         }
244 }
245
246 void operator>>(const LexicalConverter &c, TextureFilter &tf)
247 {
248         if(c.get()=="NEAREST")
249                 tf = NEAREST;
250         else if(c.get()=="LINEAR")
251                 tf = LINEAR;
252         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
253                 tf = NEAREST_MIPMAP_NEAREST;
254         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
255                 tf = NEAREST_MIPMAP_LINEAR;
256         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
257                 tf = LINEAR_MIPMAP_NEAREST;
258         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
259                 tf = LINEAR_MIPMAP_LINEAR;
260         else
261                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
262 }
263
264 void operator>>(const LexicalConverter &c, TextureWrap &tw)
265 {
266         if(c.get()=="REPEAT")
267                 tw = REPEAT;
268         else if(c.get()=="CLAMP_TO_EDGE")
269                 tw = CLAMP_TO_EDGE;
270         else if(c.get()=="CLAMP_TO_BORDER")
271                 tw = CLAMP_TO_BORDER;
272         else if(c.get()=="MIRRORED_REPEAT")
273                 tw = MIRRORED_REPEAT;
274         else
275                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
276 }
277
278
279 } // namespace GL
280 } // namespace Msp