Skip to content

Commit

Permalink
Reduce memory churn (#165)
Browse files Browse the repository at this point in the history
Move frustum culling from rendering to pre-rendering.
Use Float32Array for Polygon internals.
Reduce pool Vector leaks.
  • Loading branch information
jthomperoo committed Nov 23, 2020
1 parent 48e9bb9 commit 9426e5e
Show file tree
Hide file tree
Showing 59 changed files with 3,233 additions and 2,312 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ must be implemented on a per `Component` basis by overriding this function.
render logic.
- `AABBAlgorithm` more efficient algorithm involving sweeping horizontal then vertical.
- `AABB` method `FarthestPointInDirection` faster and using less `Vector` objects.
- Pointer, pointer move, and wheel events synced up and dispatched within the engine update loop.
- `Polygon` uses `Float32Array` internally.
- `Polygon` can now be initialised with a `Float32Array`.
- Frustum culling moved to preprocessing and removed from render pipeline.

## [v0.9.0] - 2020-09-05
### Added
Expand Down
3 changes: 2 additions & 1 deletion docs/architecture/typed_arrays.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Typed Arrays

Key JamJar geometry objects use typed arrays behind the scenes, such as [Vector], [Matrix3D] and [Matrix4D].
Key JamJar geometry objects use typed arrays behind the scenes, such as [Vector], [Polygon], [Matrix3D] and [Matrix4D].

## What are typed arrays?

Expand Down Expand Up @@ -33,6 +33,7 @@ This ultimately doesn't affect the geometry objects, the typed arrays are wrappe
by a more intuiative API, rather than direct array access.

[Vector]:../../reference/classes/vector
[Polygon]:../../reference/classes/polygon
[Matrix3D]:../../reference/classes/matrix3d
[Matrix4D]:../../reference/classes/matrix4d
[Float32Array]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32array
17 changes: 7 additions & 10 deletions docs/documentation/frustum-culling.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# Frustum Culling

*Frustum Culling* is the process of eliminating renderables that are outside of
the view that should be rendered. This process occurs with the use of Collision
Algorithms - which are abstracted out and can be swapped in and out for
performance/use cases in the [FrustumCuller].
*Frustum Culling* is the process of eliminating renderables that are outside of the view that should be rendered. This
process occurs with the use of Collision Algorithms - which are abstracted out and can be swapped in and out for
performance/use cases in the [FrustumCuller].

The [FrustumCuller] can be customised by providing it with different algorithms
to suit your need.
The [FrustumCuller] can be customised by providing it with different algorithms to suit your need.

The [FrustumCuller] can then be provided to a [RenderSystem] such as
[WebGLSystem] to provide culling logic.
The [FrustumCuller] can then be provided to a *pre-render* [System] such as [SpriteSystem] to provide culling logic.


[FrustumCuller]:../../reference/classes/frustumculler
[RenderSystem]:../../reference/classes/rendersystem
[WebGLSystem]:../../reference/classes/webglsystem
[System]:../../reference/classes/system
[SpriteSystem]:../../reference/classes/spritesystem
1 change: 0 additions & 1 deletion docs/reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
* [TestScene](classes/testscene.md)
* [TestShader](classes/testshader.md)
* [TestSystem](classes/testsystem.md)
* [TestWheelSystem](classes/testwheelsystem.md)
* [Text](classes/text.md)
* [TextRender](classes/textrender.md)
* [TextSystem](classes/textsystem.md)
Expand Down
10 changes: 5 additions & 5 deletions docs/reference/classes/frustumculler.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ colliding) - used in rendering to avoid unneccesary rendering.

### Properties

