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
//! Test descriptor set functionality

use kronos::*;
use kronos::ffi::*;
use std::ffi::CString;
use std::ptr;

// Import the implementation functions
extern "C" {
    fn vkCreateInstance(
        pCreateInfo: *const VkInstanceCreateInfo,
        pAllocator: *const VkAllocationCallbacks,
        pInstance: *mut VkInstance,
    ) -> VkResult;
    
    fn vkDestroyInstance(
        instance: VkInstance,
        pAllocator: *const VkAllocationCallbacks,
    );
    
    fn vkEnumeratePhysicalDevices(
        instance: VkInstance,
        pPhysicalDeviceCount: *mut u32,
        pPhysicalDevices: *mut VkPhysicalDevice,
    ) -> VkResult;
    
    fn vkCreateDevice(
        physicalDevice: VkPhysicalDevice,
        pCreateInfo: *const VkDeviceCreateInfo,
        pAllocator: *const VkAllocationCallbacks,
        pDevice: *mut VkDevice,
    ) -> VkResult;
    
    fn vkDestroyDevice(
        device: VkDevice,
        pAllocator: *const VkAllocationCallbacks,
    );
    
    fn vkCreateBuffer(
        device: VkDevice,
        pCreateInfo: *const VkBufferCreateInfo,
        pAllocator: *const VkAllocationCallbacks,
        pBuffer: *mut VkBuffer,
    ) -> VkResult;
    
    fn vkDestroyBuffer(
        device: VkDevice,
        buffer: VkBuffer,
        pAllocator: *const VkAllocationCallbacks,
    );
    
    fn vkCreateDescriptorSetLayout(
        device: VkDevice,
        pCreateInfo: *const VkDescriptorSetLayoutCreateInfo,
        pAllocator: *const VkAllocationCallbacks,
        pSetLayout: *mut VkDescriptorSetLayout,
    ) -> VkResult;
    
    fn vkDestroyDescriptorSetLayout(
        device: VkDevice,
        descriptorSetLayout: VkDescriptorSetLayout,
        pAllocator: *const VkAllocationCallbacks,
    );
    
    fn vkCreateDescriptorPool(
        device: VkDevice,
        pCreateInfo: *const VkDescriptorPoolCreateInfo,
        pAllocator: *const VkAllocationCallbacks,
        pDescriptorPool: *mut VkDescriptorPool,
    ) -> VkResult;
    
    fn vkDestroyDescriptorPool(
        device: VkDevice,
        descriptorPool: VkDescriptorPool,
        pAllocator: *const VkAllocationCallbacks,
    );
    
    fn vkAllocateDescriptorSets(
        device: VkDevice,
        pAllocateInfo: *const VkDescriptorSetAllocateInfo,
        pDescriptorSets: *mut VkDescriptorSet,
    ) -> VkResult;
    
    fn vkUpdateDescriptorSets(
        device: VkDevice,
        descriptorWriteCount: u32,
        pDescriptorWrites: *const VkWriteDescriptorSet,
        descriptorCopyCount: u32,
        pDescriptorCopies: *const VkCopyDescriptorSet,
    );
}

