vulkane 0.1.0

Vulkan API bindings generated entirely from vk.xml, with a complete safe RAII wrapper covering compute and graphics: instance/device/queue, buffer, image, sampler, render pass, framebuffer, graphics + compute pipelines, swapchain, a VMA-style sub-allocator with TLSF + linear pools and defragmentation, sync primitives (fences, binary + timeline semaphores, sync2 barriers), query pools, and an optional naga GLSL/WGSL→SPIR-V feature. Supports Vulkan 1.2.175 onward — swap vk.xml and rebuild.
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
//! Integration tests for the vulkane Vulkan bindings
//!
//! These tests verify that the generated bindings from vk.xml are correct
//! and complete. They run against the actual generated code included via
//! build.rs.

use crate::raw::bindings::*;

// ============================================================================
// Core type existence tests — verify key Vulkan types were generated
// ============================================================================

#[test]
fn test_handle_types_exist() {
    // Dispatchable handles should be pointer-sized
    let _: VkInstance = std::ptr::null_mut();
    let _: VkDevice = std::ptr::null_mut();
    let _: VkPhysicalDevice = std::ptr::null_mut();
    let _: VkQueue = std::ptr::null_mut();
    let _: VkCommandBuffer = std::ptr::null_mut();
}

#[test]
fn test_non_dispatchable_handle_types_exist() {
    // Non-dispatchable handles should be u64
    let _: VkSemaphore = 0u64;
    let _: VkFence = 0u64;
    let _: VkBuffer = 0u64;
    let _: VkImage = 0u64;
    let _: VkPipeline = 0u64;
    let _: VkSampler = 0u64;
    let _: VkDescriptorSet = 0u64;
    let _: VkFramebuffer = 0u64;
    let _: VkRenderPass = 0u64;
    let _: VkCommandPool = 0u64;
    let _: VkSwapchainKHR = 0u64;
    let _: VkSurfaceKHR = 0u64;
}

#[test]
fn test_base_types_exist() {
    let _: VkBool32 = 0u32;
    let _: VkFlags = 0u32;
    let _: VkFlags64 = 0u64;
    let _: VkDeviceSize = 0u64;
    let _: VkDeviceAddress = 0u64;
    let _: VkSampleMask = 0u32;
}

// ============================================================================
// Struct tests — verify key structs have correct fields
// ============================================================================

#[test]
fn test_vk_application_info_struct() {
    let info = VkApplicationInfo {
        sType: VkStructureType::STRUCTURE_TYPE_APPLICATION_INFO,
        pNext: std::ptr::null(),
        pApplicationName: std::ptr::null(),
        applicationVersion: 0,
        pEngineName: std::ptr::null(),
        engineVersion: 0,
        apiVersion: VK_API_VERSION_1_0,
    };
    assert_eq!(info.apiVersion, VK_API_VERSION_1_0);
}

#[test]
fn test_vk_instance_create_info_struct() {
    let info = VkInstanceCreateInfo {
        sType: VkStructureType::STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
        pNext: std::ptr::null(),
        flags: 0,
        pApplicationInfo: std::ptr::null(),
        enabledLayerCount: 0,
        ppEnabledLayerNames: std::ptr::null(),
        enabledExtensionCount: 0,
        ppEnabledExtensionNames: std::ptr::null(),
    };
    assert_eq!(info.enabledLayerCount, 0);
}

#[test]
fn test_struct_default_impls() {
    let app_info = VkApplicationInfo::default();
    assert!(app_info.pNext.is_null());
    assert!(app_info.pApplicationName.is_null());

    let create_info = VkInstanceCreateInfo::default();
    assert!(create_info.pNext.is_null());
    assert_eq!(create_info.enabledLayerCount, 0);
}

// ============================================================================
// Enum tests — verify enums have correct variants including extensions
// ============================================================================

