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