Skip to content

Commit

Permalink
Reduce buffer allocations (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-OOG-ah authored Dec 1, 2024
1 parent d8e2edc commit 6eb89d2
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 90 deletions.
70 changes: 37 additions & 33 deletions src/main/java/org/lwjglx/openal/AL.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
81 changes: 46 additions & 35 deletions src/main/java/org/lwjglx/opengl/Display.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/lwjglx/opengl/GL15x.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
27 changes: 17 additions & 10 deletions src/main/java/org/lwjglx/opengl/GL20x.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
}
13 changes: 6 additions & 7 deletions src/main/java/org/lwjglx/util/glu/MipMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -114,7 +113,7 @@ public static int gluBuild2DMipmaps(final int target, final int components, fina

/**
* Method gluScaleImage.
*
*
* @param format
* @param widthIn
* @param heightIn
Expand Down

0 comments on commit 6eb89d2

Please sign in to comment.