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

/*
 * Copyright 2018-2021 Mark Callow.
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @internal
 * @file
 * @~English
 *
 * @brief Base for samplesusing instancing such as array texture display.
 *
 * @author Mark Callow, github.com/MarkCallow.
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <algorithm>
#include <time.h>
#include <sstream>
#include <vector>
#include <ktx.h>

#include "argparser.h"
#include "InstancedSampleBase.h"
#include "TranscodeTargetStrToFmt.h"
#include "GLTextureTranscoder.hpp"
#include "ltexceptions.h"

#define member_size(type, member) sizeof(((type *)0)->member)

using namespace std;

const GLchar* InstancedSampleBase::pszInstancingFsDeclarations =
    "precision mediump float;\n"

    "in vec3 UVW;\n\n"

    "layout (location = 0) out vec4 outFragColor;\n\n";

const GLchar* InstancedSampleBase::pszSrgbEncodeFunc =
    "vec3 srgb_encode(vec3 color) {\n"
    "   float r = color.r < 0.0031308 ? 12.92 * color.r : 1.055 * pow(color.r, 1.0/2.4) - 0.055;\n"
    "   float g = color.g < 0.0031308 ? 12.92 * color.g : 1.055 * pow(color.g, 1.0/2.4) - 0.055;\n"
    "   float b = color.b < 0.0031308 ? 12.92 * color.b : 1.055 * pow(color.b, 1.0/2.4) - 0.055;\n"
    "   return vec3(r, g, b);\n"
    "}\n\n";

const GLchar* InstancedSampleBase::pszInstancingFsMain =
   "void main()\n"
    "{\n"
    "    outFragColor = texture(uSampler, UVW);\n"
    "}";

const GLchar* InstancedSampleBase::pszInstancingSrgbEncodeFsMain =
    "void main()\n"
    "{\n"
    "    vec4 t_color = texture(uSampler, UVW);\n"
    "    outFragColor.rgb = srgb_encode(t_color.rgb);\n"
    "    outFragColor.a = t_color.a;\n"
    "}";

const GLchar* InstancedSampleBase::pszInstancingVsDeclarations =
    "layout (location = 0) in vec4 inPos;\n"
    "layout (location = 1) in vec2 inUV;\n\n"

    "struct Instance\n"
    "{\n"
    "    mat4 model;\n"
    "};\n\n"

    "//layout (binding = 0) uniform UBO\n"
    "layout(std140) uniform UBO\n"
    "{\n"
    "    mat4 projection;\n"
    "    mat4 view;\n"
    "    Instance instance[INSTANCE_COUNT];\n"
    "} ubo;\n\n"

    "out vec3 UVW;\n\n";


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


/**
 * @internal
 * @class InstancedSampleBase
 * @~English
 *
 * @brief Test loading of 2D texture arrays.
 */
