1 #include <msp/datafile/rawdata.h>
2 #include <msp/io/memory.h>
4 #include "resourcemanager.h"
8 #include "texture2darray.h"
10 #include "texturecube.h"
17 Texture::Texture(unsigned t):
21 void Texture::set_format(PixelFormat fmt)
23 PixelComponents comp = get_components(fmt);
24 ComponentSwizzle swiz = get_required_swizzle(comp);
25 PixelComponents st_comp = unswizzle_components(comp, swiz);
27 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
28 require_pixelformat(st_fmt);
37 unsigned Texture::count_levels(unsigned size)
40 for(; size; size>>=1, ++n) ;
44 void Texture::stage_pixels(void *staging, const void *data, size_t count)
46 if(swizzle==RGBA_TO_RGB)
48 const uint32_t *src = static_cast<const uint32_t *>(data);
49 uint32_t *dst = static_cast<uint32_t *>(staging);
51 for(; i+3<count; i+=4)
53 dst[0] = src[0]|0xFF000000;
54 dst[1] = (src[0]>>24)|(src[1]<<8)|0xFF000000;
55 dst[2] = (src[1]>>16)|(src[2]<<16)|0xFF000000;
56 dst[3] = (src[2]>>8)|0xFF000000;
63 const uint8_t *src_bytes = reinterpret_cast<const uint8_t *>(src);
66 *dst++ = src_bytes[0]|(src_bytes[1]<<8)|(src_bytes[2]<<16)|0xFF000000;
73 const char *src = static_cast<const char *>(data);
74 size_t data_size = count*get_pixel_size(storage_fmt);
75 copy(src, src+data_size, static_cast<char *>(staging));
79 void Texture::load_image(const string &fn, unsigned lv)
87 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
89 static GenericLoader::TypeRegistry registry;
90 static bool initialized = false;
94 registry.register_type<Texture1D>("1d");
95 registry.register_type<Texture2D>("2d");
96 registry.register_type<Texture3D>("3d");
97 registry.register_type<Texture2DArray>("2d_array");
98 registry.register_type<TextureCube>("cube");
104 Texture::Loader::Loader(Texture &t, Collection *c):
105 CollectionObjectLoader<Texture>(t, c),
108 add("external_data", &Loader::external_data);
109 add("external_image", &Loader::external_image, false);
110 add("external_image_srgb", &Loader::external_image, true);
111 add("generate_mipmap", &Loader::generate_mipmap);
112 add("image_data", &Loader::image_data);
113 add("mipmap_levels", &Loader::mipmap_levels);
114 add("raw_data", &Loader::raw_data);
117 void Texture::Loader::finish()
119 if(obj.auto_gen_mipmap)
120 obj.generate_mipmap();
123 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
125 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
127 throw IO::file_not_found(fn);
131 void Texture::Loader::external_data(const string &fn)
134 obj.manager->set_resource_location(obj, get_collection(), fn);
137 DataFile::RawData rd;
138 rd.open_file(get_collection(), fn);
140 obj.image(0, rd.get_data());
144 void Texture::Loader::external_image(bool srgb, const string &fn)
146 obj.use_srgb_format = srgb;
148 obj.manager->set_resource_location(obj, get_collection(), fn);
152 load_external_image(img, fn);
153 obj.image(img, levels);
157 void Texture::Loader::generate_mipmap(bool gm)
159 obj.auto_gen_mipmap = gm;
162 void Texture::Loader::image_data(const string &data)
168 IO::Memory mem(data.data(), data.size());
171 obj.image(img, levels);
174 void Texture::Loader::mipmap_levels(unsigned l)
179 void Texture::Loader::raw_data(const string &data)
184 obj.image(0, data.data());