]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Use explicit mipmap generation if necessary
[libs/gl.git] / source / texture1d.cpp
1 #include <msp/gl/extensions/msp_texture1d.h>
2 #include "bindable.h"
3 #include "error.h"
4 #include "texture1d.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Texture1D::Texture1D():
12         Texture(GL_TEXTURE_1D),
13         ifmt(RGB),
14         width(0),
15         allocated(0)
16 {
17         static Require _req(MSP_texture1D);
18 }
19
20 void Texture1D::storage(PixelFormat fmt, unsigned wd)
21 {
22         if(width>0)
23                 throw invalid_operation("Texture1D::storage");
24         if(wd==0)
25                 throw invalid_argument("Texture1D::storage");
26         require_pixelformat(fmt);
27
28         ifmt = fmt;
29         width = wd;
30 }
31
32 void Texture1D::allocate(unsigned level)
33 {
34         if(allocated&(1<<level))
35                 return;
36
37         image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
38 }
39
40 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
41 {
42         if(width==0)
43                 throw invalid_operation("Texture1D::image");
44
45         unsigned w = get_level_size(level);
46
47         BindRestore _bind(this);
48         glTexImage1D(target, level, ifmt, w, 0, fmt, type, data);
49
50         allocated |= 1<<level;
51         if(gen_mipmap && level==0)
52         {
53                 auto_generate_mipmap();
54                 for(; w; w>>=1, ++level) ;
55                 allocated |= (1<<level)-1;
56         }
57 }
58
59 unsigned Texture1D::get_level_size(unsigned level)
60 {
61         return width>>level;
62 }
63
64 UInt64 Texture1D::get_data_size() const
65 {
66         return id ? width*get_pixel_size(ifmt) : 0;
67 }
68
69 } // namespace GL
70 } // namespace Msp