]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.cpp
Remove automatic mipmap generation from the public interface of Texture
[libs/gl.git] / source / core / texture.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_swizzle.h>
3 #include <msp/gl/extensions/ext_framebuffer_object.h>
4 #include <msp/gl/extensions/khr_debug.h>
5 #include <msp/io/memory.h>
6 #include "error.h"
7 #include "resourcemanager.h"
8 #include "resources.h"
9 #include "texture.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 int Texture::swizzle_orders[] =
17 {
18         GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
19         GL_RED, GL_RED, GL_RED, GL_ONE,
20         GL_RED, GL_RED, GL_RED, GL_GREEN,
21         GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
22 };
23
24 Texture *Texture::scratch_binding = 0;
25
26 Texture::Texture(GLenum t, ResourceManager *m):
27         id(0),
28         target(t),
29         format(RGB8),
30         storage_fmt(RGB8),
31         swizzle(NO_SWIZZLE),
32         use_srgb_format(false),
33         auto_gen_mipmap(false)
34 {
35         if(m)
36                 set_manager(m);
37         else
38                 generate_id();
39
40         static bool alignment_init = false;
41         if(!alignment_init)
42         {
43                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
44                 alignment_init = true;
45         }
46 }
47
48 Texture::~Texture()
49 {
50         if(this==scratch_binding)
51                 unbind_scratch();
52         if(id)
53                 glDeleteTextures(1, &id);
54 }
55
56 void Texture::generate_id()
57 {
58         if(id)
59                 throw invalid_operation("Texture::generate_id");
60         if(ARB_direct_state_access)
61                 glCreateTextures(target, 1, &id);
62         else
63                 glGenTextures(1, &id);
64
65 #ifdef DEBUG
66         if(!debug_name.empty() && KHR_debug)
67                 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
68 #endif
69 }
70
71 void Texture::set_format(PixelFormat fmt)
72 {
73         PixelComponents comp = get_components(fmt);
74         PixelComponents st_comp = comp;
75         FormatSwizzle swiz = NO_SWIZZLE;
76         switch(comp)
77         {
78         case LUMINANCE:
79                 st_comp = RED;
80                 swiz = R_TO_LUMINANCE;
81                 break;
82         case LUMINANCE_ALPHA:
83                 st_comp = RG;
84                 swiz = RG_TO_LUMINANCE_ALPHA;
85                 break;
86         case BGR:
87                 st_comp = RGB;
88                 swiz = RGB_TO_BGR;
89                 break;
90         case BGRA:
91                 st_comp = RGBA;
92                 swiz = RGB_TO_BGR;
93                 break;
94         default:;
95         }
96
97         PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
98         require_pixelformat(st_fmt);
99         if(swiz!=NO_SWIZZLE)
100                 static Require _req(ARB_texture_swizzle);
101
102         format = fmt;
103         storage_fmt = st_fmt;
104         swizzle = swiz;
105 }
106
107 void Texture::apply_swizzle()
108 {
109         if(swizzle==NO_SWIZZLE)
110                 return;
111
112         if(get_gl_api()==OPENGL_ES2)
113         {
114                 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
115                 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
116                 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
117                 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
118         }
119         else
120         {
121                 if(ARB_direct_state_access)
122                         glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
123                 else
124                         glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
125         }
126 }
127
128 void Texture::set_parameter_i(GLenum param, int value) const
129 {
130         if(ARB_direct_state_access)
131                 glTextureParameteri(id, param, value);
132         else
133                 glTexParameteri(target, param, value);
134 }
135
136 void Texture::generate_mipmap()
137 {
138         // glGenerateMipmap is defined here
139         static Require _req(EXT_framebuffer_object);
140
141         if(ARB_direct_state_access)
142                 glGenerateTextureMipmap(id);
143         else
144         {
145                 bind_scratch();
146                 glGenerateMipmap(target);
147         }
148 }
149
150 void Texture::load_image(const string &fn, bool)
151 {
152         load_image(fn, 0U);
153 }
154
155 void Texture::load_image(const string &fn, unsigned lv)
156 {
157         Graphics::Image img;
158         img.load_file(fn);
159
160         image(img, lv);
161 }
162
163 void Texture::image(const Graphics::Image &img, bool)
164 {
165         image(img, 0U);
166 }
167
168 void Texture::set_debug_name(const string &name)
169 {
170 #ifdef DEBUG
171         debug_name = name;
172         if(id && KHR_debug)
173                 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
174 #else
175         (void)name;
176 #endif
177 }
178
179 void Texture::bind_scratch()
180 {
181         if(!scratch_binding)
182                 glActiveTexture(GL_TEXTURE0);
183         if(scratch_binding!=this)
184         {
185                 if(scratch_binding && scratch_binding->target!=target)
186                         glBindTexture(scratch_binding->target, 0);
187                 glBindTexture(target, id);
188                 scratch_binding = this;
189         }
190 }
191
192 void Texture::unbind_scratch()
193 {
194         if(scratch_binding)
195         {
196                 glBindTexture(scratch_binding->target, 0);
197                 scratch_binding = 0;
198         }
199 }
200
201
202 Texture::Loader::Loader(Texture &t):
203         DataFile::CollectionObjectLoader<Texture>(t, 0)
204 {
205         init();
206 }
207
208 Texture::Loader::Loader(Texture &t, Collection &c):
209         DataFile::CollectionObjectLoader<Texture>(t, &c)
210 {
211         init();
212 }
213
214 void Texture::Loader::init()
215 {
216         levels = 0;
217
218         add("external_image", &Loader::external_image);
219         add("external_image_srgb", &Loader::external_image_srgb);
220         add("generate_mipmap", &Loader::generate_mipmap);
221         add("image_data", &Loader::image_data);
222         add("mipmap_levels", &Loader::mipmap_levels);
223 }
224
225 void Texture::Loader::finish()
226 {
227         if(obj.auto_gen_mipmap)
228                 obj.generate_mipmap();
229 }
230
231 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
232 {
233         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
234         if(!io)
235                 throw IO::file_not_found(fn);
236         img.load_io(*io);
237 }
238
239 void Texture::Loader::external_image(const string &fn)
240 {
241         obj.use_srgb_format = false;
242         external_image_common(fn);
243 }
244
245 void Texture::Loader::external_image_srgb(const string &fn)
246 {
247         obj.use_srgb_format = true;
248         external_image_common(fn);
249 }
250
251 void Texture::Loader::external_image_common(const string &fn)
252 {
253         if(obj.manager)
254                 obj.manager->set_resource_location(obj, get_collection(), fn);
255         else
256         {
257                 Graphics::Image img;
258                 load_external_image(img, fn);
259                 obj.image(img, levels);
260         }
261 }
262
263 void Texture::Loader::generate_mipmap(bool gm)
264 {
265         obj.auto_gen_mipmap = gm;
266 }
267
268 void Texture::Loader::image_data(const string &data)
269 {
270         if(obj.manager)
271         {
272                 obj.set_manager(0);
273                 if(!obj.id)
274                         obj.generate_id();
275         }
276
277         Graphics::Image img;
278         IO::Memory mem(data.data(), data.size());
279         img.load_io(mem);
280
281         obj.image(img, levels);
282 }
283
284 void Texture::Loader::mipmap_levels(unsigned l)
285 {
286         levels = l;
287 }
288
289 } // namespace GL
290 } // namespace Msp