InstancedSampleBase::InstancedSampleBase(uint32_t width, uint32_t height,
                           const char* const szArgs,
                           const string sBasePath)
        : GL3LoadTestSample(width, height, szArgs, sBasePath),
          texUnit(GL_TEXTURE0), uniformBufferBindId(0),
          bInitialized(false)
{
    zoom = -15.0f;
    rotationSpeed = 0.25f;
    //rotation = glm::vec3(-15.0f, 35.0f, 0.0f);
    rotation = glm::vec3(15.0f, 35.0f, 0.0f);
    gnTexture = 0;
    // Ensure we're using the desired unit
    glActiveTexture(texUnit);

    processArgs(szArgs);

    KTX_error_code ktxresult;
    ktxTexture* kTexture;
    GLenum glerror;
    string ktxfilepath = externalFile ? ktxfilename
                                           : getAssetPath() + ktxfilename;
    ktxresult =
           ktxTexture_CreateFromNamedFile(ktxfilepath.c_str(),
                                          KTX_TEXTURE_CREATE_NO_FLAGS,
                                          &kTexture);
    if (KTX_SUCCESS != ktxresult) {
        std::stringstream message;
        
        message << "Creation of ktxTexture from \"" << ktxfilepath
                << "\" failed: " << ktxErrorString(ktxresult);
        throw std::runtime_error(message.str());
    }

    if (ktxTexture_NeedsTranscoding(kTexture)) {
        transcodeTarget = KTX_TTF_NOSELECTION;
        TextureTranscoder tc;
        tc.transcode((ktxTexture2*)kTexture, transcodeTarget);
    }

    ktxresult = ktxTexture_GLUpload(kTexture, &gnTexture, &texTarget,
                                    &glerror);
    
    if (KTX_SUCCESS != ktxresult) {
        std::stringstream message;
        
        message << "ktxTexture_GLUpload failed: ";
        if (ktxresult != KTX_GL_ERROR) {
             message << ktxErrorString(ktxresult);
             throw std::runtime_error(message.str());
        } else if (kTexture->isCompressed && glerror == GL_INVALID_ENUM) {
             throw unsupported_ctype();
        } else {
             message << std::showbase << "GL error " << std::hex << glerror
                    << " occurred.";
             throw std::runtime_error(message.str());
        }
    }

    if (kTexture->generateMipmaps) {
        // GLUpload will have generated the mipmaps already.
        uint32_t maxDim = (std::max)(
                    (std::max)(kTexture->baseWidth, kTexture->baseHeight),
                    kTexture->baseDepth);
        textureInfo.numLevels = (uint32_t)floor(log2(maxDim)) + 1;
    } else {
        textureInfo.numLevels = kTexture->numLevels;
    }
    textureInfo.numLayers = kTexture->numLayers;
    textureInfo.baseDepth = kTexture->baseDepth;
    if (textureInfo.numLevels > 1)
        bIsMipmapped = true;
    else
        bIsMipmapped = false;

    // Checking if KVData contains keys of interest would go here.
    
    ktxTexture_Destroy(kTexture);
}

InstancedSampleBase::~InstancedSampleBase()
{
    cleanup();
}

void
InstancedSampleBase::resize(uint32_t width, uint32_t height)
{
    this->w_width = width;
    this->w_height = height;

    glViewport(0, 0, width, height);
    updateUniformBufferMatrices();
}

void
InstancedSampleBase::run(uint32_t /*msTicks*/)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Keep these permanently bound
    //glBindVertexArray(gnVao);
    // Must be done after the VAO is bound
    //glBindBuffer(GL_ARRAY_BUFFER, gnVbo[0]);
    //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gnVbo[1]);
    glDrawElementsInstanced(GL_TRIANGLES, quad.indexCount,
                            GL_UNSIGNED_INT, (GLvoid*)quad.indicesOffset,
                            instanceCount);

    assert(GL_NO_ERROR == glGetError());
}

//===================================================================

void
InstancedSampleBase::processArgs(string sArgs)
{
    // Options descriptor
    struct argparser::option longopts[] = {
      {"external",         argparser::option::no_argument, &externalFile, 1},
      {"transcode-target", argparser::option::required_argument, nullptr, 2},
      {NULL,               argparser::option::no_argument, NULL,          0}
    };

    argvector argv(sArgs);
    argparser ap(argv);

    int ch;
    while ((ch = ap.getopt(nullptr, longopts, nullptr)) != -1) {
        switch (ch) {
            case 0: break;
          case 2:
            transcodeTarget = TranscodeTargetStrToFmt(ap.optarg);
            break;
          default: assert(false); // Error in args in sample table.
        }
    }
    assert(ap.optind < argv.size());
    ktxfilename = argv[ap.optind];
}

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

void
InstancedSampleBase::cleanup()
{
    glEnable(GL_DITHER);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glDisable(GL_DEPTH_TEST);
    if (bInitialized) {
        glUseProgram(0);
        glDeleteTextures(1, &gnTexture);
        glDeleteProgram(gnInstancingProg);
        glDeleteBuffers(2, quad.gnVbo);
        glDeleteVertexArrays(1, &quad.gnVao);
        delete uboVS.instance;
    }
    assert(GL_NO_ERROR == glGetError());
}

