Closed ExpertOfNil closed 4 weeks ago
I was able to reproduce the bug, but not idea why it's happening.
The segfault looks to be in the raylib header, but I can't figure out what about the raylib wrapper broke something.
// rlgl.h
// Vertex data management
//-----------------------------------------------------------------------------------------
// Load a new attributes buffer
unsigned int rlLoadVertexBuffer(const void *buffer, int size, bool dynamic)
{
unsigned int id = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
printf("Before %u %p\n", id, &id);
glGenBuffers(1, &id);
printf("After %u %p\n", id, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, size, buffer, dynamic? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
#endif
return id;
}
zig build run
Creating mesh...
Before 0 0000007eb1bff054
Segmentation fault at address 0x0
C:\Users\Me\AppData\Local\zig\p\1220d1c8697d41a42d4eaaf3f8709865534d1f3d6ad63f8a27500fa881380651a1c5\src\rlgl.h:3788:0: 0x5ebbe2 in rlLoadVertexBuffer (raylib.lib)
glGenBuffers(1, &id);
C:\Users\Me\AppData\Local\zig\p\1220d1c8697d41a42d4eaaf3f8709865534d1f3d6ad63f8a27500fa881380651a1c5\src\rmodels.c:1269:0: 0x51e90f in UploadMesh (raylib.lib)
mesh->vboId[RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION] = rlLoadVertexBuffer(vertices, mesh->vertexCount*3*sizeof(float), dynamic);
C:\Users\Me\AppData\Local\zig\p\1220d1c8697d41a42d4eaaf3f8709865534d1f3d6ad63f8a27500fa881380651a1c5\src\rmodels.c:2625:0: 0x527049 in GenMeshPlane (raylib.lib)
UploadMesh(&mesh, false);
C:\Users\Me\AppData\Local\zig\p\1220c3d910dbb1a252e36d6574ebff57c0a63310f4f519f723ee3dbe06ad287f8fd1\lib\raylib.zig:4520:29: 0x462586 in genMeshPlane (OOEP.exe.obj)
return cdef.GenMeshPlane(width, length, @as(c_int, resX), @as(c_int, resZ));
^
C:\Users\Me\Documents\Git\OSSP\src\main.zig:58:42: 0x4616e4 in main (OOEP.exe.obj)
const mesh: rl.Mesh = rl.genMeshPlane(3840.0, 2160.0, 2, 2);
After digging a bit, glGenBuffers
is an openGL function, so I found it a bit strange there is a segfault there, using GPT apparently you do need to initialise the openGL context, and that happens within the rl.initWindow(
function. So moving that above the function call has fixed this segfault, but the program still has a bug, a blank screen for a few seconds then it crashes. So maybe you have a slightly different version on your end, or can compare to the C version for any other differences.
Thanks for the clues! I'll do some further comparisons and let you know if I find anything meaningful.
@znichola I put together a minimal example in C, Odin, and Zig. The C and Odin versions work fine, but the Zig version still fails (seemingly) during mesh generation. Something I did note is that raylib-zig
is using v5.5-dev, and the others are using v5.0 or v5.1-dev.
Using the same static libraries as my C variant compiled and ran fine, as well: https://github.com/ExpertOfNil/shader-play-minimal/tree/raw-rl-bindings
I'm porting a toy program I wrote in C (which works) to Zig and I'm getting a segmentation fault when I use
rl.genMeshPlane
:Output: