Browse Source

Math: implement BC2 flipping as well.

It's a weird format nobody uses, but since I have the full context in my
head now, it's easy to do.
pull/620/head
Vladimír Vondruš 3 years ago
parent
commit
410441791c
  1. 38
      src/Magnum/Math/ColorBatch.cpp
  2. 19
      src/Magnum/Math/ColorBatch.h
  3. 1
      src/Magnum/Math/Test/CMakeLists.txt
  4. 25
      src/Magnum/Math/Test/ColorBatchTest.cpp
  5. BIN
      src/Magnum/Math/Test/ColorBatchTestFiles/bc2.png
  6. 6
      src/Magnum/PixelFormat.h

38
src/Magnum/Math/ColorBatch.cpp

@ -60,6 +60,36 @@ inline void yFlipBc1BlockInPlace(char* const data) {
} }
} }
inline void yFlipBc2BlockInPlace(char* data) {
/* The 128-bit block is laid out as follows:
- 8 bytes for 4x4 4-bit alpha values, same order as BC1
- 2 bytes for first endpoint color
- 2 bytes for second endpoint color
- 4 bytes for 4x4 2-bit color indices, same order as BC1
Which means, swapping the alphas and then doing the same as with BC1
for the color indices in the second half.
https://learn.microsoft.com/cs-cz/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-block-compression#bc2 */
{
char tmp1 = data[0];
char tmp2 = data[1];
data[0] = data[6];
data[1] = data[7];
data[6] = tmp1;
data[7] = tmp2;
} {
char tmp1 = data[2];
char tmp2 = data[3];
data[2] = data[4];
data[3] = data[5];
data[4] = tmp1;
data[5] = tmp2;
}
yFlipBc1BlockInPlace(data + 8);
}
inline void yFlipBc4BlockInPlace(char* data) { inline void yFlipBc4BlockInPlace(char* data) {
/* The 64-bit block is laid out as follows: /* The 64-bit block is laid out as follows:
@ -173,6 +203,14 @@ void yFlipBc1InPlace(const Containers::StridedArrayView4D<char>& blocks) {
); );
} }
void yFlipBc2InPlace(const Containers::StridedArrayView4D<char>& blocks) {
yFlipBlocksInPlace<16, yFlipBc2BlockInPlace>(blocks
#ifndef CORRADE_NO_ASSERT
, "Math::yFlipBc2InPlace():"
#endif
);
}
void yFlipBc3InPlace(const Containers::StridedArrayView4D<char>& blocks) { void yFlipBc3InPlace(const Containers::StridedArrayView4D<char>& blocks) {
yFlipBlocksInPlace<16, yFlipBc3BlockInPlace>(blocks yFlipBlocksInPlace<16, yFlipBc3BlockInPlace>(blocks
#ifndef CORRADE_NO_ASSERT #ifndef CORRADE_NO_ASSERT

19
src/Magnum/Math/ColorBatch.h

@ -57,7 +57,24 @@ expected to be contiguous with size of 8.
*/ */
MAGNUM_EXPORT void yFlipBc1InPlace(const Corrade::Containers::StridedArrayView4D<char>& blocks); MAGNUM_EXPORT void yFlipBc1InPlace(const Corrade::Containers::StridedArrayView4D<char>& blocks);
/** @todo BC2, if used at all for anything */ /**
@brief Y-flip BC2 texture blocks in-place
@m_since_latest
Performs a Y flip of given 3D image by flipping block order and modifying
internal block representation to encode the same information, just upside down.
No decoding or re-encoding of the block data is performed, thus the operation
is lossless. However note that this operation flips full blocks --- if size of
the actual image isn't whole blocks, the flipped image will be shifted compared
to the original, possibly with garbage data appearing in the first few rows.
First dimension is expected to be image slices, second block rows, third
2D blocks, fourth the 128-bit 4x4 block data, i.e. the last dimension is
expected to be contiguous with size of 16.
@see @ref CompressedPixelFormat::Bc2RGBAUnorm,
@ref CompressedPixelFormat::Bc2RGBASrgb
*/
MAGNUM_EXPORT void yFlipBc2InPlace(const Corrade::Containers::StridedArrayView4D<char>& blocks);
/** /**
@brief Y-flip BC3 texture blocks in-place @brief Y-flip BC3 texture blocks in-place

1
src/Magnum/Math/Test/CMakeLists.txt

@ -50,6 +50,7 @@ corrade_add_test(MathColorBatchTest ColorBatchTest.cpp
LIBRARIES MagnumMathTestLib MagnumDebugTools LIBRARIES MagnumMathTestLib MagnumDebugTools
FILES FILES
ColorBatchTestFiles/bc1.png ColorBatchTestFiles/bc1.png
ColorBatchTestFiles/bc2.png
ColorBatchTestFiles/bc3.png ColorBatchTestFiles/bc3.png
ColorBatchTestFiles/bc4.png ColorBatchTestFiles/bc4.png
ColorBatchTestFiles/bc5.png ColorBatchTestFiles/bc5.png

25
src/Magnum/Math/Test/ColorBatchTest.cpp

@ -192,6 +192,31 @@ const struct {
'\xdd', '\xff', '\xa8', '\x6b', '\x09', '\x25', '\x95', '\x55', '\xdd', '\xff', '\xa8', '\x6b', '\x09', '\x25', '\x95', '\x55',
'\x79', '\xd6', '\xa7', '\x39', '\x35', '\xd5', '\x55', '\x5c' '\x79', '\xd6', '\xa7', '\x39', '\x35', '\xd5', '\x55', '\x5c'
}}}, }}},
{"BC2", CompressedPixelFormat::Bc2RGBAUnorm, {1, 4}, {InPlaceInit, {
/* ./extract-interesting-blocks.py testcard_bc2.dds bc2.png --offset 88
(image taken from the bcdec repository test files) */
/* [7, 52], 2.427 */
'\xf8', '\xff', '\x91', '\xff', '\x20', '\xfa', '\x00', '\xc4',
'\xff', '\xff', '\x0f', '\xb3', '\x55', '\x55', '\x54', '\x50',
/* [6, 51], 2.427 */
'\x90', '\xff', '\x20', '\xfb', '\x00', '\xd4', '\x00', '\x60',
'\xff', '\xff', '\x0f', '\xb3', '\x54', '\x54', '\x50', '\x40',
/* [53, 8], 2.412 */
'\x6d', '\x00', '\xdf', '\x06', '\xff', '\x7e', '\xff', '\xff',
'\xff', '\xff', '\xf6', '\x63', '\x05', '\x15', '\x55', '\x55',
/* [10, 8], 2.412 */
'\x00', '\xd6', '\x60', '\xfd', '\xe7', '\xff', '\xff', '\xff',
'\xff', '\xff', '\xf6', '\x63', '\x50', '\x54', '\x55', '\x55',
}}, yFlipBc2InPlace, "bc2.png", {InPlaceInit, {
'\xff', '\xff', '\xe7', '\xff', '\x60', '\xfd', '\x00', '\xd6',
'\xff', '\xff', '\xf6', '\x63', '\x55', '\x55', '\x54', '\x50',
'\xff', '\xff', '\xff', '\x7e', '\xdf', '\x06', '\x6d', '\x00',
'\xff', '\xff', '\xf6', '\x63', '\x55', '\x55', '\x15', '\x05',
'\x00', '\x60', '\x00', '\xd4', '\x20', '\xfb', '\x90', '\xff',
'\xff', '\xff', '\x0f', '\xb3', '\x40', '\x50', '\x54', '\x54',
'\x00', '\xc4', '\x20', '\xfa', '\x91', '\xff', '\xf8', '\xff',
'\xff', '\xff', '\x0f', '\xb3', '\x50', '\x54', '\x55', '\x55'
}}},
{"BC3", CompressedPixelFormat::Bc3RGBAUnorm, {1, 4}, {InPlaceInit, { {"BC3", CompressedPixelFormat::Bc3RGBAUnorm, {1, 4}, {InPlaceInit, {
/* ./extract-interesting-blocks.py dice_bc3.dds bc3.png --offset 148 /* ./extract-interesting-blocks.py dice_bc3.dds bc3.png --offset 148
(image taken from the bcdec repository test files) */ (image taken from the bcdec repository test files) */

BIN
src/Magnum/Math/Test/ColorBatchTestFiles/bc2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 B

6
src/Magnum/PixelFormat.h

@ -1078,7 +1078,8 @@ enum class CompressedPixelFormat: UnsignedInt {
* @ref Vk::PixelFormat::CompressedBc2RGBAUnorm; * @ref Vk::PixelFormat::CompressedBc2RGBAUnorm;
* @m_class{m-doc-external} [DXGI_FORMAT_BC2_UNORM](https://docs.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format) * @m_class{m-doc-external} [DXGI_FORMAT_BC2_UNORM](https://docs.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format)
* or @m_class{m-doc-external} [MTLPixelFormatBC2_RGBA](https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbc2_rgba?language=objc). * or @m_class{m-doc-external} [MTLPixelFormatBC2_RGBA](https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbc2_rgba?language=objc).
* @see @relativeref{Trade,BcDecImageConverter} * @see @ref Math::yFlipBc2InPlace(),
* @relativeref{Trade,BcDecImageConverter}
* @m_keywords{DXGI_FORMAT_BC2_UNORM MTLPixelFormatBC2_RGBA} * @m_keywords{DXGI_FORMAT_BC2_UNORM MTLPixelFormatBC2_RGBA}
*/ */
Bc2RGBAUnorm, Bc2RGBAUnorm,
@ -1094,7 +1095,8 @@ enum class CompressedPixelFormat: UnsignedInt {
* @ref Vk::PixelFormat::CompressedBc2RGBASrgb; * @ref Vk::PixelFormat::CompressedBc2RGBASrgb;
* @m_class{m-doc-external} [DXGI_FORMAT_BC2_UNORM_SRGB](https://docs.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format) * @m_class{m-doc-external} [DXGI_FORMAT_BC2_UNORM_SRGB](https://docs.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format)
* or @m_class{m-doc-external} [MTLPixelFormatBC2_RGBA_sRGB](https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbc2_rgba_srgb?language=objc). * or @m_class{m-doc-external} [MTLPixelFormatBC2_RGBA_sRGB](https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbc2_rgba_srgb?language=objc).
* @see @relativeref{Trade,BcDecImageConverter} * @see @ref Math::yFlipBc2InPlace(),
* @relativeref{Trade,BcDecImageConverter}
* @m_keywords{DXGI_FORMAT_BC2_UNORM_SRGB MTLPixelFormatBC2_RGBA_sRGB} * @m_keywords{DXGI_FORMAT_BC2_UNORM_SRGB MTLPixelFormatBC2_RGBA_sRGB}
* @m_since{2019,10} * @m_since{2019,10}
*/ */

Loading…
Cancel
Save