#[test]
fn test_vk_result_enum() {
    // Core values
    assert_eq!(VkResult::SUCCESS as i32, 0);
    assert_eq!(VkResult::NOT_READY as i32, 1);
    assert_eq!(VkResult::TIMEOUT as i32, 2);
    assert_eq!(VkResult::ERROR_OUT_OF_HOST_MEMORY as i32, -1);
    assert_eq!(VkResult::ERROR_OUT_OF_DEVICE_MEMORY as i32, -2);
    assert_eq!(VkResult::ERROR_DEVICE_LOST as i32, -4);
}

#[test]
fn test_vk_structure_type_has_extension_values() {
    // Core value
    assert_eq!(VkStructureType::STRUCTURE_TYPE_APPLICATION_INFO as i32, 0);
    // Extension value (VK_KHR_swapchain, extnumber=2, offset=0)
    // Value = 1000000000 + (2-1)*1000 + 0 = 1000001000
    assert_eq!(
        VkStructureType::STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR as i32,
        1000001000
    );
}

// ============================================================================
// Version macro tests — verify functions derived from vk.xml work correctly
// ============================================================================

#[test]
fn test_vk_make_api_version() {
    let version = vk_make_api_version(0, 1, 0, 0);
    assert_eq!(version, VK_API_VERSION_1_0);
}

#[test]
fn test_vk_api_version_extraction() {
    // Use VK_API_VERSION_1_0 which exists in all supported Vulkan versions
    let version = VK_API_VERSION_1_0;
    assert_eq!(vk_api_version_variant(version), 0);
    assert_eq!(vk_api_version_major(version), 1);
    assert_eq!(vk_api_version_minor(version), 0);
    assert_eq!(vk_api_version_patch(version), 0);
}

#[test]
fn test_vk_header_version_exists() {
    // VK_HEADER_VERSION should be parsed from vk.xml, not hardcoded
    const _: () = assert!(VK_HEADER_VERSION > 0);
}

#[test]
fn test_version_roundtrip() {
    // Encoding then decoding should produce the same values
    let v = vk_make_api_version(0, 1, 4, 42);
    assert_eq!(vk_api_version_variant(v), 0);
    assert_eq!(vk_api_version_major(v), 1);
    assert_eq!(vk_api_version_minor(v), 4);
    assert_eq!(vk_api_version_patch(v), 42);
}

// ============================================================================
// Constants tests
// ============================================================================

#[test]
fn test_constants_exist() {
    const _: () = assert!(VK_MAX_PHYSICAL_DEVICE_NAME_SIZE > 0);
    const _: () = assert!(VK_MAX_EXTENSION_NAME_SIZE > 0);
    const _: () = assert!(VK_UUID_SIZE > 0);
    const _: () = assert!(VK_MAX_MEMORY_TYPES > 0);
    const _: () = assert!(VK_MAX_MEMORY_HEAPS > 0);
}

// ============================================================================
// Function pointer typedef tests
// ============================================================================

#[test]
fn test_function_pointer_types_exist() {
    // These are type aliases for unsafe extern "system" fn(...)
    // We can't instantiate them but we can verify they're valid types
    let _: Option<vkCreateInstance> = None;
    let _: Option<vkDestroyInstance> = None;
    let _: Option<vkEnumeratePhysicalDevices> = None;
    let _: Option<vkCreateDevice> = None;
    let _: Option<vkDestroyDevice> = None;
    let _: Option<vkGetDeviceProcAddr> = None;
    let _: Option<vkGetInstanceProcAddr> = None;
}

