Skip to content

Commit bc3a715

Browse files
capdevonriccardobl
andauthored
Reduce object allocations in ShadowUtility (#2481)
* Update ShadowUtil.java * Update ShadowUtil: comments * Update ShadowUtil: rename tempVec2D to tempVec2 * ShadowUtil: updateFrustumPoints() vector cloning not needed * Update ShadowUtil: code cleanup * Update ShadowUtil.java * revert refactoring and docs * undo refactoring --------- Co-authored-by: Riccardo Balbo <os@rblb.it>
1 parent 2b00e52 commit bc3a715

1 file changed

Lines changed: 49 additions & 39 deletions

File tree

jme3-core/src/main/java/com/jme3/shadow/ShadowUtil.java

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -78,15 +78,20 @@ public static void updateFrustumPoints2(Camera viewCam, Vector3f[] points) {
7878
int w = viewCam.getWidth();
7979
int h = viewCam.getHeight();
8080

81-
points[0].set(viewCam.getWorldCoordinates(new Vector2f(0, 0), 0));
82-
points[1].set(viewCam.getWorldCoordinates(new Vector2f(0, h), 0));
83-
points[2].set(viewCam.getWorldCoordinates(new Vector2f(w, h), 0));
84-
points[3].set(viewCam.getWorldCoordinates(new Vector2f(w, 0), 0));
81+
TempVars vars = TempVars.get();
82+
Vector2f tempVec2 = vars.vect2d;
83+
84+
viewCam.getWorldCoordinates(tempVec2.set(0, 0), 0, points[0]);
85+
viewCam.getWorldCoordinates(tempVec2.set(0, h), 0, points[1]);
86+
viewCam.getWorldCoordinates(tempVec2.set(w, h), 0, points[2]);
87+
viewCam.getWorldCoordinates(tempVec2.set(w, 0), 0, points[3]);
8588

86-
points[4].set(viewCam.getWorldCoordinates(new Vector2f(0, 0), 1));
87-
points[5].set(viewCam.getWorldCoordinates(new Vector2f(0, h), 1));
88-
points[6].set(viewCam.getWorldCoordinates(new Vector2f(w, h), 1));
89-
points[7].set(viewCam.getWorldCoordinates(new Vector2f(w, 0), 1));
89+
viewCam.getWorldCoordinates(tempVec2.set(0, 0), 1, points[4]);
90+
viewCam.getWorldCoordinates(tempVec2.set(0, h), 1, points[5]);
91+
viewCam.getWorldCoordinates(tempVec2.set(w, h), 1, points[6]);
92+
viewCam.getWorldCoordinates(tempVec2.set(w, 0), 1, points[7]);
93+
94+
vars.release();
9095
}
9196

9297
/**
@@ -108,10 +113,11 @@ public static void updateFrustumPoints(Camera viewCam,
108113
float farOverride,
109114
float scale,
110115
Vector3f[] points) {
116+
TempVars vars = TempVars.get();
111117

112118
Vector3f pos = viewCam.getLocation();
113-
Vector3f dir = viewCam.getDirection();
114-
Vector3f up = viewCam.getUp();
119+
Vector3f dir = viewCam.getDirection(vars.vect1);
120+
Vector3f up = viewCam.getUp(vars.vect2);
115121

116122
float depthHeightRatio = viewCam.getFrustumTop() / viewCam.getFrustumNear();
117123
float near = nearOverride;
@@ -137,18 +143,18 @@ public static void updateFrustumPoints(Camera viewCam,
137143
far_width = far_height * ratio;
138144
}
139145

140-
Vector3f right = dir.cross(up).normalizeLocal();
146+
Vector3f right = vars.vect3.set(dir).crossLocal(up).normalizeLocal();
141147

142-
Vector3f temp = new Vector3f();
148+
Vector3f temp = vars.vect4;
143149
temp.set(dir).multLocal(far).addLocal(pos);
144-
Vector3f farCenter = temp.clone();
150+
Vector3f farCenter = vars.vect5.set(temp);
145151
temp.set(dir).multLocal(near).addLocal(pos);
146-
Vector3f nearCenter = temp.clone();
152+
Vector3f nearCenter = vars.vect6.set(temp);
147153

148-
Vector3f nearUp = temp.set(up).multLocal(near_height).clone();
149-
Vector3f farUp = temp.set(up).multLocal(far_height).clone();
150-
Vector3f nearRight = temp.set(right).multLocal(near_width).clone();
151-
Vector3f farRight = temp.set(right).multLocal(far_width).clone();
154+
Vector3f nearUp = vars.vect7.set(up).multLocal(near_height);
155+
Vector3f farUp = vars.vect8.set(up).multLocal(far_height);
156+
Vector3f nearRight = vars.vect9.set(right).multLocal(near_width);
157+
Vector3f farRight = vars.vect10.set(right).multLocal(far_width);
152158

153159
points[0].set(nearCenter).subtractLocal(nearUp).subtractLocal(nearRight);
154160
points[1].set(nearCenter).addLocal(nearUp).subtractLocal(nearRight);
@@ -168,13 +174,14 @@ public static void updateFrustumPoints(Camera viewCam,
168174
}
169175
center.divideLocal(8f);
170176

171-
Vector3f cDir = new Vector3f();
177+
Vector3f cDir = temp;
172178
for (int i = 0; i < 8; i++) {
173179
cDir.set(points[i]).subtractLocal(center);
174180
cDir.multLocal(scale - 1.0f);
175181
points[i].addLocal(cDir);
176182
}
177183
}
184+
vars.release();
178185
}
179186

180187
/**
@@ -244,18 +251,21 @@ public static BoundingBox computeUnionBound(List<BoundingVolume> bv) {
244251
* @return a new instance
245252
*/
246253
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Transform transform) {
247-
Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
248-
Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
249-
Vector3f temp = new Vector3f();
254+
TempVars vars = TempVars.get();
255+
Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
256+
Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
257+
Vector3f temp = vars.vect3;
250258
for (int i = 0; i < pts.length; i++) {
251259
transform.transformVector(pts[i], temp);
252260

253261
min.minLocal(temp);
254262
max.maxLocal(temp);
255263
}
256-
Vector3f center = min.add(max).multLocal(0.5f);
257-
Vector3f extent = max.subtract(min).multLocal(0.5f);
258-
return new BoundingBox(center, extent.x, extent.y, extent.z);
264+
Vector3f center = vars.vect4.set(min).addLocal(max).multLocal(0.5f);
265+
Vector3f extent = vars.vect5.set(max).subtractLocal(min).multLocal(0.5f);
266+
BoundingBox bbox = new BoundingBox(center, extent.x, extent.y, extent.z);
267+
vars.release();
268+
return bbox;
259269
}
260270

