// Shader language dispatch for the 3D renderer (SPIR-V vs DXIL).

///|
fn same_spv_3d(a : FixedArray[UInt32], b : FixedArray[UInt32]) -> Bool {
  if physical_equal(a, b) {
    return true
  }
  let n = a.length()
  if n != b.length() || n == 0 {
    return false
  }
  for i = 0; i < n; i = i + 1 {
    if a[i] != b[i] {
      return false
    }
  }
  true
}

///|
fn spv_to_dxil_3d(spv : FixedArray[UInt32]) -> FixedArray[UInt32] {
  if same_spv_3d(spv, spv_bloom_blur_frag) {
    dxil_bloom_blur_frag
  } else if same_spv_3d(spv, spv_bloom_extract_frag) {
    dxil_bloom_extract_frag
  } else if same_spv_3d(spv, spv_brdf_lut_frag) {
    dxil_brdf_lut_frag
  } else if same_spv_3d(spv, spv_cull_comp) {
    dxil_cull_comp
  } else if same_spv_3d(spv, spv_custom_flat_frag) {
    dxil_custom_flat_frag
  } else if same_spv_3d(spv, spv_custom_flat_vert) {
    dxil_custom_flat_vert
  } else if same_spv_3d(spv, spv_fullscreen_vert) {
    dxil_fullscreen_vert
  } else if same_spv_3d(spv, spv_lines_frag) {
    dxil_lines_frag
  } else if same_spv_3d(spv, spv_lines_vert) {
    dxil_lines_vert
  } else if same_spv_3d(spv, spv_pbr_frag) {
    dxil_pbr_frag
  } else if same_spv_3d(spv, spv_pbr_frag_nobindless) {
    dxil_pbr_frag_nobindless
  } else if same_spv_3d(spv, spv_pbr_frag_rt) {
    dxil_pbr_frag_rt
  } else if same_spv_3d(spv, spv_pbr_frag_transmission) {
    dxil_pbr_frag_transmission
  } else if same_spv_3d(spv, spv_pbr_frag_transmission_nobindless) {
    dxil_pbr_frag_transmission_nobindless
  } else if same_spv_3d(spv, spv_pbr_vert) {
    dxil_pbr_vert
  } else if same_spv_3d(spv, spv_pbr_cull_vert) {
    dxil_pbr_cull_vert
  } else if same_spv_3d(spv, spv_pbr_morph_vert) {
    dxil_pbr_morph_vert
  } else if same_spv_3d(spv, spv_pbr_skin_vert) {
    dxil_pbr_skin_vert
  } else if same_spv_3d(spv, spv_prefilter_frag) {
    dxil_prefilter_frag
  } else if same_spv_3d(spv, spv_prefilter_vert) {
    dxil_prefilter_vert
  } else if same_spv_3d(spv, spv_shadow_vert) {
    dxil_shadow_vert
  } else if same_spv_3d(spv, spv_shadow_morph_vert) {
    dxil_shadow_morph_vert
  } else if same_spv_3d(spv, spv_shadow_skin_vert) {
    dxil_shadow_skin_vert
  } else if same_spv_3d(spv, spv_skybox_frag) {
    dxil_skybox_frag
  } else if same_spv_3d(spv, spv_skybox_vert) {
    dxil_skybox_vert
  } else if same_spv_3d(spv, spv_ssao_frag) {
    dxil_ssao_frag
  } else if same_spv_3d(spv, spv_ssao_blur_frag) {
    dxil_ssao_blur_frag
  } else if same_spv_3d(spv, spv_taa_frag) {
    dxil_taa_frag
  } else if same_spv_3d(spv, spv_tonemap_frag) {
    dxil_tonemap_frag
  } else {
    spv
  }
}

