]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Use UNSIGNED_SHORT for allocating DEPTH_COMPONENT textures
[libs/gl.git] / source / texture3d.cpp
1 #include <cmath>
2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/graphics/image.h>
4 #include "bindable.h"
5 #include "error.h"
6 #include "pixelstore.h"
7 #include "texture3d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Texture3D::Texture3D():
15         Texture(GL_TEXTURE_3D),
16         ifmt(RGB),
17         width(0),
18         height(0),
19         depth(0),
20         allocated(0)
21 {
22         static Require _req(EXT_texture3D);
23 }
24
25 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
26 {
27         if(width>0)
28                 throw invalid_operation("Texture3D::storage");
29         if(wd==0 || ht==0 || dp==0)
30                 throw invalid_argument("Texture3D::storage");
31         require_pixelformat(fmt);
32
33         width = wd;
34         height = ht;
35         depth = dp;
36         ifmt = fmt;
37 }
38
39 void Texture3D::allocate(unsigned level)
40 {
41         if(allocated&(1<<level))
42                 return;
43
44         PixelFormat base_fmt = get_base_pixelformat(ifmt);
45         image(level, base_fmt, get_alloc_type(base_fmt), 0);
46 }
47
48 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
49 {
50         if(width==0 || height==0 || depth==0)
51                 throw invalid_operation("Texture3D::image");
52
53         unsigned w = width;
54         unsigned h = height;
55         unsigned d = depth;
56         get_level_size(level, w, h, d);
57
58         BindRestore _bind(this);
59         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
60
61         allocated |= 1<<level;
62         if(gen_mipmap && level==0)
63         {
64                 auto_generate_mipmap();
65                 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
66                 allocated |= (1<<level)-1;
67         }
68 }
69
70 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
71 {
72         if(width==0 || height==0 || depth==0)
73                 throw invalid_operation("Texture3D::image");
74
75         allocate(level);
76
77         BindRestore _bind(this);
78         glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
79 }
80
81 void Texture3D::load_image(const string &fn, int dp)
82 {
83         Graphics::Image img;
84         img.load_file(fn);
85
86         unsigned w = img.get_width();
87         unsigned h = img.get_height();
88         unsigned d = 1;
89
90         if(dp==-1)
91         {
92                 if(h%w)
93                         throw incompatible_data("Texture3D::load_image");
94                 d = h/w;
95                 h = w;
96         }
97         else if(dp==-2)
98         {
99                 for(d=h; d*d>h; d>>=2) ;
100                 for(; d*d<h; ++d) ;
101                 if(d*d!=h)
102                         throw incompatible_data("Texture3D::load_image");
103                 h = d;
104         }
105         else if(dp>0)
106         {
107                 d = dp;
108                 if(h%d)
109                         throw incompatible_data("Texture3D::load_image");
110                 h /= d;
111         }
112         else
113                 throw invalid_argument("Texture3D::load_image");
114
115         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
116         if(width==0)
117                 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
118         else if(w!=width || h!=height || d!=depth)
119                 throw incompatible_data("Texture3D::load_image");
120
121         PixelStore pstore = PixelStore::from_image(img);
122         BindRestore _bind_ps(pstore);
123
124         image(0, fmt, UNSIGNED_BYTE, img.get_data());
125 }
126
127 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
128 {
129         w >>= level;
130         h >>= level;
131         d >>= level;
132
133         if(!w && (h || d))
134                 w = 1;
135         if(!h && (w || d))
136                 h = 1;
137         if(!d && (w || h))
138                 d = 1;
139 }
140
141 UInt64 Texture3D::get_data_size() const
142 {
143         return id ? width*height*depth*get_pixel_size(ifmt) : 0;
144 }
145
146 } // namespace GL
147 } // namespace Msp