CMakeLists.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_sdl2_wgpu[.exe]
  7. # * build/example_sdl2_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_sdl2_wgpu[.exe]
  15. # * build/example_sdl2_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. # 3. cmake --build build
  22. # 4. emrun build/index.html
  23. cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22
  24. project(imgui_example_sdl2_wgpu C CXX)
  25. set(IMGUI_EXECUTABLE example_sdl2_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_sdl2.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(NOT IMGUI_EMSCRIPTEN_WEBGPU_FLAG) # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG not used, set by current EMSCRIPTEN version
  47. if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.10")
  48. 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")
  49. else()
  50. set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "-sUSE_WEBGPU=1" CACHE STRING "Use -sUSE_WEBGPU=1 for EMSCRIPTEN WGPU implementation")
  51. endif()
  52. else() # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG used, check correct version
  53. if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.10" AND "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
  54. # it's necessary EMSCRIPTEN >= v4.0.10 (although "--use-port=path/to/emdawnwebgpu.port.py" is supported/tested from v4.0.8)
  55. message(FATAL_ERROR "emdawnwebgpu needs EMSCRIPTEN version >= 4.0.10")
  56. endif()
  57. endif()
  58. add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
  59. else() # Native/Desktop build
  60. 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
  61. message(FATAL_ERROR "Please specify the Dawn or WGPU base directory")
  62. endif()
  63. if(IMGUI_DAWN_DIR AND IMGUI_WGPU_DIR) # both IMGUI_DAWN_DIR and IMGUI_WGPU_DIR cannot be set
  64. message(FATAL_ERROR "Please specify only one between Dawn / WGPU base directory")
  65. endif()
  66. if(APPLE) # Add SDL2 module to get Surface, with libs and file property for MacOS build
  67. set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
  68. set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa")
  69. endif()
  70. find_package(SDL2 REQUIRED) # SDL_MAIN_HANDLED
  71. if(IMGUI_DAWN_DIR) # DAWN-Native build options
  72. list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR})
  73. find_package(Threads) # required from Dawn installation
  74. find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH
  75. if(Dawn_FOUND)
  76. message("Dawn Installation has been found!")
  77. set(LIBRARIES dawn::webgpu_dawn ${OS_LIBRARIES})
  78. else()
  79. set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
  80. option(DAWN_USE_GLFW OFF) # disable builtin GLFW in DAWN when we use SDL2 / SDL3
  81. option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
  82. set(DAWN_BUILD_MONOLITHIC_LIBRARY "STATIC" CACHE STRING "Build monolithic library: SHARED, STATIC, or OFF.")
  83. # Dawn builds many things by default - disable things we don't need
  84. option(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" OFF)
  85. option(TINT_BUILD_CMD_TOOLS "Build the Tint command line tools" OFF)
  86. option(TINT_BUILD_DOCS "Build documentation" OFF)
  87. option(TINT_BUILD_TESTS "Build tests" OFF)
  88. if(NOT APPLE)
  89. option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
  90. endif()
  91. if(WIN32)
  92. option(DAWN_FORCE_SYSTEM_COMPONENT_LOAD "Allow system component fallback" ON)
  93. option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
  94. option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
  95. option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
  96. option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
  97. option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
  98. option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
  99. endif()
  100. # check if WAYLAND is the current Session Type and enable DAWN_USE_WAYLAND Wayland option @compile time
  101. # You can override this using: cmake -DDAWN_USE_WAYLAND=X (X = ON | OFF)
  102. if(LINUX)
  103. if($ENV{XDG_SESSION_TYPE} MATCHES wayland)
  104. option(DAWN_USE_WAYLAND "Enable support for Wayland surface" ON)
  105. endif()
  106. endif()
  107. add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
  108. set(LIBRARIES webgpu_dawn ${OS_LIBRARIES})
  109. endif()
  110. else() # WGPU-Native build options
  111. set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib)
  112. find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED)
  113. if(WIN32)
  114. set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject)
  115. elseif(UNIX AND NOT APPLE)
  116. set(OS_LIBRARIES "-lm -ldl")
  117. endif()
  118. set(LIBRARIES ${WGPU_LIBRARY} ${OS_LIBRARIES})
  119. endif()
  120. endif()
  121. add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES})
  122. target_include_directories(${IMGUI_EXECUTABLE} PUBLIC
  123. ${IMGUI_DIR}
  124. ${IMGUI_DIR}/backends
  125. ${SDL2_INCLUDE_DIRS}
  126. )
  127. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_SDL2_WGPU")
  128. # compiler option only for IMGUI_EXAMPLE_SOURCE_FILES
  129. if (MSVC)
  130. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4
  131. else()
  132. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic
  133. endif()
  134. # IMGUI_IMPL_WEBGPU_BACKEND_DAWN/WGPU internal define is set according to:
  135. # EMSCRIPTEN: by used FLAG
  136. # --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled (+EMSCRIPTEN)
  137. # -sUSE_WEBGPU=1 --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled (+EMSCRIPTEN)
  138. # NATIVE: by used SDK installation directory
  139. # if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled
  140. # if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled
  141. if(NOT EMSCRIPTEN) # WegGPU-Native settings
  142. if(IMGUI_DAWN_DIR)
  143. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
  144. if(NOT Dawn_FOUND)
  145. target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp)
  146. endif()
  147. else()
  148. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
  149. target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include)
  150. endif()
  151. target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES} ${SDL2_LIBRARIES})
  152. else() # Emscripten settings
  153. set(CMAKE_EXECUTABLE_SUFFIX ".html")
  154. if("${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
  155. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}")
  156. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
  157. else()
  158. target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
  159. endif()
  160. message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation")
  161. target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "-sUSE_SDL=2")
  162. target_link_options(${IMGUI_EXECUTABLE} PRIVATE
  163. "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}"
  164. "-sUSE_SDL=2"
  165. "-sWASM=1"
  166. "-sASYNCIFY=1"
  167. "-sALLOW_MEMORY_GROWTH=1"
  168. "-sNO_EXIT_RUNTIME=0"
  169. "-sASSERTIONS=1"
  170. "-sDISABLE_EXCEPTION_CATCHING=1"
  171. "-sNO_FILESYSTEM=1"
  172. "--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html"
  173. )
  174. set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index")
  175. endif()