@@ -334,16 +334,7 @@ void TextureStorage::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS:
334334
335335/* Texture API */
336336
337- Ref<Image> TextureStorage::_get_gl_image_and_format (const Ref<Image> &p_image, Image::Format p_format, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const {
338- Config *config = Config::get_singleton ();
339- r_gl_format = 0 ;
340- Ref<Image> image = p_image;
341- r_compressed = false ;
342- r_real_format = p_format;
343-
344- bool need_decompress = false ;
345- bool decompress_ra_to_rg = false ;
346-
337+ static inline Error _get_gl_uncompressed_format (Image::Format p_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type) {
347338 switch (p_format) {
348339 case Image::FORMAT_L8: {
349340 if (RasterizerGLES3::is_gles_over_gl ()) {
@@ -371,55 +362,51 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
371362 r_gl_internal_format = GL_R8;
372363 r_gl_format = GL_RED;
373364 r_gl_type = GL_UNSIGNED_BYTE;
374-
375365 } break ;
376366 case Image::FORMAT_RG8: {
377367 r_gl_internal_format = GL_RG8;
378368 r_gl_format = GL_RG;
379369 r_gl_type = GL_UNSIGNED_BYTE;
380-
381370 } break ;
382371 case Image::FORMAT_RGB8: {
383372 r_gl_internal_format = GL_RGB8;
384373 r_gl_format = GL_RGB;
385374 r_gl_type = GL_UNSIGNED_BYTE;
386-
387375 } break ;
388376 case Image::FORMAT_RGBA8: {
389- r_gl_format = GL_RGBA;
390377 r_gl_internal_format = GL_RGBA8;
378+ r_gl_format = GL_RGBA;
391379 r_gl_type = GL_UNSIGNED_BYTE;
392-
393380 } break ;
394381 case Image::FORMAT_RGBA4444: {
395382 r_gl_internal_format = GL_RGBA4;
396383 r_gl_format = GL_RGBA;
397384 r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
398-
385+ } break ;
386+ case Image::FORMAT_RGB565: {
387+ r_gl_internal_format = GL_RGB565;
388+ r_gl_format = GL_RGB;
389+ r_gl_type = GL_UNSIGNED_SHORT_5_6_5;
399390 } break ;
400391 case Image::FORMAT_RF: {
401392 r_gl_internal_format = GL_R32F;
402393 r_gl_format = GL_RED;
403394 r_gl_type = GL_FLOAT;
404-
405395 } break ;
406396 case Image::FORMAT_RGF: {
407397 r_gl_internal_format = GL_RG32F;
408398 r_gl_format = GL_RG;
409399 r_gl_type = GL_FLOAT;
410-
411400 } break ;
412401 case Image::FORMAT_RGBF: {
413402 r_gl_internal_format = GL_RGB32F;
414403 r_gl_format = GL_RGB;
415404 r_gl_type = GL_FLOAT;
416-
417405 } break ;
418406 case Image::FORMAT_RGBAF: {
419407 r_gl_internal_format = GL_RGBA32F;
420408 r_gl_format = GL_RGBA;
421409 r_gl_type = GL_FLOAT;
422-
423410 } break ;
424411 case Image::FORMAT_RH: {
425412 r_gl_internal_format = GL_R16F;
@@ -430,26 +417,48 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
430417 r_gl_internal_format = GL_RG16F;
431418 r_gl_format = GL_RG;
432419 r_gl_type = GL_HALF_FLOAT;
433-
434420 } break ;
435421 case Image::FORMAT_RGBH: {
436422 r_gl_internal_format = GL_RGB16F;
437423 r_gl_format = GL_RGB;
438424 r_gl_type = GL_HALF_FLOAT;
439-
440425 } break ;
441426 case Image::FORMAT_RGBAH: {
442427 r_gl_internal_format = GL_RGBA16F;
443428 r_gl_format = GL_RGBA;
444429 r_gl_type = GL_HALF_FLOAT;
445-
446430 } break ;
447431 case Image::FORMAT_RGBE9995: {
448432 r_gl_internal_format = GL_RGB9_E5;
449433 r_gl_format = GL_RGB;
450434 r_gl_type = GL_UNSIGNED_INT_5_9_9_9_REV;
451-
452435 } break ;
436+ default : {
437+ return ERR_UNAVAILABLE;
438+ }
439+ }
440+
441+ return OK;
442+ }
443+
444+ Ref<Image> TextureStorage::_get_gl_image_and_format (const Ref<Image> &p_image, Image::Format p_format, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const {
445+ Config *config = Config::get_singleton ();
446+ r_gl_format = 0 ;
447+ Ref<Image> image = p_image;
448+ r_compressed = false ;
449+ r_real_format = p_format;
450+
451+ if (!Image::is_format_compressed (p_format)) {
452+ Error err = _get_gl_uncompressed_format (p_format, r_gl_format, r_gl_internal_format, r_gl_type);
453+ ERR_FAIL_COND_V_MSG (err != OK, Ref<Image>(), vformat (" The image format %d is not supported by the Compatibility renderer." , p_format));
454+ return p_image;
455+ }
456+
457+ // For compressed images, some formats may not be supported by the current device and will require decompression.
458+ bool need_decompress = false ;
459+ bool decompress_ra_to_rg = false ;
460+
461+ switch (p_format) {
453462 case Image::FORMAT_DXT1: {
454463 if (config->s3tc_supported ) {
455464 r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
@@ -536,7 +545,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
536545 r_gl_format = GL_RED;
537546 r_gl_type = GL_UNSIGNED_BYTE;
538547 r_compressed = true ;
539-
540548 } else {
541549 need_decompress = true ;
542550 }
@@ -547,7 +555,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
547555 r_gl_format = GL_RED;
548556 r_gl_type = GL_UNSIGNED_BYTE;
549557 r_compressed = true ;
550-
551558 } else {
552559 need_decompress = true ;
553560 }
@@ -558,7 +565,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
558565 r_gl_format = GL_RG;
559566 r_gl_type = GL_UNSIGNED_BYTE;
560567 r_compressed = true ;
561-
562568 } else {
563569 need_decompress = true ;
564570 }
@@ -569,7 +575,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
569575 r_gl_format = GL_RG;
570576 r_gl_type = GL_UNSIGNED_BYTE;
571577 r_compressed = true ;
572-
573578 } else {
574579 need_decompress = true ;
575580 }
@@ -581,7 +586,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
581586 r_gl_format = GL_RGB;
582587 r_gl_type = GL_UNSIGNED_BYTE;
583588 r_compressed = true ;
584-
585589 } else {
586590 need_decompress = true ;
587591 }
@@ -592,7 +596,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
592596 r_gl_format = GL_RGBA;
593597 r_gl_type = GL_UNSIGNED_BYTE;
594598 r_compressed = true ;
595-
596599 } else {
597600 need_decompress = true ;
598601 }
@@ -603,7 +606,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
603606 r_gl_format = GL_RGBA;
604607 r_gl_type = GL_UNSIGNED_BYTE;
605608 r_compressed = true ;
606-
607609 } else {
608610 need_decompress = true ;
609611 }
@@ -642,7 +644,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
642644 r_gl_format = GL_RGBA;
643645 r_gl_type = GL_UNSIGNED_BYTE;
644646 r_compressed = true ;
645-
646647 } else {
647648 need_decompress = true ;
648649 }
@@ -653,7 +654,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
653654 r_gl_format = GL_RGBA;
654655 r_gl_type = GL_UNSIGNED_BYTE;
655656 r_compressed = true ;
656-
657657 } else {
658658 need_decompress = true ;
659659 }
@@ -664,7 +664,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
664664 r_gl_format = GL_RGBA;
665665 r_gl_type = GL_UNSIGNED_BYTE;
666666 r_compressed = true ;
667-
668667 } else {
669668 need_decompress = true ;
670669 }
@@ -675,7 +674,6 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
675674 r_gl_format = GL_RGBA;
676675 r_gl_type = GL_UNSIGNED_BYTE;
677676 r_compressed = true ;
678-
679677 } else {
680678 need_decompress = true ;
681679 }
@@ -690,41 +688,17 @@ Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, I
690688 image = image->duplicate ();
691689 image->decompress ();
692690 ERR_FAIL_COND_V (image->is_compressed (), image);
691+
693692 if (decompress_ra_to_rg) {
694693 image->convert_ra_rgba8_to_rg ();
695694 image->convert (Image::FORMAT_RG8);
696695 }
697- switch (image->get_format ()) {
698- case Image::FORMAT_RG8: {
699- r_gl_format = GL_RG;
700- r_gl_internal_format = GL_RG8;
701- r_gl_type = GL_UNSIGNED_BYTE;
702- r_real_format = Image::FORMAT_RG8;
703- r_compressed = false ;
704- } break ;
705- case Image::FORMAT_RGB8: {
706- r_gl_format = GL_RGB;
707- r_gl_internal_format = GL_RGB;
708- r_gl_type = GL_UNSIGNED_BYTE;
709- r_real_format = Image::FORMAT_RGB8;
710- r_compressed = false ;
711- } break ;
712- case Image::FORMAT_RGBA8: {
713- r_gl_format = GL_RGBA;
714- r_gl_internal_format = GL_RGBA;
715- r_gl_type = GL_UNSIGNED_BYTE;
716- r_real_format = Image::FORMAT_RGBA8;
717- r_compressed = false ;
718- } break ;
719- default : {
720- image->convert (Image::FORMAT_RGBA8);
721- r_gl_format = GL_RGBA;
722- r_gl_internal_format = GL_RGBA;
723- r_gl_type = GL_UNSIGNED_BYTE;
724- r_real_format = Image::FORMAT_RGBA8;
725- r_compressed = false ;
726- } break ;
727- }
696+
697+ Error err = _get_gl_uncompressed_format (image->get_format (), r_gl_format, r_gl_internal_format, r_gl_type);
698+ ERR_FAIL_COND_V_MSG (err != OK, Ref<Image>(), vformat (" The image format %d is not supported by the Compatibility renderer." , image->get_format ()));
699+
700+ r_real_format = image->get_format ();
701+ r_compressed = false ;
728702 }
729703
730704 return image;
0 commit comments