Skip to content

Commit 807f185

Browse files
committed
WebGPU: backend components created and conditionally logged
Initial groundwork in creating WebGPU backend components, namely the instance, adapter, device, and queue. If configured to do so, the backend will print out details about these components. The samples/hellotriangle.cpp was slightly modified to include a webgpu option which allows for exercising the above, but does not yet draw anything to the screen/window. NOTE: This has only been sanity tested with hello triangle on Mac OS and the Android emulator at this time, NOT IOS, Windows, or Linux yet.
1 parent e12ed90 commit 807f185

13 files changed

+1069
-109
lines changed

filament/backend/CMakeLists.txt

+24-2
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,29 @@ endif()
242242

243243
if (FILAMENT_SUPPORTS_WEBGPU)
244244
list(APPEND SRCS
245+
include/backend/platforms/WebGPUPlatform.h
246+
src/webgpu/platform/WebGPUPlatform.cpp
247+
src/webgpu/WebGPUConstants.h
245248
src/webgpu/WebGPUDriver.cpp
246249
src/webgpu/WebGPUDriver.h
247-
src/webgpu/WebGPUPlatform.cpp
248-
src/webgpu/WebGPUPlatform.h
249250
)
251+
if (LINUX OR WIN32)
252+
list(APPEND SRCS src/webgpu/platform/WebGPUPlatformLinuxWindows.cpp)
253+
elseif (APPLE OR IOS)
254+
list(APPEND SRCS src/webgpu/platform/WebGPUPlatformApple.mm)
255+
elseif (ANDROID)
256+
list(APPEND SRCS src/webgpu/platform/WebGPUPlatformAndroid.cpp)
257+
endif()
258+
259+
if (TNT_DEV)
260+
set(FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING_DEFAULT ON)
261+
else()
262+
set(FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING_DEFAULT OFF)
263+
endif()
264+
265+
option(FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING
266+
"Enable immediate_error_handling for the WebGPU backend Dawn implementation"
267+
${FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING_DEFAULT})
250268
endif()
251269

252270
if (ANDROID)
@@ -433,6 +451,10 @@ if (FILAMENT_SUPPORTS_METAL)
433451
target_compile_definitions(${TARGET} PRIVATE $<$<BOOL:${FILAMENT_METAL_PROFILING}>:FILAMENT_METAL_PROFILING>)
434452
endif()
435453

