changeset 625:c48ceb622b27

fix: buffer update with staging buffer not correctly working
author Sam <sam@basx.dev>
date Sat, 29 Apr 2023 05:40:11 -0700
parents 3f13de7d8ec4
children bf2f4a9cd962
files examples/E10_pong.nim src/semicongine/renderer.nim src/semicongine/vulkan/buffer.nim src/semicongine/vulkan/drawable.nim
diffstat 4 files changed, 11 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/examples/E10_pong.nim	Sat Apr 29 18:29:19 2023 +0700
+++ b/examples/E10_pong.nim	Sat Apr 29 05:40:11 2023 -0700
@@ -21,7 +21,7 @@
   level = newEntity("Level")
   var playerbarmesh = rect(color=barcolor)
   var playerbar = newEntity("playerbar", playerbarmesh)
-  # playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
+  playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
   var player = newEntity("player", playerbar)
   player.transform = translate3d(0'f, 0.3'f, 0'f)
   level.add player
--- a/src/semicongine/renderer.nim	Sat Apr 29 18:29:19 2023 +0700
+++ b/src/semicongine/renderer.nim	Sat Apr 29 05:40:11 2023 -0700
@@ -115,9 +115,9 @@
 
   var indexBufferOffset = 0'u64
   for mesh in allMeshes:
-    var offsets: seq[(MemoryPerformanceHint, uint64)]
+    var offsets: seq[(string, MemoryPerformanceHint, uint64)]
     for attribute in inputs:
-      offsets.add (attribute.memoryPerformanceHint, perLocationOffsets[attribute.memoryPerformanceHint])
+      offsets.add (attribute.name, attribute.memoryPerformanceHint, perLocationOffsets[attribute.memoryPerformanceHint])
       var (pdata, size) = mesh.getRawData(attribute.name)
       data.vertexBuffers[attribute.memoryPerformanceHint].setData(pdata, size, perLocationOffsets[attribute.memoryPerformanceHint])
       perLocationOffsets[attribute.memoryPerformanceHint] += size
@@ -152,7 +152,7 @@
   var (pdata, size) = mesh.getRawData(attribute)
   let memoryPerformanceHint = sceneData.attributeLocation[attribute]
   let bindingNumber = sceneData.attributeBindingNumber[attribute]
-  sceneData.vertexBuffers[memoryPerformanceHint].setData(pdata, size, sceneData.drawables[mesh].bufferOffsets[bindingNumber][1])
+  sceneData.vertexBuffers[memoryPerformanceHint].setData(pdata, size, sceneData.drawables[mesh].bufferOffsets[bindingNumber][2])
 
 
 proc refreshMeshData*(renderer: var Renderer, scene: Entity) =
--- a/src/semicongine/vulkan/buffer.nim	Sat Apr 29 18:29:19 2023 +0700
+++ b/src/semicongine/vulkan/buffer.nim	Sat Apr 29 05:40:11 2023 -0700
@@ -92,14 +92,13 @@
     pBuffer=addr result.vk
   )
   result.allocateMemory(requireMappable=requireMappable, preferVRAM=preferVRAM, preferAutoFlush=preferAutoFlush)
-  echo "New Buffer ", result
 
 
-proc copy*(src, dst: Buffer) =
+proc copy*(src, dst: Buffer, dstOffset=0'u64) =
   assert src.device.vk.valid
   assert dst.device.vk.valid
   assert src.device == dst.device
-  assert src.size < dst.size
+  assert src.size <= dst.size - dstOffset
   assert VK_BUFFER_USAGE_TRANSFER_SRC_BIT in src.usage
   assert VK_BUFFER_USAGE_TRANSFER_DST_BIT in dst.usage
 
@@ -118,9 +117,8 @@
       sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
       flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
     )
-    copyRegion = VkBufferCopy(size: VkDeviceSize(src.size))
+    copyRegion = VkBufferCopy(size: VkDeviceSize(src.size), dstOffset: dstOffset)
   checkVkResult commandBuffer.vkBeginCommandBuffer(addr(beginInfo))
-  echo "B ", dst
   commandBuffer.vkCmdCopyBuffer(src.vk, dst.vk, 1, addr(copyRegion))
   checkVkResult commandBuffer.vkEndCommandBuffer()
 
@@ -151,8 +149,7 @@
   else: # use staging buffer, slower but required if memory is not host visible
     var stagingBuffer = dst.device.createBuffer(size, [VK_BUFFER_USAGE_TRANSFER_SRC_BIT], requireMappable=true, preferVRAM=false, preferAutoFlush=true)
     stagingBuffer.setData(src, size, 0)
-    echo "B ", dst
-    stagingBuffer.copy(dst)
+    stagingBuffer.copy(dst, bufferOffset)
     stagingBuffer.destroy()
 
 proc setData*[T: seq](dst: Buffer, src: ptr T, offset=0'u64) =
--- a/src/semicongine/vulkan/drawable.nim	Sat Apr 29 18:29:19 2023 +0700
+++ b/src/semicongine/vulkan/drawable.nim	Sat Apr 29 05:40:11 2023 -0700
@@ -11,7 +11,7 @@
 type
   Drawable* = object
     elementCount*: uint32 # number of vertices or indices
-    bufferOffsets*: seq[(MemoryPerformanceHint, uint64)] # list of buffers and list of offset for each attribute in that buffer
+    bufferOffsets*: seq[(string, MemoryPerformanceHint, uint64)] # list of buffers and list of offset for each attribute in that buffer
     instanceCount*: uint32 # number of instance
     case indexed*: bool
     of true:
@@ -26,13 +26,13 @@
   else:
     &"Drawable(elementCount: {drawable.elementCount}, instanceCount: {drawable.instanceCount}, bufferOffsets: {drawable.bufferOffsets})"
 
-proc draw*(commandBuffer: VkCommandBuffer, drawable: Drawable, vertexBuffers: Table[MemoryPerformanceHint, Buffer], indexBuffer: BUffer) =
+proc draw*(commandBuffer: VkCommandBuffer, drawable: Drawable, vertexBuffers: Table[MemoryPerformanceHint, Buffer], indexBuffer: Buffer) =
     debug "Draw ", drawable
 
     var buffers: seq[VkBuffer]
     var offsets: seq[VkDeviceSize]
 
-    for (performanceHint, offset) in drawable.bufferOffsets:
+    for (name, performanceHint, offset) in drawable.bufferOffsets:
       buffers.add vertexBuffers[performanceHint].vk
       offsets.add VkDeviceSize(offset)