Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rmodels] Errors during LoadMesh on MacOS #4346

Closed
TillWege opened this issue Sep 29, 2024 · 9 comments
Closed

[rmodels] Errors during LoadMesh on MacOS #4346

TillWege opened this issue Sep 29, 2024 · 9 comments

Comments

@TillWege
Copy link

Issue description

Since #4321 uploading Meshes on MacOS will randomly fail and cause the running program to crash.

Environment

  • MacBook Pro M1 16gb running MacOS 15

Issue Screenshot

image

Code Example

#define MODEL_FOLDER "models/"

int main()
{
	const int screenWidth = 1280;
	const int screenHeight = 720;

	InitWindow(screenWidth, screenHeight, "Window");

	std::vector<Model> models;

	for (const auto& entry : std::filesystem::directory_iterator(MODEL_FOLDER)) {
		std::string path = entry.path().string();
		std::string fileName = entry.path().filename().string();
		if (IsFileExtension(fileName.c_str(), ".obj")) {
			Model model = LoadModel(path.c_str());
			models.push_back(model);
		}
	}
}

Running this leads to it crashing within the first ~4 models it tries to load. It doesn't seem to change whether its .obj or .gltf files.

@JeffM2501
Copy link
Contributor

It's possible that the mac may only support 8 VBOs, the code should check GL for the max and not try to load them all if possible.
We may need to tie the GPU skinning feature to a flag that apple platforms do not set, or perhaps remove one of the other less used VBOs

@deDug
Copy link

deDug commented Sep 29, 2024

This could be the same problem as #4343. When loading mesh a bad define can mean the wrong amount of memory is allocated for VBO indices.
You could try changing MAX_MESH_VERTEX_BUFFERS in config.h to 9 and see if that fixes it.

@TillWege
Copy link
Author

Yeah you seem to be right. When I change it to 7 the crash doesnt appear.

@JeffM2501
Copy link
Contributor

I just tested on an intel mac, and can verify that yes, the mac only supports 8 VBOs.
We will have to not support GPU meshes on the mac, and move those buffers to the end.
This includes meshes that don't even use animations, the change to use 9 VBOs just strait up breaks meshes on Mac OS.

@JeffM2501
Copy link
Contributor

PR #4348 should disable this feature for MacOS

@stevegwh
Copy link

stevegwh commented Sep 30, 2024

@raysan5 @JeffM2501 Not sure if this is the place to put this but: GPU Skinning (#4321) works fine for me on macOS (Sonoma 14.6.1) (M1 MacBook Air 2020) after changing "MAX_MESH_VERTEX_BUFFERS" to 9 in config.h. This is using the example and model from "models_gpu_skinning.c" (I'll include it below, also). So, at least on my machine, macOS seems to support GPU skinning and all other models that I have tried load fine. Unless I've misunderstood, something.

I'm happy to test more things out if you want.

EDIT:
Here is some more info:

INFO: GL: Supported extensions count: 43
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Apple
INFO:     > Renderer: Apple M1
INFO:     > Version:  4.1 Metal - 88.1
INFO:     > GLSL:     4.10`
// models_gpu_skinning.c

#include "raylib.h"

#include "raymath.h"

#if defined(PLATFORM_DESKTOP)
    #define GLSL_VERSION            330
#else   // PLATFORM_ANDROID, PLATFORM_WEB
    #define GLSL_VERSION            100
#endif

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [models] example - GPU skinning");

    // Define the camera to look into our 3d world
    Camera camera = { 0 };
    camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
    camera.target = (Vector3){ 0.0f, 2.0f, 0.0f };  // Camera looking at point
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };      // Camera up vector (rotation towards target)
    camera.fovy = 45.0f;                            // Camera field-of-view Y
    camera.projection = CAMERA_PERSPECTIVE;         // Camera projection type

    // Load gltf model
    Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); // Load character model
    
    // Load skinning shader
    Shader skinningShader = LoadShader(TextFormat("resources/shaders/glsl%i/skinning.vs", GLSL_VERSION),
                                       TextFormat("resources/shaders/glsl%i/skinning.fs", GLSL_VERSION));
    
    characterModel.materials[1].shader = skinningShader;
    
    // Load gltf model animations
    int animsCount = 0;
    unsigned int animIndex = 0;
    unsigned int animCurrentFrame = 0;
    ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount);

    Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position

    DisableCursor();                    // Limit cursor to relative movement inside the window

    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        UpdateCamera(&camera, CAMERA_THIRD_PERSON);
        
        // Select current animation
        if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount;
        else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount;

        // Update model animation
        ModelAnimation anim = modelAnimations[animIndex];
        animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
        UpdateModelAnimationBoneMatrices(characterModel, anim, animCurrentFrame);
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);
            
                // Draw character
                characterModel.transform = MatrixTranslate(position.x, position.y, position.z);
                UpdateModelAnimationBoneMatrices(characterModel, anim, animCurrentFrame);
                DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform);

                DrawGrid(10, 1.0f);
            EndMode3D();

            DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadModelAnimations(modelAnimations, animsCount);
    UnloadModel(characterModel);         // Unload character model and meshes/material
    UnloadShader(skinningShader);
    
    CloseWindow();              // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

@JeffM2501
Copy link
Contributor

@raysan5 @JeffM2501 Not sure if this is the place to put this but: GPU Skinning (#4321) works fine for me on macOS (Sonoma 14.6.1) (M1 MacBook Air 2020) after changing "MAX_MESH_VERTEX_BUFFERS" to 9 in config.h. This is using the example and model from "models_gpu_skinning.c" (I'll include it below, also). So, at least on my machine, macOS seems to support GPU skinning and all other models that I have tried load fine. Unless I've misunderstood, something.

I'm going to try this with the config fix and see if that is the issue.

@stevegwh
Copy link

stevegwh commented Oct 1, 2024

@JeffM2501 Cool. You can see here for the logic behind it: #4349
HarryDC's comment (can't tag him, for some reason): "This is the model i used to check, the issue is most likely a MAX_MESH_VERTEX_BUFFERS definition in config.h line 231 that is 7, whereas the value needs to be 9 as defined in line 123 in rmodels.c"

Once I changed MAX_MESH_VERTEX_BUFFERS to 9 in config.h, models loaded fine and I've been using GPU skinning on macOS without issue since then.

@JeffM2501
Copy link
Contributor

Yeah that was the issue, it does work on the mac. I have summited PR#4353 that should get it all in line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants