whiteoutlib 0.1.4

Read and write Blizzard game assets from Rust: models (MDX, M2, M3), textures (BLP, DDS, PNG, JPEG, BMP, TGA, TIFF, GIF) and archives (CASC, MPQ).
Documentation
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow

#include <whiteout/textures/texture.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstring>
#include <limits>

#include "bcn.h"
#include "mipmap/generator.h"
#include "mipmap/mip_convert.h"
#include "mipmap/stages.h"
#include "utils/pixel_convert.h"
#include "utils/srgb_linearize.h"

namespace whiteout::textures {

u32 bytesPerBlock(PixelFormat fmt) {
    switch (fmt) {
    case PixelFormat::R8:
        return 1;
    case PixelFormat::R16:
        return 2;
    case PixelFormat::R32F:
        return 4;
    case PixelFormat::RG8:
        return 2;
    case PixelFormat::RG16:
        return 4;
    case PixelFormat::RG32F:
        return 8;
    case PixelFormat::RGBA8:
        return 4;
    case PixelFormat::RGBA16:
        return 8;
    case PixelFormat::RGBA32F:
        return 16;
    case PixelFormat::BC1:
    case PixelFormat::BC4:
        return 8;
    case PixelFormat::BC2:
    case PixelFormat::BC3:
    case PixelFormat::BC5:
    case PixelFormat::BC6H:
    case PixelFormat::BC7:
        return 16;
    }
    return 0;
}

u32 blockEdge(PixelFormat fmt) {
    switch (fmt) {
    case PixelFormat::R8:
    case PixelFormat::R16:
    case PixelFormat::R32F:
    case PixelFormat::RG8:
    case PixelFormat::RG16:
    case PixelFormat::RG32F:
    case PixelFormat::RGBA8:
    case PixelFormat::RGBA16:
    case PixelFormat::RGBA32F:
        return 1;
    default:
        return 4;
    }
}

u64 computeImageSize(PixelFormat fmt, u32 width, u32 height) {
    const u32 edge = blockEdge(fmt);
    const u32 bpb = bytesPerBlock(fmt);

    const u32 blocks_x = (width + edge - 1) / edge;
    const u32 blocks_y = (height + edge - 1) / edge;

    return static_cast<u64>(blocks_x) * blocks_y * bpb;
}

struct Texture::Impl {
    TextureType type = TextureType::Texture2D;
    PixelFormat format = PixelFormat::RGBA8;
    TextureKind kind = TextureKind::Other;
    // Per-channel kinds — only meaningful when kind == Multikind.
    std::array<TextureKind, 4> channelKinds = {TextureKind::Other, TextureKind::Other,
                                               TextureKind::Other, TextureKind::Other};
    // Per-channel default fill values; 1.0f by convention for all channels.
    std::array<f32, 4> channelDefaults = {1.0f, 1.0f, 1.0f, 1.0f};
    bool srgb = false;

    u32 width = 0;
    u32 height = 0;
    u32 depth = 1;
    // Number of array slices (cube-maps for TextureCubeArray,
    // 2D images for Texture2DArray). 1 for non-array types.
    u32 arraySize = 1;

    std::vector<MipLevel> mips;

    std::vector<u8> data;

    u32 layerCount() const {
        switch (type) {
        case TextureType::TextureCube:
            return 6u;
        case TextureType::Texture2DArray:
            return arraySize;
        case TextureType::TextureCubeArray:
            return 6u * arraySize;
        default:
            return 1u;
        }
    }

