]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Remove remaining deprecated things from the core classes
[libs/gl.git] / source / core / texture1d.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/msp_texture1d.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         width(0),
15         allocated(0)
16 {
17         static Require _req(MSP_texture1D);
18 }
19
20 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
21 {
22         if(width>0)
23         {
24                 if(fmt!=format || wd!=width || (lv && lv!=levels))
25                         throw incompatible_data("Texture1D::storage");
26                 return;
27         }
28         if(wd==0)
29                 throw invalid_argument("Texture1D::storage");
30
31         set_format(fmt);
32         width = wd;
33         levels = get_n_levels();
34         if(lv)
35                 levels = min(levels, lv);
36 }
37
38 void Texture1D::allocate(unsigned level)
39 {
40         if(width==0)
41                 throw invalid_operation("Texture1D::allocate");
42         if(level>=levels)
43                 throw invalid_argument("Texture1D::allocate");
44         if(allocated&(1<<level))
45                 return;
46
47         if(ARB_texture_storage)
48         {
49                 GLenum fmt = get_gl_pixelformat(storage_fmt);
50                 if(ARB_direct_state_access)
51                         glTextureStorage1D(id, levels, fmt, width);
52                 else
53                 {
54                         bind_scratch();
55                         glTexStorage1D(target, levels, fmt, width);
56                 }
57                 apply_swizzle();
58                 allocated |= (1<<levels)-1;
59         }
60         else
61                 image(level, 0);
62 }
63
64 void Texture1D::image(unsigned level, const void *data)
65 {
66         if(width==0)
67                 throw invalid_operation("Texture1D::image");
68         if(level>=levels)
69                 throw out_of_range("Texture1D::image");
70
71         if(ARB_texture_storage)
72                 return sub_image(level, 0, get_level_size(level), data);
73
74         bind_scratch();
75
76         if(!allocated)
77         {
78                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
79                 apply_swizzle();
80         }
81
82         GLenum fmt = get_gl_pixelformat(storage_fmt);
83         GLenum comp = get_gl_components(get_components(storage_fmt));
84         GLenum type = get_gl_type(get_component_type(storage_fmt));
85         glTexImage1D(target, level, fmt, get_level_size(level), 0, comp, type, data);
86
87         allocated |= 1<<level;
88 }
89
90 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
91 {
92         if(width==0)
93                 throw invalid_operation("Texture1D::sub_image");
94         if(level>=levels)
95                 throw out_of_range("Texture1D::sub_image");
96
97         allocate(level);
98
99         GLenum comp = get_gl_components(get_components(storage_fmt));
100         GLenum type = get_gl_type(get_component_type(storage_fmt));
101         if(ARB_direct_state_access)
102                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
103         else
104         {
105                 bind_scratch();
106                 glTexSubImage1D(target, level, x, wd, comp, type, data);
107         }
108 }
109
110 void Texture1D::image(const Graphics::Image &img, unsigned lv)
111 {
112         if(img.get_height()!=1)
113                 throw incompatible_data("Texture1D::image");
114
115         unsigned w = img.get_width();
116         PixelFormat fmt = pixelformat_from_image(img);
117         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
118
119         image(0, img.get_pixels());
120 }
121
122 unsigned Texture1D::get_n_levels() const
123 {
124         unsigned n = 0;
125         for(unsigned s=width; s; s>>=1, ++n) ;
126         return n;
127 }
128
129 unsigned Texture1D::get_level_size(unsigned level) const
130 {
131         return width>>level;
132 }
133
134 uint64_t Texture1D::get_data_size() const
135 {
136         return id ? width*get_pixel_size(storage_fmt) : 0;
137 }
138
139
140 Texture1D::Loader::Loader(Texture1D &t):
141         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
142 {
143         init();
144 }
145
146 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
147         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
148 {
149         init();
150 }
151
152 void Texture1D::Loader::init()
153 {
154         add("raw_data", &Loader::raw_data);
155         add("storage", &Loader::storage);
156         add("storage", &Loader::storage_levels);
157 }
158
159 void Texture1D::Loader::raw_data(const string &data)
160 {
161         obj.image(0, data.data());
162 }
163
164 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
165 {
166         obj.storage(fmt, w);
167 }
168
169 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
170 {
171         obj.storage(fmt, w, l);
172 }
173
174 } // namespace GL
175 } // namespace Msp