# King Kong native renderer: guest resource conversion (brief 03).
# A standalone library with unit tests that need neither the game nor the SDK:
#   build.bat            configure, build and run the tests (Visual Studio 2022 clang + Ninja, like kk/)
#   Linux: cmake --preset res-linux-release && cmake --build --preset res-linux-release &&
#          ctest --preset res-linux-release
#   kknr_resources       the library (texture formats, tiling, conversion, buffers, caches, render targets)
#   kknr_resources_tests the unit tests (ctest, or run the exe)
#   kknr_texdump         converts a texture dump (KK_DEV_TEX_DUMP) to host data, DDS and PNG (dev tool)
#   kknr_bfscan          lists the texture formats stored in a KKTextures.bf (dev tool, reads game data)
#   kknr_formats         prints the guest -> host format table of docs/formats.md
cmake_minimum_required(VERSION 3.25)
project(kknr_resources LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(kknr_resources STATIC
    src/xenos.cpp
    src/tiling.cpp
    src/guest_texture.cpp
    src/endian.cpp
    src/texture_convert.cpp
    src/texture_workers.cpp
    src/texture_pack.cpp
    src/buffers.cpp
    src/texture_cache.cpp
    src/render_targets.cpp
    src/resolve.cpp
)
target_include_directories(kknr_resources PUBLIC include)
find_package(Threads REQUIRED)
target_link_libraries(kknr_resources PUBLIC Threads::Threads)
find_path(KKNR_XXHASH_INCLUDE_DIR xxh3.h HINTS
    "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/rexglue-vulkan/win-amd64/include"
    "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/rexglue/win-amd64/include" REQUIRED)
target_include_directories(kknr_resources PRIVATE "${KKNR_XXHASH_INCLUDE_DIR}")
if(WIN32)
    target_link_libraries(kknr_resources PUBLIC windowscodecs ole32)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    target_compile_options(kknr_resources PRIVATE -Wall -Wextra -Wno-missing-field-initializers)
endif()

option(KKNR_RESOURCES_TOOLS "Build the developer tools" ON)
if(KKNR_RESOURCES_TOOLS)
    add_executable(kknr_texdump tools/texdump.cpp tools/image_io.cpp)
    target_link_libraries(kknr_texdump PRIVATE kknr_resources)
    add_executable(kknr_bfscan tools/bfscan.cpp tools/image_io.cpp)
    target_link_libraries(kknr_bfscan PRIVATE kknr_resources)
    add_executable(kknr_formats tools/formats_table.cpp)
    target_link_libraries(kknr_formats PRIVATE kknr_resources)
    add_executable(kknr_packcheck tools/packcheck.cpp)
    target_link_libraries(kknr_packcheck PRIVATE kknr_resources)
    if(MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
        target_compile_definitions(kknr_texdump PRIVATE _CRT_SECURE_NO_WARNINGS)
        target_compile_definitions(kknr_bfscan PRIVATE _CRT_SECURE_NO_WARNINGS)
    endif()

    # kknr_sdkref: the conversion checked against the SDK's own texture code (today's renderer), compiled
    # from an SDK source tree (read only; nothing of it is copied here). Built when the tree exists.
    set(KKNR_SDK_SOURCE "C:/rexsrc" CACHE PATH "rexglue-sdk source tree for kknr_sdkref")
    if(EXISTS "${KKNR_SDK_SOURCE}/src/graphics/pipeline/texture/util.cpp")
        add_executable(kknr_sdkref tools/sdkref.cpp tools/sdkref_gpu.cpp tools/image_io.cpp
            "${KKNR_SDK_SOURCE}/src/graphics/pipeline/texture/util.cpp"
            "${KKNR_SDK_SOURCE}/src/graphics/pipeline/texture/info_formats.cpp")
        target_include_directories(kknr_sdkref PRIVATE "${KKNR_SDK_SOURCE}/include"
            "${KKNR_SDK_SOURCE}/src/graphics/shaders/bytecode/d3d12_5_1")
        target_link_libraries(kknr_sdkref PRIVATE kknr_resources)
        set_target_properties(kknr_sdkref PROPERTIES CXX_STANDARD 23)  # the SDK headers use std::byteswap
        if(MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
            target_compile_definitions(kknr_sdkref PRIVATE _CRT_SECURE_NO_WARNINGS NOMINMAX WIN32_LEAN_AND_MEAN)
        endif()
    endif()
endif()

enable_testing()
add_executable(kknr_resources_tests
    tests/test_main.cpp
    tests/test_tiling.cpp
    tests/test_roundtrip.cpp
    tests/test_tail_mips.cpp
    tests/test_convert.cpp
    tests/test_convert_workers.cpp
    tests/test_texture_pack.cpp
    tools/image_io.cpp
    tests/test_buffers.cpp
    tests/test_cache.cpp
    tests/test_render_targets.cpp
    tests/test_resolve.cpp
)
target_link_libraries(kknr_resources_tests PRIVATE kknr_resources)
add_test(NAME kknr_resources_tests COMMAND kknr_resources_tests)

# The host format map against NVRHI's own format table (needs only NVRHI's headers and format-info.cpp).
set(KKNR_NVRHI_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/nvrhi" CACHE PATH "NVRHI source tree")
if(EXISTS "${KKNR_NVRHI_DIR}/include/nvrhi/nvrhi.h")
    add_executable(kknr_nvrhi_format_tests tests/test_main.cpp tests/test_nvrhi_format.cpp
        "${KKNR_NVRHI_DIR}/src/common/format-info.cpp")
    target_include_directories(kknr_nvrhi_format_tests PRIVATE "${KKNR_NVRHI_DIR}/include")
    target_link_libraries(kknr_nvrhi_format_tests PRIVATE kknr_resources)
    add_test(NAME kknr_nvrhi_format_tests COMMAND kknr_nvrhi_format_tests)
endif()
