Merge branch 'master' into single-file-transcoder

This commit is contained in:
Carl Woffenden
2020-03-20 19:40:30 +01:00
58 changed files with 20970 additions and 6900 deletions
+41 -31
View File
@@ -19,33 +19,35 @@ else()
message("Building 32-bit")
endif(BUILD_X64)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
if (NOT MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS -std=c++11)
set(GCC_COMPILE_FLAGS "-fvisibility=hidden -fvisibility-inlines-hidden -fPIC -fno-strict-aliasing -D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64 -Wall -Wextra -Wno-unused-local-typedefs -Wno-unused-value -Wno-unused-parameter -Wno-unused-variable -Wno-reorder")
set(CMAKE_CXX_FLAGS -std=c++11)
set(GCC_COMPILE_FLAGS "-fvisibility=hidden -fPIC -fno-strict-aliasing -D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64 -Wall -Wextra -Wno-unused-local-typedefs -Wno-unused-value -Wno-unused-parameter -Wno-unused-variable")
if (NOT BUILD_X64)
set(GCC_COMPILE_FLAGS "${GCC_COMPILE_FLAGS} -m32")
if (NOT BUILD_X64)
set(GCC_COMPILE_FLAGS "${GCC_COMPILE_FLAGS} -m32")
endif()
if (STATIC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS} -static-libgcc -static-libstdc++ -static")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS} -Wl,-rpath .")
endif(STATIC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COMPILE_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GCC_COMPILE_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${GCC_COMPILE_FLAGS} -D_DEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GCC_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${GCC_COMPILE_FLAGS} -D_DEBUG")
endif()
if (STATIC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS} -static-libgcc -static-libstdc++ -static")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS} -Wl,-rpath .")
endif(STATIC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COMPILE_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${GCC_COMPILE_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${GCC_COMPILE_FLAGS} -D_DEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${GCC_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${GCC_COMPILE_FLAGS} -D_DEBUG")
set(BASISU_SRC_LIST ${COMMON_SRC_LIST}
basisu_backend.cpp
basisu_basis_file.cpp
@@ -61,28 +63,36 @@ set(BASISU_SRC_LIST ${COMMON_SRC_LIST}
basisu_ssim.cpp
basisu_tool.cpp
basisu_astc_decomp.cpp
basisu_uastc_enc.cpp
basisu_bc7enc.cpp
lodepng.cpp
apg_bmp.c
transcoder/basisu_transcoder.cpp
)
if (APPLE)
set(BIN_DIRECTORY "bin_osx")
set(BIN_DIRECTORY "bin_osx")
else()
set(BIN_DIRECTORY "bin")
set(BIN_DIRECTORY "bin")
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${BIN_DIRECTORY})
add_executable(basisu ${BASISU_SRC_LIST})
target_link_libraries(basisu m pthread)
if (NOT MSVC)
target_link_libraries(basisu m pthread)
endif()
install(TARGETS basisu DESTINATION bin)
if (CMAKE_BUILD_TYPE STREQUAL Release)
if (APPLE)
add_custom_command(TARGET basisu POST_BUILD COMMAND strip -X -x ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/basisu)
else()
add_custom_command(TARGET basisu POST_BUILD COMMAND strip -g -X -x ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/basisu)
endif()
if (NOT MSVC)
if (CMAKE_BUILD_TYPE STREQUAL Release)
if (APPLE)
add_custom_command(TARGET basisu POST_BUILD COMMAND strip -X -x ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/basisu)
else()
add_custom_command(TARGET basisu POST_BUILD COMMAND strip -g -X -x ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/basisu)
endif()
endif()
endif()
+21
View File
@@ -0,0 +1,21 @@
{
"configurations": [
{
"inheritEnvironments": [
"msvc_x64"
],
"name": "x64-Release",
"includePath": [
"${env.INCLUDE}",
"${workspaceRoot}\\**"
],
"defines": [
"WIN32",
"NDEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "windows-msvc-x64"
}
]
}
+33 -13
View File
@@ -1,29 +1,31 @@
# basis_universal
Basis Universal GPU Texture and Texture Video Compression Codec
Basis Universal Supercompressed GPU Texture Codec
Basis Universal is a ["supercompressed"](http://gamma.cs.unc.edu/GST/gst.pdf) compressed GPU texture and [compressed texture video](http://gamma.cs.unc.edu/MPTC/) compression system that outputs a highly compressed intermediate file format (.basis) that can be quickly transcoded to a [very wide variety](https://github.com/BinomialLLC/basis_universal/wiki/OpenGL-texture-format-enums-table) of GPU compressed and uncompressed pixel formats: ASTC 4x4 L/LA/RGB/RGBA, PVRTC1 4bpp RGB/RGBA, PVRTC2 RGB/RGBA, BC7 mode 6 RGB, BC7 mode 5 RGB/RGBA, BC1-5 RGB/RGBA/X/XY, ETC1 RGB, ETC2 RGBA, ATC RGB/RGBA, ETC2 EAC R11 and RG11, FXT1 RGB, and uncompressed raster image formats 8888/565/4444.
Basis Universal is a ["supercompressed"](http://gamma.cs.unc.edu/GST/gst.pdf) GPU texture compression system that outputs a highly compressed intermediate file format (.basis) that can be quickly transcoded to a [very wide variety](https://github.com/BinomialLLC/basis_universal/wiki/OpenGL-texture-format-enums-table) of GPU compressed and uncompressed pixel formats: ASTC 4x4 L/LA/RGB/RGBA, PVRTC1 4bpp RGB/RGBA, PVRTC2 RGB/RGBA, BC7 mode 6 RGB, BC7 mode 5 RGB/RGBA, BC1-5 RGB/RGBA/X/XY, ETC1 RGB, ETC2 RGBA, ATC RGB/RGBA, ETC2 EAC R11 and RG11, FXT1 RGB, and uncompressed raster image formats 8888/565/4444.
The system now supports two modes: a high quality mode which is internally based off the [UASTC compressed texture format](https://richg42.blogspot.com/2020/01/uastc-block-format-encoding.html), and the original lower quality mode which is based off a subset of ETC1 called "ETC1S". UASTC is for extremely high quality (similar to BC7 quality) textures, and ETC1S is for very small files. The ETC1S system includes built-in data compression, while the UASTC system includes an optional Rate Distortion Optimization (RDO) post-process stage that conditions the encoded UASTC texture data in the .basis file so it can be more effectively LZ compressed by the end user. More technical details about UASTC integration are [here](https://github.com/BinomialLLC/basis_universal/wiki/UASTC-implementation-details).
Basis files support non-uniform texture arrays, so cubemaps, volume textures, texture arrays, mipmap levels, video sequences, or arbitrary texture "tiles" can be stored in a single file. The compressor is able to exploit color and pattern correlations across the entire file, so multiple images with mipmaps can be stored very efficiently in a single file.
The system's bitrate depends on the quality setting and image content, but common usable bitrates are .3-1.25 bits/texel. .basis files are typically 10-25% smaller than using RDO texture compression of the internal texture data stored in the .basis file followed by LZMA. The current system is what we've been calling the "baseline" system, which is designed to reach all the GPU formats. The next major step is to extend the system to allow for much higher quality for the ASTC and BC7 texture formats.
The system's bitrate depends on the quality setting and image content, but common usable ETC1S bitrates are .3-1.25 bits/texel. ETC1S .basis files are typically 10-25% smaller than using RDO texture compression of the internal texture data stored in the .basis file followed by LZMA. For UASTC files, the bitrate is fixed at 8bpp, but with RDO post-processing and user-provided LZ compression on the .basis file the effective bitrate can be as low as 2bpp for video or for individual textures approximately 4-6bpp.
The transcoder has been fuzz tested using [zzuf](https://www.linux.com/news/fuzz-testing-zzuf).
So far, we've compiled the code using MSVS 2019, under Ubuntu x64 using cmake with either clang 3.8 or gcc 5.4, and emscripten 1.35 to asm.js. (Be sure to use this version or later of emcc, as earlier versions fail with internal errors/exceptions during compilation.) The compressor is multithreaded by default, but this can be disabled using the -no_multithreading command line option. The transcoder is currently single threaded.
A simple asm.js Texture Video demo is [here](http://binomial.biz/TextureVideoTest/).
Basis Universal supports "skip blocks" in ETC1S compressed texture arrays, which makes it useful for basic [compressed texture video](http://gamma.cs.unc.edu/MPTC/) applications. Note that Basis Universal is still at heart a GPU texture compression system, not a video codec, so bitrates will be larger than even MPEG1.
### Important Usage Notes
**The "-q X" option controls the output quality.** The default is quality level 128. "-q 255" will increase quality quite a bit. If you want even higher quality, try "-max_selectors 16128 -max_endpoints 16128" instead of -q. -q internally tries to set the codebook sizes (or the # of quantization intervals for endpoints/selectors) for you. You need to experiment with the quality level on your content.
**The "-q X" option controls the output quality in ETC1S mode.** The default is quality level 128. "-q 255" will increase quality quite a bit. If you want even higher quality, try "-max_selectors 16128 -max_endpoints 16128" instead of -q. -q internally tries to set the codebook sizes (or the # of quantization intervals for endpoints/selectors) for you. You need to experiment with the quality level on your content.
For tangent space normal maps, you should separate X into RGB and Y into Alpha, and provide the compressor with 32-bit/pixel input images. Or use the "-separate_rg_to_color_alpha" command line option which does this for you. The internal texture format that Basis Universal uses (ETC1S) doesn't handle tangent space normal maps encoded into RGB well. You need to separate the channels and recover Z in the pixel shader using z=sqrt(1-x^2-y^2).
### 3rd party code dependencies
The transcoder (in the "transcoder" directory) is a single .cpp source file library which has no 3rd party code dependencies.
The stand-alone transcoder (in the "transcoder" directory) is a single .cpp source file library which has no 3rd party code dependencies.
The encoder uses [lodepng](https://lodev.org/lodepng/) for loading and saving PNG images, which is Copyright (c) 2005-2019 Lode Vandevenne. It uses the zlib license.
The encoder uses [lodepng](https://lodev.org/lodepng/) for loading and saving PNG images, which is Copyright (c) 2005-2019 Lode Vandevenne. It uses the zlib license. It also uses [apg_bmp](https://github.com/capnramses/apg/tree/master/apg_bmp) for loading BMP images, which is Copyright 2019 Anton Gerdelan. It uses the Apache 2.0 license.
The encoder uses [tcuAstcUtil.cpp](https://chromium.googlesource.com/external/deqp/+/refs/heads/master/framework/common/tcuAstcUtil.cpp), from the [Android drawElements Quality Program (deqp) Testing Suite](https://source.android.com/devices/graphics/deqp-testing), for unpacking the transcoder's ASTC output for testing/validation purposes. This code is Copyright 2016 The Android Open Source Project, and uses the Apache 2.0 license. We have modified the code so it has no external dependencies, and disabled HDR support.
@@ -49,6 +51,10 @@ Also see [Intel® Open Source HD Graphics Programmers' Reference Manual (PRM)](h
### Release notes
3/14/20 release notes:
- UASTC support is in. We have removed BC7 mode 6 support from the ETC1S transcoder to reduce its size, although the format enum still works (it aliases to BC7 mode 5). We are still updating the docs for UASTC.
- Adding fuzz-safe BMP support using apg_bmp. We will be adding fuzz-safe JPEG and TGA next.
9/26/19 release notes:
- Automatic global selector palettes are disabled by default, because searching the virtual selector codebook is very slow.
You can enable them by specifying -auto_global_sel_pal on the command line, for slightly smaller files on small textures/images.
@@ -93,11 +99,25 @@ cmake CMakeLists.txt
make
```
To compress a sRGB image to .basis:
For Visual Studio 2019, you can now either use the CMakeLists.txt file or the included `basisu.sln` file.
To compress a sRGB image to an ETC1S .basis file:
`basisu x.png`
Note that basisu defaults to sRGB colorspace metrics. If the input is a normal map, or some other type of non-sRGB (non-photographic) texture content, be sure to use -linear to avoid extra unnecessary artifacts.
To compress a image to a higher quality UASTC .basis file:
`basisu -uastc -uastc_level 2 x.png`
To compress a image to a higher quality UASTC .basis file with RDO post processing, so the .basis file is more compressible:
`basisu -uastc -uastc_level 2 -uast_rdo_q .75 x.png`
-uastc_level X ranges from 0-4 and controls the UASTC encode'sr performance vs. quality tradeoff. Level 0 is very fast, but low quality, level 2 is the default quality, while level 3 is the highest practical quality. Level 4 is impractically slow, but highest quality.
-uastc_rdo_q X controls the rate distortion stage's quality setting. The lower this value, the higher the quality, but the larger the compressed file size. Good values to try are between .2-3.0. The default is 1.0. RDO post-processing is currently pretty slow, but we'll be optimizing it over time.
Note that basisu defaults to sRGB colorspace metrics. If the input is a normal map, or some other type of non-sRGB (non-photographic) texture content, be sure to use -linear to avoid extra unnecessary artifacts. (Note: Currently, UASTC mode always uses linear colorspace metrics. sRGB and angulate metrics are comming soon.)
To add automatically generated mipmaps to the .basis file, at a higher than default quality level (which ranges from [1,255]):
@@ -105,7 +125,7 @@ To add automatically generated mipmaps to the .basis file, at a higher than defa
There are several mipmap options that allow you to change the filter kernel, the filter colorspace for the RGB channels (linear vs. sRGB), the smallest mipmap dimension, etc. The tool also supports generating cubemap files, 2D/cubemap texture arrays, etc.
To create a slightly higher quality .basis file (one with better codebooks) at the default quality level (128) - note this is much slower to encode:
To create a slightly higher quality ETC1S .basis file (one with better codebooks) at the default quality level (128) - note this is much slower to encode:
`basisu -comp_level 2 x.png`
@@ -145,7 +165,7 @@ If you are doing rate distortion comparisons vs. other similar systems, be sure
For video, level 1 should result in decent results on most clips. For less banding, level 2 can make a big difference. This is still an active area of development, and quality/encoding perf. will improve over time.
### Compression levels (advanced option)
### ETC1S Compression levels (advanced option)
The encoder supports multiple compression "effort" levels using the "-comp_level X" command line option, where X ranges from [0,5]. Note that most users shouldn't be messing around with -comp_level. The -q option is the main option to control the quality level of .basis files. -comp_level is mostly intended for harder to handle content such as texture video. It modifies a number of internal encoder configuration parameters, which slows it down a bunch but allows it to achieve slightly higher quality per output bit.
@@ -249,10 +269,10 @@ To get development error messages printed to stdout when something goes wrong in
These transcoder macros control which formats are supported by the transcoder at compile-time:
- BASISD_SUPPORT_UASTC
- BASISD_SUPPORT_DXT1 (BC1)
- BASISD_SUPPORT_DXT5A (BC3/4/5)
- BASISD_SUPPORT_BC7
- BASISD_SUPPORT_BC7_MODE6_OPAQUE_ONLY
- BASISD_SUPPORT_BC7_MODE5
- BASISD_SUPPORT_PVRTC1
- BASISD_SUPPORT_ETC2_EAC_A8
@@ -271,7 +291,7 @@ The ATC format (which achieves nearly the same quality as BC1/BC3) is for Adreno
### Quick Basis file details
Internally, Basis files are composed of a non-uniform texture array of one or more 2D ETC1S texture "slices". ETC1S is a simple subset of the ETC1 texture format popular on Android. ETC1S has no block flips, no 4x2 or 2x4 subblocks, and each block only uses 555 base colors. ETC1S is still 100% standard ETC1, so transcoding to ETC1 or the color block of ETC2 is a no-op. We chose ETC1S because it has the very valuable property that it can be quickly transcoded to almost any other GPU texture format at very high quality using only simple per-block operations with small 1D lookup tables. Transcoding ETC1S to BC1 usually only introduces around .3 dB Y PSNR quality loss, with less loss for ETC1S->BC7. Transcoding to PVRTC1 involves only simple block level operations to compute the endpoints, and simple per-pixel scalar operations to compute the modulation values.
Internally, Basis files are composed of a non-uniform texture array of one or more 2D ETC1S or UASTC texture "slices". ETC1S is a simple subset of the ETC1 texture format popular on Android. ETC1S has no block flips, no 4x2 or 2x4 subblocks, and each block only uses 555 base colors. ETC1S is still 100% standard ETC1, so transcoding to ETC1 or the color block of ETC2 is a no-op. We chose ETC1S because it has the very valuable property that it can be quickly transcoded to almost any other GPU texture format at very high quality using only simple per-block operations with small 1D lookup tables. Transcoding ETC1S to BC1 usually only introduces around .3 dB Y PSNR quality loss, with less loss for ETC1S->BC7. Transcoding to PVRTC1 involves only simple block level operations to compute the endpoints, and simple per-pixel scalar operations to compute the modulation values.
Basis files have a single set of compressed global endpoint/selector codebooks in ETC1S format, which all slices utilize. The ETC1S texture data is compressed using vector quantization (VQ) separately on the endpoints and selectors, followed by DPCM/RLE/psuedo-MTF/canonical Huffman coding. Each ETC1S texture slice may be a different resolution. Mipmaps (if any) are always stored in order from largest to smallest level. The file format supports either storing the selector codebook directly (using DPCM+Huffman), or storing the selector codebook using a hierarchical virtual codebook scheme.
+541
View File
@@ -0,0 +1,541 @@
/*
BMP File Reader/Writer Implementation
Anton Gerdelan
Version: 3
Licence: see apg_bmp.h
C99
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include "apg_bmp.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Maximum pixel dimensions of width or height of an image. Should accommodate max used in graphics APIs.
NOTE: 65536*65536 is the biggest number storable in 32 bits.
This needs to be multiplied by n_channels so actual memory indices are not uint32 but size_t to avoid overflow.
Note this will crash stb_image_write et al at maximum size which use 32bits, so reduce max size to accom. */
#define _BMP_MAX_DIMS 65536
#define _BMP_FILE_HDR_SZ 14
#define _BMP_MIN_DIB_HDR_SZ 40
#define _BMP_MIN_HDR_SZ ( _BMP_FILE_HDR_SZ + _BMP_MIN_DIB_HDR_SZ )
#define _BMP_MAX_IMAGE_FILE_SIZE (1024ULL*1024ULL*1024ULL)
#pragma pack( push, 1 ) // supported on GCC in addition to individual packing attribs
/* All BMP files, regardless of type, start with this file header */
typedef struct _bmp_file_header_t {
char file_type[2];
uint32_t file_sz;
uint16_t reserved1;
uint16_t reserved2;
uint32_t image_data_offset;
} _bmp_file_header_t;
/* Following the file header is the BMP type header. this is the most commonly used format */
typedef struct _bmp_dib_BITMAPINFOHEADER_t {
uint32_t this_header_sz;
int32_t w; // in older headers w & h these are shorts and may be unsigned
int32_t h; //
uint16_t n_planes; // must be 1
uint16_t bpp; // bits per pixel. 1,4,8,16,24,32.
uint32_t compression_method; // 16 and 32-bit images must have a value of 3 here
uint32_t image_uncompressed_sz; // not consistently used in the wild, so ignored here.
int32_t horiz_pixels_per_meter; // not used.
int32_t vert_pixels_per_meter; // not used.
uint32_t n_colours_in_palette; //
uint32_t n_important_colours; // not used.
/* NOTE(Anton) a DIB header may end here at 40-bytes. be careful using sizeof() */
/* if 'compression' value, above, is set to 3 ie the image is 16 or 32-bit, then these colour channel masks follow the headers.
these are big-endian order bit masks to assign bits of each pixel to different colours. bits used must be contiguous and not overlap. */
uint32_t bitmask_r;
uint32_t bitmask_g;
uint32_t bitmask_b;
} _bmp_dib_BITMAPINFOHEADER_t;
#pragma pack( pop )
typedef enum _bmp_compression_t {
BI_RGB = 0,
BI_RLE8 = 1,
BI_RLE4 = 2,
BI_BITFIELDS = 3,
BI_JPEG = 4,
BI_PNG = 5,
BI_ALPHABITFIELDS = 6,
BI_CMYK = 11,
BI_CMYKRLE8 = 12,
BI_CMYRLE4 = 13
} _bmp_compression_t;
/* convenience struct and file->memory function */
typedef struct _entire_file_t {
void* data;
size_t sz;
} _entire_file_t;
/*
RETURNS
- true on success. record->data is allocated memory and must be freed by the caller.
- false on any error. Any allocated memory is freed if false is returned */
static bool _read_entire_file( const char* filename, _entire_file_t* record ) {
FILE* fp = fopen( filename, "rb" );
if ( !fp ) { return false; }
fseek( fp, 0L, SEEK_END );
record->sz = (size_t)ftell( fp );
// Immediately bail on anything larger than _BMP_MAX_IMAGE_FILE_SIZE.
if (record->sz > _BMP_MAX_IMAGE_FILE_SIZE) {
fclose( fp );
return false;
}
record->data = malloc( record->sz );
if ( !record->data ) {
fclose( fp );
return false;
}
rewind( fp );
size_t nr = fread( record->data, record->sz, 1, fp );
fclose( fp );
if ( 1 != nr ) { return false; }
return true;
}
static bool _validate_file_hdr( _bmp_file_header_t* file_hdr_ptr, size_t file_sz ) {
if ( !file_hdr_ptr ) { return false; }
if ( file_hdr_ptr->file_type[0] != 'B' || file_hdr_ptr->file_type[1] != 'M' ) { return false; }
if ( file_hdr_ptr->image_data_offset > file_sz ) { return false; }
return true;
}
static bool _validate_dib_hdr( _bmp_dib_BITMAPINFOHEADER_t* dib_hdr_ptr, size_t file_sz ) {
if ( !dib_hdr_ptr ) { return false; }
if ( _BMP_FILE_HDR_SZ + dib_hdr_ptr->this_header_sz > file_sz ) { return false; }
if ( ( 32 == dib_hdr_ptr->bpp || 16 == dib_hdr_ptr->bpp ) && ( BI_BITFIELDS != dib_hdr_ptr->compression_method && BI_ALPHABITFIELDS != dib_hdr_ptr->compression_method ) ) {
return false;
}
if ( BI_RGB != dib_hdr_ptr->compression_method && BI_BITFIELDS != dib_hdr_ptr->compression_method && BI_ALPHABITFIELDS != dib_hdr_ptr->compression_method ) {
return false;
}
// NOTE(Anton) using abs() in the if-statement was blowing up on large negative numbers. switched to labs()
if ( 0 == dib_hdr_ptr->w || 0 == dib_hdr_ptr->h || labs( dib_hdr_ptr->w ) > _BMP_MAX_DIMS || labs( dib_hdr_ptr->h ) > _BMP_MAX_DIMS ) { return false; }
/* NOTE(Anton) if images reliably used n_colours_in_palette we could have done a palette/file size integrity check here.
because some always set 0 then we have to check every palette indexing as we read them */
return true;
}
/* NOTE(Anton) this could have ifdef branches on different compilers for the intrinsics versions for perf */
static uint32_t _bitscan( uint32_t dword ) {
for ( uint32_t i = 0; i < 32; i++ ) {
if ( 1 & dword ) { return i; }
dword = dword >> 1;
}
return (uint32_t)-1;
}
unsigned char* apg_bmp_read( const char* filename, int* w, int* h, unsigned int* n_chans ) {
if ( !filename || !w || !h || !n_chans ) { return NULL; }
// read in the whole file into memory first - much faster than parsing on-the-fly
_entire_file_t record;
if ( !_read_entire_file( filename, &record ) ) { return NULL; }
if ( record.sz < _BMP_MIN_HDR_SZ ) {
free( record.data );
return NULL;
}
// grab and validate the first, file, header
_bmp_file_header_t* file_hdr_ptr = (_bmp_file_header_t*)record.data;
if ( !_validate_file_hdr( file_hdr_ptr, record.sz ) ) {
free( record.data );
return NULL;
}
// grad and validate the second, DIB, header
_bmp_dib_BITMAPINFOHEADER_t* dib_hdr_ptr = (_bmp_dib_BITMAPINFOHEADER_t*)( (uint8_t*)record.data + _BMP_FILE_HDR_SZ );
if ( !_validate_dib_hdr( dib_hdr_ptr, record.sz ) ) {
free( record.data );
return NULL;
}
// bitmaps can have negative dims to indicate the image should be flipped
uint32_t width = *w = abs( dib_hdr_ptr->w );
uint32_t height = *h = abs( dib_hdr_ptr->h );
// TODO(Anton) flip image memory at the end if this is true. because doing it per row was making me write bugs.
// bool vertically_flip = dib_hdr_ptr->h > 0 ? false : true;
// channel count and palette are not well defined in the header so we make a good guess here
uint32_t n_dst_chans = 3, n_src_chans = 3;
bool has_palette = false;
switch ( dib_hdr_ptr->bpp ) {
case 32: n_dst_chans = n_src_chans = 4; break; // technically can be RGB but not supported
case 24: n_dst_chans = n_src_chans = 3; break; // technically can be RGBA but not supported
case 8: // seems to always use a BGR0 palette, even for greyscale
n_dst_chans = 3;
has_palette = true;
n_src_chans = 1;
break;
case 4: // always has a palette - needed for a MS-saved BMP
n_dst_chans = 3;
has_palette = true;
n_src_chans = 1;
break;
case 1: // 1-bpp means the palette has 3 colour channels with 2 colours i.e. monochrome but not always black & white
n_dst_chans = 3;
has_palette = true;
n_src_chans = 1;
break;
default: // this includes 2bpp and 16bpp
free( record.data );
return NULL;
} // endswitch
*n_chans = n_dst_chans;
// NOTE(Anton) some image formats are not allowed a palette - could check for a bad header spec here also
if ( dib_hdr_ptr->n_colours_in_palette > 0 ) { has_palette = true; }
#ifdef APG_BMP_DEBUG_OUTPUT
printf( "apg_bmp_debug: reading image\n|-filename `%s`\n|-dims %ux%u pixels\n|-bpp %u\n|-n_src_chans %u\n|-n_dst_chans %u\n", filename, *w, *h,
dib_hdr_ptr->bpp, n_src_chans, n_dst_chans );
#endif
uint32_t palette_offset = _BMP_FILE_HDR_SZ + dib_hdr_ptr->this_header_sz;
bool has_bitmasks = false;
if ( BI_BITFIELDS == dib_hdr_ptr->compression_method || BI_ALPHABITFIELDS == dib_hdr_ptr->compression_method ) {
has_bitmasks = true;
palette_offset += 12;
}
if ( palette_offset > record.sz ) {
free( record.data );
return NULL;
}
// work out if any padding how much to skip at end of each row
uint32_t unpadded_row_sz = width * n_src_chans;
// bit-encoded palette indices have different padding properties
if ( 4 == dib_hdr_ptr->bpp ) {
unpadded_row_sz = width % 2 > 0 ? width / 2 + 1 : width / 2; // find how many whole bytes required for this bit width
}
if ( 1 == dib_hdr_ptr->bpp ) {
unpadded_row_sz = width % 8 > 0 ? width / 8 + 1 : width / 8; // find how many whole bytes required for this bit width
}
uint32_t row_padding_sz = 0 == unpadded_row_sz % 4 ? 0 : 4 - ( unpadded_row_sz % 4 ); // NOTE(Anton) didn't expect operator precedence of - over %
// another file size integrity check: partially validate source image data size
// 'image_data_offset' is by row padded to 4 bytes and is either colour data or palette indices.
if ( file_hdr_ptr->image_data_offset + ( unpadded_row_sz + row_padding_sz ) * height > record.sz ) {
free( record.data );
return NULL;
}
// find which bit number each colour channel starts at, so we can separate colours out
uint32_t bitshift_rgba[4] = {0, 0, 0, 0}; // NOTE(Anton) noticed this was int and not uint32_t so changed it. 17 Mar 2020
uint32_t bitmask_a = 0;
if ( has_bitmasks ) {
bitmask_a = ~( dib_hdr_ptr->bitmask_r | dib_hdr_ptr->bitmask_g | dib_hdr_ptr->bitmask_b );
bitshift_rgba[0] = _bitscan( dib_hdr_ptr->bitmask_r );
bitshift_rgba[1] = _bitscan( dib_hdr_ptr->bitmask_g );
bitshift_rgba[2] = _bitscan( dib_hdr_ptr->bitmask_b );
bitshift_rgba[3] = _bitscan( bitmask_a );
}
// allocate memory for the output pixels block. cast to size_t in case width and height are both the max of 65536 and n_dst_chans > 1
unsigned char* dst_img_ptr = malloc( (size_t)width * (size_t)height * (size_t)n_dst_chans );
if ( !dst_img_ptr ) {
free( record.data );
return NULL;
}
uint8_t* palette_data_ptr = (uint8_t*)record.data + palette_offset;
uint8_t* src_img_ptr = (uint8_t*)record.data + file_hdr_ptr->image_data_offset;
size_t dst_stride_sz = width * n_dst_chans;
// == 32-bpp -> 32-bit RGBA. == 32-bit and 16-bit require bitmasks
if ( 32 == dib_hdr_ptr->bpp ) {
// check source image has enough data in it to read from
if ( (size_t)file_hdr_ptr->image_data_offset + (size_t)height * (size_t)width * (size_t)n_src_chans > record.sz ) {
free( record.data );
free( dst_img_ptr );
return NULL;
}
size_t src_byte_idx = 0;
for ( uint32_t r = 0; r < height; r++ ) {
size_t dst_pixels_idx = r * dst_stride_sz;
for ( uint32_t c = 0; c < width; c++ ) {
uint32_t pixel;
memcpy( &pixel, &src_img_ptr[src_byte_idx], 4 );
// NOTE(Anton) the below assumes 32-bits is always RGBA 1 byte per channel. 10,10,10 RGB exists though and isn't handled.
dst_img_ptr[dst_pixels_idx++] = ( uint8_t )( ( pixel & dib_hdr_ptr->bitmask_r ) >> bitshift_rgba[0] );
dst_img_ptr[dst_pixels_idx++] = ( uint8_t )( ( pixel & dib_hdr_ptr->bitmask_g ) >> bitshift_rgba[1] );
dst_img_ptr[dst_pixels_idx++] = ( uint8_t )( ( pixel & dib_hdr_ptr->bitmask_b ) >> bitshift_rgba[2] );
dst_img_ptr[dst_pixels_idx++] = ( uint8_t )( ( pixel & bitmask_a ) >> bitshift_rgba[3] );
src_byte_idx += 4;
}
src_byte_idx += row_padding_sz;
}
// == 8-bpp -> 24-bit RGB ==
} else if ( 8 == dib_hdr_ptr->bpp && has_palette ) {
// validate indices (body of image data) fits in file
if ( file_hdr_ptr->image_data_offset + height * width > record.sz ) {
free( record.data );
free( dst_img_ptr );
return NULL;
}
size_t src_byte_idx = 0;
for ( uint32_t r = 0; r < height; r++ ) {
size_t dst_pixels_idx = ( height - 1 - r ) * dst_stride_sz;
for ( uint32_t c = 0; c < width; c++ ) {
// "most palettes are 4 bytes in RGB0 order but 3 for..." - it was actually BRG0 in old images -- Anton
uint8_t index = src_img_ptr[src_byte_idx]; // 8-bit index value per pixel
if ( palette_offset + index * 4 + 2 >= record.sz ) {
free( record.data );
return dst_img_ptr;
}
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[index * 4 + 2];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[index * 4 + 1];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[index * 4 + 0];
src_byte_idx++;
}
src_byte_idx += row_padding_sz;
}
// == 4-bpp (16-colour) -> 24-bit RGB ==
} else if ( 4 == dib_hdr_ptr->bpp && has_palette ) {
size_t src_byte_idx = 0;
for ( uint32_t r = 0; r < height; r++ ) {
size_t dst_pixels_idx = ( height - 1 - r ) * dst_stride_sz;
for ( uint32_t c = 0; c < width; c++ ) {
if ( file_hdr_ptr->image_data_offset + src_byte_idx > record.sz ) {
free( record.data );
free( dst_img_ptr );
return NULL;
}
// handle 2 pixels at a time
uint8_t pixel_duo = src_img_ptr[src_byte_idx];
uint8_t a_index = ( 0xFF & pixel_duo ) >> 4;
uint8_t b_index = 0xF & pixel_duo;
if ( palette_offset + a_index * 4 + 2 >= record.sz ) { // invalid src image
free( record.data );
return dst_img_ptr;
}
if ( dst_pixels_idx + 3 > width * height * n_dst_chans ) { // done
free( record.data );
return dst_img_ptr;
}
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[a_index * 4 + 2];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[a_index * 4 + 1];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[a_index * 4 + 0];
if ( ++c >= width ) { // advance a column
c = 0;
r++;
if ( r >= height ) { // done. no need to get second pixel. eg a 1x1 pixel image.
free( record.data );
return dst_img_ptr;
}
dst_pixels_idx = ( height - 1 - r ) * dst_stride_sz;
}
if ( palette_offset + b_index * 4 + 2 >= record.sz ) { // invalid src image
free( record.data );
return dst_img_ptr;
}
if ( dst_pixels_idx + 3 > width * height * n_dst_chans ) { // done. probably redundant check since checking r >= height.
free( record.data );
return dst_img_ptr;
}
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[b_index * 4 + 2];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[b_index * 4 + 1];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[b_index * 4 + 0];
src_byte_idx++;
}
src_byte_idx += row_padding_sz;
}
// == 1-bpp -> 24-bit RGB ==
} else if ( 1 == dib_hdr_ptr->bpp && has_palette ) {
/* encoding method for monochrome is not well documented.
a 2x2 pixel image is stored as 4 1-bit palette indexes
the palette is stored as any 2 RGB0 colours (not necessarily B&W)
so for an image with indexes like so:
1 1
0 1
it is bit-encoded as follows, starting at MSB:
01000000 00000000 00000000 00000000 (first byte val 64)
11000000 00000000 00000000 00000000 (first byte val 192)
data is still split by row and each row padded to 4 byte multiples
*/
size_t src_byte_idx = 0;
for ( uint32_t r = 0; r < height; r++ ) {
uint8_t bit_idx = 0; // used in monochrome
size_t dst_pixels_idx = ( height - 1 - r ) * dst_stride_sz;
for ( uint32_t c = 0; c < width; c++ ) {
if ( 8 == bit_idx ) { // start reading from the next byte
src_byte_idx++;
bit_idx = 0;
}
if ( file_hdr_ptr->image_data_offset + src_byte_idx > record.sz ) {
free( record.data );
return dst_img_ptr;
}
uint8_t pixel_oct = src_img_ptr[src_byte_idx];
uint8_t bit = 128 >> bit_idx;
uint8_t masked = pixel_oct & bit;
uint8_t palette_idx = masked > 0 ? 1 : 0;
if ( palette_offset + palette_idx * 4 + 2 >= record.sz ) {
free( record.data );
return dst_img_ptr;
}
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[palette_idx * 4 + 2];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[palette_idx * 4 + 1];
dst_img_ptr[dst_pixels_idx++] = palette_data_ptr[palette_idx * 4 + 0];
bit_idx++;
}
src_byte_idx += ( row_padding_sz + 1 ); // 1bpp is special here
}
// == 24-bpp -> 24-bit RGB == (but also should handle some other n_chans cases)
} else {
// NOTE(Anton) this only supports 1 byte per channel
if ( file_hdr_ptr->image_data_offset + height * width * n_dst_chans > record.sz ) {
free( record.data );
free( dst_img_ptr );
return NULL;
}
size_t src_byte_idx = 0;
for ( uint32_t r = 0; r < height; r++ ) {
size_t dst_pixels_idx = ( height - 1 - r ) * dst_stride_sz;
for ( uint32_t c = 0; c < width; c++ ) {
// re-orders from BGR to RGB
if ( n_dst_chans > 3 ) { dst_img_ptr[dst_pixels_idx++] = src_img_ptr[src_byte_idx + 3]; }
if ( n_dst_chans > 2 ) { dst_img_ptr[dst_pixels_idx++] = src_img_ptr[src_byte_idx + 2]; }
if ( n_dst_chans > 1 ) { dst_img_ptr[dst_pixels_idx++] = src_img_ptr[src_byte_idx + 1]; }
dst_img_ptr[dst_pixels_idx++] = src_img_ptr[src_byte_idx];
src_byte_idx += n_src_chans;
}
src_byte_idx += row_padding_sz;
}
} // endif bpp
free( record.data );
return dst_img_ptr;
}
void apg_bmp_free( unsigned char* pixels_ptr ) {
if ( !pixels_ptr ) { return; }
free( pixels_ptr );
}
unsigned int apg_bmp_write( const char* filename, unsigned char* pixels_ptr, int w, int h, unsigned int n_chans ) {
if ( !filename || !pixels_ptr ) { return 0; }
if ( 0 == w || 0 == h ) { return 0; }
if ( labs( w ) > _BMP_MAX_DIMS || labs( h ) > _BMP_MAX_DIMS ) { return 0; }
if ( n_chans != 3 && n_chans != 4 ) { return 0; }
uint32_t height = labs( h );
uint32_t width = labs( w );
// work out if any padding how much to skip at end of each row
const size_t unpadded_row_sz = width * n_chans;
const size_t row_padding_sz = 0 == unpadded_row_sz % 4 ? 0 : 4 - unpadded_row_sz % 4;
const size_t row_sz = unpadded_row_sz + row_padding_sz;
const size_t dst_pixels_padded_sz = row_sz * height;
const size_t dib_hdr_sz = sizeof( _bmp_dib_BITMAPINFOHEADER_t );
_bmp_file_header_t file_hdr;
{
file_hdr.file_type[0] = 'B';
file_hdr.file_type[1] = 'M';
file_hdr.file_sz = _BMP_FILE_HDR_SZ + (uint32_t)dib_hdr_sz + (uint32_t)dst_pixels_padded_sz;
file_hdr.reserved1 = 0;
file_hdr.reserved2 = 0;
file_hdr.image_data_offset = _BMP_FILE_HDR_SZ + (uint32_t)dib_hdr_sz;
}
_bmp_dib_BITMAPINFOHEADER_t dib_hdr;
{
dib_hdr.this_header_sz = _BMP_MIN_DIB_HDR_SZ; // NOTE: must be 40 and not include the bitmask memory in size here
dib_hdr.w = w;
dib_hdr.h = h;
dib_hdr.n_planes = 1;
dib_hdr.bpp = 3 == n_chans ? 24 : 32;
dib_hdr.compression_method = 3 == n_chans ? BI_RGB : BI_BITFIELDS;
dib_hdr.image_uncompressed_sz = 0;
dib_hdr.horiz_pixels_per_meter = 0;
dib_hdr.vert_pixels_per_meter = 0;
dib_hdr.n_colours_in_palette = 0;
dib_hdr.n_important_colours = 0;
// big-endian masks. only used in BI_BITFIELDS and BI_ALPHABITFIELDS ( 16 and 32-bit images )
// important note: GIMP stores BMP data in this array order for 32-bit: [A][B][G][R]
dib_hdr.bitmask_r = 0xFF000000;
dib_hdr.bitmask_g = 0x00FF0000;
dib_hdr.bitmask_b = 0x0000FF00;
}
uint8_t* dst_pixels_ptr = malloc( dst_pixels_padded_sz );
if ( !dst_pixels_ptr ) { return 0; }
{
size_t dst_byte_idx = 0;
uint8_t padding[4] = {0, 0, 0, 0};
uint8_t rgba[4] = {0, 0, 0, 0};
uint8_t bgra[4] = {0, 0, 0, 0};
for ( uint32_t row = 0; row < height; row++ ) {
size_t src_byte_idx = ( height - 1 - row ) * n_chans * width;
for ( uint32_t col = 0; col < width; col++ ) {
for ( uint32_t chan = 0; chan < n_chans; chan++ ) { rgba[chan] = pixels_ptr[src_byte_idx++]; }
if ( 3 == n_chans ) {
bgra[0] = rgba[2];
bgra[1] = rgba[1];
bgra[2] = rgba[0];
} else {
/* NOTE(Anton) RGBA with alpha channel would be better supported with an extended DIB header */
bgra[0] = rgba[3];
bgra[1] = rgba[2];
bgra[2] = rgba[1];
bgra[3] = rgba[0]; // alpha
}
memcpy( &dst_pixels_ptr[dst_byte_idx], bgra, n_chans );
dst_byte_idx += (size_t)n_chans;
} // endfor col
if ( row_padding_sz > 0 ) {
memcpy( &dst_pixels_ptr[dst_byte_idx], padding, row_padding_sz );
dst_byte_idx += row_padding_sz;
}
} // endfor row
}
{
FILE* fp = fopen( filename, "wb" );
if ( !fp ) {
free( dst_pixels_ptr );
return 0;
}
if ( 1 != fwrite( &file_hdr, _BMP_FILE_HDR_SZ, 1, fp ) ) {
free( dst_pixels_ptr );
fclose( fp );
return 0;
}
if ( 1 != fwrite( &dib_hdr, dib_hdr_sz, 1, fp ) ) {
free( dst_pixels_ptr );
fclose( fp );
return 0;
}
if ( 1 != fwrite( dst_pixels_ptr, dst_pixels_padded_sz, 1, fp ) ) {
free( dst_pixels_ptr );
fclose( fp );
return 0;
}
fclose( fp );
}
free( dst_pixels_ptr );
return 1;
}
+123
View File
@@ -0,0 +1,123 @@
/*
BMP File Reader/Writer Implementation
Anton Gerdelan
Version: 3.1 18 March 2020.
Licence: see bottom of file.
C89 ( Implementation is C99 )
Contributors:
- Anton Gerdelan - Initial code.
- Saija Sorsa - Fuzz testing.
Instructions:
- Just drop this header, and the matching .c file into your project.
- To get debug printouts during parsing define APG_BMP_DEBUG_OUTPUT.
Advantages:
- The implementation is fast, simple, and supports more formats than most BMP reader libraries.
- The reader function is fuzzed with AFL https://lcamtuf.coredump.cx/afl/.
- The reader is robust to large files and malformed files, and will return any valid partial data in an image.
- Reader supports 32bpp (with alpha channel), 24bpp, 8bpp, 4bpp, and 1bpp monochrome BMP images.
- Reader handles indexed BMP images using a colour palette.
- Writer supports 32bpp RGBA and 24bpp uncompressed RGB images.
Current Limitations:
- 16-bit images not supported (don't have any samples to test on).
- No support for interleaved channel bit layouts eg RGB101010 RGB555 RGB565.
- No support for compressed BMP images, although in practice these are not used.
- Output images with alpha channel are written in BITMAPINFOHEADER format.
For better alpha support in other apps the 124-bit v5 header could be used instead,
at the cost of some backward compatibility and bloat.
To Do:
- FUZZING
- create a unique fuzz test set for (8,4,1 BPP).
- (maybe) FEATURE Flipping the image based on negative width and height in header, and/or function arguments.
- (maybe) PERF ifdef intrinsics/asm for bitscan. Platform-specific code so won't include unless necessary.
- (maybe) FEATURE Add parameter for padding output memory to eg 4-byte alignment or n channels.
- (maybe) FEATURE Improved apps support in alpha channel writing (using v5 header).
*/
#ifndef APG_BMP_H_
#define APG_BMP_H_
#ifdef __cplusplus
extern "C" {
#endif /* CPP */
/* Reads a bitmap from a file, allocates memory for the raw image data, and returns it.
PARAMS
* w,h, - Retrieves the width and height of the BMP in pixels.
* n_chans - Retrieves the number of channels in the BMP.
RETURNS
* Tightly-packed pixel memory in RGBA order. The caller must call free() on the memory.
* NULL on any error. Any allocated memory is freed before returning NULL. */
unsigned char* apg_bmp_read( const char* filename, int* w, int* h, unsigned int* n_chans );
/* Calls free() on memory created by apg_bmp_read */
void apg_bmp_free( unsigned char* pixels_ptr );
/* Writes a bitmap to a file.
PARAMS
* filename - e.g."my_bitmap.bmp". Must not be NULL.
* pixels_ptr - Pointer to tightly-packed pixel memory in RGBA order. Must not be NULL. There must be abs(w)*abs(h)*n_chans bytes in the memory pointed to.
* w,h, - Width and height of the image in pixels.
* n_chans - The number of channels in the BMP. 3 or 4 supported for writing, which means RGB or RGBA memory, respectively.
RETURNS
* Zero on any error, non zero on success. */
unsigned int apg_bmp_write( const char* filename, unsigned char* pixels_ptr, int w, int h, unsigned int n_chans );
#ifdef __cplusplus
}
#endif /* CPP */
#endif /*_APG_BMP_H_ */
/*
-------------------------------------------------------------------------------------
This software is available under two licences - you may use it under either licence.
-------------------------------------------------------------------------------------
FIRST LICENCE OPTION
> Apache License
> Version 2.0, January 2004
> http://www.apache.org/licenses/
> Copyright 2019 Anton Gerdelan.
> Licensed under the Apache License, Version 2.0 (the "License");
> you may not use this file except in compliance with the License.
> You may obtain a copy of the License at
> http://www.apache.org/licenses/LICENSE-2.0
> Unless required by applicable law or agreed to in writing, software
> distributed under the License is distributed on an "AS IS" BASIS,
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> See the License for the specific language governing permissions and
> limitations under the License.
-------------------------------------------------------------------------------------
SECOND LICENCE OPTION
> This is free and unencumbered software released into the public domain.
>
> Anyone is free to copy, modify, publish, use, compile, sell, or
> distribute this software, either in source code form or as a compiled
> binary, for any purpose, commercial or non-commercial, and by any
> means.
>
> In jurisdictions that recognize copyright laws, the author or authors
> of this software dedicate any and all copyright interest in the
> software to the public domain. We make this dedication for the benefit
> of the public at large and to the detriment of our heirs and
> successors. We intend this dedication to be an overt act of
> relinquishment in perpetuity of all present and future rights to this
> software under copyright law.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
> OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
> ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
> OTHER DEALINGS IN THE SOFTWARE.
>
> For more information, please refer to <http://unlicense.org>
-------------------------------------------------------------------------------------
*/
+13 -5
View File
@@ -79,6 +79,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)\bin</OutDir>
<TargetName>$(ProjectName)D</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -98,9 +99,9 @@
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>ispc_texcomp</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -133,12 +134,13 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>ispc_texcomp</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BufferSecurityCheck>false</BufferSecurityCheck>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<OpenMPSupport>true</OpenMPSupport>
<ExceptionHandling>false</ExceptionHandling>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -147,9 +149,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="apg_bmp.c" />
<ClCompile Include="basisu_astc_decomp.cpp" />
<ClCompile Include="basisu_bc7enc.cpp" />
<ClCompile Include="basisu_ssim.cpp" />
<ClCompile Include="basisu_uastc_enc.cpp" />
<ClCompile Include="transcoder\basisu_transcoder.cpp" />
<ClInclude Include="apg_bmp.h" />
<ClInclude Include="basisu_bc7enc.h" />
<ClInclude Include="basisu_astc_decomp.h" />
<ClInclude Include="basisu_basis_file.h" />
<ClInclude Include="basisu_comp.h" />
@@ -165,6 +172,7 @@
<ClCompile Include="basisu_resampler.cpp" />
<ClCompile Include="basisu_resample_filters.cpp" />
<ClInclude Include="basisu_ssim.h" />
<ClInclude Include="basisu_uastc_enc.h" />
<ClInclude Include="lodepng.h" />
<ClInclude Include="transcoder\basisu.h" />
<ClInclude Include="transcoder\basisu_transcoder.h" />
@@ -172,6 +180,7 @@
<ClInclude Include="transcoder\basisu_file_headers.h" />
<ClInclude Include="transcoder\basisu_global_selector_cb.h" />
<ClInclude Include="transcoder\basisu_global_selector_palette.h" />
<ClInclude Include="transcoder\basisu_transcoder_uastc.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="basisu_basis_file.cpp" />
@@ -193,7 +202,6 @@
<None Include="transcoder\basisu_transcoder_tables_atc_56.inc" />
<None Include="transcoder\basisu_transcoder_tables_bc7_m5_alpha.inc" />
<None Include="transcoder\basisu_transcoder_tables_bc7_m5_color.inc" />
<None Include="transcoder\basisu_transcoder_tables_bc7_m6.inc" />
<None Include="transcoder\basisu_transcoder_tables_dxt1_5.inc" />
<None Include="transcoder\basisu_transcoder_tables_dxt1_6.inc" />
</ItemGroup>
+9 -3
View File
@@ -19,6 +19,9 @@
</ClCompile>
<ClCompile Include="basisu_ssim.cpp" />
<ClCompile Include="basisu_astc_decomp.cpp" />
<ClCompile Include="basisu_uastc_enc.cpp" />
<ClCompile Include="basisu_bc7enc.cpp" />
<ClCompile Include="apg_bmp.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="basisu_basis_file.h" />
@@ -49,6 +52,12 @@
</ClInclude>
<ClInclude Include="basisu_ssim.h" />
<ClInclude Include="basisu_astc_decomp.h" />
<ClInclude Include="basisu_uastc_enc.h" />
<ClInclude Include="basisu_bc7enc.h" />
<ClInclude Include="transcoder\basisu_transcoder_uastc.h">
<Filter>transcoder</Filter>
</ClInclude>
<ClInclude Include="apg_bmp.h" />
</ItemGroup>
<ItemGroup>
<None Include="transcoder\basisu_transcoder_tables_dxt1_6.inc">
@@ -57,9 +66,6 @@
<None Include="transcoder\basisu_transcoder_tables_dxt1_5.inc">
<Filter>transcoder</Filter>
</None>
<None Include="transcoder\basisu_transcoder_tables_bc7_m6.inc">
<Filter>transcoder</Filter>
</None>
<None Include="transcoder\basisu_transcoder_tables_astc.inc">
<Filter>transcoder</Filter>
</None>
+1 -1
View File
@@ -1256,7 +1256,7 @@ void interpolateWeights (TexelWeightPair* dst, const deUint32 (&unquantizedWeigh
const int numWeightsPerTexel = blockMode.isDualPlane ? 2 : 1;
const deUint32 scaleX = (1024 + blockWidth/2) / (blockWidth-1);
const deUint32 scaleY = (1024 + blockHeight/2) / (blockHeight-1);
DE_ASSERT(blockMode.weightGridWidth*blockMode.weightGridHeight*numWeightsPerTexel <= DE_LENGTH_OF_ARRAY(unquantizedWeights));
DE_ASSERT(blockMode.weightGridWidth*blockMode.weightGridHeight*numWeightsPerTexel <= (int)DE_LENGTH_OF_ARRAY(unquantizedWeights));
for (int texelY = 0; texelY < blockHeight; texelY++)
{
for (int texelX = 0; texelX < blockWidth; texelX++)
+21 -21
View File
@@ -1,5 +1,5 @@
// basisu_backend.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -176,7 +176,7 @@ namespace basisu
void basisu_backend::reoptimize_and_sort_endpoints_codebook(uint32_t total_block_endpoints_remapped, uint_vec& all_endpoint_indices)
{
basisu_frontend& r = *m_pFront_end;
const bool is_video = r.get_params().m_tex_type == basist::cBASISTexTypeVideoFrames;
//const bool is_video = r.get_params().m_tex_type == basist::cBASISTexTypeVideoFrames;
if ((total_block_endpoints_remapped) && (m_params.m_compression_level > 0))
{
@@ -201,10 +201,10 @@ namespace basisu
for (uint32_t slice_index = 0; slice_index < m_slices.size(); slice_index++)
{
const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
//const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
const uint32_t width = m_slices[slice_index].m_width;
const uint32_t height = m_slices[slice_index].m_height;
//const uint32_t width = m_slices[slice_index].m_width;
//const uint32_t height = m_slices[slice_index].m_height;
const uint32_t num_blocks_x = m_slices[slice_index].m_num_blocks_x;
const uint32_t num_blocks_y = m_slices[slice_index].m_num_blocks_y;
@@ -212,7 +212,7 @@ namespace basisu
{
for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
{
const uint32_t block_index = first_block_index + block_x + block_y * num_blocks_x;
//const uint32_t block_index = first_block_index + block_x + block_y * num_blocks_x;
encoder_block& m = m_slice_encoder_blocks[slice_index](block_x, block_y);
@@ -336,10 +336,10 @@ namespace basisu
for (uint32_t slice_index = 0; slice_index < m_slices.size(); slice_index++)
{
const bool is_iframe = m_slices[slice_index].m_iframe;
const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
//const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
const uint32_t width = m_slices[slice_index].m_width;
const uint32_t height = m_slices[slice_index].m_height;
//const uint32_t width = m_slices[slice_index].m_width;
//const uint32_t height = m_slices[slice_index].m_height;
const uint32_t num_blocks_x = m_slices[slice_index].m_num_blocks_x;
const uint32_t num_blocks_y = m_slices[slice_index].m_num_blocks_y;
const int prev_frame_slice_index = find_video_frame(slice_index, -1);
@@ -411,8 +411,8 @@ namespace basisu
const bool is_iframe = m_slices[slice_index].m_iframe;
const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
const uint32_t width = m_slices[slice_index].m_width;
const uint32_t height = m_slices[slice_index].m_height;
//const uint32_t width = m_slices[slice_index].m_width;
//const uint32_t height = m_slices[slice_index].m_height;
const uint32_t num_blocks_x = m_slices[slice_index].m_num_blocks_x;
const uint32_t num_blocks_y = m_slices[slice_index].m_num_blocks_y;
@@ -590,7 +590,7 @@ namespace basisu
{
for (uint32_t slice_index = 0; slice_index < m_slices.size(); slice_index++)
{
const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
//const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
const uint32_t width = m_slices[slice_index].m_width;
const uint32_t height = m_slices[slice_index].m_height;
const uint32_t num_blocks_x = m_slices[slice_index].m_num_blocks_x;
@@ -603,7 +603,7 @@ namespace basisu
{
for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
{
const uint32_t block_index = first_block_index + block_x + block_y * num_blocks_x;
//const uint32_t block_index = first_block_index + block_x + block_y * num_blocks_x;
encoder_block& m = m_slice_encoder_blocks[slice_index](block_x, block_y);
@@ -680,11 +680,11 @@ namespace basisu
for (uint32_t slice_index = 0; slice_index < m_slices.size(); slice_index++)
{
const int prev_frame_slice_index = is_video ? find_video_frame(slice_index, -1) : -1;
const int next_frame_slice_index = is_video ? find_video_frame(slice_index, 1) : -1;
//const int prev_frame_slice_index = is_video ? find_video_frame(slice_index, -1) : -1;
//const int next_frame_slice_index = is_video ? find_video_frame(slice_index, 1) : -1;
const uint32_t first_block_index = m_slices[slice_index].m_first_block_index;
const uint32_t width = m_slices[slice_index].m_width;
const uint32_t height = m_slices[slice_index].m_height;
//const uint32_t width = m_slices[slice_index].m_width;
//const uint32_t height = m_slices[slice_index].m_height;
const uint32_t num_blocks_x = m_slices[slice_index].m_num_blocks_x;
const uint32_t num_blocks_y = m_slices[slice_index].m_num_blocks_y;
@@ -702,7 +702,7 @@ namespace basisu
{
for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
{
const uint32_t block_index = first_block_index + block_x + block_y * num_blocks_x;
//const uint32_t block_index = first_block_index + block_x + block_y * num_blocks_x;
encoder_block& m = m_slice_encoder_blocks[slice_index](block_x, block_y);
@@ -1168,8 +1168,8 @@ namespace basisu
for (uint32_t slice_index = 0; slice_index < m_slices.size(); slice_index++)
{
const uint32_t width = m_slices[slice_index].m_width;
const uint32_t height = m_slices[slice_index].m_height;
//const uint32_t width = m_slices[slice_index].m_width;
//const uint32_t height = m_slices[slice_index].m_height;
const uint32_t num_blocks_x = m_slices[slice_index].m_num_blocks_x;
const uint32_t num_blocks_y = m_slices[slice_index].m_num_blocks_y;
@@ -1644,7 +1644,7 @@ namespace basisu
uint32_t basisu_backend::encode()
{
const bool is_video = m_pFront_end->get_params().m_tex_type == basist::cBASISTexTypeVideoFrames;
//const bool is_video = m_pFront_end->get_params().m_tex_type == basist::cBASISTexTypeVideoFrames;
m_output.m_slice_desc = m_slices;
m_output.m_etc1s = m_params.m_etc1s;
+4 -1
View File
@@ -1,5 +1,5 @@
// basisu_backend.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -139,6 +139,8 @@ namespace basisu
struct basisu_backend_output
{
basist::basis_tex_format m_tex_format;
bool m_etc1s;
uint32_t m_num_endpoints;
@@ -160,6 +162,7 @@ namespace basisu
void clear()
{
m_tex_format = basist::basis_tex_format::cETC1S;
m_etc1s = false;
m_num_endpoints = 0;
+40 -14
View File
@@ -1,5 +1,5 @@
// basisu_basis_file.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -31,12 +31,19 @@ namespace basisu
m_header.m_total_images = 0;
for (uint32_t i = 0; i < encoder_output.m_slice_desc.size(); i++)
m_header.m_total_images = maximum<uint32_t>(m_header.m_total_images, encoder_output.m_slice_desc[i].m_source_file_index + 1);
m_header.m_format = 0;// basist::block_format::cETC1;
m_header.m_tex_format = (int)encoder_output.m_tex_format;
m_header.m_flags = 0;
if (encoder_output.m_etc1s)
{
assert(encoder_output.m_tex_format == basist::basis_tex_format::cETC1S);
m_header.m_flags = m_header.m_flags | basist::cBASISHeaderFlagETC1S;
}
else
{
assert(encoder_output.m_tex_format != basist::basis_tex_format::cETC1S);
}
if (y_flipped)
m_header.m_flags = m_header.m_flags | basist::cBASISHeaderFlagYFlipped;
@@ -85,7 +92,7 @@ namespace basisu
m_images_descs[i].m_level_index = slice_descs[i].m_mip_index;
if (slice_descs[i].m_alpha)
m_images_descs[i].m_flags = m_images_descs[i].m_flags | basist::cSliceDescFlagsIsAlphaData;
m_images_descs[i].m_flags = m_images_descs[i].m_flags | basist::cSliceDescFlagsHasAlpha;
if (slice_descs[i].m_iframe)
m_images_descs[i].m_flags = m_images_descs[i].m_flags | basist::cSliceDescFlagsFrameIsIFrame;
@@ -127,14 +134,23 @@ namespace basisu
assert(m_comp_data.size() == m_slice_descs_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&m_images_descs[0]), m_images_descs.size() * sizeof(m_images_descs[0]));
assert(m_comp_data.size() == m_endpoint_cb_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&encoder_output.m_endpoint_palette[0]), encoder_output.m_endpoint_palette.size());
if (encoder_output.m_endpoint_palette.size())
{
assert(m_comp_data.size() == m_endpoint_cb_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&encoder_output.m_endpoint_palette[0]), encoder_output.m_endpoint_palette.size());
}
assert(m_comp_data.size() == m_selector_cb_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&encoder_output.m_selector_palette[0]), encoder_output.m_selector_palette.size());
if (encoder_output.m_selector_palette.size())
{
assert(m_comp_data.size() == m_selector_cb_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&encoder_output.m_selector_palette[0]), encoder_output.m_selector_palette.size());
}
assert(m_comp_data.size() == m_tables_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&encoder_output.m_slice_image_tables[0]), encoder_output.m_slice_image_tables.size());
if (encoder_output.m_slice_image_tables.size())
{
assert(m_comp_data.size() == m_tables_file_ofs);
append_vector(m_comp_data, reinterpret_cast<const uint8_t*>(&encoder_output.m_slice_image_tables[0]), encoder_output.m_slice_image_tables.size());
}
assert(m_comp_data.size() == m_first_image_file_ofs);
for (uint32_t i = 0; i < slice_descs.size(); i++)
@@ -173,10 +189,20 @@ namespace basisu
m_header_file_ofs = 0;
m_slice_descs_file_ofs = sizeof(basist::basis_file_header);
m_endpoint_cb_file_ofs = m_slice_descs_file_ofs + sizeof(basist::basis_slice_desc) * (uint32_t)slice_descs.size();
m_selector_cb_file_ofs = m_endpoint_cb_file_ofs + (uint32_t)encoder_output.m_endpoint_palette.size();
m_tables_file_ofs = m_selector_cb_file_ofs + (uint32_t)encoder_output.m_selector_palette.size();
m_first_image_file_ofs = m_tables_file_ofs + (uint32_t)encoder_output.m_slice_image_tables.size();
if (encoder_output.m_tex_format == basist::basis_tex_format::cETC1S)
{
m_endpoint_cb_file_ofs = m_slice_descs_file_ofs + sizeof(basist::basis_slice_desc) * (uint32_t)slice_descs.size();
m_selector_cb_file_ofs = m_endpoint_cb_file_ofs + (uint32_t)encoder_output.m_endpoint_palette.size();
m_tables_file_ofs = m_selector_cb_file_ofs + (uint32_t)encoder_output.m_selector_palette.size();
m_first_image_file_ofs = m_tables_file_ofs + (uint32_t)encoder_output.m_slice_image_tables.size();
}
else
{
m_endpoint_cb_file_ofs = 0;
m_selector_cb_file_ofs = 0;
m_tables_file_ofs = 0;
m_first_image_file_ofs = m_slice_descs_file_ofs + sizeof(basist::basis_slice_desc) * (uint32_t)slice_descs.size();
}
uint64_t total_file_size = m_first_image_file_ofs;
for (uint32_t i = 0; i < encoder_output.m_slice_image_data.size(); i++)
+1984
View File
File diff suppressed because it is too large Load Diff
+131
View File
@@ -0,0 +1,131 @@
// File: basisu_bc7enc.h
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "basisu_enc.h"
#include "transcoder/basisu_transcoder_uastc.h"
namespace basisu
{
#define BC7ENC_MAX_PARTITIONS1 (64)
#define BC7ENC_MAX_UBER_LEVEL (4)
typedef uint8_t bc7enc_bool;
#define BC7ENC_TRUE (1)
#define BC7ENC_FALSE (0)
typedef struct { float m_c[4]; } bc7enc_vec4F;
extern const float g_bc7_weights1x[2 * 4];
extern const float g_bc7_weights2x[4 * 4];
extern const float g_bc7_weights3x[8 * 4];
extern const float g_bc7_weights4x[16 * 4];
extern const float g_astc_weights4x[16 * 4];
extern const float g_astc_weights5x[32 * 4];
extern const float g_astc_weights_3levelsx[3 * 4];
extern basist::astc_quant_bin g_astc_sorted_order_unquant[basist::BC7ENC_TOTAL_ASTC_RANGES][256]; // [sorted unquantized order]
struct color_cell_compressor_params
{
uint32_t m_num_pixels;
const basist::color_quad_u8* m_pPixels;
uint32_t m_num_selector_weights;
const uint32_t* m_pSelector_weights;
const bc7enc_vec4F* m_pSelector_weightsx;
uint32_t m_comp_bits;
const uint8_t *m_pForce_selectors;
// Non-zero m_astc_endpoint_range enables ASTC mode. m_comp_bits and m_has_pbits are always false. We only support 2, 3, or 4 bit weight encodings.
uint32_t m_astc_endpoint_range;
uint32_t m_weights[4];
bc7enc_bool m_has_alpha;
bc7enc_bool m_has_pbits;
bc7enc_bool m_endpoints_share_pbit;
bc7enc_bool m_perceptual;
};
struct color_cell_compressor_results
{
uint64_t m_best_overall_err;
basist::color_quad_u8 m_low_endpoint;
basist::color_quad_u8 m_high_endpoint;
uint32_t m_pbits[2];
uint8_t* m_pSelectors;
uint8_t* m_pSelectors_temp;
// Encoded ASTC indices, if ASTC mode is enabled
basist::color_quad_u8 m_astc_low_endpoint;
basist::color_quad_u8 m_astc_high_endpoint;
};
struct bc7enc_compress_block_params
{
// m_max_partitions_mode1 may range from 0 (disables mode 1) to BC7ENC_MAX_PARTITIONS1. The higher this value, the slower the compressor, but the higher the quality.
uint32_t m_max_partitions_mode1;
// Relative RGBA or YCbCrA weights.
uint32_t m_weights[4];
// m_uber_level may range from 0 to BC7ENC_MAX_UBER_LEVEL. The higher this value, the slower the compressor, but the higher the quality.
uint32_t m_uber_level;
// If m_perceptual is true, colorspace error is computed in YCbCr space, otherwise RGB.
bc7enc_bool m_perceptual;
uint32_t m_least_squares_passes;
};
uint64_t color_cell_compression(uint32_t mode, const color_cell_compressor_params* pParams, color_cell_compressor_results* pResults, const bc7enc_compress_block_params* pComp_params);
uint64_t color_cell_compression_est_astc(
uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeight_table,
uint32_t num_pixels, const basist::color_quad_u8* pPixels,
uint64_t best_err_so_far, const uint32_t weights[4]);
inline void bc7enc_compress_block_params_init_linear_weights(bc7enc_compress_block_params* p)
{
p->m_perceptual = BC7ENC_FALSE;
p->m_weights[0] = 1;
p->m_weights[1] = 1;
p->m_weights[2] = 1;
p->m_weights[3] = 1;
}
inline void bc7enc_compress_block_params_init_perceptual_weights(bc7enc_compress_block_params* p)
{
p->m_perceptual = BC7ENC_TRUE;
p->m_weights[0] = 128;
p->m_weights[1] = 64;
p->m_weights[2] = 16;
p->m_weights[3] = 32;
}
inline void bc7enc_compress_block_params_init(bc7enc_compress_block_params* p)
{
p->m_max_partitions_mode1 = BC7ENC_MAX_PARTITIONS1;
p->m_least_squares_passes = 1;
p->m_uber_level = 0;
bc7enc_compress_block_params_init_perceptual_weights(p);
}
// bc7enc_compress_block_init() MUST be called before calling bc7enc_compress_block() (or you'll get artifacts).
void bc7enc_compress_block_init();
} // namespace basisu
+352 -141
View File
@@ -1,5 +1,5 @@
// basisu_comp.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
#include "basisu_comp.h"
#include "basisu_enc.h"
#include <unordered_set>
#include <atomic>
#define MINIZ_NO_ZLIB_APIS
#include "basisu_miniz.h"
#define BASISU_USE_STB_IMAGE_RESIZE_FOR_MIPMAP_GEN 0
#define DEBUG_CROP_TEXTURE_TO_64x64 (0)
@@ -24,10 +27,10 @@
namespace basisu
{
basis_compressor::basis_compressor() :
m_total_blocks(0),
m_auto_global_sel_pal(false),
m_basis_file_size(0),
m_basis_bits_per_texel(0),
m_total_blocks(0),
m_auto_global_sel_pal(false),
m_any_source_image_has_alpha(false)
{
debug_printf("basis_compressor::basis_compressor\n");
@@ -53,6 +56,7 @@ namespace basisu
debug_printf("Source images: %u, source filenames: %u, source alpha filenames: %i\n",
(uint32_t)m_params.m_source_images.size(), (uint32_t)m_params.m_source_filenames.size(), (uint32_t)m_params.m_source_alpha_filenames.size());
PRINT_BOOL_VALUE(m_uastc);
PRINT_BOOL_VALUE(m_y_flip);
PRINT_BOOL_VALUE(m_debug);
PRINT_BOOL_VALUE(m_debug_images);
@@ -96,6 +100,13 @@ namespace basisu
debug_printf("m_tex_type: %u\n", m_params.m_tex_type);
debug_printf("m_userdata0: 0x%X, m_userdata1: 0x%X\n", m_params.m_userdata0, m_params.m_userdata1);
debug_printf("m_us_per_frame: %i (%f fps)\n", m_params.m_us_per_frame, m_params.m_us_per_frame ? 1.0f / (m_params.m_us_per_frame / 1000000.0f) : 0);
debug_printf("m_pack_uastc_flags: 0x%X\n", m_params.m_pack_uastc_flags);
PRINT_BOOL_VALUE(m_rdo_uastc);
PRINT_FLOAT_VALUE(m_rdo_uastc_quality_scalar);
PRINT_INT_VALUE(m_rdo_uastc_dict_size);
PRINT_FLOAT_VALUE(m_rdo_uastc_max_allowed_rms_increase_ratio);
PRINT_FLOAT_VALUE(m_rdo_uastc_skip_block_rms_thresh);
#undef PRINT_BOOL_VALUE
#undef PRINT_INT_VALUE
@@ -122,14 +133,26 @@ namespace basisu
if (!validate_texture_type_constraints())
return cECFailedValidating;
if (!process_frontend())
if (!extract_source_blocks())
return cECFailedFrontEnd;
if (!extract_frontend_texture_data())
return cECFailedFontendExtract;
if (m_params.m_uastc)
{
error_code ec = encode_slices_to_uastc();
if (ec != cECSuccess)
return ec;
}
else
{
if (!process_frontend())
return cECFailedFrontEnd;
if (!process_backend())
return cECFailedBackend;
if (!extract_frontend_texture_data())
return cECFailedFontendExtract;
if (!process_backend())
return cECFailedBackend;
}
if (!create_basis_file_and_transcode())
return cECFailedCreateBasisFile;
@@ -140,6 +163,99 @@ namespace basisu
return cECSuccess;
}
basis_compressor::error_code basis_compressor::encode_slices_to_uastc()
{
debug_printf("basis_compressor::encode_slices_to_uastc\n");
m_uastc_slice_textures.resize(m_slice_descs.size());
for (uint32_t slice_index = 0; slice_index < m_slice_descs.size(); slice_index++)
m_uastc_slice_textures[slice_index].init(texture_format::cUASTC4x4, m_slice_descs[slice_index].m_orig_width, m_slice_descs[slice_index].m_orig_height);
m_uastc_backend_output.m_tex_format = basist::basis_tex_format::cUASTC4x4;
m_uastc_backend_output.m_etc1s = false;
m_uastc_backend_output.m_slice_desc = m_slice_descs;
m_uastc_backend_output.m_slice_image_data.resize(m_slice_descs.size());
m_uastc_backend_output.m_slice_image_crcs.resize(m_slice_descs.size());
for (uint32_t slice_index = 0; slice_index < m_slice_descs.size(); slice_index++)
{
gpu_image& tex = m_uastc_slice_textures[slice_index];
basisu_backend_slice_desc& slice_desc = m_slice_descs[slice_index];
(void)slice_desc;
const uint32_t num_blocks_x = tex.get_blocks_x();
const uint32_t num_blocks_y = tex.get_blocks_y();
const uint32_t total_blocks = tex.get_total_blocks();
const image& source_image = m_slice_images[slice_index];
std::atomic<uint32_t> total_blocks_processed;
total_blocks_processed = 0;
const uint32_t N = 256;
for (uint32_t block_index_iter = 0; block_index_iter < total_blocks; block_index_iter += N)
{
const uint32_t first_index = block_index_iter;
const uint32_t last_index = minimum<uint32_t>(total_blocks, block_index_iter + N);
m_params.m_pJob_pool->add_job([this, first_index, last_index, num_blocks_x, num_blocks_y, total_blocks, &source_image, &tex, &total_blocks_processed]
{
BASISU_NOTE_UNUSED(num_blocks_y);
for (uint32_t block_index = first_index; block_index < last_index; block_index++)
{
const uint32_t block_x = block_index % num_blocks_x;
const uint32_t block_y = block_index / num_blocks_x;
color_rgba block_pixels[4][4];
source_image.extract_block_clamped((color_rgba*)block_pixels, block_x * 4, block_y * 4, 4, 4);
basist::uastc_block& dest_block = *(basist::uastc_block*)tex.get_block_ptr(block_x, block_y);
encode_uastc(&block_pixels[0][0].r, dest_block, m_params.m_pack_uastc_flags);
total_blocks_processed++;
uint32_t val = total_blocks_processed;
if ((val & 16383) == 16383)
{
debug_printf("basis_compressor::encode_slices_to_uastc: %3.1f%% done\n", static_cast<float>(val) * 100.0f / total_blocks);
}
}
});
} // block_index_iter
m_params.m_pJob_pool->wait_for_all();
if (m_params.m_rdo_uastc)
{
uastc_rdo_params rdo_params;
rdo_params.m_quality_scaler = m_params.m_rdo_uastc_quality_scalar;
rdo_params.m_max_allowed_rms_increase_ratio = m_params.m_rdo_uastc_max_allowed_rms_increase_ratio;
rdo_params.m_skip_block_rms_thresh = m_params.m_rdo_uastc_skip_block_rms_thresh;
rdo_params.m_lz_dict_size = m_params.m_rdo_uastc_dict_size;
bool status = uastc_rdo(tex.get_total_blocks(), (basist::uastc_block*)tex.get_ptr(),
(const color_rgba *)m_source_blocks[slice_desc.m_first_block_index].m_pixels, rdo_params, m_params.m_pack_uastc_flags, m_params.m_pJob_pool,
m_params.m_pJob_pool ? std::min<uint32_t>(4, (uint32_t)m_params.m_pJob_pool->get_total_threads()) : 0);
if (!status)
{
return cECFailedUASTCRDOPostProcess;
}
}
m_uastc_backend_output.m_slice_image_data[slice_index].resize(tex.get_size_in_bytes());
memcpy(&m_uastc_backend_output.m_slice_image_data[slice_index][0], tex.get_ptr(), tex.get_size_in_bytes());
m_uastc_backend_output.m_slice_image_crcs[slice_index] = basist::crc16(tex.get_ptr(), tex.get_size_in_bytes(), 0);
} // slice_index
return cECSuccess;
}
bool basis_compressor::generate_mipmaps(const image &img, std::vector<image> &mips, bool has_alpha)
{
debug_printf("basis_compressor::generate_mipmaps\n");
@@ -247,7 +363,7 @@ namespace basisu
pSource_filename = m_params.m_source_filenames[source_file_index].c_str();
// Load the source image
if (!load_png(pSource_filename, file_image))
if (!load_image(pSource_filename, file_image))
{
error_printf("Failed reading source image: %s\n", pSource_filename);
return false;
@@ -262,7 +378,7 @@ namespace basisu
image alpha_data;
if (!load_png(pSource_alpha_image, alpha_data))
if (!load_image(pSource_alpha_image, alpha_data))
{
error_printf("Failed reading source image: %s\n", pSource_alpha_image);
return false;
@@ -349,6 +465,7 @@ namespace basisu
image &file_image = source_images[source_file_index];
const std::string &source_filename = source_filenames[source_file_index];
// Now, for each source image, create the slices corresponding to that image.
std::vector<image> slices;
slices.reserve(32);
@@ -364,9 +481,9 @@ namespace basisu
for (uint32_t i = 0; i < slices.size(); i++)
mip_indices[i] = i;
if (m_any_source_image_has_alpha)
if ((m_any_source_image_has_alpha) && (!m_params.m_uastc))
{
// If source has alpha, then even mips will have RGB, and odd mips will have alpha in RGB.
// For ETC1S, if source has alpha, then even mips will have RGB, and odd mips will have alpha in RGB.
std::vector<image> alpha_slices;
uint_vec new_mip_indices;
@@ -403,18 +520,29 @@ namespace basisu
for (uint32_t slice_index = 0; slice_index < slices.size(); slice_index++)
{
const bool is_alpha_slice = m_any_source_image_has_alpha && ((slice_index & 1) != 0);
image &slice_image = slices[slice_index];
image& slice_image = slices[slice_index];
const uint32_t orig_width = slice_image.get_width();
const uint32_t orig_height = slice_image.get_height();
bool is_alpha_slice = false;
if (m_any_source_image_has_alpha)
{
if (m_params.m_uastc)
{
is_alpha_slice = slice_image.has_alpha();
}
else
{
is_alpha_slice = (slice_index & 1) != 0;
}
}
// Enlarge the source image to 4x4 block boundaries, duplicating edge pixels if necessary to avoid introducing extra colors into blocks.
slice_image.crop_dup_borders(slice_image.get_block_width(4) * 4, slice_image.get_block_height(4) * 4);
if (m_params.m_debug_images)
{
save_png(string_format("basis_debug_source_image_%u_%u.png", source_file_index, slice_index).c_str(), slice_image);
save_png(string_format("basis_debug_source_image_%u_slice_%u.png", source_file_index, slice_index).c_str(), slice_image);
}
enlarge_vector(m_stats, 1);
@@ -505,28 +633,31 @@ namespace basisu
if (m_any_source_image_has_alpha)
{
// Alpha slices must be at odd slice indices
if (slice_desc.m_alpha)
if (!m_params.m_uastc)
{
if ((i & 1) == 0)
return false;
const basisu_backend_slice_desc &prev_slice_desc = m_slice_descs[i - 1];
// For ETC1S, alpha slices must be at odd slice indices.
if (slice_desc.m_alpha)
{
if ((i & 1) == 0)
return false;
// Make sure previous slice has this image's color data
if (prev_slice_desc.m_source_file_index != slice_desc.m_source_file_index)
return false;
if (prev_slice_desc.m_alpha)
return false;
if (prev_slice_desc.m_mip_index != slice_desc.m_mip_index)
return false;
if (prev_slice_desc.m_num_blocks_x != slice_desc.m_num_blocks_x)
return false;
if (prev_slice_desc.m_num_blocks_y != slice_desc.m_num_blocks_y)
const basisu_backend_slice_desc& prev_slice_desc = m_slice_descs[i - 1];
// Make sure previous slice has this image's color data
if (prev_slice_desc.m_source_file_index != slice_desc.m_source_file_index)
return false;
if (prev_slice_desc.m_alpha)
return false;
if (prev_slice_desc.m_mip_index != slice_desc.m_mip_index)
return false;
if (prev_slice_desc.m_num_blocks_x != slice_desc.m_num_blocks_x)
return false;
if (prev_slice_desc.m_num_blocks_y != slice_desc.m_num_blocks_y)
return false;
}
else if (i & 1)
return false;
}
else if (i & 1)
return false;
}
else if (slice_desc.m_alpha)
{
@@ -610,26 +741,33 @@ namespace basisu
return true;
}
bool basis_compressor::process_frontend()
bool basis_compressor::extract_source_blocks()
{
debug_printf("basis_compressor::process_frontend\n");
debug_printf("basis_compressor::extract_source_blocks\n");
m_source_blocks.resize(m_total_blocks);
for (uint32_t slice_index = 0; slice_index < m_slice_images.size(); slice_index++)
{
const basisu_backend_slice_desc &slice_desc = m_slice_descs[slice_index];
const basisu_backend_slice_desc& slice_desc = m_slice_descs[slice_index];
const uint32_t num_blocks_x = slice_desc.m_num_blocks_x;
const uint32_t num_blocks_y = slice_desc.m_num_blocks_y;
const image &source_image = m_slice_images[slice_index];
const image& source_image = m_slice_images[slice_index];
for (uint32_t block_y = 0; block_y < num_blocks_y; block_y++)
for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
source_image.extract_block_clamped(m_source_blocks[slice_desc.m_first_block_index + block_x + block_y * num_blocks_x].get_ptr(), block_x * 4, block_y * 4, 4, 4);
}
return true;
}
bool basis_compressor::process_frontend()
{
debug_printf("basis_compressor::process_frontend\n");
#if 0
// TODO
basis_etc1_pack_params pack_params;
@@ -925,19 +1063,26 @@ namespace basisu
{
debug_printf("basis_compressor::create_basis_file_and_transcode\n");
const basisu_backend_output &encoded_output = m_backend.get_output();
const basisu_backend_output& encoded_output = m_params.m_uastc ? m_uastc_backend_output : m_backend.get_output();
if (!m_basis_file.init(encoded_output, m_params.m_tex_type, m_params.m_userdata0, m_params.m_userdata1, m_params.m_y_flip, m_params.m_us_per_frame))
{
error_printf("basis_compressor::write_output_files_and_compute_stats: basisu_backend:init() failed!\n");
error_printf("basis_compressor::create_basis_file_and_transcode: basisu_backend:init() failed!\n");
return false;
}
const uint8_vec &comp_data = m_basis_file.get_compressed_data();
m_output_basis_file = comp_data;
// Verify the compressed data by transcoding it to ETC1/BC1 and validating the CRC's.
interval_timer tm;
tm.start();
basist::basisu_transcoder_init();
debug_printf("basist::basisu_transcoder_init: Took %f ms\n", tm.get_elapsed_ms());
// Verify the compressed data by transcoding it to ASTC (or ETC1)/BC7 and validating the CRC's.
basist::basisu_transcoder decoder(m_params.m_pSel_codebook);
if (!decoder.validate_file_checksums(&comp_data[0], (uint32_t)comp_data.size(), true))
{
@@ -948,10 +1093,9 @@ namespace basisu
m_decoded_output_textures.resize(m_slice_descs.size());
m_decoded_output_textures_unpacked.resize(m_slice_descs.size());
m_decoded_output_textures_bc1.resize(m_slice_descs.size());
m_decoded_output_textures_unpacked_bc1.resize(m_slice_descs.size());
interval_timer tm;
m_decoded_output_textures_bc7.resize(m_slice_descs.size());
m_decoded_output_textures_unpacked_bc7.resize(m_slice_descs.size());
tm.start();
if (!decoder.start_transcoding(&comp_data[0], (uint32_t)comp_data.size()))
@@ -970,74 +1114,72 @@ namespace basisu
for (uint32_t i = 0; i < m_slice_descs.size(); i++)
{
gpu_image decoded_texture;
decoded_texture.init(texture_format::cETC1, m_slice_descs[i].m_width, m_slice_descs[i].m_height);
decoded_texture.init(m_params.m_uastc ? texture_format::cASTC4x4 : texture_format::cETC1, m_slice_descs[i].m_width, m_slice_descs[i].m_height);
tm.start();
basist::block_format format = m_params.m_uastc ? basist::block_format::cASTC_4x4 : basist::block_format::cETC1;
uint32_t bytes_per_block = m_params.m_uastc ? 16 : 8;
if (!decoder.transcode_slice(&comp_data[0], (uint32_t)comp_data.size(), i,
reinterpret_cast<etc_block *>(decoded_texture.get_ptr()), m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y, basist::block_format::cETC1, 8))
reinterpret_cast<etc_block *>(decoded_texture.get_ptr()), m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y, format, bytes_per_block))
{
error_printf("Transcoding failed to ETC1 on slice %u!\n", i);
error_printf("Transcoding failed on slice %u!\n", i);
return false;
}
total_time_etc1 += tm.get_elapsed_secs();
uint32_t image_crc16 = basist::crc16(decoded_texture.get_ptr(), decoded_texture.get_size_in_bytes(), 0);
if (image_crc16 != m_backend.get_output().m_slice_image_crcs[i])
if (encoded_output.m_tex_format == basist::basis_tex_format::cETC1S)
{
error_printf("Decoded image data CRC check failed on slice %u!\n", i);
return false;
uint32_t image_crc16 = basist::crc16(decoded_texture.get_ptr(), decoded_texture.get_size_in_bytes(), 0);
if (image_crc16 != encoded_output.m_slice_image_crcs[i])
{
error_printf("Decoded image data CRC check failed on slice %u!\n", i);
return false;
}
debug_printf("Decoded image data CRC check succeeded on slice %i\n", i);
}
debug_printf("Decoded image data CRC check succeeded on slice %i\n", i);
m_decoded_output_textures[i] = decoded_texture;
total_orig_pixels += m_slice_descs[i].m_orig_width * m_slice_descs[i].m_orig_height;
total_texels += m_slice_descs[i].m_width * m_slice_descs[i].m_height;
}
tm.start();
basist::basisu_transcoder_init();
debug_printf("basist::basisu_transcoder_init: Took %f ms\n", tm.get_elapsed_ms());
double total_time_bc1 = 0;
double total_time_bc7 = 0;
for (uint32_t i = 0; i < m_slice_descs.size(); i++)
{
gpu_image decoded_texture;
decoded_texture.init(texture_format::cBC1, m_slice_descs[i].m_width, m_slice_descs[i].m_height);
decoded_texture.init(texture_format::cBC7, m_slice_descs[i].m_width, m_slice_descs[i].m_height);
tm.start();
if (!decoder.transcode_slice(&comp_data[0], (uint32_t)comp_data.size(), i,
reinterpret_cast<etc_block *>(decoded_texture.get_ptr()), m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y, basist::block_format::cBC1, 8))
reinterpret_cast<etc_block *>(decoded_texture.get_ptr()), m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y, basist::block_format::cBC7, 16))
{
error_printf("Transcoding failed to BC1 on slice %u!\n", i);
error_printf("Transcoding failed to BC7 on slice %u!\n", i);
return false;
}
total_time_bc1 += tm.get_elapsed_secs();
total_time_bc7 += tm.get_elapsed_secs();
m_decoded_output_textures_bc1[i] = decoded_texture;
m_decoded_output_textures_bc7[i] = decoded_texture;
}
for (uint32_t i = 0; i < m_slice_descs.size(); i++)
{
m_decoded_output_textures[i].unpack(m_decoded_output_textures_unpacked[i]);
m_decoded_output_textures_bc1[i].unpack(m_decoded_output_textures_unpacked_bc1[i]);
m_decoded_output_textures_bc7[i].unpack(m_decoded_output_textures_unpacked_bc7[i]);
}
debug_printf("Transcoded to ETC1 in %3.3fms, %f texels/sec\n", total_time_etc1 * 1000.0f, total_orig_pixels / total_time_etc1);
debug_printf("Transcoded to %s in %3.3fms, %f texels/sec\n", m_params.m_uastc ? "ASTC" : "ETC1", total_time_etc1 * 1000.0f, total_orig_pixels / total_time_etc1);
debug_printf("Transcoded to BC1 in %3.3fms, %f texels/sec\n", total_time_bc1 * 1000.0f, total_orig_pixels / total_time_bc1);
debug_printf("Transcoded to BC7 in %3.3fms, %f texels/sec\n", total_time_bc7 * 1000.0f, total_orig_pixels / total_time_bc7);
debug_printf("Total .basis output file size: %u, %3.3f bits/texel\n", comp_data.size(), comp_data.size() * 8.0f / total_orig_pixels);
m_output_blocks.resize(0);
uint32_t total_orig_texels = 0;
for (uint32_t slice_index = 0; slice_index < m_slice_descs.size(); slice_index++)
{
@@ -1046,10 +1188,9 @@ namespace basisu
total_orig_texels += slice_desc.m_orig_width * slice_desc.m_orig_height;
const uint32_t total_blocks = slice_desc.m_num_blocks_x * slice_desc.m_num_blocks_y;
BASISU_NOTE_UNUSED(total_blocks);
assert(m_decoded_output_textures[slice_index].get_total_blocks() == total_blocks);
memcpy(enlarge_vector(m_output_blocks, total_blocks), m_decoded_output_textures[slice_index].get_ptr(), sizeof(etc_block) * total_blocks);
}
m_basis_file_size = (uint32_t)comp_data.size();
@@ -1062,9 +1203,11 @@ namespace basisu
{
debug_printf("basis_compressor::write_output_files_and_compute_stats\n");
//const basisu_backend_output& encoded_output = m_params.m_uastc ? m_uastc_backend_output : m_backend.get_output();
const uint8_vec& comp_data = m_basis_file.get_compressed_data();
if (m_params.m_write_output_basis_files)
{
const uint8_vec &comp_data = m_basis_file.get_compressed_data();
const std::string& basis_filename = m_params.m_out_filename;
@@ -1076,6 +1219,27 @@ namespace basisu
printf("Wrote output .basis file \"%s\"\n", basis_filename.c_str());
}
size_t comp_size = 0;
if ((m_params.m_compute_stats) && (m_params.m_uastc) && (comp_data.size()))
{
void* pComp_data = tdefl_compress_mem_to_heap(&comp_data[0], comp_data.size(), &comp_size, TDEFL_MAX_PROBES_MASK);// TDEFL_DEFAULT_MAX_PROBES);
size_t decomp_size = 0;
void* pDecomp_data = tinfl_decompress_mem_to_heap(pComp_data, comp_size, &decomp_size, 0);
if ((decomp_size != comp_data.size()) || (memcmp(pDecomp_data, &comp_data[0], decomp_size) != 0))
{
printf("basis_compressor::create_basis_file_and_transcode:: miniz compression or decompression failed!\n");
return false;
}
mz_free(pComp_data);
mz_free(pDecomp_data);
uint32_t total_texels = 0;
for (uint32_t i = 0; i < m_slice_descs.size(); i++)
total_texels += (m_slice_descs[i].m_num_blocks_x * m_slice_descs[i].m_num_blocks_y) * 16;
debug_printf(".basis file size: %u, LZ compressed file size: %u, %3.2f bits/texel\n",
(uint32_t)comp_data.size(),
(uint32_t)comp_size,
comp_size * 8.0f / total_texels);
}
m_stats.resize(m_slice_descs.size());
@@ -1097,75 +1261,119 @@ namespace basisu
image_metrics em;
// ---- .basis ETC1S stats
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0);
em.print(".basis ETC1S 709 Luma: ");
s.m_basis_etc1s_luma_709_psnr = static_cast<float>(em.m_psnr);
s.m_basis_etc1s_luma_709_ssim = static_cast<float>(em.m_ssim);
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0, true, true);
em.print(".basis ETC1S 601 Luma: ");
s.m_basis_etc1s_luma_601_psnr = static_cast<float>(em.m_psnr);
// ---- .basis stats
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 3);
em.print(".basis ETC1S RGB Avg: ");
em.print(".basis RGB Avg: ");
s.m_basis_rgb_avg_psnr = em.m_psnr;
s.m_basis_etc1s_rgb_avg_psnr = em.m_psnr;
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 4);
em.print(".basis RGBA Avg: ");
s.m_basis_rgba_avg_psnr = em.m_psnr;
if (m_slice_descs.size() == 1)
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 1);
em.print(".basis R Avg: ");
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 1, 1);
em.print(".basis G Avg: ");
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 2, 1);
em.print(".basis B Avg: ");
if (m_params.m_uastc)
{
debug_printf(".basis Luma 709 PSNR per bit/texel*10000: %3.3f\n", 10000.0f * s.m_basis_etc1s_luma_709_psnr / ((m_backend.get_output().get_output_size_estimate() * 8.0f) / (slice_desc.m_orig_width * slice_desc.m_orig_height)));
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 3, 1);
em.print(".basis A Avg: ");
s.m_basis_a_avg_psnr = em.m_psnr;
}
// ---- .basis BC1 stats
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc1[slice_index], 0, 0);
em.print(".basis BC1 709 Luma: ");
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0);
em.print(".basis 709 Luma: ");
s.m_basis_luma_709_psnr = static_cast<float>(em.m_psnr);
s.m_basis_luma_709_ssim = static_cast<float>(em.m_ssim);
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0, true, true);
em.print(".basis 601 Luma: ");
s.m_basis_luma_601_psnr = static_cast<float>(em.m_psnr);
s.m_basis_bc1_luma_709_psnr = static_cast<float>(em.m_psnr);
s.m_basis_bc1_luma_709_ssim = static_cast<float>(em.m_ssim);
if (m_slice_descs.size() == 1)
{
const uint32_t output_size = comp_size ? (uint32_t)comp_size : (uint32_t)comp_data.size();
debug_printf(".basis RGB PSNR per bit/texel*10000: %3.3f\n", 10000.0f * s.m_basis_rgb_avg_psnr / ((output_size * 8.0f) / (slice_desc.m_orig_width * slice_desc.m_orig_height)));
debug_printf(".basis Luma 709 PSNR per bit/texel*10000: %3.3f\n", 10000.0f * s.m_basis_luma_709_psnr / ((output_size * 8.0f) / (slice_desc.m_orig_width * slice_desc.m_orig_height)));
}
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc1[slice_index], 0, 0, true, true);
em.print(".basis BC1 601 Luma: ");
{
// ---- BC7 stats
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 0, 3);
em.print("BC7 RGB Avg: ");
s.m_bc7_rgb_avg_psnr = em.m_psnr;
s.m_basis_bc1_luma_601_psnr = static_cast<float>(em.m_psnr);
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 0, 4);
em.print("BC7 RGBA Avg: ");
s.m_bc7_rgba_avg_psnr = em.m_psnr;
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc1[slice_index], 0, 3);
em.print(".basis BC1 RGB Avg: ");
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 0, 1);
em.print("BC7 R Avg: ");
s.m_basis_bc1_rgb_avg_psnr = static_cast<float>(em.m_psnr);
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 1, 1);
em.print("BC7 G Avg: ");
// ---- Nearly best possible ETC1S stats
em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0);
em.print("Unquantized ETC1S 709 Luma: ");
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 2, 1);
em.print("BC7 B Avg: ");
s.m_best_luma_709_psnr = static_cast<float>(em.m_psnr);
s.m_best_luma_709_ssim = static_cast<float>(em.m_ssim);
if (m_params.m_uastc)
{
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 3, 1);
em.print("BC7 A Avg: ");
em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0, true, true);
em.print("Unquantized ETC1S 601 Luma: ");
s.m_bc7_a_avg_psnr = em.m_psnr;
}
s.m_best_luma_601_psnr = static_cast<float>(em.m_psnr);
em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 3);
em.print("Unquantized ETC1S RGB Avg: ");
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 0, 0);
em.print("BC7 709 Luma: ");
s.m_bc7_luma_709_psnr = static_cast<float>(em.m_psnr);
s.m_bc7_luma_709_ssim = static_cast<float>(em.m_ssim);
s.m_best_rgb_avg_psnr = static_cast<float>(em.m_psnr);
em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked_bc7[slice_index], 0, 0, true, true);
em.print("BC7 601 Luma: ");
s.m_bc7_luma_601_psnr = static_cast<float>(em.m_psnr);
}
if (!m_params.m_uastc)
{
// ---- Nearly best possible ETC1S stats
em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0);
em.print("Unquantized ETC1S 709 Luma: ");
s.m_best_etc1s_luma_709_psnr = static_cast<float>(em.m_psnr);
s.m_best_etc1s_luma_709_ssim = static_cast<float>(em.m_ssim);
em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0, true, true);
em.print("Unquantized ETC1S 601 Luma: ");
s.m_best_etc1s_luma_601_psnr = static_cast<float>(em.m_psnr);
em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 3);
em.print("Unquantized ETC1S RGB Avg: ");
s.m_best_etc1s_rgb_avg_psnr = static_cast<float>(em.m_psnr);
}
}
if (m_frontend.get_params().m_debug_images)
{
std::string out_basename;
if (m_params.m_out_filename.size())
string_get_filename(m_params.m_out_filename.c_str(), out_basename);
else if (m_params.m_source_filenames.size())
string_get_filename(m_params.m_source_filenames[slice_desc.m_source_file_index].c_str(), out_basename);
string_remove_extension(out_basename);
out_basename = "basis_debug_" + out_basename + string_format("_slice_%u", slice_index);
std::string out_basename;
if (m_params.m_out_filename.size())
string_get_filename(m_params.m_out_filename.c_str(), out_basename);
else if (m_params.m_source_filenames.size())
string_get_filename(m_params.m_source_filenames[slice_desc.m_source_file_index].c_str(), out_basename);
string_remove_extension(out_basename);
out_basename = "basis_debug_" + out_basename + string_format("_slice_%u", slice_index);
if ((!m_params.m_uastc) && (m_frontend.get_params().m_debug_images))
{
// Write "best" ETC1S debug images
if (!m_params.m_uastc)
{
gpu_image best_etc1s_gpu_image(m_best_etc1s_images[slice_index]);
best_etc1s_gpu_image.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
@@ -1175,27 +1383,30 @@ namespace basisu
best_etc1s_gpu_image.unpack(best_etc1s_unpacked);
save_png(out_basename + "_best_etc1s.png", best_etc1s_unpacked);
}
}
// Write decoded ETC1S debug images
if (m_params.m_debug_images)
{
// Write decoded ETC1S/ASTC debug images
{
gpu_image decoded_etc1s(m_decoded_output_textures[slice_index]);
decoded_etc1s.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
write_compressed_texture_file((out_basename + "_decoded_etc1s.ktx").c_str(), decoded_etc1s);
gpu_image decoded_etc1s_or_astc(m_decoded_output_textures[slice_index]);
decoded_etc1s_or_astc.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
write_compressed_texture_file((out_basename + "_transcoded_etc1s_astc.ktx").c_str(), decoded_etc1s_or_astc);
image temp(m_decoded_output_textures_unpacked[slice_index]);
temp.crop(slice_desc.m_orig_width, slice_desc.m_orig_height);
save_png(out_basename + "_decoded_etc1s.png", temp);
save_png(out_basename + "_transcoded_etc1s_astc.png", temp);
}
// Write decoded BC1 debug images
{
gpu_image decoded_bc1(m_decoded_output_textures_bc1[slice_index]);
decoded_bc1.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
write_compressed_texture_file((out_basename + "_decoded_bc1.ktx").c_str(), decoded_bc1);
image temp(m_decoded_output_textures_unpacked_bc1[slice_index]);
// Write decoded BC7 debug images
{
gpu_image decoded_bc7(m_decoded_output_textures_bc7[slice_index]);
decoded_bc7.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height);
write_compressed_texture_file((out_basename + "_transcoded_bc7.ktx").c_str(), decoded_bc7);
image temp(m_decoded_output_textures_unpacked_bc7[slice_index]);
temp.crop(slice_desc.m_orig_width, slice_desc.m_orig_height);
save_png(out_basename + "_decoded_bc1.png", temp);
save_png(out_basename + "_transcoded_bc7.png", temp);
}
}
}
+83 -44
View File
@@ -1,5 +1,5 @@
// basisu_comp.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
#include "basisu_basis_file.h"
#include "transcoder/basisu_global_selector_palette.h"
#include "transcoder/basisu_transcoder.h"
#include "basisu_uastc_enc.h"
namespace basisu
{
@@ -52,43 +53,52 @@ namespace basisu
m_filename.clear();
m_width = 0;
m_height = 0;
m_basis_rgb_avg_psnr = 0.0f;
m_basis_rgba_avg_psnr = 0.0f;
m_basis_a_avg_psnr = 0.0f;
m_basis_luma_709_psnr = 0.0f;
m_basis_luma_601_psnr = 0.0f;
m_basis_luma_709_ssim = 0.0f;
m_basis_etc1s_rgb_avg_psnr = 0.0f;
m_basis_etc1s_luma_709_psnr = 0.0f;
m_basis_etc1s_luma_601_psnr = 0.0f;
m_basis_etc1s_luma_709_ssim = 0.0f;
m_basis_bc1_rgb_avg_psnr = 0.0f;
m_basis_bc1_luma_709_psnr = 0.0f;
m_basis_bc1_luma_601_psnr = 0.0f;
m_basis_bc1_luma_709_ssim = 0.0f;
m_best_rgb_avg_psnr = 0.0f;
m_best_luma_709_psnr = 0.0f;
m_best_luma_601_psnr = 0.0f;
m_best_luma_709_ssim = 0.0f;
m_bc7_rgb_avg_psnr = 0.0f;
m_bc7_rgba_avg_psnr = 0.0f;
m_bc7_a_avg_psnr = 0.0f;
m_bc7_luma_709_psnr = 0.0f;
m_bc7_luma_601_psnr = 0.0f;
m_bc7_luma_709_ssim = 0.0f;
m_best_etc1s_rgb_avg_psnr = 0.0f;
m_best_etc1s_luma_709_psnr = 0.0f;
m_best_etc1s_luma_601_psnr = 0.0f;
m_best_etc1s_luma_709_ssim = 0.0f;
}
std::string m_filename;
uint32_t m_width;
uint32_t m_height;
// .basis compressed
float m_basis_etc1s_rgb_avg_psnr;
float m_basis_etc1s_luma_709_psnr;
float m_basis_etc1s_luma_601_psnr;
float m_basis_etc1s_luma_709_ssim;
float m_basis_bc1_rgb_avg_psnr;
float m_basis_bc1_luma_709_psnr;
float m_basis_bc1_luma_601_psnr;
float m_basis_bc1_luma_709_ssim;
// .basis compressed (ETC1S or UASTC statistics)
float m_basis_rgb_avg_psnr;
float m_basis_rgba_avg_psnr;
float m_basis_a_avg_psnr;
float m_basis_luma_709_psnr;
float m_basis_luma_601_psnr;
float m_basis_luma_709_ssim;
// Normal (highest quality) compressed ETC1S
float m_best_rgb_avg_psnr;
float m_best_luma_709_psnr;
float m_best_luma_601_psnr;
float m_best_luma_709_ssim;
// BC7 statistics
float m_bc7_rgb_avg_psnr;
float m_bc7_rgba_avg_psnr;
float m_bc7_a_avg_psnr;
float m_bc7_luma_709_psnr;
float m_bc7_luma_601_psnr;
float m_bc7_luma_709_ssim;
// Highest achievable quality ETC1S statistics
float m_best_etc1s_rgb_avg_psnr;
float m_best_etc1s_luma_709_psnr;
float m_best_etc1s_luma_601_psnr;
float m_best_etc1s_luma_709_ssim;
};
template<bool def>
@@ -175,18 +185,23 @@ namespace basisu
struct basis_compressor_params
{
basis_compressor_params() :
m_pSel_codebook(NULL),
m_compression_level((int)BASISU_DEFAULT_COMPRESSION_LEVEL, 0, (int)BASISU_MAX_COMPRESSION_LEVEL),
m_selector_rdo_thresh(BASISU_DEFAULT_SELECTOR_RDO_THRESH, 0.0f, 1e+10f),
m_endpoint_rdo_thresh(BASISU_DEFAULT_ENDPOINT_RDO_THRESH, 0.0f, 1e+10f),
m_hybrid_sel_cb_quality_thresh(BASISU_DEFAULT_HYBRID_SEL_CB_QUALITY_THRESH, 0.0f, 1e+10f),
m_global_pal_bits(8, 0, ETC1_GLOBAL_SELECTOR_CODEBOOK_MAX_PAL_BITS),
m_global_mod_bits(8, 0, basist::etc1_global_palette_entry_modifier::cTotalBits),
m_endpoint_rdo_thresh(BASISU_DEFAULT_ENDPOINT_RDO_THRESH, 0.0f, 1e+10f),
m_selector_rdo_thresh(BASISU_DEFAULT_SELECTOR_RDO_THRESH, 0.0f, 1e+10f),
m_pSel_codebook(NULL),
m_mip_scale(1.0f, .000125f, 4.0f),
m_mip_smallest_dimension(1, 1, 16384),
m_max_endpoint_clusters(512),
m_max_selector_clusters(512),
m_quality_level(-1),
m_mip_scale(1.0f, .000125f, 4.0f),
m_mip_smallest_dimension(1, 1, 16384),
m_compression_level((int)BASISU_DEFAULT_COMPRESSION_LEVEL, 0, (int)BASISU_MAX_COMPRESSION_LEVEL),
m_pack_uastc_flags(cPackUASTCLevelDefault),
m_rdo_uastc_quality_scalar(1.0f, 0.001f, 10.0f),
m_rdo_uastc_dict_size(32768, 256, 65536),
m_rdo_uastc_max_allowed_rms_increase_ratio(UASTC_RDO_DEFAULT_MAX_ALLOWED_RMS_INCREASE_RATIO, .01f, 100.0f),
m_rdo_uastc_skip_block_rms_thresh(UASTC_RDO_DEFAULT_SKIP_BLOCK_RMS_THRESH, .01f, 100.0f),
m_pJob_pool(nullptr)
{
clear();
@@ -196,6 +211,8 @@ namespace basisu
{
m_pSel_codebook = NULL;
m_uastc.clear();
m_source_filenames.clear();
m_source_alpha_filenames.clear();
@@ -247,12 +264,21 @@ namespace basisu
m_userdata1 = 0;
m_us_per_frame = 0;
m_pack_uastc_flags = cPackUASTCLevelDefault;
m_rdo_uastc.clear();
m_rdo_uastc_quality_scalar.clear();
m_rdo_uastc_max_allowed_rms_increase_ratio.clear();
m_rdo_uastc_skip_block_rms_thresh.clear();
m_pJob_pool = nullptr;
}
// Pointer to the global selector codebook, or nullptr to not use a global selector codebook
const basist::etc1_global_selector_codebook *m_pSel_codebook;
// True to generate UASTC .basis file data, otherwise ETC1S.
bool_param<false> m_uastc;
// If m_read_source_images is true, m_source_filenames (and optionally m_source_alpha_filenames) contains the filenames of PNG images to read.
// Otherwise, the compressor processes the images in m_source_images.
std::vector<std::string> m_source_filenames;
@@ -343,6 +369,14 @@ namespace basisu
uint32_t m_userdata1;
uint32_t m_us_per_frame;
// cPackUASTCLevelDefault, etc.
uint32_t m_pack_uastc_flags;
bool_param<false> m_rdo_uastc;
param<float> m_rdo_uastc_quality_scalar;
param<int> m_rdo_uastc_dict_size;
param<float> m_rdo_uastc_max_allowed_rms_increase_ratio;
param<float> m_rdo_uastc_skip_block_rms_thresh;
job_pool *m_pJob_pool;
};
@@ -360,25 +394,26 @@ namespace basisu
cECSuccess = 0,
cECFailedReadingSourceImages,
cECFailedValidating,
cECFailedEncodeUASTC,
cECFailedFrontEnd,
cECFailedFontendExtract,
cECFailedBackend,
cECFailedCreateBasisFile,
cECFailedWritingOutput
cECFailedWritingOutput,
cECFailedUASTCRDOPostProcess
};
error_code process();
const uint8_vec &get_output_basis_file() const { return m_output_basis_file; }
const etc_block_vec &get_output_blocks() const { return m_output_blocks; }
const std::vector<image_stats> &get_stats() const { return m_stats; }
uint32_t get_basis_file_size() const { return m_basis_file_size; }
double get_basis_bits_per_texel() const { return m_basis_bits_per_texel; }
bool get_any_source_image_has_alpha() const { return m_any_source_image_has_alpha; }
private:
basis_compressor_params m_params;
@@ -408,20 +443,24 @@ namespace basisu
std::vector<gpu_image> m_decoded_output_textures;
std::vector<image> m_decoded_output_textures_unpacked;
std::vector<gpu_image> m_decoded_output_textures_bc1;
std::vector<image> m_decoded_output_textures_unpacked_bc1;
std::vector<gpu_image> m_decoded_output_textures_bc7;
std::vector<image> m_decoded_output_textures_unpacked_bc7;
uint8_vec m_output_basis_file;
etc_block_vec m_output_blocks;
std::vector<gpu_image> m_uastc_slice_textures;
basisu_backend_output m_uastc_backend_output;
bool m_any_source_image_has_alpha;
bool read_source_images();
bool extract_source_blocks();
bool process_frontend();
bool extract_frontend_texture_data();
bool process_backend();
bool create_basis_file_and_transcode();
bool write_output_files_and_compute_stats();
error_code encode_slices_to_uastc();
bool generate_mipmaps(const image &img, std::vector<image> &mips, bool has_alpha);
bool validate_texture_type_constraints();
};
+95 -13
View File
@@ -1,5 +1,5 @@
// basisu_enc.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -18,10 +18,13 @@
#include "basisu_resampler_filters.h"
#include "basisu_etc.h"
#include "transcoder/basisu_transcoder.h"
#include "basisu_bc7enc.h"
#include "apg_bmp.h"
#if defined(_WIN32)
// For QueryPerformanceCounter/QueryPerformanceFrequency
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#endif
@@ -54,6 +57,9 @@ namespace basisu
void basisu_encoder_init()
{
basist::basisu_transcoder_init();
pack_etc1_solid_color_init();
//uastc_init();
bc7enc_compress_block_init(); // must be after uastc_init()
}
void error_printf(const char *pFmt, ...)
@@ -108,7 +114,7 @@ namespace basisu
#else
#error TODO
#endif
interval_timer::interval_timer() : m_start_time(0), m_stop_time(0), m_started(false), m_stopped(false)
{
if (!g_timer_freq)
@@ -169,6 +175,61 @@ namespace basisu
return ticks * g_timer_freq;
}
const uint32_t MAX_32BIT_ALLOC_SIZE = 250000000;
bool load_bmp(const char* pFilename, image& img)
{
int w = 0, h = 0;
unsigned int n_chans = 0;
unsigned char* pImage_data = apg_bmp_read(pFilename, &w, &h, &n_chans);
if ((!pImage_data) || (!w) || (!h) || ((n_chans != 3) && (n_chans != 4)))
{
error_printf("Failed loading .BMP image \"%s\"!\n", pFilename);
if (pImage_data)
apg_bmp_free(pImage_data);
return false;
}
if (sizeof(void *) == sizeof(uint32_t))
{
if ((w * h * n_chans) > MAX_32BIT_ALLOC_SIZE)
{
error_printf("Image \"%s\" is too large (%ux%u) to process in a 32-bit build!\n", pFilename, w, h);
if (pImage_data)
apg_bmp_free(pImage_data);
return false;
}
}
img.resize(w, h);
const uint8_t *pSrc = pImage_data;
for (int y = 0; y < h; y++)
{
color_rgba *pDst = &img(0, y);
for (int x = 0; x < w; x++)
{
pDst->r = pSrc[0];
pDst->g = pSrc[1];
pDst->b = pSrc[2];
pDst->a = (n_chans == 3) ? 255 : pSrc[3];
pSrc += n_chans;
++pDst;
}
}
apg_bmp_free(pImage_data);
return true;
}
bool load_png(const char* pFilename, image& img)
{
std::vector<uint8_t> buffer;
@@ -187,10 +248,9 @@ namespace basisu
return false;
const uint32_t exepected_alloc_size = w * h * sizeof(uint32_t);
// If the file is too large on 32-bit builds then just bail now, to prevent causing a memory exception.
const uint32_t MAX_ALLOC_SIZE = 250000000;
if (exepected_alloc_size >= MAX_ALLOC_SIZE)
if (exepected_alloc_size >= MAX_32BIT_ALLOC_SIZE)
{
error_printf("Image \"%s\" is too large (%ux%u) to process in a 32-bit build!\n", pFilename, w, h);
return false;
@@ -213,12 +273,33 @@ namespace basisu
return true;
}
bool load_image(const char* pFilename, image& img)
{
std::string ext(string_get_extension(std::string(pFilename)));
if (ext.length() == 0)
return false;
const char *pExt = ext.c_str();
if (strcasecmp(pExt, "png") == 0)
return load_png(pFilename, img);
if (strcasecmp(pExt, "bmp") == 0)
return load_bmp(pFilename, img);
return false;
}
bool save_png(const char* pFilename, const image & img, uint32_t image_save_flags, uint32_t grayscale_comp)
bool save_png(const char* pFilename, const image &img, uint32_t image_save_flags, uint32_t grayscale_comp)
{
if (!img.get_total_pixels())
return false;
const uint32_t MAX_PNG_IMAGE_DIM = 32768;
if ((img.get_width() > MAX_PNG_IMAGE_DIM) || (img.get_height() > MAX_PNG_IMAGE_DIM))
return false;
std::vector<uint8_t> out;
unsigned err = 0;
@@ -231,16 +312,17 @@ namespace basisu
for (uint32_t x = 0; x < img.get_width(); x++)
*pDst++ = img(x, y)[grayscale_comp];
err = lodepng::encode(out, (const uint8_t*)& g_pixels[0], img.get_width(), img.get_height(), LCT_GREY, 8);
err = lodepng::encode(out, (const uint8_t*)&g_pixels[0], img.get_width(), img.get_height(), LCT_GREY, 8);
}
else
{
bool has_alpha = img.has_alpha();
if ((!has_alpha) || ((image_save_flags & cImageSaveIgnoreAlpha) != 0))
{
uint8_vec rgb_pixels(img.get_width() * 3 * img.get_height());
const uint64_t total_bytes = (uint64_t)img.get_width() * 3U * (uint64_t)img.get_height();
uint8_vec rgb_pixels(total_bytes);
uint8_t *pDst = &rgb_pixels[0];
for (uint32_t y = 0; y < img.get_height(); y++)
{
for (uint32_t x = 0; x < img.get_width(); x++)
@@ -1171,9 +1253,9 @@ namespace basisu
total_values *= (double)clamp<uint32_t>(total_chans, 1, 4);
m_mean = (float)clamp<double>(sum / total_values, 0.0f, 255.0);
m_mean_squared = (float)clamp<double>(sum2 / total_values, 0.0f, 255.0 * 255.0);
m_mean_squared = (float)clamp<double>(sum2 / total_values, 0.0f, 255.0f * 255.0f);
m_rms = (float)sqrt(m_mean_squared);
m_psnr = m_rms ? (float)clamp<double>(log10(255.0 / m_rms) * 20.0, 0.0f, 300.0f) : 1e+10f;
m_psnr = m_rms ? (float)clamp<double>(log10(255.0 / m_rms) * 20.0f, 0.0f, 100.0f) : 100.0f;
}
void fill_buffer_with_random_bytes(void *pBuf, size_t size, uint32_t seed)
@@ -1253,8 +1335,8 @@ namespace basisu
}
job_pool::job_pool(uint32_t num_threads) :
m_kill_flag(false),
m_num_active_jobs(0)
m_num_active_jobs(0),
m_kill_flag(false)
{
assert(num_threads >= 1U);
+172 -7
View File
@@ -1,5 +1,5 @@
// basisu_enc.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -31,7 +31,8 @@ namespace basisu
{
extern uint8_t g_hamming_dist[256];
// Encoder library initialization
// Encoder library initialization.
// This function MUST be called before encoding anything!
void basisu_encoder_init();
void error_printf(const char *pFmt, ...);
@@ -42,7 +43,67 @@ namespace basisu
{
return (uint8_t)((i & 0xFFFFFF00U) ? (~(i >> 31)) : i);
}
inline int32_t clampi(int32_t value, int32_t low, int32_t high)
{
if (value < low)
value = low;
else if (value > high)
value = high;
return value;
}
inline uint8_t mul_8(uint32_t v, uint32_t a)
{
v = v * a + 128;
return (uint8_t)((v + (v >> 8)) >> 8);
}
inline uint64_t read_bits(const uint8_t* pBuf, uint32_t& bit_offset, uint32_t codesize)
{
assert(codesize <= 64);
uint64_t bits = 0;
uint32_t total_bits = 0;
while (total_bits < codesize)
{
uint32_t byte_bit_offset = bit_offset & 7;
uint32_t bits_to_read = minimum<int>(codesize - total_bits, 8 - byte_bit_offset);
uint32_t byte_bits = pBuf[bit_offset >> 3] >> byte_bit_offset;
byte_bits &= ((1 << bits_to_read) - 1);
bits |= ((uint64_t)(byte_bits) << total_bits);
total_bits += bits_to_read;
bit_offset += bits_to_read;
}
return bits;
}
inline uint32_t read_bits32(const uint8_t* pBuf, uint32_t& bit_offset, uint32_t codesize)
{
assert(codesize <= 32);
uint32_t bits = 0;
uint32_t total_bits = 0;
while (total_bits < codesize)
{
uint32_t byte_bit_offset = bit_offset & 7;
uint32_t bits_to_read = minimum<int>(codesize - total_bits, 8 - byte_bit_offset);
uint32_t byte_bits = pBuf[bit_offset >> 3] >> byte_bit_offset;
byte_bits &= ((1 << bits_to_read) - 1);
bits |= (byte_bits << total_bits);
total_bits += bits_to_read;
bit_offset += bits_to_read;
}
return bits;
}
// Hashing
inline uint32_t bitmix32c(uint32_t v)
@@ -78,6 +139,71 @@ namespace basisu
return hash_hsieh(reinterpret_cast<const uint8_t *>(&k), sizeof(k));
}
};
class running_stat
{
public:
running_stat() :
m_n(0),
m_old_m(0), m_new_m(0), m_old_s(0), m_new_s(0)
{
}
void clear()
{
m_n = 0;
}
void push(double x)
{
m_n++;
if (m_n == 1)
{
m_old_m = m_new_m = x;
m_old_s = 0.0;
m_min = x;
m_max = x;
}
else
{
m_new_m = m_old_m + (x - m_old_m) / m_n;
m_new_s = m_old_s + (x - m_old_m) * (x - m_new_m);
m_old_m = m_new_m;
m_old_s = m_new_s;
m_min = std::min(x, m_min);
m_max = std::max(x, m_max);
}
}
uint32_t get_num() const
{
return m_n;
}
double get_mean() const
{
return (m_n > 0) ? m_new_m : 0.0;
}
double get_variance() const
{
return ((m_n > 1) ? m_new_s / (m_n - 1) : 0.0);
}
double get_std_dev() const
{
return sqrt(get_variance());
}
double get_min() const
{
return m_min;
}
double get_max() const
{
return m_max;
}
private:
uint32_t m_n;
double m_old_m, m_new_m, m_old_s, m_new_s, m_min, m_max;
};
// Linear algebra
@@ -117,7 +243,7 @@ namespace basisu
inline vec &set(const vec<OtherN, OtherT> &other)
{
uint32_t i;
if (static_cast<void *>(&other) == static_cast<void *>(this))
if ((const void *)(&other) == (const void *)(this))
return *this;
const uint32_t m = minimum(OtherN, N);
for (i = 0; i < m; i++)
@@ -357,6 +483,7 @@ namespace basisu
BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(job_pool);
public:
// num_threads is the TOTAL number of job pool threads, including the calling thread! So 2=1 new thread, 3=2 new threads, etc.
job_pool(uint32_t num_threads);
~job_pool();
@@ -419,7 +546,7 @@ namespace basisu
return *this;
}
};
class color_rgba
{
public:
@@ -439,6 +566,25 @@ namespace basisu
inline color_rgba()
{
static_assert(sizeof(*this) == 4, "sizeof(*this) != 4");
static_assert(sizeof(*this) == sizeof(basist::color32), "sizeof(*this) != sizeof(basist::color32)");
}
// Not too hot about this idea.
inline color_rgba(const basist::color32& other) :
r(other.r),
g(other.g),
b(other.b),
a(other.a)
{
}
color_rgba& operator= (const basist::color32& rhs)
{
r = rhs.r;
g = rhs.g;
b = rhs.b;
a = rhs.a;
return *this;
}
inline color_rgba(int y)
@@ -562,6 +708,14 @@ namespace basisu
inline int get_601_luma() const { return (19595U * m_comps[0] + 38470U * m_comps[1] + 7471U * m_comps[2] + 32768U) >> 16U; }
inline int get_709_luma() const { return (13938U * m_comps[0] + 46869U * m_comps[1] + 4729U * m_comps[2] + 32768U) >> 16U; }
inline int get_luma(bool luma_601) const { return luma_601 ? get_601_luma() : get_709_luma(); }
inline basist::color32 get_color32() const
{
return basist::color32(r, g, b, a);
}
static color_rgba comp_min(const color_rgba& a, const color_rgba& b) { return color_rgba(std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]), std::min(a[3], b[3])); }
static color_rgba comp_max(const color_rgba& a, const color_rgba& b) { return color_rgba(std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]), std::max(a[3], b[3])); }
};
typedef std::vector<color_rgba> color_rgba_vec;
@@ -621,6 +775,13 @@ namespace basisu
return color_distance(e1, e2, alpha);
}
static inline uint32_t color_distance_la(const color_rgba& a, const color_rgba& b)
{
const int dl = a.r - b.r;
const int da = a.a - b.a;
return dl * dl + da * da;
}
// String helpers
inline int string_find_right(const std::string& filename, char c)
@@ -1238,7 +1399,7 @@ namespace basisu
bool prep_split(const tsvq_node &node, TrainingVectorType &l_child_result, TrainingVectorType &r_child_result) const
{
const uint32_t N = TrainingVectorType::num_elements;
//const uint32_t N = TrainingVectorType::num_elements;
if (2 == node.m_training_vecs.size())
{
@@ -2634,9 +2795,13 @@ namespace basisu
};
// Image saving/loading/resampling
bool load_png(const char* pFilename, image& img);
inline bool load_png(const std::string &filename, image &img) { return load_png(filename.c_str(), img); }
// Currently loads .BMP or .PNG.
bool load_image(const char* pFilename, image& img);
inline bool load_image(const std::string &filename, image &img) { return load_image(filename.c_str(), img); }
enum
{
+337 -9
View File
@@ -1,5 +1,5 @@
// basis_etc.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -19,6 +19,228 @@
namespace basisu
{
const int8_t g_etc2_eac_tables[16][8] =
{
{ -3, -6, -9, -15, 2, 5, 8, 14 }, { -3, -7, -10, -13, 2, 6, 9, 12 }, { -2, -5, -8, -13, 1, 4, 7, 12 }, { -2, -4, -6, -13, 1, 3, 5, 12 },
{ -3, -6, -8, -12, 2, 5, 7, 11 }, { -3, -7, -9, -11, 2, 6, 8, 10 }, { -4, -7, -8, -11, 3, 6, 7, 10 }, { -3, -5, -8, -11, 2, 4, 7, 10 },
{ -2, -6, -8, -10, 1, 5, 7, 9 }, { -2, -5, -8, -10, 1, 4, 7, 9 }, { -2, -4, -8, -10, 1, 3, 7, 9 }, { -2, -5, -7, -10, 1, 4, 6, 9 },
{ -3, -4, -7, -10, 2, 3, 6, 9 }, { -1, -2, -3, -10, 0, 1, 2, 9 }, { -4, -6, -8, -9, 3, 5, 7, 8 }, { -3, -5, -7, -9, 2, 4, 6, 8 }
};
const int8_t g_etc2_eac_tables8[16][8] =
{
{ -24, -48, -72, -120, 16, 40, 64, 112 }, { -24,-56,-80,-104,16,48,72,96 }, { -16,-40,-64,-104,8,32,56,96 }, { -16,-32,-48,-104,8,24,40,96 },
{ -24,-48,-64,-96,16,40,56,88 }, { -24,-56,-72,-88,16,48,64,80 }, { -32,-56,-64,-88,24,48,56,80 }, { -24,-40,-64,-88,16,32,56,80 },
{ -16,-48,-64,-80,8,40,56,72 }, { -16,-40,-64,-80,8,32,56,72 }, { -16,-32,-64,-80,8,24,56,72 }, { -16,-40,-56,-80,8,32,48,72 },
{ -24,-32,-56,-80,16,24,48,72 }, { -8,-16,-24,-80,0,8,16,72 }, { -32,-48,-64,-72,24,40,56,64 }, { -24,-40,-56,-72,16,32,48,64 }
};
// Given an ETC1 diff/inten_table/selector, and an 8-bit desired color, this table encodes the best packed_color in the low byte, and the abs error in the high byte.
static uint16_t g_etc1_inverse_lookup[2 * 8 * 4][256]; // [ diff/inten_table/selector][desired_color ]
// g_color8_to_etc_block_config[color][table_index] = Supplies for each 8-bit color value a list of packed ETC1 diff/intensity table/selectors/packed_colors that map to that color.
// To pack: diff | (inten << 1) | (selector << 4) | (packed_c << 8)
static const uint16_t g_etc1_color8_to_etc_block_config_0_255[2][33] =
{
{ 0x0000, 0x0010, 0x0002, 0x0012, 0x0004, 0x0014, 0x0006, 0x0016, 0x0008, 0x0018, 0x000A, 0x001A, 0x000C, 0x001C, 0x000E, 0x001E, 0x0001, 0x0011, 0x0003, 0x0013, 0x0005, 0x0015, 0x0007, 0x0017, 0x0009, 0x0019, 0x000B, 0x001B, 0x000D, 0x001D, 0x000F, 0x001F, 0xFFFF },
{ 0x0F20, 0x0F30, 0x0E32, 0x0F22, 0x0E34, 0x0F24, 0x0D36, 0x0F26, 0x0C38, 0x0E28, 0x0B3A, 0x0E2A, 0x093C, 0x0E2C, 0x053E, 0x0D2E, 0x1E31, 0x1F21, 0x1D33, 0x1F23, 0x1C35, 0x1E25, 0x1A37, 0x1E27, 0x1839, 0x1D29, 0x163B, 0x1C2B, 0x133D, 0x1B2D, 0x093F, 0x1A2F, 0xFFFF },
};
// Really only [254][11].
static const uint16_t g_etc1_color8_to_etc_block_config_1_to_254[254][12] =
{
{ 0x021C, 0x0D0D, 0xFFFF }, { 0x0020, 0x0021, 0x0A0B, 0x061F, 0xFFFF }, { 0x0113, 0x0217, 0xFFFF }, { 0x0116, 0x031E, 0x0B0E, 0x0405, 0xFFFF }, { 0x0022, 0x0204, 0x050A, 0x0023, 0xFFFF }, { 0x0111, 0x0319, 0x0809, 0x170F, 0xFFFF }, {
0x0303, 0x0215, 0x0607, 0xFFFF }, { 0x0030, 0x0114, 0x0408, 0x0031, 0x0201, 0x051D, 0xFFFF }, { 0x0100, 0x0024, 0x0306, 0x0025, 0x041B, 0x0E0D, 0xFFFF }, { 0x021A, 0x0121, 0x0B0B, 0x071F, 0xFFFF }, { 0x0213, 0x0317, 0xFFFF }, { 0x0112,
0x0505, 0xFFFF }, { 0x0026, 0x070C, 0x0123, 0x0027, 0xFFFF }, { 0x0211, 0x0909, 0xFFFF }, { 0x0110, 0x0315, 0x0707, 0x0419, 0x180F, 0xFFFF }, { 0x0218, 0x0131, 0x0301, 0x0403, 0x061D, 0xFFFF }, { 0x0032, 0x0202, 0x0033, 0x0125, 0x051B,
0x0F0D, 0xFFFF }, { 0x0028, 0x031C, 0x0221, 0x0029, 0xFFFF }, { 0x0120, 0x0313, 0x0C0B, 0x081F, 0xFFFF }, { 0x0605, 0x0417, 0xFFFF }, { 0x0216, 0x041E, 0x0C0E, 0x0223, 0x0127, 0xFFFF }, { 0x0122, 0x0304, 0x060A, 0x0311, 0x0A09, 0xFFFF
}, { 0x0519, 0x190F, 0xFFFF }, { 0x002A, 0x0231, 0x0503, 0x0415, 0x0807, 0x002B, 0x071D, 0xFFFF }, { 0x0130, 0x0214, 0x0508, 0x0401, 0x0133, 0x0225, 0x061B, 0xFFFF }, { 0x0200, 0x0124, 0x0406, 0x0321, 0x0129, 0x100D, 0xFFFF }, { 0x031A,
0x0D0B, 0x091F, 0xFFFF }, { 0x0413, 0x0705, 0x0517, 0xFFFF }, { 0x0212, 0x0034, 0x0323, 0x0035, 0x0227, 0xFFFF }, { 0x0126, 0x080C, 0x0B09, 0xFFFF }, { 0x0411, 0x0619, 0x1A0F, 0xFFFF }, { 0x0210, 0x0331, 0x0603, 0x0515, 0x0907, 0x012B,
0xFFFF }, { 0x0318, 0x002C, 0x0501, 0x0233, 0x0325, 0x071B, 0x002D, 0x081D, 0xFFFF }, { 0x0132, 0x0302, 0x0229, 0x110D, 0xFFFF }, { 0x0128, 0x041C, 0x0421, 0x0E0B, 0x0A1F, 0xFFFF }, { 0x0220, 0x0513, 0x0617, 0xFFFF }, { 0x0135, 0x0805,
0x0327, 0xFFFF }, { 0x0316, 0x051E, 0x0D0E, 0x0423, 0xFFFF }, { 0x0222, 0x0404, 0x070A, 0x0511, 0x0719, 0x0C09, 0x1B0F, 0xFFFF }, { 0x0703, 0x0615, 0x0A07, 0x022B, 0xFFFF }, { 0x012A, 0x0431, 0x0601, 0x0333, 0x012D, 0x091D, 0xFFFF }, {
0x0230, 0x0314, 0x0036, 0x0608, 0x0425, 0x0037, 0x0329, 0x081B, 0x120D, 0xFFFF }, { 0x0300, 0x0224, 0x0506, 0x0521, 0x0F0B, 0x0B1F, 0xFFFF }, { 0x041A, 0x0613, 0x0717, 0xFFFF }, { 0x0235, 0x0905, 0xFFFF }, { 0x0312, 0x0134, 0x0523,
0x0427, 0xFFFF }, { 0x0226, 0x090C, 0x002E, 0x0611, 0x0D09, 0x002F, 0xFFFF }, { 0x0715, 0x0B07, 0x0819, 0x032B, 0x1C0F, 0xFFFF }, { 0x0310, 0x0531, 0x0701, 0x0803, 0x022D, 0x0A1D, 0xFFFF }, { 0x0418, 0x012C, 0x0433, 0x0525, 0x0137, 0x091B,
0x130D, 0xFFFF }, { 0x0232, 0x0402, 0x0621, 0x0429, 0xFFFF }, { 0x0228, 0x051C, 0x0713, 0x100B, 0x0C1F, 0xFFFF }, { 0x0320, 0x0335, 0x0A05, 0x0817, 0xFFFF }, { 0x0623, 0x0527, 0xFFFF }, { 0x0416, 0x061E, 0x0E0E, 0x0711, 0x0E09, 0x012F,
0xFFFF }, { 0x0322, 0x0504, 0x080A, 0x0919, 0x1D0F, 0xFFFF }, { 0x0631, 0x0903, 0x0815, 0x0C07, 0x042B, 0x032D, 0x0B1D, 0xFFFF }, { 0x022A, 0x0801, 0x0533, 0x0625, 0x0237, 0x0A1B, 0xFFFF }, { 0x0330, 0x0414, 0x0136, 0x0708, 0x0721, 0x0529,
0x140D, 0xFFFF }, { 0x0400, 0x0324, 0x0606, 0x0038, 0x0039, 0x110B, 0x0D1F, 0xFFFF }, { 0x051A, 0x0813, 0x0B05, 0x0917, 0xFFFF }, { 0x0723, 0x0435, 0x0627, 0xFFFF }, { 0x0412, 0x0234, 0x0F09, 0x022F, 0xFFFF }, { 0x0326, 0x0A0C, 0x012E,
0x0811, 0x0A19, 0x1E0F, 0xFFFF }, { 0x0731, 0x0A03, 0x0915, 0x0D07, 0x052B, 0xFFFF }, { 0x0410, 0x0901, 0x0633, 0x0725, 0x0337, 0x0B1B, 0x042D, 0x0C1D, 0xFFFF }, { 0x0518, 0x022C, 0x0629, 0x150D, 0xFFFF }, { 0x0332, 0x0502, 0x0821, 0x0139,
0x120B, 0x0E1F, 0xFFFF }, { 0x0328, 0x061C, 0x0913, 0x0A17, 0xFFFF }, { 0x0420, 0x0535, 0x0C05, 0x0727, 0xFFFF }, { 0x0823, 0x032F, 0xFFFF }, { 0x0516, 0x071E, 0x0F0E, 0x0911, 0x0B19, 0x1009, 0x1F0F, 0xFFFF }, { 0x0422, 0x0604, 0x090A,
0x0B03, 0x0A15, 0x0E07, 0x062B, 0xFFFF }, { 0x0831, 0x0A01, 0x0733, 0x052D, 0x0D1D, 0xFFFF }, { 0x032A, 0x0825, 0x0437, 0x0729, 0x0C1B, 0x160D, 0xFFFF }, { 0x0430, 0x0514, 0x0236, 0x0808, 0x0921, 0x0239, 0x130B, 0x0F1F, 0xFFFF }, { 0x0500,
0x0424, 0x0706, 0x0138, 0x0A13, 0x0B17, 0xFFFF }, { 0x061A, 0x0635, 0x0D05, 0xFFFF }, { 0x0923, 0x0827, 0xFFFF }, { 0x0512, 0x0334, 0x003A, 0x0A11, 0x1109, 0x003B, 0x042F, 0xFFFF }, { 0x0426, 0x0B0C, 0x022E, 0x0B15, 0x0F07, 0x0C19,
0x072B, 0xFFFF }, { 0x0931, 0x0B01, 0x0C03, 0x062D, 0x0E1D, 0xFFFF }, { 0x0510, 0x0833, 0x0925, 0x0537, 0x0D1B, 0x170D, 0xFFFF }, { 0x0618, 0x032C, 0x0A21, 0x0339, 0x0829, 0xFFFF }, { 0x0432, 0x0602, 0x0B13, 0x140B, 0x101F, 0xFFFF }, {
0x0428, 0x071C, 0x0735, 0x0E05, 0x0C17, 0xFFFF }, { 0x0520, 0x0A23, 0x0927, 0xFFFF }, { 0x0B11, 0x1209, 0x013B, 0x052F, 0xFFFF }, { 0x0616, 0x081E, 0x0D19, 0xFFFF }, { 0x0522, 0x0704, 0x0A0A, 0x0A31, 0x0D03, 0x0C15, 0x1007, 0x082B, 0x072D,
0x0F1D, 0xFFFF }, { 0x0C01, 0x0933, 0x0A25, 0x0637, 0x0E1B, 0xFFFF }, { 0x042A, 0x0B21, 0x0929, 0x180D, 0xFFFF }, { 0x0530, 0x0614, 0x0336, 0x0908, 0x0439, 0x150B, 0x111F, 0xFFFF }, { 0x0600, 0x0524, 0x0806, 0x0238, 0x0C13, 0x0F05,
0x0D17, 0xFFFF }, { 0x071A, 0x0B23, 0x0835, 0x0A27, 0xFFFF }, { 0x1309, 0x023B, 0x062F, 0xFFFF }, { 0x0612, 0x0434, 0x013A, 0x0C11, 0x0E19, 0xFFFF }, { 0x0526, 0x0C0C, 0x032E, 0x0B31, 0x0E03, 0x0D15, 0x1107, 0x092B, 0xFFFF }, { 0x0D01,
0x0A33, 0x0B25, 0x0737, 0x0F1B, 0x082D, 0x101D, 0xFFFF }, { 0x0610, 0x0A29, 0x190D, 0xFFFF }, { 0x0718, 0x042C, 0x0C21, 0x0539, 0x160B, 0x121F, 0xFFFF }, { 0x0532, 0x0702, 0x0D13, 0x0E17, 0xFFFF }, { 0x0528, 0x081C, 0x0935, 0x1005, 0x0B27,
0xFFFF }, { 0x0620, 0x0C23, 0x033B, 0x072F, 0xFFFF }, { 0x0D11, 0x0F19, 0x1409, 0xFFFF }, { 0x0716, 0x003C, 0x091E, 0x0F03, 0x0E15, 0x1207, 0x0A2B, 0x003D, 0xFFFF }, { 0x0622, 0x0804, 0x0B0A, 0x0C31, 0x0E01, 0x0B33, 0x092D, 0x111D,
0xFFFF }, { 0x0C25, 0x0837, 0x0B29, 0x101B, 0x1A0D, 0xFFFF }, { 0x052A, 0x0D21, 0x0639, 0x170B, 0x131F, 0xFFFF }, { 0x0630, 0x0714, 0x0436, 0x0A08, 0x0E13, 0x0F17, 0xFFFF }, { 0x0700, 0x0624, 0x0906, 0x0338, 0x0A35, 0x1105, 0xFFFF }, {
0x081A, 0x0D23, 0x0C27, 0xFFFF }, { 0x0E11, 0x1509, 0x043B, 0x082F, 0xFFFF }, { 0x0712, 0x0534, 0x023A, 0x0F15, 0x1307, 0x1019, 0x0B2B, 0x013D, 0xFFFF }, { 0x0626, 0x0D0C, 0x042E, 0x0D31, 0x0F01, 0x1003, 0x0A2D, 0x121D, 0xFFFF }, { 0x0C33,
0x0D25, 0x0937, 0x111B, 0x1B0D, 0xFFFF }, { 0x0710, 0x0E21, 0x0739, 0x0C29, 0xFFFF }, { 0x0818, 0x052C, 0x0F13, 0x180B, 0x141F, 0xFFFF }, { 0x0632, 0x0802, 0x0B35, 0x1205, 0x1017, 0xFFFF }, { 0x0628, 0x091C, 0x0E23, 0x0D27, 0xFFFF }, {
0x0720, 0x0F11, 0x1609, 0x053B, 0x092F, 0xFFFF }, { 0x1119, 0x023D, 0xFFFF }, { 0x0816, 0x013C, 0x0A1E, 0x0E31, 0x1103, 0x1015, 0x1407, 0x0C2B, 0x0B2D, 0x131D, 0xFFFF }, { 0x0722, 0x0904, 0x0C0A, 0x1001, 0x0D33, 0x0E25, 0x0A37, 0x121B,
0xFFFF }, { 0x0F21, 0x0D29, 0x1C0D, 0xFFFF }, { 0x062A, 0x0839, 0x190B, 0x151F, 0xFFFF }, { 0x0730, 0x0814, 0x0536, 0x0B08, 0x1013, 0x1305, 0x1117, 0xFFFF }, { 0x0800, 0x0724, 0x0A06, 0x0438, 0x0F23, 0x0C35, 0x0E27, 0xFFFF }, { 0x091A,
0x1709, 0x063B, 0x0A2F, 0xFFFF }, { 0x1011, 0x1219, 0x033D, 0xFFFF }, { 0x0812, 0x0634, 0x033A, 0x0F31, 0x1203, 0x1115, 0x1507, 0x0D2B, 0xFFFF }, { 0x0726, 0x0E0C, 0x052E, 0x1101, 0x0E33, 0x0F25, 0x0B37, 0x131B, 0x0C2D, 0x141D, 0xFFFF }, {
0x0E29, 0x1D0D, 0xFFFF }, { 0x0810, 0x1021, 0x0939, 0x1A0B, 0x161F, 0xFFFF }, { 0x0918, 0x062C, 0x1113, 0x1217, 0xFFFF }, { 0x0732, 0x0902, 0x0D35, 0x1405, 0x0F27, 0xFFFF }, { 0x0728, 0x0A1C, 0x1023, 0x073B, 0x0B2F, 0xFFFF }, { 0x0820,
0x1111, 0x1319, 0x1809, 0xFFFF }, { 0x1303, 0x1215, 0x1607, 0x0E2B, 0x043D, 0xFFFF }, { 0x0916, 0x023C, 0x0B1E, 0x1031, 0x1201, 0x0F33, 0x0D2D, 0x151D, 0xFFFF }, { 0x0822, 0x0A04, 0x0D0A, 0x1025, 0x0C37, 0x0F29, 0x141B, 0x1E0D, 0xFFFF }, {
0x1121, 0x0A39, 0x1B0B, 0x171F, 0xFFFF }, { 0x072A, 0x1213, 0x1317, 0xFFFF }, { 0x0830, 0x0914, 0x0636, 0x0C08, 0x0E35, 0x1505, 0xFFFF }, { 0x0900, 0x0824, 0x0B06, 0x0538, 0x1123, 0x1027, 0xFFFF }, { 0x0A1A, 0x1211, 0x1909, 0x083B, 0x0C2F,
0xFFFF }, { 0x1315, 0x1707, 0x1419, 0x0F2B, 0x053D, 0xFFFF }, { 0x0912, 0x0734, 0x043A, 0x1131, 0x1301, 0x1403, 0x0E2D, 0x161D, 0xFFFF }, { 0x0826, 0x0F0C, 0x062E, 0x1033, 0x1125, 0x0D37, 0x151B, 0x1F0D, 0xFFFF }, { 0x1221, 0x0B39, 0x1029,
0xFFFF }, { 0x0910, 0x1313, 0x1C0B, 0x181F, 0xFFFF }, { 0x0A18, 0x072C, 0x0F35, 0x1605, 0x1417, 0xFFFF }, { 0x0832, 0x0A02, 0x1223, 0x1127, 0xFFFF }, { 0x0828, 0x0B1C, 0x1311, 0x1A09, 0x093B, 0x0D2F, 0xFFFF }, { 0x0920, 0x1519, 0x063D,
0xFFFF }, { 0x1231, 0x1503, 0x1415, 0x1807, 0x102B, 0x0F2D, 0x171D, 0xFFFF }, { 0x0A16, 0x033C, 0x0C1E, 0x1401, 0x1133, 0x1225, 0x0E37, 0x161B, 0xFFFF }, { 0x0922, 0x0B04, 0x0E0A, 0x1321, 0x1129, 0xFFFF }, { 0x0C39, 0x1D0B, 0x191F, 0xFFFF
}, { 0x082A, 0x1413, 0x1705, 0x1517, 0xFFFF }, { 0x0930, 0x0A14, 0x0736, 0x0D08, 0x1323, 0x1035, 0x1227, 0xFFFF }, { 0x0A00, 0x0924, 0x0C06, 0x0638, 0x1B09, 0x0A3B, 0x0E2F, 0xFFFF }, { 0x0B1A, 0x1411, 0x1619, 0x073D, 0xFFFF }, { 0x1331,
0x1603, 0x1515, 0x1907, 0x112B, 0xFFFF }, { 0x0A12, 0x0834, 0x053A, 0x1501, 0x1233, 0x1325, 0x0F37, 0x171B, 0x102D, 0x181D, 0xFFFF }, { 0x0926, 0x072E, 0x1229, 0xFFFF }, { 0x1421, 0x0D39, 0x1E0B, 0x1A1F, 0xFFFF }, { 0x0A10, 0x1513,
0x1617, 0xFFFF }, { 0x0B18, 0x082C, 0x1135, 0x1805, 0x1327, 0xFFFF }, { 0x0932, 0x0B02, 0x1423, 0x0B3B, 0x0F2F, 0xFFFF }, { 0x0928, 0x0C1C, 0x1511, 0x1719, 0x1C09, 0xFFFF }, { 0x0A20, 0x1703, 0x1615, 0x1A07, 0x122B, 0x083D, 0xFFFF }, {
0x1431, 0x1601, 0x1333, 0x112D, 0x191D, 0xFFFF }, { 0x0B16, 0x043C, 0x0D1E, 0x1425, 0x1037, 0x1329, 0x181B, 0xFFFF }, { 0x0A22, 0x0C04, 0x0F0A, 0x1521, 0x0E39, 0x1F0B, 0x1B1F, 0xFFFF }, { 0x1613, 0x1717, 0xFFFF }, { 0x092A, 0x1235, 0x1905,
0xFFFF }, { 0x0A30, 0x0B14, 0x0836, 0x0E08, 0x1523, 0x1427, 0xFFFF }, { 0x0B00, 0x0A24, 0x0D06, 0x0738, 0x1611, 0x1D09, 0x0C3B, 0x102F, 0xFFFF }, { 0x0C1A, 0x1715, 0x1B07, 0x1819, 0x132B, 0x093D, 0xFFFF }, { 0x1531, 0x1701, 0x1803, 0x122D,
0x1A1D, 0xFFFF }, { 0x0B12, 0x0934, 0x063A, 0x1433, 0x1525, 0x1137, 0x191B, 0xFFFF }, { 0x0A26, 0x003E, 0x082E, 0x1621, 0x0F39, 0x1429, 0x003F, 0xFFFF }, { 0x1713, 0x1C1F, 0xFFFF }, { 0x0B10, 0x1335, 0x1A05, 0x1817, 0xFFFF }, { 0x0C18,
0x092C, 0x1623, 0x1527, 0xFFFF }, { 0x0A32, 0x0C02, 0x1711, 0x1E09, 0x0D3B, 0x112F, 0xFFFF }, { 0x0A28, 0x0D1C, 0x1919, 0x0A3D, 0xFFFF }, { 0x0B20, 0x1631, 0x1903, 0x1815, 0x1C07, 0x142B, 0x132D, 0x1B1D, 0xFFFF }, { 0x1801, 0x1533, 0x1625,
0x1237, 0x1A1B, 0xFFFF }, { 0x0C16, 0x053C, 0x0E1E, 0x1721, 0x1529, 0x013F, 0xFFFF }, { 0x0B22, 0x0D04, 0x1039, 0x1D1F, 0xFFFF }, { 0x1813, 0x1B05, 0x1917, 0xFFFF }, { 0x0A2A, 0x1723, 0x1435, 0x1627, 0xFFFF }, { 0x0B30, 0x0C14, 0x0936,
0x0F08, 0x1F09, 0x0E3B, 0x122F, 0xFFFF }, { 0x0C00, 0x0B24, 0x0E06, 0x0838, 0x1811, 0x1A19, 0x0B3D, 0xFFFF }, { 0x0D1A, 0x1731, 0x1A03, 0x1915, 0x1D07, 0x152B, 0xFFFF }, { 0x1901, 0x1633, 0x1725, 0x1337, 0x1B1B, 0x142D, 0x1C1D, 0xFFFF }, {
0x0C12, 0x0A34, 0x073A, 0x1629, 0x023F, 0xFFFF }, { 0x0B26, 0x013E, 0x092E, 0x1821, 0x1139, 0x1E1F, 0xFFFF }, { 0x1913, 0x1A17, 0xFFFF }, { 0x0C10, 0x1535, 0x1C05, 0x1727, 0xFFFF }, { 0x0D18, 0x0A2C, 0x1823, 0x0F3B, 0x132F, 0xFFFF }, {
0x0B32, 0x0D02, 0x1911, 0x1B19, 0xFFFF }, { 0x0B28, 0x0E1C, 0x1B03, 0x1A15, 0x1E07, 0x162B, 0x0C3D, 0xFFFF }, { 0x0C20, 0x1831, 0x1A01, 0x1733, 0x152D, 0x1D1D, 0xFFFF }, { 0x1825, 0x1437, 0x1729, 0x1C1B, 0x033F, 0xFFFF }, { 0x0D16, 0x063C,
0x0F1E, 0x1921, 0x1239, 0x1F1F, 0xFFFF }, { 0x0C22, 0x0E04, 0x1A13, 0x1B17, 0xFFFF }, { 0x1635, 0x1D05, 0xFFFF }, { 0x0B2A, 0x1923, 0x1827, 0xFFFF }, { 0x0C30, 0x0D14, 0x0A36, 0x1A11, 0x103B, 0x142F, 0xFFFF }, { 0x0D00, 0x0C24, 0x0F06,
0x0938, 0x1B15, 0x1F07, 0x1C19, 0x172B, 0x0D3D, 0xFFFF }, { 0x0E1A, 0x1931, 0x1B01, 0x1C03, 0x162D, 0x1E1D, 0xFFFF }, { 0x1833, 0x1925, 0x1537, 0x1D1B, 0xFFFF }, { 0x0D12, 0x0B34, 0x083A, 0x1A21, 0x1339, 0x1829, 0x043F, 0xFFFF }, { 0x0C26,
0x023E, 0x0A2E, 0x1B13, 0xFFFF }, { 0x1735, 0x1E05, 0x1C17, 0xFFFF }, { 0x0D10, 0x1A23, 0x1927, 0xFFFF }, { 0x0E18, 0x0B2C, 0x1B11, 0x113B, 0x152F, 0xFFFF }, { 0x0C32, 0x0E02, 0x1D19, 0x0E3D, 0xFFFF }, { 0x0C28, 0x0F1C, 0x1A31, 0x1D03,
0x1C15, 0x182B, 0x172D, 0x1F1D, 0xFFFF }, { 0x0D20, 0x1C01, 0x1933, 0x1A25, 0x1637, 0x1E1B, 0xFFFF }, { 0x1B21, 0x1929, 0x053F, 0xFFFF }, { 0x0E16, 0x073C, 0x1439, 0xFFFF }, { 0x0D22, 0x0F04, 0x1C13, 0x1F05, 0x1D17, 0xFFFF }, { 0x1B23,
0x1835, 0x1A27, 0xFFFF }, { 0x0C2A, 0x123B, 0x162F, 0xFFFF }, { 0x0D30, 0x0E14, 0x0B36, 0x1C11, 0x1E19, 0x0F3D, 0xFFFF }, { 0x0E00, 0x0D24, 0x0A38, 0x1B31, 0x1E03, 0x1D15, 0x192B, 0xFFFF }, { 0x0F1A, 0x1D01, 0x1A33, 0x1B25, 0x1737, 0x1F1B,
0x182D, 0xFFFF }, { 0x1A29, 0x063F, 0xFFFF }, { 0x0E12, 0x0C34, 0x093A, 0x1C21, 0x1539, 0xFFFF }, { 0x0D26, 0x033E, 0x0B2E, 0x1D13, 0x1E17, 0xFFFF }, { 0x1935, 0x1B27, 0xFFFF }, { 0x0E10, 0x1C23, 0x133B, 0x172F, 0xFFFF }, { 0x0F18,
0x0C2C, 0x1D11, 0x1F19, 0xFFFF }, { 0x0D32, 0x0F02, 0x1F03, 0x1E15, 0x1A2B, 0x103D, 0xFFFF }, { 0x0D28, 0x1C31, 0x1E01, 0x1B33, 0x192D, 0xFFFF }, { 0x0E20, 0x1C25, 0x1837, 0x1B29, 0x073F, 0xFFFF }, { 0x1D21, 0x1639, 0xFFFF }, { 0x0F16,
0x083C, 0x1E13, 0x1F17, 0xFFFF }, { 0x0E22, 0x1A35, 0xFFFF }, { 0x1D23, 0x1C27, 0xFFFF }, { 0x0D2A, 0x1E11, 0x143B, 0x182F, 0xFFFF }, { 0x0E30, 0x0F14, 0x0C36, 0x1F15, 0x1B2B, 0x113D, 0xFFFF }, { 0x0F00, 0x0E24, 0x0B38, 0x1D31, 0x1F01,
0x1A2D, 0xFFFF }, { 0x1C33, 0x1D25, 0x1937, 0xFFFF }, { 0x1E21, 0x1739, 0x1C29, 0x083F, 0xFFFF }, { 0x0F12, 0x0D34, 0x0A3A, 0x1F13, 0xFFFF }, { 0x0E26, 0x043E, 0x0C2E, 0x1B35, 0xFFFF }, { 0x1E23, 0x1D27, 0xFFFF }, { 0x0F10, 0x1F11, 0x153B, 0x192F, 0xFFFF }, { 0x0D2C, 0x123D, 0xFFFF },
};
static uint32_t etc1_decode_value(uint32_t diff, uint32_t inten, uint32_t selector, uint32_t packed_c)
{
const uint32_t limit = diff ? 32 : 16;
BASISU_NOTE_UNUSED(limit);
assert((diff < 2) && (inten < 8) && (selector < 4) && (packed_c < limit));
int c;
if (diff)
c = (packed_c >> 2) | (packed_c << 3);
else
c = packed_c | (packed_c << 4);
c += g_etc1_inten_tables[inten][selector];
c = clamp<int>(c, 0, 255);
return c;
}
void pack_etc1_solid_color_init()
{
for (uint32_t diff = 0; diff < 2; diff++)
{
const uint32_t limit = diff ? 32 : 16;
for (uint32_t inten = 0; inten < 8; inten++)
{
for (uint32_t selector = 0; selector < 4; selector++)
{
const uint32_t inverse_table_index = diff + (inten << 1) + (selector << 4);
for (uint32_t color = 0; color < 256; color++)
{
uint32_t best_error = UINT32_MAX, best_packed_c = 0;
for (uint32_t packed_c = 0; packed_c < limit; packed_c++)
{
int v = etc1_decode_value(diff, inten, selector, packed_c);
uint32_t err = labs(v - static_cast<int>(color));
if (err < best_error)
{
best_error = err;
best_packed_c = packed_c;
if (!best_error)
break;
}
}
assert(best_error <= 255);
g_etc1_inverse_lookup[inverse_table_index][color] = static_cast<uint16_t>(best_packed_c | (best_error << 8));
}
}
}
}
}
// Packs solid color blocks efficiently using a set of small precomputed tables.
// For random 888 inputs, MSE results are better than Erricson's ETC1 packer in "slow" mode ~9.5% of the time, is slightly worse only ~.01% of the time, and is equal the rest of the time.
uint64_t pack_etc1_block_solid_color(etc_block& block, const uint8_t* pColor)
{
assert(g_etc1_inverse_lookup[0][255]);
static uint32_t s_next_comp[4] = { 1, 2, 0, 1 };
uint32_t best_error = UINT32_MAX, best_i = 0;
int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;
// For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error.
for (uint32_t i = 0; i < 3; i++)
{
const uint32_t c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];
const int delta_range = 1;
for (int delta = -delta_range; delta <= delta_range; delta++)
{
const int c_plus_delta = clamp<int>(pColor[i] + delta, 0, 255);
const uint16_t* pTable;
if (!c_plus_delta)
pTable = g_etc1_color8_to_etc_block_config_0_255[0];
else if (c_plus_delta == 255)
pTable = g_etc1_color8_to_etc_block_config_0_255[1];
else
pTable = g_etc1_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];
do
{
const uint32_t x = *pTable++;
#ifdef _DEBUG
const uint32_t diff = x & 1;
const uint32_t inten = (x >> 1) & 7;
const uint32_t selector = (x >> 4) & 3;
const uint32_t p0 = (x >> 8) & 255;
assert(etc1_decode_value(diff, inten, selector, p0) == (uint32_t)c_plus_delta);
#endif
const uint16_t* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];
uint16_t p1 = pInverse_table[c1];
uint16_t p2 = pInverse_table[c2];
const uint32_t trial_error = square(c_plus_delta - pColor[i]) + square(p1 >> 8) + square(p2 >> 8);
if (trial_error < best_error)
{
best_error = trial_error;
best_x = x;
best_packed_c1 = p1 & 0xFF;
best_packed_c2 = p2 & 0xFF;
best_i = i;
if (!best_error)
goto found_perfect_match;
}
} while (*pTable != 0xFFFF);
}
}
found_perfect_match:
const uint32_t diff = best_x & 1;
const uint32_t inten = (best_x >> 1) & 7;
block.m_bytes[3] = static_cast<uint8_t>(((inten | (inten << 3)) << 2) | (diff << 1));
const uint32_t etc1_selector = g_selector_index_to_etc1[(best_x >> 4) & 3];
*reinterpret_cast<uint16_t*>(&block.m_bytes[4]) = (etc1_selector & 2) ? 0xFFFF : 0;
*reinterpret_cast<uint16_t*>(&block.m_bytes[6]) = (etc1_selector & 1) ? 0xFFFF : 0;
const uint32_t best_packed_c0 = (best_x >> 8) & 255;
if (diff)
{
block.m_bytes[best_i] = static_cast<uint8_t>(best_packed_c0 << 3);
block.m_bytes[s_next_comp[best_i]] = static_cast<uint8_t>(best_packed_c1 << 3);
block.m_bytes[s_next_comp[best_i + 1]] = static_cast<uint8_t>(best_packed_c2 << 3);
}
else
{
block.m_bytes[best_i] = static_cast<uint8_t>(best_packed_c0 | (best_packed_c0 << 4));
block.m_bytes[s_next_comp[best_i]] = static_cast<uint8_t>(best_packed_c1 | (best_packed_c1 << 4));
block.m_bytes[s_next_comp[best_i + 1]] = static_cast<uint8_t>(best_packed_c2 | (best_packed_c2 << 4));
}
return best_error;
}
const uint32_t BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE = 165;
static const struct { uint8_t m_v[4]; } g_cluster_fit_order_tab[BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE] =
@@ -638,9 +860,9 @@ namespace basisu
const float avg_delta_r_f = static_cast<float>(delta_sum_r) / n;
const float avg_delta_g_f = static_cast<float>(delta_sum_g) / n;
const float avg_delta_b_f = static_cast<float>(delta_sum_b) / n;
const int br1 = clamp<int>(static_cast<uint32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bg1 = clamp<int>(static_cast<uint32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bb1 = clamp<int>(static_cast<uint32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int br1 = clamp<int>(static_cast<int32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bg1 = clamp<int>(static_cast<int32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bb1 = clamp<int>(static_cast<int32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
#if BASISU_DEBUG_ETC_ENCODER_DEEPER
printf("Refinement trial %u, avg_delta %f %f %f\n", refinement_trial, avg_delta_r_f, avg_delta_g_f, avg_delta_b_f);
@@ -657,7 +879,7 @@ namespace basisu
if (m_best_solution.m_error == 0)
return;
const uint32_t n = m_pParams->m_num_src_pixels;
//const uint32_t n = m_pParams->m_num_src_pixels;
const int scan_delta_size = m_pParams->m_scan_delta_size;
// Scan through a subset of the 3D lattice centered around the avg block color trying each 3D (555 or 444) lattice point as a potential block color.
@@ -730,9 +952,9 @@ namespace basisu
const float avg_delta_g_f = static_cast<float>(delta_sum_g) / 8;
const float avg_delta_b_f = static_cast<float>(delta_sum_b) / 8;
const int br1 = clamp<int>(static_cast<uint32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bg1 = clamp<int>(static_cast<uint32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bb1 = clamp<int>(static_cast<uint32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int br1 = clamp<int>(static_cast<int32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bg1 = clamp<int>(static_cast<int32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
const int bb1 = clamp<int>(static_cast<int32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
#if BASISU_DEBUG_ETC_ENCODER_DEEPER
printf("Second refinement trial %u, avg_delta %f %f %f\n", i, avg_delta_r_f, avg_delta_g_f, avg_delta_b_f);
@@ -982,7 +1204,7 @@ namespace basisu
block_inten[s] = block_color.r + block_color.g + block_color.b;
}
// evaluate_solution_fast() enforces/assumesd a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors.
// evaluate_solution_fast() enforces/assumes a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors.
// The inputs colors have been presorted along the projection onto this axis, and ETC1 block colors are always ordered along the intensity axis, so this classification is fast.
// 0 1 2 3
// 01 12 23
@@ -1071,4 +1293,110 @@ namespace basisu
return success;
}
uint64_t pack_eac_a8(pack_eac_a8_results& results, const uint8_t* pPixels, uint32_t num_pixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask)
{
results.m_selectors.resize(num_pixels);
results.m_selectors_temp.resize(num_pixels);
uint32_t min_alpha = 255, max_alpha = 0;
for (uint32_t i = 0; i < num_pixels; i++)
{
const uint32_t a = pPixels[i];
if (a < min_alpha) min_alpha = a;
if (a > max_alpha) max_alpha = a;
}
if (min_alpha == max_alpha)
{
results.m_base = min_alpha;
results.m_table = 13;
results.m_multiplier = 1;
for (uint32_t i = 0; i < num_pixels; i++)
results.m_selectors[i] = 4;
return 0;
}
const uint32_t alpha_range = max_alpha - min_alpha;
uint64_t best_err = UINT64_MAX;
for (uint32_t table = 0; table < 16; table++)
{
if ((table_mask & (1U << table)) == 0)
continue;
const float range = (float)(g_etc2_eac_tables[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]);
const int center = (int)roundf(lerp((float)min_alpha, (float)max_alpha, (float)(0 - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range));
const int base_min = clamp255(center - base_search_rad);
const int base_max = clamp255(center + base_search_rad);
const int mul = (int)roundf(alpha_range / range);
const int mul_low = clamp<int>(mul - mul_search_rad, 1, 15);
const int mul_high = clamp<int>(mul + mul_search_rad, 1, 15);
for (int base = base_min; base <= base_max; base++)
{
for (int multiplier = mul_low; multiplier <= mul_high; multiplier++)
{
uint64_t total_err = 0;
for (uint32_t i = 0; i < num_pixels; i++)
{
const int a = pPixels[i];
uint32_t best_s_err = UINT32_MAX;
uint32_t best_s = 0;
for (uint32_t s = 0; s < 8; s++)
{
const int v = clamp255((int)multiplier * g_etc2_eac_tables[table][s] + (int)base);
uint32_t err = iabs(a - v);
if (err < best_s_err)
{
best_s_err = err;
best_s = s;
}
}
results.m_selectors_temp[i] = static_cast<uint8_t>(best_s);
total_err += best_s_err * best_s_err;
if (total_err >= best_err)
break;
}
if (total_err < best_err)
{
best_err = total_err;
results.m_base = base;
results.m_multiplier = multiplier;
results.m_table = table;
results.m_selectors.swap(results.m_selectors_temp);
if (!best_err)
return best_err;
}
} // table
} // multiplier
} // base
return best_err;
}
void pack_eac_a8(eac_a8_block* pBlock, const uint8_t* pPixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask)
{
pack_eac_a8_results results;
pack_eac_a8(results, pPixels, 16, base_search_rad, mul_search_rad, table_mask);
pBlock->m_base = results.m_base;
pBlock->m_multiplier = results.m_multiplier;
pBlock->m_table = results.m_table;
for (uint32_t y = 0; y < 4; y++)
for (uint32_t x = 0; x < 4; x++)
pBlock->set_selector(x, y, results.m_selectors[x + y * 4]);
}
} // namespace basisu
+111 -1
View File
@@ -1,5 +1,5 @@
// basis_etc.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -266,6 +266,27 @@ namespace basisu
p[-2] |= (msb << byte_bit_ofs);
}
// Selector "etc1_val" ranges from 0-3 and is a direct (raw) ETC1 selector.
inline void set_raw_selector(uint32_t x, uint32_t y, uint32_t etc1_val)
{
assert((x | y | etc1_val) < 4);
const uint32_t bit_index = x * 4 + y;
uint8_t* p = &m_bytes[7 - (bit_index >> 3)];
const uint32_t byte_bit_ofs = bit_index & 7;
const uint32_t mask = 1 << byte_bit_ofs;
const uint32_t lsb = etc1_val & 1;
const uint32_t msb = etc1_val >> 1;
p[0] &= ~mask;
p[0] |= (lsb << byte_bit_ofs);
p[-2] &= ~mask;
p[-2] |= (msb << byte_bit_ofs);
}
inline uint32_t get_raw_selector_bits() const
{
return m_bytes[4] | (m_bytes[5] << 8) | (m_bytes[6] << 16) | (m_bytes[7] << 24);
@@ -622,6 +643,23 @@ namespace basisu
return true;
}
bool set_block_color5_clamp(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
{
set_diff_bit(true);
set_base5_color(pack_color5(c0_unscaled, false));
int dr = c1_unscaled.r - c0_unscaled.r;
int dg = c1_unscaled.g - c0_unscaled.g;
int db = c1_unscaled.b - c0_unscaled.b;
dr = clamp<int>(dr, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
dg = clamp<int>(dg, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
db = clamp<int>(db, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
set_delta3_color(pack_delta3(dr, dg, db));
return true;
}
color_rgba get_selector_color(uint32_t x, uint32_t y, uint32_t s) const
{
color_rgba block_colors[4];
@@ -1042,5 +1080,77 @@ namespace basisu
{
etc1_optimizer m_optimizer;
};
void pack_etc1_solid_color_init();
uint64_t pack_etc1_block_solid_color(etc_block& block, const uint8_t* pColor);
// ETC EAC
extern const int8_t g_etc2_eac_tables[16][8];
extern const int8_t g_etc2_eac_tables8[16][8];
const uint32_t ETC2_EAC_MIN_VALUE_SELECTOR = 3, ETC2_EAC_MAX_VALUE_SELECTOR = 7;
struct eac_a8_block
{
uint16_t m_base : 8;
uint16_t m_table : 4;
uint16_t m_multiplier : 4;
uint8_t m_selectors[6];
inline uint32_t get_selector(uint32_t x, uint32_t y, uint64_t selector_bits) const
{
assert((x < 4) && (y < 4));
return static_cast<uint32_t>((selector_bits >> (45 - (y + x * 4) * 3)) & 7);
}
inline uint64_t get_selector_bits() const
{
uint64_t pixels = ((uint64_t)m_selectors[0] << 40) | ((uint64_t)m_selectors[1] << 32) | ((uint64_t)m_selectors[2] << 24) | ((uint64_t)m_selectors[3] << 16) | ((uint64_t)m_selectors[4] << 8) | m_selectors[5];
return pixels;
}
inline void set_selector_bits(uint64_t pixels)
{
m_selectors[0] = (uint8_t)(pixels >> 40);
m_selectors[1] = (uint8_t)(pixels >> 32);
m_selectors[2] = (uint8_t)(pixels >> 24);
m_selectors[3] = (uint8_t)(pixels >> 16);
m_selectors[4] = (uint8_t)(pixels >> 8);
m_selectors[5] = (uint8_t)(pixels);
}
void set_selector(uint32_t x, uint32_t y, uint32_t s)
{
assert((x < 4) && (y < 4) && (s < 8));
const uint32_t ofs = 45 - (y + x * 4) * 3;
uint64_t pixels = get_selector_bits();
pixels &= ~(7ULL << ofs);
pixels |= (static_cast<uint64_t>(s) << ofs);
set_selector_bits(pixels);
}
};
struct etc2_rgba_block
{
eac_a8_block m_alpha;
etc_block m_rgb;
};
struct pack_eac_a8_results
{
uint32_t m_base;
uint32_t m_table;
uint32_t m_multiplier;
uint8_vec m_selectors;
uint8_vec m_selectors_temp;
};
uint64_t pack_eac_a8(pack_eac_a8_results& results, const uint8_t* pPixels, uint32_t num_pixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask = UINT32_MAX);
void pack_eac_a8(eac_a8_block* pBlock, const uint8_t* pPixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask = UINT32_MAX);
} // namespace basisu
+5 -5
View File
@@ -1,5 +1,5 @@
// basisu_frontend.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ namespace basisu
const uint32_t cMaxCodebookCreationThreads = 8;
const uint32_t BASISU_MAX_ENDPOINT_REFINEMENT_STEPS = 3;
const uint32_t BASISU_MAX_SELECTOR_REFINEMENT_STEPS = 3;
//const uint32_t BASISU_MAX_SELECTOR_REFINEMENT_STEPS = 3;
const uint32_t BASISU_ENDPOINT_PARENT_CODEBOOK_SIZE = 16;
const uint32_t BASISU_SELECTOR_PARENT_CODEBOOK_SIZE = 16;
@@ -837,7 +837,7 @@ namespace basisu
continue;
#endif
const uint32_t new_endpoint_cluster_index = (uint32_t)m_endpoint_clusters.size();
//const uint32_t new_endpoint_cluster_index = (uint32_t)m_endpoint_clusters.size();
enlarge_vector(m_endpoint_clusters, 1)->push_back(training_vector_index);
enlarge_vector(m_endpoint_cluster_etc_params, 1);
@@ -1085,7 +1085,7 @@ namespace basisu
for (uint32_t block_index = first_index; block_index < last_index; block_index++)
{
const bool is_flipped = true;
//const bool is_flipped = true;
const uint32_t cluster_index = block_clusters[block_index][0];
BASISU_FRONTEND_VERIFY(cluster_index == block_clusters[block_index][1]);
@@ -1938,7 +1938,7 @@ namespace basisu
const uint32_t block_index = training_vector_index >> 1;
const uint32_t subblock_index = training_vector_index & 1;
const bool is_flipped = true;
//const bool is_flipped = true;
etc_block &blk = m_encoded_blocks[block_index];
+6 -4
View File
@@ -1,5 +1,5 @@
// basisu_frontend.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -72,16 +72,18 @@ namespace basisu
m_perceptual(true),
m_debug_stats(false),
m_debug_images(false),
m_dump_endpoint_clusterization(true),
m_validate(false),
m_multithreaded(false),
m_disable_hierarchical_endpoint_codebooks(false),
m_pGlobal_sel_codebook(NULL),
m_num_global_sel_codebook_pal_bits(0),
m_num_global_sel_codebook_mod_bits(0),
m_use_hybrid_selector_codebooks(false),
m_hybrid_codebook_quality_thresh(0.0f),
m_validate(false),
m_tex_type(basist::cBASISTexType2D),
m_multithreaded(false),
m_disable_hierarchical_endpoint_codebooks(false),
m_pJob_pool(nullptr)
{
}
+243 -219
View File
@@ -1,5 +1,5 @@
// basisu_gpu_texture.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -16,38 +16,10 @@
#include "basisu_enc.h"
#include "basisu_pvrtc1_4.h"
#include "basisu_astc_decomp.h"
#include "basisu_bc7enc.h"
namespace basisu
{
const int8_t g_etc2_eac_tables[16][8] =
{
{ -3, -6, -9, -15, 2, 5, 8, 14 }, { -3, -7, -10, -13, 2, 6, 9, 12 }, { -2, -5, -8, -13, 1, 4, 7, 12 }, { -2, -4, -6, -13, 1, 3, 5, 12 },
{ -3, -6, -8, -12, 2, 5, 7, 11 }, { -3, -7, -9, -11, 2, 6, 8, 10 }, { -4, -7, -8, -11, 3, 6, 7, 10 }, { -3, -5, -8, -11, 2, 4, 7, 10 },
{ -2, -6, -8, -10, 1, 5, 7, 9 }, { -2, -5, -8, -10, 1, 4, 7, 9 }, { -2, -4, -8, -10, 1, 3, 7, 9 }, { -2, -5, -7, -10, 1, 4, 6, 9 },
{ -3, -4, -7, -10, 2, 3, 6, 9 }, { -1, -2, -3, -10, 0, 1, 2, 9 }, { -4, -6, -8, -9, 3, 5, 7, 8 }, { -3, -5, -7, -9, 2, 4, 6, 8 }
};
struct eac_a8_block
{
uint16_t m_base : 8;
uint16_t m_table : 4;
uint16_t m_multiplier : 4;
uint8_t m_selectors[6];
inline uint32_t get_selector(uint32_t x, uint32_t y, uint64_t selector_bits) const
{
assert((x < 4) && (y < 4));
return static_cast<uint32_t>((selector_bits >> (45 - (y + x * 4) * 3)) & 7);
}
inline uint64_t get_selector_bits() const
{
uint64_t pixels = ((uint64_t)m_selectors[0] << 40) | ((uint64_t)m_selectors[1] << 32) | ((uint64_t)m_selectors[2] << 24) | ((uint64_t)m_selectors[3] << 16) | ((uint64_t)m_selectors[4] << 8) | m_selectors[5];
return pixels;
}
};
void unpack_etc2_eac(const void *pBlock_bits, color_rgba *pPixels)
{
static_assert(sizeof(eac_a8_block) == 8, "sizeof(eac_a8_block) == 8");
@@ -317,6 +289,191 @@ namespace basisu
}
}
// BC7 mode 0-7 decompression.
// Instead of one monster routine to unpack all the BC7 modes, we're lumping the 3 subset, 2 subset, 1 subset, and dual plane modes together into simple shared routines.
static inline uint32_t bc7_dequant(uint32_t val, uint32_t pbit, uint32_t val_bits) { assert(val < (1U << val_bits)); assert(pbit < 2); assert(val_bits >= 4 && val_bits <= 8); const uint32_t total_bits = val_bits + 1; val = (val << 1) | pbit; val <<= (8 - total_bits); val |= (val >> total_bits); assert(val <= 255); return val; }
static inline uint32_t bc7_dequant(uint32_t val, uint32_t val_bits) { assert(val < (1U << val_bits)); assert(val_bits >= 4 && val_bits <= 8); val <<= (8 - val_bits); val |= (val >> val_bits); assert(val <= 255); return val; }
static inline uint32_t bc7_interp2(uint32_t l, uint32_t h, uint32_t w) { assert(w < 4); return (l * (64 - basist::g_bc7_weights2[w]) + h * basist::g_bc7_weights2[w] + 32) >> 6; }
static inline uint32_t bc7_interp3(uint32_t l, uint32_t h, uint32_t w) { assert(w < 8); return (l * (64 - basist::g_bc7_weights3[w]) + h * basist::g_bc7_weights3[w] + 32) >> 6; }
static inline uint32_t bc7_interp4(uint32_t l, uint32_t h, uint32_t w) { assert(w < 16); return (l * (64 - basist::g_bc7_weights4[w]) + h * basist::g_bc7_weights4[w] + 32) >> 6; }
static inline uint32_t bc7_interp(uint32_t l, uint32_t h, uint32_t w, uint32_t bits)
{
assert(l <= 255 && h <= 255);
switch (bits)
{
case 2: return bc7_interp2(l, h, w);
case 3: return bc7_interp3(l, h, w);
case 4: return bc7_interp4(l, h, w);
default:
break;
}
return 0;
}
bool unpack_bc7_mode0_2(uint32_t mode, const void* pBlock_bits, color_rgba* pPixels)
{
//const uint32_t SUBSETS = 3;
const uint32_t ENDPOINTS = 6;
const uint32_t COMPS = 3;
const uint32_t WEIGHT_BITS = (mode == 0) ? 3 : 2;
const uint32_t ENDPOINT_BITS = (mode == 0) ? 4 : 5;
const uint32_t PBITS = (mode == 0) ? 6 : 0;
const uint32_t WEIGHT_VALS = 1 << WEIGHT_BITS;
uint32_t bit_offset = 0;
const uint8_t* pBuf = static_cast<const uint8_t*>(pBlock_bits);
if (read_bits32(pBuf, bit_offset, mode + 1) != (1U << mode)) return false;
const uint32_t part = read_bits32(pBuf, bit_offset, (mode == 0) ? 4 : 6);
color_rgba endpoints[ENDPOINTS];
for (uint32_t c = 0; c < COMPS; c++)
for (uint32_t e = 0; e < ENDPOINTS; e++)
endpoints[e][c] = (uint8_t)read_bits32(pBuf, bit_offset, ENDPOINT_BITS);
uint32_t pbits[6];
for (uint32_t p = 0; p < PBITS; p++)
pbits[p] = read_bits32(pBuf, bit_offset, 1);
uint32_t weights[16];
for (uint32_t i = 0; i < 16; i++)
weights[i] = read_bits32(pBuf, bit_offset, ((!i) || (i == basist::g_bc7_table_anchor_index_third_subset_1[part]) || (i == basist::g_bc7_table_anchor_index_third_subset_2[part])) ? (WEIGHT_BITS - 1) : WEIGHT_BITS);
assert(bit_offset == 128);
for (uint32_t e = 0; e < ENDPOINTS; e++)
for (uint32_t c = 0; c < 4; c++)
endpoints[e][c] = (uint8_t)((c == 3) ? 255 : (PBITS ? bc7_dequant(endpoints[e][c], pbits[e], ENDPOINT_BITS) : bc7_dequant(endpoints[e][c], ENDPOINT_BITS)));
color_rgba block_colors[3][8];
for (uint32_t s = 0; s < 3; s++)
for (uint32_t i = 0; i < WEIGHT_VALS; i++)
{
for (uint32_t c = 0; c < 3; c++)
block_colors[s][i][c] = (uint8_t)bc7_interp(endpoints[s * 2 + 0][c], endpoints[s * 2 + 1][c], i, WEIGHT_BITS);
block_colors[s][i][3] = 255;
}
for (uint32_t i = 0; i < 16; i++)
pPixels[i] = block_colors[basist::g_bc7_partition3[part * 16 + i]][weights[i]];
return true;
}
bool unpack_bc7_mode1_3_7(uint32_t mode, const void* pBlock_bits, color_rgba* pPixels)
{
//const uint32_t SUBSETS = 2;
const uint32_t ENDPOINTS = 4;
const uint32_t COMPS = (mode == 7) ? 4 : 3;
const uint32_t WEIGHT_BITS = (mode == 1) ? 3 : 2;
const uint32_t ENDPOINT_BITS = (mode == 7) ? 5 : ((mode == 1) ? 6 : 7);
const uint32_t PBITS = (mode == 1) ? 2 : 4;
const uint32_t SHARED_PBITS = (mode == 1) ? true : false;
const uint32_t WEIGHT_VALS = 1 << WEIGHT_BITS;
uint32_t bit_offset = 0;
const uint8_t* pBuf = static_cast<const uint8_t*>(pBlock_bits);
if (read_bits32(pBuf, bit_offset, mode + 1) != (1U << mode)) return false;
const uint32_t part = read_bits32(pBuf, bit_offset, 6);
color_rgba endpoints[ENDPOINTS];
for (uint32_t c = 0; c < COMPS; c++)
for (uint32_t e = 0; e < ENDPOINTS; e++)
endpoints[e][c] = (uint8_t)read_bits32(pBuf, bit_offset, ENDPOINT_BITS);
uint32_t pbits[4];
for (uint32_t p = 0; p < PBITS; p++)
pbits[p] = read_bits32(pBuf, bit_offset, 1);
uint32_t weights[16];
for (uint32_t i = 0; i < 16; i++)
weights[i] = read_bits32(pBuf, bit_offset, ((!i) || (i == basist::g_bc7_table_anchor_index_second_subset[part])) ? (WEIGHT_BITS - 1) : WEIGHT_BITS);
assert(bit_offset == 128);
for (uint32_t e = 0; e < ENDPOINTS; e++)
for (uint32_t c = 0; c < 4; c++)
endpoints[e][c] = (uint8_t)((c == ((mode == 7U) ? 4U : 3U)) ? 255 : bc7_dequant(endpoints[e][c], pbits[SHARED_PBITS ? (e >> 1) : e], ENDPOINT_BITS));
color_rgba block_colors[2][8];
for (uint32_t s = 0; s < 2; s++)
for (uint32_t i = 0; i < WEIGHT_VALS; i++)
{
for (uint32_t c = 0; c < COMPS; c++)
block_colors[s][i][c] = (uint8_t)bc7_interp(endpoints[s * 2 + 0][c], endpoints[s * 2 + 1][c], i, WEIGHT_BITS);
block_colors[s][i][3] = (COMPS == 3) ? 255 : block_colors[s][i][3];
}
for (uint32_t i = 0; i < 16; i++)
pPixels[i] = block_colors[basist::g_bc7_partition2[part * 16 + i]][weights[i]];
return true;
}
bool unpack_bc7_mode4_5(uint32_t mode, const void* pBlock_bits, color_rgba* pPixels)
{
const uint32_t ENDPOINTS = 2;
const uint32_t COMPS = 4;
const uint32_t WEIGHT_BITS = 2;
const uint32_t A_WEIGHT_BITS = (mode == 4) ? 3 : 2;
const uint32_t ENDPOINT_BITS = (mode == 4) ? 5 : 7;
const uint32_t A_ENDPOINT_BITS = (mode == 4) ? 6 : 8;
//const uint32_t WEIGHT_VALS = 1 << WEIGHT_BITS;
//const uint32_t A_WEIGHT_VALS = 1 << A_WEIGHT_BITS;
uint32_t bit_offset = 0;
const uint8_t* pBuf = static_cast<const uint8_t*>(pBlock_bits);
if (read_bits32(pBuf, bit_offset, mode + 1) != (1U << mode)) return false;
const uint32_t comp_rot = read_bits32(pBuf, bit_offset, 2);
const uint32_t index_mode = (mode == 4) ? read_bits32(pBuf, bit_offset, 1) : 0;
color_rgba endpoints[ENDPOINTS];
for (uint32_t c = 0; c < COMPS; c++)
for (uint32_t e = 0; e < ENDPOINTS; e++)
endpoints[e][c] = (uint8_t)read_bits32(pBuf, bit_offset, (c == 3) ? A_ENDPOINT_BITS : ENDPOINT_BITS);
const uint32_t weight_bits[2] = { index_mode ? A_WEIGHT_BITS : WEIGHT_BITS, index_mode ? WEIGHT_BITS : A_WEIGHT_BITS };
uint32_t weights[16], a_weights[16];
for (uint32_t i = 0; i < 16; i++)
(index_mode ? a_weights : weights)[i] = read_bits32(pBuf, bit_offset, weight_bits[index_mode] - ((!i) ? 1 : 0));
for (uint32_t i = 0; i < 16; i++)
(index_mode ? weights : a_weights)[i] = read_bits32(pBuf, bit_offset, weight_bits[1 - index_mode] - ((!i) ? 1 : 0));
assert(bit_offset == 128);
for (uint32_t e = 0; e < ENDPOINTS; e++)
for (uint32_t c = 0; c < 4; c++)
endpoints[e][c] = (uint8_t)bc7_dequant(endpoints[e][c], (c == 3) ? A_ENDPOINT_BITS : ENDPOINT_BITS);
color_rgba block_colors[8];
for (uint32_t i = 0; i < (1U << weight_bits[0]); i++)
for (uint32_t c = 0; c < 3; c++)
block_colors[i][c] = (uint8_t)bc7_interp(endpoints[0][c], endpoints[1][c], i, weight_bits[0]);
for (uint32_t i = 0; i < (1U << weight_bits[1]); i++)
block_colors[i][3] = (uint8_t)bc7_interp(endpoints[0][3], endpoints[1][3], i, weight_bits[1]);
for (uint32_t i = 0; i < 16; i++)
{
pPixels[i] = block_colors[weights[i]];
pPixels[i].a = block_colors[a_weights[i]].a;
if (comp_rot >= 1)
std::swap(pPixels[i].a, pPixels[i].m_comps[comp_rot - 1]);
}
return true;
}
struct bc7_mode_6
{
struct
@@ -364,9 +521,6 @@ namespace basisu
};
};
static const uint32_t g_bc7_weights4[16] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
// The transcoder only outputs mode 6 at the moment, so this is easy.
bool unpack_bc7_mode6(const void *pBlock_bits, color_rgba *pPixels)
{
static_assert(sizeof(bc7_mode_6) == 16, "sizeof(bc7_mode_6) == 16");
@@ -388,7 +542,7 @@ namespace basisu
color_rgba vals[16];
for (uint32_t i = 0; i < 16; i++)
{
const uint32_t w = g_bc7_weights4[i];
const uint32_t w = basist::g_bc7_weights4[i];
const uint32_t iw = 64 - w;
vals[i].set_noclamp_rgba(
(r0 * iw + r1 * w + 32) >> 6,
@@ -420,183 +574,37 @@ namespace basisu
return true;
}
static inline uint32_t get_block_bits(const uint8_t* pBytes, uint32_t bit_ofs, uint32_t bits_wanted)
bool unpack_bc7(const void *pBlock, color_rgba *pPixels)
{
assert(bits_wanted < 32);
const uint32_t first_byte = static_cast<const uint8_t*>(pBlock)[0];
uint32_t v = 0;
uint32_t total_bits = 0;
while (total_bits < bits_wanted)
for (uint32_t mode = 0; mode <= 7; mode++)
{
uint32_t k = pBytes[bit_ofs >> 3];
k >>= (bit_ofs & 7);
uint32_t num_bits_in_byte = 8 - (bit_ofs & 7);
v |= (k << total_bits);
total_bits += num_bits_in_byte;
bit_ofs += num_bits_in_byte;
}
return v & ((1 << bits_wanted) - 1);
}
struct bc7_mode_5
{
union
{
struct
if (first_byte & (1U << mode))
{
uint64_t m_mode : 6;
uint64_t m_rot : 2;
uint64_t m_r0 : 7;
uint64_t m_r1 : 7;
uint64_t m_g0 : 7;
uint64_t m_g1 : 7;
uint64_t m_b0 : 7;
uint64_t m_b1 : 7;
uint64_t m_a0 : 8;
uint64_t m_a1_0 : 6;
} m_lo;
uint64_t m_lo_bits;
};
union
{
struct
{
uint64_t m_a1_1 : 2;
// bit 2
uint64_t m_c00 : 1;
uint64_t m_c10 : 2;
uint64_t m_c20 : 2;
uint64_t m_c30 : 2;
uint64_t m_c01 : 2;
uint64_t m_c11 : 2;
uint64_t m_c21 : 2;
uint64_t m_c31 : 2;
uint64_t m_c02 : 2;
uint64_t m_c12 : 2;
uint64_t m_c22 : 2;
uint64_t m_c32 : 2;
uint64_t m_c03 : 2;
uint64_t m_c13 : 2;
uint64_t m_c23 : 2;
uint64_t m_c33 : 2;
// bit 33
uint64_t m_a00 : 1;
uint64_t m_a10 : 2;
uint64_t m_a20 : 2;
uint64_t m_a30 : 2;
uint64_t m_a01 : 2;
uint64_t m_a11 : 2;
uint64_t m_a21 : 2;
uint64_t m_a31 : 2;
uint64_t m_a02 : 2;
uint64_t m_a12 : 2;
uint64_t m_a22 : 2;
uint64_t m_a32 : 2;
uint64_t m_a03 : 2;
uint64_t m_a13 : 2;
uint64_t m_a23 : 2;
uint64_t m_a33 : 2;
} m_hi;
uint64_t m_hi_bits;
};
color_rgba get_low_color() const
{
return color_rgba(cNoClamp,
(int)((m_lo.m_r0 << 1) | (m_lo.m_r0 >> 6)),
(int)((m_lo.m_g0 << 1) | (m_lo.m_g0 >> 6)),
(int)((m_lo.m_b0 << 1) | (m_lo.m_b0 >> 6)),
m_lo.m_a0);
}
color_rgba get_high_color() const
{
return color_rgba(cNoClamp,
(int)((m_lo.m_r1 << 1) | (m_lo.m_r1 >> 6)),
(int)((m_lo.m_g1 << 1) | (m_lo.m_g1 >> 6)),
(int)((m_lo.m_b1 << 1) | (m_lo.m_b1 >> 6)),
(int)m_lo.m_a1_0 | ((int)m_hi.m_a1_1 << 6));
}
void get_block_colors(color_rgba* pColors) const
{
const color_rgba low_color(get_low_color());
const color_rgba high_color(get_high_color());
for (uint32_t i = 0; i < 4; i++)
{
static const uint32_t s_bc7_weights2[4] = { 0, 21, 43, 64 };
pColors[i].set_noclamp_rgba(
(low_color.r * (64 - s_bc7_weights2[i]) + high_color.r * s_bc7_weights2[i] + 32) >> 6,
(low_color.g * (64 - s_bc7_weights2[i]) + high_color.g * s_bc7_weights2[i] + 32) >> 6,
(low_color.b * (64 - s_bc7_weights2[i]) + high_color.b * s_bc7_weights2[i] + 32) >> 6,
(low_color.a * (64 - s_bc7_weights2[i]) + high_color.a * s_bc7_weights2[i] + 32) >> 6);
switch (mode)
{
case 0:
case 2:
return unpack_bc7_mode0_2(mode, pBlock, pPixels);
case 1:
case 3:
case 7:
return unpack_bc7_mode1_3_7(mode, pBlock, pPixels);
case 4:
case 5:
return unpack_bc7_mode4_5(mode, pBlock, pPixels);
case 6:
return unpack_bc7_mode6(pBlock, pPixels);
default:
break;
}
}
}
uint32_t get_selector(uint32_t idx, bool alpha) const
{
const uint32_t size = (idx == 0) ? 1 : 2;
uint32_t ofs = alpha ? 97 : 66;
if (idx)
ofs += 1 + 2 * (idx - 1);
return get_block_bits(reinterpret_cast<const uint8_t*>(this), ofs, size);
}
};
bool unpack_bc7_mode5(const void* pBlock_bits, color_rgba* pPixels)
{
static_assert(sizeof(bc7_mode_5) == 16, "sizeof(bc7_mode_5) == 16");
const bc7_mode_5& block = *static_cast<const bc7_mode_5*>(pBlock_bits);
if (block.m_lo.m_mode != (1 << 5))
return false;
color_rgba block_colors[4];
block.get_block_colors(block_colors);
const uint32_t rot = block.m_lo.m_rot;
for (uint32_t i = 0; i < 16; i++)
{
const uint32_t cs = block.get_selector(i, false);
color_rgba c(block_colors[cs]);
const uint32_t as = block.get_selector(i, true);
c.a = block_colors[as].a;
if (rot > 0)
std::swap(c[3], c[rot - 1]);
pPixels[i] = c;
}
return true;
return false;
}
struct fxt1_block
{
union
@@ -903,13 +911,14 @@ namespace basisu
etc2_eac_r11 m_c[2];
};
static void unpack_etc2_eac_r(const etc2_eac_r11* p, color_rgba* pPixels, uint32_t c)
void unpack_etc2_eac_r(const void *p, color_rgba* pPixels, uint32_t c)
{
const uint64_t sels = p->get_sels();
const etc2_eac_r11* pBlock = static_cast<const etc2_eac_r11*>(p);
const uint64_t sels = pBlock->get_sels();
const int base = (int)p->m_base * 8 + 4;
const int mul = p->m_mul ? ((int)p->m_mul * 8) : 1;
const int table = (int)p->m_table;
const int base = (int)pBlock->m_base * 8 + 4;
const int mul = pBlock->m_mul ? ((int)pBlock->m_mul * 8) : 1;
const int table = (int)pBlock->m_table;
for (uint32_t y = 0; y < 4; y++)
{
@@ -923,7 +932,8 @@ namespace basisu
val = clamp<int>(val, 0, 2047);
// Convert to 8-bits with rounding
pPixels[x + y * 4].m_comps[c] = static_cast<uint8_t>((val * 255 + 1024) / 2047);
//pPixels[x + y * 4].m_comps[c] = static_cast<uint8_t>((val * 255 + 1024) / 2047);
pPixels[x + y * 4].m_comps[c] = static_cast<uint8_t>((val * 255 + 1023) / 2047);
} // x
} // y
@@ -939,6 +949,11 @@ namespace basisu
}
}
void unpack_uastc(const void* p, color_rgba* pPixels)
{
basist::unpack_uastc(*static_cast<const basist::uastc_block*>(p), (basist::color32 *)pPixels, false);
}
// Unpacks to RGBA, R, RG, or A
bool unpack_block(texture_format fmt, const void* pBlock, color_rgba* pPixels)
{
@@ -966,14 +981,7 @@ namespace basisu
}
case texture_format::cBC7:
{
// We only support modes 5 and 6.
if (!unpack_bc7_mode5(pBlock, pPixels))
{
if (!unpack_bc7_mode6(pBlock, pPixels))
return false;
}
break;
return unpack_bc7(pBlock, pPixels);
}
// Full ETC2 color blocks (planar/T/H modes) is currently unsupported in basisu, but we do support ETC2 with alpha (using ETC1 for color)
case texture_format::cETC2_RGB:
@@ -1032,6 +1040,11 @@ namespace basisu
unpack_etc2_eac_rg(pBlock, pPixels);
break;
}
case texture_format::cUASTC4x4:
{
unpack_uastc(pBlock, pPixels);
break;
}
default:
{
assert(0);
@@ -1113,6 +1126,7 @@ namespace basisu
KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG = 0x8C02,
KTX_COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93B0,
KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR = 0x93D0,
KTX_COMPRESSED_RGBA_UASTC_4x4_KHR = 0x94CC, // TODO - Use proper value!
KTX_ATC_RGB_AMD = 0x8C92,
KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD = 0x87EE,
KTX_COMPRESSED_RGB_FXT1_3DFX = 0x86B0,
@@ -1305,6 +1319,12 @@ namespace basisu
base_internal_fmt = KTX_RG;
break;
}
case texture_format::cUASTC4x4:
{
internal_fmt = KTX_COMPRESSED_RGBA_UASTC_4x4_KHR;
base_internal_fmt = KTX_RGBA;
break;
}
case texture_format::cFXT1_RGB:
{
internal_fmt = KTX_COMPRESSED_RGB_FXT1_3DFX;
@@ -1415,7 +1435,7 @@ namespace basisu
return write_compressed_texture_file(pFilename, v, false);
}
const uint32_t OUT_FILE_MAGIC = 'TEXC';
//const uint32_t OUT_FILE_MAGIC = 'TEXC';
struct out_file_header
{
packed_uint<4> m_magic;
@@ -1428,7 +1448,11 @@ namespace basisu
bool write_3dfx_out_file(const char* pFilename, const gpu_image& gi)
{
out_file_header hdr;
hdr.m_magic = OUT_FILE_MAGIC;
//hdr.m_magic = OUT_FILE_MAGIC;
hdr.m_magic.m_bytes[0] = 67;
hdr.m_magic.m_bytes[1] = 88;
hdr.m_magic.m_bytes[2] = 69;
hdr.m_magic.m_bytes[3] = 84;
hdr.m_pad = 0;
hdr.m_width = gi.get_blocks_x() * 8;
hdr.m_height = gi.get_blocks_y() * 4;
+7 -7
View File
@@ -1,5 +1,5 @@
// basisu_gpu_texture.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -18,8 +18,7 @@
namespace basisu
{
// GPU texture image
// GPU texture "image"
class gpu_image
{
public:
@@ -133,22 +132,23 @@ namespace basisu
bool write_compressed_texture_file(const char *pFilename, const gpu_image &g);
bool write_3dfx_out_file(const char* pFilename, const gpu_image& gi);
// GPU texture block unpacking
// GPU texture block unpacking
void unpack_etc2_eac(const void *pBlock_bits, color_rgba *pPixels);
bool unpack_bc1(const void *pBlock_bits, color_rgba *pPixels, bool set_alpha);
void unpack_bc4(const void *pBlock_bits, uint8_t *pPixels, uint32_t stride);
bool unpack_bc3(const void *pBlock_bits, color_rgba *pPixels);
void unpack_bc5(const void *pBlock_bits, color_rgba *pPixels);
bool unpack_bc7_mode6(const void *pBlock_bits, color_rgba *pPixels);
bool unpack_bc7_mode5(const void* pBlock_bits, color_rgba* pPixels);
bool unpack_bc7(const void* pBlock_bits, color_rgba* pPixels);
void unpack_atc(const void* pBlock_bits, color_rgba* pPixels);
bool unpack_fxt1(const void* p, color_rgba* pPixels);
bool unpack_pvrtc2(const void* p, color_rgba* pPixels);
void unpack_etc2_eac_r(const void *p, color_rgba* pPixels, uint32_t c);
void unpack_etc2_eac_rg(const void* p, color_rgba* pPixels);
// unpack_block() is only capable of unpacking texture data created by the transcoder.
// For some texture formats (like BC7, or ETC2) it's not a complete implementation.
// unpack_block() is primarily intended to unpack texture data created by the transcoder.
// For some texture formats (like ETC2 RGB, PVRTC2, FXT1) it's not a complete implementation.
bool unpack_block(texture_format fmt, const void *pBlock, color_rgba *pPixels);
} // namespace basisu
+2515
View File
File diff suppressed because it is too large Load Diff
+296 -1
View File
@@ -1,5 +1,5 @@
// basisu_pvrtc1_4.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -16,6 +16,118 @@
namespace basisu
{
#if 0
static const uint8_t g_pvrtc_5[32] = { 0,8,16,24,33,41,49,57,66,74,82,90,99,107,115,123,132,140,148,156,165,173,181,189,198,206,214,222,231,239,247,255 };
static const uint8_t g_pvrtc_4[16] = { 0,16,33,49,66,82,99,115,140,156,173,189,206,222,239,255 };
static const uint8_t g_pvrtc_3[8] = { 0,33,74,107,148,181,222,255 };
static const uint8_t g_pvrtc_alpha[9] = { 0,34,68,102,136,170,204,238,255 };
#endif
static const uint8_t g_pvrtc_5_nearest[256] = { 0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,20,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,24,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,31,31,31,31 };
static const uint8_t g_pvrtc_4_nearest[256] = { 0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15 };
#if 0
static const uint8_t g_pvrtc_3_nearest[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 };
static const uint8_t g_pvrtc_alpha_nearest[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8 };
#endif
#if 0
static const uint8_t g_pvrtc_5_floor[256] =
{
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,
3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,
7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
11,11,11,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,
15,15,15,15,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,
19,19,19,19,19,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,23,23,23,
23,23,23,23,23,23,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,27,27,
27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,31
};
static const uint8_t g_pvrtc_5_ceil[256] =
{
0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,
4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,
8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,12,12,12,12,12,
12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,16,16,16,16,
16,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,20,20,20,
20,20,20,20,20,20,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,24,24,
24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,
28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,31,31,31,31,31,31,31,31
};
static const uint8_t g_pvrtc_4_floor[256] =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15
};
static const uint8_t g_pvrtc_4_ceil[256] =
{
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,
10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
};
static const uint8_t g_pvrtc_3_floor[256] =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7
};
static const uint8_t g_pvrtc_3_ceil[256] =
{
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
};
static const uint8_t g_pvrtc_alpha_floor[256] =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8
};
static const uint8_t g_pvrtc_alpha_ceil[256] =
{
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
#endif
uint32_t pvrtc4_swizzle_uv(uint32_t width, uint32_t height, uint32_t x, uint32_t y)
{
assert((x < width) && (y < height) && basisu::is_pow2(height) && basisu::is_pow2(width));
@@ -266,4 +378,187 @@ namespace basisu
}
}
uint64_t pvrtc4_image::local_endpoint_optimization_opaque(uint32_t bx, uint32_t by, const image& orig_img, bool perceptual)
{
uint64_t initial_error = evaluate_1x1_endpoint_error(bx, by, orig_img, perceptual, false);
if (!initial_error)
return initial_error;
vec3F c_avg_orig(0);
for (int y = 0; y < 7; y++)
{
const uint32_t py = wrap_y(by * 4 + y - 1);
for (uint32_t x = 0; x < 7; x++)
{
const uint32_t px = wrap_x(bx * 4 + x - 1);
const color_rgba& c = orig_img(px, py);
c_avg_orig[0] += c[0];
c_avg_orig[1] += c[1];
c_avg_orig[2] += c[2];
}
}
c_avg_orig *= 1.0f / 49.0f;
vec3F quant_colors[2];
quant_colors[0].set(c_avg_orig);
quant_colors[0] -= vec3F(.0125f);
quant_colors[1].set(c_avg_orig);
quant_colors[1] += vec3F(.0125f);
float total_weight[2];
bool success = true;
for (uint32_t pass = 0; pass < 4; pass++)
{
vec3F new_colors[2] = { vec3F(0), vec3F(0) };
memset(total_weight, 0, sizeof(total_weight));
static const float s_weights[7][7] =
{
{ 1.000000f, 1.637089f, 2.080362f, 2.242640f, 2.080362f, 1.637089f, 1.000000f },
{ 1.637089f, 2.414213f, 3.006572f, 3.242640f, 3.006572f, 2.414213f, 1.637089f },
{ 2.080362f, 3.006572f, 3.828426f, 4.242640f, 3.828426f, 3.006572f, 2.080362f },
{ 2.242640f, 3.242640f, 4.242640f, 5.000000f, 4.242640f, 3.242640f, 2.242640f },
{ 2.080362f, 3.006572f, 3.828426f, 4.242640f, 3.828426f, 3.006572f, 2.080362f },
{ 1.637089f, 2.414213f, 3.006572f, 3.242640f, 3.006572f, 2.414213f, 1.637089f },
{ 1.000000f, 1.637089f, 2.080362f, 2.242640f, 2.080362f, 1.637089f, 1.000000f }
};
for (int y = 0; y < 7; y++)
{
const uint32_t py = wrap_y(by * 4 + y - 1);
for (uint32_t x = 0; x < 7; x++)
{
const uint32_t px = wrap_x(bx * 4 + x - 1);
const color_rgba& orig_c = orig_img(px, py);
vec3F color(orig_c[0], orig_c[1], orig_c[2]);
uint32_t c = quant_colors[0].squared_distance(color) > quant_colors[1].squared_distance(color);
const float weight = s_weights[y][x];
new_colors[c] += color * weight;
total_weight[c] += weight;
}
}
if (!total_weight[0] || !total_weight[1])
success = false;
quant_colors[0] = new_colors[0] / (float)total_weight[0];
quant_colors[1] = new_colors[1] / (float)total_weight[1];
}
if (!success)
{
quant_colors[0] = c_avg_orig;
quant_colors[1] = c_avg_orig;
}
vec4F colors[2] = { quant_colors[0], quant_colors[1] };
colors[0] += vec3F(.5f);
colors[1] += vec3F(.5f);
color_rgba color_0((int)colors[0][0], (int)colors[0][1], (int)colors[0][2], 0);
color_rgba color_1((int)colors[1][0], (int)colors[1][1], (int)colors[1][2], 0);
pvrtc4_block cur_blocks[3][3];
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
const uint32_t block_x = wrap_block_x(bx + x);
const uint32_t block_y = wrap_block_y(by + y);
cur_blocks[x + 1][y + 1] = m_blocks(block_x, block_y);
}
}
color_rgba l1(0), h1(0);
l1[0] = g_pvrtc_5_nearest[color_0[0]];
h1[0] = g_pvrtc_5_nearest[color_1[0]];
l1[1] = g_pvrtc_5_nearest[color_0[1]];
h1[1] = g_pvrtc_5_nearest[color_1[1]];
l1[2] = g_pvrtc_4_nearest[color_0[2]];
h1[2] = g_pvrtc_5_nearest[color_0[2]];
l1[3] = 0;
h1[3] = 0;
m_blocks(bx, by).set_endpoint_raw(0, l1, true);
m_blocks(bx, by).set_endpoint_raw(1, h1, true);
uint64_t e03_err_0 = remap_pixels_influenced_by_endpoint(bx, by, orig_img, perceptual, false);
pvrtc4_block blocks0[3][3];
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
const uint32_t block_x = wrap_block_x(bx + x);
const uint32_t block_y = wrap_block_y(by + y);
blocks0[x + 1][y + 1] = m_blocks(block_x, block_y);
}
}
l1[0] = g_pvrtc_5_nearest[color_1[0]];
h1[0] = g_pvrtc_5_nearest[color_0[0]];
l1[1] = g_pvrtc_5_nearest[color_1[1]];
h1[1] = g_pvrtc_5_nearest[color_0[1]];
l1[2] = g_pvrtc_4_nearest[color_1[2]];
h1[2] = g_pvrtc_5_nearest[color_0[2]];
l1[3] = 0;
h1[3] = 0;
m_blocks(bx, by).set_endpoint_raw(0, l1, true);
m_blocks(bx, by).set_endpoint_raw(1, h1, true);
uint64_t e03_err_1 = remap_pixels_influenced_by_endpoint(bx, by, orig_img, perceptual, false);
if (initial_error < std::min(e03_err_0, e03_err_1))
{
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
const uint32_t block_x = wrap_block_x(bx + x);
const uint32_t block_y = wrap_block_y(by + y);
m_blocks(block_x, block_y) = cur_blocks[x + 1][y + 1];
}
}
return initial_error;
}
else if (e03_err_0 < e03_err_1)
{
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
const uint32_t block_x = wrap_block_x(bx + x);
const uint32_t block_y = wrap_block_y(by + y);
m_blocks(block_x, block_y) = blocks0[x + 1][y + 1];
}
}
assert(e03_err_0 == evaluate_1x1_endpoint_error(bx, by, orig_img, perceptual, false));
return e03_err_0;
}
assert(e03_err_1 == evaluate_1x1_endpoint_error(bx, by, orig_img, perceptual, false));
return e03_err_1;
}
} // basisu
+97 -3
View File
@@ -1,5 +1,5 @@
// basisu_pvrtc1_4.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -87,6 +87,14 @@ namespace basisu
return (m_modulation >> ((y * 4 + x) * 2)) & 3;
}
inline void set_modulation(uint32_t x, uint32_t y, uint32_t s)
{
assert((x < 4) && (y < 4) && (s < 4));
uint32_t n = (y * 4 + x) * 2;
m_modulation = (m_modulation & (~(3 << n))) | (s << n);
assert(get_modulation(x, y) == s);
}
// Scaled by 8
inline const uint32_t* get_scaled_modulation_values(bool block_uses_transparent_modulation) const
{
@@ -107,7 +115,7 @@ namespace basisu
}
// opaque endpoints: 554, 555
// transparent endpoints: 3443 or 3444
// transparent endpoints: 3443, 3444
inline void set_endpoint_raw(uint32_t endpoint_index, const color_rgba& c, bool opaque_endpoint)
{
assert(endpoint_index < 2);
@@ -352,7 +360,93 @@ namespace basisu
return result;
}
inline void set_modulation(uint32_t x, uint32_t y, uint32_t s)
{
assert((x < m_width) && (y < m_height));
return m_blocks(x >> 2, y >> 2).set_modulation(x & 3, y & 3, s);
}
inline uint64_t map_pixel(uint32_t x, uint32_t y, const color_rgba& c, bool perceptual, bool alpha_is_significant, bool record = true)
{
color_rgba v[4];
get_interpolated_colors(x, y, v);
uint64_t best_dist = color_distance(perceptual, c, v[0], alpha_is_significant);
uint32_t best_v = 0;
for (uint32_t i = 1; i < 4; i++)
{
uint64_t dist = color_distance(perceptual, c, v[i], alpha_is_significant);
if (dist < best_dist)
{
best_dist = dist;
best_v = i;
}
}
if (record)
set_modulation(x, y, best_v);
return best_dist;
}
inline uint64_t remap_pixels_influenced_by_endpoint(uint32_t bx, uint32_t by, const image& orig_img, bool perceptual, bool alpha_is_significant)
{
uint64_t total_error = 0;
for (int yd = -3; yd <= 3; yd++)
{
const int y = wrap_y((int)by * 4 + 2 + yd);
for (int xd = -3; xd <= 3; xd++)
{
const int x = wrap_x((int)bx * 4 + 2 + xd);
total_error += map_pixel(x, y, orig_img(x, y), perceptual, alpha_is_significant);
}
}
return total_error;
}
inline uint64_t evaluate_1x1_endpoint_error(uint32_t bx, uint32_t by, const image& orig_img, bool perceptual, bool alpha_is_significant, uint64_t threshold_error = 0) const
{
uint64_t total_error = 0;
for (int yd = -3; yd <= 3; yd++)
{
const int y = wrap_y((int)by * 4 + 2 + yd);
for (int xd = -3; xd <= 3; xd++)
{
const int x = wrap_x((int)bx * 4 + 2 + xd);
total_error += color_distance(perceptual, get_pixel(x, y), orig_img(x, y), alpha_is_significant);
if ((threshold_error) && (total_error >= threshold_error))
return total_error;
}
}
return total_error;
}
uint64_t local_endpoint_optimization_opaque(uint32_t bx, uint32_t by, const image& orig_img, bool perceptual);
inline uint64_t map_all_pixels(const image& img, bool perceptual, bool alpha_is_significant)
{
assert(m_width == img.get_width());
assert(m_height == img.get_height());
uint64_t total_error = 0;
for (uint32_t y = 0; y < img.get_height(); y++)
for (uint32_t x = 0; x < img.get_width(); x++)
total_error += map_pixel(x, y, img(x, y), perceptual, alpha_is_significant);
return total_error;
}
public:
uint32_t m_width, m_height;
pvrtc4_block_vector2D m_blocks;
uint32_t m_block_width, m_block_height;
+2 -2
View File
@@ -1,5 +1,5 @@
// basisu_resampler_filters.cpp
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -283,7 +283,7 @@ namespace basisu
return sum;
}
static const float KAISER_ALPHA = 4.0;
//static const float KAISER_ALPHA = 4.0;
static double kaiser(double alpha, double half_width, double x)
{
const double ratio = (x / half_width);
+1278 -21
View File
File diff suppressed because it is too large Load Diff
+4090
View File
File diff suppressed because it is too large Load Diff
+125
View File
@@ -0,0 +1,125 @@
// basisu_uastc_enc.h
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "basisu_etc.h"
#include "transcoder/basisu_transcoder_uastc.h"
namespace basisu
{
const uint32_t TOTAL_PACK_UASTC_LEVELS = 5;
enum
{
// Fastest is the lowest quality, although it's stil substantially higher quality vs. BC1/ETC1. It supports 5 modes.
// The output may be somewhat blocky because this setting doesn't support 2/3-subset UASTC modes, but it should be less blocky vs. BC1/ETC1.
// This setting doesn't write BC1 hints, so BC1 transcoding will be slower.
// Transcoded ETC1 quality will be lower because it only considers 2 hints out of 32.
// Avg. 43.45 dB
cPackUASTCLevelFastest = 0,
// Faster is ~3x slower than fastest. It supports 9 modes.
// Avg. 46.49 dB
cPackUASTCLevelFaster = 1,
// Default is ~5.5x slower than fastest. It supports 14 modes.
// Avg. 47.47 dB
cPackUASTCLevelDefault = 2,
// Slower is ~14.5x slower than fastest. It supports all 18 modes.
// Avg. 48.01 dB
cPackUASTCLevelSlower = 3,
// VerySlow is ~200x slower than fastest.
// The best quality the codec is capable of, but you'll need to be patient or have a lot of cores.
// Avg. 48.24 dB
cPackUASTCLevelVerySlow = 4,
cPackUASTCLevelMask = 0xF,
// By default the encoder tries to strike a balance between UASTC and transcoded BC7 quality.
// These flags allow you to favor only optimizing for lowest UASTC error, or lowest BC7 error.
cPackUASTCFavorUASTCError = 8,
cPackUASTCFavorBC7Error = 16,
cPackUASTCETC1FasterHints = 64,
cPackUASTCETC1FastestHints = 128,
cPackUASTCETC1DisableFlipAndIndividual = 256,
};
// pRGBAPixels: Pointer to source 4x4 block of RGBA pixels (R first in memory).
// block: Reference to destination UASTC block.
// level: Controls compression speed vs. performance tradeoff.
void encode_uastc(const uint8_t* pRGBAPixels, basist::uastc_block& output_block, uint32_t flags = cPackUASTCLevelDefault);
struct uastc_encode_results
{
uint32_t m_uastc_mode;
uint32_t m_common_pattern;
basist::astc_block_desc m_astc;
color_rgba m_solid_color;
uint64_t m_astc_err;
};
void pack_uastc(basist::uastc_block& blk, const uastc_encode_results& result, const etc_block& etc1_blk, uint32_t etc1_bias, const eac_a8_block& etc_eac_a8_blk, bool bc1_hint0, bool bc1_hint1);
const float UASTC_RDO_DEFAULT_MAX_ALLOWED_RMS_INCREASE_RATIO = 10.0f;
const float UASTC_RDO_DEFAULT_SKIP_BLOCK_RMS_THRESH = 8.0f;
struct uastc_rdo_params
{
uastc_rdo_params()
{
clear();
}
void clear()
{
m_quality_scaler = 1.0f;
m_lz_dict_size = 32768;
m_langrangian_multiplier = 0.025f;
m_max_allowed_rms_increase_ratio = UASTC_RDO_DEFAULT_MAX_ALLOWED_RMS_INCREASE_RATIO;
m_skip_block_rms_thresh = UASTC_RDO_DEFAULT_SKIP_BLOCK_RMS_THRESH;
m_endpoint_refinement = true;
m_lz_literal_cost = 100;
}
// m_quality_scaler: This value controls the overall quality vs. size tradeoff. Smaller values=larger/higher quality, 0=no change, larger values=smaller/lower quality.
// Good range to try is .2-2.5.
float m_quality_scaler;
// m_lz_dict_size: Size of LZ dictionary to simulate in bytes. The larger this value, the slower the encoder but the higher the quality per LZ compressed bit.
uint32_t m_lz_dict_size;
// m_langrangian_multiplier: The post-processor tries to reduce rate+distortion*langrangian_mul (rate is approximate LZ bits and distortion is squared error).
// Larger values push the postprocessor towards optimizing more for lower distortion, and smaller values more for rate.
float m_langrangian_multiplier;
// m_max_allowed_rms_increase_ratio: How much the RMS error of a block is allowed to increase before a trial is rejected. 1.0=no increase allowed, 1.05=5% increase allowed, etc.
float m_max_allowed_rms_increase_ratio;
// m_skip_block_rms_thresh: Blocks with this much RMS error or more are completely skipped by the RDO encoder.
float m_skip_block_rms_thresh;
// m_endpoint_refinement: If true, the post-process will attempt to refine the endpoints of blocks with modified selectors.
bool m_endpoint_refinement;
uint32_t m_lz_literal_cost;
};
// num_blocks, pBlocks: Number of blocks and pointer to UASTC blocks to process.
// pBlock_pixels: Pointer to an array of 4x4 blocks containing the original texture pixels. This is NOT a raster image, but a pointer to individual 4x4 blocks.
// flags: Pass in the same flags used to encode the UASTC blocks. The flags are used to reencode the transcode hints in the same way.
bool uastc_rdo(uint32_t num_blocks, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params &params, uint32_t flags = cPackUASTCLevelDefault, job_pool* pJob_pool = nullptr, uint32_t total_jobs = 0);
} // namespace basisu
+2
View File
@@ -29,6 +29,7 @@ Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning (disable : 4201)
#ifndef BASISU_NO_ITERATOR_DEBUG_LEVEL
@@ -200,6 +201,7 @@ static void uivector_init(uivector* p) {
/*returns 1 if success, 0 if failure ==> nothing done*/
static unsigned uivector_push_back(uivector* p, unsigned c) {
if(!uivector_resize(p, p->size + 1)) return 0;
if (!p->data) return 0;
p->data[p->size - 1] = c;
return 1;
}
+28 -12
View File
@@ -1,5 +1,5 @@
// basisu.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
// Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -79,9 +79,9 @@
#define strcasecmp _stricmp
#endif
// Set to one to enable debug printf()'s when any errors occur, for development/debugging.
#ifndef BASISU_DEVEL_MESSAGES
#define BASISU_DEVEL_MESSAGES 0
// Set to one to enable debug printf()'s when any errors occur, for development/debugging. Especially useful for WebGL development.
#ifndef BASISU_FORCE_DEVEL_MESSAGES
#define BASISU_FORCE_DEVEL_MESSAGES 0
#endif
#define BASISU_NOTE_UNUSED(x) (void)(x)
@@ -92,7 +92,7 @@
#define BASISU_STRINGIZE(x) #x
#define BASISU_STRINGIZE2(x) BASISU_STRINGIZE(x)
#if BASISU_DEVEL_MESSAGES
#if BASISU_FORCE_DEVEL_MESSAGES
#define BASISU_DEVEL_ERROR(...) do { basisu::debug_printf(__VA_ARGS__); } while(0)
#else
#define BASISU_DEVEL_ERROR(...)
@@ -125,9 +125,25 @@ namespace basisu
template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }
template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }
template <typename S> inline S maximum(S a, S b, S c, S d) { return maximum(maximum(maximum(a, b), c), d); }
template <typename S> inline S minimum(S a, S b) { return (a < b) ? a : b; }
template <typename S> inline S minimum(S a, S b, S c) { return minimum(minimum(a, b), c); }
template <typename S> inline S minimum(S a, S b, S c, S d) { return minimum(minimum(minimum(a, b), c), d); }
inline float clampf(float value, float low, float high) { if (value < low) value = low; else if (value > high) value = high; return value; }
inline float saturate(float value) { return clampf(value, 0, 1.0f); }
inline uint8_t minimumub(uint8_t a, uint8_t b) { return (a < b) ? a : b; }
inline uint32_t minimumu(uint32_t a, uint32_t b) { return (a < b) ? a : b; }
inline int32_t minimumi(int32_t a, int32_t b) { return (a < b) ? a : b; }
inline float minimumf(float a, float b) { return (a < b) ? a : b; }
inline uint8_t maximumub(uint8_t a, uint8_t b) { return (a > b) ? a : b; }
inline uint32_t maximumu(uint32_t a, uint32_t b) { return (a > b) ? a : b; }
inline int32_t maximumi(int32_t a, int32_t b) { return (a > b) ? a : b; }
inline float maximumf(float a, float b) { return (a > b) ? a : b; }
inline int squarei(int i) { return i * i; }
inline float squaref(float i) { return i * i; }
template<typename T> inline T square(T a) { return a * a; }
template <typename S> inline S clamp(S value, S low, S high) { return (value < low) ? low : ((value > high) ? high : value); }
@@ -137,8 +153,6 @@ namespace basisu
template<typename T> inline void clear_vector(T &vec) { vec.erase(vec.begin(), vec.end()); }
template<typename T> inline typename T::value_type *enlarge_vector(T &vec, size_t n) { size_t cs = vec.size(); vec.resize(cs + n); return &vec[cs]; }
template<typename S> inline S square(S val) { return val * val; }
inline bool is_pow2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }
inline bool is_pow2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }
@@ -256,6 +270,7 @@ namespace basisu
inline packed_uint(const packed_uint& other) { *this = other; }
inline packed_uint& operator= (uint32_t v) { for (uint32_t i = 0; i < NumBytes; i++) m_bytes[i] = static_cast<uint8_t>(v >> (i * 8)); return *this; }
inline packed_uint& operator= (const packed_uint& rhs) { memcpy(m_bytes, rhs.m_bytes, sizeof(m_bytes)); return *this; }
inline operator uint32_t() const
{
@@ -308,15 +323,15 @@ namespace basisu
// Block-based formats
cETC1, // ETC1
cETC1S, // ETC1 (subset: diff colors only, no subblocks)
cETC2_RGB, // ETC2 color block
cETC2_RGBA, // ETC2 alpha block followed by ETC2 color block
cETC2_RGB, // ETC2 color block (basisu doesn't support ETC2 planar/T/H modes - just basic ETC1)
cETC2_RGBA, // ETC2 EAC alpha block followed by ETC2 color block
cETC2_ALPHA, // ETC2 EAC alpha block
cBC1, // DXT1
cBC3, // DXT5 (DXT5A block followed by a DXT1 block)
cBC3, // DXT5 (BC4/DXT5A block followed by a BC1/DXT1 block)
cBC4, // DXT5A
cBC5, // 3DC/DXN (two DXT5A blocks)
cBC5, // 3DC/DXN (two BC4/DXT5A blocks)
cBC7,
cASTC4x4,
cASTC4x4, // LDR only
cPVRTC1_4_RGB,
cPVRTC1_4_RGBA,
cATC_RGB,
@@ -325,6 +340,7 @@ namespace basisu
cPVRTC2_4_RGBA,
cETC2_R11_EAC,
cETC2_RG11_EAC,
cUASTC4x4,
// Uncompressed/raw pixels
cRGBA32,
+11 -5
View File
@@ -1,5 +1,5 @@
// basis_file_headers.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@ namespace basist
// Slice desc header flags
enum basis_slice_desc_flags
{
cSliceDescFlagsIsAlphaData = 1,
cSliceDescFlagsHasAlpha = 1,
cSliceDescFlagsFrameIsIFrame = 2 // Video only: Frame doesn't refer to previous frame (no usage of conditional replenishment pred symbols)
};
@@ -47,9 +47,9 @@ namespace basist
// File header files
enum basis_header_flags
{
cBASISHeaderFlagETC1S = 1, // Always set for basis universal files
cBASISHeaderFlagETC1S = 1, // Always set for ETC1S files. Not set for UASTC files.
cBASISHeaderFlagYFlipped = 2, // Set if the texture had to be Y flipped before encoding
cBASISHeaderFlagHasAlphaSlices = 4 // True if the odd slices contain alpha data
cBASISHeaderFlagHasAlphaSlices = 4 // True if any slices contain alpha (for ETC1S, if the odd slices contain alpha data)
};
// The image type field attempts to describe how to interpret the image data in a Basis file.
@@ -71,6 +71,12 @@ namespace basist
cBASISMaxUSPerFrame = 0xFFFFFF
};
enum class basis_tex_format
{
cETC1S = 0,
cUASTC4x4 = 1
};
struct basis_file_header
{
enum
@@ -91,7 +97,7 @@ namespace basist
basisu::packed_uint<3> m_total_images; // The total # of images
basisu::packed_uint<1> m_format; // enum basist::block_format
basisu::packed_uint<1> m_tex_format; // enum basis_tex_format
basisu::packed_uint<2> m_flags; // enum basist::header_flags
basisu::packed_uint<1> m_tex_type; // enum basist::basis_texture_type
basisu::packed_uint<3> m_us_per_frame; // Framerate of video, in microseconds per frame
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
+1 -1
View File
@@ -1,5 +1,5 @@
// basisu_global_selector_palette.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
File diff suppressed because it is too large Load Diff
+100 -40
View File
@@ -1,5 +1,5 @@
// basisu_transcoder.h
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
// Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,10 +15,13 @@
// limitations under the License.
#pragma once
// Set BASISU_DEVEL_MESSAGES to 1 to enable debug printf()'s whenever an error occurs, for easier debugging during development.
//#define BASISU_DEVEL_MESSAGES 1
// Set BASISU_FORCE_DEVEL_MESSAGES to 1 to enable debug printf()'s whenever an error occurs, for easier debugging during development.
#ifndef BASISU_FORCE_DEVEL_MESSAGES
#define BASISU_FORCE_DEVEL_MESSAGES 0
#endif
#include "basisu_transcoder_internal.h"
#include "basisu_transcoder_uastc.h"
#include "basisu_global_selector_palette.h"
#include "basisu_file_headers.h"
@@ -45,12 +48,11 @@ namespace basist
cTFBC3_RGBA = 3, // Opaque+alpha, BC4 followed by a BC1 block, alpha channel will be opaque for opaque .basis files
cTFBC4_R = 4, // Red only, alpha slice is transcoded to output if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified
cTFBC5_RG = 5, // XY: Two BC4 blocks, X=R and Y=Alpha, .basis file should have alpha data (if not Y will be all 255's)
cTFBC7_M6_RGB = 6, // Opaque only, RGB or alpha if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified. Highest quality of all the non-ETC1 formats.
cTFBC7_M5_RGBA = 7, // Opaque+alpha, alpha channel will be opaque for opaque .basis files
cTFBC7_RGBA = 6, // RGB or RGBA, mode 5 for ETC1S, modes (1,2,3,5,6,7) for UASTC
// PVRTC1 4bpp (mobile, PowerVR devices)
cTFPVRTC1_4_RGB = 8, // Opaque only, RGB or alpha if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified, nearly lowest quality of any texture format.
cTFPVRTC1_4_RGBA = 9, // Opaque+alpha, most useful for simple opacity maps. If .basis file doens't have alpha cTFPVRTC1_4_RGB will be used instead. Lowest quality of any supported texture format.
cTFPVRTC1_4_RGBA = 9, // Opaque+alpha, most useful for simple opacity maps. If .basis file doesn't have alpha cTFPVRTC1_4_RGB will be used instead. Lowest quality of any supported texture format.
// ASTC (mobile, Intel devices, hopefully all desktop GPU's one day)
cTFASTC_4x4_RGBA = 10, // Opaque+alpha, ASTC 4x4, alpha channel will be opaque for opaque .basis files. Transcoder uses RGB/RGBA/L/LA modes, void extent, and up to two ([0,47] and [0,255]) endpoint precisions.
@@ -85,26 +87,49 @@ namespace basist
cTFBC3 = cTFBC3_RGBA,
cTFBC4 = cTFBC4_R,
cTFBC5 = cTFBC5_RG,
cTFBC7_M6_OPAQUE_ONLY = cTFBC7_M6_RGB,
cTFBC7_M5 = cTFBC7_M5_RGBA,
// Previously, the caller had some control over which BC7 mode the transcoder output. We've simplified this due to UASTC, which supports numerous modes.
cTFBC7_M6_RGB = cTFBC7_RGBA, // Opaque only, RGB or alpha if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified. Highest quality of all the non-ETC1 formats.
cTFBC7_M5_RGBA = cTFBC7_RGBA, // Opaque+alpha, alpha channel will be opaque for opaque .basis files
cTFBC7_M6_OPAQUE_ONLY = cTFBC7_RGBA,
cTFBC7_M5 = cTFBC7_RGBA,
cTFBC7_ALT = 7,
cTFASTC_4x4 = cTFASTC_4x4_RGBA,
cTFATC_RGBA_INTERPOLATED_ALPHA = cTFATC_RGBA,
};
uint32_t basis_get_bytes_per_block(transcoder_texture_format fmt);
// For compressed texture formats, this returns the # of bytes per block. For uncompressed, it returns the # of bytes per pixel.
// NOTE: Previously, this function was called basis_get_bytes_per_block(), and it always returned 16*bytes_per_pixel for uncompressed formats which was confusing.
uint32_t basis_get_bytes_per_block_or_pixel(transcoder_texture_format fmt);
// Returns format's name in ASCII
const char* basis_get_format_name(transcoder_texture_format fmt);
// Returns true if the format supports an alpha channel.
bool basis_transcoder_format_has_alpha(transcoder_texture_format fmt);
// Returns the basisu::texture_format corresponding to the specified transcoder_texture_format.
basisu::texture_format basis_get_basisu_texture_format(transcoder_texture_format fmt);
// Returns the texture type's name in ASCII.
const char* basis_get_texture_type_name(basis_texture_type tex_type);
// Returns true if the transcoder texture type is an uncompressed (raw pixel) format.
bool basis_transcoder_format_is_uncompressed(transcoder_texture_format tex_type);
// Returns the # of bytes per pixel for uncompressed formats, or 0 for block texture formats.
uint32_t basis_get_uncompressed_bytes_per_pixel(transcoder_texture_format fmt);
// Returns the block width for the specified texture format, which is currently either 4 or 8 for FXT1.
uint32_t basis_get_block_width(transcoder_texture_format tex_type);
// Returns the block height for the specified texture format, which is currently always 4.
uint32_t basis_get_block_height(transcoder_texture_format tex_type);
// Returns true if the specified format was enabled at compile time.
bool basis_is_format_supported(transcoder_texture_format tex_type);
bool basis_is_format_supported(transcoder_texture_format tex_type, basis_tex_format fmt = basis_tex_format::cETC1S);
class basisu_transcoder;
@@ -125,12 +150,12 @@ namespace basist
};
// Low-level helper class that does the actual transcoding.
class basisu_lowlevel_transcoder
class basisu_lowlevel_etc1s_transcoder
{
friend class basisu_transcoder;
public:
basisu_lowlevel_transcoder(const basist::etc1_global_selector_codebook *pGlobal_sel_codebook);
basisu_lowlevel_etc1s_transcoder(const basist::etc1_global_selector_codebook *pGlobal_sel_codebook);
bool decode_palettes(
uint32_t num_endpoints, const uint8_t *pEndpoints_data, uint32_t endpoints_data_size,
@@ -142,6 +167,17 @@ namespace basist
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header &header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0,
basisu_transcoder_state *pState = nullptr, bool astc_transcode_alpha = false, void* pAlpha_blocks = nullptr, uint32_t output_rows_in_pixels = 0);
void clear()
{
m_endpoints.clear();
m_selectors.clear();
m_endpoint_pred_model.clear();
m_delta_endpoint_model.clear();
m_selector_model.clear();
m_selector_history_buf_rle_model.clear();
m_selector_history_buf_size = 0;
}
private:
typedef std::vector<endpoint> endpoint_vec;
endpoint_vec m_endpoints;
@@ -158,6 +194,38 @@ namespace basist
basisu_transcoder_state m_def_state;
};
enum
{
// PVRTC1: decode non-pow2 ETC1S texture level to the next larger power of 2 (not implemented yet, but we're going to support it). Ignored if the slice's dimensions are already a power of 2.
cDecodeFlagsPVRTCDecodeToNextPow2 = 2,
// When decoding to an opaque texture format, if the basis file has alpha, decode the alpha slice instead of the color slice to the output texture format.
// This is primarily to allow decoding of textures with alpha to multiple ETC1 textures (one for color, another for alpha).
cDecodeFlagsTranscodeAlphaDataToOpaqueFormats = 4,
// Forbid usage of BC1 3 color blocks (we don't support BC1 punchthrough alpha yet).
// This flag is used internally when decoding to BC3.
cDecodeFlagsBC1ForbidThreeColorBlocks = 8,
// The output buffer contains alpha endpoint/selector indices.
// Used internally when decoding formats like ASTC that require both color and alpha data to be available when transcoding to the output format.
cDecodeFlagsOutputHasAlphaIndices = 16,
cDecodeFlagsHighQuality = 32
};
class basisu_lowlevel_uastc_transcoder
{
friend class basisu_transcoder;
public:
basisu_lowlevel_uastc_transcoder();
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt,
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header& header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0,
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0);
};
struct basisu_slice_info
{
uint32_t m_orig_width;
@@ -251,10 +319,12 @@ namespace basist
uint32_t m_userdata0;
uint32_t m_userdata1;
bool m_etc1s; // always true for basis universal
basis_tex_format m_tex_format; // ETC1S, UASTC, etc.
bool m_y_flipped; // true if the image was Y flipped
bool m_has_alpha_slices; // true if the texture has alpha slices (even slices RGB, odd slices alpha)
bool m_etc1s; // true if the file is ETC1S
bool m_has_alpha_slices; // true if the texture has alpha slices (for ETC1S: even slices RGB, odd slices alpha)
};
// High-level transcoder class which accepts .basis file data and allows the caller to query information about the file and transcode image levels to various texture formats.
@@ -280,6 +350,8 @@ namespace basist
// Note that the number of mipmap levels for each image may differ, and that images may have different resolutions.
uint32_t get_total_images(const void *pData, uint32_t data_size) const;
basis_tex_format get_tex_format(const void* pData, uint32_t data_size) const;
// Returns the number of mipmap levels in an image.
uint32_t get_total_image_levels(const void *pData, uint32_t data_size, uint32_t image_index) const;
@@ -296,30 +368,14 @@ namespace basist
bool get_file_info(const void *pData, uint32_t data_size, basisu_file_info &file_info) const;
// start_transcoding() must be called before calling transcode_slice() or transcode_image_level().
// This decompresses the selector/endpoint codebooks, so ideally you would only call this once per .basis file (not each image/mipmap level).
bool start_transcoding(const void *pData, uint32_t data_size) const;
// For ETC1S files, this call decompresses the selector/endpoint codebooks, so ideally you would only call this once per .basis file (not each image/mipmap level).
bool start_transcoding(const void *pData, uint32_t data_size);
bool stop_transcoding();
// Returns true if start_transcoding() has been called.
bool get_ready_to_transcode() const { return m_lowlevel_decoder.m_endpoints.size() > 0; }
enum
{
// PVRTC1: decode non-pow2 ETC1S texture level to the next larger power of 2 (not implemented yet, but we're going to support it). Ignored if the slice's dimensions are already a power of 2.
cDecodeFlagsPVRTCDecodeToNextPow2 = 2,
// When decoding to an opaque texture format, if the basis file has alpha, decode the alpha slice instead of the color slice to the output texture format.
// This is primarily to allow decoding of textures with alpha to multiple ETC1 textures (one for color, another for alpha).
cDecodeFlagsTranscodeAlphaDataToOpaqueFormats = 4,
// Forbid usage of BC1 3 color blocks (we don't support BC1 punchthrough alpha yet).
// This flag is used internally when decoding to BC3.
cDecodeFlagsBC1ForbidThreeColorBlocks = 8,
// The output buffer contains alpha endpoint/selector indices.
// Used internally when decoding formats like ASTC that require both color and alpha data to be available when transcoding to the output format.
cDecodeFlagsOutputHasAlphaIndices = 16
};
bool get_ready_to_transcode() const { return m_ready_to_transcode; }
// transcode_image_level() decodes a single mipmap level from the .basis file to any of the supported output texture formats.
// It'll first find the slice(s) to transcode, then call transcode_slice() one or two times to decode both the color and alpha texture data (or RG texture data from two slices for BC5).
// If the .basis file doesn't have alpha slices, the output alpha blocks will be set to fully opaque (all 255's).
@@ -352,10 +408,14 @@ namespace basist
// - basisu_transcoder_init() must have been called first to initialize the transcoder lookup tables before calling this function.
bool transcode_slice(const void *pData, uint32_t data_size, uint32_t slice_index,
void *pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
block_format fmt, uint32_t output_block_stride_in_bytes, uint32_t decode_flags = 0, uint32_t output_row_pitch_in_blocks_or_pixels = 0, basisu_transcoder_state * pState = nullptr, void* pAlpha_blocks = nullptr, uint32_t output_rows_in_pixels = 0) const;
block_format fmt, uint32_t output_block_stride_in_bytes, uint32_t decode_flags = 0, uint32_t output_row_pitch_in_blocks_or_pixels = 0, basisu_transcoder_state * pState = nullptr, void* pAlpha_blocks = nullptr,
uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1) const;
private:
mutable basisu_lowlevel_transcoder m_lowlevel_decoder;
mutable basisu_lowlevel_etc1s_transcoder m_lowlevel_etc1s_decoder;
mutable basisu_lowlevel_uastc_transcoder m_lowlevel_uastc_decoder;
bool m_ready_to_transcode;
int find_first_slice_index(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index) const;
+29 -11
View File
@@ -1,5 +1,5 @@
// basisu_transcoder_internal.h - Universal texture format transcoder library.
// Copyright (C) 2019 Binomial LLC. All Rights Reserved.
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
//
@@ -20,8 +20,8 @@
#pragma warning (disable: 4127) // conditional expression is constant
#endif
#define BASISD_LIB_VERSION 107
#define BASISD_VERSION_STRING "01.11"
#define BASISD_LIB_VERSION 112
#define BASISD_VERSION_STRING "01.12"
#ifdef _DEBUG
#define BASISD_BUILD_DEBUG
@@ -45,38 +45,44 @@ namespace basist
enum class block_format
{
cETC1, // ETC1S RGB
cETC2_RGBA, // full ETC2 EAC RGBA8 block
cBC1, // DXT1 RGB
cBC3, // BC4 block followed by a four color BC1 block
cBC4, // DXT5A (alpha block only)
cBC5, // two BC4 blocks
cPVRTC1_4_RGB, // opaque-only PVRTC1 4bpp
cPVRTC1_4_RGBA, // PVRTC1 4bpp RGBA
cBC7_M6_OPAQUE_ONLY, // RGB BC7 mode 6
cBC7, // Full BC7 block, any mode
cBC7_M5_COLOR, // RGB BC7 mode 5 color (writes an opaque mode 5 block)
cBC7_M5_ALPHA, // alpha portion of BC7 mode 5 (cBC7_M5_COLOR output data must have been written to the output buffer first to set the mode/rot fields etc.)
cETC2_EAC_A8, // alpha block of ETC2 EAC (first 8 bytes of the 16-bit ETC2 EAC RGBA format)
cASTC_4x4, // ASTC 4x4 (either color-only or color+alpha). Note that the transcoder always currently assumes sRGB is not enabled when outputting ASTC
// data. If you use a sRGB ASTC format you'll get ~1 LSB of additional error, because of the different way ASTC decoders scale 8-bit endpoints to 16-bits during unpacking.
cATC_RGB,
cATC_RGBA_INTERPOLATED_ALPHA,
cFXT1_RGB, // Opaque-only, has oddball 8x4 pixel block size
cPVRTC2_4_RGB,
cPVRTC2_4_RGBA,
cETC2_EAC_R11,
cETC2_EAC_RG11,
cIndices, // Used internally: Write 16-bit endpoint and selector indices directly to output (output block must be at least 32-bits)
cRGB32, // Writes RGB components to 32bpp output pixels
cRGBA32, // Writes RGB255 components to 32bpp output pixels
cA32, // Writes alpha component to 32bpp output pixels
cRGB565,
cBGR565,
cRGBA4444_COLOR,
cRGBA4444_ALPHA,
cRGBA4444_COLOR_OPAQUE,
cPVRTC2_4_RGB,
cPVRTC2_4_RGBA,
cETC2_EAC_R11,
cRGBA4444,
cTotalBlockFormats
};
@@ -635,6 +641,11 @@ namespace basist
return (uint8_t)((i & 0xFFFFFF00U) ? (~(i >> 31)) : i);
}
enum eNoClamp
{
cNoClamp = 0
};
struct color32
{
union
@@ -655,15 +666,22 @@ namespace basist
color32() { }
color32(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
color32(eNoClamp unused, uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { (void)unused; set_noclamp_rgba(vr, vg, vb, va); }
void set(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { c[0] = static_cast<uint8_t>(vr); c[1] = static_cast<uint8_t>(vg); c[2] = static_cast<uint8_t>(vb); c[3] = static_cast<uint8_t>(va); }
void set_noclamp_rgb(uint32_t vr, uint32_t vg, uint32_t vb) { c[0] = static_cast<uint8_t>(vr); c[1] = static_cast<uint8_t>(vg); c[2] = static_cast<uint8_t>(vb); }
void set_noclamp_rgba(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
void set_clamped(int vr, int vg, int vb, int va) { c[0] = clamp255(vr); c[1] = clamp255(vg); c[2] = clamp255(vb); c[3] = clamp255(va); }
uint8_t operator[] (uint32_t idx) const { assert(idx < 4); return c[idx]; }
uint8_t &operator[] (uint32_t idx) { assert(idx < 4); return c[idx]; }
bool operator== (const color32&rhs) const { return m == rhs.m; }
static color32 comp_min(const color32& a, const color32& b) { return color32(cNoClamp, std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]), std::min(a[3], b[3])); }
static color32 comp_max(const color32& a, const color32& b) { return color32(cNoClamp, std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]), std::max(a[3], b[3])); }
};
struct endpoint
File diff suppressed because it is too large Load Diff
+294
View File
@@ -0,0 +1,294 @@
// basisu_transcoder_uastc.h
#pragma once
#include "basisu_transcoder_internal.h"
namespace basist
{
struct color_quad_u8
{
uint8_t m_c[4];
};
const uint32_t TOTAL_UASTC_MODES = 19;
const uint32_t UASTC_MODE_INDEX_SOLID_COLOR = 8;
const uint32_t TOTAL_ASTC_BC7_COMMON_PARTITIONS2 = 30;
const uint32_t TOTAL_ASTC_BC7_COMMON_PARTITIONS3 = 11;
const uint32_t TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS = 19;
extern const uint8_t g_uastc_mode_weight_bits[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_weight_ranges[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_endpoint_ranges[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_subsets[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_planes[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_comps[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_has_etc1_bias[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_has_bc1_hint0[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_has_bc1_hint1[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_has_alpha[TOTAL_UASTC_MODES];
extern const uint8_t g_uastc_mode_is_la[TOTAL_UASTC_MODES];
struct astc_bc7_common_partition2_desc
{
uint8_t m_bc7;
uint16_t m_astc;
bool m_invert;
};
extern const astc_bc7_common_partition2_desc g_astc_bc7_common_partitions2[TOTAL_ASTC_BC7_COMMON_PARTITIONS2];
struct bc73_astc2_common_partition_desc
{
uint8_t m_bc73;
uint16_t m_astc2;
uint8_t k; // 0-5 - how to modify the BC7 3-subset pattern to match the ASTC pattern (LSB=invert)
};
extern const bc73_astc2_common_partition_desc g_bc7_3_astc2_common_partitions[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS];
struct astc_bc7_common_partition3_desc
{
uint8_t m_bc7;
uint16_t m_astc;
uint8_t m_astc_to_bc7_perm; // converts ASTC to BC7 partition using g_astc_bc7_partition_index_perm_tables[][]
};
extern const astc_bc7_common_partition3_desc g_astc_bc7_common_partitions3[TOTAL_ASTC_BC7_COMMON_PARTITIONS3];
extern const uint8_t g_astc_bc7_patterns2[TOTAL_ASTC_BC7_COMMON_PARTITIONS2][16];
extern const uint8_t g_astc_bc7_patterns3[TOTAL_ASTC_BC7_COMMON_PARTITIONS3][16];
extern const uint8_t g_bc7_3_astc2_patterns2[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS][16];
extern const uint8_t g_astc_bc7_pattern2_anchors[TOTAL_ASTC_BC7_COMMON_PARTITIONS2][3];
extern const uint8_t g_astc_bc7_pattern3_anchors[TOTAL_ASTC_BC7_COMMON_PARTITIONS3][3];
extern const uint8_t g_bc7_3_astc2_patterns2_anchors[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS][3];
extern const uint32_t g_uastc_mode_huff_codes[TOTAL_UASTC_MODES + 1][2];
extern const uint8_t g_astc_to_bc7_partition_index_perm_tables[6][3];
extern const uint8_t g_bc7_to_astc_partition_index_perm_tables[6][3]; // inverse of g_astc_to_bc7_partition_index_perm_tables
extern const uint8_t* s_uastc_to_bc1_weights[6];
uint32_t bc7_convert_partition_index_3_to_2(uint32_t p, uint32_t k);
inline uint32_t astc_interpolate(uint32_t l, uint32_t h, uint32_t w, bool srgb)
{
if (srgb)
{
l = (l << 8) | 0x80;
h = (h << 8) | 0x80;
}
else
{
l = (l << 8) | l;
h = (h << 8) | h;
}
uint32_t k = (l * (64 - w) + h * w + 32) >> 6;
return k >> 8;
}
struct astc_block_desc
{
int m_weight_range; // weight BISE range
int m_subsets; // number of ASTC partitions
int m_partition_seed; // partition pattern seed
int m_cem; // color endpoint mode used by all subsets
int m_ccs; // color component selector (dual plane only)
bool m_dual_plane; // true if dual plane
// Weight and endpoint BISE values.
// Note these values are NOT linear, they must be BISE encoded. See Table 97 and Table 107.
uint8_t m_endpoints[18]; // endpoint values, in RR GG BB etc. order
uint8_t m_weights[64]; // weight index values, raster order, in P0 P1, P0 P1, etc. or P0, P0, P0, P0, etc. order
};
const uint32_t BC7ENC_TOTAL_ASTC_RANGES = 21;
// See tables 81, 93, 18.13.Endpoint Unquantization
const uint32_t TOTAL_ASTC_RANGES = 21;
extern const int g_astc_bise_range_table[TOTAL_ASTC_RANGES][3];
struct astc_quant_bin
{
uint8_t m_unquant; // unquantized value
uint8_t m_index; // sorted index
};
extern astc_quant_bin g_astc_unquant[BC7ENC_TOTAL_ASTC_RANGES][256]; // [ASTC encoded endpoint index]
int astc_get_levels(int range);
bool astc_is_valid_endpoint_range(uint32_t range);
uint32_t unquant_astc_endpoint(uint32_t packed_bits, uint32_t packed_trits, uint32_t packed_quints, uint32_t range);
uint32_t unquant_astc_endpoint_val(uint32_t packed_val, uint32_t range);
const uint8_t* get_anchor_indices(uint32_t subsets, uint32_t mode, uint32_t common_pattern, const uint8_t*& pPartition_pattern);
// BC7
const uint32_t BC7ENC_BLOCK_SIZE = 16;
struct bc7_block
{
uint64_t m_qwords[2];
};
struct bc7_optimization_results
{
uint32_t m_mode;
uint32_t m_partition;
uint8_t m_selectors[16];
uint8_t m_alpha_selectors[16];
color_quad_u8 m_low[3];
color_quad_u8 m_high[3];
uint32_t m_pbits[3][2];
uint32_t m_index_selector;
uint32_t m_rotation;
};
extern const uint32_t g_bc7_weights1[2];
extern const uint32_t g_bc7_weights2[4];
extern const uint32_t g_bc7_weights3[8];
extern const uint32_t g_bc7_weights4[16];
extern const uint32_t g_astc_weights4[16];
extern const uint32_t g_astc_weights5[32];
extern const uint32_t g_astc_weights_3levels[3];
extern const uint8_t g_bc7_partition1[16];
extern const uint8_t g_bc7_partition2[64 * 16];
extern const uint8_t g_bc7_partition3[64 * 16];
extern const uint8_t g_bc7_table_anchor_index_second_subset[64];
extern const uint8_t g_bc7_table_anchor_index_third_subset_1[64];
extern const uint8_t g_bc7_table_anchor_index_third_subset_2[64];
extern const uint8_t g_bc7_num_subsets[8];
extern const uint8_t g_bc7_partition_bits[8];
extern const uint8_t g_bc7_color_index_bitcount[8];
extern const uint8_t g_bc7_mode_has_p_bits[8];
extern const uint8_t g_bc7_mode_has_shared_p_bits[8];
extern const uint8_t g_bc7_color_precision_table[8];
extern const int8_t g_bc7_alpha_precision_table[8];
extern const uint8_t g_bc7_alpha_index_bitcount[8];
inline bool get_bc7_mode_has_seperate_alpha_selectors(int mode) { return (mode == 4) || (mode == 5); }
inline int get_bc7_color_index_size(int mode, int index_selection_bit) { return g_bc7_color_index_bitcount[mode] + index_selection_bit; }
inline int get_bc7_alpha_index_size(int mode, int index_selection_bit) { return g_bc7_alpha_index_bitcount[mode] - index_selection_bit; }
struct endpoint_err
{
uint16_t m_error; uint8_t m_lo; uint8_t m_hi;
};
extern endpoint_err g_bc7_mode_6_optimal_endpoints[256][2]; // [c][pbit]
const uint32_t BC7ENC_MODE_6_OPTIMAL_INDEX = 5;
extern endpoint_err g_bc7_mode_5_optimal_endpoints[256]; // [c]
const uint32_t BC7ENC_MODE_5_OPTIMAL_INDEX = 1;
// Packs a BC7 block from a high-level description. Handles all BC7 modes.
void encode_bc7_block(void* pBlock, const bc7_optimization_results* pResults);
// Packs an ASTC block
// Constraints: Always 4x4, all subset CEM's must be equal, only tested with LDR CEM's.
bool pack_astc_block(uint32_t* pDst, const astc_block_desc* pBlock, uint32_t mode);
void pack_astc_solid_block(void* pDst_block, const color32& color);
#ifdef _DEBUG
int astc_compute_texel_partition(int seed, int x, int y, int z, int partitioncount, bool small_block);
#endif
struct uastc_block
{
union
{
uint8_t m_bytes[16];
uint32_t m_dwords[4];
uint64_t m_qwords[2];
};
};
struct unpacked_uastc_block
{
astc_block_desc m_astc;
uint32_t m_mode;
uint32_t m_common_pattern;
color32 m_solid_color;
bool m_bc1_hint0;
bool m_bc1_hint1;
bool m_etc1_flip;
bool m_etc1_diff;
uint32_t m_etc1_inten0;
uint32_t m_etc1_inten1;
uint32_t m_etc1_bias;
uint32_t m_etc2_hints;
uint32_t m_etc1_selector;
uint32_t m_etc1_r, m_etc1_g, m_etc1_b;
};
color32 apply_etc1_bias(const color32 &block_color, uint32_t bias, uint32_t limit, uint32_t subblock);
struct decoder_etc_block;
struct eac_block;
bool unpack_uastc(uint32_t mode, uint32_t common_pattern, const color32& solid_color, const astc_block_desc& astc, color32* pPixels, bool srgb);
bool unpack_uastc(const unpacked_uastc_block& unpacked_blk, color32* pPixels, bool srgb);
bool unpack_uastc(const uastc_block& blk, color32* pPixels, bool srgb);
bool unpack_uastc(const uastc_block& blk, unpacked_uastc_block& unpacked, bool undo_blue_contract, bool read_hints = true);
bool transcode_uastc_to_astc(const uastc_block& src_blk, void* pDst);
bool transcode_uastc_to_bc7(const unpacked_uastc_block& unpacked_src_blk, bc7_optimization_results& dst_blk);
bool transcode_uastc_to_bc7(const uastc_block& src_blk, bc7_optimization_results& dst_blk);
bool transcode_uastc_to_bc7(const uastc_block& src_blk, void* pDst);
void transcode_uastc_to_etc1(unpacked_uastc_block& unpacked_src_blk, color32 block_pixels[4][4], void* pDst);
bool transcode_uastc_to_etc1(const uastc_block& src_blk, void* pDst);
bool transcode_uastc_to_etc1(const uastc_block& src_blk, void* pDst, uint32_t channel);
void transcode_uastc_to_etc2_eac_a8(unpacked_uastc_block& unpacked_src_blk, color32 block_pixels[4][4], void* pDst);
bool transcode_uastc_to_etc2_rgba(const uastc_block& src_blk, void* pDst);
// Packs 16 scalar values to BC4. Same PSNR as stb_dxt's BC4 encoder, around 13% faster.
void encode_bc4(void* pDst, const uint8_t* pPixels, uint32_t stride);
void encode_bc1_solid_block(void* pDst, uint32_t fr, uint32_t fg, uint32_t fb);
enum
{
cEncodeBC1HighQuality = 1,
cEncodeBC1HigherQuality = 2,
cEncodeBC1UseSelectors = 4,
};
void encode_bc1(void* pDst, const uint8_t* pPixels, uint32_t flags);
// Alternate PCA-free encoder, around 15% faster, same (or slightly higher) avg. PSNR
void encode_bc1_alt(void* pDst, const uint8_t* pPixels, uint32_t flags);
void transcode_uastc_to_bc1_hint0(const unpacked_uastc_block& unpacked_src_blk, void* pDst);
void transcode_uastc_to_bc1_hint1(const unpacked_uastc_block& unpacked_src_blk, const color32 block_pixels[4][4], void* pDst, bool high_quality);
bool transcode_uastc_to_bc1(const uastc_block& src_blk, void* pDst, bool high_quality);
bool transcode_uastc_to_bc3(const uastc_block& src_blk, void* pDst, bool high_quality);
bool transcode_uastc_to_bc4(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0);
bool transcode_uastc_to_bc5(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0, uint32_t chan1);
bool transcode_uastc_to_etc2_eac_r11(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0);
bool transcode_uastc_to_etc2_eac_rg11(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0, uint32_t chan1);
bool transcode_uastc_to_pvrtc1_4_rgb(const uastc_block* pSrc_blocks, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, bool high_quality, bool from_alpha);
bool transcode_uastc_to_pvrtc1_4_rgba(const uastc_block* pSrc_blocks, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, bool high_quality);
// uastc_init() MUST be called before using this module.
void uastc_init();
} // namespace basist
+100 -73
View File
@@ -20,7 +20,6 @@ THREE.BasisTextureLoader = function ( manager ) {
THREE.Loader.call( this, manager );
this.useAlpha = true;
this.transcoderPath = '';
this.transcoderBinary = null;
this.transcoderPending = null;
@@ -30,12 +29,11 @@ THREE.BasisTextureLoader = function ( manager ) {
this.workerNextTaskID = 1;
this.workerSourceURL = '';
this.workerConfig = {
format: null,
useAlpha: true,
astcSupported: false,
format: null,
astcSupported: false,
etcSupported: false,
dxtSupported: false,
pvrtcSupported: false,
pvrtcSupported: false,
};
};
@@ -64,28 +62,33 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var config = this.workerConfig;
config.astcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_astc' );
config.bc7Supported = !! renderer.extensions.get( 'EXT_texture_compression_bptc' );
config.astcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_astc' );
config.etcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_etc1' );
config.dxtSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_s3tc' );
config.pvrtcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_pvrtc' )
|| !! renderer.extensions.get( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
|| !! renderer.extensions.get( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
if (config.astcSupported) {
if ( config.astcSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4;
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4;
} else if ( config.dxtSupported ) {
} else if ( config.bc7Supported ) {
config.format = this.useAlpha ? THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3 : THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1;
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC7_M6_OPAQUE_ONLY;
} else if ( config.dxtSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3;
} else if ( config.etcSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1;
} else if ( config.pvrtcSupported ) {
config.format = this.useAlpha ? THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA : THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB;
} else if ( config.etcSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1;
} else {
throw new Error( 'THREE.BasisTextureLoader: No suitable compressed texture format found.' );
@@ -121,7 +124,9 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var worker;
var taskID;
var texturePending = this._getWorker()
var taskCost = buffer.byteLength;
var texturePending = this._allocateWorker( taskCost )
.then( ( _worker ) => {
worker = _worker;
@@ -130,8 +135,6 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
return new Promise( ( resolve, reject ) => {
worker._callbacks[ taskID ] = { resolve, reject };
worker._taskCosts[ taskID ] = buffer.byteLength;
worker._taskLoad += worker._taskCosts[ taskID ];
worker.postMessage( { type: 'transcode', id: taskID, buffer }, [ buffer ] );
@@ -142,30 +145,35 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var config = this.workerConfig;
var { width, height, hasAlpha, mipmaps } = message;
var { width, height, mipmaps, format } = message;
var texture;
switch(config.format) {
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.COMPRESSED_RGBA_ASTC_4x4_KHR );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1:
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], THREE.UnsignedByteType );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_ETC1_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_PVRTC_4BPPV1_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGBA_PVRTC_4BPPV1_Format );
break;
default:
throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
}
switch ( format ) {
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGBA_ASTC_4x4_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC7_M6_OPAQUE_ONLY:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.COMPRESSED_RGBA_BPTC_UNORM );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1:
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], THREE.UnsignedByteType );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_ETC1_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_PVRTC_4BPPV1_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGBA_PVRTC_4BPPV1_Format );
break;
default:
throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
}
texture.minFilter = mipmaps.length === 1 ? THREE.LinearFilter : THREE.LinearMipmapLinearFilter;
texture.magFilter = THREE.LinearFilter;
@@ -181,9 +189,8 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
if ( worker && taskID ) {
worker._taskLoad -= worker._taskCosts[ taskID ];
worker._taskLoad -= taskCost;
delete worker._callbacks[ taskID ];
delete worker._taskCosts[ taskID ];
}
@@ -195,7 +202,7 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
_initTranscoder: function () {
if ( ! this.transcoderBinary ) {
if ( ! this.transcoderPending ) {
// Load transcoder wrapper.
var jsLoader = new THREE.FileLoader( this.manager );
@@ -239,7 +246,7 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
},
_getWorker: function () {
_allocateWorker: function ( taskCost ) {
return this._initTranscoder().then( () => {
@@ -248,10 +255,8 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var worker = new Worker( this.workerSourceURL );
worker._callbacks = {};
worker._taskCosts = {};
worker._taskLoad = 0;
this.workerConfig.useAlpha = this.useAlpha;
worker.postMessage( {
type: 'init',
config: this.workerConfig,
@@ -291,7 +296,11 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
}
return this.workerPool[ this.workerPool.length - 1 ];
var worker = this.workerPool[ this.workerPool.length - 1 ];
worker._taskLoad += taskCost;
return worker;
} );
@@ -323,16 +332,16 @@ THREE.BasisTextureLoader.BASIS_FORMAT = {
cTFBC4: 4,
cTFBC5: 5,
cTFBC7_M6_OPAQUE_ONLY: 6,
cTFBC7_M5: 7,
cTFPVRTC1_4_RGB: 8,
cTFPVRTC1_4_RGBA: 9,
cTFASTC_4x4: 10,
cTFATC_RGB: 11,
cTFATC_RGBA_INTERPOLATED_ALPHA: 12,
cTFRGBA32: 13,
cTFRGB565: 14,
cTFBGR565: 15,
cTFRGBA4444: 16,
cTFBC7_M5: 7,
cTFPVRTC1_4_RGB: 8,
cTFPVRTC1_4_RGBA: 9,
cTFASTC_4x4: 10,
cTFATC_RGB: 11,
cTFATC_RGBA_INTERPOLATED_ALPHA: 12,
cTFRGBA32: 13,
cTFRGB565: 14,
cTFBGR565: 15,
cTFRGBA4444: 16,
};
// DXT formats, from:
@@ -347,12 +356,16 @@ THREE.BasisTextureLoader.DXT_FORMAT_MAP = {};
THREE.BasisTextureLoader.DXT_FORMAT_MAP[ THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1 ] =
THREE.BasisTextureLoader.DXT_FORMAT.COMPRESSED_RGB_S3TC_DXT1_EXT;
THREE.BasisTextureLoader.DXT_FORMAT_MAP[ THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3 ] =
THREE.BasisTextureLoader.DXT_FORMAT.COMPRESSED_RGBA_S3TC_DXT5_EXT;
THREE.BasisTextureLoader.DXT_FORMAT.COMPRESSED_RGBA_S3TC_DXT5_EXT;
// ASTC formats, from:
// ASTC formats, from:
// https://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_astc/
THREE.BasisTextureLoader.COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93B0;
// BC7/BPTC format, from:
// https://www.khronos.org/registry/webgl/extensions/EXT_texture_compression_bptc/
THREE.BasisTextureLoader.COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C;
/* WEB WORKER */
THREE.BasisTextureLoader.BasisWorker = function () {
@@ -377,7 +390,7 @@ THREE.BasisTextureLoader.BasisWorker = function () {
try {
var { width, height, hasAlpha, mipmaps } = transcode( message.buffer );
var { width, height, hasAlpha, mipmaps, format } = transcode( message.buffer );
var buffers = [];
@@ -387,7 +400,7 @@ THREE.BasisTextureLoader.BasisWorker = function () {
}
self.postMessage( { type: 'transcode', id: message.id, width, height, hasAlpha, mipmaps }, buffers );
self.postMessage( { type: 'transcode', id: message.id, width, height, hasAlpha, mipmaps, format }, buffers );
} catch ( error ) {
@@ -406,15 +419,15 @@ THREE.BasisTextureLoader.BasisWorker = function () {
function init( wasmBinary ) {
var m;
transcoderPending = new Promise( ( resolve ) => {
var BasisModule;
transcoderPending = new Promise( ( resolve ) => {
m = { wasmBinary, onRuntimeInitialized: resolve };
BASIS(m);
BasisModule = { wasmBinary, onRuntimeInitialized: resolve };
BASIS( BasisModule );
} ).then( () => {
var { BasisFile, initializeBasis } = m;
var { BasisFile, initializeBasis } = BasisModule;
_BasisFile = BasisFile;
@@ -430,8 +443,8 @@ THREE.BasisTextureLoader.BasisWorker = function () {
var width = basisFile.getImageWidth( 0, 0 );
var height = basisFile.getImageHeight( 0, 0 );
var levels = basisFile.getNumLevels( 0 );
var hasAlpha = basisFile.getHasAlpha();
var levels = basisFile.getNumLevels( 0 );
var hasAlpha = basisFile.getHasAlpha();
function cleanup() {
@@ -440,6 +453,20 @@ THREE.BasisTextureLoader.BasisWorker = function () {
}
if ( ! hasAlpha ) {
switch ( config.format ) {
case 9: // Hardcoded: THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA
config.format = 8; // Hardcoded: THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB;
break;
default:
break;
}
}
if ( ! width || ! height || ! levels ) {
cleanup();
@@ -466,9 +493,9 @@ THREE.BasisTextureLoader.BasisWorker = function () {
dst,
0,
mip,
config.format,
config.useAlpha,
0
config.format,
0,
hasAlpha
);
if ( ! status ) {
@@ -484,8 +511,8 @@ THREE.BasisTextureLoader.BasisWorker = function () {
cleanup();
return { width, height, hasAlpha, mipmaps };
return { width, height, hasAlpha, mipmaps, format: config.format };
}
};
};
+528 -561
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.
+8 -4
View File
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<head>
<script src="https://cdn.jsdelivr.net/npm/three@v0.108.0"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/three@v0.108.0"></script> -->
<script src="three.min.js"></script>
<script src="GLTFLoader.js"></script>
<script src="BasisTextureLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@v0.104.0/examples/js/controls/OrbitControls.js"></script>
<!-- <script src="../transcoder/build/basis_transcoder.js"></script> -->
<style>
html, body { width:100%; height:100%; margin:0; padding:0; }
canvas { display:block; }
@@ -60,6 +60,9 @@
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3:
formatName = 'BC3';
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC7_M6_OPAQUE_ONLY:
formatName = 'BC7';
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB:
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA:
formatName = 'PVRTC';
@@ -74,11 +77,12 @@
// Register BasisTextureLoader for .basis extension.
THREE.Loader.Handlers.add( /\.basis$/, basisLoader );
let loadingManager = new THREE.LoadingManager();
loadingManager.addHandler( /\.basis$/, basisLoader );
// Create GLTFLoader, load model, and render.
const loader = new THREE.GLTFLoader();
const loader = new THREE.GLTFLoader(loadingManager);
loader.load( 'assets/AgiHqSmall.gltf', ( gltf ) => {
+1
View File
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
+21 -4
View File
@@ -90,6 +90,10 @@ COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
// BC7 format, from:
// https://www.khronos.org/registry/webgl/extensions/EXT_texture_compression_bptc/
COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C;
// ETC format, from:
// https://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_etc1/
COMPRESSED_RGB_ETC1_WEBGL = 0x8D64;
@@ -106,8 +110,7 @@ BASIS_FORMAT = {
cTFBC3: 3,
cTFBC4: 4,
cTFBC5: 5,
cTFBC7_M6_OPAQUE_ONLY: 6,
cTFBC7_M5: 7,
cTFBC7: 6,
cTFPVRTC1_4_RGB: 8,
cTFPVRTC1_4_RGBA: 9,
cTFASTC_4x4: 10,
@@ -127,10 +130,12 @@ for (var name in BASIS_FORMAT) {
DXT_FORMAT_MAP = {};
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC1] = COMPRESSED_RGB_S3TC_DXT1_EXT;
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC3] = COMPRESSED_RGBA_S3TC_DXT5_EXT;
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC7] = COMPRESSED_RGBA_BPTC_UNORM;
var astcSupported = false;
var etcSupported = false;
var dxtSupported = false;
var bc7Supported = false;
var pvrtcSupported = false;
var drawMode = 0;
@@ -167,6 +172,9 @@ function dataLoaded(data)
basisFile.delete();
return;
}
// Note: If the file is UASTC, the preferred formats are ASTC/BC7.
// If the file is ETC1S and doesn't have alpha, the preferred formats are ETC1 and BC1. For alpha, the preferred formats are ETC2, BC3 or BC7.
var formatString = 'UNKNOWN';
if (astcSupported)
@@ -174,6 +182,11 @@ function dataLoaded(data)
formatString = 'ASTC';
format = BASIS_FORMAT.cTFASTC_4x4;
}
else if (bc7Supported)
{
formatString = 'BC7';
format = BASIS_FORMAT.cTFBC7;
}
else if (dxtSupported)
{
if (has_alpha)
@@ -235,6 +248,8 @@ function dataLoaded(data)
const dstSize = basisFile.getImageTranscodedSizeInBytes(0, 0, format);
const dst = new Uint8Array(dstSize);
log(dstSize);
// if (!basisFile.transcodeImage(dst, 0, 0, format, 1, 0)) {
if (!basisFile.transcodeImage(dst, 0, 0, format, 0, 0)) {
@@ -272,7 +287,7 @@ function dataLoaded(data)
{
tex = renderer.createCompressedTexture(dst, alignedWidth, alignedHeight, COMPRESSED_RGBA_ASTC_4x4_KHR);
}
else if ((format === BASIS_FORMAT.cTFBC3 || format === BASIS_FORMAT.cTFBC1))
else if ((format === BASIS_FORMAT.cTFBC3) || (format === BASIS_FORMAT.cTFBC1) || (format == BASIS_FORMAT.cTFBC7))
{
tex = renderer.createCompressedTexture(dst, alignedWidth, alignedHeight, DXT_FORMAT_MAP[format]);
}
@@ -332,7 +347,7 @@ function viewAlpha() { drawMode = 2; redraw(); }
<br>
<br>
.basis file:
<input id="file" type="text" size=30 value="assets/kodim20_1024x1024.basis"></input>
<input id="file" type="text" size=30 value="assets/kodim26_uastc_1024.basis"></input>
<input type="button" value="Run!" onclick="runLoadFile()"></input>
<br>
<br>
@@ -357,11 +372,13 @@ function viewAlpha() { drawMode = 2; redraw(); }
etcSupported = !!gl.getExtension('WEBGL_compressed_texture_etc1');
dxtSupported = !!gl.getExtension('WEBGL_compressed_texture_s3tc');
pvrtcSupported = !!(gl.getExtension('WEBGL_compressed_texture_pvrtc')) || !!(gl.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc'));
bc7Supported = !!gl.getExtension('EXT_texture_compression_bptc');
// HACK HACK - for testing uncompressed
//astcSupported = false;
//etcSupported = false;
//dxtSupported = false;
//bc7Supported = false;
//pvrtcSupported = false;
window.renderer = new Renderer(gl);
+1 -1
View File
@@ -10,7 +10,7 @@ if (EMSCRIPTEN)
basis_wrappers.cpp
)
target_compile_definitions(basis_transcoder.js PRIVATE NDEBUG BASISD_SUPPORT_BC7=0 BASISD_SUPPORT_ATC=0 BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY=0 BASISD_SUPPORT_PVRTC2=0 BASISD_SUPPORT_FXT1=0 BASISD_SUPPORT_ETC2_EAC_RG11=0)
target_compile_definitions(basis_transcoder.js PRIVATE NDEBUG BASISD_SUPPORT_UASTC=1 BASISD_SUPPORT_BC7=1 BASISD_SUPPORT_ATC=0 BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY=0 BASISD_SUPPORT_PVRTC2=0 BASISD_SUPPORT_FXT1=0 BASISD_SUPPORT_ETC2_EAC_RG11=0)
target_compile_options(basis_transcoder.js PRIVATE -O3)
target_include_directories(basis_transcoder.js PRIVATE ../../transcoder)
+14 -3
View File
@@ -130,7 +130,7 @@ struct basis_file
else
{
// Compressed formats are 2D arrays of blocks.
const uint32_t bytes_per_block = basis_get_bytes_per_block(transcoder_format);
const uint32_t bytes_per_block = basis_get_bytes_per_block_or_pixel(transcoder_format);
if (transcoder_format == transcoder_texture_format::cTFPVRTC1_4_RGB || transcoder_format == transcoder_texture_format::cTFPVRTC1_4_RGBA)
{
@@ -145,6 +145,14 @@ struct basis_file
return total_blocks * bytes_per_block;
}
}
bool isUASTC() {
assert(m_magic == MAGIC);
if (m_magic != MAGIC)
return false;
return m_transcoder.get_tex_format(m_file.data(), m_file.size()) == basis_tex_format::cUASTC4x4;
}
uint32_t startTranscoding() {
assert(m_magic == MAGIC);
@@ -172,7 +180,7 @@ struct basis_file
std::vector<uint8_t> dst_data;
uint32_t flags = get_alpha_for_opaque_formats ? basisu_transcoder::cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0;
uint32_t flags = get_alpha_for_opaque_formats ? cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0;
uint32_t status;
@@ -195,7 +203,7 @@ struct basis_file
}
else
{
uint32_t bytes_per_block = basis_get_bytes_per_block(transcoder_format);
uint32_t bytes_per_block = basis_get_bytes_per_block_or_pixel(transcoder_format);
uint32_t required_size = total_blocks * bytes_per_block;
@@ -239,6 +247,9 @@ EMSCRIPTEN_BINDINGS(basis_transcoder) {
.function("getHasAlpha", optional_override([](basis_file& self) {
return self.getHasAlpha();
}))
.function("isUASTC", optional_override([](basis_file& self) {
return self.isUASTC();
}))
.function("getNumImages", optional_override([](basis_file& self) {
return self.getNumImages();
}))
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
+22 -6
View File
@@ -25,6 +25,7 @@ extern "C"
uint basis_get_image_transcoded_size_in_bytes(void *h, uint image_index, uint level_index, uint format);
uint basis_start_transcoding(void *h);
uint basis_stop_transcoding(void *h);
uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
uint image_index, uint level_index, uint format,
@@ -184,14 +185,16 @@ uint basis_get_image_transcoded_size_in_bytes(void *h, uint image_index, uint le
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
return 0;
uint bytes_per_block = basis_get_bytes_per_block((transcoder_texture_format)format);
uint orig_width, orig_height, total_blocks;
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
return 0;
uint bytes_per_block_or_pixel = basis_get_bytes_per_block_or_pixel((transcoder_texture_format)format);
if (basis_transcoder_format_is_uncompressed((transcoder_texture_format)format))
return orig_width * orig_height * bytes_per_block_or_pixel;
return total_blocks * bytes_per_block;
return total_blocks * bytes_per_block_or_pixel;
}
uint basis_start_transcoding(void *h)
@@ -207,6 +210,19 @@ uint basis_start_transcoding(void *h)
return f->m_transcoder.start_transcoding(f->m_pFile, f->m_file_size);
}
uint basis_stop_transcoding(void *h)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
return f->m_transcoder.stop_transcoding();
}
uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
uint image_index, uint level_index, uint format,
uint unused, uint get_alpha_for_opaque_formats)
@@ -222,10 +238,10 @@ uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
return 0;
uint bytes_per_block = basis_get_bytes_per_block((transcoder_texture_format)format);
uint bytes_per_block = basis_get_bytes_per_block_or_pixel((transcoder_texture_format)format);
return f->m_transcoder.transcode_image_level(f->m_pFile, f->m_file_size, image_index, level_index,
dst, dst_size_in_bytes / bytes_per_block,
(basist::transcoder_texture_format)format,
(get_alpha_for_opaque_formats ? basisu_transcoder::cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0));
(get_alpha_for_opaque_formats ? (uint32_t)cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0));
}
+2 -1
View File
@@ -1,3 +1,4 @@
@REM -O0 -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1
@REM -O2 -s ASSERTIONS=0
emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding','_basis_get_debug_flags','_basis_set_debug_flags']" -o basis.js -DBASISD_ENABLE_DEBUG_FLAGS=1 ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -s TOTAL_MEMORY=500000000 -std=c++11 -O2 -s ASSERTIONS=0 -I ../transcoder
emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding','_basis_stop_transcoding','_basis_get_debug_flags','_basis_set_debug_flags']" -o basis.js -DBASISD_ENABLE_DEBUG_FLAGS=1 ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -s TOTAL_MEMORY=499974144 -std=c++11 -O2 -s ASSERTIONS=0 -I ../transcoder
rem emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding','_basis_stop_transcoding','_basis_get_debug_flags','_basis_set_debug_flags']" -o basis.js -DBASISD_ENABLE_DEBUG_FLAGS=1 ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -s TOTAL_MEMORY=499974144 -std=c++11 -O0 -s ASSERTIONS=0 -I ../transcoder
+68 -26
View File
@@ -85,6 +85,7 @@ COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C;
BASIS_FORMAT = {
@@ -94,8 +95,7 @@ BASIS_FORMAT = {
cTFBC3: 3,
cTFBC4: 4,
cTFBC5: 5,
cTFBC7_M6_OPAQUE_ONLY: 6,
cTFBC7_M5: 7,
cTFBC7: 6,
cTFPVRTC1_4_RGB: 8,
cTFPVRTC1_4_RGBA: 9,
@@ -113,7 +113,7 @@ BASIS_FORMAT = {
BASIS_DEBUG_FLAGS = {
DEBUG_FLAG_VIS_CR: 1,
DEBUG_FLAG_VIS_SELS: 2,
DEBUG_FLAG_VIS_ENDPOINTS: 4
DEBUG_FLAG_VIS_ENDPOINTS: 4
};
BASIS_FORMAT_NAMES = {};
@@ -124,8 +124,10 @@ for (var name in BASIS_FORMAT) {
DXT_FORMAT_MAP = {};
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC1] = COMPRESSED_RGB_S3TC_DXT1_EXT;
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC3] = COMPRESSED_RGBA_S3TC_DXT5_EXT;
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC7] = COMPRESSED_RGBA_BPTC_UNORM;
var dxtSupported = true;
var bc7Supported = false;
var drawMode = 0;
var basisHandle = 0, src = 0, srcSize = 0;
@@ -182,7 +184,7 @@ function transcodeFrame()
{
tex = renderer.createDxtTexture(dxtData, alignedWidth, alignedHeight, DXT_FORMAT_MAP[format]);
}
else
else if (format == BASIS_FORMAT.cTFBC1)
{
var rgb565Data = dxtToRgb565(Module.HEAPU16, dst / 2, alignedWidth, alignedHeight);
@@ -199,8 +201,26 @@ var g_paused = false;
var g_Framerate = 24.0;
var g_targetFrame = 0.0;
var g_dataLoadedCountDown;
var g_receivedData;
var forceBC1 = 0;
function frame(now)
{
if (g_dataLoadedCountDown > 0)
{
g_dataLoadedCountDown = g_dataLoadedCountDown - 1;
if (g_dataLoadedCountDown == 0)
{
if (g_receivedData)
{
transcode(g_receivedData);
g_receivedData = 0;
}
}
}
now *= 0.001; // convert to seconds
const deltaTime = now - then;
then = now;
@@ -249,10 +269,8 @@ function frame(now)
// log('Here!');
}
function dataLoaded(data)
function transcode(data)
{
log('Done loading .basis file, decoded header:');
Module._basis_init();
if (basisHandle != 0)
@@ -296,18 +314,26 @@ function dataLoaded(data)
log('Failed getting info from basis file!');
return;
}
format = BASIS_FORMAT.cTFBC1;
if (has_alpha)
{
format = BASIS_FORMAT.cTFBC3;
log('Decoding .basis data to BC3');
if ((bc7Supported) && (forceBC1 == 0))
{
format = BASIS_FORMAT.cTFBC7;
log('Decoding .basis data to BC7/BPTC');
}
else
{
log('Decoding .basis data to BC1');
}
format = BASIS_FORMAT.cTFBC1;
if (has_alpha)
{
format = BASIS_FORMAT.cTFBC3;
log('Decoding .basis data to BC3');
}
else
{
log('Decoding .basis data to BC1');
}
}
log('.basis file size: ' + srcSize);
if (levels == 1)
@@ -337,12 +363,19 @@ function dataLoaded(data)
}
curImageIndex = 0;
}
function dataLoaded(data)
{
log('Done loading .basis file');
//transcodeFrame();
//redraw();
if (bc7Supported)
log('EXT_texture_compression_bptc is supported!')
else
log('EXT_texture_compression_bptc is NOT supported!')
//requestAnimationFrame(frame);
g_dataLoadedCountDown=50;
g_receivedData=data;
}
/**
@@ -373,12 +406,19 @@ function run() {
function initialize() {
var gl = elem('canvas').getContext('experimental-webgl');
// Load the DXT extension, and verify it exists.
if (!gl.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc') && !gl.getExtension('WEBGL_compressed_texture_s3tc')) {
dxtSupported = false;
elem('nodxt').style.display = 'block';
}
if (gl.getExtension('EXT_texture_compression_bptc'))
bc7Supported = true;
else
bc7Supported = false;
window.renderer = new Renderer(gl);
elem('file').addEventListener('keydown', function(e) {
@@ -393,6 +433,7 @@ function initialize() {
function alphaBlend() { drawMode = 0; redraw(); }
function viewRGB() { drawMode = 1; redraw(); }
function viewAlpha() { drawMode = 2; redraw(); }
function forceBC1Func() { forceBC1 = 1 - forceBC1; run(); }
function visCR()
{
@@ -456,20 +497,18 @@ function rewind()
<body onload="initialize()">
<br>
<div style="font-size: 24pt; font-weight: bold">
Basis Universal GPU Texture Video Transcoding Test (ETC1S->BC1)
Basis Universal GPU Texture Video UASTC->BC7 Transcoding Test
</div>
<div style="font-size: 8pt;">
<br>This demo uses the <a href="https://github.com/BinomialLLC/basis_universal">Basis C++ transcoder</a> (compiled to Javascript using Emscripten) to transcode a .basis Universal Texture Video file directly to BC1/BC3 texture data, with no intermediate recompression step.
<br>.basis universal GPU texture files can be quickly transcoded directly to any other GPU texture format with little to no quality loss. For static texture content, .basis files are 10-25% smaller than rate distortion optimized (RDO) <a href="http://richg42.blogspot.com/2018/05/some-basis-baseline-universal-format.html">ETC1S</a> texture data losslessly compressed using LZMA. For video, we achieve bitrates that are significantly lower (average .5bpp vs. 2-3bpp) than I-Frame only (BC1+LZ4) Texture Video approaches such as <a href="https://hap.video/">HAP</a>.
<br>Currently, video is a new use case for .basis files, so video encoding using the reference encoder is quite slow and can require a lot of memory (16-32GB) on larger videos, but it will be optimized over time.
<br>Compared to regular pixel-based video, texture video doesn't utilize the IDCT and doesn't require colorspace conversion, chroma upsampling, SIMD, or deblocking filters.</br>
<br>This demo uses the <a href="https://github.com/BinomialLLC/basis_universal">Basis C++ transcoder</a> (compiled to Javascript using Emscripten) to transcode a .basis Universal Texture Video file directly to GPU texture data, with no intermediate recompression step.
<br>.basis universal GPU texture files can be quickly transcoded directly to any other GPU texture format with little to no quality loss.
<br>Thanks to Evan Parker for providing <a href="https://github.com/toji/webgl-texture-utils">webgl-texture-utils</a> and this test bed.
</div>
<br>
<br>
.basis file:
<input id="file" type="text" size=30 value="kodim18.basis"></input>
<input id="file" type="text" size=30 value="kodim03.basis"></input>
<input type="button" value="Run!" onclick="run()"></input>
<br>
<br>
@@ -477,7 +516,10 @@ function rewind()
<input type="button" value="View RGB" onclick="viewRGB()"></input>
<input type="button" value="View Alpha" onclick="viewAlpha()"></input>
<br>
<br>
<input type="button" value="Force BC1" onclick="forceBC1Func()"></input>
<br>
<br>
<input type="button" value="Visualize CR's" onclick="visCR()"></input>
<input type="button" value="Visualize Selectors" onclick="visSels()"></input>
<input type="button" value="Visualize Endpoints" onclick="visEndpoints()"></input>