diff --git a/attachments/00_base_code.cpp b/attachments/00_base_code.cpp index 6d1074bf..eee9f631 100644 --- a/attachments/00_base_code.cpp +++ b/attachments/00_base_code.cpp @@ -1,5 +1,5 @@ #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/01_instance_creation.cpp b/attachments/01_instance_creation.cpp index 7e68df7f..6cd67f6d 100644 --- a/attachments/01_instance_creation.cpp +++ b/attachments/01_instance_creation.cpp @@ -4,7 +4,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/02_validation_layers.cpp b/attachments/02_validation_layers.cpp index 6aef3863..5e374989 100644 --- a/attachments/02_validation_layers.cpp +++ b/attachments/02_validation_layers.cpp @@ -6,7 +6,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/03_physical_device_selection.cpp b/attachments/03_physical_device_selection.cpp index 63fecd50..627bf87c 100644 --- a/attachments/03_physical_device_selection.cpp +++ b/attachments/03_physical_device_selection.cpp @@ -6,7 +6,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/04_logical_device.cpp b/attachments/04_logical_device.cpp index ee42963a..d846b319 100644 --- a/attachments/04_logical_device.cpp +++ b/attachments/04_logical_device.cpp @@ -7,7 +7,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/05_window_surface.cpp b/attachments/05_window_surface.cpp index b08fa68c..de18a18c 100644 --- a/attachments/05_window_surface.cpp +++ b/attachments/05_window_surface.cpp @@ -6,7 +6,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/06_swap_chain_creation.cpp b/attachments/06_swap_chain_creation.cpp index 3fff257c..cb60c8a3 100644 --- a/attachments/06_swap_chain_creation.cpp +++ b/attachments/06_swap_chain_creation.cpp @@ -8,7 +8,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/07_image_views.cpp b/attachments/07_image_views.cpp index 790832af..402b609e 100644 --- a/attachments/07_image_views.cpp +++ b/attachments/07_image_views.cpp @@ -8,7 +8,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/08_graphics_pipeline.cpp b/attachments/08_graphics_pipeline.cpp index 58d09622..ad722cc1 100644 --- a/attachments/08_graphics_pipeline.cpp +++ b/attachments/08_graphics_pipeline.cpp @@ -8,7 +8,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/09_shader_modules.cpp b/attachments/09_shader_modules.cpp index 29853b9f..2e4a99ce 100644 --- a/attachments/09_shader_modules.cpp +++ b/attachments/09_shader_modules.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/10_fixed_functions.cpp b/attachments/10_fixed_functions.cpp index 3aa1238b..50f461a1 100644 --- a/attachments/10_fixed_functions.cpp +++ b/attachments/10_fixed_functions.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/12_graphics_pipeline_complete.cpp b/attachments/12_graphics_pipeline_complete.cpp index 7ca1313c..1c0a0c54 100644 --- a/attachments/12_graphics_pipeline_complete.cpp +++ b/attachments/12_graphics_pipeline_complete.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/14_command_buffers.cpp b/attachments/14_command_buffers.cpp index 864edec0..addb6bdb 100644 --- a/attachments/14_command_buffers.cpp +++ b/attachments/14_command_buffers.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/15_hello_triangle.cpp b/attachments/15_hello_triangle.cpp index eeb58f1f..6513b012 100644 --- a/attachments/15_hello_triangle.cpp +++ b/attachments/15_hello_triangle.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/16_frames_in_flight.cpp b/attachments/16_frames_in_flight.cpp index 2c129802..d9af84e4 100644 --- a/attachments/16_frames_in_flight.cpp +++ b/attachments/16_frames_in_flight.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/17_swap_chain_recreation.cpp b/attachments/17_swap_chain_recreation.cpp index 7fc4ef75..f883ed0c 100644 --- a/attachments/17_swap_chain_recreation.cpp +++ b/attachments/17_swap_chain_recreation.cpp @@ -9,7 +9,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/18_vertex_input.cpp b/attachments/18_vertex_input.cpp index a007708e..4fee78bc 100644 --- a/attachments/18_vertex_input.cpp +++ b/attachments/18_vertex_input.cpp @@ -10,7 +10,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/19_vertex_buffer.cpp b/attachments/19_vertex_buffer.cpp index d50a5e0c..e1e12769 100644 --- a/attachments/19_vertex_buffer.cpp +++ b/attachments/19_vertex_buffer.cpp @@ -10,7 +10,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/20_staging_buffer.cpp b/attachments/20_staging_buffer.cpp index 2b056975..496de6aa 100644 --- a/attachments/20_staging_buffer.cpp +++ b/attachments/20_staging_buffer.cpp @@ -10,7 +10,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/21_index_buffer.cpp b/attachments/21_index_buffer.cpp index d727fce9..cb0124fb 100644 --- a/attachments/21_index_buffer.cpp +++ b/attachments/21_index_buffer.cpp @@ -10,7 +10,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/22_descriptor_layout.cpp b/attachments/22_descriptor_layout.cpp index 4addf1ec..6d68a468 100644 --- a/attachments/22_descriptor_layout.cpp +++ b/attachments/22_descriptor_layout.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/23_descriptor_sets.cpp b/attachments/23_descriptor_sets.cpp index ed2839b4..19210fce 100644 --- a/attachments/23_descriptor_sets.cpp +++ b/attachments/23_descriptor_sets.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/24_texture_image.cpp b/attachments/24_texture_image.cpp index 9773cceb..36ceb3d7 100644 --- a/attachments/24_texture_image.cpp +++ b/attachments/24_texture_image.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/25_sampler.cpp b/attachments/25_sampler.cpp index b2571173..04c44ae9 100644 --- a/attachments/25_sampler.cpp +++ b/attachments/25_sampler.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/26_texture_mapping.cpp b/attachments/26_texture_mapping.cpp index 30cda574..a1c25783 100644 --- a/attachments/26_texture_mapping.cpp +++ b/attachments/26_texture_mapping.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/27_depth_buffering.cpp b/attachments/27_depth_buffering.cpp index ebb596a6..d6d143c1 100644 --- a/attachments/27_depth_buffering.cpp +++ b/attachments/27_depth_buffering.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/28_model_loading.cpp b/attachments/28_model_loading.cpp index 5c72ab66..09d76336 100644 --- a/attachments/28_model_loading.cpp +++ b/attachments/28_model_loading.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/29_mipmapping.cpp b/attachments/29_mipmapping.cpp index 4a0455f0..a7e3b437 100644 --- a/attachments/29_mipmapping.cpp +++ b/attachments/29_mipmapping.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/30_multisampling.cpp b/attachments/30_multisampling.cpp index bd5a4d06..bc72d08f 100644 --- a/attachments/30_multisampling.cpp +++ b/attachments/30_multisampling.cpp @@ -11,7 +11,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/31_compute_shader.cpp b/attachments/31_compute_shader.cpp index e2ef808a..80350b59 100644 --- a/attachments/31_compute_shader.cpp +++ b/attachments/31_compute_shader.cpp @@ -15,7 +15,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/32_ecosystem_utilities.cpp b/attachments/32_ecosystem_utilities.cpp index ce1747e1..9f3cac52 100644 --- a/attachments/32_ecosystem_utilities.cpp +++ b/attachments/32_ecosystem_utilities.cpp @@ -12,7 +12,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/33_vulkan_profiles.cpp b/attachments/33_vulkan_profiles.cpp index 3f15a6dd..0b7106c8 100644 --- a/attachments/33_vulkan_profiles.cpp +++ b/attachments/33_vulkan_profiles.cpp @@ -12,7 +12,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/34_android.cpp b/attachments/34_android.cpp index 10afe987..728c2fdd 100644 --- a/attachments/34_android.cpp +++ b/attachments/34_android.cpp @@ -12,7 +12,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/35_gltf_ktx.cpp b/attachments/35_gltf_ktx.cpp index b24c5b8c..721f4bc3 100644 --- a/attachments/35_gltf_ktx.cpp +++ b/attachments/35_gltf_ktx.cpp @@ -12,7 +12,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/36_multiple_objects.cpp b/attachments/36_multiple_objects.cpp index dc1ae802..00c9909f 100644 --- a/attachments/36_multiple_objects.cpp +++ b/attachments/36_multiple_objects.cpp @@ -12,7 +12,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/37_multithreading.cpp b/attachments/37_multithreading.cpp index 49f1720a..5fe42419 100644 --- a/attachments/37_multithreading.cpp +++ b/attachments/37_multithreading.cpp @@ -17,7 +17,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/38_ray_tracing.cpp b/attachments/38_ray_tracing.cpp index 0c4b6f3e..321b1f3d 100644 --- a/attachments/38_ray_tracing.cpp +++ b/attachments/38_ray_tracing.cpp @@ -10,7 +10,7 @@ #include #include -#ifdef __INTELLISENSE__ +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) #include #else import vulkan_hpp; diff --git a/attachments/CMake/FindVulkan.cmake b/attachments/CMake/FindVulkan.cmake index 3cfe8995..55ac153e 100644 --- a/attachments/CMake/FindVulkan.cmake +++ b/attachments/CMake/FindVulkan.cmake @@ -311,6 +311,7 @@ find_path(Vulkan_INCLUDE_DIR HINTS ${_Vulkan_hint_include_search_paths} ) +message(STATUS "vulkan_include_dir ${Vulkan_INCLUDE_DIR} search paths ${_Vulkan_hint_include_search_paths}") mark_as_advanced(Vulkan_INCLUDE_DIR) find_library(Vulkan_LIBRARY @@ -318,7 +319,7 @@ find_library(Vulkan_LIBRARY HINTS ${_Vulkan_hint_library_search_paths} ) -message(STATUS vulkan_library ${Vulkan_LIBRARY} search paths ${_Vulkan_hint_library_search_paths}) +message(STATUS "vulkan_library ${Vulkan_LIBRARY} search paths ${_Vulkan_hint_library_search_paths}") mark_as_advanced(Vulkan_LIBRARY) find_library(Vulkan_Layer_API_DUMP diff --git a/attachments/CMakeLists.txt b/attachments/CMakeLists.txt index 802276b4..113fe20b 100644 --- a/attachments/CMakeLists.txt +++ b/attachments/CMakeLists.txt @@ -1,12 +1,17 @@ cmake_minimum_required (VERSION 3.29) -# Enable C++ module dependency scanning -set(CMAKE_CXX_SCAN_FOR_MODULES ON) - project (VulkanTutorial) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake") +# Add option to enable/disable C++ 20 module +option(ENABLE_CPP20_MODULE "Enable C++ 20 module support for Vulkan" OFF) + +# Enable C++ module dependency scanning only if C++ 20 module is enabled +if(ENABLE_CPP20_MODULE) + set(CMAKE_CXX_SCAN_FOR_MODULES ON) +endif() + find_package (glfw3 REQUIRED) find_package (glm REQUIRED) find_package (Vulkan REQUIRED) @@ -14,39 +19,61 @@ find_package (tinyobjloader REQUIRED) find_package (tinygltf REQUIRED) find_package (KTX REQUIRED) -# set up Vulkan C++ module -add_library(VulkanCppModule) -add_library(Vulkan::cppm ALIAS VulkanCppModule) - -target_compile_definitions(VulkanCppModule - PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1 -) -target_include_directories(VulkanCppModule - PRIVATE - "${Vulkan_INCLUDE_DIR}" -) -target_link_libraries(VulkanCppModule - PUBLIC - Vulkan::Vulkan -) - -set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20) - -target_sources(VulkanCppModule - PUBLIC - FILE_SET cxx_modules TYPE CXX_MODULES - BASE_DIRS - "${Vulkan_INCLUDE_DIR}" - FILES - "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" -) - - -# Add the vulkan.cppm file directly as a source file -target_sources(VulkanCppModule - PRIVATE - "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" -) +# set up Vulkan C++ module only if enabled +if(ENABLE_CPP20_MODULE) + add_library(VulkanCppModule) + add_library(Vulkan::cppm ALIAS VulkanCppModule) + + target_compile_definitions(VulkanCppModule + PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1 + ) + target_include_directories(VulkanCppModule + PUBLIC + "${Vulkan_INCLUDE_DIR}" + ) + target_link_libraries(VulkanCppModule + PUBLIC + Vulkan::Vulkan + ) + + set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20) + + # Add MSVC-specific compiler options for proper C++ module support + if(MSVC) + target_compile_options(VulkanCppModule PRIVATE + /std:c++latest # Use latest C++ standard for better module support + /permissive- # Standards conformance mode + /Zc:__cplusplus # Enable correct __cplusplus macro + /EHsc # Enable C++ exception handling + /Zc:preprocessor # Use conforming preprocessor + /translateInclude # Automatically translate #include to import for standard library + ) + endif() + + target_sources(VulkanCppModule + PUBLIC + FILE_SET cxx_modules TYPE CXX_MODULES + BASE_DIRS + "${Vulkan_INCLUDE_DIR}" + FILES + "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" + ) + + + # Add the vulkan.cppm file directly as a source file + target_sources(VulkanCppModule + PRIVATE + "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" + ) +else() + # Create a dummy interface library when C++ 20 module is disabled + add_library(VulkanCppModule INTERFACE) + add_library(Vulkan::cppm ALIAS VulkanCppModule) + target_link_libraries(VulkanCppModule INTERFACE Vulkan::Vulkan) + target_compile_definitions(VulkanCppModule + INTERFACE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1 + ) +endif() find_package(stb REQUIRED) set(STB_INCLUDEDIR ${stb_INCLUDE_DIRS}) @@ -107,6 +134,11 @@ function (add_chapter CHAPTER_NAME) target_link_libraries (${CHAPTER_NAME} Vulkan::cppm glfw) target_include_directories (${CHAPTER_NAME} PRIVATE ${STB_INCLUDEDIR}) + # Add compile definition if C++ 20 module is enabled + if(ENABLE_CPP20_MODULE) + target_compile_definitions(${CHAPTER_NAME} PRIVATE USE_CPP20_MODULES=1) + endif() + if(WIN32) if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*") set_target_properties(${CHAPTER_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${CHAPTER_NAME}") diff --git a/en/00_Introduction.adoc b/en/00_Introduction.adoc index aa6b9db1..6675f17d 100644 --- a/en/00_Introduction.adoc +++ b/en/00_Introduction.adoc @@ -99,11 +99,12 @@ We will use C{pp} features like classes and RAII to organize logic and To make it easier to learn to work with Vulkan, we'll be using the newer https://github.com/KhronosGroup/Vulkan-Hpp[Vulkan-Hpp] bindings that abstract some of the dirty work and help prevent certain classes of errors. -We'll also use Vulkan RAII and the Vulkan C{pp}20 module. With this -combination, we show how to use Vulkan in a method that will translate -directly into large projects where C{pp} libraries have traditionally caused -large build times while also showing one method of making Vulkan a joy to -work with. +We'll also use Vulkan RAII and, optionally, the Vulkan C{pp}20 module. The +attachments template has modules disabled by default for maximum compatibility, +but we recommend enabling them if your toolchain supports it. With this +combination, we show how to use Vulkan in a way that translates directly into +large projects where C{pp} libraries have traditionally caused long build times, +while also showing one method of making Vulkan a joy to work with. == License diff --git a/en/02_Development_environment.adoc b/en/02_Development_environment.adoc index 8a7032d6..2a0faca1 100644 --- a/en/02_Development_environment.adoc +++ b/en/02_Development_environment.adoc @@ -115,16 +115,27 @@ for an example of a working project. Vulkan has support for C{pp} modules which became available with c{pp}20. A large advantage of C{pp} modules is they give all the benefits of C{pp} without -the overhead long compile times. To do this, the .cppm file must be compiled -for your target device. This tutorial serves as an example of taking -advantage of C{pp} modules. The CMakeLists.txt in our tutorial has all the -instructions needed for building the module automatically: +the overhead of long compile times. To do this, the .cppm file must be compiled +for your target device. This tutorial demonstrates how to take advantage of C{pp} +modules. However, to maximize compatibility across compilers and IDEs, the +attachments template has C{pp}20 module support disabled by default, but it is +recommended to enable it if your toolchain supports it. + +To enable the Vulkan C{pp}20 module in the attachments template, configure CMake with: + +[,bash] +---- +cmake -DENABLE_CPP20_MODULE=ON .. +---- + +When enabled, the CMakeLists.txt contains all the instructions needed for building the +module automatically. The relevant snippet looks like this: [,cmake] ---- find_package (Vulkan REQUIRED) -# set up Vulkan C++ module +# set up Vulkan C++ module (enabled when ENABLE_CPP20_MODULE=ON) add_library(VulkanCppModule) add_library(Vulkan::cppm ALIAS VulkanCppModule) @@ -132,37 +143,34 @@ target_compile_definitions(VulkanCppModule PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1 ) -target_include_directories(VulkanCppModule - PRIVATE - "${Vulkan_INCLUDE_DIR}" -) -target_link_libraries(VulkanCppModule - PUBLIC - Vulkan::Vulkan -) + +target_include_directories(VulkanCppModule PRIVATE "${Vulkan_INCLUDE_DIR}") + +target_link_libraries(VulkanCppModule PUBLIC Vulkan::Vulkan) set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20) target_sources(VulkanCppModule PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES - BASE_DIRS - "${Vulkan_INCLUDE_DIR}" - FILES - "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" + BASE_DIRS "${Vulkan_INCLUDE_DIR}" + FILES "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm" ) ---- The VulkanCppModule target only needs to be defined once, then add it to the -dependency of your consuming project, and it will be built automatically, and -you won't need to also add Vulkan::Vulkan to your project. +dependencies of your consuming project, and it will be built automatically. You +won't need to also add Vulkan::Vulkan to your project. [,cmake] ---- target_link_libraries (${PROJECT_NAME} Vulkan::cppm) ---- -That is all that is required to add Vulkan to any project. +If you choose to keep modules disabled (the default), you can continue to use the +traditional header-based includes (e.g., `#include `). The +sample code in the attachments is written to compile either way and will import the +module only when `ENABLE_CPP20_MODULE=ON` (which defines `USE_CPP20_MODULES`). === Window Management @@ -491,4 +499,4 @@ You are now all set for xref:03_Drawing_a_triangle/00_Setup/00_Base_code.adoc[th == Android -Vulkan is a first-class API on Android and widely supported. But using it differs in several key areas from window management to build systems. So while the basic chapters focus on desktop platforms, the tutorial also has a xref:14_Android.adoc[dedicated chapter] that walks you through setting up your development environment and getting the tutorial code up-and-running on Android. \ No newline at end of file +Vulkan is a first-class API on Android and widely supported. But using it differs in several key areas from window management to build systems. So while the basic chapters focus on desktop platforms, the tutorial also has a xref:14_Android.adoc[dedicated chapter] that walks you through setting up your development environment and getting the tutorial code up-and-running on Android. diff --git a/en/03_Drawing_a_triangle/00_Setup/00_Base_code.adoc b/en/03_Drawing_a_triangle/00_Setup/00_Base_code.adoc index 70d845e2..462e85f8 100644 --- a/en/03_Drawing_a_triangle/00_Setup/00_Base_code.adoc +++ b/en/03_Drawing_a_triangle/00_Setup/00_Base_code.adoc @@ -10,7 +10,11 @@ from scratch with the following code: [,c++] ---- +#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) +#include +#else import vulkan_hpp; +#endif #include #include @@ -53,10 +57,12 @@ int main() { } ---- -We first include the Vulkan module from the LunarG SDK, which provides the -functions, structures and enumerations. The `stdexcept` and `iostream` headers -are included for reporting and propagating errors. The `cstdlib` -header provides the `EXIT_SUCCESS` and `EXIT_FAILURE` macros. +We first include the Vulkan-Hpp RAII header by default, which provides the +functions, structures and enumerations. If you enable C{pp}20 modules +(`-DENABLE_CPP20_MODULE=ON`), the code will `import vulkan_hpp;` instead via the +`USE_CPP20_MODULES` define set by CMake. The `stdexcept` and `iostream` headers +are included for reporting and propagating errors. The `cstdlib` header +provides the `EXIT_SUCCESS` and `EXIT_FAILURE` macros. The program itself is wrapped into a class where we'll store the Vulkan objects as private class members and add functions to initiate each of them, which will diff --git a/en/14_Android.adoc b/en/14_Android.adoc index ca9ac484..e5527d1e 100644 --- a/en/14_Android.adoc +++ b/en/14_Android.adoc @@ -312,7 +312,7 @@ include_directories(${ANDROID_NDK}/sources/android/game-activity/include) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Add the Vulkan C++ module +# (Optional) Add the Vulkan C++ module when ENABLE_CPP20_MODULE=ON add_library(VulkanCppModule SHARED) target_compile_definitions(VulkanCppModule PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1 @@ -388,7 +388,7 @@ Key points: * We find the Vulkan package and include the game-activity library instead of native_app_glue. * We set up shader compilation tools and define a function to compile shaders. -* We set the C{pp} standard to C{pp}20 and create a Vulkan C{pp} module. +* We set the C{pp} standard to C{pp}20 and optionally create a Vulkan C{pp} module (recommended) when modules are enabled. * We set up shader compilation for the 34_android chapter, copying shader source files from the main project. * We add the main native library, which uses the 34_android.cpp file from the main project and a bridge file to connect with GameActivity. * We link against the necessary libraries, including game-activity. @@ -840,4 +840,4 @@ The complete Android example can be found in the attachments/android directory. Remember that Vulkan HPP is not included by default in the Android NDK, so you'll need to download it separately from the https://github.com/KhronosGroup/Vulkan-Hpp[Vulkan-Hpp GitHub repository] or use the version included in the Vulkan SDK. -link:/attachments/34_android.cpp[C{pp} code] \ No newline at end of file +link:/attachments/34_android.cpp[C{pp} code]