cmake_minimum_required(VERSION 3.10)
project(VulkanTriangleDemo)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# 查找必要的包
find_package(Vulkan REQUIRED)
find_package(glfw3 REQUIRED)

# 包含目录
include_directories(${Vulkan_INCLUDE_DIRS})
include_directories(${GLFW3_INCLUDE_DIRS})

# 创建着色器编译目标
set(SHADER_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/shaders)
set(SHADER_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/shaders)

# 确保输出目录存在
file(MAKE_DIRECTORY ${SHADER_OUTPUT_DIR})

# 查找 glslangValidator
find_program(GLSLANG_VALIDATOR glslangValidator)
if(NOT GLSLANG_VALIDATOR)
    message(WARNING "glslangValidator not found. Shaders must be compiled manually.")
else()
    # 编译顶点着色器
    add_custom_command(
        OUTPUT ${SHADER_OUTPUT_DIR}/vert.spv
        COMMAND ${GLSLANG_VALIDATOR} -V ${SHADER_SOURCE_DIR}/vert.vert -o ${SHADER_OUTPUT_DIR}/vert.spv
        DEPENDS ${SHADER_SOURCE_DIR}/vert.vert
        COMMENT "Compiling vertex shader"
    )

    # 编译片段着色器
    add_custom_command(
        OUTPUT ${SHADER_OUTPUT_DIR}/frag.spv
        COMMAND ${GLSLANG_VALIDATOR} -V ${SHADER_SOURCE_DIR}/frag.frag -o ${SHADER_OUTPUT_DIR}/frag.spv
        DEPENDS ${SHADER_SOURCE_DIR}/frag.frag
        COMMENT "Compiling fragment shader"
    )

    # 创建着色器目标
    add_custom_target(Shaders ALL
        DEPENDS ${SHADER_OUTPUT_DIR}/vert.spv ${SHADER_OUTPUT_DIR}/frag.spv
    )
endif()

# 复制着色器文件到输出目录（如果无法编译）
add_custom_command(
    TARGET VulkanTriangleDemo POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:VulkanTriangleDemo>/shaders/
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${SHADER_SOURCE_DIR}/vert.spv
        $<TARGET_FILE_DIR:VulkanTriangleDemo>/shaders/vert.spv
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${SHADER_SOURCE_DIR}/frag.spv
        $<TARGET_FILE_DIR:VulkanTriangleDemo>/shaders/frag.spv
    COMMENT "Copying shaders to output directory"
)

# 添加可执行文件
add_executable(VulkanTriangleDemo main.cpp)

# 链接库
target_link_libraries(VulkanTriangleDemo
    ${Vulkan_LIBRARIES}
    glfw
)

# 在 Windows 上需要额外的设置
if(WIN32)
    # 复制 GLFW DLL（如果使用动态链接）
    find_file(GLFW3_DLL
        NAMES glfw3.dll
        PATHS ${GLFW3_ROOT_DIR}/lib-vc2019 # 根据你的 GLFW 版本调整
              ${GLFW3_ROOT_DIR}/lib
    )
    
    if(GLFW3_DLL)
        add_custom_command(TARGET VulkanTriangleDemo POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${GLFW3_DLL}
                $<TARGET_FILE_DIR:VulkanTriangleDemo>/
        )
    endif()
    
    # 设置 Windows 子系统（避免弹出控制台窗口）
    if(CMAKE_BUILD_TYPE STREQUAL "Release")
        set_target_properties(VulkanTriangleDemo PROPERTIES
            WIN32_EXECUTABLE ON
        )
    endif()
endif()

# 安装目标（可选）
install(TARGETS VulkanTriangleDemo
    RUNTIME DESTINATION bin
)

# 安装着色器（可选）
install(DIRECTORY ${SHADER_SOURCE_DIR}/
    DESTINATION shaders
    FILES_MATCHING PATTERN "*.spv"
)