Bug 1808602: Implement 'dom.webgpu.wgpu-backend' pref. r=jrmuizel

Add a new string-valued, Rust-visible pref, `dom.webgpu.wgpu-backend`, to `StaticPrefList.yaml` in `modules/libpref/init`. If this string is non-empty, use it to enable the indicated WebGPU backends when we create a `wgpu_core::Global` in `gfx/wgpu_bindings/src/server.rs`. See `StaticPrefList.yaml` for details.

As this is the first time we have exposed a `mirror: always`, `type: DataMutexString` pref to Rust, make the appropriate changes to `generate_static_pref_list.py` to generate the necessary C++ and Rust code to lock the value, make a string copy, and pass ownership back to Rust as an `nsstring::nsACString` (from the `xpcom/rust/nsstring` crate).

Differential Revision: https://phabricator.services.mozilla.com/D173335
This commit is contained in:
Jim Blandy 2023-03-23 20:14:59 +00:00
Родитель de05a66666
Коммит 46ff1ae846
7 изменённых файлов: 74 добавлений и 17 удалений

2
Cargo.lock сгенерированный
Просмотреть файл

@ -5102,6 +5102,7 @@ name = "static_prefs"
version = "0.1.0"
dependencies = [
"mozbuild",
"nsstring",
]
[[package]]
@ -6466,6 +6467,7 @@ dependencies = [
"nsstring",
"parking_lot 0.11.2",
"serde",
"static_prefs",
"wgpu-core",
"wgpu-hal",
"wgpu-types",

Просмотреть файл

@ -272,7 +272,6 @@ ipc::IPCResult WebGPUParent::RecvInstanceRequestAdapter(
aOptions.mPowerPreference.Value());
}
options.force_fallback_adapter = aOptions.mForceFallbackAdapter;
// TODO: make available backends configurable by prefs
ErrorBuffer error;
int8_t index = ffi::wgpu_server_instance_request_adapter(

Просмотреть файл

@ -60,3 +60,4 @@ log = "0.4"
parking_lot = "0.11"
serde = "1"
nsstring = { path = "../../xpcom/rust/nsstring" }
static_prefs = { path = "../../modules/libpref/init/static_prefs" }

Просмотреть файл

@ -100,11 +100,21 @@ impl std::ops::Deref for Global {
#[no_mangle]
pub extern "C" fn wgpu_server_new(factory: IdentityRecyclerFactory) -> *mut Global {
log::info!("Initializing WGPU server");
let backends_pref = static_prefs::pref!("dom.webgpu.wgpu-backend").to_string();
let backends = if backends_pref.is_empty() {
wgt::Backends::PRIMARY
} else {
log::info!(
"Selecting backends based on dom.webgpu.wgpu-backend pref: {:?}",
backends_pref
);
wgc::instance::parse_backends_from_comma_list(&backends_pref)
};
let global = Global(wgc::hub::Global::new(
"wgpu",
factory,
wgt::InstanceDescriptor {
backends: wgt::Backends::PRIMARY,
backends,
dx12_shader_compiler: wgt::Dx12Compiler::Fxc,
},
));

Просмотреть файл

@ -4610,6 +4610,24 @@
value: false
mirror: always
# Comma-separated list of wgpu backend names to permit in WebGPU adapters.
#
# If non-empty, this is parsed by `wgpu_core::instance::parse_backends_from_comma_list` to
# produce a `wgpu_types::Backends` bitset used to create a `wgpu_core::hub::Global`. As of
# 2023-3-22, recognized names are:
#
# "vulkan" | "vk" => Backends::VULKAN,
# "dx12" | "d3d12" => Backends::DX12,
# "dx11" | "d3d11" => Backends::DX11,
# "metal" | "mtl" => Backends::METAL,
# "opengl" | "gles" | "gl" => Backends::GL,
# "webgpu" => Backends::BROWSER_WEBGPU,
- name: dom.webgpu.wgpu-backend
type: DataMutexString
value: ""
mirror: always
rust: true
# Is support for HTMLInputElement.webkitEntries enabled?
- name: dom.webkitBlink.filesystem.enabled
type: bool

Просмотреть файл

@ -46,7 +46,7 @@ VALID_TYPES.update(
"SequentiallyConsistentAtomicUint32": "uint32_t",
"AtomicFloat": "float",
"String": None,
"DataMutexString": None,
"DataMutexString": "nsACString",
}
)
@ -56,6 +56,7 @@ RUST_TYPES = {
"int32_t": "i32",
"uint32_t": "u32",
"float": "f32",
"DataMutexString": "nsCString",
}
HEADER_LINE = (
@ -114,6 +115,13 @@ extern "C" {typ} StaticPrefs_{full_id}() {{
}}
"""
STATIC_PREFS_C_GETTERS_NSSTRING_TEMPLATE = """\
extern "C" void StaticPrefs_{full_id}(nsACString *result) {{
const auto preflock = mozilla::StaticPrefs::{full_id}();
result->Append(*preflock);
}}
"""
def error(msg):
raise ValueError(msg)
@ -308,22 +316,40 @@ def generate_code(pref_list, input_filename):
)
if rust:
# Generate the C getter.
static_prefs_c_getters_cpp.append(
STATIC_PREFS_C_GETTERS_TEMPLATE.format(
typ=VALID_TYPES[typ], full_id=full_id
passed_type = VALID_TYPES[typ]
if passed_type == "nsACString":
# Generate the C getter.
static_prefs_c_getters_cpp.append(
STATIC_PREFS_C_GETTERS_NSSTRING_TEMPLATE.format(full_id=full_id)
)
)
# Generate the C getter declaration, in Rust.
decl = " pub fn StaticPrefs_{full_id}() -> {typ};"
static_prefs_rs_decls.append(
decl.format(full_id=full_id, typ=RUST_TYPES[VALID_TYPES[typ]])
)
# Generate the C getter declaration, in Rust.
decl = " pub fn StaticPrefs_{full_id}(result: *mut nsstring::nsACString);"
static_prefs_rs_decls.append(decl.format(full_id=full_id))
# Generate the Rust macro entry.
macro = ' ("{name}") => (unsafe {{ $crate::StaticPrefs_{full_id}() }});'
static_prefs_rs_macro.append(macro.format(name=name, full_id=full_id))
# Generate the Rust macro entry.
macro = ' ("{name}") => (unsafe {{ let mut result = $crate::nsCString::new(); $crate::StaticPrefs_{full_id}(&mut *result); result }});'
static_prefs_rs_macro.append(macro.format(name=name, full_id=full_id))
else:
# Generate the C getter.
static_prefs_c_getters_cpp.append(
STATIC_PREFS_C_GETTERS_TEMPLATE.format(
typ=passed_type, full_id=full_id
)
)
# Generate the C getter declaration, in Rust.
decl = " pub fn StaticPrefs_{full_id}() -> {typ};"
static_prefs_rs_decls.append(
decl.format(full_id=full_id, typ=RUST_TYPES[passed_type])
)
# Generate the Rust macro entry.
macro = (
' ("{name}") => (unsafe {{ $crate::StaticPrefs_{full_id}() }});'
)
static_prefs_rs_macro.append(macro.format(name=name, full_id=full_id))
# Delete this so that `group` can be reused below without Flake8
# complaining.
@ -365,7 +391,7 @@ def generate_code(pref_list, input_filename):
)
# static_prefs.rs contains the Rust macro getters.
static_prefs_rs = [first_line, "", 'extern "C" {']
static_prefs_rs = [first_line, "", "pub use nsstring::nsCString;", 'extern "C" {']
static_prefs_rs.extend(static_prefs_rs_decls)
static_prefs_rs.extend(["}", "", "#[macro_export]", "macro_rules! pref {"])
static_prefs_rs.extend(static_prefs_rs_macro)

Просмотреть файл

@ -9,3 +9,4 @@ license = "MPL-2.0"
[dependencies]
mozbuild = "0.1"
nsstring = { path = "../../../../xpcom/rust/nsstring" }