reduce allocations per frame, by a lot

This commit is contained in:
RandomityGuy 2023-07-17 23:09:06 +05:30
parent acd512aee6
commit 95846d78b7
5 changed files with 73 additions and 59 deletions

View file

@ -173,6 +173,17 @@ class Console {
} else { } else {
error("Expected one argument, got " + (cmdSplit.length - 1)); error("Expected one argument, got " + (cmdSplit.length - 1));
} }
} else if (cmdType == "dumpMem") {
#if hl
hl.Gc.dumpMemory();
#end
} else if (cmdType == 'gcStats') {
#if hl
var gc = hl.Gc.stats();
log('Total: ${gc.totalAllocated}');
log('Allocation Count: ${gc.allocationCount}');
log('Memory usage: ${gc.currentMemory}');
#end
} else { } else {
error("Unknown command"); error("Unknown command");
} }

View file

@ -62,12 +62,15 @@ class Debug {
} }
public static function drawTriangle(p1:Vector, p2:Vector, p3:Vector) { public static function drawTriangle(p1:Vector, p2:Vector, p3:Vector) {
_triangles.push(p3.toPoint()); if (drawBounds) {
_triangles.push(p2.toPoint()); _triangles.push(p3.toPoint());
_triangles.push(p1.toPoint()); _triangles.push(p2.toPoint());
_triangles.push(p1.toPoint());
}
} }
public static function drawSphere(centre:Vector, radius:Float) { public static function drawSphere(centre:Vector, radius:Float) {
_spheres.push({position: centre.clone(), radius: radius}); if (drawBounds)
_spheres.push({position: centre.clone(), radius: radius});
} }
} }

View file

