Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
data1: ("buffer", 0, Array(i32)),
data2: ("buffer", 1, Array(i32)),
):
data2[index] = data1[index] + 1
# Create an array of 100 random int32
n = 100
in1 = [int(random.uniform(0, 100)) for i in range(n)]
in1 = (c_int32 * n)(*in1)
# Create device and shader object
device = wgpu.utils.get_default_device()
cshader = device.create_shader_module(code=compute_shader)
# Create input buffer and upload data to in
buffer1 = device.create_buffer_with_data(data=in1, usage=wgpu.BufferUsage.STORAGE)
# Create output buffer
buffer2 = device.create_buffer(
size=ctypes.sizeof(in1),
usage=wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.MAP_READ,
)
# Create buffer to hold the dispatch parameters for the indirect call
params = (ctypes.c_int32 * 3)(n - 2, 1, 1) # note the minus 2!
buffer3 = device.create_buffer_with_data(
data=params, usage=wgpu.BufferUsage.INDIRECT,
)
# Setup layout and bindings
binding_layouts = [
{
def test_enums_and_flags():
# Enums are str
assert isinstance(wgpu.BindingType.storage_buffer, str)
# Enum groups show their values
assert "storage-buffer" in repr(wgpu.BindingType)
# Flags are ints
assert isinstance(wgpu.BufferUsage.STORAGE, int)
# Flag groups show their field names (in uppercase)
assert "STORAGE" in repr(wgpu.BufferUsage)
# Create an array of 100 random int32
n = 100
in1 = [int(random.uniform(0, 100)) for i in range(n)]
in1 = (c_int32 * n)(*in1)
# Create device and shader object
device = wgpu.utils.get_default_device()
cshader = device.create_shader_module(code=compute_shader)
# Create input buffer and upload data to in
buffer1 = device.create_buffer_with_data(data=in1, usage=wgpu.BufferUsage.STORAGE)
# Create output buffer
buffer2 = device.create_buffer(
size=ctypes.sizeof(in1),
usage=wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.MAP_READ,
)
# Create buffer to hold the dispatch parameters for the indirect call
params = (ctypes.c_int32 * 3)(n - 2, 1, 1) # note the minus 2!
buffer3 = device.create_buffer_with_data(
data=params, usage=wgpu.BufferUsage.INDIRECT,
)
# Setup layout and bindings
binding_layouts = [
{
"binding": 0,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
},
{
# numpy_data = np.frombuffer(data, np.int32)
# out = compute_with_buffers({0: numpy_data}, {1: numpy_data.nbytes}, compute_shader, n=n)
# result = np.frombuffer(out[1], dtype=np.int32)
# print(result)
# %% The long version using the wgpu API
# Create device and shader object
device = wgpu.utils.get_default_device()
cshader = device.create_shader_module(code=compute_shader)
# Create buffer objects, input buffer is mapped.
buffer1 = device.create_buffer_with_data(data=data, usage=wgpu.BufferUsage.STORAGE)
buffer2 = device.create_buffer(
size=data.nbytes, usage=wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.MAP_READ
)
# Setup layout and bindings
binding_layouts = [
{
"binding": 0,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
},
{
"binding": 1,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
},
]
bindings = [
nx, ny, nz = int(n), 1, 1
elif isinstance(n, tuple) and len(n) == 3:
nx, ny, nz = int(n[0]), int(n[1]), int(n[2])
else:
raise TypeError("compute_with_buffers: n must be None, an int, or 3-int tuple.")
if not (nx >= 1 and ny >= 1 and nz >= 1):
raise ValueError("compute_with_buffers: n value(s) must be >= 1.")
# Create a device and compile the shader
device = wgpu.utils.get_default_device()
cshader = device.create_shader_module(code=shader)
# Create buffers for input and output arrays
buffers = {}
for index, array in input_arrays.items():
usage = wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.MAP_WRITE
if index in output_arrays:
usage |= wgpu.BufferUsage.MAP_READ
buffer = device.create_buffer_with_data(data=array, usage=usage)
buffers[index] = buffer
for index, info in output_infos.items():
if index in input_arrays:
continue # We already have this buffer
usage = wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.MAP_READ
buffers[index] = device.create_buffer(size=info["nbytes"], usage=usage)
# Create bindings and binding layouts
bindings = []
binding_layouts = []
for index, buffer in buffers.items():
bindings.append(
{
# import numpy as np
#
# numpy_data = np.frombuffer(data, np.int32)
# out = compute_with_buffers({0: numpy_data}, {1: numpy_data.nbytes}, compute_shader, n=n)
# result = np.frombuffer(out[1], dtype=np.int32)
# print(result)
# %% The long version using the wgpu API
# Create device and shader object
device = wgpu.utils.get_default_device()
cshader = device.create_shader_module(code=compute_shader)
# Create buffer objects, input buffer is mapped.
buffer1 = device.create_buffer_with_data(data=data, usage=wgpu.BufferUsage.STORAGE)
buffer2 = device.create_buffer(
size=data.nbytes, usage=wgpu.BufferUsage.STORAGE | wgpu.BufferUsage.MAP_READ
)
# Setup layout and bindings
binding_layouts = [
{
"binding": 0,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
},
{
"binding": 1,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
},