# The GPU interface, its test double, and the platform backend.
add_library(gpu_fake OBJECT fake/fake_device.cpp)
target_include_directories(gpu_fake PUBLIC ${POP_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(gpu_fake PRIVATE ${POP_WARN_STRICT})
pop_optimize(gpu_fake 1)

add_executable(gpu_fake_tests fake/tests/gpu_fake_tests.cpp)
target_link_libraries(gpu_fake_tests PRIVATE gpu_fake)
target_compile_options(gpu_fake_tests PRIVATE ${POP_WARN_STRICT})
pop_optimize(gpu_fake_tests 1)
pop_test_binary(gpu_fake_tests)
add_test(NAME gpu_fake_tests COMMAND gpu_fake_tests WORKING_DIRECTORY ${POP_ROOT})
set_tests_properties(gpu_fake_tests PROPERTIES LABELS nogame)

# The Vulkan backend builds everywhere; the loader is found at run time.
add_library(volk OBJECT ${POP_ROOT}/third_party/volk/volk.c)
target_include_directories(volk PUBLIC ${POP_ROOT}/third_party/volk ${POP_ROOT}/third_party/vulkan-headers/include)
target_compile_definitions(volk PUBLIC VK_NO_PROTOTYPES)
pop_optimize(volk 2)

add_library(gpu_vulkan OBJECT vulkan/vulkan_loader.cpp vulkan/vulkan_device.cpp vulkan/vulkan_pipeline.cpp
                               vulkan/vulkan_swapchain.cpp)
target_link_libraries(gpu_vulkan PUBLIC volk)
target_include_directories(gpu_vulkan PUBLIC ${POP_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(gpu_vulkan PRIVATE ${POP_WARN_HOST})
pop_link_sdl(gpu_vulkan)
if(NOT WIN32 AND NOT APPLE)
  target_link_libraries(gpu_vulkan PUBLIC dl)
endif()
pop_optimize(gpu_vulkan 2)

# gpu_backend: every backend this platform has plus the factory, as one
# static library so indirect consumers get the objects. Hosts and tests link
# this, never a backend directly.
set(POP_GPU_BACKEND_OBJECTS $<TARGET_OBJECTS:gpu_vulkan> $<TARGET_OBJECTS:volk>)
if(APPLE)
  add_library(gpu_metal OBJECT metal/metal_device.mm metal/metal_surface.mm)
  target_compile_options(gpu_metal PRIVATE ${POP_WARN_HOST} $<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc>)
  target_include_directories(gpu_metal PUBLIC ${POP_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
  pop_link_sdl(gpu_metal)
  pop_optimize(gpu_metal 2)
  list(APPEND POP_GPU_BACKEND_OBJECTS $<TARGET_OBJECTS:gpu_metal>)
endif()
add_library(gpu_backend STATIC gpu_factory.cpp ${POP_GPU_BACKEND_OBJECTS})
set_target_properties(gpu_backend PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${POP_OUT})
target_include_directories(gpu_backend PUBLIC ${POP_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_options(gpu_backend PRIVATE ${POP_WARN_HOST})
target_link_libraries(gpu_backend PUBLIC recomp_platform)
pop_link_sdl(gpu_backend)
if(NOT WIN32 AND NOT APPLE)
  target_link_libraries(gpu_backend PUBLIC dl)
endif()
if(APPLE)
  target_link_libraries(gpu_backend PUBLIC "-framework Metal" "-framework QuartzCore" "-framework CoreVideo"
                                           "-framework CoreGraphics" "-framework Foundation")
endif()
pop_optimize(gpu_backend 1)

# The contract suite, once per backend. `gpu` is the platform's default
# backend; `moltenvk` is Vulkan on Apple, which needs MoltenVK installed.
function(pop_gpu_contract_test target backend label)
  add_executable(${target} tests/gpu_contract_tests.cpp)
  target_link_libraries(${target} PRIVATE gpu_backend)
  target_compile_definitions(${target} PRIVATE POP_GPU_TEST_BACKEND="${backend}")
  target_compile_options(${target} PRIVATE ${POP_WARN_STRICT})
  pop_optimize(${target} 1)
  pop_test_binary(${target})
  add_test(NAME ${target} COMMAND ${target} WORKING_DIRECTORY ${POP_ROOT})
  set_tests_properties(${target} PROPERTIES LABELS ${label})
endfunction()
if(APPLE)
  pop_gpu_contract_test(gpu_metal_tests metal gpu)
  pop_gpu_contract_test(gpu_vulkan_tests vulkan moltenvk)
else()
  pop_gpu_contract_test(gpu_vulkan_tests vulkan gpu)
endif()
