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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */

/*
 * Copyright 2017-2020 Mark Callow.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @internal
 * @class VulkanLoadTests
 * @~English
 *
 * @brief Framework for Vulkan texture loading test samples.
 *
 * @author Mark Callow, github.com/MarkCallow.
 */

#include <exception>

#define _USE_MATH_DEFINES
#include <math.h>
#include <string>
#include <regex>

#include "VulkanLoadTests.h"
#include "Texture.h"
#include "Texture3d.h"
#include "TextureArray.h"
#include "TextureCubemap.h"
#include "TexturedCube.h"
#include "TextureMipmap.h"
#include "ltexceptions.h"

#define LT_VK_MAJOR_VERSION 1
#define LT_VK_MINOR_VERSION 0
#define LT_VK_PATCH_VERSION 0
#define LT_VK_VERSION VK_MAKE_VERSION(1, 0, 0)

VulkanLoadTests::VulkanLoadTests(const sampleInvocation samples[],
                                 const uint32_t numSamples,
                                 const char* const name)
                  : VulkanAppSDL(name, 1280, 720, LT_VK_VERSION, true),
                    siSamples(samples), sampleIndex(numSamples)
{
    pCurSample = nullptr;
}

VulkanLoadTests::~VulkanLoadTests()
{
    delete pCurSample;
}

bool
VulkanLoadTests::initialize(Args& args)
{
    if (!VulkanAppSDL::initialize(args))
        return false;

    for (auto it = args.begin() + 1; it != args.end(); it++) {
        infiles.push_back(*it);
    }
    if (infiles.size() > 0) {
        sampleIndex.setNumSamples((uint32_t)infiles.size());
    }
    // Launch the first sample.
    invokeSample(Direction::eForward);
    return true;
}

void
VulkanLoadTests::finalize()
{
    delete pCurSample;
    VulkanAppSDL::finalize();
}

bool
VulkanLoadTests::doEvent(SDL_Event* event)
{
    bool result = false;

    switch (event->type) {
      case SDL_EVENT_KEY_UP:
        switch (event->key.key) {
          case 'q':
            quit = true;
            break;
          case 'n':
            ++sampleIndex;
            invokeSample(Direction::eForward);
            break;
          case 'p':
            --sampleIndex;
            invokeSample(Direction::eBack);
            break;
          default:
            result = true;
        }
        break;

      // On macOS drop events come also when Launch Pad sends a file open event.
      case SDL_EVENT_DROP_BEGIN:
        // Opens of multiple selected files from Finder/LaunchPad come as
        // a BEGIN, COMPLETE sequence per file. Only clear infiles after a
        // suitable pause between COMPLETE and BEGIN.
        if (event->drop.timestamp - dropCompleteTime > 500) {
            infiles.clear();
        }
        break;
      case SDL_EVENT_DROP_FILE:
        infiles.push_back(event->drop.data);
        break;
      case SDL_EVENT_DROP_COMPLETE:
        if (!infiles.empty()) {
            // Guard against the drop being text.
            dropCompleteTime = event->drop.timestamp;
            sampleIndex.setNumSamples((uint32_t)infiles.size());
            invokeSample(Direction::eForward);
        }
        break;
      case SDL_EVENT_USER:
        if (event->user.code == SwipeDetector::swipeGesture) {
            SwipeDetector::Direction direction
                = SwipeDetector::pointerToDirection(event->user.data1);
            switch (direction) {
              case SwipeDetector::Direction::left:
                ++sampleIndex;
                //SDL_Log("*********** Sample changed by swipe left **********");
                invokeSample(Direction::eForward);
                break;
              case SwipeDetector::Direction::right:
                --sampleIndex;
                //SDL_Log("*********** Sample changed by swipe right *********");
                invokeSample(Direction::eBack);
                break;
              default:
                result = true;
            }
        } else {
            result = true;
        }
        break;
      default:
        result = swipeDetector.doEvent(event);
    }
    
    if (result) {
        // Further processing required.
        if (pCurSample != nullptr)
            result = pCurSample->doEvent(event);  // Give sample a chance.
        if (result)
            return VulkanAppSDL::doEvent(event);  // Pass to base class.
    }
    return result;
}


void
VulkanLoadTests::windowResized()
{
    if (pCurSample != nullptr)
        pCurSample->resize(w_width, w_height);
}


void
VulkanLoadTests::drawFrame(uint32_t msTicks)
{
    if (pCurSample != nullptr)
        pCurSample->run(msTicks);

    VulkanAppSDL::drawFrame(msTicks);
}


