]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Use a scratch binding to modify textures and buffers
[libs/gl.git] / source / core / 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 "error.h"
6 #include "texture1d.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 Texture1D::Texture1D():
14         Texture(GL_TEXTURE_1D),
15         width(0),
16         allocated(0)
17 {
18         static Require _req(MSP_texture1D);
19 }
20
21 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
22 {
23         if(width>0)
24         {
25                 if(fmt!=format || wd!=width || (lv && lv!=levels))
26                         throw incompatible_data("Texture1D::storage");
27                 return;
28         }
29         if(wd==0)
30                 throw invalid_argument("Texture1D::storage");
31
32         set_format(fmt);
33         width = wd;
34         levels = get_n_levels();
35         if(lv)
36                 levels = min(levels, lv);
37 }
38
39 void Texture1D::allocate(unsigned level)
40 {
41         if(width==0)
42                 throw invalid_operation("Texture1D::allocate");
43         if(level>=levels)
44                 throw invalid_argument("Texture1D::allocate");
45         if(allocated&(1<<level))
46                 return;
47
48         if(ARB_texture_storage)
49         {
50                 GLenum fmt = get_gl_pixelformat(storage_fmt);
51                 if(ARB_direct_state_access)
52                         glTextureStorage1D(id, levels, fmt, width);
53                 else
54                 {
55                         bind_scratch();
56                         glTexStorage1D(target, levels, fmt, width);
57                 }
58                 apply_swizzle();
59                 allocated |= (1<<levels)-1;
60         }
61         else
62                 image(level, 0);
63 }
64
65 void Texture1D::image(unsigned level, const void *data)
66 {
67         if(width==0)
68                 throw invalid_operation("Texture1D::image");
69         if(level>=levels)
70                 throw out_of_range("Texture1D::image");
71
72         if(ARB_texture_storage)
73                 return sub_image(level, 0, get_level_size(level), data);
74
75         bind_scratch();
76
77         if(!allocated)
78         {
79                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
80                 apply_swizzle();
81         }
82
83         GLenum fmt = get_gl_pixelformat(storage_fmt);
84         GLenum comp = get_gl_components(get_components(storage_fmt));
85         GLenum type = get_gl_type(get_component_type(storage_fmt));
86         glTexImage1D(target, level, fmt, get_level_size(level), 0, comp, type, data);
87
88         allocated |= 1<<level;
89         if(auto_gen_mipmap && level==0)
90         {
91                 generate_mipmap();
92                 allocated |= (1<<levels)-1;
93         }
94 }
95
96 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
97 {
98         if(comp!=get_components(format) || type!=get_component_type(format))
99                 throw incompatible_data("Texture1D::image");
100         image(level, data);
101 }
102
103 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
104 {
105         if(width==0)
106                 throw invalid_operation("Texture1D::sub_image");
107         if(level>=levels)
108                 throw out_of_range("Texture1D::sub_image");
109
110         allocate(level);
111
112         GLenum comp = get_gl_components(get_components(storage_fmt));
113         GLenum type = get_gl_type(get_component_type(storage_fmt));
114         if(ARB_direct_state_access)
115                 glTextureSubImage1D(id, level, x, wd, comp, type, data);
116         else
117         {
118                 bind_scratch();
119                 glTexSubImage1D(target, level, x, wd, comp, type, data);
120         }
121
122         if(auto_gen_mipmap && level==0)
123                 generate_mipmap();
124 }
125
126 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
127 {
128         if(comp!=get_components(format) || type!=get_component_type(format))
129                 throw incompatible_data("Texture1D::sub_image");
130         sub_image(level, x, wd, data);
131 }
132
133 void Texture1D::image(const Graphics::Image &img, unsigned lv)
134 {
135         if(img.get_height()!=1)
136                 throw incompatible_data("Texture1D::image");
137
138         unsigned w = img.get_width();
139         PixelFormat fmt = pixelformat_from_image(img);
140         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
141
142         image(0, img.get_pixels());
143 }
144
145 unsigned Texture1D::get_n_levels() const
146 {
147         unsigned n = 0;
148         for(unsigned s=width; s; s>>=1, ++n) ;
149         return n;
150 }
151
152 unsigned Texture1D::get_level_size(unsigned level) const
153 {
154         return width>>level;
155 }
156
157 UInt64 Texture1D::get_data_size() const
158 {
159         return id ? width*get_pixel_size(storage_fmt) : 0;
160 }
161
162
163 Texture1D::Loader::Loader(Texture1D &t):
164         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
165 {
166         init();
167 }
168
169 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
170         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
171 {
172         init();
173 }
174
175 void Texture1D::Loader::init()
176 {
177         add("raw_data", &Loader::raw_data);
178         add("storage", &Loader::storage);
179         add("storage", &Loader::storage_levels);
180 }
181
182 void Texture1D::Loader::raw_data(const string &data)
183 {
184         obj.image(0, data.data());
185 }
186
187 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
188 {
189         obj.storage(fmt, w);
190 }
191
192 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
193 {
194         obj.storage(fmt, w, l);
195 }
196
197 } // namespace GL
198 } // namespace Msp