]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Rewrite state management
[libs/gl.git] / source / core / texture1d.cpp
1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/gl/extensions/msp_texture1d.h>
5 #include "bindable.h"
6 #include "error.h"
7 #include "texture1d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Texture1D::Texture1D():
15         Texture(GL_TEXTURE_1D),
16         width(0),
17         allocated(0)
18 {
19         static Require _req(MSP_texture1D);
20 }
21
22 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
23 {
24         if(width>0)
25         {
26                 if(fmt!=format || wd!=width || (lv && lv!=levels))
27                         throw incompatible_data("Texture1D::storage");
28                 return;
29         }
30         if(wd==0)
31                 throw invalid_argument("Texture1D::storage");
32
33         set_format(fmt);
34         width = wd;
35         levels = get_n_levels();
36         if(lv)
37                 levels = min(levels, lv);
38 }
39
40 void Texture1D::allocate(unsigned level)
41 {
42         if(width==0)
43                 throw invalid_operation("Texture1D::allocate");
44         if(level>=levels)
45                 throw invalid_argument("Texture1D::allocate");
46
47         bool direct = ARB_texture_storage && ARB_direct_state_access;
48         if(!direct)
49         {
50                 glActiveTexture(GL_TEXTURE0);
51                 glBindTexture(target, id);
52         }
53
54         allocate_(level);
55
56         if(!direct)
57                 glBindTexture(target, 0);
58 }
59
60 void Texture1D::allocate_(unsigned level)
61 {
62         if(allocated&(1<<level))
63                 return;
64
65         if(ARB_texture_storage)
66         {
67                 if(ARB_direct_state_access)
68                         glTextureStorage1D(id, levels, storage_fmt, width);
69                 else
70                         glTexStorage1D(target, levels, storage_fmt, width);
71                 apply_swizzle();
72                 allocated |= (1<<levels)-1;
73         }
74         else
75                 image_(level, 0);
76 }
77
78 void Texture1D::image(unsigned level, const void *data)
79 {
80         if(width==0)
81                 throw invalid_operation("Texture1D::image");
82         if(level>=levels)
83                 throw out_of_range("Texture1D::image");
84
85         if(ARB_texture_storage)
86                 return sub_image(level, 0, get_level_size(level), data);
87
88         glActiveTexture(GL_TEXTURE0);
89         glBindTexture(target, id);
90
91         image_(level, data);
92
93         if(auto_gen_mipmap && level==0)
94         {
95                 generate_mipmap_();
96                 allocated |= (1<<levels)-1;
97         }
98
99         glBindTexture(target, 0);
100 }
101
102 void Texture1D::image_(unsigned level, const void *data)
103 {
104         if(!allocated)
105         {
106                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
107                 apply_swizzle();
108         }
109
110         PixelComponents comp = get_components(storage_fmt);
111         GLenum type = get_gl_type(get_component_type(storage_fmt));
112         glTexImage1D(target, level, storage_fmt, get_level_size(level), 0, comp, type, data);
113
114         allocated |= 1<<level;
115 }
116
117 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
118 {
119         if(comp!=get_components(format) || type!=get_component_type(format))
120                 throw incompatible_data("Texture1D::image");
121         image(level, data);
122 }
123
124 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
125 {
126         if(width==0)
127                 throw invalid_operation("Texture1D::sub_image");
128         if(level>=levels)
129                 throw out_of_range("Texture1D::sub_image");
130
131         bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
132         if(!direct)
133         {
134                 glActiveTexture(GL_TEXTURE0);
135                 glBindTexture(target, id);
136         }
137
138         allocate_(level);
139
140         PixelComponents comp = get_components(storage_fmt);
141         GLenum type = get_gl_type(get_component_type(storage_fmt));
142         if(ARB_direct_state_access)
143                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
144         else
145                 glTexSubImage1D(target, level, x, wd, comp, type, data);
146
147         if(auto_gen_mipmap && level==0)
148                 generate_mipmap_();
149
150         if(!direct)
151                 glBindTexture(target, 0);
152 }
153
154 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
155 {
156         if(comp!=get_components(format) || type!=get_component_type(format))
157                 throw incompatible_data("Texture1D::sub_image");
158         sub_image(level, x, wd, data);
159 }
160
161 void Texture1D::image(const Graphics::Image &img, unsigned lv)
162 {
163         if(img.get_height()!=1)
164                 throw incompatible_data("Texture1D::image");
165
166         unsigned w = img.get_width();
167         PixelFormat fmt = pixelformat_from_image(img);
168         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
169
170         image(0, img.get_pixels());
171 }
172
173 unsigned Texture1D::get_n_levels() const
174 {
175         unsigned n = 0;
176         for(unsigned s=width; s; s>>=1, ++n) ;
177         return n;
178 }
179
180 unsigned Texture1D::get_level_size(unsigned level) const
181 {
182         return width>>level;
183 }
184
185 UInt64 Texture1D::get_data_size() const
186 {
187         return id ? width*get_pixel_size(storage_fmt) : 0;
188 }
189
190
191 Texture1D::Loader::Loader(Texture1D &t):
192         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
193 {
194         init();
195 }
196
197 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
198         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
199 {
200         init();
201 }
202
203 void Texture1D::Loader::init()
204 {
205         add("raw_data", &Loader::raw_data);
206         add("storage", &Loader::storage);
207         add("storage", &Loader::storage_levels);
208 }
209
210 void Texture1D::Loader::raw_data(const string &data)
211 {
212         obj.image(0, data.data());
213 }
214
215 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
216 {
217         obj.storage(fmt, w);
218 }
219
220 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
221 {
222         obj.storage(fmt, w, l);
223 }
224
225 } // namespace GL
226 } // namespace Msp