// Tycho RHI public types, descriptors, errors, and shader/window re-exports.

///|
pub suberror RhiError {
  InitFailed(String)
  DeviceFailed(String)
  SwapchainFailed(String)
  PipelineFailed(String)
  AllocationFailed(String)
  CommandFailed(String)
  InvalidArgument(String)
}

///|
pub impl Show for RhiError with output(self, logger) {
  match self {
    InitFailed(msg) => logger.write_string("InitFailed(\{msg})")
    DeviceFailed(msg) => logger.write_string("DeviceFailed(\{msg})")
    SwapchainFailed(msg) => logger.write_string("SwapchainFailed(\{msg})")
    PipelineFailed(msg) => logger.write_string("PipelineFailed(\{msg})")
    AllocationFailed(msg) => logger.write_string("AllocationFailed(\{msg})")
    CommandFailed(msg) => logger.write_string("CommandFailed(\{msg})")
    InvalidArgument(msg) => logger.write_string("InvalidArgument(\{msg})")
  }
}

///|
#derive(Eq, Debug)
pub(all) enum BackendKind {
  Vulkan
}

///|
#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
}

///|
pub fn DeviceDesc::new(
  app_name : String,
  validation? : Bool = false,
  headless? : Bool = false,
  pipeline_cache? : Bytes = b"",
) -> DeviceDesc {
  { app_name, validation, headless, pipeline_cache }
}

///|
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
  supports_anisotropy : Bool
  supports_indirect_draw : Bool
  supports_bindless : Bool
  supports_mesh_shader : Bool
  supports_ray_query : Bool
}

///|
#derive(Eq, Debug)
pub(all) enum BufferKind {
  Vertex
  Index
  Uniform
  Storage
  Staging
  Indirect
}

///|
#derive(Eq, Debug)
pub(all) enum IndexType {
  Uint16
  Uint32
}

///|
#derive(Eq, Debug)
pub(all) enum ShaderStage {
  Vertex
  Fragment
  Compute
  Task
  Mesh
}

///|
#derive(Eq, Debug)
pub(all) enum VertexFormat {
  Float2
  Float3
  Float4
}

///|
#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
}

///|
#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? = None,
) -> 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
  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
  topology : PrimitiveTopology
  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
  cull_mode : CullMode
  front_face : FrontFace
  blend : Bool
  blend_mode : BlendMode?
  samples : SampleCount
  color_format : TextureFormat?
  color_formats : Array[TextureFormat]
  bindless_heap : BindlessHeap?
}

///|
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,
  uniform_bindings? : Array[Int] = [],
  texture_bindings? : Array[Int] = [],
  storage_bindings? : Array[Int] = [],
  storage_image_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? = None,
  depth_only? : Bool = false,
  depth_bias? : Bool = false,
  depth_bias_constant? : Float = (0.0 : Float),
  depth_bias_slope? : Float = (0.0 : Float),
  cull_mode? : CullMode = CullMode::None,
  front_face? : FrontFace = FrontFace::Clockwise,
  blend? : Bool = false,
  blend_mode? : BlendMode? = None,
  samples? : SampleCount = SampleCount::X1,
  color_format? : TextureFormat? = None,
  color_formats? : Array[TextureFormat] = [],
  bindless_heap? : BindlessHeap? = None,
) -> PipelineDesc {
  {
    vert,
    frag,
    vert_entry,
    frag_entry,
    vertex_formats,
    vertex_layouts,
    uniform_slots,
    texture_slots,
    storage_slots,
    storage_image_slots,
    uniform_bindings,
    texture_bindings,
    storage_bindings,
    storage_image_bindings,
    push_constant_size,
    topology,
    depth_test,
    depth_write,
    depth_compare,
    depth_format,
    depth_only,
    depth_bias,
    depth_bias_constant,
    depth_bias_slope,
    cull_mode,
    front_face,
    blend,
    blend_mode,
    samples,
    color_format,
    color_formats,
    bindless_heap,
  }
}

///|
fn push_unique_int(arr : Array[Int], v : Int) -> Unit {
  for x in arr {
    if x == v {
      return
    }
  }
  arr.push(v)
}