#[test]
fn test_function_pointer_signatures_match_spec() {
    // Verify that generated function pointer typedefs have the exact
    // signatures specified in the Vulkan API. We do this by assigning
    // stub functions with the expected signatures to the typedefs.
    // This catches regressions in parameter ordering, parameter types,
    // calling convention, and return type.

    // vkCreateInstance: VkResult vkCreateInstance(
    //     const VkInstanceCreateInfo*, const VkAllocationCallbacks*, VkInstance*)
    unsafe extern "system" fn create_instance_stub(
        _create_info: *const VkInstanceCreateInfo,
        _allocator: *const VkAllocationCallbacks,
        _instance: *mut VkInstance,
    ) -> VkResult {
        VkResult::SUCCESS
    }
    let _f: vkCreateInstance = create_instance_stub;

    // vkDestroyInstance: void vkDestroyInstance(VkInstance, const VkAllocationCallbacks*)
    unsafe extern "system" fn destroy_instance_stub(
        _instance: VkInstance,
        _allocator: *const VkAllocationCallbacks,
    ) {
    }
    let _f: vkDestroyInstance = destroy_instance_stub;

    // vkEnumeratePhysicalDevices
    unsafe extern "system" fn enum_phys_stub(
        _instance: VkInstance,
        _count: *mut u32,
        _devices: *mut VkPhysicalDevice,
    ) -> VkResult {
        VkResult::SUCCESS
    }
    let _f: vkEnumeratePhysicalDevices = enum_phys_stub;

    // vkEnumerateInstanceVersion: VkResult vkEnumerateInstanceVersion(uint32_t*)
    unsafe extern "system" fn enum_version_stub(_version: *mut u32) -> VkResult {
        VkResult::SUCCESS
    }
    let _f: vkEnumerateInstanceVersion = enum_version_stub;
}

#[test]
fn test_dispatch_tables_have_required_fields() {
    // VkEntryDispatchTable must contain the global Vulkan functions
    let entry: VkEntryDispatchTable = unsafe { std::mem::zeroed() };
    let _ = entry.vkCreateInstance;
    let _ = entry.vkEnumerateInstanceLayerProperties;
    let _ = entry.vkEnumerateInstanceExtensionProperties;
    let _ = entry.vkEnumerateInstanceVersion;

    // VkInstanceDispatchTable must contain instance-level functions
    let inst: VkInstanceDispatchTable = unsafe { std::mem::zeroed() };
    let _ = inst.vkDestroyInstance;
    let _ = inst.vkEnumeratePhysicalDevices;
    let _ = inst.vkGetPhysicalDeviceProperties;
    let _ = inst.vkGetPhysicalDeviceMemoryProperties;
    let _ = inst.vkGetPhysicalDeviceQueueFamilyProperties;
    let _ = inst.vkCreateDevice;

    // VkDeviceDispatchTable must contain device-level functions
    let dev: VkDeviceDispatchTable = unsafe { std::mem::zeroed() };
    let _ = dev.vkDestroyDevice;
    let _ = dev.vkGetDeviceQueue;
    let _ = dev.vkCreateBuffer;
    let _ = dev.vkAllocateMemory;
    let _ = dev.vkCreateCommandPool;
}

// ============================================================================
// Dispatch table tests
// ============================================================================

#[test]
fn test_dispatch_tables_exist() {
    // Verify the generated dispatch table structs exist as types
    let _: Option<Box<VkEntryDispatchTable>> = None;
    let _: Option<Box<VkInstanceDispatchTable>> = None;
    let _: Option<Box<VkDeviceDispatchTable>> = None;
}

// ============================================================================
// Extension struct tests — verify extension-defined structs are generated
// ============================================================================

#[test]
fn test_extension_structs_exist() {
    // VK_KHR_swapchain
    let _info = VkSwapchainCreateInfoKHR::default();

    // VK_EXT_debug_utils
    let _info = VkDebugUtilsMessengerCreateInfoEXT::default();
}

// ============================================================================
// Union type tests
// ============================================================================

#[test]
fn test_clear_color_value_is_union() {
    // VkClearColorValue must be a union — all fields overlap in memory
    assert_eq!(
        std::mem::size_of::<VkClearColorValue>(),
        std::mem::size_of::<[f32; 4]>(),
        "VkClearColorValue should be the size of its largest field, not a sum"
    );

    let mut color = VkClearColorValue::default();
    unsafe {
        color.float32 = [1.0, 0.0, 0.0, 1.0];
        assert_eq!(color.float32[0], 1.0);
    }
}

