-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiHelper.cpp
More file actions
2269 lines (2062 loc) · 63.2 KB
/
Copy pathwiHelper.cpp
File metadata and controls
2269 lines (2062 loc) · 63.2 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
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
#include "wiHelper.h"
#include "wiPlatform.h"
#include "wiBacklog.h"
#include "wiEventHandler.h"
#include "wiMath.h"
#include "wiImage.h"
#include "wiRenderer.h"
#include "Utility/lodepng.h"
#include "Utility/dds.h"
#include "Utility/stb_image_write.h"
#include "Utility/zstd/zstd.h"
#include "Utility/win32ico.h"
#if !defined(__SCE__) && !defined(PLATFORM_XBOX)
#include "Utility/portable-file-dialogs.h"
#endif // !defined(__SCE__) && !defined(PLATFORM_XBOX)
#include <thread>
#include <locale>
#include <chrono>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <filesystem>
#include <vector>
#include <iostream>
#include <cstdlib>
#if defined(_WIN32)
#include <direct.h>
#include <Psapi.h> // GetProcessMemoryInfo
#include <Commdlg.h> // openfile
#include <WinBase.h>
#endif // _WIN32
#if defined(__FREEBSD__)
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#pragma comment(lib,"kvm")
#elif defined(PLATFORM_LINUX)
#include <sys/sysinfo.h>
#endif // PLATFORM_LINUX
#ifdef PLATFORM_WINDOWS_DESKTOP
#include <comdef.h> // com_error
#endif // PLATFORM_WINDOWS_DESKTOP
#ifdef __APPLE__
#include <mach/mach.h>
#include <sys/sysctl.h>
#endif // __APPLE__
namespace wi::helper
{
std::string toUpper(const std::string& s)
{
std::string result;
std::locale loc;
for (size_t i = 0; i < s.length(); ++i)
{
result += std::toupper(s.at(i), loc);
}
return result;
}
std::string toLower(const std::string& s)
{
std::string result;
std::locale loc;
for (size_t i = 0; i < s.length(); ++i)
{
result += std::tolower(s.at(i), loc);
}
return result;
}
std::wstring toUpper(const std::wstring& s)
{
std::wstring result;
std::locale loc;
for (size_t i = 0; i < s.length(); ++i)
{
result += std::toupper(s.at(i), loc);
}
return result;
}
std::wstring toLower(const std::wstring& s)
{
std::wstring result;
std::locale loc;
for (size_t i = 0; i < s.length(); ++i)
{
result += std::tolower(s.at(i), loc);
}
return result;
}
void messageBox(const std::string& msg, const std::string& caption)
{
#if defined(PLATFORM_WINDOWS_DESKTOP)
std::wstring wmsg;
std::wstring wcaption;
StringConvert(msg, wmsg);
StringConvert(caption, wcaption);
MessageBox(GetActiveWindow(), wmsg.c_str(), wcaption.c_str(), 0);
#elif defined(__APPLE__)
wi::apple::MessageBox(caption.c_str(), msg.c_str());
#elif defined(SDL2)
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, caption.c_str(), msg.c_str(), NULL);
#endif
}
MessageBoxResult messageBoxCustom(const std::string& msg, const std::string& caption, const std::string& buttons)
{
#if defined(PLATFORM_WINDOWS_DESKTOP)
std::wstring wmsg;
std::wstring wcaption;
StringConvert(msg, wmsg);
StringConvert(caption, wcaption);
UINT type = MB_ICONQUESTION;
if (buttons == "YesNoCancel")
{
type |= MB_YESNOCANCEL;
}
else if (buttons == "YesNo")
{
type |= MB_YESNO;
}
else if (buttons == "OKCancel")
{
type |= MB_OKCANCEL;
}
else if (buttons == "AbortRetryIgnore")
{
type |= MB_ABORTRETRYIGNORE;
}
else // default to OK
{
type |= MB_OK;
}
const int result = MessageBox(GetActiveWindow(), wmsg.c_str(), wcaption.c_str(), type);
switch (result)
{
case IDOK: return MessageBoxResult::OK;
case IDCANCEL: return MessageBoxResult::Cancel;
case IDYES: return MessageBoxResult::Yes;
case IDNO: return MessageBoxResult::No;
case IDABORT: return MessageBoxResult::Abort;
case IDRETRY: return MessageBoxResult::Retry;
case IDIGNORE: return MessageBoxResult::Ignore;
default: return MessageBoxResult::Cancel;
}
#elif defined(__APPLE__)
return (MessageBoxResult)wi::apple::MessageBox(caption.c_str(), msg.c_str(), buttons.c_str());
#elif defined(SDL2)
const SDL_MessageBoxButtonData buttons_data[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Yes" },
{ 0, 1, "No" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" },
};
const SDL_MessageBoxData messageboxdata = {
SDL_MESSAGEBOX_INFORMATION,
NULL,
caption.c_str(),
msg.c_str(),
SDL_arraysize(buttons_data),
buttons_data,
NULL
};
int buttonid;
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0)
{
return MessageBoxResult::Cancel;
}
switch (buttonid)
{
case 0: return MessageBoxResult::Yes;
case 1: return MessageBoxResult::No;
case 2: return MessageBoxResult::Cancel;
default: return MessageBoxResult::Cancel;
}
#endif
return MessageBoxResult::Cancel;
}
std::string screenshot(const wi::graphics::SwapChain& swapchain, const std::string& name)
{
return screenshot(wi::graphics::GetDevice()->GetBackBuffer(&swapchain));
}
std::string screenshot(const wi::graphics::Texture& texture, const std::string& name)
{
std::string directory;
if (name.empty())
{
directory = std::filesystem::current_path().string() + "/screenshots";
}
else
{
directory = GetDirectoryFromPath(name);
}
if (!directory.empty()) //PE: Crash if only filename is used with no folder.
DirectoryCreate(directory);
std::string filename = name;
if (filename.empty())
{
filename = directory + "/sc_" + getCurrentDateTimeAsString() + ".png";
}
bool result = saveTextureToFile(texture, filename);
assert(result);
if (result)
{
return filename;
}
return "";
}
bool saveTextureToMemory(const wi::graphics::Texture& texture, wi::vector<uint8_t>& texturedata)
{
using namespace wi::graphics;
GraphicsDevice* device = wi::graphics::GetDevice();
TextureDesc desc = texture.GetDesc();
Texture stagingTex;
TextureDesc staging_desc = desc;
staging_desc.usage = Usage::READBACK;
staging_desc.layout = ResourceState::COPY_DST;
staging_desc.bind_flags = BindFlag::NONE;
staging_desc.misc_flags = ResourceMiscFlag::NONE;
bool success = device->CreateTexture(&staging_desc, nullptr, &stagingTex);
assert(success);
CommandList cmd = device->BeginCommandList();
{
GPUBarrier barriers[] = {
GPUBarrier::Image(&texture,texture.desc.layout,ResourceState::COPY_SRC),
};
device->Barrier(barriers, arraysize(barriers), cmd);
}
device->CopyResource(&stagingTex, &texture, cmd);
{
GPUBarrier barriers[] = {
GPUBarrier::Image(&texture,ResourceState::COPY_SRC,texture.desc.layout),
};
device->Barrier(barriers, arraysize(barriers), cmd);
}
device->SubmitCommandLists();
device->WaitForGPU();
texturedata.clear();
if (stagingTex.mapped_data != nullptr)
{
texturedata.resize(ComputeTextureMemorySizeInBytes(desc));
const uint32_t data_stride = GetFormatStride(desc.format);
const uint32_t block_size = GetFormatBlockSize(desc.format);
size_t cpy_offset = 0;
size_t subresourceIndex = 0;
for (uint32_t layer = 0; layer < desc.array_size; ++layer)
{
for (uint32_t mip = 0; mip < desc.mip_levels; ++mip)
{
const uint32_t mip_width = std::max(1u, desc.width >> mip);
const uint32_t mip_height = std::max(1u, desc.height >> mip);
const uint32_t mip_depth = std::max(1u, desc.depth >> mip);
const uint32_t num_blocks_x = (mip_width + block_size - 1) / block_size;
const uint32_t num_blocks_y = (mip_height + block_size - 1) / block_size;
assert(subresourceIndex < stagingTex.mapped_subresource_count);
const SubresourceData& subresourcedata = stagingTex.mapped_subresources[subresourceIndex++];
const size_t dst_rowpitch = num_blocks_x * data_stride;
for (uint32_t z = 0; z < mip_depth; ++z)
{
uint8_t* dst_slice = texturedata.data() + cpy_offset;
uint8_t* src_slice = (uint8_t*)subresourcedata.data_ptr + subresourcedata.slice_pitch * z;
for (uint32_t i = 0; i < num_blocks_y; ++i)
{
std::memcpy(
dst_slice + i * dst_rowpitch,
src_slice + i * subresourcedata.row_pitch,
dst_rowpitch
);
}
cpy_offset += num_blocks_y * dst_rowpitch;
}
}
}
}
else
{
assert(0);
}
return stagingTex.mapped_data != nullptr;
}
bool saveTextureToMemoryFile(const wi::graphics::Texture& texture, const std::string& fileExtension, wi::vector<uint8_t>& filedata)
{
using namespace wi::graphics;
TextureDesc desc = texture.GetDesc();
wi::vector<uint8_t> texturedata;
if (saveTextureToMemory(texture, texturedata))
{
return saveTextureToMemoryFile(texturedata, desc, fileExtension, filedata);
}
return false;
}
bool saveTextureToMemoryFile(const wi::vector<uint8_t>& texturedata, const wi::graphics::TextureDesc& desc, const std::string& fileExtension, wi::vector<uint8_t>& filedata)
{
using namespace wi::graphics;
const uint32_t data_stride = GetFormatStride(desc.format);
std::string extension = wi::helper::toUpper(fileExtension);
if (extension.compare("DDS") == 0)
{
filedata.resize(sizeof(dds::Header) + texturedata.size());
dds::DXGI_FORMAT dds_format = dds::DXGI_FORMAT_UNKNOWN;
switch (desc.format)
{
case wi::graphics::Format::R32G32B32A32_FLOAT:
dds_format = dds::DXGI_FORMAT_R32G32B32A32_FLOAT;
break;
case wi::graphics::Format::R32G32B32A32_UINT:
dds_format = dds::DXGI_FORMAT_R32G32B32A32_UINT;
break;
case wi::graphics::Format::R32G32B32A32_SINT:
dds_format = dds::DXGI_FORMAT_R32G32B32A32_SINT;
break;
case wi::graphics::Format::R32G32B32_FLOAT:
dds_format = dds::DXGI_FORMAT_R32G32B32_FLOAT;
break;
case wi::graphics::Format::R32G32B32_UINT:
dds_format = dds::DXGI_FORMAT_R32G32B32_UINT;
break;
case wi::graphics::Format::R32G32B32_SINT:
dds_format = dds::DXGI_FORMAT_R32G32B32_SINT;
break;
case wi::graphics::Format::R16G16B16A16_FLOAT:
dds_format = dds::DXGI_FORMAT_R16G16B16A16_FLOAT;
break;
case wi::graphics::Format::R16G16B16A16_UNORM:
dds_format = dds::DXGI_FORMAT_R16G16B16A16_UNORM;
break;
case wi::graphics::Format::R16G16B16A16_UINT:
dds_format = dds::DXGI_FORMAT_R16G16B16A16_UINT;
break;
case wi::graphics::Format::R16G16B16A16_SNORM:
dds_format = dds::DXGI_FORMAT_R16G16B16A16_SNORM;
break;
case wi::graphics::Format::R16G16B16A16_SINT:
dds_format = dds::DXGI_FORMAT_R16G16B16A16_SINT;
break;
case wi::graphics::Format::R32G32_FLOAT:
dds_format = dds::DXGI_FORMAT_R32G32_FLOAT;
break;
case wi::graphics::Format::R32G32_UINT:
dds_format = dds::DXGI_FORMAT_R32G32_UINT;
break;
case wi::graphics::Format::R32G32_SINT:
dds_format = dds::DXGI_FORMAT_R32G32_SINT;
break;
case wi::graphics::Format::R10G10B10A2_UNORM:
dds_format = dds::DXGI_FORMAT_R10G10B10A2_UNORM;
break;
case wi::graphics::Format::R10G10B10A2_UINT:
dds_format = dds::DXGI_FORMAT_R10G10B10A2_UINT;
break;
case wi::graphics::Format::R11G11B10_FLOAT:
dds_format = dds::DXGI_FORMAT_R11G11B10_FLOAT;
break;
case wi::graphics::Format::R8G8B8A8_UNORM:
dds_format = dds::DXGI_FORMAT_R8G8B8A8_UNORM;
break;
case wi::graphics::Format::R8G8B8A8_UNORM_SRGB:
dds_format = dds::DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
break;
case wi::graphics::Format::R8G8B8A8_UINT:
dds_format = dds::DXGI_FORMAT_R8G8B8A8_UINT;
break;
case wi::graphics::Format::R8G8B8A8_SNORM:
dds_format = dds::DXGI_FORMAT_R8G8B8A8_SNORM;
break;
case wi::graphics::Format::R8G8B8A8_SINT:
dds_format = dds::DXGI_FORMAT_R8G8B8A8_SINT;
break;
case wi::graphics::Format::B8G8R8A8_UNORM:
dds_format = dds::DXGI_FORMAT_B8G8R8A8_UNORM;
break;
case wi::graphics::Format::B8G8R8A8_UNORM_SRGB:
dds_format = dds::DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
break;
case wi::graphics::Format::R16G16_FLOAT:
dds_format = dds::DXGI_FORMAT_R16G16_FLOAT;
break;
case wi::graphics::Format::R16G16_UNORM:
dds_format = dds::DXGI_FORMAT_R16G16_UNORM;
break;
case wi::graphics::Format::R16G16_UINT:
dds_format = dds::DXGI_FORMAT_R16G16_UINT;
break;
case wi::graphics::Format::R16G16_SNORM:
dds_format = dds::DXGI_FORMAT_R16G16_SNORM;
break;
case wi::graphics::Format::R16G16_SINT:
dds_format = dds::DXGI_FORMAT_R16G16_SINT;
break;
case wi::graphics::Format::D32_FLOAT:
case wi::graphics::Format::R32_FLOAT:
dds_format = dds::DXGI_FORMAT_R32_FLOAT;
break;
case wi::graphics::Format::R32_UINT:
dds_format = dds::DXGI_FORMAT_R32_UINT;
break;
case wi::graphics::Format::R32_SINT:
dds_format = dds::DXGI_FORMAT_R32_SINT;
break;
case wi::graphics::Format::R9G9B9E5_SHAREDEXP:
dds_format = dds::DXGI_FORMAT_R9G9B9E5_SHAREDEXP;
break;
case wi::graphics::Format::R8G8_UNORM:
dds_format = dds::DXGI_FORMAT_R8G8_UNORM;
break;
case wi::graphics::Format::R8G8_UINT:
dds_format = dds::DXGI_FORMAT_R8G8_UINT;
break;
case wi::graphics::Format::R8G8_SNORM:
dds_format = dds::DXGI_FORMAT_R8G8_SNORM;
break;
case wi::graphics::Format::R8G8_SINT:
dds_format = dds::DXGI_FORMAT_R8G8_SINT;
break;
case wi::graphics::Format::R16_FLOAT:
dds_format = dds::DXGI_FORMAT_R16_FLOAT;
break;
case wi::graphics::Format::D16_UNORM:
case wi::graphics::Format::R16_UNORM:
dds_format = dds::DXGI_FORMAT_R16_UNORM;
break;
case wi::graphics::Format::R16_UINT:
dds_format = dds::DXGI_FORMAT_R16_UINT;
break;
case wi::graphics::Format::R16_SNORM:
dds_format = dds::DXGI_FORMAT_R16_SNORM;
break;
case wi::graphics::Format::R16_SINT:
dds_format = dds::DXGI_FORMAT_R16_SINT;
break;
case wi::graphics::Format::R8_UNORM:
dds_format = dds::DXGI_FORMAT_R8_UNORM;
break;
case wi::graphics::Format::R8_UINT:
dds_format = dds::DXGI_FORMAT_R8_UINT;
break;
case wi::graphics::Format::R8_SNORM:
dds_format = dds::DXGI_FORMAT_R8_SNORM;
break;
case wi::graphics::Format::R8_SINT:
dds_format = dds::DXGI_FORMAT_R8_SINT;
break;
case wi::graphics::Format::BC1_UNORM:
dds_format = dds::DXGI_FORMAT_BC1_UNORM;
break;
case wi::graphics::Format::BC1_UNORM_SRGB:
dds_format = dds::DXGI_FORMAT_BC1_UNORM_SRGB;
break;
case wi::graphics::Format::BC2_UNORM:
dds_format = dds::DXGI_FORMAT_BC2_UNORM;
break;
case wi::graphics::Format::BC2_UNORM_SRGB:
dds_format = dds::DXGI_FORMAT_BC2_UNORM_SRGB;
break;
case wi::graphics::Format::BC3_UNORM:
dds_format = dds::DXGI_FORMAT_BC3_UNORM;
break;
case wi::graphics::Format::BC3_UNORM_SRGB:
dds_format = dds::DXGI_FORMAT_BC3_UNORM_SRGB;
break;
case wi::graphics::Format::BC4_UNORM:
dds_format = dds::DXGI_FORMAT_BC4_UNORM;
break;
case wi::graphics::Format::BC4_SNORM:
dds_format = dds::DXGI_FORMAT_BC4_SNORM;
break;
case wi::graphics::Format::BC5_UNORM:
dds_format = dds::DXGI_FORMAT_BC5_UNORM;
break;
case wi::graphics::Format::BC5_SNORM:
dds_format = dds::DXGI_FORMAT_BC5_SNORM;
break;
case wi::graphics::Format::BC6H_UF16:
dds_format = dds::DXGI_FORMAT_BC6H_UF16;
break;
case wi::graphics::Format::BC6H_SF16:
dds_format = dds::DXGI_FORMAT_BC6H_SF16;
break;
case wi::graphics::Format::BC7_UNORM:
dds_format = dds::DXGI_FORMAT_BC7_UNORM;
break;
case wi::graphics::Format::BC7_UNORM_SRGB:
dds_format = dds::DXGI_FORMAT_BC7_UNORM_SRGB;
break;
default:
assert(0);
return false;
}
dds::write_header(
filedata.data(),
dds_format,
desc.width,
desc.type == TextureDesc::Type::TEXTURE_1D ? 0 : desc.height,
desc.mip_levels,
desc.array_size,
has_flag(desc.misc_flags, ResourceMiscFlag::TEXTURECUBE),
desc.type == TextureDesc::Type::TEXTURE_3D ? desc.depth : 0
);
std::memcpy(filedata.data() + sizeof(dds::Header), texturedata.data(), texturedata.size());
return true;
}
const bool is_png = extension.compare("PNG") == 0;
if (is_png)
{
if (desc.format == Format::R16_UNORM || desc.format == Format::R16_UINT)
{
// Specialized handling for 16-bit single channel PNG:
wi::vector<uint8_t> src_bigendian = texturedata;
uint16_t* dest = (uint16_t*)src_bigendian.data();
for (uint32_t i = 0; i < desc.width * desc.height; ++i)
{
uint16_t r = dest[i];
r = (r >> 8) | ((r & 0xFF) << 8); // little endian to big endian
dest[i] = r;
}
unsigned error = lodepng::encode(filedata, src_bigendian, desc.width, desc.height, LCT_GREY, 16);
return error == 0;
}
if (desc.format == Format::R16G16_UNORM || desc.format == Format::R16G16_UINT)
{
// Specialized handling for 16-bit PNG:
// Two channel RG data is expanded to RGBA (2-channel PNG is not good because that is interpreted as red and alpha)
wi::vector<uint8_t> src_bigendian = texturedata;
const uint32_t* src_rg = (const uint32_t*)src_bigendian.data();
wi::vector<wi::Color16> dest_rgba(desc.width * desc.height);
for (uint32_t i = 0; i < desc.width * desc.height; ++i)
{
uint32_t rg = src_rg[i];
wi::Color16& rgba = dest_rgba[i];
uint16_t r = rg & 0xFFFF;
r = (r >> 8) | ((r & 0xFF) << 8); // little endian to big endian
uint16_t g = (rg >> 16u) & 0xFFFF;
g = (g >> 8) | ((g & 0xFF) << 8); // little endian to big endian
rgba = wi::Color16(r, g, 0xFFFF, 0xFFFF);
}
unsigned error = lodepng::encode(filedata, (const unsigned char*)dest_rgba.data(), desc.width, desc.height, LCT_RGBA, 16);
return error == 0;
}
if (desc.format == Format::R16G16B16A16_UNORM || desc.format == Format::R16G16B16A16_UINT)
{
// Specialized handling for 16-bit PNG:
wi::vector<uint8_t> src_bigendian = texturedata;
wi::Color16* dest = (wi::Color16*)src_bigendian.data();
for (uint32_t i = 0; i < desc.width * desc.height; ++i)
{
wi::Color16 rgba = dest[i];
uint16_t r = rgba.getR();
r = (r >> 8) | ((r & 0xFF) << 8); // little endian to big endian
uint16_t g = rgba.getG();
g = (g >> 8) | ((g & 0xFF) << 8); // little endian to big endian
uint16_t b = rgba.getB();
b = (b >> 8) | ((b & 0xFF) << 8); // little endian to big endian
uint16_t a = rgba.getA();
a = (a >> 8) | ((a & 0xFF) << 8); // little endian to big endian
rgba = wi::Color16(r, g, b, a);
dest[i] = rgba;
}
unsigned error = lodepng::encode(filedata, src_bigendian, desc.width, desc.height, LCT_RGBA, 16);
return error == 0;
}
}
struct MipDesc
{
const uint8_t* address = nullptr;
uint32_t width = 0;
uint32_t height = 0;
uint32_t depth = 0;
};
wi::vector<MipDesc> mips;
mips.reserve(desc.mip_levels);
uint32_t data_count = 0;
uint32_t mip_width = desc.width;
uint32_t mip_height = desc.height;
uint32_t mip_depth = desc.depth;
for (uint32_t mip = 0; mip < desc.mip_levels; ++mip)
{
MipDesc& mipdesc = mips.emplace_back();
mipdesc.address = texturedata.data() + data_count * data_stride;
data_count += mip_width * mip_height * mip_depth;
mipdesc.width = mip_width;
mipdesc.height = mip_height;
mipdesc.depth = mip_depth;
mip_width = std::max(1u, mip_width / 2);
mip_height = std::max(1u, mip_height / 2);
mip_depth = std::max(1u, mip_depth / 2);
}
int dst_channel_count = 4;
if (desc.format == Format::R10G10B10A2_UNORM)
{
// This will be converted first to rgba8 before saving to common format:
uint32_t* data32 = (uint32_t*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
uint32_t pixel = data32[i];
float r = ((pixel >> 0) & 1023) / 1023.0f;
float g = ((pixel >> 10) & 1023) / 1023.0f;
float b = ((pixel >> 20) & 1023) / 1023.0f;
float a = ((pixel >> 30) & 3) / 3.0f;
#ifdef __APPLE__
// The R10G10B10A2_UNORM format only has BGRA equivalent in Metal API and we use that:
std::swap(r, b);
#endif // __APPLE__
uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;
data32[i] = rgba8;
}
}
else if (desc.format == Format::R32G32B32A32_FLOAT)
{
// This will be converted first to rgba8 before saving to common format:
XMFLOAT4* dataSrc = (XMFLOAT4*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
XMFLOAT4 pixel = dataSrc[i];
float r = std::max(0.0f, std::min(pixel.x, 1.0f));
float g = std::max(0.0f, std::min(pixel.y, 1.0f));
float b = std::max(0.0f, std::min(pixel.z, 1.0f));
float a = std::max(0.0f, std::min(pixel.w, 1.0f));
uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;
data32[i] = rgba8;
}
}
else if (desc.format == Format::R16G16B16A16_FLOAT)
{
// This will be converted first to rgba8 before saving to common format:
XMHALF4* dataSrc = (XMHALF4*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
XMHALF4 pixel = dataSrc[i];
float r = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.x), 1.0f));
float g = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.y), 1.0f));
float b = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.z), 1.0f));
float a = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.w), 1.0f));
uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;
data32[i] = rgba8;
}
}
else if (desc.format == Format::R16G16B16A16_UNORM || desc.format == Format::R16G16B16A16_UINT)
{
// This will be converted first to rgba8 before saving to common format:
wi::Color16* dataSrc = (wi::Color16*)texturedata.data();
wi::Color* data32 = (wi::Color*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
wi::Color16 pixel16 = dataSrc[i];
data32[i] = wi::Color::fromFloat4(pixel16.toFloat4());
}
}
else if (desc.format == Format::R11G11B10_FLOAT)
{
// This will be converted first to rgba8 before saving to common format:
XMFLOAT3PK* dataSrc = (XMFLOAT3PK*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
XMFLOAT3PK pixel = dataSrc[i];
XMVECTOR V = XMLoadFloat3PK(&pixel);
XMFLOAT3 pixel3;
XMStoreFloat3(&pixel3, V);
float r = std::max(0.0f, std::min(pixel3.x, 1.0f));
float g = std::max(0.0f, std::min(pixel3.y, 1.0f));
float b = std::max(0.0f, std::min(pixel3.z, 1.0f));
float a = 1;
uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;
data32[i] = rgba8;
}
}
else if (desc.format == Format::R9G9B9E5_SHAREDEXP)
{
// This will be converted first to rgba8 before saving to common format:
XMFLOAT3SE* dataSrc = (XMFLOAT3SE*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
XMFLOAT3SE pixel = dataSrc[i];
XMVECTOR V = XMLoadFloat3SE(&pixel);
XMFLOAT3 pixel3;
XMStoreFloat3(&pixel3, V);
float r = std::max(0.0f, std::min(pixel3.x, 1.0f));
float g = std::max(0.0f, std::min(pixel3.y, 1.0f));
float b = std::max(0.0f, std::min(pixel3.z, 1.0f));
float a = 1;
uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;
data32[i] = rgba8;
}
}
else if (desc.format == Format::B8G8R8A8_UNORM || desc.format == Format::B8G8R8A8_UNORM_SRGB)
{
// This will be converted first to rgba8 before saving to common format:
uint32_t* data32 = (uint32_t*)texturedata.data();
for (uint32_t i = 0; i < data_count; ++i)
{
uint32_t pixel = data32[i];
uint8_t b = (pixel >> 0u) & 0xFF;
uint8_t g = (pixel >> 8u) & 0xFF;
uint8_t r = (pixel >> 16u) & 0xFF;
uint8_t a = (pixel >> 24u) & 0xFF;
data32[i] = r | (g << 8u) | (b << 16u) | (a << 24u);
}
}
else if (desc.format == Format::R8_UNORM)
{
// This can be saved by reducing target channel count, no conversion needed
dst_channel_count = 1;
}
else if (desc.format == Format::R8G8_UNORM)
{
// This can be saved by reducing target channel count, no conversion needed
dst_channel_count = 2;
}
else
{
assert(desc.format == Format::R8G8B8A8_UNORM || desc.format == Format::R8G8B8A8_UNORM_SRGB); // If you need to save other texture format, implement data conversion for it
}
if (!extension.compare("ICO"))
{
const uint32_t minsize = 32;
size_t filesize = sizeof(ico::ICONDIR);
ico::ICONDIR icondir = { 0,1,0 };
for (auto& mip : mips)
{
if (mip.width > 256 || mip.height > 256)
continue;
if (mip.width < minsize || mip.height < minsize)
break;
icondir.idCount++;
filesize += sizeof(ico::ICONDIRENTRY);
}
if (icondir.idCount < 1)
{
wilog_assert(0, "No valid images were found that can be added to ICO file format!");
return false;
}
uint32_t imageDataOffset = (uint32_t)filesize;
for (auto& mip : mips)
{
if (mip.width > 256 || mip.height > 256)
continue;
if (mip.width < minsize || mip.height < minsize)
break;
const uint32_t pixelCount = mip.width * mip.height;
const uint32_t rgbDataSize = pixelCount * 4; // 32-bit RGBA
const uint32_t maskSize = ((mip.width + 7) / 8) * mip.height; // 1-bit mask, padded to byte
const uint32_t imageDataSize = sizeof(ico::BITMAPINFOHEADER) + rgbDataSize + maskSize;
filesize += imageDataSize;
}
filedata.resize(filesize);
uint8_t* ptr = filedata.data();
std::memcpy(ptr, &icondir, sizeof(ico::ICONDIR));
ptr += sizeof(ico::ICONDIR);
for (auto& mip : mips)
{
if (mip.width > 256 || mip.height > 256)
continue;
if (mip.width < minsize || mip.height < minsize)
break;
const uint32_t pixelCount = mip.width * mip.height;
const uint32_t rgbDataSize = pixelCount * 4; // 32-bit RGBA
const uint32_t maskSize = ((mip.width + 7) / 8) * mip.height; // 1-bit mask, padded to byte
const uint32_t imageDataSize = sizeof(ico::BITMAPINFOHEADER) + rgbDataSize + maskSize;
ico::ICONDIRENTRY iconEntry = {
static_cast<uint8_t>(mip.width > 255 ? 0 : mip.width), // Width (0 for 256+)
static_cast<uint8_t>(mip.height > 255 ? 0 : mip.height), // Height (0 for 256+)
0, // Color count (0 for 32-bit)
0, // Reserved
1, // Color planes
32, // Bits per pixel
imageDataSize, // Size of image data
imageDataOffset // Offset to image data
};
std::memcpy(ptr, &iconEntry, sizeof(ico::ICONDIRENTRY));
ptr += sizeof(ico::ICONDIRENTRY);
imageDataOffset += imageDataSize;
}
for (auto& mip : mips)
{
if (mip.width > 256 || mip.height > 256)
continue;
if (mip.width < minsize || mip.height < minsize)
break;
const uint32_t pixelCount = mip.width * mip.height;
const uint32_t rgbDataSize = pixelCount * 4; // 32-bit RGBA
const uint32_t maskSize = ((mip.width + 7) / 8) * mip.height; // 1-bit mask, padded to byte
ico::BITMAPINFOHEADER bmpHeader = {
sizeof(ico::BITMAPINFOHEADER), // Size of header
int32_t(mip.width), // Width
int32_t(mip.height * 2), // Height (doubled for XOR + AND mask)
1, // Planes
32, // Bits per pixel
0, // No compression
rgbDataSize + maskSize, // Image size
0, // X pixels per meter
0, // Y pixels per meter
0, // Colors used
0 // Important colors
};
std::memcpy(ptr, &bmpHeader, sizeof(ico::BITMAPINFOHEADER));
ptr += sizeof(ico::BITMAPINFOHEADER);
// Convert RGBA to BGRA and write XOR mask (flipped vertically)
for (uint32_t y = mip.height; y > 0; --y)
{
const uint8_t* src = mip.address + (y - 1) * mip.width * 4;
for (uint32_t x = 0; x < mip.width; ++x)
{
// Convert RGBA to BGRA
ptr[0] = src[2]; // B
ptr[1] = src[1]; // G
ptr[2] = src[0]; // R
ptr[3] = src[3]; // A
ptr += 4;
src += 4;
}
}
// Write AND mask (1-bit transparency mask)
for (uint32_t y = mip.height; y > 0; --y)
{
const uint8_t* src = mip.address + (y - 1) * mip.width * 4;
for (uint32_t x = 0; x < mip.width; x += 8)
{
uint8_t maskByte = 0;
for (uint32_t bit = 0; bit < 8 && (x + bit) < mip.width; ++bit)
{
// Set bit to 0 if pixel is opaque (alpha > 0), 1 if transparent
if (src[(x + bit) * 4 + 3] == 0)
{
maskByte |= (1 << (7 - bit));
}
}
*ptr++ = maskByte;
}
}
}
return true;
}
int write_result = 0;
filedata.clear();
stbi_write_func* func = [](void* context, void* data, int size) {
wi::vector<uint8_t>& filedata = *(wi::vector<uint8_t>*)context;
for (int i = 0; i < size; ++i)
{
filedata.push_back(*((uint8_t*)data + i));
}
};
static int mip_request = 0; // you can use this while debugging to write specific mip level to file (todo: option param?)
const MipDesc& mip = mips[mip_request];
if (is_png)
{
write_result = stbi_write_png_to_func(func, &filedata, (int)mip.width, (int)mip.height, dst_channel_count, mip.address, 0);
}
else if (!extension.compare("JPG") || !extension.compare("JPEG"))
{
write_result = stbi_write_jpg_to_func(func, &filedata, (int)mip.width, (int)mip.height, dst_channel_count, mip.address, 100);
}
else if (!extension.compare("TGA"))
{
write_result = stbi_write_tga_to_func(func, &filedata, (int)mip.width, (int)mip.height, dst_channel_count, mip.address);
}
else if (!extension.compare("BMP"))
{
write_result = stbi_write_bmp_to_func(func, &filedata, (int)mip.width, (int)mip.height, dst_channel_count, mip.address);
}
else if (!extension.compare("H"))
{
const size_t datasize = mip.width * mip.height * sizeof(wi::Color);
std::string ss;
ss += "struct EmbeddedImage {\n";
ss += "const unsigned int width = " + std::to_string(mip.width) + ";\n";
ss += "const unsigned int height = " + std::to_string(mip.height) + ";\n";
ss += "const unsigned int bytes_per_pixel = 4;\n";
ss += "const char comment[256] = \"RGBA Image Header Generated By HMMWV4\";\n";
ss += "const unsigned char pixel_data[" + std::to_string(mip.width) + " * " + std::to_string(mip.height) + " * 4] = {";
for (size_t i = 0; i < datasize; ++i)
{
if (i % 32 == 0)
{
ss += "\n";
}
ss += std::to_string((uint32_t)mip.address[i]) + ",";
}
ss += "\n};\n};\n";
filedata.resize(ss.size());
std::memcpy(filedata.data(), ss.c_str(), ss.length());
return true;
}
else if (!extension.compare("RAW"))
{
filedata.resize(mip.width* mip.height * sizeof(wi::Color));
std::memcpy(filedata.data(), mip.address, filedata.size());
return true;
}
else
{
assert(0 && "Unsupported extension");
}