Skip to content
Permalink
Browse files

perf(memory): optimize getNormalMatrixTypeArray: now reuse normal mat…

…rix cache typeArr
  • Loading branch information...
yyc-git committed Mar 10, 2019
1 parent 6b03e45 commit 8c322a9aa171100e1bc731f8dce4bd7bcc4d6f69
@@ -1,3 +1,5 @@
let isNotInMap = value => value === Obj.magic(Js.Nullable.undefined);

let isInMap = value => value !== Obj.magic(Js.Nullable.undefined);
let isInMap = value => value !== Obj.magic(Js.Nullable.undefined);

let isInArray = value => value !== Obj.magic(Js.Nullable.undefined);
@@ -2,5 +2,6 @@ open Js.Typed_array;

type globalTempRecord = {
mutable float16Array1: Float32Array.t,
mutable float9Array1: Float32Array.t
mutable float9Array1: Float32Array.t,
mutable unusedFloat9ArrayArr: array(Float32Array.t),
};
@@ -1,3 +1,22 @@
open GlobalTempType;

let getFloat32Array1 = (record) => record.float16Array1;
let getFloat32Array1 = record => record.float16Array1;

let addUnUsedFloat9 = (float9Array1, record) => {
record.unusedFloat9ArrayArr =
record.unusedFloat9ArrayArr |> ArrayService.push(float9Array1);

record;
};

let popUnUsedFloat9Array = record => {
let value =
WonderCommonlib.ArrayService.unsafePop(record.unusedFloat9ArrayArr);

(
record,
NullService.isInArray(value),
value,
/* record.unusedFloat9ArrayArr |> Js.Array.pop, */
);
};
@@ -4,6 +4,24 @@ open GlobalTempType;

let create = () => {
float16Array1:
Float32Array.make([|1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.|]),
float9Array1: Float32Array.make([|1., 0., 0., 1., 0., 0., 1., 0., 0.|])
Float32Array.make([|
1.,
0.,
0.,
0.,
0.,
1.,
0.,
0.,
0.,
0.,
1.,
0.,
0.,
0.,
0.,
1.,
|]),
float9Array1: Float32Array.make([|1., 0., 0., 1., 0., 0., 1., 0., 0.|]),
unusedFloat9ArrayArr: [||],
};
@@ -37,14 +37,14 @@ let _getNormalMatrixTypeArray =
(
transform,
localToWorldMatrices,
(localToWorldTargetTypeArr, normalTargetTypeArr),
(localToWorldMatrixCacheMap, normalTargetTypeArr),
getLocalToWorldMatrixTypeArrayFunc,
) =>
Matrix4Service.invertTo3x3(
getLocalToWorldMatrixTypeArrayFunc(.
transform,
localToWorldMatrices,
localToWorldTargetTypeArr,
localToWorldMatrixCacheMap,
),
normalTargetTypeArr,
)
@@ -55,20 +55,24 @@ let getNormalMatrixTypeArray =
transform: transform,
localToWorldMatrices,
(localToWorldMatrixCacheMap, normalMatrixCacheMap),
globalTempRecord,
) => {
let (has, matrix) =
normalMatrixCacheMap |> MutableSparseMapService.fastGet(transform);

has ?
matrix :
{
let (_, has, unUsedFloat9Array) =
GlobalTempService.popUnUsedFloat9Array(globalTempRecord);

let matrix =
_getNormalMatrixTypeArray(
transform,
localToWorldMatrices,
(
localToWorldMatrixCacheMap,
Matrix3Service.createIdentityMatrix3(),
has ? unUsedFloat9Array : Matrix3Service.createIdentityMatrix3(),
),
getLocalToWorldMatrixTypeArray,
);
@@ -15,12 +15,24 @@ open ModelMatrixTransformService;
open DirtyTransformService;

let _clearCache =
(transform, {localToWorldMatrixCacheMap, normalMatrixCacheMap} as record) => {
(
transform,
globalTempRecord,
{localToWorldMatrixCacheMap, normalMatrixCacheMap} as record,
) => {
let (has, normalMatrix) =
normalMatrixCacheMap |> MutableSparseMapService.fastGet(transform);

let globalTempRecord =
has ?
GlobalTempService.addUnUsedFloat9(normalMatrix, globalTempRecord) :
globalTempRecord;

normalMatrixCacheMap
|> WonderCommonlib.MutableSparseMapService.deleteVal(transform)
|> ignore;

record;
(record, globalTempRecord);
};

let rec update =
@@ -33,8 +45,9 @@ let rec update =
| false => transformRecord
| true =>
/* TODO perf: translation not clear normalMatrixCacheMap, only rotation/scale clear */
let transformRecord =
mark(transform, false, transformRecord) |> _clearCache(transform);
let (transformRecord, globalTempRecord) =
mark(transform, false, transformRecord)
|> _clearCache(transform, globalTempRecord);
switch (getParent(transform, transformRecord)) {
| Some(parent) =>
let transformRecord =
@@ -99,6 +112,7 @@ let updateAndGetNormalMatrixTypeArray =
transform,
localToWorldMatrices,
(localToWorldMatrixCacheMap, normalMatrixCacheMap),
globalTempRecord,
);
};

@@ -151,9 +165,11 @@ let updateAndSetRotationByTuple =
| Some(parent) =>
setLocalRotationByTuple(
transform,
updateAndGetRotationTuple(parent, globalTempRecord, record)
|> QuaternionService.invert
|. QuaternionService.multiply(rotation),
(
updateAndGetRotationTuple(parent, globalTempRecord, record)
|> QuaternionService.invert
)
->(QuaternionService.multiply(rotation)),
record,
)
};
@@ -10,6 +10,7 @@ let createState =
pointLightRecord,
transformRecord,
jobDataRecord,
globalTempRecord,
} as state: StateRenderType.renderState,
) => {
cameraRecord,
@@ -18,6 +19,7 @@ let createState =
directionLightRecord,
pointLightRecord,
transformRecord,
globalTempRecord,
jobDataRecord: {
outlineData: {
outlineColor:
@@ -10,6 +10,8 @@ open RenderTransformType;

open GetRenderDataJobDataType;

open GlobalTempType;

type getRenderDataSubState = {
cameraRecord: option(RenderCameraType.renderCameraRecord),
basicMaterialRecord,
@@ -18,4 +20,5 @@ type getRenderDataSubState = {
pointLightRecord,
transformRecord,
jobDataRecord,
globalTempRecord,
};
@@ -13,12 +13,13 @@ let getLocalToWorldMatrixTypeArray =
);

let getNormalMatrixTypeArray =
(. transform, {transformRecord}) =>
(. transform, {transformRecord, globalTempRecord}) =>
ModelMatrixTransformService.getNormalMatrixTypeArray(
transform,
transformRecord.localToWorldMatrices,
(
transformRecord.localToWorldMatrixCacheMap,
transformRecord.normalMatrixCacheMap,
),
globalTempRecord,
);

0 comments on commit 8c322a9

Please sign in to comment.
You can’t perform that action at this time.