///|
// Builds a PipelineDesc by automatically reflecting SPIR-V bytecode for vertex
// inputs, entry points, descriptor bindings, push constant byte size, and MRT color targets.
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? = None,
  depth_only? : Bool = false,
  depth_bias? : Bool = false,
  depth_bias_constant? : Float = (0.0 : Float),
  depth_bias_slope? : Float = (0.0 : Float),
  cull_mode? : CullMode = CullMode::None,
  front_face? : FrontFace = FrontFace::Clockwise,
  blend? : Bool = false,
  blend_mode? : BlendMode? = None,
  samples? : SampleCount = SampleCount::X1,
  color_format? : TextureFormat? = None,
  color_formats? : Array[TextureFormat] = [],
  vertex_layouts? : Array[VertexBufferLayout] = [],
  bindless_heap? : BindlessHeap? = None,
) -> PipelineDesc {
  let vr = @shader.reflect_spirv(vert)
  let fr = @shader.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 vi in vr.vertex_inputs {
    match vi.format {
      @shader.ReflectedVertexFormat::Float2 =>
        vertex_formats.push(VertexFormat::Float2)
      @shader.ReflectedVertexFormat::Float3 =>
        vertex_formats.push(VertexFormat::Float3)
      @shader.ReflectedVertexFormat::Float4 =>
        vertex_formats.push(VertexFormat::Float4)
      _ => ()
    }
  }
  let u_binds : Array[Int] = []
  let t_binds : Array[Int] = []
  let s_binds : Array[Int] = []
  let si_binds : Array[Int] = []
  let all_b : Array[@shader.ReflectedBinding] = []
  for b in vr.bindings {
    all_b.push(b)
  }
  for b in fr.bindings {
    all_b.push(b)
  }
  for b in all_b {
    if b.set != 0 {
      continue
    }
    match b.kind {
      @shader.ReflectedDescriptorKind::UniformBuffer =>
        push_unique_int(u_binds, b.binding)
      @shader.ReflectedDescriptorKind::CombinedImageSampler |
      @shader.ReflectedDescriptorKind::SampledImage =>
        push_unique_int(t_binds, b.binding)
      @shader.ReflectedDescriptorKind::StorageBuffer =>
        push_unique_int(s_binds, b.binding)
      @shader.ReflectedDescriptorKind::StorageImage =>
        push_unique_int(si_binds, b.binding)
      @shader.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 })
  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 f in color_formats {
      resolved_formats.push(f)
    }
  } 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~,
    vertex_layouts~,
    uniform_slots=u_binds.length(),
    texture_slots=t_binds.length(),
    storage_slots=s_binds.length(),
    storage_image_slots=si_binds.length(),
    uniform_bindings=u_binds,
    texture_bindings=t_binds,
    storage_bindings=s_binds,
    storage_image_bindings=si_binds,
    push_constant_size~,
    topology~,
    depth_test~,
    depth_write~,
    depth_compare~,
    depth_format~,
    depth_only~,
    depth_bias~,
    depth_bias_constant~,
    depth_bias_slope~,
    cull_mode~,
    front_face~,
    blend~,
    blend_mode~,
    samples~,
    color_format~,
    color_formats=resolved_formats,
    bindless_heap~,
  )
}

///|
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
  depth_test : Bool
  depth_write : Bool
  depth_compare : CompareOp
  depth_format : TextureFormat?
  cull_mode : CullMode
  front_face : FrontFace
  blend : Bool
  blend_mode : BlendMode?
  samples : SampleCount
  color_format : TextureFormat?
  color_formats : Array[TextureFormat]
  bindless_heap : BindlessHeap?
}

///|
pub fn MeshPipelineDesc::new(
  mesh : FixedArray[UInt32],
  frag : FixedArray[UInt32],
  task? : FixedArray[UInt32]? = None,
  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? = None,
  cull_mode? : CullMode = CullMode::None,
  front_face? : FrontFace = FrontFace::Clockwise,
  blend? : Bool = false,
  blend_mode? : BlendMode? = None,
  samples? : SampleCount = SampleCount::X1,
  color_format? : TextureFormat? = None,
  color_formats? : Array[TextureFormat] = [],
  bindless_heap? : BindlessHeap? = None,
) -> 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,
    depth_test,
    depth_write,
    depth_compare,
    depth_format,
    cull_mode,
    front_face,
    blend,
    blend_mode,
    samples,
    color_format,
    color_formats,
    bindless_heap,
  }
}

///|
pub(all) struct SwapchainDesc {
  title : String
  width : Int
  height : Int
  depth : Bool
  clear_color : ClearColor
  enable_readback : Bool
  present_mode : PresentMode
}

