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