-
-Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
- texture(t),
- io(i),
- mapped_address(0),
- img_loader(Graphics::ImageLoader::open_io(io)),
- phase(0)
-{ }
-
-Texture2D::AsyncLoader::~AsyncLoader()
-{
- if(mapped_address)
- pixel_buffer.unmap();
- delete img_loader;
-}
-
-bool Texture2D::AsyncLoader::needs_sync() const
-{
- return phase%2;
-}
-
-bool Texture2D::AsyncLoader::process()
-{
- if(phase==0)
- {
- image.load_headers(*img_loader);
- n_bytes = image.get_stride()*image.get_height();
- }
- else if(phase==1)
- {
- pixel_buffer.storage(n_bytes);
- mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
- }
- else if(phase==2)
- 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.generate_id();
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id());
- texture.image(image, 0, true);
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
- if(texture.auto_gen_mipmap)
- texture.generate_mipmap();
- }
-
- ++phase;
- return phase>3;
-}
-