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