CMakeLists.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_glfw_wgpu[.exe]
  7. # * build/example_glfw_wgpu[.exe]
  8. # Building for Emscripten:
  9. # 1. Install Emscripten SDK following the instructions: https://emscripten.org/docs/getting_started/downloads.html
  10. # 2. Install Ninja build system
  11. # 3. emcmake cmake -G Ninja -B build
  12. # 3. cmake --build build
  13. # 4. emrun build/index.html
  14. cmake_minimum_required(VERSION 3.10.2)
  15. project(imgui_example_glfw_wgpu C CXX)
  16. if(NOT CMAKE_BUILD_TYPE)
  17. set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
  18. endif()
  19. set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
  20. # Dear ImGui
  21. set(IMGUI_DIR ../../)
  22. # Libraries
  23. if(EMSCRIPTEN)
  24. if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "3.1.57")
  25. set(IMGUI_EMSCRIPTEN_GLFW3 "--use-port=contrib.glfw3" CACHE STRING "Choose between --use-port=contrib.glfw3 and -sUSE_GLFW=3 for GLFW implementation (default to --use-port=contrib.glfw3)")
  26. else()
  27. # cannot use contrib.glfw3 prior to 3.1.57
  28. set(IMGUI_EMSCRIPTEN_GLFW3 "-sUSE_GLFW=3" CACHE STRING "Use -sUSE_GLFW=3 for GLFW implementation" FORCE)
  29. endif()
  30. set(LIBRARIES glfw)
  31. add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
  32. else()
  33. # Dawn wgpu desktop
  34. set(DAWN_FETCH_DEPENDENCIES ON)
  35. set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
  36. if (NOT IMGUI_DAWN_DIR)
  37. message(FATAL_ERROR "Please specify the Dawn repository by setting IMGUI_DAWN_DIR")
  38. endif()
  39. option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
  40. # Dawn builds many things by default - disable things we don't need
  41. option(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" OFF)
  42. option(TINT_BUILD_CMD_TOOLS "Build the Tint command line tools" OFF)
  43. option(TINT_BUILD_DOCS "Build documentation" OFF)
  44. option(TINT_BUILD_TESTS "Build tests" OFF)
  45. if (NOT APPLE)
  46. option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
  47. endif()
  48. if(WIN32)
  49. option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
  50. option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
  51. option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
  52. option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
  53. option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" OFF)
  54. option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
  55. endif()
  56. add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
  57. set(LIBRARIES webgpu_dawn webgpu_cpp webgpu_glfw glfw)
  58. endif()
  59. add_executable(example_glfw_wgpu
  60. main.cpp
  61. # backend files
  62. ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
  63. ${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
  64. # Dear ImGui files
  65. ${IMGUI_DIR}/imgui.cpp
  66. ${IMGUI_DIR}/imgui_draw.cpp
  67. ${IMGUI_DIR}/imgui_demo.cpp
  68. ${IMGUI_DIR}/imgui_tables.cpp
  69. ${IMGUI_DIR}/imgui_widgets.cpp
  70. )
  71. IF(NOT EMSCRIPTEN)
  72. target_compile_definitions(example_glfw_wgpu PUBLIC
  73. "IMGUI_IMPL_WEBGPU_BACKEND_DAWN"
  74. )
  75. endif()
  76. target_include_directories(example_glfw_wgpu PUBLIC
  77. ${IMGUI_DIR}
  78. ${IMGUI_DIR}/backends
  79. )
  80. target_link_libraries(example_glfw_wgpu PUBLIC ${LIBRARIES})
  81. # Emscripten settings
  82. if(EMSCRIPTEN)
  83. if("${IMGUI_EMSCRIPTEN_GLFW3}" STREQUAL "--use-port=contrib.glfw3")
  84. target_compile_options(example_glfw_wgpu PUBLIC
  85. "${IMGUI_EMSCRIPTEN_GLFW3}"
  86. )
  87. endif()
  88. message(STATUS "Using ${IMGUI_EMSCRIPTEN_GLFW3} GLFW implementation")
  89. target_link_options(example_glfw_wgpu PRIVATE
  90. "-sUSE_WEBGPU=1"
  91. "${IMGUI_EMSCRIPTEN_GLFW3}"
  92. "-sWASM=1"
  93. "-sALLOW_MEMORY_GROWTH=1"
  94. "-sNO_EXIT_RUNTIME=0"
  95. "-sASSERTIONS=1"
  96. "-sDISABLE_EXCEPTION_CATCHING=1"
  97. "-sNO_FILESYSTEM=1"
  98. )
  99. set_target_properties(example_glfw_wgpu PROPERTIES OUTPUT_NAME "index")
  100. # copy our custom index.html to build directory
  101. add_custom_command(TARGET example_glfw_wgpu POST_BUILD
  102. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_LIST_DIR}/web/index.html" $<TARGET_FILE_DIR:example_glfw_wgpu>
  103. )
  104. endif()