Skip to content

Fix waitFor not being implemented in lwjgl3 - #1048

Closed
tlf30 wants to merge 5 commits into
jMonkeyEngine:masterfrom
tlf30:waif-for-fix-lwjgl3
Closed

Fix waitFor not being implemented in lwjgl3#1048
tlf30 wants to merge 5 commits into
jMonkeyEngine:masterfrom
tlf30:waif-for-fix-lwjgl3

Conversation

@tlf30

@tlf30 tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor

So this seems like a big bug to me. I noticed that for some reason for the lwjgl 3 display context, the wait for was being ignored. There was a comment about a OSX fix, but with no information to go with it.

It seems to me that it is important for the waitFor to work, as it broke one of my applications that I was swapping from lwjgl2 to lwjgl3.

Does anyone know about this, or can provide insight into this?
This PR adds support for the waitFor in the same way that it was implemented in lwjgl2.
Also, if someone can confirm that this still works for them, that would be great.

PS: I hope I opened this PR correctly. If not I'm sorry in advance, every project has their preferred way of doing things.

Thank you,
Trevor Flynn

@tlf30

tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor Author

Not sure why the compare is not working correctly, only a couple lines changed...

@pspeed42

Copy link
Copy Markdown
Contributor

Probably you checked the file in with a different line ending. You may want to fix it as it's harder to see what actually changed.

@tlf30

tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor Author

Ah, I did not think about that. I'll fix it right now

@MeFisto94

Copy link
Copy Markdown
Member

FYI: https://github.blog/2018-05-01-ignore-white-space-in-code-review/
As pointed out below:
https://github.com/jMonkeyEngine/jmonkeyengine/pull/1048/files?w=1 also works

Note though that this still means we don't want "unclean" commits, as git tools might not ignore whitespaces and it's not clear which line ending/white space is the correct one.

Actually we should have linters/auto formatters as bots which comment on the PRs :)

@pspeed42

Copy link
Copy Markdown
Contributor

Or I sort of remember that it's possible to set line ending style on the repo or something... but that may make a whole bunch of source change because I don't believe it's consistent right now.

}

