Path: blob/master/thirdparty/cvtt/ConvectionKernels_API.cpp
9903 views
/*1Convection Texture Tools2Copyright (c) 2018-2019 Eric Lasota34Permission is hereby granted, free of charge, to any person obtaining5a copy of this software and associated documentation files (the6"Software"), to deal in the Software without restriction, including7without limitation the rights to use, copy, modify, merge, publish,8distribute, sublicense, and/or sell copies of the Software, and to9permit persons to whom the Software is furnished to do so, subject10to the following conditions:1112The above copyright notice and this permission notice shall be included13in all copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.18IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY19CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,20TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.22*/23#include "ConvectionKernels_Config.h"2425#if !defined(CVTT_SINGLE_FILE) || defined(CVTT_SINGLE_FILE_IMPL)2627#include <stdint.h>28#include "ConvectionKernels.h"29#include "ConvectionKernels_Util.h"30#include "ConvectionKernels_BC67.h"31#include "ConvectionKernels_ETC.h"32#include "ConvectionKernels_S3TC.h"3334#include <assert.h>3536namespace cvtt37{38namespace Kernels39{40void EncodeBC7(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, const BC7EncodingPlan &encodingPlan)41{42assert(pBlocks);43assert(pBC);4445float channelWeights[4];46Util::FillWeights(options, channelWeights);4748for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)49{50Internal::BC7Computer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, encodingPlan, options.refineRoundsBC7);51pBC += ParallelMath::ParallelSize * 16;52}53}5455void EncodeBC6HU(uint8_t *pBC, const PixelBlockF16 *pBlocks, const cvtt::Options &options)56{57assert(pBlocks);58assert(pBC);5960float channelWeights[4];61Util::FillWeights(options, channelWeights);6263for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)64{65Internal::BC6HComputer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, false, options.seedPoints, options.refineRoundsBC6H);66pBC += ParallelMath::ParallelSize * 16;67}68}6970void EncodeBC6HS(uint8_t *pBC, const PixelBlockF16 *pBlocks, const cvtt::Options &options)71{72assert(pBlocks);73assert(pBC);7475float channelWeights[4];76Util::FillWeights(options, channelWeights);7778for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)79{80Internal::BC6HComputer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, true, options.seedPoints, options.refineRoundsBC6H);81pBC += ParallelMath::ParallelSize * 16;82}83}8485void EncodeBC1(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options)86{87assert(pBlocks);88assert(pBC);8990float channelWeights[4];91Util::FillWeights(options, channelWeights);9293for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)94{95Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC, 8, channelWeights, true, options.threshold, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);96pBC += ParallelMath::ParallelSize * 8;97}98}99100void EncodeBC2(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)101{102assert(pBlocks);103assert(pBC);104105float channelWeights[4];106Util::FillWeights(options, channelWeights);107108for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)109{110Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC + 8, 16, channelWeights, false, 1.0f, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);111Internal::S3TCComputer::PackExplicitAlpha(options.flags, pBlocks + blockBase, 3, pBC, 16);112pBC += ParallelMath::ParallelSize * 16;113}114}115116void EncodeBC3(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)117{118assert(pBlocks);119assert(pBC);120121float channelWeights[4];122Util::FillWeights(options, channelWeights);123124for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)125{126Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC + 8, 16, channelWeights, false, 1.0f, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);127Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 3, pBC, 16, false, options.seedPoints, options.refineRoundsIIC);128pBC += ParallelMath::ParallelSize * 16;129}130}131132void EncodeBC4U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)133{134assert(pBlocks);135assert(pBC);136137float channelWeights[4];138Util::FillWeights(options, channelWeights);139140for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)141{142Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 0, pBC, 8, false, options.seedPoints, options.refineRoundsIIC);143pBC += ParallelMath::ParallelSize * 8;144}145}146147void EncodeBC4S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options)148{149assert(pBlocks);150assert(pBC);151152float channelWeights[4];153Util::FillWeights(options, channelWeights);154155for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)156{157PixelBlockU8 inputBlocks[ParallelMath::ParallelSize];158Util::BiasSignedInput(inputBlocks, pBlocks + blockBase);159160Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 0, pBC, 8, true, options.seedPoints, options.refineRoundsIIC);161pBC += ParallelMath::ParallelSize * 8;162}163}164165void EncodeBC5U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)166{167assert(pBlocks);168assert(pBC);169170float channelWeights[4];171Util::FillWeights(options, channelWeights);172173for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)174{175Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 0, pBC, 16, false, options.seedPoints, options.refineRoundsIIC);176Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 1, pBC + 8, 16, false, options.seedPoints, options.refineRoundsIIC);177pBC += ParallelMath::ParallelSize * 16;178}179}180181void EncodeBC5S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options)182{183assert(pBlocks);184assert(pBC);185186float channelWeights[4];187Util::FillWeights(options, channelWeights);188189for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)190{191PixelBlockU8 inputBlocks[ParallelMath::ParallelSize];192Util::BiasSignedInput(inputBlocks, pBlocks + blockBase);193194Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 0, pBC, 16, true, options.seedPoints, options.refineRoundsIIC);195Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 1, pBC + 8, 16, true, options.seedPoints, options.refineRoundsIIC);196pBC += ParallelMath::ParallelSize * 16;197}198}199200void EncodeETC1(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC1CompressionData *compressionData)201{202assert(pBlocks);203assert(pBC);204205float channelWeights[4];206Util::FillWeights(options, channelWeights);207208for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)209{210Internal::ETCComputer::CompressETC1Block(pBC, pBlocks + blockBase, compressionData, options);211pBC += ParallelMath::ParallelSize * 8;212}213}214215void EncodeETC2(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC2CompressionData *compressionData)216{217assert(pBlocks);218assert(pBC);219220float channelWeights[4];221Util::FillWeights(options, channelWeights);222223for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)224{225Internal::ETCComputer::CompressETC2Block(pBC, pBlocks + blockBase, compressionData, options, false);226pBC += ParallelMath::ParallelSize * 8;227}228}229230void EncodeETC2PunchthroughAlpha(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC2CompressionData *compressionData)231{232assert(pBlocks);233assert(pBC);234235float channelWeights[4];236Util::FillWeights(options, channelWeights);237238for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)239{240Internal::ETCComputer::CompressETC2Block(pBC, pBlocks + blockBase, compressionData, options, true);241pBC += ParallelMath::ParallelSize * 8;242}243}244245void EncodeETC2Alpha(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options)246{247assert(pBlocks);248assert(pBC);249250for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)251{252Internal::ETCComputer::CompressETC2AlphaBlock(pBC, pBlocks + blockBase, options);253pBC += ParallelMath::ParallelSize * 8;254}255}256257void EncodeETC2Alpha11(uint8_t *pBC, const PixelBlockScalarS16 *pBlocks, bool isSigned, const cvtt::Options &options)258{259assert(pBlocks);260assert(pBC);261262for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)263{264Internal::ETCComputer::CompressEACBlock(pBC, pBlocks + blockBase, isSigned, options);265pBC += ParallelMath::ParallelSize * 8;266}267}268269void EncodeETC2RGBA(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC2CompressionData *compressionData)270{271uint8_t alphaBlockData[cvtt::NumParallelBlocks * 8];272uint8_t colorBlockData[cvtt::NumParallelBlocks * 8];273274EncodeETC2(colorBlockData, pBlocks, options, compressionData);275EncodeETC2Alpha(alphaBlockData, pBlocks, options);276277for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)278{279for (size_t blockData = 0; blockData < 8; blockData++)280pBC[blockBase * 16 + blockData] = alphaBlockData[blockBase * 8 + blockData];281282for (size_t blockData = 0; blockData < 8; blockData++)283pBC[blockBase * 16 + 8 + blockData] = colorBlockData[blockBase * 8 + blockData];284}285}286287void DecodeBC7(PixelBlockU8 *pBlocks, const uint8_t *pBC)288{289assert(pBlocks);290assert(pBC);291292for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)293{294Internal::BC7Computer::UnpackOne(pBlocks[blockBase], pBC);295pBC += 16;296}297}298299void DecodeBC6HU(PixelBlockF16 *pBlocks, const uint8_t *pBC)300{301assert(pBlocks);302assert(pBC);303304for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)305{306Internal::BC6HComputer::UnpackOne(pBlocks[blockBase], pBC, false);307pBC += 16;308}309}310311void DecodeBC6HS(PixelBlockF16 *pBlocks, const uint8_t *pBC)312{313assert(pBlocks);314assert(pBC);315316for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)317{318Internal::BC6HComputer::UnpackOne(pBlocks[blockBase], pBC, true);319pBC += 16;320}321}322323ETC1CompressionData *AllocETC1Data(allocFunc_t allocFunc, void *context)324{325return cvtt::Internal::ETCComputer::AllocETC1Data(allocFunc, context);326}327328void ReleaseETC1Data(ETC1CompressionData *compressionData, freeFunc_t freeFunc)329{330cvtt::Internal::ETCComputer::ReleaseETC1Data(compressionData, freeFunc);331}332333ETC2CompressionData *AllocETC2Data(allocFunc_t allocFunc, void *context, const cvtt::Options &options)334{335return cvtt::Internal::ETCComputer::AllocETC2Data(allocFunc, context, options);336}337338void ReleaseETC2Data(ETC2CompressionData *compressionData, freeFunc_t freeFunc)339{340cvtt::Internal::ETCComputer::ReleaseETC2Data(compressionData, freeFunc);341}342}343}344345#endif346347348