WebGPU Library for Lily
Render passes, shaders and buffers for WebGPU.
Structs
ShaderTag
struct ShaderTag {
}
ImageTag
struct ImageTag {
}
Range
struct Range {
count: Int
start: Int
}
Viewport
struct Viewport {
height: F32
max_depth: F32
min_depth: F32
width: F32
x: F32
y: F32
}
Viewport for rendering to a region of the render target
ScissorRect
struct ScissorRect {
height: Int
width: Int
x: Int
y: Int
}
Scissor rectangle for clipping rendering to a region
StencilReference
struct StencilReference {
value: Int
}
Stencil reference value for stencil testing
SamplerConfig
struct SamplerConfig {
address_mode: AddressMode
mag_filter: FilterMode
min_filter: FilterMode
}
Sampler configuration for texture sampling
RenderPipelineOptions
struct RenderPipelineOptions {
blend_mode: BlendMode
cull_mode: CullMode
depth_bias_clamp: F32
depth_bias_constant: Int
depth_bias_slope_scale: F32
depth_compare: CompareFunction
depth_format: DepthTextureFormat
depth_write_enabled: Bool
enable_depth: Bool
topology: PrimitiveTopology
}
Options for creating render pipeline.
SetBindGroup
struct SetBindGroup {
group: BindGroupHandle
index: Int
}
DrawIndexed
struct DrawIndexed {
index: Range
instance: Range
}
Draw
struct Draw {
instance: Range
vertex: Range
}
SetIndexBuffer
struct SetIndexBuffer {
buffer: BufferHandle
}
SetVertexBuffer
struct SetVertexBuffer {
buffer: BufferHandle
slot: Int
}
ClearColor
struct ClearColor {
a: F32
b: F32
g: F32
r: F32
}
Clear Color for a render pass
ColorAttachment
struct ColorAttachment {
clear_color: ClearColor
should_clear: Bool
view: TextureViewHandle
}
Describes a color attachment for rendering
RenderPass
struct RenderPass {
Color attachments to render to. If empty, renders to screen
color_attachments: Vec<ColorAttachment, 8>
Optional depth attachment. Set to -1 for none. TODO: Should probably be proper handle or optional?
depth_attachment: Int
Clear depth value when depth_should_clear is true
depth_clear_value: F32
depth_should_clear: Bool
If true, store depth results after the pass
depth_store: Bool
entries: Vec<Entry, 2048>
}
Builder for recording draw commands for a render pass.
Configure attachments, then record state changes and draw calls in order.
Submit the finished pass with [add_pass].
Member Functions
add_color_attachment
fn RenderPass::add_color_attachment(
self,
attachment: ColorAttachment
)
Adds a [ColorAttachment] to render into.
If no color attachments are set, the pass renders to the screen.
set_depth_attachment
fn RenderPass::set_depth_attachment(
self,
depth_view: TextureViewHandle
)
Sets the depth attachment for depth testing using a [TextureViewHandle].
set_depth_ops
fn RenderPass::set_depth_ops(
self,
should_clear: Bool,
clear_value: F32,
store: Bool
)
Configures depth load/store.
set_pipeline
fn RenderPass::set_pipeline(
self,
pipeline: RenderPipelineHandle
)
Sets the active [RenderPipelineHandle] for upcoming draw calls.
set_viewport
fn RenderPass::set_viewport(
self,
viewport: Viewport
)
Sets the [Viewport] for future draw calls.
set_scissor_rect
fn RenderPass::set_scissor_rect(
self,
rect: ScissorRect
)
Clips rendering to a [ScissorRect].
set_stencil_reference
fn RenderPass::set_stencil_reference(
self,
reference_value: Int
)
Sets the stencil reference value for stencil testing.
set_bind_group
fn RenderPass::set_bind_group(
self,
group_index: Int,
bind_group: BindGroupHandle
)
Binds a [BindGroupHandle] at group_index for future draw calls.
group_index is the bind group slot, the @group(N) in WGSL and the
position of that group’s layout in the array passed to [create_pipeline_layout].
set_vertex_buffer
fn RenderPass::set_vertex_buffer(
self,
slot: Int,
vertex_buffer: BufferHandle
)
Sets the [BufferHandle] at vertex buffer slot.
slot is the index of the [VertexBufferLayout] in
[create_render_pipeline].
set_index_buffer
fn RenderPass::set_index_buffer(
self,
index_buffer: BufferHandle
)
Sets the index [BufferHandle] for indexed drawing.
Only buffers created with [create_index_buffer_u16] are supported.
draw
fn RenderPass::draw(
self,
vertex_range: Range,
instance_range: Range
)
Draws vertices without an index buffer. Prefer [draw_indexed] if possible.
vertex_range selects vertices by [Range] (start and count).
instance_range selects instances the same way. Use count: 1 for a single draw.
draw_indexed
fn RenderPass::draw_indexed(
self,
index_range: Range,
instance_range: Range
)
Draws indexed geometry using the index buffer set by [RenderPass::set_index_buffer].
index_range selects indices by [Range] (start and ocunt).
instance_range selects instances the same way. Use count: 1 for a single draw.
StorageTextureBindingType
struct StorageTextureBindingType {
access: StorageTextureAccess
format: StorageTextureFormat
}
Configuration for a storage texture binding in a bind group layout.
TODO: Texture bindings use D2 dimensions only for now. @catnipped will determine when cube maps, texture arrays, or 3D volumes are needed.
BindGroupLayoutEntry
struct BindGroupLayoutEntry {
Binding index within the group. Matches @binding(N) in WGSL.
Entries must use bindings 0, 1, 2, in array order.
binding: Int
ty: BindingType
}
One resource slot in a bind group layout.
VertexAttribute
struct VertexAttribute {
format: VertexFormat
Vertex input shader location. Matches @location(N) in the vertex shader.
location: Int
offset: Int
}
VertexBufferLayout
struct VertexBufferLayout {
array_stride: Int
step_mode: VertexStepMode
vertex_attribute: Block<VertexAttribute, 32>
vertex_attribute_count: Int
}
Distinct Types
Shader
type Shader = Res<ShaderTag>
Image
type Image = Res<ImageTag>
BufferHandle
type BufferHandle = Int
SamplerHandle
type SamplerHandle = Int
TextureHandle
type TextureHandle = Int
TextureViewHandle
type TextureViewHandle = Int
BindGroupHandle
type BindGroupHandle = Int
BindGroupLayoutHandle
type BindGroupLayoutHandle = Int
PipelineLayoutHandle
type PipelineLayoutHandle = Int
RenderPipelineHandle
type RenderPipelineHandle = Int
ShaderModuleHandle
type ShaderModuleHandle = Int
Enums
VertexFormat
enum VertexFormat {
Uint8
Sint8
Unorm8
Snorm8
Uint8x2
Sint8x2
Unorm8x2
Snorm8x2
Uint16
Sint16
Unorm16
Snorm16
Uint8x4
Sint8x4
Unorm8x4
Snorm8x4
Uint16x2
Sint16x2
Unorm16x2
Snorm16x2
Float32
Uint32
Sint32
Uint16x4
Sint16x4
Unorm16x4
Snorm16x4
Float32x2
Uint32x2
Sint32x2
Float32x3
Uint32x3
Sint32x3
Float32x4
Uint32x4
Sint32x4
}
Vertex attribute format
The list is intended to be a minimal list of things that are broadly supported in Metal, Vulkan, and DX12.
It should also not include formats that cannot (or I don’t want to) be supported in Swamp, such as half-floats (float16) and 64-bit formats (Swamp is 32-bit).
And no obscure packed and normalized formats (e.g. unorm_10_10_10_2).
Unorm* expands to floats in [0.0, 1.0], and Snorm* expands to floats in [-1.0, 1.0].
VertexStepMode
enum VertexStepMode {
Vertex
Instance
}
AddressMode
enum AddressMode {
Repeat the texture
Repeat
Clamp to edge pixel
ClampToEdge
Mirror and repeat
MirrorRepeat
}
Texture address mode for sampling outside [0, 1] range
FilterMode
enum FilterMode {
Nearest neighbor (pixelated)
Nearest
Linear interpolation (smooth)
Linear
}
Texture filtering mode
BlendMode
enum BlendMode {
Standard alpha blending (src_alpha, one_minus_src_alpha)
Alpha
Additive blending (one, one)
Additive
Premultiplied alpha (one, one_minus_src_alpha)
PremultipliedAlpha
No blending (opaque)
Opaque
}
Blend mode for rendering
CullMode
enum CullMode {
No culling (render both sides)
None
Cull front faces
Front
Cull back faces (default for solid objects)
Back
}
Face culling mode
PrimitiveTopology
enum PrimitiveTopology {
PointList
LineList
LineStrip
TriangleList
TriangleStrip
}
vertices into geometry
CompareFunction
enum CompareFunction {
Less
LessEqual
Greater
Always
}
Comparison function used for depth testing
SampledTextureFormat
enum SampledTextureFormat {
R8Unorm
Rgba8Unorm
Rgba8UnormSrgb
Rgba16Float
}
Texture format for textures that are sampled (read).
RenderTextureFormat
enum RenderTextureFormat {
R8Unorm
Rgba8Unorm
Rgba8UnormSrgb
Rgba16Float
R32Uint
}
Texture format for color attachments.
StorageTextureFormat
enum StorageTextureFormat {
Rgba8Unorm
Rgba16Float
R32Uint
}
Texture format for storage textures.
DepthTextureFormat
enum DepthTextureFormat {
Depth24Plus
Depth24PlusStencil8
Depth32Float
}
Texture format for depth/stencil attachments.
Depth24Plus is guaranteed to be at least 24 bits
BufferUsage
enum BufferUsage {
For vertex data
Vertex
For index data
Index
For uniform/constant data
Uniform
For storage buffers
Storage
}
Buffer usage flags
Entry
enum Entry {
SetIndexBuffer(SetIndexBuffer)
SetVertexBuffer(SetVertexBuffer)
SetBindGroup(SetBindGroup)
DrawIndexed(DrawIndexed)
Draw(Draw)
SetPipeline(RenderPipelineHandle)
SetViewport(Viewport)
SetScissorRect(ScissorRect)
SetStencilReference(StencilReference)
}
BindGroupEntry
enum BindGroupEntry {
Buffer(BufferHandle)
TextureView(TextureViewHandle)
Sampler(SamplerHandle)
}
BufferBindingType
enum BufferBindingType {
Uniform
Storage({ read_only: Bool })
}
StorageTextureAccess
enum StorageTextureAccess {
WriteOnly
ReadOnly
ReadWrite
}
Access mode for storage texture bindings
BindingType
enum BindingType {
Buffer(BufferBindingType)
Sampler
Texture
StorageTexture(StorageTextureBindingType)
}
Resource type for one slot in a bind group layout.
TODO: Texture bindings use D2 dimensions only for now. @catnipped will determine when cube maps, texture arrays, or 3D volumes are needed.
Functions
create_index_buffer_u16
fn create_index_buffer_u16(
buffer: Any,
description: String
) -> BufferHandle
create_vertex_buffer
fn create_vertex_buffer(
buffer: Any,
description: String
) -> BufferHandle
create_buffer
fn create_buffer(
size: Int,
usage: BufferUsage,
description: String
) -> BufferHandle
Creates an empty GPU buffer without initial data.
create_bind_group
fn create_bind_group(
bind_group_layout: BindGroupLayoutHandle,
entries: [BindGroupEntry],
description: String
) -> BindGroupHandle
Creates a bind group instance for a layout from [create_bind_group_layout].
entries are assigned to @binding(0), @binding(1), … in array order.
The layout must declare bindings in that same sequential order.
create_sampler
fn create_sampler(
config: SamplerConfig,
description: String
) -> SamplerHandle
create_sampled_texture
fn create_sampled_texture(
width: Int,
height: Int,
format: SampledTextureFormat,
description: String
) -> TextureHandle
Creates an empty texture that can be sampled (read).
create_render_texture
fn create_render_texture(
width: Int,
height: Int,
format: RenderTextureFormat,
description: String
) -> TextureHandle
Creates an empty texture that can be used as a color render attachment.
create_render_sampled_texture
fn create_render_sampled_texture(
width: Int,
height: Int,
format: SampledTextureFormat,
description: String
) -> TextureHandle
Creates an empty texture that can be both rendered to (write) and sampled (read).
create_storage_texture
fn create_storage_texture(
width: Int,
height: Int,
format: StorageTextureFormat,
description: String
) -> TextureHandle
Creates an empty texture that can be used as a storage texture.
create_depth_texture
fn create_depth_texture(
width: Int,
height: Int,
format: DepthTextureFormat,
description: String
) -> TextureHandle
Creates an empty texture that can be used as a depth/stencil attachment.
create_sampled_texture_png
fn create_sampled_texture_png(
image: Image,
format: SampledTextureFormat,
description: String
) -> TextureHandle
create_render_sampled_texture_png
fn create_render_sampled_texture_png(
image: Image,
format: SampledTextureFormat,
description: String
) -> TextureHandle
create_texture_view
fn create_texture_view(
texture: TextureHandle,
description: String
) -> TextureViewHandle
Creates a view into a texture for rendering or sampling
create_bind_group_layout
fn create_bind_group_layout(
entries: [BindGroupLayoutEntry],
description: String
) -> BindGroupLayoutHandle
Describes the resources at each [BindGroupLayoutEntry::binding] within one bind group.
create_pipeline_layout
fn create_pipeline_layout(
groups: [BindGroupLayoutHandle],
description: String
) -> PipelineLayoutHandle
Creates a pipeline layout from bind group layouts in group-index order.
The first layout is group index 0 (@group(0)), the second is 1, and so on.
create_render_pipeline
fn create_render_pipeline(
layout: PipelineLayoutHandle,
buffers: [VertexBufferLayout],
shader_module: Shader,
options: RenderPipelineOptions,
description: String
) -> RenderPipelineHandle
Creates a render pipeline targeting the current surface (swapchain) format.
create_render_pipeline_with_color_format
fn create_render_pipeline_with_color_format(
layout: PipelineLayoutHandle,
buffers: [VertexBufferLayout],
shader_module: Shader,
color_format: RenderTextureFormat,
options: RenderPipelineOptions,
description: String
) -> RenderPipelineHandle
Creates a render pipeline targeting an explicit color format (for offscreen pipelines).
add_pass
fn add_pass(pass: RenderPass, description: String)
Submits a render pass for execution
create_uniform_buffer
fn create_uniform_buffer(
buffer: Any,
description: String
) -> BufferHandle
surface_extent
fn surface_extent() -> (Int, Int)
The size of the renderable surface. Return (Width, Height).
The texture view to render to