kronos_compute/implementation/
device.rs1use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6use std::ptr;
7use std::sync::atomic::{AtomicU64, Ordering};
8use std::sync::Mutex;
9use std::collections::HashMap;
10
11static DEVICE_COUNTER: AtomicU64 = AtomicU64::new(1);
13
14lazy_static::lazy_static! {
16 static ref DEVICES: Mutex<HashMap<u64, DeviceData>> = Mutex::new(HashMap::new());
17}
18
19struct DeviceData {
20 physical_device: VkPhysicalDevice,
21 queue_family_index: u32,
22 queue: VkQueue,
23}
24
25#[no_mangle]
27pub unsafe extern "C" fn vkGetPhysicalDeviceProperties(
28 physicalDevice: VkPhysicalDevice,
29 pProperties: *mut VkPhysicalDeviceProperties,
30) {
31 if physicalDevice.is_null() || pProperties.is_null() {
32 return;
33 }
34
35 let props = &mut *pProperties;
36
37 props.apiVersion = VK_API_VERSION_1_3;
39 props.driverVersion = VK_MAKE_VERSION(0, 2, 3);
40 props.vendorID = 0x1337; props.deviceID = 0x0001;
42 props.deviceType = VkPhysicalDeviceType::VirtualGpu;
43
44 let name = b"Kronos Virtual Compute Device\0";
46 let len = name.len().min(VK_MAX_PHYSICAL_DEVICE_NAME_SIZE as usize);
47 props.deviceName[..len].copy_from_slice(&name[..len].iter().map(|&b| b as i8).collect::<Vec<_>>());
48
49 props.limits.maxComputeSharedMemorySize = 49152; props.limits.maxComputeWorkGroupCount = [65536, 65536, 65536];
52 props.limits.maxComputeWorkGroupInvocations = 1024;
53 props.limits.maxComputeWorkGroupSize = [1024, 1024, 64];
54
55 props.sparseProperties = VkPhysicalDeviceSparseProperties {
57 residencyStandard2DBlockShape: VK_FALSE,
58 residencyStandard2DMultisampleBlockShape: VK_FALSE,
59 residencyStandard3DBlockShape: VK_FALSE,
60 residencyAlignedMipSize: VK_FALSE,
61 residencyNonResidentStrict: VK_FALSE,
62 };
63}
64
65#[no_mangle]
67pub unsafe extern "C" fn vkGetPhysicalDeviceQueueFamilyProperties(
68 physicalDevice: VkPhysicalDevice,
69 pQueueFamilyPropertyCount: *mut u32,
70 pQueueFamilyProperties: *mut VkQueueFamilyProperties,
71) {
72 if physicalDevice.is_null() || pQueueFamilyPropertyCount.is_null() {
73 return;
74 }
75
76 if pQueueFamilyProperties.is_null() {
77 *pQueueFamilyPropertyCount = 1;
78 return;
79 }
80
81 let count = *pQueueFamilyPropertyCount;
82 if count == 0 {
83 return;
84 }
85
86 let props = &mut *pQueueFamilyProperties;
88 props.queueFlags = VkQueueFlags::COMPUTE | VkQueueFlags::TRANSFER;
89 props.queueCount = 1;
90 props.timestampValidBits = 64;
91 props.minImageTransferGranularity = VkExtent3D { width: 1, height: 1, depth: 1 };
92
93 *pQueueFamilyPropertyCount = 1;
94}
95
96#[no_mangle]
98pub unsafe extern "C" fn vkCreateDevice(
99 physicalDevice: VkPhysicalDevice,
100 pCreateInfo: *const VkDeviceCreateInfo,
101 _pAllocator: *const VkAllocationCallbacks,
102 pDevice: *mut VkDevice,
103) -> VkResult {
104 if physicalDevice.is_null() || pCreateInfo.is_null() || pDevice.is_null() {
105 return VkResult::ErrorInitializationFailed;
106 }
107
108 let create_info = &*pCreateInfo;
109
110 if create_info.queueCreateInfoCount == 0 {
112 return VkResult::ErrorInitializationFailed;
113 }
114
115 let queue_info = &*create_info.pQueueCreateInfos;
117 let queue_family_index = queue_info.queueFamilyIndex;
118
119 let device_handle = DEVICE_COUNTER.fetch_add(1, Ordering::SeqCst);
121 let queue_handle = DEVICE_COUNTER.fetch_add(1, Ordering::SeqCst);
122
123 let device_data = DeviceData {
125 physical_device: physicalDevice,
126 queue_family_index,
127 queue: VkQueue::from_raw(queue_handle),
128 };
129
130 DEVICES.lock().unwrap().insert(device_handle, device_data);
131
132 *pDevice = VkDevice::from_raw(device_handle);
133
134 log::info!("Created Kronos device {:?} - pure Rust compute implementation", device_handle);
135
136 VkResult::Success
137}
138
139#[no_mangle]
141pub unsafe extern "C" fn vkGetDeviceQueue(
142 device: VkDevice,
143 queueFamilyIndex: u32,
144 queueIndex: u32,
145 pQueue: *mut VkQueue,
146) {
147 if device.is_null() || pQueue.is_null() {
148 return;
149 }
150
151 if queueIndex != 0 {
152 return; }
154
155 let handle = device.as_raw();
156 if let Some(device_data) = DEVICES.lock().unwrap().get(&handle) {
157 if queueFamilyIndex == device_data.queue_family_index {
158 *pQueue = device_data.queue;
159 }
160 }
161}
162
163#[no_mangle]
165pub unsafe extern "C" fn vkGetPhysicalDeviceMemoryProperties(
166 physicalDevice: VkPhysicalDevice,
167 pMemoryProperties: *mut VkPhysicalDeviceMemoryProperties,
168) {
169 if physicalDevice.is_null() || pMemoryProperties.is_null() {
170 return;
171 }
172
173 let props = &mut *pMemoryProperties;
174
175 props.memoryTypeCount = 1;
177 props.memoryTypes[0] = VkMemoryType {
178 propertyFlags: VkMemoryPropertyFlags::HOST_VISIBLE | VkMemoryPropertyFlags::HOST_COHERENT,
179 heapIndex: 0,
180 };
181
182 props.memoryHeapCount = 1;
184 props.memoryHeaps[0] = VkMemoryHeap {
185 size: 16 * 1024 * 1024 * 1024, flags: 0, };
188}
189
190#[no_mangle]
192pub unsafe extern "C" fn vkDestroyDevice(
193 device: VkDevice,
194 _pAllocator: *const VkAllocationCallbacks,
195) {
196 if device.is_null() {
197 return;
198 }
199
200 let handle = device.as_raw();
201 DEVICES.lock().unwrap().remove(&handle);
202
203 log::info!("Destroyed Kronos device {:?}", handle);
204}