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