LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: spasi on March 13, 2010, 00:45:44

Title: String and primitive parameters/return values - Feedback request
Post by: spasi on March 13, 2010, 00:45:44
Support for String arguments and return values has been added to LWJGL:

- Existing methods have been overloaded, your current code will run unmodified.
- Return values are Strings, input values are CharSequences, so you can also use String, StringBuilder or any other implementation.
- Support for CharSequence[] arguments is also included.
- The implementation uses ThreadLocal buffers internally, so it's multi-context ready.

Some examples:

glShaderSource(int shader, ByteBuffer string); // Original
glShaderSource(int shader, CharSequence string); // New - single source
glShaderSource(int shader, CharSequence[] strings); // New - multiple sources

void glGetShaderInfoLog(int shader, IntBuffer length, ByteBuffer infoLog); // Original
String glGetShaderInfoLog(int shader, int maxLength); // New

----------------------

EDIT: I have extended this API feature to primitive types. Functions with Buffer arguments that often read or write a single value in those Buffers have been overloaded to accept or return that single primitive value. Examples:

int texID = glGenTextures();
glDeleteTextures(texID);

int maxTexUnits = glGetInteger(GL_MAX_TEXTURE_UNITS);
float aniso = glGetFloat(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT);

if ( glGetShader(shaderID, GL_COMPILE_STATUS) == GL11.GL_FALSE ) throw new RuntimeException();

----------------------

EDIT2: Added a javadoc comment to all the new methods that says which GL call is being overloaded.

----------------------

I would very much appreciate it if you could download a nightly build (https://www.newdawnsoftware.com/hudson/view/LWJGL/) and try it out in your projects. Let me know if something doesn't work or if I have missed any functions that could be overloaded.
Title: Re: String support added - Feedback request
Post by: princec on March 14, 2010, 13:28:18
Fancy adding conveniences for glGetInteger etc. too?

Cas :)
Title: Re: String support added - Feedback request
Post by: spasi on March 14, 2010, 15:15:53
It's what I'm doing the past two days. ;)
Title: Re: String and primitive parameters/return values - Feedback request
Post by: spasi on March 15, 2010, 00:01:11
Support for primitive parameters and return values has been added, I've updated the first post with examples.
Title: Re: String and primitive parameters/return values - Feedback request
Post by: Rene on March 15, 2010, 13:49:01
Wow, that's all looking great. I'll check the nightly when I have a moment
Title: Re: String and primitive parameters/return values - Feedback request
Post by: Rene on March 16, 2010, 10:41:27
Quote from: Rene on March 15, 2010, 13:49:01
Wow, that's all looking great. I'll check the nightly when I have a moment

Right, I just checked some of the new functions, and it all works like a charm. This cleans up my code quite a bit! Thanks!
Title: Re: String and primitive parameters/return values - Feedback request
Post by: Fool Running on March 16, 2010, 12:50:20
I agree, this looks like a welcome change. :)
Title: Re: String and primitive parameters/return values - Feedback request
Post by: ryanm on March 16, 2010, 13:35:35
Lovely stuff. Any chance of this rolling across to the OpenAL side too?
Title: Re: String and primitive parameters/return values - Feedback request
Post by: spasi on March 16, 2010, 18:03:40
I've added some AL functions, should be available on the next nightly. They are only 6 though, 4 Gen/Delete ones and the two alSource(Un)QueueBuffers. The rest of the API already has alternate function versions for single values, unless I missed something.
Title: Re: String and primitive parameters/return values - Feedback request
Post by: ryanm on March 17, 2010, 13:29:52
Even lovelier stuff! GLUtil.scratch buffer begone!
Title: Re: String and primitive parameters/return values - Feedback request
Post by: delt0r on March 18, 2010, 09:15:56
Great change. Hope this doesn't complicated the auto JNI stuff for you devs however. We don't want to give you too much work and put you all off the project.
Title: Re: String and primitive parameters/return values - Feedback request
Post by: EvilOne on March 23, 2010, 07:59:27
This is damn great.

Would be nice to have some additional stuff for getActiveAttrib/Uniform to get rid of those pesky IntBuffers... glGetActiveAttribType and glGetActiveAttribSize maybe?

Cheers,
E1.
Title: Re: String and primitive parameters/return values - Feedback request
Post by: spasi on March 23, 2010, 12:44:42
Quote from: EvilOne on March 23, 2010, 07:59:27Would be nice to have some additional stuff for getActiveAttrib/Uniform to get rid of those pesky IntBuffers... glGetActiveAttribType and glGetActiveAttribSize maybe?

Done, added glGetActiveUniformSize, glGetActiveUniformType, glGetActiveAttribSize, glGetActiveAttribType. Could you please test it when the next build is up? I'm not sure how it's going to behave with a 0 maxlength passed to the native call. Btw, for uniforms a better choice would be glGetActiveUniforms(int program, int uniformIndex, int pname) in GL31 and ARB_uniform_buffer_object.

I've also added a javadoc comment to all the new methods, so that it's easy to tell which GL method it overloads. It can be quite messy without it in some cases.
Title: Re: String and primitive parameters/return values - Feedback request
Post by: elFarto on March 26, 2010, 19:54:01
Hi

Thanks for the new methods, they clean up the code nicely. Any chance of getting glGetActiveUniformBlock added aswell?

Thanks & Regards
elFarto
Title: Re: String and primitive parameters/return values - Feedback request
Post by: spasi on March 26, 2010, 20:25:15
Thanks, glGetActiveUniformBlock was ok in ARB_uniform_buffer_object but missed it in GL31. Gonna add it when I commit the APPLE extensions.
Title: Re: String and primitive parameters/return values - Feedback request
Post by: elFarto on March 29, 2010, 11:53:37
Thanks for that.

Is there any plan for improving the native implementation of those methods? Currently you use the original method that expects a IntBuffer to be passed in, but when dealing with, say glGetTextures, you can write the native implementation like:

GLint foo;
glGenTextures(1, &foo);
return foo;

This would presumably be faster/more efficient as you wouldn't need to manage the buffers.

Regards
elFarto
Title: Re: String and primitive parameters/return values - Feedback request
Post by: spasi on March 29, 2010, 13:21:00
We already have support for this (see the 6 new methods in OpenAL) and I did consider doing it for OpenGL too. The problem is that it would require a new native implementation for each alternative method and, based on some quick calculations, it would add 80-90 kb to LWJGL's native binaries. Along with the fact that all these methods are not performance sensitive, I decided it wasn't worth doing. LWJGL is already getting bloated with all these new extensions coming out every 6 months, I thought it will be important for web-deployed apps that LWJGL stays small in size.

In any case, this was a personal decision and can be changed easily if the community decides otherwise.