Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
name: gpucpp-ci
name: gpu.cpp

on:
on:
push:
branches:
- main
branches: [main]
pull_request:
types: [opened, reopened, labeled, unlabeled, synchronize]
branches:
- main
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
test:
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: No-op Step
run: echo "This is a no-op action"

- name: Install CMake
run: sudo apt-get install cmake

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libvulkan1 mesa-vulkan-drivers vulkan-tools
sudo apt-get install -y libxrandr-dev
- uses: actions/checkout@v4
- if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y libvulkan1 mesa-vulkan-drivers ninja-build
- run: python3 -m pip install numpy
- run: tools/build_dawn.sh
- run: ./test

- name: Build
run: make all

- name: Run hello world
run: make
browser-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
repository: emscripten-core/emsdk
path: emsdk
fetch-depth: 1
- run: |
sudo apt-get update
sudo apt-get install -y ninja-build
- run: ./test --build-web
env:
GPUCPP_EMSDK_ROOT: ${{ github.workspace }}/emsdk
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
build/*
build-web/
build-dawn/
# any build subdirectory in the tree
**/build/
examples/hello_gpu/build/*
Expand All @@ -8,6 +10,8 @@ source
.DS_Store
third_party/lib/*
third_party/local/*
third_party/dawn/*
third_party/emdawnwebgpu/*

# formatter files
.cmake-format.py
Expand All @@ -19,4 +23,3 @@ build
# clangd files
.cache
compile_commands.json

35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Changelog

## 0.2.0 - 2026-08-21

This release modernizes gpu.cpp around current Dawn and adds a maintained
browser target.

### Added

- A browser backend using Emdawnwebgpu, Emscripten, JSPI, and an ES-module
Embind API for persistent contexts and JavaScript typed arrays.
- Explicit read/read-write bindings, reusable kernels, uniform parameters, and
asynchronous dispatch and readback through `gpu::Future`.
- Native SPIR-V input with an executable WebGPU-profile contract test.
- C++ stories for WGSL, native f16, and SPIR-V; a NumPy Python story; and a
Chrome story covering browser errors, context reuse, dispatch, and readback.
- Reproducible scripts pinning Dawn, Emdawn, emsdk, SPIRV-Tools, and
SPIRV-Headers to exact revisions.

### Changed

- WebGPU objects now use Dawn's generated C++ RAII facade and normal C++ value
semantics.
- The Python binding now follows the C++ API and preserves NumPy shape and
`float16`, `float32`, and `int32` dtypes.
- Host-side f16 uses native `_Float16` where supported; the portable core
treats f16 as two-byte IEEE 754 storage.
- Examples and the build are consolidated under the root CMake project and the
single `./test` entrypoint.

### Removed

- The legacy raw WebGPU C API implementation, manual resource pools, custom
half implementation, Haskell binding, obsolete build files, and abandoned
experimental targets.
174 changes: 139 additions & 35 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,154 @@
cmake_minimum_required(VERSION 3.28)
project(gpu)
project(gpu.cpp LANGUAGES CXX)

include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/webgpu.cmake")

set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with
# LSP
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(EMSCRIPTEN)
set(GPUCPP_BUILD_PYTHON_DEFAULT OFF)
set(GPUCPP_BUILD_EXAMPLES_DEFAULT OFF)
else()
set(GPUCPP_BUILD_PYTHON_DEFAULT ${PROJECT_IS_TOP_LEVEL})
set(GPUCPP_BUILD_EXAMPLES_DEFAULT ON)
endif()
option(GPUCPP_BUILD_PYTHON "Build the Python binding" ${GPUCPP_BUILD_PYTHON_DEFAULT})
option(GPUCPP_BUILD_EXAMPLES "Build gpu.cpp examples" ${GPUCPP_BUILD_EXAMPLES_DEFAULT})

add_library(gpucpp INTERFACE)
target_include_directories(gpucpp INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")

option(USE_LOCAL_LIBS
"Use local libraries instead of fetching from the internet" OFF)
if(EMSCRIPTEN)
set(GPUCPP_EMDAWN_ROOT
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/emdawnwebgpu" CACHE PATH
"Staged Emdawnwebgpu package")
set(EMDAWN_PORT
"--use-port=${GPUCPP_EMDAWN_ROOT}/emdawnwebgpu.port.py")
if(NOT EXISTS "${GPUCPP_EMDAWN_ROOT}/emdawnwebgpu.port.py")
message(FATAL_ERROR "Emdawnwebgpu is not built; run tools/build_emdawn.sh")
endif()
target_compile_options(gpucpp INTERFACE "${EMDAWN_PORT}" "-fwasm-exceptions")
target_link_options(gpucpp INTERFACE "${EMDAWN_PORT}" "-fwasm-exceptions"
"-sJSPI=1" "--closure=1")
if(CMAKE_HOST_APPLE)
target_link_options(gpucpp INTERFACE "--closure-args=--platform=java")
endif()
else()

# Ensure the build type is set
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Choose the type of build: Debug or Release" FORCE)
set(GPUCPP_DAWN_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/third_party/dawn" CACHE PATH
"Staged Dawn distribution")
set(GPUCPP_LOCAL_DAWN "${CMAKE_CURRENT_SOURCE_DIR}/third_party/local/dawn" CACHE PATH
"Developer Dawn source/build tree")

if(EXISTS "${GPUCPP_DAWN_ROOT}/include/dawn/webgpu_cpp.h")
set(DAWN_INCLUDE_DIRS "${GPUCPP_DAWN_ROOT}/include")
if(APPLE)
set(DAWN_LIBRARY "${GPUCPP_DAWN_ROOT}/lib/libwebgpu_dawn.dylib")
elseif(UNIX)
set(DAWN_LIBRARY "${GPUCPP_DAWN_ROOT}/lib/libwebgpu_dawn.so")
endif()
set(SPIRV_AS "${GPUCPP_DAWN_ROOT}/bin/spirv-as")
elseif(EXISTS "${GPUCPP_LOCAL_DAWN}/build-latest/gen/include/dawn/webgpu_cpp.h")
set(DAWN_INCLUDE_DIRS
"${GPUCPP_LOCAL_DAWN}/build-latest/gen/include"
"${GPUCPP_LOCAL_DAWN}/source/include")
if(APPLE)
set(DAWN_LIBRARY
"${GPUCPP_LOCAL_DAWN}/build-latest/src/dawn/native/libwebgpu_dawn.dylib")
elseif(UNIX)
set(DAWN_LIBRARY
"${GPUCPP_LOCAL_DAWN}/build-latest/src/dawn/native/libwebgpu_dawn.so")
endif()
set(SPIRV_AS "${GPUCPP_LOCAL_DAWN}/build-spirv-tools/tools/spirv-as")
else()
message(FATAL_ERROR "Dawn is not built; run tools/build_dawn.sh")
endif()

option(FASTBUILD "Option to enable fast builds" OFF)
if(FASTBUILD)
set(CMAKE_BUILD_TYPE None) # Avoid default flags of predefined build types
set(CMAKE_CXX_FLAGS "-O0")
if(NOT EXISTS "${DAWN_LIBRARY}")
message(FATAL_ERROR "Dawn library not found at ${DAWN_LIBRARY}")
endif()
if(NOT EXISTS "${SPIRV_AS}")
message(FATAL_ERROR "spirv-as not found at ${SPIRV_AS}")
endif()

option(DEBUG "Option to enable debug flags" OFF)
if(DEBUG)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "-O0 -g")
add_library(Dawn::webgpu SHARED IMPORTED)
set_target_properties(Dawn::webgpu PROPERTIES
IMPORTED_LOCATION "${DAWN_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${DAWN_INCLUDE_DIRS}")

target_link_libraries(gpucpp INTERFACE Dawn::webgpu)
endif()

if(WIN64)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWEBGPU_BACKEND_DAWN")
if(GPUCPP_BUILD_PYTHON)
find_package(Python 3.9 COMPONENTS Interpreter Development.Module REQUIRED)
include(FetchContent)
FetchContent_Declare(pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v3.0.4
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(pybind11)

pybind11_add_module(gpu_cpp bindings/python/gpu_cpp.cpp)
target_link_libraries(gpu_cpp PRIVATE gpucpp)
endif()

include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/gpu.cmake")
if(GPUCPP_BUILD_EXAMPLES)
add_executable(hello_gpu examples/hello_world/run.cpp)
add_executable(float16_gpu examples/float16/run.cpp)
add_executable(gpu_puzzles examples/gpu_puzzles/run.cpp)
add_executable(gpu_puzzles_key examples/gpu_puzzles/key.cpp)
add_executable(matmul_gpu examples/matmul/run.cpp)
add_executable(physics_gpu examples/physics/run.cpp)
add_executable(render_gpu examples/render/run.cpp)
add_executable(shadertui_gpu examples/shadertui/run.cpp)
add_executable(transpose_gpu examples/transpose/run.cpp)

message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
message(
STATUS
"Include directories for wgpu: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/headers"
)
foreach(target IN ITEMS hello_gpu float16_gpu gpu_puzzles gpu_puzzles_key
matmul_gpu physics_gpu render_gpu shadertui_gpu
transpose_gpu)
target_link_libraries(${target} PRIVATE gpucpp)
endforeach()
target_include_directories(matmul_gpu PRIVATE third_party/headers)
target_include_directories(transpose_gpu PRIVATE third_party/headers)

add_library(gpud SHARED gpu.hpp)
set_target_properties(gpud PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(gpud PRIVATE wgpu)
target_link_libraries(gpud PRIVATE webgpu)
target_link_libraries(gpud PRIVATE gpu)
install(TARGETS gpud)
endif()

enable_testing()
if(EMSCRIPTEN)
add_library(gpu_cpp_web_bindings OBJECT bindings/web/gpu_cpp.cpp)
target_link_libraries(gpu_cpp_web_bindings PRIVATE gpucpp)

add_executable(gpu_cpp_web $<TARGET_OBJECTS:gpu_cpp_web_bindings>)
set_target_properties(gpu_cpp_web PROPERTIES SUFFIX ".mjs")
target_link_libraries(gpu_cpp_web PRIVATE gpucpp)
target_link_options(gpu_cpp_web PRIVATE "--bind" "--no-entry"
"-sMODULARIZE=1" "-sEXPORT_ES6=1" "-sEXPORT_NAME=createGpuCpp"
"-sENVIRONMENT=web" "-sALLOW_MEMORY_GROWTH=1")

add_executable(web_binding_test tests/web_binding_test.cpp
$<TARGET_OBJECTS:gpu_cpp_web_bindings>)
set_target_properties(web_binding_test PROPERTIES SUFFIX ".html")
target_link_libraries(web_binding_test PRIVATE gpucpp)
target_link_options(web_binding_test PRIVATE "--bind" "--emrun"
"-sMODULARIZE=1" "-sEXPORT_ES6=1"
"-sALLOW_MEMORY_GROWTH=1")
else()
set(TEST_SPIRV "${CMAKE_CURRENT_BINARY_DIR}/write42.spv")
add_custom_command(
OUTPUT "${TEST_SPIRV}"
COMMAND "${SPIRV_AS}" --target-env spv1.3
"${CMAKE_CURRENT_SOURCE_DIR}/tests/write42.spvasm" -o "${TEST_SPIRV}"
DEPENDS tests/write42.spvasm
VERBATIM)

add_executable(gpu_test tests/gpu_test.cpp "${TEST_SPIRV}")
target_link_libraries(gpu_test PRIVATE gpucpp)
add_test(NAME gpu COMMAND gpu_test "${TEST_SPIRV}")
endif()
if(GPUCPP_BUILD_PYTHON)
add_test(NAME python
COMMAND "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/bindings/python/test_gpu_cpp.py")
set_tests_properties(python PROPERTIES
ENVIRONMENT "PYTHONPATH=$<TARGET_FILE_DIR:gpu_cpp>")
endif()
Loading
Loading