// Setup vertices for a single uv-mapped quad
void
InstancedSampleBase::generateQuad()
{
#define dim 2.5f
    //std::vector<TAVertex> vertices =
    TAVertex vertices[] =
    {
        { {  dim,  dim, 0.0f }, { 1.0f, 1.0f } },
        { { -dim,  dim, 0.0f }, { 0.0f, 1.0f } },
        { { -dim, -dim, 0.0f }, { 0.0f, 0.0f } },
        { {  dim, -dim, 0.0f }, { 1.0f, 0.0f } }
    };
#undef dim

    // Setup indices
    uint32_t indices[] =  { 0,1,2, 2,3,0 };
    quad.indexCount = static_cast<uint32_t>(ARRAY_LEN(indices));
    
    // Create a VAO and bind it.
    glGenVertexArrays(1, &quad.gnVao);
    glBindVertexArray(quad.gnVao);
    
    // Must have vertex data in buffer objects to use VAO's on ES3/GL Core
    glGenBuffers(2, quad.gnVbo);
    glBindBuffer(GL_ARRAY_BUFFER, quad.gnVbo[0]);
    // Must be done after the VAO is bound
    // WebGL requires different buffers for data and indices.
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad.gnVbo[1]);

    // Create the buffer data store.
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), NULL, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    GLsizeiptr offset = 0;
    glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(vertices), vertices);
    quad.verticesOffset = offset;
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TAVertex),
                          (GLvoid*)offset);
    offset += member_size(TAVertex, pos);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
                          sizeof(TAVertex), (GLvoid*)offset);
    offset = sizeof(vertices);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
                 indices, GL_STATIC_DRAW);
    quad.indicesOffset = 0;
}

#define _PAD16(nbytes) (ktx_uint32_t)(16 * ceilf((float)(nbytes) / 16))

void
InstancedSampleBase::prepareUniformBuffers()
{
    uProgramUniforms = glGetUniformBlockIndex(gnInstancingProg, "UBO");
    if (uProgramUniforms == -1) {
        std::stringstream message;
        
        message << "prepareUniformBuffers: UBO not found in program";
        throw std::runtime_error(message.str());
    }

    // INSTANCE_COUNT is set in GLSL code via define set in prepareProgram.
    //
    // Elements of the array of UboInstanceData will be aligned on 16-byte
    // boundaries per the std140 rule for mat4/vec4. _PAD16 is unnecessary
    // right now but will become so if anything is added to the ubo before
    // the UboInstanceData. _PAD16 is put here as a warning.
    uint32_t uboSize = _PAD16(sizeof(uboVS.matrices))
             + instanceCount * sizeof(UboInstanceData);
    uboVS.instance = new UboInstanceData[instanceCount];

    glGenBuffers(1, &gnUbo);
    glBindBuffer(GL_UNIFORM_BUFFER, gnUbo);
    // Create the data store.
    glBufferData(GL_UNIFORM_BUFFER, uboSize, 0, GL_DYNAMIC_DRAW);

    float offset = 1.5f;
    float center = (instanceCount * offset) / 2;
    for (uint32_t i = 0; i < instanceCount; i++)
    {
        // Instance model matrix
        uboVS.instance[i].model
           = glm::translate(glm::mat4(), glm::vec3(0.0f,
                                                   i * offset - center, 0.0f));
        uboVS.instance[i].model = glm::rotate(uboVS.instance[i].model,
                                              glm::radians(120.0f),
                                              glm::vec3(1.0f, 0.0f, 0.0f));
    }

    // Update instanced part of the uniform buffer
    // N.B. See comment re _PAD16 before uboSize above.
    uint32_t dataOffset = _PAD16(sizeof(uboVS.matrices));
    uint32_t dataSize = instanceCount * sizeof(UboInstanceData);
    glBufferSubData(GL_UNIFORM_BUFFER, dataOffset, dataSize, uboVS.instance);

    glBindBuffer(GL_UNIFORM_BUFFER, 0);
    glBindBufferBase(GL_UNIFORM_BUFFER, uniformBufferBindId, gnUbo);

    glUseProgram(gnInstancingProg);
    glUniformBlockBinding(gnInstancingProg, uProgramUniforms,
                          uniformBufferBindId);
    updateUniformBufferMatrices();
    glUseProgram(0);

    assert(glGetError() == GL_NO_ERROR);
}

