Skip to content

Commit

Permalink
Fix some small errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fyellin committed Sep 19, 2024
1 parent 5aa7a92 commit 1625262
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
16 changes: 11 additions & 5 deletions tests/test_set_override.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import wgpu.utils
from tests.testutils import can_use_wgpu_lib, run_tests
from wgpu import TextureFormat
from wgpu import GPUValidationError, TextureFormat

if not can_use_wgpu_lib:
pytest.skip("Skipping tests that need the wgpu lib", allow_module_level=True)
Expand Down Expand Up @@ -197,10 +197,16 @@ def test_override_compute_constants(runner):

def test_numbered_constants_must_be_overridden_by_number(runner):
overrides = {"c": 23, "d": 24}
# This does absolutely nothing. It doesn't even error.
assert [1, 2, 3, 0, 1, 2, 3, 0] == runner.run_test(
render=True, vertex_constants=overrides, fragment_constants=overrides
)
try:
# In naga, the bad constant is ignored.
# In the JS implementation, this throws an exception, which I think is the
# correct behavior. So just in case this ever gets fixed, we accept either.
result = runner.run_test(
render=True, vertex_constants=overrides, fragment_constants=overrides
)
except GPUValidationError:
return
assert [1, 2, 3, 0, 1, 2, 3, 0] == result


if __name__ == "__main__":
Expand Down
57 changes: 36 additions & 21 deletions tests/test_wgpu_vertex_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@


class Runner:
REQUIRED_FEATURES = ["multi-draw-indirect", "indirect-first-instance"]
REQUIRED_FEATURES = ["indirect-first-instance"]
OPTIONAL_FEATURES = ["multi-draw-indirect"] # we'll be adding more

@classmethod
def is_usable(cls):
Expand All @@ -76,7 +77,11 @@ def is_usable(cls):

