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