]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Guard against hitting the end of the array in get_gl_type
[libs/gl.git] / source / core / 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_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                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
48                 if(ARB_direct_state_access)
49                         glTextureStorage1D(id, levels, storage_fmt, width);
50                 else
51                         glTexStorage1D(target, levels, storage_fmt, width);
52                 apply_swizzle();
53                 allocated |= (1<<levels)-1;
54         }
55         else
56                 image(level, 0);
57 }
58
59 void Texture1D::image(unsigned level, const void *data)
60 {
61         if(width==0)
62                 throw invalid_operation("Texture1D::image");
63
64         unsigned w = get_level_size(level);
65
66         if(ARB_texture_storage)
67                 return sub_image(level, 0, w, data);
68
69         BindRestore _bind(this);
70
71         if(!allocated)
72         {
73                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
74                 apply_swizzle();
75         }
76
77         PixelComponents comp = get_components(storage_fmt);
78         GLenum type = get_gl_type(get_component_type(storage_fmt));
79         glTexImage1D(target, level, storage_fmt, w, 0, comp, type, data);
80
81         allocated |= 1<<level;
82         if(auto_gen_mipmap && level==0)
83         {
84                 generate_mipmap();
85                 allocated |= (1<<levels)-1;
86         }
87 }
88
89 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
90 {
91         if(comp!=get_components(format) || type!=get_component_type(format))
92                 throw incompatible_data("Texture1D::image");
93         image(level, data);
94 }
95
96 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
97 {
98         if(width==0)
99                 throw invalid_operation("Texture3D::image");
100
101         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
102         allocate(level);
103
104         PixelComponents comp = get_components(storage_fmt);
105         GLenum type = get_gl_type(get_component_type(storage_fmt));
106         if(ARB_direct_state_access)
107                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
108         else
109                 glTexSubImage1D(target, level, x, wd, comp, type, data);
110
111         if(auto_gen_mipmap && level==0)
112                 generate_mipmap();
113 }
114
115 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
116 {
117         if(comp!=get_components(format) || type!=get_component_type(format))
118                 throw incompatible_data("Texture1D::sub_image");
119         sub_image(level, x, wd, data);
120 }
121
122 void Texture1D::image(const Graphics::Image &img, unsigned lv)
123 {
124         if(img.get_height()!=1)
125                 throw incompatible_data("Texture1D::image");
126
127         unsigned w = img.get_width();
128         PixelFormat fmt = pixelformat_from_image(img);
129         if(width==0)
130                 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
131         else if(w!=width)
132                 throw incompatible_data("Texture1D::image");
133
134         image(0, img.get_pixels());
135 }
136
137 unsigned Texture1D::get_n_levels() const
138 {
139         unsigned n = 0;
140         for(unsigned s=width; s; s>>=1, ++n) ;
141         return n;
142 }
143
144 unsigned Texture1D::get_level_size(unsigned level) const
145 {
146         return width>>level;
147 }
148
149 UInt64 Texture1D::get_data_size() const
150 {
151         return id ? width*get_pixel_size(storage_fmt) : 0;
152 }
153
154
155 Texture1D::Loader::Loader(Texture1D &t):
156         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
157 {
158         init();
159 }
160
161 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
162         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
163 {
164         init();
165 }
166
167 void Texture1D::Loader::init()
168 {
169         add("raw_data", &Loader::raw_data);
170         add("storage", &Loader::storage);
171         add("storage", &Loader::storage_levels);
172 }
173
174 void Texture1D::Loader::raw_data(const string &data)
175 {
176         obj.image(0, data.data());
177 }
178
179 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
180 {
181         obj.storage(fmt, w);
182 }
183
184 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
185 {
186         obj.storage(fmt, w, l);
187 }
188
189 } // namespace GL
190 } // namespace Msp