From 942fdb82bb026350f1a27856efbc231e89fcada6 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 24 Nov 2019 19:19:41 -0800 Subject: clang-formatted TextureLoader project --- TextureLoader/src/DDSLoader.cpp | 2 + TextureLoader/src/Image.cpp | 417 +++++++++++++++++---------------- TextureLoader/src/KTXLoader.cpp | 202 ++++++++-------- TextureLoader/src/TextureLoader.cpp | 212 ++++++++--------- TextureLoader/src/TextureUtilities.cpp | 19 +- TextureLoader/src/pch.cpp | 1 - 6 files changed, 430 insertions(+), 423 deletions(-) (limited to 'TextureLoader/src') diff --git a/TextureLoader/src/DDSLoader.cpp b/TextureLoader/src/DDSLoader.cpp index 4c4e7c6..3f11bfe 100644 --- a/TextureLoader/src/DDSLoader.cpp +++ b/TextureLoader/src/DDSLoader.cpp @@ -41,6 +41,8 @@ // http://go.microsoft.com/fwlink/?LinkId=248929 //-------------------------------------------------------------------------------------- +// clang-format off + #include "pch.h" #include "dxgiformat.h" #include diff --git a/TextureLoader/src/Image.cpp b/TextureLoader/src/Image.cpp index 281622b..6472fd7 100644 --- a/TextureLoader/src/Image.cpp +++ b/TextureLoader/src/Image.cpp @@ -47,164 +47,164 @@ namespace Diligent class TIFFClientOpenWrapper { public: - TIFFClientOpenWrapper( IDataBlob *pData ) : - m_Offset( 0 ), - m_Size( pData->GetSize() ), - m_pData( pData ) + TIFFClientOpenWrapper(IDataBlob* pData) : + m_Offset{0}, + m_Size{pData->GetSize()}, + m_pData{pData} { } - static tmsize_t TIFFReadProc( thandle_t pClientData, void* pBuffer, tmsize_t Size ) + static tmsize_t TIFFReadProc(thandle_t pClientData, void* pBuffer, tmsize_t Size) { - auto *pThis = reinterpret_cast(pClientData); - auto *pSrcPtr = reinterpret_cast(pThis->m_pData->GetDataPtr()) + pThis->m_Offset; - memcpy( pBuffer, pSrcPtr, Size ); + auto* pThis = reinterpret_cast(pClientData); + auto* pSrcPtr = reinterpret_cast(pThis->m_pData->GetDataPtr()) + pThis->m_Offset; + memcpy(pBuffer, pSrcPtr, Size); pThis->m_Offset += Size; return Size; } - static tmsize_t TIFFWriteProc( thandle_t pClientData, void* pBuffer, tmsize_t Size ) + static tmsize_t TIFFWriteProc(thandle_t pClientData, void* pBuffer, tmsize_t Size) { - auto *pThis = reinterpret_cast(pClientData); - if( pThis->m_Offset + Size > pThis->m_Size ) + auto* pThis = reinterpret_cast(pClientData); + if (pThis->m_Offset + Size > pThis->m_Size) { pThis->m_Size = pThis->m_Offset + Size; - pThis->m_pData->Resize( pThis->m_Size ); + pThis->m_pData->Resize(pThis->m_Size); } - auto *pDstPtr = reinterpret_cast(pThis->m_pData->GetDataPtr()) + pThis->m_Offset; - memcpy( pDstPtr, pBuffer, Size ); + auto* pDstPtr = reinterpret_cast(pThis->m_pData->GetDataPtr()) + pThis->m_Offset; + memcpy(pDstPtr, pBuffer, Size); pThis->m_Offset += Size; return Size; } - static toff_t TIFFSeekProc( thandle_t pClientData, toff_t Offset, int Whence ) + static toff_t TIFFSeekProc(thandle_t pClientData, toff_t Offset, int Whence) { - auto *pThis = reinterpret_cast(pClientData); - switch( Whence ) + auto* pThis = reinterpret_cast(pClientData); + switch (Whence) { case SEEK_SET: pThis->m_Offset = static_cast(Offset); break; case SEEK_CUR: pThis->m_Offset += static_cast(Offset); break; case SEEK_END: pThis->m_Offset = pThis->m_Size + static_cast(Offset); break; - default: UNEXPECTED( "Unexpected whence" ); + default: UNEXPECTED("Unexpected whence"); } return pThis->m_Offset; } - static int TIFFCloseProc( thandle_t pClientData ) + static int TIFFCloseProc(thandle_t pClientData) { - auto *pThis = reinterpret_cast(pClientData); + auto* pThis = reinterpret_cast(pClientData); pThis->m_pData.Release(); - pThis->m_Size = 0; + pThis->m_Size = 0; pThis->m_Offset = 0; return 0; } - static toff_t TIFFSizeProc( thandle_t pClientData ) + static toff_t TIFFSizeProc(thandle_t pClientData) { - auto *pThis = reinterpret_cast(pClientData); + auto* pThis = reinterpret_cast(pClientData); return pThis->m_Size; } - static int TIFFMapFileProc( thandle_t pClientData, void** base, toff_t* size ) + static int TIFFMapFileProc(thandle_t pClientData, void** base, toff_t* size) { - UNEXPECTED( "Client file mapping is not implemented. Use \'m\' when opening TIFF file to disable file mapping." ); + UNEXPECTED("Client file mapping is not implemented. Use \'m\' when opening TIFF file to disable file mapping."); return 0; } - static void TIFFUnmapFileProc( thandle_t pClientData, void* base, toff_t size ) + static void TIFFUnmapFileProc(thandle_t pClientData, void* base, toff_t size) { - UNEXPECTED( "Client file mapping is not implemented. Use \'m\' when opening TIFF file to disable file mapping." ); + UNEXPECTED("Client file mapping is not implemented. Use \'m\' when opening TIFF file to disable file mapping."); } private: - size_t m_Offset; - size_t m_Size; + size_t m_Offset; + size_t m_Size; RefCntAutoPtr m_pData; }; -void Image::LoadTiffFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) +void Image::LoadTiffFile(IDataBlob* pFileData, const ImageLoadInfo& LoadInfo) { TIFFClientOpenWrapper TiffClientOpenWrpr(pFileData); auto TiffFile = TIFFClientOpen("", "rm", &TiffClientOpenWrpr, - TIFFClientOpenWrapper::TIFFReadProc, - TIFFClientOpenWrapper::TIFFWriteProc, - TIFFClientOpenWrapper::TIFFSeekProc, - TIFFClientOpenWrapper::TIFFCloseProc, - TIFFClientOpenWrapper::TIFFSizeProc, - TIFFClientOpenWrapper::TIFFMapFileProc, - TIFFClientOpenWrapper::TIFFUnmapFileProc); + TIFFClientOpenWrapper::TIFFReadProc, + TIFFClientOpenWrapper::TIFFWriteProc, + TIFFClientOpenWrapper::TIFFSeekProc, + TIFFClientOpenWrapper::TIFFCloseProc, + TIFFClientOpenWrapper::TIFFSizeProc, + TIFFClientOpenWrapper::TIFFMapFileProc, + TIFFClientOpenWrapper::TIFFUnmapFileProc); TIFFGetField(TiffFile, TIFFTAG_IMAGEWIDTH, &m_Desc.Width); TIFFGetField(TiffFile, TIFFTAG_IMAGELENGTH, &m_Desc.Height); - + Uint16 SamplesPerPixel = 0; - // SamplesPerPixel is usually 1 for bilevel, grayscale, and palette-color images. - // SamplesPerPixel is usually 3 for RGB images. If this value is higher, ExtraSamples + // SamplesPerPixel is usually 1 for bilevel, grayscale, and palette-color images. + // SamplesPerPixel is usually 3 for RGB images. If this value is higher, ExtraSamples // should give an indication of the meaning of the additional channels. TIFFGetField(TiffFile, TIFFTAG_SAMPLESPERPIXEL, &SamplesPerPixel); m_Desc.NumComponents = SamplesPerPixel; Uint16 BitsPerSample = 0; TIFFGetField(TiffFile, TIFFTAG_BITSPERSAMPLE, &BitsPerSample); - + Uint16 SampleFormat = 0; TIFFGetField(TiffFile, TIFFTAG_SAMPLEFORMAT, &SampleFormat); if (SampleFormat == 0) SampleFormat = SAMPLEFORMAT_UINT; - - switch(SampleFormat) + + switch (SampleFormat) { case SAMPLEFORMAT_UINT: - switch(BitsPerSample) + switch (BitsPerSample) { - case 8: m_Desc.ComponentType = VT_UINT8; break; + case 8: m_Desc.ComponentType = VT_UINT8; break; case 16: m_Desc.ComponentType = VT_UINT16; break; case 32: m_Desc.ComponentType = VT_UINT32; break; default: LOG_ERROR_AND_THROW(BitsPerSample, " is not a valid UINT component bit depth. Only 8, 16 and 32 are allowed"); } - break; + break; case SAMPLEFORMAT_INT: - switch(BitsPerSample) + switch (BitsPerSample) { - case 8: m_Desc.ComponentType = VT_INT8; break; + case 8: m_Desc.ComponentType = VT_INT8; break; case 16: m_Desc.ComponentType = VT_INT16; break; case 32: m_Desc.ComponentType = VT_INT32; break; default: LOG_ERROR_AND_THROW(BitsPerSample, " is not a valid INT component bit depth. Only 8, 16 and 32 are allowed"); } - break; + break; case SAMPLEFORMAT_IEEEFP: - switch(BitsPerSample) + switch (BitsPerSample) { case 16: m_Desc.ComponentType = VT_FLOAT16; break; case 32: m_Desc.ComponentType = VT_FLOAT32; break; default: LOG_ERROR_AND_THROW(BitsPerSample, " is not a valid FLOAT component bit depth. Only 16 and 32 are allowed"); } - break; + break; case SAMPLEFORMAT_VOID: LOG_ERROR_AND_THROW("Untyped tif images are not supported"); - break; + break; case SAMPLEFORMAT_COMPLEXINT: LOG_ERROR_AND_THROW("Complex int tif images are not supported"); - break; + break; case SAMPLEFORMAT_COMPLEXIEEEFP: LOG_ERROR_AND_THROW("Complex floating point tif images are not supported"); - break; + break; default: LOG_ERROR_AND_THROW("Unknown sample format: ", Uint32{SampleFormat}); } auto ScanlineSize = TIFFScanlineSize(TiffFile); - m_Desc.RowStride = Align(static_cast( ScanlineSize ), 4u); + m_Desc.RowStride = Align(static_cast(ScanlineSize), 4u); m_pData->Resize(size_t{m_Desc.Height} * size_t{m_Desc.RowStride}); - auto *pDataPtr = reinterpret_cast( m_pData->GetDataPtr() ); + auto* pDataPtr = reinterpret_cast(m_pData->GetDataPtr()); for (Uint32 row = 0; row < m_Desc.Height; row++, pDataPtr += m_Desc.RowStride) { TIFFReadScanline(TiffFile, pDataPtr, row); @@ -216,25 +216,25 @@ void Image::LoadTiffFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) class PNGReadFnHelper { public: - PNGReadFnHelper( IDataBlob *pData ) : - m_pData( pData ), - m_Offset( 0 ) + PNGReadFnHelper(IDataBlob* pData) : + m_pData{pData}, + m_Offset{0} { } - static void ReadData( png_structp pngPtr, png_bytep data, png_size_t length ) + static void ReadData(png_structp pngPtr, png_bytep data, png_size_t length) { - auto pThis = reinterpret_cast( png_get_io_ptr(pngPtr) ); - memcpy( data, reinterpret_cast(pThis->m_pData->GetDataPtr()) + pThis->m_Offset, length ); + auto pThis = reinterpret_cast(png_get_io_ptr(pngPtr)); + memcpy(data, reinterpret_cast(pThis->m_pData->GetDataPtr()) + pThis->m_Offset, length); pThis->m_Offset += length; } private: RefCntAutoPtr m_pData; - size_t m_Offset; + size_t m_Offset; }; -void Image::LoadPngFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) +void Image::LoadPngFile(IDataBlob* pFileData, const ImageLoadInfo& LoadInfo) { // http://www.piko3d.net/tutorials/libpng-tutorial-loading-png-files-from-streams/ // http://www.libpng.org/pub/png/book/chapter13.html#png.ch13.div.10 @@ -242,25 +242,25 @@ void Image::LoadPngFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) PNGReadFnHelper ReadFnHelper(pFileData); - const size_t PngSigSize = 8; - png_const_bytep pngsig = reinterpret_cast(pFileData->GetDataPtr()); + const size_t PngSigSize = 8; + png_const_bytep pngsig = reinterpret_cast(pFileData->GetDataPtr()); //Let LibPNG check the signature. If this function returns 0, everything is OK. - if( png_sig_cmp( pngsig, 0, PngSigSize ) != 0 ) + if (png_sig_cmp(pngsig, 0, PngSigSize) != 0) { - LOG_ERROR_AND_THROW( "Invalid png signature" ); + LOG_ERROR_AND_THROW("Invalid png signature"); } png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); VERIFY(png, "png_create_read_struct() failed"); - + png_infop info = png_create_info_struct(png); VERIFY(info, "png_create_info_struct() failed"); - - if( setjmp( png_jmpbuf( png ) ) ) + + if (setjmp(png_jmpbuf(png))) { // When an error occurs during parsing, libPNG will jump to here png_destroy_read_struct(&png, &info, (png_infopp)0); - LOG_ERROR_AND_THROW( "Failed to read png file" ); + LOG_ERROR_AND_THROW("Failed to read png file"); } png_set_read_fn(png, (png_voidp)&ReadFnHelper, PNGReadFnHelper::ReadData); @@ -268,32 +268,32 @@ void Image::LoadPngFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) png_read_info(png, info); auto bit_depth = png_get_bit_depth(png, info); - - // PNG files store 16-bit pixels in network byte order (big-endian, ie - // most significant bytes first). png_set_swap() shall switch the byte-order + + // PNG files store 16-bit pixels in network byte order (big-endian, ie + // most significant bytes first). png_set_swap() shall switch the byte-order // to little-endian (ie, least significant bits first). - if( bit_depth == 16 ) + if (bit_depth == 16) png_set_swap(png); auto color_type = png_get_color_type(png, info); // See http://www.libpng.org/pub/png/libpng-manual.txt - if( color_type == PNG_COLOR_TYPE_PALETTE ) + if (color_type == PNG_COLOR_TYPE_PALETTE) { // Transform paletted images into 8-bit rgba - png_set_palette_to_rgb( png ); - png_set_filler( png, 0xFF, PNG_FILLER_AFTER ); + png_set_palette_to_rgb(png); + png_set_filler(png, 0xFF, PNG_FILLER_AFTER); } // PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth. - if( color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 ) + if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) { // Expand 1, 2, or 4-bit images to 8-bit - png_set_expand_gray_1_2_4_to_8( png ); + png_set_expand_gray_1_2_4_to_8(png); } - if( png_get_valid( png, info, PNG_INFO_tRNS ) ) - png_set_tRNS_to_alpha( png ); + if (png_get_valid(png, info, PNG_INFO_tRNS)) + png_set_tRNS_to_alpha(png); #if 0 // These color_type don't have an alpha channel then fill it with 0xff. @@ -307,15 +307,15 @@ void Image::LoadPngFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) png_set_gray_to_rgb( png ); #endif - png_read_update_info( png, info ); - + png_read_update_info(png, info); + bit_depth = png_get_bit_depth(png, info); m_Desc.Width = png_get_image_width(png, info); m_Desc.Height = png_get_image_height(png, info); m_Desc.NumComponents = png_get_channels(png, info); - switch(bit_depth) + switch (bit_depth) { - case 8: m_Desc.ComponentType = VT_UINT8; break; + case 8: m_Desc.ComponentType = VT_UINT8; break; case 16: m_Desc.ComponentType = VT_UINT16; break; case 32: m_Desc.ComponentType = VT_UINT32; break; default: LOG_ERROR_AND_THROW("Unsupported component bit depth: ", bit_depth, ". Only 8, 16 and 32-bit components are supported"); @@ -326,41 +326,42 @@ void Image::LoadPngFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) //Alocate a buffer with enough space. Align stride to 4 bytes m_Desc.RowStride = Align(m_Desc.Width * static_cast(bit_depth) * m_Desc.NumComponents / 8u, 4u); - m_pData->Resize( size_t{m_Desc.Height} * size_t{m_Desc.RowStride} ); - for( size_t i = 0; i < m_Desc.Height; i++ ) + m_pData->Resize(size_t{m_Desc.Height} * size_t{m_Desc.RowStride}); + for (size_t i = 0; i < m_Desc.Height; i++) rowPtrs[i] = reinterpret_cast(m_pData->GetDataPtr()) + i * m_Desc.RowStride; //Read the imagedata and write it to the adresses pointed to //by rowptrs (in other words: our image databuffer) - png_read_image( png, rowPtrs.data() ); + png_read_image(png, rowPtrs.data()); png_destroy_read_struct(&png, &info, (png_infopp)0); } -struct my_jpeg_error_mgr { +struct my_jpeg_error_mgr +{ jpeg_error_mgr pub; - char padding[8]; - jmp_buf setjmp_buffer;// for return to caller + char padding[8]; + jmp_buf setjmp_buffer; // for return to caller }; // Here's the routine that will replace the standard error_exit method: METHODDEF(void) -my_error_exit (j_common_ptr cinfo) +my_error_exit(j_common_ptr cinfo) { // cinfo->err really points to a my_jpeg_error_mgr struct, so coerce pointer - my_jpeg_error_mgr* myerr = (my_jpeg_error_mgr*) cinfo->err; + my_jpeg_error_mgr* myerr = (my_jpeg_error_mgr*)cinfo->err; /* Always display the message. */ /* We could postpone this until after returning, if we chose. */ - (*cinfo->err->output_message) (cinfo); + (*cinfo->err->output_message)(cinfo); // Return control to the setjmp point longjmp(myerr->setjmp_buffer, 1); } -void Image::LoadJpegFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) +void Image::LoadJpegFile(IDataBlob* pFileData, const ImageLoadInfo& LoadInfo) { // https://github.com/LuaDist/libjpeg/blob/master/example.c @@ -376,24 +377,24 @@ void Image::LoadJpegFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) // Step 1: allocate and initialize JPEG decompression object // We set up the normal JPEG error routines, then override error_exit. - cinfo.err = jpeg_std_error( &jerr.pub ); + cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; // Establish the setjmp return context for my_error_exit to use. - if( setjmp( jerr.setjmp_buffer ) ) + if (setjmp(jerr.setjmp_buffer)) { // If we get here, the JPEG code has signaled an error. // We need to clean up the JPEG object, close the input file, and return. - jpeg_destroy_decompress( &cinfo ); - LOG_ERROR_AND_THROW( "Failed to decompress JPEG image" ); + jpeg_destroy_decompress(&cinfo); + LOG_ERROR_AND_THROW("Failed to decompress JPEG image"); } // Now we can initialize the JPEG decompression object. - jpeg_create_decompress( &cinfo ); + jpeg_create_decompress(&cinfo); // Step 2: specify data source - jpeg_mem_src( &cinfo, reinterpret_cast(pFileData->GetDataPtr()), static_cast(pFileData->GetSize()) ); + jpeg_mem_src(&cinfo, reinterpret_cast(pFileData->GetDataPtr()), static_cast(pFileData->GetSize())); // Step 3: read file parameters with jpeg_read_header() - jpeg_read_header( &cinfo, TRUE ); + jpeg_read_header(&cinfo, TRUE); // We can ignore the return value from jpeg_read_header since // (a) suspension is not possible with the stdio data source, and // (b) we passed TRUE to reject a tables-only JPEG file as an error. @@ -408,7 +409,7 @@ void Image::LoadJpegFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) // Step 5: Start decompressor - jpeg_start_decompress( &cinfo ); + jpeg_start_decompress(&cinfo); // We can ignore the return value since suspension is not possible // with the stdio data source. @@ -429,63 +430,64 @@ void Image::LoadJpegFile( IDataBlob *pFileData, const ImageLoadInfo& LoadInfo ) // Here we use the library's state variable cinfo.output_scanline as the // loop counter, so that we don't have to keep track ourselves. - while( cinfo.output_scanline < cinfo.output_height ) { + while (cinfo.output_scanline < cinfo.output_height) + { // jpeg_read_scanlines expects an array of pointers to scanlines. // Here the array is only one element long, but you could ask for // more than one scanline at a time if that's more convenient. - auto *pDstScanline = reinterpret_cast( m_pData->GetDataPtr() ) + size_t{cinfo.output_scanline} * size_t{m_Desc.RowStride}; - JSAMPROW RowPtrs[] = { reinterpret_cast(pDstScanline) }; - jpeg_read_scanlines( &cinfo, RowPtrs, 1 ); + auto* pDstScanline = reinterpret_cast(m_pData->GetDataPtr()) + size_t{cinfo.output_scanline} * size_t{m_Desc.RowStride}; + JSAMPROW RowPtrs[] = {reinterpret_cast(pDstScanline)}; + jpeg_read_scanlines(&cinfo, RowPtrs, 1); } // Step 7: Finish decompression - jpeg_finish_decompress( &cinfo ); + jpeg_finish_decompress(&cinfo); // We can ignore the return value since suspension is not possible // with the stdio data source. // Step 8: Release JPEG decompression object // This is an important step since it will release a good deal of memory. - jpeg_destroy_decompress( &cinfo ); + jpeg_destroy_decompress(&cinfo); // At this point you may want to check to see whether any corrupt-data // warnings occurred (test whether jerr.pub.num_warnings is nonzero). } -Image::Image( IReferenceCounters* pRefCounters, - IDataBlob* pFileData, - const ImageLoadInfo& LoadInfo ) : - TBase(pRefCounters), - m_pData( MakeNewRCObj()(0) ) +Image::Image(IReferenceCounters* pRefCounters, + IDataBlob* pFileData, + const ImageLoadInfo& LoadInfo) : + TBase{pRefCounters}, + m_pData{MakeNewRCObj()(0)} { - if( LoadInfo.Format == EImageFileFormat::tiff ) + if (LoadInfo.Format == EImageFileFormat::tiff) { - LoadTiffFile(pFileData, LoadInfo ); + LoadTiffFile(pFileData, LoadInfo); } - else if( LoadInfo.Format == EImageFileFormat::png ) + else if (LoadInfo.Format == EImageFileFormat::png) { - LoadPngFile(pFileData, LoadInfo ); + LoadPngFile(pFileData, LoadInfo); } - else if( LoadInfo.Format == EImageFileFormat::jpeg ) + else if (LoadInfo.Format == EImageFileFormat::jpeg) { - LoadJpegFile(pFileData, LoadInfo ); + LoadJpegFile(pFileData, LoadInfo); } - else if( LoadInfo.Format == EImageFileFormat::dds ) + else if (LoadInfo.Format == EImageFileFormat::dds) { LOG_ERROR("An image can't be created from DDS file. Use CreateTextureFromFile() or CreateTextureFromDDS() functions."); } - else if( LoadInfo.Format == EImageFileFormat::ktx ) + else if (LoadInfo.Format == EImageFileFormat::ktx) { LOG_ERROR("An image can't be created from KTX file. Use CreateTextureFromFile() or CreateTextureFromKTX() functions."); } } -void Image::CreateFromDataBlob(IDataBlob* pFileData, - const ImageLoadInfo& LoadInfo, - Image** ppImage) +void Image::CreateFromDataBlob(IDataBlob* pFileData, + const ImageLoadInfo& LoadInfo, + Image** ppImage) { *ppImage = MakeNewRCObj()(pFileData, LoadInfo); (*ppImage)->AddRef(); @@ -505,31 +507,30 @@ static void WritePng(const Uint8* pData, Uint32 Width, Uint32 Height, Uint32 Str } ~PngWrapper() - { + { if (strct) - { + { png_destroy_write_struct(&strct, &info); } } png_struct* strct = nullptr; png_info* info = nullptr; - }Png; + } Png; VERIFY(setjmp(png_jmpbuf(Png.strct)) == 0, "setjmp(png_jmpbuf(p) failed"); png_set_IHDR(Png.strct, Png.info, Width, Height, 8, - PngColorType, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT); + PngColorType, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); //png_set_compression_level(p, 1); std::vector rows(Height); for (size_t y = 0; y < Height; ++y) rows[y] = const_cast(pData) + y * Stride; png_set_rows(Png.strct, Png.info, rows.data()); - - auto PngWriteCallback = [](png_structp png_ptr, png_bytep data, png_size_t length) - { + + auto PngWriteCallback = [](png_structp png_ptr, png_bytep data, png_size_t length) { auto* pEncodedData = reinterpret_cast(png_get_io_ptr(png_ptr)); pEncodedData->Resize(pEncodedData->GetSize() + length); auto* pBytes = reinterpret_cast(pEncodedData->GetDataPtr()); @@ -543,83 +544,83 @@ static void WritePng(const Uint8* pData, Uint32 Width, Uint32 Height, Uint32 Str static void WriteJPEG(JSAMPLE* pRGBData, Uint32 Width, Uint32 Height, int quality, IDataBlob* pEncodedData) { /* This struct contains the JPEG compression parameters and pointers to - * working space (which is allocated as needed by the JPEG library). - * It is possible to have several such structures, representing multiple - * compression/decompression processes, in existence at once. We refer - * to any one struct (and its associated working data) as a "JPEG object". - */ + * working space (which is allocated as needed by the JPEG library). + * It is possible to have several such structures, representing multiple + * compression/decompression processes, in existence at once. We refer + * to any one struct (and its associated working data) as a "JPEG object". + */ jpeg_compress_struct cinfo; /* This struct represents a JPEG error handler. It is declared separately - * because applications often want to supply a specialized error handler - * (see the second half of this file for an example). But here we just - * take the easy way out and use the standard error handler, which will - * print a message on stderr and call exit() if compression fails. - * Note that this struct must live as long as the main JPEG parameter - * struct, to avoid dangling-pointer problems. - */ + * because applications often want to supply a specialized error handler + * (see the second half of this file for an example). But here we just + * take the easy way out and use the standard error handler, which will + * print a message on stderr and call exit() if compression fails. + * Note that this struct must live as long as the main JPEG parameter + * struct, to avoid dangling-pointer problems. + */ jpeg_error_mgr jerr; /* Step 1: allocate and initialize JPEG compression object */ /* We have to set up the error handler first, in case the initialization - * step fails. (Unlikely, but it could happen if you are out of memory.) - * This routine fills in the contents of struct jerr, and returns jerr's - * address which we place into the link field in cinfo. - */ + * step fails. (Unlikely, but it could happen if you are out of memory.) + * This routine fills in the contents of struct jerr, and returns jerr's + * address which we place into the link field in cinfo. + */ cinfo.err = jpeg_std_error(&jerr); /* Now we can initialize the JPEG compression object. */ jpeg_create_compress(&cinfo); /* Step 2: specify data destination (memory) */ /* Note: steps 2 and 3 can be done in either order. */ - unsigned char *mem = NULL; - unsigned long mem_size = 0; + unsigned char* mem = NULL; + unsigned long mem_size = 0; jpeg_mem_dest(&cinfo, &mem, &mem_size); /* Step 3: set parameters for compression */ /* First we supply a description of the input image. - * Four fields of the cinfo struct must be filled in: - */ - cinfo.image_width = Width; /* image width and height, in pixels */ + * Four fields of the cinfo struct must be filled in: + */ + cinfo.image_width = Width; /* image width and height, in pixels */ cinfo.image_height = Height; - cinfo.input_components = 3; /* # of color components per pixel */ - cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ + cinfo.input_components = 3; /* # of color components per pixel */ + cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ /* Now use the library's routine to set default compression parameters. - * (You must set at least cinfo.in_color_space before calling this, - * since the defaults depend on the source color space.) - */ + * (You must set at least cinfo.in_color_space before calling this, + * since the defaults depend on the source color space.) + */ jpeg_set_defaults(&cinfo); /* Now you can set any non-default parameters you wish to. - * Here we just illustrate the use of quality (quantization table) scaling: - */ + * Here we just illustrate the use of quality (quantization table) scaling: + */ jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); /* Step 4: Start compressor */ /* TRUE ensures that we will write a complete interchange-JPEG file. - * Pass TRUE unless you are very sure of what you're doing. - */ + * Pass TRUE unless you are very sure of what you're doing. + */ jpeg_start_compress(&cinfo, TRUE); /* Step 5: while (scan lines remain to be written) */ /* jpeg_write_scanlines(...); */ /* Here we use the library's state variable cinfo.next_scanline as the - * loop counter, so that we don't have to keep track ourselves. - * To keep things simple, we pass one scanline per call; you can pass - * more if you wish, though. - */ - auto row_stride = Width * 3; /* JSAMPLEs per row in image_buffer */ + * loop counter, so that we don't have to keep track ourselves. + * To keep things simple, we pass one scanline per call; you can pass + * more if you wish, though. + */ + auto row_stride = Width * 3; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height) { /* jpeg_write_scanlines expects an array of pointers to scanlines. - * Here the array is only one element long, but you could pass - * more than one scanline at a time if that's more convenient. - */ - JSAMPROW row_pointer[1] = { &pRGBData[cinfo.next_scanline * row_stride] }; + * Here the array is only one element long, but you could pass + * more than one scanline at a time if that's more convenient. + */ + JSAMPROW row_pointer[1] = {&pRGBData[cinfo.next_scanline * row_stride]}; jpeg_write_scanlines(&cinfo, row_pointer, 1); } @@ -640,24 +641,24 @@ static void WriteJPEG(JSAMPLE* pRGBData, Uint32 Width, Uint32 Height, int qualit static const std::array GetRGBAOffsets(TEXTURE_FORMAT Format) { - switch(Format) + switch (Format) { case TEX_FORMAT_BGRA8_TYPELESS: case TEX_FORMAT_BGRA8_UNORM: case TEX_FORMAT_BGRA8_UNORM_SRGB: - return {{2,1,0,3}}; + return {{2, 1, 0, 3}}; default: - return {{0,1,2,3}}; + return {{0, 1, 2, 3}}; } } -std::vector Image::ConvertImageData(Uint32 Width, - Uint32 Height, - const Uint8* pData, - Uint32 Stride, - TEXTURE_FORMAT SrcFormat, - TEXTURE_FORMAT DstFormat, - bool KeepAlpha) +std::vector Image::ConvertImageData(Uint32 Width, + Uint32 Height, + const Uint8* pData, + Uint32 Stride, + TEXTURE_FORMAT SrcFormat, + TEXTURE_FORMAT DstFormat, + bool KeepAlpha) { const auto& SrcFmtAttribs = GetTextureFormatAttribs(SrcFormat); const auto& DstFmtAttribs = GetTextureFormatAttribs(DstFormat); @@ -673,14 +674,14 @@ std::vector Image::ConvertImageData(Uint32 Width, std::vector ConvertedData(DstFmtAttribs.ComponentSize * NumDstComponents * Width * Height); - for (Uint32 j=0; j < Height; ++j) + for (Uint32 j = 0; j < Height; ++j) { - for (Uint32 i=0; i < Width; ++i) + for (Uint32 i = 0; i < Width; ++i) { for (Uint32 c = 0; c < NumDstComponents; ++c) { - ConvertedData[j * Width * NumDstComponents + i*NumDstComponents + DstOffsets[c]] = - pData[j * Stride + i*SrcFmtAttribs.NumComponents + SrcOffsets[c]]; + ConvertedData[j * Width * NumDstComponents + i * NumDstComponents + DstOffsets[c]] = + pData[j * Stride + i * SrcFmtAttribs.NumComponents + SrcOffsets[c]]; } } } @@ -699,14 +700,14 @@ void Image::Encode(const EncodeInfo& Info, IDataBlob** ppEncodedData) } else if (Info.FileFormat == EImageFileFormat::png) { - const auto* pData = reinterpret_cast(Info.pData); - auto Stride = Info.Stride; + const auto* pData = reinterpret_cast(Info.pData); + auto Stride = Info.Stride; std::vector ConvertedData; - if ( !((Info.TexFormat == TEX_FORMAT_RGBA8_UNORM || Info.TexFormat == TEX_FORMAT_RGBA8_UNORM_SRGB) && Info.KeepAlpha) ) + if (!((Info.TexFormat == TEX_FORMAT_RGBA8_UNORM || Info.TexFormat == TEX_FORMAT_RGBA8_UNORM_SRGB) && Info.KeepAlpha)) { ConvertedData = ConvertImageData(Info.Width, Info.Height, reinterpret_cast(Info.pData), Info.Stride, Info.TexFormat, TEX_FORMAT_RGBA8_UNORM, Info.KeepAlpha); - pData = ConvertedData.data(); - Stride = Info.Width * (Info.KeepAlpha ? 4 : 3); + pData = ConvertedData.data(); + Stride = Info.Width * (Info.KeepAlpha ? 4 : 3); } WritePng(pData, Info.Width, Info.Height, Stride, Info.KeepAlpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB, pEncodedData); @@ -723,13 +724,13 @@ EImageFileFormat Image::GetFileFormat(const Uint8* pData, size_t Size) if (Size >= 3 && pData[0] == 0xFF && pData[1] == 0xD8 && pData[2] == 0xFF) return EImageFileFormat::jpeg; - if (Size >= 8 && + if (Size >= 8 && pData[0] == 0x89 && pData[1] == 0x50 && pData[2] == 0x4E && pData[3] == 0x47 && pData[4] == 0x0D && pData[5] == 0x0A && pData[6] == 0x1A && pData[7] == 0x0A) return EImageFileFormat::png; - if (Size >= 4 && - ((pData[0] == 0x49 && pData[1] == 0x20 && pData[2] == 0x49) || + if (Size >= 4 && + ((pData[0] == 0x49 && pData[1] == 0x20 && pData[2] == 0x49) || (pData[0] == 0x49 && pData[1] == 0x49 && pData[2] == 0x2A && pData[3] == 0x00) || (pData[0] == 0x4D && pData[1] == 0x4D && pData[2] == 0x00 && pData[3] == 0x2A) || (pData[0] == 0x4D && pData[1] == 0x4D && pData[2] == 0x00 && pData[3] == 0x2B))) @@ -740,7 +741,7 @@ EImageFileFormat Image::GetFileFormat(const Uint8* pData, size_t Size) static constexpr Uint8 KTX10FileIdentifier[12] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; static constexpr Uint8 KTX20FileIdentifier[12] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; - if (Size >= 12 && + if (Size >= 12 && (memcmp(pData, KTX10FileIdentifier, sizeof(KTX10FileIdentifier)) == 0 || memcmp(pData, KTX20FileIdentifier, sizeof(KTX20FileIdentifier)) == 0)) return EImageFileFormat::ktx; @@ -749,15 +750,15 @@ EImageFileFormat Image::GetFileFormat(const Uint8* pData, size_t Size) } -EImageFileFormat CreateImageFromFile(const Char* FilePath, - Image** ppImage, - IDataBlob** ppRawData) +EImageFileFormat CreateImageFromFile(const Char* FilePath, + Image** ppImage, + IDataBlob** ppRawData) { EImageFileFormat ImgFileFormat = EImageFileFormat::unknown; try { RefCntAutoPtr pFileStream(MakeNewRCObj()(FilePath, EFileAccessMode::Read)); - if(!pFileStream->IsValid()) + if (!pFileStream->IsValid()) LOG_ERROR_AND_THROW("Failed to open image file \"", FilePath, '\"'); RefCntAutoPtr pFileData(MakeNewRCObj()(0)); @@ -769,11 +770,11 @@ EImageFileFormat CreateImageFromFile(const Char* FilePath, LOG_WARNING_MESSAGE("Unable to derive image format from the header for file \"", FilePath, "\". Trying to analyze extension."); // Try to use extension to derive format - auto *pDotPos = strrchr(FilePath, '.'); + auto* pDotPos = strrchr(FilePath, '.'); if (pDotPos == nullptr) LOG_ERROR_AND_THROW("Unable to recognize file format: file name \"", FilePath, "\" does not contain extension"); - auto *pExtension = pDotPos + 1; + auto* pExtension = pDotPos + 1; if (*pExtension == 0) LOG_ERROR_AND_THROW("Unable to recognize file format: file name \"", FilePath, "\" contain empty extension"); @@ -792,7 +793,7 @@ EImageFileFormat CreateImageFromFile(const Char* FilePath, LOG_ERROR_AND_THROW("Unsupported file format ", Extension); } - if (ImgFileFormat == EImageFileFormat::png || + if (ImgFileFormat == EImageFileFormat::png || ImgFileFormat == EImageFileFormat::jpeg || ImgFileFormat == EImageFileFormat::tiff) { @@ -800,12 +801,12 @@ EImageFileFormat CreateImageFromFile(const Char* FilePath, ImgLoadInfo.Format = ImgFileFormat; Image::CreateFromDataBlob(pFileData, ImgLoadInfo, ppImage); } - else if(ppRawData != nullptr) + else if (ppRawData != nullptr) { *ppRawData = pFileData.Detach(); } } - catch (std::runtime_error &err) + catch (std::runtime_error& err) { LOG_ERROR("Failed to create image from file: ", err.what()); } diff --git a/TextureLoader/src/KTXLoader.cpp b/TextureLoader/src/KTXLoader.cpp index 9c70ac1..c358a6a 100644 --- a/TextureLoader/src/KTXLoader.cpp +++ b/TextureLoader/src/KTXLoader.cpp @@ -26,71 +26,71 @@ #include "GraphicsAccessories.h" #include "Align.h" -#define GL_RGBA32F 0x8814 -#define GL_RGBA32UI 0x8D70 -#define GL_RGBA32I 0x8D82 -#define GL_RGB32F 0x8815 -#define GL_RGB32UI 0x8D71 -#define GL_RGB32I 0x8D83 -#define GL_RGBA16F 0x881A -#define GL_RGBA16 0x805B -#define GL_RGBA16UI 0x8D76 -#define GL_RGBA16_SNORM 0x8F9B -#define GL_RGBA16I 0x8D88 -#define GL_RG32F 0x8230 -#define GL_RG32UI 0x823C -#define GL_RG32I 0x823B -#define GL_DEPTH32F_STENCIL8 0x8CAD -#define GL_RGB10_A2 0x8059 -#define GL_RGB10_A2UI 0x906F -#define GL_R11F_G11F_B10F 0x8C3A -#define GL_RGBA8 0x8058 -#define GL_RGBA8UI 0x8D7C -#define GL_RGBA8_SNORM 0x8F97 -#define GL_RGBA8I 0x8D8E -#define GL_RG16F 0x822F -#define GL_RG16 0x822C -#define GL_RG16UI 0x823A -#define GL_RG16_SNORM 0x8F99 -#define GL_RG16I 0x8239 -#define GL_R32F 0x822E -#define GL_DEPTH_COMPONENT32F 0x8CAC -#define GL_R32UI 0x8236 -#define GL_R32I 0x8235 -#define GL_DEPTH24_STENCIL8 0x88F0 -#define GL_RG8 0x822B -#define GL_RG8UI 0x8238 -#define GL_RG8_SNORM 0x8F95 -#define GL_RG8I 0x8237 -#define GL_R16F 0x822D -#define GL_DEPTH_COMPONENT16 0x81A5 -#define GL_R16 0x822A -#define GL_R16UI 0x8234 -#define GL_R16_SNORM 0x8F98 -#define GL_R16I 0x8233 -#define GL_R8 0x8229 -#define GL_R8UI 0x8232 -#define GL_R8_SNORM 0x8F94 -#define GL_R8I 0x8231 -#define GL_RGB9_E5 0x8C3D +#define GL_RGBA32F 0x8814 +#define GL_RGBA32UI 0x8D70 +#define GL_RGBA32I 0x8D82 +#define GL_RGB32F 0x8815 +#define GL_RGB32UI 0x8D71 +#define GL_RGB32I 0x8D83 +#define GL_RGBA16F 0x881A +#define GL_RGBA16 0x805B +#define GL_RGBA16UI 0x8D76 +#define GL_RGBA16_SNORM 0x8F9B +#define GL_RGBA16I 0x8D88 +#define GL_RG32F 0x8230 +#define GL_RG32UI 0x823C +#define GL_RG32I 0x823B +#define GL_DEPTH32F_STENCIL8 0x8CAD +#define GL_RGB10_A2 0x8059 +#define GL_RGB10_A2UI 0x906F +#define GL_R11F_G11F_B10F 0x8C3A +#define GL_RGBA8 0x8058 +#define GL_RGBA8UI 0x8D7C +#define GL_RGBA8_SNORM 0x8F97 +#define GL_RGBA8I 0x8D8E +#define GL_RG16F 0x822F +#define GL_RG16 0x822C +#define GL_RG16UI 0x823A +#define GL_RG16_SNORM 0x8F99 +#define GL_RG16I 0x8239 +#define GL_R32F 0x822E +#define GL_DEPTH_COMPONENT32F 0x8CAC +#define GL_R32UI 0x8236 +#define GL_R32I 0x8235 +#define GL_DEPTH24_STENCIL8 0x88F0 +#define GL_RG8 0x822B +#define GL_RG8UI 0x8238 +#define GL_RG8_SNORM 0x8F95 +#define GL_RG8I 0x8237 +#define GL_R16F 0x822D +#define GL_DEPTH_COMPONENT16 0x81A5 +#define GL_R16 0x822A +#define GL_R16UI 0x8234 +#define GL_R16_SNORM 0x8F98 +#define GL_R16I 0x8233 +#define GL_R8 0x8229 +#define GL_R8UI 0x8232 +#define GL_R8_SNORM 0x8F94 +#define GL_R8I 0x8231 +#define GL_RGB9_E5 0x8C3D -#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 -#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 -#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 -#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 -#define GL_COMPRESSED_SRGB_S3TC_DXT1_EXT 0x8C4C -#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D -#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E -#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F -#define GL_COMPRESSED_RED_RGTC1 0x8DBB -#define GL_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC -#define GL_COMPRESSED_RG_RGTC2 0x8DBD -#define GL_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE -#define GL_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C -#define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D -#define GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E -#define GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F +#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 +#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 +#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 +#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 +#define GL_COMPRESSED_SRGB_S3TC_DXT1_EXT 0x8C4C +#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D +#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E +#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F +#define GL_COMPRESSED_RED_RGTC1 0x8DBB +#define GL_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC +#define GL_COMPRESSED_RG_RGTC2 0x8DBD +#define GL_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE +#define GL_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C +#define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D +#define GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E +#define GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F namespace Diligent { @@ -102,6 +102,7 @@ TEXTURE_FORMAT FindDiligentTextureFormat(std::uint32_t GLInternalFormat) { switch (GLInternalFormat) { + // clang-format off case GL_RGBA32F: return TEX_FORMAT_RGBA32_FLOAT; case GL_RGBA32UI: return TEX_FORMAT_RGBA32_UINT; case GL_RGBA32I: return TEX_FORMAT_RGBA32_SINT; @@ -178,6 +179,7 @@ TEXTURE_FORMAT FindDiligentTextureFormat(std::uint32_t GLInternalFormat) case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT: return TEX_FORMAT_BC6H_SF16; case GL_COMPRESSED_RGBA_BPTC_UNORM: return TEX_FORMAT_BC7_UNORM; case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM: return TEX_FORMAT_BC7_UNORM_SRGB; + // clang-format on default: UNSUPPORTED("Unsupported internal format"); return TEX_FORMAT_UNKNOWN; @@ -189,82 +191,82 @@ TEXTURE_FORMAT FindDiligentTextureFormat(std::uint32_t GLInternalFormat) struct KTX10Header { - std::uint32_t Endianness; - std::uint32_t GLType; - std::uint32_t GLTypeSize; - std::uint32_t GLFormat; - std::uint32_t GLInternalFormat; - std::uint32_t GLBaseInternalFormat; - std::uint32_t Width; - std::uint32_t Height; - std::uint32_t Depth; - std::uint32_t NumberOfArrayElements; - std::uint32_t NumberOfFaces; - std::uint32_t NumberOfMipmapLevels; - std::uint32_t BytesOfKeyValueData; + std::uint32_t Endianness; + std::uint32_t GLType; + std::uint32_t GLTypeSize; + std::uint32_t GLFormat; + std::uint32_t GLInternalFormat; + std::uint32_t GLBaseInternalFormat; + std::uint32_t Width; + std::uint32_t Height; + std::uint32_t Depth; + std::uint32_t NumberOfArrayElements; + std::uint32_t NumberOfFaces; + std::uint32_t NumberOfMipmapLevels; + std::uint32_t BytesOfKeyValueData; }; -void CreateTextureFromKTX( IDataBlob* pKTXData, - const TextureLoadInfo& TexLoadInfo, - IRenderDevice* pDevice, - ITexture** ppTexture ) +void CreateTextureFromKTX(IDataBlob* pKTXData, + const TextureLoadInfo& TexLoadInfo, + IRenderDevice* pDevice, + ITexture** ppTexture) { static constexpr Uint8 KTX10FileIdentifier[12] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}; - const Uint8* pData = reinterpret_cast(pKTXData->GetDataPtr()); - const auto DataSize = pKTXData->GetSize(); + const Uint8* pData = reinterpret_cast(pKTXData->GetDataPtr()); + const auto DataSize = pKTXData->GetSize(); if (DataSize >= 12 && memcmp(pData, KTX10FileIdentifier, sizeof(KTX10FileIdentifier)) == 0) { pData += sizeof(KTX10FileIdentifier); - const KTX10Header& Header = *reinterpret_cast(pData); - pData += sizeof(KTX10Header); - // Skip key value data - pData += Header.BytesOfKeyValueData; + const KTX10Header& Header = *reinterpret_cast(pData); + pData += sizeof(KTX10Header); + // Skip key value data + pData += Header.BytesOfKeyValueData; TextureDesc TexDesc; - TexDesc.Name = TexLoadInfo.Name; - TexDesc.Format = FindDiligentTextureFormat(Header.GLInternalFormat); + TexDesc.Name = TexLoadInfo.Name; + TexDesc.Format = FindDiligentTextureFormat(Header.GLInternalFormat); if (TexDesc.Format == TEX_FORMAT_UNKNOWN) LOG_ERROR_AND_THROW("Failed to find appropriate Diligent format for internal gl format ", Header.GLInternalFormat); TexDesc.Width = Header.Width; TexDesc.Height = std::max(Header.Height, 1u); - TexDesc.Depth = std::max(Header.Depth, 1u); + TexDesc.Depth = std::max(Header.Depth, 1u); TexDesc.MipLevels = std::max(Header.NumberOfMipmapLevels, 1u); TexDesc.BindFlags = TexLoadInfo.BindFlags; TexDesc.Usage = TexLoadInfo.Usage; - auto NumFaces = std::max(Header.NumberOfFaces, 1u); + auto NumFaces = std::max(Header.NumberOfFaces, 1u); if (NumFaces == 6) { TexDesc.ArraySize = std::max(Header.NumberOfArrayElements, 1u) * NumFaces; - TexDesc.Type = TexDesc.ArraySize > 6 ? RESOURCE_DIM_TEX_CUBE_ARRAY : RESOURCE_DIM_TEX_CUBE; + TexDesc.Type = TexDesc.ArraySize > 6 ? RESOURCE_DIM_TEX_CUBE_ARRAY : RESOURCE_DIM_TEX_CUBE; } else { if (TexDesc.Depth > 1) { TexDesc.ArraySize = 1; - TexDesc.Type = RESOURCE_DIM_TEX_3D; + TexDesc.Type = RESOURCE_DIM_TEX_3D; } else { TexDesc.ArraySize = std::max(Header.NumberOfArrayElements, 1u); - TexDesc.Type = TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_2D_ARRAY : RESOURCE_DIM_TEX_2D; + TexDesc.Type = TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_2D_ARRAY : RESOURCE_DIM_TEX_2D; } } - auto ArraySize = (TexDesc.Type != RESOURCE_DIM_TEX_3D ? TexDesc.ArraySize : 1); + auto ArraySize = (TexDesc.Type != RESOURCE_DIM_TEX_3D ? TexDesc.ArraySize : 1); std::vector SubresData(TexDesc.MipLevels * ArraySize); - for (Uint32 mip = 0; mip < TexDesc.MipLevels; ++mip) - { - pData += sizeof(std::uint32_t); + for (Uint32 mip = 0; mip < TexDesc.MipLevels; ++mip) + { + pData += sizeof(std::uint32_t); auto MipInfo = GetMipLevelProperties(TexDesc, mip); - for (Uint32 layer = 0; layer < ArraySize; ++layer) + for (Uint32 layer = 0; layer < ArraySize; ++layer) { SubresData[mip + layer * TexDesc.MipLevels] = TextureSubResData{pData, MipInfo.RowSize, MipInfo.DepthSliceSize}; pData += Align(MipInfo.MipSize, 4u); } - } + } VERIFY(pData - reinterpret_cast(pKTXData->GetDataPtr()) == static_cast(DataSize), "Unexpected data size"); TextureData InitData(SubresData.data(), static_cast(SubresData.size())); @@ -276,4 +278,4 @@ void CreateTextureFromKTX( IDataBlob* pKTXData, } } -} +} // namespace Diligent diff --git a/TextureLoader/src/TextureLoader.cpp b/TextureLoader/src/TextureLoader.cpp index 4451494..ec4f774 100644 --- a/TextureLoader/src/TextureLoader.cpp +++ b/TextureLoader/src/TextureLoader.cpp @@ -35,77 +35,71 @@ namespace Diligent { -template +template ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelType c3) { constexpr float NormVal = static_cast(std::numeric_limits::max()); - float fc0 = static_cast(c0) / NormVal; - float fc1 = static_cast(c1) / NormVal; - float fc2 = static_cast(c2) / NormVal; - float fc3 = static_cast(c3) / NormVal; - - float fLinearAverage = (SRGBToLinear( fc0 ) + SRGBToLinear( fc1 ) + SRGBToLinear( fc2 ) + SRGBToLinear( fc3 )) / 4.f; - float fSRGBAverage = LinearToSRGB(fLinearAverage); - Int32 SRGBAverage = static_cast(fSRGBAverage * NormVal); - SRGBAverage = std::min(SRGBAverage, static_cast( std::numeric_limits::max()) ); - SRGBAverage = std::max(SRGBAverage, static_cast( std::numeric_limits::min()) ); + float fc0 = static_cast(c0) / NormVal; + float fc1 = static_cast(c1) / NormVal; + float fc2 = static_cast(c2) / NormVal; + float fc3 = static_cast(c3) / NormVal; + + float fLinearAverage = (SRGBToLinear(fc0) + SRGBToLinear(fc1) + SRGBToLinear(fc2) + SRGBToLinear(fc3)) / 4.f; + float fSRGBAverage = LinearToSRGB(fLinearAverage); + Int32 SRGBAverage = static_cast(fSRGBAverage * NormVal); + SRGBAverage = std::min(SRGBAverage, static_cast(std::numeric_limits::max())); + SRGBAverage = std::max(SRGBAverage, static_cast(std::numeric_limits::min())); return static_cast(SRGBAverage); } -template < typename ChannelType > -void ComputeCoarseMip(Uint32 NumChannels, bool IsSRGB, - const void* pFineMip, - Uint32 FineMipStride, - void* pCoarseMip, - Uint32 CoarseMipStride, - Uint32 CoarseMipWidth, - Uint32 CoarseMipHeight) +template +void ComputeCoarseMip(Uint32 NumChannels, bool IsSRGB, const void* pFineMip, Uint32 FineMipStride, void* pCoarseMip, Uint32 CoarseMipStride, Uint32 CoarseMipWidth, Uint32 CoarseMipHeight) { - for( size_t row = 0; row < size_t{CoarseMipHeight}; ++row ) - for( size_t col = 0; col < size_t{CoarseMipWidth}; ++col ) + for (size_t row = 0; row < size_t{CoarseMipHeight}; ++row) + for (size_t col = 0; col < size_t{CoarseMipWidth}; ++col) { - auto FineRow0 = reinterpret_cast( reinterpret_cast(pFineMip) + row * 2 * size_t{FineMipStride} ); - auto FineRow1 = reinterpret_cast( reinterpret_cast(pFineMip) + (row * 2 + 1 ) * size_t{FineMipStride} ); - - for( Uint32 c = 0; c < NumChannels; ++c ) + auto FineRow0 = reinterpret_cast(reinterpret_cast(pFineMip) + row * 2 * size_t{FineMipStride}); + auto FineRow1 = reinterpret_cast(reinterpret_cast(pFineMip) + (row * 2 + 1) * size_t{FineMipStride}); + + for (Uint32 c = 0; c < NumChannels; ++c) { - auto Col00 = FineRow0[ col*2 * NumChannels + c ]; - auto Col01 = FineRow0[ (col*2+1) * NumChannels + c ]; - auto Col10 = FineRow1[ col*2 * NumChannels + c ]; - auto Col11 = FineRow1[ (col*2+1) * NumChannels + c ]; - auto &DstCol = reinterpret_cast(reinterpret_cast(pCoarseMip) + row * size_t{CoarseMipStride})[col * NumChannels + c]; - if( IsSRGB ) - DstCol = SRGBAverage( Col00, Col01, Col10, Col11 ); + auto Col00 = FineRow0[col * 2 * NumChannels + c]; + auto Col01 = FineRow0[(col * 2 + 1) * NumChannels + c]; + auto Col10 = FineRow1[col * 2 * NumChannels + c]; + auto Col11 = FineRow1[(col * 2 + 1) * NumChannels + c]; + auto& DstCol = reinterpret_cast(reinterpret_cast(pCoarseMip) + row * size_t{CoarseMipStride})[col * NumChannels + c]; + if (IsSRGB) + DstCol = SRGBAverage(Col00, Col01, Col10, Col11); else - DstCol = (Col00 + Col01 + Col10 + Col11)/4; + DstCol = (Col00 + Col01 + Col10 + Col11) / 4; } } } -template < typename ChannelType > -void RGBToRGBA(const void* pRGBData, - Uint32 RGBStride, - void* pRGBAData, - Uint32 RGBAStride, - Uint32 Width, - Uint32 Height) +template +void RGBToRGBA(const void* pRGBData, + Uint32 RGBStride, + void* pRGBAData, + Uint32 RGBAStride, + Uint32 Width, + Uint32 Height) { - for( size_t row = 0; row < size_t{Height}; ++row ) - for( size_t col = 0; col < size_t{Width}; ++col ) + for (size_t row = 0; row < size_t{Height}; ++row) + for (size_t col = 0; col < size_t{Width}; ++col) { - for( int c = 0; c < 3; ++c ) + for (int c = 0; c < 3; ++c) { - reinterpret_cast( (reinterpret_cast(pRGBAData) + size_t{RGBAStride} * row) ) [col *4 + c] = - reinterpret_cast( (reinterpret_cast(pRGBData) + size_t{RGBStride} * row))[col*3 + c]; + reinterpret_cast((reinterpret_cast(pRGBAData) + size_t{RGBAStride} * row))[col * 4 + c] = + reinterpret_cast((reinterpret_cast(pRGBData) + size_t{RGBStride} * row))[col * 3 + c]; } - reinterpret_cast( (reinterpret_cast(pRGBAData) + size_t{RGBAStride} * row) ) [col *4 + 3] = std::numeric_limits::max(); + reinterpret_cast((reinterpret_cast(pRGBAData) + size_t{RGBAStride} * row))[col * 4 + 3] = std::numeric_limits::max(); } } -void CreateTextureFromImage( Image* pSrcImage, - const TextureLoadInfo& TexLoadInfo, - IRenderDevice* pDevice, - ITexture** ppTexture ) +void CreateTextureFromImage(Image* pSrcImage, + const TextureLoadInfo& TexLoadInfo, + IRenderDevice* pDevice, + ITexture** ppTexture) { const auto& ImgDesc = pSrcImage->GetDesc(); TextureDesc TexDesc; @@ -113,103 +107,111 @@ void CreateTextureFromImage( Image* pSrcImage, TexDesc.Type = RESOURCE_DIM_TEX_2D; TexDesc.Width = ImgDesc.Width; TexDesc.Height = ImgDesc.Height; - TexDesc.MipLevels = ComputeMipLevelsCount( TexDesc.Width, TexDesc.Height ); - if( TexLoadInfo.MipLevels > 0 ) + TexDesc.MipLevels = ComputeMipLevelsCount(TexDesc.Width, TexDesc.Height); + if (TexLoadInfo.MipLevels > 0) TexDesc.MipLevels = std::min(TexDesc.MipLevels, TexLoadInfo.MipLevels); TexDesc.Usage = TexLoadInfo.Usage; TexDesc.BindFlags = TexLoadInfo.BindFlags; TexDesc.Format = TexLoadInfo.Format; TexDesc.CPUAccessFlags = TexLoadInfo.CPUAccessFlags; - auto ChannelDepth = GetValueSize(ImgDesc.ComponentType) * 8; + auto ChannelDepth = GetValueSize(ImgDesc.ComponentType) * 8; Uint32 NumComponents = ImgDesc.NumComponents == 3 ? 4 : ImgDesc.NumComponents; - bool IsSRGB = (ImgDesc.NumComponents >= 3 && ChannelDepth == 8) ? TexLoadInfo.IsSRGB : false; - if( TexDesc.Format == TEX_FORMAT_UNKNOWN ) + bool IsSRGB = (ImgDesc.NumComponents >= 3 && ChannelDepth == 8) ? TexLoadInfo.IsSRGB : false; + if (TexDesc.Format == TEX_FORMAT_UNKNOWN) { - if( ChannelDepth == 8 ) + if (ChannelDepth == 8) { - switch( NumComponents ) + switch (NumComponents) { case 1: TexDesc.Format = TEX_FORMAT_R8_UNORM; break; case 2: TexDesc.Format = TEX_FORMAT_RG8_UNORM; break; case 4: TexDesc.Format = IsSRGB ? TEX_FORMAT_RGBA8_UNORM_SRGB : TEX_FORMAT_RGBA8_UNORM; break; - default: LOG_ERROR_AND_THROW( "Unexpected number of color channels (", ImgDesc.NumComponents, ")" ); + default: LOG_ERROR_AND_THROW("Unexpected number of color channels (", ImgDesc.NumComponents, ")"); } } - else if( ChannelDepth == 16 ) + else if (ChannelDepth == 16) { - switch( NumComponents ) + switch (NumComponents) { case 1: TexDesc.Format = TEX_FORMAT_R16_UNORM; break; case 2: TexDesc.Format = TEX_FORMAT_RG16_UNORM; break; case 4: TexDesc.Format = TEX_FORMAT_RGBA16_UNORM; break; - default: LOG_ERROR_AND_THROW( "Unexpected number of color channels (", ImgDesc.NumComponents, ")" ); + default: LOG_ERROR_AND_THROW("Unexpected number of color channels (", ImgDesc.NumComponents, ")"); } } else - LOG_ERROR_AND_THROW( "Unsupported color channel depth (", ChannelDepth, ")" ); + LOG_ERROR_AND_THROW("Unsupported color channel depth (", ChannelDepth, ")"); } else { - const auto& TexFmtDesc = GetTextureFormatAttribs( TexDesc.Format ); - if( TexFmtDesc.NumComponents != NumComponents ) - LOG_ERROR_AND_THROW( "Incorrect number of components ", ImgDesc.NumComponents, ") for texture format ", TexFmtDesc.Name ); - if( TexFmtDesc.ComponentSize != ChannelDepth / 8 ) - LOG_ERROR_AND_THROW( "Incorrect channel size ", ChannelDepth, ") for texture format ", TexFmtDesc.Name ); + const auto& TexFmtDesc = GetTextureFormatAttribs(TexDesc.Format); + if (TexFmtDesc.NumComponents != NumComponents) + LOG_ERROR_AND_THROW("Incorrect number of components ", ImgDesc.NumComponents, ") for texture format ", TexFmtDesc.Name); + if (TexFmtDesc.ComponentSize != ChannelDepth / 8) + LOG_ERROR_AND_THROW("Incorrect channel size ", ChannelDepth, ") for texture format ", TexFmtDesc.Name); } - - std::vector pSubResources(TexDesc.MipLevels); - std::vector< std::vector > Mips(TexDesc.MipLevels); - if( ImgDesc.NumComponents == 3 ) + std::vector pSubResources(TexDesc.MipLevels); + std::vector> Mips(TexDesc.MipLevels); + + if (ImgDesc.NumComponents == 3) { - VERIFY_EXPR( NumComponents == 4 ); + VERIFY_EXPR(NumComponents == 4); auto RGBAStride = ImgDesc.Width * NumComponents * ChannelDepth / 8; - RGBAStride = (RGBAStride + 3) & (-4); + RGBAStride = (RGBAStride + 3) & (-4); Mips[0].resize(size_t{RGBAStride} * size_t{ImgDesc.Height}); - pSubResources[0].pData = Mips[0].data(); + pSubResources[0].pData = Mips[0].data(); pSubResources[0].Stride = RGBAStride; - if( ChannelDepth == 8 ) - RGBToRGBA( pSrcImage->GetData()->GetDataPtr(), ImgDesc.RowStride, - Mips[0].data(), RGBAStride, - ImgDesc.Width, ImgDesc.Height); - else if( ChannelDepth == 16 ) - RGBToRGBA( pSrcImage->GetData()->GetDataPtr(), ImgDesc.RowStride, - Mips[0].data(), RGBAStride, - ImgDesc.Width, ImgDesc.Height); + if (ChannelDepth == 8) + { + RGBToRGBA(pSrcImage->GetData()->GetDataPtr(), ImgDesc.RowStride, + Mips[0].data(), RGBAStride, + ImgDesc.Width, ImgDesc.Height); + } + else if (ChannelDepth == 16) + { + RGBToRGBA(pSrcImage->GetData()->GetDataPtr(), ImgDesc.RowStride, + Mips[0].data(), RGBAStride, + ImgDesc.Width, ImgDesc.Height); + } } else { - pSubResources[0].pData = pSrcImage->GetData()->GetDataPtr(); + pSubResources[0].pData = pSrcImage->GetData()->GetDataPtr(); pSubResources[0].Stride = ImgDesc.RowStride; } - + auto MipWidth = TexDesc.Width; auto MipHeight = TexDesc.Height; - for( Uint32 m = 1; m < TexDesc.MipLevels; ++m ) + for (Uint32 m = 1; m < TexDesc.MipLevels; ++m) { - auto CoarseMipWidth = std::max(MipWidth/2u, 1u); - auto CoarseMipHeight = std::max(MipHeight/2u, 1u); + auto CoarseMipWidth = std::max(MipWidth / 2u, 1u); + auto CoarseMipHeight = std::max(MipHeight / 2u, 1u); auto CoarseMipStride = CoarseMipWidth * NumComponents * ChannelDepth / 8; - CoarseMipStride = (CoarseMipStride + 3) & (-4); + CoarseMipStride = (CoarseMipStride + 3) & (-4); Mips[m].resize(size_t{CoarseMipStride} * size_t{CoarseMipHeight}); if (TexLoadInfo.GenerateMips) { if (ChannelDepth == 8) + { ComputeCoarseMip(NumComponents, IsSRGB, - pSubResources[m - 1].pData, pSubResources[m - 1].Stride, - Mips[m].data(), CoarseMipStride, - CoarseMipWidth, CoarseMipHeight); + pSubResources[m - 1].pData, pSubResources[m - 1].Stride, + Mips[m].data(), CoarseMipStride, + CoarseMipWidth, CoarseMipHeight); + } else if (ChannelDepth == 16) + { ComputeCoarseMip(NumComponents, IsSRGB, - pSubResources[m - 1].pData, pSubResources[m - 1].Stride, - Mips[m].data(), CoarseMipStride, - CoarseMipWidth, CoarseMipHeight); + pSubResources[m - 1].pData, pSubResources[m - 1].Stride, + Mips[m].data(), CoarseMipStride, + CoarseMipWidth, CoarseMipHeight); + } } - pSubResources[m].pData = Mips[m].data(); + pSubResources[m].pData = Mips[m].data(); pSubResources[m].Stride = CoarseMipStride; MipWidth = CoarseMipWidth; @@ -217,18 +219,18 @@ void CreateTextureFromImage( Image* pSrcImage, } TextureData TexData; - TexData.pSubResources = pSubResources.data(); + TexData.pSubResources = pSubResources.data(); TexData.NumSubresources = TexDesc.MipLevels; - pDevice->CreateTexture( TexDesc, &TexData, ppTexture ); + pDevice->CreateTexture(TexDesc, &TexData, ppTexture); } -void CreateTextureFromDDS( IDataBlob* pDDSData, - const TextureLoadInfo& TexLoadInfo, - IRenderDevice* pDevice, - ITexture** ppTexture ) +void CreateTextureFromDDS(IDataBlob* pDDSData, + const TextureLoadInfo& TexLoadInfo, + IRenderDevice* pDevice, + ITexture** ppTexture) { - CreateDDSTextureFromMemoryEx(pDevice, + CreateDDSTextureFromMemoryEx(pDevice, reinterpret_cast(pDDSData->GetDataPtr()), static_cast(pDDSData->GetSize()), 0, // maxSize @@ -237,8 +239,8 @@ void CreateTextureFromDDS( IDataBlob* pDDSData, TexLoadInfo.BindFlags, TexLoadInfo.CPUAccessFlags, MISC_TEXTURE_FLAG_NONE, // miscFlags - TexLoadInfo.IsSRGB, // forceSRGB - ppTexture ); + TexLoadInfo.IsSRGB, // forceSRGB + ppTexture); } -} +} // namespace Diligent diff --git a/TextureLoader/src/TextureUtilities.cpp b/TextureLoader/src/TextureUtilities.cpp index ff95a70..32019ae 100644 --- a/TextureLoader/src/TextureUtilities.cpp +++ b/TextureLoader/src/TextureUtilities.cpp @@ -32,26 +32,27 @@ namespace Diligent { -void CreateTextureFromFile(const Char* FilePath, - const TextureLoadInfo& TexLoadInfo, - IRenderDevice* pDevice, - ITexture** ppTexture ) +void CreateTextureFromFile(const Char* FilePath, + const TextureLoadInfo& TexLoadInfo, + IRenderDevice* pDevice, + ITexture** ppTexture) { RefCntAutoPtr pImage; RefCntAutoPtr pRawData; - auto ImgFmt = CreateImageFromFile( FilePath, &pImage, &pRawData ); + + auto ImgFmt = CreateImageFromFile(FilePath, &pImage, &pRawData); if (pImage) - CreateTextureFromImage( pImage, TexLoadInfo, pDevice, ppTexture ); + CreateTextureFromImage(pImage, TexLoadInfo, pDevice, ppTexture); else if (pRawData) { if (ImgFmt == EImageFileFormat::dds) - CreateTextureFromDDS( pRawData, TexLoadInfo, pDevice, ppTexture ); + CreateTextureFromDDS(pRawData, TexLoadInfo, pDevice, ppTexture); else if (ImgFmt == EImageFileFormat::ktx) - CreateTextureFromKTX( pRawData, TexLoadInfo, pDevice, ppTexture ); + CreateTextureFromKTX(pRawData, TexLoadInfo, pDevice, ppTexture); else UNEXPECTED("Unexpected format"); } } -} +} // namespace Diligent diff --git a/TextureLoader/src/pch.cpp b/TextureLoader/src/pch.cpp index 6feaa0c..3eef26a 100644 --- a/TextureLoader/src/pch.cpp +++ b/TextureLoader/src/pch.cpp @@ -22,4 +22,3 @@ */ #include "pch.h" - -- cgit v1.2.3