// Direct3D 12 bindings for Tycho — M1a: hand-written minimal surface.
//
// Everything here is pure MoonBit: d3d12.dll / dxgi.dll are loaded at runtime by
// the native shim, no import libraries are needed, and every COM vtable call
// lives behind these functions. The shipped surface is deliberately small
// (device / command list / pipeline / render target / readback) until the
// codegen takes over in M1b.

// ---------------------------------------------------------------------------
// Native shim
// ---------------------------------------------------------------------------

extern "c" fn init_ffi() -> Int32 = "tycho_dx12_init"

extern "c" fn last_error_ffi() -> Int32 = "tycho_dx12_last_error"

extern "c" fn env_requests_warp_ffi() -> Int32 = "tycho_dx12_env_requests_warp"

#borrow(values)
extern "c" fn int32_ptr_ffi(values : FixedArray[Int32]) -> UInt64 = "tycho_dx12_as_ptr"

#borrow(values)
extern "c" fn bytes_ptr_ffi(values : FixedArray[Byte]) -> UInt64 = "tycho_dx12_as_ptr"

#borrow(values)
extern "c" fn int64_ptr_ffi(values : FixedArray[Int64]) -> UInt64 = "tycho_dx12_as_ptr"

extern "c" fn com_query_interface_ffi(obj : UInt64, riid : UInt64, ppv_out : UInt64) -> Int32 = "tycho_dx12_com_query_interface"

extern "c" fn com_add_ref_ffi(obj : UInt64) -> UInt64 = "tycho_dx12_com_add_ref"

extern "c" fn com_release_ffi(obj : UInt64) -> UInt64 = "tycho_dx12_com_release"

