]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Add an srgb flag to pixelformat_from_image
[libs/gl.git] / source / core / texture1d.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/msp_texture1d.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         width(0)
15 {
16         static Require _req(MSP_texture1D);
17 }
18
19 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
20 {
21         if(width>0)
22         {
23                 if(fmt!=format || wd!=width || (lv && lv!=levels))
24                         throw incompatible_data("Texture1D::storage");
25                 return;
26         }
27         if(wd==0)
28                 throw invalid_argument("Texture1D::storage");
29
30         set_format(fmt);
31         width = wd;
32         levels = get_n_levels();
33         if(lv)
34                 levels = min(levels, lv);
35
36         GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
37         if(ARB_texture_storage)
38         {
39                 if(ARB_direct_state_access)
40                         glTextureStorage1D(id, levels, gl_fmt, width);
41                 else
42                 {
43                         bind_scratch();
44                         glTexStorage1D(target, levels, gl_fmt, width);
45                 }
46         }
47         else
48         {
49                 bind_scratch();
50                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
51                 GLenum comp = get_gl_components(get_components(storage_fmt));
52                 GLenum type = get_gl_type(get_component_type(storage_fmt));
53                 for(unsigned i=0; i<levels; ++i)
54                         glTexImage1D(target, i, gl_fmt, get_level_size(i), 0, comp, type, 0);
55         }
56
57         apply_swizzle();
58 }
59
60 void Texture1D::image(unsigned level, const void *data)
61 {
62         return sub_image(level, 0, get_level_size(level), data);
63 }
64
65 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
66 {
67         if(width==0)
68                 throw invalid_operation("Texture1D::sub_image");
69         if(level>=levels)
70                 throw out_of_range("Texture1D::sub_image");
71
72         GLenum comp = get_gl_components(get_components(storage_fmt));
73         GLenum type = get_gl_type(get_component_type(storage_fmt));
74         if(ARB_direct_state_access)
75                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
76         else
77         {
78                 bind_scratch();
79                 glTexSubImage1D(target, level, x, wd, comp, type, data);
80         }
81 }
82
83 void Texture1D::image(const Graphics::Image &img, unsigned lv)
84 {
85         if(img.get_height()!=1)
86                 throw incompatible_data("Texture1D::image");
87
88         storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), lv);
89         image(0, img.get_pixels());
90 }
91
92 unsigned Texture1D::get_n_levels() const
93 {
94         unsigned n = 0;
95         for(unsigned s=width; s; s>>=1, ++n) ;
96         return n;
97 }
98
99 unsigned Texture1D::get_level_size(unsigned level) const
100 {
101         return width>>level;
102 }
103
104 uint64_t Texture1D::get_data_size() const
105 {
106         return id ? width*get_pixel_size(storage_fmt) : 0;
107 }
108
109
110 Texture1D::Loader::Loader(Texture1D &t):
111         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
112 {
113         init();
114 }
115
116 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
117         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
118 {
119         init();
120 }
121
122 void Texture1D::Loader::init()
123 {
124         add("raw_data", &Loader::raw_data);
125         add("storage", &Loader::storage);
126         add("storage", &Loader::storage_levels);
127 }
128
129 void Texture1D::Loader::raw_data(const string &data)
130 {
131         obj.image(0, data.data());
132 }
133
134 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
135 {
136         obj.storage(fmt, w);
137 }
138
139 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
140 {
141         obj.storage(fmt, w, l);
142 }
143
144 } // namespace GL
145 } // namespace Msp