]> git.tdb.fi Git - libs/gl.git/blob - source/core/sampler.cpp
Use a streaming buffer for ProgramData only when needed
[libs/gl.git] / source / core / sampler.cpp
1 #include <msp/strings/format.h>
2 #include "device.h"
3 #include "error.h"
4 #include "sampler.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 void Sampler::update() const
12 {
13         SamplerBackend::update(dirty_params);
14         dirty_params = 0;
15 }
16
17 void Sampler::set_min_filter(TextureFilter f)
18 {
19         min_filter = f;
20         dirty_params |= MIN_FILTER;
21 }
22
23 void Sampler::set_mag_filter(TextureFilter f)
24 {
25         if(is_mipmapped(f))
26                 throw invalid_argument("Sampler::set_mag_filter");
27         mag_filter = f;
28         dirty_params |= MAG_FILTER;
29 }
30
31 void Sampler::set_filter(TextureFilter f)
32 {
33         set_min_filter(f);
34         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
35 }
36
37 void Sampler::set_max_anisotropy(float a)
38 {
39         if(a<1.0f)
40                 throw invalid_argument("Sampler::set_max_anisotropy");
41         if(a>Device::get_current().get_info().limits.max_anisotropy)
42                 throw out_of_range("Sampler::set_max_anisotropy");
43         bool supported = check_anisotropic(a>1.0f);
44         max_anisotropy = a;
45         if(supported)
46                 dirty_params |= MAX_ANISOTROPY;
47 }
48
49 void Sampler::set_wrap_s(TextureWrap w)
50 {
51         wrap_s = w;
52         dirty_params |= WRAP_S;
53 }
54
55 void Sampler::set_wrap_t(TextureWrap w)
56 {
57         wrap_t = w;
58         dirty_params |= WRAP_T;
59 }
60
61 void Sampler::set_wrap_r(TextureWrap w)
62 {
63         wrap_r = w;
64         dirty_params |= WRAP_R;
65 }
66
67 void Sampler::set_wrap(TextureWrap w)
68 {
69         set_wrap_s(w);
70         set_wrap_t(w);
71         set_wrap_r(w);
72 }
73
74 void Sampler::set_border_color(const Color &c)
75 {
76         border_color = c;
77         dirty_params |= BORDER_COLOR;
78 }
79
80 void Sampler::disable_compare()
81 {
82         compare = false;
83         dirty_params |= COMPARE;
84 }
85
86 void Sampler::set_compare(Predicate f)
87 {
88         compare = true;
89         cmp_func = f;
90         dirty_params |= COMPARE;
91 }
92
93
94 Sampler::Loader::Loader(Sampler &s):
95         DataFile::ObjectLoader<Sampler>(s)
96 {
97         add("border_color", &Loader::border_color);
98         add("compare", &Loader::compare);
99         add("filter", &Loader::filter);
100         add("mag_filter", &Loader::mag_filter);
101         add("max_anisotropy", &Loader::max_anisotropy);
102         add("min_filter", &Loader::min_filter);
103         add("wrap", &Loader::wrap);
104         add("wrap_r", &Loader::wrap_r);
105         add("wrap_s", &Loader::wrap_s);
106         add("wrap_t", &Loader::wrap_t);
107 }
108
109 void Sampler::Loader::border_color(float r, float g, float b, float a)
110 {
111         obj.set_border_color(Color(r, g, b, a));
112 }
113
114 void Sampler::Loader::compare(Predicate c)
115 {
116         obj.set_compare(c);
117 }
118
119 void Sampler::Loader::filter(TextureFilter f)
120 {
121         obj.set_filter(f);
122 }
123
124 void Sampler::Loader::mag_filter(TextureFilter f)
125 {
126         obj.set_mag_filter(f);
127 }
128
129 void Sampler::Loader::max_anisotropy(float a)
130 {
131         obj.set_max_anisotropy(a);
132 }
133
134 void Sampler::Loader::min_filter(TextureFilter f)
135 {
136         obj.set_min_filter(f);
137 }
138
139 void Sampler::Loader::wrap(TextureWrap w)
140 {
141         obj.set_wrap(w);
142 }
143
144 void Sampler::Loader::wrap_r(TextureWrap w)
145 {
146         obj.set_wrap_r(w);
147 }
148
149 void Sampler::Loader::wrap_s(TextureWrap w)
150 {
151         obj.set_wrap_s(w);
152 }
153
154 void Sampler::Loader::wrap_t(TextureWrap w)
155 {
156         obj.set_wrap_t(w);
157 }
158
159
160 bool is_mipmapped(TextureFilter filter)
161 {
162         return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
163                 filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
164 }
165
166 void operator>>(const LexicalConverter &c, TextureFilter &tf)
167 {
168         if(c.get()=="NEAREST")
169                 tf = NEAREST;
170         else if(c.get()=="LINEAR")
171                 tf = LINEAR;
172         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
173                 tf = NEAREST_MIPMAP_NEAREST;
174         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
175                 tf = NEAREST_MIPMAP_LINEAR;
176         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
177                 tf = LINEAR_MIPMAP_NEAREST;
178         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
179                 tf = LINEAR_MIPMAP_LINEAR;
180         else
181                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
182 }
183
184 void operator>>(const LexicalConverter &c, TextureWrap &tw)
185 {
186         if(c.get()=="REPEAT")
187                 tw = REPEAT;
188         else if(c.get()=="CLAMP_TO_EDGE")
189                 tw = CLAMP_TO_EDGE;
190         else if(c.get()=="CLAMP_TO_BORDER")
191                 tw = CLAMP_TO_BORDER;
192         else if(c.get()=="MIRRORED_REPEAT")
193                 tw = MIRRORED_REPEAT;
194         else
195                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
196 }
197
198
199 } // namespace GL
200 } // namespace Msp