fix(agc): avoid exception-driven render target format probing

This commit is contained in:
SamG-Coder
2026-09-28 09:55:00 +10:00
parent e1e3a12227
commit 9814c93f75
5 changed files with 34 additions and 13 deletions
+9
View File
@@ -109,6 +109,15 @@ endforeach()
add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS})
if(BUILD_TESTING)
add_executable(texture_format_lookup_tests tests/TextureFormatLookup.cpp)
target_include_directories(texture_format_lookup_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/3rdparty/Vulkan-Headers/include)
target_link_libraries(texture_format_lookup_tests PRIVATE libSceAgcDriver libc)
configure_windows_unwind(texture_format_lookup_tests)
add_test(NAME texture_format_lookup COMMAND texture_format_lookup_tests)
if(WIN32)
set_tests_properties(texture_format_lookup PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_executable(linear_display_buffer_tests tests/LinearDisplayBuffer.cpp)
target_include_directories(linear_display_buffer_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(linear_display_buffer_tests PRIVATE libSceAgcDriver libc)
@@ -6,10 +6,12 @@
#endif
#include <vulkan/vulkan.h>
#include <cstdint>
#include <optional>
namespace AgcDriver::Graphics {
VkFormat ResolveTextureFormat(std::uint32_t guestFormat);
std::optional<std::uint32_t> FindGuestTextureFormat(VkFormat format, std::uint32_t elementBytes);
std::uint32_t BytesPerElement(std::uint32_t guestFormat);
bool IsBlockCompressed(std::uint32_t guestFormat);
std::uint32_t BlockWidth(std::uint32_t guestFormat);
@@ -34,19 +34,7 @@ namespace {
// A guest texture format code with the same Vulkan format and texel size as a color buffer.
std::uint32_t GuestFormatFor(VkFormat format, std::uint32_t elementBytes) {
static std::mutex mutex;
static std::map<std::pair<VkFormat, std::uint32_t>, std::uint32_t> known;
std::lock_guard lock(mutex);
if (const auto found = known.find({format, elementBytes}); found != known.end()) return found->second;
for (std::uint32_t code = 0; code < 256; ++code) {
try {
if (!IsBlockCompressed(code) && ResolveTextureFormat(code) == format && BytesPerElement(code) == elementBytes) {
known.emplace(std::make_pair(format, elementBytes), code);
return code;
}
} catch (const std::exception&) {
}
}
if (const auto guest = FindGuestTextureFormat(format, elementBytes)) return *guest;
throw std::runtime_error("AGC graphics: no guest texture format matches the color buffer format " + std::to_string(static_cast<int>(format)));
}
@@ -108,6 +108,13 @@ VkFormat ResolveTextureFormat(std::uint32_t guestFormat) {
return findFormatEntry(guestFormat).vkFormat;
}
std::optional<std::uint32_t> FindGuestTextureFormat(VkFormat format, std::uint32_t elementBytes) {
for (const auto& entry : kFormatLookup) {
if (!entry.blockCompressed && entry.vkFormat == format && entry.bytesPerElement == elementBytes) return entry.guestFormat;
}
return std::nullopt;
}
std::uint32_t BytesPerElement(std::uint32_t guestFormat) {
return findFormatEntry(guestFormat).bytesPerElement;
}
+15
View File
@@ -0,0 +1,15 @@
#include "prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp"
#include <cstdlib>
int main() {
using namespace AgcDriver::Graphics;
for (auto format : {VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R16G16B16A16_SFLOAT}) {
const unsigned bytes = format == VK_FORMAT_R8_UNORM ? 1 : format == VK_FORMAT_R8G8B8A8_UNORM ? 4 : 8;
const auto guest = FindGuestTextureFormat(format, bytes);
if (!guest || ResolveTextureFormat(*guest) != format || BytesPerElement(*guest) != bytes) std::abort();
if (FindGuestTextureFormat(format, bytes + 1)) std::abort();
}
if (FindGuestTextureFormat(VK_FORMAT_B8G8R8A8_UNORM, 4)) std::abort();
if (FindGuestTextureFormat(VK_FORMAT_UNDEFINED, 4)) std::abort();
if (FindGuestTextureFormat(VK_FORMAT_BC1_RGBA_UNORM_BLOCK, 8)) std::abort();
}