-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathcompressonator.cpp
More file actions
745 lines (672 loc) · 29.5 KB
/
compressonator.cpp
File metadata and controls
745 lines (672 loc) · 29.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//===============================================================================
// Copyright (c) 2007-2024 Advanced Micro Devices, Inc. All rights reserved.
// Copyright (c) 2004-2006 ATI Technologies Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// File Name: Compressonator.cpp
// Description: A library to compress/decompress textures
//
// Revisions
// Feb 2016 - Fix Parameter processing & Swizzle issue for DXTC Codecs
// Jan 2016 - Added ASTC support -
// Jan 2014 - Completed support for BC6H and Command line options for new compressonator
// Apr 2014 - Refactored Library
// Code clean to support MSV 2010 and up
//////////////////////////////////////////////////////////////////////////////
#include "compressonator.h" // User shared: Keep private code out of this header
#include <cassert>
#include "atiformats.h"
#include "codec.h"
#include "codec_common.h"
#include "cmp_mips.h"
#include "common.h"
#include "compress.h"
#include "debug.h"
#include "format_conversion.h"
#include "texture_utils.h"
using namespace CMP;
extern CMP_ERROR GetError(CodecError err);
#ifdef _LOCAL_DEBUG
char DbgTracer::buff[MAX_DBGBUFF_SIZE];
char DbgTracer::PrintBuff[MAX_DBGPPRINTBUFF_SIZE];
#endif
// This call is used to swizzle source data content.
// Use example: CMP_Map_Bytes(pData, dwWidth, dwHeight, { 2, 1, 0, 3 },4);
void CMP_Map_Bytes(BYTE* src, int width, int height, CMP_MAP_BYTES_SET map, CMP_BYTE offset)
{
int i, j;
BYTE b[4];
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
if (offset == 4)
{
b[0] = *src;
b[1] = *(src + 1);
b[2] = *(src + 2);
b[3] = *(src + 3);
*(src) = b[map.B0];
*(src + 1) = b[map.B1];
*(src + 2) = b[map.B2];
*(src + 3) = b[map.B3];
src = src + 4; // move to next set of bytes
}
else if (offset == 3)
{
b[0] = *src;
b[1] = *(src + 1);
b[2] = *(src + 2);
b[3] = *(src + 3);
*(src) = b[map.B0];
*(src + 1) = b[map.B1];
*(src + 2) = b[map.B2];
src = src + 3; // move to next set of bytes
}
}
}
}
#ifndef USE_OLD_SWIZZLE
// For now this function will only handle a single case
// where the source data remains the same size and only RGBA channels
// are swizzled according output compressed formats,
// if source is compressed then no change is performed
static void CMP_PrepareSourceForCMP_Destination(CMP_Texture* pTexture, CMP_FORMAT DestFormat)
{
CMP_DWORD dwWidth = pTexture->dwWidth;
CMP_DWORD dwHeight = pTexture->dwHeight;
CMP_FORMAT newSrcFormat = pTexture->format;
CMP_BYTE* pData;
pData = pTexture->pData;
switch (newSrcFormat)
{
case CMP_FORMAT_BGRA_8888: {
switch (DestFormat)
{
case CMP_FORMAT_ATI1N:
case CMP_FORMAT_ATI2N:
case CMP_FORMAT_ATI2N_XY:
case CMP_FORMAT_ATI2N_DXT5:
case CMP_FORMAT_ATC_RGB:
case CMP_FORMAT_ATC_RGBA_Explicit:
case CMP_FORMAT_ATC_RGBA_Interpolated:
case CMP_FORMAT_BC1:
case CMP_FORMAT_BC2:
case CMP_FORMAT_BC3:
case CMP_FORMAT_BC4:
case CMP_FORMAT_BC4_S:
case CMP_FORMAT_BC5:
case CMP_FORMAT_BC5_S:
case CMP_FORMAT_DXT1:
case CMP_FORMAT_DXT3:
case CMP_FORMAT_DXT5:
case CMP_FORMAT_DXT5_xGBR:
case CMP_FORMAT_DXT5_RxBG:
case CMP_FORMAT_DXT5_RBxG:
case CMP_FORMAT_DXT5_xRBG:
case CMP_FORMAT_DXT5_RGxB:
case CMP_FORMAT_DXT5_xGxR: {
// The source format is correct for these codecs
break;
}
#if (OPTION_BUILD_ASTC == 1)
case CMP_FORMAT_ASTC:
#endif
case CMP_FORMAT_BC6H:
case CMP_FORMAT_BC7:
case CMP_FORMAT_GT:
case CMP_FORMAT_ETC_RGB:
case CMP_FORMAT_ETC2_RGB:
case CMP_FORMAT_ETC2_SRGB:
case CMP_FORMAT_ETC2_RGBA:
case CMP_FORMAT_ETC2_RGBA1:
case CMP_FORMAT_ETC2_SRGBA:
case CMP_FORMAT_ETC2_SRGBA1: {
newSrcFormat = CMP_FORMAT_RGBA_8888;
CMP_Map_Bytes(pData, dwWidth, dwHeight, {2, 1, 0, 3}, 4);
break;
}
default:
break;
}
break;
}
case CMP_FORMAT_RGBA_8888: {
switch (DestFormat)
{
case CMP_FORMAT_ATI1N:
case CMP_FORMAT_ATI2N:
case CMP_FORMAT_ATI2N_XY:
case CMP_FORMAT_ATI2N_DXT5:
case CMP_FORMAT_ATC_RGB:
case CMP_FORMAT_ATC_RGBA_Explicit:
case CMP_FORMAT_ATC_RGBA_Interpolated:
case CMP_FORMAT_BC1:
case CMP_FORMAT_BC2:
case CMP_FORMAT_BC3:
case CMP_FORMAT_BC4:
case CMP_FORMAT_BC4_S:
case CMP_FORMAT_BC5:
case CMP_FORMAT_BC5_S:
case CMP_FORMAT_DXT1:
case CMP_FORMAT_DXT3:
case CMP_FORMAT_DXT5:
case CMP_FORMAT_DXT5_xGBR:
case CMP_FORMAT_DXT5_RxBG:
case CMP_FORMAT_DXT5_RBxG:
case CMP_FORMAT_DXT5_xRBG:
case CMP_FORMAT_DXT5_RGxB:
case CMP_FORMAT_DXT5_xGxR: {
newSrcFormat = CMP_FORMAT_BGRA_8888;
CMP_Map_Bytes(pData, dwWidth, dwHeight, {2, 1, 0, 3}, 4);
break;
}
#if (OPTION_BUILD_ASTC == 1)
case CMP_FORMAT_ASTC:
#endif
case CMP_FORMAT_BC6H:
case CMP_FORMAT_BC7:
case CMP_FORMAT_ETC_RGB:
case CMP_FORMAT_ETC2_RGB:
case CMP_FORMAT_ETC2_SRGB:
case CMP_FORMAT_ETC2_RGBA:
case CMP_FORMAT_ETC2_RGBA1:
case CMP_FORMAT_ETC2_SRGBA:
case CMP_FORMAT_ETC2_SRGBA1:
case CMP_FORMAT_GT: {
// format is correct
}
default:
break;
}
break;
}
case CMP_FORMAT_ARGB_8888: {
switch (DestFormat)
{
case CMP_FORMAT_ATI1N:
case CMP_FORMAT_ATI2N:
case CMP_FORMAT_ATI2N_XY:
case CMP_FORMAT_ATI2N_DXT5:
case CMP_FORMAT_ATC_RGB:
case CMP_FORMAT_ATC_RGBA_Explicit:
case CMP_FORMAT_ATC_RGBA_Interpolated:
case CMP_FORMAT_BC1:
case CMP_FORMAT_BC2:
case CMP_FORMAT_BC3:
case CMP_FORMAT_BC4:
case CMP_FORMAT_BC5:
case CMP_FORMAT_DXT1:
case CMP_FORMAT_DXT3:
case CMP_FORMAT_DXT5:
case CMP_FORMAT_DXT5_xGBR:
case CMP_FORMAT_DXT5_RxBG:
case CMP_FORMAT_DXT5_RBxG:
case CMP_FORMAT_DXT5_xRBG:
case CMP_FORMAT_DXT5_RGxB:
case CMP_FORMAT_DXT5_xGxR: {
newSrcFormat = CMP_FORMAT_BGRA_8888;
CMP_Map_Bytes(pData, dwWidth, dwHeight, {3, 2, 1, 0}, 4);
break;
}
#if (OPTION_BUILD_ASTC == 1)
case CMP_FORMAT_ASTC:
#endif
case CMP_FORMAT_BC6H:
case CMP_FORMAT_BC7:
case CMP_FORMAT_ETC_RGB:
case CMP_FORMAT_ETC2_RGB:
case CMP_FORMAT_ETC2_SRGB:
case CMP_FORMAT_ETC2_RGBA:
case CMP_FORMAT_ETC2_RGBA1:
case CMP_FORMAT_ETC2_SRGBA:
case CMP_FORMAT_ETC2_SRGBA1:
case CMP_FORMAT_GT: {
newSrcFormat = CMP_FORMAT_RGBA_8888;
CMP_Map_Bytes(pData, dwWidth, dwHeight, {1, 2, 3, 0}, 4);
break;
}
default:
break;
}
break;
}
default:
break;
}
// Update Source format to new one
pTexture->format = newSrcFormat;
}
#endif
CMP_ERROR CMP_API CMP_ConvertTexture(CMP_Texture* pSourceTexture,
CMP_Texture* pDestTexture,
const CMP_CompressOptions* pOptions,
CMP_Feedback_Proc pFeedbackProc)
{
#ifdef USE_DBGTRACE
DbgTrace(("-------> pSourceTexture [%x] pDestTexture [%x] pOptions [%x]", pSourceTexture, pDestTexture, pOptions));
#endif
CMP_ERROR tc_err = CheckTexture(pSourceTexture, true);
if (tc_err != CMP_OK)
return tc_err;
// make a local copy of the texture to avoid modifying the user's data
CMP_Texture srcTextureCopy = *pSourceTexture;
#ifdef ENABLE_MAKE_COMPATIBLE_API
FloatParams floatParams(pOptions);
ConvertedBuffer compatibleBuffer = CreateCompatibleBuffer(pDestTexture->format, &srcTextureCopy, &floatParams);
srcTextureCopy.format = compatibleBuffer.format;
srcTextureCopy.pData = (CMP_BYTE*)compatibleBuffer.data;
srcTextureCopy.dwDataSize = compatibleBuffer.dataSize;
#endif
tc_err = CheckTexture(pDestTexture, false);
if (tc_err != CMP_OK)
return tc_err;
if (srcTextureCopy.dwWidth != pDestTexture->dwWidth || srcTextureCopy.dwHeight != pDestTexture->dwHeight)
return CMP_ERR_SIZE_MISMATCH;
CodecType srcType = GetCodecType(srcTextureCopy.format);
assert(srcType != CT_Unknown);
if (srcType == CT_Unknown)
return CMP_ERR_UNSUPPORTED_SOURCE_FORMAT;
CodecType destType = GetCodecType(pDestTexture->format);
assert(destType != CT_Unknown);
if (destType == CT_Unknown)
return CMP_ERR_UNSUPPORTED_SOURCE_FORMAT;
// Figure out the type of processing we are doing
bool compressing = srcType == CT_None && destType != CT_None;
bool decompressing = srcType != CT_None && destType == CT_None;
if (srcType == destType) // Easy case
{
if (srcTextureCopy.format == pDestTexture->format && srcTextureCopy.dwPitch == pDestTexture->dwPitch)
memcpy(pDestTexture->pData, srcTextureCopy.pData, CMP_CalculateBufferSize(&srcTextureCopy));
else
{
CodecBufferType srcBufferType = GetCodecBufferType(srcTextureCopy.format);
CodecBufferType destBufferType = GetCodecBufferType(pDestTexture->format);
CCodecBuffer* pSrcBuffer = CreateCodecBuffer(srcBufferType,
srcTextureCopy.nBlockWidth,
srcTextureCopy.nBlockHeight,
srcTextureCopy.nBlockDepth,
srcTextureCopy.dwWidth,
srcTextureCopy.dwHeight,
srcTextureCopy.dwPitch,
srcTextureCopy.pData,
srcTextureCopy.dwDataSize);
assert(pSrcBuffer);
if (!pSrcBuffer)
return CMP_ERR_GENERIC;
CCodecBuffer* pDestBuffer = CreateCodecBuffer(destBufferType,
pDestTexture->nBlockWidth,
pDestTexture->nBlockHeight,
pDestTexture->nBlockDepth,
pDestTexture->dwWidth,
pDestTexture->dwHeight,
pDestTexture->dwPitch,
pDestTexture->pData,
pDestTexture->dwDataSize);
assert(pDestBuffer);
if (!pDestBuffer)
{
delete pSrcBuffer;
return CMP_ERR_GENERIC;
}
DISABLE_FP_EXCEPTIONS;
pDestBuffer->Copy(*pSrcBuffer);
RESTORE_FP_EXCEPTIONS;
delete pSrcBuffer;
delete pDestBuffer;
}
return CMP_OK;
}
else if (compressing && !decompressing) // Compression
{
#ifndef USE_OLD_SWIZZLE
CMP_PrepareSourceForCMP_Destination(&srcTextureCopy, pDestTexture->format);
#endif
CMP_BOOL bMultithread = true;
if (!pOptions->bDisableMultiThreading && (pOptions->dwnumThreads == 1))
bMultithread = false;
#ifdef THREADED_COMPRESS
// Note:
// BC7/BC6H has issues with this setting - we already set multithreading via numThreads so
// this call is disabled for BC7/BC6H ASTC Codecs.
// if the user has set DisableMultiThreading then numThreads will be set to 1 (regardless of its original value)
if (((!pOptions || !pOptions->bDisableMultiThreading) && CMP_GetNumberOfProcessors() > 1) && (bMultithread) &&
#if (OPTION_BUILD_ASTC == 1)
(destType != CT_ASTC) &&
#endif
(destType != CT_BC7) && (destType != CT_BC6H) && (destType != CT_BC6H_SF)
#ifdef USE_APC
&& (destType != CT_APC)
#endif
#ifdef USE_GTC
&& (destType != CT_GTC)
#endif
#ifdef USE_LOSSLESS_COMPRESSION
&& (destType != CT_BRLG)
#endif
#ifdef USE_BASIS
&& (destType != CT_BASIS)
#endif
)
{
return CodecCompressTextureThreaded(&srcTextureCopy, pDestTexture, pOptions, pFeedbackProc);
}
else
#endif // THREADED_COMPRESS
{
return CodecCompressTexture(&srcTextureCopy, pDestTexture, pOptions, pFeedbackProc);
}
}
else if (!compressing && decompressing) // Decompression
{
return CodecDecompressTexture(&srcTextureCopy, pDestTexture, pOptions, pFeedbackProc);
}
else // Decompressing & then compressing
{
CCodec* pCodecIn = CreateCodec(srcType);
CCodec* pCodecOut = CreateCodec(destType);
assert(pCodecIn);
assert(pCodecOut);
if (pCodecIn == NULL || pCodecOut == NULL)
{
SAFE_DELETE(pCodecIn);
SAFE_DELETE(pCodecOut);
return CMP_ERR_UNABLE_TO_INIT_CODEC;
}
CCodecBuffer* pSrcBuffer = pCodecIn->CreateBuffer(srcTextureCopy.nBlockWidth,
srcTextureCopy.nBlockHeight,
srcTextureCopy.nBlockDepth,
srcTextureCopy.dwWidth,
srcTextureCopy.dwHeight,
srcTextureCopy.dwPitch,
srcTextureCopy.pData,
srcTextureCopy.dwDataSize);
CCodecBuffer* pTempBuffer = CreateCodecBuffer(
CBT_RGBA32F, pDestTexture->nBlockWidth, pDestTexture->nBlockHeight, pDestTexture->nBlockDepth, pDestTexture->dwWidth, pDestTexture->dwHeight);
CCodecBuffer* pDestBuffer = pCodecOut->CreateBuffer(pDestTexture->nBlockWidth,
pDestTexture->nBlockHeight,
pDestTexture->nBlockDepth,
pDestTexture->dwWidth,
pDestTexture->dwHeight,
pDestTexture->dwPitch,
pDestTexture->pData,
pDestTexture->dwDataSize);
assert(pSrcBuffer);
assert(pTempBuffer);
assert(pDestBuffer);
if (pSrcBuffer == NULL || pTempBuffer == NULL || pDestBuffer == NULL)
{
SAFE_DELETE(pCodecIn);
SAFE_DELETE(pCodecOut);
SAFE_DELETE(pSrcBuffer);
SAFE_DELETE(pTempBuffer);
SAFE_DELETE(pDestBuffer);
return CMP_ERR_GENERIC;
}
DISABLE_FP_EXCEPTIONS;
CodecError err2 = pCodecIn->Decompress(*pSrcBuffer, *pTempBuffer, pFeedbackProc);
if (err2 == CE_OK)
{
err2 = pCodecOut->Compress(*pTempBuffer, *pDestBuffer, pFeedbackProc);
}
RESTORE_FP_EXCEPTIONS;
SAFE_DELETE(pCodecIn);
SAFE_DELETE(pCodecOut);
SAFE_DELETE(pSrcBuffer);
SAFE_DELETE(pTempBuffer);
SAFE_DELETE(pDestBuffer);
return GetError(err2);
}
}
CMP_ERROR CMP_API CMP_ConvertMipTexture(CMP_MipSet* p_MipSetIn, CMP_MipSet* p_MipSetOut, const CMP_CompressOptions* pOptions, CMP_Feedback_Proc pFeedbackProc)
{
assert(p_MipSetIn);
assert(p_MipSetOut);
assert(pOptions);
CMP_CMIPS CMips;
// --------------------------------
// Setup Compressed Mip Set Target
// --------------------------------
//if (GetCodecType(pOptions->DestFormat) == CT_Unknown) return CMP_ERR_UNKNOWN_DESTINATION_FORMAT;
// -------------
// Output
// -------------
memset(p_MipSetOut, 0, sizeof(CMP_MipSet));
p_MipSetOut->m_Flags = MS_FLAG_Default;
p_MipSetOut->m_format = pOptions->DestFormat;
p_MipSetOut->m_nHeight = p_MipSetIn->m_nHeight;
p_MipSetOut->m_nWidth = p_MipSetIn->m_nWidth;
CMP_Format2FourCC(pOptions->DestFormat, p_MipSetOut);
// Default compression block size if not set!
p_MipSetIn->m_nBlockWidth = (p_MipSetIn->m_nBlockWidth == 0) ? 4 : p_MipSetIn->m_nBlockWidth;
p_MipSetIn->m_nBlockHeight = (p_MipSetIn->m_nBlockHeight == 0) ? 4 : p_MipSetIn->m_nBlockHeight;
p_MipSetIn->m_nDepth = (p_MipSetIn->m_nDepth < 1) ? 1 : p_MipSetIn->m_nDepth;
// Allocate compression data
p_MipSetOut->m_nMipLevels = 1; // this is overwritten depending on input.
p_MipSetOut->m_ChannelFormat = CF_Compressed;
p_MipSetOut->m_nMaxMipLevels = p_MipSetIn->m_nMaxMipLevels;
p_MipSetOut->m_nBlockWidth = p_MipSetIn->m_nBlockWidth;
p_MipSetOut->m_nBlockHeight = p_MipSetIn->m_nBlockHeight;
p_MipSetOut->m_nDepth = p_MipSetIn->m_nDepth;
p_MipSetOut->m_TextureType = p_MipSetIn->m_TextureType;
if (pOptions->DestFormat == CMP_FORMAT_BROTLIG)
p_MipSetOut->m_transcodeFormat = p_MipSetIn->m_format;
p_MipSetOut->m_nIterations = 0; // tracks number of processed data miplevels
//=====================================================
// Case Uncompressed Source to Compressed Destination
//=====================================================
CMP_Texture srcTexture = {};
srcTexture.dwSize = sizeof(srcTexture);
srcTexture.pMipSet = p_MipSetIn;
#ifdef USE_BASIS
if (pOptions->DestFormat == CMP_FORMAT_BASIS)
{
p_MipSetOut->m_format = CMP_FORMAT_BASIS;
p_MipSetOut->m_transcodeFormat = CMP_FORMAT_BC1;
p_MipSetOut->m_TextureType = TT_2D;
p_MipSetOut->m_TextureDataType = TDT_ARGB;
p_MipSetOut->m_ChannelFormat = CF_Compressed;
p_MipSetOut->dwDataSize = 0; // will be set after compression
p_MipSetOut->dwWidth = 1; // Container for 1 linear data buffer
p_MipSetOut->dwHeight = 1; // Container for 1 linear data buffer
p_MipSetOut->m_nDepth = 1;
if (!CMips.AllocateMipSet(p_MipSetOut, p_MipSetOut->m_ChannelFormat, TDT_ARGB, p_MipSetOut->m_TextureType, 1, 1, 1))
{
return CMP_ERR_MEM_ALLOC_FOR_MIPSET;
}
//=====================
// Uncompressed source
//======================
CMP_MipLevel* srcMipLevel = CMips.GetMipLevel(p_MipSetIn, 0, 0);
srcTexture.dwPitch = 0;
srcTexture.nBlockWidth = p_MipSetIn->m_nBlockWidth;
srcTexture.nBlockHeight = p_MipSetIn->m_nBlockHeight;
srcTexture.nBlockDepth = p_MipSetIn->m_nBlockDepth;
srcTexture.format = p_MipSetIn->m_format;
srcTexture.dwWidth = p_MipSetIn->m_nWidth;
srcTexture.dwHeight = p_MipSetIn->m_nHeight;
srcTexture.transcodeFormat = p_MipSetIn->m_transcodeFormat;
srcTexture.pData = srcMipLevel->m_pbData;
srcTexture.dwWidth = srcMipLevel->m_nWidth;
srcTexture.dwHeight = srcMipLevel->m_nHeight;
srcTexture.pData = srcMipLevel->m_pbData;
srcTexture.dwDataSize = CMP_CalculateBufferSize(&srcTexture);
//========================
// Compressed Destination
//========================
CMP_Texture destTexture;
MipLevel* pOutMipLevel = CMips.GetMipLevel(p_MipSetOut, 0, 0);
pOutMipLevel->m_pvec8Data = new CMP_VEC8(); // size = 0 before compression, will be set during processing
p_MipSetOut->pData = pOutMipLevel->m_pbData;
destTexture.dwSize = sizeof(destTexture);
destTexture.dwWidth = p_MipSetIn->m_nWidth;
destTexture.dwHeight = p_MipSetIn->m_nHeight;
destTexture.dwPitch = 0;
destTexture.nBlockWidth = p_MipSetIn->m_nBlockWidth;
destTexture.nBlockHeight = p_MipSetIn->m_nBlockHeight;
destTexture.format = pOptions->DestFormat;
srcTexture.transcodeFormat = p_MipSetIn->m_transcodeFormat;
destTexture.dwDataSize = CMP_CalculateBufferSize(&srcTexture); // This will be reset after compression
destTexture.pData = pOutMipLevel->m_pbData;
//========================
// Process ConvertTexture
//========================
CMP_ERROR cmp_status = CMP_ConvertTexture(&srcTexture, &destTexture, pOptions, pFeedbackProc);
if (cmp_status != CMP_OK)
{
return cmp_status;
}
}
else
#endif
{
if (!CMips.AllocateMipSet(p_MipSetOut,
p_MipSetOut->m_ChannelFormat,
TDT_ARGB,
p_MipSetOut->m_TextureType,
p_MipSetIn->m_nWidth,
p_MipSetIn->m_nHeight,
p_MipSetOut->m_nDepth))
{
return CMP_ERR_MEM_ALLOC_FOR_MIPSET;
}
CMP_INT srcNumMipmapLevels = p_MipSetIn->m_nMipLevels;
// It is possible that the source texture will have an "m_nMipLevels" value greater than 1 when compressing to Brotli-G,
// but it will not contain any data, so we always want to compress only the one mipmap level.
// We do this instead of altering the value because it is still important for us to be able to know how many mipmap levels
// the source texture has when compressing to Brotli-G
if (pOptions->DestFormat == CMP_FORMAT_BROTLIG)
{
srcNumMipmapLevels = 1;
}
p_MipSetOut->m_nMipLevels = p_MipSetIn->m_nMipLevels;
for (int nMipLevel = 0; nMipLevel < srcNumMipmapLevels; nMipLevel++)
{
if (pOptions->m_PrintInfoStr && srcNumMipmapLevels > 1)
{
char buff[256];
snprintf(buff, sizeof(buff), "Processing miplevel %d for texture...\n", nMipLevel);
pOptions->m_PrintInfoStr(buff);
}
for (int nFaceOrSlice = 0; nFaceOrSlice < CMP_MaxFacesOrSlices(p_MipSetIn, nMipLevel); nFaceOrSlice++)
{
CMP_DWORD sourceDataSize = 0;
//=====================
// Uncompressed source
//======================
CMP_MipLevel* srcMipLevel = CMips.GetMipLevel(p_MipSetIn, nMipLevel, nFaceOrSlice);
srcTexture.dwPitch = 0;
srcTexture.nBlockWidth = p_MipSetIn->m_nBlockWidth;
srcTexture.nBlockHeight = p_MipSetIn->m_nBlockHeight;
srcTexture.nBlockDepth = p_MipSetIn->m_nBlockDepth;
srcTexture.format = p_MipSetIn->m_format;
srcTexture.transcodeFormat = p_MipSetIn->m_transcodeFormat;
srcTexture.dwWidth = srcMipLevel->m_nWidth;
srcTexture.dwHeight = srcMipLevel->m_nHeight;
srcTexture.pData = srcMipLevel->m_pbData;
srcTexture.dwDataSize = CMP_CalculateBufferSize(&srcTexture);
// Temporary settings
p_MipSetIn->dwWidth = srcTexture.dwWidth;
p_MipSetIn->dwHeight = srcTexture.dwHeight;
p_MipSetIn->pData = srcTexture.pData;
p_MipSetIn->dwDataSize = srcTexture.dwDataSize;
//========================
// Compressed Destination
//========================
CMP_Texture destTexture = {};
destTexture.dwSize = sizeof(destTexture);
destTexture.dwWidth = srcMipLevel->m_nWidth;
destTexture.dwHeight = srcMipLevel->m_nHeight;
destTexture.dwPitch = 0;
destTexture.nBlockWidth = p_MipSetIn->m_nBlockWidth;
destTexture.nBlockHeight = p_MipSetIn->m_nBlockHeight;
destTexture.format = pOptions->DestFormat;
destTexture.transcodeFormat = p_MipSetOut->m_transcodeFormat;
destTexture.dwDataSize = CMP_CalculateBufferSize(&destTexture);
p_MipSetOut->m_format = destTexture.format;
p_MipSetOut->dwDataSize = destTexture.dwDataSize;
p_MipSetOut->dwWidth = destTexture.dwWidth;
p_MipSetOut->dwHeight = destTexture.dwHeight;
//--------------------------------------
// Allocate MipSet for Block Compressors
//--------------------------------------
CMP_MipLevel* pOutMipLevel = CMips.GetMipLevel(p_MipSetOut, nMipLevel, nFaceOrSlice);
if (!CMips.AllocateCompressedMipLevelData(pOutMipLevel, destTexture.dwWidth, destTexture.dwHeight, destTexture.dwDataSize))
{
return CMP_ERR_MEM_ALLOC_FOR_MIPSET;
}
destTexture.pData = pOutMipLevel->m_pbData;
p_MipSetOut->pData = pOutMipLevel->m_pbData;
//==========================
// Print info about input
//==========================
// NOTE: This is duplicated in CMP_ConvertMipTextureCGP
if (pOptions->m_PrintInfoStr)
{
char buff[256];
if ((p_MipSetOut->m_format == CMP_FORMAT_BROTLIG) || (p_MipSetOut->m_format == CMP_FORMAT_BINARY))
snprintf(buff, sizeof(buff), "Source data size = %d Bytes\n", srcTexture.dwDataSize);
else
snprintf(buff,
sizeof(buff),
"Source data size = %d Bytes, width = %d px height = %d px\n",
srcTexture.dwDataSize,
srcTexture.dwWidth,
srcTexture.dwHeight);
pOptions->m_PrintInfoStr(buff);
}
// this is needed to preserve the correct initial source size because CMP_ConvertTexture might
// edit the srcTexture and change its format into one better suited for processing
sourceDataSize = srcTexture.dwDataSize;
//========================
// Process ConvertTexture
//========================
CMP_ERROR cmp_status = CMP_ConvertTexture(&srcTexture, &destTexture, pOptions, pFeedbackProc);
if (cmp_status != CMP_OK)
{
return cmp_status;
}
else
p_MipSetOut->m_nIterations++;
if (p_MipSetOut->m_format == CMP_FORMAT_BROTLIG)
{
p_MipSetOut->dwDataSize = destTexture.dwDataSize;
}
//==========================
// Print info about output
//==========================
// NOTE: This is mostly duplicated in CMP_ConvertMipTextureCGP
if (pOptions->m_PrintInfoStr && (destTexture.dwDataSize > 0) && (p_MipSetOut->m_format != CMP_FORMAT_BINARY))
{
char buff[256];
snprintf(buff,
sizeof(buff),
"\rDestination data size = %d Bytes Resulting compression ratio = %2.2f:1\n",
destTexture.dwDataSize,
sourceDataSize / (float)destTexture.dwDataSize);
pOptions->m_PrintInfoStr(buff);
}
}
}
}
//if (pFeedbackProc)
// pFeedbackProc(100, NULL, NULL);
return CMP_OK;
}