Skip to content

Commit

Permalink
Merge branch 'webgpu' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
hudrima1 committed Nov 28, 2023
2 parents 824d454 + 9312a80 commit f8f037b
Show file tree
Hide file tree
Showing 7 changed files with 1,414 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ option(SL_BUILD_WAI "Specifies if the WAI library should be built" ON)
option(SL_BUILD_APPS "Specifies if sample apps should be built" ON)
option(SL_BUILD_EXERCISES "Specifies if exercise apps should be built" ON)
option(SL_BUILD_VULKAN_APPS "Specifies if vulkan apps should be built" OFF)
option(SL_BUILD_WEBGPU_DEMO "Specifies if WebGPU demo should be built" ON)
option(SL_BUILD_WITH_OPTIX "Specifies if Optix renderer should be built" OFF)
option(SL_BUILD_WITH_KTX "Specifies if Kronos Texture library (ktx) should be used" ON)
option(SL_BUILD_WITH_OPENSSL "Specifies if OpenSSL should be used" ON)
Expand All @@ -71,6 +72,7 @@ message(STATUS "SL_BUILD_WAI: ${SL_BUILD_WAI}")
message(STATUS "SL_BUILD_APPS: ${SL_BUILD_APPS}")
message(STATUS "SL_BUILD_EXERCISES: ${SL_BUILD_EXERCISES}")
message(STATUS "SL_BUILD_VULKAN_APPS: ${SL_BUILD_VULKAN_APPS}")
message(STATUS "SL_BUILD_WEBGPU_DEMO: ${SL_BUILD_WEBGPU_DEMO}")
message(STATUS "SL_BUILD_WITH_OPTIX: ${SL_BUILD_WITH_OPTIX}")
message(STATUS "SL_BUILD_WITH_KTX: ${SL_BUILD_WITH_KTX}")
message(STATUS "SL_BUILD_WITH_OPENSSL: ${SL_BUILD_WITH_OPENSSL}")
Expand Down
6 changes: 5 additions & 1 deletion apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ else()
add_subdirectory(app_demo_imgui)
add_subdirectory(app_demo_node)
add_subdirectory(app_demo_slproject)
endif()

if (SL_BUILD_WEBGPU_DEMO)
add_subdirectory(webgpu)
endif ()
endif()
70 changes: 70 additions & 0 deletions apps/webgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
if (SYSTEM_NAME_UPPER MATCHES "WINDOWS")
set(WGPU_OS "windows")
elseif (SYSTEM_NAME_UPPER MATCHES "DARWIN")
set(WGPU_OS "macos")
elseif (SYSTEM_NAME_UPPER MATCHES "LINUX")
set(WGPU_OS "linux")
else ()
message(FATAL_ERROR "[WebGPU] System '${SYSTEM_NAME_UPPER}' is not supported")
endif ()

if (CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
set(WGPU_ARCH "x86_64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
set(WGPU_ARCH "arm64")
else ()
message(FATAL_ERROR "[WebGPU] Architecture '${CMAKE_SYSTEM_PROCESSOR}' is not supported")
endif ()

set(WGPU_VERSION "v0.18.0.1")
set(WGPU_DIR "${SL_PROJECT_ROOT}/externals/prebuilt/wgpu_${WGPU_VERSION}")
set(WGPU_ZIP "wgpu-${WGPU_OS}-${WGPU_ARCH}-release.zip")

if (NOT EXISTS "${WGPU_DIR}")
set(BASE_URL "https://github.com/gfx-rs/wgpu-native/releases/download")
set(DOWNLOAD_URL "${BASE_URL}/${WGPU_VERSION}/${WGPU_ZIP}")

message(STATUS "[WebGPU] Downloading ${WGPU_ZIP}...")
file(DOWNLOAD "${DOWNLOAD_URL}" "${WGPU_DIR}/${WGPU_ZIP}")
message(STATUS "[WebGPU] Download complete")

execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf "${WGPU_ZIP}" WORKING_DIRECTORY "${WGPU_DIR}")
message(STATUS "[WebGPU] Files extracted")

file(REMOVE "${WGPU_DIR}/commit-sha" "${WGPU_DIR}/${WGPU_ZIP}")
message(STATUS "[WebGPU] Directory cleaned up")
else ()
message(STATUS "[WebGPU] Directory existing")
endif ()

add_library(wgpu STATIC IMPORTED)

if (SYSTEM_NAME_UPPER MATCHES "WINDOWS")
target_link_libraries(wgpu INTERFACE "ws2_32" "userenv" "bcrypt" "d3dcompiler" "ntdll" "opengl32")
set_target_properties(wgpu PROPERTIES
IMPORTED_LOCATION "${WGPU_DIR}/wgpu_native.lib"
INTERFACE_INCLUDE_DIRECTORIES "${WGPU_DIR}")
elseif (SYSTEM_NAME_UPPER MATCHES "LINUX")
set_target_properties(wgpu PROPERTIES
IMPORTED_LOCATION "${WGPU_DIR}/libwgpu_native.a"
INTERFACE_INCLUDE_DIRECTORIES "${WGPU_DIR}")
elseif (SYSTEM_NAME_UPPER MATCHES "DARWIN")
target_link_libraries(wgpu INTERFACE "-framework CoreFoundation" "-framework QuartzCore" "-framework Metal")
set_target_properties(wgpu PROPERTIES
IMPORTED_LOCATION "${WGPU_DIR}/libwgpu_native.a"
INTERFACE_INCLUDE_DIRECTORIES "${WGPU_DIR}")
endif ()

set(TARGET_SPECIFIC_SOURCES)
if (SYSTEM_NAME_UPPER MATCHES "DARWIN")
set(TARGET_SPECIFIC_SOURCES metal_layer.mm)
endif ()

add_executable(webgpu-demo webgpu_demo.cpp ${TARGET_SPECIFIC_SOURCES})
target_link_libraries(webgpu-demo PRIVATE wgpu sl_cv sl_math ${glfw_LIBS})

if (SYSTEM_NAME_UPPER MATCHES "LINUX")
target_link_libraries(webgpu-demo PRIVATE "X11")
elseif (SYSTEM_NAME_UPPER MATCHES "DARWIN")
target_link_libraries(webgpu-demo PRIVATE "-framework Cocoa" "-framework IOKit")
endif ()
11 changes: 11 additions & 0 deletions apps/webgpu/metal_layer.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <AppKit/AppKit.h>
#include <QuartzCore/CAMetalLayer.h>

extern "C" id createMetalLayer(NSWindow* window)
{
[window.contentView setWantsLayer:YES];
id metalLayer = [CAMetalLayer layer];
[window.contentView setLayer:metalLayer];

return metalLayer;
}
Loading

0 comments on commit f8f037b

Please sign in to comment.