]> git.tdb.fi Git - libs/gl.git/blob - source/texunit.cpp
Make postprocessor shaders compatible with modern interface
[libs/gl.git] / source / texunit.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/arb_multitexture.h>
3 #include <msp/gl/extensions/arb_vertex_shader.h>
4 #include <msp/gl/extensions/msp_legacy_features.h>
5 #include "gl.h"
6 #include "misc.h"
7 #include "texunit.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 vector<TexUnit> TexUnit::units;
15 TexUnit *TexUnit::cur_unit = 0;
16
17 TexUnit::TexUnit():
18         legacy(false),
19         texture(0)
20 { }
21
22 bool TexUnit::set_texture(const Texture *tex)
23 {
24         bool result = (tex!=texture);
25         texture = tex;
26         return result;
27 }
28
29 void TexUnit::bind()
30 {
31         if(cur_unit!=this && (cur_unit || index))
32                 glActiveTexture(GL_TEXTURE0+index);
33         cur_unit = this;
34 }
35
36 unsigned TexUnit::get_n_units()
37 {
38         static int count = -1;
39         if(count<0)
40         {
41                 if(ARB_vertex_shader)
42                         count = get_i(GL_MAX_TEXTURE_IMAGE_UNITS);
43                 else if(ARB_multitexture)
44                         count = get_i(GL_MAX_TEXTURE_UNITS);
45                 else
46                         count = 1;
47         }
48         return count;
49 }
50
51 unsigned TexUnit::get_n_legacy_units()
52 {
53         static int count = -1;
54         if(count<0)
55         {
56                 if(MSP_legacy_features)
57                         count = get_i(GL_MAX_TEXTURE_UNITS);
58                 else
59                         count = 0;
60         }
61         return count;
62 }
63
64 TexUnit &TexUnit::get_unit(unsigned n)
65 {
66         if(n>0)
67                 static Require _req(ARB_multitexture);
68         if(n>=get_n_units())
69                 throw out_of_range("TexUnit::get_unit");
70
71         if(units.size()<=n)
72         {
73                 unsigned i = units.size();
74                 unsigned n_legacy = get_n_legacy_units();
75                 units.resize(n+1, TexUnit());
76                 for(; i<units.size(); ++i)
77                 {
78                         units[i].index = i;
79                         units[i].legacy = (i<n_legacy);
80                 }
81         }
82
83         return units[n];
84 }
85
86 TexUnit &TexUnit::current()
87 {
88         if(!cur_unit)
89                 get_unit(0).bind();
90         return *cur_unit;
91 }
92
93 TexUnit *TexUnit::find_unit(const Texture *tex)
94 {
95         for(vector<TexUnit>::iterator i=units.begin(); i!=units.end(); ++i)
96                 if(i->texture==tex)
97                         return &*i;
98         return 0;
99 }
100
101 } // namespace GL
102 } // namespace Msp