#[test]
fn test_clear_value_is_union() {
    // VkClearValue contains VkClearColorValue (a union) — should itself be a union
    let val = VkClearValue::default();
    // Size should be max of its fields, not sum
    assert!(
        std::mem::size_of::<VkClearValue>() <= 16,
        "VkClearValue should be at most 16 bytes (4 x f32), got {}",
        std::mem::size_of::<VkClearValue>()
    );
    let _ = val;
}

#[test]
fn test_null_handle() {
    // VK_NULL_HANDLE should be 0, derived from vk.xml
    assert_eq!(VK_NULL_HANDLE, 0u64);
}

// ============================================================================
// Struct layout assertions
// ============================================================================

#[test]
fn test_vk_application_info_size_matches_spec() {
    // VkApplicationInfo per the Vulkan spec is:
    //   VkStructureType    sType;             // 4 bytes (i32)
    //   const void*        pNext;             // 8 bytes on 64-bit
    //   const char*        pApplicationName;  // 8
    //   uint32_t           applicationVersion;// 4
    //   const char*        pEngineName;       // 8
    //   uint32_t           engineVersion;     // 4
    //   uint32_t           apiVersion;        // 4
    // With C alignment, this is 48 bytes on 64-bit platforms.
    #[cfg(target_pointer_width = "64")]
    assert_eq!(std::mem::size_of::<VkApplicationInfo>(), 48);

    // Independent of pointer width, alignment must be at least 8
    // (it contains pointers).
    #[cfg(target_pointer_width = "64")]
    assert_eq!(std::mem::align_of::<VkApplicationInfo>(), 8);
}

#[test]
fn test_vk_extent_3d_layout() {
    // VkExtent3D is { uint32_t width, height, depth } — 12 bytes, aligned to 4
    assert_eq!(std::mem::size_of::<VkExtent3D>(), 12);
    assert_eq!(std::mem::align_of::<VkExtent3D>(), 4);

    let e = VkExtent3D {
        width: 1024,
        height: 768,
        depth: 1,
    };
    assert_eq!(e.width, 1024);
    assert_eq!(e.height, 768);
    assert_eq!(e.depth, 1);
}

#[test]
fn test_vk_offset_2d_layout() {
    // VkOffset2D is { int32_t x, y } — 8 bytes
    assert_eq!(std::mem::size_of::<VkOffset2D>(), 8);
    assert_eq!(std::mem::align_of::<VkOffset2D>(), 4);
}

#[test]
fn test_vk_rect_2d_layout() {
    // VkRect2D is { VkOffset2D offset; VkExtent2D extent; }
    // offset = 8 bytes, extent (uint32_t x 2) = 8 bytes, total 16
    assert_eq!(std::mem::size_of::<VkRect2D>(), 16);
}

// ============================================================================
// VkResult helper tests
// ============================================================================

#[test]
fn test_vk_result_into_result_success() {
    use crate::raw::VkResultExt;
    assert!(VkResult::SUCCESS.into_result().is_ok());
}

#[test]
fn test_vk_result_into_result_error() {
    use crate::raw::VkResultExt;
    let err = VkResult::ERROR_OUT_OF_HOST_MEMORY.into_result();
    assert!(err.is_err());
    assert_eq!(err.unwrap_err(), VkResult::ERROR_OUT_OF_HOST_MEMORY);
}

#[test]
fn test_vk_result_is_success_and_is_error() {
    use crate::raw::VkResultExt;
    assert!(VkResult::SUCCESS.is_success());
    assert!(!VkResult::SUCCESS.is_error());

    assert!(!VkResult::ERROR_DEVICE_LOST.is_success());
    assert!(VkResult::ERROR_DEVICE_LOST.is_error());

    // NOT_READY is positive (1), so it's not "success" but also not "error"
    assert!(!VkResult::NOT_READY.is_success());
    assert!(!VkResult::NOT_READY.is_error());
}

#[test]
fn test_vk_check_macro_success() {
    fn returns_success() -> VkResult {
        VkResult::SUCCESS
    }
    let result = crate::vk_check!(returns_success());
    assert!(result.is_ok());
}

