kronos-compute 0.1.0

A high-performance compute-only Vulkan implementation with cutting-edge GPU optimizations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Core structures for Kronos API

use std::ffi::{c_char, c_void};
use std::ptr;
use crate::sys::*;
use crate::core::enums::*;
use crate::core::flags::*;

/// Helper for null-terminated string pointers
pub type PtrCStr = *const c_char;

/// 3D extent
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct VkExtent3D {
    pub width: u32,
    pub height: u32,
    pub depth: u32,
}

impl Default for VkExtent3D {
    fn default() -> Self {
        Self {
            width: 1,
            height: 1,
            depth: 1,
        }
    }
}

/// Application information
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkApplicationInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub pApplicationName: PtrCStr,
    pub applicationVersion: u32,
    pub pEngineName: PtrCStr,
    pub engineVersion: u32,
    pub apiVersion: u32,
}

impl Default for VkApplicationInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::ApplicationInfo,
            pNext: ptr::null(),
            pApplicationName: ptr::null(),
            applicationVersion: 0,
            pEngineName: ptr::null(),
            engineVersion: 0,
            apiVersion: VK_API_VERSION_1_0,
        }
    }
}

/// Instance creation information
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkInstanceCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkInstanceCreateFlags,
    pub pApplicationInfo: *const VkApplicationInfo,
    pub enabledLayerCount: u32,
    pub ppEnabledLayerNames: *const PtrCStr,
    pub enabledExtensionCount: u32,
    pub ppEnabledExtensionNames: *const PtrCStr,
}

impl Default for VkInstanceCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::InstanceCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            pApplicationInfo: ptr::null(),
            enabledLayerCount: 0,
            ppEnabledLayerNames: ptr::null(),
            enabledExtensionCount: 0,
            ppEnabledExtensionNames: ptr::null(),
        }
    }
}

/// Queue family properties
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkQueueFamilyProperties {
    pub queueFlags: VkQueueFlags,
    pub queueCount: u32,
    pub timestampValidBits: u32,
    pub minImageTransferGranularity: VkExtent3D,
}

/// Physical device features (compute-relevant only)
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkPhysicalDeviceFeatures {
    pub robustBufferAccess: VkBool32,
    pub shaderFloat64: VkBool32,
    pub shaderInt64: VkBool32,
    pub shaderInt16: VkBool32,
    pub shaderStorageBufferArrayDynamicIndexing: VkBool32,
    pub shaderStorageImageArrayDynamicIndexing: VkBool32,
    pub shaderStorageImageReadWithoutFormat: VkBool32,
    pub shaderStorageImageWriteWithoutFormat: VkBool32,
}

impl Default for VkPhysicalDeviceFeatures {
    fn default() -> Self {
        Self {
            robustBufferAccess: VK_FALSE,
            shaderFloat64: VK_FALSE,
            shaderInt64: VK_FALSE,
            shaderInt16: VK_FALSE,
            shaderStorageBufferArrayDynamicIndexing: VK_FALSE,
            shaderStorageImageArrayDynamicIndexing: VK_FALSE,
            shaderStorageImageReadWithoutFormat: VK_FALSE,
            shaderStorageImageWriteWithoutFormat: VK_FALSE,
        }
    }
}

/// Device queue creation info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkDeviceQueueCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkDeviceQueueCreateFlags,
    pub queueFamilyIndex: u32,
    pub queueCount: u32,
    pub pQueuePriorities: *const f32,
}

impl Default for VkDeviceQueueCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::DeviceQueueCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            queueFamilyIndex: 0,
            queueCount: 0,
            pQueuePriorities: ptr::null(),
        }
    }
}

/// Device creation info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkDeviceCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkDeviceCreateFlags,
    pub queueCreateInfoCount: u32,
    pub pQueueCreateInfos: *const VkDeviceQueueCreateInfo,
    pub enabledLayerCount: u32,
    pub ppEnabledLayerNames: *const PtrCStr,
    pub enabledExtensionCount: u32,
    pub ppEnabledExtensionNames: *const PtrCStr,
    pub pEnabledFeatures: *const VkPhysicalDeviceFeatures,
}