///|
pub fn SwapchainDesc::new(
  title : String,
  width : Int,
  height : Int,
  depth? : Bool = false,
  clear_color? : ClearColor = ClearColor::black(),
  enable_readback? : Bool = false,
  present_mode? : PresentMode = PresentMode::Fifo,
) -> SwapchainDesc {
  {
    title,
    width,
    height,
    depth,
    clear_color,
    enable_readback,
    present_mode,
  }
}

///|
pub(all) struct RenderTargetDesc {
  texture : Texture
  load_op : LoadOp
  store_op : StoreOp
  clear_color : ClearColor
  resolve_texture : Texture?
}

///|
pub fn RenderTargetDesc::new(
  texture : Texture,
  load_op? : LoadOp = LoadOp::Clear,
  store_op? : StoreOp = StoreOp::Store,
  clear_color? : ClearColor = ClearColor::black(),
  resolve_texture? : Texture? = None,
) -> RenderTargetDesc {
  { texture, load_op, store_op, clear_color, resolve_texture }
}

///|
pub(all) struct DepthTargetDesc {
  texture : Texture
  load_op : LoadOp
  store_op : StoreOp
  clear_depth : Float
}

///|
pub fn DepthTargetDesc::new(
  texture : Texture,
  load_op? : LoadOp = LoadOp::Clear,
  store_op? : StoreOp = StoreOp::DontCare,
  clear_depth? : Float = (1.0 : Float),
) -> DepthTargetDesc {
  { texture, load_op, store_op, clear_depth }
}

// Re-export window events, cursor mode, and key codes so users only need `@tycho`.

///|
pub type Event = @win.Event

///|
pub type CursorMode = @win.CursorMode

///|
pub const KEY_ESCAPE : Int = @win.KEY_ESCAPE

///|
pub const KEY_RETURN : Int = @win.KEY_RETURN

///|
pub const KEY_TAB : Int = @win.KEY_TAB

///|
pub const KEY_BACKSPACE : Int = @win.KEY_BACKSPACE

///|
pub const KEY_SPACE : Int = @win.KEY_SPACE

///|
pub const KEY_LEFT : Int = @win.KEY_LEFT

///|
pub const KEY_UP : Int = @win.KEY_UP

///|
pub const KEY_RIGHT : Int = @win.KEY_RIGHT

///|
pub const KEY_DOWN : Int = @win.KEY_DOWN

///|
pub const KEY_0 : Int = @win.KEY_0

///|
pub const KEY_1 : Int = @win.KEY_1

///|
pub const KEY_2 : Int = @win.KEY_2

///|
pub const KEY_3 : Int = @win.KEY_3

///|
pub const KEY_4 : Int = @win.KEY_4

///|
pub const KEY_5 : Int = @win.KEY_5

///|
pub const KEY_6 : Int = @win.KEY_6

///|
pub const KEY_7 : Int = @win.KEY_7

///|
pub const KEY_8 : Int = @win.KEY_8

///|
pub const KEY_9 : Int = @win.KEY_9

///|
pub const KEY_A : Int = @win.KEY_A

///|
pub const KEY_B : Int = @win.KEY_B

///|
pub const KEY_C : Int = @win.KEY_C

///|
pub const KEY_D : Int = @win.KEY_D

///|
pub const KEY_E : Int = @win.KEY_E

///|
pub const KEY_F : Int = @win.KEY_F

///|
pub const KEY_G : Int = @win.KEY_G

///|
pub const KEY_H : Int = @win.KEY_H

///|
pub const KEY_I : Int = @win.KEY_I

///|
pub const KEY_J : Int = @win.KEY_J

///|
pub const KEY_K : Int = @win.KEY_K

///|
pub const KEY_L : Int = @win.KEY_L

///|
pub const KEY_M : Int = @win.KEY_M

///|
pub const KEY_N : Int = @win.KEY_N

///|
pub const KEY_O : Int = @win.KEY_O

///|
pub const KEY_P : Int = @win.KEY_P

///|
pub const KEY_Q : Int = @win.KEY_Q

///|
pub const KEY_R : Int = @win.KEY_R

///|
pub const KEY_S : Int = @win.KEY_S

///|
pub const KEY_T : Int = @win.KEY_T

///|
pub const KEY_U : Int = @win.KEY_U

///|
pub const KEY_V : Int = @win.KEY_V

///|
pub const KEY_W : Int = @win.KEY_W

///|
pub const KEY_X : Int = @win.KEY_X

///|
pub const KEY_Y : Int = @win.KEY_Y

///|
pub const KEY_Z : Int = @win.KEY_Z

///|
pub const KEY_F1 : Int = @win.KEY_F1

