]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Refactor get_level_size in various texture classes
[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         if(allocated&(1<<level))
47                 return;
48
49         if(ARB_texture_storage)
50         {
51                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
52                 if(ARB_direct_state_access)
53                         glTextureStorage1D(id, levels, storage_fmt, width);
54                 else
55                         glTexStorage1D(target, levels, storage_fmt, width);
56                 apply_swizzle();
57                 allocated |= (1<<levels)-1;
58         }
59         else
60                 image(level, 0);
61 }
62
63 void Texture1D::image(unsigned level, const void *data)
64 {
65         if(width==0)
66                 throw invalid_operation("Texture1D::image");
67         if(level>=levels)
68                 throw out_of_range("Texture1D::image");
69
70         unsigned w = get_level_size(level);
71
72         if(ARB_texture_storage)
73                 return sub_image(level, 0, w, data);
74
75         BindRestore _bind(this);
76
77         if(!allocated)
78         {
79                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
80                 apply_swizzle();
81         }
82
83         PixelComponents comp = get_components(storage_fmt);
84         GLenum type = get_gl_type(get_component_type(storage_fmt));
85         glTexImage1D(target, level, storage_fmt, w, 0, comp, type, data);
86
87         allocated |= 1<<level;
88         if(auto_gen_mipmap && level==0)
89         {
90                 generate_mipmap();
91                 allocated |= (1<<levels)-1;
92         }
93 }
94
95 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
96 {
97         if(comp!=get_components(format) || type!=get_component_type(format))
98                 throw incompatible_data("Texture1D::image");
99         image(level, data);
100 }
101
102 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
103 {
104         if(width==0)
105                 throw invalid_operation("Texture1D::sub_image");
106         if(level>=levels)
107                 throw out_of_range("Texture1D::sub_image");
108
109         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
110         allocate(level);
111
112         PixelComponents comp = get_components(storage_fmt);
113         GLenum type = get_gl_type(get_component_type(storage_fmt));
114         if(ARB_direct_state_access)
115                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
116         else
117                 glTexSubImage1D(target, level, x, wd, comp, type, data);
118
119         if(auto_gen_mipmap && level==0)
120                 generate_mipmap();
121 }
122
123 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
124 {
125         if(comp!=get_components(format) || type!=get_component_type(format))
126                 throw incompatible_data("Texture1D::sub_image");
127         sub_image(level, x, wd, data);
128 }
129
130 void Texture1D::image(const Graphics::Image &img, unsigned lv)
131 {
132         if(img.get_height()!=1)
133                 throw incompatible_data("Texture1D::image");
134
135         unsigned w = img.get_width();
136         PixelFormat fmt = pixelformat_from_image(img);
137         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
138
139         image(0, img.get_pixels());
140 }
141
142 unsigned Texture1D::get_n_levels() const
143 {
144         unsigned n = 0;
145         for(unsigned s=width; s; s>>=1, ++n) ;
146         return n;
147 }
148
149 unsigned Texture1D::get_level_size(unsigned level) const
150 {
151         return width>>level;
152 }
153
154 UInt64 Texture1D::get_data_size() const
155 {
156         return id ? width*get_pixel_size(storage_fmt) : 0;
157 }
158
159
160 Texture1D::Loader::Loader(Texture1D &t):
161         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
162 {
163         init();
164 }
165
166 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
167         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
168 {
169         init();
170 }
171
172 void Texture1D::Loader::init()
173 {
174         add("raw_data", &Loader::raw_data);
175         add("storage", &Loader::storage);
176         add("storage", &Loader::storage_levels);
177 }
178
179 void Texture1D::Loader::raw_data(const string &data)
180 {
181         obj.image(0, data.data());
182 }
183
184 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
185 {
186         obj.storage(fmt, w);
187 }
188
189 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
190 {
191         obj.storage(fmt, w, l);
192 }
193
194 } // namespace GL
195 } // namespace Msp