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

Possibility to make bindings mockable in LWJGL N (4?) #1006

Open
xxDark opened this issue Oct 1, 2024 · 1 comment
Open

Possibility to make bindings mockable in LWJGL N (4?) #1006

xxDark opened this issue Oct 1, 2024 · 1 comment

Comments

@xxDark
Copy link

xxDark commented Oct 1, 2024

Question

This is not a question about current major version of LWJGL, but rather a future version (4 or perhaps later?..)
Would it be possible to make bindings a non-utility classes?
For example:

public interface Binding<C, T> {

	T create(C context);
}

public interface GL11 {
	
	void glLoadIdentity();
	
	void glPushMatrix();
	
	void glPopMatrix();
	
	...
}
Binding<GlContext, GL11> GL11 = GL11C::new;
Binding<GlContext, GL$N> GL$N = GL${N}C::new;

Java provides a way to import static fields, so the old "singleton" style could still be used for developers who don't want to maintain the binding instance or they are sure that it is safe to store it in a static final field.
This:

  • Eliminates LWJGL-specific thread local variable stored in JNIEnv function table;
  • Allows developers to implement a layer on top of GL${N}C (or any binding in general);
  • LWJGL can utilize this style of the API to generate implementation of bindings with unsupported methods stubbed, instead of doing this.

HotSpot is good at inlining virtual calls, thanks to type profiling, but the approach to structuring the API this way should of course be measured.

@Spasi
Copy link
Member

Spasi commented Oct 1, 2024

Hey @xxDark,

I don't know about the Binding interface, but yes, the current FFM-based binding generation prototype produces instances (implementations of bindings specified as interfaces), i.e. a non-static API. This is very much a work-in-progress, but you can see some examples in the ffm branch:

https://github.com/LWJGL/lwjgl3/blob/ffm/modules/samples/src/test/java22/org/lwjgl/demo/glfw/CustomBindings.java
https://github.com/LWJGL/lwjgl3/blob/ffm/modules/samples/src/test/java22/org/lwjgl/demo/system/windows/CustomKernel32.java

With this approach, a library like GLFW would still be exposed as a static API (the actual generated instance will be encapsulated internally). However, OpenGL(ES) bindings will become instance-based (gl.Clear(...) or ctx.glClear(...) instead of glClear(...)) and that instance will implicitly carry what is GLCapabilities now and will solve all the issues you mention above. A static GL(ES) API may also be available to simplify migration from lwjgl3, but it will suffer from thread-local lookup overhead on every call.

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

No branches or pull requests

2 participants