    u32 mipCount() const {
        const u32 layers = layerCount();
        return layers == 0 ? 0u : static_cast<u32>(mips.size()) / layers;
    }
};

u32 computeMaxMipCount(u32 w, u32 h, u32 d) {
    u32 dim = std::max({w, h, d});
    u32 count = 1;
    while (dim > 1) {
        dim >>= 1;
        ++count;
    }
    return count;
}

namespace {

u64 build_mip_chain(PixelFormat fmt, u32 w, u32 h, u32 d, u32 mipCount, u32 layers,
                    std::vector<MipLevel>& out) {
    out.clear();
    out.reserve(static_cast<size_t>(layers) * mipCount);

    u64 offset = 0;
    for (u32 layer = 0; layer < layers; ++layer) {
        u32 mw = w, mh = h, md = d;
        for (u32 mip = 0; mip < mipCount; ++mip) {
            const u64 slice_size = computeImageSize(fmt, mw, mh);
            const u64 total = slice_size * md;

            out.push_back(MipLevel{
                .width = mw,
                .height = mh,
                .depth = md,
                .offset = offset,
                .size = total,
            });

            offset += total;

            mw = std::max(mw >> 1, 1u);
            mh = std::max(mh >> 1, 1u);
            md = std::max(md >> 1, 1u);
        }
    }
    return offset;
}

} // anonymous namespace

Texture::Texture() : impl_(std::make_unique<Impl>()) {}
Texture::~Texture() = default;

Texture::Texture(const Texture& other) : impl_(std::make_unique<Impl>(*other.impl_)) {}

Texture& Texture::operator=(const Texture& other) {
    if (this != &other) {
        impl_ = std::make_unique<Impl>(*other.impl_);
    }
    return *this;
}

Texture::Texture(Texture&& other) noexcept = default;
Texture& Texture::operator=(Texture&& other) noexcept = default;

Texture Texture::create2D(PixelFormat fmt, u32 width, u32 height, u32 mipCount) {
    assert(width > 0 && height > 0);

    if (mipCount == 0) {
        mipCount = computeMaxMipCount(width, height, 1);
    }

    Texture tex;
    tex.impl_->type = TextureType::Texture2D;
    tex.impl_->format = fmt;
    tex.impl_->width = width;
    tex.impl_->height = height;
    tex.impl_->depth = 1;

    const u64 total = build_mip_chain(fmt, width, height, 1, mipCount, 1, tex.impl_->mips);
    tex.impl_->data.resize(static_cast<size_t>(total), 0);
    return tex;
}

Texture Texture::create3D(PixelFormat fmt, u32 width, u32 height, u32 depth, u32 mipCount) {
    assert(width > 0 && height > 0 && depth > 0);

    if (mipCount == 0) {
        mipCount = computeMaxMipCount(width, height, depth);
    }

    Texture tex;
    tex.impl_->type = TextureType::Texture3D;
    tex.impl_->format = fmt;
    tex.impl_->width = width;
    tex.impl_->height = height;
    tex.impl_->depth = depth;

    const u64 total = build_mip_chain(fmt, width, height, depth, mipCount, 1, tex.impl_->mips);
    tex.impl_->data.resize(static_cast<size_t>(total), 0);
    return tex;
}

Texture Texture::createCube(PixelFormat fmt, u32 size, u32 mipCount) {
    assert(size > 0);

    if (mipCount == 0) {
        mipCount = computeMaxMipCount(size, size, 1);
    }

    Texture tex;
    tex.impl_->type = TextureType::TextureCube;
    tex.impl_->format = fmt;
    tex.impl_->width = size;
    tex.impl_->height = size;
    tex.impl_->depth = 1;

    const u64 total = build_mip_chain(fmt, size, size, 1, mipCount, 6, tex.impl_->mips);
    tex.impl_->data.resize(static_cast<size_t>(total), 0);
    return tex;
}

Texture Texture::create2DArray(PixelFormat fmt, u32 width, u32 height, u32 arraySize,
                               u32 mipCount) {
    assert(width > 0 && height > 0 && arraySize > 0);

    if (mipCount == 0) {
        mipCount = computeMaxMipCount(width, height, 1);
    }

    Texture tex;
    tex.impl_->type = TextureType::Texture2DArray;
    tex.impl_->format = fmt;
    tex.impl_->width = width;
    tex.impl_->height = height;
    tex.impl_->depth = 1;
    tex.impl_->arraySize = arraySize;

    const u64 total = build_mip_chain(fmt, width, height, 1, mipCount, arraySize, tex.impl_->mips);
    tex.impl_->data.resize(static_cast<size_t>(total), 0);
    return tex;
}

Texture Texture::createCubeArray(PixelFormat fmt, u32 size, u32 arraySize, u32 mipCount) {
    assert(size > 0 && arraySize > 0);

    if (mipCount == 0) {
        mipCount = computeMaxMipCount(size, size, 1);
    }

    Texture tex;
    tex.impl_->type = TextureType::TextureCubeArray;
    tex.impl_->format = fmt;
    tex.impl_->width = size;
    tex.impl_->height = size;
    tex.impl_->depth = 1;
    tex.impl_->arraySize = arraySize;

    const u32 totalLayers = 6u * arraySize;
    const u64 total = build_mip_chain(fmt, size, size, 1, mipCount, totalLayers, tex.impl_->mips);
    tex.impl_->data.resize(static_cast<size_t>(total), 0);
    return tex;
}

TextureType Texture::type() const {
    return impl_->type;
}
PixelFormat Texture::format() const {
    return impl_->format;
}
TextureKind Texture::kind() const {
    return impl_->kind;
}
void Texture::setKind(TextureKind k) {
    if (k == TextureKind::ORM) {
        impl_->kind = TextureKind::Multikind;
        impl_->channelKinds[0] = TextureKind::AmbientOcclusion;
        impl_->channelKinds[1] = TextureKind::Roughness;
        impl_->channelKinds[2] = TextureKind::Metalness;
        impl_->channelKinds[3] = TextureKind::Unused;
        return;
    }
    impl_->kind = k;
}
TextureKind Texture::channelKind(Channel ch) const {
    const u32 idx = static_cast<u32>(ch);
    return (idx < 4) ? impl_->channelKinds[idx] : TextureKind::Other;
}
void Texture::setChannelKind(Channel ch, TextureKind kind) {
    const u32 idx = static_cast<u32>(ch);
    if (idx < 4)
        impl_->channelKinds[idx] = kind;
}
f32 Texture::channelDefault(Channel ch) const {
    const u32 idx = static_cast<u32>(ch);
    return (idx < 4) ? impl_->channelDefaults[idx] : 1.0f;
}
void Texture::setChannelDefault(Channel ch, f32 value) {
    const u32 idx = static_cast<u32>(ch);
    if (idx < 4)
        impl_->channelDefaults[idx] = value;
}
bool Texture::isSrgb() const {
    return impl_->srgb;
}
void Texture::setSrgb(bool srgb) {
    impl_->srgb = srgb;
}

u32 Texture::width() const {
    return impl_->width;
}
u32 Texture::height() const {
    return impl_->height;
}
u32 Texture::depth() const {
    return impl_->depth;
}

u32 Texture::layerCount() const {
    return impl_->layerCount();
}

u32 Texture::arraySize() const {
    return impl_->arraySize;
}

u32 Texture::mipCount() const {
    return impl_->mipCount();
}

const MipLevel& Texture::mipLevel(u32 mip, u32 layer) const {
    return impl_->mips[layer * mipCount() + mip];
}

u64 Texture::dataSize() const {
    return impl_->data.size();
}

std::span<const u8> Texture::data() const {
    return {impl_->data.data(), impl_->data.size()};
}

std::span<u8> Texture::data() {
    return {impl_->data.data(), impl_->data.size()};
}

const u8* Texture::dataPtr() const {
    return impl_->data.data();
}

u8* Texture::dataPtr() {
    return impl_->data.data();
}

std::span<const u8> Texture::mipData(u32 mip, u32 layer) const {
    const auto& m = mipLevel(mip, layer);
    return {impl_->data.data() + m.offset, static_cast<size_t>(m.size)};
}

std::span<u8> Texture::mipData(u32 mip, u32 layer) {
    const auto& m = mipLevel(mip, layer);
    return {impl_->data.data() + m.offset, static_cast<size_t>(m.size)};
}

std::vector<u8> Texture::takeData() {
    impl_->mips.clear();
    impl_->width = impl_->height = impl_->depth = 0;
    return std::move(impl_->data);
}

void Texture::setData(std::vector<u8> new_data) {
    assert(new_data.size() == impl_->data.size());
    impl_->data = std::move(new_data);
}

namespace {

// Runtime format-info tables — auto-derived from FormatTraits.
template <PixelFormat... Fmts>
constexpr std::array<u32, sizeof...(Fmts)> make_channels_table(FormatList<Fmts...>) {
    return {{FormatTraits<Fmts>::channels...}};
}

template <PixelFormat... Fmts>
constexpr std::array<u32, sizeof...(Fmts)> make_bpc_table(FormatList<Fmts...>) {
    return {{(FormatTraits<Fmts>::bytes_per_pixel / FormatTraits<Fmts>::channels)...}};
}

constexpr auto kChannelCounts = make_channels_table(UncompressedFormats{});
constexpr auto kBytesPerChannel = make_bpc_table(UncompressedFormats{});

u32 format_channel_count(PixelFormat fmt) {
    const u32 idx = static_cast<u32>(fmt);
    return (idx < kUncompressedCount) ? kChannelCounts[idx] : 0u;
}

u32 format_bytes_per_channel(PixelFormat fmt) {
    const u32 idx = static_cast<u32>(fmt);
    return (idx < kUncompressedCount) ? kBytesPerChannel[idx] : 0u;
}

PixelFormat single_channel_format(PixelFormat fmt) {
    switch (format_bytes_per_channel(fmt)) {
    case 1:
        return PixelFormat::R8;
    case 2:
        return PixelFormat::R16;
    case 4:
        return PixelFormat::R32F;
    default:
        return fmt;
    }
}

PixelFormat rgba_format_for(PixelFormat fmt) {
    switch (format_bytes_per_channel(fmt)) {
    case 1:
        return PixelFormat::RGBA8;
    case 2:
        return PixelFormat::RGBA16;
    case 4:
        return PixelFormat::RGBA32F;
    default:
        return fmt;
    }
}

bool is_rg_normal_format(PixelFormat format) {
    return format == PixelFormat::RG8 || format == PixelFormat::RG16 ||
           format == PixelFormat::RG32F;
}

bool is_rgba_normal_format(PixelFormat format) {
    return format == PixelFormat::RGBA8 || format == PixelFormat::RGBA16 ||
           format == PixelFormat::RGBA32F;
}

Texture make_texture_like(const Texture& src, PixelFormat new_fmt) {
    switch (src.type()) {
    case TextureType::Texture2D:
        return Texture::create2D(new_fmt, src.width(), src.height(), src.mipCount());
    case TextureType::Texture3D:
        return Texture::create3D(new_fmt, src.width(), src.height(), src.depth(), src.mipCount());
    case TextureType::TextureCube:
        return Texture::createCube(new_fmt, src.width(), src.mipCount());
    case TextureType::Texture2DArray:
        return Texture::create2DArray(new_fmt, src.width(), src.height(), src.arraySize(),
                                      src.mipCount());
    case TextureType::TextureCubeArray:
        return Texture::createCubeArray(new_fmt, src.width(), src.arraySize(), src.mipCount());
    }
    return {};
}

void copy_texture_metadata(const Texture& src, Texture& dst) {
    dst.setKind(src.kind());
    dst.setSrgb(src.isSrgb());
    if (src.kind() == TextureKind::Multikind) {
        for (u32 i = 0; i < 4; ++i) {
            const auto ch = static_cast<Channel>(i);
            dst.setChannelKind(ch, src.channelKind(ch));
        }
    }
}

// ============================================================================
// Uncompressed format conversion
// ============================================================================

/// Convert an image between any two uncompressed formats.
/// Uses direct channel conversion (no float intermediate) when possible.
/// Both textures must already have the same dimensions and mip structure.
Texture convert_uncompressed(const Texture& src, PixelFormat new_fmt) {
    assert(!bcn::isCompressed(src.format()));
    assert(!bcn::isCompressed(new_fmt));

    const PixelFormat src_fmt = src.format();
    if (src_fmt == new_fmt)
        return src;

    const u32 layers = src.layerCount();
    const u32 mips = src.mipCount();

    // Build destination texture with the same shape but the new format.
    Texture dst = make_texture_like(src, new_fmt);
    copy_texture_metadata(src, dst);

    // Direct bulk conversion — avoids per-pixel float intermediate.
    auto fn = get_converter(src_fmt, new_fmt);
    assert(fn && "unsupported format pair");

    for (u32 layer = 0; layer < layers; ++layer) {
        for (u32 mip = 0; mip < mips; ++mip) {
            const auto src_span = src.mipData(mip, layer);
            auto dst_span = dst.mipData(mip, layer);

            const u32 w = src.mipLevel(mip, layer).width;
            const u32 h = src.mipLevel(mip, layer).height;
            const u32 d = src.mipLevel(mip, layer).depth;

            fn(src_span.data(), dst_span.data(), w * h * d);
        }
    }
    return dst;
}

[[maybe_unused]] std::optional<Texture> copy_normal_to_rgba8(const Texture& src,
                                                             PixelFormat orig_fmt) {
    const bool is_rg = is_rg_normal_format(src.format());
    const bool is_rgba = is_rgba_normal_format(src.format());
    if (!is_rg && !is_rgba)
        return std::nullopt;

    size_t x = 0, y = 1;
    bool flip_y = false;
    // BC3N
    if (is_rgba && orig_fmt == PixelFormat::BC3) {
        y = 1;
        x = 3;
        flip_y = true;
    }

    Texture dst = make_texture_like(src, PixelFormat::RGBA8);
    copy_texture_metadata(src, dst);

    auto to_f32 = get_to_rgba32f(src.format());
    auto from_f32 = get_from_rgba32f(PixelFormat::RGBA8);
    if (!to_f32 || !from_f32)
        return std::nullopt;

    const u32 src_bpp = bytesPerBlock(src.format());
    const u32 layers = src.layerCount();
    const u32 mips = src.mipCount();

    for (u32 layer = 0; layer < layers; ++layer) {
        for (u32 mip = 0; mip < mips; ++mip) {
            const auto src_span = src.mipData(mip, layer);
            auto dst_span = dst.mipData(mip, layer);

            const u32 width = src.mipLevel(mip, layer).width;
            const u32 height = src.mipLevel(mip, layer).height;
            const u32 depth = src.mipLevel(mip, layer).depth;
            const u32 pixel_count = width * height * depth;

            const u8* src_bytes = src_span.data();
            u8* dst_bytes = dst_span.data();

            for (u32 pixel = 0; pixel < pixel_count; ++pixel) {
                f32 rgba[4];
                to_f32(src_bytes + pixel * src_bpp, rgba);

                const f32 x_value = rgba[x];
                const f32 y_value = rgba[y];
                const f32 normal_x = x_value * 2.0f - 1.0f;
                const f32 normal_y = y_value * 2.0f - 1.0f;
                const f32 normal_z =
                    std::sqrt(std::max(0.0f, 1.0f - normal_x * normal_x - normal_y * normal_y));
                rgba[0] = x_value;
                rgba[1] = (flip_y ? 1.0f - y_value : y_value);
                rgba[2] = (normal_z + 1.0f) * 0.5f;
                rgba[3] = 1.0f;
                from_f32(rgba, dst_bytes + pixel * bytesPerBlock(PixelFormat::RGBA8));
            }
        }
    }

    return dst;
}

// ============================================================================
// Channel operation dispatch structs
// ============================================================================

struct SwapChannelsOp {
    template <PixelFormat Fmt>
    static void apply(Texture::Impl& impl, u32 ai, u32 bi, bool& ok) {
        using T = typename FormatTraits<Fmt>::channel_type;
        constexpr u32 ch = FormatTraits<Fmt>::channels;
        constexpr u32 bpp = FormatTraits<Fmt>::bytes_per_pixel;

        if (ai >= ch || bi >= ch) {
            ok = false;
            return;
        }
        if (ai == bi) {
            ok = true;
            return;
        }

        const u32 layers = impl.layerCount();
        const u32 mips = impl.mipCount();

        for (u32 layer = 0; layer < layers; ++layer) {
            for (u32 mip = 0; mip < mips; ++mip) {
                const auto& lvl = impl.mips[layer * mips + mip];
                const u32 pixel_count = lvl.width * lvl.height * lvl.depth;
                u8* pixels = impl.data.data() + lvl.offset;

                for (u32 px = 0; px < pixel_count; ++px) {
                    u8* pixel = pixels + px * bpp;
                    T va, vb;
                    std::memcpy(&va, pixel + ai * sizeof(T), sizeof(T));
                    std::memcpy(&vb, pixel + bi * sizeof(T), sizeof(T));
                    std::memcpy(pixel + ai * sizeof(T), &vb, sizeof(T));
                    std::memcpy(pixel + bi * sizeof(T), &va, sizeof(T));
                }
            }
        }
        ok = true;
    }
};

struct InvertChannelOp {
    template <PixelFormat Fmt>
    static void apply(Texture::Impl& impl, u32 ci, bool& ok) {
        using T = typename FormatTraits<Fmt>::channel_type;
        constexpr u32 ch = FormatTraits<Fmt>::channels;
        constexpr u32 bpp = FormatTraits<Fmt>::bytes_per_pixel;

        if (ci >= ch) {
            ok = false;
            return;
        }

        const u32 layers = impl.layerCount();
        const u32 mips = impl.mipCount();

        for (u32 layer = 0; layer < layers; ++layer) {
            for (u32 mip = 0; mip < mips; ++mip) {
                const auto& lvl = impl.mips[layer * mips + mip];
                const u32 pixel_count = lvl.width * lvl.height * lvl.depth;
                u8* pixels = impl.data.data() + lvl.offset;

                for (u32 px = 0; px < pixel_count; ++px) {
                    u8* ch_ptr = pixels + px * bpp + ci * sizeof(T);
                    T val;
                    std::memcpy(&val, ch_ptr, sizeof(T));
                    if constexpr (std::is_same_v<T, u8>)
                        val = static_cast<u8>(255u - val);
                    else if constexpr (std::is_same_v<T, u16>)
                        val = static_cast<u16>(65535u - val);
                    else
                        val = 1.0f - val;
                    std::memcpy(ch_ptr, &val, sizeof(T));
                }
            }
        }
        ok = true;
    }
};

struct ExpandNormalOp {
    template <PixelFormat Fmt>
    static void apply(Texture::Impl& impl, u32 ai, u32 bi, u32 ci, bool& ok) {
        using T = typename FormatTraits<Fmt>::channel_type;
        constexpr u32 ch = FormatTraits<Fmt>::channels;
        constexpr u32 bpp = FormatTraits<Fmt>::bytes_per_pixel;

        if (ai >= ch || bi >= ch || ci >= ch) {
            ok = false;
            return;
        }

        // UNORM decode/encode constants folded at compile time.
        // For integer types the stored range is [0, numeric_limits::max];
        // for f32 it is the normalised [0, 1] interval, so max is 1.
        constexpr f32 kTypeMax =
            std::is_same_v<T, f32> ? 1.0f : static_cast<f32>(std::numeric_limits<T>::max());
        constexpr f32 kDecodeScale = 2.0f / kTypeMax; // maps [0, max] → [-1, 1]
        constexpr f32 kHalfMax = kTypeMax * 0.5f;     // maps [-1, 1] → [0, max]

        const u32 x_off = ai * sizeof(T);
        const u32 y_off = bi * sizeof(T);
        const u32 z_off = ci * sizeof(T);

        const u32 layers = impl.layerCount();
        const u32 mips = impl.mipCount();

        // Three float arrays at this chunk size fit comfortably in L1 cache.
        constexpr u32 kChunk = 1024;
        f32 xs[kChunk], ys[kChunk], zs[kChunk];

        for (u32 layer = 0; layer < layers; ++layer) {
            for (u32 mip = 0; mip < mips; ++mip) {
                const auto& lvl = impl.mips[layer * mips + mip];
                const u32 pixel_count = lvl.width * lvl.height * lvl.depth;
                u8* pixels = impl.data.data() + lvl.offset;

                for (u32 base = 0; base < pixel_count; base += kChunk) {
                    const u32 n = std::min(pixel_count - base, kChunk);
                    u8* const row = pixels + static_cast<size_t>(base) * bpp;

                    // Phase 1 – gather: deinterleave X and Y into contiguous
                    // float arrays with UNORM → [-1, 1] decoding applied.
                    for (u32 i = 0; i < n; ++i) {
                        T va, vb;
                        std::memcpy(&va, row + i * bpp + x_off, sizeof(T));
                        std::memcpy(&vb, row + i * bpp + y_off, sizeof(T));
                        xs[i] = static_cast<f32>(va) * kDecodeScale - 1.0f;
                        ys[i] = static_cast<f32>(vb) * kDecodeScale - 1.0f;
                    }

                    // Phase 2 – compute: reconstruct Z for every pixel.
                    // Tight loop over contiguous float arrays → auto-vectorizable
                    // (SIMD sqrt + FMA).
                    for (u32 i = 0; i < n; ++i)
                        zs[i] = std::sqrt(std::max(0.0f, 1.0f - xs[i] * xs[i] - ys[i] * ys[i]));

                    // Phase 3 – scatter: encode Z and write back to channel ci
                    // in the interleaved pixel buffer.
                    for (u32 i = 0; i < n; ++i) {
                        // (z + 1) * kHalfMax maps [-1, 1] → [0, max].
                        // Integer types require rounding to the nearest
                        // representable value; f32 stores the exact result.
                        const f32 encoded = (zs[i] + 1.0f) * kHalfMax;
                        T vc;
                        if constexpr (std::is_same_v<T, f32>)
                            vc = encoded;
                        else
                            vc = static_cast<T>(std::round(encoded));
                        std::memcpy(row + i * bpp + z_off, &vc, sizeof(T));
                    }
                }
            }
        }
        ok = true;
    }
};

struct FillChannelOp {
    template <PixelFormat Fmt>
    static void apply(Texture::Impl& impl, u32 ci, f32 value, bool& ok) {
        using T = typename FormatTraits<Fmt>::channel_type;
        constexpr u32 ch = FormatTraits<Fmt>::channels;
        constexpr u32 bpp = FormatTraits<Fmt>::bytes_per_pixel;

        if (ci >= ch) {
            ok = false;
            return;
        }

        const T encoded = convert_channel<T, f32>(value);

        const u32 layers = impl.layerCount();
        const u32 mips = impl.mipCount();

        for (u32 layer = 0; layer < layers; ++layer) {
            for (u32 mip = 0; mip < mips; ++mip) {
                const auto& lvl = impl.mips[layer * mips + mip];
                const u32 pixel_count = lvl.width * lvl.height * lvl.depth;
                u8* pixels = impl.data.data() + lvl.offset;

                for (u32 px = 0; px < pixel_count; ++px)
                    std::memcpy(pixels + px * bpp + ci * sizeof(T), &encoded, sizeof(T));
            }
        }
        ok = true;
    }
};

} // anonymous namespace

// ============================================================================
// format() and copyAsFormat()
// ============================================================================

/// Convert this texture in-place to @p new_fmt.
///
/// Conversion path:
///   1. Same format → no-op.
///   2. BCn source  → decode to native format (R8/RG8/RGBA8/RGBA32F),
///      then proceed.
///   3. Uncompressed → uncompressed → pixel-level conversion.
///   4. Uncompressed → BCn → bcn::encode (with an intermediate pixel-level
///      conversion if the decoded format doesn't match what the encoder needs).
void Texture::format(PixelFormat new_fmt) {
    *this = copyAsFormat(new_fmt);
}

Texture Texture::copyAsFormat(PixelFormat new_fmt, interfaces::WorkerPool* pool) const {
    // 1. Same format — just copy.
    if (impl_->format == new_fmt)
        return *this;

    // 2. If source is compressed, decode it first.
    if (bcn::isCompressed(impl_->format)) {
        std::string err;
        auto decoded = bcn::decode(*this, &err, pool);
        if (!decoded)
            return Texture{}; // empty texture on codec failure (was: throw)
        return decoded->copyAsFormat(new_fmt, pool);
    }

    // Source is now guaranteed to be uncompressed.

    // 3. Target is also uncompressed.
    if (!bcn::isCompressed(new_fmt))
        return convert_uncompressed(*this, new_fmt);

    // 4. Target is BCn — encode.
    //    bcn::encode requires R8 for BC4, RG8 for BC5, RGBA32F for BC6H,
    //    RGBA8 for all others.
    PixelFormat needed;
    if (new_fmt == PixelFormat::BC4)
        needed = PixelFormat::R8;
    else if (new_fmt == PixelFormat::BC5)
        needed = PixelFormat::RG8;
    else if (new_fmt == PixelFormat::BC6H)
        needed = PixelFormat::RGBA32F;
    else
        needed = PixelFormat::RGBA8;

    // Convert to the needed intermediate format if necessary.
    const Texture* src_ptr = this;
    Texture intermediate;
    if (impl_->format != needed) {
        intermediate = convert_uncompressed(*this, needed);
        src_ptr = &intermediate;
    }

    std::string err;
    auto encoded = bcn::encode(*src_ptr, new_fmt, &err, pool);
    if (!encoded)
        return Texture{}; // empty texture on codec failure (was: throw)
    return std::move(*encoded);
}

std::optional<Texture> Texture::copyFromNormalToRGBA(interfaces::WorkerPool* pool) const {
    if (kind() != TextureKind::Normal)
        return std::nullopt;

    if (bcn::isCompressed(impl_->format)) {
        std::string err;
        auto decoded = bcn::decode(*this, &err, pool);
        if (!decoded)
            return std::nullopt;

        decoded->setKind(kind());
        // BC5 decodes to RG8, BC4 to R8 — ensure RGBA8 for channel operations.
        if (decoded->format() != PixelFormat::RGBA8)
            *decoded = decoded->copyAsFormat(PixelFormat::RGBA8, pool);
        ensureColorSpace(*decoded, false);
        if (impl_->format == PixelFormat::BC3) {
            decoded->invertChannel(Channel::G);
            decoded->swapChannels(Channel::R, Channel::A);
        }
        decoded->expandNormal(Channel::R, Channel::G, Channel::B);
        decoded->fillChannel(Channel::A, 1.0f);
        return decoded;
    }

    auto dst = copyAsFormat(PixelFormat::RGBA8, pool);
    dst.setKind(TextureKind::Normal);
    ensureColorSpace(dst, false);
    dst.expandNormal(Channel::R, Channel::G, Channel::B);
    dst.fillChannel(Channel::A, 1.0f);
    return dst;
}

std::optional<std::vector<Texture>> Texture::splitChannels(
    const std::vector<Channel>& channels) const {
    const u32 srcChCount = format_channel_count(impl_->format);
    if (srcChCount == 0) // BCn
        return std::nullopt;

    for (auto ch : channels) {
        if (static_cast<u32>(ch) >= srcChCount)
            return std::nullopt;
    }

    const PixelFormat singleFmt = single_channel_format(impl_->format);
    const u32 bytesPerCh = format_bytes_per_channel(impl_->format);
    const u32 srcBpp = bytesPerBlock(impl_->format);
    const u32 layers = layerCount();
    const u32 mips = mipCount();

    std::vector<Texture> result;
    result.reserve(channels.size());

    for (auto ch : channels) {
        const u32 ci = static_cast<u32>(ch);
        Texture dst = make_texture_like(*this, singleFmt);
        dst.setSrgb(isSrgb());

        for (u32 layer = 0; layer < layers; ++layer) {
            for (u32 mip = 0; mip < mips; ++mip) {
                const auto srcSpan = mipData(mip, layer);
                auto dstSpan = dst.mipData(mip, layer);
                const auto& lvl = mipLevel(mip, layer);
                const u32 n = lvl.width * lvl.height * lvl.depth;

                const u8* s = srcSpan.data();
                u8* d = dstSpan.data();

                for (u32 px = 0; px < n; ++px)
                    std::memcpy(d + static_cast<size_t>(px) * bytesPerCh,
                                s + static_cast<size_t>(px) * srcBpp + ci * bytesPerCh, bytesPerCh);
            }
        }

        result.push_back(std::move(dst));
    }

    return result;
}

std::optional<Texture> Texture::mergeChannels(const std::vector<Texture>& sources,
                                              const std::vector<Channel>& targetChannels) {
    if (sources.empty() || sources.size() != targetChannels.size())
        return std::nullopt;

    const Texture& ref = sources[0];
    const u32 srcChCount = format_channel_count(ref.format());
    if (srcChCount != 1)
        return std::nullopt;

    const u32 bytesPerCh = format_bytes_per_channel(ref.format());
    if (bytesPerCh == 0)
        return std::nullopt;

    // Validate all sources match.
    for (size_t i = 1; i < sources.size(); ++i) {
        const Texture& s = sources[i];
        if (s.format() != ref.format() || s.type() != ref.type() || s.width() != ref.width() ||
            s.height() != ref.height() || s.depth() != ref.depth() ||
            s.mipCount() != ref.mipCount())
            return std::nullopt;
    }

    const PixelFormat dstFmt = rgba_format_for(ref.format());
    const u32 dstBpp = bytesPerBlock(dstFmt);
    Texture dst = make_texture_like(ref, dstFmt);

    const u32 layers = ref.layerCount();
    const u32 mips = ref.mipCount();

    for (size_t i = 0; i < sources.size(); ++i) {
        const u32 ci = static_cast<u32>(targetChannels[i]);
        if (ci >= 4)
            return std::nullopt;

        for (u32 layer = 0; layer < layers; ++layer) {
            for (u32 mip = 0; mip < mips; ++mip) {
                const auto srcSpan = sources[i].mipData(mip, layer);
                auto dstSpan = dst.mipData(mip, layer);
                const auto& lvl = ref.mipLevel(mip, layer);
                const u32 n = lvl.width * lvl.height * lvl.depth;

                const u8* s = srcSpan.data();
                u8* d = dstSpan.data();

                for (u32 px = 0; px < n; ++px)
                    std::memcpy(d + static_cast<size_t>(px) * dstBpp + ci * bytesPerCh,
                                s + static_cast<size_t>(px) * bytesPerCh, bytesPerCh);
            }
        }
    }

    return dst;
}

std::optional<std::string> Texture::generateMipmaps(interfaces::WorkerPool* pool) {
    return generateMipmaps(kKeepMipCount, pool);
}

std::optional<std::string> Texture::generateMipmaps(u32 newMipCount, interfaces::WorkerPool* pool) {
    // Validate and apply the requested mip count.
    const u32 maxMips = computeMaxMipCount(impl_->width, impl_->height, impl_->depth);

    if (newMipCount != kKeepMipCount) {
        if (newMipCount > maxMips)
            return std::string("newMipCount exceeds maximum (" + std::to_string(maxMips) + ")");

        if (newMipCount != mipCount()) {
            // Rebuild the mip chain with the new count, preserving mip 0 data.
            const u32 layers = impl_->layerCount();
            std::vector<MipLevel> newMips;
            const u64 total = build_mip_chain(impl_->format, impl_->width, impl_->height,
                                              impl_->depth, newMipCount, layers, newMips);

            std::vector<u8> newData(static_cast<size_t>(total), 0);

            // Copy mip 0 from each layer.
            for (u32 layer = 0; layer < layers; ++layer) {
                const auto& oldMip0 = impl_->mips[layer * mipCount()];
                const auto& newMip0 = newMips[layer * newMipCount];
                const size_t copySize = std::min<size_t>(oldMip0.size, newMip0.size);
                std::memcpy(newData.data() + newMip0.offset, impl_->data.data() + oldMip0.offset,
                            copySize);
            }

            impl_->mips = std::move(newMips);
            impl_->data = std::move(newData);
        }

        // Single mip level means no generation work needed.
        if (newMipCount == 1)
            return std::nullopt;
    }

    const bool isMultikind = (impl_->kind == TextureKind::Multikind);

    if (!isMultikind)
        return mipmap::generateMipmaps(*this, pool);

    // Multikind: split into per-channel textures, generate
    // mipmaps independently with kind-appropriate pipelines, then recombine.
    const u32 srcChCount = format_channel_count(impl_->format);
    if (srcChCount == 0)
        return std::string("Multikind mipmap generation requires an uncompressed format");

    // Build the per-channel kind array.
    std::array<TextureKind, 4> chKinds = impl_->channelKinds;

    // Determine which channels to process.
    const u32 splitCount = srcChCount;
    std::vector<Channel> channels;
    std::vector<u32> indices_map;
    channels.reserve(splitCount);
    for (u32 i = 0; i < splitCount; ++i) {
        if (chKinds[i] == TextureKind::Unused)
            continue;
        channels.push_back(static_cast<Channel>(i));
        indices_map.push_back(i);
    }

    auto split = splitChannels(channels);
    if (!split)
        return std::string("Multikind splitChannels failed");

    auto& channelTextures = *split;
    for (size_t i = 0; i < channelTextures.size(); ++i) {
        // Map Unused → Other so pipelineForKind uses a plain box filter.
        const TextureKind ck = chKinds[indices_map[i]];
        channelTextures[i].setKind(ck);
    }

    for (auto& chTex : channelTextures) {
        auto err = mipmap::generateMipmaps(chTex, pool);
        if (err)
            return err;
    }

    // Merge filtered channels back into this texture (mips 1+).
    auto merged = Texture::mergeChannels(channelTextures, channels);
    if (!merged)
        return std::string("Multikind mergeChannels failed");

    // Multikind: fill with default
    for (size_t i = 0; i < splitCount; ++i) {
        if (chKinds[i] == TextureKind::Unused) {
            Channel const ch = static_cast<Channel>(i);
            merged->fillChannel(ch, channelDefault(ch));
        }
    }

    // mergeChannels produces rgba_format_for(singleFmt) which may differ from
    // the original format (e.g. RG8 splits to R8, merges back to RGBA8).
    // Convert back to the original format so data layout matches impl_->mips.
    if (merged->format() != impl_->format) {
        *merged = convert_uncompressed(*merged, impl_->format);
    }

    std::swap(impl_->data, merged->impl_->data);

    return std::nullopt;
}

std::optional<std::string> Texture::downscale(u32 levels, interfaces::WorkerPool* pool) {
    if (levels == 0)
        return std::nullopt;

    const u32 maxMips = computeMaxMipCount(impl_->width, impl_->height, impl_->depth);
    if (levels >= maxMips)
        return std::string("cannot downscale by " + std::to_string(levels) + " levels (max " +
                           std::to_string(maxMips - 1) + ")");

    const u32 currentMips = mipCount();
    const u32 targetMips = std::min(currentMips + levels, maxMips);

    // Regenerate mipmaps with extra levels.
    auto err = generateMipmaps(targetMips, pool);
    if (err)
        return err;

    // Drop the first `levels` mips: promote mip[levels] to the new base.
    const u32 layers = impl_->layerCount();
    const u32 oldMipCount = mipCount();
    const u32 newMipCount = oldMipCount - levels;

    const auto& newBase = impl_->mips[levels];
    const u32 newWidth = newBase.width;
    const u32 newHeight = newBase.height;
    const u32 newDepth = newBase.depth;

    std::vector<MipLevel> newMips;
    const u64 newTotal =
        build_mip_chain(impl_->format, newWidth, newHeight, newDepth, newMipCount, layers, newMips);

    std::vector<u8> newData(static_cast<size_t>(newTotal), 0);

    for (u32 layer = 0; layer < layers; ++layer) {
        for (u32 mip = 0; mip < newMipCount; ++mip) {
            const auto& src = impl_->mips[layer * oldMipCount + mip + levels];
            const auto& dst = newMips[layer * newMipCount + mip];
            const size_t copySize = std::min<size_t>(src.size, dst.size);
            std::memcpy(newData.data() + dst.offset, impl_->data.data() + src.offset, copySize);
        }
    }

    impl_->width = newWidth;
    impl_->height = newHeight;
    impl_->depth = newDepth;
    impl_->mips = std::move(newMips);
    impl_->data = std::move(newData);

    return std::nullopt;
}

bool Texture::swapChannels(Channel a, Channel b) {
    if (bcn::isCompressed(impl_->format))
        return false;

    u32 ai = static_cast<u32>(a);
    u32 bi = static_cast<u32>(b);
    bool ok = false;
    dispatch_uncompressed<SwapChannelsOp>(impl_->format, *impl_, ai, bi, ok);
    return ok;
}

bool Texture::invertChannel(Channel ch) {
    if (bcn::isCompressed(impl_->format))
        return false;

    u32 ci = static_cast<u32>(ch);
    bool ok = false;
    dispatch_uncompressed<InvertChannelOp>(impl_->format, *impl_, ci, ok);
    return ok;
}

bool Texture::fillChannel(Channel target, f32 value) {
    if (bcn::isCompressed(impl_->format))
        return false;

    u32 ci = static_cast<u32>(target);
    bool ok = false;
    dispatch_uncompressed<FillChannelOp>(impl_->format, *impl_, ci, value, ok);
    return ok;
}

bool Texture::expandNormal(Channel xChannel, Channel yChannel, Channel zChannel) {
    if (bcn::isCompressed(impl_->format))
        return false;

    u32 ai = static_cast<u32>(xChannel);
    u32 bi = static_cast<u32>(yChannel);
    u32 ci = static_cast<u32>(zChannel);
    bool ok = false;
    dispatch_uncompressed<ExpandNormalOp>(impl_->format, *impl_, ai, bi, ci, ok);
    return ok;
}

} // namespace whiteout::textures