Skip to content

Commit

Permalink
Replace some uses of buffer allocation with MemoryStack
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-OOG-ah committed Nov 26, 2024
1 parent d8e2edc commit 813d2c7
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 114 deletions.
69 changes: 37 additions & 32 deletions src/main/java/org/lwjglx/openal/AL.java
Original file line number Diff line number Diff line change
@@ -1,9 +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.lwjgl.system.MemoryStack;
import org.lwjglx.BufferUtils;
import org.lwjglx.LWJGLException;
import org.lwjglx.Sys;
Expand Down Expand Up @@ -32,50 +35,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
79 changes: 45 additions & 34 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,19 +646,21 @@ 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);
glfwImages.free();
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
12 changes: 8 additions & 4 deletions src/main/java/org/lwjglx/opengl/GL15x.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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);
}
}
}
26 changes: 17 additions & 9 deletions src/main/java/org/lwjglx/opengl/GL20x.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.lwjglx.opengl;

import static org.lwjgl.system.MemoryStack.stackPush;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
Expand All @@ -8,6 +10,7 @@
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 +35,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);
}
}
}
Loading

0 comments on commit 813d2c7

Please sign in to comment.