// Pure-MoonBit native WebGPU bindings over the standard `webgpu.h` C ABI.
//
// `wgpu_stub.c` resolves wgpu-native / Dawn at runtime (zero link flags) and
// pumps `wgpuInstanceProcessEvents` so native callers see synchronous APIs.
///|
pub using @wgpu_types {
type WgpuError,
type TextureFormat,
type Topology,
type CullMode,
type FrontFace,
type VertexFormat,
type StepMode,
type VertexAttribute,
type VertexBufferLayout,
type IndexFormat,
type BufferUsage,
type TextureUsage,
type Filter,
type AddressMode,
type CompareOp,
type BlendFactor,
type BlendOp,
type BlendState,
type StencilOp,
type StencilFaceState,
type StencilState,
type DepthStencilState,
type SamplerDesc,
type LoadOp,
type StoreOp,
type Platform,
type PresentMode,
wgsl_words,
wgsl_from_words,
is_spirv_words,
}
// ---------------------------------------------------------------------------
// Native shim
// ---------------------------------------------------------------------------
extern "c" fn init_ffi() -> Int32 = "tycho_wgpu_init"
extern "c" fn last_error_bytes_ffi() -> Bytes = "tycho_wgpu_last_error_bytes"
#borrow(app_name)
extern "c" fn device_create_ffi(
app_name : Bytes,
len : Int,
validation : Int,
) -> UInt64 = "tycho_wgpu_device_create"
extern "c" fn device_destroy_ffi(device : UInt64) = "tycho_wgpu_device_destroy"
extern "c" fn device_wait_idle_ffi(device : UInt64) -> Int32 = "tycho_wgpu_device_wait_idle"
extern "c" fn device_validation_errors_ffi(device : UInt64) -> Int32 = "tycho_wgpu_device_validation_errors"
extern "c" fn device_lost_ffi(device : UInt64) -> Int32 = "tycho_wgpu_device_lost"
extern "c" fn device_backend_type_ffi(device : UInt64) -> Int32 = "tycho_wgpu_device_backend_type"
extern "c" fn device_max_texture_dimension_2d_ffi(device : UInt64) -> Int32 = "tycho_wgpu_device_max_texture_dimension_2d"
extern "c" fn device_min_uniform_offset_alignment_ffi(device : UInt64) -> Int32 = "tycho_wgpu_device_min_uniform_offset_alignment"
extern "c" fn device_adapter_name_ffi(device : UInt64) -> Bytes = "tycho_wgpu_device_adapter_name"
#borrow(code)
extern "c" fn shader_create_ffi(
device : UInt64,
code : Bytes,
len : Int,
) -> UInt64 = "tycho_wgpu_shader_create"
#borrow(words)
extern "c" fn shader_create_words_ffi(
device : UInt64,
words : FixedArray[UInt32],
word_count : Int,
) -> UInt64 = "tycho_wgpu_shader_create_words"
extern "c" fn shader_destroy_ffi(shader : UInt64) = "tycho_wgpu_shader_destroy"
#borrow(vs_entry, fs_entry, strides, attrs)
extern "c" fn render_pipeline_create_ffi(
device : UInt64,
vs : UInt64,
fs : UInt64,
vs_entry : Bytes,
vs_len : Int,
fs_entry : Bytes,
fs_len : Int,
format_code : Int,
topology_code : Int,
strides : FixedArray[Int],
stride_count : Int,
attrs : FixedArray[Int],
attr_count : Int,
sample_count : Int,
depth_format : Int,
depth_write : Bool,
depth_compare : Int,
depth_bias : Int,
depth_bias_slope : Double,
) -> UInt64 = "tycho_wgpu_render_pipeline_create"
extern "c" fn pipeline_destroy_ffi(pipeline : UInt64) = "tycho_wgpu_pipeline_destroy"
extern "c" fn texture_create_ffi(
device : UInt64,
width : Int,
height : Int,
format_code : Int,
usage_bits : Int,
samples : Int,
) -> UInt64 = "tycho_wgpu_texture_create"
extern "c" fn texture_destroy_ffi(texture : UInt64) = "tycho_wgpu_texture_destroy"
#borrow(data)
extern "c" fn texture_create_uploaded_ffi(
device : UInt64,
width : Int,
height : Int,
format_code : Int,
usage_bits : Int,
data : Bytes,
len : Int,
) -> UInt64 = "tycho_wgpu_texture_create_uploaded"
extern "c" fn texture_width_ffi(texture : UInt64) -> Int32 = "tycho_wgpu_texture_width"
extern "c" fn texture_height_ffi(texture : UInt64) -> Int32 = "tycho_wgpu_texture_height"
#borrow(dst)
extern "c" fn texture_readback_ffi(
device : UInt64,
texture : UInt64,
dst : Bytes,
dst_len : Int,
) -> Int32 = "tycho_wgpu_texture_readback"
extern "c" fn cmd_create_ffi(device : UInt64) -> UInt64 = "tycho_wgpu_cmd_create"
extern "c" fn cmd_begin_pass_ffi(
cmd : UInt64,
texture : UInt64,
load_op : Int,
r : Double,
g : Double,
b : Double,
a : Double,
depth_texture : UInt64,
depth_load_op : Int,
depth_clear : Double,
resolve_texture : UInt64,
) -> Int32 = "tycho_wgpu_cmd_begin_pass"
extern "c" fn cmd_set_pipeline_ffi(cmd : UInt64, pipeline : UInt64) = "tycho_wgpu_cmd_set_pipeline"
extern "c" fn cmd_draw_ffi(cmd : UInt64, vertices : Int, instances : Int) = "tycho_wgpu_cmd_draw"
extern "c" fn cmd_end_pass_ffi(cmd : UInt64) -> Int32 = "tycho_wgpu_cmd_end_pass"
extern "c" fn cmd_submit_ffi(device : UInt64, cmd : UInt64) -> Int32 = "tycho_wgpu_cmd_submit"
extern "c" fn cmd_destroy_ffi(cmd : UInt64) = "tycho_wgpu_cmd_destroy"
extern "c" fn sampler_create_ffi(
device : UInt64,
mag : Int,
min : Int,
mipmap : Int,
address : Int,
min_lod : Float,
max_lod : Float,
anisotropy : Float,
compare : Int,
) -> UInt64 = "tycho_wgpu_sampler_create"
extern "c" fn sampler_destroy_ffi(sampler : UInt64) = "tycho_wgpu_sampler_destroy"
extern "c" fn buffer_create_ffi(
device : UInt64,
size : Int,
usage_bits : Int,
) -> UInt64 = "tycho_wgpu_buffer_create"
#borrow(data)
extern "c" fn buffer_write_ffi(
buffer : UInt64,
offset : Int,
data : Bytes,
len : Int,
) -> Int32 = "tycho_wgpu_buffer_write"
#borrow(data)
extern "c" fn buffer_write_floats_ffi(
buffer : UInt64,
offset : Int,
data : FixedArray[Float],
count : Int,
) -> Int32 = "tycho_wgpu_buffer_write_floats"
extern "c" fn buffer_size_ffi(buffer : UInt64) -> Int32 = "tycho_wgpu_buffer_size"
extern "c" fn buffer_destroy_ffi(buffer : UInt64) = "tycho_wgpu_buffer_destroy"
extern "c" fn cmd_set_vertex_ffi(
cmd : UInt64,
slot : Int,
buffer : UInt64,
offset : Int,
) -> Int32 = "tycho_wgpu_cmd_set_vertex"
extern "c" fn cmd_set_index_ffi(
cmd : UInt64,
buffer : UInt64,
format_code : Int,
offset : Int,
size : Int,
) -> Int32 = "tycho_wgpu_cmd_set_index"
extern "c" fn cmd_draw_indexed_ffi(
cmd : UInt64,
index_count : Int,
instances : Int,
first_index : Int,
base_vertex : Int,
first_instance : Int,
) = "tycho_wgpu_cmd_draw_indexed"
extern "c" fn bind_group_begin_ffi(
pipeline : UInt64,
group_index : Int,
) -> UInt64 = "tycho_wgpu_bind_group_begin"
extern "c" fn bind_group_uniform_ffi(
group : UInt64,
binding : Int,
buffer : UInt64,
offset : Int,
size : Int,
) -> Int32 = "tycho_wgpu_bind_group_uniform"
extern "c" fn bind_group_texture_ffi(
group : UInt64,
binding : Int,
texture : UInt64,
) -> Int32 = "tycho_wgpu_bind_group_texture"
extern "c" fn bind_group_sampler_ffi(
group : UInt64,
binding : Int,
sampler : UInt64,
) -> Int32 = "tycho_wgpu_bind_group_sampler"
extern "c" fn bind_group_end_ffi(group : UInt64) -> UInt64 = "tycho_wgpu_bind_group_end"
extern "c" fn bind_group_cancel_ffi(group : UInt64) = "tycho_wgpu_bind_group_cancel"
extern "c" fn bind_group_destroy_ffi(group : UInt64) = "tycho_wgpu_bind_group_destroy"
extern "c" fn cmd_set_bind_group_ffi(
cmd : UInt64,
index : Int,
group : UInt64,
) -> Int32 = "tycho_wgpu_cmd_set_bind_group"
extern "c" fn surface_create_ffi(
device : UInt64,
platform : Int,
is_wayland : Int,
display : UInt64,
handle : UInt64,
) -> UInt64 = "tycho_wgpu_surface_create"
extern "c" fn surface_preferred_format_ffi(
surface : UInt64,
requested_code : Int,
) -> Int32 = "tycho_wgpu_surface_preferred_format"
extern "c" fn surface_configure_ffi(
surface : UInt64,
device : UInt64,
width : Int,
height : Int,
format_code : Int,
present_mode : Int,
copy_src : Int,
) -> Int32 = "tycho_wgpu_surface_configure"
extern "c" fn surface_acquire_ffi(surface : UInt64, device : UInt64) -> UInt64 = "tycho_wgpu_surface_acquire"
extern "c" fn surface_present_ffi(surface : UInt64) -> Int32 = "tycho_wgpu_surface_present"
extern "c" fn surface_destroy_ffi(surface : UInt64) = "tycho_wgpu_surface_destroy"
// ---------------------------------------------------------------------------
// Availability
// ---------------------------------------------------------------------------
///|
pub fn available() -> Bool {
init_ffi() != 0
}
///|
pub fn last_error() -> String {
@utf8.decode_lossy(last_error_bytes_ffi()[:])
}
// ---------------------------------------------------------------------------
// Device
// ---------------------------------------------------------------------------
///|
pub struct Device {
priv handle : UInt64
}
///|
pub fn Device::create(
app_name : String,
validation? : Bool = false,
) -> Device raise WgpuError {
if !available() {
raise WgpuError::InitFailed(last_error())
}
let name = @utf8.encode(app_name)
let handle = device_create_ffi(
name,
name.length(),
if validation { 1 } else { 0 },
)
if handle == (0 : UInt64) {
raise WgpuError::DeviceFailed(last_error())
}
{ handle, }
}
///|
pub fn Device::adapter_name(self : Device) -> String {
@utf8.decode_lossy(device_adapter_name_ffi(self.handle)[:])
}
///|
pub fn Device::backend_type(self : Device) -> Int {
device_backend_type_ffi(self.handle).to_int()
}
///|
pub fn Device::max_texture_dimension_2d(self : Device) -> Int {
device_max_texture_dimension_2d_ffi(self.handle).to_int()
}
///|
pub fn Device::min_uniform_buffer_offset_alignment(self : Device) -> Int {
device_min_uniform_offset_alignment_ffi(self.handle).to_int()
}
///|
pub fn Device::validation_errors(self : Device) -> Int {
device_validation_errors_ffi(self.handle).to_int()
}
///|
pub fn Device::is_lost(self : Device) -> Bool {
device_lost_ffi(self.handle) != 0
}
///|
pub fn Device::wait_idle(self : Device) -> Bool {
device_wait_idle_ffi(self.handle) != 0
}
///|
pub fn Device::destroy(self : Device) -> Unit {
device_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Shaders and pipelines
// ---------------------------------------------------------------------------
///|
pub struct Shader {
priv handle : UInt64
}
///|
pub fn Device::create_shader(
self : Device,
wgsl : String,
) -> Shader raise WgpuError {
let code = @utf8.encode(wgsl)
let handle = shader_create_ffi(self.handle, code, code.length())
if handle == (0 : UInt64) {
raise WgpuError::ShaderFailed(last_error())
}
{ handle, }
}
///|
pub fn Device::create_shader_from_words(
self : Device,
words : FixedArray[UInt32],
) -> Shader raise WgpuError {
if words.length() == 0 {
raise WgpuError::InvalidArgument("the WGSL word blob is empty")
}
let handle = shader_create_words_ffi(self.handle, words, words.length())
if handle == (0 : UInt64) {
raise WgpuError::ShaderFailed(last_error())
}
{ handle, }
}
///|
pub fn Shader::destroy(self : Shader) -> Unit {
shader_destroy_ffi(self.handle)
}
///|
pub struct Pipeline {
priv handle : UInt64
}
///|
pub fn Device::create_render_pipeline(
self : Device,
vs : Shader,
fs : Shader?,
vs_entry? : String = "vs_main",
fs_entry? : String = "fs_main",
format? : TextureFormat = TextureFormat::RGBA8,
topology? : Topology = Topology::TriangleList,
vertex_layouts? : Array[VertexBufferLayout] = [],
samples? : Int = 1,
depth_format? : TextureFormat? = None,
depth_write? : Bool = false,
depth_compare? : CompareOp = CompareOp::Always,
depth_bias? : Int = 0,
depth_bias_slope? : Double = 0.0,
) -> Pipeline raise WgpuError {
if samples != 1 && samples != 4 {
raise WgpuError::InvalidArgument("WebGPU accepts 1 or 4 samples per pixel")
}
let fs_handle = match fs {
Some(shader) => shader.handle
None => (0 : UInt64)
}
let vs_name = @utf8.encode(vs_entry)
let fs_name = @utf8.encode(fs_entry)
let depth_code = match depth_format {
Some(format) => format.code()
None => -1
}
let strides = FixedArray::make(vertex_layouts.length(), 0)
let mut attr_count = 0
for i = 0; i < vertex_layouts.length(); i = i + 1 {
strides[i] = vertex_layouts[i].stride
attr_count = attr_count + vertex_layouts[i].attributes.length()
}
let attrs = FixedArray::make(attr_count * 5, 0)
let mut cursor = 0
for slot = 0; slot < vertex_layouts.length(); slot = slot + 1 {
let layout = vertex_layouts[slot]
for attribute in layout.attributes {
attrs[cursor * 5] = attribute.location
attrs[cursor * 5 + 1] = attribute.format.code()
attrs[cursor * 5 + 2] = slot
attrs[cursor * 5 + 3] = attribute.offset
attrs[cursor * 5 + 4] = layout.step_mode.code()
cursor = cursor + 1
}
}
let handle = render_pipeline_create_ffi(
self.handle,
vs.handle,
fs_handle,
vs_name,
vs_name.length(),
fs_name,
fs_name.length(),
format.code(),
topology.code(),
strides,
strides.length(),
attrs,
cursor,
samples,
depth_code,
depth_write,
depth_compare.code(),
depth_bias,
depth_bias_slope,
)
if handle == (0 : UInt64) {
raise WgpuError::PipelineFailed(last_error())
}
{ handle, }
}
///|
pub fn Pipeline::destroy(self : Pipeline) -> Unit {
pipeline_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Textures
// ---------------------------------------------------------------------------
///|
pub struct Texture {
priv handle : UInt64
}
///|
pub fn Device::create_texture(
self : Device,
width : Int,
height : Int,
format? : TextureFormat = TextureFormat::RGBA8,
usage? : TextureUsage = TextureUsage::render_attachment()
.combine(TextureUsage::copy_src())
.combine(TextureUsage::sampled()),
samples? : Int = 1,
) -> Texture raise WgpuError {
if width <= 0 || height <= 0 {
raise WgpuError::InvalidArgument(
"texture size must be positive, got \{width}x\{height}",
)
}
if samples != 1 && samples != 4 {
raise WgpuError::InvalidArgument("WebGPU accepts 1 or 4 samples per pixel")
}
let handle = texture_create_ffi(
self.handle,
width,
height,
format.code(),
usage.bits(),
samples,
)
if handle == (0 : UInt64) {
raise WgpuError::TextureFailed(last_error())
}
{ handle, }
}
///|
pub fn Device::create_texture_with_data(
self : Device,
width : Int,
height : Int,
format? : TextureFormat = TextureFormat::RGBA8,
usage? : TextureUsage = TextureUsage::sampled().combine(
TextureUsage::copy_dst(),
),
data? : Bytes = b"",
) -> Texture raise WgpuError {
if width <= 0 || height <= 0 {
raise WgpuError::InvalidArgument("texture size must be positive")
}
let handle = texture_create_uploaded_ffi(
self.handle,
width,
height,
format.code(),
usage.bits(),
data,
data.length(),
)
if handle == (0 : UInt64) {
raise WgpuError::TextureFailed(last_error())
}
{ handle, }
}
///|
pub fn Texture::width(self : Texture) -> Int {
texture_width_ffi(self.handle).to_int()
}
///|
pub fn Texture::height(self : Texture) -> Int {
texture_height_ffi(self.handle).to_int()
}
///|
pub fn Texture::readback(
self : Texture,
device : Device,
) -> Bytes raise WgpuError {
let total = self.width() * self.height() * 4
let out = Bytes::makei(total, fn(_i) { (0 : Byte) })
if texture_readback_ffi(device.handle, self.handle, out, total) == 0 {
raise WgpuError::ReadbackFailed(last_error())
}
out
}
///|
pub fn Texture::destroy(self : Texture) -> Unit {
texture_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Samplers
// ---------------------------------------------------------------------------
///|
pub struct Sampler {
priv handle : UInt64
}
///|
pub fn Device::create_sampler(
self : Device,
desc? : SamplerDesc = SamplerDesc::new(),
) -> Sampler raise WgpuError {
let compare = match desc.compare {
Some(op) => op.code()
None => -1
}
let handle = sampler_create_ffi(
self.handle,
desc.mag.code(),
desc.min.code(),
desc.mipmap.code(),
desc.address.code(),
desc.min_lod,
desc.max_lod,
desc.anisotropy,
compare,
)
if handle == (0 : UInt64) {
raise WgpuError::DeviceFailed(last_error())
}
{ handle, }
}
///|
pub fn Sampler::destroy(self : Sampler) -> Unit {
sampler_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Buffers
// ---------------------------------------------------------------------------
///|
pub struct Buffer {
priv handle : UInt64
}
///|
pub fn Device::create_buffer(
self : Device,
size : Int,
usage? : BufferUsage = BufferUsage::vertex(),
) -> Buffer raise WgpuError {
if size <= 0 {
raise WgpuError::InvalidArgument(
"buffer size must be positive, got \{size}",
)
}
let handle = buffer_create_ffi(self.handle, size, usage.bits())
if handle == (0 : UInt64) {
raise WgpuError::BufferFailed(last_error())
}
{ handle, }
}
///|
pub fn Buffer::write(
self : Buffer,
data : Bytes,
offset? : Int = 0,
) -> Unit raise WgpuError {
if data.length() == 0 {
raise WgpuError::InvalidArgument("nothing to upload")
}
if buffer_write_ffi(self.handle, offset, data, data.length()) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn Buffer::size(self : Buffer) -> Int {
buffer_size_ffi(self.handle).to_int()
}
///|
pub fn Buffer::write_floats(
self : Buffer,
data : FixedArray[Float],
offset? : Int = 0,
) -> Unit raise WgpuError {
if data.length() == 0 {
raise WgpuError::InvalidArgument("nothing to upload")
}
if buffer_write_floats_ffi(self.handle, offset, data, data.length()) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn Buffer::destroy(self : Buffer) -> Unit {
buffer_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Bind groups
// ---------------------------------------------------------------------------
///|
pub(all) enum BindEntry {
Uniform(Int, Buffer, Int, Int)
Texture(Int, Texture)
Sampler(Int, Sampler)
}
///|
pub fn BindEntry::uniform(
binding : Int,
buffer : Buffer,
offset? : Int = 0,
size? : Int = 0,
) -> BindEntry {
Uniform(binding, buffer, offset, size)
}
///|
pub fn BindEntry::texture(binding : Int, texture : Texture) -> BindEntry {
Texture(binding, texture)
}
///|
pub fn BindEntry::sampler(binding : Int, sampler : Sampler) -> BindEntry {
Sampler(binding, sampler)
}
///|
pub struct BindGroup {
priv handle : UInt64
}
///|
pub fn Pipeline::create_bind_group(
self : Pipeline,
entries : Array[BindEntry],
) -> BindGroup raise WgpuError {
let builder = bind_group_begin_ffi(self.handle, 0)
if builder == (0 : UInt64) {
raise WgpuError::PipelineFailed(last_error())
}
for entry in entries {
let ok = match entry {
Uniform(binding, buffer, offset, size) =>
bind_group_uniform_ffi(builder, binding, buffer.handle, offset, size)
Texture(binding, texture) =>
bind_group_texture_ffi(builder, binding, texture.handle)
Sampler(binding, sampler) =>
bind_group_sampler_ffi(builder, binding, sampler.handle)
}
if ok == 0 {
bind_group_cancel_ffi(builder)
raise WgpuError::InvalidArgument(last_error())
}
}
let handle = bind_group_end_ffi(builder)
if handle == (0 : UInt64) {
raise WgpuError::PipelineFailed(last_error())
}
{ handle, }
}
///|
pub fn BindGroup::destroy(self : BindGroup) -> Unit {
bind_group_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Command recording
// ---------------------------------------------------------------------------
///|
pub struct CommandList {
priv handle : UInt64
}
///|
pub fn Device::create_command_list(
self : Device,
) -> CommandList raise WgpuError {
let handle = cmd_create_ffi(self.handle)
if handle == (0 : UInt64) {
raise WgpuError::CommandFailed(last_error())
}
{ handle, }
}
///|
pub fn CommandList::begin_pass(
self : CommandList,
target : Texture,
load_op? : LoadOp = LoadOp::Clear,
r? : Double = 0.0,
g? : Double = 0.0,
b? : Double = 0.0,
a? : Double = 1.0,
depth? : Texture? = None,
depth_load_op? : LoadOp = LoadOp::Clear,
depth_clear? : Double = 1.0,
resolve? : Texture? = None,
) -> Unit raise WgpuError {
let depth_handle = match depth {
Some(texture) => texture.handle
None => (0 : UInt64)
}
let resolve_handle = match resolve {
Some(texture) => texture.handle
None => (0 : UInt64)
}
if cmd_begin_pass_ffi(
self.handle,
target.handle,
load_op.code(),
r,
g,
b,
a,
depth_handle,
depth_load_op.code(),
depth_clear,
resolve_handle,
) ==
0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn CommandList::set_pipeline(
self : CommandList,
pipeline : Pipeline,
) -> Unit {
cmd_set_pipeline_ffi(self.handle, pipeline.handle)
}
///|
pub fn CommandList::draw(
self : CommandList,
vertices : Int,
instances? : Int = 1,
) -> Unit {
cmd_draw_ffi(self.handle, vertices, instances)
}
///|
pub fn CommandList::set_bind_group(
self : CommandList,
index : Int,
group : BindGroup,
) -> Unit raise WgpuError {
if cmd_set_bind_group_ffi(self.handle, index, group.handle) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn CommandList::set_vertex(
self : CommandList,
slot : Int,
buffer : Buffer,
offset? : Int = 0,
) -> Unit raise WgpuError {
if slot < 0 {
raise WgpuError::InvalidArgument("vertex slot must not be negative")
}
if cmd_set_vertex_ffi(self.handle, slot, buffer.handle, offset) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn CommandList::set_index(
self : CommandList,
buffer : Buffer,
format : IndexFormat,
offset? : Int = 0,
size? : Int = 0,
) -> Unit raise WgpuError {
if cmd_set_index_ffi(self.handle, buffer.handle, format.code(), offset, size) ==
0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn CommandList::draw_indexed(
self : CommandList,
index_count : Int,
instances? : Int = 1,
first_index? : Int = 0,
base_vertex? : Int = 0,
first_instance? : Int = 0,
) -> Unit {
cmd_draw_indexed_ffi(
self.handle,
index_count,
instances,
first_index,
base_vertex,
first_instance,
)
}
///|
pub fn CommandList::end_pass(self : CommandList) -> Unit raise WgpuError {
if cmd_end_pass_ffi(self.handle) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn Device::submit(
self : Device,
cmd : CommandList,
) -> Unit raise WgpuError {
if cmd_submit_ffi(self.handle, cmd.handle) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn CommandList::destroy(self : CommandList) -> Unit {
cmd_destroy_ffi(self.handle)
}
// ---------------------------------------------------------------------------
// Surfaces (window presentation)
// ---------------------------------------------------------------------------
///|
pub struct Surface {
priv handle : UInt64
}
///|
pub fn Surface::create(
device : Device,
platform : Platform,
is_wayland : Bool,
display : UInt64,
handle : UInt64,
) -> Surface raise WgpuError {
if !available() {
raise WgpuError::InitFailed(last_error())
}
let raw = surface_create_ffi(
device.handle,
platform.code(),
if is_wayland { 1 } else { 0 },
display,
handle,
)
if raw == (0 : UInt64) {
raise WgpuError::DeviceFailed(last_error())
}
{ handle: raw }
}
///|
pub fn Surface::preferred_format(
self : Surface,
requested : TextureFormat,
) -> TextureFormat {
TextureFormat::from_code(
surface_preferred_format_ffi(self.handle, requested.code()).to_int(),
)
}
///|
pub fn Surface::configure(
self : Surface,
device : Device,
width : Int,
height : Int,
format : TextureFormat,
present_mode? : PresentMode = PresentMode::Fifo,
copy_src? : Bool = false,
) -> Unit raise WgpuError {
if width <= 0 || height <= 0 {
raise WgpuError::InvalidArgument("surface size must be positive")
}
let ok = surface_configure_ffi(
self.handle,
device.handle,
width,
height,
format.code(),
present_mode.code(),
if copy_src { 1 } else { 0 },
)
if ok == 0 {
raise WgpuError::CommandFailed("configuring the WebGPU surface failed")
}
}
///|
pub fn Surface::acquire(
self : Surface,
device : Device,
) -> Texture raise WgpuError {
let raw = surface_acquire_ffi(self.handle, device.handle)
if raw == (0 : UInt64) {
raise WgpuError::CommandFailed(last_error())
}
{ handle: raw }
}
///|
pub fn Surface::present(self : Surface) -> Unit raise WgpuError {
if surface_present_ffi(self.handle) == 0 {
raise WgpuError::CommandFailed(last_error())
}
}
///|
pub fn Surface::destroy(self : Surface) -> Unit {
surface_destroy_ffi(self.handle)
}