changeset 237:2fca78b0c0d6

fix: incorrect vertex data alignment
author Sam <sam@basx.dev>
date Sat, 20 May 2023 18:17:21 +0700
parents 4a7b8cd88e0e
children ef8cea2545f8
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(