euv-core 0.47.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Probed physical GPU adapter and native shader compilation telemetry.
#[derive(Clone, Debug, Eq, Getter, New, PartialEq)]
pub struct GpuAdapterInfo {
    /// Active graphics backend identifier.
    #[get(type(copy))]
    pub(crate) backend: GraphicsBackend,
    /// Detected physical GPU adapter name (for example `"Apple M3 Max"`).
    pub(crate) device_name: String,
    /// True when a real physical OS GPU device/runtime was successfully initialized.
    #[get(type(copy))]
    pub(crate) hardware_verified: bool,
    /// True when the native UI shader (`MSL`/`HLSL`/`GLSL`/`WGSL`) compiled on the host GPU.
    #[get(type(copy))]
    pub(crate) shader_compiled: bool,
}

/// One hardware-fenced staging buffer slot inside `NativeGpuDriver`'s triple-buffered GPU ring.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct NativeGpuRingSlot {
    /// Native OS shared-memory buffer handle (`id<MTLBuffer>` on Apple, `0` when unallocated).
    #[get(type(copy))]
    pub(crate) buffer_handle: usize,
    /// CPU-writable mapped memory address (`[MTLBuffer contents]` on Apple, `0` when unallocated).
    #[get(type(copy))]
    pub(crate) contents_ptr: usize,
    /// Allocated byte capacity of this ring slot buffer.
    #[get(type(copy))]
    pub(crate) capacity_bytes: usize,
    /// Retained in-flight command buffer handle guarding reuse (`id<MTLCommandBuffer>` on Apple).
    #[get(type(copy))]
    pub(crate) in_flight_cmd_buffer: usize,
}

/// Physical OS GPU driver wrapping Metal, D3D12, Vulkan, or WebGL2 with real fenced ring submission and automatic fallback.
#[derive(Debug, Getter)]
pub struct NativeGpuDriver {
    /// Probed physical adapter and shader status.
    pub(crate) adapter: GpuAdapterInfo,
    /// Opaque OS GPU device handle (`id<MTLDevice>` on Apple, `0` when fallback).
    #[get(type(copy))]
    pub(crate) device_handle: usize,
    /// Opaque OS GPU command queue handle (`id<MTLCommandQueue>` on Apple, `0` when fallback).
    #[get(type(copy))]
    pub(crate) queue_handle: usize,
    /// Opaque compiled shader library handle (`id<MTLLibrary>` on Apple, `0` when fallback).
    #[get(type(copy))]
    pub(crate) library_handle: usize,
    /// Opaque compiled uber-shader render pipeline handle (`id<MTLRenderPipelineState>` on Apple).
    #[get(type(copy))]
    pub(crate) pipeline_handle: usize,
    /// Opaque compiled Dual-Kawase `BackdropBlur` pipeline handle (`id<MTLRenderPipelineState>` on Apple).
    #[get(type(copy))]
    pub(crate) blur_pipeline_handle: usize,
    /// Attached native presentation layer handle (`CAMetalLayer*` on Apple, `0` when headless).
    #[get(type(copy))]
    pub(crate) surface_layer_handle: usize,
    /// Resident R8Unorm monochrome glyph atlas `MTLTexture` handle (`0` when unallocated).
    #[get(type(copy))]
    pub(crate) glyph_r8_texture_handle: usize,
    /// Resident RGBA8Unorm color glyph atlas `MTLTexture` handle (`0` when unallocated).
    #[get(type(copy))]
    pub(crate) glyph_rgba8_texture_handle: usize,
    /// Resident RGBA8Unorm UI image/sprite `MTLTexture` handle (`0` when unallocated).
    #[get(type(copy))]
    pub(crate) image_texture_handle: usize,
    /// Bound external GPU texture handle (3D game render target or hardware video surface).
    #[get(type(copy))]
    pub(crate) external_texture_handle: usize,
    /// Triple-buffered fenced GPU staging ring slots eliminating CPU/GPU buffer hazards.
    pub(crate) ring_slots: [NativeGpuRingSlot; GPU_DRIVER_RING_SLOTS],
    /// Active ring slot cursor cycling `0 -> 1 -> 2 -> 0` across frames.
    #[get(type(copy))]
    pub(crate) ring_cursor: usize,
    /// Persistent grow-only staging handle mirror reflecting the active ring slot.
    #[get(skip)]
    pub(crate) staging_buffer_handle: usize,
    /// Cached CPU-writable contents pointer mirror reflecting the active ring slot.
    #[get(skip)]
    pub(crate) staging_contents_ptr: usize,
    /// Byte capacity mirror reflecting the active ring slot.
    #[get(skip)]
    pub(crate) staging_buffer_capacity: usize,
    /// Total GPU frames submitted through this driver.
    #[get(type(copy))]
    pub(crate) submitted_frames: u64,
    /// Total instanced draw primitives uploaded to the GPU.
    #[get(type(copy))]
    pub(crate) uploaded_instances: u64,
    /// Cumulative instance payload bytes uploaded to the GPU across all frames.
    #[get(type(copy))]
    pub(crate) uploaded_bytes: u64,
    /// Instance payload bytes uploaded during the most recent submitted frame.
    #[get(type(copy))]
    pub(crate) last_upload_bytes: usize,
    /// Number of draw batches submitted during the most recent frame.
    #[get(type(copy))]
    pub(crate) last_submitted_batches: usize,
    /// Number of staging ring slot allocations or grows across the driver's lifetime.
    #[get(type(copy))]
    pub(crate) staging_reallocations: u64,
    /// Most recent hardware presentation timestamp in nanoseconds reported by Metal `addPresentedHandler:`.
    #[get(type(copy))]
    pub(crate) last_presented_time_ns: u64,
    /// Most recent GPU execution duration in nanoseconds (`GPUEndTime - GPUStartTime`).
    #[get(type(copy))]
    pub(crate) last_gpu_exec_ns: u64,
    /// Scratch buffer packing 64-byte `GpuPrimitiveInstance` records when legacy batches lack pre-packed primitives.
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    #[get(skip)]
    pub(crate) scratch_primitives: Vec<GpuPrimitiveInstance>,
    /// Fallback command recorder active on headless hosts.
    pub(crate) fallback: RecordingGpuDriver,
}

