annotate src/zamikongine/mesh.nim @ 500:8025ab67d931

fix: hello cube, add: run_all command
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 13:49:12 +0700
parents 3f1111f3b9f8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/typetraits
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
2
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
3 import ./vulkan
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./thing
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
5 import ./buffer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./vertex
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
7 import ./math/vector
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
8
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
9 type
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
10 Mesh*[T] = object of Part
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
11 vertexData*: T
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
12 IndexedMesh*[T: object, U: uint16|uint32] = object of Part
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
13 vertexData*: T
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
14 indices*: seq[array[3, U]]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
15
481
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
16 func createUberMesh*[T](meshes: openArray[Mesh[T]]): Mesh[T] =
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
17 for mesh in meshes:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
18 for srcname, srcvalue in mesh.vertexData.fieldPairs:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
19 when typeof(srcvalue) is VertexAttribute:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
20 for dstname, dstvalue in result.vertexData.fieldPairs:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
21 when srcname == dstname:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
22 dstvalue.data.add srcvalue.data
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
23
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
24 func createUberMesh*[T: object, U: uint16|uint32](meshes: openArray[IndexedMesh[T, U]]): IndexedMesh[T, U] =
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
25 var indexoffset = U(0)
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
26 for mesh in meshes:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
27 for srcname, srcvalue in mesh.vertexData.fieldPairs:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
28 when typeof(srcvalue) is VertexAttribute:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
29 for dstname, dstvalue in result.vertexData.fieldPairs:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
30 when srcname == dstname:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
31 dstvalue.data.add srcvalue.data
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
32 var indexdata: seq[array[3, U]]
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
33 for i in mesh.indices:
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
34 indexdata.add [i[0] + indexoffset, i[1] + indexoffset, i[2] + indexoffset]
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
35 result.indices.add indexdata
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
36 indexoffset += U(mesh.vertexData.VertexCount)
c472abfcee57 add: generate ubermeshes
Sam <sam@basx.dev>
parents: 480
diff changeset
37
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
38 func getVkIndexType[T: object, U: uint16|uint32](m: IndexedMesh[T, U]): VkIndexType =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
39 when U is uint16: VK_INDEX_TYPE_UINT16
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
40 elif U is uint32: VK_INDEX_TYPE_UINT32
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
41
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
42 proc createVertexBuffers*[M: Mesh|IndexedMesh](
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
43 mesh: var M,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
44 device: VkDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
45 physicalDevice: VkPhysicalDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
46 commandPool: VkCommandPool,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
47 queue: VkQueue,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
48 useDeviceLocalBuffer: bool = true # decides if data is transfered to the fast device-local memory or not
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
49 ): (seq[Buffer], uint32) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
50 result[1] = mesh.vertexData.VertexCount
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
51 for name, value in mesh.vertexData.fieldPairs:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
52 when typeof(value) is VertexAttribute:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
53 assert value.data.len > 0
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
54 var flags = if useDeviceLocalBuffer: {TransferSrc} else: {VertexBuffer}
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
55 var stagingBuffer = device.InitBuffer(physicalDevice, value.datasize, flags, {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT})
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
56 var d: pointer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
57 stagingBuffer.withMapping(d):
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
58 copyMem(d, addr(value.data[0]), value.datasize)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
59
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
60 if useDeviceLocalBuffer:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
61 var finalBuffer = device.InitBuffer(physicalDevice, value.datasize, {TransferDst, VertexBuffer}, {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT})
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
62 copyBuffer(commandPool, queue, stagingBuffer, finalBuffer, value.datasize)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
63 stagingBuffer.trash()
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
64 result[0].add(finalBuffer)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
65 else:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
66 result[0].add(stagingBuffer)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
67
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
68 proc createIndexBuffer*(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
69 mesh: var IndexedMesh,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
70 device: VkDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
71 physicalDevice: VkPhysicalDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
72 commandPool: VkCommandPool,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
73 queue: VkQueue,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
74 useDeviceLocalBuffer: bool = true # decides if data is transfered to the fast device-local memory or not
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
75 ): Buffer =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
76 let bufferSize = uint64(mesh.indices.len * sizeof(get(genericParams(typeof(mesh.indices)), 0)))
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
77 let flags = if useDeviceLocalBuffer: {TransferSrc} else: {IndexBuffer}
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
78
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
79 var stagingBuffer = device.InitBuffer(physicalDevice, bufferSize, flags, {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT})
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
80 var d: pointer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
81 stagingBuffer.withMapping(d):
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
82 copyMem(d, addr(mesh.indices[0]), bufferSize)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
83
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
84 if useDeviceLocalBuffer:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
85 var finalBuffer = device.InitBuffer(physicalDevice, bufferSize, {TransferDst, IndexBuffer}, {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT})
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
86 copyBuffer(commandPool, queue, stagingBuffer, finalBuffer, bufferSize)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
87 stagingBuffer.trash()
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
88 return finalBuffer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
89 else:
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
90 return stagingBuffer
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
91
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
92 proc createIndexedVertexBuffers*(
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
93 mesh: var IndexedMesh,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
94 device: VkDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
95 physicalDevice: VkPhysicalDevice,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
96 commandPool: VkCommandPool,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
97 queue: VkQueue,
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
98 useDeviceLocalBuffer: bool = true # decides if data is transfered to the fast device-local memory or not
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
99 ): (seq[Buffer], Buffer, uint32, VkIndexType) =
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
100 result[0] = createVertexBuffers(mesh, device, physicalDevice, commandPool, queue, useDeviceLocalBuffer)[0]
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
101 result[1] = createIndexBuffer(mesh, device, physicalDevice, commandPool, queue, useDeviceLocalBuffer)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
102 result[2] = uint32(mesh.indices.len * mesh.indices[0].len)
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
103
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
104 result[3] = getVkIndexType(mesh)
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
105
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
106 func squareData*[T:SomeFloat](): auto = PositionAttribute[Vec2[T]](
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
107 data: @[Vec2[T]([T(0), T(0)]), Vec2[T]([T(0), T(1)]), Vec2[T]([T(1), T(1)]), Vec2[T]([T(1), T(0)])]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
108 )
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
109 func squareIndices*[T:uint16|uint32](): auto = seq[array[3, T]](
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 494
diff changeset
110 @[[T(0), T(1), T(3)], [T(2), T(1), T(3)]]
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 481
diff changeset
111 )