extern "c" fn fn_create_device_ffi(adapter : UInt64, feature_level : Int, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_device"

extern "c" fn fn_get_debug_interface_ffi(riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_get_debug_interface"

extern "c" fn fn_serialize_root_signature_ffi(desc : UInt64, version : Int, pp_blob : UInt64, pp_err : UInt64) -> Int32 = "tycho_dx12_fn_serialize_root_signature"

extern "c" fn fn_create_root_signature_deserializer_ffi(src : UInt64, src_bytes : Int64, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_root_signature_deserializer"

extern "c" fn fn_serialize_versioned_root_signature_ffi(desc : UInt64, pp_blob : UInt64, pp_err : UInt64) -> Int32 = "tycho_dx12_fn_serialize_versioned_root_signature"

extern "c" fn fn_create_versioned_root_signature_deserializer_ffi(src : UInt64, src_bytes : Int64, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_versioned_root_signature_deserializer"

extern "c" fn fn_create_versioned_root_signature_deserializer_from_subobject_in_library_ffi(src : UInt64, src_bytes : Int64, name_wstr : UInt64, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_versioned_root_signature_deserializer_from_subobject_in_library"

extern "c" fn fn_enable_experimental_features_ffi(num : Int, riids : UInt64, configs : UInt64, config_sizes : UInt64) -> Int32 = "tycho_dx12_fn_enable_experimental_features"

extern "c" fn fn_get_interface_ffi(rclsid : UInt64, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_get_interface"

extern "c" fn fn_create_dxgi_factory_ffi(riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_dxgi_factory"

extern "c" fn fn_create_dxgi_factory1_ffi(riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_dxgi_factory1"

extern "c" fn fn_create_dxgi_factory2_ffi(flags : Int, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_create_dxgi_factory2"

extern "c" fn fn_dxgi_get_debug_interface1_ffi(flags : Int, riid : UInt64, ppv : UInt64) -> Int32 = "tycho_dx12_fn_dxgi_get_debug_interface1"

extern "c" fn fn_dxgi_declare_adapter_removal_support_ffi() -> Int32 = "tycho_dx12_fn_dxgi_declare_adapter_removal_support"

extern "c" fn fn_dxgi_disable_vblank_virtualization_ffi() -> Int32 = "tycho_dx12_fn_dxgi_disable_vblank_virtualization"

extern "c" fn create_event_ffi() -> UInt64 = "tycho_dx12_create_event"

extern "c" fn wait_event_ffi(h : UInt64, timeout_ms : Int) -> Int32 = "tycho_dx12_wait_event"

extern "c" fn close_handle_ffi(h : UInt64) = "tycho_dx12_close_handle"

extern "c" fn read_u32_ffi(ptr : UInt64, byte_offset : Int) -> UInt32 = "tycho_dx12_read_u32"

extern "c" fn read_u64_ffi(ptr : UInt64, byte_offset : Int) -> UInt64 = "tycho_dx12_read_u64"

extern "c" fn write_u32_ffi(ptr : UInt64, byte_offset : Int, val : UInt32) = "tycho_dx12_write_u32"

extern "c" fn write_u64_ffi(ptr : UInt64, byte_offset : Int, val : UInt64) = "tycho_dx12_write_u64"

extern "c" fn memcpy_ptr_ffi(dst : UInt64, src : UInt64, bytes : Int) = "tycho_dx12_memcpy_ptr"

extern "c" fn raw_alloc_ffi(bytes : Int) -> UInt64 = "tycho_dx12_alloc"

extern "c" fn raw_free_ffi(ptr : UInt64) = "tycho_dx12_free"

extern "c" fn device_create_ffi(use_warp : Int, debug : Int) -> UInt64 = "tycho_dx12_device_create"

extern "c" fn device_destroy_ffi(dev : UInt64) = "tycho_dx12_device_destroy"

extern "c" fn device_validation_errors_ffi(dev : UInt64) -> Int32 = "tycho_dx12_device_validation_errors"

extern "c" fn device_wait_idle_ffi(dev : UInt64) -> Int32 = "tycho_dx12_device_wait_idle"

extern "c" fn device_is_warp_ffi(dev : UInt64) -> Int32 = "tycho_dx12_device_is_warp"

extern "c" fn device_raw_ffi(dev : UInt64) -> UInt64 = "tycho_dx12_device_raw"

extern "c" fn device_shader_model_ffi(dev : UInt64) -> Int32 = "tycho_dx12_device_shader_model"

extern "c" fn device_mesh_tier_ffi(dev : UInt64) -> Int32 = "tycho_dx12_device_mesh_tier"

extern "c" fn device_raytracing_tier_ffi(dev : UInt64) -> Int32 = "tycho_dx12_device_raytracing_tier"

extern "c" fn commands_create_ffi(dev : UInt64) -> UInt64 = "tycho_dx12_commands_create"

extern "c" fn commands_destroy_ffi(cmds : UInt64) = "tycho_dx12_commands_destroy"

extern "c" fn commands_begin_ffi(cmds : UInt64) -> Int32 = "tycho_dx12_commands_begin"

extern "c" fn cmd_transition_ffi(cmds : UInt64, tex : UInt64, state : Int) -> Int32 = "tycho_dx12_cmd_transition"

extern "c" fn texture_state_ffi(tex : UInt64) -> Int32 = "tycho_dx12_texture_state"

extern "c" fn swapchain_create_ffi(dev : UInt64, hwnd : UInt64, width : Int, height : Int) -> UInt64 = "tycho_dx12_swapchain_create"

extern "c" fn swapchain_destroy_ffi(sc : UInt64) = "tycho_dx12_swapchain_destroy"

extern "c" fn swapchain_width_ffi(sc : UInt64) -> Int32 = "tycho_dx12_swapchain_width"

extern "c" fn swapchain_height_ffi(sc : UInt64) -> Int32 = "tycho_dx12_swapchain_height"

extern "c" fn swapchain_current_index_ffi(sc : UInt64) -> Int32 = "tycho_dx12_swapchain_current_index"

extern "c" fn swapchain_present_ffi(sc : UInt64) -> Int32 = "tycho_dx12_swapchain_present"

extern "c" fn swapchain_target_ffi(sc : UInt64, index : Int) -> UInt64 = "tycho_dx12_swapchain_target"

extern "c" fn swapchain_resize_ffi(dev : UInt64, sc : UInt64, width : Int, height : Int) -> Int32 = "tycho_dx12_swapchain_resize"

extern "c" fn cmd_clear_ffi(cmds : UInt64, tex : UInt64, r : Float, g : Float, b : Float, a : Float) -> Int32 = "tycho_dx12_cmd_clear"

extern "c" fn cmd_set_pipeline_ffi(cmds : UInt64, pipe : UInt64) -> Int32 = "tycho_dx12_cmd_set_pipeline"

extern "c" fn cmd_set_extra_targets_ffi(cmds : UInt64, t1 : UInt64, t2 : UInt64, t3 : UInt64) -> Int32 = "tycho_dx12_cmd_set_extra_targets"

#borrow(ms, ps)
extern "c" fn mesh_pipeline_create_ffi(dev : UInt64, ms : FixedArray[UInt32], ms_bytes : Int, ps : FixedArray[UInt32], ps_bytes : Int, color_format : Int, depth_enable : Int, depth_write : Int, flip_y : Int) -> UInt64 = "tycho_dx12_mesh_pipeline_create"

extern "c" fn accel_blas_create_ffi(dev : UInt64, vertex_address : UInt64, vertex_count : Int, vertex_stride : Int, index_address : UInt64, index_count : Int, index32 : Int, is_opaque : Int) -> UInt64 = "tycho_dx12_blas_create"

#borrow(transform)
extern "c" fn accel_tlas_create_ffi(dev : UInt64, blas_address : UInt64, transform : FixedArray[Float], custom_index : Int, mask : Int) -> UInt64 = "tycho_dx12_tlas_create"

extern "c" fn cmd_set_accel_ffi(cmds : UInt64, root_index : Int, accel : UInt64, compute : Int) -> Int32 = "tycho_dx12_cmd_set_accel"

extern "c" fn accel_destroy_ffi(accel : UInt64) = "tycho_dx12_accel_destroy"

extern "c" fn accel_gpu_address_ffi(accel : UInt64) -> UInt64 = "tycho_dx12_accel_gpu_address"

extern "c" fn cmd_dispatch_mesh_ffi(cmds : UInt64, tex : UInt64, x : Int, y : Int, z : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_dispatch_mesh"

extern "c" fn cmd_draw_ffi(cmds : UInt64, tex : UInt64, vertices : Int, instances : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_draw"

extern "c" fn cmd_draw_instanced_ffi(cmds : UInt64, tex : UInt64, vertices : Int, instances : Int, first_vertex : Int, first_instance : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_draw_instanced"

extern "c" fn cmd_set_cbv_ffi(cmds : UInt64, index : Int, gpu_address : UInt64) -> Int32 = "tycho_dx12_cmd_set_cbv"

extern "c" fn cmd_set_compute_cbv_ffi(cmds : UInt64, index : Int, gpu_address : UInt64) -> Int32 = "tycho_dx12_cmd_set_compute_cbv"

extern "c" fn buffer_create_ffi(dev : UInt64, size : Int) -> UInt64 = "tycho_dx12_buffer_create"

#borrow(data)
extern "c" fn buffer_write_ffi(buf : UInt64, offset : Int, data : Bytes, len : Int) -> Int32 = "tycho_dx12_buffer_write"

#borrow(data)
extern "c" fn buffer_write_floats_ffi(buf : UInt64, offset : Int, data : FixedArray[Float], count : Int) -> Int32 = "tycho_dx12_buffer_write_floats"

extern "c" fn buffer_gpu_address_ffi(buf : UInt64) -> UInt64 = "tycho_dx12_buffer_gpu_address"

extern "c" fn buffer_size_ffi(buf : UInt64) -> Int32 = "tycho_dx12_buffer_size"

extern "c" fn buffer_destroy_ffi(buf : UInt64) = "tycho_dx12_buffer_destroy"

#borrow(data)
extern "c" fn cmd_set_root_constants_bytes_ffi(cmds : UInt64, index : Int, data : Bytes, words : Int) -> Int32 = "tycho_dx12_cmd_set_root_constants_bytes"

#borrow(data)
extern "c" fn cmd_set_root_constants_bytes_offset_ffi(cmds : UInt64, index : Int, data : Bytes, words : Int, word_offset : Int) -> Int32 = "tycho_dx12_cmd_set_root_constants_bytes_offset"

#borrow(data)
extern "c" fn cmd_set_compute_root_constants_bytes_ffi(cmds : UInt64, index : Int, data : Bytes, words : Int) -> Int32 = "tycho_dx12_cmd_set_compute_root_constants_bytes"

#borrow(data)
extern "c" fn cmd_set_compute_root_constants_bytes_offset_ffi(cmds : UInt64, index : Int, data : Bytes, words : Int, word_offset : Int) -> Int32 = "tycho_dx12_cmd_set_compute_root_constants_bytes_offset"

#borrow(dst)
extern "c" fn buffer_download_ffi(dev : UInt64, buf : UInt64, dst : FixedArray[Byte], size : Int) -> Int32 = "tycho_dx12_buffer_download"

#borrow(data)
extern "c" fn cmd_set_root_constants_ffi(cmds : UInt64, index : Int, data : FixedArray[Float], count : Int) -> Int32 = "tycho_dx12_cmd_set_root_constants"

#borrow(data)
extern "c" fn cmd_set_root_constants_offset_ffi(cmds : UInt64, index : Int, data : FixedArray[Float], count : Int, word_offset : Int) -> Int32 = "tycho_dx12_cmd_set_root_constants_offset"

#borrow(data)
extern "c" fn cmd_set_compute_root_constants_ffi(cmds : UInt64, index : Int, data : FixedArray[Float], count : Int) -> Int32 = "tycho_dx12_cmd_set_compute_root_constants"

#borrow(data)
extern "c" fn cmd_set_compute_root_constants_offset_ffi(cmds : UInt64, index : Int, data : FixedArray[Float], count : Int, word_offset : Int) -> Int32 = "tycho_dx12_cmd_set_compute_root_constants_offset"

extern "c" fn storage_buffer_create_ffi(dev : UInt64, size : Int) -> UInt64 = "tycho_dx12_storage_buffer_create"

extern "c" fn cmd_set_buffer_uav_ffi(cmds : UInt64, root_index : Int, buf : UInt64) -> Int32 = "tycho_dx12_cmd_set_buffer_uav"

extern "c" fn readback_buffer_create_ffi(dev : UInt64, size : Int) -> UInt64 = "tycho_dx12_readback_buffer_create"

extern "c" fn cmd_copy_buffer_ffi(cmds : UInt64, src : UInt64, dst : UInt64, size : Int) -> Int32 = "tycho_dx12_cmd_copy_buffer"

extern "c" fn cmd_execute_indirect_counted_ffi(cmds : UInt64, tex : UInt64, kind : Int, arg_buffer : UInt64, arg_offset : Int, max_count : Int, count_buffer : UInt64, count_offset : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_execute_indirect_counted"

extern "c" fn cmd_copy_to_readback_ffi(cmds : UInt64, tex : UInt64, rb : UInt64) -> Int32 = "tycho_dx12_cmd_copy_to_readback"

extern "c" fn commands_execute_wait_ffi(dev : UInt64, cmds : UInt64) -> Int32 = "tycho_dx12_commands_execute_wait"

#borrow(vs, ps, colors, inputs)
extern "c" fn pipeline_create_ffi(dev : UInt64, vs : FixedArray[UInt32], vs_bytes : Int, ps : FixedArray[UInt32], ps_bytes : Int, cbv_count : Int, flip_y : Int, depth_enable : Int, depth_write : Int, srv_tables : Int, push_bytes : Int, color_count : Int, colors : FixedArray[Int], input_count : Int, inputs : FixedArray[Int]) -> UInt64 = "tycho_dx12_pipeline_create"

extern "c" fn cmd_draw_indexed_ffi(cmds : UInt64, tex : UInt64, index_count : Int, instances : Int, first_index : Int, vertex_offset : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_draw_indexed"

extern "c" fn cmd_draw_indexed_instanced_ffi(cmds : UInt64, tex : UInt64, index_count : Int, instances : Int, first_index : Int, vertex_offset : Int, first_instance : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_draw_indexed_instanced"

extern "c" fn cmd_bind_vertex_ffi(cmds : UInt64, slot : Int, gpu_address : UInt64, size : Int, stride : Int) -> Int32 = "tycho_dx12_cmd_bind_vertex"

extern "c" fn cmd_bind_index_ffi(cmds : UInt64, gpu_address : UInt64, size : Int, index32 : Int) -> Int32 = "tycho_dx12_cmd_bind_index"

extern "c" fn cmd_execute_indirect_ffi(cmds : UInt64, tex : UInt64, kind : Int, arg_buffer : UInt64, arg_offset : Int, max_count : Int, flip_y : Int) -> Int32 = "tycho_dx12_cmd_execute_indirect"

extern "c" fn timestamp_pool_create_ffi(dev : UInt64, count : Int) -> UInt64 = "tycho_dx12_timestamp_pool_create"

extern "c" fn timestamp_pool_write_ffi(cmds : UInt64, pool : UInt64, index : Int) -> Int32 = "tycho_dx12_cmd_write_timestamp"

extern "c" fn timestamp_pool_resolve_ffi(cmds : UInt64, pool : UInt64) -> Int32 = "tycho_dx12_cmd_resolve_timestamps"

extern "c" fn timestamp_pool_read_ffi(pool : UInt64, index : Int) -> Int64 = "tycho_dx12_timestamp_read"

extern "c" fn timestamp_pool_frequency_ffi(pool : UInt64) -> Int64 = "tycho_dx12_timestamp_frequency"

extern "c" fn timestamp_pool_count_ffi(pool : UInt64) -> Int32 = "tycho_dx12_timestamp_pool_count"

extern "c" fn timestamp_pool_destroy_ffi(pool : UInt64) = "tycho_dx12_timestamp_pool_destroy"

#borrow(pixels)
extern "c" fn texture_create_uploaded_ffi(dev : UInt64, width : Int, height : Int, pixels : Bytes, pixel_bytes : Int, format : Int) -> UInt64 = "tycho_dx12_texture_create_uploaded"

extern "c" fn cmd_set_texture_ffi(cmds : UInt64, root_index : Int, sampler_root : Int, tex : UInt64, sampler : UInt64) -> Int32 = "tycho_dx12_cmd_set_texture"

extern "c" fn sampler_create_ffi(dev : UInt64, filter : Int, address : Int, anisotropy : Float, min_lod : Float, max_lod : Float, mip_lod_bias : Float) -> UInt64 = "tycho_dx12_sampler_create"

extern "c" fn sampler_destroy_ffi(sampler : UInt64) = "tycho_dx12_sampler_destroy"

extern "c" fn storage_texture_create_ffi(dev : UInt64, width : Int, height : Int) -> UInt64 = "tycho_dx12_storage_texture_create"

#borrow(cs)
extern "c" fn compute_pipeline_create_ffi(dev : UInt64, cs : FixedArray[UInt32], cs_bytes : Int, cbv_count : Int, uav_tables : Int, accel_tables : Int, push_bytes : Int) -> UInt64 = "tycho_dx12_compute_pipeline_create"

extern "c" fn compute_pipeline_destroy_ffi(pipe : UInt64) = "tycho_dx12_pipeline_destroy"

extern "c" fn cmd_set_compute_pipeline_ffi(cmds : UInt64, pipe : UInt64) -> Int32 = "tycho_dx12_cmd_set_compute_pipeline"

extern "c" fn cmd_set_uav_ffi(cmds : UInt64, root_index : Int, tex : UInt64) -> Int32 = "tycho_dx12_cmd_set_uav"

extern "c" fn cmd_dispatch_ffi(cmds : UInt64, x : Int, y : Int, z : Int) -> Int32 = "tycho_dx12_cmd_dispatch"

extern "c" fn buffer_heap_create_ffi(dev : UInt64, size : Int) -> UInt64 = "tycho_dx12_buffer_heap_create"

extern "c" fn buffer_heap_destroy_ffi(heap : UInt64) = "tycho_dx12_buffer_heap_destroy"

extern "c" fn buffer_place_ffi(heap : UInt64, offset : Int, size : Int) -> UInt64 = "tycho_dx12_buffer_place"

extern "c" fn depth_create_ffi(dev : UInt64, width : Int, height : Int, format : Int) -> UInt64 = "tycho_dx12_depth_create"

extern "c" fn cmd_set_depth_ffi(cmds : UInt64, tex : UInt64) -> Int32 = "tycho_dx12_cmd_set_depth"

extern "c" fn cmd_clear_depth_ffi(cmds : UInt64, tex : UInt64, depth : Float) -> Int32 = "tycho_dx12_cmd_clear_depth"

extern "c" fn pipeline_destroy_ffi(pipe : UInt64) = "tycho_dx12_pipeline_destroy"

extern "c" fn texture_create_ffi(dev : UInt64, width : Int, height : Int, format : Int) -> UInt64 = "tycho_dx12_texture_create"

extern "c" fn texture_destroy_ffi(tex : UInt64) = "tycho_dx12_texture_destroy"

extern "c" fn readback_create_ffi(dev : UInt64, width : Int, height : Int) -> UInt64 = "tycho_dx12_readback_create"

extern "c" fn readback_destroy_ffi(rb : UInt64) = "tycho_dx12_readback_destroy"

extern "c" fn readback_bytes_ffi(rb : UInt64) -> Bytes = "tycho_dx12_readback_bytes"

#borrow(vs, ps, regs, state, color_formats, blend_params, inputs)
extern "c" fn pipeline_create_ex_ffi(dev : UInt64, vs : FixedArray[UInt32], vs_bytes : Int, ps : FixedArray[UInt32], ps_bytes : Int, regs : FixedArray[Int], regs_len : Int, state : FixedArray[Int], state_len : Int, bias_constant : Float, bias_slope : Float, color_count : Int, color_formats : FixedArray[Int], blend_params : FixedArray[Int], input_count : Int, inputs : FixedArray[Int]) -> UInt64 = "tycho_dx12_pipeline_create_ex"

#borrow(cs, cbv_regs, storage_regs, image_regs, accel_regs)
extern "c" fn compute_pipeline_create_ex_ffi(dev : UInt64, cs : FixedArray[UInt32], cs_bytes : Int, cbv_count : Int, cbv_regs : FixedArray[Int], storage_count : Int, storage_regs : FixedArray[Int], storage_srv_mask : Int, image_count : Int, image_regs : FixedArray[Int], accel_count : Int, accel_regs : FixedArray[Int], push_bytes : Int) -> UInt64 = "tycho_dx12_compute_pipeline_create_ex"

#borrow(bc, regs)
extern "c" fn dxil_psv0_storage_srv_mask_ffi(bc : FixedArray[UInt32], bc_bytes : Int, regs : FixedArray[Int], count : Int) -> Int32 = "tycho_dx12_dxil_psv0_storage_srv_mask"

#borrow(bc, out)
extern "c" fn dxil_reflect_compute_ffi(bc : FixedArray[UInt32], bc_bytes : Int, out : FixedArray[Int]) -> Int32 = "tycho_dx12_dxil_reflect_compute"

extern "c" fn texture_create_ex_ffi(dev : UInt64, width : Int, height : Int, format_code : Int, flags : Int, mip_levels : Int, array_layers : Int, is_cube : Int, sample_count : Int) -> UInt64 = "tycho_dx12_texture_create_ex"

extern "c" fn storage_texture_create_ex_ffi(dev : UInt64, width : Int, height : Int, format_code : Int) -> UInt64 = "tycho_dx12_storage_texture_create_ex"

#borrow(pixels)
extern "c" fn texture_upload_subregion_ffi(dev : UInt64, tex : UInt64, x : Int, y : Int, w : Int, h : Int, pixels : Bytes, pixel_bytes : Int, layer : Int, mip : Int) -> Int32 = "tycho_dx12_texture_upload_subregion"

extern "c" fn readback_create_ex_ffi(dev : UInt64, width : Int, height : Int, bpp : Int) -> UInt64 = "tycho_dx12_readback_create_ex"

extern "c" fn sampler_create_ex_ffi(dev : UInt64, mag : Int, min_f : Int, mip : Int, address : Int, border_color : Int, anisotropy : Float, min_lod : Float, max_lod : Float, mip_lod_bias : Float, compare_op : Int) -> UInt64 = "tycho_dx12_sampler_create_ex"

extern "c" fn cmd_set_viewport_ffi(cmds : UInt64, x : Float, y : Float, w : Float, h : Float, flip_y : Int) -> Int32 = "tycho_dx12_cmd_set_viewport"

extern "c" fn cmd_set_scissor_ffi(cmds : UInt64, x : Int, y : Int, w : Int, h : Int) -> Int32 = "tycho_dx12_cmd_set_scissor"

extern "c" fn cmd_reset_viewport_scissor_ffi(cmds : UInt64) -> Int32 = "tycho_dx12_cmd_reset_viewport_scissor"

extern "c" fn cmd_set_stencil_ref_ffi(cmds : UInt64, ref_val : Int) -> Int32 = "tycho_dx12_cmd_set_stencil_ref"

extern "c" fn cmd_clear_depth_stencil_ffi(cmds : UInt64, tex : UInt64, clear_depth : Int, depth : Float, clear_stencil : Int, stencil : Int) -> Int32 = "tycho_dx12_cmd_clear_depth_stencil"

extern "c" fn cmd_uav_barrier_ffi(cmds : UInt64) -> Int32 = "tycho_dx12_cmd_uav_barrier"

extern "c" fn cmd_blit_ffi(cmds : UInt64, src : UInt64, dst : UInt64, linear : Int) -> Int32 = "tycho_dx12_cmd_blit"

extern "c" fn cmd_resolve_ffi(cmds : UInt64, src : UInt64, dst : UInt64) -> Int32 = "tycho_dx12_cmd_resolve"

extern "c" fn cmd_copy_to_readback_mip_ffi(cmds : UInt64, tex : UInt64, rb : UInt64, mip : Int, layer : Int) -> Int32 = "tycho_dx12_cmd_copy_to_readback_mip"

extern "c" fn cmd_set_buffer_srv_ffi(cmds : UInt64, root_index : Int, buf : UInt64, offset : Int, size : Int, compute : Int) -> Int32 = "tycho_dx12_cmd_set_buffer_srv"

extern "c" fn cmd_set_buffer_uav_ex_ffi(cmds : UInt64, root_index : Int, buf : UInt64, offset : Int, size : Int) -> Int32 = "tycho_dx12_cmd_set_buffer_uav_ex"

#borrow(vaddrs, vcounts, vstrides, iaddrs, icounts, index32s, opaques)
extern "c" fn blas_create_multi_ffi(dev : UInt64, count : Int, vaddrs : FixedArray[UInt64], vcounts : FixedArray[Int], vstrides : FixedArray[Int], iaddrs : FixedArray[UInt64], icounts : FixedArray[Int], index32s : FixedArray[Int], opaques : FixedArray[Int]) -> UInt64 = "tycho_dx12_blas_create_multi"

#borrow(blas_addrs, transforms12, custom_indices, masks)
extern "c" fn tlas_create_multi_ffi(dev : UInt64, count : Int, blas_addrs : FixedArray[UInt64], transforms12 : FixedArray[Float], custom_indices : FixedArray[Int], masks : FixedArray[Int]) -> UInt64 = "tycho_dx12_tlas_create_multi"

extern "c" fn fence_create_ffi(dev : UInt64, signaled : Int) -> UInt64 = "tycho_dx12_fence_create"

extern "c" fn fence_wait_ffi(fence : UInt64) -> Int32 = "tycho_dx12_fence_wait"

extern "c" fn fence_reset_ffi(fence : UInt64) = "tycho_dx12_fence_reset"

extern "c" fn fence_destroy_ffi(fence : UInt64) = "tycho_dx12_fence_destroy"

extern "c" fn commands_submit_fence_ffi(dev : UInt64, cmds : UInt64, fence : UInt64) -> Int32 = "tycho_dx12_commands_submit_fence"

extern "c" fn bindless_create_ffi(dev : UInt64, capacity : Int) -> UInt64 = "tycho_dx12_bindless_create"

extern "c" fn bindless_register_ffi(heap : UInt64, tex : UInt64, samp : UInt64) -> Int32 = "tycho_dx12_bindless_register"

extern "c" fn bindless_unregister_ffi(heap : UInt64, index : Int) = "tycho_dx12_bindless_unregister"

extern "c" fn cmd_set_bindless_ffi(cmds : UInt64, srv_root : Int, samp_root : Int, heap : UInt64) -> Int32 = "tycho_dx12_cmd_set_bindless"

extern "c" fn bindless_destroy_ffi(heap : UInt64) = "tycho_dx12_bindless_destroy"

// ---------------------------------------------------------------------------
// Device
// ---------------------------------------------------------------------------

///|
/// A Direct3D 12 device plus its single direct queue and fence.
pub struct Device {
  handle : UInt64
}

///|
/// Returns `true` when `d3d12.dll` and `dxgi.dll` are loadable on this host.
pub fn available() -> Bool {
  init_ffi() != 0
}

///|
/// The HRESULT of the last failing call, as a signed 32-bit value.
pub fn last_error() -> Int {
  last_error_ffi().to_int()
}

///|
/// `TYCHO_DX12_WARP=1` asks runners to use the WARP software adapter, the DX12
/// counterpart of `TYCHO_VULKAN_LIB` for driver selection.
pub fn env_requests_warp() -> Bool {
  env_requests_warp_ffi() != 0
}

///|
/// Data pointer of an `Int32` array, for the raw generated-dispatch ABI.
pub fn int32_ptr(values : FixedArray[Int32]) -> UInt64 {
  int32_ptr_ffi(values)
}

///|
/// Data pointer of a `Byte` array, for marshalling raw structs into the
/// generated-dispatch ABI (a by-value struct arrives as a pointer to its bytes).
pub fn bytes_ptr(values : FixedArray[Byte]) -> UInt64 {
  bytes_ptr_ffi(values)
}

///|
/// Data pointer of an `Int64` array, for raw `void **` out-parameters and
/// 64-bit struct fields in the generated-dispatch ABI.
pub fn int64_ptr(values : FixedArray[Int64]) -> UInt64 {
  int64_ptr_ffi(values)
}

///|
/// `IUnknown::QueryInterface` on a raw COM object from the generated dispatch layer.
/// Returns the HRESULT (`0` on success).
pub fn com_query_interface(obj : UInt64, riid : UInt64, ppv_out : UInt64) -> Int {
  com_query_interface_ffi(obj, riid, ppv_out).to_int()
}

///|
/// `IUnknown::AddRef` on a raw COM object from the generated dispatch layer.
/// Returns the new reference count. The manifest omits the IUnknown methods, so
/// these helpers are the lifetime boundary for raw-dispatch callers.
pub fn com_add_ref(obj : UInt64) -> UInt64 {
  com_add_ref_ffi(obj)
}

///|
/// `IUnknown::Release` on a raw COM object from the generated dispatch layer.
/// Returns the new reference count. Release every object a generated `Create*`
/// method hands back once the GPU is done with it.
pub fn com_release(obj : UInt64) -> UInt64 {
  com_release_ffi(obj)
}

///|
/// `D3D12CreateDevice` loaded dynamically from `d3d12.dll`.
pub fn d3d12_create_device(
  adapter : UInt64,
  feature_level : Int,
  riid : UInt64,
  ppv : UInt64,
) -> Int {
  fn_create_device_ffi(adapter, feature_level, riid, ppv).to_int()
}

///|
/// `D3D12GetDebugInterface` loaded dynamically from `d3d12.dll`.
pub fn d3d12_get_debug_interface(riid : UInt64, ppv : UInt64) -> Int {
  fn_get_debug_interface_ffi(riid, ppv).to_int()
}

///|
/// `D3D12SerializeRootSignature` loaded dynamically from `d3d12.dll`.
pub fn d3d12_serialize_root_signature(
  desc : UInt64,
  version : Int,
  pp_blob : UInt64,
  pp_err : UInt64,
) -> Int {
  fn_serialize_root_signature_ffi(desc, version, pp_blob, pp_err).to_int()
}

///|
/// `D3D12CreateRootSignatureDeserializer` loaded dynamically from `d3d12.dll`.
pub fn d3d12_create_root_signature_deserializer(
  src : UInt64,
  src_bytes : Int64,
  riid : UInt64,
  ppv : UInt64,
) -> Int {
  fn_create_root_signature_deserializer_ffi(src, src_bytes, riid, ppv).to_int()
}

///|
/// `D3D12SerializeVersionedRootSignature` loaded dynamically from `d3d12.dll`.
pub fn d3d12_serialize_versioned_root_signature(
  desc : UInt64,
  pp_blob : UInt64,
  pp_err : UInt64,
) -> Int {
  fn_serialize_versioned_root_signature_ffi(desc, pp_blob, pp_err).to_int()
}

///|
/// `D3D12CreateVersionedRootSignatureDeserializer` loaded dynamically from `d3d12.dll`.
pub fn d3d12_create_versioned_root_signature_deserializer(
  src : UInt64,
  src_bytes : Int64,
  riid : UInt64,
  ppv : UInt64,
) -> Int {
  fn_create_versioned_root_signature_deserializer_ffi(src, src_bytes, riid, ppv).to_int()
}

///|
/// `D3D12CreateVersionedRootSignatureDeserializerFromSubobjectInLibrary` loaded dynamically from `d3d12.dll`.
pub fn d3d12_create_versioned_root_signature_deserializer_from_subobject_in_library(
  src : UInt64,
  src_bytes : Int64,
  name_wstr : UInt64,
  riid : UInt64,
  ppv : UInt64,
) -> Int {
  fn_create_versioned_root_signature_deserializer_from_subobject_in_library_ffi(
    src, src_bytes, name_wstr, riid, ppv,
  ).to_int()
}

///|
/// `D3D12EnableExperimentalFeatures` loaded dynamically from `d3d12.dll`.
pub fn d3d12_enable_experimental_features(
  num : Int,
  riids : UInt64,
  configs : UInt64,
  config_sizes : UInt64,
) -> Int {
  fn_enable_experimental_features_ffi(num, riids, configs, config_sizes).to_int()
}

///|
/// `D3D12GetInterface` loaded dynamically from `d3d12.dll`.
pub fn d3d12_get_interface(rclsid : UInt64, riid : UInt64, ppv : UInt64) -> Int {
  fn_get_interface_ffi(rclsid, riid, ppv).to_int()
}

///|
/// `CreateDXGIFactory` loaded dynamically from `dxgi.dll`.
pub fn create_dxgi_factory(riid : UInt64, ppv : UInt64) -> Int {
  fn_create_dxgi_factory_ffi(riid, ppv).to_int()
}

///|
/// `CreateDXGIFactory1` loaded dynamically from `dxgi.dll`.
pub fn create_dxgi_factory1(riid : UInt64, ppv : UInt64) -> Int {
  fn_create_dxgi_factory1_ffi(riid, ppv).to_int()
}

///|
/// `CreateDXGIFactory2` loaded dynamically from `dxgi.dll`.
pub fn create_dxgi_factory2(flags : Int, riid : UInt64, ppv : UInt64) -> Int {
  fn_create_dxgi_factory2_ffi(flags, riid, ppv).to_int()
}

///|
/// `DXGIGetDebugInterface1` loaded dynamically from `dxgi.dll`.
pub fn dxgi_get_debug_interface1(flags : Int, riid : UInt64, ppv : UInt64) -> Int {
  fn_dxgi_get_debug_interface1_ffi(flags, riid, ppv).to_int()
}

///|
/// `DXGIDeclareAdapterRemovalSupport` loaded dynamically from `dxgi.dll`.
pub fn dxgi_declare_adapter_removal_support() -> Int {
  fn_dxgi_declare_adapter_removal_support_ffi().to_int()
}

///|
/// `DXGIDisableVBlankVirtualization` loaded dynamically from `dxgi.dll`.
pub fn dxgi_disable_vblank_virtualization() -> Int {
  fn_dxgi_disable_vblank_virtualization_ffi().to_int()
}

///|
/// Creates an auto-reset Win32 event handle (`CreateEventW`).
pub fn create_event() -> UInt64 {
  create_event_ffi()
}

///|
/// Waits for a Win32 handle (`WaitForSingleObject`). Returns `0` for `WAIT_OBJECT_0`.
pub fn wait_event(h : UInt64, timeout_ms : Int) -> Int {
  wait_event_ffi(h, timeout_ms).to_int()
}

///|
/// Closes a Win32 `HANDLE` (`CloseHandle`).
pub fn close_handle(h : UInt64) -> Unit {
  close_handle_ffi(h)
}

///|
/// Reads a 32-bit unsigned integer at `ptr + byte_offset`.
pub fn read_u32(ptr : UInt64, byte_offset : Int) -> UInt32 {
  read_u32_ffi(ptr, byte_offset)
}

///|
/// Reads a 64-bit unsigned integer at `ptr + byte_offset`.
pub fn read_u64(ptr : UInt64, byte_offset : Int) -> UInt64 {
  read_u64_ffi(ptr, byte_offset)
}

///|
/// Writes a 32-bit unsigned integer at `ptr + byte_offset`.
pub fn write_u32(ptr : UInt64, byte_offset : Int, val : UInt32) -> Unit {
  write_u32_ffi(ptr, byte_offset, val)
}

///|
/// Writes a 64-bit unsigned integer at `ptr + byte_offset`.
pub fn write_u64(ptr : UInt64, byte_offset : Int, val : UInt64) -> Unit {
  write_u64_ffi(ptr, byte_offset, val)
}

///|
/// Copies `bytes` from raw pointer `src` to raw pointer `dst`.
pub fn memcpy_ptr(dst : UInt64, src : UInt64, bytes : Int) -> Unit {
  memcpy_ptr_ffi(dst, src, bytes)
}

///|
/// Allocates `bytes` zero-initialized native memory for raw struct marshalling.
pub fn raw_alloc(bytes : Int) -> UInt64 {
  raw_alloc_ffi(bytes)
}

///|
/// Frees memory allocated by `raw_alloc`.
pub fn raw_free(ptr : UInt64) -> Unit {
  raw_free_ffi(ptr)
}

///|
/// Encodes a MoonBit `String` (UTF-16) into a null-terminated UTF-16LE `FixedArray[Byte]`.
pub fn to_utf16(s : String) -> FixedArray[Byte] {
  let len = s.length()
  let out = FixedArray::make((len + 1) * 2, (0 : Byte))
  for i = 0; i < len; i = i + 1 {
    let cu = s[i].to_int()
    out[i * 2] = (cu & 0xFF).to_byte()
    out[i * 2 + 1] = ((cu >> 8) & 0xFF).to_byte()
  }
  out
}

///|
/// Creates a device on the first hardware adapter, or on WARP when `warp` is set.
/// `debug` enables the D3D12 debug layer (and its message queue).
pub fn Device::create(warp? : Bool = false, debug? : Bool = false) -> Device? {
  let handle = device_create_ffi(
    if warp { 1 } else { 0 },
    if debug { 1 } else { 0 },
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Device::handle(self : Device) -> UInt64 {
  self.handle
}

///|
pub fn Device::destroy(self : Device) -> Unit {
  device_destroy_ffi(self.handle)
}

///|
/// Number of debug-layer messages with ERROR/CORRUPTION severity.
pub fn Device::validation_errors(self : Device) -> Int {
  device_validation_errors_ffi(self.handle).to_int()
}

///|
/// Blocks until the queue has drained. Call this before releasing anything the
/// GPU may still be using (swapchain, render targets, readback buffers).
pub fn Device::wait_idle(self : Device) -> Bool {
  device_wait_idle_ffi(self.handle) != 0
}

///|
pub fn Device::is_warp(self : Device) -> Bool {
  device_is_warp_ffi(self.handle) != 0
}

///|
/// The underlying `ID3D12Device *`, for the generated dispatch layer and other
/// raw interop. `Device::handle` is the wrapper, not the COM object.
pub fn Device::raw(self : Device) -> UInt64 {
  device_raw_ffi(self.handle)
}

///|
/// Highest shader model as the raw `D3D_SHADER_MODEL` value (0x60 = 6.0, 0x67 = 6.7).
pub fn Device::shader_model(self : Device) -> Int {
  device_shader_model_ffi(self.handle).to_int()
}

///|
/// `D3D12_MESH_SHADER_TIER` (0 = none, 10 = 1.0).
pub fn Device::mesh_tier(self : Device) -> Int {
  device_mesh_tier_ffi(self.handle).to_int()
}

///|
/// `D3D12_RAYTRACING_TIER` (0 = none, 10 = 1.0, 11 = 1.1).
pub fn Device::raytracing_tier(self : Device) -> Int {
  device_raytracing_tier_ffi(self.handle).to_int()
}

// ---------------------------------------------------------------------------
// Pipelines, render targets, readback buffers
// ---------------------------------------------------------------------------

///|
/// Graphics pipeline: an empty root signature plus a PSO built from DXIL blobs.
pub struct Pipeline {
  handle : UInt64
}

///|
///|
/// `cbv_count` root constant buffers are declared at `register(b, space0)`;
/// `flip_y` renders with a negative viewport height (Vulkan clip-space
/// convention) and mirrors the front-face winding to match.
pub fn Pipeline::create(
  device : Device,
  vs_dxil : FixedArray[UInt32],
  vs_bytes : Int,
  ps_dxil : FixedArray[UInt32],
  ps_bytes : Int,
  cbv_count? : Int = 0,
  flip_y? : Bool = false,
  depth? : Bool = false,
  depth_write? : Bool = true,
  srv_tables? : Int = 0,
  push_bytes? : Int = 0,
  color_formats? : FixedArray[Int] = [],
  inputs? : FixedArray[Int] = [],
) -> Pipeline? {
  let handle = pipeline_create_ffi(
    device.handle,
    vs_dxil,
    vs_bytes,
    ps_dxil,
    ps_bytes,
    cbv_count,
    if flip_y { 1 } else { 0 },
    if depth { 1 } else { 0 },
    if depth_write { 1 } else { 0 },
    srv_tables,
    push_bytes,
    color_formats.length(),
    color_formats,
    inputs.length() / 5,
    inputs,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Pipeline::handle(self : Pipeline) -> UInt64 {
  self.handle
}

///|
pub fn Pipeline::destroy(self : Pipeline) -> Unit {
  pipeline_destroy_ffi(self.handle)
}

///|
/// RGBA8 UNORM render target with its own RTV descriptor heap.
pub struct Texture {
  handle : UInt64
}

///|
pub fn Texture::create(device : Device, width : Int, height : Int, format? : Int = 0) -> Texture? {
  let handle = texture_create_ffi(device.handle, width, height, format)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
///|
/// Uploads tightly packed RGBA8 pixels into a DEFAULT-heap texture and gives it a
/// shader-visible SRV, so it can be bound with `CommandList::set_texture`.
pub fn Texture::create_uploaded(
  device : Device,
  width : Int,
  height : Int,
  pixels : Bytes,
  format? : Int = 0,
) -> Texture? {
  let handle = texture_create_uploaded_ffi(
    device.handle,
    width,
    height,
    pixels,
    pixels.length(),
    format,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
/// A storage texture (`RWTexture2D` / UAV) that a compute shader writes into. It
/// starts in `ResourceState::UnorderedAccess` and can be read back afterwards.
pub fn Texture::create_storage(device : Device, width : Int, height : Int) -> Texture? {
  let handle = storage_texture_create_ffi(device.handle, width, height)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
/// A depth buffer (`D32_FLOAT`) with its own DSV. Bind it with
/// `CommandList::set_depth` before clearing or drawing.
pub fn Texture::create_depth(
  device : Device,
  width : Int,
  height : Int,
  format? : Int = 8,
) -> Texture? {
  let handle = depth_create_ffi(device.handle, width, height, format)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Texture::is_depth(self : Texture) -> Bool {
  match self.state() {
    Some(DepthWrite) | Some(DepthRead) => true
    _ => false
  }
}

pub fn Texture::handle(self : Texture) -> UInt64 {
  self.handle
}

///|
/// The texture's current resource state, as last set by `CommandList::transition`.
pub fn Texture::state(self : Texture) -> ResourceState? {
  ResourceState::from_code(texture_state_ffi(self.handle).to_int())
}

///|
pub fn Texture::destroy(self : Texture) -> Unit {
  texture_destroy_ffi(self.handle)
}

///|
/// Readback (CPU-readable) buffer sized for one RGBA8 copy of `width x height`.
pub struct Readback {
  handle : UInt64
}

///|
pub fn Readback::create(device : Device, width : Int, height : Int) -> Readback? {
  let handle = readback_create_ffi(device.handle, width, height)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Readback::handle(self : Readback) -> UInt64 {
  self.handle
}

///|
/// Tightly packed RGBA8 pixels, row pitch padding already removed.
pub fn Readback::bytes(self : Readback) -> Bytes {
  readback_bytes_ffi(self.handle)
}

///|
pub fn Readback::destroy(self : Readback) -> Unit {
  readback_destroy_ffi(self.handle)
}

// ---------------------------------------------------------------------------
// Command list
// ---------------------------------------------------------------------------

///|
/// A direct command allocator + command list pair.
pub struct CommandList {
  handle : UInt64
}

///|
pub fn CommandList::create(device : Device) -> CommandList? {
  let handle = commands_create_ffi(device.handle)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn CommandList::handle(self : CommandList) -> UInt64 {
  self.handle
}

///|
pub fn CommandList::destroy(self : CommandList) -> Unit {
  commands_destroy_ffi(self.handle)
}

///|
pub fn CommandList::begin(self : CommandList) -> Bool {
  commands_begin_ffi(self.handle) != 0
}

///|
/// Typed resource states. `Present` and `Common` both map to the D3D12 value 0,
/// but keeping them apart lets a caller express intent (and lets the shim report
/// what a texture was last used for).
pub(all) enum ResourceState {
  Common
  RenderTarget
  CopySource
  CopyDest
  Present
  ShaderResource
  UnorderedAccess
  DepthWrite
  DepthRead
  ResolveSource
  ResolveDest
}

///|
fn ResourceState::code(self : ResourceState) -> Int {
  match self {
    Common => 0
    RenderTarget => 1
    CopySource => 2
    CopyDest => 3
    Present => 4
    ShaderResource => 5
    UnorderedAccess => 6
    DepthWrite => 7
    DepthRead => 8
    ResolveSource => 9
    ResolveDest => 10
  }
}

///|
fn ResourceState::from_code(code : Int) -> ResourceState? {
  match code {
    0 => Some(Common)
    1 => Some(RenderTarget)
    2 => Some(CopySource)
    3 => Some(CopyDest)
    4 => Some(Present)
    5 => Some(ShaderResource)
    6 => Some(UnorderedAccess)
    7 => Some(DepthWrite)
    8 => Some(DepthRead)
    9 => Some(ResolveSource)
    10 => Some(ResolveDest)
    _ => None
  }
}

///|
/// Transitions the texture into `state`, emitting a barrier only when the native
/// state actually changes. Drawing or copying from a texture in the wrong state
/// fails with `E_ILLEGAL_METHOD_CALL` instead of corrupting the frame.
pub fn CommandList::transition(
  self : CommandList,
  tex : Texture,
  state : ResourceState,
) -> Bool {
  cmd_transition_ffi(self.handle, tex.handle, state.code()) != 0
}

///|
pub fn CommandList::clear(
  self : CommandList,
  tex : Texture,
  r : Float,
  g : Float,
  b : Float,
  a : Float,
) -> Bool {
  cmd_clear_ffi(self.handle, tex.handle, r, g, b, a) != 0
}

///|
pub fn CommandList::set_pipeline(self : CommandList, pipe : Pipeline) -> Bool {
  cmd_set_pipeline_ffi(self.handle, pipe.handle) != 0
}

///|
pub fn CommandList::draw(
  self : CommandList,
  tex : Texture,
  vertices : Int,
  instances? : Int = 1,
  flip_y? : Bool = false,
) -> Bool {
  cmd_draw_ffi(self.handle, tex.handle, vertices, instances, if flip_y { 1 } else { 0 }) != 0
}

///|
pub fn CommandList::draw_instanced(
  self : CommandList,
  tex : Texture,
  vertices : Int,
  instances? : Int = 1,
  first_vertex? : Int = 0,
  first_instance? : Int = 0,
  flip_y? : Bool = false,
) -> Bool {
  cmd_draw_instanced_ffi(
    self.handle,
    tex.handle,
    vertices,
    instances,
    first_vertex,
    first_instance,
    if flip_y { 1 } else { 0 },
  ) != 0
}

///|
/// Binds colour targets 1..3 of a multi-render-target pass; slot 0 is the
/// texture passed to `draw`/`draw_indexed`. A null handle clears a slot.
pub fn CommandList::set_extra_targets(
  self : CommandList,
  t1 : Texture?,
  t2 : Texture?,
  t3 : Texture?,
) -> Bool {
  let h1 = match t1 {
    Some(t) => t.handle
    None => (0 : UInt64)
  }
  let h2 = match t2 {
    Some(t) => t.handle
    None => (0 : UInt64)
  }
  let h3 = match t3 {
    Some(t) => t.handle
    None => (0 : UInt64)
  }
  cmd_set_extra_targets_ffi(self.handle, h1, h2, h3) != 0
}

///|
/// Binds an upload buffer to graphics root CBV `index` (see `Pipeline::create`'s
/// `cbv_count`).
pub fn CommandList::set_cbv(
  self : CommandList,
  index : Int,
  buffer : Buffer,
  offset? : Int = 0,
) -> Bool {
  cmd_set_cbv_ffi(self.handle, index, buffer.gpu_address() + offset.to_uint64()) != 0
}

///|
/// Binds an upload buffer to compute root CBV `index`.
pub fn CommandList::set_compute_cbv(
  self : CommandList,
  index : Int,
  buffer : Buffer,
  offset? : Int = 0,
) -> Bool {
  cmd_set_compute_cbv_ffi(self.handle, index, buffer.gpu_address() + offset.to_uint64()) != 0
}

// ---------------------------------------------------------------------------
// Upload buffer (CPU-visible, persistently mapped)
// ---------------------------------------------------------------------------

///|
/// A CPU-writable buffer on the UPLOAD heap. Uniform data is its only consumer
/// today; it is bound as a root CBV, so no descriptor heap is involved.
pub struct Buffer {
  handle : UInt64
}

///|
pub fn Buffer::create(device : Device, size : Int) -> Buffer? {
  let handle = buffer_create_ffi(device.handle, size)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Buffer::handle(self : Buffer) -> UInt64 {
  self.handle
}

///|
pub fn Buffer::size(self : Buffer) -> Int {
  buffer_size_ffi(self.handle).to_int()
}

///|
pub fn Buffer::gpu_address(self : Buffer) -> UInt64 {
  buffer_gpu_address_ffi(self.handle)
}

///|
pub fn Buffer::write(self : Buffer, data : Bytes, offset? : Int = 0) -> Bool {
  buffer_write_ffi(self.handle, offset, data, data.length()) != 0
}

///|
pub fn Buffer::write_floats(
  self : Buffer,
  data : FixedArray[Float],
  offset? : Int = 0,
) -> Bool {
  buffer_write_floats_ffi(self.handle, offset, data, data.length()) != 0
}

///|
pub fn Buffer::destroy(self : Buffer) -> Unit {
  buffer_destroy_ffi(self.handle)
}

///|
pub fn CommandList::copy_to_readback(self : CommandList, tex : Texture, rb : Readback) -> Bool {
  cmd_copy_to_readback_ffi(self.handle, tex.handle, rb.handle) != 0
}

///|
///|
/// Binds the depth attachment used by the next draws (and by `clear_depth`).
/// Passing the depth texture here is what makes `draw` emit a DSV.
pub fn CommandList::set_depth(self : CommandList, tex : Texture) -> Bool {
  cmd_set_depth_ffi(self.handle, tex.handle) != 0
}

///|
/// Binds a texture's SRV through the descriptor table at `root_index` (the table
/// follows the root CBVs in the signature the pipelines build). The sampler is a
/// static one declared by the root signature, so no sampler object is needed.
pub fn CommandList::set_texture(
  self : CommandList,
  root_index : Int,
  sampler_root : Int,
  tex : Texture,
  sampler? : Sampler,
) -> Bool {
  let sampler_handle = match sampler {
    Some(s) => s.handle
    None => (0 : UInt64)
  }
  cmd_set_texture_ffi(
    self.handle,
    root_index,
    sampler_root,
    tex.handle,
    sampler_handle,
  ) != 0
}

///|
/// Clears the bound depth attachment; requires `ResourceState::DepthWrite`.
pub fn CommandList::clear_depth(self : CommandList, tex : Texture, depth? : Float = 1.0) -> Bool {
  cmd_clear_depth_ffi(self.handle, tex.handle, depth) != 0
}

pub fn CommandList::execute_wait(self : CommandList, device : Device) -> Bool {
  commands_execute_wait_ffi(device.handle, self.handle) != 0
}

// ---------------------------------------------------------------------------
// Compute pipeline (root signature: root CBVs, then one UAV table)
// ---------------------------------------------------------------------------

///|
/// Root constant buffers at `register(b, space0)` followed by one UAV table
/// covering `u0..u(n-1)` in space 0.
pub fn ComputePipeline::create(
  device : Device,
  cs_dxil : FixedArray[UInt32],
  cs_bytes : Int,
  cbv_count? : Int = 0,
  uav_tables? : Int = 0,
  accel_tables? : Int = 0,
  push_bytes? : Int = 0,
) -> ComputePipeline? {
  let handle = compute_pipeline_create_ffi(
    device.handle,
    cs_dxil,
    cs_bytes,
    cbv_count,
    uav_tables,
    accel_tables,
    push_bytes,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn ComputePipeline::handle(self : ComputePipeline) -> UInt64 {
  self.handle
}

///|
pub fn ComputePipeline::destroy(self : ComputePipeline) -> Unit {
  compute_pipeline_destroy_ffi(self.handle)
}

///|
/// Binds the compute pipeline and its root signature. Graphics and compute use
/// different D3D12 entry points, so this is separate from `set_pipeline`.
pub fn CommandList::set_compute_pipeline(
  self : CommandList,
  pipe : ComputePipeline,
) -> Bool {
  cmd_set_compute_pipeline_ffi(self.handle, pipe.handle) != 0
}

///|
/// Binds a storage texture as an UAV table at `root_index` (the UAV table follows
/// the root CBVs in the signature `ComputePipeline::create` builds).
pub fn CommandList::set_uav(self : CommandList, root_index : Int, tex : Texture) -> Bool {
  cmd_set_uav_ffi(self.handle, root_index, tex.handle) != 0
}

///|
pub fn CommandList::dispatch(self : CommandList, x : Int, y : Int, z? : Int = 1) -> Bool {
  cmd_dispatch_ffi(self.handle, x, y, z) != 0
}

///|
/// A compute pipeline: one DXIL blob, its own root signature.
pub struct ComputePipeline {
  handle : UInt64
}

///|
/// Uploads `data` into the pipeline's root constants (`b15, space0`). The value
/// count is the number of 32-bit words, so it must match the pipeline's
/// `push_bytes`.
pub fn CommandList::set_root_constants(
  self : CommandList,
  index : Int,
  data : FixedArray[Float],
  offset? : Int = 0,
) -> Bool {
  if offset == 0 {
    cmd_set_root_constants_ffi(self.handle, index, data, data.length()) != 0
  } else {
    cmd_set_root_constants_offset_ffi(
      self.handle,
      index,
      data,
      data.length(),
      offset / 4,
    ) != 0
  }
}

///|
/// The compute-pipeline counterpart of `set_root_constants`.
pub fn CommandList::set_compute_root_constants(
  self : CommandList,
  index : Int,
  data : FixedArray[Float],
  offset? : Int = 0,
) -> Bool {
  if offset == 0 {
    cmd_set_compute_root_constants_ffi(self.handle, index, data, data.length()) != 0
  } else {
    cmd_set_compute_root_constants_offset_ffi(
      self.handle,
      index,
      data,
      data.length(),
      offset / 4,
    ) != 0
  }
}

///|
/// A DEFAULT-heap buffer with an UAV (`RWStructuredBuffer`-style, 32-bit
/// elements). Written by compute shaders and read back with `Buffer::download`.
pub fn Buffer::create_storage(device : Device, size : Int) -> Buffer? {
  let handle = storage_buffer_create_ffi(device.handle, size)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
/// Binds a storage buffer as the UAV table at `root_index` of a compute pipeline.
pub fn CommandList::set_buffer_uav(self : CommandList, root_index : Int, buffer : Buffer) -> Bool {
  cmd_set_buffer_uav_ffi(self.handle, root_index, buffer.handle) != 0
}

///|
/// Byte-oriented root constants: the caller supplies the exact word block the
/// shader declares (see `set_root_constants` for the float convenience wrapper).
pub fn CommandList::set_root_constants_bytes(
  self : CommandList,
  index : Int,
  data : Bytes,
  offset? : Int = 0,
) -> Bool {
  if offset == 0 {
    cmd_set_root_constants_bytes_ffi(self.handle, index, data, data.length() / 4) != 0
  } else {
    cmd_set_root_constants_bytes_offset_ffi(
      self.handle,
      index,
      data,
      data.length() / 4,
      offset / 4,
    ) != 0
  }
}

///|
pub fn CommandList::set_compute_root_constants_bytes(
  self : CommandList,
  index : Int,
  data : Bytes,
  offset? : Int = 0,
) -> Bool {
  if offset == 0 {
    cmd_set_compute_root_constants_bytes_ffi(self.handle, index, data, data.length() / 4) != 0
  } else {
    cmd_set_compute_root_constants_bytes_offset_ffi(
      self.handle,
      index,
      data,
      data.length() / 4,
      offset / 4,
    ) != 0
  }
}

///|
/// Copies a storage buffer through a READBACK staging buffer and returns its
/// contents. Valid once every list that wrote it has completed.
pub fn Buffer::download(self : Buffer, device : Device, size : Int) -> FixedArray[Byte]? {
  if size <= 0 || size > self.size() {
    return None
  }
  let out = FixedArray::make(size, (0 : Byte))
  if buffer_download_ffi(device.handle, self.handle, out, size) == 0 {
    None
  } else {
    Some(out)
  }
}

///|
/// A READBACK-heap buffer: the destination of `copy_buffer`, readable on the CPU
/// as soon as the submitting list has completed.
pub fn Buffer::create_readback(device : Device, size : Int) -> Buffer? {
  let handle = readback_buffer_create_ffi(device.handle, size)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn CommandList::copy_buffer(
  self : CommandList,
  src : Buffer,
  dst : Buffer,
  size : Int,
) -> Bool {
  cmd_copy_buffer_ffi(self.handle, src.handle, dst.handle, size) != 0
}

///|
/// Indirect execution bounded by a GPU-side count: only the first `count[0]`
/// arguments run, `max_count` bounds the argument buffer.
pub fn CommandList::execute_indirect_counted(
  self : CommandList,
  kind : IndirectKind,
  buffer : Buffer,
  count_buffer : Buffer,
  max_count? : Int = 1,
  tex? : Texture,
  offset? : Int = 0,
  count_offset? : Int = 0,
  flip_y? : Bool = false,
) -> Bool {
  let tex_handle = match tex {
    Some(tex) => tex.handle
    None => (0 : UInt64)
  }
  let code = match kind {
    IndirectKind::Draw => 0
    IndirectKind::DrawIndexed => 1
    IndirectKind::Dispatch => 2
  }
  cmd_execute_indirect_counted_ffi(
    self.handle,
    tex_handle,
    code,
    buffer.handle(),
    offset,
    max_count,
    count_buffer.handle(),
    count_offset,
    if flip_y { 1 } else { 0 },
  ) != 0
}

// ---------------------------------------------------------------------------
// Acceleration structures (ray queries)
// ---------------------------------------------------------------------------

///|
/// A bottom-level (blas = true) or top-level acceleration structure. Ray queries
/// read it as an SRV at `t0`; there is no state object / shader binding table
/// path here, which is what `trace_rays` pipelines would need.
pub struct AccelStruct {
  handle : UInt64
}

///|
pub fn AccelStruct::create_blas(
  device : Device,
  vertex_address : UInt64,
  vertex_count : Int,
  vertex_stride? : Int = 12,
  index_address? : UInt64 = (0 : UInt64),
  index_count? : Int = 0,
  index32? : Bool = true,
  is_opaque? : Bool = true,
) -> AccelStruct? {
  let handle = accel_blas_create_ffi(
    device.handle,
    vertex_address,
    vertex_count,
    vertex_stride,
    index_address,
    index_count,
    if index32 { 1 } else { 0 },
    if is_opaque { 1 } else { 0 },
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn AccelStruct::create_tlas(
  device : Device,
  blas_address : UInt64,
  transform : FixedArray[Float],
  custom_index? : Int = 0,
  mask? : Int = 0xFF,
) -> AccelStruct? {
  if blas_address == (0 : UInt64) || transform.length() < 12 {
    return None
  }
  let handle = accel_tlas_create_ffi(
    device.handle,
    blas_address,
    transform,
    custom_index,
    mask,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn AccelStruct::handle(self : AccelStruct) -> UInt64 {
  self.handle
}

///|
pub fn AccelStruct::gpu_address(self : AccelStruct) -> UInt64 {
  accel_gpu_address_ffi(self.handle)
}

///|
pub fn CommandList::set_accel(
  self : CommandList,
  root_index : Int,
  accel : AccelStruct,
  compute? : Bool = true,
) -> Bool {
  cmd_set_accel_ffi(self.handle, root_index, accel.handle, if compute { 1 } else { 0 }) != 0
}

///|
pub fn AccelStruct::destroy(self : AccelStruct) -> Unit {
  accel_destroy_ffi(self.handle)
}

// ---------------------------------------------------------------------------
// Mesh shaders
// ---------------------------------------------------------------------------

///|
/// A pipeline whose vertices come from a mesh shader. There is no input layout
/// and no vertex buffer: `CommandList::dispatch_mesh` launches the workgroups.
pub struct MeshPipeline {
  handle : UInt64
}

///|
pub fn MeshPipeline::create(
  device : Device,
  ms_dxil : FixedArray[UInt32],
  ps_dxil : FixedArray[UInt32],
  color_format? : Int = 0,
  depth? : Bool = false,
  depth_write? : Bool = true,
  flip_y? : Bool = true,
) -> MeshPipeline? {
  let handle = mesh_pipeline_create_ffi(
    device.handle,
    ms_dxil,
    ms_dxil.length() * 4,
    ps_dxil,
    ps_dxil.length() * 4,
    color_format,
    if depth { 1 } else { 0 },
    if depth_write { 1 } else { 0 },
    if flip_y { 1 } else { 0 },
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn MeshPipeline::handle(self : MeshPipeline) -> UInt64 {
  self.handle
}

///|
pub fn MeshPipeline::destroy(self : MeshPipeline) -> Unit {
  pipeline_destroy_ffi(self.handle)
}

///|
pub fn CommandList::set_mesh_pipeline(self : CommandList, pipe : MeshPipeline) -> Bool {
  cmd_set_pipeline_ffi(self.handle, pipe.handle) != 0
}

///|
/// Runs `x * y * z` mesh-shader workgroups into `tex`.
pub fn CommandList::dispatch_mesh(
  self : CommandList,
  tex : Texture,
  x : Int,
  y? : Int = 1,
  z? : Int = 1,
  flip_y? : Bool = true,
) -> Bool {
  cmd_dispatch_mesh_ffi(
    self.handle,
    tex.handle,
    x,
    y,
    z,
    if flip_y { 1 } else { 0 },
  ) != 0
}

// ---------------------------------------------------------------------------
// Indirect arguments and timestamp queries
// ---------------------------------------------------------------------------

///|
/// The three argument layouts D3D12 executes. Their byte layouts match Vulkan's
/// `VkDrawIndirectCommand` (16 B), `VkDrawIndexedIndirectCommand` (20 B) and
/// `VkDispatchIndirectCommand` (12 B), so one argument buffer serves both backends.
pub(all) enum IndirectKind {
  Draw
  DrawIndexed
  Dispatch
}

///|
/// Runs `count` indirect arguments starting at `buffer + offset`. `tex` is the
/// render target for the draw kinds and is omitted for `Dispatch`.
pub fn CommandList::execute_indirect(
  self : CommandList,
  kind : IndirectKind,
  buffer : Buffer,
  tex? : Texture,
  count? : Int = 1,
  offset? : Int = 0,
  flip_y? : Bool = false,
) -> Bool {
  let tex_handle = match tex {
    Some(tex) => tex.handle
    None => (0 : UInt64)
  }
  let code = match kind {
    IndirectKind::Draw => 0
    IndirectKind::DrawIndexed => 1
    IndirectKind::Dispatch => 2
  }
  cmd_execute_indirect_ffi(
    self.handle,
    tex_handle,
    code,
    buffer.handle(),
    offset,
    count,
    if flip_y { 1 } else { 0 },
  ) != 0
}

///|
/// A TIMESTAMP query heap plus its readback staging buffer.
pub struct TimestampPool {
  handle : UInt64
}

///|
pub fn TimestampPool::create(device : Device, count : Int) -> TimestampPool? {
  let handle = timestamp_pool_create_ffi(device.handle, count)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn TimestampPool::handle(self : TimestampPool) -> UInt64 {
  self.handle
}

///|
pub fn TimestampPool::count(self : TimestampPool) -> Int {
  timestamp_pool_count_ffi(self.handle).to_int()
}

///|
/// GPU ticks per second (0 when the queue would not report one).
pub fn TimestampPool::frequency(self : TimestampPool) -> Int64 {
  timestamp_pool_frequency_ffi(self.handle)
}

///|
pub fn TimestampPool::write(self : TimestampPool, cmd : CommandList, index : Int) -> Bool {
  timestamp_pool_write_ffi(cmd.handle, self.handle, index) != 0
}

///|
/// Copies every query into the staging buffer; must be recorded after the writes.
pub fn TimestampPool::resolve(self : TimestampPool, cmd : CommandList) -> Bool {
  timestamp_pool_resolve_ffi(cmd.handle, self.handle) != 0
}

///|
/// Raw tick value, valid once the command list that resolved it has completed.
/// Returns -1 for an out-of-range index.
pub fn TimestampPool::read(self : TimestampPool, index : Int) -> Int64 {
  timestamp_pool_read_ffi(self.handle, index)
}

///|
pub fn TimestampPool::destroy(self : TimestampPool) -> Unit {
  timestamp_pool_destroy_ffi(self.handle)
}

// ---------------------------------------------------------------------------
// Vertex and index buffers
// ---------------------------------------------------------------------------

///|
/// Binds a vertex buffer to input slot `slot`. `stride` must match the pipeline's
/// vertex layout, because D3D12 takes it per binding (Vulkan takes it from the
/// pipeline as well, just at creation time).
pub fn CommandList::bind_vertex(
  self : CommandList,
  slot : Int,
  buffer : Buffer,
  size : Int,
  stride : Int,
  offset? : Int = 0,
) -> Bool {
  cmd_bind_vertex_ffi(self.handle, slot, buffer.gpu_address() + offset.to_uint64(), size, stride) !=
    0
}

///|
/// Binds an index buffer; `index32` selects `R32_UINT` over `R16_UINT`.
pub fn CommandList::bind_index(
  self : CommandList,
  buffer : Buffer,
  size : Int,
  index32 : Bool,
  offset? : Int = 0,
) -> Bool {
  cmd_bind_index_ffi(
    self.handle,
    buffer.gpu_address() + offset.to_uint64(),
    size,
    if index32 { 1 } else { 0 },
  ) != 0
}

///|
/// Indexed draw. `flip_y` must match the pipeline (see `Pipeline::create`).
pub fn CommandList::draw_indexed(
  self : CommandList,
  tex : Texture,
  index_count : Int,
  instances? : Int = 1,
  first_index? : Int = 0,
  vertex_offset? : Int = 0,
  first_instance? : Int = 0,
  flip_y? : Bool = false,
) -> Bool {
  if first_instance == 0 {
    cmd_draw_indexed_ffi(
      self.handle,
      tex.handle,
      index_count,
      instances,
      first_index,
      vertex_offset,
      if flip_y { 1 } else { 0 },
    ) != 0
  } else {
    cmd_draw_indexed_instanced_ffi(
      self.handle,
      tex.handle,
      index_count,
      instances,
      first_index,
      vertex_offset,
      first_instance,
      if flip_y { 1 } else { 0 },
    ) != 0
  }
}

// ---------------------------------------------------------------------------
// Placed buffers (a heap the caller carves offsets out of)
// ---------------------------------------------------------------------------

///|
/// A UPLOAD heap whose contents are carved into placed buffers. The free-list
/// itself lives on the caller's side (the RHI uses the shared `RegionPool`).
pub struct BufferHeap {
  handle : UInt64
}

///|
pub fn BufferHeap::create(device : Device, size : Int) -> BufferHeap? {
  let handle = buffer_heap_create_ffi(device.handle, size)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn BufferHeap::handle(self : BufferHeap) -> UInt64 {
  self.handle
}

///|
/// Places a buffer at `offset` inside the heap. `offset` must satisfy
/// `D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT` (64 KiB).
pub fn Buffer::place(heap : BufferHeap, offset : Int, size : Int) -> Buffer? {
  let handle = buffer_place_ffi(heap.handle, offset, size)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn BufferHeap::destroy(self : BufferHeap) -> Unit {
  buffer_heap_destroy_ffi(self.handle)
}

// ---------------------------------------------------------------------------
// Sampler (a slot in the device's shader-visible sampler heap)
// ---------------------------------------------------------------------------

///|
/// How the sampled texels are interpolated.
pub(all) enum SamplerFilter {
  Point
  Linear
  Anisotropic
}

///|
/// Out-of-range addressing, applied to all three axes.
pub(all) enum SamplerAddress {
  Wrap
  Mirror
  Clamp
  Border
  MirrorOnce
}

///|
/// A sampler heap slot. Bound with `CommandList::set_texture`.
pub struct Sampler {
  handle : UInt64
}

///|
pub fn Sampler::create(
  device : Device,
  filter : SamplerFilter,
  address : SamplerAddress,
  anisotropy? : Float = 1.0,
  min_lod? : Float = 0.0,
  max_lod? : Float = 1000.0,
  mip_lod_bias? : Float = 0.0,
) -> Sampler? {
  let filter_code = match filter {
    Point => 0
    Linear => 1
    Anisotropic => 2
  }
  let address_code = match address {
    Wrap => 0
    Mirror => 1
    Clamp => 2
    Border => 3
    MirrorOnce => 4
  }
  let handle = sampler_create_ffi(
    device.handle,
    filter_code,
    address_code,
    anisotropy,
    min_lod,
    max_lod,
    mip_lod_bias,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Sampler::handle(self : Sampler) -> UInt64 {
  self.handle
}

///|
/// Returns the slot to the device's free list; the heap slot is reused by the
/// next sampler created.
pub fn Sampler::destroy(self : Sampler) -> Unit {
  sampler_destroy_ffi(self.handle)
}

// ---------------------------------------------------------------------------
// DXGI flip-model swapchain
// ---------------------------------------------------------------------------

///|
/// Outcome of `Swapchain::present`.
pub(all) enum PresentResult {
  Presented
  /// Presented, but the window is not visible (minimised / occluded).
  Occluded
  Failed
}

///|
/// DXGI flip-model swapchain bound to a native window handle (`HWND`).
pub struct Swapchain {
  handle : UInt64
}

///|
pub fn Swapchain::create(
  device : Device,
  hwnd : UInt64,
  width : Int,
  height : Int,
) -> Swapchain? {
  let handle = swapchain_create_ffi(device.handle, hwnd, width, height)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Swapchain::handle(self : Swapchain) -> UInt64 {
  self.handle
}

///|
pub fn Swapchain::width(self : Swapchain) -> Int {
  swapchain_width_ffi(self.handle).to_int()
}

///|
pub fn Swapchain::height(self : Swapchain) -> Int {
  swapchain_height_ffi(self.handle).to_int()
}

///|
pub fn Swapchain::current_index(self : Swapchain) -> Int {
  swapchain_current_index_ffi(self.handle).to_int()
}

///|
/// A borrowed handle to back buffer `index`. The returned `Texture` must be
/// destroyed (which only frees the wrapper), and it starts in
/// `ResourceState::Present`.
pub fn Swapchain::target(self : Swapchain, index : Int) -> Texture? {
  let handle = swapchain_target_ffi(self.handle, index)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Swapchain::present(self : Swapchain) -> PresentResult {
  match swapchain_present_ffi(self.handle).to_int() {
    1 => Presented
    2 => Occluded
    _ => Failed
  }
}

///|
pub fn Swapchain::resize(
  self : Swapchain,
  device : Device,
  width : Int,
  height : Int,
) -> Bool {
  swapchain_resize_ffi(device.handle, self.handle, width, height) != 0
}

///|
pub fn Swapchain::destroy(self : Swapchain) -> Unit {
  swapchain_destroy_ffi(self.handle)
}

// ---------------------------------------------------------------------------
// Extended RHI surface (full PSO, dynamic state, blit/resolve, PSV0 reflection)
// ---------------------------------------------------------------------------

///|
pub fn Pipeline::create_ex(
  device : Device,
  vs : FixedArray[UInt32],
  ps : FixedArray[UInt32],
  regs : FixedArray[Int],
  state : FixedArray[Int],
  bias_constant : Float,
  bias_slope : Float,
  color_formats : FixedArray[Int],
  blend_params : FixedArray[Int],
  inputs : FixedArray[Int],
) -> Pipeline? {
  let handle = pipeline_create_ex_ffi(
    device.handle,
    vs,
    vs.length() * 4,
    ps,
    ps.length() * 4,
    regs,
    regs.length(),
    state,
    state.length(),
    bias_constant,
    bias_slope,
    color_formats.length(),
    color_formats,
    blend_params,
    inputs.length() / 5,
    inputs,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn ComputePipeline::create_ex(
  device : Device,
  cs : FixedArray[UInt32],
  cbv_regs : FixedArray[Int],
  storage_regs : FixedArray[Int],
  storage_srv_mask : Int,
  image_regs : FixedArray[Int],
  accel_regs : FixedArray[Int],
  push_bytes : Int,
) -> ComputePipeline? {
  let handle = compute_pipeline_create_ex_ffi(
    device.handle,
    cs,
    cs.length() * 4,
    cbv_regs.length(),
    cbv_regs,
    storage_regs.length(),
    storage_regs,
    storage_srv_mask,
    image_regs.length(),
    image_regs,
    accel_regs.length(),
    accel_regs,
    push_bytes,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn dxil_psv0_storage_srv_mask(
  bc : FixedArray[UInt32],
  regs : FixedArray[Int],
) -> Int {
  dxil_psv0_storage_srv_mask_ffi(bc, bc.length() * 4, regs, regs.length()).to_int()
}

///|
pub fn dxil_reflect_compute(bc : FixedArray[UInt32]) -> FixedArray[Int]? {
  let out : FixedArray[Int] = FixedArray::make(48, 0)
  if dxil_reflect_compute_ffi(bc, bc.length() * 4, out) != 0 {
    Some(out)
  } else {
    None
  }
}

///|
pub fn Texture::create_ex(
  device : Device,
  width : Int,
  height : Int,
  format_code : Int,
  flags : Int,
  mip_levels? : Int = 1,
  array_layers? : Int = 1,
  is_cube? : Bool = false,
  sample_count? : Int = 1,
) -> Texture? {
  let handle = texture_create_ex_ffi(
    device.handle,
    width,
    height,
    format_code,
    flags,
    mip_levels,
    array_layers,
    if is_cube { 1 } else { 0 },
    sample_count,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Texture::create_storage_ex(
  device : Device,
  width : Int,
  height : Int,
  format_code : Int,
) -> Texture? {
  let handle = storage_texture_create_ex_ffi(
    device.handle,
    width,
    height,
    format_code,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Texture::upload_subregion(
  self : Texture,
  device : Device,
  x : Int,
  y : Int,
  w : Int,
  h : Int,
  pixels : Bytes,
  layer? : Int = 0,
  mip? : Int = 0,
) -> Bool {
  texture_upload_subregion_ffi(
    device.handle,
    self.handle,
    x,
    y,
    w,
    h,
    pixels,
    pixels.length(),
    layer,
    mip,
  ) != 0
}

///|
pub fn Readback::create_ex(
  device : Device,
  width : Int,
  height : Int,
  bpp : Int,
) -> Readback? {
  let handle = readback_create_ex_ffi(device.handle, width, height, bpp)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Sampler::create_ex(
  device : Device,
  mag : Int,
  min_f : Int,
  mip : Int,
  address : Int,
  border_color : Int,
  anisotropy : Float,
  min_lod : Float,
  max_lod : Float,
  mip_lod_bias : Float,
  compare_op : Int,
) -> Sampler? {
  let handle = sampler_create_ex_ffi(
    device.handle,
    mag,
    min_f,
    mip,
    address,
    border_color,
    anisotropy,
    min_lod,
    max_lod,
    mip_lod_bias,
    compare_op,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn CommandList::set_viewport(
  self : CommandList,
  x : Float,
  y : Float,
  w : Float,
  h : Float,
  flip_y? : Bool = false,
) -> Bool {
  cmd_set_viewport_ffi(
    self.handle,
    x,
    y,
    w,
    h,
    if flip_y { 1 } else { 0 },
  ) != 0
}

///|
pub fn CommandList::set_scissor(
  self : CommandList,
  x : Int,
  y : Int,
  w : Int,
  h : Int,
) -> Bool {
  cmd_set_scissor_ffi(self.handle, x, y, w, h) != 0
}

///|
pub fn CommandList::reset_viewport_scissor(self : CommandList) -> Bool {
  cmd_reset_viewport_scissor_ffi(self.handle) != 0
}

///|
pub fn CommandList::set_stencil_ref(
  self : CommandList,
  ref_val : Int,
) -> Bool {
  cmd_set_stencil_ref_ffi(self.handle, ref_val) != 0
}

///|
pub fn CommandList::clear_depth_stencil(
  self : CommandList,
  tex : Texture,
  clear_depth : Bool,
  depth : Float,
  clear_stencil : Bool,
  stencil : Int,
) -> Bool {
  cmd_clear_depth_stencil_ffi(
    self.handle,
    tex.handle,
    if clear_depth { 1 } else { 0 },
    depth,
    if clear_stencil { 1 } else { 0 },
    stencil,
  ) != 0
}

///|
pub fn CommandList::uav_barrier(self : CommandList) -> Bool {
  cmd_uav_barrier_ffi(self.handle) != 0
}

///|
pub fn CommandList::blit(
  self : CommandList,
  src : Texture,
  dst : Texture,
  linear? : Bool = true,
) -> Bool {
  cmd_blit_ffi(
    self.handle,
    src.handle,
    dst.handle,
    if linear { 1 } else { 0 },
  ) != 0
}

///|
pub fn CommandList::resolve(
  self : CommandList,
  src : Texture,
  dst : Texture,
) -> Bool {
  cmd_resolve_ffi(self.handle, src.handle, dst.handle) != 0
}

///|
pub fn CommandList::copy_to_readback_mip(
  self : CommandList,
  tex : Texture,
  rb : Readback,
  mip : Int,
  layer? : Int = 0,
) -> Bool {
  cmd_copy_to_readback_mip_ffi(self.handle, tex.handle, rb.handle, mip, layer) != 0
}

///|
pub fn CommandList::set_buffer_srv(
  self : CommandList,
  root_index : Int,
  buf : Buffer,
  offset? : Int = 0,
  size? : Int = 0,
  compute? : Bool = false,
) -> Bool {
  cmd_set_buffer_srv_ffi(
    self.handle,
    root_index,
    buf.handle,
    offset,
    size,
    if compute { 1 } else { 0 },
  ) != 0
}

///|
pub fn CommandList::set_buffer_uav_ex(
  self : CommandList,
  root_index : Int,
  buf : Buffer,
  offset? : Int = 0,
  size? : Int = 0,
) -> Bool {
  cmd_set_buffer_uav_ex_ffi(self.handle, root_index, buf.handle, offset, size) != 0
}

///|
pub fn CommandList::clear_depth_target(self : CommandList) -> Bool {
  cmd_set_depth_ffi(self.handle, (0 : UInt64)) != 0
}

///|
pub fn CommandList::draw_opt(
  self : CommandList,
  tex : Texture?,
  vertices : Int,
  instances? : Int = 1,
  flip_y? : Bool = false,
) -> Bool {
  let th = match tex {
    Some(t) => t.handle
    None => (0 : UInt64)
  }
  cmd_draw_ffi(
    self.handle,
    th,
    vertices,
    instances,
    if flip_y { 1 } else { 0 },
  ) != 0
}

///|
pub fn CommandList::draw_instanced_opt(
  self : CommandList,
  tex : Texture?,
  vertices : Int,
  instances? : Int = 1,
  first_vertex? : Int = 0,
  first_instance? : Int = 0,
  flip_y? : Bool = false,
) -> Bool {
  let th = match tex {
    Some(t) => t.handle
    None => (0 : UInt64)
  }
  cmd_draw_instanced_ffi(
    self.handle,
    th,
    vertices,
    instances,
    first_vertex,
    first_instance,
    if flip_y { 1 } else { 0 },
  ) != 0
}

///|
pub fn CommandList::draw_indexed_opt(
  self : CommandList,
  tex : Texture?,
  index_count : Int,
  instances? : Int = 1,
  first_index? : Int = 0,
  vertex_offset? : Int = 0,
  first_instance? : Int = 0,
  flip_y? : Bool = false,
) -> Bool {
  let th = match tex {
    Some(t) => t.handle
    None => (0 : UInt64)
  }
  cmd_draw_indexed_instanced_ffi(
    self.handle,
    th,
    index_count,
    instances,
    first_index,
    vertex_offset,
    first_instance,
    if flip_y { 1 } else { 0 },
  ) != 0
}

///|
pub fn AccelStruct::create_blas_multi(
  device : Device,
  vaddrs : FixedArray[UInt64],
  vcounts : FixedArray[Int],
  vstrides : FixedArray[Int],
  iaddrs : FixedArray[UInt64],
  icounts : FixedArray[Int],
  index32s : FixedArray[Int],
  opaques : FixedArray[Int],
) -> AccelStruct? {
  let handle = blas_create_multi_ffi(
    device.handle,
    vaddrs.length(),
    vaddrs,
    vcounts,
    vstrides,
    iaddrs,
    icounts,
    index32s,
    opaques,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn AccelStruct::create_tlas_multi(
  device : Device,
  blas_addrs : FixedArray[UInt64],
  transforms12 : FixedArray[Float],
  custom_indices : FixedArray[Int],
  masks : FixedArray[Int],
) -> AccelStruct? {
  let handle = tlas_create_multi_ffi(
    device.handle,
    blas_addrs.length(),
    blas_addrs,
    transforms12,
    custom_indices,
    masks,
  )
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub struct Fence {
  handle : UInt64
}

///|
pub fn Fence::create(device : Device, signaled? : Bool = false) -> Fence? {
  let handle = fence_create_ffi(device.handle, if signaled { 1 } else { 0 })
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn Fence::wait(self : Fence) -> Bool {
  fence_wait_ffi(self.handle) != 0
}

///|
pub fn Fence::reset(self : Fence) -> Unit {
  fence_reset_ffi(self.handle)
}

///|
pub fn Fence::destroy(self : Fence) -> Unit {
  fence_destroy_ffi(self.handle)
}

///|
pub fn CommandList::submit_fence(
  self : CommandList,
  device : Device,
  fence : Fence?,
) -> Bool {
  let fh = match fence {
    Some(f) => f.handle
    None => (0 : UInt64)
  }
  commands_submit_fence_ffi(device.handle, self.handle, fh) != 0
}

///|
pub struct BindlessHeap {
  handle : UInt64
}

///|
pub fn BindlessHeap::create(device : Device, capacity : Int) -> BindlessHeap? {
  let handle = bindless_create_ffi(device.handle, capacity)
  if handle == (0 : UInt64) {
    None
  } else {
    Some({ handle, })
  }
}

///|
pub fn BindlessHeap::register_texture(
  self : BindlessHeap,
  tex : Texture,
  sampler? : Sampler,
) -> Int {
  let sh = match sampler {
    Some(s) => s.handle
    None => (0 : UInt64)
  }
  bindless_register_ffi(self.handle, tex.handle, sh).to_int()
}

///|
pub fn BindlessHeap::unregister_texture(
  self : BindlessHeap,
  index : Int,
) -> Unit {
  bindless_unregister_ffi(self.handle, index)
}

///|
pub fn BindlessHeap::destroy(self : BindlessHeap) -> Unit {
  bindless_destroy_ffi(self.handle)
}

///|
pub fn CommandList::set_bindless(
  self : CommandList,
  srv_root : Int,
  samp_root : Int,
  heap : BindlessHeap,
) -> Bool {
  cmd_set_bindless_ffi(self.handle, srv_root, samp_root, heap.handle) != 0
}