From 95846d78b734b2a6ed3f6bc76de8875843b07842 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Mon, 17 Jul 2023 23:09:06 +0530 Subject: [PATCH] reduce allocations per frame, by a lot --- src/Console.hx | 11 +++++++ src/Debug.hx | 11 ++++--- src/DtsObject.hx | 73 ++++++++++++++++++++++--------------------- src/Marble.hx | 34 ++++++++++---------- src/PathedInterior.hx | 3 +- 5 files changed, 73 insertions(+), 59 deletions(-) diff --git a/src/Console.hx b/src/Console.hx index d4374c70..a3eff9b6 100644 --- a/src/Console.hx +++ b/src/Console.hx @@ -173,6 +173,17 @@ class Console { } else { 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 { error("Unknown command"); } diff --git a/src/Debug.hx b/src/Debug.hx index b8274d1c..fff1fc65 100644 --- a/src/Debug.hx +++ b/src/Debug.hx @@ -62,12 +62,15 @@ class Debug { } public static function drawTriangle(p1:Vector, p2:Vector, p3:Vector) { - _triangles.push(p3.toPoint()); - _triangles.push(p2.toPoint()); - _triangles.push(p1.toPoint()); + if (drawBounds) { + _triangles.push(p3.toPoint()); + _triangles.push(p2.toPoint()); + _triangles.push(p1.toPoint()); + } } public static function drawSphere(centre:Vector, radius:Float) { - _spheres.push({position: centre.clone(), radius: radius}); + if (drawBounds) + _spheres.push({position: centre.clone(), radius: radius}); } } diff --git a/src/DtsObject.hx b/src/DtsObject.hx index 2d93fa1d..f300af95 100644 --- a/src/DtsObject.hx +++ b/src/DtsObject.hx @@ -1,5 +1,6 @@ package src; +import h3d.col.Point; import collision.CollisionWorld; import shaders.EnvMap; import h3d.shader.CubeMap; @@ -60,11 +61,11 @@ typedef GraphNode = { } typedef MaterialGeometry = { - var vertices:Array; - var normals:Array; - var tangents:Array; - var bitangents:Array; - var texNormals:Array; + var vertices:Array; + var normals:Array; + var tangents:Array; + var bitangents:Array; + var texNormals:Array; var uvs:Array; var indices:Array; } @@ -215,12 +216,12 @@ class DtsObject extends GameObject { if (geometry[k].vertices.length == 0) continue; - var poly = new Polygon(geometry[k].vertices.map(x -> x.toPoint())); - poly.normals = geometry[k].normals.map(x -> x.toPoint()); + var poly = new Polygon(geometry[k].vertices); + poly.normals = geometry[k].normals; poly.uvs = geometry[k].uvs; - poly.tangents = geometry[k].tangents.map(x -> x.toPoint()); - poly.bitangents = geometry[k].bitangents.map(x -> x.toPoint()); - poly.texMatNormals = geometry[k].texNormals.map(x -> x.toPoint()); + poly.tangents = geometry[k].tangents; + poly.bitangents = geometry[k].bitangents; + poly.texMatNormals = geometry[k].texNormals; var obj = new Mesh(poly, materials[k], this.graphNodes[i]); meshToIndex.set(obj, dts.objects.indexOf(object)); @@ -292,8 +293,8 @@ class DtsObject extends GameObject { if (geometry[k].vertices.length == 0) continue; - var poly = new DynamicPolygon(geometry[k].vertices.map(x -> x.toPoint())); - poly.normals = geometry[k].normals.map(x -> x.toPoint()); + var poly = new DynamicPolygon(geometry[k].vertices); + poly.normals = geometry[k].normals; poly.uvs = geometry[k].uvs; var obj = new Mesh(poly, materials[k], skinObj); @@ -838,19 +839,19 @@ class DtsObject extends GameObject { return [ { - tangent: t0, - bitangent: b0, - normal: n0 + tangent: t0.toPoint(), + bitangent: b0.toPoint(), + normal: n0.toPoint() }, { - tangent: t1, - bitangent: b1, - normal: n1 + tangent: t1.toPoint(), + bitangent: b1.toPoint(), + normal: n1.toPoint() }, { - tangent: t2, - bitangent: b2, - normal: n2 + tangent: t2.toPoint(), + bitangent: b2.toPoint(), + normal: n2.toPoint() } ]; } @@ -888,9 +889,9 @@ class DtsObject extends GameObject { var tri = { material: materialIndex, vertices: [ - new Vector(vertices[i3].x, vertices[i3].y, vertices[i3].z), - new Vector(vertices[i2].x, vertices[i2].y, vertices[i2].z), - new Vector(vertices[i1].x, vertices[i1].y, vertices[i1].z) + new Point(vertices[i3].x, vertices[i3].y, vertices[i3].z), + new Point(vertices[i2].x, vertices[i2].y, vertices[i2].z), + new Point(vertices[i1].x, vertices[i1].y, vertices[i1].z) ], uvs: [ 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) ], normals: [ - new Vector(vertexNormals[i3].x, vertexNormals[i3].y, vertexNormals[i3].z), - new Vector(vertexNormals[i2].x, vertexNormals[i2].y, vertexNormals[i2].z), - new Vector(vertexNormals[i1].x, vertexNormals[i1].y, vertexNormals[i1].z) + new Point(vertexNormals[i3].x, vertexNormals[i3].y, vertexNormals[i3].z), + new Point(vertexNormals[i2].x, vertexNormals[i2].y, vertexNormals[i2].z), + new Point(vertexNormals[i1].x, vertexNormals[i1].y, vertexNormals[i1].z) ], indices: [i1, i2, i3], t: [], @@ -992,11 +993,11 @@ class DtsObject extends GameObject { } } - var vertexBuckets = new Map, - normals:Array, - ns:Array + normals:Array, + ns:Array }>>(); for (i in 0...triangles.length) { @@ -1010,10 +1011,10 @@ class DtsObject extends GameObject { vertexBuckets.set(v, buckets); } var bucket:{ - refNormal:Vector, + refNormal:Point, triangles:Array, - normals:Array, - ns:Array + normals:Array, + ns:Array } = null; for (j in 0...buckets.length) { bucket = buckets[j]; @@ -1040,8 +1041,8 @@ class DtsObject extends GameObject { for (vtex => buckets in vertexBuckets) { for (i in 0...buckets.length) { var bucket = buckets[i]; - var avgNormal = new Vector(); - var averageN = new Vector(); + var avgNormal = new Point(); + var averageN = new Point(); for (normal in bucket.normals) avgNormal = avgNormal.add(normal); avgNormal.scale(1 / bucket.normals.length); diff --git a/src/Marble.hx b/src/Marble.hx index 88655941..7efb5afb 100644 --- a/src/Marble.hx +++ b/src/Marble.hx @@ -568,7 +568,7 @@ class Marble extends GameObject { } } forceObjectCount++; - contactNormal = contactNormal.add(contact.normal); + contactNormal.load(contactNormal.add(contact.normal)); contactForce += contact.force; forceObjects.push(contact.otherObject); } @@ -583,7 +583,7 @@ class Marble extends GameObject { if (dot > 0) 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) { 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; } @@ -616,7 +616,7 @@ class Marble extends GameObject { mv = mv.multiply(1.538461565971375); var mvlen = mv.length(); if (mvlen > 1) { - mv = mv.multiply(1 / mvlen); + mv.scale(1 / mvlen); } var desiredYVelocity = this._maxRollVelocity * mv.y; 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 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)); - contacts[i].velocity = otherMarble.velocity; + otherMarble.velocity.load(otherMarble.velocity.add(normP.multiply(1 / theirMass))); + contacts[i].velocity.load(otherMarble.velocity); } else { 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 = this.velocity.multiply(velLen); + this.velocity.scale(velLen); surfaceSlide = true; } else if (surfaceDot >= -this._minBounceVel) { - this.velocity = this.velocity.sub(surfaceVel); + this.velocity.load(this.velocity.sub(surfaceVel)); } else { var restitution = this._bounceRestitution; restitution *= contacts[i].restitution; @@ -701,7 +701,7 @@ class Marble extends GameObject { 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(); if (vAtCMag != 0) { @@ -714,11 +714,11 @@ class Marble extends GameObject { var vAtCDir = vAtC.multiply(1 / vAtCMag); 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; if (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; } 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")) { AudioManager.playSound(ResourceLoader.getResource("data/sound/jump.wav", ResourceLoader.getAudio, this.soundResources)); playedSounds.push("data/sound/jump.wav"); @@ -861,7 +861,7 @@ class Marble extends GameObject { friction2 = 0; if (mode != Start) 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); diff --git a/src/PathedInterior.hx b/src/PathedInterior.hx index 7d5f653f..ec5e9646 100644 --- a/src/PathedInterior.hx +++ b/src/PathedInterior.hx @@ -314,8 +314,7 @@ class PathedInterior extends InteriorObject { } // Offset by the position of the first marker var firstPosition = this.markerData[0].position; - position = position.sub(firstPosition); - position = position.add(basePosition); // Add the base position + position = position.sub(firstPosition).add(basePosition); var tmp = new Matrix(); var mat = Matrix.S(this.baseScale.x, this.baseScale.y, this.baseScale.z);