rtcNewSharedBuffer needs to be 16 byte aligned
from the embree docs, it reads:
When the buffer will be used as a vertex buffer (RTC_BUFFER_TYPE_VERTEX and RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE), the last buffer element must be readable using 16-byte SSE load instructions, thus padding the last element is required for certain layouts. E.g. a standard float3 vertex buffer layout should add storage for at least one more float to the end of the buffer.
This seems not always to be the case in: star-3d/src/s3d_scene_view.c:264
rtcSetGeometryBuffer(geom->rtc, RTC_BUFFER_TYPE_VERTEX, 0/*slot*/,
RTC_FORMAT_FLOAT3, buf, 0/*offset*/, sizeof(float[3])/*stride*/, nverts);
at least for my use case in which I call:
nverts = 9
s3d_shape_create_mesh
s3d_mesh_setup_indexed_vertices
A quick fix in my case (following the suggestion of the embree docs) is to add this extra float to the mesh-data in i.e.:
star-3d/src/s3d_mesh.c:117
res = darray_float_resize(&mesh->attribs[S3D_POSITION]->data, nverts*3+1);
this does however invalidate the assertion in
star-3d/src/s3d_mesh.c:318
ASSERT(ncoords % 3 == 0);
and I am not sure what the consequences of these extra 4 bytes are.
Also, see attached a valgrind output of the buffer error with stacktrace:
==5265== Invalid read of size 4
==5265== at 0x2A086359: checkPadding16 (buffer.h:230)
==5265== by 0x2A086359: embree::TriangleMesh::setBuffer(RTCBufferType, unsigned int, RTCFormat, embree::Ref<embree::Buffer> const&, unsigned long, unsigned long, unsigned int) (scene_triangle_mesh.cpp:67)
==5265== by 0x2A0229A9: rtcSetGeometryBuffer (rtcore.cpp:1402)
==5265== by 0x27724C19: embree_geometry_setup_positions (s3d_scene_view.c:264)
==5265== by 0x277252FF: scene_view_setup_embree (s3d_scene_view.c:407)
==5265== by 0x2772920D: scene_view_sync (s3d_scene_view.c:968)
==5265== by 0x2772A37F: s3d_scene_view_create2 (s3d_scene_view.c:1151)
==5265== by 0x2772A2D9: s3d_scene_view_create (s3d_scene_view.c:1124)
==5265== by 0x220EB0: init_star_engine (triangle_surface.c:144)
==5265== by 0x22170C: setup_triangular_surface (triangle_surface.c:661)
==5265== by 0x188724: setup_atmosphere3D (cloud3d.c:1482)
==5265== by 0x12432B: uvspec (uvspec.c:179)
==5265== by 0x11DE1E: main (uvspec_lex.l:9563)
==5265== Address 0x32cb573c is 0 bytes after a block of size 108 alloc'd
==5265== at 0x25C6DE76: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5265== by 0x25C6DF91: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5265== by 0x29D7B5CF: mem_alloc_aligned (mem_allocator.c:141)
==5265== by 0x29D7BA7B: default_alloc_aligned (mem_allocator.c:330)
==5265== by 0x277076EF: darray_float_reserve (dynamic_array.h:168)
==5265== by 0x27707946: darray_float_resize (dynamic_array.h:207)
==5265== by 0x27708437: mesh_setup_positions (s3d_mesh.c:117)
==5265== by 0x2770BE7C: mesh_setup_indexed_vertices (s3d_mesh.c:500)
==5265== by 0x27742A21: s3d_mesh_setup_indexed_vertices (s3d_shape.c:343)
==5265== by 0x220D9A: set_attach_surf (triangle_surface.c:108)
==5265== by 0x220E94: init_star_engine (triangle_surface.c:140)
==5265== by 0x22170C: setup_triangular_surface (triangle_surface.c:661)
Let me know if you have troubles to reproduce and need an example.
Many thanks,
Fabian