]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.cpp
Require buffer to have storage for mapping
[libs/gl.git] / source / core / texture.cpp
1 #include <msp/datafile/rawdata.h>
2 #include <msp/io/memory.h>
3 #include "error.h"
4 #include "resourcemanager.h"
5 #include "texture.h"
6 #include "texture1d.h"
7 #include "texture2d.h"
8 #include "texture2darray.h"
9 #include "texture3d.h"
10 #include "texturecube.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Texture::Texture(unsigned t):
18         TextureBackend(t)
19 { }
20
21 void Texture::set_format(PixelFormat fmt)
22 {
23         PixelComponents comp = get_components(fmt);
24         ComponentSwizzle swiz = get_required_swizzle(comp);
25         PixelComponents st_comp = unswizzle_components(comp, swiz);
26
27         PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
28         require_pixelformat(st_fmt);
29         if(swiz!=NO_SWIZZLE)
30                 require_swizzle();
31
32         format = fmt;
33         storage_fmt = st_fmt;
34         swizzle = swiz;
35 }
36
37 unsigned Texture::count_levels(unsigned size)
38 {
39         unsigned n = 0;
40         for(; size; size>>=1, ++n) ;
41         return n;
42 }
43
44 void Texture::load_image(const string &fn, unsigned lv)
45 {
46         Graphics::Image img;
47         img.load_file(fn);
48
49         image(img, lv);
50 }
51
52 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
53 {
54         static GenericLoader::TypeRegistry registry;
55         static bool initialized = false;
56         if(!initialized)
57         {
58                 initialized = true;
59                 registry.register_type<Texture1D>("1d");
60                 registry.register_type<Texture2D>("2d");
61                 registry.register_type<Texture3D>("3d");
62                 registry.register_type<Texture2DArray>("2d_array");
63                 registry.register_type<TextureCube>("cube");
64         }
65         return registry;
66 }
67
68
69 Texture::Loader::Loader(Texture &t, Collection *c):
70         CollectionObjectLoader<Texture>(t, c),
71         levels(0)
72 {
73         add("external_data", &Loader::external_data);
74         add("external_image", &Loader::external_image, false);
75         add("external_image_srgb", &Loader::external_image, true);
76         add("generate_mipmap", &Loader::generate_mipmap);
77         add("image_data", &Loader::image_data);
78         add("mipmap_levels", &Loader::mipmap_levels);
79         add("raw_data", &Loader::raw_data);
80 }
81
82 void Texture::Loader::finish()
83 {
84         if(obj.auto_gen_mipmap)
85                 obj.generate_mipmap();
86 }
87
88 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
89 {
90         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
91         if(!io)
92                 throw IO::file_not_found(fn);
93         img.load_io(*io);
94 }
95
96 void Texture::Loader::external_data(const string &fn)
97 {
98         if(obj.manager)
99                 obj.manager->set_resource_location(obj, get_collection(), fn);
100         else
101         {
102                 DataFile::RawData rd;
103                 rd.open_file(get_collection(), fn);
104                 rd.load();
105                 obj.image(0, rd.get_data());
106         }
107 }
108
109 void Texture::Loader::external_image(bool srgb, const string &fn)
110 {
111         obj.use_srgb_format = srgb;
112         if(obj.manager)
113                 obj.manager->set_resource_location(obj, get_collection(), fn);
114         else
115         {
116                 Graphics::Image img;
117                 load_external_image(img, fn);
118                 obj.image(img, levels);
119         }
120 }
121
122 void Texture::Loader::generate_mipmap(bool gm)
123 {
124         obj.auto_gen_mipmap = gm;
125 }
126
127 void Texture::Loader::image_data(const string &data)
128 {
129         if(obj.manager)
130                 obj.set_manager(0);
131
132         Graphics::Image img;
133         IO::Memory mem(data.data(), data.size());
134         img.load_io(mem);
135
136         obj.image(img, levels);
137 }
138
139 void Texture::Loader::mipmap_levels(unsigned l)
140 {
141         levels = l;
142 }
143
144 void Texture::Loader::raw_data(const string &data)
145 {
146         if(obj.manager)
147                 obj.set_manager(0);
148
149         obj.image(0, data.data());
150 }
151
152 } // namespace GL
153 } // namespace Msp