]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Separate abstract pixel compositions from concrete pixel formats
[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                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
48                 if(ARB_direct_state_access)
49                         glTextureStorage1D(id, levels, ifmt, width);
50                 else
51                         glTexStorage1D(target, levels, ifmt, width);
52                 apply_swizzle();
53                 allocated |= (1<<levels)-1;
54         }
55         else
56                 image(level, get_components(ifmt), get_component_type(ifmt), 0);
57 }
58
59 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, 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, comp, type, 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         glTexImage1D(target, level, ifmt, w, 0, get_upload_components(comp), type, data);
77
78         allocated |= 1<<level;
79         if(auto_gen_mipmap && level==0)
80         {
81                 generate_mipmap();
82                 allocated |= (1<<levels)-1;
83         }
84 }
85
86 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
87 {
88         if(width==0)
89                 throw invalid_operation("Texture3D::image");
90
91         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
92         allocate(level);
93
94         comp = get_upload_components(comp);
95         if(ARB_direct_state_access)
96                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
97         else
98                 glTexSubImage1D(target, level, x, wd, comp, type, data);
99
100         if(auto_gen_mipmap && level==0)
101                 generate_mipmap();
102 }
103
104 void Texture1D::image(const Graphics::Image &img, unsigned lv, bool srgb)
105 {
106         if(img.get_height()!=1)
107                 throw incompatible_data("Texture1D::image");
108
109         unsigned w = img.get_width();
110         PixelFormat fmt = pixelformat_from_image(img);
111         PixelComponents comp = get_components(fmt);
112         DataType type = get_component_type(fmt);
113         if(width==0)
114                 storage(make_pixelformat(comp, type, srgb), w, lv);
115         else if(w!=width)
116                 throw incompatible_data("Texture1D::image");
117
118         image(0, comp, type, img.get_data());
119 }
120
121 unsigned Texture1D::get_n_levels() const
122 {
123         unsigned n = 0;
124         for(unsigned s=width; s; s>>=1, ++n) ;
125         return n;
126 }
127
128 unsigned Texture1D::get_level_size(unsigned level) const
129 {
130         return width>>level;
131 }
132
133 UInt64 Texture1D::get_data_size() const
134 {
135         return id ? width*get_pixel_size(ifmt) : 0;
136 }
137
138
139 Texture1D::Loader::Loader(Texture1D &t):
140         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
141 {
142         init();
143 }
144
145 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
146         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
147 {
148         init();
149 }
150
151 void Texture1D::Loader::init()
152 {
153         add("raw_data", &Loader::raw_data);
154         add("storage", &Loader::storage);
155         add("storage", &Loader::storage_levels);
156 }
157
158 void Texture1D::Loader::raw_data(const string &data)
159 {
160         obj.image(0, get_components(obj.ifmt), get_component_type(obj.ifmt), data.data());
161 }
162
163 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
164 {
165         obj.storage(fmt, w);
166 }
167
168 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
169 {
170         obj.storage(fmt, w, l);
171 }
172
173 } // namespace GL
174 } // namespace Msp