| 1 | # Building for desktop 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/example_sdl3_wgpu[.exe] or build/Debug/example_sdl3_wgpu[.exe] |
| 7 | |
| 8 | # Building for desktop 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/example_sdl3_wgpu[.exe] or build/Debug/example_sdl3_wgpu[.exe] |
| 15 | |
| 16 | # Building for desktop with WGVK (MUCH EASIER) |
| 17 | # 1. git clone https://github.com/manuel5975p/WGVK wgvk |
| 18 | # 2. cmake -B build -DIMGUI_WGVK_DIR=wgvk |
| 19 | # 3. cmake --build build |
| 20 | # The resulting binary will be found at one of the following locations: |
| 21 | # * build/example_sdl3_wgpu[.exe] or build/Debug/example_sdl3_wgpu[.exe] |
| 22 | |
| 23 | # Building for Emscripten: |
| 24 | # 1. Install Emscripten SDK following the instructions: https://emscripten.org/docs/getting_started/downloads.html |
| 25 | # 2. Install Ninja build system |
| 26 | # 3. emcmake cmake -G Ninja -B build |
| 27 | # (optional) -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py", see ReadMe.md |
| 28 | # 4. cmake --build build |
| 29 | # 5. emrun build/index.html |
| 30 | |
| 31 | cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22 |
| 32 | project(imgui_example_sdl3_wgpu C CXX) |
| 33 | |
| 34 | set(IMGUI_EXECUTABLE example_sdl3_wgpu) |
| 35 | |
| 36 | if(NOT CMAKE_BUILD_TYPE) |
| 37 | set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE) |
| 38 | endif() |
| 39 | |
| 40 | set(CMAKE_CXX_STANDARD 20) # Dawn requires C++20 |
| 41 | |
| 42 | # Dear ImGui |
| 43 | set(IMGUI_DIR ../../) |
| 44 | |
| 45 | # ImGui example commons source files |
| 46 | set(IMGUI_EXAMPLE_SOURCE_FILES |
| 47 | main.cpp |
| 48 | # backend files |
| 49 | ${IMGUI_DIR}/backends/imgui_impl_sdl3.cpp |
| 50 | ${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp |
| 51 | # Dear ImGui files |
| 52 | ${IMGUI_DIR}/imgui.cpp |
| 53 | ${IMGUI_DIR}/imgui_draw.cpp |
| 54 | ${IMGUI_DIR}/imgui_demo.cpp |
| 55 | ${IMGUI_DIR}/imgui_tables.cpp |
| 56 | ${IMGUI_DIR}/imgui_widgets.cpp |
| 57 | ) |
| 58 | |
| 59 | if(EMSCRIPTEN) |
| 60 | if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.15") |
| 61 | message(FATAL_ERROR "Using Emscripten with SDL3 needs Emscripten version >= 4.0.15") |
| 62 | endif() |
| 63 | # emdawnwebgpu was introduced in 4.0.10 so, due to the prior requirement, this will work |
| 64 | set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "--use-port=emdawnwebgpu" CACHE STRING "Default to --use-port=emdawnwebgpu. You can override to provide your own local port.") |
| 65 | |
| 66 | add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1) |
| 67 | else() # Native/Desktop build |
| 68 | |
| 69 | # Check DAWN/WGPU/WGVK directory |
| 70 | # If it's Native/Desktop build, IMGUI_DAWN_DIR or IMGUI_WGPU_DIR or IMGUI_WGVK_DIR must be specified |
| 71 | if(NOT IMGUI_DAWN_DIR AND NOT IMGUI_WGPU_DIR AND NOT IMGUI_WGVK_DIR) |
| 72 | message(FATAL_ERROR "Please specify one of IMGUI_DAWN_DIR/IMGUI_WGPU_DIR/IMGUI_WGVK_DIR base directory.") |
| 73 | endif() |
| 74 | if((IMGUI_DAWN_DIR AND (IMGUI_WGPU_DIR OR IMGUI_WGVK_DIR)) OR (IMGUI_WGPU_DIR AND IMGUI_WGVK_DIR)) |
| 75 | message(FATAL_ERROR "Please specify only one of IMGUI_DAWN_DIR/IMGUI_WGPU_DIR/IMGUI_WGVK_DIR base directory.") |
| 76 | endif() |
| 77 | |
| 78 | if(APPLE) # Add SDL3 module to get Surface, with libs and file property for MacOS build |
| 79 | set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++") |
| 80 | set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa") |
| 81 | endif() |
| 82 | |
| 83 | find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) |
| 84 | |
| 85 | if(IMGUI_DAWN_DIR) |
| 86 | list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR}) |
| 87 | find_package(Threads) # required from Dawn installation |
| 88 | find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH |
| 89 | if(Dawn_FOUND) |
| 90 | message("Dawn Installation has been found!") |
| 91 | set(LIBRARIES dawn::webgpu_dawn ${OS_LIBRARIES}) |
| 92 | else() |
| 93 | set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository") |
| 94 | |
| 95 | option(DAWN_USE_GLFW OFF) # disable builtin GLFW in DAWN when we use SDL2 / SDL3 |
| 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 ${OS_LIBRARIES}) |
| 128 | endif() |
| 129 | endif() |
| 130 | |
| 131 | if(IMGUI_WGPU_DIR) |
| 132 | set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib) |
| 133 | find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED) |
| 134 | if(WIN32) |
| 135 | set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject) |
| 136 | elseif(UNIX AND NOT APPLE) |
| 137 | set(OS_LIBRARIES "-lm -ldl") |
| 138 | endif() |
| 139 | |
| 140 | set(LIBRARIES ${WGPU_LIBRARY} ${OS_LIBRARIES}) |
| 141 | endif() |
| 142 | |
| 143 | if(IMGUI_WGVK_DIR) |
| 144 | find_package(Vulkan REQUIRED) |
| 145 | set(WGVK_PLATFORM_LIBS) |
| 146 | set(WGVK_PLATFORM_DEFS) |
| 147 | if(WIN32) |
| 148 | list(APPEND WGVK_PLATFORM_DEFS SUPPORT_WIN32_SURFACE=1) |
| 149 | elseif(APPLE) |
| 150 | list(APPEND WGVK_PLATFORM_DEFS SUPPORT_METAL_SURFACE=1) |
| 151 | elseif(UNIX) |
| 152 | find_package(PkgConfig QUIET) |
| 153 | pkg_check_modules(WAYLAND_CLIENT QUIET wayland-client) |
| 154 | if(WAYLAND_CLIENT_FOUND) |
| 155 | list(APPEND WGVK_PLATFORM_DEFS SUPPORT_WAYLAND_SURFACE=1) |
| 156 | list(APPEND WGVK_PLATFORM_LIBS ${WAYLAND_CLIENT_LIBRARIES}) |
| 157 | endif() |
| 158 | find_package(X11 QUIET) |
| 159 | if(X11_FOUND) |
| 160 | list(APPEND WGVK_PLATFORM_DEFS SUPPORT_XLIB_SURFACE=1) |
| 161 | list(APPEND WGVK_PLATFORM_LIBS X11::X11) |
| 162 | endif() |
| 163 | list(APPEND WGVK_PLATFORM_LIBS m dl pthread) |
| 164 | endif() |
| 165 | set(LIBRARIES glfw Vulkan::Vulkan ${WGVK_PLATFORM_LIBS}) |
| 166 | endif() |
| 167 | endif() |
| 168 | |
| 169 | add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES}) |
| 170 | |
| 171 | target_include_directories(${IMGUI_EXECUTABLE} PUBLIC |
| 172 | ${IMGUI_DIR} |
| 173 | ${IMGUI_DIR}/backends |
| 174 | ${SDL3_INCLUDE_DIRS} |
| 175 | ) |
| 176 | |
| 177 | target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_SDL3_WGPU") |
| 178 | |
| 179 | # Enable warning level compiler option only for IMGUI_EXAMPLE_SOURCE_FILES |
| 180 | if (MSVC) |
| 181 | target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4 |
| 182 | else() |
| 183 | target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic |
| 184 | endif() |
| 185 | |
| 186 | # IMGUI_IMPL_WEBGPU_BACKEND_DAWN/WGPU internal define is set according to: |
| 187 | # EMSCRIPTEN: by used FLAG |
| 188 | # --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled (+EMSCRIPTEN) |
| 189 | # NATIVE: by used SDK installation directory |
| 190 | # if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled |
| 191 | # if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled |
| 192 | |
| 193 | if(NOT EMSCRIPTEN) # WegGPU-Native settings |
| 194 | if(IMGUI_DAWN_DIR) |
| 195 | target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN") |
| 196 | if(NOT Dawn_FOUND) |
| 197 | target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp) |
| 198 | endif() |
| 199 | endif() |
| 200 | if(IMGUI_WGPU_DIR) |
| 201 | target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU") |
| 202 | target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include) |
| 203 | endif() |
| 204 | if(IMGUI_WGVK_DIR) |
| 205 | target_sources(${IMGUI_EXECUTABLE} PRIVATE ${IMGUI_WGVK_DIR}/src/wgvk.c) |
| 206 | target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGVK") |
| 207 | target_compile_definitions(${IMGUI_EXECUTABLE} PRIVATE ${WGVK_PLATFORM_DEFS}) |
| 208 | target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGVK_DIR}/include) |
| 209 | if (MSVC) |
| 210 | target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /std:clatest /experimental:c11atomics) |
| 211 | endif() |
| 212 | endif() |
| 213 | |
| 214 | target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES} SDL3::SDL3) |
| 215 | else() # Emscripten settings |
| 216 | set(CMAKE_EXECUTABLE_SUFFIX ".html") |
| 217 | |
| 218 | target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}") |
| 219 | target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN") |
| 220 | message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation") |
| 221 | |
| 222 | target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "-sUSE_SDL=3") |
| 223 | target_link_options(${IMGUI_EXECUTABLE} PRIVATE |
| 224 | "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" |
| 225 | "-sUSE_SDL=3" |
| 226 | "-sWASM=1" |
| 227 | "-sASYNCIFY=1" |
| 228 | "-sALLOW_MEMORY_GROWTH=1" |
| 229 | "-sNO_EXIT_RUNTIME=0" |
| 230 | "-sASSERTIONS=1" |
| 231 | "-sDISABLE_EXCEPTION_CATCHING=1" |
| 232 | # "-sNO_FILESYSTEM=1" |
| 233 | "--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html" |
| 234 | ) |
| 235 | set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index") |
| 236 | endif() |