/// Retained native GPU device, queue, library, compiled pipelines, and dummy texture handles returned by `probe_native_gpu_full`.
#[derive(Clone, Debug)]
pub(crate) struct NativeGpuProbeOutput {
    /// Probed adapter capabilities and hardware backend descriptor.
    pub(crate) adapter: GpuAdapterInfo,
    /// Retained native GPU device handle (`0` when headless).
    pub(crate) device_handle: usize,
    /// Retained native GPU command queue handle (`0` when headless).
    pub(crate) queue_handle: usize,
    /// Retained compiled native shader library handle (`0` when headless).
    pub(crate) library_handle: usize,
    /// Retained cached uber-shader render pipeline handle (`0` when headless).
    pub(crate) pipeline_handle: usize,
    /// Retained cached Dual-Kawase blur render pipeline handle (`0` when headless).
    pub(crate) blur_pipeline_handle: usize,
    /// Retained 1x1 fallback R8Unorm texture handle (`0` when headless).
    pub(crate) dummy_r8_tex: usize,
    /// Retained 1x1 fallback RGBA8Unorm texture handle (`0` when headless).
    pub(crate) dummy_rgba8_tex: usize,
}

/// Process-wide cached Objective-C selector pointers for Apple Metal hot-path submission and encoding.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct MetalSelectors {
    /// Cached `retain` selector address.
    pub(crate) retain: usize,
    /// Cached `newBufferWithBytes:length:options:` selector address.
    pub(crate) new_buffer_with_bytes: usize,
    /// Cached `newBufferWithLength:options:` selector address.
    pub(crate) new_buffer_with_length: usize,
    /// Cached `contents` selector address.
    pub(crate) contents: usize,
    /// Cached `commandBuffer` selector address.
    pub(crate) command_buffer: usize,
    /// Cached `renderCommandEncoderWithDescriptor:` selector address.
    pub(crate) render_encoder: usize,
    /// Cached `setRenderPipelineState:` selector address.
    pub(crate) set_pipeline_state: usize,
    /// Cached `setVertexBuffer:offset:atIndex:` selector address.
    pub(crate) set_vertex_buffer: usize,
    /// Cached `setVertexBytes:length:atIndex:` selector address.
    pub(crate) set_vertex_bytes: usize,
    /// Cached `setFragmentTexture:atIndex:` selector address.
    pub(crate) set_fragment_texture: usize,
    /// Cached `setScissorRect:` selector address.
    pub(crate) set_scissor_rect: usize,
    /// Cached `drawPrimitives:vertexStart:vertexCount:instanceCount:baseInstance:` selector address.
    pub(crate) draw_primitives_instanced: usize,
    /// Cached `endEncoding` selector address.
    pub(crate) end_encoding: usize,
    /// Cached `presentDrawable:` selector address.
    pub(crate) present_drawable: usize,
    /// Cached `addPresentedHandler:` selector address (on `MTLDrawable`).
    pub(crate) add_presented_handler: usize,
    /// Cached `addCompletedHandler:` selector address (on `MTLCommandBuffer`).
    pub(crate) add_completed_handler: usize,
    /// Cached `presentedTime` selector address (on `MTLDrawable`).
    pub(crate) presented_time: usize,
    /// Cached `respondsToSelector:` selector address.
    pub(crate) responds_to_sel: usize,
    /// Cached `status` selector address.
    pub(crate) status: usize,
    /// Cached `waitUntilCompleted` selector address.
    pub(crate) wait_until_completed: usize,
    /// Cached `GPUStartTime` selector address.
    pub(crate) gpu_start_time: usize,
    /// Cached `GPUEndTime` selector address.
    pub(crate) gpu_end_time: usize,
    /// Cached `nextDrawable` selector address.
    pub(crate) next_drawable: usize,
    /// Cached `texture` selector address.
    pub(crate) texture: usize,
    /// Cached `width` selector address.
    pub(crate) width: usize,
    /// Cached `height` selector address.
    pub(crate) height: usize,
    /// Cached `renderPassDescriptor` selector address.
    pub(crate) render_pass_desc: usize,
    /// Cached `colorAttachments` selector address.
    pub(crate) color_attachments: usize,
    /// Cached `objectAtIndexedSubscript:` selector address.
    pub(crate) object_at_index: usize,
    /// Cached `setTexture:` selector address.
    pub(crate) set_texture: usize,
    /// Cached `setLoadAction:` selector address.
    pub(crate) set_load_action: usize,
    /// Cached `setStoreAction:` selector address.
    pub(crate) set_store_action: usize,
    /// Cached `setClearColor:` selector address.
    pub(crate) set_clear_color: usize,
    /// Cached `replaceRegion:mipmapLevel:withBytes:bytesPerRow:` selector address.
    pub(crate) replace_region: usize,
    /// Cached `commit` selector address.
    pub(crate) commit: usize,
    /// Cached `release` selector address.
    pub(crate) release: usize,
}