impl Default for VkDeviceCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::DeviceCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            queueCreateInfoCount: 0,
            pQueueCreateInfos: ptr::null(),
            enabledLayerCount: 0,
            ppEnabledLayerNames: ptr::null(),
            enabledExtensionCount: 0,
            ppEnabledExtensionNames: ptr::null(),
            pEnabledFeatures: ptr::null(),
        }
    }
}

/// Memory type
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkMemoryType {
    pub propertyFlags: VkMemoryPropertyFlags,
    pub heapIndex: u32,
}

/// Memory heap
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkMemoryHeap {
    pub size: VkDeviceSize,
    pub flags: VkFlags,
}

/// Physical device memory properties
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkPhysicalDeviceMemoryProperties {
    pub memoryTypeCount: u32,
    pub memoryTypes: [VkMemoryType; VK_MAX_MEMORY_TYPES],
    pub memoryHeapCount: u32,
    pub memoryHeaps: [VkMemoryHeap; VK_MAX_MEMORY_HEAPS],
}

impl Default for VkPhysicalDeviceMemoryProperties {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

/// Memory type cache for O(1) lookups
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct VkMemoryTypeCache {
    pub hostVisibleCoherent: u32,
    pub deviceLocal: u32,
    pub hostVisibleCached: u32,
    pub deviceLocalLazy: u32,
}

/// Memory allocate info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkMemoryAllocateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub allocationSize: VkDeviceSize,
    pub memoryTypeIndex: u32,
}

impl Default for VkMemoryAllocateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::MemoryAllocateInfo,
            pNext: ptr::null(),
            allocationSize: 0,
            memoryTypeIndex: 0,
        }
    }
}

/// Memory requirements
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkMemoryRequirements {
    pub size: VkDeviceSize,
    pub alignment: VkDeviceSize,
    pub memoryTypeBits: u32,
}

impl Default for VkMemoryRequirements {
    fn default() -> Self {
        Self {
            size: 0,
            alignment: 0,
            memoryTypeBits: 0,
        }
    }
}

/// Fence creation info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkFenceCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkFenceCreateFlags,
}

impl Default for VkFenceCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::FenceCreateInfo,
            pNext: ptr::null(),
            flags: VkFenceCreateFlags::empty(),
        }
    }
}

/// Semaphore creation info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkSemaphoreCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkFlags,
}

impl Default for VkSemaphoreCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::SemaphoreCreateInfo,
            pNext: ptr::null(),
            flags: 0,
        }
    }
}

/// Event creation info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkEventCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkFlags,
}

impl Default for VkEventCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::EventCreateInfo,
            pNext: ptr::null(),
            flags: 0,
        }
    }
}

/// Buffer creation info (optimized packing)
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkBufferCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub size: VkDeviceSize,
    pub usage: VkBufferUsageFlags,
    pub sharingMode: VkSharingMode,
    pub queueFamilyIndexCount: u32,
    pub pQueueFamilyIndices: *const u32,
    pub flags: VkBufferCreateFlags,
}

impl Default for VkBufferCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::BufferCreateInfo,
            pNext: ptr::null(),
            size: 0,
            usage: VkBufferUsageFlags::empty(),
            sharingMode: VkSharingMode::Exclusive,
            queueFamilyIndexCount: 0,
            pQueueFamilyIndices: ptr::null(),
            flags: VkBufferCreateFlags::empty(),
        }
    }
}

/// Command pool creation info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkCommandPoolCreateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkCommandPoolCreateFlags,
    pub queueFamilyIndex: u32,
}

impl Default for VkCommandPoolCreateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::CommandPoolCreateInfo,
            pNext: ptr::null(),
            flags: VkCommandPoolCreateFlags::empty(),
            queueFamilyIndex: 0,
        }
    }
}

/// Command buffer allocate info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkCommandBufferAllocateInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub commandPool: VkCommandPool,
    pub level: VkCommandBufferLevel,
    pub commandBufferCount: u32,
}

impl Default for VkCommandBufferAllocateInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::CommandBufferAllocateInfo,
            pNext: ptr::null(),
            commandPool: VkCommandPool::NULL,
            level: VkCommandBufferLevel::Primary,
            commandBufferCount: 0,
        }
    }
}

