Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,35 @@ endif()
# Link against compressonator libs & set runtime lib
if (UNIX)
if (VTFLIB_SHARED)
target_link_directories(vtflib PUBLIC thirdparty/lib/x64)
target_link_libraries(vtflib PUBLIC CMP_Compressonator pthread)
target_link_directories(vtflib PUBLIC thirdparty/lib/linux_x86_64)
target_link_libraries(vtflib PUBLIC
Compressonator
CMP_Core
CMP_Core_AVX
CMP_Core_AVX512
CMP_Core_SSE
pthread
Copy link
Member

@craftablescience craftablescience Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use cmake's Threads::Threads target instead of pthread

from stackoverflow:

set(CMAKE_THREAD_PREFER_PTHREAD ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_executable(test test.cpp)
target_link_libraries(test Threads::Threads)

also, is this actually required? i cant remember any vtflib code that uses pthreads or any threading stuff in std

)
endif()
if (VTFLIB_STATIC)
target_link_directories(vtflib_static PUBLIC thirdparty/lib/x64)
target_link_libraries(vtflib_static PUBLIC CMP_Compressonator pthread)
target_link_directories(vtflib_static PUBLIC thirdparty/lib/linux_x86_64)
target_link_libraries(vtflib_static PUBLIC
Compressonator
CMP_Core
CMP_Core_AVX
CMP_Core_AVX512
CMP_Core_SSE
pthread
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

)
endif()
else()
if (VTFLIB_SHARED)
target_link_directories(vtflib PUBLIC thirdparty/lib)
target_link_directories(vtflib PUBLIC thirdparty/lib/win_x86_64)
target_link_libraries(vtflib PUBLIC "Compressonator_MT$<$<CONFIG:Debug>:d>")
endif()
if (VTFLIB_STATIC)
target_link_libraries(vtflib_static PUBLIC "Compressonator_MT$<$<CONFIG:Debug>:d>")
target_link_directories(vtflib_static PUBLIC thirdparty/lib)
target_link_directories(vtflib_static PUBLIC thirdparty/lib/win_x86_64)
endif()
endif()

Expand Down
91 changes: 85 additions & 6 deletions VTFLib/VTFFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ static CMP_FORMAT GetCMPFormat( VTFImageFormat imageFormat, bool bDXT5GA )
// Swizzle is technically wrong for below but we reverse it in the shader!
case IMAGE_FORMAT_ATI2N: return CMP_FORMAT_ATI2N;

case IMAGE_FORMAT_BC6H: return CMP_FORMAT_BC6H;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should come after BC7

case IMAGE_FORMAT_BC7: return CMP_FORMAT_BC7;

default: return CMP_FORMAT_Unknown;
Expand Down Expand Up @@ -3032,7 +3033,8 @@ static SVTFImageFormatInfo VTFImageFormatInfo[] =
{},
{},
{},
{ "BC7", 8, 0, 0, 0, 0, 0, vlTrue, vlTrue } // IMAGE_FORMAT_BC7
{ "BC7", 8, 0, 0, 0, 0, 0, vlTrue, vlTrue }, // IMAGE_FORMAT_BC7
{ "BC6H", 8, 0, 0, 0, 0, 0, vlTrue, vlTrue } // IMAGE_FORMAT_BC6H
};

SVTFImageFormatInfo const &CVTFFile::GetImageFormatInfo(VTFImageFormat ImageFormat)
Expand Down Expand Up @@ -3066,6 +3068,7 @@ vlUInt CVTFFile::ComputeImageSize(vlUInt uiWidth, vlUInt uiHeight, vlUInt uiDept
case IMAGE_FORMAT_DXT3:
case IMAGE_FORMAT_DXT5:
case IMAGE_FORMAT_ATI2N:
case IMAGE_FORMAT_BC6H:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should be after BC7

case IMAGE_FORMAT_BC7:
if(uiWidth < 4 && uiWidth > 0)
uiWidth = 4;
Expand Down Expand Up @@ -3301,20 +3304,23 @@ vlBool CVTFFile::DecompressBCn(const vlByte *src, vlByte *dst, vlUInt uiWidth, v
return vlTrue;
}

//
//-----------------------------------------------------------------------------------------------------
// ConvertFromRGBA8888()
//
// Convert input image data (lpSource) to output image data (lpDest) of format DestFormat.
//
//-----------------------------------------------------------------------------------------------------
vlBool CVTFFile::ConvertFromRGBA8888(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat DestFormat)
{
return CVTFFile::Convert(lpSource, lpDest, uiWidth, uiHeight, IMAGE_FORMAT_RGBA8888, DestFormat);
}

//
//-----------------------------------------------------------------------------------------------------
// CompressBCn()
//
// Compress input image data (lpSource) to output image data (lpDest) of format DestFormat
// where DestFormat is of format BCn. Uses Compressonator library.
//
// where DestFormat is of format BCn. Uses Compressonator library.
// --Does not support BC6H
//-----------------------------------------------------------------------------------------------------
vlBool CVTFFile::CompressBCn(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat DestFormat)
{
CMP_Texture srcTexture = {0};
Expand Down Expand Up @@ -3350,6 +3356,74 @@ vlBool CVTFFile::CompressBCn(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWi
return vlTrue;
}

//-----------------------------------------------------------------------------------------------------
// CompressBC6H()
//
// Compress input image data (lpSource) to output image data (lpDest) of format DestFormat
// where DestFormat is of format BC6H. Uses Compressonator library.
//
// NOTE: For BC6H conversion, an extra step is needed that puts the source data into a half float
// format before conversion. Do not use BCn for BC6H compression. -klaxon
//-----------------------------------------------------------------------------------------------------
vlBool CVTFFile::CompressBC6H(const vlByte* lpSource, vlByte* lpDest, vlUInt uiWidth, vlUInt uiHeight)
{
CMP_Texture srcTexture = { 0 };
srcTexture.dwSize = sizeof(srcTexture);
srcTexture.dwWidth = uiWidth;
srcTexture.dwHeight = uiHeight;
srcTexture.dwPitch = 4 * uiWidth;
srcTexture.format = CMP_FORMAT_RGBA_8888;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the input to this function always assumed to be RGBA8888?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, it was while i was testing things out. ill go ahead and add the SourceFormat param back to make sure an edge case isn't hit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My issue with this code is less about assuming lpSource is always RGBA8888 and more about the potential data loss. If textures are getting squished down to RGBA8888 before being turned into BC6H, what's the point of using BC6H at all

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still blocking merge, basically what JJ said

srcTexture.dwDataSize = uiHeight * srcTexture.dwPitch;
srcTexture.pData = (CMP_BYTE*)lpSource;

CMP_CompressOptions options = { 0 };
options.dwSize = sizeof(options);
options.dwnumThreads = 0;
options.bDXT1UseAlpha = false;

vlByte* lpMidBuf = new vlByte[CVTFFile::ComputeImageSize(uiWidth, uiHeight, 1, IMAGE_FORMAT_RGBA16161616F)];
CMP_Texture midTexture = { 0 };
midTexture.dwSize = sizeof(midTexture);
midTexture.dwWidth = uiWidth;
midTexture.dwHeight = uiHeight;
midTexture.dwPitch = 8 * uiWidth;
midTexture.format = CMP_FORMAT_RGBA_16F;
midTexture.dwDataSize = CMP_CalculateBufferSize(&midTexture);
midTexture.pData = (CMP_BYTE*)lpMidBuf;

CMP_Texture destTexture = { 0 };
destTexture.dwSize = sizeof(destTexture);
destTexture.dwWidth = uiWidth;
destTexture.dwHeight = uiHeight;
destTexture.dwPitch = 0;
destTexture.format = CMP_FORMAT_BC6H;
destTexture.dwDataSize = CMP_CalculateBufferSize(&destTexture);
destTexture.pData = (CMP_BYTE*)lpDest;

// convert to the mid texture first (to pass a half-float format for bc6h)
CMP_ERROR cmp_status = CMP_ConvertTexture(&srcTexture, &midTexture, &options, NULL);
if (cmp_status != CMP_OK)
{
delete[] lpMidBuf;

LastError.Set(GetCMPErrorString(cmp_status));
return vlFalse;
}

cmp_status = CMP_ConvertTexture(&midTexture, &destTexture, &options, NULL);
if (cmp_status != CMP_OK)
{
delete[] lpMidBuf;

LastError.Set(GetCMPErrorString(cmp_status));
return vlFalse;
}

delete[] lpMidBuf;

return vlTrue;
}

typedef vlVoid (*TransformProc)(vlUInt16& R, vlUInt16& G, vlUInt16& B, vlUInt16& A);

vlVoid ToLuminance(vlUInt16& R, vlUInt16& G, vlUInt16& B, vlUInt16& A)
Expand Down Expand Up @@ -3558,6 +3632,7 @@ static SVTFImageConvertInfo VTFImageConvertInfo[IMAGE_FORMAT_COUNT] =
{},
{},
{ 8, 0, 0, 0, 0, 0, -1, -1, -1, -1, vlTrue, vlTrue, NULL, NULL, IMAGE_FORMAT_BC7},
{ 8, 0, 0, 0, 0, 0, -1, -1, -1, -1, vlTrue, vlTrue, NULL, NULL, IMAGE_FORMAT_BC6H}
};

// Get each channels shift and mask (for encoding and decoding).
Expand Down Expand Up @@ -3857,6 +3932,7 @@ vlBool CVTFFile::Convert(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth,
case IMAGE_FORMAT_DXT5:
case IMAGE_FORMAT_ATI2N:
case IMAGE_FORMAT_ATI1N:
case IMAGE_FORMAT_BC6H:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should be below.. yeah you get the idea

case IMAGE_FORMAT_BC7:
bResult = CVTFFile::DecompressBCn(lpSource, lpConvBuf, uiWidth, uiHeight, SourceFormat);
break;
Expand All @@ -3879,6 +3955,9 @@ vlBool CVTFFile::Convert(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth,
case IMAGE_FORMAT_BC7:
bResult = CVTFFile::CompressBCn(lpConvBuf ? lpConvBuf : lpSource, lpDest, uiWidth, uiHeight, DestFormat);
break;
case IMAGE_FORMAT_BC6H:
bResult = CVTFFile::CompressBC6H(lpConvBuf ? lpConvBuf : lpSource, lpDest, uiWidth, uiHeight);
break;
default:
bResult = CVTFFile::Convert(lpConvBuf ? lpConvBuf : lpSource, lpDest, uiWidth, uiHeight, IMAGE_FORMAT_RGBA8888, DestFormat);
break;
Expand Down
5 changes: 4 additions & 1 deletion VTFLib/VTFFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,12 @@ namespace VTFLib
// BCn format decompression function
static vlBool DecompressBCn(const vlByte *src, vlByte *dst, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat SourceFormat);

// BCn format compression function
// BCn format compression function - not for bc6h
static vlBool CompressBCn(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat DestFormat);

// BC6H specific compression function
static vlBool CompressBC6H(const vlByte* lpSource, vlByte* lpDest, vlUInt uiWidth, vlUInt uiHeight);

public:

//! Correct and images gamma.
Expand Down
3 changes: 2 additions & 1 deletion VTFLib/VTFFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ typedef enum tagVTFImageFormat

IMAGE_FORMAT_ATI2N, //!< = Red, Green BC5 compressed format - 8 bpp
IMAGE_FORMAT_ATI1N, //!< = Red BC4 compressed format - 4 bpp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space

IMAGE_FORMAT_BC7 = 70, //!< = Red, Green, Blue, Alpha BC7 compressed format - 8 bpp
IMAGE_FORMAT_BC6H, //!< = Red, Green, Blue, BC6H (signed) compressed format - 8 bpp
/*
XBox:
IMAGE_FORMAT_X360_DST16,
Expand Down
Loading