void
VulkanLoadTests::getOverlayText(float yOffset)
{
    if (enableTextOverlay) {
        textOverlay->addText("Press \"n\" or 2-finger swipe left for next "
                             "sample, \"p\" or swipe right for previous.",
                             5.0f, yOffset, VulkanTextOverlay::alignLeft);
        yOffset += 20;
        textOverlay->addText("2-finger rotate or left mouse + drag to rotate.",
                             5.0f, yOffset, VulkanTextOverlay::alignLeft);
        yOffset += 20;
        textOverlay->addText("Pinch/zoom or right mouse + drag to change "
                             "object size.",
                             5.0f, yOffset, VulkanTextOverlay::alignLeft);
        yOffset += 20;
        if (pCurSample != nullptr) {
            pCurSample->getOverlayText(textOverlay, yOffset);
        }
    }
}

void
VulkanLoadTests::invokeSample(Direction dir)
{
    const sampleInvocation* sampleInv
            = &siSamples[sampleIndex];

    prepared = false;  // Prevent any more rendering.
    if (pCurSample != nullptr) {
        vkctx.queue.waitIdle(); // Wait for current rendering to finish.
        delete pCurSample;
        // Certain events can be triggered during new sample initialization
        // while pCurSample is not valid, e.g. FOCUS_LOST caused by a Vulkan
        // validation failure raising a message box. Protect against problems
        // from this by indicating there is no current sample.
        pCurSample = nullptr;
    }

    uint32_t unsupportedTypeExceptions = 0;
    std::string fileTitle;
    for (;;) {
        try {
            if (infiles.size() > 0) {
                fileTitle = "Viewing file ";
                fileTitle += infiles[sampleIndex];
                pCurSample = showFile(infiles[sampleIndex]);
            } else {
                sampleInv = &siSamples[sampleIndex];
                pCurSample = sampleInv->createSample(vkctx, w_width, w_height,
                                                     sampleInv->args,
                                                     sBasePath);
            }
            break;
        } catch (unsupported_ttype& e) {
            (void)e; // To quiet unused variable warnings from some compilers.
            unsupportedTypeExceptions++;
            if (unsupportedTypeExceptions == sampleIndex.getNumSamples()) {
                std::string manyFailMessage =
                    "All of the specified samples or files use texture types "
                    "or features not supported on this platform.";
                const std::string& message = unsupportedTypeExceptions == 1 ?
                                       e.what() : manyFailMessage;
                SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
                    infiles.size() > 0 ? fileTitle.c_str() : sampleInv->title,
                    message.c_str(),
                    NULL);
                exit(0);
            } else {
                dir == Direction::eForward ? ++sampleIndex : --sampleIndex;
            }
        } catch (bad_vulkan_alloc& e) {
            const SDL_MessageBoxButtonData buttons[] = {
                /* .flags, .buttonid, .text */
                { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Abort" },
            };
            const SDL_MessageBoxData messageboxdata = {
                SDL_MESSAGEBOX_ERROR,                           // .flags
                NULL,                                           // .window
                infiles.size() > 0 ?
                  fileTitle.c_str() : sampleInv->title,         // .title
                e.what(),                                       // .message
                SDL_arraysize(buttons),                         // .numbuttons
                buttons,                                        // .buttons
                NULL //&colorScheme                             // .colorScheme
            };
            int buttonid;
            if (!SDL_ShowMessageBox(&messageboxdata, &buttonid)) {
                SDL_Log("error displaying error message box");
                exit(1);
            }
            if (buttonid == 0) {
                // We've been told to quit or no button was pressed.
                exit(1);
            }
        } catch (std::exception& e) {
            const SDL_MessageBoxButtonData buttons[] = {
                /* .flags, .buttonid, .text */
                { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Continue" },
                { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 1, "Abort" },
            };
#if 0
            const SDL_MessageBoxColorScheme colorScheme = {
                { /* .colors (.r, .g, .b) */
                    /* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
                    { 255,   0,   0 },
                    /* [SDL_MESSAGEBOX_COLOR_TEXT] */
                    {   0, 255,   0 },
                    /* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
                    { 255, 255,   0 },
                    /* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
                    {   0,   0, 255 },
                    /* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
                    { 255,   0, 255 }
                }
            };
#endif
            const SDL_MessageBoxData messageboxdata = {
                SDL_MESSAGEBOX_ERROR,                               // .flags
                NULL,                                               // .window
                infiles.size() > 0 ?
                 fileTitle.c_str() : sampleInv->title,              // .title
                e.what(),                                           // .message
                SDL_arraysize(buttons),                             // .numbuttons
                buttons,                                            // .buttons
                NULL //&colorScheme                                 // .colorScheme
            };
            int buttonid;
            if (!SDL_ShowMessageBox(&messageboxdata, &buttonid)) {
                SDL_Log("error displaying error message box");
                exit(1);
            }
            if (buttonid == 0) {
                // Continue
                dir == Direction::eForward ? ++sampleIndex : --sampleIndex;
            } else {
                // We've been told to quit or no button was pressed.
                exit(1);
            }
        }
    }
    prepared = true;
    setAppTitle(pCurSample->customizeTitle(infiles.size() > 0 ?
                                                            fileTitle.c_str()
                                                          : sampleInv->title));
}


void
VulkanLoadTests::onFPSUpdate()
{
    VulkanAppSDL::onFPSUpdate();
}

VulkanLoadTestSample*
VulkanLoadTests::showFile(const std::string& filename)
{
    KTX_error_code ktxresult;
    ktxTexture* kTexture;
    ktxresult = ktxTexture_CreateFromNamedFile(filename.c_str(),
                                        KTX_TEXTURE_CREATE_NO_FLAGS,
                                        &kTexture);
    if (KTX_SUCCESS != ktxresult) {
        std::stringstream message;

        message << "Creation of ktxTexture from \"" << filename
                << "\" failed: " << ktxErrorString(ktxresult);
        throw std::runtime_error(message.str());
    }

    VulkanLoadTestSample::PFN_create createViewer;
    VulkanLoadTestSample* pViewer;
    if (kTexture->numDimensions == 3)
       createViewer = Texture3d::create;
    else if (kTexture->isArray && kTexture->isCubemap) {
        // TODO: Add cubemap array app.
        std::stringstream message;
        message << "Display of cubemap array textures not yet implemented.";
        throw std::runtime_error(message.str());
    } else if (kTexture->isArray) {
        createViewer = TextureArray::create;
    } else if (kTexture->isCubemap) {
        createViewer = TextureCubemap::create;
    } else if (kTexture->numLevels > 1) {
        createViewer = TextureMipmap::create;
    } else {
        createViewer = Texture::create;
    }
    ktxTexture_Destroy(kTexture);

    // Escape any spaces in filename.
    std::string args = "--external " + std::regex_replace( filename, std::regex(" "), "\\ " );
    pViewer = createViewer(vkctx, w_width, w_height, args.c_str(), sBasePath);
    return pViewer;
}

/* ------------------------------------------------------------------------ */

const VulkanLoadTests::sampleInvocation siSamples[] = {
    { Texture::create,
      "etc1s_Iron_Bars_001_normal.ktx2",
      "Transcode of ETC1S+BasisLZ Compressed KTX2 XY normal map mipmapped"
    },
    { Texture::create,
      "uastc_Iron_Bars_001_normal.ktx2",
      "Transcode of UASTC+zstd Compressed KTX2 XY normal map mipmapped"
    },
    { Texture::create,
      "--use-vma uastc_Iron_Bars_001_normal.ktx2",
      "Transcode of UASTC+zstd Compressed KTX2 XY normal map mipmapped, using VMA"
    },
    { Texture::create,
      "ktx_document_uastc_rdo4_zstd5.ktx2",
      "UASTC+rdo+zstd compressed KTX2 RGBA8 mipmapped"
    },
    { Texture::create,
      "color_grid_uastc_zstd.ktx2",
      "UASTC+zstd Compressed KTX2 RGB non-mipmapped"
    },
    { Texture::create,
      "color_grid_zstd.ktx2",
      "Zstd Compressed KTX2 RGB non-mipmapped"
    },
    { Texture::create,
      "color_grid_uastc.ktx2",
      "UASTC Compressed KTX2 RGB non-mipmapped"
    },
    { Texture::create,
      "color_grid_basis.ktx2",
      "ETC1S+BasisLZ Compressed KTX2 RGB non-mipmapped"
    },
    { Texture::create,
      "kodim17_basis.ktx2",
      "ETC1S+BasisLZ Compressed KTX2 RGB non-mipmapped"
    },
    { Texture::create,
        "--qcolor 0.0,0.0,0.0 pattern_02_bc2.ktx2",
        "KTX2: BC2 (S3TC DXT3) Compressed 2D"
    },
    { TextureMipmap::create,
      "ktx_document_basis.ktx2",
      "ETC1S+BasisLZ  compressed RGBA + Mipmap"
    },
    { TextureMipmap::create,
      "rgba-mipmap-reference-basis.ktx2",
      // ETC1S encoder removed all-1 alpha from rgba-mipmap-reference.
      "ETC1S+BasisLZ Compressed RGB + Mipmap"
    },
    { Texture3d::create,
      "3dtex_7_reference_u.ktx2",
      "RGBA8 3d Texture, Depth == 7"
    },
    { TextureArray::create,
      "arraytex_7_mipmap_reference_u.ktx2",
      "RGBA8 Array Texture, Layers = 7"
    },
    { TextureCubemap::create,
      "cubemap_goldengate_uastc_rdo4_zstd5_rd.ktx2",
      "UASTC+rdo+zstd Compressed RGB Cube Map"
    },
    { TextureCubemap::create,
      "--preload cubemap_goldengate_uastc_rdo4_zstd5_rd.ktx2",
      "UASTC+rdo+zstd Compressed RGB Cube Map from pre-loaded images"
    },
    { TextureCubemap::create,
        "cubemap_yokohama_basis_rd.ktx2",
        "ETC1S+BasisLZ Compressed RGB Cube Map"
    },
    { TextureCubemap::create,
        "--preload cubemap_yokohama_basis_rd.ktx2",
        "ETC1S+BasisLZ Compressed RGB Cube Map from pre-loaded images"
    },
    { TextureCubemap::create,
        "skybox_zstd.ktx2",
        "Zstd Compressed B10G11R11_UFLOAT Cube Map. Tests for correct blockSizeInBits after inflation"
    },
    { Texture::create,
      "orient-down-metadata.ktx",
      "RGB8 2D + KTXOrientation down"
    },
    { Texture::create,
      "orient-up-metadata.ktx",
      "RGB8 2D + KTXOrientation up"
    },
    { Texture::create,
      "--linear-tiling orient-up-metadata.ktx",
      "RGB8 2D + KTXOrientation up with Linear Tiling"
    },
    { Texture::create,
      "rgba-reference.ktx",
      "RGBA8 2D"
    },
    { Texture::create,
        "--linear-tiling rgba-reference.ktx",
        "RGBA8 2D using Linear Tiling"
    },
    { Texture::create,
      "etc2-rgb.ktx",
      "ETC2 RGB8"
    },
    { Texture::create,
      "etc2-rgba8.ktx",
      "ETC2 RGB8A8"
    },
    { Texture::create,
      "etc2-sRGB.ktx",
      "ETC2 sRGB8"
    },
    { Texture::create,
        "etc2-sRGBa8.ktx",
        "ETC2 sRGB8a8"
    },
    { Texture::create,
        "--qcolor 0.0,0.0,0.0 pattern_02_bc2.ktx",
        "BC2 (S3TC DXT3) Compressed 2D"
    },
    { TextureMipmap::create,
      "rgb-amg-reference.ktx",
      "RGB8 + Auto Mipmap"
    },
    { TextureMipmap::create,
      "--linear-tiling rgb-amg-reference.ktx",
      "RGB8 + Auto Mipmap using Linear Tiling"
    },
    { TextureMipmap::create,
      "metalplate-amg-rgba8.ktx",
      "RGBA8 2D + Auto Mipmap"
    },
    { TextureMipmap::create,
      "--linear-tiling metalplate-amg-rgba8.ktx",
      "RGBA8 2D + Auto Mipmap using Linear Tiling"
    },
    { TextureMipmap::create,
      "not4_rgb888_srgb.ktx",
      "RGB8 2D, Row length not Multiple of 4"
    },
    { TextureMipmap::create,
      "--linear-tiling not4_rgb888_srgb.ktx",
      "RGB8 2D, Row length not Multiple of 4 using Linear Tiling"
    },
    { TextureArray::create,
        "texturearray_bc3_unorm.ktx",
        "BC2 (S3TC DXT3) Compressed Texture Array"
    },
    { TextureArray::create,
        "--linear-tiling texturearray_bc3_unorm.ktx",
        "BC2 (S3TC DXT3) Compressed Texture Array using Linear Tiling"
    },
    { TextureArray::create,
        "texturearray_astc_8x8_unorm.ktx",
        "ASTC 8x8 Compressed Texture Array"
    },
    { TextureArray::create,
        "texturearray_etc2_unorm.ktx",
        "ETC2 Compressed Texture Array"
    },
#if 0
    { TexturedCube::create,
      "rgb-amg-reference.ktx",
      "RGB8 + Auto Mipmap"
    },
    { TexturedCube::create,
      "rgb-amg-reference.ktx",
      "RGB8 + Auto Mipmap"
    },
#endif
};

const uint32_t uNumSamples = sizeof(siSamples) / sizeof(VulkanLoadTests::sampleInvocation);

AppBaseSDL* theApp = new VulkanLoadTests(siSamples, uNumSamples,
                                     "KTX Loader Tests for Vulkan");