Reduce buffer allocations (#190)

This commit is contained in:
ah-OOG-ah authored and GitHub committed 2024-12-01 20:40:03 +00:00
1 parent d8e2edc97e
commit 6eb89d2cbf
5 files changed
+117 -93

No files matched your search

+42 -38
View File
@@ -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 class AL {
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.EXTEfx.ALC_MAX_AUXILIARY_SENDS);
attribs.put(4);
attribs.put(org.lwjgl.openal.SOFTHRTF.ALC_HRTF_ID_SOFT);
attribs.put(0);
attribs.flip();
String defaultDevice = org.lwjgl.openal.ALC10.alcGetString(0, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER);
long deviceHandle = org.lwjgl.openal.ALC10.alcOpenDevice(defaultDevice);
if (deviceHandle == 0) {
throw new LWJGLException("Could not open ALC device");
}
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;
}
/////////////////////////////////////////////
attribs.put(org.lwjgl.openal.EXTEfx.ALC_MAX_AUXILIARY_SENDS);
attribs.put(4);
attribs.put(0);
attribs.flip();
String defaultDevice = org.lwjgl.openal.ALC10.alcGetString(0, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER);
long deviceHandle = org.lwjgl.openal.ALC10.alcOpenDevice(defaultDevice);
if (deviceHandle == 0) {
throw new LWJGLException("Could not open ALC device");
}
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;
}
public static boolean isCreated() {
+45 -34
View File
@@ -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.GLFWWindowRefreshCallback;
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 class Display {
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 class Display {
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 class Display {
}
private static PositionedGLFWVidMode getPositionedMonitorInfo(long monitorId) {
IntBuffer posX = BufferUtils.createIntBuffer(1);
IntBuffer posY = BufferUtils.createIntBuffer(1);
int x, y;
try (MemoryStack stack = stackPush()) {
IntBuffer posX = stack.mallocInt(1);
IntBuffer posY = stack.mallocInt(1);
glfwGetMonitorPos(monitorId, posX, posY);
int x = posX.get(0);
int y = posY.get(0);
glfwGetMonitorPos(monitorId, posX, posY);
x = posX.get(0);
y = posY.get(0);
}
GLFWVidMode vidmode = glfwGetVideoMode(monitorId);
assert vidmode != null;
@@ -817,20 +826,22 @@ public class Display {
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;
}
+8 -5
View File
@@ -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);
}
}
}
+16 -9
View File
@@ -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 class GL20x {
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);
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);
strings.put(0, string);
lengths.put(0, new String(string.array()).length()); // source.length());
org.lwjgl.opengl.GL20.glShaderSource(shader, strings, lengths);
}
}
}
@@ -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 class MipMap extends Util {
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 class MipMap extends Util {
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 class MipMap extends Util {
/**
* Method gluScaleImage.
*
*
* @param format
* @param widthIn
* @param heightIn