Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/compressed_texture.h
21136 views
1
/**************************************************************************/
2
/* compressed_texture.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/resource_loader.h"
34
#include "scene/resources/texture.h"
35
#include "servers/rendering/rendering_server.h"
36
37
class BitMap;
38
39
class CompressedTexture2D : public Texture2D {
40
GDCLASS(CompressedTexture2D, Texture2D);
41
42
public:
43
enum DataFormat {
44
DATA_FORMAT_IMAGE,
45
DATA_FORMAT_PNG,
46
DATA_FORMAT_WEBP,
47
DATA_FORMAT_BASIS_UNIVERSAL,
48
};
49
50
enum {
51
FORMAT_VERSION = 1
52
};
53
54
enum FormatBits {
55
FORMAT_BIT_STREAM = 1 << 22,
56
FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
57
FORMAT_BIT_DETECT_3D = 1 << 24,
58
//FORMAT_BIT_DETECT_SRGB = 1 << 25,
59
FORMAT_BIT_DETECT_NORMAL = 1 << 26,
60
FORMAT_BIT_DETECT_ROUGNESS = 1 << 27,
61
};
62
63
private:
64
String path_to_file;
65
mutable RID texture;
66
Image::Format format = Image::FORMAT_L8;
67
int w = 0;
68
int h = 0;
69
mutable Ref<BitMap> alpha_cache;
70
71
Error _load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit = 0);
72
virtual void reload_from_file() override;
73
74
static void _requested_3d(void *p_ud);
75
static void _requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
76
static void _requested_normal(void *p_ud);
77
78
protected:
79
static void _bind_methods();
80
81
public:
82
static Ref<Image> load_image_from_file(Ref<FileAccess> p_file, int p_size_limit);
83
84
typedef void (*TextureFormatRequestCallback)(const Ref<CompressedTexture2D> &);
85
typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<CompressedTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
86
87
static TextureFormatRequestCallback request_3d_callback;
88
static TextureFormatRoughnessRequestCallback request_roughness_callback;
89
static TextureFormatRequestCallback request_normal_callback;
90
91
Image::Format get_format() const;
92
Error load(const String &p_path);
93
String get_load_path() const;
94
95
int get_width() const override;
96
int get_height() const override;
97
virtual RID get_rid() const override;
98
99
virtual void set_path(const String &p_path, bool p_take_over) override;
100
101
virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
102
virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
103
virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;
104
105
virtual bool has_alpha() const override;
106
bool is_pixel_opaque(int p_x, int p_y) const override;
107
108
virtual Ref<Image> get_image() const override;
109
110
~CompressedTexture2D();
111
};
112
113
class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {
114
GDSOFTCLASS(ResourceFormatLoaderCompressedTexture2D, ResourceFormatLoader);
115
116
public:
117
virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
118
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
119
virtual bool handles_type(const String &p_type) const override;
120
virtual String get_resource_type(const String &p_path) const override;
121
};
122
123
class CompressedTextureLayered : public TextureLayered {
124
GDCLASS(CompressedTextureLayered, TextureLayered);
125
126
public:
127
enum DataFormat {
128
DATA_FORMAT_IMAGE,
129
DATA_FORMAT_PNG,
130
DATA_FORMAT_WEBP,
131
DATA_FORMAT_BASIS_UNIVERSAL,
132
};
133
134
enum {
135
FORMAT_VERSION = 1
136
};
137
138
enum FormatBits {
139
FORMAT_BIT_STREAM = 1 << 22,
140
FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
141
};
142
143
private:
144
Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
145
String path_to_file;
146
mutable RID texture;
147
Image::Format format = Image::FORMAT_L8;
148
int w = 0;
149
int h = 0;
150
int layers = 0;
151
bool mipmaps = false;
152
LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
153
154
virtual void reload_from_file() override;
155
156
protected:
157
static void _bind_methods();
158
159
public:
160
Image::Format get_format() const override;
161
Error load(const String &p_path);
162
String get_load_path() const;
163
virtual LayeredType get_layered_type() const override;
164
165
int get_width() const override;
166
int get_height() const override;
167
int get_layers() const override;
168
virtual bool has_mipmaps() const override;
169
virtual RID get_rid() const override;
170
171
virtual void set_path(const String &p_path, bool p_take_over) override;
172
173
virtual Ref<Image> get_layer_data(int p_layer) const override;
174
175
CompressedTextureLayered(LayeredType p_layered_type);
176
~CompressedTextureLayered();
177
};
178
179
class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {
180
GDSOFTCLASS(ResourceFormatLoaderCompressedTextureLayered, ResourceFormatLoader);
181
182
public:
183
virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
184
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
185
virtual bool handles_type(const String &p_type) const override;
186
virtual String get_resource_type(const String &p_path) const override;
187
};
188
189
class CompressedTexture2DArray : public CompressedTextureLayered {
190
GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)
191
public:
192
CompressedTexture2DArray() :
193
CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
194
};
195
196
class CompressedCubemap : public CompressedTextureLayered {
197
GDCLASS(CompressedCubemap, CompressedTextureLayered);
198
199
public:
200
CompressedCubemap() :
201
CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}
202
};
203
204
class CompressedCubemapArray : public CompressedTextureLayered {
205
GDCLASS(CompressedCubemapArray, CompressedTextureLayered);
206
207
public:
208
CompressedCubemapArray() :
209
CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
210
};
211
212
class CompressedTexture3D : public Texture3D {
213
GDCLASS(CompressedTexture3D, Texture3D);
214
215
public:
216
enum DataFormat {
217
DATA_FORMAT_IMAGE,
218
DATA_FORMAT_PNG,
219
DATA_FORMAT_WEBP,
220
DATA_FORMAT_BASIS_UNIVERSAL,
221
};
222
223
enum {
224
FORMAT_VERSION = 1
225
};
226
227
enum FormatBits {
228
FORMAT_BIT_STREAM = 1 << 22,
229
FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
230
};
231
232
private:
233
Error _load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps);
234
String path_to_file;
235
mutable RID texture;
236
Image::Format format = Image::FORMAT_L8;
237
int w = 0;
238
int h = 0;
239
int d = 0;
240
bool mipmaps = false;
241
242
virtual void reload_from_file() override;
243
244
protected:
245
static void _bind_methods();
246
247
public:
248
Image::Format get_format() const override;
249
Error load(const String &p_path);
250
String get_load_path() const;
251
252
int get_width() const override;
253
int get_height() const override;
254
int get_depth() const override;
255
virtual bool has_mipmaps() const override;
256
virtual RID get_rid() const override;
257
258
virtual void set_path(const String &p_path, bool p_take_over) override;
259
260
virtual Vector<Ref<Image>> get_data() const override;
261
262
~CompressedTexture3D();
263
};
264
265
class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {
266
GDSOFTCLASS(ResourceFormatLoaderCompressedTexture3D, ResourceFormatLoader);
267
268
public:
269
virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
270
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
271
virtual bool handles_type(const String &p_type) const override;
272
virtual String get_resource_type(const String &p_path) const override;
273
};
274
275