// 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 same_spv(a : FixedArray[UInt32], b : FixedArray[UInt32]) -> Bool {
  let n = a.length()
  if n != b.length() || n == 0 {
    return false
  }
  a[0] == b[0] && a[n / 2] == b[n / 2] && a[n - 1] == b[n - 1]
}

///|
fn spv_to_dxil(spv : FixedArray[UInt32]) -> FixedArray[UInt32] {
  if same_spv(spv, spv_prim2d_vert) {
    dxil_prim2d_vert
  } else if same_spv(spv, spv_prim2d_frag) {
    dxil_prim2d_frag
  } else if same_spv(spv, spv_prim2d_bindless_frag) {
    dxil_prim2d_bindless_frag
  } else if same_spv(spv, spv_path_stencil_vert) {
    dxil_path_stencil_vert
  } else if same_spv(spv, spv_path_stencil_frag) {
    dxil_path_stencil_frag
  } else if same_spv(spv, spv_path_cover_vert) {
    dxil_path_cover_vert
  } else if same_spv(spv, spv_path_cover_frag) {
    dxil_path_cover_frag
  } else if same_spv(spv, spv_blur_vert) {
    dxil_blur_vert
  } else if same_spv(spv, spv_blur_frag) {
    dxil_blur_frag
  } else if same_spv(spv, spv_vertices_vert) {
    dxil_vertices_vert
  } else if same_spv(spv, spv_vertices_frag) {
    dxil_vertices_frag
  } else {
    spv
  }
}

///|
fn spv_to_msl(spv : FixedArray[UInt32]) -> FixedArray[UInt32] {
  if same_spv(spv, spv_prim2d_vert) {
    msl_prim2d_vert
  } else if same_spv(spv, spv_prim2d_frag) {
    msl_prim2d_frag
  } else if same_spv(spv, spv_prim2d_bindless_frag) {
    msl_prim2d_bindless_frag
  } else if same_spv(spv, spv_path_stencil_vert) {
    msl_path_stencil_vert
  } else if same_spv(spv, spv_path_stencil_frag) {
    msl_path_stencil_frag
  } else if same_spv(spv, spv_path_cover_vert) {
    msl_path_cover_vert
  } else if same_spv(spv, spv_path_cover_frag) {
    msl_path_cover_frag
  } else if same_spv(spv, spv_blur_vert) {
    msl_blur_vert
  } else if same_spv(spv, spv_blur_frag) {
    msl_blur_frag
  } else if same_spv(spv, spv_vertices_vert) {
    msl_vertices_vert
  } else if same_spv(spv, spv_vertices_frag) {
    msl_vertices_frag
  } else {
    spv
  }
}

///|
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
      }
    }
    @rhi.ShaderLanguage::Dxil =>
      { ..desc, vert: spv_to_dxil(desc.vert), frag: spv_to_dxil(desc.frag) }
    @rhi.ShaderLanguage::Msl =>
      { ..desc, vert: spv_to_msl(desc.vert), frag: spv_to_msl(desc.frag) }
    _ => 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,
  )
}