|
| 1 | +# ============================================================================= |
| 2 | +# Courses/CMakeLists.txt |
| 3 | +# ============================================================================= |
| 4 | +# |
| 5 | +# This subdirectory contains self-contained course modules that extend the |
| 6 | +# Simple Engine with new rendering features. Each module is optional — if |
| 7 | +# the parent CMakeLists.txt includes this directory with: |
| 8 | +# |
| 9 | +# option(ENABLE_COURSES "Build the Simple Engine course modules" ON) |
| 10 | +# if(ENABLE_COURSES) |
| 11 | +# add_subdirectory(Courses) |
| 12 | +# endif() |
| 13 | +# |
| 14 | +# then all enabled courses are compiled as a STATIC library that is linked |
| 15 | +# into the SimpleEngine target. No existing engine source files are modified |
| 16 | +# by this build; the modules attach to the engine via the public extension |
| 17 | +# points added to renderer.h. |
| 18 | +# |
| 19 | +# ADDING A NEW COURSE |
| 20 | +# ------------------- |
| 21 | +# 1. Create a subdirectory under Courses/ (e.g. Courses/My_New_Topic/). |
| 22 | +# 2. Add its source files to the COURSE_SOURCES list below. |
| 23 | +# 3. If the course has a Slang shader, add it to COURSE_SLANG_SHADERS below. |
| 24 | +# 4. Document the course in en/Building_a_Simple_Engine/Courses/. |
| 25 | +# |
| 26 | +# The course modules in this file: |
| 27 | +# * Opacity Micromaps — VK_EXT_opacity_micromap hardware shadow acceleration |
| 28 | +# ============================================================================= |
| 29 | + |
| 30 | +cmake_minimum_required(VERSION 3.29) |
| 31 | + |
| 32 | +# ───────────────────────────────────────────────────────────────────────────── |
| 33 | +# Per-course opt-in options |
| 34 | +# ───────────────────────────────────────────────────────────────────────────── |
| 35 | +option(ENABLE_COURSE_OPACITY_MICROMAPS |
| 36 | + "Build the Opacity Micromaps course module (VK_EXT_opacity_micromap)" |
| 37 | + ON) |
| 38 | + |
| 39 | +# ───────────────────────────────────────────────────────────────────────────── |
| 40 | +# Collect sources from enabled course modules |
| 41 | +# ───────────────────────────────────────────────────────────────────────────── |
| 42 | +set(COURSE_SOURCES "") |
| 43 | +set(COURSE_SLANG_SHADERS "") |
| 44 | + |
| 45 | +if(ENABLE_COURSE_OPACITY_MICROMAPS) |
| 46 | + list(APPEND COURSE_SOURCES |
| 47 | + ${CMAKE_CURRENT_SOURCE_DIR}/opacity_micromap_builder.cpp |
| 48 | + ${CMAKE_CURRENT_SOURCE_DIR}/omm_integration.cpp |
| 49 | + ${CMAKE_CURRENT_SOURCE_DIR}/omm_imgui_panel.cpp |
| 50 | + ) |
| 51 | + # No standalone shader needed: VK_EXT_opacity_micromap is transparent to |
| 52 | + # the existing ray_query.slang shader — the hardware handles everything. |
| 53 | + message(STATUS "[Courses] Opacity Micromaps module ENABLED") |
| 54 | +else() |
| 55 | + message(STATUS "[Courses] Opacity Micromaps module disabled") |
| 56 | +endif() |
| 57 | + |
| 58 | +# ───────────────────────────────────────────────────────────────────────────── |
| 59 | +# Build the course modules library (only if there are sources) |
| 60 | +# ───────────────────────────────────────────────────────────────────────────── |
| 61 | +if(COURSE_SOURCES) |
| 62 | + add_library(SimpleEngineCourses STATIC ${COURSE_SOURCES}) |
| 63 | + |
| 64 | + set_target_properties(SimpleEngineCourses PROPERTIES CXX_STANDARD 20) |
| 65 | + |
| 66 | + if(ENABLE_COURSE_OPACITY_MICROMAPS) |
| 67 | + target_compile_definitions(SimpleEngineCourses PUBLIC ENABLE_COURSE_OPACITY_MICROMAPS) |
| 68 | + endif() |
| 69 | + |
| 70 | + # The course modules include engine headers directly (../renderer.h etc.) |
| 71 | + target_include_directories(SimpleEngineCourses PUBLIC |
| 72 | + ${CMAKE_CURRENT_SOURCE_DIR}/.. # engine root (renderer.h, etc.) |
| 73 | + ) |
| 74 | + |
| 75 | + # Inherit the same compile definitions as the main engine target so that |
| 76 | + # the Vulkan-Hpp dispatch loader and GLM settings are consistent. |
| 77 | + target_compile_definitions(SimpleEngineCourses PRIVATE |
| 78 | + GLM_ENABLE_EXPERIMENTAL |
| 79 | + _USE_MATH_DEFINES |
| 80 | + VULKAN_HPP_NO_STRUCT_CONSTRUCTORS |
| 81 | + VULKAN_HPP_DISPATCH_LOADER_DYNAMIC |
| 82 | + ) |
| 83 | + |
| 84 | + # MSVC portability |
| 85 | + if(MSVC) |
| 86 | + target_compile_definitions(SimpleEngineCourses PRIVATE |
| 87 | + NOMINMAX |
| 88 | + WIN32_LEAN_AND_MEAN |
| 89 | + _CRT_SECURE_NO_WARNINGS |
| 90 | + ) |
| 91 | + target_compile_options(SimpleEngineCourses PRIVATE |
| 92 | + /permissive- |
| 93 | + /Zc:__cplusplus |
| 94 | + /EHsc |
| 95 | + /W3 |
| 96 | + /MP |
| 97 | + ) |
| 98 | + endif() |
| 99 | + |
| 100 | + # Link against Vulkan (same path as the engine). |
| 101 | + if(TARGET Vulkan::cppm) |
| 102 | + target_link_libraries(SimpleEngineCourses PUBLIC Vulkan::cppm) |
| 103 | + else() |
| 104 | + target_link_libraries(SimpleEngineCourses PUBLIC Vulkan::Vulkan) |
| 105 | + endif() |
| 106 | + |
| 107 | + target_link_libraries(SimpleEngineCourses PUBLIC glm::glm) |
| 108 | + |
| 109 | + # Export the library name to the parent scope so CMakeLists.txt can link it. |
| 110 | + set(SIMPLE_ENGINE_COURSES_LIB SimpleEngineCourses PARENT_SCOPE) |
| 111 | + |
| 112 | + # ── Shader compilation (future courses that DO add shaders) ────────────── |
| 113 | + if(COURSE_SLANG_SHADERS AND SLANGC_EXECUTABLE) |
| 114 | + file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/shaders) |
| 115 | + foreach(SHADER ${COURSE_SLANG_SHADERS}) |
| 116 | + get_filename_component(SHADER_NAME ${SHADER} NAME) |
| 117 | + add_custom_command( |
| 118 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_NAME}.spv |
| 119 | + COMMAND ${SLANGC_EXECUTABLE} ${SHADER} |
| 120 | + -target spirv -profile spirv_1_4 -emit-spirv-directly |
| 121 | + -o ${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_NAME}.spv |
| 122 | + DEPENDS ${SHADER} |
| 123 | + COMMENT "Compiling course shader ${SHADER_NAME}" |
| 124 | + ) |
| 125 | + list(APPEND COURSE_SHADER_SPVS |
| 126 | + ${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_NAME}.spv) |
| 127 | + endforeach() |
| 128 | + |
| 129 | + if(COURSE_SHADER_SPVS) |
| 130 | + add_custom_target(CourseShaders DEPENDS ${COURSE_SHADER_SPVS}) |
| 131 | + add_dependencies(SimpleEngineCourses CourseShaders) |
| 132 | + endif() |
| 133 | + endif() |
| 134 | + |
| 135 | +else() |
| 136 | + # No course sources enabled — export an empty library name. |
| 137 | + set(SIMPLE_ENGINE_COURSES_LIB "" PARENT_SCOPE) |
| 138 | + message(STATUS "[Courses] No course modules enabled; skipping library build.") |
| 139 | +endif() |
0 commit comments