]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Simplify applying texture swizzling
[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         {
57                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
58                 image(level, base_fmt, get_alloc_type(base_fmt), 0);
59         }
60 }
61
62 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
63 {
64         if(width==0)
65                 throw invalid_operation("Texture1D::image");
66
67         unsigned w = get_level_size(level);
68
69         if(ARB_texture_storage)
70                 return sub_image(level, 0, w, fmt, type, data);
71
72         BindRestore _bind(this);
73
74         if(!allocated)
75         {
76                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
77                 apply_swizzle();
78         }
79         glTexImage1D(target, level, ifmt, w, 0, get_upload_format(fmt), 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::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
90 {
91         if(width==0)
92                 throw invalid_operation("Texture3D::image");
93
94         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
95         allocate(level);
96
97         fmt = get_upload_format(fmt);
98         if(ARB_direct_state_access)
99                 glTextureSubImage1D(id, level, x, wd, fmt, type, data);
100         else
101                 glTexSubImage1D(target, level, x, wd, fmt, type, data);
102
103         if(auto_gen_mipmap && level==0)
104                 generate_mipmap();
105 }
106
107 void Texture1D::image(const Graphics::Image &img, unsigned lv, bool srgb)
108 {
109         if(img.get_height()!=1)
110                 throw incompatible_data("Texture1D::image");
111
112         unsigned w = img.get_width();
113         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
114         if(width==0)
115                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, lv);
116         else if(w!=width)
117                 throw incompatible_data("Texture1D::image");
118
119         image(0, fmt, UNSIGNED_BYTE, img.get_data());
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 Texture1D::get_data_size() const
135 {
136         return id ? width*get_pixel_size(ifmt) : 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, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, 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