annotate src/semicongine/vulkan/buffer.nim @ 113:7b695fb335ed

did: first final implementation of scene-graph <-> pipeline connection, not working yet
author Sam <sam@basx.dev>
date Sun, 02 Apr 2023 01:22:09 +0700
parents 0c5a74885796
children 056e08dfad10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
1 import std/strformat
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
2 import std/typetraits
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
3 import std/sequtils
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
4 import std/tables
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
5
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./api
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 92
diff changeset
7 import ./device
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
8 import ./memory
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
9 import ./physicaldevice
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
10 import ./commandbuffer
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
11
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
12 type
105
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 97
diff changeset
13 Buffer* = object
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 97
diff changeset
14 device*: Device
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 97
diff changeset
15 vk*: VkBuffer
4059aa0d689b add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 97
diff changeset
16 size*: uint64
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
17 usage*: seq[VkBufferUsageFlagBits]
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
18 case hasMemory*: bool
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
19 of false: discard
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
20 of true:
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
21 memory*: DeviceMemory
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
22 data*: pointer
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
23
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
24 func `$`*(buffer: Buffer): string = &"Buffer(size: {buffer.size}, usage: {buffer.usage})"
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
25
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
26 proc setData*(dst: Buffer, src: pointer, len: uint64, offset=0'u64) =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
27 assert offset + len <= dst.size
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
28 copyMem(cast[pointer](cast[uint64](dst.data) + offset), src, len)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
29
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
30 proc setData*[T: seq](dst: Buffer, src: ptr T, offset=0'u64) =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
31 dst.setData(src, sizeof(get(genericParams(T), 0)) * src[].len, offset=offset)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
33 proc setData*[T](dst: Buffer, src: ptr T, offset=0'u64) =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
34 dst.setData(src, sizeof(T), offset=offset)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
35
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
36 proc allocateMemory(buffer: var Buffer, flags: openArray[VkMemoryPropertyFlagBits]) =
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
37 assert buffer.device.vk.valid
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
38 assert buffer.hasMemory == false
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
39
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
40 buffer.hasMemory = true
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
41 buffer.memory = buffer.device.allocate(buffer.size, flags)
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
42 checkVkResult buffer.device.vk.vkBindBufferMemory(buffer.vk, buffer.memory.vk, VkDeviceSize(0))
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
43 buffer.data = buffer.memory.map()
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
44
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
45 # currently no support for extended structure and concurrent/shared use
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
46 # (shardingMode = VK_SHARING_MODE_CONCURRENT not supported)
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
47 proc createBuffer*(
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
48 device: Device,
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
49 size: uint64,
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
50 usage: openArray[VkBufferUsageFlagBits],
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
51 flags: openArray[VkBufferCreateFlagBits] = @[],
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
52 memoryFlags: openArray[VkMemoryPropertyFlagBits] = @[],
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
53 ): Buffer =
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
54 assert device.vk.valid
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
55 assert size > 0
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
56
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
57 result.device = device
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
58 result.size = size
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
59 result.usage = usage.toSeq
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
60 var createInfo = VkBufferCreateInfo(
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
61 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
62 flags: toBits(flags),
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
63 size: size,
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
64 usage: toBits(usage),
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
65 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
66 )
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
67
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
68 checkVkResult vkCreateBuffer(
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 92
diff changeset
69 device=device.vk,
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
70 pCreateInfo=addr createInfo,
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
71 pAllocator=nil,
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
72 pBuffer=addr result.vk
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
73 )
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
74 result.allocateMemory(memoryFlags)
89
6e8037c1f655 add: better vulkan wrappers
Sam <sam@basx.dev>
parents:
diff changeset
75
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
76
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
77 proc copy*(src, dst: Buffer) =
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
78 assert src.device.vk.valid
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
79 assert dst.device.vk.valid
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
80 assert src.device == dst.device
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
81 assert src.size == dst.size
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
82 assert VK_BUFFER_USAGE_TRANSFER_SRC_BIT in src.usage
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
83 assert VK_BUFFER_USAGE_TRANSFER_DST_BIT in dst.usage
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
84
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
85 var queue: Queue
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
86 for q in src.device.queues.values:
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
87 if q.family.canDoTransfer:
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
88 queue = q
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
89 if not queue.vk.valid:
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
90 raise newException(Exception, "No queue that supports buffer transfer")
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
91
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
92 var
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
93 commandBufferPool = src.device.createCommandBufferPool(family=queue.family, nBuffers=1)
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
94 commandBuffer = commandBufferPool.buffers[0]
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
95
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
96 beginInfo = VkCommandBufferBeginInfo(
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
97 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
98 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
99 )
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
100 copyRegion = VkBufferCopy(size: VkDeviceSize(src.size))
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
101 checkVkResult commandBuffer.vkBeginCommandBuffer(addr(beginInfo))
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
102 commandBuffer.vkCmdCopyBuffer(src.vk, dst.vk, 1, addr(copyRegion))
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
103 checkVkResult commandBuffer.vkEndCommandBuffer()
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
104
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
105 var submitInfo = VkSubmitInfo(
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
106 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
107 commandBufferCount: 1,
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
108 pCommandBuffers: addr(commandBuffer),
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
109 )
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
110 checkVkResult queue.vk.vkQueueSubmit(1, addr(submitInfo), VkFence(0))
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
111 checkVkResult queue.vk.vkQueueWaitIdle()
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
112 commandBufferPool.destroy()
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
113
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
114 proc destroy*(buffer: var Buffer) =
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 92
diff changeset
115 assert buffer.device.vk.valid
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 92
diff changeset
116 assert buffer.vk.valid
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
117 if buffer.hasMemory:
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
118 assert buffer.memory.vk.valid
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 105
diff changeset
119 buffer.memory.free
94
f036546f5ea2 add: device, surface and refactoring
Sam <sam@basx.dev>
parents: 92
diff changeset
120 buffer.device.vk.vkDestroyBuffer(buffer.vk, nil)
97
110ed3ee5df8 add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents: 94
diff changeset
121 buffer.vk.reset()