File size: 16,302 Bytes
dec7e7d | 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 | /* -*- tab-iWidth: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */
/*
* Copyright 2018-2020 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Tests texture loading by using glDrawTexture to draw the texture.
*
* @author Mark Callow
*/
#if defined(_WIN32)
#if _MSC_VER < 1900
#define snprintf _snprintf
#endif
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <assert.h>
#include <sstream>
#include <ktx.h>
#include "disable_glm_warnings.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "reenable_warnings.h"
#include "DrawTexture.h"
#include "GLTextureTranscoder.hpp"
#include "TranscodeTargetStrToFmt.h"
#include "frame.h"
#include "quad.h"
#include "argparser.h"
#include "ltexceptions.h"
#if !defined(GL_TEXTURE_1D)
#define GL_TEXTURE_1D 0x0DE0
#endif
/* ------------------------------------------------------------------------- */
extern const GLchar* pszVs;
extern const GLchar *pszDecalFs, *pszDecalSrgbEncodeFs;
extern const GLchar *pszColorFs, *pszColorSrgbEncodeFs;
/* ------------------------------------------------------------------------- */
LoadTestSample*
DrawTexture::create(uint32_t width, uint32_t height,
const char* const szArgs,
const std::string sBasePath)
{
return new DrawTexture(width, height, szArgs, sBasePath);
}
DrawTexture::DrawTexture(uint32_t width, uint32_t height,
const char* const szArgs,
const std::string sBasePath)
: GL3LoadTestSample(width, height, szArgs, sBasePath)
{
GLfloat* pfQuadTexCoords = quad_texture;
GLfloat fTmpTexCoords[sizeof(quad_texture)/sizeof(GLfloat)];
GLenum target;
GLenum glerror;
GLint sign_s = 1, sign_t = 1;
GLint i;
GLuint gnColorFs, gnDecalFs, gnVs;
GLsizeiptr offset;
ktxTexture* kTexture;
KTX_error_code ktxresult;
bInitialized = false;
transcodeTarget = KTX_TTF_NOSELECTION;
gnTexture = 0;
processArgs(szArgs);
std::string ktxfilepath = externalFile ? ktxfilename
: getAssetPath() + ktxfilename;
ktxresult = ktxTexture_CreateFromNamedFile(ktxfilepath.c_str(),
preloadImages ? KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT
: 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)) {
TextureTranscoder tc;
tc.transcode((ktxTexture2*)kTexture, transcodeTarget);
}
ktxresult = ktxTexture_GLUpload(kTexture, &gnTexture, &target, &glerror);
if (KTX_SUCCESS == ktxresult) {
// GLUpload won't set target to GL_TEXTURE_1D not supported by context.
if (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D) {
/* Can only draw 1D & 2D textures */
std::stringstream message;
glDeleteTextures(1, &gnTexture);
message << "DrawTexture supports only 1D & 2D textures. \""
<< ktxfilename << "\" is not one of these.";
throw std::runtime_error(message.str());
}
if (kTexture->orientation.x == KTX_ORIENT_X_LEFT)
sign_s = -1;
if (kTexture->orientation.y == KTX_ORIENT_Y_DOWN)
sign_t = -1;
if (sign_s < 0 || sign_t < 0) {
// Transform the texture coordinates to get correct image
// orientation.
int iNumCoords = sizeof(quad_texture) / sizeof(float);
for (i = 0; i < iNumCoords; i++) {
fTmpTexCoords[i] = quad_texture[i];
if (i & 1) { // odd, i.e. a y coordinate
if (sign_t < 1) {
fTmpTexCoords[i] = fTmpTexCoords[i] * -1 + 1;
}
} else { // an x coordinate
if (sign_s < 1) {
fTmpTexCoords[i] = fTmpTexCoords[i] * -1 + 1;
}
}
}
pfQuadTexCoords = fTmpTexCoords;
}
uTexWidth = kTexture->baseWidth;
uTexHeight = kTexture->baseHeight;
if (kTexture->numLevels > 1)
// Enable bilinear mipmapping.
// TO DO: application can consider inserting a key,value pair in
// the KTX file that indicates what type of filtering to use.
glTexParameteri(target,
GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
else
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ktx_uint32_t swizzleLen;
char* swizzleStr;
ktxresult = ktxHashList_FindValue(&kTexture->kvDataHead, KTX_SWIZZLE_KEY,
&swizzleLen, (void**)&swizzleStr);
if (ktxresult == KTX_SUCCESS && swizzleLen == 5) {
if (contextSupportsSwizzle()) {
GLint swizzle[4];
swizzle[0] = swizzleStr[0] == 'r' ? GL_RED
: swizzleStr[0] == 'g' ? GL_GREEN
: swizzleStr[0] == 'b' ? GL_BLUE
: swizzleStr[0] == 'a' ? GL_ALPHA
: swizzleStr[0] == '0' ? GL_ZERO
: GL_ONE;
swizzle[1] = swizzleStr[1] == 'r' ? GL_RED
: swizzleStr[1] == 'g' ? GL_GREEN
: swizzleStr[1] == 'b' ? GL_BLUE
: swizzleStr[1] == 'a' ? GL_ALPHA
: swizzleStr[1] == '0' ? GL_ZERO
: GL_ONE;
swizzle[2] = swizzleStr[2] == 'r' ? GL_RED
: swizzleStr[2] == 'g' ? GL_GREEN
: swizzleStr[2] == 'b' ? GL_BLUE
: swizzleStr[2] == 'a' ? GL_ALPHA
: swizzleStr[2] == '2' ? GL_ZERO
: GL_ONE;
swizzle[3] = swizzleStr[3] == 'r' ? GL_RED
: swizzleStr[3] == 'g' ? GL_GREEN
: swizzleStr[3] == 'b' ? GL_BLUE
: swizzleStr[3] == 'a' ? GL_ALPHA
: swizzleStr[3] == '3' ? GL_ZERO
: GL_ONE;
glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, swizzle[0]);
glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, swizzle[1]);
glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, swizzle[2]);
glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, swizzle[3]);
} else {
std::stringstream message;
message << "Input file has swizzle metadata but the "
<< "GL context does not support swizzling.";
// I have absolutely no idea why the following line makes clang
// raise an error about no matching conversion from
// std::__1::basic_stringstream to unsupported_ttype
// so I've resorted to using a temporary variable.
//throw(unsupported_ttype(message.str());
std::string msg = message.str();
throw(unsupported_ttype(msg));
}
}
assert(GL_NO_ERROR == glGetError());
ktxTexture_Destroy(kTexture);
} else {
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
// Emscripten/WebGL returns INVALID_VALUE for unsupported
// ETC formats.
&& (glerror == GL_INVALID_ENUM || glerror == GL_INVALID_VALUE)) {
throw unsupported_ctype();
} else {
message << std::showbase << "GL error " << std::hex << glerror
<< " occurred.";
throw std::runtime_error(message.str());
}
}
glClearColor(0.4f, 0.4f, 0.5f, 1.0f);
// Must have vertex data in buffer objects to use VAO's on ES3/GL Core
glGenBuffers(1, &gnVbo);
glBindBuffer(GL_ARRAY_BUFFER, gnVbo);
// Create the buffer data store
glBufferData(GL_ARRAY_BUFFER,
sizeof(frame_position) + sizeof(frame_color)
+ sizeof(quad_position) + sizeof(quad_color)
+ sizeof(quad_texture),
NULL, GL_STATIC_DRAW);
glGenVertexArrays(2, gnVaos);
// Interleave data copying and attrib pointer setup so offset is only
// computed once.
// Setup VAO and buffer the data for frame
glBindVertexArray(gnVaos[FRAME]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
offset = 0;
glBufferSubData(GL_ARRAY_BUFFER, offset,
sizeof(frame_position), frame_position);
glVertexAttribPointer(0, 3, GL_BYTE, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(frame_position);
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(frame_color), frame_color);
glVertexAttribPointer(1, 3, GL_BYTE, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(frame_color);
// Setup VAO for quad
glBindVertexArray(gnVaos[QUAD]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBufferSubData(GL_ARRAY_BUFFER, offset,
sizeof(quad_position), quad_position);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(quad_position);
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(quad_color), quad_color);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(quad_color);
glBufferSubData(GL_ARRAY_BUFFER, offset,
sizeof(quad_texture), pfQuadTexCoords);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
glBindVertexArray(0);
const GLchar *actualColorFs, *actualDecalFs;
if (framebufferColorEncoding() == GL_LINEAR) {
actualColorFs = pszColorSrgbEncodeFs;
actualDecalFs = pszDecalSrgbEncodeFs;
} else {
actualColorFs = pszColorFs;
actualDecalFs = pszDecalFs;
}
try {
makeShader(GL_VERTEX_SHADER, pszVs, &gnVs);
makeShader(GL_FRAGMENT_SHADER, actualColorFs, &gnColorFs);
makeProgram(gnVs, gnColorFs, &gnColProg);
gulMvMatrixLocCP = glGetUniformLocation(gnColProg, "mvmatrix");
gulPMatrixLocCP = glGetUniformLocation(gnColProg, "pmatrix");
makeShader(GL_FRAGMENT_SHADER, actualDecalFs, &gnDecalFs);
makeProgram(gnVs, gnDecalFs, &gnTexProg);
} catch (std::exception& e) {
(void)e; // To quiet unused variable warnings from some compilers.
throw;
}
gulMvMatrixLocTP = glGetUniformLocation(gnTexProg, "mvmatrix");
gulPMatrixLocTP = glGetUniformLocation(gnTexProg, "pmatrix");
gulSamplerLocTP = glGetUniformLocation(gnTexProg, "sampler");
glUseProgram(gnTexProg);
// We're using the default texture unit 0
glUniform1i(gulSamplerLocTP, 0);
glDeleteShader(gnVs);
glDeleteShader(gnColorFs);
glDeleteShader(gnDecalFs);
// Set the quad's mv matrix to scale by the texture size.
// With the pixel-mapping ortho projection set below, the texture will
// be rendered at actual size just like DrawTex*OES.
quadMvMatrix = glm::scale(glm::mat4(),
glm::vec3((float)uTexWidth / 2,
(float)uTexHeight / 2,
1));
assert(GL_NO_ERROR == glGetError());
bInitialized = true;
}
/* ------------------------------------------------------------------------- */
DrawTexture::~DrawTexture()
{
if (bInitialized) {
// A bug in the PVR SDK 3.1 emulator causes the
// glDeleteProgram(gnColProg) below to raise an INVALID_VALUE error
// if the following glUseProgram(0) has been executed. Strangely the
// equivalent line in TexturedCube.cpp, where only 1 program is used,
// does not raise an error.
glUseProgram(0);
glDeleteTextures(1, &gnTexture);
glDeleteProgram(gnTexProg);
glDeleteProgram(gnColProg);
glDeleteBuffers(1, &gnVbo);
glDeleteVertexArrays(2, gnVaos);
}
assert(GL_NO_ERROR == glGetError());
}
/* ------------------------------------------------------------------------- */
void
DrawTexture::processArgs(std::string sArgs)
{
// Options descriptor
struct argparser::option longopts[] = {
{"external", argparser::option::no_argument, &externalFile, 1},
{"preload", argparser::option::no_argument, &preloadImages, 1},
{"transcode-target", argparser::option::required_argument, nullptr, 2},
{NULL, argparser::option::no_argument, nullptr, 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
DrawTexture::resize(uint32_t uNewWidth, uint32_t uNewHeight)
{
glViewport(0, 0, uNewWidth, uNewHeight);
this->uWidth = uNewWidth;
this->uHeight = uNewHeight;
// Set up an orthographic projection where 1 = 1 pixel, and 0,0,0
// is at the center of the window.
//atSetOrthoZeroAtCenterMatrix(fPMatrix,
// 0.0f, (float)iWidth,
// 0.0f, (float)iHeight,
// -1.0f, 1.0f);
// Set up an orthographic projection where 1 = 1 pixel
pMatrix = glm::ortho(0.f, (float)uWidth, 0.f, (float)uHeight);
// Move (0,0,0) to the center of the window.
pMatrix *= glm::translate(glm::mat4(),
glm::vec3((float)uWidth/2, (float)uHeight/2, 0));
// Scale the frame to fill the viewport. To guarantee its lines
// appear we need to inset them by half-a-pixel hence the -1.
// [Lines at the edges of the clip volume may or may not appear
// depending on the OpenGL ES implementation. This is because
// (a) the edges are on the points of the diamonds of the diamond
// exit rule and slight precision errors can easily push the
// lines outside the diamonds.
// (b) the specification allows lines to be up to 1 pixel either
// side of the exact position.]
frameMvMatrix = glm::scale(glm::mat4(),
glm::vec3((float)(uWidth - 1)/2,
(float)(uHeight - 1)/2,
1));
}
/* ------------------------------------------------------------------------- */
void
DrawTexture::run(uint32_t /*msTicks*/)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(gnVaos[FRAME]);
glUseProgram(gnColProg);
glUniformMatrix4fv(gulMvMatrixLocCP, 1, GL_FALSE,
glm::value_ptr(frameMvMatrix));
glUniformMatrix4fv(gulPMatrixLocCP, 1, GL_FALSE, glm::value_ptr(pMatrix));
glDrawArrays(GL_LINE_LOOP, 0, 4);
glBindVertexArray(gnVaos[QUAD]);
glUseProgram(gnTexProg);
glUniformMatrix4fv(gulMvMatrixLocTP, 1, GL_FALSE,
glm::value_ptr(quadMvMatrix));
glUniformMatrix4fv(gulPMatrixLocTP, 1, GL_FALSE, glm::value_ptr(pMatrix));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
assert(GL_NO_ERROR == glGetError());
}
/* ------------------------------------------------------------------------- */
|