# Xbox D3D8 Compatibility Layer
#   _WIN32 -> backed by Direct3D 11 (d3d8_*.c, d3d8_combiners.c, d3d8_vsh.c)
#   POSIX  -> backed by OpenGL 3.3 core via SDL2 + libepoxy (d3d8_gl.c)

if(WIN32)
    add_library(xbox_d3d8 STATIC
        d3d8_device.c
        d3d8_gamma.c
        d3d8_resources.c
        d3d8_shaders.c
        d3d8_states.c
        d3d8_combiners.c
        d3d8_vsh.c
    )
    target_link_libraries(xbox_d3d8 PUBLIC platform dxguid)   # d3d11, dxgi, d3dcompiler: loaded at run time

    # Offline smoke test for the pure format layer (no D3D11 device needed).
    add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../tests/d3d8_smoke
                     ${CMAKE_CURRENT_BINARY_DIR}/d3d8_smoke)
else()
    # The D3D8 layer serves titles that link the Xbox D3D8 library as
    # imports; a title whose own D3D8 was recompiled (drawing through the
    # pushbuffer executor) never calls it. Build the GL version only when
    # libepoxy is there, and otherwise an empty library.
    find_package(PkgConfig QUIET)
    if(PKG_CONFIG_FOUND)
        pkg_check_modules(EPOXY QUIET epoxy)
    endif()
    if(EPOXY_FOUND)
        add_library(xbox_d3d8 STATIC
            d3d8_gl.c
        )
        find_package(SDL2 REQUIRED)
        target_include_directories(xbox_d3d8 PRIVATE ${EPOXY_INCLUDE_DIRS})
        # EPOXY_LINK_LIBRARIES, not EPOXY_LIBRARIES: the latter is the bare name
        # `epoxy`, which only resolves if EPOXY_LIBRARY_DIRS happens to be on the
        # link path. The _LINK_LIBRARIES form is absolute paths.
        target_link_libraries(xbox_d3d8 PUBLIC platform SDL2::SDL2 ${EPOXY_LINK_LIBRARIES})
    else()
        message(STATUS "xboxrecomp: libepoxy not found; the D3D8 compatibility layer is left out")
        add_library(xbox_d3d8 INTERFACE)
        target_link_libraries(xbox_d3d8 INTERFACE platform)
        target_include_directories(xbox_d3d8 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
        return()
    endif()
endif()

target_include_directories(xbox_d3d8 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

if(MSVC)
    target_compile_options(xbox_d3d8 PRIVATE /W4 /O2)
else()
    target_compile_options(xbox_d3d8 PRIVATE -Wall -Wextra -Wno-unused-parameter -O2)
endif()
