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