#[test]
fn test_vk_check_macro_error() {
    fn returns_error() -> VkResult {
        VkResult::ERROR_OUT_OF_HOST_MEMORY
    }
    let result = crate::vk_check!(returns_error());
    assert_eq!(result.unwrap_err(), VkResult::ERROR_OUT_OF_HOST_MEMORY);
}

#[test]
fn test_vk_result_implements_error_trait() {
    // VkResult must implement std::error::Error so it works with `?`
    // in functions returning Box<dyn Error>.
    fn returns_box_error() -> Result<(), Box<dyn std::error::Error>> {
        use crate::raw::VkResultExt;
        VkResult::SUCCESS.into_result()?;
        Ok(())
    }
    assert!(returns_box_error().is_ok());

    fn propagates_error() -> Result<(), Box<dyn std::error::Error>> {
        use crate::raw::VkResultExt;
        VkResult::ERROR_DEVICE_LOST.into_result()?;
        Ok(())
    }
    assert!(propagates_error().is_err());
}

// ============================================================================
// Bitmask flag tests
// ============================================================================

#[test]
fn test_bitmask_flags_combinable() {
    // Bitmask flags should be emitted as pub const so they can be combined with |
    let flags: VkBufferUsageFlagBits =
        BUFFER_USAGE_TRANSFER_SRC_BIT | BUFFER_USAGE_TRANSFER_DST_BIT;
    assert!(flags != 0);
}

// ============================================================================
// Generator quality regression tests
// ============================================================================

/// Read the generated `vulkan_bindings.rs` and assert that the
/// generator emitted **zero** `// TODO:` placeholders. Each TODO line
/// indicates a Vulkan macro / type / member the generator gave up on
/// translating, which leaks into the public API as either silent
/// missing functionality or noisy commented-out C source.
#[test]
fn test_generated_bindings_have_zero_todo_lines() {
    let path = env!("SPOCK_GENERATED_BINDINGS");
    let content = std::fs::read_to_string(path)
        .expect("SPOCK_GENERATED_BINDINGS env var should point at the generated bindings file");
    let mut offending: Vec<(usize, &str)> = Vec::new();
    for (lineno, line) in content.lines().enumerate() {
        if line.contains("// TODO:") || line.contains("// FIXME:") {
            offending.push((lineno + 1, line.trim()));
        }
    }
    assert!(
        offending.is_empty(),
        "generated bindings should contain zero TODO/FIXME lines, but found {} \
         (this means the macro/type/member generator gave up on translating something \
         — fix the generator or add a special case rather than letting the TODO ship). \
         First 10 offending lines:\n{}",
        offending.len(),
        offending
            .iter()
            .take(10)
            .map(|(n, l)| format!("  line {}: {}", n, l))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

/// Verify that `VK_HEADER_VERSION_COMPLETE` was translated by the
/// macro generator's `VK_MAKE_API_VERSION` invocation translator (not
/// dropped to the // TODO fallback). It should be a usable `pub const`
/// equal to `vk_make_api_version(0, 1, header_minor, VK_HEADER_VERSION)`
/// for whatever spec version vk.xml ships.
#[test]
fn test_vk_header_version_complete_is_usable_const() {
    // The const itself must compile. Variant byte should be 0
    // (desktop Vulkan profile, not Vulkan SC).
    let v = VK_HEADER_VERSION_COMPLETE;
    assert_eq!(
        v >> 29,
        0,
        "VK_HEADER_VERSION_COMPLETE variant byte should be 0 for desktop Vulkan, got {}",
        v >> 29
    );

    // The patch portion should equal VK_HEADER_VERSION (the 12-bit
    // field at the bottom of the encoded version).
    let patch = v & 0xFFF;
    assert_eq!(
        patch, VK_HEADER_VERSION,
        "patch field of VK_HEADER_VERSION_COMPLETE should equal VK_HEADER_VERSION"
    );

    // Major must be at least 1 (this is Vulkan, not Vulkan 0).
    let major = (v >> 22) & 0x7F;
    assert!(major >= 1, "major version should be >= 1, got {}", major);
}