454+
if (FILAMENT_SUPPORTS_WEBGPU)
455+
target_compile_definitions(${TARGET} PRIVATE $<$<BOOL:${FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING}>:FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING>)
456+
endif()
457+
436458
target_link_libraries(${TARGET} PRIVATE
437459
${OSMESA_LINKER_FLAGS}
438460
$<$<AND:$<PLATFORM_ID:Linux>,$<CONFIG:Release>>:${LINUX_LINKER_OPTIMIZATION_FLAGS}>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TNT_FILAMENT_BACKEND_PLATFORMS_WEBGPUPLATFORM_H
18+
#define TNT_FILAMENT_BACKEND_PLATFORMS_WEBGPUPLATFORM_H
19+
20+
#include <cstdint>
21+
22+
#include <webgpu/webgpu_cpp.h>
23+
24+
#include "backend/Platform.h"
25+
26+
namespace filament::backend {
27+
28+
/**
29+
* A Platform interface, handling the environment-specific concerns, e.g. OS, for creating a WebGPU
30+
* driver (backend).
31+
*/
32+
class WebGPUPlatform final : public Platform {
33+
public:
34+
35+
struct SurfaceBundle final {
36+
wgpu::Surface surface = nullptr;
37+
// may or may not be defined based on the platform
38+
wgpu::Extent2D fallbackExtent = {.width = 0, .height = 0};
39+
};
40+
41+
WebGPUPlatform();
42+
~WebGPUPlatform() override = default;
43+
44+
[[nodiscard]] int getOSVersion() const noexcept final { return 0; }
45+
46+
[[nodiscard]] wgpu::Instance& getInstance() noexcept { return mInstance; }
47+
48+
//either returns a valid surface or panics
49+
[[nodiscard]] SurfaceBundle createSurface(void* nativeWindow, uint64_t flags);
50+
//either returns a valid adapter or panics
51+
[[nodiscard]] wgpu::Adapter requestAdapter(wgpu::Surface const& surface);
52+
//either returns a valid device or panics
53+
[[nodiscard]] wgpu::Device requestDevice(wgpu::Adapter const& adapter);
54+
55+
protected:
56+
[[nodiscard]] Driver* createDriver(void* sharedContext,
57+
const Platform::DriverConfig& driverConfig) noexcept override;
58+
59+
private:
60+
wgpu::Instance mInstance; // we may consider having the driver own this in the future
61+
};
62+
63+
}// namespace filament::backend
64+
65+
#endif// TNT_FILAMENT_BACKEND_PLATFORMS_WEBGPUPLATFORM_H

filament/backend/src/PlatformFactory.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ filament::backend::Platform* createDefaultMetalPlatform();
6666

6767
#include "noop/PlatformNoop.h"
6868
#if defined(FILAMENT_SUPPORTS_WEBGPU)
69-
#include "webgpu/WebGPUPlatform.h"
69+
#include "backend/platforms/WebGPUPlatform.h"
7070
#endif
7171

7272
namespace filament::backend {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TNT_FILAMENT_BACKEND_WEBGPUCONSTANTS_H
18+
#define TNT_FILAMENT_BACKEND_WEBGPUCONSTANTS_H
19+
20+
#include <cstdint>
21+
22+
#include "utils/Log.h"
23+
24+
// FWGPU is short for Filament WebGPU
25+
26+
// turn on runtime validation, namely for debugging, that would normally not run (for release)
27+
#define FWGPU_DEBUG_VALIDATION 0x00000001
28+
// print/log system component details to the console, e.g. about the
29+
// instance, surface, adapter, device, etc.
30+
#define FWGPU_PRINT_SYSTEM 0x00000002
31+
32+
// Set this to enable logging "only" to one output stream. This is useful in the case where we want
33+
// to debug with print statements and want ordered logging (e.g slog.i and slog.e will not appear in
34+
// order of calls).
35+
#define FWGPU_DEBUG_FORCE_LOG_TO_I 0x00000004
36+
37+
// Useful default combinations
38+
#define FWGPU_DEBUG_EVERYTHING 0xFFFFFFFF
39+
40+
#if defined(FILAMENT_BACKEND_DEBUG_FLAG)
41+
#define FWGPU_DEBUG_FORWARDED_FLAG (FILAMENT_BACKEND_DEBUG_FLAG & FWGPU_DEBUG_EVERYTHING)
42+
#else
43+
#define FWGPU_DEBUG_FORWARDED_FLAG 0
44+
#endif
45+
46+
#ifndef NDEBUG
47+
#define FWGPU_DEBUG_FLAGS FWGPU_DEBUG_FORWARDED_FLAG
48+
#else
49+
#define FWGPU_DEBUG_FLAGS 0
50+
#endif
51+
52+
#define FWGPU_ENABLED(flags) (((FWGPU_DEBUG_FLAGS) & (flags)) == (flags))
53+
54+
#if FWGPU_ENABLED(FWGPU_DEBUG_FORCE_LOG_TO_I)
55+
#define FWGPU_LOGI (utils::slog.i)
56+
#define FWGPU_LOGD FWGPU_LOGI
57+
#define FWGPU_LOGE FWGPU_LOGI
58+
#define FWGPU_LOGW FWGPU_LOGI
59+
#else
60+
#define FWGPU_LOGE (utils::slog.e)
61+
#define FWGPU_LOGW (utils::slog.w)
62+
#define FWGPU_LOGD (utils::slog.d)
63+
#define FWGPU_LOGI (utils::slog.i)
64+
#endif
65+
66+
#endif// TNT_FILAMENT_BACKEND_WEBGPUCONSTANTS_H

0 commit comments

Comments
 (0)