Skip to content

Commit 24f510f

Browse files
capdevonriccardobl
andauthored
Reduce object allocations in AudioListener (#2501)
* Update Listener.java * preserve backward compatibility * reduce allocations in ALAudioRenderer * Refactor getLeft, getUp, and getDirection methods --------- Co-authored-by: Riccardo Balbo <os@rblb.it>
1 parent b37a9c4 commit 24f510f

2 files changed

Lines changed: 82 additions & 15 deletions

File tree

jme3-core/src/main/java/com/jme3/audio/Listener.java

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,17 @@
4141
*/
4242
public class Listener {
4343

44-
private final Vector3f location;
45-
private final Vector3f velocity;
46-
private final Quaternion rotation;
47-
private float volume = 1;
44+
private final Vector3f location = new Vector3f();
45+
private final Vector3f velocity = new Vector3f();
46+
private final Quaternion rotation = new Quaternion();
47+
private float volume = 1f;
4848
private AudioRenderer renderer;
4949

50+
5051
/**
5152
* Constructs a new {@code Listener} with default parameters.
5253
*/
5354
public Listener() {
54-
location = new Vector3f();
55-
velocity = new Vector3f();
56-
rotation = new Quaternion();
5755
}
5856

5957
/**
@@ -62,9 +60,9 @@ public Listener() {
6260
* @param source The {@code Listener} to copy the properties from.
6361
*/
6462
public Listener(Listener source) {
65-
this.location = source.location.clone();
66-
this.velocity = source.velocity.clone();
67-
this.rotation = source.rotation.clone();
63+
this.location.set(source.location);
64+
this.velocity.set(source.velocity);
65+
this.rotation.set(source.rotation);
6866
this.volume = source.volume;
6967
this.renderer = source.renderer; // Note: Renderer is also copied
7068
}
@@ -109,6 +107,17 @@ public Vector3f getLocation() {
109107
return location;
110108
}
111109

110+
/**
111+
* Gets the current location of the listener in world space.
112+
*
113+
* @param store The vector to store the result in.
114+
* @return The listener's location as a {@link Vector3f}.
115+
*/
116+
public Vector3f getLocation(Vector3f store) {
117+
if (store == null) store = new Vector3f();
118+
return store.set(location);
119+
}
120+
112121
/**
113122
* Gets the current rotation of the listener in world space.
114123
*
@@ -118,6 +127,17 @@ public Quaternion getRotation() {
118127
return rotation;
119128
}
120129

130+
/**
131+
* Gets the current rotation of the listener in world space.
132+
*
133+
* @param store The quaternion to store the result in.
134+
* @return The listener's rotation as a {@link Quaternion}.
135+
*/
136+
public Quaternion getRotation(Quaternion store) {
137+
if (store == null) store = new Quaternion();
138+
return store.set(rotation);
139+
}
140+
121141
/**
122142
* Gets the current velocity of the listener.
123143
* This is used for Doppler effect calculations.
@@ -128,14 +148,35 @@ public Vector3f getVelocity() {
128148
return velocity;
129149
}
130150

151+
/**
152+
* Gets the current velocity of the listener.
153+
*
154+
* @param store The vector to store the result in.
155+
* @return The listener's velocity as a {@link Vector3f}.
156+
*/
157+
public Vector3f getVelocity(Vector3f store) {
158+
if (store == null) store = new Vector3f();
159+
return store.set(velocity);
160+
}
161+
131162
/**
132163
* Gets the left direction vector of the listener.
133164
* This vector is derived from the listener's rotation.
134165
*
135166
* @return The listener's left direction as a {@link Vector3f}.
136167
*/
137168
public Vector3f getLeft() {
138-
return rotation.getRotationColumn(0);
169+
return getLeft(null);
170+
}
171+
172+
/**
173+
* Gets the left direction vector of the listener. This vector is derived from the listener's rotation.
174+
*
175+
* @param store The vector to store the result in.
176+
* @return The listener's left direction as a {@link Vector3f}.
177+
*/
178+
public Vector3f getLeft(Vector3f store) {
179+
return rotation.getRotationColumn(0, store);
139180
}
140181

141182
/**
@@ -145,7 +186,18 @@ public Vector3f getLeft() {
145186
* @return The listener's up direction as a {@link Vector3f}.
146187
*/
147188
public Vector3f getUp() {
148-
return rotation.getRotationColumn(1);
189+
return getUp(null);
190+
}
191+
192+
/**
193+
* Gets the up direction vector of the listener.
194+
* This vector is derived from the listener's rotation.
195+
*
196+
* @param store The vector to store the result in.
197+
* @return The listener's up direction as a {@link Vector3f}.
198+
*/
199+
public Vector3f getUp(Vector3f store) {
200+
return rotation.getRotationColumn(1, store);
149201
}
150202

151203
/**
@@ -155,7 +207,18 @@ public Vector3f getUp() {
155207
* @return The listener's forward direction.
156208
*/
157209
public Vector3f getDirection() {
158-
return rotation.getRotationColumn(2);
210+
return getDirection(null);
211+
}
212+
213+
/**
214+
* Gets the forward direction vector of the listener.
215+
* This vector is derived from the listener's rotation.
216+
*
217+
* @param store The vector to store the result in.
218+
* @return The listener's forward direction.
219+
*/
220+
public Vector3f getDirection(Vector3f store) {
221+
return rotation.getRotationColumn(2, store);
159222
}
160223

161224
/**

jme3-core/src/main/java/com/jme3/audio/openal/ALAudioRenderer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import com.jme3.math.Vector3f;
5151
import com.jme3.util.BufferUtils;
5252
import com.jme3.util.NativeObjectManager;
53+
import com.jme3.util.TempVars;
54+
5355
import java.nio.ByteBuffer;
5456
import java.nio.FloatBuffer;
5557
import java.nio.IntBuffer;
@@ -766,14 +768,16 @@ private void applyListenerPosition(Listener listener) {
766768
}
767769

768770
private void applyListenerRotation(Listener listener) {
769-
Vector3f dir = listener.getDirection();
770-
Vector3f up = listener.getUp();
771+
TempVars vars = TempVars.get();
772+
Vector3f dir = listener.getDirection(vars.vect1);
773+
Vector3f up = listener.getUp(vars.vect2);
771774
// Use the shared FloatBuffer fb
772775
fb.rewind();
773776
fb.put(dir.x).put(dir.y).put(dir.z);
774777
fb.put(up.x).put(up.y).put(up.z);
775778
fb.flip();
776779
al.alListener(AL_ORIENTATION, fb);
780+
vars.release();
777781
}
778782

779783
private void applyListenerVelocity(Listener listener) {

0 commit comments

Comments
 (0)