comparison src/zamikongine/buffer.nim @ 33:94c38e4b5782

did: refactoring, move more from make to nimscript
author Sam <sam@basx.dev>
date Sun, 15 Jan 2023 23:23:54 +0700
parents 9edca5dc4e93
children
comparison
equal deleted inserted replaced
32:9edca5dc4e93 33:94c38e4b5782
19 persistentMapping: bool 19 persistentMapping: bool
20 mapped: pointer 20 mapped: pointer
21 21
22 proc trash*(buffer: var Buffer) = 22 proc trash*(buffer: var Buffer) =
23 assert int64(buffer.vkBuffer) != 0 23 assert int64(buffer.vkBuffer) != 0
24 assert int64(buffer.memory) != 0
25 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil) 24 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil)
26 buffer.vkBuffer = VkBuffer(0) 25 buffer.vkBuffer = VkBuffer(0)
26 if buffer.size == 0: # for zero-size buffers there are no memory allocations
27 return
28 assert int64(buffer.memory) != 0
27 vkFreeMemory(buffer.device, buffer.memory, nil) 29 vkFreeMemory(buffer.device, buffer.memory, nil)
28 buffer.memory = VkDeviceMemory(0) 30 buffer.memory = VkDeviceMemory(0)
29 31
30 proc findMemoryType(buffer: Buffer, physicalDevice: VkPhysicalDevice, properties: VkMemoryPropertyFlags): uint32 = 32 proc findMemoryType(buffer: Buffer, physicalDevice: VkPhysicalDevice, properties: VkMemoryPropertyFlags): uint32 =
31 var physicalProperties: VkPhysicalDeviceMemoryProperties 33 var physicalProperties: VkPhysicalDeviceMemoryProperties
63 var allocInfo = VkMemoryAllocateInfo( 65 var allocInfo = VkMemoryAllocateInfo(
64 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 66 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
65 allocationSize: result.memoryRequirements.size, 67 allocationSize: result.memoryRequirements.size,
66 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties)) 68 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties))
67 ) 69 )
68 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory)) 70 if result.size > 0:
71 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory))
69 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0)) 72 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0))
70 if persistentMapping: 73 if persistentMapping:
71 checkVkResult vkMapMemory( 74 checkVkResult vkMapMemory(
72 result.device, 75 result.device,
73 result.memory, 76 result.memory,