blob: 1cce6d8533bc2f6e25846a528b9538bf69c35072 [file] [log] [blame] [view]
Jaswant Panchumarti14d1f6c2024-07-10 02:59:02 +00001# Quickstart With CMake
2
3This document is designed to allow you to build Dawn with CMake and link to it from other CMake projects. If you're planning to work on Dawn specifically, please use `gclient` and `depot_tools`. It ensures
4that you always get the right dependencies and compilers.
5
6## Prerequisites
7
8- A compatible platform (e.g. Windows, macOS, Linux, etc.). Most platforms are fully supported.
9- A compatible C++ compiler supporting at least C++17. Most major compilers are supported.
10- Git for interacting with the Dawn source code repository.
11- CMake for building your project and Dawn. Dawn supports CMake 3.13+.
12
13## Getting the Dawn Code
14
15```shell
16# Change to the directory where you want to create the code repository
17$ git clone https://dawn.googlesource.com/dawn
18Cloning into 'dawn'...
19```
20
21Git will create the repository within a directory named `dawn`. Navigate into this directory and configure the project.
22
23## Build and install Dawn with CMake
24```shell
25$ cd dawn
26$ cmake -S . -B out/Release -DDAWN_FETCH_DEPENDENCIES=ON -DDAWN_ENABLE_INSTALL=ON -DCMAKE_BUILD_TYPE=Release
27...
28-- Configuring done (34.9s)
29-- Generating done (0.8s)
30-- Build files have been written to: ${PWD}/out/Release
31```
32
33- `DAWN_FETCH_DEPENDENCIES=ON` uses a Python script `fetch_dawn_dependencies.py` as a less tedious alternative to setting up depot_tools and fetching dependencies via `gclient`.
34- `DAWN_ENABLE_INSTALL=ON` configures the project to install binaries, headers and cmake configuration files in a directory of your choice. This allows other projects to discover Dawn in CMake and link with it.
35
36Now you can build Dawn:
37
38```shell
39$ cmake --build out/Release
40...
41[100%] Linking CXX static library libdawn_utils.a
42[100%] Built target dawn_utils
43```
44
45Once you have built the Dawn library, install it with `cmake`
46
47```shell
48$ cmake --install out/Release --prefix install/Release
49```
50
51This installs Dawn from the `out/Release` build tree into `install/Release`.
52
53## Linking your code to the Dawn installation
54
55### Create test code
56
57```shell
58$ mkdir TestDawn
59$ cd TestDawn
60```
61
62Now, create a `hello_webgpu.cpp` C++ file within the `TestDawn` directory.
63
64```cpp
Loko Kungc2aeab72024-07-25 20:08:34 +000065#include <webgpu/webgpu_cpp.h>
66
Jaswant Panchumarti14d1f6c2024-07-10 02:59:02 +000067#include <cstdlib>
68#include <iostream>
Jaswant Panchumarti14d1f6c2024-07-10 02:59:02 +000069
70int main(int argc, char *argv[]) {
71 wgpu::InstanceDescriptor instanceDescriptor{};
72 instanceDescriptor.features.timedWaitAnyEnable = true;
73 wgpu::Instance instance = wgpu::CreateInstance(&instanceDescriptor);
74 if (instance == nullptr) {
75 std::cerr << "Instance creation failed!\n";
76 return EXIT_FAILURE;
77 }
78 // Synchronously request the adapter.
79 wgpu::RequestAdapterOptions options = {};
80 wgpu::Adapter adapter;
81 wgpu::RequestAdapterCallbackInfo callbackInfo = {};
82 callbackInfo.nextInChain = nullptr;
83 callbackInfo.mode = wgpu::CallbackMode::WaitAnyOnly;
84 callbackInfo.callback = [](WGPURequestAdapterStatus status,
85 WGPUAdapter adapter, const char *message,
86 void *userdata) {
87 if (status != WGPURequestAdapterStatus_Success) {
88 std::cerr << "Failed to get an adapter:" << message;
89 return;
90 }
91 *static_cast<wgpu::Adapter *>(userdata) = wgpu::Adapter::Acquire(adapter);
92 };
93 callbackInfo.userdata = &adapter;
94 instance.WaitAny(instance.RequestAdapter(&options, callbackInfo), UINT64_MAX);
95 if (adapter == nullptr) {
96 std::cerr << "RequestAdapter failed!\n";
97 return EXIT_FAILURE;
98 }
99
100 wgpu::DawnAdapterPropertiesPowerPreference power_props{};
101
102 wgpu::AdapterInfo info{};
103 info.nextInChain = &power_props;
104
105 adapter.GetInfo(&info);
106 std::cout << "VendorID: " << std::hex << info.vendorID << std::dec << "\n";
107 std::cout << "Vendor: " << info.vendor << "\n";
108 std::cout << "Architecture: " << info.architecture << "\n";
109 std::cout << "DeviceID: " << std::hex << info.deviceID << std::dec << "\n";
110 std::cout << "Name: " << info.device << "\n";
111 std::cout << "Driver description: " << info.description << "\n";
112 return EXIT_SUCCESS;
113}
114```
115
116### Create CMakeLists.txt file
117
118Now, create a `CMakeLists.txt` file within the `TestDawn` directory like the following:
119
120```cmake
121cmake_minimum_required(VERSION 3.13)
122
123project(hello_webgpu)
124
125find_package(Dawn REQUIRED)
126add_executable(hello_webgpu hello_webgpu.cpp)
127
128# Declare dependency on the dawn::webgpu_dawn library
129target_link_libraries(hello_webgpu dawn::webgpu_dawn)
130```
131
132For more information on how to create CMakeLists.txt files, consult the [CMake Tutorial](https://cmake.org/getting-started/).
133
134### Build test project
135
136Configure the CMake build from a fresh binary directory. This configuration is called an “out of source” build and is the preferred method for CMake projects.
137
138```shell
139$ export CMAKE_PREFIX_PATH=/path/to/dawn/install/Release
140$ cmake -S . -B out/Release -DCMAKE_BUILD_TYPE=Release
141...
142-- Configuring done (0.2s)
143-- Generating done (0.0s)
144-- Build files have been written to: ${PWD}/out/Release
145```
146
147Now build our test application:
148
149```shell
150$ cmake --build out/Release
151[ 50%] Building CXX object CMakeFiles/hello_webgpu.dir/hello_webgpu.cpp.o
152[100%] Linking CXX executable hello_webgpu
153[100%] Built target hello_webgpu
154```
155
156Now run your binary:
157
158```shell
159$ ./out/Release/hello_webgpu
160VendorID: 8086
161Vendor: intel
162Architecture: gen-12lp
163DeviceID: 9a60
164Name: Intel(R) UHD Graphics (TGL GT1)
165Driver description: Intel open-source Mesa driver: Mesa 23.2.1-1ubuntu3.1~22.04.2
166```
167
168Note: You may move the contents of `install/Release` to any other directory and `cmake` will still be able to discover dawn as long as `CMAKE_PREFIX_PATH` points to the new install tree.