]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/texture2d_backend.cpp
Move the Resource function override of Texture classes into backend
[libs/gl.git] / source / backends / opengl / texture2d_backend.cpp
1 #include <msp/datafile/rawdata.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/arb_vertex_buffer_object.h>
5 #include <msp/graphics/imageloader.h>
6 #include "buffer.h"
7 #include "gl.h"
8 #include "texture2d.h"
9 #include "texture2d_backend.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 class OpenGLTexture2D::AsyncLoader: public Resource::AsyncLoader
17 {
18 private:
19         Texture2D &texture;
20         IO::Seekable &io;
21         Buffer pixel_buffer;
22         char *mapped_address = 0;
23         Graphics::Image image;
24         Graphics::ImageLoader *img_loader = 0;
25         DataFile::RawData *raw_data = 0;
26         unsigned n_bytes = 0;
27         int phase = 0;
28
29 public:
30         AsyncLoader(Texture2D &, IO::Seekable &);
31         ~AsyncLoader();
32
33         virtual bool needs_sync() const;
34         virtual bool process();
35 };
36
37
38 OpenGLTexture2D::OpenGLTexture2D():
39         Texture(GL_TEXTURE_2D)
40 { }
41
42 void OpenGLTexture2D::allocate()
43 {
44         unsigned width = static_cast<const Texture2D *>(this)->width;
45         unsigned height = static_cast<const Texture2D *>(this)->height;
46         unsigned levels = static_cast<const Texture2D *>(this)->levels;
47
48         if(!id)
49                 create();
50
51         GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
52         if(ARB_texture_storage)
53         {
54                 if(ARB_direct_state_access)
55                         glTextureStorage2D(id, levels, gl_fmt, width, height);
56                 else
57                 {
58                         bind_scratch();
59                         glTexStorage2D(target, levels, gl_fmt, width, height);
60                 }
61         }
62         else
63         {
64                 bind_scratch();
65                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
66                 GLenum comp = get_gl_components(get_components(storage_fmt));
67                 GLenum type = get_gl_type(get_component_type(storage_fmt));
68                 for(unsigned i=0; i<levels; ++i)
69                 {
70                         auto lv_size = static_cast<const Texture2D *>(this)->get_level_size(i);
71                         glTexImage2D(target, i, gl_fmt, lv_size.x, lv_size.y, 0, comp, type, 0);
72                 }
73         }
74
75         apply_swizzle();
76 }
77
78 void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
79 {
80         GLenum comp = get_gl_components(get_components(storage_fmt));
81         GLenum type = get_gl_type(get_component_type(storage_fmt));
82         if(ARB_direct_state_access)
83                 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
84         else
85         {
86                 bind_scratch();
87                 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
88         }
89 }
90
91 void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const Buffer &buffer, unsigned offset)
92 {
93         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer.id);
94         sub_image(level, x, y, wd, ht, reinterpret_cast<void *>(offset));
95         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
96 }
97
98 Resource::AsyncLoader *OpenGLTexture2D::load(IO::Seekable &io, const Resources *)
99 {
100         return new AsyncLoader(static_cast<Texture2D &>(*this), io);
101 }
102
103 uint64_t OpenGLTexture2D::get_data_size() const
104 {
105         unsigned width = static_cast<const Texture2D *>(this)->width;
106         unsigned height = static_cast<const Texture2D *>(this)->height;
107         return id ? width*height*get_pixel_size(format) : 0;
108 }
109
110 void OpenGLTexture2D::unload()
111 {
112         glDeleteTextures(1, &id);
113         id = 0;
114 }
115
116
117 OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
118         texture(t),
119         io(i)
120 {
121         char magic[4] = { };
122         io.read(magic, 4);
123         io.seek(0, IO::S_BEG);
124
125         if(DataFile::RawData::detect_signature(string(magic, 4)))
126         {
127                 raw_data = new DataFile::RawData;
128                 raw_data->open_io(io, "async");
129         }
130         else
131                 img_loader = Graphics::ImageLoader::open_io(io);
132 }
133
134 OpenGLTexture2D::AsyncLoader::~AsyncLoader()
135 {
136         if(mapped_address)
137                 pixel_buffer.unmap();
138         delete img_loader;
139         delete raw_data;
140 }
141
142 bool OpenGLTexture2D::AsyncLoader::needs_sync() const
143 {
144         return phase%2;
145 }
146
147 bool OpenGLTexture2D::AsyncLoader::process()
148 {
149         if(phase==0)
150         {
151                 if(raw_data)
152                         n_bytes = raw_data->get_size();
153                 else
154                 {
155                         image.load_headers(*img_loader);
156                         n_bytes = image.get_stride()*image.get_height();
157                 }
158         }
159         else if(phase==1)
160         {
161                 pixel_buffer.storage(n_bytes);
162                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
163         }
164         else if(phase==2)
165         {
166                 if(raw_data)
167                         raw_data->load_into(mapped_address);
168                 else
169                         image.load_into(*img_loader, mapped_address);
170         }
171         else if(phase==3)
172         {
173                 mapped_address = 0;
174                 if(!pixel_buffer.unmap())
175                 {
176                         phase = 1;
177                         return false;
178                 }
179
180                 if(!texture.id)
181                         texture.create();
182
183                 if(img_loader)
184                 {
185                         unsigned w = image.get_width();
186                         unsigned h = image.get_height();
187                         texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h);
188                 }
189                 texture.OpenGLTexture2D::sub_image(0, 0, 0, texture.width, texture.height, pixel_buffer, 0);
190
191                 if(texture.auto_gen_mipmap)
192                         texture.generate_mipmap();
193         }
194
195         ++phase;
196         return phase>3;
197 }
198
199 } // namespace GL
200 } // namespace Msp