From 6eb89d2cbffed5e80b2d85f03ccfd523e591450e Mon Sep 17 00:00:00 2001 From: ah-OOG-ah <75745146+ah-OOG-ah@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:40:03 -0500 Subject: [PATCH] Reduce buffer allocations (#190) --- src/main/java/org/lwjglx/openal/AL.java | 70 ++++++++-------- src/main/java/org/lwjglx/opengl/Display.java | 81 +++++++++++-------- src/main/java/org/lwjglx/opengl/GL15x.java | 13 +-- src/main/java/org/lwjglx/opengl/GL20x.java | 27 ++++--- src/main/java/org/lwjglx/util/glu/MipMap.java | 13 ++- 5 files changed, 114 insertions(+), 90 deletions(-) diff --git a/src/main/java/org/lwjglx/openal/AL.java b/src/main/java/org/lwjglx/openal/AL.java index af382015..88909443 100644 --- a/src/main/java/org/lwjglx/openal/AL.java +++ b/src/main/java/org/lwjglx/openal/AL.java @@ -1,10 +1,12 @@ package org.lwjglx.openal; +import static org.lwjgl.system.MemoryStack.stackPush; + import java.nio.IntBuffer; import org.lwjgl.openal.ALC10; import org.lwjgl.openal.ALCCapabilities; -import org.lwjglx.BufferUtils; +import org.lwjgl.system.MemoryStack; import org.lwjglx.LWJGLException; import org.lwjglx.Sys; @@ -32,50 +34,52 @@ public static void create(String deviceArguments, int contextFrequency, int cont public static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized, boolean openDevice) throws LWJGLException { - IntBuffer attribs = BufferUtils.createIntBuffer(16); + try (MemoryStack stack = stackPush()) { + IntBuffer attribs = stack.mallocInt(16); - attribs.put(org.lwjgl.openal.ALC10.ALC_FREQUENCY); - attribs.put(contextFrequency); + attribs.put(org.lwjgl.openal.ALC10.ALC_FREQUENCY); + attribs.put(contextFrequency); - attribs.put(org.lwjgl.openal.ALC10.ALC_REFRESH); - attribs.put(contextRefresh); + attribs.put(org.lwjgl.openal.ALC10.ALC_REFRESH); + attribs.put(contextRefresh); - attribs.put(org.lwjgl.openal.ALC10.ALC_SYNC); - attribs.put(contextSynchronized ? org.lwjgl.openal.ALC10.ALC_TRUE : org.lwjgl.openal.ALC10.ALC_FALSE); + attribs.put(org.lwjgl.openal.ALC10.ALC_SYNC); + attribs.put(contextSynchronized ? org.lwjgl.openal.ALC10.ALC_TRUE : org.lwjgl.openal.ALC10.ALC_FALSE); - ///////////////////////////////////////////// - // HRTF - if (!Config.OPENAL_ENABLE_HRTF) { - attribs.put(org.lwjgl.openal.SOFTHRTF.ALC_HRTF_SOFT); - attribs.put(org.lwjgl.openal.ALC10.ALC_FALSE); + ///////////////////////////////////////////// + // HRTF + if (!Config.OPENAL_ENABLE_HRTF) { + attribs.put(org.lwjgl.openal.SOFTHRTF.ALC_HRTF_SOFT); + attribs.put(org.lwjgl.openal.ALC10.ALC_FALSE); - attribs.put(org.lwjgl.openal.SOFTHRTF.ALC_HRTF_ID_SOFT); - attribs.put(0); - } - ///////////////////////////////////////////// + attribs.put(org.lwjgl.openal.SOFTHRTF.ALC_HRTF_ID_SOFT); + attribs.put(0); + } + ///////////////////////////////////////////// - attribs.put(org.lwjgl.openal.EXTEfx.ALC_MAX_AUXILIARY_SENDS); - attribs.put(4); + attribs.put(org.lwjgl.openal.EXTEfx.ALC_MAX_AUXILIARY_SENDS); + attribs.put(4); - attribs.put(0); - attribs.flip(); + attribs.put(0); + attribs.flip(); - String defaultDevice = org.lwjgl.openal.ALC10.alcGetString(0, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER); + String defaultDevice = org.lwjgl.openal.ALC10.alcGetString(0, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER); - long deviceHandle = org.lwjgl.openal.ALC10.alcOpenDevice(defaultDevice); + long deviceHandle = org.lwjgl.openal.ALC10.alcOpenDevice(defaultDevice); - if (deviceHandle == 0) { - throw new LWJGLException("Could not open ALC device"); - } + if (deviceHandle == 0) { + throw new LWJGLException("Could not open ALC device"); + } - alcDevice = new ALCdevice(deviceHandle); - final ALCCapabilities deviceCaps = org.lwjgl.openal.ALC.createCapabilities(deviceHandle); + alcDevice = new ALCdevice(deviceHandle); + final ALCCapabilities deviceCaps = org.lwjgl.openal.ALC.createCapabilities(deviceHandle); - long contextHandle = org.lwjgl.openal.ALC10.alcCreateContext(AL.getDevice().device, attribs); - alcContext = new ALCcontext(contextHandle); - org.lwjgl.openal.ALC10.alcMakeContextCurrent(contextHandle); - org.lwjgl.openal.AL.createCapabilities(deviceCaps); - created = true; + long contextHandle = org.lwjgl.openal.ALC10.alcCreateContext(AL.getDevice().device, attribs); + alcContext = new ALCcontext(contextHandle); + org.lwjgl.openal.ALC10.alcMakeContextCurrent(contextHandle); + org.lwjgl.openal.AL.createCapabilities(deviceCaps); + created = true; + } } public static boolean isCreated() { diff --git a/src/main/java/org/lwjglx/opengl/Display.java b/src/main/java/org/lwjglx/opengl/Display.java index ec96337d..d4577b15 100644 --- a/src/main/java/org/lwjglx/opengl/Display.java +++ b/src/main/java/org/lwjglx/opengl/Display.java @@ -1,6 +1,7 @@ package org.lwjglx.opengl; import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.system.MemoryStack.stackPush; import static org.lwjgl.system.MemoryUtil.NULL; import java.awt.Canvas; @@ -33,6 +34,7 @@ import org.lwjgl.glfw.GLFWWindowSizeCallback; import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL11; +import org.lwjgl.system.MemoryStack; import org.lwjgl.system.Platform; import org.lwjglx.BufferUtils; import org.lwjglx.Sys; @@ -449,11 +451,13 @@ public void invoke(long window, int width, int height) { displayWidth = mode.getWidth(); displayHeight = mode.getHeight(); - IntBuffer fbw = BufferUtils.createIntBuffer(1); - IntBuffer fbh = BufferUtils.createIntBuffer(1); - GLFW.glfwGetFramebufferSize(Window.handle, fbw, fbh); - displayFramebufferWidth = fbw.get(0); - displayFramebufferHeight = fbh.get(0); + try (MemoryStack stack = stackPush()) { + IntBuffer fbw = stack.mallocInt(1); + IntBuffer fbh = stack.mallocInt(1); + GLFW.glfwGetFramebufferSize(Window.handle, fbw, fbh); + displayFramebufferWidth = fbw.get(0); + displayFramebufferHeight = fbh.get(0); + } displayX = (monitorWidth - mode.getWidth()) / 2; displayY = (monitorHeight - mode.getHeight()) / 2; @@ -642,21 +646,23 @@ public static int setIcon(java.nio.ByteBuffer[] icons) { return 0; } GLFWImage.Buffer glfwImages = GLFWImage.calloc(icons.length); - ByteBuffer[] nativeBuffers = new ByteBuffer[icons.length]; - for (int icon = 0; icon < icons.length; icon++) { - nativeBuffers[icon] = org.lwjgl.BufferUtils.createByteBuffer(icons[icon].capacity()); - nativeBuffers[icon].put(icons[icon]); - nativeBuffers[icon].flip(); - int dimension = (int) Math.sqrt(nativeBuffers[icon].limit() / 4D); - if (dimension * dimension * 4 != nativeBuffers[icon].limit()) { - throw new IllegalStateException(); + try (MemoryStack stack = stackPush()) { + ByteBuffer[] nativeBuffers = new ByteBuffer[icons.length]; + for (int icon = 0; icon < icons.length; icon++) { + nativeBuffers[icon] = stack.malloc(icons[icon].capacity()); + nativeBuffers[icon].put(icons[icon]); + nativeBuffers[icon].flip(); + int dimension = (int) Math.sqrt(nativeBuffers[icon].limit() / 4D); + if (dimension * dimension * 4 != nativeBuffers[icon].limit()) { + throw new IllegalStateException(); + } + glfwImages.put( + icon, + GLFWImage.create() + .set(dimension, dimension, nativeBuffers[icon])); } - glfwImages.put( - icon, - GLFWImage.create() - .set(dimension, dimension, nativeBuffers[icon])); + GLFW.glfwSetWindowIcon(getWindow(), glfwImages); } - GLFW.glfwSetWindowIcon(getWindow(), glfwImages); glfwImages.free(); return 0; } @@ -711,12 +717,15 @@ public static PositionedGLFWVidMode getTargetFullscreenMonitor() { } private static PositionedGLFWVidMode getPositionedMonitorInfo(long monitorId) { - IntBuffer posX = BufferUtils.createIntBuffer(1); - IntBuffer posY = BufferUtils.createIntBuffer(1); - - glfwGetMonitorPos(monitorId, posX, posY); - int x = posX.get(0); - int y = posY.get(0); + int x, y; + try (MemoryStack stack = stackPush()) { + IntBuffer posX = stack.mallocInt(1); + IntBuffer posY = stack.mallocInt(1); + + glfwGetMonitorPos(monitorId, posX, posY); + x = posX.get(0); + y = posY.get(0); + } GLFWVidMode vidmode = glfwGetVideoMode(monitorId); assert vidmode != null; @@ -817,20 +826,22 @@ public static boolean isBorderless() { long window = Display.getWindow(); long windowMonitor = glfwGetWindowMonitor(Display.getWindow()); if (Display.getWindow() != 0 && windowMonitor == NULL) { - IntBuffer windowX = BufferUtils.createIntBuffer(1); - IntBuffer windowY = BufferUtils.createIntBuffer(1); - IntBuffer windowWidth = BufferUtils.createIntBuffer(1); - IntBuffer windowHeight = BufferUtils.createIntBuffer(1); + try (MemoryStack stack = stackPush()) { + IntBuffer windowX = stack.mallocInt(1); + IntBuffer windowY = stack.mallocInt(1); + IntBuffer windowWidth = stack.mallocInt(1); + IntBuffer windowHeight = stack.mallocInt(1); - glfwGetWindowPos(window, windowX, windowY); - glfwGetWindowSize(window, windowWidth, windowHeight); + glfwGetWindowPos(window, windowX, windowY); + glfwGetWindowSize(window, windowWidth, windowHeight); - Display.PositionedGLFWVidMode monitorInfo = Display.getTargetFullscreenMonitor(); - GLFWVidMode vidMode = monitorInfo.vidMode(); + Display.PositionedGLFWVidMode monitorInfo = Display.getTargetFullscreenMonitor(); + GLFWVidMode vidMode = monitorInfo.vidMode(); - return windowX.get(0) == monitorInfo.x() && windowY.get(0) == monitorInfo.y() - && windowWidth.get(0) == vidMode.width() - && (windowHeight.get(0) >= vidMode.height()); + return windowX.get(0) == monitorInfo.x() && windowY.get(0) == monitorInfo.y() + && windowWidth.get(0) == vidMode.width() + && (windowHeight.get(0) >= vidMode.height()); + } } return false; } diff --git a/src/main/java/org/lwjglx/opengl/GL15x.java b/src/main/java/org/lwjglx/opengl/GL15x.java index 415ee14a..ab9cba73 100644 --- a/src/main/java/org/lwjglx/opengl/GL15x.java +++ b/src/main/java/org/lwjglx/opengl/GL15x.java @@ -1,19 +1,22 @@ package org.lwjglx.opengl; +import static org.lwjgl.system.MemoryStack.stackPush; + import java.nio.ByteBuffer; -import org.lwjgl.BufferUtils; import org.lwjgl.PointerBuffer; import org.lwjgl.opengl.GL15; +import org.lwjgl.system.MemoryStack; public class GL15x { public static ByteBuffer glGetBufferPointer(int target, int pname) { int size = GL15.glGetBufferParameteri(target, GL15.GL_BUFFER_SIZE); + try (MemoryStack stack = stackPush()) { + PointerBuffer pb = stack.mallocPointer(1); + GL15.glGetBufferPointerv(target, pname, pb); - PointerBuffer pb = BufferUtils.createPointerBuffer(1); - GL15.glGetBufferPointerv(target, pname, pb); - - return pb.getByteBuffer(0, size); + return pb.getByteBuffer(0, size); + } } } diff --git a/src/main/java/org/lwjglx/opengl/GL20x.java b/src/main/java/org/lwjglx/opengl/GL20x.java index 61805b0f..b11f24d7 100644 --- a/src/main/java/org/lwjglx/opengl/GL20x.java +++ b/src/main/java/org/lwjglx/opengl/GL20x.java @@ -1,13 +1,15 @@ package org.lwjglx.opengl; +import static org.lwjgl.system.MemoryStack.stackPush; + import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; -import org.lwjgl.BufferUtils; import org.lwjgl.PointerBuffer; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL20; +import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryUtil; public class GL20x { @@ -32,18 +34,23 @@ public static void glVertexAttribPointer(int index, int size, boolean unsigned, public static String glGetActiveAttrib(int program, int index, int maxLength, IntBuffer sizeType) { // TODO check if correct - IntBuffer type = BufferUtils.createIntBuffer(1); - String s = GL20.glGetActiveAttrib(program, index, maxLength, sizeType, type); - sizeType.put(type.get(0)); + String s; + try (MemoryStack stack = stackPush()) { + IntBuffer type = stack.mallocInt(1); + s = GL20.glGetActiveAttrib(program, index, maxLength, sizeType, type); + sizeType.put(type.get(0)); + } return s; } public static void glShaderSource(int shader, java.nio.ByteBuffer string) { - PointerBuffer strings = BufferUtils.createPointerBuffer(1); - IntBuffer lengths = BufferUtils.createIntBuffer(1); - - strings.put(0, string); - lengths.put(0, new String(string.array()).length()); // source.length()); - org.lwjgl.opengl.GL20.glShaderSource(shader, strings, lengths); + try (MemoryStack stack = stackPush()) { + PointerBuffer strings = stack.mallocPointer(1); + IntBuffer lengths = stack.mallocInt(1); + + strings.put(0, string); + lengths.put(0, new String(string.array()).length()); // source.length()); + org.lwjgl.opengl.GL20.glShaderSource(shader, strings, lengths); + } } } diff --git a/src/main/java/org/lwjglx/util/glu/MipMap.java b/src/main/java/org/lwjglx/util/glu/MipMap.java index 31690afa..5fd8ecfa 100644 --- a/src/main/java/org/lwjglx/util/glu/MipMap.java +++ b/src/main/java/org/lwjglx/util/glu/MipMap.java @@ -15,20 +15,19 @@ */ package org.lwjglx.util.glu; +import static java.nio.ByteBuffer.allocateDirect; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.stb.STBImageResize.*; import static org.lwjglx.util.glu.GLU.*; import java.nio.ByteBuffer; -import org.lwjglx.BufferUtils; - /** * MipMap.java * * * Created 11-jan-2004 - * + * * @author Erik Duijs */ public class MipMap extends Util { @@ -66,7 +65,7 @@ public static int gluBuild2DMipmaps(final int target, final int components, fina if (w != width || h != height) { // must rescale image to get "top" mipmap texture image - image = BufferUtils.createByteBuffer((w + 4) * h * bpp); + image = allocateDirect((w + 4) * h * bpp); int error = gluScaleImage(format, width, height, type, data, w, h, type, image); if (error != 0) { retVal = error; @@ -91,8 +90,8 @@ public static int gluBuild2DMipmaps(final int target, final int components, fina final ByteBuffer newImage; - if (bufferA == null) newImage = (bufferA = BufferUtils.createByteBuffer((newW + 4) * newH * bpp)); - else if (bufferB == null) newImage = (bufferB = BufferUtils.createByteBuffer((newW + 4) * newH * bpp)); + if (bufferA == null) newImage = (bufferA = allocateDirect((newW + 4) * newH * bpp)); + else if (bufferB == null) newImage = (bufferB = allocateDirect((newW + 4) * newH * bpp)); else newImage = bufferB; int error = gluScaleImage(format, w, h, type, image, newW, newH, type, newImage); @@ -114,7 +113,7 @@ public static int gluBuild2DMipmaps(final int target, final int components, fina /** * Method gluScaleImage. - * + * * @param format * @param widthIn * @param heightIn