* [narrowAlgorithm](frustumculler.md#private-narrowalgorithm)
* [collisionAlgorithm](frustumculler.md#private-collisionalgorithm)

### Methods

Expand All @@ -30,21 +30,21 @@ colliding) - used in rendering to avoid unneccesary rendering.

### constructor

\+ **new FrustumCuller**(`narrowAlgorithm`: [ICollisionAlgorithm](../interfaces/icollisionalgorithm.md)): *[FrustumCuller](frustumculler.md)*
\+ **new FrustumCuller**(`collisionAlgorithm`: [ICollisionAlgorithm](../interfaces/icollisionalgorithm.md)): *[FrustumCuller](frustumculler.md)*

**Parameters:**

Name | Type | Default |
------ | ------ | ------ |
`narrowAlgorithm` | [ICollisionAlgorithm](../interfaces/icollisionalgorithm.md) | new AABBAlgorithm() |
`collisionAlgorithm` | [ICollisionAlgorithm](../interfaces/icollisionalgorithm.md) | new AABBAlgorithm() |

**Returns:** *[FrustumCuller](frustumculler.md)*

## Properties

### `Private` narrowAlgorithm
### `Private` collisionAlgorithm

**narrowAlgorithm**: *[ICollisionAlgorithm](../interfaces/icollisionalgorithm.md)*
**collisionAlgorithm**: *[ICollisionAlgorithm](../interfaces/icollisionalgorithm.md)*

## Methods

Expand Down
15 changes: 15 additions & 0 deletions docs/reference/classes/gjkalgorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ algorithm for collision detection.

* [CalculateCollisions](gjkalgorithm.md#calculatecollisions)
* [calculateDirection](gjkalgorithm.md#private-calculatedirection)
* [freeSimplex](gjkalgorithm.md#private-freesimplex)
* [gjk](gjkalgorithm.md#private-gjk)
* [support](gjkalgorithm.md#private-support)

Expand Down Expand Up @@ -53,6 +54,20 @@ Name | Type |

___

### `Private` freeSimplex

**freeSimplex**(`simplex`: [Vector](vector.md)[]): *void*

**Parameters:**

Name | Type |
------ | ------ |
`simplex` | [Vector](vector.md)[] |

**Returns:** *void*

___

### `Private` gjk

**gjk**(`a`: [IShape](../interfaces/ishape.md), `b`: [IShape](../interfaces/ishape.md)): *[CollisionInfo](collisioninfo.md) | undefined*
Expand Down
18 changes: 18 additions & 0 deletions docs/reference/classes/pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ the element the game is running in.

* **Pointer**

## Implements

* [IFreeable](../interfaces/ifreeable.md)

## Index

### Constructors
Expand All @@ -20,6 +24,10 @@ the element the game is running in.
* [elementPosition](pointer.md#elementposition)
* [event](pointer.md#event)

### Methods

* [Free](pointer.md#free)

## Constructors

### constructor
Expand Down Expand Up @@ -59,3 +67,13 @@ ___
**event**: *PointerEvent*

Standard PointerEvent dispatched from JS.

## Methods

### Free

**Free**(): *void*

*Implementation of [IFreeable](../interfaces/ifreeable.md)*

**Returns:** *void*
18 changes: 18 additions & 0 deletions docs/reference/classes/pointercamerainfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ PointerCameraInfo pointer information relevant to a camera.

* **PointerCameraInfo**

## Implements

* [IFreeable](../interfaces/ifreeable.md)

## Index

### Constructors
Expand All @@ -20,6 +24,10 @@ PointerCameraInfo pointer information relevant to a camera.
* [withinBounds](pointercamerainfo.md#withinbounds)
* [worldPosition](pointercamerainfo.md#worldposition)

### Methods

* [Free](pointercamerainfo.md#free)

## Constructors

### constructor
Expand Down Expand Up @@ -68,3 +76,13 @@ ___
**worldPosition**: *[Vector](vector.md)*

Position in the world of the pointer using this camera.

## Methods

### Free

**Free**(): *void*

*Implementation of [IFreeable](../interfaces/ifreeable.md)*

**Returns:** *void*
74 changes: 66 additions & 8 deletions docs/reference/classes/pointersystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ PointerSystem handles Pointer (mouse, touch etc.) input events, converting them

[TestPointerSystem](testpointersystem.md)

[TestWheelSystem](testwheelsystem.md)

## Implements

* [ISubscriber](../interfaces/isubscriber.md)
Expand All @@ -28,9 +26,12 @@ PointerSystem handles Pointer (mouse, touch etc.) input events, converting them
* [entities](pointersystem.md#protected-entities)
* [inputElement](pointersystem.md#private-inputelement)
* [isFullscreen](pointersystem.md#private-isfullscreen)
* [lastMoveEvent](pointersystem.md#private-lastmoveevent)
* [lastPublishedPointers](pointersystem.md#private-lastpublishedpointers)
* [lastWheelEvent](pointersystem.md#private-lastwheelevent)
* [lockedPointerPosition](pointersystem.md#private-lockedpointerposition)
* [messageBus](pointersystem.md#protected-messagebus)
* [pointerEventsToPublish](pointersystem.md#private-pointereventstopublish)
* [scene](pointersystem.md#protected-optional-scene)
* [subscriberID](pointersystem.md#subscriberid)
* [MESSAGE_DEREGISTER](pointersystem.md#static-message_deregister)
Expand All @@ -43,15 +44,17 @@ PointerSystem handles Pointer (mouse, touch etc.) input events, converting them
* [OnDestroy](pointersystem.md#protected-ondestroy)
* [OnMessage](pointersystem.md#onmessage)
* [Update](pointersystem.md#update)
* [moveEvent](pointersystem.md#protected-moveevent)
* [pointerEvent](pointersystem.md#protected-pointerevent)
* [processPointerEvent](pointersystem.md#private-processpointerevent)
* [wheelEvent](pointersystem.md#protected-wheelevent)
* [EVALUATOR](pointersystem.md#static-private-evaluator)

## Constructors

### constructor

\+ **new PointerSystem**(`messageBus`: [IMessageBus](../interfaces/imessagebus.md), `inputElement`: HTMLElement, `scene?`: [IScene](../interfaces/iscene.md), `entities?`: Map‹number, [SystemEntity](systementity.md)›, `subscriberID?`: undefined | number, `isFullscreen`: boolean, `lockedPointerPosition?`: [Vector](vector.md), `lastWheelEvent?`: WheelEvent): *[PointerSystem](pointersystem.md)*
\+ **new PointerSystem**(`messageBus`: [IMessageBus](../interfaces/imessagebus.md), `inputElement`: HTMLElement, `scene?`: [IScene](../interfaces/iscene.md), `entities?`: Map‹number, [SystemEntity](systementity.md)›, `subscriberID?`: undefined | number, `isFullscreen`: boolean, `lockedPointerPosition?`: [Vector](vector.md), `lastWheelEvent?`: WheelEvent, `lastMoveEvent?`: PointerEvent, `pointersToPublish`: PointerEvent[], `lastPublishedPointers`: [Pointer](pointer.md)[]): *[PointerSystem](pointersystem.md)*

*Overrides [System](system.md).[constructor](system.md#constructor)*

Expand All @@ -67,6 +70,9 @@ Name | Type | Default |
`isFullscreen` | boolean | false |
`lockedPointerPosition?` | [Vector](vector.md) | - |
`lastWheelEvent?` | WheelEvent | - |
`lastMoveEvent?` | PointerEvent | - |
`pointersToPublish` | PointerEvent[] | [] |
`lastPublishedPointers` | [Pointer](pointer.md)[] | [] |

**Returns:** *[PointerSystem](pointersystem.md)*

Expand Down Expand Up @@ -103,21 +109,37 @@ true = in fullscreen, false = not in fullscreen

___

### `Private` lastMoveEvent

**lastMoveEvent**: *PointerEvent | undefined*

Last pointer move event captured, stored here to throttle a potentially frequently firing event.

___

### `Private` lastPublishedPointers

**lastPublishedPointers**: *[Pointer](pointer.md)[]*

The pointers published in the last update.
Used to free up objects back into pools.

___

### `Private` lastWheelEvent

**lastWheelEvent**: *WheelEvent | undefined*

Last wheel event captured, stored here to throttle a potentially
frequently firing event
Last wheel event captured, stored here to throttle a potentially frequently firing event.

___

### `Private` lockedPointerPosition

**lockedPointerPosition**: *[Vector](vector.md) | undefined*

Position of the pointer if it is locked, used with the PointerAPI to
keep track of pointer position using movementX and movementY.
Position of the pointer if it is locked, used with the PointerAPI to keep track of pointer position using
movementX and movementY.
If it is undefined there is no pointer lock.

___
Expand All @@ -133,6 +155,14 @@ for communicating with other parts of the engine.

___

### `Private` pointerEventsToPublish

**pointerEventsToPublish**: *PointerEvent[]*

The pointer events recieved since the last update that should be published.

___

### `Protected` `Optional` scene

**scene**? : *[IScene](../interfaces/iscene.md)*
Expand Down Expand Up @@ -234,10 +264,38 @@ ___

___

### `Protected` moveEvent

**moveEvent**(`event`: PointerEvent): *void*

**Parameters:**

Name | Type |
------ | ------ |
`event` | PointerEvent |

**Returns:** *void*

___

### `Protected` pointerEvent

**pointerEvent**(`event`: PointerEvent): *void*

**Parameters:**

Name | Type |
------ | ------ |
`event` | PointerEvent |

**Returns:** *void*

___

### `Private` processPointerEvent

**processPointerEvent**(`event`: PointerEvent): *[Pointer](pointer.md)*

When a Pointer Event occurs; dispatches the pointer event with extra info
through the JamJar messaging system as a Pointer.
Adds in useful information, such as pointer position within camera
Expand All @@ -250,7 +308,7 @@ Name | Type | Description |
------ | ------ | ------ |
`event` | PointerEvent | Pointer Event |

**Returns:** *void*
**Returns:** *[Pointer](pointer.md)*

___

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/classes/polygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Can be used in collision detection and rendering.

### constructor

\+ **new Polygon**(`points`: [Vector](vector.md)[], `wrap`: boolean): *[Polygon](polygon.md)*
\+ **new Polygon**(`points`: [Vector](vector.md)[] | Float32Array, `wrap`: boolean): *[Polygon](polygon.md)*

**Parameters:**

Name | Type | Default |
------ | ------ | ------ |
`points` | [Vector](vector.md)[] | - |
`points` | [Vector](vector.md)[] | Float32Array | - |
`wrap` | boolean | false |

**Returns:** *[Polygon](polygon.md)*
Expand All @@ -57,7 +57,7 @@ Name | Type | Default |

### points

**points**: *[Vector](vector.md)[]*
**points**: *Float32Array*

## Methods

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/classes/pooled.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ___

### `Static` `Protected` new

**new**<**T**>(`poolKey`: string, `type`: object, ...`args`: any): *T*
**new**<**T**>(`poolKey`: string, `type`: object, `args`: any[]): *T*

new is used to request a new object from the pool specified, if the pool is unavailable or empty it will use
the type to provision a new object through a constructor.
Expand All @@ -122,7 +122,7 @@ Name | Type |
------ | ------ |
`constructor` | |

... **args**: *any*
**args**: *any[]*

The args to use when creating/recycling the object.

Expand Down
Loading

0 comments on commit 9426e5e

Please sign in to comment.