]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.cpp
Mark the start of a SPIR-V function as reachable
[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         format(NO_PIXELFORMAT),
20         storage_fmt(format),
21         swizzle(NO_SWIZZLE),
22         use_srgb_format(false),
23         auto_gen_mipmap(false)
24 { }
25
26 void Texture::set_format(PixelFormat fmt)
27 {
28         PixelComponents comp = get_components(fmt);
29         PixelComponents st_comp = comp;
30         FormatSwizzle swiz = NO_SWIZZLE;
31         switch(comp)
32         {
33         case LUMINANCE:
34                 st_comp = RED;
35                 swiz = R_TO_LUMINANCE;
36                 break;
37         case LUMINANCE_ALPHA:
38                 st_comp = RG;
39                 swiz = RG_TO_LUMINANCE_ALPHA;
40                 break;
41         case BGR:
42                 st_comp = RGB;
43                 swiz = RGB_TO_BGR;
44                 break;
45         case BGRA:
46                 st_comp = RGBA;
47                 swiz = RGB_TO_BGR;
48                 break;
49         default:;
50         }
51
52         PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
53         require_pixelformat(st_fmt);
54         if(swiz!=NO_SWIZZLE)
55                 require_swizzle();
56
57         format = fmt;
58         storage_fmt = st_fmt;
59         swizzle = swiz;
60 }
61
62 void Texture::load_image(const string &fn, unsigned lv)
63 {
64         Graphics::Image img;
65         img.load_file(fn);
66
67         image(img, lv);
68 }
69
70 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
71 {
72         static GenericLoader::TypeRegistry registry;
73         static bool initialized = false;
74         if(!initialized)
75         {
76                 initialized = true;
77                 registry.register_type<Texture1D>("1d");
78                 registry.register_type<Texture2D>("2d");
79                 registry.register_type<Texture3D>("3d");
80                 registry.register_type<Texture2DArray>("2d_array");
81                 registry.register_type<TextureCube>("cube");
82         }
83         return registry;
84 }
85
86
87 Texture::Loader::Loader(Texture &t, Collection *c):
88         CollectionObjectLoader<Texture>(t, c),
89         levels(0)
90 {
91         add("external_data", &Loader::external_data);
92         add("external_image", &Loader::external_image, false);
93         add("external_image_srgb", &Loader::external_image, true);
94         add("generate_mipmap", &Loader::generate_mipmap);
95         add("image_data", &Loader::image_data);
96         add("mipmap_levels", &Loader::mipmap_levels);
97         add("raw_data", &Loader::raw_data);
98 }
99
100 void Texture::Loader::finish()
101 {
102         if(obj.auto_gen_mipmap)
103                 obj.generate_mipmap();
104 }
105
106 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
107 {
108         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
109         if(!io)
110                 throw IO::file_not_found(fn);
111         img.load_io(*io);
112 }
113
114 void Texture::Loader::external_data(const string &fn)
115 {
116         if(obj.manager)
117                 obj.manager->set_resource_location(obj, get_collection(), fn);
118         else
119         {
120                 DataFile::RawData rd;
121                 rd.open_file(get_collection(), fn);
122                 rd.load();
123                 obj.image(0, rd.get_data());
124         }
125 }
126
127 void Texture::Loader::external_image(bool srgb, const string &fn)
128 {
129         obj.use_srgb_format = srgb;
130         if(obj.manager)
131                 obj.manager->set_resource_location(obj, get_collection(), fn);
132         else
133         {
134                 Graphics::Image img;
135                 load_external_image(img, fn);
136                 obj.image(img, levels);
137         }
138 }
139
140 void Texture::Loader::generate_mipmap(bool gm)
141 {
142         obj.auto_gen_mipmap = gm;
143 }
144
145 void Texture::Loader::image_data(const string &data)
146 {
147         if(obj.manager)
148                 obj.set_manager(0);
149
150         Graphics::Image img;
151         IO::Memory mem(data.data(), data.size());
152         img.load_io(mem);
153
154         obj.image(img, levels);
155 }
156
157 void Texture::Loader::mipmap_levels(unsigned l)
158 {
159         levels = l;
160 }
161
162 void Texture::Loader::raw_data(const string &data)
163 {
164         if(obj.manager)
165                 obj.set_manager(0);
166
167         obj.image(0, data.data());
168 }
169
170 } // namespace GL
171 } // namespace Msp