]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
c5e57df22843d0e0dc84675c24d08bebcaaf1c74
[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)
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 }
32
33 void Texture1D::allocate(unsigned level)
34 {
35         if(width==0)
36                 throw invalid_operation("Texture1D::allocate");
37         if(allocated&(1<<level))
38                 return;
39
40         if(ARB_texture_storage)
41         {
42                 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
43                 if(ARB_direct_state_access)
44                         glTextureStorage1D(id, n_levels, ifmt, width);
45                 else
46                 {
47                         BindRestore _bind(this);
48                         glTexStorage1D(target, n_levels, ifmt, width);
49                 }
50                 allocated |= (1<<n_levels)-1;
51         }
52         else
53         {
54                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
55                 image(level, base_fmt, get_alloc_type(base_fmt), 0);
56         }
57 }
58
59 void Texture1D::image(unsigned level, PixelFormat fmt, 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, fmt, type, data);
68
69         BindRestore _bind(this);
70         glTexImage1D(target, level, ifmt, w, 0, get_upload_format(fmt), type, data);
71
72         allocated |= 1<<level;
73         if(gen_mipmap && level==0)
74         {
75                 auto_generate_mipmap();
76                 allocated |= (1<<get_n_levels())-1;
77         }
78 }
79
80 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
81 {
82         if(width==0)
83                 throw invalid_operation("Texture3D::image");
84
85         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
86         allocate(level);
87
88         fmt = get_upload_format(fmt);
89         if(ARB_direct_state_access)
90                 glTextureSubImage1D(id, level, x, wd, fmt, type, data);
91         else
92                 glTexSubImage1D(target, level, x, wd, fmt, type, data);
93
94         if(gen_mipmap && level==0)
95                 auto_generate_mipmap();
96 }
97
98 void Texture1D::image(const Graphics::Image &img, bool srgb)
99 {
100         if(img.get_height()!=1)
101                 throw incompatible_data("Texture1D::image");
102
103         unsigned w = img.get_width();
104         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
105         if(width==0)
106                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
107         else if(w!=width)
108                 throw incompatible_data("Texture1D::image");
109
110         image(0, fmt, UNSIGNED_BYTE, img.get_data());
111 }
112
113 unsigned Texture1D::get_n_levels() const
114 {
115         unsigned n = 0;
116         for(unsigned s=width; s; s>>=1, ++n) ;
117         return n;
118 }
119
120 unsigned Texture1D::get_level_size(unsigned level) const
121 {
122         return width>>level;
123 }
124
125 UInt64 Texture1D::get_data_size() const
126 {
127         return id ? width*get_pixel_size(ifmt) : 0;
128 }
129
130
131 Texture1D::Loader::Loader(Texture1D &t):
132         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
133 {
134         init();
135 }
136
137 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
138         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
139 {
140         init();
141 }
142
143 void Texture1D::Loader::init()
144 {
145         add("raw_data", &Loader::raw_data);
146         add("storage", &Loader::storage);
147 }
148
149 void Texture1D::Loader::raw_data(const string &data)
150 {
151         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
152 }
153
154 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
155 {
156         if(srgb)
157                 fmt = get_srgb_pixelformat(fmt);
158         obj.storage(fmt, w);
159 }
160
161 } // namespace GL
162 } // namespace Msp