/// Process-wide cached Apple Metal runtime pipeline state eliminating redundant 40-180ms shader compiles.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[derive(Clone, Debug)]
pub(crate) struct MetalSharedPipelineCache {
    /// Probed physical adapter description and shader compile status.
    pub(crate) adapter: GpuAdapterInfo,
    /// Retained `id<MTLDevice>` handle.
    pub(crate) device: usize,
    /// Retained `id<MTLCommandQueue>` handle.
    pub(crate) queue: usize,
    /// Retained `id<MTLLibrary>` handle.
    pub(crate) library: usize,
    /// Retained `id<MTLRenderPipelineState>` handle for the 64-byte uber-shader.
    pub(crate) pipeline: usize,
    /// Retained `id<MTLRenderPipelineState>` handle for the Dual-Kawase blur shader.
    pub(crate) blur_pipeline: usize,
    /// Retained 1x1 fallback R8Unorm `MTLTexture` bound when glyph atlas is empty.
    pub(crate) dummy_r8_tex: usize,
    /// Retained 1x1 fallback RGBA8Unorm `MTLTexture` bound when color/image textures are unbound.
    pub(crate) dummy_rgba8_tex: usize,
}

/// Metal `MTLOrigin` 3D texel origin ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlOrigin {
    /// Horizontal texel origin.
    pub(crate) x: usize,
    /// Vertical texel origin.
    pub(crate) y: usize,
    /// Slice depth origin (`0`).
    pub(crate) z: usize,
}

