diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index 63bd99a00..cb33a343f 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -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:$") + 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) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp index a631ca03e..3f2d1f0d8 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp @@ -6,10 +6,12 @@ #endif #include #include +#include namespace AgcDriver::Graphics { VkFormat ResolveTextureFormat(std::uint32_t guestFormat); +std::optional 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); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp index ed00f454a..9f008e3aa 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp @@ -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::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(format))); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp index 06c67ae59..83b6bfbec 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp @@ -108,6 +108,13 @@ VkFormat ResolveTextureFormat(std::uint32_t guestFormat) { return findFormatEntry(guestFormat).vkFormat; } +std::optional 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; } diff --git a/core/libs/tests/TextureFormatLookup.cpp b/core/libs/tests/TextureFormatLookup.cpp new file mode 100644 index 000000000..fd8901eb5 --- /dev/null +++ b/core/libs/tests/TextureFormatLookup.cpp @@ -0,0 +1,15 @@ +#include "prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp" +#include + +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(); +}