]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.cpp
Decouple the Predicate enum from 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, min_filter);
40         if(dirty_params&MAG_FILTER)
41                 glSamplerParameteri(id, GL_TEXTURE_MAG_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, wrap_s);
46         if(dirty_params&WRAP_T)
47                 glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap_t);
48         if(dirty_params&WRAP_R)
49                 glSamplerParameteri(id, GL_TEXTURE_WRAP_R, 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 void operator>>(const LexicalConverter &c, TextureFilter &tf)
222 {
223         if(c.get()=="NEAREST")
224                 tf = NEAREST;
225         else if(c.get()=="LINEAR")
226                 tf = LINEAR;
227         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
228                 tf = NEAREST_MIPMAP_NEAREST;
229         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
230                 tf = NEAREST_MIPMAP_LINEAR;
231         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
232                 tf = LINEAR_MIPMAP_NEAREST;
233         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
234                 tf = LINEAR_MIPMAP_LINEAR;
235         else
236                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
237 }
238
239 void operator>>(const LexicalConverter &c, TextureWrap &tw)
240 {
241         if(c.get()=="REPEAT")
242                 tw = REPEAT;
243         else if(c.get()=="CLAMP_TO_EDGE")
244                 tw = CLAMP_TO_EDGE;
245         else if(c.get()=="CLAMP_TO_BORDER")
246                 tw = CLAMP_TO_BORDER;
247         else if(c.get()=="MIRRORED_REPEAT")
248                 tw = MIRRORED_REPEAT;
249         else
250                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
251 }
252
253
254 } // namespace GL
255 } // namespace Msp