void
InstancedSampleBase::updateUniformBufferMatrices()
{
    // Only updates the uniform buffer block part containing the global matrices

    // Projection
    uboVS.matrices.projection = glm::perspective(glm::radians(60.0f),
                                                 (float)w_width / w_height,
                                                 .01f, 256.f);

    // View
    uboVS.matrices.view = glm::translate(glm::mat4(),
                                         glm::vec3(0.0f, 1.0f, zoom));
    uboVS.matrices.view *= glm::translate(glm::mat4(), cameraPos);
    uboVS.matrices.view = glm::rotate(uboVS.matrices.view,
                                      glm::radians(rotation.x),
                                      glm::vec3(1.0f, 0.0f, 0.0f));
    uboVS.matrices.view = glm::rotate(uboVS.matrices.view,
                                      glm::radians(rotation.y),
                                      glm::vec3(0.0f, 1.0f, 0.0f));
    uboVS.matrices.view = glm::rotate(uboVS.matrices.view,
                                      glm::radians(rotation.z),
                                      glm::vec3(0.0f, 0.0f, 1.0f));

#if !defined(EMSCRIPTEN)
    // Only update the matrices part of the uniform buffer
    uint8_t *pData = (uint8_t*)glMapBufferRange(GL_UNIFORM_BUFFER, 0,
                                                sizeof(uboVS.matrices),
                                                GL_MAP_WRITE_BIT);
    memcpy(pData, &uboVS.matrices, sizeof(uboVS.matrices));
    glUnmapBuffer(GL_UNIFORM_BUFFER);
#else
    glBufferSubData(GL_UNIFORM_BUFFER, 0,
                    sizeof(uboVS.matrices), &uboVS.matrices);
#endif
}

void
InstancedSampleBase::prepareSampler()
{
    glBindTexture(texTarget, gnTexture);
    if (bIsMipmapped)
        glTexParameteri(texTarget,
                        GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    else
        glTexParameteri(texTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(texTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glBindTexture(texTarget, 0);

    glUseProgram(gnInstancingProg);
    if ((uSampler = glGetUniformLocation(gnInstancingProg,
                                              "uSampler")) == -1) {
        std::stringstream message;
        
        message << "prepareSampler: uSampler not found in program";
        throw std::runtime_error(message.str());
    }
    glUniform1i(uSampler, texUnit - GL_TEXTURE0);
    glUseProgram(0);

}

void
InstancedSampleBase::prepareProgram(ShaderSource& fs, ShaderSource& vs)
{
    GLuint gnInstancingFs, gnInstancingVs;

    try {
        std::stringstream ssDefine;
        ssDefine << "#define INSTANCE_COUNT " << instanceCount << "U" << endl;
        // str().c_str() doesn't work because str goes outof scope immediately.
        // Hence this 2 step process.
        string sDefine = ssDefine.str();
        vs.insert(vs.begin(), sDefine.c_str());
        makeShader(GL_VERTEX_SHADER, vs, &gnInstancingVs);
        makeShader(GL_FRAGMENT_SHADER, fs, &gnInstancingFs);
        makeProgram(gnInstancingVs, gnInstancingFs, &gnInstancingProg);
    } catch (std::exception& e) {
        (void)e; // To quiet unused variable warnings from some compilers.
        throw;
    }
    glDeleteShader(gnInstancingVs);
    glDeleteShader(gnInstancingFs);
}

void
InstancedSampleBase::prepare(ShaderSource& fs, ShaderSource& vs)
{
    // By default dithering is enabled. Dithering does not provide visual
    // improvement in this sample so disable it to improve performance.
    glDisable(GL_DITHER);

    glFrontFace(GL_CW);
    glDisable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.2f,0.3f,0.4f,1.0f);


    //prepareSamplerAndView();
    //setupVertexDescriptions();
    generateQuad();
    prepareProgram(fs, vs);
    prepareUniformBuffers();
    prepareSampler();

    glUseProgram(gnInstancingProg);
    glBindTexture(texTarget, gnTexture);
}