261271
/**
@@ -266,10 +276,10 @@ public static BoundingBox computeBoundForPoints(Vector3f[] pts, Transform transf
266276
* @return a new BoundingBox
267277
*/
268278
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Matrix4f mat) {
269-
Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
270-
Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
271279
TempVars vars = TempVars.get();
272-
Vector3f temp = vars.vect1;
280+
Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
281+
Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
282+
Vector3f temp = vars.vect3;
273283

274284
for (int i = 0; i < pts.length; i++) {
275285
float w = mat.multProj(pts[i], temp);
@@ -282,11 +292,13 @@ public static BoundingBox computeBoundForPoints(Vector3f[] pts, Matrix4f mat) {
282292
min.minLocal(temp);
283293
max.maxLocal(temp);
284294
}
295+
Vector3f center = vars.vect4.set(min).addLocal(max).multLocal(0.5f);
296+
Vector3f extent = vars.vect5.set(max).subtractLocal(min).multLocal(0.5f);
297+
// Nehon 08/18/2010 : Added an offset to the extend, to avoid banding artifacts when the frustums are
298+
// aligned.
299+
BoundingBox bbox = new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z + 2.5f);
285300
vars.release();
286-
Vector3f center = min.add(max).multLocal(0.5f);
287-
Vector3f extent = max.subtract(min).multLocal(0.5f);
288-
//Nehon 08/18/2010 : Added an offset to the extend, to avoid banding artifacts when the frustums are aligned.
289-
return new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z + 2.5f);
301+
return bbox;
290302
}
291303

292304
/**
@@ -335,13 +347,12 @@ public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
335347
0f, 0f, scaleZ, offsetZ,
336348
0f, 0f, 0f, 1f);
337349

338-
339-
Matrix4f result = new Matrix4f();
350+
Matrix4f result = vars.tempMat42;
340351
result.set(cropMatrix);
341352
result.multLocal(projMatrix);
342353

343-
vars.release();
344354
shadowCam.setProjectionMatrix(result);
355+
vars.release();
345356
}
346357

347358
/**
@@ -610,13 +621,12 @@ public static void updateShadowCamera(ViewPort viewPort,
610621
0f, 0f, scaleZ, offsetZ,
611622
0f, 0f, 0f, 1f);
612623

613-
614-
Matrix4f result = new Matrix4f();
624+
Matrix4f result = vars.tempMat42;
615625
result.set(cropMatrix);
616626
result.multLocal(projMatrix);
617-
vars.release();
618627

619628
shadowCam.setProjectionMatrix(result);
629+
vars.release();
620630
}
621631

622632
/**

0 commit comments

Comments
 (0)