Skip to content

Commit

Permalink
Show bad blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Evstyukhin committed Jan 21, 2020
1 parent 5a4898a commit e9be191
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 15 deletions.
25 changes: 19 additions & 6 deletions src/Bc7Compress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void PackTexture(uint8_t* dst_bc7, uint8_t* src_bgra, uint8_t* mask_agrb,

auto finish = std::chrono::high_resolution_clock::now();

if (blockKernel != DecompressKernel)
if (blockKernel == CompressKernel)
{
int pixels = src_h * src_w;

Expand All @@ -104,10 +104,7 @@ static void PackTexture(uint8_t* dst_bc7, uint8_t* src_bgra, uint8_t* mask_agrb,
PRINTF(" Compressed %d blocks, elapsed %i ms, throughput %d.%03d Mpx/s", pixels >> 4, span, kpx_s / 1000, kpx_s % 1000);

#if defined(OPTION_COUNTERS)
if (blockKernel == CompressKernel)
{
CompressStatistics();
}
CompressStatistics();
#endif
}
}
Expand Down Expand Up @@ -249,6 +246,7 @@ int Bc7MainWithArgs(const std::vector<std::string>& args)
const char* dst_name = nullptr;
const char* result_name = nullptr;
const char* partitions_name = nullptr;
const char* bad_name = nullptr;

for (int i = 0, n = (int)args.size(); i < n; i++)
{
Expand Down Expand Up @@ -327,6 +325,14 @@ int Bc7MainWithArgs(const std::vector<std::string>& args)
}
continue;
}
else if (strcmp(arg, "/bad") == 0)
{
if (++i < n)
{
bad_name = args[i].c_str();
}
continue;
}
#if defined(WIN32)
else
{
Expand Down Expand Up @@ -504,6 +510,13 @@ int Bc7MainWithArgs(const std::vector<std::string>& args)

PackTexture(dst_bc7, dst_texture_bgra, mask_agrb, src_texture_stride, src_texture_w, src_texture_h, &DecompressKernel, 16, mse_alpha, mse_color, ssim);

if ((bad_name != nullptr) && bad_name[0])
{
ShowBadBlocks(src_texture_bgra, dst_texture_bgra, mask_agrb, src_texture_stride, src_texture_w, src_texture_h);

WriteImage(bad_name, mask_agrb, src_texture_w, src_texture_h, flip);
}

if ((partitions_name != nullptr) && partitions_name[0])
{
VisualizePartitionsGRB(dst_bc7, Size);
Expand Down Expand Up @@ -535,7 +548,7 @@ int __cdecl main(int argc, char* argv[])
if (argc < 2)
{
PRINTF("Usage: Bc7Compress [/fast | /normal | /slow | /draft] [/retina] [/nomask] [/noflip] src");
PRINTF(" [dst.ktx] [/debug result.png] [/map partitions.png]");
PRINTF(" [dst.ktx] [/debug result.png] [/map partitions.png] [/bad bad.png]");
return 1;
}

Expand Down
4 changes: 0 additions & 4 deletions src/Metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ BlockSSIM CompareBlocksSSIM(const Cell& cell1, const Cell& cell2) noexcept
return BlockSSIM(_mm_cvtsd_f64(mssim_ga), ssim * (1.0 / kColor));
}

#if defined(OPTION_COUNTERS)

bool DetectGlitches(const Cell& input, const Cell& output) noexcept
{
const __m128i msign = _mm_set1_epi8(-0x80);
Expand Down Expand Up @@ -169,5 +167,3 @@ bool DetectGlitches(const Cell& input, const Cell& output) noexcept

return _mm_movemask_epi8(me) != 0;
}

#endif
4 changes: 0 additions & 4 deletions src/Metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,4 @@ BlockError CompareBlocks(const Cell& cell1, const Cell& cell2) noexcept;

BlockSSIM CompareBlocksSSIM(const Cell& cell1, const Cell& cell2) noexcept;

#if defined(OPTION_COUNTERS)

bool DetectGlitches(const Cell& input, const Cell& output) noexcept;

#endif
121 changes: 120 additions & 1 deletion src/Worker.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "pch.h"
#include "Worker.h"
#include "Metrics.h"

#if defined(WIN32)
#include <windows.h>
Expand Down Expand Up @@ -242,7 +243,7 @@ class Worker

if (_Running <= 0)
break;
}
}
#endif

