@@ -30,6 +30,7 @@ THE SOFTWARE.
3030
3131#include  < string> 
3232#include  < ctype.h> 
33+ #include  < algorithm> 
3334
3435#include  " axmol/tlx/utility.hpp" 
3536#include  " axmol/base/Config.h" //  AX_USE_JPEG, AX_USE_WEBP
@@ -186,38 +187,51 @@ enum class PVR2TexturePixelFormat : uint8_t
186187//  v3
187188enum  class  PVR3TexturePixelFormat  : uint64_t 
188189{
189-     PVRTC2BPP_RGB     = 0ULL ,
190-     PVRTC2BPP_RGBA    = 1ULL ,
191-     PVRTC4BPP_RGB     = 2ULL ,
192-     PVRTC4BPP_RGBA    = 3ULL ,
193-     PVRTC2_2BPP_RGBA  = 4ULL ,
194-     PVRTC2_4BPP_RGBA  = 5ULL ,
195-     ETC1              = 6ULL ,
196-     DXT1              = 7ULL ,
197-     DXT2              = 8ULL ,
198-     DXT3              = 9ULL ,
199-     DXT4              = 10ULL ,
200-     DXT5              = 11ULL ,
201-     BC1               = 7ULL ,
202-     BC2               = 9ULL ,
203-     BC3               = 11ULL ,
204-     BC4               = 12ULL ,
205-     BC5               = 13ULL ,
206-     BC6               = 14ULL ,
207-     BC7               = 15ULL ,
208-     UYVY              = 16ULL ,
209-     YUY2              = 17ULL ,
210-     BW1bpp            = 18ULL ,
211-     R9G9B9E5          = 19ULL ,
212-     RGBG8888          = 20ULL ,
213-     GRGB8888          = 21ULL ,
214-     ETC2_RGB          = 22ULL ,
215-     ETC2_RGBA         = 23ULL ,
216-     ETC2_RGBA1        = 24ULL ,
217-     EAC_R11_Unsigned  = 25ULL ,
218-     EAC_R11_Signed    = 26ULL ,
219-     EAC_RG11_Unsigned = 27ULL ,
220-     EAC_RG11_Signed   = 28ULL ,
190+     PVRTC2BPP_RGB    = 0ULL ,
191+     PVRTC2BPP_RGBA   = 1ULL ,
192+     PVRTC4BPP_RGB    = 2ULL ,
193+     PVRTC4BPP_RGBA   = 3ULL ,
194+     PVRTC2_2BPP_RGBA = 4ULL ,
195+     PVRTC2_4BPP_RGBA = 5ULL ,
196+     ETC1             = 6ULL ,
197+     DXT1             = 7ULL ,
198+     DXT2             = 8ULL ,
199+     DXT3             = 9ULL ,
200+     DXT4             = 10ULL ,
201+     DXT5             = 11ULL ,
202+     BC1              = 7ULL ,
203+     BC2              = 9ULL ,
204+     BC3              = 11ULL ,
205+     BC4              = 12ULL ,
206+     BC5              = 13ULL ,
207+     BC6              = 14ULL ,
208+     BC7              = 15ULL ,
209+     UYVY             = 16ULL ,
210+     YUY2             = 17ULL ,
211+     BW1bpp           = 18ULL ,
212+     R9G9B9E5         = 19ULL ,
213+     RGBG8888         = 20ULL ,
214+     GRGB8888         = 21ULL ,
215+     ETC2_RGB         = 22ULL ,
216+     ETC2_RGBA        = 23ULL ,
217+     ETC2_RGBA1       = 24ULL ,
218+     EAC_R11          = 25ULL ,
219+     EAC_RG11         = 26ULL ,
220+ 
221+     ASTC_4x4   = 27ULL ,
222+     ASTC_5x4   = 28ULL ,
223+     ASTC_5x5   = 29ULL ,
224+     ASTC_6x5   = 30ULL ,
225+     ASTC_6x6   = 31ULL ,
226+     ASTC_8x5   = 32ULL ,
227+     ASTC_8x6   = 33ULL ,
228+     ASTC_8x8   = 34ULL ,
229+     ASTC_10x5  = 35ULL ,
230+     ASTC_10x6  = 36ULL ,
231+     ASTC_10x8  = 37ULL ,
232+     ASTC_10x10 = 38ULL ,
233+     ASTC_12x10 = 39ULL ,
234+     ASTC_12x12 = 40ULL ,
221235
222236    BGRA8888 = 0x0808080861726762ULL ,
223237    RGBA8888 = 0x0808080861626772ULL ,
@@ -273,6 +287,21 @@ static _pixel3_formathash::value_type v3_pixel_formathash_value[] = {
273287    _pixel3_formathash::value_type (PVR3TexturePixelFormat::ETC1, rhi::PixelFormat::ETC1),
274288    _pixel3_formathash::value_type (PVR3TexturePixelFormat::ETC2_RGB, rhi::PixelFormat::ETC2_RGB),
275289    _pixel3_formathash::value_type (PVR3TexturePixelFormat::ETC2_RGBA, rhi::PixelFormat::ETC2_RGBA),
290+ 
291+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_4x4, rhi::PixelFormat::ASTC4x4),
292+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_5x4, rhi::PixelFormat::ASTC5x4),
293+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_5x5, rhi::PixelFormat::ASTC5x5),
294+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_6x5, rhi::PixelFormat::ASTC6x5),
295+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_6x6, rhi::PixelFormat::ASTC6x6),
296+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_8x5, rhi::PixelFormat::ASTC8x5),
297+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_8x6, rhi::PixelFormat::ASTC8x6),
298+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_8x8, rhi::PixelFormat::ASTC8x8),
299+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_10x5, rhi::PixelFormat::ASTC10x5),
300+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_10x6, rhi::PixelFormat::ASTC10x6),
301+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_10x8, rhi::PixelFormat::ASTC10x8),
302+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_10x10, rhi::PixelFormat::ASTC10x10),
303+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_12x10, rhi::PixelFormat::ASTC12x10),
304+     _pixel3_formathash::value_type (PVR3TexturePixelFormat::ASTC_12x12, rhi::PixelFormat::ASTC12x12),
276305};
277306
278307static  const  int  PVR3_MAX_TABLE_ELEMENTS = sizeof (v3_pixel_formathash_value) / sizeof (v3_pixel_formathash_value[0 ]);
@@ -480,6 +509,23 @@ static rhi::PixelFormat getDevicePVRPixelFormat(rhi::PixelFormat format)
480509            return  rhi::PixelFormat::ETC2_RGB;
481510        else 
482511            return  rhi::PixelFormat::RGBA8;
512+ 
513+     case  rhi::PixelFormat::ASTC4x4:
514+     case  rhi::PixelFormat::ASTC5x4:
515+     case  rhi::PixelFormat::ASTC5x5:
516+     case  rhi::PixelFormat::ASTC6x5:
517+     case  rhi::PixelFormat::ASTC6x6:
518+     case  rhi::PixelFormat::ASTC8x5:
519+     case  rhi::PixelFormat::ASTC8x6:
520+     case  rhi::PixelFormat::ASTC8x8:
521+     case  rhi::PixelFormat::ASTC10x5:
522+     case  rhi::PixelFormat::ASTC10x6:
523+     case  rhi::PixelFormat::ASTC10x8:
524+     case  rhi::PixelFormat::ASTC10x10:
525+     case  rhi::PixelFormat::ASTC12x10:
526+     case  rhi::PixelFormat::ASTC12x12:
527+         return  Configuration::getInstance ()->supportsASTC () ? format : rhi::PixelFormat::RGBA8;
528+ 
483529    default :
484530        return  format;
485531    }
@@ -522,6 +568,22 @@ bool testFormatForPvr3TCSupport(PVR3TexturePixelFormat format)
522568    case  PVR3TexturePixelFormat::LA88:
523569        return  true ;
524570
571+     case  PVR3TexturePixelFormat::ASTC_4x4:
572+     case  PVR3TexturePixelFormat::ASTC_5x4:
573+     case  PVR3TexturePixelFormat::ASTC_5x5:
574+     case  PVR3TexturePixelFormat::ASTC_6x5:
575+     case  PVR3TexturePixelFormat::ASTC_6x6:
576+     case  PVR3TexturePixelFormat::ASTC_8x5:
577+     case  PVR3TexturePixelFormat::ASTC_8x6:
578+     case  PVR3TexturePixelFormat::ASTC_8x8:
579+     case  PVR3TexturePixelFormat::ASTC_10x5:
580+     case  PVR3TexturePixelFormat::ASTC_10x6:
581+     case  PVR3TexturePixelFormat::ASTC_10x8:
582+     case  PVR3TexturePixelFormat::ASTC_10x10:
583+     case  PVR3TexturePixelFormat::ASTC_12x10:
584+     case  PVR3TexturePixelFormat::ASTC_12x12:
585+         return  Configuration::getInstance ()->supportsASTC ();
586+ 
525587    default :
526588        return  false ;
527589    }
@@ -797,7 +859,7 @@ bool Image::isEtc1(const uint8_t* data, ssize_t /*dataLen*/)
797859    return  !!etc1_pkm_is_valid ((etc1_byte*)data);
798860}
799861
800- bool  Image::isEtc2 (const  uint8_t * data, ssize_t  dataLen)
862+ bool  Image::isEtc2 (const  uint8_t * data, ssize_t  /* dataLen*/ 
801863{
802864    return  !!etc2_pkm_is_valid ((etc2_byte*)data);
803865}
@@ -846,7 +908,7 @@ bool Image::isWebp(const uint8_t* data, ssize_t dataLen)
846908
847909bool  Image::isPvr (const  uint8_t * data, ssize_t  dataLen)
848910{
849-     if  (static_cast <size_t >(dataLen) < sizeof (PVRv2TexHeader) ||  static_cast < size_t >(dataLen) <  sizeof (PVRv3TexHeader))
911+     if  (static_cast <size_t >(dataLen) < std::max ( sizeof (PVRv2TexHeader),  sizeof (PVRv3TexHeader) ))
850912    {
851913        return  false ;
852914    }
@@ -1599,6 +1661,7 @@ bool Image::initWithPVRv2Data(uint8_t* data, ssize_t dataLen, bool ownData)
15991661                AXLOGD (" Image. BGRA8888 not supported on this device" 
16001662                return  false ;
16011663            }
1664+             [[fallthrough]];
16021665        default :
16031666            blockSize    = 1 ;
16041667            widthBlocks  = width;
@@ -1787,6 +1850,130 @@ bool Image::initWithPVRv3Data(uint8_t* data, ssize_t dataLen, bool ownData)
17871850                AXLOGW (" Image. BGRA8888 not supported on this device" 
17881851                return  false ;
17891852            }
1853+             blockSize    = 1 ;
1854+             widthBlocks  = width;
1855+             heightBlocks = height;
1856+             break ;
1857+ 
1858+         case  PVR3TexturePixelFormat::ASTC_4x4:
1859+         case  PVR3TexturePixelFormat::ASTC_5x4:
1860+         case  PVR3TexturePixelFormat::ASTC_5x5:
1861+         case  PVR3TexturePixelFormat::ASTC_6x5:
1862+         case  PVR3TexturePixelFormat::ASTC_6x6:
1863+         case  PVR3TexturePixelFormat::ASTC_8x5:
1864+         case  PVR3TexturePixelFormat::ASTC_8x6:
1865+         case  PVR3TexturePixelFormat::ASTC_8x8:
1866+         case  PVR3TexturePixelFormat::ASTC_10x5:
1867+         case  PVR3TexturePixelFormat::ASTC_10x6:
1868+         case  PVR3TexturePixelFormat::ASTC_10x8:
1869+         case  PVR3TexturePixelFormat::ASTC_10x10:
1870+         case  PVR3TexturePixelFormat::ASTC_12x10:
1871+         case  PVR3TexturePixelFormat::ASTC_12x12:
1872+         {
1873+ 
1874+             if  (!(_hasPremultipliedAlpha) && !(flags & (unsigned )PVR3TextureFlag::PremultipliedAlpha))
1875+             {
1876+                 _hasPremultipliedAlpha = isCompressedImageHavePMA (CompressedImagePMAFlag::ASTC);
1877+             }
1878+ 
1879+             int  bx = 4 , by = 4 ;
1880+             switch  ((PVR3TexturePixelFormat)pixelFormat)
1881+             {
1882+             case  PVR3TexturePixelFormat::ASTC_4x4:
1883+                 bx = 4 ;
1884+                 by = 4 ;
1885+                 break ;
1886+             case  PVR3TexturePixelFormat::ASTC_5x4:
1887+                 bx = 5 ;
1888+                 by = 4 ;
1889+                 break ;
1890+             case  PVR3TexturePixelFormat::ASTC_5x5:
1891+                 bx = 5 ;
1892+                 by = 5 ;
1893+                 break ;
1894+             case  PVR3TexturePixelFormat::ASTC_6x5:
1895+                 bx = 6 ;
1896+                 by = 5 ;
1897+                 break ;
1898+             case  PVR3TexturePixelFormat::ASTC_6x6:
1899+                 bx = 6 ;
1900+                 by = 6 ;
1901+                 break ;
1902+             case  PVR3TexturePixelFormat::ASTC_8x5:
1903+                 bx = 8 ;
1904+                 by = 5 ;
1905+                 break ;
1906+             case  PVR3TexturePixelFormat::ASTC_8x6:
1907+                 bx = 8 ;
1908+                 by = 6 ;
1909+                 break ;
1910+             case  PVR3TexturePixelFormat::ASTC_8x8:
1911+                 bx = 8 ;
1912+                 by = 8 ;
1913+                 break ;
1914+             case  PVR3TexturePixelFormat::ASTC_10x5:
1915+                 bx = 10 ;
1916+                 by = 5 ;
1917+                 break ;
1918+             case  PVR3TexturePixelFormat::ASTC_10x6:
1919+                 bx = 10 ;
1920+                 by = 6 ;
1921+                 break ;
1922+             case  PVR3TexturePixelFormat::ASTC_10x8:
1923+                 bx = 10 ;
1924+                 by = 8 ;
1925+                 break ;
1926+             case  PVR3TexturePixelFormat::ASTC_10x10:
1927+                 bx = 10 ;
1928+                 by = 10 ;
1929+                 break ;
1930+             case  PVR3TexturePixelFormat::ASTC_12x10:
1931+                 bx = 12 ;
1932+                 by = 10 ;
1933+                 break ;
1934+             case  PVR3TexturePixelFormat::ASTC_12x12:
1935+                 bx = 12 ;
1936+                 by = 12 ;
1937+                 break ;
1938+             default :
1939+                 break ;
1940+             }
1941+ 
1942+             const  bool  hw =
1943+                 Configuration::getInstance ()->supportsASTC () &&
1944+                 (v3_pixel_formathash.find ((PVR3TexturePixelFormat)pixelFormat) != v3_pixel_formathash.end ());
1945+ 
1946+             int  wBlocks = (width + bx - 1 ) / bx;
1947+             int  hBlocks = (height + by - 1 ) / by;
1948+             dataSize    = wBlocks * hBlocks * 16 ;
1949+ 
1950+             if  (hw)
1951+             {
1952+ 
1953+                 _mipmaps[i].data      = pixelData + dataOffset;
1954+                 _mipmaps[i].dataSize  = dataSize;
1955+             }
1956+             else 
1957+             {
1958+ 
1959+                 _unpack              = true ;
1960+                 _mipmaps[i].dataSize  = width * height * 4 ;
1961+                 _mipmaps[i].data      = (uint8_t *)malloc (_mipmaps[i].dataSize );
1962+                 if  (AX_UNLIKELY (astc_decompress_image (pixelData + dataOffset, dataSize,
1963+                                                       static_cast <uint8_t *>(_mipmaps[i].data ), width, height, bx,
1964+                                                       by) != 0 ))
1965+                     return  false ;
1966+                 _pixelFormat = rhi::PixelFormat::RGBA8;
1967+                 dataSize     = ((width + bx - 1 ) / bx) * ((height + by - 1 ) / by) * 16 ;
1968+             }
1969+ 
1970+             dataOffset += dataSize;
1971+             AXASSERT (dataOffset <= pixelLen, " Image: Invalid length" 
1972+             width  = MAX (width >> 1 , 1 );
1973+             height = MAX (height >> 1 , 1 );
1974+             continue ;
1975+         }
1976+ 
17901977        default :
17911978            blockSize    = 1 ;
17921979            widthBlocks  = width;
@@ -1993,10 +2180,18 @@ bool Image::initWithASTCData(uint8_t* data, ssize_t dataLen, bool ownData)
19932180            {
19942181                _pixelFormat = rhi::PixelFormat::ASTC4x4;
19952182            }
2183+             else  if  (block_x == 5  && block_y == 4 )
2184+             {
2185+                 _pixelFormat = rhi::PixelFormat::ASTC5x4;
2186+             }
19962187            else  if  (block_x == 5  && block_y == 5 )
19972188            {
19982189                _pixelFormat = rhi::PixelFormat::ASTC5x5;
19992190            }
2191+             else  if  (block_x == 6  && block_y == 5 )
2192+             {
2193+                 _pixelFormat = rhi::PixelFormat::ASTC6x5;
2194+             }
20002195            else  if  (block_x == 6  && block_y == 6 )
20012196            {
20022197                _pixelFormat = rhi::PixelFormat::ASTC6x6;
@@ -2017,6 +2212,26 @@ bool Image::initWithASTCData(uint8_t* data, ssize_t dataLen, bool ownData)
20172212            {
20182213                _pixelFormat = rhi::PixelFormat::ASTC10x5;
20192214            }
2215+             else  if  (block_x == 10  && block_y == 6 )
2216+             {
2217+                 _pixelFormat = rhi::PixelFormat::ASTC10x6;
2218+             }
2219+             else  if  (block_x == 10  && block_y == 8 )
2220+             {
2221+                 _pixelFormat = rhi::PixelFormat::ASTC10x8;
2222+             }
2223+             else  if  (block_x == 10  && block_y == 10 )
2224+             {
2225+                 _pixelFormat = rhi::PixelFormat::ASTC10x10;
2226+             }
2227+             else  if  (block_x == 12  && block_y == 10 )
2228+             {
2229+                 _pixelFormat = rhi::PixelFormat::ASTC12x10;
2230+             }
2231+             else  if  (block_x == 12  && block_y == 12 )
2232+             {
2233+                 _pixelFormat = rhi::PixelFormat::ASTC12x12;
2234+             }
20202235
20212236            forwardPixels (data, dataLen, ASTC_HEAD_SIZE, ownData);
20222237        }
@@ -2619,7 +2834,7 @@ void Image::premultiplyAlpha()
26192834{
26202835#if  AX_ENABLE_PREMULTIPLIED_ALPHA
26212836    AXASSERT ((_pixelFormat == rhi::PixelFormat::RGBA8) || (_pixelFormat == rhi::PixelFormat::RG8),
2622-              " The pixel format should be RGBA8888 or RG88 ." 
2837+              " The pixel format should be RGBA8888 or RG8 ." 
26232838
26242839    if  (_pixelFormat == rhi::PixelFormat::RGBA8)
26252840    {
0 commit comments