/// Command buffer begin info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkCommandBufferBeginInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub flags: VkCommandBufferUsageFlags,
    pub pInheritanceInfo: *const c_void,
}

impl Default for VkCommandBufferBeginInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::CommandBufferBeginInfo,
            pNext: ptr::null(),
            flags: VkCommandBufferUsageFlags::empty(),
            pInheritanceInfo: ptr::null(),
        }
    }
}

/// Memory barrier
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkMemoryBarrier {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub srcAccessMask: VkAccessFlags,
    pub dstAccessMask: VkAccessFlags,
}

impl Default for VkMemoryBarrier {
    fn default() -> Self {
        Self {
            sType: VkStructureType::MemoryBarrier,
            pNext: ptr::null(),
            srcAccessMask: VkAccessFlags::empty(),
            dstAccessMask: VkAccessFlags::empty(),
        }
    }
}

/// Buffer memory barrier
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkBufferMemoryBarrier {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub srcAccessMask: VkAccessFlags,
    pub dstAccessMask: VkAccessFlags,
    pub srcQueueFamilyIndex: u32,
    pub dstQueueFamilyIndex: u32,
    pub buffer: VkBuffer,
    pub offset: VkDeviceSize,
    pub size: VkDeviceSize,
}

impl Default for VkBufferMemoryBarrier {
    fn default() -> Self {
        Self {
            sType: VkStructureType::BufferMemoryBarrier,
            pNext: ptr::null(),
            srcAccessMask: VkAccessFlags::empty(),
            dstAccessMask: VkAccessFlags::empty(),
            srcQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
            dstQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
            buffer: VkBuffer::NULL,
            offset: 0,
            size: VK_WHOLE_SIZE,
        }
    }
}

/// Submit info
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkSubmitInfo {
    pub sType: VkStructureType,
    pub pNext: *const c_void,
    pub waitSemaphoreCount: u32,
    pub pWaitSemaphores: *const VkSemaphore,
    pub pWaitDstStageMask: *const VkPipelineStageFlags,
    pub commandBufferCount: u32,
    pub pCommandBuffers: *const VkCommandBuffer,
    pub signalSemaphoreCount: u32,
    pub pSignalSemaphores: *const VkSemaphore,
}

/// Buffer copy region
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkBufferCopy {
    pub srcOffset: VkDeviceSize,
    pub dstOffset: VkDeviceSize,
    pub size: VkDeviceSize,
}

impl Default for VkSubmitInfo {
    fn default() -> Self {
        Self {
            sType: VkStructureType::SubmitInfo,
            pNext: ptr::null(),
            waitSemaphoreCount: 0,
            pWaitSemaphores: ptr::null(),
            pWaitDstStageMask: ptr::null(),
            commandBufferCount: 0,
            pCommandBuffers: ptr::null(),
            signalSemaphoreCount: 0,
            pSignalSemaphores: ptr::null(),
        }
    }
}

/// Physical device limits (simplified for compute)
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkPhysicalDeviceLimits {
    pub maxComputeSharedMemorySize: u32,
    pub maxComputeWorkGroupCount: [u32; 3],
    pub maxComputeWorkGroupInvocations: u32,
    pub maxComputeWorkGroupSize: [u32; 3],
    // Add more fields as needed...
}

impl Default for VkPhysicalDeviceLimits {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

/// Physical device sparse properties
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkPhysicalDeviceSparseProperties {
    pub residencyStandard2DBlockShape: VkBool32,
    pub residencyStandard2DMultisampleBlockShape: VkBool32,
    pub residencyStandard3DBlockShape: VkBool32,
    pub residencyAlignedMipSize: VkBool32,
    pub residencyNonResidentStrict: VkBool32,
}

impl Default for VkPhysicalDeviceSparseProperties {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

/// Physical device properties
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct VkPhysicalDeviceProperties {
    pub apiVersion: u32,
    pub driverVersion: u32,
    pub vendorID: u32,
    pub deviceID: u32,
    pub deviceType: VkPhysicalDeviceType,
    pub deviceName: [c_char; VK_MAX_PHYSICAL_DEVICE_NAME_SIZE],
    pub pipelineCacheUUID: [u8; VK_UUID_SIZE],
    pub limits: VkPhysicalDeviceLimits,
    pub sparseProperties: VkPhysicalDeviceSparseProperties,
}

impl Default for VkPhysicalDeviceProperties {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_extent3d_default() {
        let extent = VkExtent3D::default();
        assert_eq!(extent.width, 1);
        assert_eq!(extent.height, 1);
        assert_eq!(extent.depth, 1);
    }
    