@ -1,5 +1,6 @@
package src; package src;
import h3d.col.Point;
import collision.CollisionWorld; import collision.CollisionWorld;
import shaders.EnvMap; import shaders.EnvMap;
import h3d.shader.CubeMap; import h3d.shader.CubeMap;
@ -60,11 +61,11 @@ typedef GraphNode = {
} }
typedef MaterialGeometry = { typedef MaterialGeometry = {
var vertices:Array<Vector>; var vertices:Array<Point>;
var normals:Array<Vector>; var normals:Array<Point>;
var tangents:Array<Vector>; var tangents:Array<Point>;
var bitangents:Array<Vector>; var bitangents:Array<Point>;
var texNormals:Array<Vector>; var texNormals:Array<Point>;
var uvs:Array<UV>; var uvs:Array<UV>;
var indices:Array<Int>; var indices:Array<Int>;
} }
@ -215,12 +216,12 @@ class DtsObject extends GameObject {
if (geometry[k].vertices.length == 0) if (geometry[k].vertices.length == 0)
continue; continue;
var poly = new Polygon(geometry[k].vertices.map(x -> x.toPoint())); var poly = new Polygon(geometry[k].vertices);
poly.normals = geometry[k].normals.map(x -> x.toPoint()); poly.normals = geometry[k].normals;
poly.uvs = geometry[k].uvs; poly.uvs = geometry[k].uvs;
poly.tangents = geometry[k].tangents.map(x -> x.toPoint()); poly.tangents = geometry[k].tangents;
poly.bitangents = geometry[k].bitangents.map(x -> x.toPoint()); poly.bitangents = geometry[k].bitangents;
poly.texMatNormals = geometry[k].texNormals.map(x -> x.toPoint()); poly.texMatNormals = geometry[k].texNormals;
var obj = new Mesh(poly, materials[k], this.graphNodes[i]); var obj = new Mesh(poly, materials[k], this.graphNodes[i]);
meshToIndex.set(obj, dts.objects.indexOf(object)); meshToIndex.set(obj, dts.objects.indexOf(object));
@ -292,8 +293,8 @@ class DtsObject extends GameObject {
if (geometry[k].vertices.length == 0) if (geometry[k].vertices.length == 0)
continue; continue;
var poly = new DynamicPolygon(geometry[k].vertices.map(x -> x.toPoint())); var poly = new DynamicPolygon(geometry[k].vertices);
poly.normals = geometry[k].normals.map(x -> x.toPoint()); poly.normals = geometry[k].normals;
poly.uvs = geometry[k].uvs; poly.uvs = geometry[k].uvs;
var obj = new Mesh(poly, materials[k], skinObj); var obj = new Mesh(poly, materials[k], skinObj);
@ -838,19 +839,19 @@ class DtsObject extends GameObject {
return [ return [
{ {
tangent: t0, tangent: t0.toPoint(),
bitangent: b0, bitangent: b0.toPoint(),
normal: n0 normal: n0.toPoint()
}, },
{ {
tangent: t1, tangent: t1.toPoint(),
bitangent: b1, bitangent: b1.toPoint(),
normal: n1 normal: n1.toPoint()
}, },
{ {
tangent: t2, tangent: t2.toPoint(),
bitangent: b2, bitangent: b2.toPoint(),
normal: n2 normal: n2.toPoint()
} }
]; ];
} }
@ -888,9 +889,9 @@ class DtsObject extends GameObject {
var tri = { var tri = {
material: materialIndex, material: materialIndex,
vertices: [ vertices: [
new Vector(vertices[i3].x, vertices[i3].y, vertices[i3].z), new Point(vertices[i3].x, vertices[i3].y, vertices[i3].z),
new Vector(vertices[i2].x, vertices[i2].y, vertices[i2].z), new Point(vertices[i2].x, vertices[i2].y, vertices[i2].z),
new Vector(vertices[i1].x, vertices[i1].y, vertices[i1].z) new Point(vertices[i1].x, vertices[i1].y, vertices[i1].z)
], ],
uvs: [ uvs: [
new UV(dtsMesh.uv[i3].x, dtsMesh.uv[i3].y), new UV(dtsMesh.uv[i3].x, dtsMesh.uv[i3].y),
@ -898,9 +899,9 @@ class DtsObject extends GameObject {
new UV(dtsMesh.uv[i1].x, dtsMesh.uv[i1].y) new UV(dtsMesh.uv[i1].x, dtsMesh.uv[i1].y)
], ],
normals: [ normals: [
new Vector(vertexNormals[i3].x, vertexNormals[i3].y, vertexNormals[i3].z), new Point(vertexNormals[i3].x, vertexNormals[i3].y, vertexNormals[i3].z),
new Vector(vertexNormals[i2].x, vertexNormals[i2].y, vertexNormals[i2].z), new Point(vertexNormals[i2].x, vertexNormals[i2].y, vertexNormals[i2].z),
new Vector(vertexNormals[i1].x, vertexNormals[i1].y, vertexNormals[i1].z) new Point(vertexNormals[i1].x, vertexNormals[i1].y, vertexNormals[i1].z)
], ],
indices: [i1, i2, i3], indices: [i1, i2, i3],
t: [], t: [],
@ -992,11 +993,11 @@ class DtsObject extends GameObject {
} }
} }
var vertexBuckets = new Map<Vector, Array<{ var vertexBuckets = new Map<Point, Array<{
refNormal:Vector, refNormal:Point,
triangles:Array<Int>, triangles:Array<Int>,
normals:Array<Vector>, normals:Array<Point>,
ns:Array<Vector> ns:Array<Point>
}>>(); }>>();
for (i in 0...triangles.length) { for (i in 0...triangles.length) {
@ -1010,10 +1011,10 @@ class DtsObject extends GameObject {
vertexBuckets.set(v, buckets); vertexBuckets.set(v, buckets);
} }
var bucket:{ var bucket:{
refNormal:Vector, refNormal:Point,
triangles:Array<Int>, triangles:Array<Int>,
normals:Array<Vector>, normals:Array<Point>,
ns:Array<Vector> ns:Array<Point>
} = null; } = null;
for (j in 0...buckets.length) { for (j in 0...buckets.length) {
bucket = buckets[j]; bucket = buckets[j];
@ -1040,8 +1041,8 @@ class DtsObject extends GameObject {
for (vtex => buckets in vertexBuckets) { for (vtex => buckets in vertexBuckets) {
for (i in 0...buckets.length) { for (i in 0...buckets.length) {
var bucket = buckets[i]; var bucket = buckets[i];
var avgNormal = new Vector(); var avgNormal = new Point();
var averageN = new Vector(); var averageN = new Point();
for (normal in bucket.normals) for (normal in bucket.normals)
avgNormal = avgNormal.add(normal); avgNormal = avgNormal.add(normal);
avgNormal.scale(1 / bucket.normals.length); avgNormal.scale(1 / bucket.normals.length);

View file

@ -568,7 +568,7 @@ class Marble extends GameObject {
} }
} }
forceObjectCount++; forceObjectCount++;
contactNormal = contactNormal.add(contact.normal); contactNormal.load(contactNormal.add(contact.normal));
contactForce += contact.force; contactForce += contact.force;
forceObjects.push(contact.otherObject); forceObjects.push(contact.otherObject);
} }
@ -583,7 +583,7 @@ class Marble extends GameObject {
if (dot > 0) if (dot > 0)
a -= dot; a -= dot;
A = A.add(contactNormal.multiply(a / dt)); A.load(A.add(contactNormal.multiply(a / dt)));
} }
} }
} }
@ -596,7 +596,7 @@ class Marble extends GameObject {
if (currentTime - this.helicopterEnableTime < 5) { if (currentTime - this.helicopterEnableTime < 5) {
airAccel *= 2; airAccel *= 2;
} }
A = A.add(sideDir.multiply(m.d.x).add(motionDir.multiply(m.d.y)).multiply(airAccel)); A.load(A.add(sideDir.multiply(m.d.x).add(motionDir.multiply(m.d.y)).multiply(airAccel)));
} }
return A; return A;
} }
@ -616,7 +616,7 @@ class Marble extends GameObject {
mv = mv.multiply(1.538461565971375); mv = mv.multiply(1.538461565971375);
var mvlen = mv.length(); var mvlen = mv.length();
if (mvlen > 1) { if (mvlen > 1) {
mv = mv.multiply(1 / mvlen); mv.scale(1 / mvlen);
} }
var desiredYVelocity = this._maxRollVelocity * mv.y; var desiredYVelocity = this._maxRollVelocity * mv.y;
var desiredXVelocity = this._maxRollVelocity * mv.x; var desiredXVelocity = this._maxRollVelocity * mv.x;
@ -679,18 +679,18 @@ class Marble extends GameObject {
var dp = this.velocity.multiply(ourMass).sub(otherMarble.velocity.multiply(theirMass)); var dp = this.velocity.multiply(ourMass).sub(otherMarble.velocity.multiply(theirMass));
var normP = contacts[i].normal.multiply(dp.dot(contacts[i].normal)); var normP = contacts[i].normal.multiply(dp.dot(contacts[i].normal));
normP = normP.multiply(1 + bounce); normP.scale(1 + bounce);
otherMarble.velocity = otherMarble.velocity.add(normP.multiply(1 / theirMass)); otherMarble.velocity.load(otherMarble.velocity.add(normP.multiply(1 / theirMass)));
contacts[i].velocity = otherMarble.velocity; contacts[i].velocity.load(otherMarble.velocity);
} else { } else {
if (contacts[i].velocity.length() == 0 && !surfaceSlide && surfaceDot > -this._maxDotSlide * velLen) { if (contacts[i].velocity.length() == 0 && !surfaceSlide && surfaceDot > -this._maxDotSlide * velLen) {
this.velocity = this.velocity.sub(surfaceVel); this.velocity.load(this.velocity.sub(surfaceVel));
this.velocity.normalize(); this.velocity.normalize();
this.velocity = this.velocity.multiply(velLen); this.velocity.scale(velLen);
surfaceSlide = true; surfaceSlide = true;
} else if (surfaceDot >= -this._minBounceVel) { } else if (surfaceDot >= -this._minBounceVel) {
this.velocity = this.velocity.sub(surfaceVel); this.velocity.load(this.velocity.sub(surfaceVel));
} else { } else {
var restitution = this._bounceRestitution; var restitution = this._bounceRestitution;
restitution *= contacts[i].restitution; restitution *= contacts[i].restitution;
@ -701,7 +701,7 @@ class Marble extends GameObject {
bounceEmitter(sVel.length() * restitution, contacts[i].normal); bounceEmitter(sVel.length() * restitution, contacts[i].normal);
vAtC = vAtC.sub(contacts[i].normal.multiply(contacts[i].normal.dot(sVel))); vAtC.load(vAtC.sub(contacts[i].normal.multiply(contacts[i].normal.dot(sVel))));
var vAtCMag = vAtC.length(); var vAtCMag = vAtC.length();
if (vAtCMag != 0) { if (vAtCMag != 0) {
@ -714,11 +714,11 @@ class Marble extends GameObject {
var vAtCDir = vAtC.multiply(1 / vAtCMag); var vAtCDir = vAtC.multiply(1 / vAtCMag);
var deltaOmega = contacts[i].normal.cross(vAtCDir).multiply(angVMagnitude); var deltaOmega = contacts[i].normal.cross(vAtCDir).multiply(angVMagnitude);
this.omega = this.omega.add(deltaOmega); this.omega.load(this.omega.add(deltaOmega));
this.velocity = this.velocity.sub(deltaOmega.cross(contacts[i].normal.multiply(_radius))); this.velocity.load(this.velocity.sub(deltaOmega.cross(contacts[i].normal.multiply(_radius))));
} }
this.velocity = this.velocity.add(velocityAdd); this.velocity.load(this.velocity.add(velocityAdd));
} }
} }
@ -768,7 +768,7 @@ class Marble extends GameObject {
soFar = -25; soFar = -25;
if (soFar > 25) if (soFar > 25)
soFar = 25; soFar = 25;
this.velocity = this.velocity.add(dir.multiply(soFar)); this.velocity.load(this.velocity.add(dir.multiply(soFar)));
} }
} }
@ -799,7 +799,7 @@ class Marble extends GameObject {
sv = 0; sv = 0;
} }
if (sv < this._jumpImpulse) { if (sv < this._jumpImpulse) {
this.velocity = this.velocity.add(bestContact.normal.multiply((this._jumpImpulse - sv))); this.velocity.load(this.velocity.add(bestContact.normal.multiply((this._jumpImpulse - sv))));
if (!playedSounds.contains("data/sound/jump.wav")) { if (!playedSounds.contains("data/sound/jump.wav")) {
AudioManager.playSound(ResourceLoader.getResource("data/sound/jump.wav", ResourceLoader.getAudio, this.soundResources)); AudioManager.playSound(ResourceLoader.getResource("data/sound/jump.wav", ResourceLoader.getAudio, this.soundResources));
playedSounds.push("data/sound/jump.wav"); playedSounds.push("data/sound/jump.wav");
@ -861,7 +861,7 @@ class Marble extends GameObject {
friction2 = 0; friction2 = 0;
if (mode != Start) if (mode != Start)
friction2 = this._kineticFriction * bestContact.friction; friction2 = this._kineticFriction * bestContact.friction;
Aadd = Aadd.multiply(friction2 * bestNormalForce / aAtCMag); Aadd.load(Aadd.multiply(friction2 * bestNormalForce / aAtCMag));
} }
A.set(A.x + Aadd.x, A.y + Aadd.y, A.z + Aadd.z); A.set(A.x + Aadd.x, A.y + Aadd.y, A.z + Aadd.z);
a.set(a.x + aadd.x, a.y + aadd.y, a.z + aadd.z); a.set(a.x + aadd.x, a.y + aadd.y, a.z + aadd.z);

View file

@ -314,8 +314,7 @@ class PathedInterior extends InteriorObject {
} }
// Offset by the position of the first marker // Offset by the position of the first marker
var firstPosition = this.markerData[0].position; var firstPosition = this.markerData[0].position;
position = position.sub(firstPosition); position = position.sub(firstPosition).add(basePosition);
position = position.add(basePosition); // Add the base position
var tmp = new Matrix(); var tmp = new Matrix();
var mat = Matrix.S(this.baseScale.x, this.baseScale.y, this.baseScale.z); var mat = Matrix.S(this.baseScale.x, this.baseScale.y, this.baseScale.z);