Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bindings = [
{"binding": 0, "resource": texture_view},
{"binding": 1, "resource": sampler},
]
binding_layouts = [
{
"binding": 0,
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampled_texture,
"view_dimension": wgpu.TextureViewDimension.d2,
"texture_component_type": texture_component_type,
},
{
"binding": 1,
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampler,
},
]
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(
bind_group_layouts=[bind_group_layout]
)
bind_group = device.create_bind_group(layout=bind_group_layout, entries=bindings)
# Render
render_args = device, vertex_shader, fragment_shader, pipeline_layout, bind_group
# render_to_screen(*render_args)
a = render_to_texture(*render_args, size=(64, 64))
# print(a.max(), a[:,:,0].max())
# Check that the background is all zero
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,
},
{
"binding": 1,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
},
]
bindings = [
{
"binding": 0,
"resource": {"buffer": buffer1, "offset": 0, "size": buffer1.size},
},
{
"binding": 1,
"resource": {"buffer": buffer2, "offset": 0, "size": buffer2.size},
},
]
# Put everything together
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(
texture_component_type = wgpu.TextureComponentType.float
elif "uint" in texture_format:
texture_component_type = wgpu.TextureComponentType.uint
else:
texture_component_type = wgpu.TextureComponentType.sint
# Bindings and layout
bindings = [
{"binding": 0, "resource": texture_view},
{"binding": 1, "resource": sampler},
]
binding_layouts = [
{
"binding": 0,
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampled_texture,
"view_dimension": wgpu.TextureViewDimension.d2,
"texture_component_type": texture_component_type,
},
{
"binding": 1,
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampler,
},
]
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(
bind_group_layouts=[bind_group_layout]
)
bind_group = device.create_bind_group(layout=bind_group_layout, entries=bindings)
# Render
| wgpu.BufferUsage.COPY_DST
)
buffer = device.create_buffer_with_data(data=data1, usage=buffer_usage)
assert buffer.usage == buffer_usage
# Define bindings
# One can see here why we need 2 textures: one is readonly, one writeonly
bindings = [
{"binding": 0, "resource": texture_view1},
{"binding": 1, "resource": texture_view2},
]
binding_layouts = [
{
"binding": 0,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.readonly_storage_texture, # <-
"view_dimension": texture_dim,
"storage_texture_format": texture_format,
"texture_component_type": texture_component_type,
},
{
"binding": 1,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.writeonly_storage_texture, # <-
"view_dimension": texture_dim,
"storage_texture_format": texture_format,
"texture_component_type": texture_component_type,
},
]
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(
bind_group_layouts=[bind_group_layout]
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)
bind_groups_layout_entries[SAMPLER_BINDING[0]].append(
{
"binding": SAMPLER_BINDING[1],
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampler,
}
)
bind_groups_entries[TEXTURE_BINDING[0]].append(
{"binding": TEXTURE_BINDING[1], "resource": texture_view}
)
bind_groups_layout_entries[TEXTURE_BINDING[0]].append(
{
"binding": TEXTURE_BINDING[1],
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampled_texture,
"view_dimension": wgpu.TextureViewDimension.d2,
"texture_component_type": wgpu.TextureComponentType.uint,
}
)
# Create the wgou binding objects
bind_group_layouts = []
bind_groups = []
for entries, layout_entries in zip(bind_groups_entries, bind_groups_layout_entries):
bind_group_layout = device.create_bind_group_layout(entries=layout_entries)
bind_group_layouts.append(bind_group_layout)
bind_groups.append(
device.create_bind_group(layout=bind_group_layout, entries=entries)
)
# Create bindings and binding layouts
bindings = []
binding_layouts = []
for index, buffer in buffers.items():
bindings.append(
{
"binding": index,
"resource": {"buffer": buffer, "offset": 0, "size": buffer.size},
}
)
binding_layouts.append(
{
"binding": index,
"visibility": wgpu.ShaderStage.COMPUTE,
"type": wgpu.BindingType.storage_buffer,
"has_dynamic_offset": False,
}
)
# Put buffers together
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(
bind_group_layouts=[bind_group_layout]
)
bind_group = device.create_bind_group(layout=bind_group_layout, entries=bindings)
# Create a pipeline and "run it"
compute_pipeline = device.create_compute_pipeline(
layout=pipeline_layout,
compute_stage={"module": cshader, "entry_point": "main"},
)
bind_groups_entries[UNIFORM_BINDING[0]].append(
{
"binding": UNIFORM_BINDING[1],
"resource": {
"buffer": uniform_buffer,
"offset": 0,
"size": uniform_buffer.size,
},
}
)
bind_groups_layout_entries[UNIFORM_BINDING[0]].append(
{
"binding": UNIFORM_BINDING[1],
"visibility": wgpu.ShaderStage.VERTEX | wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.uniform_buffer,
}
)
bind_groups_entries[SAMPLER_BINDING[0]].append(
{"binding": SAMPLER_BINDING[1], "resource": sampler}
)
bind_groups_layout_entries[SAMPLER_BINDING[0]].append(
{
"binding": SAMPLER_BINDING[1],
"visibility": wgpu.ShaderStage.FRAGMENT,
"type": wgpu.BindingType.sampler,
}
)
bind_groups_entries[TEXTURE_BINDING[0]].append(
{"binding": TEXTURE_BINDING[1], "resource": texture_view}
# 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 = [
{"binding": 0, "resource": {"buffer": buffer1, "offset": 0, "size": buffer1.size},},
{"binding": 1, "resource": {"buffer": buffer2, "offset": 0, "size": buffer2.size},},
]
# Put everything together
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(bind_group_layouts=[bind_group_layout])
bind_group = device.create_bind_group(layout=bind_group_layout, entries=bindings)
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 = [
{"binding": 0, "resource": {"buffer": buffer1, "offset": 0, "size": buffer1.size},},
{"binding": 1, "resource": {"buffer": buffer2, "offset": 0, "size": buffer2.size},},
]
# Put everything together
bind_group_layout = device.create_bind_group_layout(entries=binding_layouts)
pipeline_layout = device.create_pipeline_layout(bind_group_layouts=[bind_group_layout])
bind_group = device.create_bind_group(layout=bind_group_layout, entries=bindings)
# Create and run the pipeline
compute_pipeline = device.create_compute_pipeline(
layout=pipeline_layout, compute_stage={"module": cshader, "entry_point": "main"},
)