Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
TODO: not yet available in wgpu-native
"""
# wgpu.help('RenderBundleDescriptor', 'renderbundleencoderfinish', dev=True)
# IDL: GPURenderBundle finish(optional GPURenderBundleDescriptor descriptor = {});
def finish(self, *, label=""):
""" Finish recording and return a :class:`GPURenderBundle`.
Arguments:
label (str): A human readable label. Optional.
"""
raise NotImplementedError()
class GPUQueue(GPUObject):
"""
A queue can be used to submit command buffers to.
You can obtain a queue object via the :attr:`GPUDevice.default_queue` property.
"""
# wgpu.help('queuesubmit', dev=True)
# IDL: void submit(sequence commandBuffers);
def submit(self, command_buffers):
""" Submit a :class:`GPUCommandBuffer` to the queue.
Arguments:
command_buffers (list): The :class:`GPUCommandBuffer` objects to add.
"""
raise NotImplementedError()
""" The texture object to which this is a view.
"""
return self._texture
class GPUSampler(GPUObject):
"""
A sampler specifies how a texture (view) must be sampled by the shader,
in terms of subsampling, sampling between mip levels, and sampling out
of the image boundaries.
Create a sampler using :func:`GPUDevice.create_sampler`.
"""
class GPUBindGroupLayout(GPUObject):
"""
A bind group layout defines the interface between a set of
resources bound in a :class:`GPUBindGroup` and their accessibility in shader
stages.
Create a bind group layout using :func:`GPUDevice.create_bind_group_layout`.
"""
def __init__(self, label, internal, device, bindings):
super().__init__(label, internal, device)
self._bindings = tuple(bindings)
class GPUBindGroup(GPUObject):
"""
A bind group represents a group of bindings, the shader slot,
# wgpu.help('shadermodulecompilationinfo', dev=True)
# IDL: Promise compilationInfo();
def compilation_info(self):
""" Get shader compilation info. Always returns empty string at the moment.
"""
return []
# wgpu.help('shadermodulecompilationinfo', dev=True)
# IDL: Promise compilationInfo();
async def compilation_info_async(self):
""" Async version of compilation_info()
"""
return self.compilation_info() # no-cover
class PipelineBase(GPUObject):
""" Base object for compute and render pipelines.
"""
def __init__(self, label, internal, device, layout):
super().__init__(label, internal, device)
self._layout = layout
@property
def layout(self):
""" The the layout of this pipeline.
"""
return self._layout
# wgpu.help('pipelinebasegetbindgrouplayout', dev=True)
# IDL: GPUBindGroupLayout getBindGroupLayout(unsigned long index);
def get_bind_group_layout(self, index):
"""
raise NotImplementedError()
# wgpu.help('CommandBufferDescriptor', 'commandencoderfinish', dev=True)
# IDL: GPUCommandBuffer finish(optional GPUCommandBufferDescriptor descriptor = {});
def finish(self, *, label=""):
""" Finish recording. Returns a :class:`GPUCommandBuffer` to
submit to a :class:`GPUQueue`.
Arguments:
label (str): A human readable label. Optional.
"""
raise NotImplementedError()
class GPUProgrammablePassEncoder(GPUObject):
"""
Base class for the different pass encoder classes.
"""
# wgpu.help('BindGroup', 'Index32', 'Size32', 'Size64', 'programmablepassencodersetbindgroup', dev=True)
# IDL: void setBindGroup(GPUIndex32 index, GPUBindGroup bindGroup, Uint32Array dynamicOffsetsData, GPUSize64 dynamicOffsetsDataStart, GPUSize32 dynamicOffsetsDataLength);
def set_bind_group(
self,
index,
bind_group,
dynamic_offsets_data,
dynamic_offsets_data_start,
dynamic_offsets_data_length,
):
""" Associate the given bind group (i.e. group or resources) with the
given slot/index.
""" A human-readable name identifying the GPU object.
"""
return self._label
def _destroy(self):
""" Subclasses can implement this to clean up.
"""
pass
def __del__(self):
self._destroy()
# Public destroy() methods are implemented on classes as the WebGPU spec specifies.
class GPUDevice(GPUObject):
"""
A device is the logical instantiation of an adapter, through which
internal objects are created. It can be shared across threads.
A device is the exclusive owner of all internal objects created
from it: when the device is lost, all objects created from it become
invalid.
Create a device using :func:`GPUAdapter.request_device` or
:func:`GPUAdapter.request_device_async`.
"""
def __init__(self, label, internal, adapter, extensions, limits, default_queue):
super().__init__(label, internal, None)
assert isinstance(adapter, GPUAdapter)
self._adapter = adapter
self._extensions = tuple(sorted([str(x) for x in extensions]))
self._size = size
@property
def size(self):
""" The texture size (as a 3-tuple).
"""
return self._size
@property
def texture(self):
""" The texture object to which this is a view.
"""
return self._texture
class GPUSampler(GPUObject):
"""
A sampler specifies how a texture (view) must be sampled by the shader,
in terms of subsampling, sampling between mip levels, and sampling out
of the image boundaries.
Create a sampler using :func:`GPUDevice.create_sampler`.
"""
class GPUBindGroupLayout(GPUObject):
"""
A bind group layout defines the interface between a set of
resources bound in a :class:`GPUBindGroup` and their accessibility in shader
stages.
Create a bind group layout using :func:`GPUDevice.create_bind_group_layout`.
rendering).
Create a render pipeline using :func:`GPUDevice.create_render_pipeline`.
"""
class GPUCommandBuffer(GPUObject):
"""
A command buffer stores a series of commands, generated by a
:class:`GPUCommandEncoder`, to be submitted to a :class:`GPUQueue`.
Create a command buffer using :func:`GPUCommandEncoder.finish`.
"""
class GPUCommandEncoder(GPUObject):
"""
A command encoder is used to record a series of commands. When done,
call :func:`finish` to obtain a GPUCommandBuffer object.
Create a command encoder using :func:`GPUDevice.create_command_encoder`.
"""
# wgpu.help('ComputePassDescriptor', 'commandencoderbegincomputepass', dev=True)
# IDL: GPUComputePassEncoder beginComputePass(optional GPUComputePassDescriptor descriptor = {});
def begin_compute_pass(self, *, label=""):
""" Record the beginning of a compute pass. Returns a
:class:`GPUComputePassEncoder` object.
Arguments:
label (str): A human readable label. Optional.
"""
Create a compute pipeline using :func:`GPUDevice.create_compute_pipeline`.
"""
class GPURenderPipeline(PipelineBase):
"""
A render pipeline represents a single pipeline to draw something
using a vertex and a fragment shader. The render target can come
from a window on the screen or from an in-memory texture (off-screen
rendering).
Create a render pipeline using :func:`GPUDevice.create_render_pipeline`.
"""
class GPUCommandBuffer(GPUObject):
"""
A command buffer stores a series of commands, generated by a
:class:`GPUCommandEncoder`, to be submitted to a :class:`GPUQueue`.
Create a command buffer using :func:`GPUCommandEncoder.finish`.
"""
class GPUCommandEncoder(GPUObject):
"""
A command encoder is used to record a series of commands. When done,
call :func:`finish` to obtain a GPUCommandBuffer object.
Create a command encoder using :func:`GPUDevice.create_command_encoder`.
"""
class GPUPipelineLayout(GPUObject):
"""
A pipeline layout describes the layout of a pipeline, as a list
of :class:`GPUBindGroupLayout` objects.
Create a pipeline layout using :func:`GPUDevice.create_pipeline_layout`.
"""
def __init__(self, label, internal, device, layouts):
super().__init__(label, internal, device)
self._layouts = tuple(layouts) # GPUBindGroupLayout objects
class GPUShaderModule(GPUObject):
"""
A shader module represents a programmable shader.
Create a shader module using :func:`GPUDevice.create_shader_module`.
"""
# wgpu.help('shadermodulecompilationinfo', dev=True)
# IDL: Promise compilationInfo();
def compilation_info(self):
""" Get shader compilation info. Always returns empty string at the moment.
"""
return []
# wgpu.help('shadermodulecompilationinfo', dev=True)
# IDL: Promise compilationInfo();
async def compilation_info_async(self):
format (TextureFormat): The texture format, e.g. "bgra8unorm-srgb".
usage (TextureUsage): Default ``TextureUsage.OUTPUT_ATTACHMENT``.
"""
# This was made a method of device to help decouple the canvas
# implementation from the wgpu API.
raise NotImplementedError()
def get_swap_chain_preferred_format(self, canvas):
""" Get the preferred swap chain format. In the WebGPU spec
this is a method of the canvas. In wgpu-py it's a method of the
device.
"""
return "bgra8unorm-srgb" # seems to be a good default
class GPUBuffer(GPUObject):
"""
A GPUBuffer represents a block of memory that can be used in GPU
operations. Data is stored in linear layout, meaning that each byte
of the allocation can be addressed by its offset from the start of
the buffer, subject to alignment restrictions depending on the
operation.
Create a buffer using :func:`GPUDevice.create_buffer`,
:func:`GPUDevice.create_buffer_mapped` or :func:`GPUDevice.create_buffer_mapped_async`.
One can sync data in a buffer by mapping it (or by creating a mapped
buffer) and then setting/getting the values in the mapped memoryview.
Alternatively, one can tell the GPU (via the command encoder) to
copy data between buffers and textures.
"""