CMakeLists.txt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Building for desktop (WebGPU-native) with Dawn:
  2. # 1. git clone https://github.com/google/dawn dawn
  3. # 2. cmake -B build -DIMGUI_DAWN_DIR=dawn
  4. # 3. cmake --build build
  5. # The resulting binary will be found at one of the following locations:
  6. # * build/Debug/example_sdl3_wgpu[.exe]
  7. # * build/example_sdl3_wgpu[.exe]
  8. # Building for desktop (WGPU-Native) with WGPU-Native:
  9. # 1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
  10. # 2. unzip the downloaded file in your_preferred_folder
  11. # 3. cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder ("full path" or "relative" starting from current directory)
  12. # 4. cmake --build build
  13. # The resulting binary will be found at one of the following locations:
  14. # * build/Debug/example_sdl3_wgpu[.exe]
  15. # * build/example_sdl3_wgpu[.exe]
  16. # Building for Emscripten:
  17. # 1. Install Emscripten SDK following the instructions: https://emscripten.org/docs/getting_started/downloads.html
  18. # 2. Install Ninja build system
  19. # 3. emcmake cmake -G Ninja -B build
  20. # (optional) -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py", see ReadMe.md
  21. # 4. cmake --build build
  22. # 5. emrun build/index.html
  23. cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22
  24. project(imgui_example_sdl3_wgpu C CXX)
  25. set(IMGUI_EXECUTABLE example_sdl3_wgpu)
  26. if(NOT CMAKE_BUILD_TYPE)
  27. set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
  28. endif()
  29. set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
  30. # Dear ImGui
  31. set(IMGUI_DIR ../../)
  32. # ImGui example commons source files
  33. set(IMGUI_EXAMPLE_SOURCE_FILES
  34. main.cpp
  35. # backend files
  36. ${IMGUI_DIR}/backends/imgui_impl_sdl3.cpp
  37. ${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
  38. # Dear ImGui files
  39. ${IMGUI_DIR}/imgui.cpp
  40. ${IMGUI_DIR}/imgui_draw.cpp
  41. ${IMGUI_DIR}/imgui_demo.cpp
  42. ${IMGUI_DIR}/imgui_tables.cpp
  43. ${IMGUI_DIR}/imgui_widgets.cpp
  44. )
  45. if(EMSCRIPTEN)
  46. if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.15")
  47. message(FATAL_ERROR "Using Emscripten with SDL3 needs Emscripten version >= 4.0.15")
  48. endif()
  49. if(NOT IMGUI_EMSCRIPTEN_WEBGPU_FLAG) # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG not used, set by current EMSCRIPTEN version
  50. if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.10")
  51. set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "--use-port=emdawnwebgpu" CACHE STRING "Choose between --use-port=emdawnwebgpu (Dawn implementation of EMSCRIPTEN) and -sUSE_WEBGPU=1 (WGPU implementation of EMSCRIPTEN, deprecated in 4.0.10): default to --use-port=emdawnwebgpu for EMSCRIPTEN >= 4.0.10")
  52. else()
  53. set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "-sUSE_WEBGPU=1" CACHE STRING "Use -sUSE_WEBGPU=1 for EMSCRIPTEN WGPU implementation")
  54. endif()
  55. else() # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG used, check correct version
  56. if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.10" AND "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
  57. # it's necessary EMSCRIPTEN >= v4.0.10 (although "--use-port=path/to/emdawnwebgpu.port.py" is supported/tested from v4.0.8)
  58. message(FATAL_ERROR "emdawnwebgpu needs EMSCRIPTEN version >= 4.0.10")
  59. endif()
  60. endif()
  61. add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
  62. else() # Native/Desktop build
  63. if(NOT IMGUI_DAWN_DIR AND NOT IMGUI_WGPU_DIR) # if it's Native/Desktop build, IMGUI_DAWN_DIR or IMGUI_WGPU_DIR must be specified
  64. message(FATAL_ERROR "Please specify the Dawn or WGPU base directory")
  65. endif()
  66. if(IMGUI_DAWN_DIR AND IMGUI_WGPU_DIR) # both IMGUI_DAWN_DIR and IMGUI_WGPU_DIR cannot be set
  67. message(FATAL_ERROR "Please specify only one between Dawn / WGPU base directory")
  68. endif()
  69. if(APPLE) # Add SDL3 module to get Surface, with libs and file property for MacOS build
  70. set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
  71. set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa")
  72. endif()
  73. find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
  74. if(IMGUI_DAWN_DIR) # DAWN-Native build options
  75. list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR})
  76. find_package(Threads) # required from Dawn installation
  77. find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH
  78. if(Dawn_FOUND)
  79. message("Dawn Installation has been found!")
  80. set(LIBRARIES dawn::webgpu_dawn ${OS_LIBRARIES})
  81. else()
  82. set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
  83. option(DAWN_USE_GLFW OFF) # disable builtin GLFW in DAWN when we use SDL2 / SDL3
  84. option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
  85. set(DAWN_BUILD_MONOLITHIC_LIBRARY "STATIC" CACHE STRING "Build monolithic library: SHARED, STATIC, or OFF.")
  86. # Dawn builds many things by default - disable things we don't need
  87. option(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" OFF)
  88. option(TINT_BUILD_CMD_TOOLS "Build the Tint command line tools" OFF)
  89. option(TINT_BUILD_DOCS "Build documentation" OFF)
  90. option(TINT_BUILD_TESTS "Build tests" OFF)
  91. if(NOT APPLE)
  92. option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
  93. endif()
  94. if(WIN32)
  95. option(DAWN_FORCE_SYSTEM_COMPONENT_LOAD "Allow system component fallback" ON)
  96. option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
  97. option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
  98. option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
  99. option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
  100. option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
  101. option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
  102. endif()
  103. # check if WAYLAND is the current Session Type and enable DAWN_USE_WAYLAND Wayland option @compile time
  104. # You can override this using: cmake -DDAWN_USE_WAYLAND=X (X = ON | OFF)
  105. if(LINUX)
  106. if($ENV{XDG_SESSION_TYPE} MATCHES wayland)
  107. option(DAWN_USE_WAYLAND "Enable support for Wayland surface" ON)
  108. endif()
  109. endif()
  110. add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
  111. set(LIBRARIES webgpu_dawn ${OS_LIBRARIES})
  112. endif()
  113. else() # WGPU-Native build options
  114. set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib)
  115. find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED)
  116. if(WIN32)
  117. set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject)
  118. elseif(UNIX AND NOT APPLE)
  119. set(OS_LIBRARIES "-lm -ldl")
  120. endif()
  121. set(LIBRARIES ${WGPU_LIBRARY} ${OS_LIBRARIES})
  122. endif()
  123. endif()
  124. add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES})
  125. target_include_directories(${IMGUI_EXECUTABLE} PUBLIC
  126. ${IMGUI_DIR}
  127. ${IMGUI_DIR}/backends
  128. ${SDL3_INCLUDE_DIRS}
  129. )
  130. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_SDL3_WGPU")
  131. # Enable warning level compiler option only for IMGUI_EXAMPLE_SOURCE_FILES
  132. if (MSVC)
  133. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4
  134. else()
  135. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic
  136. endif()
  137. # IMGUI_IMPL_WEBGPU_BACKEND_DAWN/WGPU internal define is set according to:
  138. # EMSCRIPTEN: by used FLAG
  139. # --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled (+EMSCRIPTEN)
  140. # -sUSE_WEBGPU=1 --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled (+EMSCRIPTEN)
  141. # NATIVE: by used SDK installation directory
  142. # if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled
  143. # if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled
  144. if(NOT EMSCRIPTEN) # WegGPU-Native settings
  145. if(IMGUI_DAWN_DIR)
  146. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
  147. if(NOT Dawn_FOUND)
  148. target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp)
  149. endif()
  150. else()
  151. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
  152. target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include)
  153. endif()
  154. target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES} SDL3::SDL3)
  155. else() # Emscripten settings
  156. set(CMAKE_EXECUTABLE_SUFFIX ".html")
  157. if("${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
  158. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}")
  159. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
  160. else()
  161. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
  162. endif()
  163. message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation")
  164. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "-sUSE_SDL=3")
  165. target_link_options(${IMGUI_EXECUTABLE} PRIVATE
  166. "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}"
  167. "-sUSE_SDL=3"
  168. "-sWASM=1"
  169. "-sASYNCIFY=1"
  170. "-sALLOW_MEMORY_GROWTH=1"
  171. "-sNO_EXIT_RUNTIME=0"
  172. "-sASSERTIONS=1"
  173. "-sDISABLE_EXCEPTION_CATCHING=1"
  174. # "-sNO_FILESYSTEM=1"
  175. "--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html"
  176. )
  177. set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index")
  178. endif()