-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
test_object_retention.py
368 lines (307 loc) · 12.5 KB
/
test_object_retention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import struct
import pytest
import wgpu
from tests.testutils import run_tests
from wgpu.backends.wgpu_native.extras import (
PipelineStatisticName,
begin_pipeline_statistics_query,
create_statistics_query_set,
end_pipeline_statistics_query,
)
#
# wgpu is generally good at keeping alive those objects that it needs to complete a
# GPU operation. This file has tests that agressively deletes objects (so that their
# release method will be called) even though the GPU has pending operations on them.
# We confirm that the operations complete successfully.
SHADER_SOURCE = """
@group(0) @binding(0) var<uniform> offset: vec2f;
@vertex
fn vertex_main( @location(0) position: vec2f ) -> @builtin(position) vec4f {
return vec4f(position + offset, 0, 1.0);
}
@fragment
fn fragment_main() -> @location(0) vec4f {
return vec4f();
}
@compute
@workgroup_size(1)
fn main(@builtin(global_invocation_id) index: vec3<u32>) {
_ = offset;
}
"""
VERTEX_BUFFER_LAYOUT = {
"array_stride": 4 * 2,
"attributes": [
{
"format": wgpu.VertexFormat.float32x2,
"offset": 0,
"shader_location": 0,
},
],
}
BIND_GROUP_ENTRIES = [
{
"binding": 0,
"visibility": wgpu.flags.ShaderStage.VERTEX | wgpu.flags.ShaderStage.COMPUTE,
"buffer": {},
}
]
@pytest.mark.parametrize("use_render_bundle", [False, True])
def test_object_retention_in_render(use_render_bundle, capsys):
with capsys.disabled():
adapter = wgpu.gpu.request_adapter_sync(power_preference="high-performance")
desired_features = ["timestamp-query", "pipeline-statistics-query"]
features = [x for x in desired_features if x in adapter.features]
device = adapter.request_device_sync(required_features=features)
has_timestamps = "timestamp-query" in features
has_statistics = "pipeline-statistics-query" in features
output_texture_format = wgpu.TextureFormat.rgba8unorm
depth_texture_format = wgpu.TextureFormat.depth32float
shader = device.create_shader_module(code=SHADER_SOURCE)
bind_group_entries = BIND_GROUP_ENTRIES.copy()
bind_group_layout = device.create_bind_group_layout(entries=bind_group_entries)
del bind_group_entries
pipeline_layout = device.create_pipeline_layout(
bind_group_layouts=[bind_group_layout]
)
del bind_group_layout
pipeline = device.create_render_pipeline(
layout=pipeline_layout,
vertex={
"module": shader,
"buffers": [VERTEX_BUFFER_LAYOUT],
},
fragment={
"module": shader,
"targets": [{"format": output_texture_format}],
},
depth_stencil={"format": depth_texture_format},
primitive={"topology": "triangle-strip"},
)
del pipeline_layout
vertex_buffer = device.create_buffer_with_data(
data=struct.pack("8f", -0.5, -0.5, -0.5, +0.5, +0.5, -0.5, +0.5, +0.5),
usage=wgpu.BufferUsage.VERTEX,
)
indirect_buffer = device.create_buffer_with_data(
# Used for both indexed and non-indexed draws.
data=struct.pack("5i", 4, 1, 0, 0, 0),
usage=wgpu.BufferUsage.INDIRECT,
)
index_buffer = device.create_buffer_with_data(
data=struct.pack("4i", 0, 1, 2, 3),
usage=wgpu.BufferUsage.INDEX,
)
data_buffer = device.create_buffer(size=2 * 4, usage="UNIFORM")
bind_group = device.create_bind_group(
layout=pipeline.get_bind_group_layout(0),
entries=[
{"binding": 0, "resource": {"buffer": data_buffer}},
],
)
del data_buffer
output_texture = device.create_texture(
size=[128, 128],
format=output_texture_format,
usage="RENDER_ATTACHMENT",
)
depth_texture = device.create_texture(
size=[128, 128],
format=depth_texture_format,
usage="RENDER_ATTACHMENT",
)
occlusion_query_set = device.create_query_set(type="occlusion", count=2)
timestamp_query_set = None
if has_timestamps:
timestamp_query_set = device.create_query_set(type="timestamp", count=2)
render_pass_descriptor = {
"color_attachments": [
{
"clear_value": (0, 0, 0, 0),
"load_op": wgpu.LoadOp.clear,
"store_op": wgpu.StoreOp.store,
"view": output_texture.create_view(),
}
],
"depth_stencil_attachment": {
"view": depth_texture.create_view(),
"depth_clear_value": 0.1,
"depth_load_op": wgpu.LoadOp.clear,
"depth_store_op": wgpu.StoreOp.store,
},
"occlusion_query_set": occlusion_query_set,
}
if has_timestamps:
render_pass_descriptor["timestamp_writes"] = {
"query_set": timestamp_query_set,
"beginning_of_pass_write_index": 0,
"end_of_pass_write_index": 1,
}
del output_texture
del depth_texture
del occlusion_query_set
del timestamp_query_set
def do_draw(encoder):
nonlocal pipeline, bind_group, vertex_buffer, index_buffer, indirect_buffer
encoder.set_pipeline(pipeline)
encoder.set_bind_group(0, bind_group)
encoder.set_vertex_buffer(0, vertex_buffer)
encoder.set_index_buffer(index_buffer, "uint32")
encoder.draw(4, 1, 0, 0)
encoder.draw_indexed(4, 1, 0, 0)
encoder.draw_indirect(indirect_buffer, 0)
encoder.draw_indexed_indirect(indirect_buffer, 0)
pipeline = bind_group = vertex_buffer = index_buffer = indirect_buffer = (
None
)
command_encoder = device.create_command_encoder()
render_pass = command_encoder.begin_render_pass(**render_pass_descriptor)
del render_pass_descriptor
if use_render_bundle:
render_bundle_encoder = device.create_render_bundle_encoder(
color_formats=[output_texture_format],
depth_stencil_format=depth_texture_format,
)
do_draw(render_bundle_encoder)
render_bundle = render_bundle_encoder.finish()
render_pass.execute_bundles([render_bundle])
else:
if has_statistics:
statistics_query_set = create_statistics_query_set(
device,
count=2,
statistics=[PipelineStatisticName.FragmentShaderInvocations],
)
begin_pipeline_statistics_query(render_pass, statistics_query_set, 0)
del statistics_query_set
do_draw(render_pass)
if has_statistics:
end_pipeline_statistics_query(render_pass)
render_pass.end()
device.queue.submit([command_encoder.finish()])
def test_object_retention_in_compute():
adapter = wgpu.gpu.request_adapter_sync(power_preference="high-performance")
desired_features = ["timestamp-query", "pipeline-statistics-query"]
features = [x for x in desired_features if x in adapter.features]
device = adapter.request_device_sync(required_features=features)
has_timestamps = "timestamp-query" in features
has_statistics = "pipeline-statistics-query" in features
shader = device.create_shader_module(code=SHADER_SOURCE)
bind_group_entries = BIND_GROUP_ENTRIES.copy()
bind_group_layout = device.create_bind_group_layout(entries=bind_group_entries)
del bind_group_entries
pipeline_layout = device.create_pipeline_layout(
bind_group_layouts=[bind_group_layout]
)
del bind_group_layout
pipeline = device.create_compute_pipeline(
layout=pipeline_layout,
compute={
"module": shader,
},
)
del pipeline_layout
data_buffer = device.create_buffer(size=2 * 4, usage="UNIFORM")
bind_group = device.create_bind_group(
layout=pipeline.get_bind_group_layout(0),
entries=[
{"binding": 0, "resource": {"buffer": data_buffer}},
],
)
del data_buffer
timestamp_query_set = None
if has_timestamps:
timestamp_query_set = device.create_query_set(type="timestamp", count=2)
compute_pass_descriptor = {}
if has_timestamps:
compute_pass_descriptor["timestamp_writes"] = {
"query_set": timestamp_query_set,
"beginning_of_pass_write_index": 0,
"end_of_pass_write_index": 1,
}
del timestamp_query_set
def setup_compute(encoder):
indirect_buffer = device.create_buffer_with_data(
# Used for both indexed and non-indexed draws.
data=struct.pack("3i", 1, 1, 1),
usage=wgpu.BufferUsage.INDIRECT,
)
nonlocal pipeline, bind_group
encoder.set_pipeline(pipeline)
encoder.set_bind_group(0, bind_group)
encoder.dispatch_workgroups(1, 1, 1)
encoder.dispatch_workgroups_indirect(indirect_buffer, 0)
pipeline = bind_group = None
command_encoder = device.create_command_encoder()
compute_pass = command_encoder.begin_compute_pass(**compute_pass_descriptor)
del compute_pass_descriptor
if has_statistics:
statistics_query_set = create_statistics_query_set(
device,
count=2,
statistics=[PipelineStatisticName.ComputeShaderInvocations],
)
begin_pipeline_statistics_query(compute_pass, statistics_query_set, 0)
del statistics_query_set
setup_compute(compute_pass)
if has_statistics:
end_pipeline_statistics_query(compute_pass)
compute_pass.end()
device.queue.submit([command_encoder.finish()])
def test_clear_buffer():
device = wgpu.utils.get_default_device()
buffer = device.create_buffer(size=100, usage=wgpu.BufferUsage.COPY_DST)
command_encoder = device.create_command_encoder()
command_encoder.clear_buffer(buffer)
del buffer
device.queue.submit([command_encoder.finish()])
def test_copy_buffer_to_buffer():
device = wgpu.utils.get_default_device()
buffer1 = device.create_buffer(size=100, usage=wgpu.BufferUsage.COPY_SRC)
buffer2 = device.create_buffer(size=100, usage=wgpu.BufferUsage.COPY_DST)
command_encoder = device.create_command_encoder()
command_encoder.copy_buffer_to_buffer(buffer1, 0, buffer2, 0, 100)
del buffer1
del buffer2
device.queue.submit([command_encoder.finish()])
def test_copy_buffer_to_texture():
device = wgpu.utils.get_default_device()
texture = device.create_texture(size=[64, 64], format="r32uint", usage="COPY_DST")
buffer = device.create_buffer(size=texture._nbytes, usage=wgpu.BufferUsage.COPY_SRC)
command_encoder = device.create_command_encoder()
command_encoder.copy_buffer_to_texture(
{"buffer": buffer, "offset": 0, "bytes_per_row": 64 * 4, "rows_per_image": 64},
{"texture": texture, "mip_level": 0, "origin": (0, 0, 0)},
(64, 64),
)
del buffer
del texture
device.queue.submit([command_encoder.finish()])
def test_copy_texture_to_buffer():
device = wgpu.utils.get_default_device()
texture = device.create_texture(size=[64, 64], format="r32uint", usage="COPY_SRC")
buffer = device.create_buffer(size=texture._nbytes, usage=wgpu.BufferUsage.COPY_DST)
command_encoder = device.create_command_encoder()
command_encoder.copy_texture_to_buffer(
{"texture": texture, "mip_level": 0, "origin": (0, 0, 0)},
{"buffer": buffer, "offset": 0, "bytes_per_row": 64 * 4, "rows_per_image": 64},
(64, 64),
)
del buffer
del texture
device.queue.submit([command_encoder.finish()])
def test_copy_texture_to_texture():
device = wgpu.utils.get_default_device()
texture1 = device.create_texture(size=[64, 64], format="r32uint", usage="COPY_SRC")
texture2 = device.create_texture(size=[64, 64], format="r32uint", usage="COPY_DST")
command_encoder = device.create_command_encoder()
command_encoder.copy_texture_to_texture(
{"texture": texture1, "mip_level": 0, "origin": (0, 0, 0)},
{"texture": texture2, "mip_level": 0, "origin": (0, 0, 0)},
(64, 64),
)
del texture1
del texture2
device.queue.submit([command_encoder.finish()])
if __name__ == "__main__":
run_tests(globals())