// Which shader blob the device gets (ADR-005 D7).
//
// The Vulkan path embeds SPIR-V; a browser takes WGSL. Both ride the same
// `FixedArray[UInt32]` channel, so a pipeline descriptor does not need a second
// set of fields -- but the renderer does hold both forms, because the choice is
// made at runtime by whichever backend the target links.
//
// Two rules keep the failure mode honest:
// * ask `Device::shader_language()`, never `BackendKind` directly, so a future
// DXIL backend is a value here instead of a new branch in every renderer;
// * an empty WGSL source leaves the descriptor alone. The js backend then
// rejects the SPIR-V by magic number and names the stage, which is a better
// error than a pipeline built from an empty module.
///|
fn prefer_wgsl(
dev : @rhi.Device,
desc : @rhi.PipelineDesc,
vert_wgsl : String,
frag_wgsl : String,
layout? : @rhi.BindingLayout,
) -> @rhi.PipelineDesc {
match dev.shader_language() {
@rhi.ShaderLanguage::Wgsl => {
let swapped = desc.with_wgsl(vert_wgsl, frag_wgsl)
// `layout` only matters when the WGSL actually replaced the SPIR-V: the
// binding numbers a SPIR-V pipeline uses are the ones its GLSL declares,
// and `BindingLayout::legacy` already describes those.
match layout {
Some(l) => { ..swapped, layout: l }
None => swapped
}
}
_ => desc
}
}
///|
/// The binding numbers a shader actually declares, as the WGSL generator recorded
/// them (`tools/gen-wgsl-shaders.py`).
///
/// The backend cannot derive these: WGSL has no push constants and no combined
/// sampler, so a converted shader has *more* bindings than the GLSL it came from,
/// and where they land depends on which numbers the GLSL already occupied. Passing
/// the generator's own numbers is what makes `bind_texture`, `bind_storage` and
/// `push_constants_*` agree with the shader instead of agreeing with a formula.
fn pipeline_layout(
texture_base : Int,
sampler_base : Int,
storage_base : Int,
push : Int,
) -> @rhi.BindingLayout {
{
texture_base,
sampler_base,
storage_base,
uniform_base: 0,
push,
}
}
///|
/// `prim2d`: storage 0/1 (prim + gradient buffers), atlases 2/3/4, samplers
/// 5/6/7, push block 8.
fn prim2d_layout() -> @rhi.BindingLayout {
pipeline_layout(
wgsl_prim2d_texture_base,
wgsl_prim2d_sampler_base,
wgsl_prim2d_storage_base,
wgsl_prim2d_push_binding,
)
}
///|
/// `path_stencil`: push constants only (binding 0), and its fragment stage has no
/// resources at all.
fn path_stencil_layout() -> @rhi.BindingLayout {
pipeline_layout(
wgsl_path_stencil_texture_base,
wgsl_path_stencil_sampler_base,
wgsl_path_stencil_storage_base,
wgsl_path_stencil_push_binding,
)
}
///|
fn path_cover_layout() -> @rhi.BindingLayout {
pipeline_layout(
wgsl_path_cover_texture_base,
wgsl_path_cover_sampler_base,
wgsl_path_cover_storage_base,
wgsl_path_cover_push_binding,
)
}
///|
/// `blur`: one texture at 0, its sampler at 2 (the combined declaration used 0),
/// the colour-matrix storage buffer keeps 1, push block 3.
fn blur_layout() -> @rhi.BindingLayout {
pipeline_layout(
wgsl_blur_texture_base,
wgsl_blur_sampler_base,
wgsl_blur_storage_base,
wgsl_blur_push_binding,
)
}
///|
fn vertices_layout() -> @rhi.BindingLayout {
pipeline_layout(
wgsl_vertices_texture_base,
wgsl_vertices_sampler_base,
wgsl_vertices_storage_base,
wgsl_vertices_push_binding,
)
}