-
-OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
- texture(t),
- io(i)
-{
- char magic[4] = { };
- io.read(magic, 4);
- io.seek(0, IO::S_BEG);
-
- if(DataFile::RawData::detect_signature(string(magic, 4)))
- {
- raw_data = new DataFile::RawData;
- raw_data->open_io(io, "async");
- }
- else
- img_loader = Graphics::ImageLoader::open_io(io);
-}
-
-OpenGLTexture2D::AsyncLoader::~AsyncLoader()
-{
- if(mapped_address)
- pixel_buffer.unmap();
- delete img_loader;
- delete raw_data;
-}
-
-bool OpenGLTexture2D::AsyncLoader::needs_sync() const
-{
- return phase%2;
-}
-
-bool OpenGLTexture2D::AsyncLoader::process()
-{
- if(phase==0)
- {
- if(raw_data)
- n_bytes = raw_data->get_size();
- else
- {
- image.load_headers(*img_loader);
- n_bytes = image.get_stride()*image.get_height();
- }
- }
- else if(phase==1)
- {
- pixel_buffer.storage(n_bytes, STREAMING);
- mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
- }
- else if(phase==2)
- {
- if(raw_data)
- raw_data->load_into(mapped_address);
- else
- image.load_into(*img_loader, mapped_address);
- }
- else if(phase==3)
- {
- mapped_address = 0;
- if(!pixel_buffer.unmap())
- {
- phase = 1;
- return false;
- }
-
- if(!texture.id)
- texture.create();
-
- if(img_loader)
- {
- unsigned w = image.get_width();
- unsigned h = image.get_height();
- texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h);
- }
- texture.OpenGLTexture2D::sub_image(0, 0, 0, texture.width, texture.height, pixel_buffer, 0);
-
- if(texture.auto_gen_mipmap)
- texture.generate_mipmap();
- }
-
- ++phase;
- return phase>3;
-}
-