fn main() {
    println!("Kronos Descriptor Test");
    println!("======================\n");
    
    unsafe {
        // 1. Create instance
        println!("Creating instance...");
        let app_name = CString::new("Kronos Descriptor Test").unwrap();
        
        let app_info = VkApplicationInfo {
            sType: VkStructureType::ApplicationInfo,
            pNext: ptr::null(),
            pApplicationName: app_name.as_ptr(),
            applicationVersion: VK_MAKE_VERSION(1, 0, 0),
            pEngineName: ptr::null(),
            engineVersion: VK_MAKE_VERSION(1, 0, 0),
            apiVersion: VK_API_VERSION_1_0,
        };
        
        let create_info = VkInstanceCreateInfo {
            sType: VkStructureType::InstanceCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            pApplicationInfo: &app_info,
            enabledLayerCount: 0,
            ppEnabledLayerNames: ptr::null(),
            enabledExtensionCount: 0,
            ppEnabledExtensionNames: ptr::null(),
        };
        
        let mut instance = VkInstance::NULL;
        let result = vkCreateInstance(&create_info, ptr::null(), &mut instance);
        
        if result != VkResult::Success {
            println!("✗ Failed to create instance: {:?}", result);
            return;
        }
        println!("✓ Instance created");
        
        // 2. Get physical device
        let mut device_count = 1;
        let mut physical_device = VkPhysicalDevice::NULL;
        vkEnumeratePhysicalDevices(instance, &mut device_count, &mut physical_device);
        println!("✓ Got physical device");
        
        // 3. Create logical device
        let queue_priorities = [1.0f32];
        let queue_create_info = VkDeviceQueueCreateInfo {
            sType: VkStructureType::DeviceQueueCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            queueFamilyIndex: 0,
            queueCount: 1,
            pQueuePriorities: queue_priorities.as_ptr(),
        };
        
        let device_create_info = VkDeviceCreateInfo {
            sType: VkStructureType::DeviceCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            queueCreateInfoCount: 1,
            pQueueCreateInfos: &queue_create_info,
            enabledLayerCount: 0,
            ppEnabledLayerNames: ptr::null(),
            enabledExtensionCount: 0,
            ppEnabledExtensionNames: ptr::null(),
            pEnabledFeatures: ptr::null(),
        };
        
        let mut device = VkDevice::NULL;
        let result = vkCreateDevice(physical_device, &device_create_info, ptr::null(), &mut device);
        
        if result != VkResult::Success {
            println!("✗ Failed to create device: {:?}", result);
            vkDestroyInstance(instance, ptr::null());
            return;
        }
        println!("✓ Device created");
        
        // 4. Create descriptor set layout
        println!("\nCreating descriptor set layout...");
        
        // Define bindings for compute shader
        let bindings = [
            VkDescriptorSetLayoutBinding {
                binding: 0,
                descriptorType: VkDescriptorType::StorageBuffer,
                descriptorCount: 1,
                stageFlags: VkShaderStageFlags::COMPUTE,
                pImmutableSamplers: ptr::null(),
            },
            VkDescriptorSetLayoutBinding {
                binding: 1,
                descriptorType: VkDescriptorType::StorageBuffer,
                descriptorCount: 1,
                stageFlags: VkShaderStageFlags::COMPUTE,
                pImmutableSamplers: ptr::null(),
            },
            VkDescriptorSetLayoutBinding {
                binding: 2,
                descriptorType: VkDescriptorType::UniformBuffer,
                descriptorCount: 1,
                stageFlags: VkShaderStageFlags::COMPUTE,
                pImmutableSamplers: ptr::null(),
            },
        ];
        
        let layout_info = VkDescriptorSetLayoutCreateInfo {
            sType: VkStructureType::DescriptorSetLayoutCreateInfo,
            pNext: ptr::null(),
            flags: 0,
            bindingCount: bindings.len() as u32,
            pBindings: bindings.as_ptr(),
        };
        
        let mut set_layout = VkDescriptorSetLayout::NULL;
        let result = vkCreateDescriptorSetLayout(device, &layout_info, ptr::null(), &mut set_layout);
        
        if result != VkResult::Success {
            println!("✗ Failed to create descriptor set layout: {:?}", result);
        } else {
            println!("✓ Descriptor set layout created");
        }
        
        // 5. Create descriptor pool
        println!("\nCreating descriptor pool...");
        
        let pool_sizes = [
            VkDescriptorPoolSize {
                type_: VkDescriptorType::StorageBuffer,
                descriptorCount: 10,
            },
            VkDescriptorPoolSize {
                type_: VkDescriptorType::UniformBuffer,
                descriptorCount: 5,
            },
        ];
        
        let pool_info = VkDescriptorPoolCreateInfo {
            sType: VkStructureType::DescriptorPoolCreateInfo,
            pNext: ptr::null(),
            flags: VkDescriptorPoolCreateFlags::empty(),
            maxSets: 5,
            poolSizeCount: pool_sizes.len() as u32,
            pPoolSizes: pool_sizes.as_ptr(),
        };
        
        let mut descriptor_pool = VkDescriptorPool::NULL;
        let result = vkCreateDescriptorPool(device, &pool_info, ptr::null(), &mut descriptor_pool);
        
        if result != VkResult::Success {
            println!("✗ Failed to create descriptor pool: {:?}", result);
        } else {
            println!("✓ Descriptor pool created");
        }
        
        // 6. Allocate descriptor sets
        println!("\nAllocating descriptor sets...");
        
        let alloc_info = VkDescriptorSetAllocateInfo {
            sType: VkStructureType::DescriptorSetAllocateInfo,
            pNext: ptr::null(),
            descriptorPool: descriptor_pool,
            descriptorSetCount: 1,
            pSetLayouts: &set_layout,
        };
        
        let mut descriptor_set = VkDescriptorSet::NULL;
        let result = vkAllocateDescriptorSets(device, &alloc_info, &mut descriptor_set);
        
        if result != VkResult::Success {
            println!("✗ Failed to allocate descriptor sets: {:?}", result);
        } else {
            println!("✓ Descriptor set allocated");
        }
        
        // 7. Create buffers to bind
        println!("\nCreating buffers...");
        
        let buffer_size = 1024 * 1024; // 1MB
        let mut buffers = vec![VkBuffer::NULL; 3];
        
        for (i, buffer) in buffers.iter_mut().enumerate() {
            let usage = if i < 2 {
                VkBufferUsageFlags::STORAGE_BUFFER
            } else {
                VkBufferUsageFlags::UNIFORM_BUFFER
            };
            
            let buffer_info = VkBufferCreateInfo {
                sType: VkStructureType::BufferCreateInfo,
                pNext: ptr::null(),
                flags: VkBufferCreateFlags::empty(),
                size: buffer_size,
                usage,
                sharingMode: VkSharingMode::Exclusive,
                queueFamilyIndexCount: 0,
                pQueueFamilyIndices: ptr::null(),
            };
            
            let result = vkCreateBuffer(device, &buffer_info, ptr::null(), buffer);
            if result != VkResult::Success {
                println!("✗ Failed to create buffer {}: {:?}", i, result);
            }
        }
        println!("✓ Created {} buffers", buffers.len());
        
        // 8. Update descriptor sets
        println!("\nUpdating descriptor sets...");
        
        let buffer_infos = [
            VkDescriptorBufferInfo {
                buffer: buffers[0],
                offset: 0,
                range: buffer_size,
            },
            VkDescriptorBufferInfo {
                buffer: buffers[1],
                offset: 0,
                range: buffer_size,
            },
            VkDescriptorBufferInfo {
                buffer: buffers[2],
                offset: 0,
                range: 256, // Smaller for uniform buffer
            },
        ];
        
        let writes = [
            VkWriteDescriptorSet {
                sType: VkStructureType::WriteDescriptorSet,
                pNext: ptr::null(),
                dstSet: descriptor_set,
                dstBinding: 0,
                dstArrayElement: 0,
                descriptorCount: 1,
                descriptorType: VkDescriptorType::StorageBuffer,
                pImageInfo: ptr::null(),
                pBufferInfo: &buffer_infos[0],
                pTexelBufferView: ptr::null(),
            },
            VkWriteDescriptorSet {
                sType: VkStructureType::WriteDescriptorSet,
                pNext: ptr::null(),
                dstSet: descriptor_set,
                dstBinding: 1,
                dstArrayElement: 0,
                descriptorCount: 1,
                descriptorType: VkDescriptorType::StorageBuffer,
                pImageInfo: ptr::null(),
                pBufferInfo: &buffer_infos[1],
                pTexelBufferView: ptr::null(),
            },
            VkWriteDescriptorSet {
                sType: VkStructureType::WriteDescriptorSet,
                pNext: ptr::null(),
                dstSet: descriptor_set,
                dstBinding: 2,
                dstArrayElement: 0,
                descriptorCount: 1,
                descriptorType: VkDescriptorType::UniformBuffer,
                pImageInfo: ptr::null(),
                pBufferInfo: &buffer_infos[2],
                pTexelBufferView: ptr::null(),
            },
        ];
        
        vkUpdateDescriptorSets(device, writes.len() as u32, writes.as_ptr(), 0, ptr::null());
        println!("✓ Descriptor sets updated");
        
        // 9. Test descriptor copying
        println!("\nTesting descriptor copying...");
        
        // Allocate another descriptor set
        let mut descriptor_set2 = VkDescriptorSet::NULL;
        let result = vkAllocateDescriptorSets(device, &alloc_info, &mut descriptor_set2);
        
        if result == VkResult::Success {
            // Copy descriptors from first set to second
            let copy = VkCopyDescriptorSet {
                sType: VkStructureType::CopyDescriptorSet,
                pNext: ptr::null(),
                srcSet: descriptor_set,
                srcBinding: 0,
                srcArrayElement: 0,
                dstSet: descriptor_set2,
                dstBinding: 0,
                dstArrayElement: 0,
                descriptorCount: 1,
            };
            
            vkUpdateDescriptorSets(device, 0, ptr::null(), 1, &copy);
            println!("✓ Descriptor copied successfully");
        }
        
        // Cleanup
        println!("\nCleaning up...");
        for buffer in &buffers {
            vkDestroyBuffer(device, *buffer, ptr::null());
        }
        vkDestroyDescriptorPool(device, descriptor_pool, ptr::null());
        vkDestroyDescriptorSetLayout(device, set_layout, ptr::null());
        vkDestroyDevice(device, ptr::null());
        vkDestroyInstance(instance, ptr::null());
        
        println!("✓ Cleanup complete");
        println!("\n✓ All descriptor tests passed!");
    }
}

// Version macros
const fn VK_MAKE_VERSION(major: u32, minor: u32, patch: u32) -> u32 {
    (major << 22) | (minor << 12) | patch
}