Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/ReplacedTexture.cpp
3187 views
1
// Copyright (c) 2016- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
20
#include "ppsspp_config.h"
21
22
#include <png.h>
23
24
#include "ext/basis_universal/basisu_transcoder.h"
25
#include "ext/basis_universal/basisu_file_headers.h"
26
27
#include "GPU/Common/ReplacedTexture.h"
28
#include "GPU/Common/TextureReplacer.h"
29
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"
38
39
#define MK_FOURCC(str) (str[0] | ((uint8_t)str[1] << 8) | ((uint8_t)str[2] << 16) | ((uint8_t)str[3] << 24))
40
41
static ReplacedImageType IdentifyMagic(const uint8_t magic[4]) {
42
if (memcmp((const char *)magic, "ZIMG", 4) == 0)
43
return ReplacedImageType::ZIM;
44
else if (magic[0] == 0x89 && strncmp((const char *)&magic[1], "PNG", 3) == 0)
45
return ReplacedImageType::PNG;
46
else if (memcmp((const char *)magic, "DDS ", 4) == 0)
47
return ReplacedImageType::DDS;
48
else if (magic[0] == 's' && magic[1] == 'B') {
49
uint16_t ver = magic[2] | (magic[3] << 8);
50
if (ver >= 0x10) {
51
return ReplacedImageType::BASIS;
52
}
53
} else if (memcmp((const char *)magic, "\xabKTX", 4) == 0) {
54
// Technically, should read 12 bytes here, but this'll do.
55
return ReplacedImageType::KTX2;
56
}
57
return ReplacedImageType::INVALID;
58
}
59
60
static ReplacedImageType Identify(VFSBackend *vfs, VFSOpenFile *openFile, std::string *outMagic) {
61
uint8_t magic[4];
62
if (vfs->Read(openFile, magic, 4) != 4) {
63
*outMagic = "FAIL";
64
return 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);
68
for (int i = 0; i < outMagic->size(); i++) {
69
if ((s8)(*outMagic)[i] < 32) {
70
(*outMagic)[i] = '_';
71
}
72
}
73
vfs->Rewind(openFile);
74
return IdentifyMagic(magic);
75
}
76
77
class ReplacedTextureTask : public Task {
78
public:
79
ReplacedTextureTask(VFSBackend *vfs, ReplacedTexture &tex, LimitedWaitable *w) : vfs_(vfs), tex_(tex), waitable_(w) {}
80
81
TaskType Type() const override { return TaskType::IO_BLOCKING; }
82
TaskPriority Priority() const override { return TaskPriority::NORMAL; }
83
84
void Run() override {
85
tex_.Prepare(vfs_);
86
waitable_->Notify();
87
}
88
89
private:
90
VFSBackend *vfs_;
91
ReplacedTexture &tex_;
92
LimitedWaitable *waitable_;
93
};
94
95
ReplacedTexture::ReplacedTexture(VFSBackend *vfs, const ReplacementDesc &desc) : vfs_(vfs), desc_(desc) {
96
logId_ = desc.logId;
97
}
98
99
ReplacedTexture::~ReplacedTexture() {
100
if (threadWaitable_) {
101
SetState(ReplacementState::CANCEL_INIT);
102
103
threadWaitable_->WaitAndRelease();
104
threadWaitable_ = nullptr;
105
}
106
107
for (auto &level : levels_) {
108
vfs_->ReleaseFile(level.fileRef);
109
level.fileRef = nullptr;
110
}
111
}
112
113
void ReplacedTexture::PurgeIfNotUsedSinceTime(double t) {
114
if (State() != ReplacementState::ACTIVE) {
115
return;
116
}
117
118
// If there's some leftover threadWaitable, get rid of it.
119
if (threadWaitable_) {
120
if (threadWaitable_->WaitFor(0.0)) {
121
delete threadWaitable_;
122
threadWaitable_ = nullptr;
123
// Continue with purging.
124
} else {
125
// Try next time.
126
return;
127
}
128
}
129
130
// This is the only place except shutdown where a texture can transition
131
// from ACTIVE to anything else, so we don't actually need to lock here.
132
if (lastUsed_ >= t) {
133
return;
134
}
135
136
data_.clear();
137
levels_.clear();
138
fmt = Draw::DataFormat::UNDEFINED;
139
alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
140
141
// This means we have to reload. If we never purge any, there's no need.
142
SetState(ReplacementState::UNLOADED);
143
}
144
145
// This can only return true if ACTIVE or NOT_FOUND.
146
bool ReplacedTexture::Poll(double budget) {
147
_assert_(vfs_ != nullptr);
148
149
double now = time_now_d();
150
151
switch (State()) {
152
case ReplacementState::ACTIVE:
153
case ReplacementState::NOT_FOUND:
154
if (threadWaitable_) {
155
if (!threadWaitable_->WaitFor(budget)) {
156
lastUsed_ = now;
157
return false;
158
}
159
// Successfully waited! Can get rid of it.
160
threadWaitable_->WaitAndRelease();
161
threadWaitable_ = nullptr;
162
lastUsed = now;
163
}
164
lastUsed_ = now;
165
return true;
166
case ReplacementState::CANCEL_INIT:
167
case ReplacementState::PENDING:
168
return false;
169
case ReplacementState::UNLOADED:
170
// We're gonna need to spawn a task.
171
break;
172
default:
173
break;
174
}
175
176
lastUsed_ = now;
177
178
// Let's not even start a new texture if we're already behind.
179
// Note that 0.0 is used as a signalling value that we don't want to wait (just handling already finished textures).
180
if (budget < 0.0)
181
return false;
182
183
_assert_(!threadWaitable_);
184
threadWaitable_ = new LimitedWaitable();
185
SetState(ReplacementState::PENDING);
186
g_threadManager.EnqueueTask(new ReplacedTextureTask(vfs_, *this, threadWaitable_));
187
if (threadWaitable_->WaitFor(budget)) {
188
// If we successfully wait here, we're done. The thread will set state accordingly.
189
_assert_(State() == ReplacementState::ACTIVE || State() == ReplacementState::NOT_FOUND || State() == ReplacementState::CANCEL_INIT);
190
delete threadWaitable_;
191
threadWaitable_ = nullptr;
192
return true;
193
}
194
// Still pending on thread.
195
return false;
196
}
197
198
inline uint32_t RoundUpTo4(uint32_t value) {
199
return (value + 3) & ~3;
200
}
201
202
void ReplacedTexture::Prepare(VFSBackend *vfs) {
203
_assert_(vfs != nullptr);
204
205
this->vfs_ = vfs;
206
207
std::unique_lock<std::mutex> lock(lock_);
208
209
fmt = Draw::DataFormat::UNDEFINED;
210
211
Draw::DataFormat pixelFormat;
212
LoadLevelResult result = LoadLevelResult::LOAD_ERROR;
213
if (desc_.filenames.empty()) {
214
result = LoadLevelResult::DONE;
215
}
216
for (int i = 0; i < std::min(MAX_REPLACEMENT_MIP_LEVELS, (int)desc_.filenames.size()); ++i) {
217
if (State() == ReplacementState::CANCEL_INIT) {
218
break;
219
}
220
221
if (desc_.filenames[i].empty()) {
222
// Out of valid mip levels. Bail out.
223
break;
224
}
225
226
VFSFileReference *fileRef = vfs_->GetFile(desc_.filenames[i].c_str());
227
if (!fileRef) {
228
if (i == 0) {
229
INFO_LOG(Log::TexReplacement, "Texture replacement file '%s' not found in %s", desc_.filenames[i].c_str(), vfs_->toString().c_str());
230
// No file at all. Mark as NOT_FOUND.
231
SetState(ReplacementState::NOT_FOUND);
232
return;
233
}
234
// If the file doesn't exist, let's just bail immediately here.
235
// Mark as DONE, not error.
236
result = LoadLevelResult::DONE;
237
break;
238
}
239
240
if (i == 0) {
241
fmt = Draw::DataFormat::R8G8B8A8_UNORM;
242
}
243
244
result = LoadLevelData(fileRef, desc_.filenames[i], i, &pixelFormat);
245
if (result == LoadLevelResult::DONE) {
246
// Loaded all the levels we're gonna get.
247
fmt = pixelFormat;
248
break;
249
} else if (result == LoadLevelResult::CONTINUE) {
250
if (i == 0) {
251
fmt = pixelFormat;
252
} else {
253
if (fmt != pixelFormat) {
254
ERROR_LOG(Log::TexReplacement, "Replacement mipmap %d doesn't have the same pixel format as mipmap 0. Stopping.", i);
255
break;
256
}
257
}
258
} else {
259
// Error state.
260
break;
261
}
262
}
263
264
if (levels_.empty()) {
265
// No replacement found.
266
std::string name = TextureReplacer::HashName(desc_.cachekey, desc_.hash, 0);
267
if (result == LoadLevelResult::LOAD_ERROR) {
268
WARN_LOG(Log::TexReplacement, "Failed to load replacement texture '%s'", name.c_str());
269
}
270
SetState(ReplacementState::NOT_FOUND);
271
return;
272
}
273
274
// Update the level dimensions.
275
for (auto &level : levels_) {
276
level.fullW = (level.w * desc_.w) / desc_.newW;
277
level.fullH = (level.h * desc_.h) / desc_.newH;
278
279
int blockSize;
280
bool bc = Draw::DataFormatIsBlockCompressed(fmt, &blockSize);
281
if (!bc) {
282
level.fullDataSize = level.fullW * level.fullH * 4;
283
} else {
284
level.fullDataSize = RoundUpTo4(level.fullW) * RoundUpTo4(level.fullH) * blockSize / 16;
285
}
286
}
287
288
SetState(ReplacementState::ACTIVE);
289
290
// the caller calls threadWaitable->notify().
291
}
292
293
// Returns true if Prepare should keep calling this to load more levels.
294
ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference *fileRef, const std::string &filename, int mipLevel, Draw::DataFormat *pixelFormat) {
295
bool good = false;
296
297
if (data_.size() <= mipLevel) {
298
data_.resize(mipLevel + 1);
299
}
300
301
if (!vfs_) {
302
ERROR_LOG(Log::TexReplacement, "Unexpected null vfs_ pointer in LoadLevelData");
303
return LoadLevelResult::LOAD_ERROR;
304
}
305
306
ReplacedTextureLevel level;
307
size_t fileSize;
308
VFSOpenFile *openFile = vfs_->OpenFileForRead(fileRef, &fileSize);
309
if (!openFile) {
310
// File missing, no more levels. This is alright.
311
return LoadLevelResult::DONE;
312
}
313
314
std::string magic;
315
ReplacedImageType imageType = Identify(vfs_, openFile, &magic);
316
317
bool ddsDX10 = false;
318
int numMips = 1;
319
320
if (imageType == ReplacedImageType::KTX2) {
321
KTXHeader header;
322
good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);
323
324
level.w = header.pixelWidth;
325
level.h = header.pixelHeight;
326
numMips = header.levelCount;
327
328
// Additional quick checks
329
good = good && header.layerCount <= 1;
330
} else if (imageType == ReplacedImageType::BASIS) {
331
WARN_LOG(Log::TexReplacement, "The basis texture format is not supported. Use KTX2 (basisu texture.png -uastc -ktx2 -mipmap)");
332
333
// We simply don't support basis files currently.
334
good = false;
335
} else if (imageType == ReplacedImageType::DDS) {
336
DDSHeader header;
337
DDSHeaderDXT10 header10{};
338
good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);
339
340
*pixelFormat = Draw::DataFormat::UNDEFINED;
341
u32 format;
342
if (good && (header.ddspf.dwFlags & DDPF_FOURCC)) {
343
char *fcc = (char *)&header.ddspf.dwFourCC;
344
// INFO_LOG(Log::TexReplacement, "DDS fourcc: %c%c%c%c", fcc[0], fcc[1], fcc[2], fcc[3]);
345
if (header.ddspf.dwFourCC == MK_FOURCC("DX10")) {
346
ddsDX10 = true;
347
good = good && vfs_->Read(openFile, &header10, sizeof(header10)) == sizeof(header10);
348
format = header10.dxgiFormat;
349
switch (format) {
350
case 71: // DXGI_FORMAT_BC1_UNORM
351
case 72: // DXGI_FORMAT_BC1_UNORM_SRGB
352
if (!desc_.formatSupport.bc123) {
353
WARN_LOG(Log::TexReplacement, "BC1 format not supported, skipping texture");
354
good = false;
355
}
356
*pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;
357
break;
358
case 74: // DXGI_FORMAT_BC2_UNORM
359
case 75: // DXGI_FORMAT_BC2_UNORM_SRGB
360
if (!desc_.formatSupport.bc123) {
361
WARN_LOG(Log::TexReplacement, "BC2 format not supported, skipping texture");
362
good = false;
363
}
364
*pixelFormat = Draw::DataFormat::BC2_UNORM_BLOCK;
365
break;
366
case 77: // DXGI_FORMAT_BC3_UNORM
367
case 78: // DXGI_FORMAT_BC3_UNORM_SRGB
368
if (!desc_.formatSupport.bc123) {
369
WARN_LOG(Log::TexReplacement, "BC3 format not supported, skipping texture");
370
good = false;
371
}
372
*pixelFormat = Draw::DataFormat::BC3_UNORM_BLOCK;
373
break;
374
case 98: // DXGI_FORMAT_BC7_UNORM:
375
case 99: // DXGI_FORMAT_BC7_UNORM_SRGB:
376
if (!desc_.formatSupport.bc7) {
377
WARN_LOG(Log::TexReplacement, "BC7 format not supported, skipping texture");
378
good = false;
379
}
380
*pixelFormat = Draw::DataFormat::BC7_UNORM_BLOCK;
381
break;
382
default:
383
WARN_LOG(Log::TexReplacement, "DXGI pixel format %d not supported.", header10.dxgiFormat);
384
good = false;
385
}
386
} else {
387
if (!desc_.formatSupport.bc123) {
388
WARN_LOG(Log::TexReplacement, "BC1-3 formats not supported");
389
good = false;
390
}
391
format = header.ddspf.dwFourCC;
392
// OK, there are a number of possible formats we might have ended up with. We choose just a few
393
// to support for now.
394
switch (format) {
395
case MK_FOURCC("DXT1"):
396
*pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;
397
break;
398
case MK_FOURCC("DXT3"):
399
*pixelFormat = Draw::DataFormat::BC2_UNORM_BLOCK;
400
break;
401
case MK_FOURCC("DXT5"):
402
*pixelFormat = Draw::DataFormat::BC3_UNORM_BLOCK;
403
break;
404
default:
405
ERROR_LOG(Log::TexReplacement, "DDS pixel format not supported.");
406
good = false;
407
}
408
}
409
} else if (good) {
410
ERROR_LOG(Log::TexReplacement, "DDS non-fourCC format not supported.");
411
good = false;
412
}
413
414
level.w = header.dwWidth;
415
level.h = header.dwHeight;
416
numMips = header.dwMipMapCount;
417
} else if (imageType == ReplacedImageType::ZIM) {
418
uint32_t ignore = 0;
419
struct ZimHeader {
420
uint32_t magic;
421
uint32_t w;
422
uint32_t h;
423
uint32_t flags;
424
} header;
425
good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);
426
level.w = header.w;
427
level.h = header.h;
428
good = good && (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888;
429
*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;
430
} else if (imageType == ReplacedImageType::PNG) {
431
PNGHeaderPeek headerPeek;
432
good = vfs_->Read(openFile, &headerPeek, sizeof(headerPeek)) == sizeof(headerPeek);
433
if (good && headerPeek.IsValidPNGHeader()) {
434
level.w = headerPeek.Width();
435
level.h = headerPeek.Height();
436
good = true;
437
} else {
438
ERROR_LOG(Log::TexReplacement, "Could not get PNG dimensions: %s (zip)", filename.c_str());
439
good = false;
440
}
441
*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;
442
} else {
443
ERROR_LOG(Log::TexReplacement, "Could not load texture replacement info: %s - unsupported format %s", filename.c_str(), magic.c_str());
444
}
445
446
// TODO: We no longer really need to have a split in this function, the upper and lower parts can be merged now.
447
448
if (good && mipLevel != 0) {
449
// If loading a low mip directly (through png most likely), check that the mipmap size is correct.
450
// Can't load mips of the wrong size.
451
if (level.w != std::max(1, (levels_[0].w >> mipLevel)) || level.h != std::max(1, (levels_[0].h >> mipLevel))) {
452
WARN_LOG(Log::TexReplacement, "Replacement mipmap invalid: size=%dx%d, expected=%dx%d (level %d)",
453
level.w, level.h, levels_[0].w >> mipLevel, levels_[0].h >> mipLevel, mipLevel);
454
good = false;
455
}
456
}
457
458
if (!good) {
459
vfs_->CloseFile(openFile);
460
return LoadLevelResult::LOAD_ERROR;
461
}
462
463
vfs_->Rewind(openFile);
464
465
level.fileRef = fileRef;
466
467
if (imageType == ReplacedImageType::KTX2) {
468
// Just slurp the whole file in one go and feed to the decoder.
469
std::vector<uint8_t> buffer;
470
buffer.resize(fileSize);
471
buffer.resize(vfs_->Read(openFile, &buffer[0], buffer.size()));
472
vfs_->CloseFile(openFile);
473
474
basist::ktx2_transcoder transcoder;
475
if (!transcoder.init(buffer.data(), (int)buffer.size())) {
476
WARN_LOG(Log::TexReplacement, "Error reading KTX file");
477
return LoadLevelResult::LOAD_ERROR;
478
}
479
480
// Figure out the target format.
481
basist::transcoder_texture_format transcoderFormat;
482
if (transcoder.is_etc1s()) {
483
// We only support opaque colors with this compression method.
484
alphaStatus_ = ReplacedTextureAlpha::FULL;
485
// Let's pick a suitable compatible format.
486
if (desc_.formatSupport.bc123) {
487
transcoderFormat = basist::transcoder_texture_format::cTFBC1;
488
*pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;
489
} else if (desc_.formatSupport.etc2) {
490
transcoderFormat = basist::transcoder_texture_format::cTFETC1_RGB;
491
*pixelFormat = Draw::DataFormat::ETC2_R8G8B8_UNORM_BLOCK;
492
} else {
493
// Transcode to RGBA8 instead as a fallback. A bit slow and takes a lot of memory, but better than nothing.
494
WARN_LOG(Log::TexReplacement, "Replacement texture format not supported - transcoding to RGBA8888");
495
transcoderFormat = basist::transcoder_texture_format::cTFRGBA32;
496
*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;
497
}
498
} else if (transcoder.is_uastc()) {
499
// TODO: Try to recover some indication of alpha from the actual data blocks.
500
alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
501
// Let's pick a suitable compatible format.
502
if (desc_.formatSupport.bc7) {
503
transcoderFormat = basist::transcoder_texture_format::cTFBC7_RGBA;
504
*pixelFormat = Draw::DataFormat::BC7_UNORM_BLOCK;
505
} else if (desc_.formatSupport.astc) {
506
transcoderFormat = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
507
*pixelFormat = Draw::DataFormat::ASTC_4x4_UNORM_BLOCK;
508
} else {
509
// Transcode to RGBA8 instead as a fallback. A bit slow and takes a lot of memory, but better than nothing.
510
WARN_LOG(Log::TexReplacement, "Replacement texture format not supported - transcoding to RGBA8888");
511
transcoderFormat = basist::transcoder_texture_format::cTFRGBA32;
512
*pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;
513
}
514
} else {
515
WARN_LOG(Log::TexReplacement, "PPSSPP currently only supports KTX for basis/UASTC textures. This may change in the future.");
516
return LoadLevelResult::LOAD_ERROR;
517
}
518
519
int blockSize = 0;
520
bool bc = Draw::DataFormatIsBlockCompressed(*pixelFormat, &blockSize);
521
_dbg_assert_(bc || *pixelFormat == Draw::DataFormat::R8G8B8A8_UNORM);
522
523
if (bc && ((level.w & 3) != 0 || (level.h & 3) != 0)) {
524
WARN_LOG(Log::TexReplacement, "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);
525
}
526
527
data_.resize(numMips);
528
529
basist::ktx2_transcoder_state transcodeState; // Each thread needs one of these.
530
531
transcoder.start_transcoding();
532
levels_.reserve(numMips);
533
for (int i = 0; i < numMips; i++) {
534
std::vector<uint8_t> &out = data_[mipLevel + i];
535
536
basist::ktx2_image_level_info levelInfo{};
537
bool result = transcoder.get_image_level_info(levelInfo, i, 0, 0);
538
_dbg_assert_(result);
539
540
size_t dataSizeBytes = levelInfo.m_total_blocks * blockSize;
541
size_t outputSize = levelInfo.m_total_blocks;
542
size_t outputPitch = levelInfo.m_num_blocks_x;
543
// Support transcoded-to-RGBA8888 images too.
544
if (!bc) {
545
dataSizeBytes = levelInfo.m_orig_width * levelInfo.m_orig_height * 4;
546
outputSize = levelInfo.m_orig_width * levelInfo.m_orig_height;
547
outputPitch = levelInfo.m_orig_width;
548
}
549
data_[i].resize(dataSizeBytes);
550
551
transcodeState.clear();
552
transcoder.transcode_image_level(i, 0, 0, &out[0], (uint32_t)outputSize, transcoderFormat, 0, (uint32_t)outputPitch, level.h, -1, -1, &transcodeState);
553
level.w = levelInfo.m_orig_width;
554
level.h = levelInfo.m_orig_height;
555
if (i != 0)
556
level.fileRef = nullptr;
557
levels_.push_back(level);
558
}
559
transcoder.clear();
560
561
return LoadLevelResult::DONE; // don't read more levels
562
} else if (imageType == ReplacedImageType::DDS) {
563
// TODO: Do better with alphaStatus, it's possible.
564
alphaStatus_ = ReplacedTextureAlpha::UNKNOWN;
565
566
DDSHeader header;
567
DDSHeaderDXT10 header10{};
568
vfs_->Read(openFile, &header, sizeof(header));
569
if (ddsDX10) {
570
vfs_->Read(openFile, &header10, sizeof(header10));
571
}
572
573
int blockSize = 0;
574
bool bc = Draw::DataFormatIsBlockCompressed(*pixelFormat, &blockSize);
575
_dbg_assert_(bc);
576
577
if (bc && ((level.w & 3) != 0 || (level.h & 3) != 0)) {
578
WARN_LOG(Log::TexReplacement, "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
}
580
581
data_.resize(numMips);
582
583
// A DDS File can contain multiple mipmaps.
584
levels_.reserve(numMips);
585
for (int i = 0; i < numMips; i++) {
586
std::vector<uint8_t> &out = data_[mipLevel + i];
587
588
int bytesToRead = RoundUpTo4(level.w) * RoundUpTo4(level.h) * blockSize / 16;
589
out.resize(bytesToRead);
590
591
size_t read_bytes = vfs_->Read(openFile, &out[0], bytesToRead);
592
if (read_bytes != bytesToRead) {
593
WARN_LOG(Log::TexReplacement, "DDS: Expected %d bytes, got %d", bytesToRead, (int)read_bytes);
594
}
595
596
levels_.push_back(level);
597
level.w = std::max(level.w / 2, 1);
598
level.h = std::max(level.h / 2, 1);
599
if (i != 0)
600
level.fileRef = nullptr; // We only provide a fileref on level 0 if we have mipmaps.
601
}
602
vfs_->CloseFile(openFile);
603
604
return LoadLevelResult::DONE; // don't read more levels
605
606
} else if (imageType == ReplacedImageType::ZIM) {
607
608
auto zim = std::make_unique<uint8_t[]>(fileSize);
609
if (!zim) {
610
ERROR_LOG(Log::TexReplacement, "Failed to allocate memory for texture replacement");
611
vfs_->CloseFile(openFile);
612
return LoadLevelResult::LOAD_ERROR;
613
}
614
615
if (vfs_->Read(openFile, &zim[0], fileSize) != fileSize) {
616
ERROR_LOG(Log::TexReplacement, "Could not load texture replacement: %s - failed to read ZIM", filename.c_str());
617
vfs_->CloseFile(openFile);
618
return LoadLevelResult::LOAD_ERROR;
619
}
620
vfs_->CloseFile(openFile);
621
622
int w, h, f;
623
uint8_t *image;
624
std::vector<uint8_t> &out = data_[mipLevel];
625
// TODO: Zim files can actually hold mipmaps (although no tool has ever been made to create them :P)
626
if (LoadZIMPtr(&zim[0], fileSize, &w, &h, &f, &image)) {
627
if (w > level.w || h > level.h) {
628
ERROR_LOG(Log::TexReplacement, "Texture replacement changed since header read: %s", filename.c_str());
629
return LoadLevelResult::LOAD_ERROR;
630
}
631
632
out.resize(level.w * level.h * 4);
633
if (w == level.w) {
634
memcpy(&out[0], image, level.w * 4 * level.h);
635
} else {
636
for (int y = 0; y < h; ++y) {
637
memcpy(&out[level.w * 4 * y], image + w * 4 * y, w * 4);
638
}
639
}
640
free(image);
641
642
CheckAlphaResult res = CheckAlpha32Rect((u32 *)&out[0], level.w, w, h, 0xFF000000);
643
if (res == CHECKALPHA_ANY || mipLevel == 0) {
644
alphaStatus_ = ReplacedTextureAlpha(res);
645
}
646
levels_.push_back(level);
647
} else {
648
good = false;
649
}
650
651
return LoadLevelResult::CONTINUE;
652
653
} else if (imageType == ReplacedImageType::PNG) {
654
png_image png = {};
655
png.version = PNG_IMAGE_VERSION;
656
657
std::string pngdata;
658
pngdata.resize(fileSize);
659
pngdata.resize(vfs_->Read(openFile, &pngdata[0], fileSize));
660
vfs_->CloseFile(openFile);
661
if (!png_image_begin_read_from_memory(&png, &pngdata[0], pngdata.size())) {
662
ERROR_LOG(Log::TexReplacement, "Could not load texture replacement info: %s - %s (zip)", filename.c_str(), png.message);
663
return LoadLevelResult::LOAD_ERROR;
664
}
665
if (png.width > (uint32_t)level.w || png.height > (uint32_t)level.h) {
666
ERROR_LOG(Log::TexReplacement, "Texture replacement changed since header read: %s", filename.c_str());
667
return LoadLevelResult::LOAD_ERROR;
668
}
669
670
bool checkedAlpha = false;
671
if ((png.format & PNG_FORMAT_FLAG_ALPHA) == 0) {
672
// Well, we know for sure it doesn't have alpha.
673
if (mipLevel == 0) {
674
alphaStatus_ = ReplacedTextureAlpha::FULL;
675
}
676
checkedAlpha = true;
677
}
678
png.format = PNG_FORMAT_RGBA;
679
680
std::vector<uint8_t> &out = data_[mipLevel];
681
// TODO: Should probably try to handle out-of-memory gracefully here.
682
out.resize(level.w * level.h * 4);
683
if (!png_image_finish_read(&png, nullptr, &out[0], level.w * 4, nullptr)) {
684
ERROR_LOG(Log::TexReplacement, "Could not load texture replacement: %s - %s", filename.c_str(), png.message);
685
out.resize(0);
686
return LoadLevelResult::LOAD_ERROR;
687
}
688
png_image_free(&png);
689
690
if (!checkedAlpha) {
691
// This will only check the hashed bits.
692
CheckAlphaResult res = CheckAlpha32Rect((u32 *)&out[0], level.w, png.width, png.height, 0xFF000000);
693
if (res == CHECKALPHA_ANY || mipLevel == 0) {
694
alphaStatus_ = ReplacedTextureAlpha(res);
695
}
696
}
697
698
levels_.push_back(level);
699
return LoadLevelResult::CONTINUE;
700
} else {
701
WARN_LOG(Log::TexReplacement, "Don't know how to load this image type! %d", (int)imageType);
702
vfs_->CloseFile(openFile);
703
}
704
return LoadLevelResult::LOAD_ERROR;
705
}
706
707
bool ReplacedTexture::CopyLevelTo(int level, uint8_t *out, size_t outDataSize, int rowPitch) {
708
_assert_msg_((size_t)level < levels_.size(), "Invalid miplevel");
709
_assert_msg_(out != nullptr && rowPitch > 0, "Invalid out/pitch");
710
711
if (State() != ReplacementState::ACTIVE) {
712
WARN_LOG(Log::TexReplacement, "Init not done yet");
713
return false;
714
}
715
716
// We pad the images right here during the copy.
717
// TODO: Add support for the texture cache to scale texture coordinates instead.
718
// It already supports this for render target textures that aren't powers of 2.
719
720
int outW = levels_[level].fullW;
721
int outH = levels_[level].fullH;
722
723
// We probably could avoid this lock, but better to play it safe.
724
std::lock_guard<std::mutex> guard(lock_);
725
726
const ReplacedTextureLevel &info = levels_[level];
727
const std::vector<uint8_t> &data = data_[level];
728
729
if (data.empty()) {
730
WARN_LOG(Log::TexReplacement, "Level %d is empty", level);
731
return false;
732
}
733
734
#define PARALLEL_COPY
735
736
int blockSize;
737
if (!Draw::DataFormatIsBlockCompressed(fmt, &blockSize)) {
738
if (fmt != Draw::DataFormat::R8G8B8A8_UNORM) {
739
ERROR_LOG(Log::TexReplacement, "Unexpected linear data format");
740
return false;
741
}
742
743
if (rowPitch < info.w * 4) {
744
ERROR_LOG(Log::TexReplacement, "Replacement rowPitch=%d, but w=%d (level=%d) (too small)", rowPitch, info.w * 4, level);
745
return false;
746
}
747
748
_assert_msg_(data.size() == info.w * info.h * 4, "Data has wrong size");
749
750
if (rowPitch == info.w * 4) {
751
#ifdef PARALLEL_COPY
752
ParallelMemcpy(&g_threadManager, out, data.data(), info.w * 4 * info.h);
753
#else
754
memcpy(out, data.data(), info.w * 4 * info.h);
755
#endif
756
} else {
757
#ifdef PARALLEL_COPY
758
const int MIN_LINES_PER_THREAD = 4;
759
ParallelRangeLoop(&g_threadManager, [&](int l, int h) {
760
int extraPixels = outW - info.w;
761
for (int y = l; y < h; ++y) {
762
memcpy((uint8_t *)out + rowPitch * y, data.data() + info.w * 4 * y, info.w * 4);
763
// Fill the rest of the line with black.
764
memset((uint8_t *)out + rowPitch * y + info.w * 4, 0, extraPixels * 4);
765
}
766
}, 0, info.h, MIN_LINES_PER_THREAD);
767
#else
768
int extraPixels = outW - info.w;
769
for (int y = 0; y < info.h; ++y) {
770
memcpy((uint8_t *)out + rowPitch * y, data.data() + info.w * 4 * y, info.w * 4);
771
memset((uint8_t *)out + rowPitch * y + info.w * 4, 0, extraPixels * 4);
772
}
773
#endif
774
// Memset the rest of the padding to avoid leaky edge pixels. Guess we could parallelize this too, but meh.
775
for (int y = info.h; y < outH; y++) {
776
uint8_t *dest = (uint8_t *)out + rowPitch * y;
777
memset(dest, 0, outW * 4);
778
}
779
}
780
} else {
781
#ifdef PARALLEL_COPY
782
// Only parallel copy in the simple case for now.
783
if (info.w == outW && info.h == outH) {
784
// TODO: Add sanity checks here for other formats?
785
ParallelMemcpy(&g_threadManager, out, data.data(), data.size());
786
return true;
787
}
788
#endif
789
// Alright, so careful copying of blocks it is, padding with zero-blocks as needed.
790
int inBlocksW = (info.w + 3) / 4;
791
int inBlocksH = (info.h + 3) / 4;
792
int outBlocksW = (info.fullW + 3) / 4;
793
int outBlocksH = (info.fullH + 3) / 4;
794
795
int paddingBlocksX = outBlocksW - inBlocksW;
796
797
// Copy all the known blocks, and zero-fill out the lines.
798
for (int y = 0; y < inBlocksH; y++) {
799
const uint8_t *input = data.data() + y * inBlocksW * blockSize;
800
uint8_t *output = (uint8_t *)out + y * outBlocksW * blockSize;
801
memcpy(output, input, inBlocksW * blockSize);
802
memset(output + inBlocksW * blockSize, 0, paddingBlocksX * blockSize);
803
}
804
805
// Vertical zero-padding.
806
for (int y = inBlocksH; y < outBlocksH; y++) {
807
uint8_t *output = (uint8_t *)out + y * outBlocksW * blockSize;
808
memset(output, 0, outBlocksW * blockSize);
809
}
810
}
811
812
return true;
813
}
814
815
const char *StateString(ReplacementState state) {
816
switch (state) {
817
case ReplacementState::UNLOADED: return "UNLOADED";
818
case ReplacementState::PENDING: return "PENDING";
819
case ReplacementState::NOT_FOUND: return "NOT_FOUND";
820
case ReplacementState::ACTIVE: return "ACTIVE";
821
case ReplacementState::CANCEL_INIT: return "CANCEL_INIT";
822
default: return "N/A";
823
}
824
}
825
826