|
| 1 | +cmake_minimum_required(VERSION 3.29) |
| 2 | + |
| 3 | +project(SimpleEngine VERSION 1.0.0 LANGUAGES CXX C) |
| 4 | + |
| 5 | +# Option to enable/disable Vulkan C++20 module support for this standalone project |
| 6 | +option(ENABLE_CPP20_MODULE "Enable C++ 20 module support for Vulkan in SimpleEngine" OFF) |
| 7 | + |
| 8 | +# Enable C++ module dependency scanning only when modules are enabled |
| 9 | +if(ENABLE_CPP20_MODULE) |
| 10 | + set(CMAKE_CXX_SCAN_FOR_MODULES ON) |
| 11 | +endif() |
| 12 | + |
| 13 | +# Add CMake module path for custom find modules |
| 14 | +set(SIMPLE_ENGINE_DIR "${CMAKE_CURRENT_LIST_DIR}/../simple_engine") |
| 15 | +list(APPEND CMAKE_MODULE_PATH "${SIMPLE_ENGINE_DIR}/CMake") |
| 16 | + |
| 17 | +# Find required packages |
| 18 | +find_package (glm REQUIRED) |
| 19 | +find_package (Vulkan REQUIRED) |
| 20 | +find_package (tinygltf REQUIRED) |
| 21 | +find_package (KTX REQUIRED) |
| 22 | + |
| 23 | +# Find or download Vulkan-Hpp headers matching the Vulkan SDK/NDK version |
| 24 | +find_package(VulkanHpp REQUIRED) |
| 25 | + |
| 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" |
| 53 | + ) |
| 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}") |
| 73 | +endif() |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +# Platform-specific settings |
| 78 | +if(ANDROID) |
| 79 | + # Android-specific settings |
| 80 | + add_definitions(-DPLATFORM_ANDROID) |
| 81 | + find_package(game-activity REQUIRED CONFIG) |
| 82 | +else() |
| 83 | + # Desktop-specific settings |
| 84 | + add_definitions(-DPLATFORM_DESKTOP) |
| 85 | + find_package(glfw3 REQUIRED) |
| 86 | + find_package(OpenAL REQUIRED) |
| 87 | +endif() |
| 88 | + |
| 89 | +# Shader compilation |
| 90 | +# Find Slang shaders from simple_engine (exclude utility modules) |
| 91 | +file(GLOB SLANG_SHADER_SOURCES ${SIMPLE_ENGINE_DIR}/shaders/*.slang) |
| 92 | +list(FILTER SLANG_SHADER_SOURCES EXCLUDE REGEX ".*/(common_types|pbr_utils|lighting_utils|tonemapping_utils)\\.slang$") |
| 93 | + |
| 94 | +# Find slangc executable (optional) |
| 95 | +find_program(SLANGC_EXECUTABLE slangc HINTS $ENV{VULKAN_SDK}/bin) |
| 96 | + |
| 97 | +if(SLANGC_EXECUTABLE) |
| 98 | + # Ensure the output directory for compiled shaders exists |
| 99 | + file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/shaders) |
| 100 | + |
| 101 | + # Compile Slang shaders using slangc |
| 102 | + foreach(SHADER ${SLANG_SHADER_SOURCES}) |
| 103 | + get_filename_component(SHADER_NAME ${SHADER} NAME) |
| 104 | + get_filename_component(SHADER_NAME_WE ${SHADER_NAME} NAME_WE) |
| 105 | + string(REGEX REPLACE "\.slang$" "" OUTPUT_NAME ${SHADER_NAME}) |
| 106 | + add_custom_command( |
| 107 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shaders/${OUTPUT_NAME}.spv |
| 108 | + COMMAND ${SLANGC_EXECUTABLE} ${SHADER} -target spirv -profile spirv_1_4 -emit-spirv-directly -o ${CMAKE_CURRENT_BINARY_DIR}/shaders/${OUTPUT_NAME}.spv |
| 109 | + DEPENDS ${SHADER} |
| 110 | + COMMENT "Compiling Slang shader ${SHADER_NAME} with slangc" |
| 111 | + ) |
| 112 | + list(APPEND SHADER_SPVS ${CMAKE_CURRENT_BINARY_DIR}/shaders/${OUTPUT_NAME}.spv) |
| 113 | + endforeach() |
| 114 | + |
| 115 | + add_custom_target(shaders DEPENDS ${SHADER_SPVS}) |
| 116 | +else() |
| 117 | + message(STATUS "slangc not found. Skipping shader compilation step.") |
| 118 | + add_custom_target(shaders) |
| 119 | +endif() |
| 120 | + |
| 121 | +# Source files |
| 122 | +# NOTE: Android builds include this project via `add_subdirectory(...)` from |
| 123 | +# `android/app/src/main/cpp/CMakeLists.txt`, so we must not require a desktop `main()`. |
| 124 | +set(SOURCES_COMMON |
| 125 | + engine.cpp |
| 126 | + scene_loading.cpp |
| 127 | + ${SIMPLE_ENGINE_DIR}/platform.cpp |
| 128 | + renderer_core.cpp |
| 129 | + renderer_rendering.cpp |
| 130 | + renderer_pipelines.cpp |
| 131 | + renderer_compute.cpp |
| 132 | + renderer_utils.cpp |
| 133 | + renderer_resources.cpp |
| 134 | + renderer_ray_query.cpp |
| 135 | + memory_pool.cpp |
| 136 | + ${SIMPLE_ENGINE_DIR}/resource_manager.cpp |
| 137 | + ${SIMPLE_ENGINE_DIR}/entity.cpp |
| 138 | + ${SIMPLE_ENGINE_DIR}/component.cpp |
| 139 | + ${SIMPLE_ENGINE_DIR}/transform_component.cpp |
| 140 | + ${SIMPLE_ENGINE_DIR}/mesh_component.cpp |
| 141 | + ${SIMPLE_ENGINE_DIR}/camera_component.cpp |
| 142 | + ${SIMPLE_ENGINE_DIR}/animation_component.cpp |
| 143 | + model_loader.cpp |
| 144 | + audio_system.cpp |
| 145 | + physics_system.cpp |
| 146 | + imgui_system.cpp |
| 147 | + ${SIMPLE_ENGINE_DIR}/imgui/imgui.cpp |
| 148 | + ${SIMPLE_ENGINE_DIR}/imgui/imgui_draw.cpp |
| 149 | + ${SIMPLE_ENGINE_DIR}/vulkan_device.cpp |
| 150 | + pipeline.cpp |
| 151 | + ${SIMPLE_ENGINE_DIR}/descriptor_manager.cpp |
| 152 | + ${SIMPLE_ENGINE_DIR}/renderdoc_debug_system.cpp |
| 153 | + ${SIMPLE_ENGINE_DIR}/mikktspace.c |
| 154 | +) |
| 155 | + |
| 156 | +set(SOURCES_DESKTOP |
| 157 | + main.cpp |
| 158 | +) |
| 159 | + |
| 160 | +# Create target |
| 161 | +if (ANDROID) |
| 162 | + # Android: build the engine as a library to be linked into the app's `simple_engine_android` SHARED library. |
| 163 | + add_library(SimpleEngine STATIC ${SOURCES_COMMON}) |
| 164 | +else () |
| 165 | + # Desktop: build the runnable executable (unchanged behavior vs `HEAD`). |
| 166 | + add_executable(SimpleEngine ${SOURCES_COMMON} ${SOURCES_DESKTOP}) |
| 167 | +endif () |
| 168 | + |
| 169 | +target_include_directories(SimpleEngine PRIVATE |
| 170 | + ${CMAKE_CURRENT_SOURCE_DIR} |
| 171 | + ${SIMPLE_ENGINE_DIR} |
| 172 | + ${SIMPLE_ENGINE_DIR}/imgui |
| 173 | +) |
| 174 | + |
| 175 | +add_dependencies(SimpleEngine shaders) |
| 176 | +set_target_properties (SimpleEngine PROPERTIES CXX_STANDARD 20) |
| 177 | + |
| 178 | +# Enable required defines for GLM experimental extensions and MSVC math constants |
| 179 | +target_compile_definitions(SimpleEngine PRIVATE |
| 180 | + GLM_ENABLE_EXPERIMENTAL |
| 181 | + _USE_MATH_DEFINES |
| 182 | + VULKAN_HPP_NO_STRUCT_CONSTRUCTORS |
| 183 | + VULKAN_HPP_DISPATCH_LOADER_DYNAMIC |
| 184 | +) |
| 185 | + |
| 186 | +# Link libraries |
| 187 | +# Prefer the Vulkan C++ module target when available (configured at the parent level), |
| 188 | +# but fall back to the standard Vulkan library otherwise. |
| 189 | +if(TARGET Vulkan::cppm) |
| 190 | + target_link_libraries(SimpleEngine PUBLIC Vulkan::cppm) |
| 191 | +else() |
| 192 | + target_link_libraries(SimpleEngine PUBLIC Vulkan::Vulkan) |
| 193 | +endif() |
| 194 | + |
| 195 | +target_link_libraries(SimpleEngine PUBLIC |
| 196 | + glm::glm |
| 197 | + tinygltf::tinygltf |
| 198 | + KTX::ktx |
| 199 | +) |
| 200 | + |
| 201 | +if (ANDROID) |
| 202 | + target_link_libraries(SimpleEngine PUBLIC game-activity::game-activity OpenSLES android log) |
| 203 | +else () |
| 204 | + target_link_libraries(SimpleEngine PRIVATE glfw OpenAL::OpenAL) |
| 205 | +endif() |
| 206 | + |
| 207 | +# Windows/MSVC portability and build settings |
| 208 | +if(MSVC) |
| 209 | + # Avoid Windows.h macro pollution and CRT warnings; improve conformance and build perf |
| 210 | + target_compile_definitions(SimpleEngine PRIVATE |
| 211 | + NOMINMAX |
| 212 | + WIN32_LEAN_AND_MEAN |
| 213 | + _CRT_SECURE_NO_WARNINGS |
| 214 | + ) |
| 215 | + target_compile_options(SimpleEngine PRIVATE |
| 216 | + /permissive- |
| 217 | + /Zc:__cplusplus |
| 218 | + /EHsc |
| 219 | + /W3 |
| 220 | + /MP |
| 221 | + /bigobj |
| 222 | + ) |
| 223 | + # Crash reporter uses Dbghelp; pragma should suffice, but make it explicit for clarity |
| 224 | + target_link_libraries(SimpleEngine PRIVATE Dbghelp) |
| 225 | +elseif(WIN32) |
| 226 | + # Non-MSVC Windows toolchains (e.g., MinGW) |
| 227 | + target_compile_definitions(SimpleEngine PRIVATE |
| 228 | + NOMINMAX |
| 229 | + WIN32_LEAN_AND_MEAN |
| 230 | + _CRT_SECURE_NO_WARNINGS |
| 231 | + ) |
| 232 | +endif() |
| 233 | + |
| 234 | +# Copy model and texture files from simple_engine if they exist |
| 235 | +if(EXISTS ${SIMPLE_ENGINE_DIR}/models) |
| 236 | + if (NOT ANDROID) |
| 237 | + add_custom_command(TARGET SimpleEngine POST_BUILD |
| 238 | + COMMAND ${CMAKE_COMMAND} -E copy_directory ${SIMPLE_ENGINE_DIR}/models ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/models |
| 239 | + COMMENT "Copying models to output directory" |
| 240 | + ) |
| 241 | +endif() |
| 242 | +endif () |
| 243 | + |
| 244 | +if(EXISTS ${SIMPLE_ENGINE_DIR}/textures) |
| 245 | + if (NOT ANDROID) |
| 246 | + add_custom_command(TARGET SimpleEngine POST_BUILD |
| 247 | + COMMAND ${CMAKE_COMMAND} -E copy_directory ${SIMPLE_ENGINE_DIR}/textures ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/textures |
| 248 | + COMMENT "Copying textures to output directory" |
| 249 | + ) |
| 250 | +endif() |
| 251 | +endif () |
| 252 | + |
| 253 | +# Add packaging configuration |
| 254 | +include(CPack) |
| 255 | + |
| 256 | +# Set package properties |
| 257 | +set(CPACK_PACKAGE_NAME "SimpleEngine") |
| 258 | +set(CPACK_PACKAGE_VENDOR "SimpleEngine Team") |
| 259 | +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A simple game engine built with Vulkan") |
| 260 | +set(CPACK_PACKAGE_VERSION "1.0.0") |
| 261 | +set(CPACK_PACKAGE_VERSION_MAJOR "1") |
| 262 | +set(CPACK_PACKAGE_VERSION_MINOR "0") |
| 263 | +set(CPACK_PACKAGE_VERSION_PATCH "0") |
| 264 | +set(CPACK_PACKAGE_INSTALL_DIRECTORY "SimpleEngine") |
| 265 | + |
| 266 | +# Set platform-specific package generators |
| 267 | +if(WIN32) |
| 268 | + set(CPACK_GENERATOR "ZIP;NSIS") |
| 269 | + set(CPACK_NSIS_PACKAGE_NAME "SimpleEngine") |
| 270 | + set(CPACK_NSIS_DISPLAY_NAME "SimpleEngine") |
| 271 | + set(CPACK_NSIS_HELP_LINK "https://github.com/yourusername/SimpleEngine") |
| 272 | + set(CPACK_NSIS_URL_INFO_ABOUT "https://github.com/yourusername/SimpleEngine") |
| 273 | + set(CPACK_NSIS_CONTACT "your.email@example.com") |
| 274 | + set(CPACK_NSIS_MODIFY_PATH ON) |
| 275 | +elseif(APPLE) |
| 276 | + set(CPACK_GENERATOR "ZIP;DragNDrop") |
| 277 | + set(CPACK_DMG_VOLUME_NAME "SimpleEngine") |
| 278 | + set(CPACK_DMG_FORMAT "UDBZ") |
| 279 | +else() |
| 280 | + set(CPACK_GENERATOR "ZIP;DEB") |
| 281 | + set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Your Name <your.email@example.com>") |
| 282 | + set(CPACK_DEBIAN_PACKAGE_SECTION "games") |
| 283 | + set(CPACK_DEBIAN_PACKAGE_DEPENDS "libvulkan1, libglfw3, libglm-dev, libktx-dev") |
| 284 | +endif() |
| 285 | + |
| 286 | +# Include binary and resource directories in the package |
| 287 | +if (NOT ANDROID) |
| 288 | +install(TARGETS SimpleEngine DESTINATION bin) |
| 289 | +if(SLANGC_EXECUTABLE) |
| 290 | + install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/shaders DESTINATION share/SimpleEngine) |
| 291 | +endif() |
| 292 | + |
| 293 | +# Install models and textures if they exist in simple_engine |
| 294 | +if(EXISTS ${SIMPLE_ENGINE_DIR}/models) |
| 295 | + install(DIRECTORY ${SIMPLE_ENGINE_DIR}/models DESTINATION share/SimpleEngine) |
| 296 | +endif() |
| 297 | + |
| 298 | +if(EXISTS ${SIMPLE_ENGINE_DIR}/textures) |
| 299 | + install(DIRECTORY ${SIMPLE_ENGINE_DIR}/textures DESTINATION share/SimpleEngine) |
| 300 | +endif() |
| 301 | + |
| 302 | +# Install README from simple_engine if it exists |
| 303 | +if(EXISTS ${SIMPLE_ENGINE_DIR}/README.md) |
| 304 | + install(FILES ${SIMPLE_ENGINE_DIR}/README.md DESTINATION share/SimpleEngine) |
| 305 | +endif() |
| 306 | +endif () |
0 commit comments