# HG changeset patch
# User Sam <sam@basx.dev>
# Date 1684581441 -25200
# Node ID c136bfdca960f05c7f383798baf43e3a0c16133d
# Parent  75cd6f9703431e85658a0f0dd23d753446dabe5e
fix: incorrect vertex data alignment

diff -r 75cd6f970343 -r c136bfdca960 src/semicongine/renderer.nim
--- a/src/semicongine/renderer.nim	Sat May 20 17:45:12 2023 +0700
+++ b/src/semicongine/renderer.nim	Sat May 20 18:17:21 2023 +0700
@@ -52,9 +52,9 @@
 
 proc setupDrawableBuffers*(renderer: var Renderer, scene: Scene, inputs: seq[ShaderAttribute], transformAttribute="") =
   assert not (scene in renderer.scenedata)
+  const VERTEX_ATTRIB_ALIGNMENT = 4 # used for buffer alignment
   var data = SceneData()
 
-
   # when mesh transformation are handled through the scenegraph-transformation, set it up here
   if transformattribute != "":
     var hasTransformAttribute = false
@@ -111,6 +111,10 @@
     inc bindingNumber
     # setup one buffer per attribute-location-type
     for mesh in allMeshes:
+      # align size to VERTEX_ATTRIB_ALIGNMENT bytes (the important thing is the correct alignment of the offsets, bu
+      # we need to expand the buffer size as well, therefore considering alignment already here as well
+      if perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0:
+        perLocationSizes[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT)
       perLocationSizes[attribute.memoryPerformanceHint] += mesh.dataSize(attribute.name)
   for memoryPerformanceHint, bufferSize in perLocationSizes.pairs:
     if bufferSize > 0:
@@ -131,6 +135,8 @@
       if pdata != nil: # no data
         data.vertexBuffers[attribute.memoryPerformanceHint].setData(pdata, size, perLocationOffsets[attribute.memoryPerformanceHint])
         perLocationOffsets[attribute.memoryPerformanceHint] += size
+        if perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0:
+          perLocationOffsets[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT)
 
     let indexed = mesh.indexType != None
     var drawable = Drawable(