Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/compressed_texture.h
9903 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
36
class BitMap;
37
38
class CompressedTexture2D : public Texture2D {
39
GDCLASS(CompressedTexture2D, Texture2D);
40
41
public:
42
enum DataFormat {
43
DATA_FORMAT_IMAGE,
44
DATA_FORMAT_PNG,
45
DATA_FORMAT_WEBP,
46
DATA_FORMAT_BASIS_UNIVERSAL,
47
};
48
49
enum {
50
FORMAT_VERSION = 1
51
};
52
53
enum FormatBits {
54
FORMAT_BIT_STREAM = 1 << 22,
55
FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
56
FORMAT_BIT_DETECT_3D = 1 << 24,
57
//FORMAT_BIT_DETECT_SRGB = 1 << 25,
58
FORMAT_BIT_DETECT_NORMAL = 1 << 26,
59
FORMAT_BIT_DETECT_ROUGNESS = 1 << 27,
60
};
61
62
private:
63
String path_to_file;
64
mutable RID texture;
65
Image::Format format = Image::FORMAT_L8;
66
int w = 0;
67
int h = 0;
68
mutable Ref<BitMap> alpha_cache;
69
70
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);
71
virtual void reload_from_file() override;
72
73
static void _requested_3d(void *p_ud);
74
static void _requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
75
static void _requested_normal(void *p_ud);
76
77
protected:
78
static void _bind_methods();
79
80
public:
81
static Ref<Image> load_image_from_file(Ref<FileAccess> p_file, int p_size_limit);
82
83
typedef void (*TextureFormatRequestCallback)(const Ref<CompressedTexture2D> &);
84
typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<CompressedTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
85
86
static TextureFormatRequestCallback request_3d_callback;
87
static TextureFormatRoughnessRequestCallback request_roughness_callback;
88
static TextureFormatRequestCallback request_normal_callback;
89
90
Image::Format get_format() const;
91
Error load(const String &p_path);
92
String get_load_path() const;
93
94
int get_width() const override;
95
int get_height() const override;
96
virtual RID get_rid() const override;
97
98
virtual void set_path(const String &p_path, bool p_take_over) override;
99
100
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;
101
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;
102
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;
103
104
virtual bool has_alpha() const override;
105
bool is_pixel_opaque(int p_x, int p_y) const override;
106
107
virtual Ref<Image> get_image() const override;
108
109
~CompressedTexture2D();
110
};
111
112
class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {
113
public:
114
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;
115
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
116
virtual bool handles_type(const String &p_type) const override;
117
virtual String get_resource_type(const String &p_path) const override;
118
};
119
120
class CompressedTextureLayered : public TextureLayered {
121
GDCLASS(CompressedTextureLayered, TextureLayered);
122
123
public:
124
enum DataFormat {
125
DATA_FORMAT_IMAGE,
126
DATA_FORMAT_PNG,
127
DATA_FORMAT_WEBP,
128
DATA_FORMAT_BASIS_UNIVERSAL,
129
};
130
131
enum {
132
FORMAT_VERSION = 1
133
};
134
135
enum FormatBits {
136
FORMAT_BIT_STREAM = 1 << 22,
137
FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
138
};
139
140
private:
141
Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
142
String path_to_file;
143
mutable RID texture;
144
Image::Format format = Image::FORMAT_L8;
145
int w = 0;
146
int h = 0;
147
int layers = 0;
148
bool mipmaps = false;
149
LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
150
151
virtual void reload_from_file() override;
152
153
protected:
154
static void _bind_methods();
155
156
public:
157
Image::Format get_format() const override;
158
Error load(const String &p_path);
159
String get_load_path() const;
160
virtual LayeredType get_layered_type() const override;
161
162
int get_width() const override;
163
int get_height() const override;
164
int get_layers() const override;
165
virtual bool has_mipmaps() const override;
166
virtual RID get_rid() const override;
167
168
virtual void set_path(const String &p_path, bool p_take_over) override;
169
170
virtual Ref<Image> get_layer_data(int p_layer) const override;
171
172
CompressedTextureLayered(LayeredType p_layered_type);
173
~CompressedTextureLayered();
174
};
175
176
class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {
177
public:
178
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;
179
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
180
virtual bool handles_type(const String &p_type) const override;
181
virtual String get_resource_type(const String &p_path) const override;
182
};
183
184
class CompressedTexture2DArray : public CompressedTextureLayered {
185
GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)
186
public:
187
CompressedTexture2DArray() :
188
CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
189
};
190
191
class CompressedCubemap : public CompressedTextureLayered {
192
GDCLASS(CompressedCubemap, CompressedTextureLayered);
193
194
public:
195
CompressedCubemap() :
196
CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}
197
};
198
199
class CompressedCubemapArray : public CompressedTextureLayered {
200
GDCLASS(CompressedCubemapArray, CompressedTextureLayered);
201
202
public:
203
CompressedCubemapArray() :
204
CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
205
};
206
207
class CompressedTexture3D : public Texture3D {
208
GDCLASS(CompressedTexture3D, Texture3D);
209
210
public:
211
enum DataFormat {
212
DATA_FORMAT_IMAGE,
213
DATA_FORMAT_PNG,
214
DATA_FORMAT_WEBP,
215
DATA_FORMAT_BASIS_UNIVERSAL,
216
};
217
218
enum {
219
FORMAT_VERSION = 1
220
};
221
222
enum FormatBits {
223
FORMAT_BIT_STREAM = 1 << 22,
224
FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
225
};
226
227
private:
228
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);
229
String path_to_file;
230
mutable RID texture;
231
Image::Format format = Image::FORMAT_L8;
232
int w = 0;
233
int h = 0;
234
int d = 0;
235
bool mipmaps = false;
236
237
virtual void reload_from_file() override;
238
239
protected:
240
static void _bind_methods();
241
242
public:
243
Image::Format get_format() const override;
244
Error load(const String &p_path);
245
String get_load_path() const;
246
247
int get_width() const override;
248
int get_height() const override;
249
int get_depth() const override;
250
virtual bool has_mipmaps() const override;
251
virtual RID get_rid() const override;
252
253
virtual void set_path(const String &p_path, bool p_take_over) override;
254
255
virtual Vector<Ref<Image>> get_data() const override;
256
257
~CompressedTexture3D();
258
};
259
260
class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {
261
public:
262
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;
263
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
264
virtual bool handles_type(const String &p_type) const override;
265
virtual String get_resource_type(const String &p_path) const override;
266
};
267
268