]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.cpp
Remove default sampler from Texture
[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 #include "texunit.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 Sampler::Sampler():
19         min_filter(NEAREST_MIPMAP_LINEAR),
20         mag_filter(LINEAR),
21         max_anisotropy(1.0f),
22         wrap_s(REPEAT),
23         wrap_t(REPEAT),
24         wrap_r(REPEAT),
25         border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)),
26         compare(false),
27         cmp_func(LEQUAL),
28         dirty_params(0)
29 {
30         Require _req(ARB_sampler_objects);
31         if(ARB_direct_state_access)
32                 glCreateSamplers(1, &id);
33         else
34                 glGenSamplers(1, &id);
35 }
36
37 void Sampler::update_parameter(int mask) const
38 {
39         if(mask&MIN_FILTER)
40                 glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, min_filter);
41         if(mask&MAG_FILTER)
42                 glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, mag_filter);
43         if(mask&MAX_ANISOTROPY)
44                 glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
45         if(mask&WRAP_S)
46                 glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap_s);
47         if(mask&WRAP_T)
48                 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap_t);
49         if(mask&WRAP_R)
50                 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, wrap_r);
51         if(mask&BORDER_COLOR)
52                 glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r);
53         if(mask&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, cmp_func);
58         }
59 }
60
61 void Sampler::set_min_filter(TextureFilter f)
62 {
63         min_filter = f;
64         update_parameter(MIN_FILTER);
65 }
66
67 void Sampler::set_mag_filter(TextureFilter f)
68 {
69         mag_filter = f;
70         update_parameter(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                 update_parameter(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         update_parameter(WRAP_S);
102 }
103
104 void Sampler::set_wrap_t(TextureWrap w)
105 {
106         wrap_t = w;
107         update_parameter(WRAP_T);
108 }
109
110 void Sampler::set_wrap_r(TextureWrap w)
111 {
112         static Require _req(EXT_texture3D);
113         wrap_r = w;
114         update_parameter(WRAP_R);
115 }
116
117 void Sampler::set_border_color(const Color &c)
118 {
119         border_color = c;
120         update_parameter(BORDER_COLOR);
121 }
122
123 void Sampler::disable_compare()
124 {
125         compare = false;
126         update_parameter(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::bind_to(unsigned i) const
138 {
139         TexUnit &unit = TexUnit::get_unit(i);
140
141         if(unit.set_sampler(this))
142         {
143                 glBindSampler(i, id);
144
145                 if(dirty_params)
146                 {
147                         update_parameter(dirty_params);
148                         dirty_params = 0;
149                 }
150         }
151 }
152
153 const Sampler *Sampler::current(unsigned i)
154 {
155         return TexUnit::get_unit(i).get_sampler();
156 }
157
158 void Sampler::unbind_from(unsigned i)
159 {
160         TexUnit &unit = TexUnit::get_unit(i);
161         if(unit.set_sampler(0))
162                 glBindSampler(i, 0);
163 }
164
165 void Sampler::set_debug_name(const string &name)
166 {
167 #ifdef DEBUG
168         if(id && KHR_debug)
169                 glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
170 #else
171         (void)name;
172 #endif
173 }
174
175
176 Sampler::Loader::Loader(Sampler &s):
177         DataFile::ObjectLoader<Sampler>(s)
178 {
179         add("border_color", &Loader::border_color);
180         add("compare", &Loader::compare);
181         add("filter", &Loader::filter);
182         add("mag_filter", &Loader::mag_filter);
183         add("max_anisotropy", &Loader::max_anisotropy);
184         add("min_filter", &Loader::min_filter);
185         add("wrap", &Loader::wrap);
186         add("wrap_r", &Loader::wrap_r);
187         add("wrap_s", &Loader::wrap_s);
188         add("wrap_t", &Loader::wrap_t);
189 }
190
191 void Sampler::Loader::border_color(float r, float g, float b, float a)
192 {
193         obj.set_border_color(Color(r, g, b, a));
194 }
195
196 void Sampler::Loader::compare(Predicate c)
197 {
198         obj.set_compare(c);
199 }
200
201 void Sampler::Loader::filter(TextureFilter f)
202 {
203         obj.set_filter(f);
204 }
205
206 void Sampler::Loader::mag_filter(TextureFilter f)
207 {
208         obj.set_mag_filter(f);
209 }
210
211 void Sampler::Loader::max_anisotropy(float a)
212 {
213         obj.set_max_anisotropy(a);
214 }
215
216 void Sampler::Loader::min_filter(TextureFilter f)
217 {
218         obj.set_min_filter(f);
219 }
220
221 void Sampler::Loader::wrap(TextureWrap w)
222 {
223         obj.set_wrap(w);
224 }
225
226 void Sampler::Loader::wrap_r(TextureWrap w)
227 {
228         obj.set_wrap_r(w);
229 }
230
231 void Sampler::Loader::wrap_s(TextureWrap w)
232 {
233         obj.set_wrap_s(w);
234 }
235
236 void Sampler::Loader::wrap_t(TextureWrap w)
237 {
238         obj.set_wrap_t(w);
239 }
240
241
242 bool is_mipmapped(TextureFilter filter)
243 {
244         return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
245                 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
246 }
247
248 void operator>>(const LexicalConverter &c, TextureFilter &tf)
249 {
250         if(c.get()=="NEAREST")
251                 tf = NEAREST;
252         else if(c.get()=="LINEAR")
253                 tf = LINEAR;
254         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
255                 tf = NEAREST_MIPMAP_NEAREST;
256         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
257                 tf = NEAREST_MIPMAP_LINEAR;
258         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
259                 tf = LINEAR_MIPMAP_NEAREST;
260         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
261                 tf = LINEAR_MIPMAP_LINEAR;
262         else
263                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
264 }
265
266 void operator>>(const LexicalConverter &c, TextureWrap &tw)
267 {
268         if(c.get()=="REPEAT")
269                 tw = REPEAT;
270         else if(c.get()=="CLAMP_TO_EDGE")
271                 tw = CLAMP_TO_EDGE;
272         else if(c.get()=="CLAMP_TO_BORDER")
273                 tw = CLAMP_TO_BORDER;
274         else if(c.get()=="MIRRORED_REPEAT")
275                 tw = MIRRORED_REPEAT;
276         else
277                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
278 }
279
280
281 } // namespace GL
282 } // namespace Msp