]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
Allow repeated storage calls with the same parameters
[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         {
40                 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
41                         throw incompatible_data("Texture3D::storage");
42                 return;
43         }
44         if(wd==0 || ht==0 || dp==0)
45                 throw invalid_argument("Texture3D::storage");
46
47         set_format(fmt);
48         width = wd;
49         height = ht;
50         depth = dp;
51         levels = get_n_levels();
52         if(lv>0)
53                 levels = min(levels, lv);
54 }
55
56 void Texture3D::allocate(unsigned level)
57 {
58         if(width==0 || height==0 || depth==0)
59                 throw invalid_operation("Texture3D::allocate");
60         if(level>=levels)
61                 throw invalid_argument("Texture3D::allocate");
62         if(allocated&(1<<level))
63                 return;
64
65         if(ARB_texture_storage)
66         {
67                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
68                 if(ARB_direct_state_access)
69                         glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
70                 else
71                         glTexStorage3D(target, levels, storage_fmt, width, height, depth);
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
84         unsigned w = width;
85         unsigned h = height;
86         unsigned d = depth;
87         get_level_size(level, w, h, d);
88
89         if(ARB_texture_storage)
90                 return sub_image(level, 0, 0, 0, w, h, d, data);
91
92         BindRestore _bind(this);
93
94         if(!allocated)
95         {
96                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
97                 apply_swizzle();
98         }
99
100         PixelComponents comp = get_components(storage_fmt);
101         GLenum type = get_gl_type(get_component_type(storage_fmt));
102         glTexImage3D(target, level, storage_fmt, width, height, depth, 0, comp, type, data);
103
104         allocated |= 1<<level;
105         if(auto_gen_mipmap && level==0)
106         {
107                 generate_mipmap();
108                 allocated |= (1<<levels)-1;
109         }
110 }
111
112 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
113 {
114         if(comp!=get_components(format) || type!=get_component_type(format))
115                 throw incompatible_data("Texture3D::image");
116         image(level, data);
117 }
118
119 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
120 {
121         if(width==0 || height==0 || depth==0)
122                 throw invalid_operation("Texture3D::image");
123
124         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
125         allocate(level);
126
127         PixelComponents comp = get_components(storage_fmt);
128         GLenum type = get_gl_type(get_component_type(storage_fmt));
129         if(ARB_direct_state_access)
130                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
131         else
132                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
133
134         if(auto_gen_mipmap && level==0)
135                 generate_mipmap();
136 }
137
138 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)
139 {
140         if(comp!=get_components(format) || type!=get_component_type(format))
141                 throw incompatible_data("Texture3D::sub_image");
142         sub_image(level, x, y, z, wd, ht, dp, data);
143 }
144
145 void Texture3D::image(const Graphics::Image &img, unsigned lv)
146 {
147         unsigned w = img.get_width();
148         unsigned h = img.get_height();
149
150         if(h%w)
151                 throw incompatible_data("Texture3D::load_image");
152         unsigned d = h/w;
153         h = w;
154
155         PixelFormat fmt = pixelformat_from_image(img);
156         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
157
158         PixelStore pstore = PixelStore::from_image(img);
159         BindRestore _bind_ps(pstore);
160
161         image(0, img.get_pixels());
162 }
163
164 unsigned Texture3D::get_n_levels() const
165 {
166         unsigned s = max(width, height);
167         if(target!=GL_TEXTURE_2D_ARRAY)
168                 s = max(s, depth);
169         unsigned n = 0;
170         for(; s; s>>=1, ++n) ;
171         return n;
172 }
173
174 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
175 {
176         w >>= level;
177         h >>= level;
178         if(target!=GL_TEXTURE_2D_ARRAY)
179                 d >>= level;
180
181         if(!w && (h || d))
182                 w = 1;
183         if(!h && (w || d))
184                 h = 1;
185         if(!d && (w || h))
186                 d = 1;
187 }
188
189 UInt64 Texture3D::get_data_size() const
190 {
191         return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
192 }
193
194
195 Texture3D::Loader::Loader(Texture3D &t):
196         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
197 {
198         init();
199 }
200
201 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
202         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
203 {
204         init();
205 }
206
207 void Texture3D::Loader::init()
208 {
209         add("raw_data", &Loader::raw_data);
210         add("storage", &Loader::storage);
211         add("storage", &Loader::storage_levels);
212 }
213
214 void Texture3D::Loader::raw_data(const string &data)
215 {
216         obj.image(0, data.data());
217 }
218
219 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
220 {
221         obj.storage(fmt, w, h, d);
222 }
223
224 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
225 {
226         obj.storage(fmt, w, h, d, l);
227 }
228
229 } // namespace GL
230 } // namespace Msp