Debian Codegraph @

scene/resources/image_texture.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* image_texture.cpp */
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 #include "image_texture.h"
32
33 #include "core/io/image_loader.h"
34 #include "scene/resources/bit_map.h"
35 #include "scene/resources/placeholder_textures.h"
36 #include "servers/rendering/rendering_server.h"
37
38 void ImageTexture::reload_from_file() {
39 String path = ResourceLoader::path_remap(get_path());
40 if (!path.is_resource_file()) {
41 return;
42 }
43
44 Ref<Image> img;
45 img.instantiate();
46
47 if (ImageLoader::load_image(path, img) == OK) {
48 set_image(img);
49 } else {
50 Resource::reload_from_file();
51 notify_property_list_changed();
52 emit_changed();
53 }
54 }
55
56 Ref<ImageTexture> ImageTexture::create_from_image(const Ref<Image> &p_image) {
57 ERR_FAIL_COND_V_MSG(p_image.is_null(), Ref<ImageTexture>(), "Invalid image: null");
58 ERR_FAIL_COND_V_MSG(p_image->is_empty(), Ref<ImageTexture>(), "Invalid image: image is empty");
59
60 Ref<ImageTexture> image_texture;
61 image_texture.instantiate();
62 image_texture->set_image(p_image);
63 return image_texture;
64 }
65
66 void ImageTexture::set_image(const Ref<Image> &p_image) {
67 if (p_image.is_null() || p_image->is_empty()) {
68 if (image_stored) {
69 ERR_PRINT("Invalid image");
70 }
71 return;
72 }
73
74 w = p_image->get_width();
75 h = p_image->get_height();
76 format = p_image->get_format();
77 mipmaps = p_image->has_mipmaps();
78
79 if (texture.is_null()) {
80 texture = RenderingServer::get_singleton()->texture_2d_create(p_image);
81 } else {
82 RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_image);
83 RenderingServer::get_singleton()->texture_replace(texture, new_texture);
84 }
85 notify_property_list_changed();
86 emit_changed();
87
88 image_stored = true;
89 }
90
91 Image::Format ImageTexture::get_format() const {
92 return format;
93 }
94
95 void ImageTexture::update(const Ref<Image> &p_image) {
96 ERR_FAIL_COND_MSG(p_image.is_null(), "Invalid image");
97 ERR_FAIL_COND_MSG(texture.is_null(), "Texture is not initialized.");
98 ERR_FAIL_COND_MSG(p_image->get_width() != w || p_image->get_height() != h,
99 "The new image dimensions must match the texture size.");
100 ERR_FAIL_COND_MSG(p_image->get_format() != format,
101 "The new image format must match the texture's image format.");
102 ERR_FAIL_COND_MSG(mipmaps != p_image->has_mipmaps(),
103 "The new image mipmaps configuration must match the texture's image mipmaps configuration");
104
105 RS::get_singleton()->texture_2d_update(texture, p_image);
106
107 notify_property_list_changed();
108 emit_changed();
109
110 alpha_cache.unref();
111 image_stored = true;
112 }
113
114 Ref<Image> ImageTexture::get_image() const {
115 if (image_stored) {
116 return RenderingServer::get_singleton()->texture_2d_get(texture);
117 } else {
118 return Ref<Image>();
119 }
120 }
121
122 int ImageTexture::get_width() const {
123 return w;
124 }
125
126 int ImageTexture::get_height() const {
127 return h;
128 }
129
130 RID ImageTexture::get_rid() const {
131 if (texture.is_null()) {
132 // We are in trouble, create something temporary.
133 texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
134 }
135 return texture;
136 }
137
138 bool ImageTexture::has_alpha() const {
139 return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
140 }
141
142 void ImageTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
143 if ((w | h) == 0) {
144 return;
145 }
146 RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose);
147 }
148
149 void ImageTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
150 if ((w | h) == 0) {
151 return;
152 }
153 RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
154 }
155
156 void ImageTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
157 if ((w | h) == 0) {
158 return;
159 }
160 RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
161 }
162
163 bool ImageTexture::is_pixel_opaque(int p_x, int p_y) const {
164 if (alpha_cache.is_null()) {
165 Ref<Image> img = get_image();
166 if (img.is_valid()) {
167 if (img->is_compressed()) { //must decompress, if compressed
168 Ref<Image> decom = img->duplicate();
169 decom->decompress();
170 img = decom;
171 }
172 alpha_cache.instantiate();
173 alpha_cache->create_from_image_alpha(img);
174 }
175 }
176
177 if (alpha_cache.is_valid()) {
178 int aw = int(alpha_cache->get_size().width);
179 int ah = int(alpha_cache->get_size().height);
180 if (aw == 0 || ah == 0) {
181 return true;
182 }
183
184 int x = p_x * aw / w;
185 int y = p_y * ah / h;
186
187 x = CLAMP(x, 0, aw - 1);
188 y = CLAMP(y, 0, ah - 1);
189
190 return alpha_cache->get_bit(x, y);
191 }
192
193 return true;
194 }
195
196 void ImageTexture::set_size_override(const Size2i &p_size) {
197 Size2i s = p_size;
198 if (s.x != 0) {
199 w = s.x;
200 }
201 if (s.y != 0) {
202 h = s.y;
203 }
204 RenderingServer::get_singleton()->texture_set_size_override(texture, w, h);
205 }
206
207 void ImageTexture::set_path(const String &p_path, bool p_take_over) {
208 if (texture.is_valid()) {
209 RenderingServer::get_singleton()->texture_set_path(texture, p_path);
210 }
211
212 Resource::set_path(p_path, p_take_over);
213 }
214
215 void ImageTexture::_bind_methods() {
216 ClassDB::bind_static_method("ImageTexture", D_METHOD("create_from_image", "image"), &ImageTexture::create_from_image);
217 ClassDB::bind_method(D_METHOD("get_format"), &ImageTexture::get_format);
218
219 ClassDB::bind_method(D_METHOD("set_image", "image"), &ImageTexture::set_image);
220 ClassDB::bind_method(D_METHOD("update", "image"), &ImageTexture::update);
221 ClassDB::bind_method(D_METHOD("set_size_override", "size"), &ImageTexture::set_size_override);
222
223 ClassDB::bind_method(D_METHOD("_set_image", "image"), &ImageTexture::set_image);
224
225 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT), "_set_image", "get_image");
226 }
227
228 ImageTexture::~ImageTexture() {
229 if (texture.is_valid()) {
230 ERR_FAIL_NULL(RenderingServer::get_singleton());
231 RenderingServer::get_singleton()->free_rid(texture);
232 }
233 }
234
235 Image::Format ImageTextureLayered::get_format() const {
236 return format;
237 }
238
239 int ImageTextureLayered::get_width() const {
240 return width;
241 }
242
243 int ImageTextureLayered::get_height() const {
244 return height;
245 }
246
247 int ImageTextureLayered::get_layers() const {
248 return layers;
249 }
250
251 bool ImageTextureLayered::has_mipmaps() const {
252 return mipmaps;
253 }
254
255 ImageTextureLayered::LayeredType ImageTextureLayered::get_layered_type() const {
256 return layered_type;
257 }
258
259 Error ImageTextureLayered::_create_from_images(const TypedArray<Image> &p_images) {
260 Vector<Ref<Image>> images;
261 for (int i = 0; i < p_images.size(); i++) {
262 Ref<Image> img = p_images[i];
263 ERR_FAIL_COND_V(img.is_null(), ERR_INVALID_PARAMETER);
264 images.push_back(img);
265 }
266
267 return create_from_images(images);
268 }
269
270 TypedArray<Image> ImageTextureLayered::_get_images() const {
271 TypedArray<Image> images;
272 for (int i = 0; i < layers; i++) {
273 images.push_back(get_layer_data(i));
274 }
275 return images;
276 }
277
278 void ImageTextureLayered::_set_images(const TypedArray<Image> &p_images) {
279 ERR_FAIL_COND(_create_from_images(p_images) != OK);
280 }
281
282 Error ImageTextureLayered::create_from_images(Vector<Ref<Image>> p_images) {
283 int new_layers = p_images.size();
284 ERR_FAIL_COND_V(new_layers == 0, ERR_INVALID_PARAMETER);
285 if (layered_type == LAYERED_TYPE_CUBEMAP) {
286 ERR_FAIL_COND_V_MSG(new_layers != 6, ERR_INVALID_PARAMETER,
287 "Cubemaps require exactly 6 layers");
288 } else if (layered_type == LAYERED_TYPE_CUBEMAP_ARRAY) {
289 ERR_FAIL_COND_V_MSG((new_layers % 6) != 0, ERR_INVALID_PARAMETER,
290 "Cubemap array layers must be a multiple of 6");
291 }
292
293 ERR_FAIL_COND_V(p_images[0].is_null() || p_images[0]->is_empty(), ERR_INVALID_PARAMETER);
294
295 Image::Format new_format = p_images[0]->get_format();
296 int new_width = p_images[0]->get_width();
297 int new_height = p_images[0]->get_height();
298 bool new_mipmaps = p_images[0]->has_mipmaps();
299
300 for (int i = 1; i < p_images.size(); i++) {
301 ERR_FAIL_COND_V_MSG(p_images[i]->get_format() != new_format, ERR_INVALID_PARAMETER,
302 "All images must share the same format");
303 ERR_FAIL_COND_V_MSG(p_images[i]->get_width() != new_width || p_images[i]->get_height() != new_height, ERR_INVALID_PARAMETER,
304 "All images must share the same dimensions");
305 ERR_FAIL_COND_V_MSG(p_images[i]->has_mipmaps() != new_mipmaps, ERR_INVALID_PARAMETER,
306 "All images must share the usage of mipmaps");
307 }
308
309 if (texture.is_valid()) {
310 RID new_texture = RS::get_singleton()->texture_2d_layered_create(p_images, RS::TextureLayeredType(layered_type));
311 ERR_FAIL_COND_V(!new_texture.is_valid(), ERR_CANT_CREATE);
312 RS::get_singleton()->texture_replace(texture, new_texture);
313 } else {
314 texture = RS::get_singleton()->texture_2d_layered_create(p_images, RS::TextureLayeredType(layered_type));
315 ERR_FAIL_COND_V(!texture.is_valid(), ERR_CANT_CREATE);
316 }
317
318 format = new_format;
319 width = new_width;
320 height = new_height;
321 layers = new_layers;
322 mipmaps = new_mipmaps;
323 return OK;
324 }
325
326 void ImageTextureLayered::update_layer(const Ref<Image> &p_image, int p_layer) {
327 ERR_FAIL_COND_MSG(texture.is_null(), "Texture is not initialized.");
328 ERR_FAIL_COND_MSG(p_image.is_null(), "Invalid image.");
329 ERR_FAIL_COND_MSG(p_image->get_format() != format, "Image format must match texture's image format.");
330 ERR_FAIL_COND_MSG(p_image->get_width() != width || p_image->get_height() != height, "Image size must match texture's image size.");
331 ERR_FAIL_COND_MSG(p_image->has_mipmaps() != mipmaps, "Image mipmap configuration must match texture's image mipmap configuration.");
332 ERR_FAIL_INDEX_MSG(p_layer, layers, "Layer index is out of bounds.");
333 RS::get_singleton()->texture_2d_update(texture, p_image, p_layer);
334 }
335
336 Ref<Image> ImageTextureLayered::get_layer_data(int p_layer) const {
337 ERR_FAIL_INDEX_V(p_layer, layers, Ref<Image>());
338 return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
339 }
340
341 RID ImageTextureLayered::get_rid() const {
342 if (texture.is_null()) {
343 texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
344 }
345 return texture;
346 }
347
348 void ImageTextureLayered::set_path(const String &p_path, bool p_take_over) {
349 if (texture.is_valid()) {
350 RS::get_singleton()->texture_set_path(texture, p_path);
351 }
352
353 Resource::set_path(p_path, p_take_over);
354 }
355
356 void ImageTextureLayered::_bind_methods() {
357 ClassDB::bind_method(D_METHOD("create_from_images", "images"), &ImageTextureLayered::_create_from_images);
358 ClassDB::bind_method(D_METHOD("update_layer", "image", "layer"), &ImageTextureLayered::update_layer);
359
360 ClassDB::bind_method(D_METHOD("_get_images"), &ImageTextureLayered::_get_images);
361 ClassDB::bind_method(D_METHOD("_set_images", "images"), &ImageTextureLayered::_set_images);
362
363 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_images", PROPERTY_HINT_ARRAY_TYPE, "Image", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT), "_set_images", "_get_images");
364 }
365
366 ImageTextureLayered::ImageTextureLayered(LayeredType p_layered_type) {
367 layered_type = p_layered_type;
368 }
369
370 ImageTextureLayered::~ImageTextureLayered() {
371 if (texture.is_valid()) {
372 ERR_FAIL_NULL(RenderingServer::get_singleton());
373 RS::get_singleton()->free_rid(texture);
374 }
375 }
376
377 Image::Format ImageTexture3D::get_format() const {
378 return format;
379 }
380 int ImageTexture3D::get_width() const {
381 return width;
382 }
383 int ImageTexture3D::get_height() const {
384 return height;
385 }
386 int ImageTexture3D::get_depth() const {
387 return depth;
388 }
389 bool ImageTexture3D::has_mipmaps() const {
390 return mipmaps;
391 }
392
393 Error ImageTexture3D::_create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const TypedArray<Image> &p_data) {
394 Vector<Ref<Image>> images;
395 images.resize(p_data.size());
396 for (int i = 0; i < images.size(); i++) {
397 images.write[i] = p_data[i];
398 }
399 return create(p_format, p_width, p_height, p_depth, p_mipmaps, images);
400 }
401
402 void ImageTexture3D::_update(const TypedArray<Image> &p_data) {
403 Vector<Ref<Image>> images;
404 images.resize(p_data.size());
405 for (int i = 0; i < images.size(); i++) {
406 images.write[i] = p_data[i];
407 }
408 return update(images);
409 }
410
411 Error ImageTexture3D::create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
412 RID tex = RenderingServer::get_singleton()->texture_3d_create(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
413 ERR_FAIL_COND_V(tex.is_null(), ERR_CANT_CREATE);
414
415 if (texture.is_valid()) {
416 RenderingServer::get_singleton()->texture_replace(texture, tex);
417 } else {
418 texture = tex;
419 }
420
421 format = p_format;
422 width = p_width;
423 height = p_height;
424 depth = p_depth;
425 mipmaps = p_mipmaps;
426
427 return OK;
428 }
429
430 void ImageTexture3D::update(const Vector<Ref<Image>> &p_data) {
431 ERR_FAIL_COND(!texture.is_valid());
432 RenderingServer::get_singleton()->texture_3d_update(texture, p_data);
433 }
434
435 Vector<Ref<Image>> ImageTexture3D::get_data() const {
436 ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());
437 return RS::get_singleton()->texture_3d_get(texture);
438 }
439
440 RID ImageTexture3D::get_rid() const {
441 if (!texture.is_valid()) {
442 texture = RS::get_singleton()->texture_3d_placeholder_create();
443 }
444 return texture;
445 }
446 void ImageTexture3D::set_path(const String &p_path, bool p_take_over) {
447 if (texture.is_valid()) {
448 RenderingServer::get_singleton()->texture_set_path(texture, p_path);
449 }
450
451 Resource::set_path(p_path, p_take_over);
452 }
453
454 TypedArray<Image> ImageTexture3D::_get_images() const {
455 TypedArray<Image> images;
456 if (texture.is_valid()) {
457 Vector<Ref<Image>> raw_images = get_data();
458 ERR_FAIL_COND_V(raw_images.is_empty(), TypedArray<Image>());
459
460 for (int i = 0; i < raw_images.size(); i++) {
461 images.push_back(raw_images[i]);
462 }
463 }
464 return images;
465 }
466
467 void ImageTexture3D::_set_images(const TypedArray<Image> &p_images) {
468 if (p_images.size() == 0) {
469 if (images_stored) {
470 ERR_PRINT("Invalid images");
471 }
472 return;
473 }
474
475 Ref<Image> img_base = p_images[0];
476 ERR_FAIL_COND(img_base.is_null());
477
478 Image::Format new_format = img_base->get_format();
479 int new_width = img_base->get_width();
480 int new_height = img_base->get_height();
481 int new_depth = 0;
482 bool new_mipmaps = false;
483
484 for (int i = 1; i < p_images.size(); i++) {
485 Ref<Image> img = p_images[i];
486 ERR_FAIL_COND(img.is_null());
487 ERR_FAIL_COND_MSG(img->get_format() != new_format, "All images must share the same format.");
488
489 if (img->get_width() != new_width || img->get_height() != new_height) {
490 new_mipmaps = true;
491 if (new_depth == 0) {
492 new_depth = i;
493 }
494 }
495 }
496
497 if (new_depth == 0) {
498 new_depth = p_images.size();
499 }
500
501 Error err = _create(new_format, new_width, new_height, new_depth, new_mipmaps, p_images);
502 ERR_FAIL_COND(err != OK);
503
504 images_stored = true;
505 }
506
507 void ImageTexture3D::_bind_methods() {
508 ClassDB::bind_method(D_METHOD("create", "format", "width", "height", "depth", "use_mipmaps", "data"), &ImageTexture3D::_create);
509 ClassDB::bind_method(D_METHOD("update", "data"), &ImageTexture3D::_update);
510 ClassDB::bind_method(D_METHOD("_get_images"), &ImageTexture3D::_get_images);
511 ClassDB::bind_method(D_METHOD("_set_images", "images"), &ImageTexture3D::_set_images);
512 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_images", PROPERTY_HINT_ARRAY_TYPE, "Image", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT), "_set_images", "_get_images");
513 }
514
515 ImageTexture3D::ImageTexture3D() {
516 }
517
518 ImageTexture3D::~ImageTexture3D() {
519 if (texture.is_valid()) {
520 ERR_FAIL_NULL(RenderingServer::get_singleton());
521 RS::get_singleton()->free_rid(texture);
522 }
523 }
524
525 void Texture2DArray::_bind_methods() {
526 ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2DArray::create_placeholder);
527 }
528
529 Ref<Resource> Texture2DArray::create_placeholder() const {
530 Ref<PlaceholderTexture2DArray> placeholder;
531 placeholder.instantiate();
532 placeholder->set_size(Size2i(get_width(), get_height()));
533 placeholder->set_layers(get_layers());
534 return placeholder;
535 }
536
537 void Cubemap::_bind_methods() {
538 ClassDB::bind_method(D_METHOD("create_placeholder"), &Cubemap::create_placeholder);
539 }
540
541 Ref<Resource> Cubemap::create_placeholder() const {
542 Ref<PlaceholderCubemap> placeholder;
543 placeholder.instantiate();
544 placeholder->set_size(Size2i(get_width(), get_height()));
545 placeholder->set_layers(get_layers());
546 return placeholder;
547 }
548
549 void CubemapArray::_bind_methods() {
550 ClassDB::bind_method(D_METHOD("create_placeholder"), &CubemapArray::create_placeholder);
551 }
552
553 Ref<Resource> CubemapArray::create_placeholder() const {
554 Ref<PlaceholderCubemapArray> placeholder;
555 placeholder.instantiate();
556 placeholder->set_size(Size2i(get_width(), get_height()));
557 placeholder->set_layers(get_layers());
558 return placeholder;
559 }