changeset 698:c136bfdca960

fix: incorrect vertex data alignment
author Sam <sam@basx.dev>
date Sat, 20 May 2023 18:17:21 +0700
parents 75cd6f970343
children c29f3ad1604f
files src/semicongine/renderer.nim
diffstat 1 files changed, 7 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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(