def __init__(self):
adapter = wgpu.gpu.request_adapter(power_preference="high-performance")
self.device = adapter.request_device(required_features=self.REQUIRED_FEATURES)
features = [
*self.REQUIRED_FEATURES,
*[x for x in self.OPTIONAL_FEATURES if x in adapter.features],
]
self.device = adapter.request_device(required_features=features)
self.output_texture = self.device.create_texture(
# Actual size is immaterial. Could just be 1x1
size=[128, 128],
Expand Down Expand Up @@ -158,7 +163,8 @@ def __init__(self):
# We're going to want to try calling these draw functions from a buffer, and it
# would be nice to test that these buffers have an offset
self.draw_data_buffer = self.device.create_buffer_with_data(
data=np.uint32([0, 0, *self.draw_args1, *self.draw_args2]), usage="INDIRECT"
data=np.uint32([0, 0, *self.draw_args1, *self.draw_args2]),
usage="INDIRECT",
)
self.draw_data_buffer_indexed = self.device.create_buffer_with_data(
data=np.uint32([0, 0, *self.draw_indexed_args1, *self.draw_indexed_args2]),
Expand All @@ -181,7 +187,7 @@ def create_render_bundle_encoder(self, draw_function):
draw_function(render_bundle_encoder)
return render_bundle_encoder.finish()

def run_draw_test(self, expected_result, draw_function):
def run_draw_test(self, draw_function, indexed, *, expected_result=None):
encoder = self.device.create_command_encoder()
encoder.clear_buffer(self.counter_buffer)
this_pass = encoder.begin_render_pass(**self.render_pass_descriptor)
Expand All @@ -194,12 +200,17 @@ def run_draw_test(self, expected_result, draw_function):
count = self.device.queue.read_buffer(self.counter_buffer).cast("i")[0]
if count > MAX_INFO:
pytest.fail("Too many data points written to output buffer")
if count == 0:
pytest.fail("No data points written to output buffer")
# Get the result as a series of tuples
info_view = self.device.queue.read_buffer(self.data_buffer, size=count * 2 * 4)
info = np.frombuffer(info_view, dtype=np.uint32).reshape(-1, 2)
info = [tuple(info[i]) for i in range(len(info))]
info_set = set(info)
assert len(info) == len(info_set)
info = info_view.cast("I", (count, 2)).tolist()
info_set = set(tuple(x) for x in info)
if expected_result is None:
if indexed:
expected_result = self.expected_result_draw_indexed
else:
expected_result = self.expected_result_draw
assert info_set == expected_result


Expand All @@ -217,30 +228,33 @@ def draw(encoder):
encoder.draw(*runner.draw_args1)
encoder.draw(*runner.draw_args2)

runner.run_draw_test(runner.expected_result_draw, draw)
runner.run_draw_test(draw, False)


def test_draw_indirect(runner):
def draw(encoder):
encoder.draw_indirect(runner.draw_data_buffer, 8)
encoder.draw_indirect(runner.draw_data_buffer, 8 + 16)

runner.run_draw_test(runner.expected_result_draw, draw)
runner.run_draw_test(draw, False)


def test_draw_mixed(runner):
def draw(encoder):
encoder.draw(*runner.draw_args1)
encoder.draw_indirect(runner.draw_data_buffer, 8 + 16)

runner.run_draw_test(runner.expected_result_draw, draw)
runner.run_draw_test(draw, False)


def test_multi_draw_indirect(runner):
if "multi-draw-indirect" not in runner.device.features:
pytest.skip("Must have 'multi-draw-indirect' to run")

def draw(encoder):
multi_draw_indirect(encoder, runner.draw_data_buffer, offset=8, count=2)

runner.run_draw_test(runner.expected_result_draw, draw)
runner.run_draw_test(draw, False)


def test_draw_via_encoder(runner):
Expand All @@ -252,8 +266,7 @@ def draw(encoder):
for _ in range(2):
# We run this test twice to verify that encoders are reusable.
runner.run_draw_test(
runner.expected_result_draw,
lambda encoder: encoder.execute_bundles([render_bundle_encoder]),
lambda encoder: encoder.execute_bundles([render_bundle_encoder]), False
)


Expand All @@ -269,10 +282,10 @@ def draw2(encoder):
render_bundle_encoder2 = runner.create_render_bundle_encoder(draw2)

runner.run_draw_test(
runner.expected_result_draw,
lambda encoder: encoder.execute_bundles(
[render_bundle_encoder1, render_bundle_encoder2]
),
False,
)


Expand All @@ -281,32 +294,35 @@ def draw(encoder):
encoder.draw_indexed(*runner.draw_indexed_args1)
encoder.draw_indexed(*runner.draw_indexed_args2)

runner.run_draw_test(runner.expected_result_draw_indexed, draw)
runner.run_draw_test(draw, True)


def test_draw_indexed_indirect(runner):
def draw(encoder):
encoder.draw_indexed_indirect(runner.draw_data_buffer_indexed, 8)
encoder.draw_indexed_indirect(runner.draw_data_buffer_indexed, 8 + 20)

runner.run_draw_test(runner.expected_result_draw_indexed, draw)
runner.run_draw_test(draw, True)


def test_draw_indexed_mixed(runner):
def draw(encoder):
encoder.draw_indexed_indirect(runner.draw_data_buffer_indexed, 8)
encoder.draw_indexed(*runner.draw_indexed_args2)

runner.run_draw_test(runner.expected_result_draw_indexed, draw)
runner.run_draw_test(draw, True)


def test_multi_draw_indexed_indirect(runner):
if "multi-draw-indirect" not in runner.device.features:
pytest.skip("Must have 'multi-draw-indirect' to run")

def draw(encoder):
multi_draw_indexed_indirect(
encoder, runner.draw_data_buffer_indexed, offset=8, count=2
)

runner.run_draw_test(runner.expected_result_draw_indexed, draw)
runner.run_draw_test(draw, True)


def test_draw_indexed_via_encoder(runner):
Expand All @@ -317,8 +333,7 @@ def draw(encoder):
render_bundle_encoder = runner.create_render_bundle_encoder(draw)
for _ in range(2):
runner.run_draw_test(
runner.expected_result_draw_indexed,
lambda encoder: encoder.execute_bundles([render_bundle_encoder]),
lambda encoder: encoder.execute_bundles([render_bundle_encoder]), True
)


Expand Down
6 changes: 0 additions & 6 deletions wgpu/backends/wgpu_native/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3031,12 +3031,6 @@ def _multi_draw_indexed_indirect(self, buffer, offset, count):
self._internal, buffer._internal, int(offset), int(count)
)

def _release(self):
if self._internal is not None and libf is not None:
self._internal, internal = None, self._internal
# H: void f(WGPURenderPassEncoder renderPassEncoder)
libf.wgpuRenderPassEncoderRelease(internal)


class GPURenderBundleEncoder(
classes.GPURenderBundleEncoder,
Expand Down
4 changes: 2 additions & 2 deletions wgpu/resources/codegen_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Diffs for GPUQueue: add read_buffer, add read_texture, hide copy_external_image_to_texture
* Validated 37 classes, 112 methods, 45 properties
### Patching API for backends/wgpu_native/_api.py
* Validated 37 classes, 101 methods, 0 properties
* Validated 37 classes, 100 methods, 0 properties
## Validating backends/wgpu_native/_api.py
* Enum field FeatureName.texture-compression-bc-sliced-3d missing in wgpu.h
* Enum field FeatureName.clip-distances missing in wgpu.h
Expand All @@ -35,6 +35,6 @@
* Enum CanvasAlphaMode missing in wgpu.h
* Enum CanvasToneMappingMode missing in wgpu.h
* Wrote 236 enum mappings and 47 struct-field mappings to wgpu_native/_mappings.py
* Validated 134 C function calls
* Validated 133 C function calls
* Not using 70 C functions
* Validated 81 C structs

0 comments on commit 1625262

Please sign in to comment.