Skip to content

Commit d7aaf9b

Browse files
committed
Make C++20 module support disabled by default for Vulkan tutorial
- Update tutorial documentation to reflect optional module usage. - Adjust CMake option `ENABLE_CPP20_MODULE` default to `OFF`. - Incorporate fallback to traditional header-based includes when modules are disabled. - Clarify enablement steps and compatibility details in example code and instructions.
1 parent 4b18dac commit d7aaf9b

File tree

5 files changed

+50
-35
lines changed

5 files changed

+50
-35
lines changed

attachments/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project (VulkanTutorial)
55
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
66

77
# Add option to enable/disable C++ 20 module
8-
option(ENABLE_CPP20_MODULE "Enable C++ 20 module support for Vulkan" ON)
8+
option(ENABLE_CPP20_MODULE "Enable C++ 20 module support for Vulkan" OFF)
99

1010
# Enable C++ module dependency scanning only if C++ 20 module is enabled
1111
if(ENABLE_CPP20_MODULE)
@@ -121,7 +121,7 @@ function (add_chapter CHAPTER_NAME)
121121
set_target_properties (${CHAPTER_NAME} PROPERTIES CXX_STANDARD 20)
122122
target_link_libraries (${CHAPTER_NAME} Vulkan::cppm glfw)
123123
target_include_directories (${CHAPTER_NAME} PRIVATE ${STB_INCLUDEDIR})
124-
124+
125125
# Add compile definition if C++ 20 module is enabled
126126
if(ENABLE_CPP20_MODULE)
127127
target_compile_definitions(${CHAPTER_NAME} PRIVATE USE_CPP20_MODULES=1)

en/00_Introduction.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ We will use C{pp} features like classes and RAII to organize logic and
9999
To make it easier to learn to work with Vulkan, we'll be using the newer
100100
https://github.com/KhronosGroup/Vulkan-Hpp[Vulkan-Hpp] bindings that
101101
abstract some of the dirty work and help prevent certain classes of errors.
102-
We'll also use Vulkan RAII and the Vulkan C{pp}20 module. With this
103-
combination, we show how to use Vulkan in a method that will translate
104-
directly into large projects where C{pp} libraries have traditionally caused
105-
large build times while also showing one method of making Vulkan a joy to
106-
work with.
102+
We'll also use Vulkan RAII and, optionally, the Vulkan C{pp}20 module. The
103+
attachments template has modules disabled by default for maximum compatibility,
104+
but we recommend enabling them if your toolchain supports it. With this
105+
combination, we show how to use Vulkan in a way that translates directly into
106+
large projects where C{pp} libraries have traditionally caused long build times,
107+
while also showing one method of making Vulkan a joy to work with.
107108

108109
== License
109110

en/02_Development_environment.adoc

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,54 +115,62 @@ for an example of a working project.
115115

116116
Vulkan has support for C{pp} modules which became available with c{pp}20. A
117117
large advantage of C{pp} modules is they give all the benefits of C{pp} without
118-
the overhead long compile times. To do this, the .cppm file must be compiled
119-
for your target device. This tutorial serves as an example of taking
120-
advantage of C{pp} modules. The CMakeLists.txt in our tutorial has all the
121-
instructions needed for building the module automatically:
118+
the overhead of long compile times. To do this, the .cppm file must be compiled
119+
for your target device. This tutorial demonstrates how to take advantage of C{pp}
120+
modules. However, to maximize compatibility across compilers and IDEs, the
121+
attachments template has C{pp}20 module support disabled by default, but it is
122+
recommended to enable it if your toolchain supports it.
123+
124+
To enable the Vulkan C{pp}20 module in the attachments template, configure CMake with:
125+
126+
[,bash]
127+
----
128+
cmake -DENABLE_CPP20_MODULE=ON ..
129+
----
130+
131+
When enabled, the CMakeLists.txt contains all the instructions needed for building the
132+
module automatically. The relevant snippet looks like this:
122133

