moonbitstack/moonwgpu/core does not have a README file

    Refused

    pub suberror Refused {
    Validation(kind~ :
    ErrorKind
    , at~ : String, want~ : String, got~ : String)
    OutOfMemory(at~ : String, limit~ : Int, want~ : Int)
    DeviceLost(reason~ :
    DeviceLostReason
    )
    Internal(message~ : String)
    }

    Why a call was refused.

    The specification generates these on the device timeline and delivers them to an error scope; this library runs the device timeline in the caller's thread and raises them, which is the same information at the same point, without a callback. The kind is the class the specification names, so a caller can branch on it exactly as a browser test does.

    Refused::from_hal

    Turn a backend's complaint into the error the caller sees.

    Refused::message

    fn Refused::message(self : Refused) -> String

    The message a caller sees, carrying the same three fields the specification's own messages carry.

    Refused::pipeline_reason

    How a refusal reads to a caller of createRenderPipelineAsync and its compute twin: GPUPipelineError carries a reason, and the two classes that can resolve that promise are the ones this reports.

    BindGroupLayoutState

    The state of one bind group layout.

    BindGroupState

    The state of one bind group.

    BufferState

    pub struct BufferState {
    descriptor :
    BufferDescriptor

    destroyed : Bool
    mapped : Bool
    }

    The state of one buffer as the device tracks it.

    ErrorScope

    pub struct ErrorScope {
    filter :
    ErrorFilter

    errors : Array[Refused]
    }

    One pushErrorScope: the filter it was pushed with and what it collected.

    ExternalTextureState

    pub struct ExternalTextureState {
    descriptor :
    ExternalTextureDescriptor

    destroyed : Bool
    }

    An external texture as the device keeps it.

    Frame

    pub struct Frame {
    commands : Array[
    Command
    ]
    open : Bool
    finished : Bool
    }

    What one CommandEncoder has recorded so far.

    The frame is a small state machine: a pass has to be ended before the next command, and a finished encoder records nothing more. Both are rules of the specification that a caller notices immediately, so both are refused at once rather than at submit.

    Frame::check

    fn Frame::check(self : Frame, at : String) -> Unit raise Refused

    Refuse a command that arrives while a pass is open or after the encoder has finished. Debug markers and other commands with no recorded effect still have to obey these two rules.

    Frame::close_pass

    fn Frame::close_pass(self : Frame, command :
    Command
    ) -> Unit raise Refused

    Mark the end of a pass and keep its recorded commands.

    Frame::finish

    The command list, once the encoder is finished.

    Frame::new

    fn Frame::new() -> Frame

    An encoder that has recorded nothing.

    Frame::open_pass

    fn Frame::open_pass(self : Frame) -> Unit raise Refused

    Mark the start of a pass.

    Frame::record

    fn Frame::record(self : Frame, command :
    Command
    ) -> Unit raise Refused

    Record a command that stands on its own, such as a copy or a clear.

    Hub

    pub struct Hub {
    backend : &
    Hal

    limits :
    Limits

    features :
    Features

    buffers : Map[UInt64, BufferState]
    textures : Map[UInt64, TextureState]
    views : Map[UInt64, ViewState]
    samplers : Map[UInt64, SamplerState]
    shader_modules : Map[UInt64, ShaderModuleState]
    bind_group_layouts : Map[UInt64, BindGroupLayoutState]
    bind_groups : Map[UInt64, BindGroupState]
    pipeline_layouts : Map[UInt64, PipelineLayoutState]
    render_pipelines : Map[UInt64,
    RenderPipelineDescriptor
    ]
    compute_pipelines : Map[UInt64,
    ComputePipelineDescriptor
    ]
    query_sets : Map[UInt64,
    QuerySetDescriptor
    ]
    external_textures : Map[UInt64, ExternalTextureState]
    lost : Bool
    scopes : Array[ErrorScope]
    uncaptured : Array[Refused]
    handler : (Refused) -> Unit?
    }

    The device state: every resource it has made, the limits it was created with, and the one place that talks to the backend.

    The specification keeps this in the device object; a MoonBit struct with a trait object in it does the same job, and lets Instance and Device be thin handles a caller can copy freely.

    Hub::backend_name

    fn Hub::backend_name(self : Hub) -> String

    The backend this hub drives, for the odd caller that needs to name it.

    Hub::bind_group_layout

    A bind group layout this device made.

    Hub::buffer

    Look up a buffer the caller still holds.

    Hub::clear_uncaptured_errors

    fn Hub::clear_uncaptured_errors(self : Hub) -> Unit

    Forget the errors kept so far.

    Hub::compilation_info

    getCompilationInfo: what the backend's compiler said about a module.

    A backend without a compiler reports no message, which the trait documents; the module is then only as good as the caller's own checking. https://www.w3.org/TR/webgpu/#dom-gpushadermodule-getcompilationinfo

    Hub::compute_pipeline_layout

    The explicit layout a compute pipeline was built with, if it had one.

    Hub::copy_external_image_to_texture

    copyExternalImageToTexture: put the pixels of a frame into a texture, turning them over when the host's rows are bottom-first, and premultiplying alpha when the caller asks for it.

    Colour space conversion is the host's: the bytes are copied as they are, and the colour space is recorded with them. https://www.w3.org/TR/webgpu/#dom-gpuqueue-copyexternalimagetotexture

    Hub::destroy_buffer

    fn Hub::destroy_buffer(self : Hub, id :
    BufferId
    ) -> Unit

    Hub::destroy_external_texture

    fn Hub::destroy_external_texture(self : Hub, id :
    ExternalTextureId
    ) -> Unit

    Release an external texture. A frame a host has moved on from is released the same way.

    Hub::destroy_query_set

    fn Hub::destroy_query_set(self : Hub, id :
    QuerySetId
    ) -> Unit

    Forget a query set.

    Hub::destroy_texture

    fn Hub::destroy_texture(self : Hub, id :
    TextureId
    ) -> Unit

    Hub::ensure_alive

    fn Hub::ensure_alive(self : Hub) -> Unit raise Refused

    Refuse everything once the device is gone, the way a lost device does.

    Hub::external_texture

    An external texture this device imported and the caller has not released.

    Hub::features

    Hub::handler

    fn Hub::handler(self : Hub) -> (Refused) -> Unit?

    The handler an uncaptured error is handed to, if one is set.

    Hub::import_external_texture

    importExternalTexture: take a decoded frame and hold it as a texture.

    The bytes must be there in full: the frame is copied when it is imported, so that a host may reuse its buffer as soon as this call returns. https://www.w3.org/TR/webgpu/#dom-gpudevice-importexternaltexture

    Hub::limits

    Hub::lose

    fn Hub::lose(self : Hub) -> Unit

    Mark the device as gone: every later call is refused.

    Hub::new

    A hub over one backend, holding the limits and features a device was created with.

    Hub::pipeline_layout

    A pipeline layout this device made.

    Hub::pop_error_scope

    fn Hub::pop_error_scope(self : Hub) -> Array[Refused] raise Refused

    Pop the innermost scope, handing back what it collected. https://www.w3.org/TR/webgpu/#dom-gpudevice-poperrorscope

    Hub::push_error_scope

    fn Hub::push_error_scope(self : Hub, filter :
    ErrorFilter
    ) -> Unit

    Push an error scope. Errors the device generates itself - an allocation the host could not make, or a backend that fails - are collected here; errors about the caller's descriptors are raised instead, because this library runs the device timeline in the caller's thread and a raised error carries the same information at the same point. https://www.w3.org/TR/webgpu/#dom-gpudevice-pusherrorscope

    Hub::read_buffer

    fn Hub::read_buffer(self : Hub, id :
    BufferId
    , offset : Int, size : Int) -> Bytes raise Refused

    Read back size bytes at offset. The caller has checked that the buffer is mapped for reading; this is the native form of getMappedRange.

    Hub::render_pipeline_layout

    The explicit layout a render pipeline was built with, if it had one.

    Hub::report

    fn Hub::report(self : Hub, error : Refused) -> Refused

    Record an error in the innermost scope that accepts its filter; an error no scope asked for is the device's own and is kept for uncapturederror.

    A caller in MoonBit gets a refusal where it was raised, so this is only called where the specification's error model has to be honoured instead: by the device for an error a host reported, and by a binding that has to turn a raised refusal into one popErrorScope can return.

    Hub::set_handler

    fn Hub::set_handler(self : Hub, handler : (Refused) -> Unit?) -> Unit

    Set, or with None clear, the handler an uncaptured error is handed to.

    Hub::submit

    fn Hub::submit(self : Hub, commands : Array[
    Command
    ]) -> Unit raise Refused

    Check every command of a finished command buffer, then hand the batch to the backend. Nothing has touched the host until the whole batch is legal.

    Hub::texture

    Look up a texture the caller still holds.

    Hub::uncaptured_errors

    fn Hub::uncaptured_errors(self : Hub) -> Array[Refused]

    The errors no error scope collected, oldest first.

    Hub::unmap_write

    fn Hub::unmap_write(self : Hub, id :
    BufferId
    , offset : Int, data : Bytes) -> Unit raise Refused

    Write back what a write mapping was given, when the mapping is dropped.

    This is not write_buffer: a buffer mapped for writing carries MAP_WRITE and may carry nothing else, so asking for COPY_DST here would refuse the only write the buffer was ever meant to take. The range was checked when the mapping was taken; this checks it again against the same buffer, since the mapping outlives any amount of caller code.

    Hub::write_buffer

    fn Hub::write_buffer(self : Hub, id :
    BufferId
    , offset : Int, data : Bytes) -> Unit raise Refused

    Write into a buffer, checking that it has copy_dst and that the range fits. This is queue.writeBuffer: the write happens before the next submit.

    Hub::write_texture

    queue.writeTexture: the same region rules as a buffer-to-texture copy, without the 256-byte row alignment a copy buffer needs. https://www.w3.org/TR/webgpu/#dom-gpuqueue-writetexture

    PipelineLayoutState

    The state of one pipeline layout.

    SamplerState

    pub struct SamplerState {
    descriptor :
    SamplerDescriptor

    filtering : Bool
    comparison : Bool
    }

    The state of one sampler.

    ShaderModuleState

    The state of one shader module.

    TextureState

    pub struct TextureState {
    descriptor :
    TextureDescriptor

    destroyed : Bool
    }

    The state of one texture.

    UsageScope

    pub struct UsageScope {
    buffers : Map[UInt64, UInt]
    textures : Map[UInt64, UInt]
    }

    The usages of the subresources one pass touches.

    The specification validates usage compatibility per pass: a subresource used writably in a pass cannot be used at all anywhere else in that same pass, and one used read-only can only be read again. Copies and clears between passes are separate steps and are not compared with each other. https://www.w3.org/TR/webgpu/#programming-model "Usage scopes"

    UsageScope::new

    fn UsageScope::new() -> UsageScope

    A scope that has touched nothing yet.

    UsageScope::use_buffer

    Record a use of one buffer in this scope, refusing a conflict.

    UsageScope::use_texture

    Record a use of one texture in this scope, refusing a conflict.

    ViewState

    The state of one texture view: what it resolves to, so that a bind group can be checked against a layout without asking the backend.

    COPY_BYTES_PER_ROW_ALIGNMENT

    let COPY_BYTES_PER_ROW_ALIGNMENT : Int

    The 256-byte line a buffer-to-texture copy pads every row to. The copy APIs require it so that a backend can hand the buffer to the hardware row by row.

    array_layer_count_of_texture

    fn array_layer_count_of_texture(texture :
    TextureDescriptor
    ) -> Int

    A 1d or 3d texture has one array layer; a 2d texture has one per element of depthOrArrayLayers.

    buffer_usage_names

    fn buffer_usage_names() -> Array[(String, UInt)]

    The bit names of GPUBufferUsage, for the got field of a refusal.

    immediates

    fn immediates(range_offset : Int, data : Bytes, data_offset : Int, data_size : Int?) ->
    SetImmediates
    raise Refused

    The arguments of setImmediates, checked and taken as one run of bytes.

    The specification loads the bytes into a range of the immediate constants of the bind group that was bound before the draw or dispatch; both the range and the bytes are counted in four-byte words, which is the alignment hardware keeps them in. https://www.w3.org/TR/webgpu/#dom-gpubindingcommandsmixin-setimmediates

    invalid

    fn invalid(kind :
    ErrorKind
    , at : String, want : String, got : String) -> Refused

    A validation error about one place in one descriptor.

    require

    fn require(condition : Bool, kind :
    ErrorKind
    , at : String, want : String, got : String) -> Unit raise Refused

    Raise kind unless condition holds.

    require_int

    fn require_int(condition : Bool, kind :
    ErrorKind
    , at : String, want : Int, got : Int) -> Unit raise Refused

    Raise kind unless condition holds, formatting the two values.

    require_same

    fn require_same(want : Int, got : Int, kind :
    ErrorKind
    , at : String) -> Unit raise Refused

    Raise kind unless want and got are the same.

    resolve_aspect_format

    The format of one aspect of a texture format: a single-aspect view of a depth-stencil format has the aspect's own format.

    texture_usage_names

    fn texture_usage_names() -> Array[(String, UInt)]

    The bit names of GPUTextureUsage, for the got field of a refusal.

    validate_bind_group_descriptor

    fn validate_bind_group_descriptor(descriptor :
    BindGroupDescriptor
    , layout : BindGroupLayoutState, buffers : Map[UInt64, BufferState], views : Map[UInt64, ViewState], textures : Map[UInt64, TextureState], samplers : Map[UInt64, SamplerState], limits :
    Limits
    ) -> Unit raise Refused

    The rules a bind group must satisfy against its layout. https://www.w3.org/TR/webgpu/#bind-group-creation

    validate_bind_group_layout_descriptor

    The rules a whole bind group layout must satisfy.

    validate_buffer_descriptor

    The rules a GPUBufferDescriptor must satisfy before a buffer exists. https://www.w3.org/TR/webgpu/#dom-gpudevice-createbuffer "device timeline initialization steps".

    validate_buffer_range

    fn validate_buffer_range(size : Int, offset : Int, length : Int, at : String) -> Unit raise Refused

    The range a copy or write addresses has to be inside the buffer, and its offset aligned to four bytes. https://www.w3.org/TR/webgpu/#buffer-copies

    validate_buffer_to_texture_copy

    fn validate_buffer_to_texture_copy(buffer : BufferState, layout :
    TexelCopyBufferLayout
    , texture : TextureState, destination :
    TexelCopyTextureInfo
    , size :
    Extent3d
    , bytes_per_row_alignment : Int, at : String) -> Unit raise Refused

    copyBufferToTexture and writeTexture share these rules; only the row alignment differs, which the caller passes in.

    validate_compute_pipeline_descriptor

    The rules a GPUComputePipelineDescriptor must satisfy.

    validate_copy_buffer_to_buffer

    fn validate_copy_buffer_to_buffer(source : BufferState, source_offset : Int, destination : BufferState, destination_offset : Int, size : Int) -> Unit raise Refused

    copyBufferToBuffer: both offsets and the size are multiples of four and the two ranges are inside their buffers. https://www.w3.org/TR/webgpu/#dom-gpucommandencoder-copybuffertobuffer

    validate_pipeline_layout_descriptor

    The rules a pipeline layout must satisfy.

    validate_render_pipeline_descriptor

    The rules a GPURenderPipelineDescriptor must satisfy.

    A pipeline whose shaders were compiled would also be checked against what the entry points declare. That needs a WGSL front end; this layer checks everything the descriptor itself decides, which is where most of the specification's rules live. https://www.w3.org/TR/webgpu/#dom-gpudevice-createrenderpipeline

    validate_sampler_descriptor

    fn validate_sampler_descriptor(descriptor :
    SamplerDescriptor
    ) -> Unit raise Refused

    The rules a GPUSamplerDescriptor must satisfy. https://www.w3.org/TR/webgpu/#dom-gpudevice-createsampler

    validate_texture_descriptor

    The rules a GPUTextureDescriptor must satisfy. https://www.w3.org/TR/webgpu/#dom-gpudevice-createtexture "validating GPUTextureDescriptor".

    validate_texture_to_buffer_copy

    copyTextureToBuffer.

    validate_texture_to_texture_copy

    copyTextureToTexture: both sides have to agree on the texel size and the aspect, and both have to carry the usage the direction needs.

    validate_texture_view_descriptor

    The rules a GPUTextureViewDescriptor must satisfy against the texture it views. https://www.w3.org/TR/webgpu/#dom-gputexture-createview "device timeline initialization steps".