///|
pub const KEY_F2 : Int = @win.KEY_F2

///|
pub const KEY_F3 : Int = @win.KEY_F3

///|
pub const KEY_F4 : Int = @win.KEY_F4

///|
pub const KEY_F5 : Int = @win.KEY_F5

///|
pub const KEY_F6 : Int = @win.KEY_F6

///|
pub const KEY_F7 : Int = @win.KEY_F7

///|
pub const KEY_F8 : Int = @win.KEY_F8

///|
pub const KEY_F9 : Int = @win.KEY_F9

///|
pub const KEY_F10 : Int = @win.KEY_F10

///|
pub const KEY_F11 : Int = @win.KEY_F11

///|
pub const KEY_F12 : Int = @win.KEY_F12

///|
pub const KEY_LSHIFT : Int = @win.KEY_LSHIFT

///|
pub const KEY_RSHIFT : Int = @win.KEY_RSHIFT

///|
pub const KEY_LCTRL : Int = @win.KEY_LCTRL

///|
pub const KEY_RCTRL : Int = @win.KEY_RCTRL

///|
pub const KEY_LALT : Int = @win.KEY_LALT

///|
pub const KEY_RALT : Int = @win.KEY_RALT

// Shader compilation, SPIR-V reflection & image loading helpers (via laplaceliu/tycho_shader).

///|
pub type ReflectedStage = @shader.ReflectedStage

///|
pub type ReflectedDescriptorKind = @shader.ReflectedDescriptorKind

///|
pub type ReflectedVertexFormat = @shader.ReflectedVertexFormat

///|
pub type ReflectedBinding = @shader.ReflectedBinding

///|
pub type ReflectedVertexInput = @shader.ReflectedVertexInput

///|
pub type SpirvReflection = @shader.SpirvReflection

///|
pub fn reflect_spirv(spv : FixedArray[UInt32]) -> SpirvReflection {
  @shader.reflect_spirv(spv)
}

///|
fn shader_kind(stage : ShaderStage) -> UInt32 {
  match stage {
    ShaderStage::Vertex => @shader.SHADER_KIND_VERTEX
    ShaderStage::Fragment => @shader.SHADER_KIND_FRAGMENT
    ShaderStage::Compute => @shader.SHADER_KIND_COMPUTE
    ShaderStage::Task => @shader.SHADER_KIND_TASK
    ShaderStage::Mesh => @shader.SHADER_KIND_MESH
  }
}

///|
pub fn compile_glsl(path : String, stage : ShaderStage) -> FixedArray[UInt32] {
  match stage {
    ShaderStage::Task | ShaderStage::Mesh =>
      @shader.compile_shader_file_env(
        path,
        shader_kind(stage),
        @shader.SHADER_ENV_VULKAN_1_3,
      )
    _ => @shader.compile_shader_file(path, shader_kind(stage))
  }
}

///|
pub fn compile_glsl_source(source : String, stage : ShaderStage) -> FixedArray[UInt32] {
  match stage {
    ShaderStage::Task | ShaderStage::Mesh =>
      @shader.compile_shader_env(
        source,
        shader_kind(stage),
        @shader.SHADER_ENV_VULKAN_1_3,
      )
    _ => @shader.compile_shader(source, shader_kind(stage))
  }
}

///|
pub fn compile_slang(path : String, entry : String) -> FixedArray[UInt32] {
  @shader.compile_slang_file(path, entry)
}

///|
pub fn load_spirv(path : String) -> FixedArray[UInt32] {
  @shader.load_spirv(path)
}

///|
pub fn load_image(path : String) -> (FixedArray[Byte], Int, Int) {
  let (px, w, h) = @shader.load_image(path)
  (px, w.to_int(), h.to_int())
}

///|
pub fn read_file_bytes(path : String) -> Bytes {
  @shader.read_file_bytes(path)
}

///|
pub fn want_window() -> Bool {
  @shader.want_window()
}

///|
pub fn screenshot_path() -> String {
  @shader.screenshot_path()
}

///|
pub fn save_png(
  path : String,
  width : Int,
  height : Int,
  rgba : FixedArray[Byte],
  bgra? : Bool = false,
) -> Bool {
  @shader.save_png(path, width, height, rgba, bgra~)
}

///|
pub fn load_hdr_image(path : String) -> (FixedArray[Float], Int, Int) {
  let (px, w, h) = @shader.load_hdr_image(path)
  (px, w.to_int(), h.to_int())
}