This is the possible bug I was talking the last months. May be it only affects to Pascal because I can't reproduce it in C.
The problem is "polygon3d" generates a segmentation fault when rendering textured 3D polygons with "POLYTYPE_PTEX" modifier when the polygon is (almost) perpendicular to the screen plane. After some tests I did find it fails with angles <188, 74, 188>, <196, 53, 128>, <31, 62, 188> and its equivalents (<1212, 330, 1212>, <193, 53, 384>, etc).
The attached file is the simplest test program I used (included the wrapper units needed). Just change the initial angle values to reproduce the error faster. You can compile it with Free Pascal (included in several Linux distros). To compile, from the same directory, just execute "fpc -g test" ("-g" generates debug information). Of course you need Allegro installed.
I will perhaps give it a go this evening, but no promise Niunio.
I don't think it can be a real allegro bug if you can't reproduce it in C.
[edit] I've updated the attached file on the first message because I find an error in one of the loops. Anyway, it fails the same way[/edit]
I tried to reproduce it in C, so I translate my test program to C:
I think it's a perfect translation of the Pascal program, but I don't know why it's so chaotic.
And so, is the bug also visible in C with your chaotic (;-)) test program ?
ouch ^^
That code has an array overrun.
Duh!
And so, is the bug also visible in C with your chaotic (;-)) test program ?
Once fixed the problem gnolam pointed to it seems to work.
I don't think it can be a real allegro bug if you can't reproduce it in C.
I'm not sure. If you use Allegro.pas with Allegro 4.4.0 RC1 there is a similar problem with rotate_sprite, and previously with fixed-point maths, but there are no problems with RC2 and I didn't change my code.
May be there's a calling convention issue or something?
I can reproduce the crash in the pascal version. It doesn't crash under valgrind though, nor does valgrind flag anything strange.
EDIT: fpc is probably enabling overflow detection in float->int conversion. The problem is in cscan.h (FUNC_POLY_SCANLINE_PTEX):
for (x = w - 1; x >= 0; x -= 4) { long nextu, nextv, du, dv; PIXEL_PTR s; unsigned long color; fu += dfu; fv += dfv; fz += dfz; nextu = fu * z1; // here nextv = fv * z1; // here // ...snip... }
If I change nextu, nextv to int64_t instead of long, the SIGFPE goes away.
Next question is if this is the correct fix.
fpc is probably enabling overflow detection in float->int conversion. The problem is in cscan.h (FUNC_POLY_SCANLINE_PTEX):
for (x = w - 1; x >= 0; x -= 4) { long nextu, nextv, du, dv; PIXEL_PTR s; unsigned long color; fu += dfu; fv += dfv; fz += dfz; nextu = fu * z1; // here nextv = fv * z1; // here // ...snip... }
If I change nextu, nextv to int64_t instead of long, the SIGFPE goes away.
Next question is if this is the correct fix.
I confirm this change fixes the problem. Thanks for the tip, Peter.
Pascal is very strict about data typing, but I didn't know that "strictness" affects also to libraries written and compiled in other languages!
If it uses a CPU exception to handle the conversion, yeah it'll break anything
A small update:
I was testing the fix suggested by Peter. It seems to work but should be applied in more functions. I did changed it in all the ones that uses similar code in cscan.h file and now all examples works.
If it uses a CPU exception to handle the conversion, yeah it'll break anything
Didn't know about CPU exception. That explains a lot. Thanks.
So, next release will have this change, will not?
int64_t looks like a better choice than long here - it's in general only very rare situations where you would want to use long instead of int64_t. But when that code originally was written most likely we didn't have the fixed-width types yet.