Mercurial > games > semicongine
comparison src/zamikongine/buffer.nim @ 28:b1b05d4efb52
big refactoring, part1
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 14 Jan 2023 14:08:00 +0700 |
parents | b55d6ecde79d |
children | 9edca5dc4e93 |
comparison
equal
deleted
inserted
replaced
27:8cb2d96ac28e | 28:b1b05d4efb52 |
---|---|
4 type | 4 type |
5 BufferType* = enum | 5 BufferType* = enum |
6 None = 0 | 6 None = 0 |
7 TransferSrc = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | 7 TransferSrc = VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
8 TransferDst = VK_BUFFER_USAGE_TRANSFER_DST_BIT | 8 TransferDst = VK_BUFFER_USAGE_TRANSFER_DST_BIT |
9 UniformBuffer = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | |
9 IndexBuffer = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | 10 IndexBuffer = VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
10 VertexBuffer = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | 11 VertexBuffer = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
11 Buffer* = object | 12 Buffer* = object |
12 device*: VkDevice | 13 device*: VkDevice |
13 vkBuffer*: VkBuffer | 14 vkBuffer*: VkBuffer |
14 size*: uint64 | 15 size*: uint64 |
15 memoryRequirements*: VkMemoryRequirements | 16 memoryRequirements*: VkMemoryRequirements |
16 memory*: VkDeviceMemory | 17 memory*: VkDeviceMemory |
17 bufferTypes*: set[BufferType] | 18 bufferTypes*: set[BufferType] |
19 persistentMapping: bool | |
20 mapped: pointer | |
18 | 21 |
19 proc trash*(buffer: var Buffer) = | 22 proc trash*(buffer: var Buffer) = |
20 assert int64(buffer.vkBuffer) != 0 | 23 assert int64(buffer.vkBuffer) != 0 |
21 assert int64(buffer.memory) != 0 | 24 assert int64(buffer.memory) != 0 |
22 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil) | 25 vkDestroyBuffer(buffer.device, buffer.vkBuffer, nil) |
35 proc InitBuffer*( | 38 proc InitBuffer*( |
36 device: VkDevice, | 39 device: VkDevice, |
37 physicalDevice: VkPhysicalDevice, | 40 physicalDevice: VkPhysicalDevice, |
38 size: uint64, | 41 size: uint64, |
39 bufferTypes: set[BufferType], | 42 bufferTypes: set[BufferType], |
40 properties: set[VkMemoryPropertyFlagBits] | 43 properties: set[VkMemoryPropertyFlagBits], |
44 persistentMapping: bool = false | |
41 ): Buffer = | 45 ): Buffer = |
42 result = Buffer(device: device, size: size, bufferTypes: bufferTypes) | 46 result = Buffer(device: device, size: size, bufferTypes: bufferTypes, persistentMapping: persistentMapping) |
43 var usageFlags = 0 | 47 var usageFlags = 0 |
44 for usage in bufferTypes: | 48 for usage in bufferTypes: |
45 usageFlags = ord(usageFlags) or ord(usage) | 49 usageFlags = ord(usageFlags) or ord(usage) |
46 var bufferInfo = VkBufferCreateInfo( | 50 var bufferInfo = VkBufferCreateInfo( |
47 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | 51 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
61 allocationSize: result.memoryRequirements.size, | 65 allocationSize: result.memoryRequirements.size, |
62 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties)) | 66 memoryTypeIndex: result.findMemoryType(physicalDevice, VkMemoryPropertyFlags(memoryProperties)) |
63 ) | 67 ) |
64 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory)) | 68 checkVkResult result.device.vkAllocateMemory(addr(allocInfo), nil, addr(result.memory)) |
65 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0)) | 69 checkVkResult result.device.vkBindBufferMemory(result.vkBuffer, result.memory, VkDeviceSize(0)) |
70 if persistentMapping: | |
71 checkVkResult vkMapMemory( | |
72 result.device, | |
73 result.memory, | |
74 offset=VkDeviceSize(0), | |
75 VkDeviceSize(result.size), | |
76 VkMemoryMapFlags(0), | |
77 addr(result.mapped) | |
78 ) | |
66 | 79 |
67 | 80 |
68 template withMapping*(buffer: Buffer, data: pointer, body: untyped): untyped = | 81 template withMapping*(buffer: Buffer, data: pointer, body: untyped): untyped = |
82 assert not buffer.persistentMapping | |
69 checkVkResult vkMapMemory(buffer.device, buffer.memory, offset=VkDeviceSize(0), VkDeviceSize(buffer.size), VkMemoryMapFlags(0), addr(data)) | 83 checkVkResult vkMapMemory(buffer.device, buffer.memory, offset=VkDeviceSize(0), VkDeviceSize(buffer.size), VkMemoryMapFlags(0), addr(data)) |
70 body | 84 body |
71 vkUnmapMemory(buffer.device, buffer.memory) | 85 vkUnmapMemory(buffer.device, buffer.memory) |
72 | 86 |
87 # note: does not work with seq | |
88 proc updateData*[T](buffer: Buffer, data: var T) = | |
89 if buffer.persistentMapping: | |
90 copyMem(buffer.mapped, addr(data), sizeof(T)) | |
91 else: | |
92 var p: pointer | |
93 buffer.withMapping(p): | |
94 copyMem(p, addr(data), sizeof(T)) | |
73 | 95 |
74 proc copyBuffer*(commandPool: VkCommandPool, queue: VkQueue, src, dst: Buffer, size: uint64) = | 96 proc copyBuffer*(commandPool: VkCommandPool, queue: VkQueue, src, dst: Buffer, size: uint64) = |
75 assert uint64(src.device) == uint64(dst.device) | 97 assert uint64(src.device) == uint64(dst.device) |
76 var | 98 var |
77 allocInfo = VkCommandBufferAllocateInfo( | 99 allocInfo = VkCommandBufferAllocateInfo( |