Skip to content

Commit

Permalink
Rename wgpu features to be consistent with webgpu features (#566)
Browse files Browse the repository at this point in the history
* Implementaton of NativeFeatures

* Update codegen_report.md

---------

Co-authored-by: Korijn van Golen <[email protected]>
  • Loading branch information
fyellin and Korijn authored Sep 4, 2024
1 parent 43c8096 commit ce1694e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
12 changes: 10 additions & 2 deletions codegen/wgpu_native_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import re
from collections import defaultdict

from codegen.utils import print, blacken, Patcher
from codegen.utils import print, blacken, Patcher, to_snake_case
from codegen.hparser import get_h_parser
from codegen.idlparser import get_idl_parser
from codegen.files import file_cache
Expand Down Expand Up @@ -128,7 +128,15 @@ def write_mappings():
if key == "Force32":
continue
pylines.append(f' "{key}": {val},')
pylines.append(" }")
pylines.append(" },")
for name in ["NativeFeature"]:
pylines.append(f' "{name}":' + " {")
for key, val in hp.enums[name].items():
if key == "Force32":
continue
xkey = to_snake_case(key).replace("_", "-")
pylines.append(f' "{xkey}": {val},')
pylines.append(" },")
pylines.append("}")

# Write a few native-only mappings: int => key
Expand Down
43 changes: 43 additions & 0 deletions tests/test_wgpu_native_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,48 @@ def test_get_memoryview_and_address():
assert address > 0


def are_features_wgpu_legal(features):
"""Returns true if the list of features is legal. Determining whether a specific
set of features is implemented on a particular device would make the tests fragile,
so we only verify that the names are legal feature names."""
adapter = wgpu.gpu.request_adapter(power_preference="high-performance")
try:
adapter.request_device(required_features=features)
return True
except RuntimeError as e:
assert "Unsupported features were requested" in str(e)
return True
except KeyError:
return False


def test_features_are_legal():
# A standard feature. Probably exists
assert are_features_wgpu_legal(["shader-f16"])
# Two common extension features
assert are_features_wgpu_legal(["multi-draw-indirect", "vertex-writable-storage"])
# An uncommon extension feature. Certainly not on a mac.
assert are_features_wgpu_legal(["pipeline-statistics-query"])
assert are_features_wgpu_legal(
["push-constants", "vertex-writable-storage", "depth-clip-control"]
)
# We can also use underscore
assert are_features_wgpu_legal(["push_constants", "vertex_writable_storage"])


def test_features_are_illegal():
# not camel Case
assert not are_features_wgpu_legal(["pushConstants"])
# writable is misspelled
assert not are_features_wgpu_legal(
["multi-draw-indirect", "vertex-writeable-storage"]
)
assert not are_features_wgpu_legal(["my-made-up-feature"])


if __name__ == "__main__":
run_tests(globals())


if __name__ == "__main__":
run_tests(globals())
30 changes: 9 additions & 21 deletions wgpu/backends/wgpu_native/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@
# %% Helper functions and objects


# Features that wgpu-native supports that are not part of WebGPU
NATIVE_FEATURES = (
"PushConstants",
"TextureAdapterSpecificFormatFeatures",
"MultiDrawIndirect",
"MultiDrawIndirectCount",
"VertexWritableStorage",
)

# Object to be able to bind the lifetime of objects to other objects
_refs_per_struct = WeakKeyDictionary()

Expand Down Expand Up @@ -398,11 +389,10 @@ def to_py_str(key):
features.add(f)

# Native features
for f in NATIVE_FEATURES:
i = getattr(lib, f"WGPUNativeFeature_{f}")
for name, i in enum_str2int["NativeFeature"].items():
# H: WGPUBool f(WGPUAdapter adapter, WGPUFeatureName feature)
if libf.wgpuAdapterHasFeature(adapter_id, i):
features.add(f)
features.add(name)

# ----- Done

Expand Down Expand Up @@ -774,12 +764,11 @@ def _request_device(
c_features = set()
for f in required_features:
if isinstance(f, str):
if "_" in f:
f = "".join(x.title() for x in f.split("_"))
i1 = enummap.get(f"FeatureName.{f}", None)
i2 = getattr(lib, f"WGPUNativeFeature_{f}", None)
i = i2 if i1 is None else i1
if i is None: # pragma: no cover
f = f.replace("_", "-")
i = enummap.get(f"FeatureName.{f}", None)
if i is None:
i = enum_str2int["NativeFeature"].get(f, None)
if i is None:
raise KeyError(f"Unknown feature: '{f}'")
c_features.add(i)
else:
Expand Down Expand Up @@ -905,11 +894,10 @@ def callback(status, result, message, userdata):
features.add(f)

# Native features
for f in NATIVE_FEATURES:
i = getattr(lib, f"WGPUNativeFeature_{f}")
for name, i in enum_str2int["NativeFeature"].items():
# H: WGPUBool f(WGPUDevice device, WGPUFeatureName feature)
if libf.wgpuDeviceHasFeature(device_id, i):
features.add(f)
features.add(name)

# ---- Get queue

Expand Down
14 changes: 13 additions & 1 deletion wgpu/backends/wgpu_native/_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,19 @@
"Vulkan": 6,
"OpenGL": 7,
"OpenGLES": 8,
}
},
"NativeFeature": {
"push-constants": 196609,
"texture-adapter-specific-format-features": 196610,
"multi-draw-indirect": 196611,
"multi-draw-indirect-count": 196612,
"vertex-writable-storage": 196613,
"texture-binding-array": 196614,
"sampled-texture-and-storage-buffer-array-non-uniform-indexing": 196615,
"pipeline-statistics-query": 196616,
"storage-resource-binding-array": 196617,
"partially-bound-binding-array": 196618,
},
}
enum_int2str = {
"BackendType": {
Expand Down

0 comments on commit ce1694e

Please sign in to comment.