tazdij / raylib-pas

A complete header translation (binding) of the raylib 2.6.0-dev to Pascal. Without any funky helper functions for memory alignment issues. Inspired and partially based on the drezgames/raylib-pascal binding, however a little cleaner and more recent, with FPC 3.0.4 support.
28 stars 5 forks source link

macOS fpc build working but Access violation in animation_test at SetMaterialTexture #1

Closed ludoza closed 4 years ago

ludoza commented 4 years ago

Hi,

For fun trying to build and run animation_test.pas on my macos 10.14.6.

I can compile it if I include unit math and add this snippet:

{$IFDEF DARWIN}         SetExceptionMask([exDenormalized,exInvalidOp,exOverflow,exPrecision,exUnderflow,exZeroDivide]);
{$IFEND}  

But it seems to raise a access violation at line 31:

SetMaterialTexture(@model.materials[0], MAP_DIFFUSE, texture);

As seen below:

$ ./animation_test
INFO: Initializing raylib 2.5
INFO: Display device initialized successfully
INFO: Display size: 1440 x 900
INFO: Render size: 800 x 450
INFO: Screen size: 800 x 450
INFO: Viewport offsets: 0, 0
INFO: GPU: Vendor:   Intel Inc.
INFO: GPU: Renderer: Intel(R) HD Graphics 6000
INFO: GPU: Version:  4.1 INTEL-12.10.12
INFO: GPU: GLSL:     4.10
INFO: Number of supported extensions: 45
INFO: [EXTENSION] DXT compressed textures supported
INFO: [EXTENSION] Anisotropic textures filtering supported (max: 16X)
INFO: [EXTENSION] Debug Marker supported
INFO: [TEX ID 1] Texture created successfully (1x1 - 1 mipmaps)
INFO: [TEX ID 1] Base white texture loaded successfully
INFO: [SHDR ID 1] Shader compiled successfully
INFO: [SHDR ID 2] Shader compiled successfully
INFO: [SHDR ID 3] Shader program loaded successfully
INFO: [SHDR ID 3] Default shader loaded successfully
INFO: Internal buffers initialized successfully (CPU)
INFO: Internal buffers uploaded successfully (GPU)
INFO: OpenGL default states initialized successfully
INFO: [TEX ID 2] Texture created successfully (128x128 - 1 mipmaps)
INFO: [TEX ID 2] Default font loaded successfully
WARNING: [res/guy/guy.iqm] No meshes can be loaded, default to cube mesh
INFO: [VAO ID 2] Mesh uploaded successfully to VRAM (GPU)
WARNING: [res/guy/guy.iqm] No materials can be loaded, default to white material
INFO: [res/guy/guytex.png] Image loaded successfully (1024x1024)
INFO: [TEX ID 3] Texture created successfully (1024x1024 - 1 mipmaps)
An unhandled exception occurred at $00000001000E5587:
EAccessViolation: Access violation
  $00000001000E5587

Any suggestion?

ludoza commented 4 years ago

A colleague once mentioned my problem is I try to quickly to go full throttle with something new.

So here I worked on porting the c raylib basic window example to raylib-pas.

https://github.com/Morabaraba/raylib-pas/commit/81bd5a15e3a2e6333f096af8a3bc1c7abde358d4

It seems to compile and run on my macos and will try ubuntu next.

Thanks for this bindings!

ludoza commented 4 years ago

Tested the core basic window example on ubuntu as well. Just patched the raylib.pas to IFDEF check the library string:

https://github.com/Morabaraba/raylib-pas/commit/905c2c51d1e073e19f7cff28605a78364b7d6655

tazdij commented 4 years ago

Hey, thanks for the update. Can you send a Pull Request? I will merge the change in.

Also, I have found an issue with Shader Loading. I will have to create a few additional examples, to try and figure out what is happening. I have not thoroughly tested all of the raylib api with this binding.

tazdij commented 4 years ago

Oh, I had this issue with macOS and the Animation. It seems that the pre-built raylib 2.5.0 is not correct. I will mention this to @raysan5 when I get a chance.

This was fixed by cloning the source from github at the branch tagged 2.5.0 and compiling from source. This is binary I have included with the binding, maybe I can spend a little time in the next few days, compiling binaries for the other OSes.

ludoza commented 4 years ago

Thanks for accepting the pull request.

The latest example I added models_first_person_maze.pas still gives an access violation. I tried the homebrew dylib and built my own from source on macos but still the same result :/

$ ./models_first_person_maze
INFO: Initializing raylib 2.5
INFO: Display device initialized successfully
INFO: Display size: 1440 x 900
INFO: Render size: 800 x 450
INFO: Screen size: 800 x 450
INFO: Viewport offsets: 0, 0
INFO: GPU: Vendor:   Intel Inc.
INFO: GPU: Renderer: Intel(R) HD Graphics 6000
INFO: GPU: Version:  4.1 INTEL-12.10.14
INFO: GPU: GLSL:     4.10
INFO: Number of supported extensions: 45
INFO: [EXTENSION] DXT compressed textures supported
INFO: [EXTENSION] Anisotropic textures filtering supported (max: 16X)
INFO: [EXTENSION] Debug Marker supported
INFO: [TEX ID 1] Texture created successfully (1x1 - 1 mipmaps)
INFO: [TEX ID 1] Base white texture loaded successfully
INFO: [SHDR ID 1] Shader compiled successfully
INFO: [SHDR ID 2] Shader compiled successfully
INFO: [SHDR ID 3] Shader program loaded successfully
INFO: [SHDR ID 3] Default shader loaded successfully
INFO: Internal buffers initialized successfully (CPU)
INFO: Internal buffers uploaded successfully (GPU)
INFO: OpenGL default states initialized successfully
INFO: [TEX ID 2] Texture created successfully (128x128 - 1 mipmaps)
INFO: [TEX ID 2] Default font loaded successfully
INFO: [res/textures/cubicmap.png] Image loaded successfully (32x16)
INFO: [TEX ID 3] Texture created successfully (32x16 - 1 mipmaps)
INFO: [VAO ID 2] Mesh uploaded successfully to VRAM (GPU)
INFO: [res/textures/cubicmap_atlas.png] Image loaded successfully (256x256)
INFO: [TEX ID 4] Texture created successfully (256x256 - 1 mipmaps)
An unhandled exception occurred at $00000001000012E7:
EAccessViolation: Access violation
  $00000001000012E7

