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

Add single output buffer support #179

Closed
ShadowLordAlpha opened this issue Apr 12, 2016 · 3 comments
Closed

Add single output buffer support #179

ShadowLordAlpha opened this issue Apr 12, 2016 · 3 comments

Comments

@ShadowLordAlpha
Copy link

There are several methods that output values to different buffers. In C these can be simplified to output to a single array using pointer math and simply telling it to place the value in different parts of the array however there is no way of doing this directly in LWJGL however it can be done by simply calling the native version of the methods and passing in the modified address.

For example this method in GLFW

public static void glfwGetVersion(IntBuffer major, IntBuffer minor, IntBuffer rev) {
    if ( CHECKS ) {
        if ( major != null ) checkBuffer(major, 1);
        if ( minor != null ) checkBuffer(minor, 1);
        if ( rev != null ) checkBuffer(rev, 1);
    }
    nglfwGetVersion(memAddressSafe(major), memAddressSafe(minor), memAddressSafe(rev));
}

Would have an override that would look similar to this

public static void glfwGetVersion(IntBuffer version) {
    if ( CHECKS ) {
        if ( version != null ) checkBuffer(version, 3);
    }
    nglfwGetVersion(memAddressSafe(version), memAddressSafe(version) + 1, memAddressSafe(version) + 2);
}

This would return all three values in one buffer sequentially without needing to use the native type method directly.

@httpdigest
Copy link
Member

You can create slices/views of the same Buffer and use those as the separate buffer arguments:

IntBuffer major = ...;
IntBuffer minor = major.slice();
minor.position(1);
IntBuffer rev = major.slice()
rev.position(2);

It looks a bit ugly, but works. :)
If you don't like to do that, then I find that those 'n' methods are exactly the right thing to use when wanting to do this manual Buffer aliasing. Also, in general for every other method that happens to take two or more NIO buffers as out parameters, how would LWJGL know that you wanted to store all out values in the same buffer?

@Spasi
Copy link
Member

Spasi commented Apr 12, 2016

This case is too rare to deserve special support in the generator, there are maybe 1 or 2 such functions in each binding. However, I agree that it's annoying in Java.

Maybe new memSlice methods in MemoryUtil would help?

MemoryStack stack = stackPush();
try {
    // Multiple buffers
    IntBuffer major = stack.mallocInt(1);
    IntBuffer minor = stack.mallocInt(1);
    IntBuffer rev = stack.mallocInt(1);

    glfwGetVersion(major, minor, rev);

    // Unsafe
    IntBuffer nv = stack.mallocInt(3);
    long a = memAddress(nv);
    nglfwGetVersion(a, a + 4, a + 8);

    // If we add new memSlice methods to MemoryUtil
    IntBuffer v = stack.mallocInt(3);
    glfwGetVersion(v, memSlice(v, 1, 1), memSlice(v, 2, 1));
} finally {
    stack.pop();
}

@ShadowLordAlpha
Copy link
Author

I like the memSlice idea more and your right it is a bit rare so it probably shouldn't have its own thing in the generator though I do think a clear, clean way of splitting a single buffer so it can be used as multiple arguments should be included (memSlice would do this perfectly)

@Spasi Spasi closed this as completed in 6e43b64 Apr 12, 2016
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

3 participants