Mercurial > games > semicongine
annotate src/semicongine/vulkan/drawable.nim @ 617:f7e7af33e9ee
did: refactor memory selection
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 29 Apr 2023 16:50:43 +0700 |
parents | 5eadc1e1d6d8 |
children | 041a114db1dc |
rev | line source |
---|---|
588 | 1 import std/tables |
2 import std/strformat | |
3 import std/logging | |
4 | |
5 import ./api | |
6 import ./utils | |
7 import ./buffer | |
617 | 8 import ./memory |
588 | 9 |
10 import ../gpu_data | |
11 | |
12 type | |
13 Drawable* = object | |
14 elementCount*: uint32 # number of vertices or indices | |
617 | 15 bufferOffsets*: seq[(MemoryPerformanceHint, uint64)] # list of buffers and list of offset for each attribute in that buffer |
588 | 16 instanceCount*: uint32 # number of instance |
17 case indexed*: bool | |
18 of true: | |
19 indexType*: VkIndexType | |
20 indexBufferOffset*: uint64 | |
21 of false: | |
22 discard | |
23 | |
24 func `$`*(drawable: Drawable): string = | |
25 if drawable.indexed: | |
26 &"Drawable(elementCount: {drawable.elementCount}, instanceCount: {drawable.instanceCount}, bufferOffsets: {drawable.bufferOffsets}, indexType: {drawable.indexType}, indexBufferOffset: {drawable.indexBufferOffset})" | |
27 else: | |
28 &"Drawable(elementCount: {drawable.elementCount}, instanceCount: {drawable.instanceCount}, bufferOffsets: {drawable.bufferOffsets})" | |
29 | |
617 | 30 proc draw*(commandBuffer: VkCommandBuffer, drawable: Drawable, vertexBuffers: Table[MemoryPerformanceHint, Buffer], indexBuffer: BUffer) = |
588 | 31 debug "Draw ", drawable |
32 | |
33 var buffers: seq[VkBuffer] | |
34 var offsets: seq[VkDeviceSize] | |
35 | |
617 | 36 for (performanceHint, offset) in drawable.bufferOffsets: |
37 buffers.add vertexBuffers[performanceHint].vk | |
599
5eadc1e1d6d8
fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
38 offsets.add VkDeviceSize(offset) |
588 | 39 |
40 commandBuffer.vkCmdBindVertexBuffers( | |
41 firstBinding=0'u32, | |
42 bindingCount=uint32(buffers.len), | |
43 pBuffers=buffers.toCPointer(), | |
44 pOffsets=offsets.toCPointer() | |
45 ) | |
46 if drawable.indexed: | |
47 commandBuffer.vkCmdBindIndexBuffer(indexBuffer.vk, VkDeviceSize(drawable.indexBufferOffset), drawable.indexType) | |
48 commandBuffer.vkCmdDrawIndexed( | |
49 indexCount=drawable.elementCount, | |
50 instanceCount=drawable.instanceCount, | |
51 firstIndex=0, | |
52 vertexOffset=0, | |
53 firstInstance=0 | |
54 ) | |
55 else: | |
56 commandBuffer.vkCmdDraw( | |
57 vertexCount=drawable.elementCount, | |
58 instanceCount=drawable.instanceCount, | |
59 firstVertex=0, | |
60 firstInstance=0 | |
61 ) |