From 415c78b87c008f0d963679ea9bc06c8aa566b506 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 20 May 2023 17:15:36 -0400 Subject: textures: add BC1 and BC3 compressors and recompression setting --- src/video_core/textures/astc.cpp | 5 +-- src/video_core/textures/bcn.cpp | 87 +++++++++++++++++++++++++++++++++++++ src/video_core/textures/bcn.h | 17 ++++++++ src/video_core/textures/workers.cpp | 15 +++++++ src/video_core/textures/workers.h | 12 +++++ 5 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 src/video_core/textures/bcn.cpp create mode 100644 src/video_core/textures/bcn.h create mode 100644 src/video_core/textures/workers.cpp create mode 100644 src/video_core/textures/workers.h (limited to 'src/video_core/textures') diff --git a/src/video_core/textures/astc.cpp b/src/video_core/textures/astc.cpp index a68bc0d77..fef0be31d 100644 --- a/src/video_core/textures/astc.cpp +++ b/src/video_core/textures/astc.cpp @@ -16,8 +16,8 @@ #include "common/alignment.h" #include "common/common_types.h" #include "common/polyfill_ranges.h" -#include "common/thread_worker.h" #include "video_core/textures/astc.h" +#include "video_core/textures/workers.h" class InputBitStream { public: @@ -1656,8 +1656,7 @@ void Decompress(std::span data, uint32_t width, uint32_t height, const u32 rows = Common::DivideUp(height, block_height); const u32 cols = Common::DivideUp(width, block_width); - static Common::ThreadWorker workers{std::max(std::thread::hardware_concurrency(), 2U) / 2, - "ASTCDecompress"}; + Common::ThreadWorker& workers{GetThreadWorkers()}; for (u32 z = 0; z < depth; ++z) { const u32 depth_offset = z * height * width * 4; diff --git a/src/video_core/textures/bcn.cpp b/src/video_core/textures/bcn.cpp new file mode 100644 index 000000000..671212a49 --- /dev/null +++ b/src/video_core/textures/bcn.cpp @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include "common/alignment.h" +#include "video_core/textures/bcn.h" +#include "video_core/textures/workers.h" + +namespace Tegra::Texture::BCN { + +using BCNCompressor = void(u8* block_output, const u8* block_input, bool any_alpha); + +template +void CompressBCN(std::span data, uint32_t width, uint32_t height, uint32_t depth, + std::span output, BCNCompressor f) { + constexpr u8 alpha_threshold = 128; + constexpr u32 bytes_per_px = 4; + const u32 plane_dim = width * height; + + Common::ThreadWorker& workers{GetThreadWorkers()}; + + for (u32 z = 0; z < depth; z++) { + for (u32 y = 0; y < height; y += 4) { + auto compress_row = [z, y, width, height, plane_dim, f, data, output]() { + for (u32 x = 0; x < width; x += 4) { + // Gather 4x4 block of RGBA texels + u8 input_colors[4][4][4]; + bool any_alpha = false; + + for (u32 j = 0; j < 4; j++) { + for (u32 i = 0; i < 4; i++) { + const size_t coord = + (z * plane_dim + (y + j) * width + (x + i)) * bytes_per_px; + + if ((x + i < width) && (y + j < height)) { + if constexpr (ThresholdAlpha) { + if (data[coord + 3] >= alpha_threshold) { + input_colors[j][i][0] = data[coord + 0]; + input_colors[j][i][1] = data[coord + 1]; + input_colors[j][i][2] = data[coord + 2]; + input_colors[j][i][3] = 255; + } else { + any_alpha = true; + memset(input_colors[j][i], 0, bytes_per_px); + } + } else { + memcpy(input_colors[j][i], &data[coord], bytes_per_px); + } + } else { + memset(input_colors[j][i], 0, bytes_per_px); + } + } + } + + const u32 bytes_per_row = BytesPerBlock * Common::DivideUp(width, 4U); + const u32 bytes_per_plane = bytes_per_row * Common::DivideUp(height, 4U); + f(output.data() + z * bytes_per_plane + (y / 4) * bytes_per_row + + (x / 4) * BytesPerBlock, + reinterpret_cast(input_colors), any_alpha); + } + }; + workers.QueueWork(std::move(compress_row)); + } + workers.WaitForRequests(); + } +} + +void CompressBC1(std::span data, uint32_t width, uint32_t height, uint32_t depth, + std::span output) { + CompressBCN<8, true>(data, width, height, depth, output, + [](u8* block_output, const u8* block_input, bool any_alpha) { + stb_compress_bc1_block(block_output, block_input, any_alpha, + STB_DXT_NORMAL); + }); +} + +void CompressBC3(std::span data, uint32_t width, uint32_t height, uint32_t depth, + std::span output) { + CompressBCN<16, false>(data, width, height, depth, output, + [](u8* block_output, const u8* block_input, bool any_alpha) { + stb_compress_bc3_block(block_output, block_input, STB_DXT_NORMAL); + }); +} + +} // namespace Tegra::Texture::BCN diff --git a/src/video_core/textures/bcn.h b/src/video_core/textures/bcn.h new file mode 100644 index 000000000..6464af885 --- /dev/null +++ b/src/video_core/textures/bcn.h @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +namespace Tegra::Texture::BCN { + +void CompressBC1(std::span data, uint32_t width, uint32_t height, uint32_t depth, + std::span output); + +void CompressBC3(std::span data, uint32_t width, uint32_t height, uint32_t depth, + std::span output); + +} // namespace Tegra::Texture::BCN diff --git a/src/video_core/textures/workers.cpp b/src/video_core/textures/workers.cpp new file mode 100644 index 000000000..a71c305f4 --- /dev/null +++ b/src/video_core/textures/workers.cpp @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "video_core/textures/workers.h" + +namespace Tegra::Texture { + +Common::ThreadWorker& GetThreadWorkers() { + static Common::ThreadWorker workers{std::max(std::thread::hardware_concurrency(), 2U) / 2, + "ImageTranscode"}; + + return workers; +} + +} // namespace Tegra::Texture diff --git a/src/video_core/textures/workers.h b/src/video_core/textures/workers.h new file mode 100644 index 000000000..008dd05b3 --- /dev/null +++ b/src/video_core/textures/workers.h @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/thread_worker.h" + +namespace Tegra::Texture { + +Common::ThreadWorker& GetThreadWorkers(); + +} -- cgit v1.2.3