Find in rtcore.zig all the definitions for the types and functions used in the example.
e.g. How is embree.RTCIntersectContext defined? We give a pointer to an allocation in stack, is the allocated size correct?
looking at RTCIntersectContext - i see some macro ifs and now i wonder, since they can change struct’s size
from what I could gather - zig created correct struct, at least in case of RTCIntersectContext since mentioned inside RTC_MAX_INSTANCE_LEVEL_COUNT should default to 1. actually, how did zig manage to generate it correctly? how did it know default value should be 1 ? (ok, my bad, found all defines in rtcore_config.h)
frame variable displays all the local variables s runs the next line print name where name is a variable name, prints the variable contents gui enters a curses ui, s also single steps there
Single step the lines, until the program crashes.
Try to print the variables before crashing.
Keep track of the various addresses, when debugging the code that does not crash vs the code that crash. (e.g. The address of scene minus the address of device remains the same?)
Write down all the stack variables addresses for the crash and not crash case (to print the address of a variable use: p &variable)
Check if the variables are aligned, the library expects the alignment to be 16 bytes, that means that the last hex digit in address must be 0.
Maybe when the stack size is increasing, by adding more variables and/or calls that return values, something is changing in the placement of the other variables.
Note the differences, see also what happens when you remove the @alignCast.
EDIT: see what happens if you place align(16) after * or ] in variable declarations.
e.g.
i’ve checked that vertex_buff and index_buff are 16-aligned, and difference between them stays the same,
BUT
address of rayhit variable on the stack was 16-aligned when program run, and not aligned when it crashed.
so changing it’s declaration to
var rayhit: embree.RTCRayHit align(16) = std.mem.zeroes(embree.RTCRayHit);
prevented the crash!
Have not tested anything else, but at least all cases above are not segfaulting any more
What is happening:
There is a define macro RTC_ALIGN(x) called with x=16.
This is translated by the c preprocessor to __attribute__((aligned(16))) that means: the alignment must be at least 16 bytes.
Unfortunately zig translate-c does not work correctly and ignores the alignment directive.
Workaround:
If you have RTC_ALIGN(16) in the C declaration you put align(16) in zig variable type.