Path: blob/master/modules/etcpak/image_compress_etcpak.cpp
10277 views
/**************************************************************************/1/* image_compress_etcpak.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "image_compress_etcpak.h"3132#ifdef TOOLS_ENABLED3334#include "core/os/os.h"35#include "core/string/print_string.h"3637#include <ProcessDxtc.hpp>38#include <ProcessRGB.hpp>3940EtcpakType _determine_etc_type(Image::UsedChannels p_channels) {41switch (p_channels) {42case Image::USED_CHANNELS_L:43return EtcpakType::ETCPAK_TYPE_ETC2;44case Image::USED_CHANNELS_LA:45return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;46case Image::USED_CHANNELS_R:47return EtcpakType::ETCPAK_TYPE_ETC2_R;48case Image::USED_CHANNELS_RG:49return EtcpakType::ETCPAK_TYPE_ETC2_RG;50case Image::USED_CHANNELS_RGB:51return EtcpakType::ETCPAK_TYPE_ETC2;52case Image::USED_CHANNELS_RGBA:53return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;5455default:56return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;57}58}5960EtcpakType _determine_dxt_type(Image::UsedChannels p_channels) {61switch (p_channels) {62case Image::USED_CHANNELS_L:63return EtcpakType::ETCPAK_TYPE_DXT1;64case Image::USED_CHANNELS_LA:65return EtcpakType::ETCPAK_TYPE_DXT5;66case Image::USED_CHANNELS_R:67return EtcpakType::ETCPAK_TYPE_RGTC_R;68case Image::USED_CHANNELS_RG:69return EtcpakType::ETCPAK_TYPE_RGTC_RG;70case Image::USED_CHANNELS_RGB:71return EtcpakType::ETCPAK_TYPE_DXT1;72case Image::USED_CHANNELS_RGBA:73return EtcpakType::ETCPAK_TYPE_DXT5;7475default:76return EtcpakType::ETCPAK_TYPE_DXT5;77}78}7980void _compress_etc1(Image *r_img) {81_compress_etcpak(EtcpakType::ETCPAK_TYPE_ETC1, r_img);82}8384void _compress_etc2(Image *r_img, Image::UsedChannels p_channels) {85_compress_etcpak(_determine_etc_type(p_channels), r_img);86}8788void _compress_bc(Image *r_img, Image::UsedChannels p_channels) {89_compress_etcpak(_determine_dxt_type(p_channels), r_img);90}9192void _compress_etcpak(EtcpakType p_compress_type, Image *r_img) {93uint64_t start_time = OS::get_singleton()->get_ticks_msec();9495// The image is already compressed, return.96if (r_img->is_compressed()) {97return;98}99100// Convert to RGBA8 for compression.101r_img->convert(Image::FORMAT_RGBA8);102103// Determine output format based on Etcpak type.104Image::Format target_format = Image::FORMAT_RGBA8;105106switch (p_compress_type) {107case EtcpakType::ETCPAK_TYPE_ETC1:108target_format = Image::FORMAT_ETC;109break;110111case EtcpakType::ETCPAK_TYPE_ETC2:112target_format = Image::FORMAT_ETC2_RGB8;113break;114115case EtcpakType::ETCPAK_TYPE_ETC2_ALPHA:116target_format = Image::FORMAT_ETC2_RGBA8;117break;118119case EtcpakType::ETCPAK_TYPE_ETC2_R:120target_format = Image::FORMAT_ETC2_R11;121break;122123case EtcpakType::ETCPAK_TYPE_ETC2_RG:124target_format = Image::FORMAT_ETC2_RG11;125break;126127case EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG:128target_format = Image::FORMAT_ETC2_RA_AS_RG;129r_img->convert_rg_to_ra_rgba8();130break;131132case EtcpakType::ETCPAK_TYPE_DXT1:133target_format = Image::FORMAT_DXT1;134break;135136case EtcpakType::ETCPAK_TYPE_DXT5:137target_format = Image::FORMAT_DXT5;138break;139140case EtcpakType::ETCPAK_TYPE_RGTC_R:141target_format = Image::FORMAT_RGTC_R;142break;143144case EtcpakType::ETCPAK_TYPE_RGTC_RG:145target_format = Image::FORMAT_RGTC_RG;146break;147148case EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG:149target_format = Image::FORMAT_DXT5_RA_AS_RG;150r_img->convert_rg_to_ra_rgba8();151break;152153default:154ERR_FAIL_MSG("Invalid or unsupported etcpak compression format, not ETC or DXT.");155break;156}157158// It's badly documented but ETCPAK seems to expect BGRA8 for ETC formats.159if (p_compress_type < EtcpakType::ETCPAK_TYPE_DXT1) {160r_img->convert_rgba8_to_bgra8();161}162163// Compress image data and (if required) mipmaps.164const bool has_mipmaps = r_img->has_mipmaps();165int width = r_img->get_width();166int height = r_img->get_height();167168/*169The first mipmap level of a compressed texture must be a multiple of 4. Quote from D3D11.3 spec:170171BC format surfaces are always multiples of full blocks, each block representing 4x4 pixels.172For mipmaps, the top level map is required to be a multiple of 4 size in all dimensions.173The sizes for the lower level maps are computed as they are for all mipmapped surfaces,174and thus may not be a multiple of 4, for example a top level map of 20 results in a second level175map size of 10. For these cases, there is a differing 'physical' size and a 'virtual' size.176The virtual size is that computed for each mip level without adjustment, which is 10 for the example.177The physical size is the virtual size rounded up to the next multiple of 4, which is 12 for the example,178and this represents the actual memory size. The sampling hardware will apply texture address179processing based on the virtual size (using, for example, border color if specified for accesses180beyond 10), and thus for the example case will not access the 11th and 12th row of the resource.181So for mipmap chains when an axis becomes < 4 in size, only texels 'a','b','e','f'182are used for a 2x2 map, and texel 'a' is used for 1x1. Note that this is similar to, but distinct from,183the surface pitch, which can encompass additional padding beyond the physical surface size.184*/185186if (width % 4 != 0 || height % 4 != 0) {187width = width <= 2 ? width : (width + 3) & ~3;188height = height <= 2 ? height : (height + 3) & ~3;189}190191// Multiple-of-4 should be guaranteed by above.192// However, power-of-two 3d textures will create Nx2 and Nx1 mipmap levels,193// which are individually compressed Image objects that violate the above rule.194// Hence, we allow Nx1 and Nx2 images through without forcing to multiple-of-4.195196// Create the buffer for compressed image data.197Vector<uint8_t> dest_data;198dest_data.resize(Image::get_image_data_size(width, height, target_format, has_mipmaps));199uint8_t *dest_write = dest_data.ptrw();200201const uint8_t *src_read = r_img->get_data().ptr();202203const int mip_count = has_mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;204Vector<uint32_t> padded_src;205206for (int i = 0; i < mip_count + 1; i++) {207// Get write mip metrics for target image.208int dest_mip_w, dest_mip_h;209int64_t dest_mip_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, dest_mip_w, dest_mip_h);210211// Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).212ERR_FAIL_COND(dest_mip_ofs % 8 != 0);213uint64_t *dest_mip_write = reinterpret_cast<uint64_t *>(dest_write + dest_mip_ofs);214215// Block size.216dest_mip_w = (dest_mip_w + 3) & ~3;217dest_mip_h = (dest_mip_h + 3) & ~3;218const uint32_t blocks = dest_mip_w * dest_mip_h / 16;219220// Get mip data from source image for reading.221int64_t src_mip_ofs, src_mip_size;222int src_mip_w, src_mip_h;223224r_img->get_mipmap_offset_size_and_dimensions(i, src_mip_ofs, src_mip_size, src_mip_w, src_mip_h);225226const uint32_t *src_mip_read = reinterpret_cast<const uint32_t *>(src_read + src_mip_ofs);227228// Pad textures to nearest block by smearing.229if (dest_mip_w != src_mip_w || dest_mip_h != src_mip_h) {230// Reserve the buffer for padded image data.231padded_src.resize(dest_mip_w * dest_mip_h);232uint32_t *ptrw = padded_src.ptrw();233234int x = 0, y = 0;235for (y = 0; y < src_mip_h; y++) {236for (x = 0; x < src_mip_w; x++) {237ptrw[dest_mip_w * y + x] = src_mip_read[src_mip_w * y + x];238}239240// First, smear in x.241for (; x < dest_mip_w; x++) {242ptrw[dest_mip_w * y + x] = ptrw[dest_mip_w * y + x - 1];243}244}245246// Then, smear in y.247for (; y < dest_mip_h; y++) {248for (x = 0; x < dest_mip_w; x++) {249ptrw[dest_mip_w * y + x] = ptrw[dest_mip_w * y + x - dest_mip_w];250}251}252253// Override the src_mip_read pointer to our temporary Vector.254src_mip_read = padded_src.ptr();255}256257switch (p_compress_type) {258case EtcpakType::ETCPAK_TYPE_ETC1:259CompressEtc1RgbDither(src_mip_read, dest_mip_write, blocks, dest_mip_w);260break;261262case EtcpakType::ETCPAK_TYPE_ETC2:263CompressEtc2Rgb(src_mip_read, dest_mip_write, blocks, dest_mip_w, true);264break;265266case EtcpakType::ETCPAK_TYPE_ETC2_ALPHA:267case EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG:268CompressEtc2Rgba(src_mip_read, dest_mip_write, blocks, dest_mip_w, true);269break;270271case EtcpakType::ETCPAK_TYPE_ETC2_R:272CompressEacR(src_mip_read, dest_mip_write, blocks, dest_mip_w);273break;274275case EtcpakType::ETCPAK_TYPE_ETC2_RG:276CompressEacRg(src_mip_read, dest_mip_write, blocks, dest_mip_w);277break;278279case EtcpakType::ETCPAK_TYPE_DXT1:280CompressBc1Dither(src_mip_read, dest_mip_write, blocks, dest_mip_w);281break;282283case EtcpakType::ETCPAK_TYPE_DXT5:284case EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG:285CompressBc3(src_mip_read, dest_mip_write, blocks, dest_mip_w);286break;287288case EtcpakType::ETCPAK_TYPE_RGTC_R:289CompressBc4(src_mip_read, dest_mip_write, blocks, dest_mip_w);290break;291292case EtcpakType::ETCPAK_TYPE_RGTC_RG:293CompressBc5(src_mip_read, dest_mip_write, blocks, dest_mip_w);294break;295296default:297ERR_FAIL_MSG("etcpak: Invalid or unsupported compression format.");298break;299}300}301302// Replace original image with compressed one.303r_img->set_data(width, height, has_mipmaps, target_format, dest_data);304305print_verbose(vformat("etcpak: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));306}307#endif // TOOLS_ENABLED308309310