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