LWJGL / lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.
https://www.lwjgl.org
BSD 3-Clause "New" or "Revised" License
4.67k stars 629 forks source link

glBufferData mem error #908

Closed TheoW03 closed 11 months ago

TheoW03 commented 11 months ago

Version

3.2.3

Platform

Windows x86

JDK

Java 17

Module

openGL

Bug description

glBufferData(int target, Buffer (FloatBuffer,Intbuffer...) data, int usage);

has a memory error.

       int[] buffer = new int[2];
        int location = glGetAttribLocation(shaderProgram, "position");

        glGenBuffers(buffer);
        glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);

        glVertexAttribPointer(location, 3, GL_FLOAT, false, 0, 0);
        glBufferData(GL_ARRAY_BUFFER, FloatBuffer.wrap(vertices),GL_STATIC_DRAW);

here is the full usage that caused this.

vertices - being an array of 12 floats between -1,1. its the points to draw a square.

shaderProgram - compiled and linked shader program.

Stacktrace or crash log output

Events (20 events):
Event: 0.163 loading class jdk/internal/reflect/UnsafeQualifiedStaticObjectFieldAccessorImpl done
Event: 0.166 loading class java/nio/BufferOverflowException
Event: 0.166 loading class java/nio/BufferOverflowException done
Event: 0.166 loading class java/nio/InvalidMarkException
Event: 0.166 loading class java/nio/InvalidMarkException done
Event: 0.166 loading class java/nio/BufferUnderflowException
Event: 0.166 loading class java/nio/BufferUnderflowException done
Event: 0.184 loading class java/lang/NoSuchFieldError
Event: 0.184 loading class java/lang/NoSuchFieldError done
Event: 0.188 loading class jdk/internal/reflect/UnsafeQualifiedStaticIntegerFieldAccessorImpl
Event: 0.188 loading class jdk/internal/reflect/UnsafeQualifiedStaticIntegerFieldAccessorImpl done
Event: 0.457 loading class jdk/internal/misc/ScopedMemoryAccess$Scope
Event: 0.457 loading class jdk/internal/misc/ScopedMemoryAccess$Scope done
Event: 0.461 Loaded shared library
openGL.dll
Event: 0.593 loading class jdk/internal/reflect/UnsafeQualifiedLongFieldAccessorImpl
Event: 0.594 loading class jdk/internal/reflect/UnsafeQualifiedFieldAccessorImpl
Event: 0.594 loading class jdk/internal/reflect/UnsafeQualifiedFieldAccessorImpl done
Event: 0.594 loading class jdk/internal/reflect/UnsafeQualifiedLongFieldAccessorImpl done
Event: 0.646 loading class java/nio/HeapFloatBuffer
Event: 0.646 loading class java/nio/HeapFloatBuffer done
TheoW03 commented 11 months ago

one of the solutions I have is to may be have a size param instead of just sending the Buffer in with no size

the error doesn't occur with

glBufferData(int target, Array (float[],int[]...) data, int usage);

octylFractal commented 11 months ago

You need to use a direct buffer, see https://github.com/LWJGL/lwjgl3-wiki/wiki/1.3.-Memory-FAQ#how-do-i-allocate-and-deallocate-buffers.

TheoW03 commented 11 months ago

the problem still continues when i implement it, like this.

try (MemoryStack stack = stackPush()) {
            IntBuffer ip = stack.callocInt(1);
            glGenBuffers(ip);
            buffer[0] = ip.get(0);
        } // stack automatically popped, ip memory automatically reclaimed
        glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);

        glVertexAttribPointer(location, 3, GL_FLOAT, false, 0, 0);
        glBufferData(GL_ARRAY_BUFFER, FloatBuffer.wrap(vertices),GL_STATIC_DRAW);
TheoW03 commented 11 months ago

i did run the debugger, and it does crash around glBufferData.

octylFractal commented 11 months ago

Your code is still using FloatBuffer.wrap, which allocates a heap, and not direct, buffer.

TheoW03 commented 11 months ago

that worked.