]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Always set uniform array size to at least one
[libs/gl.git] / source / core / texture1d.cpp
1 #include "error.h"
2 #include "texture1d.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
10 {
11         if(width>0)
12         {
13                 if(fmt!=format || wd!=width || (lv && lv!=levels))
14                         throw incompatible_data("Texture1D::storage");
15                 return;
16         }
17         if(wd==0)
18                 throw invalid_argument("Texture1D::storage");
19
20         set_format(fmt);
21         width = wd;
22         levels = get_n_levels();
23         if(lv)
24                 levels = min(levels, lv);
25
26         allocate();
27 }
28
29 void Texture1D::image(unsigned level, const void *data)
30 {
31         return sub_image(level, 0, get_level_size(level), data);
32 }
33
34 void Texture1D::sub_image(unsigned level, unsigned x, unsigned wd, const void *data)
35 {
36         if(width==0)
37                 throw invalid_operation("Texture1D::sub_image");
38         if(level>=levels || x>width || x+wd>width)
39                 throw out_of_range("Texture1D::sub_image");
40
41         Texture1DBackend::sub_image(level, x, wd, data);
42 }
43
44 void Texture1D::image(const Graphics::Image &img, unsigned lv)
45 {
46         if(img.get_height()!=1)
47                 throw incompatible_data("Texture1D::image");
48
49         storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), lv);
50         image(0, img.get_pixels());
51 }
52
53 unsigned Texture1D::get_n_levels() const
54 {
55         unsigned n = 0;
56         for(unsigned s=width; s; s>>=1, ++n) ;
57         return n;
58 }
59
60 unsigned Texture1D::get_level_size(unsigned level) const
61 {
62         return width>>level;
63 }
64
65 uint64_t Texture1D::get_data_size() const
66 {
67         return id ? width*get_pixel_size(storage_fmt) : 0;
68 }
69
70
71 Texture1D::Loader::Loader(Texture1D &t):
72         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
73 {
74         init();
75 }
76
77 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
78         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
79 {
80         init();
81 }
82
83 void Texture1D::Loader::init()
84 {
85         add("storage", &Loader::storage);
86         add("storage", &Loader::storage_levels);
87 }
88
89 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
90 {
91         obj.storage(fmt, w);
92 }
93
94 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
95 {
96         obj.storage(fmt, w, l);
97 }
98
99 } // namespace GL
100 } // namespace Msp