CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Common/ReplacedTexture.cpp
Views: 1401
// Copyright (c) 2016- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <algorithm>1819#include "ppsspp_config.h"2021#include <png.h>2223#include "ext/basis_universal/basisu_transcoder.h"24#include "ext/basis_universal/basisu_file_headers.h"2526#include "GPU/Common/ReplacedTexture.h"27#include "GPU/Common/TextureReplacer.h"2829#include "Common/Data/Format/IniFile.h"30#include "Common/Data/Format/DDSLoad.h"31#include "Common/Data/Format/ZIMLoad.h"32#include "Common/Data/Format/PNGLoad.h"33#include "Common/Thread/ParallelLoop.h"34#include "Common/Thread/Waitable.h"35#include "Common/Thread/ThreadManager.h"36#include "Common/Log.h"37#include "Common/TimeUtil.h"3839#define MK_FOURCC(str) (str[0] | ((uint8_t)str[1] << 8) | ((uint8_t)str[2] << 16) | ((uint8_t)str[3] << 24))4041static ReplacedImageType IdentifyMagic(const uint8_t magic[4]) {42if (memcmp((const char *)magic, "ZIMG", 4) == 0)43return ReplacedImageType::ZIM;44else if (magic[0] == 0x89 && strncmp((const char *)&magic[1], "PNG", 3) == 0)45return ReplacedImageType::PNG;46else if (memcmp((const char *)magic, "DDS ", 4) == 0)47return ReplacedImageType::DDS;48else if (magic[0] == 's' && magic[1] == 'B') {49uint16_t ver = magic[2] | (magic[3] << 8);50if (ver >= 0x10) {51return ReplacedImageType::BASIS;52}53} else if (memcmp((const char *)magic, "\xabKTX", 4) == 0) {54// Technically, should read 12 bytes here, but this'll do.55return ReplacedImageType::KTX2;56}57return ReplacedImageType::INVALID;58}5960static ReplacedImageType Identify(VFSBackend *vfs, VFSOpenFile *openFile, std::string *outMagic) {61uint8_t magic[4];62if (vfs->Read(openFile, magic, 4) != 4) {63*outMagic = "FAIL";64return ReplacedImageType::INVALID;65}66// Turn the signature into a readable string that we can display in an error message.67*outMagic = std::string((const char *)magic, 4);68for (int i = 0; i < outMagic->size(); i++) {69if ((s8)(*outMagic)[i] < 32) {70(*outMagic)[i] = '_';71}72}73vfs->Rewind(openFile);74return IdentifyMagic(magic);75}7677class ReplacedTextureTask : public Task {78public:79ReplacedTextureTask(VFSBackend *vfs, ReplacedTexture &tex, LimitedWaitable *w) : vfs_(vfs), tex_(tex), waitable_(w) {}8081TaskType Type() const override { return TaskType::IO_BLOCKING; }82TaskPriority Priority() const override { return TaskPriority::NORMAL; }8384void Run() override {85tex_.Prepare(vfs_);86waitable_->Notify();87}8889private:90VFSBackend *vfs_;91ReplacedTexture &tex_;92LimitedWaitable *waitable_;93};9495ReplacedTexture::ReplacedTexture(VFSBackend *vfs, const ReplacementDesc &desc) : vfs_(vfs), desc_(desc) {96logId_ = desc.logId;97}9899ReplacedTexture::~ReplacedTexture() {100if (threadWaitable_) {101SetState(ReplacementState::CANCEL_INIT);102103std::unique_lock<std::mutex> lock(lock_);104threadWaitable_->WaitAndRelease();105threadWaitable_ = nullptr;106}107108for (auto &level : levels_) {109vfs_->ReleaseFile(level.fileRef);110level.fileRef = nullptr;111}112}113114void ReplacedTexture::PurgeIfNotUsedSinceTime(double t) {115if (State() != ReplacementState::ACTIVE) {116return;117}118119// If there's some leftover threadWaitable, get rid of it.120if (threadWaitable_) {121if (threadWaitable_->WaitFor(0.0)) {122delete threadWaitable_;123threadWaitable_ = nullptr;124// Continue with purging.125} else {126// Try next time.127return;128}129}130131// This is the only place except shutdown where a texture can transition132// from ACTIVE to anything else, so we don't actually need to lock here.133if (lastUsed_ >= t) {134return;135}136137data_.clear();138levels_.clear();139fmt = Draw::DataFormat::UNDEFINED;140alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;141142// This means we have to reload. If we never purge any, there's no need.143SetState(ReplacementState::UNLOADED);144}145146// This can only return true if ACTIVE or NOT_FOUND.147bool ReplacedTexture::Poll(double budget) {148_assert_(vfs_ != nullptr);149150double now = time_now_d();151152switch (State()) {153case ReplacementState::ACTIVE:154case ReplacementState::NOT_FOUND:155if (threadWaitable_) {156if (!threadWaitable_->WaitFor(budget)) {157lastUsed_ = now;158return false;159}160// Successfully waited! Can get rid of it.161threadWaitable_->WaitAndRelease();162threadWaitable_ = nullptr;163lastUsed = now;164}165lastUsed_ = now;166return true;167case ReplacementState::CANCEL_INIT:168case ReplacementState::PENDING:169return false;170case ReplacementState::UNLOADED:171// We're gonna need to spawn a task.172break;173}174175lastUsed_ = now;176177// Let's not even start a new texture if we're already behind.178if (budget < 0.0)179return false;180181_assert_(!threadWaitable_);182threadWaitable_ = new LimitedWaitable();183SetState(ReplacementState::PENDING);184g_threadManager.EnqueueTask(new ReplacedTextureTask(vfs_, *this, threadWaitable_));185if (threadWaitable_->WaitFor(budget)) {186// If we successfully wait here, we're done. The thread will set state accordingly.187_assert_(State() == ReplacementState::ACTIVE || State() == ReplacementState::NOT_FOUND || State() == ReplacementState::CANCEL_INIT);188delete threadWaitable_;189threadWaitable_ = nullptr;190return true;191}192// Still pending on thread.193return false;194}195196inline uint32_t RoundUpTo4(uint32_t value) {197return (value + 3) & ~3;198}199200void ReplacedTexture::Prepare(VFSBackend *vfs) {201_assert_(vfs != nullptr);202203this->vfs_ = vfs;204205std::unique_lock<std::mutex> lock(lock_);206207fmt = Draw::DataFormat::UNDEFINED;208209Draw::DataFormat pixelFormat;210LoadLevelResult result = LoadLevelResult::LOAD_ERROR;211if (desc_.filenames.empty()) {212result = LoadLevelResult::DONE;213}214for (int i = 0; i < std::min(MAX_REPLACEMENT_MIP_LEVELS, (int)desc_.filenames.size()); ++i) {215if (State() == ReplacementState::CANCEL_INIT) {216break;217}218219if (desc_.filenames[i].empty()) {220// Out of valid mip levels. Bail out.221break;222}223224VFSFileReference *fileRef = vfs_->GetFile(desc_.filenames[i].c_str());225if (!fileRef) {226if (i == 0) {227INFO_LOG(Log::G3D, "Texture replacement file '%s' not found", desc_.filenames[i].c_str());228// No file at all. Mark as NOT_FOUND.229SetState(ReplacementState::NOT_FOUND);230return;231}232// If the file doesn't exist, let's just bail immediately here.233// Mark as DONE, not error.234result = LoadLevelResult::DONE;235break;236}237238if (i == 0) {239fmt = Draw::DataFormat::R8G8B8A8_UNORM;240}241242result = LoadLevelData(fileRef, desc_.filenames[i], i, &pixelFormat);243if (result == LoadLevelResult::DONE) {244// Loaded all the levels we're gonna get.245fmt = pixelFormat;246break;247} else if (result == LoadLevelResult::CONTINUE) {248if (i == 0) {249fmt = pixelFormat;250} else {251if (fmt != pixelFormat) {252ERROR_LOG(Log::G3D, "Replacement mipmap %d doesn't have the same pixel format as mipmap 0. Stopping.", i);253break;254}255}256} else {257// Error state.258break;259}260}261262if (levels_.empty()) {263// No replacement found.264std::string name = TextureReplacer::HashName(desc_.cachekey, desc_.hash, 0);265if (result == LoadLevelResult::LOAD_ERROR) {266WARN_LOG(Log::G3D, "Failed to load replacement texture '%s'", name.c_str());267}268SetState(ReplacementState::NOT_FOUND);269return;270}271272// Update the level dimensions.273for (auto &level : levels_) {274level.fullW = (level.w * desc_.w) / desc_.newW;275level.fullH = (level.h * desc_.h) / desc_.newH;276277int blockSize;278bool bc = Draw::DataFormatIsBlockCompressed(fmt, &blockSize);279if (!bc) {280level.fullDataSize = level.fullW * level.fullH * 4;281} else {282level.fullDataSize = RoundUpTo4(level.fullW) * RoundUpTo4(level.fullH) * blockSize / 16;283}284}285286SetState(ReplacementState::ACTIVE);287288// the caller calls threadWaitable->notify().289}290291// Returns true if Prepare should keep calling this to load more levels.292ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference *fileRef, const std::string &filename, int mipLevel, Draw::DataFormat *pixelFormat) {293bool good = false;294295if (data_.size() <= mipLevel) {296data_.resize(mipLevel + 1);297}298299if (!vfs_) {300ERROR_LOG(Log::G3D, "Unexpected null vfs_ pointer in LoadLevelData");301return LoadLevelResult::LOAD_ERROR;302}303304ReplacedTextureLevel level;305size_t fileSize;306VFSOpenFile *openFile = vfs_->OpenFileForRead(fileRef, &fileSize);307if (!openFile) {308// File missing, no more levels. This is alright.309return LoadLevelResult::DONE;310}311312std::string magic;313ReplacedImageType imageType = Identify(vfs_, openFile, &magic);314315bool ddsDX10 = false;316int numMips = 1;317318if (imageType == ReplacedImageType::KTX2) {319KTXHeader header;320good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);321322level.w = header.pixelWidth;323level.h = header.pixelHeight;324numMips = header.levelCount;325326// Additional quick checks327good = good && header.layerCount <= 1;328} else if (imageType == ReplacedImageType::BASIS) {329WARN_LOG(Log::G3D, "The basis texture format is not supported. Use KTX2 (basisu texture.png -uastc -ktx2 -mipmap)");330331// We simply don't support basis files currently.332good = false;333} else if (imageType == ReplacedImageType::DDS) {334DDSHeader header;335DDSHeaderDXT10 header10{};336good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);337338*pixelFormat = Draw::DataFormat::UNDEFINED;339u32 format;340if (good && (header.ddspf.dwFlags & DDPF_FOURCC)) {341char *fcc = (char *)&header.ddspf.dwFourCC;342// INFO_LOG(Log::G3D, "DDS fourcc: %c%c%c%c", fcc[0], fcc[1], fcc[2], fcc[3]);343if (header.ddspf.dwFourCC == MK_FOURCC("DX10")) {344ddsDX10 = true;345good = good && vfs_->Read(openFile, &header10, sizeof(header10)) == sizeof(header10);346format = header10.dxgiFormat;347switch (format) {348case 71: // DXGI_FORMAT_BC1_UNORM349case 72: // DXGI_FORMAT_BC1_UNORM_SRGB350if (!desc_.formatSupport.bc123) {351WARN_LOG(Log::G3D, "BC1 format not supported, skipping texture");352good = false;353}354*pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;355break;356case 74: // DXGI_FORMAT_BC2_UNORM357case 75: // DXGI_FORMAT_BC2_UNORM_SRGB358if (!desc_.formatSupport.bc123) {359WARN_LOG(Log::G3D, "BC2 format not supported, skipping texture");360good = false;361}362*pixelFormat = Draw::DataFormat::BC2_UNORM_BLOCK;363break;364case 77: // DXGI_FORMAT_BC3_UNORM365case 78: // DXGI_FORMAT_BC3_UNORM_SRGB366if (!desc_.formatSupport.bc123) {367WARN_LOG(Log::G3D, "BC3 format not supported, skipping texture");368good = false;369}370*pixelFormat = Draw::DataFormat::BC3_UNORM_BLOCK;371break;372case 98: // DXGI_FORMAT_BC7_UNORM:373case 99: // DXGI_FORMAT_BC7_UNORM_SRGB:374if (!desc_.formatSupport.bc7) {375WARN_LOG(Log::G3D, "BC7 format not supported, skipping texture");376good = false;377}378*pixelFormat = Draw::DataFormat::BC7_UNORM_BLOCK;379break;380default:381WARN_LOG(Log::G3D, "DXGI pixel format %d not supported.", header10.dxgiFormat);382good = false;383}384} else {385if (!desc_.formatSupport.bc123) {386WARN_LOG(Log::G3D, "BC1-3 formats not supported");387good = false;388}389format = header.ddspf.dwFourCC;390// OK, there are a number of possible formats we might have ended up with. We choose just a few391// to support for now.392switch (format) {393case MK_FOURCC("DXT1"):394*pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;395break;396case MK_FOURCC("DXT3"):397*pixelFormat = Draw::DataFormat::BC2_UNORM_BLOCK;398break;399case MK_FOURCC("DXT5"):400*pixelFormat = Draw::DataFormat::BC3_UNORM_BLOCK;401break;402default:403ERROR_LOG(Log::G3D, "DDS pixel format not supported.");404good = false;405}406}407} else if (good) {408ERROR_LOG(Log::G3D, "DDS non-fourCC format not supported.");409good = false;410}411412level.w = header.dwWidth;413level.h = header.dwHeight;414numMips = header.dwMipMapCount;415} else if (imageType == ReplacedImageType::ZIM) {416uint32_t ignore = 0;417struct ZimHeader {418uint32_t magic;419uint32_t w;420uint32_t h;421uint32_t flags;422} header;423good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);424level.w = header.w;425level.h = header.h;426good = good && (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888;427*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;428} else if (imageType == ReplacedImageType::PNG) {429PNGHeaderPeek headerPeek;430good = vfs_->Read(openFile, &headerPeek, sizeof(headerPeek)) == sizeof(headerPeek);431if (good && headerPeek.IsValidPNGHeader()) {432level.w = headerPeek.Width();433level.h = headerPeek.Height();434good = true;435} else {436ERROR_LOG(Log::G3D, "Could not get PNG dimensions: %s (zip)", filename.c_str());437good = false;438}439*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;440} else {441ERROR_LOG(Log::G3D, "Could not load texture replacement info: %s - unsupported format %s", filename.c_str(), magic.c_str());442}443444// TODO: We no longer really need to have a split in this function, the upper and lower parts can be merged now.445446if (good && mipLevel != 0) {447// If loading a low mip directly (through png most likely), check that the mipmap size is correct.448// Can't load mips of the wrong size.449if (level.w != std::max(1, (levels_[0].w >> mipLevel)) || level.h != std::max(1, (levels_[0].h >> mipLevel))) {450WARN_LOG(Log::G3D, "Replacement mipmap invalid: size=%dx%d, expected=%dx%d (level %d)",451level.w, level.h, levels_[0].w >> mipLevel, levels_[0].h >> mipLevel, mipLevel);452good = false;453}454}455456if (!good) {457vfs_->CloseFile(openFile);458return LoadLevelResult::LOAD_ERROR;459}460461vfs_->Rewind(openFile);462463level.fileRef = fileRef;464465if (imageType == ReplacedImageType::KTX2) {466// Just slurp the whole file in one go and feed to the decoder.467std::vector<uint8_t> buffer;468buffer.resize(fileSize);469buffer.resize(vfs_->Read(openFile, &buffer[0], buffer.size()));470471basist::ktx2_transcoder transcoder;472if (!transcoder.init(buffer.data(), (int)buffer.size())) {473WARN_LOG(Log::G3D, "Error reading KTX file");474vfs_->CloseFile(openFile);475return LoadLevelResult::LOAD_ERROR;476}477478// Figure out the target format.479basist::transcoder_texture_format transcoderFormat;480if (transcoder.is_etc1s()) {481// We only support opaque colors with this compression method.482alphaStatus_ = ReplacedTextureAlpha::FULL;483// Let's pick a suitable compatible format.484if (desc_.formatSupport.bc123) {485transcoderFormat = basist::transcoder_texture_format::cTFBC1;486*pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;487} else if (desc_.formatSupport.etc2) {488transcoderFormat = basist::transcoder_texture_format::cTFETC1_RGB;489*pixelFormat = Draw::DataFormat::ETC2_R8G8B8_UNORM_BLOCK;490} else {491// Transcode to RGBA8 instead as a fallback. A bit slow and takes a lot of memory, but better than nothing.492WARN_LOG(Log::G3D, "Replacement texture format not supported - transcoding to RGBA8888");493transcoderFormat = basist::transcoder_texture_format::cTFRGBA32;494*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;495}496} else if (transcoder.is_uastc()) {497// TODO: Try to recover some indication of alpha from the actual data blocks.498alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;499// Let's pick a suitable compatible format.500if (desc_.formatSupport.bc7) {501transcoderFormat = basist::transcoder_texture_format::cTFBC7_RGBA;502*pixelFormat = Draw::DataFormat::BC7_UNORM_BLOCK;503} else if (desc_.formatSupport.astc) {504transcoderFormat = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;505*pixelFormat = Draw::DataFormat::ASTC_4x4_UNORM_BLOCK;506} else {507// Transcode to RGBA8 instead as a fallback. A bit slow and takes a lot of memory, but better than nothing.508WARN_LOG(Log::G3D, "Replacement texture format not supported - transcoding to RGBA8888");509transcoderFormat = basist::transcoder_texture_format::cTFRGBA32;510*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;511}512} else {513WARN_LOG(Log::G3D, "PPSSPP currently only supports KTX for basis/UASTC textures. This may change in the future.");514vfs_->CloseFile(openFile);515return LoadLevelResult::LOAD_ERROR;516}517518int blockSize = 0;519bool bc = Draw::DataFormatIsBlockCompressed(*pixelFormat, &blockSize);520_dbg_assert_(bc || *pixelFormat == Draw::DataFormat::R8G8B8A8_UNORM);521522if (bc && ((level.w & 3) != 0 || (level.h & 3) != 0)) {523WARN_LOG(Log::G3D, "Block compressed replacement texture '%s' not divisible by 4x4 (%dx%d). In D3D11 (only!) we will have to expand (potentially causing glitches).", filename.c_str(), level.w, level.h);524}525526data_.resize(numMips);527528basist::ktx2_transcoder_state transcodeState; // Each thread needs one of these.529530transcoder.start_transcoding();531levels_.reserve(numMips);532for (int i = 0; i < numMips; i++) {533std::vector<uint8_t> &out = data_[mipLevel + i];534535basist::ktx2_image_level_info levelInfo{};536bool result = transcoder.get_image_level_info(levelInfo, i, 0, 0);537_dbg_assert_(result);538539size_t dataSizeBytes = levelInfo.m_total_blocks * blockSize;540size_t outputSize = levelInfo.m_total_blocks;541size_t outputPitch = levelInfo.m_num_blocks_x;542// Support transcoded-to-RGBA8888 images too.543if (!bc) {544dataSizeBytes = levelInfo.m_orig_width * levelInfo.m_orig_height * 4;545outputSize = levelInfo.m_orig_width * levelInfo.m_orig_height;546outputPitch = levelInfo.m_orig_width;547}548data_[i].resize(dataSizeBytes);549550transcodeState.clear();551transcoder.transcode_image_level(i, 0, 0, &out[0], (uint32_t)outputSize, transcoderFormat, 0, (uint32_t)outputPitch, level.h, -1, -1, &transcodeState);552level.w = levelInfo.m_orig_width;553level.h = levelInfo.m_orig_height;554if (i != 0)555level.fileRef = nullptr;556levels_.push_back(level);557}558transcoder.clear();559vfs_->CloseFile(openFile);560561return LoadLevelResult::DONE; // don't read more levels562} else if (imageType == ReplacedImageType::DDS) {563// TODO: Do better with alphaStatus, it's possible.564alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;565566DDSHeader header;567DDSHeaderDXT10 header10{};568vfs_->Read(openFile, &header, sizeof(header));569if (ddsDX10) {570vfs_->Read(openFile, &header10, sizeof(header10));571}572573int blockSize = 0;574bool bc = Draw::DataFormatIsBlockCompressed(*pixelFormat, &blockSize);575_dbg_assert_(bc);576577if (bc && ((level.w & 3) != 0 || (level.h & 3) != 0)) {578WARN_LOG(Log::G3D, "Block compressed replacement texture '%s' not divisible by 4x4 (%dx%d). In D3D11 (only!) we will have to expand (potentially causing glitches).", filename.c_str(), level.w, level.h);579}580581data_.resize(numMips);582583// A DDS File can contain multiple mipmaps.584levels_.reserve(numMips);585for (int i = 0; i < numMips; i++) {586std::vector<uint8_t> &out = data_[mipLevel + i];587588int bytesToRead = RoundUpTo4(level.w) * RoundUpTo4(level.h) * blockSize / 16;589out.resize(bytesToRead);590591size_t read_bytes = vfs_->Read(openFile, &out[0], bytesToRead);592if (read_bytes != bytesToRead) {593WARN_LOG(Log::G3D, "DDS: Expected %d bytes, got %d", bytesToRead, (int)read_bytes);594}595596levels_.push_back(level);597level.w = std::max(level.w / 2, 1);598level.h = std::max(level.h / 2, 1);599if (i != 0)600level.fileRef = nullptr; // We only provide a fileref on level 0 if we have mipmaps.601}602vfs_->CloseFile(openFile);603604return LoadLevelResult::DONE; // don't read more levels605606} else if (imageType == ReplacedImageType::ZIM) {607608auto zim = std::make_unique<uint8_t[]>(fileSize);609if (!zim) {610ERROR_LOG(Log::G3D, "Failed to allocate memory for texture replacement");611vfs_->CloseFile(openFile);612return LoadLevelResult::LOAD_ERROR;613}614615if (vfs_->Read(openFile, &zim[0], fileSize) != fileSize) {616ERROR_LOG(Log::G3D, "Could not load texture replacement: %s - failed to read ZIM", filename.c_str());617vfs_->CloseFile(openFile);618return LoadLevelResult::LOAD_ERROR;619}620621int w, h, f;622uint8_t *image;623std::vector<uint8_t> &out = data_[mipLevel];624// TODO: Zim files can actually hold mipmaps (although no tool has ever been made to create them :P)625if (LoadZIMPtr(&zim[0], fileSize, &w, &h, &f, &image)) {626if (w > level.w || h > level.h) {627ERROR_LOG(Log::G3D, "Texture replacement changed since header read: %s", filename.c_str());628vfs_->CloseFile(openFile);629return LoadLevelResult::LOAD_ERROR;630}631632out.resize(level.w * level.h * 4);633if (w == level.w) {634memcpy(&out[0], image, level.w * 4 * level.h);635} else {636for (int y = 0; y < h; ++y) {637memcpy(&out[level.w * 4 * y], image + w * 4 * y, w * 4);638}639}640free(image);641642CheckAlphaResult res = CheckAlpha32Rect((u32 *)&out[0], level.w, w, h, 0xFF000000);643if (res == CHECKALPHA_ANY || mipLevel == 0) {644alphaStatus_ = ReplacedTextureAlpha(res);645}646levels_.push_back(level);647} else {648good = false;649}650651vfs_->CloseFile(openFile);652return LoadLevelResult::CONTINUE;653654} else if (imageType == ReplacedImageType::PNG) {655png_image png = {};656png.version = PNG_IMAGE_VERSION;657658std::string pngdata;659pngdata.resize(fileSize);660pngdata.resize(vfs_->Read(openFile, &pngdata[0], fileSize));661if (!png_image_begin_read_from_memory(&png, &pngdata[0], pngdata.size())) {662ERROR_LOG(Log::G3D, "Could not load texture replacement info: %s - %s (zip)", filename.c_str(), png.message);663vfs_->CloseFile(openFile);664return LoadLevelResult::LOAD_ERROR;665}666if (png.width > (uint32_t)level.w || png.height > (uint32_t)level.h) {667ERROR_LOG(Log::G3D, "Texture replacement changed since header read: %s", filename.c_str());668vfs_->CloseFile(openFile);669return LoadLevelResult::LOAD_ERROR;670}671672bool checkedAlpha = false;673if ((png.format & PNG_FORMAT_FLAG_ALPHA) == 0) {674// Well, we know for sure it doesn't have alpha.675if (mipLevel == 0) {676alphaStatus_ = ReplacedTextureAlpha::FULL;677}678checkedAlpha = true;679}680png.format = PNG_FORMAT_RGBA;681682std::vector<uint8_t> &out = data_[mipLevel];683// TODO: Should probably try to handle out-of-memory gracefully here.684out.resize(level.w * level.h * 4);685if (!png_image_finish_read(&png, nullptr, &out[0], level.w * 4, nullptr)) {686ERROR_LOG(Log::G3D, "Could not load texture replacement: %s - %s", filename.c_str(), png.message);687vfs_->CloseFile(openFile);688out.resize(0);689return LoadLevelResult::LOAD_ERROR;690}691png_image_free(&png);692693if (!checkedAlpha) {694// This will only check the hashed bits.695CheckAlphaResult res = CheckAlpha32Rect((u32 *)&out[0], level.w, png.width, png.height, 0xFF000000);696if (res == CHECKALPHA_ANY || mipLevel == 0) {697alphaStatus_ = ReplacedTextureAlpha(res);698}699}700701levels_.push_back(level);702703vfs_->CloseFile(openFile);704return LoadLevelResult::CONTINUE;705} else {706WARN_LOG(Log::G3D, "Don't know how to load this image type! %d", (int)imageType);707vfs_->CloseFile(openFile);708}709return LoadLevelResult::LOAD_ERROR;710}711712bool ReplacedTexture::CopyLevelTo(int level, uint8_t *out, size_t outDataSize, int rowPitch) {713_assert_msg_((size_t)level < levels_.size(), "Invalid miplevel");714_assert_msg_(out != nullptr && rowPitch > 0, "Invalid out/pitch");715716if (State() != ReplacementState::ACTIVE) {717WARN_LOG(Log::G3D, "Init not done yet");718return false;719}720721// We pad the images right here during the copy.722// TODO: Add support for the texture cache to scale texture coordinates instead.723// It already supports this for render target textures that aren't powers of 2.724725int outW = levels_[level].fullW;726int outH = levels_[level].fullH;727728// We probably could avoid this lock, but better to play it safe.729std::lock_guard<std::mutex> guard(lock_);730731const ReplacedTextureLevel &info = levels_[level];732const std::vector<uint8_t> &data = data_[level];733734if (data.empty()) {735WARN_LOG(Log::G3D, "Level %d is empty", level);736return false;737}738739#define PARALLEL_COPY740741int blockSize;742if (!Draw::DataFormatIsBlockCompressed(fmt, &blockSize)) {743if (fmt != Draw::DataFormat::R8G8B8A8_UNORM) {744ERROR_LOG(Log::G3D, "Unexpected linear data format");745return false;746}747748if (rowPitch < info.w * 4) {749ERROR_LOG(Log::G3D, "Replacement rowPitch=%d, but w=%d (level=%d) (too small)", rowPitch, info.w * 4, level);750return false;751}752753_assert_msg_(data.size() == info.w * info.h * 4, "Data has wrong size");754755if (rowPitch == info.w * 4) {756#ifdef PARALLEL_COPY757ParallelMemcpy(&g_threadManager, out, data.data(), info.w * 4 * info.h);758#else759memcpy(out, data.data(), info.w * 4 * info.h);760#endif761} else {762#ifdef PARALLEL_COPY763const int MIN_LINES_PER_THREAD = 4;764ParallelRangeLoop(&g_threadManager, [&](int l, int h) {765int extraPixels = outW - info.w;766for (int y = l; y < h; ++y) {767memcpy((uint8_t *)out + rowPitch * y, data.data() + info.w * 4 * y, info.w * 4);768// Fill the rest of the line with black.769memset((uint8_t *)out + rowPitch * y + info.w * 4, 0, extraPixels * 4);770}771}, 0, info.h, MIN_LINES_PER_THREAD);772#else773int extraPixels = outW - info.w;774for (int y = 0; y < info.h; ++y) {775memcpy((uint8_t *)out + rowPitch * y, data.data() + info.w * 4 * y, info.w * 4);776memset((uint8_t *)out + rowPitch * y + info.w * 4, 0, extraPixels * 4);777}778#endif779// Memset the rest of the padding to avoid leaky edge pixels. Guess we could parallelize this too, but meh.780for (int y = info.h; y < outH; y++) {781uint8_t *dest = (uint8_t *)out + rowPitch * y;782memset(dest, 0, outW * 4);783}784}785} else {786#ifdef PARALLEL_COPY787// Only parallel copy in the simple case for now.788if (info.w == outW && info.h == outH) {789// TODO: Add sanity checks here for other formats?790ParallelMemcpy(&g_threadManager, out, data.data(), data.size());791return true;792}793#endif794// Alright, so careful copying of blocks it is, padding with zero-blocks as needed.795int inBlocksW = (info.w + 3) / 4;796int inBlocksH = (info.h + 3) / 4;797int outBlocksW = (info.fullW + 3) / 4;798int outBlocksH = (info.fullH + 3) / 4;799800int paddingBlocksX = outBlocksW - inBlocksW;801802// Copy all the known blocks, and zero-fill out the lines.803for (int y = 0; y < inBlocksH; y++) {804const uint8_t *input = data.data() + y * inBlocksW * blockSize;805uint8_t *output = (uint8_t *)out + y * outBlocksW * blockSize;806memcpy(output, input, inBlocksW * blockSize);807memset(output + inBlocksW * blockSize, 0, paddingBlocksX * blockSize);808}809810// Vertical zero-padding.811for (int y = inBlocksH; y < outBlocksH; y++) {812uint8_t *output = (uint8_t *)out + y * outBlocksW * blockSize;813memset(output, 0, outBlocksW * blockSize);814}815}816817return true;818}819820const char *StateString(ReplacementState state) {821switch (state) {822case ReplacementState::UNLOADED: return "UNLOADED";823case ReplacementState::PENDING: return "PENDING";824case ReplacementState::NOT_FOUND: return "NOT_FOUND";825case ReplacementState::ACTIVE: return "ACTIVE";826case ReplacementState::CANCEL_INIT: return "CANCEL_INIT";827default: return "N/A";828}829}830831832