kronos_compute/implementation/
buffer.rs

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