blob: 1417f94839fb6dc80a7d1302db2c4aeb43a3416d [file] [log] [blame]
Avi Drissman05dfbc822022-09-13 21:25:341// Copyright 2022 The Chromium Authors
Austin Eng0fa5bb62022-05-11 20:33:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "gpu/command_buffer/service/dawn_instance.h"
6
Loko Kungf9f76372023-05-09 23:21:567#include <dawn/webgpu_cpp.h>
8
Austin Eng0fa5bb62022-05-11 20:33:059#include "base/base_paths.h"
10#include "base/files/file_path.h"
11#include "base/path_service.h"
Corentin Walleza70e84682023-12-18 19:23:4012#include "base/strings/string_split.h"
Austin Eng0fa5bb62022-05-11 20:33:0513#include "build/buildflag.h"
Corentin Walleza70e84682023-12-18 19:23:4014#include "gpu/config/gpu_finch_features.h"
Austin Eng0fa5bb62022-05-11 20:33:0515#include "gpu/config/gpu_preferences.h"
16
17#if BUILDFLAG(IS_MAC)
Avi Drissmand4f07082023-05-12 18:05:4418#include "base/apple/bundle_locations.h"
Avi Drissmaneac566b02023-08-18 02:56:2119#include "base/apple/foundation_util.h"
Austin Eng0fa5bb62022-05-11 20:33:0520#endif
21
22namespace gpu::webgpu {
23
24// static
25std::unique_ptr<DawnInstance> DawnInstance::Create(
26 dawn::platform::Platform* platform,
Corentin Wallez7d6a4f12023-12-05 19:44:1627 const GpuPreferences& gpu_preferences,
Peng Huang55a881da2024-02-22 10:57:0528 SafetyLevel safety,
Lokbondo Kunga1226d82025-01-15 02:13:5929 dawn::native::DawnInstanceDescriptor* dawn_instance_descriptor) {
Corentin Walleza70e84682023-12-18 19:23:4030 // Populate the WGSL blocklist based on the Finch feature.
31 std::vector<std::string> wgsl_unsafe_features_owned;
32 std::vector<const char*> wgsl_unsafe_features;
33
34 if (safety != SafetyLevel::kUnsafe) {
35 wgsl_unsafe_features_owned =
36 base::SplitString(features::kWGSLUnsafeFeatures.Get(), ",",
37 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
38 wgsl_unsafe_features.reserve(wgsl_unsafe_features_owned.size());
39 for (const auto& f : wgsl_unsafe_features_owned) {
40 wgsl_unsafe_features.push_back(f.c_str());
41 }
42 }
43 wgpu::DawnWGSLBlocklist wgsl_blocklist;
44 wgsl_blocklist.nextInChain = nullptr;
45 wgsl_blocklist.blocklistedFeatureCount = wgsl_unsafe_features.size();
46 wgsl_blocklist.blocklistedFeatures = wgsl_unsafe_features.data();
47
48 // Populate the instance toggles becaused on command line parameters and
49 // safety levels. Toggles which are not instance toggles will be ignored by
50 // the instance.
51 std::vector<const char*> require_instance_enabled_toggles;
52 std::vector<const char*> require_instance_disabled_toggles;
53
54 if (safety == SafetyLevel::kSafeExperimental) {
55 require_instance_enabled_toggles.push_back(
56 "expose_wgsl_experimental_features");
57 } else if (safety == SafetyLevel::kUnsafe) {
58 require_instance_enabled_toggles.push_back("allow_unsafe_apis");
59 }
60
Brian Sheedy2084c132024-01-17 23:16:1261 for (const std::string& toggles :
62 gpu_preferences.enabled_dawn_features_list) {
63 require_instance_enabled_toggles.push_back(toggles.c_str());
Corentin Walleza70e84682023-12-18 19:23:4064 }
Brian Sheedy2084c132024-01-17 23:16:1265 for (const std::string& toggles :
Corentin Walleza70e84682023-12-18 19:23:4066 gpu_preferences.disabled_dawn_features_list) {
Brian Sheedy2084c132024-01-17 23:16:1267 require_instance_disabled_toggles.push_back(toggles.c_str());
Corentin Walleza70e84682023-12-18 19:23:4068 }
69
70 wgpu::DawnTogglesDescriptor dawn_toggle_desc;
71 dawn_toggle_desc.nextInChain = &wgsl_blocklist;
72 dawn_toggle_desc.enabledToggleCount = require_instance_enabled_toggles.size();
73 dawn_toggle_desc.enabledToggles = require_instance_enabled_toggles.data();
74 dawn_toggle_desc.disabledToggleCount =
75 require_instance_disabled_toggles.size();
76 dawn_toggle_desc.disabledToggles = require_instance_disabled_toggles.data();
77
78 // Use DawnInstanceDescriptor to pass in the platform and additional search
79 // paths
Austin Eng0fa5bb62022-05-11 20:33:0580 std::string dawn_search_path;
81 base::FilePath module_path;
82#if BUILDFLAG(IS_MAC)
Avi Drissmaneac566b02023-08-18 02:56:2183 if (base::apple::AmIBundled()) {
Avi Drissmand4f07082023-05-12 18:05:4484 dawn_search_path = base::apple::FrameworkBundlePath()
Austin Eng0fa5bb62022-05-11 20:33:0585 .Append("Libraries")
86 .AsEndingWithSeparator()
87 .MaybeAsASCII();
88 }
89 if (dawn_search_path.empty())
90#endif
91 {
Ian Vollick12a72412023-06-20 18:36:1792#if BUILDFLAG(IS_IOS)
93 if (base::PathService::Get(base::DIR_ASSETS, &module_path)) {
94#else
Austin Eng0fa5bb62022-05-11 20:33:0595 if (base::PathService::Get(base::DIR_MODULE, &module_path)) {
Ian Vollick12a72412023-06-20 18:36:1796#endif
Austin Eng0fa5bb62022-05-11 20:33:0597 dawn_search_path = module_path.AsEndingWithSeparator().MaybeAsASCII();
98 }
99 }
100 const char* dawn_search_path_c_str = dawn_search_path.c_str();
101
Jiawei Shao920a1792023-09-01 00:59:32102 dawn::native::DawnInstanceDescriptor dawn_instance_desc;
Lokbondo Kunga1226d82025-01-15 02:13:59103 if (dawn_instance_descriptor) {
104 dawn_instance_desc = *dawn_instance_descriptor;
105 }
Corentin Walleza70e84682023-12-18 19:23:40106 dawn_instance_desc.nextInChain = &dawn_toggle_desc;
Loko Kungf9f76372023-05-09 23:21:56107 dawn_instance_desc.additionalRuntimeSearchPathsCount =
108 dawn_search_path.empty() ? 0u : 1u;
109 dawn_instance_desc.additionalRuntimeSearchPaths = &dawn_search_path_c_str;
Loko Kung4083b352023-05-17 06:26:04110 dawn_instance_desc.platform = platform;
Loko Kungf9f76372023-05-09 23:21:56111
Corentin Walleza70e84682023-12-18 19:23:40112 // Create the instance with all the previous descriptors chained.
Loko Kungf9f76372023-05-09 23:21:56113 wgpu::InstanceDescriptor instance_desc;
114 instance_desc.nextInChain = &dawn_instance_desc;
FranΓ§ois Beaufort4ffd2dc2025-07-10 08:53:12115 static constexpr auto kInstanceFeatures = std::array{
116 wgpu::InstanceFeatureName::MultipleDevicesPerAdapter,
117 wgpu::InstanceFeatureName::TimedWaitAny,
118 };
119 instance_desc.requiredFeatureCount = kInstanceFeatures.size();
120 instance_desc.requiredFeatures = kInstanceFeatures.data();
Brian Sheedy2084c132024-01-17 23:16:12121
122 auto instance = std::make_unique<DawnInstance>(
Loko Kungf9f76372023-05-09 23:21:56123 reinterpret_cast<const WGPUInstanceDescriptor*>(&instance_desc));
Brian Sheedy2084c132024-01-17 23:16:12124
125 switch (gpu_preferences.enable_dawn_backend_validation) {
126 case DawnBackendValidationLevel::kDisabled:
127 break;
128 case DawnBackendValidationLevel::kPartial:
129 instance->SetBackendValidationLevel(
130 dawn::native::BackendValidationLevel::Partial);
131 break;
132 case DawnBackendValidationLevel::kFull:
133 instance->SetBackendValidationLevel(
134 dawn::native::BackendValidationLevel::Full);
135 break;
136 }
137
138 return instance;
Austin Eng0fa5bb62022-05-11 20:33:05139}
140
Loko Kungf9f76372023-05-09 23:21:56141} // namespace gpu::webgpu