Actually implement multiple inputs to phases. Surprising amounts of stuff needed...
[movit] / effect_chain.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #include <GL/gl.h>
8 #include <GL/glext.h>
9
10 #include <algorithm>
11 #include <set>
12
13 #include "util.h"
14 #include "effect_chain.h"
15 #include "gamma_expansion_effect.h"
16 #include "gamma_compression_effect.h"
17 #include "lift_gamma_gain_effect.h"
18 #include "colorspace_conversion_effect.h"
19 #include "sandbox_effect.h"
20 #include "saturation_effect.h"
21 #include "mirror_effect.h"
22 #include "vignette_effect.h"
23 #include "blur_effect.h"
24
25 EffectChain::EffectChain(unsigned width, unsigned height)
26         : width(width),
27           height(height),
28           last_added_effect(NULL),
29           use_srgb_texture_format(false),
30           finalized(false) {}
31
32 void EffectChain::add_input(const ImageFormat &format)
33 {
34         input_format = format;
35         output_color_space.insert(std::make_pair(static_cast<Effect *>(NULL), format.color_space));
36         output_gamma_curve.insert(std::make_pair(static_cast<Effect *>(NULL), format.gamma_curve));
37         effect_ids.insert(std::make_pair(static_cast<Effect *>(NULL), "src_image"));
38 }
39
40 void EffectChain::add_output(const ImageFormat &format)
41 {
42         output_format = format;
43 }
44
45 void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
46 {
47         char effect_id[256];
48         sprintf(effect_id, "eff%u", (unsigned)effects.size());
49
50         effects.push_back(effect);
51         effect_ids.insert(std::make_pair(effect, effect_id));
52         assert(inputs.size() == effect->num_inputs());
53         for (unsigned i = 0; i < inputs.size(); ++i) {
54                 if (inputs[i] != NULL) {
55                         assert(std::find(effects.begin(), effects.end(), inputs[i]) != effects.end());
56                 }
57                 outgoing_links[inputs[i]].push_back(effect);
58         }
59         incoming_links.insert(std::make_pair(effect, inputs));
60         last_added_effect = effect;
61 }
62
63 Effect *instantiate_effect(EffectId effect)
64 {
65         switch (effect) {
66         case EFFECT_GAMMA_EXPANSION:
67                 return new GammaExpansionEffect();
68         case EFFECT_GAMMA_COMPRESSION:
69                 return new GammaCompressionEffect();
70         case EFFECT_COLOR_SPACE_CONVERSION:
71                 return new ColorSpaceConversionEffect();
72         case EFFECT_SANDBOX:
73                 return new SandboxEffect();
74         case EFFECT_LIFT_GAMMA_GAIN:
75                 return new LiftGammaGainEffect();
76         case EFFECT_SATURATION:
77                 return new SaturationEffect();
78         case EFFECT_MIRROR:
79                 return new MirrorEffect();
80         case EFFECT_VIGNETTE:
81                 return new VignetteEffect();
82         case EFFECT_BLUR:
83                 return new BlurEffect();
84         }
85         assert(false);
86 }
87
88 Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
89 {
90         GammaCurve current_gamma_curve = output_gamma_curve[input];
91         if (current_gamma_curve == GAMMA_sRGB) {
92                 // TODO: check if the extension exists
93                 use_srgb_texture_format = true;
94                 current_gamma_curve = GAMMA_LINEAR;
95                 return input;
96         } else {
97                 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
98                 gamma_conversion->set_int("source_curve", current_gamma_curve);
99                 std::vector<Effect *> inputs;
100                 inputs.push_back(input);
101                 gamma_conversion->add_self_to_effect_chain(this, inputs);
102                 current_gamma_curve = GAMMA_LINEAR;
103                 return gamma_conversion;
104         }
105 }
106
107 Effect *EffectChain::normalize_to_srgb(Effect *input)
108 {
109         GammaCurve current_gamma_curve = output_gamma_curve[input];
110         ColorSpace current_color_space = output_color_space[input];
111         assert(current_gamma_curve == GAMMA_LINEAR);
112         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
113         colorspace_conversion->set_int("source_space", current_color_space);
114         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
115         std::vector<Effect *> inputs;
116         inputs.push_back(input);
117         colorspace_conversion->add_self_to_effect_chain(this, inputs);
118         current_color_space = COLORSPACE_sRGB;
119         return colorspace_conversion;
120 }
121
122 Effect *EffectChain::add_effect(EffectId effect_id, const std::vector<Effect *> &inputs)
123 {
124         Effect *effect = instantiate_effect(effect_id);
125
126         assert(inputs.size() == effect->num_inputs());
127
128         std::vector<Effect *> normalized_inputs = inputs;
129         for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
130                 if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
131                         normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
132                 }
133                 if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
134                         normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
135                 }
136         }
137
138         effect->add_self_to_effect_chain(this, normalized_inputs);
139         return effect;
140 }
141
142 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
143 std::string replace_prefix(const std::string &text, const std::string &prefix)
144 {
145         std::string output;
146         size_t start = 0;
147
148         while (start < text.size()) {
149                 size_t pos = text.find("PREFIX(", start);
150                 if (pos == std::string::npos) {
151                         output.append(text.substr(start, std::string::npos));
152                         break;
153                 }
154
155                 output.append(text.substr(start, pos - start));
156                 output.append(prefix);
157                 output.append("_");
158
159                 pos += strlen("PREFIX(");
160         
161                 // Output stuff until we find the matching ), which we then eat.
162                 int depth = 1;
163                 size_t end_arg_pos = pos;
164                 while (end_arg_pos < text.size()) {
165                         if (text[end_arg_pos] == '(') {
166                                 ++depth;
167                         } else if (text[end_arg_pos] == ')') {
168                                 --depth;
169                                 if (depth == 0) {
170                                         break;
171                                 }
172                         }
173                         ++end_arg_pos;
174                 }
175                 output.append(text.substr(pos, end_arg_pos - pos));
176                 ++end_arg_pos;
177                 assert(depth == 0);
178                 start = end_arg_pos;
179         }
180         return output;
181 }
182
183 EffectChain::Phase EffectChain::compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects)
184 {
185         assert(!inputs.empty());
186         assert(!effects.empty());
187
188         // Figure out the true set of inputs to this phase. These are the ones
189         // that we need somehow but don't calculate ourselves.
190         std::set<Effect *> effect_set(effects.begin(), effects.end());
191         std::set<Effect *> input_set(inputs.begin(), inputs.end());
192         std::vector<Effect *> true_inputs;
193         std::set_difference(input_set.begin(), input_set.end(),
194                 effect_set.begin(), effect_set.end(),
195                 std::back_inserter(true_inputs));
196
197         bool input_needs_mipmaps = false;
198         std::string frag_shader = read_file("header.frag");
199
200         // Create functions for all the texture inputs that we need.
201         for (unsigned i = 0; i < true_inputs.size(); ++i) {
202                 Effect *effect = true_inputs[i];
203                 assert(effect_ids.count(effect) != 0);
204                 std::string effect_id = effect_ids[effect];
205         
206                 frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";       
207                 frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
208                 if (effect == NULL) {
209                         // OpenGL's origin is bottom-left, but most graphics software assumes
210                         // a top-left origin. Thus, for inputs that come from the user,
211                         // we flip the y coordinate. However, for FBOs, the origin
212                         // is all correct, so don't do anything.
213                         frag_shader += "\ttc.y = 1.0f - tc.y;\n";
214                 }
215                 frag_shader += "\treturn texture2D(tex_" + effect_id + ", tc);\n";
216                 frag_shader += "}\n";
217                 frag_shader += "\n";
218         }
219
220         std::string last_effect_id;
221         for (unsigned i = 0; i < effects.size(); ++i) {
222                 Effect *effect = effects[i];
223                 assert(effect != NULL);
224                 assert(effect_ids.count(effect) != 0);
225                 std::string effect_id = effect_ids[effect];
226                 last_effect_id = effect_id;
227
228                 if (incoming_links[effect].size() == 1) {
229                         frag_shader += std::string("#define INPUT ") + effect_ids[incoming_links[effect][0]] + "\n";
230                 } else {
231                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
232                                 char buf[256];
233                                 sprintf(buf, "#define INPUT%d %s\n", j + 1, effect_ids[incoming_links[effect][j]].c_str());
234                                 frag_shader += buf;
235                         }
236                 }
237         
238                 frag_shader += "\n";
239                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
240                 frag_shader += replace_prefix(effect->output_convenience_uniforms(), effect_id);
241                 frag_shader += replace_prefix(effect->output_fragment_shader(), effect_id);
242                 frag_shader += "#undef PREFIX\n";
243                 frag_shader += "#undef FUNCNAME\n";
244                 if (incoming_links[effect].size() == 1) {
245                         frag_shader += "#undef INPUT\n";
246                 } else {
247                         for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
248                                 char buf[256];
249                                 sprintf(buf, "#undef INPUT%d\n", j + 1);
250                                 frag_shader += buf;
251                         }
252                 }
253                 frag_shader += "\n";
254
255                 input_needs_mipmaps |= effect->needs_mipmaps();
256         }
257         assert(!last_effect_id.empty());
258         frag_shader += std::string("#define INPUT ") + last_effect_id + "\n";
259         frag_shader.append(read_file("footer.frag"));
260         printf("%s\n", frag_shader.c_str());
261         
262         GLuint glsl_program_num = glCreateProgram();
263         GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
264         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
265         glAttachShader(glsl_program_num, vs_obj);
266         check_error();
267         glAttachShader(glsl_program_num, fs_obj);
268         check_error();
269         glLinkProgram(glsl_program_num);
270         check_error();
271
272         Phase phase;
273         phase.glsl_program_num = glsl_program_num;
274         phase.input_needs_mipmaps = input_needs_mipmaps;
275         phase.inputs = true_inputs;
276         phase.effects = effects;
277
278         return phase;
279 }
280
281 // Construct GLSL programs, starting at the given effect and following
282 // the chain from there. We end a program every time we come to an effect
283 // marked as "needs texture bounce", one that is used by multiple other
284 // effects, and of course at the end.
285 void EffectChain::construct_glsl_programs(Effect *start, std::set<Effect *> *completed_effects)
286 {
287         if (completed_effects->count(start) != 0) {
288                 // This has already been done for us.
289                 return;
290         }
291
292         std::vector<Effect *> this_phase_inputs;  // Also includes all intermediates; these will be filtered away later.
293         std::vector<Effect *> this_phase_effects;
294         Effect *node = start;
295         for ( ;; ) {  // Termination condition within loop.
296                 if (node == NULL) {
297                         this_phase_inputs.push_back(node);
298                 } else {
299                         // Check that we have all the inputs we need for this effect.
300                         // If not, we end the phase here right away; the other side
301                         // of the input chain will eventually come and pick the effect up.
302                         assert(incoming_links.count(node) != 0);
303                         std::vector<Effect *> deps = incoming_links[node];
304                         assert(!deps.empty());
305                         bool have_all_deps = true;
306                         for (unsigned i = 0; i < deps.size(); ++i) {
307                                 if (completed_effects->count(deps[i]) == 0) {
308                                         have_all_deps = false;
309                                         break;
310                                 }
311                         }
312                 
313                         if (!have_all_deps) {
314                                 if (!this_phase_effects.empty()) {
315                                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
316                                 }
317                                 return;
318                         }
319                         this_phase_inputs.insert(this_phase_inputs.end(), deps.begin(), deps.end());    
320                         this_phase_effects.push_back(node);
321                 }
322                 completed_effects->insert(node);        
323
324                 // Find all the effects that use this one as a direct input.
325                 if (outgoing_links.count(node) == 0) {
326                         // End of the line; output.
327                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
328                         return;
329                 }
330
331                 std::vector<Effect *> next = outgoing_links[node];
332                 assert(!next.empty());
333                 if (next.size() > 1) {
334                         // More than one effect uses this as the input.
335                         // The easiest thing to do (and probably also the safest
336                         // performance-wise in most cases) is to bounce it to a texture
337                         // and then let the next passes read from that.
338                         if (node != NULL) {
339                                 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
340                         }
341
342                         // Start phases for all the effects that need us (in arbitrary order).
343                         for (unsigned i = 0; i < next.size(); ++i) {
344                                 construct_glsl_programs(next[i], completed_effects);
345                         }
346                         return;
347                 }
348         
349                 // OK, only one effect uses this as the input. Keep iterating,
350                 // but first see if it requires a texture bounce; if so, give it
351                 // one by starting a new phase.
352                 node = next[0];
353                 if (node->needs_texture_bounce()) {
354                         phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
355                         this_phase_inputs.clear();
356                         this_phase_effects.clear();
357                 }
358         }
359 }
360
361 void EffectChain::finalize()
362 {
363         // Add normalizers to get the output format right.
364         GammaCurve current_gamma_curve = output_gamma_curve[last_added_effect];  // FIXME
365         ColorSpace current_color_space = output_color_space[last_added_effect];  // FIXME
366         if (current_color_space != output_format.color_space) {
367                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
368                 colorspace_conversion->set_int("source_space", current_color_space);
369                 colorspace_conversion->set_int("destination_space", output_format.color_space);
370                 effects.push_back(colorspace_conversion);
371                 current_color_space = output_format.color_space;
372         }
373         if (current_gamma_curve != output_format.gamma_curve) {
374                 if (current_gamma_curve != GAMMA_LINEAR) {
375                         normalize_to_linear_gamma(last_added_effect);  // FIXME
376                 }
377                 assert(current_gamma_curve == GAMMA_LINEAR);
378                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
379                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
380                 effects.push_back(gamma_conversion);
381                 current_gamma_curve = output_format.gamma_curve;
382         }
383
384         // Construct all needed GLSL programs, starting at the input.
385         std::set<Effect *> completed_effects;
386         construct_glsl_programs(NULL, &completed_effects);
387
388         // If we have more than one phase, we need intermediate render-to-texture.
389         // Construct an FBO, and then as many textures as we need.
390         // We choose the simplest option of having one texture per output,
391         // since otherwise this turns into an (albeit simple)
392         // register allocation problem.
393         if (phases.size() > 1) {
394                 glGenFramebuffers(1, &fbo);
395
396                 for (unsigned i = 0; i < phases.size() - 1; ++i) {
397                         Effect *output_effect = phases[i].effects.back();
398                         GLuint temp_texture;
399                         glGenTextures(1, &temp_texture);
400                         check_error();
401                         glBindTexture(GL_TEXTURE_2D, temp_texture);
402                         check_error();
403                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
404                         check_error();
405                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
406                         check_error();
407                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
408                         check_error();
409                         effect_output_textures.insert(std::make_pair(output_effect, temp_texture));
410                 }
411         }
412         
413         // Translate the input format to OpenGL's enums.
414         GLenum internal_format;
415         if (use_srgb_texture_format) {
416                 internal_format = GL_SRGB8;
417         } else {
418                 internal_format = GL_RGBA8;
419         }
420         if (input_format.pixel_format == FORMAT_RGB) {
421                 format = GL_RGB;
422                 bytes_per_pixel = 3;
423         } else if (input_format.pixel_format == FORMAT_RGBA) {
424                 format = GL_RGBA;
425                 bytes_per_pixel = 4;
426         } else if (input_format.pixel_format == FORMAT_BGR) {
427                 format = GL_BGR;
428                 bytes_per_pixel = 3;
429         } else if (input_format.pixel_format == FORMAT_BGRA) {
430                 format = GL_BGRA;
431                 bytes_per_pixel = 4;
432         } else {
433                 assert(false);
434         }
435
436         // Create PBO to hold the texture holding the input image, and then the texture itself.
437         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
438         check_error();
439         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
440         check_error();
441         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
442         check_error();
443         
444         glGenTextures(1, &source_image_num);
445         check_error();
446         glBindTexture(GL_TEXTURE_2D, source_image_num);
447         check_error();
448         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
449         check_error();
450         // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here.
451         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, phases[0].input_needs_mipmaps ? GL_TRUE : GL_FALSE);
452         check_error();
453         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
454         check_error();
455
456         finalized = true;
457 }
458
459 void EffectChain::render_to_screen(unsigned char *src)
460 {
461         assert(finalized);
462
463         // Copy the pixel data into the PBO.
464         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
465         check_error();
466         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
467         memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
468         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
469         check_error();
470
471         // Re-upload the texture from the PBO.
472         glActiveTexture(GL_TEXTURE0);
473         check_error();
474         glBindTexture(GL_TEXTURE_2D, source_image_num);
475         check_error();
476         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
477         check_error();
478         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
479         check_error();
480         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
481         check_error();
482         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
483         check_error();
484
485         // Basic state.
486         glDisable(GL_BLEND);
487         check_error();
488         glDisable(GL_DEPTH_TEST);
489         check_error();
490         glDepthMask(GL_FALSE);
491         check_error();
492
493         glMatrixMode(GL_PROJECTION);
494         glLoadIdentity();
495         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
496
497         glMatrixMode(GL_MODELVIEW);
498         glLoadIdentity();
499
500         if (phases.size() > 1) {
501                 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
502                 check_error();
503         }
504
505         std::set<Effect *> generated_mipmaps;
506         generated_mipmaps.insert(NULL);  // Already done further up.
507
508         for (unsigned phase = 0; phase < phases.size(); ++phase) {
509                 glUseProgram(phases[phase].glsl_program_num);
510                 check_error();
511
512                 // Set up inputs for this phase.
513                 assert(!phases[phase].inputs.empty());
514                 for (unsigned sampler = 0; sampler < phases[phase].inputs.size(); ++sampler) {
515                         glActiveTexture(GL_TEXTURE0 + sampler);
516                         Effect *input = phases[phase].inputs[sampler];
517                         if (input == NULL) {
518                                 glBindTexture(GL_TEXTURE_2D, source_image_num);
519                                 check_error();
520                         } else {
521                                 assert(effect_output_textures.count(input) != 0);
522                                 glBindTexture(GL_TEXTURE_2D, effect_output_textures[input]);
523                                 check_error();
524                         }
525                         if (phases[phase].input_needs_mipmaps) {
526                                 if (generated_mipmaps.count(input) == 0) {
527                                         glGenerateMipmap(GL_TEXTURE_2D);
528                                         check_error();
529                                         generated_mipmaps.insert(input);
530                                 }
531                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
532                                 check_error();
533                         } else {
534                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
535                                 check_error();
536                         }
537
538                         assert(effect_ids.count(input));
539                         std::string texture_name = std::string("tex_") + effect_ids[input];
540                         glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, texture_name.c_str()), sampler);
541                         check_error();
542                 }
543
544                 // And now the output.
545                 if (phase == phases.size() - 1) {
546                         // Last phase goes directly to the screen.
547                         glBindFramebuffer(GL_FRAMEBUFFER, 0);
548                         check_error();
549                 } else {
550                         Effect *last_effect = phases[phase].effects.back();
551                         assert(effect_output_textures.count(last_effect) != 0);
552                         glFramebufferTexture2D(
553                                 GL_FRAMEBUFFER,
554                                 GL_COLOR_ATTACHMENT0,
555                                 GL_TEXTURE_2D,
556                                 effect_output_textures[last_effect],
557                                 0);
558                         check_error();
559                 }
560
561                 // Give the required parameters to all the effects.
562                 unsigned sampler_num = phases[phase].inputs.size();
563                 for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
564                         Effect *effect = phases[phase].effects[i];
565                         effect->set_uniforms(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
566                 }
567
568                 // Now draw!
569                 glBegin(GL_QUADS);
570
571                 glTexCoord2f(0.0f, 0.0f);
572                 glVertex2f(0.0f, 0.0f);
573
574                 glTexCoord2f(1.0f, 0.0f);
575                 glVertex2f(1.0f, 0.0f);
576
577                 glTexCoord2f(1.0f, 1.0f);
578                 glVertex2f(1.0f, 1.0f);
579
580                 glTexCoord2f(0.0f, 1.0f);
581                 glVertex2f(0.0f, 1.0f);
582
583                 glEnd();
584                 check_error();
585
586                 // HACK
587                 glActiveTexture(GL_TEXTURE0);
588                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
589                 check_error();
590                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
591                 check_error();
592         }
593 }