pErrorAlpha = _errorAlpha;
Expand Down Expand Up @@ -389,3 +390,121 @@ void ProcessTexture(uint8_t* dst, uint8_t* src_bgra, uint8_t* mask_agrb, int str

worker.Run(blockKernel, stride, pErrorAlpha, pErrorColor, pssim);
}

void ShowBadBlocks(const uint8_t* src_bgra, const uint8_t* dst_bgra, uint8_t* mask_agrb, int stride, int src_w, int src_h) noexcept
{
Cell input;
Cell output;

for (int y = 0; y < src_h; y += 4)
{
const uint8_t* src = src_bgra + y * stride;
const uint8_t* dst = dst_bgra + y * stride;
uint8_t* mask = mask_agrb + y * stride;

for (int x = 0; x < src_w; x += 4)
{
{
const uint8_t* p = src;

input.ImageRows_U8[0] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));

p += stride;

input.ImageRows_U8[1] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));

p += stride;

input.ImageRows_U8[2] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));

p += stride;

input.ImageRows_U8[3] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));
}

{
const uint8_t* p = mask;

input.MaskRows_S8[0] = _mm_loadu_si128((const __m128i*)p);

p += stride;

input.MaskRows_S8[1] = _mm_loadu_si128((const __m128i*)p);

p += stride;

input.MaskRows_S8[2] = _mm_loadu_si128((const __m128i*)p);

p += stride;

input.MaskRows_S8[3] = _mm_loadu_si128((const __m128i*)p);
}

{
const uint8_t* p = dst;

output.ImageRows_U8[0] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));

p += stride;

output.ImageRows_U8[1] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));

p += stride;

output.ImageRows_U8[2] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));

p += stride;

output.ImageRows_U8[3] = ConvertBgraToAgrb(_mm_loadu_si128((const __m128i*)p));
}

bool bad = DetectGlitches(input, output);
if (bad)
{
const uint8_t* r = src;
uint8_t* p = mask;

_mm_storeu_si128((__m128i*)p, _mm_loadu_si128((const __m128i*)r));

r += stride;
p += stride;

_mm_storeu_si128((__m128i*)p, _mm_loadu_si128((const __m128i*)r));

r += stride;
p += stride;

_mm_storeu_si128((__m128i*)p, _mm_loadu_si128((const __m128i*)r));

r += stride;
p += stride;

_mm_storeu_si128((__m128i*)p, _mm_loadu_si128((const __m128i*)r));
}
else
{
__m128i mc = _mm_setzero_si128();

uint8_t* p = mask;

_mm_storeu_si128((__m128i*)p, mc);

p += stride;

_mm_storeu_si128((__m128i*)p, mc);

p += stride;

_mm_storeu_si128((__m128i*)p, mc);

p += stride;

_mm_storeu_si128((__m128i*)p, mc);
}

src += 16;
dst += 16;
mask += 16;
}
}
}
2 changes: 2 additions & 0 deletions src/Worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ void DecompressKernel(const WorkerItem* begin, const WorkerItem* end, int stride
void CompressKernel(const WorkerItem* begin, const WorkerItem* end, int stride, int64_t& pErrorAlpha, int64_t& pErrorColor, BlockSSIM& pssim) noexcept;

void ProcessTexture(uint8_t* dst, uint8_t* src_bgra, uint8_t* mask_agrb, int stride, int src_w, int src_h, PBlockKernel blockKernel, size_t block_size, int64_t& pErrorAlpha, int64_t& pErrorColor, BlockSSIM& pssim);

void ShowBadBlocks(const uint8_t* src_bgra, const uint8_t* dst_bgra, uint8_t* mask_agrb, int stride, int src_w, int src_h) noexcept;

0 comments on commit e9be191

Please sign in to comment.