/// Metal `MTLSize` 3D texel extent ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlSize {
    /// Extent width in texels.
    pub(crate) width: usize,
    /// Extent height in texels.
    pub(crate) height: usize,
    /// Extent depth in slices (`1`).
    pub(crate) depth: usize,
}

/// Metal `MTLRegion` 3D sub-texture upload rectangle ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlRegion {
    /// Sub-region origin.
    pub(crate) origin: MtlOrigin,
    /// Sub-region extent.
    pub(crate) size: MtlSize,
}

/// Metal `MTLScissorRect` physical scissor ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlScissorRect {
    /// Left physical pixel edge.
    pub(crate) x: usize,
    /// Top physical pixel edge.
    pub(crate) y: usize,
    /// Scissor width in physical pixels.
    pub(crate) width: usize,
    /// Scissor height in physical pixels.
    pub(crate) height: usize,
}

/// Metal `MTLClearColor` RGBA64F clear color ABI struct.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MtlClearColor {
    /// Red channel in `[0.0..=1.0]`.
    pub(crate) red: f64,
    /// Green channel in `[0.0..=1.0]`.
    pub(crate) green: f64,
    /// Blue channel in `[0.0..=1.0]`.
    pub(crate) blue: f64,
    /// Alpha channel in `[0.0..=1.0]`.
    pub(crate) alpha: f64,
}

/// Objective-C runtime global block descriptor ABI layout.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
pub(crate) struct ObjcBlockDescriptor {
    /// Reserved word (`0`).
    pub(crate) reserved: usize,
    /// Byte size of the block struct.
    pub(crate) size: usize,
}

/// Objective-C runtime zero-allocation global block ABI struct for Metal `addPresentedHandler:`.
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
pub(crate) struct ObjcGlobalBlock {
    /// Pointer to `_NSConcreteGlobalBlock`.
    pub(crate) isa: *const c_void,
    /// Block flags (`OBJC_BLOCK_IS_GLOBAL`).
    pub(crate) flags: i32,
    /// Reserved flags (`0`).
    pub(crate) reserved: i32,
    /// Block invoke C callback `(block, cmd_buffer)`.
    pub(crate) invoke: unsafe extern "C" fn(*mut c_void, *mut c_void),
    /// Descriptor pointer.
    pub(crate) descriptor: *const ObjcBlockDescriptor,
}

/// Process-wide cached Windows Direct3D 12 probe status eliminating repeated `LoadLibraryA`/`FreeLibrary` thrashing.
#[cfg(target_os = "windows")]
#[derive(Clone, Copy, Debug)]
pub(crate) struct WindowsD3d12ProbeCache {
    /// True when `d3d12.dll` exports `D3D12CreateDevice`.
    pub(crate) has_d3d12: bool,
    /// True when `d3dcompiler_47.dll` exports `D3DCompile`.
    pub(crate) has_compiler: bool,
}

/// Process-wide cached Vulkan loader probe status eliminating repeated `dlopen`/`dlclose` thrashing.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct VulkanProbeCache {
    /// True when Vulkan loader exports `vkCreateInstance` and `vkEnumeratePhysicalDevices`.
    pub(crate) available: bool,
    /// Cached `dlopen` handle kept resident for process lifetime.
    pub(crate) lib_handle: usize,
}