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