File size: 10,892 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 | /* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 sts expandtab: */
/*
* Copyright 2018-2020 Mark Callow.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Encode a texture then texture a cube with it, transcoding if necessary.
*
* This is used principally to check the encoders are properly linked on platforms where the ktx tools are
* unavailable and libktx is a static library.
*
* @author Mark Callow, github.com/MarkCallow.
*/
#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 "EncodeTexture.h"
#include "GLTextureTranscoder.hpp"
#include "TranscodeTargetStrToFmt.h"
#include "cube.h"
#include "argparser.h"
#include "ltexceptions.h"
/* ------------------------------------------------------------------------- */
extern const GLchar* pszVs;
extern const GLchar *pszDecalFs, *pszDecalSrgbEncodeFs;
/* ------------------------------------------------------------------------- */
LoadTestSample*
EncodeTexture::create(uint32_t width, uint32_t height,
const char* const szArgs,
const std::string sBasePath)
{
return new EncodeTexture(width, height, szArgs, sBasePath);
}
EncodeTexture::EncodeTexture(uint32_t width, uint32_t height,
const char* const szArgs,
const std::string sBasePath)
: GL3LoadTestSample(width, height, szArgs, sBasePath)
{
std::string filename;
GLenum target;
GLenum glerror;
GLuint gnDecalFs, gnVs;
GLsizeiptr offset;
ktxTexture2* kTexture;
KTX_error_code ktxresult;
bInitialized = GL_FALSE;
gnTexture = 0;
encodeTarget = EF_ETC1S;
transcodeTarget = KTX_TTF_NOSELECTION;
processArgs(szArgs);
filename = getAssetPath() + ktxfilename;
ktxresult = ktxTexture_CreateFromNamedFile(filename.c_str(),
KTX_TEXTURE_CREATE_NO_FLAGS,
(ktxTexture**)&kTexture);
if (KTX_SUCCESS != ktxresult) {
std::stringstream message;
message << "Creation of ktxTexture from \"" << filename
<< "\" failed: " << ktxErrorString(ktxresult);
throw std::runtime_error(message.str());
}
if (!kTexture->isCompressed) {
switch (encodeTarget) {
case EF_ASTC:
ktxresult = ktxTexture2_CompressAstc(kTexture, 0);
break;
case EF_ETC1S:
ktxresult = ktxTexture2_CompressBasis(kTexture, 0);
break;
case EF_UASTC:
{
ktxBasisParams params = { };
params.structSize = sizeof(params);
params.uastc = KTX_TRUE;
params.threadCount = 1;
ktxresult = ktxTexture2_CompressBasisEx(kTexture, ¶ms);
break;
}
}
if (KTX_SUCCESS != ktxresult) {
std::stringstream message;
message << "Encoding of ktxTexture2 to " << encodeTarget
<< " failed: " << ktxErrorString(ktxresult);
throw std::runtime_error(message.str());
}
}
if (ktxTexture2_NeedsTranscoding(kTexture)) {
TextureTranscoder tc;
tc.transcode((ktxTexture2*)kTexture);
}
ktxresult = ktxTexture_GLUpload(ktxTexture(kTexture), &gnTexture, &target,
&glerror);
if (KTX_SUCCESS == ktxresult) {
if (target != GL_TEXTURE_2D) {
/* Can only draw 2D textures */
std::stringstream message;
glDeleteTextures(1, &gnTexture);
message << "App can only draw 2D textures.";
throw std::runtime_error(message.str());
}
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);
ktxTexture_Destroy(ktxTexture(kTexture));
assert(GL_NO_ERROR == glGetError());
} else {
std::stringstream message;
message << "Load of texture from \"" << filename << "\" failed: ";
if (ktxresult == KTX_GL_ERROR && glerror == GL_INVALID_ENUM) {
throw unsupported_ctype();
} else {
if (ktxresult == KTX_GL_ERROR) {
message << std::showbase << "GL error " << std::hex << glerror
<< " occurred.";
} else {
message << ktxErrorString(ktxresult);
}
throw std::runtime_error(message.str());
}
}
// By default dithering is enabled. Dithering does not provide visual
// improvement in this sample so disable it to improve performance.
glDisable(GL_DITHER);
glEnable(GL_CULL_FACE);
glClearColor(0.2f,0.3f,0.4f,1.0f);
// Create a VAO and bind it.
glGenVertexArrays(1, &gnVao);
glBindVertexArray(gnVao);
// Must have vertex data in buffer objects to use VAO's on ES3/GL Core
glGenBuffers(2, gnVbo);
glBindBuffer(GL_ARRAY_BUFFER, gnVbo[0]);
// Must be done after the VAO is bound
// WebGL requires different buffers for data and indices.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gnVbo[1]);
// Create the buffer data store.
glBufferData(GL_ARRAY_BUFFER,
sizeof(cube_face) + sizeof(cube_color) + sizeof(cube_texture)
+ sizeof(cube_normal), NULL, GL_STATIC_DRAW);
// Interleave data copying and attrib pointer setup so offset is only
// computed once.
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
offset = 0;
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(cube_face), cube_face);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(cube_face);
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(cube_color), cube_color);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(cube_color);
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(cube_texture), cube_texture);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(cube_texture);
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(cube_normal), cube_normal);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)offset);
offset += sizeof(cube_normal);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_index_buffer),
cube_index_buffer, GL_STATIC_DRAW);
const GLchar* actualDecalFs;
if (framebufferColorEncoding() == GL_LINEAR) {
actualDecalFs = pszDecalSrgbEncodeFs;
} else {
actualDecalFs = pszDecalFs;
}
try {
makeShader(GL_VERTEX_SHADER, pszVs, &gnVs);
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(gnDecalFs);
assert(GL_NO_ERROR == glGetError());
bInitialized = GL_TRUE;
}
EncodeTexture::~EncodeTexture()
{
glEnable(GL_DITHER);
glDisable(GL_CULL_FACE);
if (bInitialized) {
glUseProgram(0);
glDeleteTextures(1, &gnTexture);
glDeleteProgram(gnTexProg);
glDeleteBuffers(2, gnVbo);
glDeleteVertexArrays(1, &gnVao);
}
assert(GL_NO_ERROR == glGetError());
}
/* ------------------------------------------------------------------------- */
void
EncodeTexture::processArgs(std::string sArgs)
{
// Options descriptor
struct argparser::option longopts[] = {
{"encode", argparser::option::required_argument, nullptr, 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 1:
if (!ap.optarg.compare("astc"))
encodeTarget = EF_ASTC;
else if (!ap.optarg.compare("etc1s"))
encodeTarget = EF_ETC1S;
else if (!ap.optarg.compare("uastc"))
encodeTarget = EF_UASTC;
else
assert(false && "Error in args in sample table");
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
EncodeTexture::resize(uint32_t uWidth, uint32_t uHeight)
{
glm::mat4 matProj;
glViewport(0, 0, uWidth, uHeight);
matProj = glm::perspective(glm::radians(45.f),
uWidth / (float)uHeight,
1.f, 100.f);
glUniformMatrix4fv(gulPMatrixLocTP, 1, GL_FALSE, glm::value_ptr(matProj));
}
void
EncodeTexture::run(uint32_t msTicks)
{
// Setup the view matrix : just turn around the cube.
const float fDistance = 5.0f;
glm::vec3 eye((float)cos( msTicks*0.001f ) * fDistance,
(float)sin( msTicks*0.0007f ) * fDistance,
(float)sin( msTicks*0.001f ) * fDistance);
glm::vec3 look(0.,0.,0.);
glm::vec3 up(0.,1.,0.);
glm::mat4 matView = glm::lookAt( eye, look, up );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(gulMvMatrixLocTP, 1, GL_FALSE, glm::value_ptr(matView));
glDrawElements(GL_TRIANGLES, CUBE_NUM_INDICES, GL_UNSIGNED_SHORT, 0);
assert(GL_NO_ERROR == glGetError());
}
/* ------------------------------------------------------------------------- */
|