Mercurial > games > semicongine
comparison semiconginev2/old/vulkan/drawable.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/vulkan/drawable.nim@a3eb305bcac2 |
children |
comparison
equal
deleted
inserted
replaced
1217:f819a874058f | 1218:56781cc0fc7c |
---|---|
1 import std/tables | |
2 import std/strformat | |
3 import std/logging | |
4 | |
5 import ../core | |
6 import ./buffer | |
7 | |
8 type | |
9 Drawable* = object | |
10 name*: string | |
11 elementCount*: int # number of vertices or indices | |
12 bufferOffsets*: Table[VkPipeline, seq[(string, MemoryPerformanceHint, uint64)]] # list of buffers and list of offset for each attribute in that buffer | |
13 instanceCount*: int # number of instance | |
14 case indexed*: bool | |
15 of true: | |
16 indexType*: VkIndexType | |
17 indexBufferOffset*: uint64 | |
18 of false: | |
19 discard | |
20 | |
21 func `$`*(drawable: Drawable): string = | |
22 if drawable.indexed: | |
23 &"Drawable({drawable.name}, elementCount: {drawable.elementCount}, instanceCount: {drawable.instanceCount}, bufferOffsets: {drawable.bufferOffsets}, indexType: {drawable.indexType}, indexBufferOffset: {drawable.indexBufferOffset})" | |
24 else: | |
25 &"Drawable({drawable.name}, elementCount: {drawable.elementCount}, instanceCount: {drawable.instanceCount}, bufferOffsets: {drawable.bufferOffsets})" | |
26 | |
27 proc Draw*(drawable: Drawable, commandBuffer: VkCommandBuffer, vertexBuffers: Table[MemoryPerformanceHint, Buffer], indexBuffer: Buffer, pipeline: VkPipeline) = | |
28 debug &"Draw {drawable} with pipeline {pipeline}" | |
29 | |
30 var buffers: seq[VkBuffer] | |
31 var offsets: seq[VkDeviceSize] | |
32 | |
33 for (name, performanceHint, offset) in drawable.bufferOffsets[pipeline]: | |
34 assert vertexBuffers[performanceHint].vk.Valid | |
35 buffers.add vertexBuffers[performanceHint].vk | |
36 offsets.add VkDeviceSize(offset) | |
37 | |
38 debug "Binding buffers: ", buffers | |
39 debug "with offsets ", offsets | |
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 assert indexBuffer.vk.Valid | |
48 commandBuffer.vkCmdBindIndexBuffer(indexBuffer.vk, VkDeviceSize(drawable.indexBufferOffset), drawable.indexType) | |
49 commandBuffer.vkCmdDrawIndexed( | |
50 indexCount = uint32(drawable.elementCount), | |
51 instanceCount = uint32(drawable.instanceCount), | |
52 firstIndex = 0, | |
53 vertexOffset = 0, | |
54 firstInstance = 0 | |
55 ) | |
56 else: | |
57 commandBuffer.vkCmdDraw( | |
58 vertexCount = uint32(drawable.elementCount), | |
59 instanceCount = uint32(drawable.instanceCount), | |
60 firstVertex = 0, | |
61 firstInstance = 0 | |
62 ) |