Skip to content

Commit 8dca074

Browse files
committed
This is kinda what I had in mind. It won't work in Linux the way I wanted it to and it's kind of a MSVC only solution as that has the working directory CMake variable. Doing a symlink would require a bit more code changes in the actual tutorial itself. So, it'll need to take a little more thought. However, it's probably possible to make it so you could load/build on any platform from both the parent directory and the subdirectories. It's just we're going to need to think through it a bit to get it right for all platforms.
Give this a try and see if you like it in Windows. THe goal of this change is to enable your desired goal of loading/building from the top level and not having to copy assets to the top level. I haven't tested this yet, so I'm doing it kinda blind (am working on the next tutorial so have a bunch of stuff everywhere; so hard to test); but it should work I think.
1 parent 7e49123 commit 8dca074

3 files changed

Lines changed: 83 additions & 120 deletions

File tree

CMakeLists.txt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
cmake_minimum_required (VERSION 3.29)
22

33
project (VulkanTutorialRoot)
4-
# This is to allow the CMakeLists.txt files in attachments/ to be accessed from root level
5-
# Use command: cmake -S . -B build to configure from root level
6-
# Then use command: cmake --build build to build from root level
7-
# This will generate the build files in the build/ directory
8-
# The build files will reference the source files in the attachments/ directory
9-
# No need to change or delete any lines in attachments/ CMakeLists.txt files
10-
# You can still build from attachments/ directory if needed
11-
# Just navigate to attachments/ and run cmake and build commands as usual
4+
5+
if(MSVC)
6+
set(CMAKE_VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/attachments")
7+
endif()
8+
129
add_subdirectory(attachments)
10+
add_subdirectory(attachments/simple_engine)
1311

14-
# Any other subdirectories can be added here
15-
# Need to be carefull using CMAKE_SOURCE_DIR is points to the root level CMakeLists.txt
16-
# Use CMAKE_CURRENT_SOURCE_DIR to point to the current directory instead
17-
# For Simple Vulkan Engine project please uncomment the following line
18-
# add_subdirectory(attachments/simple_engine)
12+
# 6. Target-specific adjustments after they are created.
13+
if(TARGET SimpleEngine)
14+
if(MSVC)
15+
# On Windows, we set the working directory to a subdirectory of simple_engine
16+
# so that "../Assets" resolves correctly to "attachments/simple_engine/Assets".
17+
set_target_properties(SimpleEngine PROPERTIES
18+
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/attachments/simple_engine/build")
19+
endif()
20+
endif()

attachments/CMakeLists.txt

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ if(ENABLE_CPP20_MODULE)
5151
endif()
5252

5353
target_sources(VulkanCppModule
54-
PUBLIC
55-
FILE_SET cxx_modules TYPE CXX_MODULES
56-
BASE_DIRS
57-
"${Vulkan_INCLUDE_DIR}"
58-
FILES
59-
"${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm"
54+
PUBLIC
55+
FILE_SET cxx_modules TYPE CXX_MODULES
56+
BASE_DIRS
57+
"${Vulkan_INCLUDE_DIR}"
58+
FILES
59+
"${Vulkan_INCLUDE_DIR}/vulkan/vulkan.cppm"
6060
)
6161

6262

@@ -85,12 +85,7 @@ find_program(SLANGC_EXECUTABLE slangc HINTS $ENV{VULKAN_SDK}/bin REQUIRED)
8585

8686
function (add_shaders_target TARGET)
8787
cmake_parse_arguments ("SHADER" "" "CHAPTER_NAME" "SOURCES" ${ARGN})
88-
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*" OR
89-
${CMAKE_GENERATOR} MATCHES "Ninja Multi-Config")
90-
set (SHADERS_DIR ${CMAKE_BINARY_DIR}/${SHADER_CHAPTER_NAME}/$<CONFIG>/shaders)
91-
else()
92-
set (SHADERS_DIR ${CMAKE_BINARY_DIR}/${SHADER_CHAPTER_NAME}/shaders)
93-
endif()
88+
set (SHADERS_DIR ${SHADER_CHAPTER_NAME}/shaders)
9489
add_custom_command (
9590
OUTPUT ${SHADERS_DIR}
9691
COMMAND ${CMAKE_COMMAND} -E make_directory ${SHADERS_DIR}
@@ -100,37 +95,32 @@ function (add_shaders_target TARGET)
10095
COMMAND glslang::validator
10196
ARGS --target-env vulkan1.0 ${SHADER_SOURCES} --quiet
10297
WORKING_DIRECTORY ${SHADERS_DIR}
103-
DEPENDS ${SHADER_SOURCES}
104-
COMMENT "Compiling Shaders for ${TARGET}"
98+
DEPENDS ${SHADERS_DIR} ${SHADER_SOURCES}
99+
COMMENT "Compiling Shaders"
105100
VERBATIM
106101
)
107102
add_custom_target (${TARGET} DEPENDS ${SHADERS_DIR}/frag.spv ${SHADERS_DIR}/vert.spv)
108103
endfunction ()
109104

110105
function (add_slang_shader_target TARGET)
111106
cmake_parse_arguments ("SHADER" "" "CHAPTER_NAME" "SOURCES" ${ARGN})
112-
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*" OR
113-
${CMAKE_GENERATOR} MATCHES "Ninja Multi-Config")
114-
set (SHADERS_DIR ${CMAKE_BINARY_DIR}/${SHADER_CHAPTER_NAME}/$<CONFIG>/shaders)
115-
else()
116-
set (SHADERS_DIR ${CMAKE_BINARY_DIR}/${SHADER_CHAPTER_NAME}/shaders)
117-
endif()
107+
set (SHADERS_DIR ${SHADER_CHAPTER_NAME}/shaders)
118108
file(GLOB HAS_COMPUTE ${CHAPTER_SHADER}.comp)
119109
set (ENTRY_POINTS -entry vertMain -entry fragMain)
120110
if(HAS_COMPUTE)
121111
list(APPEND ENTRY_POINTS -entry compMain)
122112
endif()
123113
add_custom_command (
124-
OUTPUT ${SHADERS_DIR}
125-
COMMAND ${CMAKE_COMMAND} -E make_directory ${SHADERS_DIR}
114+
OUTPUT ${SHADERS_DIR}
115+
COMMAND ${CMAKE_COMMAND} -E make_directory ${SHADERS_DIR}
126116
)
127117
add_custom_command (
128-
OUTPUT ${SHADERS_DIR}/slang.spv
129-
COMMAND ${SLANGC_EXECUTABLE} ${SHADER_SOURCES} -target spirv -profile spirv_1_4+spvRayQueryKHR -emit-spirv-directly -fvk-use-entrypoint-name ${ENTRY_POINTS} -o slang.spv
130-
WORKING_DIRECTORY ${SHADERS_DIR}
131-
DEPENDS ${SHADER_SOURCES}
132-
COMMENT "Compiling Slang Shaders for ${TARGET}"
133-
VERBATIM
118+
OUTPUT ${SHADERS_DIR}/slang.spv
119+
COMMAND ${SLANGC_EXECUTABLE} ${SHADER_SOURCES} -target spirv -profile spirv_1_4+spvRayQueryKHR -emit-spirv-directly -fvk-use-entrypoint-name ${ENTRY_POINTS} -o slang.spv
120+
WORKING_DIRECTORY ${SHADERS_DIR}
121+
DEPENDS ${SHADERS_DIR} ${SHADER_SOURCES}
122+
COMMENT "Compiling Slang Shaders"
123+
VERBATIM
134124
)
135125
add_custom_target (${TARGET} DEPENDS ${SHADERS_DIR}/slang.spv)
136126
endfunction()
@@ -152,10 +142,9 @@ function (add_chapter CHAPTER_NAME)
152142
# Define VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS to treat VK_ERROR_OUT_OF_DATE_KHR as a success code
153143
target_compile_definitions(${CHAPTER_NAME} PRIVATE "VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS" )
154144

