// RHI data types shared by the core and every backend implementation.
//
// Moved out of `laplaceliu/tycho` so it can be compiled for every target: this
// package must stay free of `@shader` / `@tycho_window` / native-only modules.
// Shader compilation helpers and the window type aliases stay behind in
// `laplaceliu/tycho` (they are native-only by nature).
// Tycho RHI public types, descriptors, errors, and shader/window re-exports.
///|
#derive(Eq, Debug)
pub(all) enum BackendKind {
Vulkan
Dx12
WebGpu
/// Apple Metal (macOS / iOS). Planned native driver seam.
Metal
}
///|
pub fn BackendKind::name(self : BackendKind) -> String {
match self {
BackendKind::Vulkan => "vulkan"
BackendKind::Dx12 => "dx12"
BackendKind::WebGpu => "webgpu"
BackendKind::Metal => "metal"
}
}
///|
pub fn BackendKind::shader_language(self : BackendKind) -> ShaderLanguage {
match self {
BackendKind::Vulkan => ShaderLanguage::SpirV
BackendKind::Dx12 => ShaderLanguage::Dxil
BackendKind::WebGpu => ShaderLanguage::Wgsl
BackendKind::Metal => ShaderLanguage::Msl
}
}
///|
#derive(Eq, Debug)
pub(all) enum PresentMode {
Fifo
Mailbox
Immediate
}
///|
#derive(Eq, Debug)
pub(all) enum MemoryLocation {
GpuOnly
CpuToGpu
GpuToCpu
}
///|
pub(all) struct DeviceDesc {
app_name : String
validation : Bool
headless : Bool
pipeline_cache : Bytes
backend : BackendKind
}
///|
pub fn DeviceDesc::new(
app_name : String,
validation? : Bool = false,
headless? : Bool = false,
pipeline_cache? : Bytes = b"",
backend? : BackendKind = BackendKind::Vulkan,
) -> DeviceDesc {
{ app_name, validation, headless, pipeline_cache, backend }
}
///|
pub(all) struct DeviceCapabilities {
device_name : String
is_discrete : Bool
api_version : UInt32
driver_version : UInt32
max_image_dimension_2d : Int
max_framebuffer_samples : Int
max_sampler_anisotropy : Float
/// Minimum byte alignment for uniform-buffer descriptor offsets / ranges
/// (Vulkan `minUniformBufferOffsetAlignment`; 256 for D3D12 constant buffers).
/// Any ring of uniform blocks must stride by a multiple of this value.
min_uniform_buffer_offset_alignment : Int
supports_anisotropy : Bool
supports_indirect_draw : Bool
supports_bindless : Bool
supports_mesh_shader : Bool
supports_ray_query : Bool
}
///|
/// Where a pipeline's shader expects each kind of resource.
///
/// The RHI binds by *slot*; WGSL binds by number. Until now the two agreed only
/// because a single global formula (`textures 0..n`, `samplers n..2n`,
/// `push 2n`) happened to fit the one converted shader. It does not scale: any
/// shader that occupies a binding of its own -- 2d's `blur` uses binding 2,
/// `prim2d` uses 0 and 1 for storage buffers -- collides with it.
///
/// So the layout travels with the pipeline. `legacy` reproduces the old formula
/// exactly, which is what every SPIR-V pipeline still means.
#derive(Eq, Debug)
pub(all) struct BindingLayout {
/// texture slot k binds at `texture_base + k`
texture_base : Int
/// sampler slot k binds at `sampler_base + k`
sampler_base : Int
/// storage slot k binds at `storage_base + k`
storage_base : Int
/// uniform slot k binds at `uniform_base + k`
uniform_base : Int
/// the uniform block a Vulkan push-constant block was rewritten into
push : Int
}
///|
/// The layout every pipeline used before per-shader plans existed: textures from
/// 0, samplers one panel later, push constants after both.
pub fn BindingLayout::legacy(texture_slots : Int) -> BindingLayout {
{
texture_base: 0,
sampler_base: texture_slots,
storage_base: 0,
uniform_base: 0,
push: 2 * texture_slots,
}
}
///|
/// The shader blob format a backend can consume (ADR-005 D7).
///
/// The same `FixedArray[UInt32]` channel carries whichever of these the device
/// wants -- SPIR-V words as-is, WGSL as UTF-8 zero-padded to a 4-byte boundary --
/// so a pipeline descriptor does not need a dozen shader fields. What the caller
/// must not do is guess: ask the device.
///
/// `Dxil` exists so that asking has an honest answer before a DX12 backend can
/// build pipelines; a caller that only knows SPIR-V and WGSL must report it as
/// unsupported rather than shipping the wrong bytes.
#derive(Eq, Debug)
pub(all) enum ShaderLanguage {
SpirV
Wgsl
Dxil
/// Metal Shading Language (macOS / iOS).
Msl
}
///|
#derive(Eq, Debug)
pub(all) enum BufferKind {
Vertex
Index
Uniform
Storage
Staging
Indirect
}
///|
/// Composable buffer usage flags. `BufferKind` remains the shortcut for the
/// common single-purpose cases; this type expresses the combinations GPU-driven
/// pipelines need (`Storage | Indirect`, `Storage | Vertex`, ...).
pub(all) struct BufferUsage {
priv bits : UInt32
}
///|
pub fn BufferUsage::none() -> BufferUsage {
{ bits: (0 : UInt32) }
}
///|
pub fn BufferUsage::vertex() -> BufferUsage {
{ bits: (1 : UInt32) }
}
///|
pub fn BufferUsage::index() -> BufferUsage {
{ bits: (2 : UInt32) }
}
///|
pub fn BufferUsage::uniform() -> BufferUsage {
{ bits: (4 : UInt32) }
}
///|
pub fn BufferUsage::storage() -> BufferUsage {
{ bits: (8 : UInt32) }
}
///|
pub fn BufferUsage::indirect() -> BufferUsage {
{ bits: (16 : UInt32) }
}
///|
pub fn BufferUsage::copy_src() -> BufferUsage {
{ bits: (32 : UInt32) }
}
///|
pub fn BufferUsage::copy_dst() -> BufferUsage {
{ bits: (64 : UInt32) }
}
///|
pub fn BufferUsage::combine(self : BufferUsage, other : BufferUsage) -> BufferUsage {
{ bits: self.bits | other.bits }
}
///|
pub fn BufferUsage::contains(self : BufferUsage, other : BufferUsage) -> Bool {
(self.bits & other.bits) == other.bits
}
///|
pub fn BufferUsage::bits(self : BufferUsage) -> UInt32 {
self.bits
}
///|
/// The usage set the legacy `BufferKind` implies (including the transfer bits
/// the backends used to add implicitly).
pub fn BufferUsage::from_kind(kind : BufferKind) -> BufferUsage {
let u = match kind {
BufferKind::Vertex => BufferUsage::vertex().combine(BufferUsage::copy_dst())
BufferKind::Index => BufferUsage::index().combine(BufferUsage::copy_dst())
BufferKind::Uniform => BufferUsage::uniform()
BufferKind::Storage =>
BufferUsage::storage().combine(BufferUsage::copy_src()).combine(BufferUsage::copy_dst())
BufferKind::Staging => BufferUsage::copy_src().combine(BufferUsage::copy_dst())
BufferKind::Indirect =>
BufferUsage::indirect()
.combine(BufferUsage::storage())
.combine(BufferUsage::copy_src())
.combine(BufferUsage::copy_dst())
}
u
}
///|
/// The single-purpose kind that best describes `usage`, for backends that only
/// distinguish storage from everything else.
pub fn BufferUsage::as_kind(self : BufferUsage) -> BufferKind {
if self.contains(BufferUsage::indirect()) {
BufferKind::Indirect
} else if self.contains(BufferUsage::storage()) {
BufferKind::Storage
} else if self.contains(BufferUsage::uniform()) {
BufferKind::Uniform
} else if self.contains(BufferUsage::index()) {
BufferKind::Index
} else if self.contains(BufferUsage::vertex()) {
BufferKind::Vertex
} else {
BufferKind::Staging
}
}
///|
#derive(Eq, Debug)
pub(all) enum IndexType {
Uint16
Uint32
}
///|
#derive(Eq, Debug)
pub(all) enum VertexFormat {
Float2
Float3
Float4
Float1
Int1
Uint1
Unorm8x4
Uint16x4
}
///|
/// Backend-neutral encoding index. The DX12 shim uses the same numbering for
/// its `DXGI_FORMAT` table, so keep the two in sync.
pub fn VertexFormat::code(self : VertexFormat) -> Int {
match self {
Float2 => 0
Float3 => 1
Float4 => 2
Float1 => 3
Int1 => 4
Uint1 => 5
Unorm8x4 => 6
Uint16x4 => 7
}
}
///|
/// Bytes consumed per vertex attribute.
pub fn VertexFormat::bytes(self : VertexFormat) -> Int {
match self {
Float1 | Int1 | Uint1 | Unorm8x4 => 4
Float2 | Uint16x4 => 8
Float3 => 12
Float4 => 16
}
}
///|
/// Composable texture usage flags.
pub(all) struct TextureUsage {
priv bits : UInt32
}
///|
pub fn TextureUsage::none() -> TextureUsage {
{ bits: (0 : UInt32) }
}
///|
pub fn TextureUsage::sampled() -> TextureUsage {
{ bits: (1 : UInt32) }
}
///|
pub fn TextureUsage::storage() -> TextureUsage {
{ bits: (2 : UInt32) }
}
///|
pub fn TextureUsage::render_attachment() -> TextureUsage {
{ bits: (4 : UInt32) }
}
///|
pub fn TextureUsage::copy_src() -> TextureUsage {
{ bits: (8 : UInt32) }
}
///|
pub fn TextureUsage::copy_dst() -> TextureUsage {
{ bits: (16 : UInt32) }
}
///|
pub fn TextureUsage::combine(self : TextureUsage, other : TextureUsage) -> TextureUsage {
{ bits: self.bits | other.bits }
}
///|
pub fn TextureUsage::contains(self : TextureUsage, other : TextureUsage) -> Bool {
(self.bits & other.bits) == other.bits
}
///|
pub fn TextureUsage::bits(self : TextureUsage) -> UInt32 {
self.bits
}
///|
pub(all) enum TextureDimension {
D2
D2Array(Int)
Cube
}
///|
/// One descriptor for every texture shape: 2D, 2D array and cubemap, with
/// composable usage flags instead of a factory per combination.
pub(all) struct TextureDesc {
width : Int
height : Int
format : TextureFormat
usage : TextureUsage
dimension : TextureDimension
/// `1` for a single level; `0` asks the backend for the full chain when the
/// backend supports generating it.
mip_levels : Int
samples : SampleCount
}
///|
pub fn TextureDesc::new(
width : Int,
height : Int,
format : TextureFormat,
usage? : TextureUsage = TextureUsage::sampled(),
dimension? : TextureDimension = TextureDimension::D2,
mip_levels? : Int = 1,
samples? : SampleCount = SampleCount::X1,
) -> TextureDesc {
{ width, height, format, usage, dimension, mip_levels, samples }
}
///|
/// Formats get printed in sample output and error messages, so they carry a
/// stable lowercase name rather than a debug dump.
pub impl Show for TextureFormat with output(self, logger) {
match self {
RGBA8 => logger.write_string("rgba8")
BGRA8 => logger.write_string("bgra8")
RGBA8Srgb => logger.write_string("rgba8-srgb")
BGRA8Srgb => logger.write_string("bgra8-srgb")
RGBA16F => logger.write_string("rgba16f")
RG16F => logger.write_string("rg16f")
R8 => logger.write_string("r8")
RGBA32F => logger.write_string("rgba32f")
Depth32F => logger.write_string("depth32f")
Depth24Stencil8 => logger.write_string("depth24-stencil8")
}
}
///|
#derive(Eq, Debug)
pub(all) enum TextureFormat {
RGBA8
BGRA8
RGBA8Srgb
BGRA8Srgb
RGBA16F
RG16F
R8
RGBA32F
Depth32F
Depth24Stencil8
}
///|
#derive(Eq, Debug)
pub(all) enum Filter {
Nearest
Linear
}
///|
#derive(Eq, Debug)
pub(all) enum AddressMode {
Repeat
ClampToEdge
ClampToBorder
}
///|
#derive(Eq, Debug)
pub(all) enum BorderColor {
TransparentBlack
OpaqueBlack
OpaqueWhite
}
///|
#derive(Eq, Debug)
pub(all) enum CullMode {
None
Back
Front
}
///|
#derive(Eq, Debug)
pub(all) enum FrontFace {
Clockwise
CounterClockwise
}
///|
#derive(Eq, Debug)
pub(all) enum LoadOp {
Clear
Load
DontCare
}
///|
#derive(Eq, Debug)
pub(all) enum StoreOp {
Store
DontCare
}
///|
#derive(Eq, Debug)
pub(all) enum SampleCount {
X1
X4
}
///|
#derive(Eq, Debug)
pub(all) enum PrimitiveTopology {
TriangleList
TriangleStrip
LineList
LineStrip
PointList
}
///|
#derive(Eq, Debug)
pub(all) enum CompareOp {
Never
Less
Equal
LessEqual
Greater
NotEqual
GreaterEqual
Always
}
///|
#derive(Eq, Debug)
pub(all) enum BlendMode {
Opaque
Alpha
Additive
Premultiplied
Multiply
DstIn
SrcIn
Clear
}
///|
#derive(Eq, Debug)
pub(all) enum BlendFactor {
Zero
One
SrcColor
OneMinusSrcColor
DstColor
OneMinusDstColor
SrcAlpha
OneMinusSrcAlpha
DstAlpha
OneMinusDstAlpha
}
///|
#derive(Eq, Debug)
pub(all) enum BlendOp {
Add
Subtract
ReverseSubtract
Min
Max
}
///|
// Explicit blend equation, used for Porter-Duff and separable modes that the
// preset `BlendMode` values do not cover.
#derive(Eq, Debug)
pub(all) struct BlendState {
src_color : BlendFactor
dst_color : BlendFactor
color_op : BlendOp
src_alpha : BlendFactor
dst_alpha : BlendFactor
alpha_op : BlendOp
}
///|
pub fn BlendState::new(
src_color : BlendFactor,
dst_color : BlendFactor,
src_alpha : BlendFactor,
dst_alpha : BlendFactor,
color_op? : BlendOp = BlendOp::Add,
alpha_op? : BlendOp = BlendOp::Add,
) -> BlendState {
{ src_color, dst_color, color_op, src_alpha, dst_alpha, alpha_op }
}
///|
#derive(Eq, Debug)
pub(all) enum VertexStepMode {
Vertex
Instance
}
///|
pub(all) struct VertexAttribute {
location : Int
format : VertexFormat
offset : Int
}
///|
pub fn VertexAttribute::new(
location : Int,
format : VertexFormat,
offset : Int,
) -> VertexAttribute {
{ location, format, offset }
}
///|
pub(all) struct VertexBufferLayout {
stride : Int
step_mode : VertexStepMode
attributes : Array[VertexAttribute]
}
///|
pub fn VertexBufferLayout::new(
stride : Int,
attributes : Array[VertexAttribute],
step_mode? : VertexStepMode = VertexStepMode::Vertex,
) -> VertexBufferLayout {
{ stride, step_mode, attributes }
}
///|
pub(all) struct ClearColor {
r : Float
g : Float
b : Float
a : Float
}
///|
pub fn ClearColor::rgba(r : Float, g : Float, b : Float, a : Float) -> ClearColor {
{ r, g, b, a }
}
///|
pub fn ClearColor::black() -> ClearColor {
{ r: (0.0 : Float), g: (0.0 : Float), b: (0.0 : Float), a: (1.0 : Float) }
}
///|
pub(all) struct SamplerDesc {
mag : Filter
min : Filter
mipmap : Filter
address : AddressMode
border_color : BorderColor
min_lod : Float
max_lod : Float
mip_lod_bias : Float
anisotropy : Float
compare_op : CompareOp?
}
///|
pub fn SamplerDesc::new(
mag? : Filter = Filter::Linear,
min? : Filter = Filter::Linear,
mipmap? : Filter = Filter::Linear,
address? : AddressMode = AddressMode::Repeat,
border_color? : BorderColor = BorderColor::TransparentBlack,
min_lod? : Float = (0.0 : Float),
max_lod? : Float = (1000.0 : Float),
mip_lod_bias? : Float = (0.0 : Float),
anisotropy? : Float = (1.0 : Float),
compare_op? : CompareOp,
) -> SamplerDesc {
{
mag,
min,
mipmap,
address,
border_color,
min_lod,
max_lod,
mip_lod_bias,
anisotropy,
compare_op,
}
}
///|
pub(all) struct PipelineDesc {
vert : FixedArray[UInt32]
frag : FixedArray[UInt32]
vert_entry : String
frag_entry : String
vertex_formats : Array[VertexFormat]
vertex_layouts : Array[VertexBufferLayout]
uniform_slots : Int
texture_slots : Int
/// binding numbers this pipeline's shader actually declares
layout : BindingLayout
storage_slots : Int
storage_image_slots : Int
accel_slots : Int
uniform_bindings : Array[Int]
texture_bindings : Array[Int]
storage_bindings : Array[Int]
storage_image_bindings : Array[Int]
accel_bindings : Array[Int]
push_constant_size : Int
// Drawing state is grouped: rasterisation, depth/stencil and one entry per
// colour attachment, instead of 20 flat mirror fields.
raster : RasterState
// `None` when the pipeline has no depth/stencil attachment at all.
depth_stencil : DepthStencilState?
color_targets : Array[ColorTargetState]
/// `BindlessHeap::id()`; the core resolves it before the backend sees it.
bindless_heap : UInt64?
}
///|
#derive(Eq, Debug)
pub(all) enum StencilOp {
Keep
Zero
Replace
IncrementAndClamp
DecrementAndClamp
Invert
IncrementAndWrap
DecrementAndWrap
}
///|
pub(all) struct StencilFaceState {
fail_op : StencilOp
pass_op : StencilOp
depth_fail_op : StencilOp
compare_op : CompareOp
}
///|
pub fn StencilFaceState::new(
fail_op? : StencilOp = StencilOp::Keep,
pass_op? : StencilOp = StencilOp::Keep,
depth_fail_op? : StencilOp = StencilOp::Keep,
compare_op? : CompareOp = CompareOp::Always,
) -> StencilFaceState {
{ fail_op, pass_op, depth_fail_op, compare_op }
}
///|
pub(all) struct StencilState {
front : StencilFaceState
back : StencilFaceState
compare_mask : UInt32
write_mask : UInt32
reference : UInt32
}
///|
pub fn StencilState::new(
front? : StencilFaceState = StencilFaceState::new(),
back? : StencilFaceState = StencilFaceState::new(),
compare_mask? : UInt32 = (0xFF : UInt32),
write_mask? : UInt32 = (0xFF : UInt32),
reference? : UInt32 = (0 : UInt32),
) -> StencilState {
{ front, back, compare_mask, write_mask, reference }
}
///|
pub(all) struct RayGeometryDesc {
// Opaque handles: this package stays free of RHI types.
vertex_buffer : UInt64
vertex_count : Int
vertex_stride : Int
index_buffer : UInt64?
index_count : Int
index_type : IndexType
is_opaque : Bool
}
///|
pub fn RayGeometryDesc::new(
vertex_buffer : UInt64,
vertex_count : Int,
vertex_stride? : Int = 12,
index_buffer? : UInt64,
index_count? : Int = 0,
index_type? : IndexType = IndexType::Uint32,
is_opaque? : Bool = true,
) -> RayGeometryDesc {
{
vertex_buffer,
vertex_count,
vertex_stride,
index_buffer,
index_count,
index_type,
is_opaque,
}
}
///|
pub(all) struct RayInstanceDesc {
/// `AccelStruct::id()`; the core resolves it before the backend sees it.
blas : UInt64
transform : FixedArray[Float]
custom_index : Int
mask : Int
}
///|
pub fn RayInstanceDesc::new(
/// `AccelStruct::id()`; the core resolves it before the backend sees it.
blas : UInt64,
transform? : FixedArray[Float] = @tmath.Affine3::identity().to_vk_transform_3x4(),
custom_index? : Int = 0,
mask? : Int = 0xFF,
) -> RayInstanceDesc {
{ blas, transform, custom_index, mask }
}
///|
/// Instance descriptor whose TLAS transform comes from an affine transform
/// (row-major 3x4, the same layout as `VkTransformMatrixKHR`).
pub fn RayInstanceDesc::from_affine3(
/// `AccelStruct::id()`; the core resolves it before the backend sees it.
blas : UInt64,
affine : @tmath.Affine3,
custom_index? : Int = 0,
mask? : Int = 0xFF,
) -> RayInstanceDesc {
{
blas,
transform: affine.to_vk_transform_3x4(),
custom_index,
mask,
}
}
///|
/// Rasterisation state, grouped out of the flat `PipelineDesc`.
pub(all) struct RasterState {
topology : PrimitiveTopology
cull_mode : CullMode
front_face : FrontFace
samples : SampleCount
}
///|
pub fn RasterState::new(
topology? : PrimitiveTopology = PrimitiveTopology::TriangleList,
cull_mode? : CullMode = CullMode::None,
front_face? : FrontFace = FrontFace::Clockwise,
samples? : SampleCount = SampleCount::X1,
) -> RasterState {
{ topology, cull_mode, front_face, samples }
}
///|
/// Depth/stencil state, grouped out of the flat `PipelineDesc`.
pub(all) struct DepthStencilState {
depth_test : Bool
write : Bool
compare : CompareOp
format : TextureFormat?
depth_only : Bool
bias : Bool
bias_constant : Float
bias_slope : Float
stencil : StencilState?
}
///|
pub fn DepthStencilState::new(
depth_test? : Bool = false,
write? : Bool = false,
compare? : CompareOp = CompareOp::Less,
format? : TextureFormat,
depth_only? : Bool = false,
bias? : Bool = false,
bias_constant? : Float = (0.0 : Float),
bias_slope? : Float = (0.0 : Float),
stencil? : StencilState,
) -> DepthStencilState {
{
depth_test,
write,
compare,
format,
depth_only,
bias,
bias_constant,
bias_slope,
stencil,
}
}
///|
/// One colour attachment: format plus its write mask and blend state.
pub(all) struct ColorTargetState {
format : TextureFormat?
write : Bool
blend_mode : BlendMode?
blend_state : BlendState?
}
///|
pub fn ColorTargetState::new(
format? : TextureFormat,
write? : Bool = true,
blend_mode? : BlendMode,
blend_state? : BlendState,
) -> ColorTargetState {
{ format, write, blend_mode, blend_state }
}
///|
/// The rasterisation half of a pipeline description.
pub fn PipelineDesc::raster_state(self : PipelineDesc) -> RasterState {
self.raster
}
///|
/// The depth/stencil state with defaults filled in, for backends that always
/// need a value.
pub fn PipelineDesc::depth_or_default(self : PipelineDesc) -> DepthStencilState {
match self.depth_stencil {
Some(d) => d
None => DepthStencilState::new()
}
}
///|
/// The depth/stencil half, or `None` when the pipeline has no depth or stencil
/// state at all.
pub fn PipelineDesc::depth_stencil_state(self : PipelineDesc) -> DepthStencilState? {
self.depth_stencil
}
///|
/// The colour attachments, one entry per target. A single target is reported
/// through `color_format`, an MRT list through `color_formats`.
pub fn PipelineDesc::color_target_states(self : PipelineDesc) -> Array[ColorTargetState] {
self.color_targets
}
///|
/// The colour target at `index`, falling back to a default when the descriptor
/// has no explicit target (single-target pipelines without a format).
pub fn PipelineDesc::color_target(self : PipelineDesc, index : Int) -> ColorTargetState {
if index < self.color_targets.length() {
self.color_targets[index]
} else {
ColorTargetState::new()
}
}
///|
/// The resolved blend mode, `Opaque` when the target does not ask for blending.
pub fn ColorTargetState::effective_blend_mode(self : ColorTargetState) -> BlendMode {
match self.blend_mode {
Some(m) => m
None => BlendMode::Opaque
}
}
///|
/// The depth/stencil state of a mesh pipeline, with defaults filled in.
pub fn MeshPipelineDesc::depth_or_default(self : MeshPipelineDesc) -> DepthStencilState {
match self.depth_stencil {
Some(d) => d
None => DepthStencilState::new()
}
}
///|
/// The colour target at `index`, falling back to a default when the mesh
/// pipeline descriptor has no explicit target.
pub fn MeshPipelineDesc::color_target(self : MeshPipelineDesc, index : Int) -> ColorTargetState {
if index < self.color_targets.length() {
self.color_targets[index]
} else {
ColorTargetState::new()
}
}
///|
/// Build a graphics pipeline from grouped state instead of 38 flat arguments.
/// The result is the same `PipelineDesc` the flat constructor produces, so the
/// two styles interoperate.
pub fn PipelineDesc::graphics(
vert : FixedArray[UInt32],
frag : FixedArray[UInt32],
vert_entry? : String = "main",
frag_entry? : String = "main",
raster? : RasterState = RasterState::new(),
depth_stencil? : DepthStencilState,
color_targets? : Array[ColorTargetState] = [],
vertex_formats? : Array[VertexFormat] = [],
vertex_layouts? : Array[VertexBufferLayout] = [],
uniform_slots? : Int = 0,
texture_slots? : Int = 0,
storage_slots? : Int = 0,
storage_image_slots? : Int = 0,
accel_slots? : Int = 0,
uniform_bindings? : Array[Int] = [],
texture_bindings? : Array[Int] = [],
storage_bindings? : Array[Int] = [],
storage_image_bindings? : Array[Int] = [],
accel_bindings? : Array[Int] = [],
push_constant_size? : Int = 0,
/// `BindlessHeap::id()`; the core resolves it before the backend sees it.
bindless_heap? : UInt64,
) -> PipelineDesc {
// Depth/stencil: absent means "no depth attachment".
let (d_test, d_write, d_compare, d_format, d_only, d_bias, d_bc, d_bs, d_stencil) =
match depth_stencil {
Some(d) => (
d.depth_test,
d.write,
d.compare,
d.format,
d.depth_only,
d.bias,
d.bias_constant,
d.bias_slope,
d.stencil,
)
None => (
false,
false,
CompareOp::Less,
None,
false,
false,
(0.0 : Float),
(0.0 : Float),
(None : StencilState?),
)
}
// Colour: one target maps to `color_format`, several to the MRT list.
let formats : Array[TextureFormat] = []
for t in color_targets {
match t.format {
Some(f) => formats.push(f)
None => ()
}
}
let single = color_targets.length() <= 1
let primary = if single && formats.length() > 0 {
Some(formats[0])
} else {
None
}
let mrt = if single { [] } else { formats }
let color_write = if color_targets.length() > 0 { color_targets[0].write } else { true }
let blend_mode = if color_targets.length() > 0 {
color_targets[0].blend_mode
} else {
None
}
let blend_state = if color_targets.length() > 0 {
color_targets[0].blend_state
} else {
None
}
let blend = blend_mode is Some(_) || blend_state is Some(_)
PipelineDesc::new(
vert,
frag,
vert_entry~,
frag_entry~,
vertex_formats~,
vertex_layouts~,
uniform_slots~,
texture_slots~,
storage_slots~,
storage_image_slots~,
accel_slots~,
uniform_bindings~,
texture_bindings~,
storage_bindings~,
storage_image_bindings~,
accel_bindings~,
push_constant_size~,
topology=raster.topology,
depth_test=d_test,
depth_write=d_write,
depth_compare=d_compare,
depth_format?=d_format,
depth_only=d_only,
depth_bias=d_bias,
depth_bias_constant=d_bc,
depth_bias_slope=d_bs,
stencil?=d_stencil,
cull_mode=raster.cull_mode,
front_face=raster.front_face,
blend=blend,
blend_mode?=blend_mode,
blend_state?=blend_state,
color_write=color_write,
samples=raster.samples,
color_format?=primary,
color_formats=mrt,
bindless_heap?=bindless_heap,
)
}
///|
pub fn PipelineDesc::new(
vert : FixedArray[UInt32],
frag : FixedArray[UInt32],
vert_entry? : String = "main",
frag_entry? : String = "main",
vertex_formats? : Array[VertexFormat] = [],
vertex_layouts? : Array[VertexBufferLayout] = [],
uniform_slots? : Int = 0,
texture_slots? : Int = 0,
storage_slots? : Int = 0,
storage_image_slots? : Int = 0,
accel_slots? : Int = 0,
uniform_bindings? : Array[Int] = [],
texture_bindings? : Array[Int] = [],
storage_bindings? : Array[Int] = [],
storage_image_bindings? : Array[Int] = [],
accel_bindings? : Array[Int] = [],
push_constant_size? : Int = 0,
topology? : PrimitiveTopology = PrimitiveTopology::TriangleList,
depth_test? : Bool = false,
depth_write? : Bool = false,
depth_compare? : CompareOp = CompareOp::Less,
depth_format? : TextureFormat,
depth_only? : Bool = false,
depth_bias? : Bool = false,
depth_bias_constant? : Float = (0.0 : Float),
depth_bias_slope? : Float = (0.0 : Float),
stencil? : StencilState,
cull_mode? : CullMode = CullMode::None,
front_face? : FrontFace = FrontFace::Clockwise,
blend? : Bool = false,
blend_mode? : BlendMode,
blend_state? : BlendState,
color_write? : Bool = true,
samples? : SampleCount = SampleCount::X1,
color_format? : TextureFormat,
color_formats? : Array[TextureFormat] = [],
/// `BindlessHeap::id()`; the core resolves it before the backend sees it.
bindless_heap? : UInt64,
) -> PipelineDesc {
{
vert,
frag,
vert_entry,
frag_entry,
vertex_formats,
vertex_layouts,
uniform_slots,
texture_slots,
layout: BindingLayout::legacy(texture_slots),
storage_slots,
storage_image_slots,
accel_slots,
uniform_bindings,
texture_bindings,
storage_bindings,
storage_image_bindings,
accel_bindings,
push_constant_size,
raster: { topology, cull_mode, front_face, samples },
depth_stencil: make_depth_stencil(
depth_test,
depth_write,
depth_compare,
depth_format,
depth_only,
depth_bias,
depth_bias_constant,
depth_bias_slope,
stencil,
),
color_targets: make_color_targets(
color_format,
color_formats,
color_write,
blend,
blend_mode,
blend_state,
),
bindless_heap,
}
}
///|
/// Keeps a flat constructor but stores the depth/stencil state as one value.
fn make_depth_stencil(
depth_test : Bool,
depth_write : Bool,
depth_compare : CompareOp,
depth_format : TextureFormat?,
depth_only : Bool,
depth_bias : Bool,
depth_bias_constant : Float,
depth_bias_slope : Float,
stencil : StencilState?,
) -> DepthStencilState? {
let has_state = depth_test ||
depth_write ||
depth_only ||
depth_bias ||
depth_format is Some(_) ||
stencil is Some(_)
if !has_state {
return None
}
Some({
depth_test,
write: depth_write,
compare: depth_compare,
format: depth_format,
depth_only,
bias: depth_bias,
bias_constant: depth_bias_constant,
bias_slope: depth_bias_slope,
stencil,
})
}
///|
/// Keeps a flat constructor but stores one target per colour attachment. The
/// legacy `blend` flag is folded into the resolved blend mode.
fn make_color_targets(
color_format : TextureFormat?,
color_formats : Array[TextureFormat],
color_write : Bool,
blend : Bool,
blend_mode : BlendMode?,
blend_state : BlendState?,
) -> Array[ColorTargetState] {
let resolved_blend : BlendMode? = match blend_mode {
Some(m) => Some(m)
None => if blend { Some(BlendMode::Alpha) } else { None }
}
let out : Array[ColorTargetState] = []
if color_formats.length() > 0 {
for f in color_formats {
out.push({ format: Some(f), write: color_write, blend_mode: resolved_blend, blend_state })
}
} else {
match color_format {
Some(f) =>
out.push({ format: Some(f), write: color_write, blend_mode: resolved_blend, blend_state })
None => ()
}
}
out
}
///|
pub(all) struct MeshPipelineDesc {
task : FixedArray[UInt32]?
mesh : FixedArray[UInt32]
frag : FixedArray[UInt32]
task_entry : String
mesh_entry : String
frag_entry : String
uniform_slots : Int
texture_slots : Int
storage_slots : Int
storage_image_slots : Int
uniform_bindings : Array[Int]
texture_bindings : Array[Int]
storage_bindings : Array[Int]
storage_image_bindings : Array[Int]
push_constant_size : Int
raster : RasterState
depth_stencil : DepthStencilState?
color_targets : Array[ColorTargetState]
/// `BindlessHeap::id()`; the core resolves it before the backend sees it.
bindless_heap : UInt64?
}
///|
pub fn MeshPipelineDesc::new(
mesh : FixedArray[UInt32],
frag : FixedArray[UInt32],
task? : FixedArray[UInt32],
task_entry? : String = "main",
mesh_entry? : String = "main",
frag_entry? : String = "main",
uniform_slots? : Int = 0,
texture_slots? : Int = 0,
storage_slots? : Int = 0,
storage_image_slots? : Int = 0,
uniform_bindings? : Array[Int] = [],
texture_bindings? : Array[Int] = [],
storage_bindings? : Array[Int] = [],
storage_image_bindings? : Array[Int] = [],
push_constant_size? : Int = 0,
depth_test? : Bool = false,
depth_write? : Bool = false,
depth_compare? : CompareOp = CompareOp::Less,
depth_format? : TextureFormat,
cull_mode? : CullMode = CullMode::None,
front_face? : FrontFace = FrontFace::Clockwise,
blend? : Bool = false,
blend_mode? : BlendMode,
color_write? : Bool = true,
samples? : SampleCount = SampleCount::X1,
color_format? : TextureFormat,
color_formats? : Array[TextureFormat] = [],
/// `BindlessHeap::id()`; the core resolves it before the backend sees it.
bindless_heap? : UInt64,
) -> MeshPipelineDesc {
{
task,
mesh,
frag,
task_entry,
mesh_entry,
frag_entry,
uniform_slots,
texture_slots,
storage_slots,
storage_image_slots,
uniform_bindings,
texture_bindings,
storage_bindings,
storage_image_bindings,
push_constant_size,
raster: { topology: PrimitiveTopology::TriangleList, cull_mode, front_face, samples },
depth_stencil: make_depth_stencil(
depth_test,
depth_write,
depth_compare,
depth_format,
false,
false,
(0.0 : Float),
(0.0 : Float),
(None : StencilState?),
),
color_targets: make_color_targets(
color_format,
color_formats,
color_write,
blend,
blend_mode,
(None : BlendState?),
),
bindless_heap,
}
}
///|
/// Native surface kind for externally owned surfaces (iOS `CAMetalLayer`,
/// Android `ANativeWindow`, embedded desktop windows, or an existing web
/// canvas). Pure L1 data so `rhi/types` stays independent of `plat`.
#derive(Eq, Debug)
pub(all) enum ExternalSurfaceKind {
Xlib
Xcb
Wayland
Win32
/// `CAMetalLayer*` on macOS / iOS (UIKit).
MetalLayer
/// `ANativeWindow*` on Android (NDK).
AndroidNativeWindow
/// Browser canvas registry id on the JS target.
WebCanvas
Headless
}
///|
/// Host-owned surface handle passed into `SwapchainDesc` when the OS/app owns
/// window creation (mandatory on iOS/Android, optional on desktop/web).
/// `display` / `handle` meanings match `ExternalSurfaceKind`:
/// - `Xlib`: `(Display*, Window)`
/// - `Wayland`: `(wl_display*, wl_surface*)`
/// - `Win32`: `(HINSTANCE, HWND)`
/// - `MetalLayer`: `(0, CAMetalLayer*)`
/// - `AndroidNativeWindow`: `(0, ANativeWindow*)`
/// - `WebCanvas`: `(canvas_id, 0)`
#derive(Eq, Debug)
pub(all) struct ExternalSurface {
kind : ExternalSurfaceKind
display : UInt64
handle : UInt64
}
///|
pub fn ExternalSurface::new(
kind : ExternalSurfaceKind,
display : UInt64,
handle : UInt64,
) -> ExternalSurface {
{ kind, display, handle }
}
///|
pub(all) struct SwapchainDesc {
title : String
width : Int
height : Int
depth : Bool
depth_format : TextureFormat
clear_color : ClearColor
enable_readback : Bool
present_mode : PresentMode
/// Optional host-owned native surface (for iOS UIKit, Android NDK, or
/// embedded views). When `None`, the backend creates its own window/canvas.
external_surface : ExternalSurface?
}
///|
pub fn SwapchainDesc::new(
title : String,
width : Int,
height : Int,
depth? : Bool = false,
depth_format? : TextureFormat = TextureFormat::Depth32F,
clear_color? : ClearColor = ClearColor::black(),
enable_readback? : Bool = false,
present_mode? : PresentMode = PresentMode::Fifo,
external_surface? : ExternalSurface,
) -> SwapchainDesc {
{
title,
width,
height,
depth,
depth_format,
clear_color,
enable_readback,
present_mode,
external_surface,
}
}
///|
pub(all) struct RenderTargetDesc {
/// `Texture::id()`; the core resolves it before the backend sees it.
texture : UInt64
load_op : LoadOp
store_op : StoreOp
clear_color : ClearColor
/// `Texture::id()`; the core resolves it before the backend sees it.
resolve_texture : UInt64?
}
///|
pub fn RenderTargetDesc::new(
/// `Texture::id()`; the core resolves it before the backend sees it.
texture : UInt64,
load_op? : LoadOp = LoadOp::Clear,
store_op? : StoreOp = StoreOp::Store,
clear_color? : ClearColor = ClearColor::black(),
/// `Texture::id()`; the core resolves it before the backend sees it.
resolve_texture? : UInt64,
) -> RenderTargetDesc {
{ texture, load_op, store_op, clear_color, resolve_texture }
}
///|
pub(all) struct DepthTargetDesc {
/// `Texture::id()`; the core resolves it before the backend sees it.
texture : UInt64
load_op : LoadOp
store_op : StoreOp
clear_depth : Float
stencil_load_op : LoadOp
stencil_store_op : StoreOp
clear_stencil : Int
}
///|
pub fn DepthTargetDesc::new(
/// `Texture::id()`; the core resolves it before the backend sees it.
texture : UInt64,
load_op? : LoadOp = LoadOp::Clear,
store_op? : StoreOp = StoreOp::DontCare,
clear_depth? : Float = (1.0 : Float),
stencil_load_op? : LoadOp = LoadOp::DontCare,
stencil_store_op? : StoreOp = StoreOp::DontCare,
clear_stencil? : Int = 0,
) -> DepthTargetDesc {
{
texture,
load_op,
store_op,
clear_depth,
stencil_load_op,
stencil_store_op,
clear_stencil,
}
}
// Re-export window events, cursor mode, and key codes so users only need `@tycho`.
///|
fn push_unique_int(list : Array[Int], value : Int) -> Unit {
if !list.contains(value) {
list.push(value)
}
}
///|
/// Build a `PipelineDesc` from vertex/fragment SPIR-V by reflecting the modules
/// (vertex inputs, descriptor bindings, push-constant size, entry points).
///
/// The reflector is pure MoonBit (`laplaceliu/tycho_shader/reflect`), so this
/// stays available on every target — unlike the glslang-backed compiler.
///|
/// Pack WGSL text into the shader blob channel.
///
/// WebGPU takes WGSL as text, but the pipeline channel is `FixedArray[UInt32]`,
/// so the established encoding (`tools/gen-wgsl-shaders.py`, and the legacy
/// `wgpu_backend.mbt`) is UTF-8 zero-padded to a 4-byte boundary. Packing it here
/// keeps that detail out of every renderer.
pub fn wgsl_words(text : String) -> FixedArray[UInt32] {
let bytes = @utf8.encode(text)
let n = bytes.length()
let words = FixedArray::make((n + 3) / 4, (0 : UInt32))
for i = 0; i < n; i = i + 1 {
let b = (bytes[i].to_int() & 0xFF).to_uint()
let shift = (i % 4) * 8
words[i / 4] = words[i / 4] | (b << shift)
}
words
}
///|
/// Swap this pipeline's shaders for WGSL equivalents.
///
/// Callers hold both forms because the format is chosen at runtime, not at build
/// time: the same example links against whichever backend the target provides
/// (ADR-005 D7). Pass `""` for a pipeline that has no WGSL yet -- the descriptor
/// is left alone, and a backend that needs WGSL will reject the SPIR-V by magic
/// number with a message naming the stage, rather than building a pipeline from
/// an empty module.
pub fn PipelineDesc::with_wgsl(
self : PipelineDesc,
vert : String,
frag : String,
) -> PipelineDesc {
if vert == "" || frag == "" {
return self
}
{ ..self, vert: wgsl_words(vert), frag: wgsl_words(frag) }
}
///|
pub fn PipelineDesc::from_spirv(
vert : FixedArray[UInt32],
frag : FixedArray[UInt32],
topology? : PrimitiveTopology = PrimitiveTopology::TriangleList,
depth_test? : Bool = false,
depth_write? : Bool = false,
depth_compare? : CompareOp = CompareOp::Less,
depth_format? : TextureFormat,
depth_only? : Bool = false,
depth_bias? : Bool = false,
depth_bias_constant? : Float = (0.0 : Float),
depth_bias_slope? : Float = (0.0 : Float),
stencil? : StencilState,
cull_mode? : CullMode = CullMode::None,
front_face? : FrontFace = FrontFace::Clockwise,
blend? : Bool = false,
blend_mode? : BlendMode,
blend_state? : BlendState,
color_write? : Bool = true,
samples? : SampleCount = SampleCount::X1,
color_format? : TextureFormat,
color_formats? : Array[TextureFormat] = [],
vertex_layouts? : Array[VertexBufferLayout] = [],
bindless_heap? : UInt64,
) -> PipelineDesc {
let vr = @reflect.reflect_spirv(vert)
let fr = @reflect.reflect_spirv(frag)
let vert_entry = if vr.entry_point != "" { vr.entry_point } else { "main" }
let frag_entry = if fr.entry_point != "" { fr.entry_point } else { "main" }
let vertex_formats : Array[VertexFormat] = []
for input in vr.vertex_inputs {
match input.format {
@reflect.ReflectedVertexFormat::Float1 => vertex_formats.push(VertexFormat::Float1)
@reflect.ReflectedVertexFormat::Float2 => vertex_formats.push(VertexFormat::Float2)
@reflect.ReflectedVertexFormat::Float3 => vertex_formats.push(VertexFormat::Float3)
@reflect.ReflectedVertexFormat::Float4 => vertex_formats.push(VertexFormat::Float4)
@reflect.ReflectedVertexFormat::Int1 => vertex_formats.push(VertexFormat::Int1)
@reflect.ReflectedVertexFormat::Uint1 => vertex_formats.push(VertexFormat::Uint1)
_ => ()
}
}
// A shader that declares vertex inputs needs a layout to match, and WebGPU
// validates that strictly ("Vertex attribute slot 0 ... is not present in the
// VertexState"). Vulkan never complained because the inputs are baked into the
// SPIR-V, so a pipeline built without an explicit layout worked there. When the
// caller supplies none, pack the reflected inputs in declaration order -- which
// is exactly the shape of a tightly packed vertex array.
let resolved_layouts : Array[VertexBufferLayout] = []
if vertex_layouts.length() > 0 {
for layout in vertex_layouts {
resolved_layouts.push(layout)
}
} else if vr.vertex_inputs.length() > 0 {
let attrs : Array[VertexAttribute] = []
let mut offset = 0
for input in vr.vertex_inputs {
let (fmt, size) = match input.format {
@reflect.ReflectedVertexFormat::Float1 => (VertexFormat::Float1, 4)
@reflect.ReflectedVertexFormat::Float2 => (VertexFormat::Float2, 8)
@reflect.ReflectedVertexFormat::Float3 => (VertexFormat::Float3, 12)
@reflect.ReflectedVertexFormat::Float4 => (VertexFormat::Float4, 16)
@reflect.ReflectedVertexFormat::Int1 => (VertexFormat::Int1, 4)
@reflect.ReflectedVertexFormat::Uint1 => (VertexFormat::Uint1, 4)
_ => (VertexFormat::Float4, 16)
}
attrs.push(VertexAttribute::new(input.location, fmt, offset))
offset = offset + size
}
resolved_layouts.push(VertexBufferLayout::new(offset, attrs))
}
let u_binds : Array[Int] = []
let t_binds : Array[Int] = []
let s_binds : Array[Int] = []
let si_binds : Array[Int] = []
let a_binds : Array[Int] = []
let all_bindings : Array[@reflect.ReflectedBinding] = []
for binding in vr.bindings {
all_bindings.push(binding)
}
for binding in fr.bindings {
all_bindings.push(binding)
}
for binding in all_bindings {
if binding.set != 0 {
continue
}
match binding.kind {
@reflect.ReflectedDescriptorKind::UniformBuffer =>
push_unique_int(u_binds, binding.binding)
@reflect.ReflectedDescriptorKind::CombinedImageSampler |
@reflect.ReflectedDescriptorKind::SampledImage =>
push_unique_int(t_binds, binding.binding)
@reflect.ReflectedDescriptorKind::StorageBuffer =>
push_unique_int(s_binds, binding.binding)
@reflect.ReflectedDescriptorKind::StorageImage =>
push_unique_int(si_binds, binding.binding)
@reflect.ReflectedDescriptorKind::AccelerationStructure =>
push_unique_int(a_binds, binding.binding)
@reflect.ReflectedDescriptorKind::Sampler => ()
}
}
u_binds.sort_by(fn(a, b) { a - b })
t_binds.sort_by(fn(a, b) { a - b })
s_binds.sort_by(fn(a, b) { a - b })
si_binds.sort_by(fn(a, b) { a - b })
a_binds.sort_by(fn(a, b) { a - b })
let push_constant_size = if vr.push_constant_size > fr.push_constant_size {
vr.push_constant_size
} else {
fr.push_constant_size
}
let resolved_formats : Array[TextureFormat] = []
if color_formats.length() > 0 {
for format in color_formats {
resolved_formats.push(format)
}
} else if color_format is None && fr.color_output_count > 1 {
for i = 0; i < fr.color_output_count; i = i + 1 {
let _ = i
resolved_formats.push(TextureFormat::RGBA8)
}
}
PipelineDesc::new(
vert,
frag,
vert_entry~,
frag_entry~,
vertex_formats~,
uniform_slots=u_binds.length(),
vertex_layouts=resolved_layouts,
texture_slots=t_binds.length(),
storage_slots=s_binds.length(),
storage_image_slots=si_binds.length(),
accel_slots=a_binds.length(),
uniform_bindings=u_binds,
texture_bindings=t_binds,
storage_bindings=s_binds,
storage_image_bindings=si_binds,
accel_bindings=a_binds,
push_constant_size~,
topology~,
depth_test~,
depth_write~,
depth_compare~,
depth_format?=depth_format,
depth_only~,
depth_bias~,
depth_bias_constant~,
depth_bias_slope~,
stencil?=stencil,
cull_mode~,
front_face~,
blend~,
blend_mode?=blend_mode,
blend_state?=blend_state,
color_write~,
samples~,
color_format?=color_format,
color_formats=resolved_formats,
bindless_heap?=bindless_heap,
)
}