    #[test]
    fn test_application_info_default() {
        let info = VkApplicationInfo::default();
        assert_eq!(info.sType, VkStructureType::ApplicationInfo);
        assert!(info.pNext.is_null());
        assert!(info.pApplicationName.is_null());
        assert_eq!(info.applicationVersion, 0);
        assert_eq!(info.apiVersion, VK_API_VERSION_1_0);
    }
    
    #[test]
    fn test_instance_create_info_default() {
        let info = VkInstanceCreateInfo::default();
        assert_eq!(info.sType, VkStructureType::InstanceCreateInfo);
        assert!(info.pNext.is_null());
        assert_eq!(info.flags, 0);
        assert_eq!(info.enabledLayerCount, 0);
        assert_eq!(info.enabledExtensionCount, 0);
    }
    
    #[test]
    fn test_buffer_create_info_default() {
        let info = VkBufferCreateInfo::default();
        assert_eq!(info.sType, VkStructureType::BufferCreateInfo);
        assert!(info.pNext.is_null());
        assert_eq!(info.size, 0);
        assert!(info.usage.is_empty());
        assert_eq!(info.sharingMode, VkSharingMode::Exclusive);
        assert!(info.flags.is_empty());
    }
    
    #[test]
    fn test_memory_allocate_info_default() {
        let info = VkMemoryAllocateInfo::default();
        assert_eq!(info.sType, VkStructureType::MemoryAllocateInfo);
        assert!(info.pNext.is_null());
        assert_eq!(info.allocationSize, 0);
        assert_eq!(info.memoryTypeIndex, 0);
    }
    
    #[test]
    fn test_fence_create_info_default() {
        let info = VkFenceCreateInfo::default();
        assert_eq!(info.sType, VkStructureType::FenceCreateInfo);
        assert!(info.pNext.is_null());
        assert!(info.flags.is_empty());
    }
    
    #[test]
    fn test_submit_info_default() {
        let info = VkSubmitInfo::default();
        assert_eq!(info.sType, VkStructureType::SubmitInfo);
        assert!(info.pNext.is_null());
        assert_eq!(info.waitSemaphoreCount, 0);
        assert_eq!(info.commandBufferCount, 0);
        assert_eq!(info.signalSemaphoreCount, 0);
    }
    
    #[test]
    fn test_command_buffer_begin_info_default() {
        let info = VkCommandBufferBeginInfo::default();
        assert_eq!(info.sType, VkStructureType::CommandBufferBeginInfo);
        assert!(info.pNext.is_null());
        assert!(info.flags.is_empty());
        assert!(info.pInheritanceInfo.is_null());
    }
    
    #[test]
    fn test_memory_barrier_default() {
        let barrier = VkMemoryBarrier::default();
        assert_eq!(barrier.sType, VkStructureType::MemoryBarrier);
        assert!(barrier.pNext.is_null());
        assert!(barrier.srcAccessMask.is_empty());
        assert!(barrier.dstAccessMask.is_empty());
    }
    
    #[test]
    fn test_buffer_memory_barrier_default() {
        let barrier = VkBufferMemoryBarrier::default();
        assert_eq!(barrier.sType, VkStructureType::BufferMemoryBarrier);
        assert!(barrier.pNext.is_null());
        assert_eq!(barrier.srcQueueFamilyIndex, VK_QUEUE_FAMILY_IGNORED);
        assert_eq!(barrier.dstQueueFamilyIndex, VK_QUEUE_FAMILY_IGNORED);
        assert_eq!(barrier.size, VK_WHOLE_SIZE);
    }
}