Path: blob/master/thirdparty/basis_universal/encoder/basisu_pvrtc1_4.h
21646 views
// basisu_pvrtc1_4.cpp1// Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14#pragma once15#include "basisu_gpu_texture.h"1617namespace basisu18{19enum20{21PVRTC2_MIN_WIDTH = 16,22PVRTC2_MIN_HEIGHT = 8,23PVRTC4_MIN_WIDTH = 8,24PVRTC4_MIN_HEIGHT = 825};2627struct pvrtc4_block28{29uint32_t m_modulation;30uint32_t m_endpoints;3132pvrtc4_block() : m_modulation(0), m_endpoints(0) { }3334inline bool operator== (const pvrtc4_block& rhs) const35{36return (m_modulation == rhs.m_modulation) && (m_endpoints == rhs.m_endpoints);37}3839inline void clear()40{41m_modulation = 0;42m_endpoints = 0;43}4445inline bool get_block_uses_transparent_modulation() const46{47return (m_endpoints & 1) != 0;48}4950inline bool is_endpoint_opaque(uint32_t endpoint_index) const51{52static const uint32_t s_bitmasks[2] = { 0x8000U, 0x80000000U };53return (m_endpoints & s_bitmasks[open_range_check(endpoint_index, 2U)]) != 0;54}5556// Returns raw endpoint or 888857color_rgba get_endpoint(uint32_t endpoint_index, bool unpack) const;5859color_rgba get_endpoint_5554(uint32_t endpoint_index) const;6061static uint32_t get_component_precision_in_bits(uint32_t c, uint32_t endpoint_index, bool opaque_endpoint)62{63static const uint32_t s_comp_prec[4][4] =64{65// R0 G0 B0 A0 R1 G1 B1 A166{ 4, 4, 3, 3 }, { 4, 4, 4, 3 }, // transparent endpoint6768{ 5, 5, 4, 0 }, { 5, 5, 5, 0 } // opaque endpoint69};70return s_comp_prec[open_range_check(endpoint_index, 2U) + (opaque_endpoint * 2)][open_range_check(c, 4U)];71}7273static color_rgba get_color_precision_in_bits(uint32_t endpoint_index, bool opaque_endpoint)74{75static const color_rgba s_color_prec[4] =76{77color_rgba(4, 4, 3, 3), color_rgba(4, 4, 4, 3), // transparent endpoint78color_rgba(5, 5, 4, 0), color_rgba(5, 5, 5, 0) // opaque endpoint79};80return s_color_prec[open_range_check(endpoint_index, 2U) + (opaque_endpoint * 2)];81}8283inline uint32_t get_modulation(uint32_t x, uint32_t y) const84{85assert((x < 4) && (y < 4));86return (m_modulation >> ((y * 4 + x) * 2)) & 3;87}8889inline void set_modulation(uint32_t x, uint32_t y, uint32_t s)90{91assert((x < 4) && (y < 4) && (s < 4));92uint32_t n = (y * 4 + x) * 2;93m_modulation = (m_modulation & (~(3 << n))) | (s << n);94assert(get_modulation(x, y) == s);95}9697// Scaled by 898inline const uint32_t* get_scaled_modulation_values(bool block_uses_transparent_modulation) const99{100static const uint32_t s_block_scales[2][4] = { { 0, 3, 5, 8 }, { 0, 4, 4, 8 } };101return s_block_scales[block_uses_transparent_modulation];102}103104// Scaled by 8105inline uint32_t get_scaled_modulation(uint32_t x, uint32_t y) const106{107return get_scaled_modulation_values(get_block_uses_transparent_modulation())[get_modulation(x, y)];108}109110inline void byte_swap()111{112m_modulation = byteswap32(m_modulation);113m_endpoints = byteswap32(m_endpoints);114}115116// opaque endpoints: 554, 555117// transparent endpoints: 3443, 3444118inline void set_endpoint_raw(uint32_t endpoint_index, const color_rgba& c, bool opaque_endpoint)119{120assert(endpoint_index < 2);121const uint32_t m = m_endpoints & 1;122uint32_t r = c[0], g = c[1], b = c[2], a = c[3];123124uint32_t packed;125126if (opaque_endpoint)127{128if (!endpoint_index)129{130// 554131// 1RRRRRGGGGGBBBBM132assert((r < 32) && (g < 32) && (b < 16));133packed = 0x8000 | (r << 10) | (g << 5) | (b << 1) | m;134}135else136{137// 555138// 1RRRRRGGGGGBBBBB139assert((r < 32) && (g < 32) && (b < 32));140packed = 0x8000 | (r << 10) | (g << 5) | b;141}142}143else144{145if (!endpoint_index)146{147// 3443148// 0AAA RRRR GGGG BBBM149assert((r < 16) && (g < 16) && (b < 8) && (a < 8));150packed = (a << 12) | (r << 8) | (g << 4) | (b << 1) | m;151}152else153{154// 3444155// 0AAA RRRR GGGG BBBB156assert((r < 16) && (g < 16) && (b < 16) && (a < 8));157packed = (a << 12) | (r << 8) | (g << 4) | b;158}159}160161assert(packed <= 0xFFFF);162163if (endpoint_index)164m_endpoints = (m_endpoints & 0xFFFFU) | (packed << 16);165else166m_endpoints = (m_endpoints & 0xFFFF0000U) | packed;167}168};169170typedef vector2D<pvrtc4_block> pvrtc4_block_vector2D;171172uint32_t pvrtc4_swizzle_uv(uint32_t XSize, uint32_t YSize, uint32_t XPos, uint32_t YPos);173174class pvrtc4_image175{176public:177inline pvrtc4_image() :178m_width(0), m_height(0), m_block_width(0), m_block_height(0), m_uses_alpha(false)179{180}181182inline pvrtc4_image(uint32_t width, uint32_t height) :183m_width(0), m_height(0), m_block_width(0), m_block_height(0), m_uses_alpha(false)184{185resize(width, height);186}187188inline void clear()189{190m_width = 0;191m_height = 0;192m_block_width = 0;193m_block_height = 0;194m_blocks.clear();195m_uses_alpha = false;196}197198inline void resize(uint32_t width, uint32_t height)199{200if ((width == m_width) && (height == m_height))201return;202203m_width = width;204m_height = height;205206m_block_width = (width + 3) >> 2;207m_block_height = (height + 3) >> 2;208209m_blocks.resize(m_block_width, m_block_height);210}211212inline uint32_t get_width() const { return m_width; }213inline uint32_t get_height() const { return m_height; }214215inline uint32_t get_block_width() const { return m_block_width; }216inline uint32_t get_block_height() const { return m_block_height; }217218inline const pvrtc4_block_vector2D &get_blocks() const { return m_blocks; }219inline pvrtc4_block_vector2D &get_blocks() { return m_blocks; }220221inline uint32_t get_total_blocks() const { return m_block_width * m_block_height; }222223inline bool get_uses_alpha() const { return m_uses_alpha; }224inline void set_uses_alpha(bool uses_alpha) { m_uses_alpha = uses_alpha; }225226inline bool are_blocks_equal(const pvrtc4_image& rhs) const227{228return m_blocks == rhs.m_blocks;229}230231inline void set_to_black()232{233memset((void *)m_blocks.get_ptr(), 0, m_blocks.size_in_bytes());234}235236inline bool get_block_uses_transparent_modulation(uint32_t bx, uint32_t by) const237{238return m_blocks(bx, by).get_block_uses_transparent_modulation();239}240241inline bool is_endpoint_opaque(uint32_t bx, uint32_t by, uint32_t endpoint_index) const242{243return m_blocks(bx, by).is_endpoint_opaque(endpoint_index);244}245246color_rgba get_endpoint(uint32_t bx, uint32_t by, uint32_t endpoint_index, bool unpack) const247{248assert((bx < m_block_width) && (by < m_block_height));249return m_blocks(bx, by).get_endpoint(endpoint_index, unpack);250}251252inline uint32_t get_modulation(uint32_t x, uint32_t y) const253{254assert((x < m_width) && (y < m_height));255return m_blocks(x >> 2, y >> 2).get_modulation(x & 3, y & 3);256}257258// Returns true if the block uses transparent modulation.259bool get_interpolated_colors(uint32_t x, uint32_t y, color_rgba* pColors) const;260261color_rgba get_pixel(uint32_t x, uint32_t y, uint32_t m) const;262263inline color_rgba get_pixel(uint32_t x, uint32_t y) const264{265assert((x < m_width) && (y < m_height));266return get_pixel(x, y, m_blocks(x >> 2, y >> 2).get_modulation(x & 3, y & 3));267}268269void deswizzle()270{271pvrtc4_block_vector2D temp(m_blocks);272273for (uint32_t y = 0; y < m_block_height; y++)274for (uint32_t x = 0; x < m_block_width; x++)275m_blocks(x, y) = temp[pvrtc4_swizzle_uv(m_block_width, m_block_height, x, y)];276}277278void swizzle()279{280pvrtc4_block_vector2D temp(m_blocks);281282for (uint32_t y = 0; y < m_block_height; y++)283for (uint32_t x = 0; x < m_block_width; x++)284m_blocks[pvrtc4_swizzle_uv(m_block_width, m_block_height, x, y)] = temp(x, y);285}286287void unpack_all_pixels(image& img) const288{289img.crop(m_width, m_height);290291for (uint32_t y = 0; y < m_height; y++)292for (uint32_t x = 0; x < m_width; x++)293img(x, y) = get_pixel(x, y);294}295296void unpack_block(image &dst, uint32_t block_x, uint32_t block_y)297{298for (uint32_t y = 0; y < 4; y++)299for (uint32_t x = 0; x < 4; x++)300dst(x, y) = get_pixel(block_x * 4 + x, block_y * 4 + y);301}302303inline int wrap_x(int x) const304{305return posmod(x, m_width);306}307308inline int wrap_y(int y) const309{310return posmod(y, m_height);311}312313inline int wrap_block_x(int bx) const314{315return posmod(bx, m_block_width);316}317318inline int wrap_block_y(int by) const319{320return posmod(by, m_block_height);321}322323inline vec2F get_interpolation_factors(uint32_t x, uint32_t y) const324{325// 0 1 2 3326// 2 3 0 1327// .5 .75 0 .25328static const float s_interp[4] = { 2, 3, 0, 1 };329return vec2F(s_interp[x & 3], s_interp[y & 3]);330}331332inline color_rgba interpolate(int x, int y,333const color_rgba& p, const color_rgba& q,334const color_rgba& r, const color_rgba& s) const335{336static const int s_interp[4] = { 2, 3, 0, 1 };337const int u_interp = s_interp[x & 3];338const int v_interp = s_interp[y & 3];339340color_rgba result;341342for (uint32_t c = 0; c < 4; c++)343{344int t = p[c] * 4 + u_interp * ((int)q[c] - (int)p[c]);345int b = r[c] * 4 + u_interp * ((int)s[c] - (int)r[c]);346int v = t * 4 + v_interp * (b - t);347if (c < 3)348{349v >>= 1;350v += (v >> 5);351}352else353{354v += (v >> 4);355}356assert((v >= 0) && (v < 256));357result[c] = static_cast<uint8_t>(v);358}359360return result;361}362363inline void set_modulation(uint32_t x, uint32_t y, uint32_t s)364{365assert((x < m_width) && (y < m_height));366return m_blocks(x >> 2, y >> 2).set_modulation(x & 3, y & 3, s);367}368369inline uint64_t map_pixel(uint32_t x, uint32_t y, const color_rgba& c, bool perceptual, bool alpha_is_significant, bool record = true)370{371color_rgba v[4];372get_interpolated_colors(x, y, v);373374uint64_t best_dist = color_distance(perceptual, c, v[0], alpha_is_significant);375uint32_t best_v = 0;376for (uint32_t i = 1; i < 4; i++)377{378uint64_t dist = color_distance(perceptual, c, v[i], alpha_is_significant);379if (dist < best_dist)380{381best_dist = dist;382best_v = i;383}384}385386if (record)387set_modulation(x, y, best_v);388389return best_dist;390}391392inline uint64_t remap_pixels_influenced_by_endpoint(uint32_t bx, uint32_t by, const image& orig_img, bool perceptual, bool alpha_is_significant)393{394uint64_t total_error = 0;395396for (int yd = -3; yd <= 3; yd++)397{398const int y = wrap_y((int)by * 4 + 2 + yd);399400for (int xd = -3; xd <= 3; xd++)401{402const int x = wrap_x((int)bx * 4 + 2 + xd);403404total_error += map_pixel(x, y, orig_img(x, y), perceptual, alpha_is_significant);405}406}407408return total_error;409}410411inline 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) const412{413uint64_t total_error = 0;414415for (int yd = -3; yd <= 3; yd++)416{417const int y = wrap_y((int)by * 4 + 2 + yd);418419for (int xd = -3; xd <= 3; xd++)420{421const int x = wrap_x((int)bx * 4 + 2 + xd);422423total_error += color_distance(perceptual, get_pixel(x, y), orig_img(x, y), alpha_is_significant);424425if ((threshold_error) && (total_error >= threshold_error))426return total_error;427}428}429430return total_error;431}432433uint64_t local_endpoint_optimization_opaque(uint32_t bx, uint32_t by, const image& orig_img, bool perceptual);434435inline uint64_t map_all_pixels(const image& img, bool perceptual, bool alpha_is_significant)436{437assert(m_width == img.get_width());438assert(m_height == img.get_height());439440uint64_t total_error = 0;441for (uint32_t y = 0; y < img.get_height(); y++)442for (uint32_t x = 0; x < img.get_width(); x++)443total_error += map_pixel(x, y, img(x, y), perceptual, alpha_is_significant);444445return total_error;446}447448public:449uint32_t m_width, m_height;450pvrtc4_block_vector2D m_blocks;451uint32_t m_block_width, m_block_height;452453bool m_uses_alpha;454};455456} // namespace basisu457458459