18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 02:27:06 +01:00
Files
maszyna/model/materialdata.cpp
Hirek 7d3ccd6dc4 Textures: read KTX2 files (BC1-BC7, RGBA8, zstd) in both renderers
The game now reads .ktx2 texture files, for the OpenGL 4.6 and the Vulkan
renderer alike (texture_source::load_KTX2(), parser in model/ktx2.*):

- formats: BC1 (RGB and RGBA), BC2, BC3, BC4, BC5 (unsigned and signed),
  BC6H (unsigned and signed float), BC7, and plain R8G8B8A8 / B8G8R8A8;
- the levels stored as they are or packed with zstd (the bundled zstd);
  with gfx.textures.streaming the worker threads read and unpack them;
- the KTXorientation key is honoured: "rd" files are flipped to the bottom
  row first, BC1-BC5 block by block, plain texels row by row. BC6H and BC7
  can't be flipped without decoding: an "rd" file of these is loaded as
  stored, with a warning in the log, as the BC7 dds files are. A file
  without the key is taken the way the dds files are (ddsupperorigin);
- srgb or linear colour is chosen by the material, as for the dds files;
- the alpha of BC7 is checked on a small level, as for the dds files.

Not supported, refused with the reason in the log: Basis Universal
(ETC1S / UASTC, BasisLZ), zlib supercompression, arrays, cube maps and
3d images.

BC1 without alpha, BC4, BC5 and BC6H are new formats for the renderers:
opengl_texture::mapping and the Vulkan texture_format_of() map them (no
srgb variants for BC4/BC5/BC6H; their alpha reads 1).

.ktx2 is looked for right after .dds (textures, materials, the editor's
texture browsers). Existing data holds no .ktx2 files, so nothing changes
for existing sceneries and vehicles, and no asset change in the SVN is
needed.

Unit tests: tests/unit/test_ktx2.cpp (header, levels, zstd, orientation,
refused files, flipping of BC1-BC5 and plain texels).
2026-11-30 15:08:45 +01:00

95 lines
3.9 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "model/materialdata.h"
#include "model/Texture.h"
#include "rendering/renderer.h"
#include "utilities/parser.h"
#include "utilities/utilities.h"
#include "utilities/Globals.h"
// Ra 2015-01: sprawdzenie dostępności tekstury o podanej nazwie
std::string
TextureTest( std::string const &Name ) {
auto const lookup {
FileExists(
{ Global.asCurrentTexturePath + Name, Name, paths::textures + Name },
{ ".mat", ".dds", ".ktx2", ".tga", ".ktx", ".png", ".bmp", ".jpg", ".tex" } ) };
return lookup.first + lookup.second;
}
// assigns specified texture or a group of textures to replacable texture slots
void
material_data::assign( std::string const &Replacableskin ) {
// a template puts its parameter after a folder ("tabl/(p6)"): a texture made by a script given there keeps its scheme
if( auto const scheme { Replacableskin.find( "/make:" ) };
scheme != std::string::npos
&& false == contains( Replacableskin, '|' ) ) {
assign( Replacableskin.substr( scheme + 1 ) );
return;
}
// check for the pipe method first
if( contains( Replacableskin, '|' ) ) {
cParser nameparser( Replacableskin );
nameparser.getTokens( 4, false, "|" );
int skinindex = 0;
std::string texturename; nameparser >> texturename;
while( texturename != "" && skinindex < 4 ) {
replacable_skins[ skinindex + 1 ] = GfxRenderer->Fetch_Material( texturename );
++skinindex;
texturename = ""; nameparser >> texturename;
}
multi_textures = skinindex;
}
else {
// otherwise try the basic approach
int skinindex = 0;
do {
// test quietly for file existence so we don't generate tons of false errors in the log
// NOTE: this means actual missing files won't get reported which is hardly ideal, but still somewhat better
auto const material { TextureTest( ToLower( Replacableskin + "," + std::to_string( skinindex + 1 ) ) ) };
if( true == material.empty() ) { break; }
replacable_skins[ skinindex + 1 ] = GfxRenderer->Fetch_Material( material );
++skinindex;
} while( skinindex < 4 );
multi_textures = skinindex;
if( multi_textures == 0 ) {
// zestaw nie zadziałał, próbujemy normanie
replacable_skins[ 1 ] = GfxRenderer->Fetch_Material( Replacableskin );
}
}
if( replacable_skins[ 1 ] == null_handle ) {
// last ditch attempt, check for single replacable skin texture
replacable_skins[ 1 ] = GfxRenderer->Fetch_Material( Replacableskin );
}
// BUGS! it's not entierly designed whether opacity is property of material or submodel,
// and code does confusing things with this in various places
textures_alpha = GfxRenderer->Material(replacable_skins[1])->is_translucent() ? 0x31310031 : // tekstura -1 z kanałem alfa - nie renderować w cyklu nieprzezroczystych
0x30300030; // wszystkie tekstury nieprzezroczyste - nie renderować w cyklu przezroczystych
if( GfxRenderer->Material( replacable_skins[ 2 ] )->is_translucent() ) {
// tekstura -2 z kanałem alfa - nie renderować w cyklu nieprzezroczystych
textures_alpha |= 0x02020002;
}
if( GfxRenderer->Material( replacable_skins[ 3 ] )->is_translucent() ) {
// tekstura -3 z kanałem alfa - nie renderować w cyklu nieprzezroczystych
textures_alpha |= 0x04040004;
}
if( GfxRenderer->Material( replacable_skins[ 4 ] )->is_translucent() ) {
// tekstura -4 z kanałem alfa - nie renderować w cyklu nieprzezroczystych
textures_alpha |= 0x08080008;
}
}