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