summaryrefslogtreecommitdiffstats
path: root/src/video_core/textures
diff options
context:
space:
mode:
authorLiam <byteslice@airmail.cc>2023-05-20 23:15:36 +0200
committerLiam <byteslice@airmail.cc>2023-05-23 18:54:40 +0200
commit415c78b87c008f0d963679ea9bc06c8aa566b506 (patch)
tree11e6a5d2211a99660a48678059c703e849c06da3 /src/video_core/textures
parentMerge pull request #10392 from danilaml/update-cubeb-again (diff)
downloadyuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.tar
yuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.tar.gz
yuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.tar.bz2
yuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.tar.lz
yuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.tar.xz
yuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.tar.zst
yuzu-415c78b87c008f0d963679ea9bc06c8aa566b506.zip
Diffstat (limited to 'src/video_core/textures')
-rw-r--r--src/video_core/textures/astc.cpp5
-rw-r--r--src/video_core/textures/bcn.cpp87
-rw-r--r--src/video_core/textures/bcn.h17
-rw-r--r--src/video_core/textures/workers.cpp15
-rw-r--r--src/video_core/textures/workers.h12
5 files changed, 133 insertions, 3 deletions
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<const uint8_t> 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 <stb_dxt.h>
+#include <string.h>
+
+#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 <u32 BytesPerBlock, bool ThresholdAlpha = false>
+void CompressBCN(std::span<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
+ std::span<uint8_t> 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<u8*>(input_colors), any_alpha);
+ }
+ };
+ workers.QueueWork(std::move(compress_row));
+ }
+ workers.WaitForRequests();
+ }
+}
+
+void CompressBC1(std::span<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
+ std::span<uint8_t> 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<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
+ std::span<uint8_t> 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 <span>
+#include <stdint.h>
+
+namespace Tegra::Texture::BCN {
+
+void CompressBC1(std::span<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
+ std::span<uint8_t> output);
+
+void CompressBC3(std::span<const uint8_t> data, uint32_t width, uint32_t height, uint32_t depth,
+ std::span<uint8_t> 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();
+
+}