+#include <msp/datafile/rawdata.h>
#include <msp/io/memory.h>
#include "error.h"
#include "resourcemanager.h"
namespace GL {
Texture::Texture(unsigned t):
- TextureBackend(t),
- format(NO_PIXELFORMAT),
- storage_fmt(format),
- swizzle(NO_SWIZZLE),
- use_srgb_format(false),
- auto_gen_mipmap(false)
+ TextureBackend(t)
{ }
void Texture::set_format(PixelFormat fmt)
{
PixelComponents comp = get_components(fmt);
- PixelComponents st_comp = comp;
- FormatSwizzle swiz = NO_SWIZZLE;
- switch(comp)
- {
- case LUMINANCE:
- st_comp = RED;
- swiz = R_TO_LUMINANCE;
- break;
- case LUMINANCE_ALPHA:
- st_comp = RG;
- swiz = RG_TO_LUMINANCE_ALPHA;
- break;
- case BGR:
- st_comp = RGB;
- swiz = RGB_TO_BGR;
- break;
- case BGRA:
- st_comp = RGBA;
- swiz = RGB_TO_BGR;
- break;
- default:;
- }
+ ComponentSwizzle swiz = get_required_swizzle(comp);
+ PixelComponents st_comp = unswizzle_components(comp, swiz);
PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
require_pixelformat(st_fmt);
swizzle = swiz;
}
+unsigned Texture::count_levels(unsigned size)
+{
+ unsigned n = 0;
+ for(; size; size>>=1, ++n) ;
+ return n;
+}
+
+void Texture::stage_pixels(void *staging, const void *data, size_t count)
+{
+ if(swizzle==RGBA_TO_RGB)
+ {
+ const uint32_t *src = static_cast<const uint32_t *>(data);
+ uint32_t *dst = static_cast<uint32_t *>(staging);
+ size_t i = 0;
+ for(; i+3<count; i+=4)
+ {
+ dst[0] = src[0]|0xFF000000;
+ dst[1] = (src[0]>>24)|(src[1]<<8)|0xFF000000;
+ dst[2] = (src[1]>>16)|(src[2]<<16)|0xFF000000;
+ dst[3] = (src[2]>>8)|0xFF000000;
+ src += 3;
+ dst += 4;
+ }
+
+ if(i<count)
+ {
+ const uint8_t *src_bytes = reinterpret_cast<const uint8_t *>(src);
+ for(; i<count; ++i)
+ {
+ *dst++ = src_bytes[0]|(src_bytes[1]<<8)|(src_bytes[2]<<16)|0xFF000000;
+ src_bytes += 3;
+ }
+ }
+ }
+ else
+ {
+ const char *src = static_cast<const char *>(data);
+ size_t data_size = count*get_pixel_size(storage_fmt);
+ copy(src, src+data_size, static_cast<char *>(staging));
+ }
+}
+
void Texture::load_image(const string &fn, unsigned lv)
{
Graphics::Image img;
CollectionObjectLoader<Texture>(t, c),
levels(0)
{
+ add("external_data", &Loader::external_data);
add("external_image", &Loader::external_image, false);
add("external_image_srgb", &Loader::external_image, true);
add("generate_mipmap", &Loader::generate_mipmap);
img.load_io(*io);
}
+void Texture::Loader::external_data(const string &fn)
+{
+ if(obj.manager)
+ obj.manager->set_resource_location(obj, get_collection(), fn);
+ else
+ {
+ DataFile::RawData rd;
+ rd.open_file(get_collection(), fn);
+ rd.load();
+ obj.image(0, rd.get_data());
+ }
+}
+
void Texture::Loader::external_image(bool srgb, const string &fn)
{
obj.use_srgb_format = srgb;