changeset 138:62bc83b8a8c7

fix: mixing memory location types is not working
author Sam <sam@basx.dev>
date Sat, 22 Apr 2023 16:48:07 +0700
parents ac2b6777c5db
children 34112bf5abf8
files src/semicongine/renderer.nim src/semicongine/vulkan/drawable.nim tests/test_vulkan_wrapper.nim
diffstat 3 files changed, 17 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/renderer.nim	Fri Apr 21 00:29:09 2023 +0700
+++ b/src/semicongine/renderer.nim	Sat Apr 22 16:48:07 2023 +0700
@@ -72,16 +72,16 @@
       mappable=false,
     )
 
-  # TODO: groupByMemoryLocation does bad stuff when we have mixed types of memory locations for different mesh attributes
-
   # one vertex data buffer per memory location
   var perLocationOffsets: Table[MemoryLocation, uint64]
-  for location, attributes in inputs.groupByMemoryLocation().pairs:
+  var perLocationSizes: Table[MemoryLocation, uint64]
+  for attribute in inputs:
     # setup one buffer per attribute-location-type
-    var bufferSize = 0'u64
+    if not (attribute.memoryLocation in perLocationSizes):
+      perLocationSizes[attribute.memoryLocation] = 0'u64
     for mesh in allMeshes:
-      for attribute in attributes:
-        bufferSize += mesh.dataSize(attribute.name)
+      perLocationSizes[attribute.memoryLocation] += mesh.dataSize(attribute.name)
+  for location, bufferSize in perLocationSizes.pairs:
     if bufferSize > 0:
       data.vertexBuffers[location] = renderer.device.createBuffer(
         size=bufferSize,
@@ -93,15 +93,12 @@
 
   var indexBufferOffset = 0'u64
   for mesh in allMeshes:
-    var offsets: Table[MemoryLocation, seq[uint64]]
-    for location, attributes in inputs.groupByMemoryLocation().pairs:
-      for attribute in attributes:
-        if not (location in offsets):
-          offsets[location] = @[]
-        offsets[location].add perLocationOffsets[location]
-        var (pdata, size) = mesh.getRawData(attribute.name)
-        data.vertexBuffers[location].setData(pdata, size, perLocationOffsets[location])
-        perLocationOffsets[location] += size
+    var offsets: seq[(MemoryLocation, uint64)]
+    for attribute in inputs:
+      offsets.add (attribute.memoryLocation, perLocationOffsets[attribute.memoryLocation])
+      var (pdata, size) = mesh.getRawData(attribute.name)
+      data.vertexBuffers[attribute.memoryLocation].setData(pdata, size, perLocationOffsets[attribute.memoryLocation])
+      perLocationOffsets[attribute.memoryLocation] += size
 
     let indexed = mesh.indexType != None
     var drawable = Drawable(
--- a/src/semicongine/vulkan/drawable.nim	Fri Apr 21 00:29:09 2023 +0700
+++ b/src/semicongine/vulkan/drawable.nim	Sat Apr 22 16:48:07 2023 +0700
@@ -11,7 +11,7 @@
 type
   Drawable* = object
     elementCount*: uint32 # number of vertices or indices
-    bufferOffsets*: Table[MemoryLocation, seq[uint64]] # list of buffers and list of offset for each attribute in that buffer
+    bufferOffsets*: seq[(MemoryLocation, uint64)] # list of buffers and list of offset for each attribute in that buffer
     instanceCount*: uint32 # number of instance
     case indexed*: bool
     of true:
@@ -32,10 +32,9 @@
     var buffers: seq[VkBuffer]
     var offsets: seq[VkDeviceSize]
 
-    for (location, bufferOffsets) in drawable.bufferOffsets.pairs:
-      for offset in bufferOffsets:
-        buffers.add vertexBuffers[location].vk
-        offsets.add VkDeviceSize(offset)
+    for (location, offset) in drawable.bufferOffsets:
+      buffers.add vertexBuffers[location].vk
+      offsets.add VkDeviceSize(offset)
 
     commandBuffer.vkCmdBindVertexBuffers(
       firstBinding=0'u32,
--- a/tests/test_vulkan_wrapper.nim	Fri Apr 21 00:29:09 2023 +0700
+++ b/tests/test_vulkan_wrapper.nim	Sat Apr 22 16:48:07 2023 +0700
@@ -112,7 +112,7 @@
   const
     vertexInput = @[
       attr[Vec3f]("position", memoryLocation=VRAM),
-      attr[Vec3f]("color", memoryLocation=VRAM),
+      attr[Vec3f]("color", memoryLocation=RAM),
       attr[Vec3f]("translate", perInstance=true)
     ]
     vertexOutput = @[attr[Vec3f]("outcolor")]