123134
[,cmake]
124135
----
125136
find_package (Vulkan REQUIRED)
126137
127-
# set up Vulkan C++ module
138+
# set up Vulkan C++ module (enabled when ENABLE_CPP20_MODULE=ON)
128139
add_library(VulkanCppModule)
129140
add_library(Vulkan::cppm ALIAS VulkanCppModule)
130141
131142
target_compile_definitions(VulkanCppModule PUBLIC
132143
VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1
133144
VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
134145
)
135-
target_include_directories(VulkanCppModule
136-
PRIVATE
137-
"${Vulkan_INCLUDE_DIR}"
138-
)
139-
target_link_libraries(VulkanCppModule
140-
PUBLIC
141-
Vulkan::Vulkan
142-
)
146+
147+
target_include_directories(VulkanCppModule PRIVATE "${Vulkan_INCLUDE_DIR}")
148+
149+
target_link_libraries(VulkanCppModule PUBLIC Vulkan::Vulkan)
143150
144151
set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20)
145152
146153
target_sources(VulkanCppModule
147154
PUBLIC
148155
FILE_SET cxx_modules TYPE CXX_MODULES
149-
BASE_DIRS
150-
"${Vulkan_INCLUDE_DIR}"
151-
FILES
152-
"${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm"
156+
BASE_DIRS "${Vulkan_INCLUDE_DIR}"
157+
FILES "${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm"
153158
)
154159
----
155160

156161
The VulkanCppModule target only needs to be defined once, then add it to the
157-
dependency of your consuming project, and it will be built automatically, and
158-
you won't need to also add Vulkan::Vulkan to your project.
162+
dependencies of your consuming project, and it will be built automatically. You
163+
won't need to also add Vulkan::Vulkan to your project.
159164

160165
[,cmake]
161166
----
162167
target_link_libraries (${PROJECT_NAME} Vulkan::cppm)
163168
----
164169

165-
That is all that is required to add Vulkan to any project.
170+
If you choose to keep modules disabled (the default), you can continue to use the
171+
traditional header-based includes (e.g., `#include <vulkan/vulkan_raii.hpp>`). The
172+
sample code in the attachments is written to compile either way and will import the
173+
module only when `ENABLE_CPP20_MODULE=ON` (which defines `USE_CPP20_MODULES`).
166174

167175
=== Window Management
168176

@@ -491,4 +499,4 @@ You are now all set for xref:03_Drawing_a_triangle/00_Setup/00_Base_code.adoc[th
491499

492500
== Android
493501

494-
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.
502+
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.

en/03_Drawing_a_triangle/00_Setup/00_Base_code.adoc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ from scratch with the following code:
1010

1111
[,c++]
1212
----
13+
#if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES)
14+
#include <vulkan/vulkan_raii.hpp>
15+
#else
1316
import vulkan_hpp;
17+
#endif
1418
#include <GLFW/glfw3.h>
1519
1620
#include <iostream>
@@ -53,10 +57,12 @@ int main() {
5357
}
5458
----
5559

56-
We first include the Vulkan module from the LunarG SDK, which provides the
57-
functions, structures and enumerations. The `stdexcept` and `iostream` headers
58-
are included for reporting and propagating errors. The `cstdlib`
59-
header provides the `EXIT_SUCCESS` and `EXIT_FAILURE` macros.
60+
We first include the Vulkan-Hpp RAII header by default, which provides the
61+
functions, structures and enumerations. If you enable C{pp}20 modules
62+
(`-DENABLE_CPP20_MODULE=ON`), the code will `import vulkan_hpp;` instead via the
63+
`USE_CPP20_MODULES` define set by CMake. The `stdexcept` and `iostream` headers
64+
are included for reporting and propagating errors. The `cstdlib` header
65+
provides the `EXIT_SUCCESS` and `EXIT_FAILURE` macros.
6066

6167
The program itself is wrapped into a class where we'll store the Vulkan objects
6268
as private class members and add functions to initiate each of them, which will

en/14_Android.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ include_directories(${ANDROID_NDK}/sources/android/game-activity/include)
312312
set(CMAKE_CXX_STANDARD 20)
313313
set(CMAKE_CXX_STANDARD_REQUIRED ON)
314314
315-
# Add the Vulkan C++ module
315+
# (Optional) Add the Vulkan C++ module when ENABLE_CPP20_MODULE=ON
316316
add_library(VulkanCppModule SHARED)
317317
target_compile_definitions(VulkanCppModule
318318
PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
@@ -388,7 +388,7 @@ Key points:
388388

389389
* We find the Vulkan package and include the game-activity library instead of native_app_glue.
390390
* We set up shader compilation tools and define a function to compile shaders.
391-
* We set the C{pp} standard to C{pp}20 and create a Vulkan C{pp} module.
391+
* We set the C{pp} standard to C{pp}20 and optionally create a Vulkan C{pp} module (recommended) when modules are enabled.
392392
* We set up shader compilation for the 34_android chapter, copying shader source files from the main project.
393393
* 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.
394394
* 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.
840840

841841
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.
842842

843-
link:/attachments/34_android.cpp[C{pp} code]
843+
link:/attachments/34_android.cpp[C{pp} code]

0 commit comments

Comments
 (0)