///|
fn spv_to_msl_3d(spv : FixedArray[UInt32]) -> FixedArray[UInt32] {
  if same_spv_3d(spv, spv_bloom_blur_frag) {
    msl_bloom_blur_frag
  } else if same_spv_3d(spv, spv_bloom_extract_frag) {
    msl_bloom_extract_frag
  } else if same_spv_3d(spv, spv_brdf_lut_frag) {
    msl_brdf_lut_frag
  } else if same_spv_3d(spv, spv_cull_comp) {
    msl_cull_comp
  } else if same_spv_3d(spv, spv_custom_flat_frag) {
    msl_custom_flat_frag
  } else if same_spv_3d(spv, spv_custom_flat_vert) {
    msl_custom_flat_vert
  } else if same_spv_3d(spv, spv_fullscreen_vert) {
    msl_fullscreen_vert
  } else if same_spv_3d(spv, spv_lines_frag) {
    msl_lines_frag
  } else if same_spv_3d(spv, spv_lines_vert) {
    msl_lines_vert
  } else if same_spv_3d(spv, spv_pbr_frag) {
    msl_pbr_frag
  } else if same_spv_3d(spv, spv_pbr_frag_nobindless) {
    msl_pbr_frag_nobindless
  } else if same_spv_3d(spv, spv_pbr_frag_rt) {
    msl_pbr_frag_rt
  } else if same_spv_3d(spv, spv_pbr_frag_transmission) {
    msl_pbr_frag_transmission
  } else if same_spv_3d(spv, spv_pbr_frag_transmission_nobindless) {
    msl_pbr_frag_transmission_nobindless
  } else if same_spv_3d(spv, spv_pbr_vert) {
    msl_pbr_vert
  } else if same_spv_3d(spv, spv_pbr_cull_vert) {
    msl_pbr_cull_vert
  } else if same_spv_3d(spv, spv_pbr_morph_vert) {
    msl_pbr_morph_vert
  } else if same_spv_3d(spv, spv_pbr_skin_vert) {
    msl_pbr_skin_vert
  } else if same_spv_3d(spv, spv_prefilter_frag) {
    msl_prefilter_frag
  } else if same_spv_3d(spv, spv_prefilter_vert) {
    msl_prefilter_vert
  } else if same_spv_3d(spv, spv_shadow_vert) {
    msl_shadow_vert
  } else if same_spv_3d(spv, spv_shadow_morph_vert) {
    msl_shadow_morph_vert
  } else if same_spv_3d(spv, spv_shadow_skin_vert) {
    msl_shadow_skin_vert
  } else if same_spv_3d(spv, spv_skybox_frag) {
    msl_skybox_frag
  } else if same_spv_3d(spv, spv_skybox_vert) {
    msl_skybox_vert
  } else if same_spv_3d(spv, spv_ssao_frag) {
    msl_ssao_frag
  } else if same_spv_3d(spv, spv_ssao_blur_frag) {
    msl_ssao_blur_frag
  } else if same_spv_3d(spv, spv_taa_frag) {
    msl_taa_frag
  } else if same_spv_3d(spv, spv_tonemap_frag) {
    msl_tonemap_frag
  } else {
    spv
  }
}

///|
fn create_3d_pipeline(
  dev : @rhi.Device,
  desc : @rhi.PipelineDesc,
) -> @rhi.Pipeline raise @rhi.RhiError {
  let final_desc = match dev.shader_language() {
    @rhi.ShaderLanguage::Dxil =>
      {
        ..desc,
        vert: spv_to_dxil_3d(desc.vert),
        frag: if desc.frag.length() > 0 {
          spv_to_dxil_3d(desc.frag)
        } else {
          desc.frag
        },
      }
    @rhi.ShaderLanguage::Msl =>
      {
        ..desc,
        vert: spv_to_msl_3d(desc.vert),
        frag: if desc.frag.length() > 0 {
          spv_to_msl_3d(desc.frag)
        } else {
          desc.frag
        },
      }
    _ => desc
  }
  dev.create_graphics_pipeline(final_desc)
}

///|
fn create_3d_compute_pipeline(
  dev : @rhi.Device,
  cs : FixedArray[UInt32],
) -> @rhi.Pipeline raise @rhi.RhiError {
  let final_cs = match dev.shader_language() {
    @rhi.ShaderLanguage::Dxil => spv_to_dxil_3d(cs)
    @rhi.ShaderLanguage::Msl => spv_to_msl_3d(cs)
    _ => cs
  }
  dev.create_compute_pipeline_auto(final_cs)
}