]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Remove the separate allocation step from textures and buffers
[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         unsigned w = img.get_width();
89         PixelFormat fmt = pixelformat_from_image(img);
90         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
91
92         image(0, img.get_pixels());
93 }
94
95 unsigned Texture1D::get_n_levels() const
96 {
97         unsigned n = 0;
98         for(unsigned s=width; s; s>>=1, ++n) ;
99         return n;
100 }
101
102 unsigned Texture1D::get_level_size(unsigned level) const
103 {
104         return width>>level;
105 }
106
107 uint64_t Texture1D::get_data_size() const
108 {
109         return id ? width*get_pixel_size(storage_fmt) : 0;
110 }
111
112
113 Texture1D::Loader::Loader(Texture1D &t):
114         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
115 {
116         init();
117 }
118
119 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
120         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
121 {
122         init();
123 }
124
125 void Texture1D::Loader::init()
126 {
127         add("raw_data", &Loader::raw_data);
128         add("storage", &Loader::storage);
129         add("storage", &Loader::storage_levels);
130 }
131
132 void Texture1D::Loader::raw_data(const string &data)
133 {
134         obj.image(0, data.data());
135 }
136
137 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
138 {
139         obj.storage(fmt, w);
140 }
141
142 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
143 {
144         obj.storage(fmt, w, l);
145 }
146
147 } // namespace GL
148 } // namespace Msp