If i use the laz debugger the exception happens at this line:

  model.materials[0].maps[MAP_DIFFUSE].texture := texture;             // Set map diffuse texture
  //SetMaterialTexture(@model.materials[0], MAP_DIFFUSE, texture);

What is not correct in the pre-built raylibs. Because I believe we should rather try to mend pascal around the c lib.

I'm really impressed how easily the core_2d_camera.pas demo works.

So would just <3 to get more of the examples working on all major OS platforms.

ludoza commented 4 years ago

ps converted the models_billboard.dpr from drezgames/raylib-pascal examples to fpc and raylib-pas in my examples branch and it works on macos. Should I just keep the pull requests coming or rather send a big one at the end of the week as I make progress?

Morabaraba commented 4 years ago

@tazdij

Ok used lazarus debugger to look at the TModel structure when it comes back from LoadModelFromMesh and inside the c method before it returns.

In this gallery you will find 3 screenshots of the lazarus debug inspector(sorry imgur reversed the images):

You will notice TModel.Materiels = $1 and TModel.MaterielCount = 0 but the c struct had valid mem addr.

So I believe something is going wrong in our packed record.

Hope this helps.

tazdij commented 4 years ago

@Morabaraba Hey, thanks for looking into this. And I do believe there is an issue with the packed record. It seems in newer versions of the FPC 2.6+ they have changed the Default {$PACKRECORDS } setting to not longer Default to C ABI compatibility, I have ran a new test after adding this, and it looks to have fixed my issue on Windows, however I do not readily have access to MacOS to test this (yet), I will push a commit in a few minutes, with this change 'adding back the windows DLL too ;) which I will explain about, in a wiki page a little later.'

@ludoza, Please do keep the changes coming. And put them into a single big pull request, or one per. However you prefer to do that. I am just happy to receive some help with this.

Morabaraba commented 4 years ago

Yip seems you found the $PACKRECORDS issue! I actually changed TModel = record and added the {$PACKRECORDS C} switch and this allows models_first_person_maze to work on linux. Will test it on macos and give feedback.

Was so excited already did a pull request but seems {$PACKRECORDS C} might break some other packed record(s) https://github.com/tazdij/raylib-pas/pull/4 so will test it first a bit more.

tazdij commented 4 years ago

Hey, so there might be another issue I discovered, at least on my machine. I had compiled a 2.6-dev DLL however the library, at the moment is targeted at 2.5. There are some things that need to change to match the latest master branch. But Using the 2.5 DLL breaks the animation stuff. I think due to a bug in the raylib 2.5 release. I end up with a box being rendered, an access violation if I press space. So I am going to spend a little time today updating everything to match master. Then will try to figure out the issues (if any still exist) with the records vs. structs

ludoza commented 4 years ago

2.6-dev seems to break core_2d_camera. But I can also confirm the 2.5 animation bug you are experiencing.

But as mentioned if you changed TModel = record and wrap it around a {$PACKRECORDS C} switch you can SetMaterialTexture(s) https://github.com/Morabaraba/raylib-pas/blob/master/libs/raylib.pas#L374

Tested on macOS and Ubuntu.

ps. btw collision detection seems broken in models_first_person_maze.pas I think it is because I round() instead of trunct() but will look into it.

tazdij commented 4 years ago

Hey, I think the latest update to 2.6-dev (master) fixed all of the memory issues. However I don't have instructions for Linux or MacOS build in the readme. I am planning to build the 32 and 64 bit DLL and Static Lib for Windows, and include them in the repo. Especially, since we are now trailing Master, I want to make sure that we include the "Exact" version or commit of raylib that raylib-pas targets. What do you think of this strategy?

As for the collision, it seems to be either the round or maybe a 1 vs. 0 based Index issue, just from looking over the code briefly. I can try and fix that, for the next commit.

ludoza commented 4 years ago

Does core_2d_camera work with raylib 2.6-dev lib?

I think it is fine if raylib-pas master follow raylib master but maybe create a 2.5 branch were we try to get raylib-pas working against the 2.5 binaries, as well? For example the C ABI {$PACKRECORD C} problems.

I don't know if we should build raylib binaries and should rather try to use package manager like brew and link to the prebuilt binaries on the raylib github project. But I see atm it is outdated, so maybe coordinate with raylib to build newer binaries for popular platforms?

I'm planning to grind a bit on the examples over the weekend, but if you find the first person maze bug and fix it, it would be great. Will maybe also setup a ci pipeline on gitlab to build raylib for us on ubuntu and macos mojave, will see how my adventures go.

ps. I'm not against building binaries, I just don't want us to check it into git, that should be a package or a asset, git should mostly contain source code with some resources for examples or games.

tazdij commented 4 years ago

Yes core_2d_camera works with raylib 2.6-dev, now. I just committed and pushed changes to the example to fix issues with it.

I am going to stop replying to this thread now, since I think the main issue has been addressed, with the latest few updates. Please feel free to create a new PR or issue, for anything that you would like or think I should at least consider.

Again, thanks for the assistance with this binding.