godot/thirdparty/basis_universal/encoder/basisu_uastc_enc.cpp

// basisu_uastc_enc.cpp
// Copyright (C) 2019-2024 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_uastc_enc.h"
#include "3rdparty/android_astc_decomp.h"
#include "basisu_gpu_texture.h"
#include "basisu_bc7enc.h"

#ifdef _DEBUG
// When BASISU_VALIDATE_UASTC_ENC is 1, we pack and unpack to/from UASTC and ASTC, then validate that each codec returns the exact same results. This is slower.
#define BASISU_VALIDATE_UASTC_ENC
#endif

#define BASISU_SUPPORT_FORCE_MODE

usingnamespacebasist;

namespace basisu
{
	const uint32_t MAX_ENCODE_RESULTS =;

#if BASISU_VALIDATE_UASTC_ENC
	static void validate_func(bool condition, int line)
	{
		if (!condition)
		{
			fprintf(stderr, "basisu_uastc_enc: Internal validation failed on line %u!\n", line);
		}
	}

	#define VALIDATE
#else
	#define VALIDATE(c)
#endif

	enum dxt_constants
	{};

	struct dxt1_block
	{};

#define UASTC_WRITE_MODE_DESCS

	static inline void uastc_write_bits(uint8_t* pBuf, uint32_t& bit_offset, uint64_t code, uint32_t codesize, const char* pDesc)
	{}

	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)
	{}
	
	// MODE 0
	// 0. DualPlane: 0, WeightRange: 8 (16), Subsets: 1, CEM: 8 (RGB Direct       ), EndpointRange: 19 (192)       MODE6 RGB
	// 18. DualPlane: 0, WeightRange: 11 (32), Subsets: 1, CEM: 8 (RGB Direct       ), EndpointRange: 11 (32)       MODE6 RGB
	static void astc_mode0_or_18(uint32_t mode, const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, const uint8_t *pForce_selectors = nullptr)
	{}

	// MODE 1
	// 1-subset, 2-bit indices, 8-bit endpoints, BC7 mode 3
	// DualPlane: 0, WeightRange: 2 (4), Subsets: 1, CEM: 8 (RGB Direct       ), EndpointRange: 20 (256)        MODE3 or MODE5 RGB
	static void astc_mode1(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	static uint32_t estimate_partition2(uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeights, const color_rgba block[4][4], const uint32_t weights[4])
	{}

	// MODE 2
	// 2-subset, 3-bit indices, 4-bit endpoints, BC7 mode 1
	// DualPlane: 0, WeightRange: 5 (8), Subsets: 2, CEM: 8 (RGB Direct       ), EndpointRange: 8 (16)          MODE1
	static void astc_mode2(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
	{}

	// MODE 3
	// 3-subsets, 2-bit indices, [0,11] endpoints, BC7 mode 2
	// DualPlane: 0, WeightRange: 2 (4), Subsets: 3, CEM: 8 (RGB Direct	     ), EndpointRange: 7 (12)		   MODE2
	static void astc_mode3(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
	{}

	// MODE 4
	// DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 8 (RGB Direct       ), EndpointRange: 12 (40)         MODE3
	static void astc_mode4(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
	{}

	// MODE 5 
	// DualPlane: 0, WeightRange: 5 (8), Subsets: 1, CEM: 8 (RGB Direct       ), EndpointRange: 20 (256) 		BC7 MODE 6 (or MODE 1 1-subset)
	static void astc_mode5(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// MODE 6
	// DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 8 (RGB Direct       ), EndpointRange: 18 (160)		BC7 MODE5
	static void astc_mode6(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// MODE 7 - 2 subset ASTC, 3 subset BC7
	// DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 8 (RGB Direct       ), EndpointRange: 12 (40)         MODE2
	static void astc_mode7(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
	{}

	static void estimate_partition2_list(uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeights, const color_rgba block[4][4], uint32_t* pParts, uint32_t max_parts, const uint32_t weights[4])
	{}
		
	// 9. DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 12 (RGBA Direct), EndpointRange: 8 (16) - BC7 MODE 7
	// 16. DualPlane: 0, WeightRange : 2 (4), Subsets : 2, CEM: 4 (LA Direct), EndpointRange : 20 (256) - BC7 MODE 7
	static void astc_mode9_or_16(uint32_t mode, const color_rgba source_block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, uint32_t estimate_partition_list_size)
	{}

	// MODE 10
	// DualPlane: 0, WeightRange: 8 (16), Subsets: 1, CEM: 12 (RGBA Direct      ), EndpointRange: 13 (48)       MODE6
	static void astc_mode10(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// 11. DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 12 (RGBA Direct), EndpointRange: 13 (48)        MODE5
	// 17. DualPlane: 1, WeightRange : 2 (4), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256)    BC7 MODE5
	static void astc_mode11_or_17(uint32_t mode, const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// MODE 12
	// DualPlane: 0, WeightRange: 5 (8), Subsets: 1, CEM: 12 (RGBA Direct      ), EndpointRange: 19 (192)       MODE6
	static void astc_mode12(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// 13. DualPlane: 1, WeightRange: 0 (2), Subsets: 1, CEM: 12 (RGBA Direct      ), EndpointRange: 20 (256)        MODE5
	static void astc_mode13(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// MODE14
	// DualPlane: 0, WeightRange: 2 (4), Subsets: 1, CEM: 12 (RGBA Direct      ), EndpointRange: 20 (256)		MODE6
	static void astc_mode14(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}

	// MODE 15
	// DualPlane: 0, WeightRange : 8 (16), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256)   BC7 MODE6
	static void astc_mode15(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
	{}
		
	static void compute_block_error(const color_rgba block[4][4], const color_rgba decoded_block[4][4], uint64_t &total_rgb_err, uint64_t &total_rgba_err, uint64_t &total_la_err)
	{}

	static void compute_bc1_hints(bool &bc1_hint0, bool &bc1_hint1, const uastc_encode_results &best_results, const color_rgba block[4][4], const color_rgba decoded_uastc_block[4][4])
	{}

	struct ycbcr
	{};

	static inline void rgb_to_y_cb_cr(const color_rgba& c, ycbcr& dst)
	{}

	static inline uint64_t color_diff(const ycbcr& a, const ycbcr& b)
	{}

	static inline int gray_distance2(const color_rgba& c, int r, int g, int b)
	{}

	static bool pack_etc1_estimate_flipped(const color_rgba* pSrc_pixels)
	{}

	static void compute_etc1_hints(etc_block& best_etc1_blk, uint32_t& best_etc1_bias, const uastc_encode_results& best_results, const color_rgba block[4][4], const color_rgba decoded_uastc_block[4][4], int level, uint32_t flags)
	{}

	struct uastc_pack_eac_a8_results
	{};
	
	static uint64_t uastc_pack_eac_a8(uastc_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)
	{}

	const int32_t DEFAULT_BC7_ERROR_WEIGHT =;
	const float UASTC_ERROR_THRESH =;

	// TODO: This is a quick hack to favor certain modes when we know we'll be followed up with an RDO postprocess.
	static inline float get_uastc_mode_weight(uint32_t mode)
	{}

	void encode_uastc(const uint8_t* pRGBAPixels, uastc_block& output_block, uint32_t flags)
	{}

	static bool uastc_recompute_hints(basist::uastc_block* pBlock, const color_rgba* pBlock_pixels, uint32_t flags, const unpacked_uastc_block *pUnpacked_blk)
	{}

	static const uint8_t g_uastc_mode_selector_bits[TOTAL_UASTC_MODES][2] =;

	static inline uint32_t set_block_bits(uint8_t* pBytes, uint64_t val, uint32_t num_bits, uint32_t cur_ofs)
	{}

	static const uint8_t g_tdefl_small_dist_extra[512] =;

	static const uint8_t g_tdefl_large_dist_extra[128] =;

	static inline uint32_t compute_match_cost_estimate(uint32_t dist)
	{}

	struct selector_bitsequence
	{};

	struct selector_bitsequence_hash
	{};

	class tracked_stat
	{};
		
	static bool uastc_rdo_blocks(uint32_t first_index, uint32_t last_index, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params& params, uint32_t flags, 
		uint32_t &total_skipped, uint32_t &total_refined, uint32_t &total_modified, uint32_t &total_smooth)
	{}
				
	// This function implements a basic form of rate distortion optimization (RDO) for UASTC. 
	// It only changes selectors and then updates the hints. It uses very approximate LZ bitprice estimation.
	// There's A LOT that can be done better in here, but it's a start.
	// One nice advantage of the method used here is that it works for any input, no matter which or how many modes it uses.
	bool uastc_rdo(uint32_t num_blocks, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params& params, uint32_t flags, job_pool* pJob_pool, uint32_t total_jobs)
	{}
} // namespace basisu