mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
add support for ETC2_EAC in .ktx files
This commit is contained in:
@@ -22,7 +22,8 @@ include_directories("."
|
||||
"Console"
|
||||
"McZapkie"
|
||||
"gl"
|
||||
"ref/glad/include")
|
||||
"ref/glad/include"
|
||||
"ref/dds-ktx/include")
|
||||
|
||||
file(GLOB HEADERS "*.h"
|
||||
"Console/*.h"
|
||||
@@ -158,6 +159,7 @@ set(SOURCES
|
||||
"widgets/perfgraphs.cpp"
|
||||
|
||||
"ref/glad/src/glad.c"
|
||||
"ref/dds-ktx/src/dds-ktx.c"
|
||||
|
||||
"gl/shader.cpp"
|
||||
"gl/vao.cpp"
|
||||
|
||||
@@ -64,7 +64,7 @@ TextureTest( std::string const &Name ) {
|
||||
auto const lookup {
|
||||
FileExists(
|
||||
{ Global.asCurrentTexturePath + Name, Name, szTexturePath + Name },
|
||||
{ ".mat", ".dds", ".tga", ".png", ".bmp", ".jpg", ".tex" } ) };
|
||||
{ ".mat", ".dds", ".tga", ".ktx", ".png", ".bmp", ".jpg", ".tex" } ) };
|
||||
|
||||
return ( lookup.first + lookup.second );
|
||||
}
|
||||
|
||||
86
Texture.cpp
86
Texture.cpp
@@ -26,6 +26,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "flip-s3tc.h"
|
||||
#include "stb/stb_image.h"
|
||||
#include <png.h>
|
||||
#include "dds-ktx/dds-ktx.h"
|
||||
|
||||
#define EU07_DEFERRED_TEXTURE_UPLOAD
|
||||
|
||||
@@ -34,6 +35,8 @@ GLint opengl_texture::m_activeunit = -1;
|
||||
|
||||
std::unordered_map<GLint, int> opengl_texture::precompressed_formats =
|
||||
{
|
||||
{ GL_COMPRESSED_RGBA8_ETC2_EAC, 16 },
|
||||
{ GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, 16 },
|
||||
{ GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 8 },
|
||||
{ GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 16 },
|
||||
{ GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 16 },
|
||||
@@ -55,6 +58,12 @@ std::unordered_map<GLint, GLint> opengl_texture::drivercompressed_formats =
|
||||
std::unordered_map<GLint, std::unordered_map<GLint, GLint>> opengl_texture::mapping =
|
||||
{
|
||||
// image have, material wants, gl internalformat
|
||||
{ GL_COMPRESSED_RGBA8_ETC2_EAC , { { GL_SRGB_ALPHA, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
|
||||
{ GL_SRGB, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC },
|
||||
{ GL_RGBA, GL_COMPRESSED_RGBA8_ETC2_EAC },
|
||||
{ GL_RGB, GL_COMPRESSED_RGBA8_ETC2_EAC },
|
||||
{ GL_RG, GL_COMPRESSED_RGBA8_ETC2_EAC },
|
||||
{ GL_RED, GL_COMPRESSED_RGBA8_ETC2_EAC } } },
|
||||
{ GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, { { GL_SRGB_ALPHA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT },
|
||||
{ GL_SRGB, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT },
|
||||
{ GL_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT },
|
||||
@@ -109,21 +118,15 @@ texture_manager::texture_manager() {
|
||||
// required for GLES, on desktop GL it will be done by driver
|
||||
void opengl_texture::gles_match_internalformat(GLuint internalformat)
|
||||
{
|
||||
// ignore compressed formats (and hope that GLES driver will support it)
|
||||
if (precompressed_formats.find(internalformat) != precompressed_formats.end())
|
||||
return;
|
||||
|
||||
// don't care about sRGB here
|
||||
if (internalformat == GL_SRGB8)
|
||||
internalformat = GL_RGB8;
|
||||
if (internalformat == GL_SRGB8_ALPHA8)
|
||||
internalformat = GL_RGBA8;
|
||||
if (internalformat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT)
|
||||
internalformat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
|
||||
if (internalformat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT)
|
||||
internalformat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
|
||||
if (internalformat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT)
|
||||
internalformat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
|
||||
// ignore compressed formats (and hope that GLES driver will support it)
|
||||
if (precompressed_formats.find(internalformat) != precompressed_formats.end())
|
||||
return;
|
||||
|
||||
// we don't want BGR(A), reverse it
|
||||
if (data_format == GL_BGR)
|
||||
@@ -250,6 +253,7 @@ opengl_texture::load() {
|
||||
if( type == ".dds" ) { load_DDS(); }
|
||||
else if( type == ".tga" ) { load_TGA(); }
|
||||
else if( type == ".png" ) { load_PNG(); }
|
||||
else if( type == ".ktx" ) { load_KTX(); }
|
||||
else if( type == ".bmp" ) { load_STBI(); }
|
||||
else if( type == ".jpg" ) { load_STBI(); }
|
||||
else if( type == ".tex" ) { load_TEX(); }
|
||||
@@ -592,6 +596,66 @@ opengl_texture::load_DDS() {
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
opengl_texture::load_KTX() {
|
||||
std::ifstream file( name + type, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
|
||||
std::size_t filesize = static_cast<size_t>(file.tellg()); // ios::ate already positioned us at the end of the file
|
||||
file.seekg( 0, std::ios::beg ); // rewind the caret afterwards
|
||||
|
||||
std::vector<char> filecontent;
|
||||
filecontent.resize(filesize);
|
||||
file.read(filecontent.data(), filecontent.size());
|
||||
|
||||
ddsktx_texture_info info;
|
||||
if (!ddsktx_parse(&info, filecontent.data(), filecontent.size(), nullptr)) {
|
||||
ErrorLog("Bad texture: KTX parsing failed", logtype::texture);
|
||||
data_state = resource_state::failed;
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.format != DDSKTX_FORMAT_ETC2A) {
|
||||
ErrorLog("Bad texture: currently unsupported KTX type", logtype::texture);
|
||||
data_state = resource_state::failed;
|
||||
return;
|
||||
}
|
||||
|
||||
data_format = GL_COMPRESSED_RGBA8_ETC2_EAC;
|
||||
data_components = GL_RGBA;
|
||||
data_mapcount = info.num_mips;
|
||||
|
||||
bool started = false;
|
||||
|
||||
for (int level = 0; level < info.num_mips; level++) {
|
||||
ddsktx_sub_data sub_data;
|
||||
ddsktx_get_sub(&info, &sub_data, filecontent.data(), filecontent.size(), 0, 0, level);
|
||||
|
||||
if (!started) {
|
||||
data_width = sub_data.width;
|
||||
data_height = sub_data.height;
|
||||
|
||||
if( ( data_width > Global.CurrentMaxTextureSize ) || ( data_height > Global.CurrentMaxTextureSize ) ) {
|
||||
data_mapcount--;
|
||||
continue;
|
||||
}
|
||||
|
||||
started = true;
|
||||
}
|
||||
|
||||
size_t data_offset = data.size();
|
||||
data.resize(data.size() + sub_data.size_bytes);
|
||||
memcpy(data.data() + data_offset, sub_data.buff, sub_data.size_bytes);
|
||||
}
|
||||
|
||||
if (!data_mapcount) {
|
||||
// there's a chance we've discarded the provided mipmap(s) as too large
|
||||
WriteLog( "Texture \"" + name + "\" has no mipmaps which can fit currently set texture pixelcount limits." );
|
||||
data_state = resource_state::failed;
|
||||
return;
|
||||
}
|
||||
|
||||
data_state = resource_state::good;
|
||||
}
|
||||
|
||||
void
|
||||
opengl_texture::load_TEX() {
|
||||
|
||||
@@ -1403,7 +1467,7 @@ texture_manager::find_on_disk( std::string const &Texturename ) const {
|
||||
return (
|
||||
FileExists(
|
||||
filenames,
|
||||
{ ".dds", ".tga", ".png", ".bmp", ".jpg", ".tex" } ) );
|
||||
{ ".dds", ".tga", ".ktx", ".png", ".bmp", ".jpg", ".tex" } ) );
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
21
Texture.h
21
Texture.h
@@ -16,10 +16,10 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "gl/ubo.h"
|
||||
|
||||
struct opengl_texture {
|
||||
static DDSURFACEDESC2 deserialize_ddsd(std::istream&);
|
||||
static DDCOLORKEY deserialize_ddck(std::istream&);
|
||||
static DDPIXELFORMAT deserialize_ddpf(std::istream&);
|
||||
static DDSCAPS2 deserialize_ddscaps(std::istream&);
|
||||
static DDSURFACEDESC2 deserialize_ddsd(std::istream&);
|
||||
static DDCOLORKEY deserialize_ddck(std::istream&);
|
||||
static DDPIXELFORMAT deserialize_ddpf(std::istream&);
|
||||
static DDSCAPS2 deserialize_ddscaps(std::istream&);
|
||||
|
||||
// constructors
|
||||
opengl_texture() = default;
|
||||
@@ -68,17 +68,18 @@ struct opengl_texture {
|
||||
std::size_t size{ 0 }; // size of the texture data, in kb
|
||||
GLint components_hint = 0; // components that material wants
|
||||
|
||||
GLenum target = GL_TEXTURE_2D;
|
||||
GLenum target = GL_TEXTURE_2D;
|
||||
static std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> units;
|
||||
static GLint m_activeunit;
|
||||
|
||||
private:
|
||||
// methods
|
||||
void make_request();
|
||||
void load_PNG();
|
||||
void make_request();
|
||||
void load_PNG();
|
||||
void load_DDS();
|
||||
void load_KTX();
|
||||
void load_TEX();
|
||||
void load_STBI();
|
||||
void load_STBI();
|
||||
void load_TGA();
|
||||
void set_filtering() const;
|
||||
void downsize( GLuint const Format );
|
||||
@@ -99,8 +100,8 @@ private:
|
||||
GLint data_format{ 0 },
|
||||
data_components{ 0 };
|
||||
GLint data_type = GL_UNSIGNED_BYTE;
|
||||
GLint wrap_mode_s = GL_REPEAT;
|
||||
GLint wrap_mode_t = GL_REPEAT;
|
||||
GLint wrap_mode_s = GL_REPEAT;
|
||||
GLint wrap_mode_t = GL_REPEAT;
|
||||
/*
|
||||
std::atomic<bool> is_loaded{ false }; // indicates the texture data was loaded and can be processed
|
||||
std::atomic<bool> is_good{ false }; // indicates the texture data was retrieved without errors
|
||||
|
||||
1265
ref/dds-ktx/include/dds-ktx/dds-ktx.h
Normal file
1265
ref/dds-ktx/include/dds-ktx/dds-ktx.h
Normal file
File diff suppressed because it is too large
Load Diff
2
ref/dds-ktx/src/dds-ktx.c
Normal file
2
ref/dds-ktx/src/dds-ktx.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define DDSKTX_IMPLEMENT
|
||||
#include "dds-ktx/dds-ktx.h"
|
||||
Reference in New Issue
Block a user