Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webgpu: backend components created and conditionally logged #8507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions filament/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,29 @@ endif()

if (FILAMENT_SUPPORTS_WEBGPU)
list(APPEND SRCS
include/backend/platforms/WebGPUPlatform.h
src/webgpu/platform/WebGPUPlatform.cpp
src/webgpu/WebGPUConstants.h
src/webgpu/WebGPUDriver.cpp
src/webgpu/WebGPUDriver.h
src/webgpu/WebGPUPlatform.cpp
src/webgpu/WebGPUPlatform.h
)
if (LINUX OR WIN32)
list(APPEND SRCS src/webgpu/platform/WebGPUPlatformLinuxWindows.cpp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better to split linux and windows into two.

Sorry for the counter-example in the vulkan backend. I kind of regret combining the two.

Copy link
Author

@AndyHovingh AndyHovingh Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's completely fine with me! I agree that would be cleaner! Note, we are not testing this at that time (Windows, Linux, and IOS scenarios); we are only testing Mac OS and Android (emulator in Android Studio). But, we think that is fine, as these become placeholders when testing those become important; at that time, we can fix them if needed.

elseif (APPLE OR IOS)
list(APPEND SRCS src/webgpu/platform/WebGPUPlatformApple.mm)
elseif (ANDROID)
list(APPEND SRCS src/webgpu/platform/WebGPUPlatformAndroid.cpp)
endif()

if (TNT_DEV)
set(FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING_DEFAULT ON)
else()
set(FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING_DEFAULT OFF)
endif()

option(FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING
"Enable immediate_error_handling for the WebGPU backend Dawn implementation"
${FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING_DEFAULT})
endif()

if (ANDROID)
Expand Down Expand Up @@ -433,6 +451,10 @@ if (FILAMENT_SUPPORTS_METAL)
target_compile_definitions(${TARGET} PRIVATE $<$<BOOL:${FILAMENT_METAL_PROFILING}>:FILAMENT_METAL_PROFILING>)
endif()

if (FILAMENT_SUPPORTS_WEBGPU)
target_compile_definitions(${TARGET} PRIVATE $<$<BOOL:${FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING}>:FILAMENT_WEBGPU_IMMEDIATE_ERROR_HANDLING>)
endif()

target_link_libraries(${TARGET} PRIVATE
${OSMESA_LINKER_FLAGS}
$<$<AND:$<PLATFORM_ID:Linux>,$<CONFIG:Release>>:${LINUX_LINKER_OPTIMIZATION_FLAGS}>
Expand Down
65 changes: 65 additions & 0 deletions filament/backend/include/backend/platforms/WebGPUPlatform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TNT_FILAMENT_BACKEND_PLATFORMS_WEBGPUPLATFORM_H
#define TNT_FILAMENT_BACKEND_PLATFORMS_WEBGPUPLATFORM_H

#include <cstdint>

#include <webgpu/webgpu_cpp.h>

#include "backend/Platform.h"

namespace filament::backend {

/**
* A Platform interface, handling the environment-specific concerns, e.g. OS, for creating a WebGPU
* driver (backend).
*/
class WebGPUPlatform final : public Platform {
public:

struct SurfaceBundle final {
wgpu::Surface surface = nullptr;
// may or may not be defined based on the platform
wgpu::Extent2D fallbackExtent = {.width = 0, .height = 0};
};

WebGPUPlatform();
~WebGPUPlatform() override = default;

[[nodiscard]] int getOSVersion() const noexcept final { return 0; }

[[nodiscard]] wgpu::Instance& getInstance() noexcept { return mInstance; }

//either returns a valid surface or panics
[[nodiscard]] SurfaceBundle createSurface(void* nativeWindow, uint64_t flags);
//either returns a valid adapter or panics
[[nodiscard]] wgpu::Adapter requestAdapter(wgpu::Surface const& surface);
//either returns a valid device or panics
[[nodiscard]] wgpu::Device requestDevice(wgpu::Adapter const& adapter);

protected:
[[nodiscard]] Driver* createDriver(void* sharedContext,
const Platform::DriverConfig& driverConfig) noexcept override;

private:
wgpu::Instance mInstance; // we may consider having the driver own this in the future
};

}// namespace filament::backend

#endif// TNT_FILAMENT_BACKEND_PLATFORMS_WEBGPUPLATFORM_H
2 changes: 1 addition & 1 deletion filament/backend/src/PlatformFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ filament::backend::Platform* createDefaultMetalPlatform();

#include "noop/PlatformNoop.h"
#if defined(FILAMENT_SUPPORTS_WEBGPU)
#include "webgpu/WebGPUPlatform.h"
#include "backend/platforms/WebGPUPlatform.h"
#endif

namespace filament::backend {
Expand Down
66 changes: 66 additions & 0 deletions filament/backend/src/webgpu/WebGPUConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TNT_FILAMENT_BACKEND_WEBGPUCONSTANTS_H
#define TNT_FILAMENT_BACKEND_WEBGPUCONSTANTS_H

#include <cstdint>

#include "utils/Log.h"

// FWGPU is short for Filament WebGPU

// turn on runtime validation, namely for debugging, that would normally not run (for release)
#define FWGPU_DEBUG_VALIDATION 0x00000001
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this seems based on the vulkan backend it seems. Not a critique, but just kind of a FYI -->

Each of the backend has, historically, been owned by a single individual, and each of them has its own style because of that. I put together these constant things because I kind of preferred it this way. But no one says that the webgpu should be designed/implemented this way. In short, you have some freedom here on how you'd like to implement the backend. You can also look at metal and gl for some inspirations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@poweifeng understood, and that reflects my impression as well. I felt like it was a good starting point, pretty flexible. As the code evolves and alternative implementation(s) become more appropriate we can pivot accordingly. Thank you for call-out on freedom!

So, far a lot of this code was mostly inspired by the Vulkan approach (in part because I am more familiar with Vulkan than others), but also the abstractions seem fairly clear. I am excited to see how it evolves as we build this out and uncover challenges to it.

// print/log system component details to the console, e.g. about the
// instance, surface, adapter, device, etc.
#define FWGPU_PRINT_SYSTEM 0x00000002

// Set this to enable logging "only" to one output stream. This is useful in the case where we want
// to debug with print statements and want ordered logging (e.g slog.i and slog.e will not appear in
// order of calls).
#define FWGPU_DEBUG_FORCE_LOG_TO_I 0x00000004

// Useful default combinations
#define FWGPU_DEBUG_EVERYTHING 0xFFFFFFFF

#if defined(FILAMENT_BACKEND_DEBUG_FLAG)
#define FWGPU_DEBUG_FORWARDED_FLAG (FILAMENT_BACKEND_DEBUG_FLAG & FWGPU_DEBUG_EVERYTHING)
#else
#define FWGPU_DEBUG_FORWARDED_FLAG 0
#endif

#ifndef NDEBUG
#define FWGPU_DEBUG_FLAGS FWGPU_DEBUG_FORWARDED_FLAG
#else
#define FWGPU_DEBUG_FLAGS 0
#endif

#define FWGPU_ENABLED(flags) (((FWGPU_DEBUG_FLAGS) & (flags)) == (flags))

#if FWGPU_ENABLED(FWGPU_DEBUG_FORCE_LOG_TO_I)
#define FWGPU_LOGI (utils::slog.i)
#define FWGPU_LOGD FWGPU_LOGI
#define FWGPU_LOGE FWGPU_LOGI
#define FWGPU_LOGW FWGPU_LOGI
#else
#define FWGPU_LOGE (utils::slog.e)
#define FWGPU_LOGW (utils::slog.w)
#define FWGPU_LOGD (utils::slog.d)
#define FWGPU_LOGI (utils::slog.i)
#endif

#endif// TNT_FILAMENT_BACKEND_WEBGPUCONSTANTS_H
Loading