kronos_compute/implementation/
buffer.rs

1//! Buffer creation and management
2
3use crate::sys::*;
4use crate::core::*;
5use crate::ffi::*;
6use crate::implementation::icd_loader;
7
8/// Create a buffer
9// SAFETY: This function is called from C code. Caller must ensure:
10// 1. device is a valid VkDevice created by vkCreateDevice
11// 2. pCreateInfo points to a valid VkBufferCreateInfo structure
12// 3. pAllocator is either null or points to valid allocation callbacks
13// 4. pBuffer points to valid memory for writing the buffer handle
14// 5. All fields in pCreateInfo are valid (size > 0, valid usage flags, etc.)
15#[no_mangle]
16pub unsafe extern "C" fn vkCreateBuffer(
17    device: VkDevice,
18    pCreateInfo: *const VkBufferCreateInfo,
19    pAllocator: *const VkAllocationCallbacks,
20    pBuffer: *mut VkBuffer,
21) -> VkResult {
22    if device.is_null() || pCreateInfo.is_null() || pBuffer.is_null() {
23        return VkResult::ErrorInitializationFailed;
24    }
25    
26    // Route via owning ICD if known
27    if let Some(icd) = icd_loader::icd_for_device(device) {
28        if let Some(f) = icd.create_buffer { return f(device, pCreateInfo, pAllocator, pBuffer); }
29    }
30    // Fallback
31    if let Some(icd) = super::forward::get_icd_if_enabled() {
32        if let Some(create_buffer) = icd.create_buffer { return create_buffer(device, pCreateInfo, pAllocator, pBuffer); }
33    }
34    VkResult::ErrorInitializationFailed
35}
36
37/// Destroy a buffer
38// SAFETY: This function is called from C code. Caller must ensure:
39// 1. device is a valid VkDevice
40// 2. buffer is a valid VkBuffer created by vkCreateBuffer, or VK_NULL_HANDLE
41// 3. pAllocator matches the allocator used in vkCreateBuffer (or both are null)
42// 4. The buffer is not currently bound to memory or in use by any operations
43// 5. All command buffers using this buffer have completed execution
44#[no_mangle]
45pub unsafe extern "C" fn vkDestroyBuffer(
46    device: VkDevice,
47    buffer: VkBuffer,
48    pAllocator: *const VkAllocationCallbacks,
49) {
50    if device.is_null() || buffer.is_null() {
51        return;
52    }
53    
54    if let Some(icd) = icd_loader::icd_for_device(device) {
55        if let Some(f) = icd.destroy_buffer { f(device, buffer, pAllocator); }
56        return;
57    }
58    if let Some(icd) = super::forward::get_icd_if_enabled() {
59        if let Some(destroy_buffer) = icd.destroy_buffer { destroy_buffer(device, buffer, pAllocator); }
60    }
61}
62
63/// Get buffer memory requirements
64// SAFETY: This function is called from C code. Caller must ensure:
65// 1. device is a valid VkDevice
66// 2. buffer is a valid VkBuffer created by vkCreateBuffer
67// 3. pMemoryRequirements points to valid memory for a VkMemoryRequirements structure
68// 4. The buffer has not been destroyed
69#[no_mangle]
70pub unsafe extern "C" fn vkGetBufferMemoryRequirements(
71    device: VkDevice,
72    buffer: VkBuffer,
73    pMemoryRequirements: *mut VkMemoryRequirements,
74) {
75    if device.is_null() || buffer.is_null() || pMemoryRequirements.is_null() {
76        return;
77    }
78    
79    if let Some(icd) = icd_loader::icd_for_device(device) {
80        if let Some(f) = icd.get_buffer_memory_requirements { f(device, buffer, pMemoryRequirements); }
81        return;
82    }
83    if let Some(icd) = super::forward::get_icd_if_enabled() {
84        if let Some(get_buffer_memory_requirements) = icd.get_buffer_memory_requirements { get_buffer_memory_requirements(device, buffer, pMemoryRequirements); }
85    }
86}
87
88/// Bind buffer to memory
89// SAFETY: This function is called from C code. Caller must ensure:
90// 1. device is a valid VkDevice
91// 2. buffer is a valid VkBuffer that has not been bound to memory yet
92// 3. memory is a valid VkDeviceMemory allocated with vkAllocateMemory
93// 4. memoryOffset + buffer.size <= memory.size (fits within allocated memory)
94// 5. The memory type is compatible with the buffer's memory requirements
95// 6. Neither buffer nor memory are currently in use by GPU operations
96#[no_mangle]
97pub unsafe extern "C" fn vkBindBufferMemory(
98    device: VkDevice,
99    buffer: VkBuffer,
100    memory: VkDeviceMemory,
101    memoryOffset: VkDeviceSize,
102) -> VkResult {
103    if device.is_null() || buffer.is_null() || memory.is_null() {
104        return VkResult::ErrorInitializationFailed;
105    }
106    
107    if let Some(icd) = icd_loader::icd_for_device(device) {
108        if let Some(f) = icd.bind_buffer_memory { return f(device, buffer, memory, memoryOffset); }
109    }
110    if let Some(icd) = super::forward::get_icd_if_enabled() {
111        if let Some(bind_buffer_memory) = icd.bind_buffer_memory { return bind_buffer_memory(device, buffer, memory, memoryOffset); }
112    }
113    VkResult::ErrorInitializationFailed
114}