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