// NOTE: this is required for Mac OS X!
mainThread = Thread.currentThread();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what they are doing here and your method might break OSX support. Do some research on the hub please:
For OpenGL to work on Mac OS X it has to be started on the MainThread (which is the Thread the Application is started with, hence Thread.currentThread(). Your change destroys this. So it changes semantics.

WaitFor() might be useless because if you're already on the MainThread, you don't have to wait for it.
But it's an inconsistent behavior as lwjgl2 probably returns from the Application#start() call.
So a proper solution would be to document this or provide AppSettings to enable "forking", unless on OS X.
Or always use the CurrentThread, which to me is expectable if you hand control over to a framework.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to check if we are on OS X, then ignore the wiatFor. Also, does this mean that jme-lwjgl does not work on OS X?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is probably better, but I don't like it cause we'd have an OS dependant behavior then. I need to triple check that with lwjgl3, but as pointed out in #1193, VR has the same problem.

lwjgl2 however is capable of working on Mac OS X, which is related to the way it creates the window, I guess. Which is why we need to double check things and re-check the above assumption. I guess I need to try that on mac.

@tlf30
tlf30 force-pushed the waif-for-fix-lwjgl3 branch from 02cb54d to 4c86de0 Compare March 19, 2019 18:30
@tlf30

tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor Author

OK I have line endings fixed.

@tlf30

tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor Author

I would also like to note, that with LWJGL3, the headless context does return from start, which makes inconsistent behavior within the library itself.

@tlf30

tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor Author

OK, I just made a new commit. If waitFor is false, we run it on the main thread, otherwise, create a new thread. How about this solution?

@tlf30

tlf30 commented Mar 19, 2019

Copy link
Copy Markdown
Contributor Author

I do not have an apple computer, perhaps someone can test this?

@tlf30

tlf30 commented Mar 20, 2019

Copy link
Copy Markdown
Contributor Author

Just for documentation purpose, it was issue #433 that has the os x workaround info in it.

I have tested this proposed fix on my apps in windows, and I think it is ready but still needs tested in OS X. Any thoughts?

Thanks,
Trevor

@stephengold

Copy link
Copy Markdown
Member

Perhaps someone at the Forum would be willing to help us.

@Jeddic

Jeddic commented Apr 24, 2019

Copy link
Copy Markdown
Contributor

What exactly needs to be tested here? Is there some sort of test case to run?

@tlf30

tlf30 commented Apr 24, 2019

Copy link
Copy Markdown
Contributor Author

Just needs to be tested in a working application. Behavior should not change on OS X and that needs to be confirmed.

@MeFisto94

MeFisto94 commented Jan 11, 2020

Copy link
Copy Markdown
Member

Sorry for not reacting on this so long, I've just re-encountered this without the PR:
Currently, start() blocks until the application is closed, where it should:

  • Return instantly in the case of start(false)
  • Return as soon as the context is initialized in start(true).

I think your PR addresses the first point, but not the second one? [Which would at least be one improvement]

Edit: Forgot to say: the current behavior is required on Mac OS X regardless of the waitFor, since start() may NOT return, since we have to run on that thread.

Edit2: Here's a read up on why things are like they are: LWJGL/lwjgl3#311
I'll have to give it a read too, but it seems to be related to GlfwWindow, so maybe we can rework lwjgl3 so it just creates the render thread after the window is created (which would be the true waitFor=true behavior anyway)

Edit3: It is a problem. Most GLFW functions are required to be called from the main thread, which also has to be the first thread on macOS (see -XstartOnFirstThread). The javadoc of each function mentions if it's callable from any thread or only from main. This is neither an LWJGL nor a GLFW limitation; it's the only way to write a usable cross-platform windowing system. Your application may work on Windows, but will certainly fail on macOS. Note that this applies to GLFW functions only; OpenGL contexts can be made current in other threads and those threads can be used for rendering.

So a separate rendering thread is possible, but then all calls like resizing the window will fail as the main thread aka. first thread has already exited...

Edit 4: https://www.glfw.org/docs/latest/group__input.html#ga1caf18159767e761185e49a3be019f8d
I think we can't merge this PR as is, as in the current form, not even the keyboard input can be run off-thread, so it would need to happen before creating the render thread etc.

@MeFisto94

Copy link
Copy Markdown
Member

Okay, so after Review in #1248, I think this PR cannot work like this, it needs a more complicated approach, which I didn't manage to do.

The thing is: the GLFW docs say you must not access these functions (most of initInThread) from outside the main thread. They say it's because some platforms require that. I don't know if that will lead to exceptions or work on all those other platforms (i.e. not working on mac os soley), but it's discouraged by the docs.

I think we have to accept that behavior, because everything else gives additional problems, starting with context restarts and a proper app shutdown.

For now I'd recommend discussing on #1248 about our options.

@tlf30

tlf30 commented Jan 17, 2020

Copy link
Copy Markdown
Contributor Author

@MeFisto94 thank you for looking into this. I am not sure why the documentation is so unclear on LWJGL's side on this, but I have been using a different thread for quite some time now in my applications be using this approached:

                Thread t = new Thread() {
                    @Override
                    public void run() {
                        jme.start();
                    }
                };
                t.start();

This was taken right from a working application. I have only tested it in windows though. Using JME3 LWJGL3.

Perhaps this only works in Windows...?

@MeFisto94

Copy link
Copy Markdown
Member

why the documentation is so unclear on LWJGL's side on this

Because it's a limitation of glfw and their Docs state this clearly.

Perhaps this only works in Windows...?

And Linux, but it seems to just crash on Mac OS. So while it's forbidden you can get away with that on most platforms.
But as you said in your comment: having start() blocking isn't that bad.

@stephengold

Copy link
Copy Markdown
Member

I'm confused about the status of this PR. Does it need code changes? Testing? Or just integration?

@Jeddic

Jeddic commented Apr 24, 2020

Copy link
Copy Markdown
Contributor

From what I've gathered, there isn't a good way to maintain the previous functionality with lwjgl3 on all platforms. Creating a new thread will crash on OSX. So this isn't really a simple fix, because we can't match the lwjgl2 functionality.

@MeFisto94

Copy link
Copy Markdown
Member

What Jeddic says, we're violating the docs/contract when creating a new thread. I tried a different way but failed.

It's a problem: Currently one cannot use start(false) and thus the thread calling into app.start() is locked forever, which is bad for Editors/SDKs. With this PR, that would work on Windows and Linux but crash the JVM on Mac OS X.

I don't know if some (if Mac OS waitFor == true) would work, but we might need to find a solution to the glfw problems anyway

@tlf30

tlf30 commented Apr 25, 2020

Copy link
Copy Markdown
Contributor Author

That is what this PR does. It keeps Mac os on main thread, and forks for everything else.

EDIT: @MeFisto94 I did not look at the username to realize I was replying to you. I know you know what this PR is. Sorry about that.

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

Successfully merging this pull request may close these issues.

6 participants