Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
91 changes: 84 additions & 7 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
Copy Markdown
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 @@ -3031,7 +3032,7 @@ static SVTFImageFormatInfo VTFImageFormatInfo[] =
{},
{},
{},
{},
{ "BC6H", 8, 0, 0, 0, 0, 0, vlTrue, vlTrue }, // IMAGE_FORMAT_BC6H
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will need to get moved to after IMAGE_FORMAT_BC7 when you update the enum (see the later comment)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

{ "BC7", 8, 0, 0, 0, 0, 0, vlTrue, vlTrue } // IMAGE_FORMAT_BC7
};

Expand Down Expand Up @@ -3066,6 +3067,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
Copy Markdown
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 +3303,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 +3355,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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 = 4 * uiWidth;
midTexture.format = CMP_FORMAT_RGBA_16F;
midTexture.dwDataSize = CMP_CalculateBufferSize(&srcTexture);
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 @@ -3556,7 +3629,7 @@ static SVTFImageConvertInfo VTFImageConvertInfo[IMAGE_FORMAT_COUNT] =
{},
{},
{},
{},
{ 8, 0, 0, 0, 0, 0, -1, -1, -1, -1, vlTrue, vlTrue, NULL, NULL, IMAGE_FORMAT_BC6H},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This will need to get moved to after IMAGE_FORMAT_BC7 when you update the enum (see the later comment)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

{ 8, 0, 0, 0, 0, 0, -1, -1, -1, -1, vlTrue, vlTrue, NULL, NULL, IMAGE_FORMAT_BC7},
};

Expand Down Expand Up @@ -3857,6 +3930,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
Copy Markdown
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 +3953,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
3 changes: 3 additions & 0 deletions VTFLib/VTFFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ namespace VTFLib
// BCn format compression function
static vlBool CompressBCn(const vlByte *lpSource, vlByte *lpDest, vlUInt uiWidth, vlUInt uiHeight, VTFImageFormat DestFormat);

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

public:

//! Correct and images gamma.
Expand Down
1 change: 1 addition & 0 deletions VTFLib/VTFFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef enum tagVTFImageFormat
IMAGE_FORMAT_ATI2N, //!< = Red, Green BC5 compressed format - 8 bpp
IMAGE_FORMAT_ATI1N, //!< = Red BC4 compressed format - 4 bpp

IMAGE_FORMAT_BC6H = 69, //!< = Red, Green, Blue, BC6H compressed format - 8 bpp
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMAGE_FORMAT_BC6H should be 71

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Cool, I'll change the rest of the enums / lists to reflect that also.

IMAGE_FORMAT_BC7 = 70, //!< = Red, Green, Blue, Alpha BC7 compressed format - 8 bpp
/*
XBox:
Expand Down
Loading