155-
if(WIN32)
156-
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
157-
set_target_properties(${CHAPTER_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${CHAPTER_NAME}")
158-
endif()
145+
# Set working directory for Visual Studio
146+
if(MSVC)
147+
set_target_properties(${CHAPTER_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${CHAPTER_NAME}")
159148
endif()
160149

161150
if (DEFINED CHAPTER_SHADER)
@@ -177,43 +166,13 @@ function (add_chapter CHAPTER_NAME)
177166
if (DEFINED CHAPTER_MODELS)
178167
list(TRANSFORM CHAPTER_MODELS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/assets/")
179168
file (COPY ${CHAPTER_MODELS} DESTINATION ${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/models)
180-
181-
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*" OR
182-
${CMAKE_GENERATOR} MATCHES "Ninja Multi-Config")
183-
sync_directory(
184-
"${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/models"
185-
"${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/$<CONFIG>/models"
186-
${CHAPTER_NAME}
187-
"${CHAPTER_NAME} - Models"
188-
)
189-
endif()
190169
endif ()
191170
if (DEFINED CHAPTER_TEXTURES)
192171
list(TRANSFORM CHAPTER_TEXTURES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/assets/")
193172
file (COPY ${CHAPTER_TEXTURES} DESTINATION ${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/textures)
194-
195-
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*" OR
196-
${CMAKE_GENERATOR} MATCHES "Ninja Multi-Config")
197-
sync_directory(
198-
"${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/textures"
199-
"${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/$<CONFIG>/textures"
200-
${CHAPTER_NAME}
201-
"${CHAPTER_NAME} - Textures"
202-
)
203-
endif()
204173
endif ()
205174
endfunction ()
206175

207-
function(sync_directory SRC_DIR DST_DIR TARGET_NAME LABEL)
208-
add_custom_command(
209-
TARGET ${TARGET_NAME}
210-
POST_BUILD
211-
COMMAND ${CMAKE_COMMAND} -E make_directory "${DST_DIR}"
212-
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different "${SRC_DIR}" "${DST_DIR}"
213-
COMMENT "${LABEL}: Syncing directory"
214-
)
215-
endfunction()
216-
217176
add_chapter (00_base_code)
218177

219178
add_chapter (01_instance_creation)

attachments/simple_engine/CMakeLists.txt

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,55 @@ find_package (KTX REQUIRED)
2222
# Find or download Vulkan-Hpp headers matching the Vulkan SDK/NDK version
2323
find_package(VulkanHpp REQUIRED)
2424

25-
if(ENABLE_CPP20_MODULE)
26-
# Set up Vulkan C++ module for this standalone project
27-
add_library(VulkanCppModule)
28-
add_library(Vulkan::cppm ALIAS VulkanCppModule)
29-
30-
target_compile_definitions(VulkanCppModule
31-
PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
32-
)
33-
target_include_directories(VulkanCppModule
34-
PUBLIC
35-
"${Vulkan_INCLUDE_DIR}"
36-
"${VulkanHpp_INCLUDE_DIRS}"
37-
)
38-
target_link_libraries(VulkanCppModule
39-
PUBLIC
40-
Vulkan::Vulkan
41-
)
42-
43-
set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20)
44-
45-
target_sources(VulkanCppModule
46-
PUBLIC
47-
FILE_SET cxx_modules TYPE CXX_MODULES
48-
BASE_DIRS
49-
"${VulkanHpp_CPPM_DIR}"
50-
FILES
51-
"${VulkanHpp_CPPM_DIR}/vulkan/vulkan.cppm"
52-
)
53-
54-
# MSVC-specific options to improve module support
55-
if(MSVC)
56-
target_compile_options(VulkanCppModule PRIVATE
57-
/std:c++latest
58-
/permissive-
59-
/Zc:__cplusplus
60-
/EHsc
61-
/Zc:preprocessor
25+
if(NOT TARGET VulkanCppModule)
26+
if(ENABLE_CPP20_MODULE)
27+
# Set up Vulkan C++ module for this standalone project
28+
add_library(VulkanCppModule)
29+
add_library(Vulkan::cppm ALIAS VulkanCppModule)
30+
31+
target_compile_definitions(VulkanCppModule
32+
PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
33+
)
34+
target_include_directories(VulkanCppModule
35+
PUBLIC
36+
"${Vulkan_INCLUDE_DIR}"
37+
"${VulkanHpp_INCLUDE_DIRS}"
38+
)
39+
target_link_libraries(VulkanCppModule
40+
PUBLIC
41+
Vulkan::Vulkan
42+
)
43+
44+
set_target_properties(VulkanCppModule PROPERTIES CXX_STANDARD 20)
45+
46+
target_sources(VulkanCppModule
47+
PUBLIC
48+
FILE_SET cxx_modules TYPE CXX_MODULES
49+
BASE_DIRS
50+
"${VulkanHpp_CPPM_DIR}"
51+
FILES
52+
"${VulkanHpp_CPPM_DIR}/vulkan/vulkan.cppm"
6253
)
54+
55+
# MSVC-specific options to improve module support
56+
if(MSVC)
57+
target_compile_options(VulkanCppModule PRIVATE
58+
/std:c++latest
59+
/permissive-
60+
/Zc:__cplusplus
61+
/EHsc
62+
/Zc:preprocessor
63+
)
64+
endif()
65+
else()
66+
add_library(VulkanCppModule INTERFACE)
67+
add_library(Vulkan::cppm ALIAS VulkanCppModule)
68+
target_link_libraries(VulkanCppModule INTERFACE Vulkan::Vulkan)
69+
target_compile_definitions(VulkanCppModule
70+
INTERFACE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
71+
)
72+
target_include_directories(VulkanCppModule INTERFACE "${VulkanHpp_INCLUDE_DIRS}")
6373
endif()
64-
else()
65-
add_library(VulkanCppModule INTERFACE)
66-
add_library(Vulkan::cppm ALIAS VulkanCppModule)
67-
target_link_libraries(VulkanCppModule INTERFACE Vulkan::Vulkan)
68-
target_compile_definitions(VulkanCppModule
69-
INTERFACE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 VULKAN_HPP_NO_STRUCT_CONSTRUCTORS=1
70-
)
71-
target_include_directories(VulkanCppModule INTERFACE "${VulkanHpp_INCLUDE_DIRS}")
7274
endif()
7375

7476

0 commit comments

Comments
 (0)