]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Deprecate the mipmap_levels parameter in Texture
[libs/gl.git] / source / texture1d.cpp
1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/gl/extensions/msp_texture1d.h>
5 #include "bindable.h"
6 #include "error.h"
7 #include "texture1d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Texture1D::Texture1D():
15         Texture(GL_TEXTURE_1D),
16         width(0),
17         allocated(0)
18 {
19         static Require _req(MSP_texture1D);
20 }
21
22 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
23 {
24         if(width>0)
25                 throw invalid_operation("Texture1D::storage");
26         if(wd==0)
27                 throw invalid_argument("Texture1D::storage");
28
29         set_internal_format(fmt);
30         width = wd;
31         levels = get_n_levels();
32         if(lv)
33                 levels = min(levels, lv);
34 }
35
36 void Texture1D::allocate(unsigned level)
37 {
38         if(width==0)
39                 throw invalid_operation("Texture1D::allocate");
40         if(level>=levels)
41                 throw invalid_argument("Texture1D::allocate");
42         if(allocated&(1<<level))
43                 return;
44
45         if(ARB_texture_storage)
46         {
47                 if(ARB_direct_state_access)
48                         glTextureStorage1D(id, levels, ifmt, width);
49                 else
50                 {
51                         BindRestore _bind(this);
52                         glTexStorage1D(target, levels, ifmt, width);
53                 }
54                 allocated |= (1<<levels)-1;
55         }
56         else
57         {
58                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
59                 image(level, base_fmt, get_alloc_type(base_fmt), 0);
60         }
61 }
62
63 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
64 {
65         if(width==0)
66                 throw invalid_operation("Texture1D::image");
67
68         unsigned w = get_level_size(level);
69
70         if(ARB_texture_storage)
71                 return sub_image(level, 0, w, fmt, type, data);
72
73         BindRestore _bind(this);
74
75         if(!allocated)
76                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
77         glTexImage1D(target, level, ifmt, w, 0, get_upload_format(fmt), type, data);
78
79         allocated |= 1<<level;
80         if(auto_gen_mipmap && level==0)
81         {
82                 generate_mipmap();
83                 allocated |= (1<<levels)-1;
84         }
85 }
86
87 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
88 {
89         if(width==0)
90                 throw invalid_operation("Texture3D::image");
91
92         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
93         allocate(level);
94
95         fmt = get_upload_format(fmt);
96         if(ARB_direct_state_access)
97                 glTextureSubImage1D(id, level, x, wd, fmt, type, data);
98         else
99                 glTexSubImage1D(target, level, x, wd, fmt, type, data);
100
101         if(auto_gen_mipmap && level==0)
102                 generate_mipmap();
103 }
104
105 void Texture1D::image(const Graphics::Image &img, unsigned lv, bool srgb)
106 {
107         if(img.get_height()!=1)
108                 throw incompatible_data("Texture1D::image");
109
110         unsigned w = img.get_width();
111         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112         if(width==0)
113         {
114                 unsigned l = (is_mipmapped(min_filter) ? lv : 1);
115                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, l);
116         }
117         else if(w!=width)
118                 throw incompatible_data("Texture1D::image");
119
120         image(0, fmt, UNSIGNED_BYTE, img.get_data());
121 }
122
123 unsigned Texture1D::get_n_levels() const
124 {
125         unsigned n = 0;
126         for(unsigned s=width; s; s>>=1, ++n) ;
127         return n;
128 }
129
130 unsigned Texture1D::get_level_size(unsigned level) const
131 {
132         return width>>level;
133 }
134
135 UInt64 Texture1D::get_data_size() const
136 {
137         return id ? width*get_pixel_size(ifmt) : 0;
138 }
139
140
141 Texture1D::Loader::Loader(Texture1D &t):
142         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
143 {
144         init();
145 }
146
147 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
148         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
149 {
150         init();
151 }
152
153 void Texture1D::Loader::init()
154 {
155         add("raw_data", &Loader::raw_data);
156         add("storage", &Loader::storage);
157 }
158
159 void Texture1D::Loader::raw_data(const string &data)
160 {
161         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
162 }
163
164 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
165 {
166         obj.storage(fmt, w);
167 }
168
169 } // namespace GL
170 } // namespace Msp