CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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