|
| 1 | +cmake_minimum_required (VERSION 3.29) |
| 2 | + |
| 3 | +set (TEMPLATE_NAME "VulkanTutorial") |
| 4 | + |
| 5 | +project (${TEMPLATE_NAME}) |
| 6 | + |
| 7 | +# Add option to enable/disable C++ 20 module |
| 8 | +option(ENABLE_CPP20_MODULE "Enable C++ 20 module support for Vulkan" OFF) |
| 9 | + |
| 10 | +# Enable C++ module dependency scanning only if C++ 20 module is enabled |
| 11 | +if(ENABLE_CPP20_MODULE) |
| 12 | + set(CMAKE_CXX_SCAN_FOR_MODULES ON) |
| 13 | +endif() |
| 14 | + |
| 15 | +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake") |
| 16 | + |
| 17 | +find_package(glfw3 REQUIRED) |
| 18 | +find_package(glm REQUIRED) |
| 19 | +find_package(Vulkan 1.4.335 REQUIRED) |
| 20 | +find_package(tinyobjloader REQUIRED) |
| 21 | +find_package(tinygltf REQUIRED) |
| 22 | +find_package(KTX REQUIRED) |
| 23 | + |
| 24 | +# Set up Vulkan C++ module only if enabled |
| 25 | +if(ENABLE_CPP20_MODULE) |
| 26 | + add_library(VulkanCppModule) |
| 27 | + add_library(Vulkan::cppm ALIAS VulkanCppModule) |
| 28 | + |
| 29 | + target_compile_definitions(VulkanCppModule PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1) |
| 30 | + target_include_directories(VulkanCppModule PUBLIC "${Vulkan_INCLUDE_DIR}") |
| 31 | + target_link_libraries(VulkanCppModule PUBLIC Vulkan::Vulkan) |
| 32 | + |
| 33 | + set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20) |
| 34 | + |
| 35 | + # Add MSVC-specific compiler options for proper C++ module support |
| 36 | + if(MSVC) |
| 37 | + target_compile_options(VulkanCppModule PRIVATE |
| 38 | + /std:c++latest # Use latest C++ standard for better module support |
| 39 | + /permissive- # Standards conformance mode |
| 40 | + /Zc:__cplusplus # Enable correct __cplusplus macro |
| 41 | + /EHsc # Enable C++ exception handling |
| 42 | + /Zc:preprocessor # Use conforming preprocessor |
| 43 | + /translateInclude # Automatically translate #include to import for standard library |
| 44 | + ) |
| 45 | + endif() |
| 46 | + |
| 47 | + target_sources(VulkanCppModule |
| 48 | + PUBLIC |
| 49 | + FILE_SET cxx_modules TYPE CXX_MODULES |
| 50 | + BASE_DIRS |
| 51 | + "${Vulkan_INCLUDE_DIR}" |
| 52 | + FILES |
| 53 | + "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | + # Add the vulkan.cppm file directly as a source file |
| 58 | + target_sources(VulkanCppModule |
| 59 | + PRIVATE |
| 60 | + "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" |
| 61 | + ) |
| 62 | +else() |
| 63 | + # Create a dummy interface library when C++ 20 module is disabled |
| 64 | + add_library(VulkanCppModule INTERFACE) |
| 65 | + add_library(Vulkan::cppm ALIAS VulkanCppModule) |
| 66 | + target_link_libraries(VulkanCppModule INTERFACE Vulkan::Vulkan) |
| 67 | + target_compile_definitions(VulkanCppModule |
| 68 | + INTERFACE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1 |
| 69 | + ) |
| 70 | +endif() |
| 71 | + |
| 72 | +find_package(stb REQUIRED) |
| 73 | +set(STB_INCLUDEDIR ${stb_INCLUDE_DIRS}) |
| 74 | + |
| 75 | +find_program(SLANGC_EXECUTABLE slangc HINTS $ENV{VULKAN_SDK}/bin REQUIRED) |
| 76 | + |
| 77 | +set(TEMPLATE_DIR ${CMAKE_BINARY_DIR}/${TEMPLATE_NAME}) |
| 78 | + |
| 79 | +add_executable (${TEMPLATE_NAME} main.cpp) |
| 80 | +set_target_properties (${TEMPLATE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TEMPLATE_DIR}) |
| 81 | +set_target_properties (${TEMPLATE_NAME} PROPERTIES CXX_STANDARD 20) |
| 82 | +target_link_libraries (${TEMPLATE_NAME} Vulkan::cppm glfw) |
| 83 | +target_include_directories (${TEMPLATE_NAME} PRIVATE ${STB_INCLUDEDIR}) |
| 84 | + |
| 85 | +# Add compile definition if C++ 20 module is enabled |
| 86 | +if(ENABLE_CPP20_MODULE) |
| 87 | + target_compile_definitions(${TEMPLATE_NAME} PRIVATE USE_CPP20_MODULES=1) |
| 88 | +endif() |
| 89 | + |
| 90 | +# Define VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS to treat VK_ERROR_OUT_OF_DATE_KHR as a success code |
| 91 | +target_compile_definitions(${TEMPLATE_NAME} PRIVATE "VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS" ) |
| 92 | + |
| 93 | +if(WIN32) |
| 94 | + if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*") |
| 95 | + set_target_properties(${TEMPLATE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${TEMPLATE_DIR}) |
| 96 | + endif() |
| 97 | +endif() |
| 98 | + |
| 99 | +# Slang shader |
| 100 | +set (SHADER_TARGET ${TEMPLATE_NAME}Shader) |
| 101 | +set (SHADERS_DIR ${TEMPLATE_DIR}/shaders) |
| 102 | +set (SHADER_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/shader.slang") |
| 103 | +set (ENTRY_POINTS -entry vertMain -entry fragMain) |
| 104 | +add_custom_command(OUTPUT ${SHADERS_DIR} COMMAND ${CMAKE_COMMAND} -E make_directory ${SHADERS_DIR}) |
| 105 | +add_custom_command( |
| 106 | + OUTPUT ${SHADERS_DIR}/slang.spv |
| 107 | + COMMAND ${SLANGC_EXECUTABLE} ${SHADER_SOURCES} -target spirv -profile spirv_1_4 -emit-spirv-directly -fvk-use-entrypoint-name ${ENTRY_POINTS} -o ${TEMPLATE_DIR}/shaders/slang.spv |
| 108 | + WORKING_DIRECTORY ${SHADERS_DIR} |
| 109 | + DEPENDS ${SHADERS_DIR} ${SHADER_SOURCES} |
| 110 | + COMMENT "Compiling Slang Shaders" |
| 111 | + VERBATIM |
| 112 | +) |
| 113 | +add_custom_target(${SHADER_TARGET} DEPENDS ${SHADERS_DIR}/slang.spv) |
| 114 | +add_dependencies(${TEMPLATE_NAME} ${SHADER_TARGET}) |
| 115 | + |
| 116 | +# Assets |
| 117 | +set (ASSET_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../assets/") |
| 118 | +file (COPY ${ASSET_DIR}/viking_room.obj DESTINATION ${TEMPLATE_DIR}/models) |
| 119 | +file (COPY ${ASSET_DIR}/viking_room.png DESTINATION ${TEMPLATE_DIR}/textures) |
0 commit comments