# Intel Masked Software Occlusion Culling, vendored.
#
#   Upstream:  https://github.com/GameTechDev/MaskedOcclusionCulling
#   Licence:   Apache License 2.0, "Copyright 2017 Intel Corporation".
#              Full text in license.txt beside this file. Apache-2.0 is
#              GPLv3-compatible, so it may ship inside our client.
#   Reference: gitgud.io/loregamer/openmw-fps 874442c570 extern/maskedoc/,
#              which is byte-identical to github.com/kartoffels-ci/openmw
#              5e3bcebe7 ("Add software occlusion culling via Intel MOC").
#
# Three deviations from the fork's copy of this file, all deliberate; the library sources themselves
# are byte-for-byte the fork's:
#
#  1. sse2neon.h is NOT vendored. The fork carries an 11,222-line ARM/NEON
#     SSE-translation shim (MIT, "Copyright (c) 2015-2026 SSE2NEON
#     Contributors") added by kartoffels 5d25eb99b for Apple Silicon. Nothing
#     in the vendored library includes it - grep every .h/.inl/.cpp here - so
#     on our two targets (Windows x86_64 and Linux x86_64, the Steam Deck
#     included) it is 73% of the vendored line count and zero of the code.
#     If an ARM target is ever wanted, bring it back WITH its MIT notice.
#  2. Because of (1) the non-x86 branch cannot work, so it is an explicit
#     FATAL_ERROR rather than a build that fails later with a wall of
#     intrinsics errors.
#  3. MSVC's /arch:SSE2 exists only for 32-bit x86; passing it to the x64
#     compiler is warning D9002 "ignoring unknown option" on every build of
#     the file. Guarded on the pointer size. SSE2 is the x64 baseline anyway,
#     which is exactly what the fork's comment wanted.

if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|i[3-6]86")
    message(FATAL_ERROR
        "extern/maskedoc supports x86 only: the ARM path needs sse2neon.h, "
        "which is deliberately not vendored. See the comment in this file.")
endif()

set(MOC_SOURCES
    MaskedOcclusionCulling.cpp
    MaskedOcclusionCullingAVX2.cpp
    MaskedOcclusionCullingAVX512.cpp
)

add_library(maskedoc STATIC ${MOC_SOURCES})
target_include_directories(maskedoc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

if(MSVC)
    # The dispatcher TU must be built at the SSE baseline: it probes CPUID at
    # runtime and must not itself contain AVX instructions.
    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
        set_source_files_properties(MaskedOcclusionCulling.cpp PROPERTIES COMPILE_FLAGS "/arch:SSE2")
    endif()
    set_source_files_properties(MaskedOcclusionCullingAVX2.cpp PROPERTIES COMPILE_FLAGS "/arch:AVX2")
    set_source_files_properties(MaskedOcclusionCullingAVX512.cpp PROPERTIES COMPILE_FLAGS "/arch:AVX512")
else()
    set_source_files_properties(MaskedOcclusionCulling.cpp PROPERTIES COMPILE_FLAGS "-msse4.1 -mxsave")
    set_source_files_properties(MaskedOcclusionCullingAVX2.cpp PROPERTIES COMPILE_FLAGS "-mavx2 -mfma")
    set_source_files_properties(MaskedOcclusionCullingAVX512.cpp PROPERTIES COMPILE_FLAGS "-mavx512f -mavx512bw")
endif()
