Fix texture format bounds checking to prevent rendering corruption (black triangles)

This commit is contained in:
salh
2026-04-20 18:56:20 +03:00
parent d9f1c20d60
commit 9af6f59938
2 changed files with 16 additions and 0 deletions
@@ -13,6 +13,7 @@
#include <cmath>
#include <rex/graphics/pipeline/texture/info.h>
#include <rex/logging.h>
#include <rex/math.h>
namespace rex::graphics {
@@ -46,6 +47,13 @@ static TextureExtent CalculateExtent(const FormatInfo* format_info, uint32_t pit
uint32_t byte_pitch = extent.block_pitch_h * bytes_per_block;
if (!is_tiled) {
if (bytes_per_block == 0) {
REXGPU_ERROR(
"TextureExtent::Calculate received linear guest texture format {} with zero "
"bytes_per_block (pitch={}, height={}, depth={})",
format_info->name, pitch, height, depth);
return extent;
}
// Each row must be a multiple of 256 bytes in linear textures.
byte_pitch = rex::round_up(byte_pitch, 256);
extent.block_pitch_h = byte_pitch / bytes_per_block;
@@ -12,6 +12,7 @@
// clang-format off
#include <rex/graphics/pipeline/texture/info.h>
#include <rex/logging.h>
namespace rex::graphics {
@@ -86,6 +87,13 @@ const FormatInfo* FormatInfo::Get(uint32_t gpu_format) {
FORMAT_INFO(k_8_8_8_8_GAMMA_EDRAM , kUncompressed, 1, 1, 32),
FORMAT_INFO(k_2_10_10_10_FLOAT_EDRAM , kUncompressed, 1, 1, 32),
};
static const FormatInfo invalid_format_info = {
xenos::TextureFormat::k_8_8_8_8, "k_invalid", FormatType::kUncompressed, 1, 1, 32};
if (gpu_format >= (sizeof(format_infos) / sizeof(format_infos[0]))) {
REXGPU_ERROR("Texture format index {} is out of range; falling back to {}", gpu_format,
invalid_format_info.name);
return &invalid_format_info;
}
return &format_infos[gpu_format];
}
#undef FORMAT_INFO