procedural-3d-engine/external/ktx/lib/etcunpack.cxx

289 lines
8.3 KiB
C++
Raw Normal View History

Merge glTF branch (#747) * Added helper function for easy pipeline vertex input state create info structure setup from glTF model vertex class * Split glTF loader into header and implementation * Updated sample to use glTF * Removed collada files Replaced assets are now part of the asset pack * Return value for glTF model vertex input state create info helper * Removed unused assets * Use glTF assets * Added default material for glTF node's without materials * Use glTF assets * Apply pre-transforms to normals * Use glTF assets * Use glTF assets * Use vertex input state from glTF model class * Scene setup * Use glTF assets * Use glTF assets * Display error message and exit if glTF file could not be loaded * Use glTF assets * Use glTF assets * Use glTF assets * Remove unused buffer binds * Use glTF assets * Remove no longer used model files * Remove no longer used model files * Added support for rendering glTF models with images * glTF model normal pre-transform ignores translation * Use glTF assets * Use glTF assets * Use glTF assets * Use glTF assets * Use glTF assets * Only add combined image samplers to pool if actually used in the scene * Use global descriptor set layouts * Use global descriptor set layouts * Use glTF assets * Use glTF assets * Use glTF assets Code cleanup Updated GLSL and HLSL shaders * Use glTF assets Code cleanup * Use glTF assets Code cleanup Updated GLSL and HLSL shaders * Remove no-longer used model * Use glTF assets Code cleanup Updated GLSL and HLSL shaders * Use glTF assets Code cleanup Updated GLSL and HLSL shaders Removed no-longer used model * Use glTF assets Code cleanup Use RGBA texture instead of different compressed formats Removed no-longer used assets * Adnrdoid build file * Use glTF assets Code cleanup and refactoring Updated GLSL and HLSL shaders * Added vertex count and way of passing additional memory property type flags to glTF loader * Use glTF assets Updated GLSL and HLSL shaders Removed no-longer used assets * Use glTF assets Updated GLSL and HLSL shaders * Remove unfinished sample * Completely reworked push constants sample Use glTF assets Updated GLSL and HLSL shaders Removed no-longer used assets * Android CMake build files * Removed un-used asset * Explicit buffer binding function * Use glTF assets Code cleanup Updated GLSL and HLSL shaders * Use glTF assets Code cleanup * Use glTF assets Code cleanup Removed no-longer used assets * Use glTF assets Code cleanup Updated GLSL and HLSL shaders Removed no-longer used assets * Remove no-longer used asset * Use glTF assets Code cleanup and refactoring Performance optimizations Updated GLSL and HLSL shaders Removed no-longer used assets * Use glTF assets Code cleanup and refactoring Updated GLSL and HLSL shaders Removed no-longer used assets * Use glTF assets Code cleanup and refactoring Updated GLSL and HLSL shaders Removed no-longer used assets * Use glTF assets Code cleanup and refactoring Removed no-longer used assets * Use glTF assets Code cleanup and refactoring Removed no-longer used assets * Use glTF assets Code cleanup and refactoring * Use glTF assets Code cleanup and refactoring * Use glTF assets Code cleanup and refactoring Removed no-longer used assets * Pass vertex size and calculate multiplier in shaders instead of hard-coding With this, changes to the glTF vertex structure won't break the ray tracing samples * Load tangents (if present) * Use glTF assets Code cleanup and heavy refactoring Reworked debug display code * Android build * Normal mapping fixes Udpated HLSL shaders * Use glTF assets Code cleanup and heavy refactoring Reworked debug display code Updated GLSL and HLSL shaders * Code cleanup, comments * Use glTF assets Code cleanup and heavy refactoring Reworked debug display code Updated GLSL and HLSL shaders * Added sample count to framebuffer create info * Removed no-longer used assets * Android build Removed no-longer used assets * Code cleanup and heavy refactoring Updated GLSL and HLSL shaders Use tangents stored in GLSL instead of calculating them in the fragment shader * Renamed textured PBR sample main cpp file * Use glTF assets Code cleanup and refactoring Updated GLSL and HLSL shaders Removed no-longer used assets * Use glTF assets Removed no-longer used assets * Android build files * Android build files * Use glTF assets Removed no-longer used assets * Fixed HLSL shaders * Android build files * Use glTF assets Updated GLSL and HLSL shaders Removed no-longer used assets * Use glTF assets Updated GLSL and HLSL shaders Removed no-longer used assets * Added flag to disable glTF image loading Useful for samples that use their own textures or don't use textures at all to speed up loading * Use glTF assets Code cleanup Use Sponza scene instead of Sibenik to better highlight the effect Updated GLSL and HLSL shaders * Updated Android build files * Removed left-over comment * Use Sponza scene for the SSAO sample * Removed unused code * Removed ASSIMP No longer required as all samples now use the glTF file format * Added missing vertex shader stage * Removed old ASSIMP-based model loader * Added support for loading external glTF images from ktx Android fixes for loading external buffer files * Scene setup * Added missing shader stages * Removed ASSIMP from build files * Fixed compiler warning * Removed ASSIMP from readmes * Android build files cleanup * Replaced ktx submodule with only the files required for this repo The ktx submodule was a tad too big and contained lots of files not required for this repo * Moved ktx build files into base project * Use glTF assets * Use glTF assets * Removed license files, will be moved to asset pack * Use RGBA textures * Use RGBA cubemp texture with face assignment based on original images Refs #679 * Android build files * Removed textures All textures will be moved to the asset pack * Ignore asset folders * Removed font Fonts will be moved to the asset pack * Link to gltf asset pack * Updated gitignore * Android build file
2020-07-28 20:20:38 +02:00
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4: */
/* $Id: 764e83ee5c8febe3233114f9ebe9a9ef21d120f1 $ */
/*
* ©2010 The khronos Group, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* @internal
* @~English
* @file
*
* Unpack a texture compressed with ETC1
*
* @author Mark Callow, HI Corporation.
*/
#include <assert.h>
#include <stdlib.h>
#include "GL/glcorearb.h"
// Not defined in glcorearb.h.
#define GL_ETC1_RGB8_OES 0x8D64
#include "ktx.h"
#include "ktxint.h"
#if SUPPORT_SOFTWARE_ETC_UNPACK
typedef unsigned int uint;
typedef unsigned char uint8;
extern void decompressBlockETC2c(uint block_part1, uint block_part2, uint8* img,
int width, int height, int startx, int starty, int channels);
extern void decompressBlockETC21BitAlphaC(uint block_part1, uint block_part2, uint8* img, uint8* alphaimg,
int width, int height, int startx, int starty, int channels);
extern void decompressBlockAlphaC(uint8* data, uint8* img,
int width, int height, int startx, int starty, int channels);
extern void decompressBlockAlpha16bitC(uint8* data, uint8* img,
int width, int height, int startx, int starty, int channels);
extern void setupAlphaTable();
// This global variable affects the behaviour of decompressBlockAlpha16bitC.
extern int formatSigned;
static void
readBigEndian4byteWord(ktx_uint32_t* pBlock, const GLubyte *s)
{
*pBlock = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
}
/* Unpack an ETC1_RGB8_OES format compressed texture */
extern "C" KTX_error_code
_ktxUnpackETC(const GLubyte* srcETC, const GLenum srcFormat,
ktx_uint32_t activeWidth, ktx_uint32_t activeHeight,
GLubyte** dstImage,
GLenum* format, GLenum* internalFormat, GLenum* type,
GLint R16Formats, GLboolean supportsSRGB)
{
unsigned int width, height;
unsigned int block_part1, block_part2;
unsigned int x, y;
/*const*/ GLubyte* src = (GLubyte*)srcETC;
// AF_11BIT is used to compress R11 & RG11 though its not alpha data.
enum {AF_NONE, AF_1BIT, AF_8BIT, AF_11BIT} alphaFormat = AF_NONE;
int dstChannels, dstChannelBytes;
switch (srcFormat) {
case GL_COMPRESSED_SIGNED_R11_EAC:
if (R16Formats & _KTX_R16_FORMATS_SNORM) {
dstChannelBytes = sizeof(GLshort);
dstChannels = 1;
formatSigned = GL_TRUE;
*internalFormat = GL_R16_SNORM;
*format = GL_RED;
*type = GL_SHORT;
alphaFormat = AF_11BIT;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
case GL_COMPRESSED_R11_EAC:
if (R16Formats & _KTX_R16_FORMATS_NORM) {
dstChannelBytes = sizeof(GLshort);
dstChannels = 1;
formatSigned = GL_FALSE;
*internalFormat = GL_R16;
*format = GL_RED;
*type = GL_UNSIGNED_SHORT;
alphaFormat = AF_11BIT;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
case GL_COMPRESSED_SIGNED_RG11_EAC:
if (R16Formats & _KTX_R16_FORMATS_SNORM) {
dstChannelBytes = sizeof(GLshort);
dstChannels = 2;
formatSigned = GL_TRUE;
*internalFormat = GL_RG16_SNORM;
*format = GL_RG;
*type = GL_SHORT;
alphaFormat = AF_11BIT;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
case GL_COMPRESSED_RG11_EAC:
if (R16Formats & _KTX_R16_FORMATS_NORM) {
dstChannelBytes = sizeof(GLshort);
dstChannels = 2;
formatSigned = GL_FALSE;
*internalFormat = GL_RG16;
*format = GL_RG;
*type = GL_UNSIGNED_SHORT;
alphaFormat = AF_11BIT;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
case GL_ETC1_RGB8_OES:
case GL_COMPRESSED_RGB8_ETC2:
dstChannelBytes = sizeof(GLubyte);
dstChannels = 3;
*internalFormat = GL_RGB8;
*format = GL_RGB;
*type = GL_UNSIGNED_BYTE;
break;
case GL_COMPRESSED_RGBA8_ETC2_EAC:
dstChannelBytes = sizeof(GLubyte);
dstChannels = 4;
*internalFormat = GL_RGBA8;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
alphaFormat = AF_8BIT;
break;
case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
dstChannelBytes = sizeof(GLubyte);
dstChannels = 4;
*internalFormat = GL_RGBA8;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
alphaFormat = AF_1BIT;
break;
case GL_COMPRESSED_SRGB8_ETC2:
if (supportsSRGB) {
dstChannelBytes = sizeof(GLubyte);
dstChannels = 3;
*internalFormat = GL_SRGB8;
*format = GL_RGB;
*type = GL_UNSIGNED_BYTE;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
if (supportsSRGB) {
dstChannelBytes = sizeof(GLubyte);
dstChannels = 4;
*internalFormat = GL_SRGB8_ALPHA8;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
alphaFormat = AF_8BIT;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
if (supportsSRGB) {
dstChannelBytes = sizeof(GLubyte);
dstChannels = 4;
*internalFormat = GL_SRGB8_ALPHA8;
*format = GL_RGBA;
*type = GL_UNSIGNED_BYTE;
alphaFormat = AF_1BIT;
} else
return KTX_UNSUPPORTED_TEXTURE_TYPE;
break;
default:
assert(0); // Upper levels should pass only one of the above srcFormats.
return KTX_UNSUPPORTED_TEXTURE_TYPE; // For Release configurations.
}
/* active_{width,height} show how many pixels contain active data,
* (the rest are just for making sure we have a 2*a x 4*b size).
*/
/* Compute the full width & height. */
width = ((activeWidth+3)/4)*4;
height = ((activeHeight+3)/4)*4;
/* printf("Width = %d, Height = %d\n", width, height); */
/* printf("active pixel area: top left %d x %d area.\n", activeWidth, activeHeight); */
*dstImage = (GLubyte*)malloc(dstChannels*dstChannelBytes*width*height);
if (!*dstImage) {
return KTX_OUT_OF_MEMORY;
}
if (alphaFormat != AF_NONE)
setupAlphaTable();
// NOTE: none of the decompress functions actually use the <height> parameter
if (alphaFormat == AF_11BIT) {
// One or two 11-bit alpha channels for R or RG.
for (y=0; y < height/4; y++) {
for (x=0; x < width/4; x++) {
decompressBlockAlpha16bitC(src, *dstImage, width, height, 4*x, 4*y, dstChannels);
src += 8;
if (srcFormat == GL_COMPRESSED_RG11_EAC || srcFormat == GL_COMPRESSED_SIGNED_RG11_EAC) {
decompressBlockAlpha16bitC(src, *dstImage + dstChannelBytes, width, height, 4*x, 4*y, dstChannels);
src += 8;
}
}
}
} else {
for (y=0; y < height/4; y++) {
for (x=0; x < width/4; x++) {
// Decode alpha channel for RGBA
if (alphaFormat == AF_8BIT) {
decompressBlockAlphaC(src, *dstImage + 3, width, height, 4*x, 4*y, dstChannels);
src += 8;
}
// Decode color dstChannels
readBigEndian4byteWord(&block_part1, src);
src += 4;
readBigEndian4byteWord(&block_part2, src);
src += 4;
if (alphaFormat == AF_1BIT)
decompressBlockETC21BitAlphaC(block_part1, block_part2, *dstImage, 0, width, height, 4*x, 4*y, dstChannels);
else
decompressBlockETC2c(block_part1, block_part2, *dstImage, width, height, 4*x, 4*y, dstChannels);
}
}
}
/* Ok, now write out the active pixels to the destination image.
* (But only if the active pixels differ from the total pixels)
*/
if( !(height == activeHeight && width == activeWidth) ) {
int dstPixelBytes = dstChannels * dstChannelBytes;
int dstRowBytes = dstPixelBytes * width;
int activeRowBytes = activeWidth * dstPixelBytes;
GLubyte *newimg = (GLubyte*)malloc(dstPixelBytes * activeWidth * activeHeight);
unsigned int xx, yy;
int zz;
if (!newimg) {
free(*dstImage);
return KTX_OUT_OF_MEMORY;
}
/* Convert from total area to active area: */
for (yy = 0; yy < activeHeight; yy++) {
for (xx = 0; xx < activeWidth; xx++) {
for (zz = 0; zz < dstPixelBytes; zz++) {
newimg[ yy*activeRowBytes + xx*dstPixelBytes + zz ] = (*dstImage)[ yy*dstRowBytes + xx*dstPixelBytes + zz];
}
}
}
free(*dstImage);
*dstImage = newimg;
}
return KTX_SUCCESS;
}
#endif /* SUPPORT_SOFTWARE_ETC_UNPACK */