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