]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Use ARB_texture_storage for defining texture storage
[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         BindRestore _bind(this);
63         if(ARB_texture_storage)
64                 sub_image(level, 0, w, fmt, type, data);
65         else
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         allocate(level);
82
83         BindRestore _bind(this);
84         glTexSubImage1D(target, level, x, wd, fmt, type, data);
85 }
86
87 void Texture1D::image(const Graphics::Image &img, bool srgb)
88 {
89         if(img.get_height()!=1)
90                 throw incompatible_data("Texture1D::image");
91
92         unsigned w = img.get_width();
93         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
94         if(width==0)
95                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
96         else if(w!=width)
97                 throw incompatible_data("Texture1D::image");
98
99         image(0, fmt, UNSIGNED_BYTE, img.get_data());
100 }
101
102 unsigned Texture1D::get_n_levels() const
103 {
104         unsigned n = 0;
105         for(unsigned s=width; s; s>>=1, ++n) ;
106         return n;
107 }
108
109 unsigned Texture1D::get_level_size(unsigned level) const
110 {
111         return width>>level;
112 }
113
114 UInt64 Texture1D::get_data_size() const
115 {
116         return id ? width*get_pixel_size(ifmt) : 0;
117 }
118
119
120 Texture1D::Loader::Loader(Texture1D &t):
121         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
122 {
123         init();
124 }
125
126 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
127         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
128 {
129         init();
130 }
131
132 void Texture1D::Loader::init()
133 {
134         add("raw_data", &Loader::raw_data);
135         add("storage", &Loader::storage);
136 }
137
138 void Texture1D::Loader::raw_data(const string &data)
139 {
140         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
141 }
142
143 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
144 {
145         if(srgb)
146                 fmt = get_srgb_pixelformat(fmt);
147         obj.storage(fmt, w);
148 }
149
150 } // namespace GL
151 } // namespace Msp