diff --git a/package.json b/package.json index 2f04c518b2..52b97ffa64 100755 --- a/package.json +++ b/package.json @@ -75,11 +75,11 @@ "wonder-imgui": "^0.0.34", "wonder-log": "^0.2.11", "wonder-most-animation-frame": "0.0.3", - "wonder-webgl": "^0.0.9" + "wonder-webgl": "^0.0.10" }, "resolutions": { "wonder-log": "^0.2.11", - "wonder-webgl": "^0.0.9", + "wonder-webgl": "^0.0.10", "wonder-commonlib": "0.2.31", "wonder-bs-jest": "0.3.0", "wonder-bs-puppeteer": "^0.1.6" diff --git a/src/service/primitive/GameObjectsMapGroupService.re b/src/service/primitive/GameObjectsMapGroupService.re new file mode 100755 index 0000000000..5420bcd8b0 --- /dev/null +++ b/src/service/primitive/GameObjectsMapGroupService.re @@ -0,0 +1,19 @@ +let isGroup = (component, gameObjectsMap) => + switch (GameObjectsMapService.getGameObjects(component, gameObjectsMap)) { + | Some(arr) when arr |> Js.Array.length > 0 => true + | _ => false + }; + +let removeGameObject = (gameObject, component, gameObjectsMap) => + GameObjectsMapService.removeGameObject( + gameObject, + component, + gameObjectsMap, + ); + +let batchRemoveGameObjects = (gameObjectArr, component, gameObjectsMap) => + GameObjectsMapService.batchRemoveGameObjects( + gameObjectArr, + component, + gameObjectsMap, + ); \ No newline at end of file diff --git a/src/service/primitive/GroupService.re b/src/service/primitive/GroupService.re deleted file mode 100755 index b11775ea05..0000000000 --- a/src/service/primitive/GroupService.re +++ /dev/null @@ -1,86 +0,0 @@ -/* let getGroupCount = (component: int, groupCountMap) => - switch (groupCountMap |> WonderCommonlib.MutableSparseMapService.get(component)) { - | None => 0 - | Some(count) => count - }; - - let isGroupComponent = (component: int, groupCountMap) => - getGroupCount(component, groupCountMap) > 0; - - let increaseGroupCount = (component: int, groupCountMap) => - groupCountMap - |> WonderCommonlib.MutableSparseMapService.set( - component, - getGroupCount(component, groupCountMap) |> succ - ); - - let decreaseGroupCount = (component: int, groupCountMap) => - groupCountMap - |> WonderCommonlib.MutableSparseMapService.set( - component, - getGroupCount(component, groupCountMap) |> pred - ); */ - -let isGroup = (component, gameObjectsMap) => - /* switch (GameObjectsMapService.getGameObjects(component, gameObjectsMap)) { - | Some(arr) when arr |> Js.Array.length > 1 => true - | _ => false - }; */ - switch (GameObjectsMapService.getGameObjects(component, gameObjectsMap)) { - | Some(arr) when arr |> Js.Array.length > 0 => true - | _ => false - }; - -/* let isGroupGeometry = (component, gameObjectsMap) => - /* WonderLog.Contract.requireCheck( - () => - WonderLog.( - Contract.( - Operators.( - test( - Log.buildAssertMessage( - ~expect= - {j|gameObjectsMap->gameObjectArr.length >= gameObjects.length|j}, - ~actual={j|not|j}, - ), - () => - switch ( - GameObjectsMapService.getGameObjects( - component, - gameObjectsMap, - ) - ) { - | Some(arr) => - Js.Array.length(arr) >= Js.Array.length(gameObjects) - | _ => assertPass() - } - ) - ) - ) - ), - IsDebugMainService.getIsDebug(StateDataMain.stateData), - ); - - switch (GameObjectsMapService.getGameObjects(component, gameObjectsMap)) { - | Some(arr) when Js.Array.length(arr) === Js.Array.length(gameObjects) => - true - | _ => false - }; */ - switch (GameObjectsMapService.getGameObjects(component, gameObjectsMap)) { - | Some(arr) when arr |> Js.Array.length > 0 => true - | _ => false - }; */ - -let removeGameObject = (gameObject, component, gameObjectsMap) => - GameObjectsMapService.removeGameObject( - gameObject, - component, - gameObjectsMap, - ); - -let batchRemoveGameObjects = (gameObjectArr, component, gameObjectsMap) => - GameObjectsMapService.batchRemoveGameObjects( - gameObjectArr, - component, - gameObjectsMap, - ); \ No newline at end of file diff --git a/src/service/primitive/buffer/DisposeTypeArrayService.re b/src/service/primitive/buffer/DisposeTypeArrayService.re index ee6d2a3d82..53cf02160e 100755 --- a/src/service/primitive/buffer/DisposeTypeArrayService.re +++ b/src/service/primitive/buffer/DisposeTypeArrayService.re @@ -1,75 +1,72 @@ let deleteBySwapAndResetFloat32TypeArr = - [@bs] - ( - ((sourceIndex, targetIndex), typeArr, length, defaultValueArr) => { - open Js.Typed_array; - for (i in 0 to length - 1) { - Float32Array.unsafe_set( - typeArr, - sourceIndex + i, - Float32Array.unsafe_get(typeArr, targetIndex + i) - ); - Float32Array.unsafe_set(typeArr, targetIndex + i, defaultValueArr[i]) - }; - typeArr - } - ); + (. (sourceIndex, targetIndex), typeArr, length, defaultValueArr) => { + open Js.Typed_array; + for (i in 0 to length - 1) { + Float32Array.unsafe_set( + typeArr, + sourceIndex + i, + Float32Array.unsafe_get(typeArr, targetIndex + i), + ); + Float32Array.unsafe_set(typeArr, targetIndex + i, defaultValueArr[i]); + }; + typeArr; + }; let deleteSingleValueBySwapAndResetFloat32TypeArr = - [@bs] - ( - ((sourceIndex, targetIndex), typeArr, length: int, defaultValue) => { - open Js.Typed_array; - Float32Array.unsafe_set(typeArr, sourceIndex, Float32Array.unsafe_get(typeArr, targetIndex)); - Float32Array.unsafe_set(typeArr, targetIndex, defaultValue); - typeArr - } - ); + (. (sourceIndex, targetIndex), typeArr, length: int, defaultValue) => { + open Js.Typed_array; + Float32Array.unsafe_set( + typeArr, + sourceIndex, + Float32Array.unsafe_get(typeArr, targetIndex), + ); + Float32Array.unsafe_set(typeArr, targetIndex, defaultValue); + typeArr; + }; let deleteSingleValueBySwapUint32TypeArr = (sourceIndex, lastIndex, typeArr) => { open Js.Typed_array; - Uint32Array.unsafe_set(typeArr, sourceIndex, Uint32Array.unsafe_get(typeArr, lastIndex)); - typeArr + Uint32Array.unsafe_set( + typeArr, + sourceIndex, + Uint32Array.unsafe_get(typeArr, lastIndex), + ); + typeArr; }; let deleteAndResetFloat32TypeArr = - [@bs] - ( - (sourceIndex, length, defaultValueArr, typeArr) => { - open Js.Typed_array; - for (i in 0 to length - 1) { - Float32Array.unsafe_set(typeArr, sourceIndex + i, defaultValueArr[i]) - }; - typeArr - } - ); + (. sourceIndex, length, defaultValueArr, typeArr) => { + open Js.Typed_array; + for (i in 0 to length - 1) { + Float32Array.unsafe_set(typeArr, sourceIndex + i, defaultValueArr[i]); + }; + typeArr; + }; let deleteAndResetFloat32 = - [@bs] - ( - (sourceIndex, defaultValue, typeArr) => { - open Js.Typed_array; - Float32Array.unsafe_set(typeArr, sourceIndex, defaultValue); - typeArr - } - ); + (. sourceIndex, defaultValue, typeArr) => { + open Js.Typed_array; + Float32Array.unsafe_set(typeArr, sourceIndex, defaultValue); + typeArr; + }; let deleteAndResetUint32 = - [@bs] - ( - (sourceIndex, defaultValue, typeArr) => { - open Js.Typed_array; - Uint32Array.unsafe_set(typeArr, sourceIndex, defaultValue); - typeArr - } - ); + (. sourceIndex, defaultValue, typeArr) => { + open Js.Typed_array; + Uint32Array.unsafe_set(typeArr, sourceIndex, defaultValue); + typeArr; + }; let deleteAndResetUint8 = - [@bs] - ( - (sourceIndex, defaultValue, typeArr) => { - open Js.Typed_array; - Uint8Array.unsafe_set(typeArr, sourceIndex, defaultValue); - typeArr - } - ); \ No newline at end of file + (. sourceIndex, defaultValue, typeArr) => { + open Js.Typed_array; + Uint8Array.unsafe_set(typeArr, sourceIndex, defaultValue); + typeArr; + }; + +let deleteAndResetUint16 = + (. sourceIndex, defaultValue, typeArr) => { + open Js.Typed_array; + Uint16Array.unsafe_set(typeArr, sourceIndex, defaultValue); + typeArr; + }; \ No newline at end of file diff --git a/src/service/primitive/data/MaterialType.re b/src/service/primitive/data/MaterialType.re index 731a1885ec..5b36af1acf 100755 --- a/src/service/primitive/data/MaterialType.re +++ b/src/service/primitive/data/MaterialType.re @@ -4,4 +4,8 @@ type shaderIndexMap = WonderCommonlib.MutableSparseMapService.t(int); type colorMap = WonderCommonlib.MutableSparseMapService.t(array(float)); -type disposedIndexArray = array(int); \ No newline at end of file +type disposedIndexArray = array(int); + +type materialType = + | BasicMaterial + | LightMaterial; \ No newline at end of file diff --git a/src/service/record/main/data/texture/ArrayBufferViewSourceTextureType.re b/src/service/record/main/data/texture/ArrayBufferViewSourceTextureType.re index 55e376ca31..187761ffa1 100755 --- a/src/service/record/main/data/texture/ArrayBufferViewSourceTextureType.re +++ b/src/service/record/main/data/texture/ArrayBufferViewSourceTextureType.re @@ -11,16 +11,22 @@ type arrayBufferViewSourceTextureRecord = { flipYs: Js.Typed_array.Uint8Array.t, widths: Js.Typed_array.Uint16Array.t, heights: Js.Typed_array.Uint16Array.t, - sourceMap: WonderCommonlib.MutableSparseMapService.t(Js.Typed_array.Uint8Array.t), - glTextureMap: WonderCommonlib.MutableSparseMapService.t(WonderWebgl.GlType.texture), + sourceMap: + WonderCommonlib.MutableSparseMapService.t(Js.Typed_array.Uint8Array.t), + glTextureMap: + WonderCommonlib.MutableSparseMapService.t(WonderWebgl.GlType.texture), bindTextureUnitCacheMap: WonderCommonlib.MutableSparseMapService.t(int), disposedIndexArray: array(int), needAddedSourceArray: array((int, Js.Typed_array.Uint8Array.t)), needInitedTextureIndexArray: array(int), nameMap: WonderCommonlib.MutableSparseMapService.t(string), + materialsMap: + WonderCommonlib.MutableSparseMapService.t( + array((int, MaterialType.materialType)), + ), }; -external arrayArrayBufferViewSourceToSparseMap : +external arrayArrayBufferViewSourceToSparseMap: array(Js.Typed_array.Uint8Array.t) => WonderCommonlib.MutableSparseMapService.t(Js.Typed_array.Uint8Array.t) = "%identity"; \ No newline at end of file diff --git a/src/service/record/main/data/texture/BasicSourceTextureType.re b/src/service/record/main/data/texture/BasicSourceTextureType.re index f7157b84c2..ed7f90dc7e 100755 --- a/src/service/record/main/data/texture/BasicSourceTextureType.re +++ b/src/service/record/main/data/texture/BasicSourceTextureType.re @@ -11,11 +11,19 @@ type basicSourceTextureRecord = { types: Js.Typed_array.Uint8Array.t, isNeedUpdates: Js.Typed_array.Uint8Array.t, flipYs: Js.Typed_array.Uint8Array.t, - sourceMap: WonderCommonlib.MutableSparseMapService.t(WonderWebgl.DomExtendType.imageElement), - glTextureMap: WonderCommonlib.MutableSparseMapService.t(WonderWebgl.GlType.texture), + sourceMap: + WonderCommonlib.MutableSparseMapService.t( + WonderWebgl.DomExtendType.imageElement, + ), + glTextureMap: + WonderCommonlib.MutableSparseMapService.t(WonderWebgl.GlType.texture), bindTextureUnitCacheMap: WonderCommonlib.MutableSparseMapService.t(int), disposedIndexArray: array(int), needAddedSourceArray: array((int, WonderWebgl.DomExtendType.imageElement)), needInitedTextureIndexArray: array(int), - nameMap: WonderCommonlib.MutableSparseMapService.t(string) + nameMap: WonderCommonlib.MutableSparseMapService.t(string), + materialsMap: + WonderCommonlib.MutableSparseMapService.t( + array((int, MaterialType.materialType)), + ), }; \ No newline at end of file diff --git a/src/service/record/main/geometry/GroupGeometryService.re b/src/service/record/main/geometry/GroupGeometryService.re index 06c67682df..2024e87295 100755 --- a/src/service/record/main/geometry/GroupGeometryService.re +++ b/src/service/record/main/geometry/GroupGeometryService.re @@ -1,19 +1,19 @@ open GeometryType; let isGroupGeometry = (geometry, record) => - GroupService.isGroup(geometry, record.gameObjectsMap); + GameObjectsMapGroupService.isGroup(geometry, record.gameObjectsMap); let removeGameObject = (gameObject, geometry, {gameObjectsMap} as record) => { ...record, gameObjectsMap: - GroupService.removeGameObject(gameObject, geometry, gameObjectsMap), + GameObjectsMapGroupService.removeGameObject(gameObject, geometry, gameObjectsMap), }; let batchRemoveGameObjects = (gameObjectArr, geometry, {gameObjectsMap} as record) => { ...record, gameObjectsMap: - GroupService.batchRemoveGameObjects( + GameObjectsMapGroupService.batchRemoveGameObjects( gameObjectArr, geometry, gameObjectsMap, diff --git a/src/service/record/main/material/basic/GroupBasicMaterialService.re b/src/service/record/main/material/basic/GroupBasicMaterialService.re index ede2a62846..cd3cfc6b45 100755 --- a/src/service/record/main/material/basic/GroupBasicMaterialService.re +++ b/src/service/record/main/material/basic/GroupBasicMaterialService.re @@ -1,19 +1,19 @@ open BasicMaterialType; let isGroupBasicMaterial = (material, record) => - GroupService.isGroup(material, record.gameObjectsMap); + GameObjectsMapGroupService.isGroup(material, record.gameObjectsMap); let removeGameObject = (gameObject, material, {gameObjectsMap} as record) => { ...record, gameObjectsMap: - GroupService.removeGameObject(gameObject, material, gameObjectsMap), + GameObjectsMapGroupService.removeGameObject(gameObject, material, gameObjectsMap), }; let batchRemoveGameObjects = (gameObjectArr, material, {gameObjectsMap} as record) => { ...record, gameObjectsMap: - GroupService.batchRemoveGameObjects( + GameObjectsMapGroupService.batchRemoveGameObjects( gameObjectArr, material, gameObjectsMap, diff --git a/src/service/record/main/material/light/GroupLightMaterialService.re b/src/service/record/main/material/light/GroupLightMaterialService.re index 00d1efce43..3ad3042676 100755 --- a/src/service/record/main/material/light/GroupLightMaterialService.re +++ b/src/service/record/main/material/light/GroupLightMaterialService.re @@ -1,19 +1,19 @@ open LightMaterialType; let isGroupLightMaterial = (material, record) => - GroupService.isGroup(material, record.gameObjectsMap); + GameObjectsMapGroupService.isGroup(material, record.gameObjectsMap); let removeGameObject = (gameObject, material, {gameObjectsMap} as record) => { ...record, gameObjectsMap: - GroupService.removeGameObject(gameObject, material, gameObjectsMap), + GameObjectsMapGroupService.removeGameObject(gameObject, material, gameObjectsMap), }; let batchRemoveGameObjects = (gameObjectArr, material, {gameObjectsMap} as record) => { ...record, gameObjectsMap: - GroupService.batchRemoveGameObjects( + GameObjectsMapGroupService.batchRemoveGameObjects( gameObjectArr, material, gameObjectsMap, diff --git a/src/service/record/main/texture/GroupArrayBufferViewSourceTextureService.re b/src/service/record/main/texture/GroupArrayBufferViewSourceTextureService.re new file mode 100644 index 0000000000..058f925599 --- /dev/null +++ b/src/service/record/main/texture/GroupArrayBufferViewSourceTextureService.re @@ -0,0 +1,20 @@ +open ArrayBufferViewSourceTextureType; + +let addMaterial = (materialData, texture, record) => { + ...record, + materialsMap: + GroupTextureService.addMaterial( + materialData, + texture, + record.materialsMap, + ), +}; + +let isGroupArrayBufferViewSourceTexture = (texture, record) => + GroupTextureService.isGroup(texture, record.materialsMap); + +let removeMaterial = (materialData, texture, {materialsMap} as record) => { + ...record, + materialsMap: + GroupTextureService.removeMaterial(materialData, texture, materialsMap), +}; \ No newline at end of file diff --git a/src/service/record/main/texture/GroupBasicSourceTextureService.re b/src/service/record/main/texture/GroupBasicSourceTextureService.re new file mode 100644 index 0000000000..44ff7b8843 --- /dev/null +++ b/src/service/record/main/texture/GroupBasicSourceTextureService.re @@ -0,0 +1,20 @@ +open BasicSourceTextureType; + +let addMaterial = (materialData, texture, record) => { + ...record, + materialsMap: + GroupTextureService.addMaterial( + materialData, + texture, + record.materialsMap, + ), +}; + +let isGroupBasicSourceTexture = (texture, record) => + GroupTextureService.isGroup(texture, record.materialsMap); + +let removeMaterial = (materialData, texture, {materialsMap} as record) => { + ...record, + materialsMap: + GroupTextureService.removeMaterial(materialData, texture, materialsMap), +}; \ No newline at end of file diff --git a/src/service/record/main/texture/GroupTextureService.re b/src/service/record/main/texture/GroupTextureService.re new file mode 100644 index 0000000000..29b5d4acfc --- /dev/null +++ b/src/service/record/main/texture/GroupTextureService.re @@ -0,0 +1,11 @@ +let addMaterial = (materialData, texture, materialsMap) => + MaterialsMapService.addMaterial(materialData, texture, materialsMap); + +let isGroup = (texture, materialsMap) => + switch (MaterialsMapService.getMaterialDataArr(texture, materialsMap)) { + | Some(arr) when arr |> Js.Array.length > 0 => true + | _ => false + }; + +let removeMaterial = (materialData, texture, materialsMap) => + MaterialsMapService.removeMaterial(materialData, texture, materialsMap); \ No newline at end of file diff --git a/src/service/record/main/texture/MaterialsMapService.re b/src/service/record/main/texture/MaterialsMapService.re new file mode 100755 index 0000000000..bcb5a85b72 --- /dev/null +++ b/src/service/record/main/texture/MaterialsMapService.re @@ -0,0 +1,41 @@ +let getMaterialDataArr = (texture, materialsMap) => + WonderCommonlib.MutableSparseMapService.get(texture, materialsMap); + +let unsafeGetMaterialDataArr = (texture, materialsMap) => + WonderCommonlib.MutableSparseMapService.unsafeGet(texture, materialsMap) + |> WonderLog.Contract.ensureCheck( + materialData => + WonderLog.( + Contract.( + Operators.( + test( + Log.buildAssertMessage( + ~expect={j|texture's materialData exist|j}, + ~actual={j|not|j}, + ), + () => + materialData |> assertNullableExist + ) + ) + ) + ), + IsDebugMainService.getIsDebug(StateDataMain.stateData), + ); + +let addMaterial = ((material, materialType), texture, materialsMap) => + ArrayMapService.addValue(texture, (material, materialType), materialsMap); + +let removeMaterial = ((material, materialType), texture, materialsMap) => { + let (has, arr) = MutableSparseMapService.fastGet(texture, materialsMap); + + has ? + materialsMap + |> WonderCommonlib.MutableSparseMapService.set( + texture, + arr + |> Js.Array.filter(((materialInArr, materialTypeInArr)) => + materialTypeInArr !== materialType || materialInArr !== material + ), + ) : + materialsMap; +}; \ No newline at end of file diff --git a/src/service/state/main/material/DisposeMaterialMainService.re b/src/service/state/main/material/DisposeMaterialMainService.re index 27db86eeea..579fdc1935 100755 --- a/src/service/state/main/material/DisposeMaterialMainService.re +++ b/src/service/state/main/material/DisposeMaterialMainService.re @@ -21,4 +21,53 @@ let disposeTextureIndices = }; let isAlive = (material, disposedIndexArray) => - DisposeComponentService.isAlive(material, disposedIndexArray); \ No newline at end of file + DisposeComponentService.isAlive(material, disposedIndexArray); + +let addAllMaps = (mapArr, state) => + mapArr + |> Js.Array.filter(mapOpt => mapOpt |> Js.Option.isSome) + |> Js.Array.map(mapOpt => mapOpt |> OptionService.unsafeGet) + |> WonderCommonlib.ArrayService.reduceOneParam( + (. (basicSourceTextureArr, arrayBufferViewSourceTextureArr), map) => + IndexSourceTextureMainService.isBasicSourceTextureIndex(map, state) ? + ( + basicSourceTextureArr |> ArrayService.push(map), + arrayBufferViewSourceTextureArr, + ) : + IndexSourceTextureMainService.isArrayBufferViewSourceTextureIndex( + map, + state, + ) ? + ( + basicSourceTextureArr, + arrayBufferViewSourceTextureArr |> ArrayService.push(map), + ) : + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="addAllMaps", + ~description={j|unknown map: $map|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ), + ( + WonderCommonlib.ArrayService.createEmpty(), + WonderCommonlib.ArrayService.createEmpty(), + ), + ); + +let disposeMaps = (material, mapArr, state) => { + let (basicSourceTextureArr, arrayBufferViewSourceTextureArr) = + addAllMaps(mapArr, state); + + state + |> DisposeBasicSourceTextureMainService.handleBatchDispose( + material, + basicSourceTextureArr, + ) + |> DisposeArrayBufferViewSourceTextureMainService.handleBatchDispose( + material, + arrayBufferViewSourceTextureArr, + ); +}; \ No newline at end of file diff --git a/src/service/state/main/material/basic/DisposeBasicMaterialMainService.re b/src/service/state/main/material/basic/DisposeBasicMaterialMainService.re index 049345482d..7faaf668ad 100755 --- a/src/service/state/main/material/basic/DisposeBasicMaterialMainService.re +++ b/src/service/state/main/material/basic/DisposeBasicMaterialMainService.re @@ -11,17 +11,15 @@ open DisposeComponentService; let isAlive = (material, {disposedIndexArray}) => DisposeMaterialMainService.isAlive(material, disposedIndexArray); -/*! - not dispose texture when dispose material! - because different materials may use same texture, if dispose one material's texture which is shared, then will affect other materials! - - so need user mannually dispose texture! - */ -let _disposeData = - ( - material, - textureCountPerMaterial, - { +let _disposeData = (material, textureCountPerMaterial, state) => { + let state = + state + |> DisposeMaterialMainService.disposeMaps( + (material, MaterialType.BasicMaterial), + [|OperateBasicMaterialMainService.getMap(material, state)|], + ); + + let { shaderIndices, colors, textureIndices, @@ -32,8 +30,9 @@ let _disposeData = defaultColor, nameMap, gameObjectsMap, - } as basicMaterialRecord, - ) => { + } as basicMaterialRecord = + RecordBasicMaterialMainService.getRecord(state); + let shaderIndices = DisposeMaterialService.disposeData( material, @@ -42,42 +41,46 @@ let _disposeData = ); { - ...basicMaterialRecord, - shaderIndices, - colors: - DisposeTypeArrayService.deleteAndResetFloat32TypeArr(. - BufferBasicMaterialService.getColorIndex(material), - BufferBasicMaterialService.getColorsSize(), - defaultColor, - colors, - ), - textureIndices: - DisposeMaterialMainService.disposeTextureIndices( - material, - textureCountPerMaterial, - textureIndices, - ), - mapUnits: - DisposeTypeArrayService.deleteAndResetUint8(. - BufferBasicMaterialService.getMapUnitIndex(material), - MapUnitService.getDefaultUnit(), - mapUnits, - ), - isDepthTests: - DisposeTypeArrayService.deleteAndResetUint8(. - BufferBasicMaterialService.getIsDepthTestIndex(material), - BufferMaterialService.getDefaultIsDepthTest(), - isDepthTests, - ), - alphas: - DisposeTypeArrayService.deleteAndResetFloat32(. - BufferBasicMaterialService.getAlphaIndex(material), - BufferBasicMaterialService.getDefaultAlpha(), - alphas, - ), - emptyMapUnitArrayMap: - emptyMapUnitArrayMap |> disposeSparseMapData(material), - nameMap: nameMap |> disposeSparseMapData(material), + ...state, + basicMaterialRecord: + Some({ + ...basicMaterialRecord, + shaderIndices, + colors: + DisposeTypeArrayService.deleteAndResetFloat32TypeArr(. + BufferBasicMaterialService.getColorIndex(material), + BufferBasicMaterialService.getColorsSize(), + defaultColor, + colors, + ), + textureIndices: + DisposeMaterialMainService.disposeTextureIndices( + material, + textureCountPerMaterial, + textureIndices, + ), + mapUnits: + DisposeTypeArrayService.deleteAndResetUint8(. + BufferBasicMaterialService.getMapUnitIndex(material), + MapUnitService.getDefaultUnit(), + mapUnits, + ), + isDepthTests: + DisposeTypeArrayService.deleteAndResetUint8(. + BufferBasicMaterialService.getIsDepthTestIndex(material), + BufferMaterialService.getDefaultIsDepthTest(), + isDepthTests, + ), + alphas: + DisposeTypeArrayService.deleteAndResetFloat32(. + BufferBasicMaterialService.getAlphaIndex(material), + BufferBasicMaterialService.getDefaultAlpha(), + alphas, + ), + emptyMapUnitArrayMap: + emptyMapUnitArrayMap |> disposeSparseMapData(material), + nameMap: nameMap |> disposeSparseMapData(material), + }), }; }; @@ -99,44 +102,50 @@ let handleBatchDisposeComponentData = ), IsDebugMainService.getIsDebug(StateDataMain.stateData), ); - let {disposedIndexArray} as basicMaterialRecord = - RecordBasicMaterialMainService.getRecord(state); let textureCountPerMaterial = BufferSettingService.getTextureCountPerMaterial(settingRecord); - let basicMaterialRecord = - materialDataMap - |> WonderCommonlib.MutableSparseMapService.reduceiValid( - (. basicMaterialRecord, gameObjectArr, material) => { - let basicMaterialRecord = - GroupBasicMaterialService.batchRemoveGameObjects( - gameObjectArr, - material, - basicMaterialRecord, - ); + materialDataMap + |> WonderCommonlib.MutableSparseMapService.reduceiValid( + (. state, gameObjectArr, material) => { + let basicMaterialRecord = + RecordBasicMaterialMainService.getRecord(state); - GroupBasicMaterialService.isGroupBasicMaterial( + let basicMaterialRecord = + GroupBasicMaterialService.batchRemoveGameObjects( + gameObjectArr, material, basicMaterialRecord, - ) ? - basicMaterialRecord : - { - ... - basicMaterialRecord - |> _disposeData(material, textureCountPerMaterial), - disposedIndexArray: - DisposeMaterialService.addDisposeIndex( - material, - disposedIndexArray, - ), - }; - }, - basicMaterialRecord, - ); + ); + + GroupBasicMaterialService.isGroupBasicMaterial( + material, + basicMaterialRecord, + ) ? + {...state, basicMaterialRecord: Some(basicMaterialRecord)} : + { + let state = + state |> _disposeData(material, textureCountPerMaterial); - state.basicMaterialRecord = Some(basicMaterialRecord); + let basicMaterialRecord = + RecordBasicMaterialMainService.getRecord(state); - state; + { + ...state, + basicMaterialRecord: + Some({ + ...basicMaterialRecord, + disposedIndexArray: + DisposeMaterialService.addDisposeIndex( + material, + basicMaterialRecord.disposedIndexArray, + ), + }), + }; + }; + }, + state, + ); }; let handleBatchDisposeComponent = @@ -181,23 +190,27 @@ let handleBatchDisposeComponent = let textureCountPerMaterial = BufferSettingService.getTextureCountPerMaterial(settingRecord); - let basicMaterialRecord = - materialHasNoGameObjectArray - |> WonderCommonlib.ArrayService.reduceOneParam( - (. basicMaterialRecord, material) => { - ... - basicMaterialRecord - |> _disposeData(material, textureCountPerMaterial), - disposedIndexArray: - DisposeMaterialService.addDisposeIndex( - material, - disposedIndexArray, - ), - }, - basicMaterialRecord, - ); - - state.basicMaterialRecord = Some(basicMaterialRecord); - - state; + materialHasNoGameObjectArray + |> WonderCommonlib.ArrayService.reduceOneParam( + (. state, material) => { + let state = state |> _disposeData(material, textureCountPerMaterial); + + let basicMaterialRecord = + RecordBasicMaterialMainService.getRecord(state); + + { + ...state, + basicMaterialRecord: + Some({ + ...basicMaterialRecord, + disposedIndexArray: + DisposeMaterialService.addDisposeIndex( + material, + basicMaterialRecord.disposedIndexArray, + ), + }), + }; + }, + state, + ); }; \ No newline at end of file diff --git a/src/service/state/main/material/basic/mapManager/ManageMapBasicMaterialMainService.re b/src/service/state/main/material/basic/mapManager/ManageMapBasicMaterialMainService.re index e2b564c976..7958d5ebd8 100755 --- a/src/service/state/main/material/basic/mapManager/ManageMapBasicMaterialMainService.re +++ b/src/service/state/main/material/basic/mapManager/ManageMapBasicMaterialMainService.re @@ -20,6 +20,13 @@ let unsafeGetMap = (material, state) => getMap(material, state) |> OptionService.unsafeGet; let setMap = (material, texture, {settingRecord} as state) => { + let state = + state + |> GroupTextureMainService.addMaterial( + (material, MaterialType.BasicMaterial), + texture, + ); + let {textureIndices, mapUnits, emptyMapUnitArrayMap} as basicMaterialRecord = RecordBasicMaterialMainService.getRecord(state); let (textureIndices, mapUnits, emptyMapUnitArrayMap) = @@ -54,6 +61,13 @@ let hasMap = (material, state) => getMap(material, state) |> Js.Option.isSome; let removeMap = (material, {settingRecord} as state) => { + let state = + state + |> GroupTextureMainService.removeMaterial( + (material, MaterialType.BasicMaterial), + unsafeGetMap(material, state), + ); + let {textureIndices, mapUnits, emptyMapUnitArrayMap} as basicMaterialRecord = RecordBasicMaterialMainService.getRecord(state); let (textureIndices, mapUnits, emptyMapUnitArrayMap) = diff --git a/src/service/state/main/material/light/DisposeLightMaterialMainService.re b/src/service/state/main/material/light/DisposeLightMaterialMainService.re index f62e68c584..de2b198fe3 100755 --- a/src/service/state/main/material/light/DisposeLightMaterialMainService.re +++ b/src/service/state/main/material/light/DisposeLightMaterialMainService.re @@ -11,14 +11,18 @@ open DisposeComponentService; let isAlive = (material, {disposedIndexArray}) => DisposeMaterialMainService.isAlive(material, disposedIndexArray); -/*! - not dispose texture when dispose material! - */ -let _disposeData = - ( - material, - textureCountPerMaterial, - { +let _disposeData = (material, textureCountPerMaterial, state) => { + let state = + state + |> DisposeMaterialMainService.disposeMaps( + (material, MaterialType.LightMaterial), + [| + OperateLightMaterialMainService.getDiffuseMap(material, state), + OperateLightMaterialMainService.getSpecularMap(material, state), + |], + ); + + let { shaderIndices, diffuseColors, specularColors, @@ -32,8 +36,9 @@ let _disposeData = defaultShininess, nameMap, gameObjectsMap, - } as lightMaterialRecord, - ) => { + } as lightMaterialRecord = + RecordLightMaterialMainService.getRecord(state); + let shaderIndices = DisposeMaterialService.disposeData( material, @@ -42,49 +47,53 @@ let _disposeData = ); { - ...lightMaterialRecord, - shaderIndices, - diffuseColors: - DisposeTypeArrayService.deleteAndResetFloat32TypeArr(. - BufferLightMaterialService.getDiffuseColorIndex(material), - BufferLightMaterialService.getDiffuseColorsSize(), - defaultDiffuseColor, - diffuseColors, - ), - specularColors: - DisposeTypeArrayService.deleteAndResetFloat32TypeArr(. - BufferLightMaterialService.getSpecularColorIndex(material), - BufferLightMaterialService.getSpecularColorsSize(), - defaultSpecularColor, - specularColors, - ), - shininess: - DisposeTypeArrayService.deleteAndResetFloat32(. - BufferLightMaterialService.getShininessIndex(material), - defaultShininess, - shininess, - ), - textureIndices: - DisposeMaterialMainService.disposeTextureIndices( - material, - textureCountPerMaterial, - textureIndices, - ), - diffuseMapUnits: - DisposeTypeArrayService.deleteAndResetUint8(. - BufferLightMaterialService.getDiffuseMapUnitIndex(material), - MapUnitService.getDefaultUnit(), - diffuseMapUnits, - ), - specularMapUnits: - DisposeTypeArrayService.deleteAndResetUint8(. - BufferLightMaterialService.getDiffuseMapUnitIndex(material), - MapUnitService.getDefaultUnit(), - specularMapUnits, - ), - emptyMapUnitArrayMap: - emptyMapUnitArrayMap |> disposeSparseMapData(material), - nameMap: nameMap |> disposeSparseMapData(material), + ...state, + lightMaterialRecord: + Some({ + ...lightMaterialRecord, + shaderIndices, + diffuseColors: + DisposeTypeArrayService.deleteAndResetFloat32TypeArr(. + BufferLightMaterialService.getDiffuseColorIndex(material), + BufferLightMaterialService.getDiffuseColorsSize(), + defaultDiffuseColor, + diffuseColors, + ), + specularColors: + DisposeTypeArrayService.deleteAndResetFloat32TypeArr(. + BufferLightMaterialService.getSpecularColorIndex(material), + BufferLightMaterialService.getSpecularColorsSize(), + defaultSpecularColor, + specularColors, + ), + shininess: + DisposeTypeArrayService.deleteAndResetFloat32(. + BufferLightMaterialService.getShininessIndex(material), + defaultShininess, + shininess, + ), + textureIndices: + DisposeMaterialMainService.disposeTextureIndices( + material, + textureCountPerMaterial, + textureIndices, + ), + diffuseMapUnits: + DisposeTypeArrayService.deleteAndResetUint8(. + BufferLightMaterialService.getDiffuseMapUnitIndex(material), + MapUnitService.getDefaultUnit(), + diffuseMapUnits, + ), + specularMapUnits: + DisposeTypeArrayService.deleteAndResetUint8(. + BufferLightMaterialService.getDiffuseMapUnitIndex(material), + MapUnitService.getDefaultUnit(), + specularMapUnits, + ), + emptyMapUnitArrayMap: + emptyMapUnitArrayMap |> disposeSparseMapData(material), + nameMap: nameMap |> disposeSparseMapData(material), + }), }; }; @@ -106,44 +115,50 @@ let handleBatchDisposeComponentData = ), IsDebugMainService.getIsDebug(StateDataMain.stateData), ); - let {disposedIndexArray} as lightMaterialRecord = - RecordLightMaterialMainService.getRecord(state); let textureCountPerMaterial = BufferSettingService.getTextureCountPerMaterial(settingRecord); - let lightMaterialRecord = - materialDataMap - |> WonderCommonlib.MutableSparseMapService.reduceiValid( - (. lightMaterialRecord, gameObjectArr, material) => { - let lightMaterialRecord = - GroupLightMaterialService.batchRemoveGameObjects( - gameObjectArr, - material, - lightMaterialRecord, - ); + materialDataMap + |> WonderCommonlib.MutableSparseMapService.reduceiValid( + (. state, gameObjectArr, material) => { + let lightMaterialRecord = + RecordLightMaterialMainService.getRecord(state); - GroupLightMaterialService.isGroupLightMaterial( + let lightMaterialRecord = + GroupLightMaterialService.batchRemoveGameObjects( + gameObjectArr, material, lightMaterialRecord, - ) ? - lightMaterialRecord : - { - ... - lightMaterialRecord - |> _disposeData(material, textureCountPerMaterial), - disposedIndexArray: - DisposeMaterialService.addDisposeIndex( - material, - disposedIndexArray, - ), - }; - }, - lightMaterialRecord, - ); + ); + + GroupLightMaterialService.isGroupLightMaterial( + material, + lightMaterialRecord, + ) ? + {...state, lightMaterialRecord: Some(lightMaterialRecord)} : + { + let state = + state |> _disposeData(material, textureCountPerMaterial); - state.lightMaterialRecord = Some(lightMaterialRecord); + let lightMaterialRecord = + RecordLightMaterialMainService.getRecord(state); - state; + { + ...state, + lightMaterialRecord: + Some({ + ...lightMaterialRecord, + disposedIndexArray: + DisposeMaterialService.addDisposeIndex( + material, + lightMaterialRecord.disposedIndexArray, + ), + }), + }; + }; + }, + state, + ); }; let handleBatchDisposeComponent = @@ -183,28 +198,30 @@ let handleBatchDisposeComponent = IsDebugMainService.getIsDebug(StateDataMain.stateData), ); - let {disposedIndexArray} as lightMaterialRecord = - RecordLightMaterialMainService.getRecord(state); let textureCountPerMaterial = BufferSettingService.getTextureCountPerMaterial(settingRecord); - let lightMaterialRecord = - materialHasNoGameObjectArray - |> WonderCommonlib.ArrayService.reduceOneParam( - (. lightMaterialRecord, material) => { - ... - lightMaterialRecord - |> _disposeData(material, textureCountPerMaterial), - disposedIndexArray: - DisposeMaterialService.addDisposeIndex( - material, - disposedIndexArray, - ), - }, - lightMaterialRecord, - ); - - state.lightMaterialRecord = Some(lightMaterialRecord); - - state; + materialHasNoGameObjectArray + |> WonderCommonlib.ArrayService.reduceOneParam( + (. state, material) => { + let state = state |> _disposeData(material, textureCountPerMaterial); + + let lightMaterialRecord = + RecordLightMaterialMainService.getRecord(state); + + { + ...state, + lightMaterialRecord: + Some({ + ...lightMaterialRecord, + disposedIndexArray: + DisposeMaterialService.addDisposeIndex( + material, + lightMaterialRecord.disposedIndexArray, + ), + }), + }; + }, + state, + ); }; \ No newline at end of file diff --git a/src/service/state/main/material/light/mapManager/ManageMapLightMaterialMainService.re b/src/service/state/main/material/light/mapManager/ManageMapLightMaterialMainService.re index 3b6efaebe3..f7b41dc0f9 100755 --- a/src/service/state/main/material/light/mapManager/ManageMapLightMaterialMainService.re +++ b/src/service/state/main/material/light/mapManager/ManageMapLightMaterialMainService.re @@ -20,6 +20,13 @@ let unsafeGetDiffuseMap = (material, {settingRecord} as state) => getDiffuseMap(material, state) |> OptionService.unsafeGet; let setDiffuseMap = (material, texture, {settingRecord} as state) => { + let state = + state + |> GroupTextureMainService.addMaterial( + (material, MaterialType.LightMaterial), + texture, + ); + let {textureIndices, diffuseMapUnits, emptyMapUnitArrayMap} as lightMaterialRecord = RecordLightMaterialMainService.getRecord(state); let (textureIndices, diffuseMapUnits, emptyMapUnitArrayMap) = @@ -36,6 +43,7 @@ let setDiffuseMap = (material, texture, {settingRecord} as state) => { textureIndices, diffuseMapUnits, emptyMapUnitArrayMap, + /* (basicSourceTextureRecord, arrayBufferViewSourceTextureRecord), */ ), ); { @@ -54,6 +62,13 @@ let hasDiffuseMap = (material, {settingRecord} as state) => getDiffuseMap(material, state) |> Js.Option.isSome; let removeDiffuseMap = (material, {settingRecord} as state) => { + let state = + state + |> GroupTextureMainService.removeMaterial( + (material, MaterialType.LightMaterial), + unsafeGetDiffuseMap(material, state), + ); + let {textureIndices, diffuseMapUnits, emptyMapUnitArrayMap} as lightMaterialRecord = RecordLightMaterialMainService.getRecord(state); let (textureIndices, diffuseMapUnits, emptyMapUnitArrayMap) = @@ -101,6 +116,13 @@ let unsafeGetSpecularMap = (material, {settingRecord} as state) => getSpecularMap(material, state) |> OptionService.unsafeGet; let setSpecularMap = (material, texture, {settingRecord} as state) => { + let state = + state + |> GroupTextureMainService.addMaterial( + (material, MaterialType.LightMaterial), + texture, + ); + let {textureIndices, specularMapUnits, emptyMapUnitArrayMap} as lightMaterialRecord = RecordLightMaterialMainService.getRecord(state); let (textureIndices, specularMapUnits, emptyMapUnitArrayMap) = @@ -136,6 +158,13 @@ let hasSpecularMap = (material, {settingRecord} as state) => getSpecularMap(material, state) |> Js.Option.isSome; let removeSpecularMap = (material, {settingRecord} as state) => { + let state = + state + |> GroupTextureMainService.removeMaterial( + (material, MaterialType.LightMaterial), + unsafeGetDiffuseMap(material, state), + ); + let {textureIndices, specularMapUnits, emptyMapUnitArrayMap} as lightMaterialRecord = RecordLightMaterialMainService.getRecord(state); let (textureIndices, specularMapUnits, emptyMapUnitArrayMap) = diff --git a/src/service/state/main/material/mapManager/ManagerMapMaterialMainService.re b/src/service/state/main/material/mapManager/ManagerMapMaterialMainService.re index 5a69ff7c32..f31b7ce342 100755 --- a/src/service/state/main/material/mapManager/ManagerMapMaterialMainService.re +++ b/src/service/state/main/material/mapManager/ManagerMapMaterialMainService.re @@ -42,7 +42,11 @@ let setMap = ( material, texture, - (getMapUnitFunc, setMapUnitFunc, setTextureIndexFunc), + ( + getMapUnitFunc, + setMapUnitFunc, + setTextureIndexFunc, + ), ( textureCountPerMaterial, textureIndices, diff --git a/src/service/state/main/texture/DisposeTextureMainService.re b/src/service/state/main/texture/DisposeTextureMainService.re new file mode 100644 index 0000000000..f5ba871b2e --- /dev/null +++ b/src/service/state/main/texture/DisposeTextureMainService.re @@ -0,0 +1,17 @@ +/* TODO optimize: add gl texture to pool? */ +let disposeGlTextureMap = (texture, gl, glTextureMap) => + switch ( + glTextureMap |> WonderCommonlib.MutableSparseMapService.get(texture) + ) { + | Some(glTexture) => + gl |> WonderWebgl.Gl.deleteTexture(glTexture); + + glTextureMap |> WonderCommonlib.MutableSparseMapService.deleteVal(texture); + | None => glTextureMap + }; + +let disposeNeedAddedSourceArray = (texture, needAddedSourceArray) => + needAddedSourceArray + |> Js.Array.filter(((needAddedSourceTexture, _)) => + needAddedSourceTexture !== texture + ); \ No newline at end of file diff --git a/src/service/state/main/texture/GroupTextureMainService.re b/src/service/state/main/texture/GroupTextureMainService.re new file mode 100644 index 0000000000..720a83385f --- /dev/null +++ b/src/service/state/main/texture/GroupTextureMainService.re @@ -0,0 +1,77 @@ +open StateDataMainType; + +let addMaterial = (materialData, texture, state) => + IndexSourceTextureMainService.isBasicSourceTextureIndex(texture, state) ? + { + ...state, + basicSourceTextureRecord: + Some( + GroupBasicSourceTextureService.addMaterial( + materialData, + texture, + RecordBasicSourceTextureMainService.getRecord(state), + ), + ), + } : + IndexSourceTextureMainService.isArrayBufferViewSourceTextureIndex( + texture, + state, + ) ? + { + ...state, + arrayBufferViewSourceTextureRecord: + Some( + GroupArrayBufferViewSourceTextureService.addMaterial( + materialData, + texture, + RecordArrayBufferViewSourceTextureMainService.getRecord(state), + ), + ), + } : + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="addMaterial", + ~description={j|unknown texture: $texture|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ); + +let removeMaterial = (materialData, texture, state) => + IndexSourceTextureMainService.isBasicSourceTextureIndex(texture, state) ? + { + ...state, + basicSourceTextureRecord: + Some( + GroupBasicSourceTextureService.removeMaterial( + materialData, + texture, + RecordBasicSourceTextureMainService.getRecord(state), + ), + ), + } : + IndexSourceTextureMainService.isArrayBufferViewSourceTextureIndex( + texture, + state, + ) ? + { + ...state, + arrayBufferViewSourceTextureRecord: + Some( + GroupArrayBufferViewSourceTextureService.removeMaterial( + materialData, + texture, + RecordArrayBufferViewSourceTextureMainService.getRecord(state), + ), + ), + } : + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="removeMaterial", + ~description={j|unknown texture: $texture|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ); \ No newline at end of file diff --git a/src/service/state/main/texture/IndexSourceTextureMainService.re b/src/service/state/main/texture/IndexSourceTextureMainService.re index 5ad6a8b427..113649a810 100755 --- a/src/service/state/main/texture/IndexSourceTextureMainService.re +++ b/src/service/state/main/texture/IndexSourceTextureMainService.re @@ -7,15 +7,29 @@ let getBasicSourceTextureIndexOffset = () => let getArrayBufferViewSourceTextureIndexOffset = ({settingRecord} as state) => IndexSourceTextureService.getArrayBufferViewSourceTextureIndexOffset( - BufferSettingService.getBasicSourceTextureCount(settingRecord) + BufferSettingService.getBasicSourceTextureCount(settingRecord), ); -let generateBasicSourceTextureIndex = (basicSourceTextureIndex) => - IndexSourceTextureService.generateBasicSourceTextureIndex(basicSourceTextureIndex); +let generateBasicSourceTextureIndex = basicSourceTextureIndex => + IndexSourceTextureService.generateBasicSourceTextureIndex( + basicSourceTextureIndex, + ); let generateArrayBufferViewSourceTextureIndex = (arrayBufferViewSourceTextureIndex, {settingRecord} as state) => IndexSourceTextureService.generateArrayBufferViewSourceTextureIndex( arrayBufferViewSourceTextureIndex, - BufferSettingService.getBasicSourceTextureCount(settingRecord) + BufferSettingService.getBasicSourceTextureCount(settingRecord), + ); + +let isBasicSourceTextureIndex = (texture, state) => + IndexSourceTextureService.isBasicSourceTextureIndex( + texture, + getArrayBufferViewSourceTextureIndexOffset(state), + ); + +let isArrayBufferViewSourceTextureIndex = (texture, state) => + IndexSourceTextureService.isArrayBufferViewSourceTextureIndex( + texture, + getArrayBufferViewSourceTextureIndexOffset(state), ); \ No newline at end of file diff --git a/src/service/state/main/texture/arrayBufferView_source/DisposeArrayBufferViewSourceTextureMainService.re b/src/service/state/main/texture/arrayBufferView_source/DisposeArrayBufferViewSourceTextureMainService.re index f7050e359e..d51b424be4 100755 --- a/src/service/state/main/texture/arrayBufferView_source/DisposeArrayBufferViewSourceTextureMainService.re +++ b/src/service/state/main/texture/arrayBufferView_source/DisposeArrayBufferViewSourceTextureMainService.re @@ -1,5 +1,154 @@ -/* TODO add dispose logic +open StateDataMainType; - optimize: add gl texture to pool +open ArrayBufferViewSourceTextureType; - */ \ No newline at end of file +open DisposeComponentService; + +let _disposeData = + ( + texture, + gl, + { + wrapSs, + wrapTs, + magFilters, + minFilters, + formats, + types, + isNeedUpdates, + flipYs, + widths, + heights, + sourceMap, + glTextureMap, + bindTextureUnitCacheMap, + needAddedSourceArray, + needInitedTextureIndexArray, + nameMap, + } as arrayBufferViewSourceTextureRecord, + ) => { + ...arrayBufferViewSourceTextureRecord, + wrapSs: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getWrapSsSize(), + BufferArrayBufferViewSourceTextureService.getDefaultWrapS() + |> SourceTextureType.wrapToUint8, + wrapSs, + ), + wrapTs: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getWrapTsSize(), + BufferArrayBufferViewSourceTextureService.getDefaultWrapT() + |> SourceTextureType.wrapToUint8, + wrapTs, + ), + magFilters: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getMagFiltersSize(), + BufferArrayBufferViewSourceTextureService.getDefaultMagFilter() + |> SourceTextureType.filterToUint8, + magFilters, + ), + minFilters: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getMinFiltersSize(), + BufferArrayBufferViewSourceTextureService.getDefaultMinFilter() + |> SourceTextureType.filterToUint8, + minFilters, + ), + formats: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getFormatsSize(), + BufferArrayBufferViewSourceTextureService.getDefaultFormat() + |> SourceTextureType.formatToUint8, + formats, + ), + types: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getTypesSize(), + BufferArrayBufferViewSourceTextureService.getDefaultType(), + types, + ), + isNeedUpdates: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getIsNeedUpdatesSize(), + BufferArrayBufferViewSourceTextureService.getDefaultIsNeedUpdate(), + isNeedUpdates, + ), + flipYs: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getFlipYsSize(), + BufferArrayBufferViewSourceTextureService.getDefaultFlipY(), + flipYs, + ), + widths: + DisposeTypeArrayService.deleteAndResetUint16(. + texture * BufferSourceSizeTextureService.getWidthsSize(), + BufferArrayBufferViewSourceTextureService.getDefaultWidth(), + widths, + ), + heights: + DisposeTypeArrayService.deleteAndResetUint16(. + texture * BufferSourceSizeTextureService.getHeightsSize(), + BufferArrayBufferViewSourceTextureService.getDefaultHeight(), + heights, + ), + sourceMap: sourceMap |> disposeSparseMapData(texture), + bindTextureUnitCacheMap: + bindTextureUnitCacheMap |> disposeSparseMapData(texture), + glTextureMap: + DisposeTextureMainService.disposeGlTextureMap(texture, gl, glTextureMap), + needAddedSourceArray: + DisposeTextureMainService.disposeNeedAddedSourceArray( + texture, + needAddedSourceArray, + ), + needInitedTextureIndexArray: + needInitedTextureIndexArray + |> Js.Array.filter(needInitedTexture => needInitedTexture !== texture), + nameMap: nameMap |> disposeSparseMapData(texture), +}; + +let handleBatchDispose = (materialData, textureArr, state) => { + let gl = DeviceManagerService.unsafeGetGl(. state.deviceManagerRecord); + + let arrayBufferViewSourceTextureRecord = + textureArr + |> WonderCommonlib.ArrayService.reduceOneParam( + (. arrayBufferViewSourceTextureRecord, texture) => { + let arrayBufferViewSourceTextureRecord = + GroupArrayBufferViewSourceTextureService.removeMaterial( + materialData, + texture, + arrayBufferViewSourceTextureRecord, + ); + + GroupArrayBufferViewSourceTextureService.isGroupArrayBufferViewSourceTexture( + texture, + arrayBufferViewSourceTextureRecord, + ) ? + arrayBufferViewSourceTextureRecord : + { + let arrayBufferViewSourceTextureRecord = + arrayBufferViewSourceTextureRecord + |> _disposeData(texture, gl); + + { + ...arrayBufferViewSourceTextureRecord, + disposedIndexArray: + DisposeMaterialService.addDisposeIndex( + texture, + arrayBufferViewSourceTextureRecord.disposedIndexArray, + ), + }; + }; + }, + RecordArrayBufferViewSourceTextureMainService.getRecord(state), + ); + + { + ...state, + arrayBufferViewSourceTextureRecord: + Some(arrayBufferViewSourceTextureRecord), + }; +}; \ No newline at end of file diff --git a/src/service/state/main/texture/arrayBufferView_source/RecordArrayBufferViewSourceTextureMainService.re b/src/service/state/main/texture/arrayBufferView_source/RecordArrayBufferViewSourceTextureMainService.re index 9c715eef9b..cc74f49335 100755 --- a/src/service/state/main/texture/arrayBufferView_source/RecordArrayBufferViewSourceTextureMainService.re +++ b/src/service/state/main/texture/arrayBufferView_source/RecordArrayBufferViewSourceTextureMainService.re @@ -206,8 +206,10 @@ let create = ({settingRecord} as state) => { heights, sourceMap: WonderCommonlib.MutableSparseMapService.createEmpty(), nameMap: WonderCommonlib.MutableSparseMapService.createEmpty(), + materialsMap: WonderCommonlib.MutableSparseMapService.createEmpty(), glTextureMap: WonderCommonlib.MutableSparseMapService.createEmpty(), - bindTextureUnitCacheMap: WonderCommonlib.MutableSparseMapService.createEmpty(), + bindTextureUnitCacheMap: + WonderCommonlib.MutableSparseMapService.createEmpty(), disposedIndexArray: WonderCommonlib.ArrayService.createEmpty(), needAddedSourceArray: [||], needInitedTextureIndexArray: [||], @@ -231,6 +233,7 @@ let deepCopyForRestore = ({settingRecord} as state) => { widths, heights, nameMap, + materialsMap, sourceMap, glTextureMap, bindTextureUnitCacheMap, @@ -296,8 +299,12 @@ let deepCopyForRestore = ({settingRecord} as state) => { index * getHeightsSize(), ), nameMap: nameMap |> WonderCommonlib.MutableSparseMapService.copy, + /* TODO test */ + materialsMap: + materialsMap |> WonderCommonlib.MutableSparseMapService.copy, sourceMap: sourceMap |> WonderCommonlib.MutableSparseMapService.copy, - glTextureMap: glTextureMap |> WonderCommonlib.MutableSparseMapService.copy, + glTextureMap: + glTextureMap |> WonderCommonlib.MutableSparseMapService.copy, bindTextureUnitCacheMap: WonderCommonlib.MutableSparseMapService.createEmpty(), disposedIndexArray: disposedIndexArray |> Js.Array.copy, diff --git a/src/service/state/main/texture/basic_source/DisposeBasicSourceTextureMainService.re b/src/service/state/main/texture/basic_source/DisposeBasicSourceTextureMainService.re index f7050e359e..ea4703d8cf 100755 --- a/src/service/state/main/texture/basic_source/DisposeBasicSourceTextureMainService.re +++ b/src/service/state/main/texture/basic_source/DisposeBasicSourceTextureMainService.re @@ -1,5 +1,135 @@ -/* TODO add dispose logic +open StateDataMainType; - optimize: add gl texture to pool +open BasicSourceTextureType; - */ \ No newline at end of file +open DisposeComponentService; + +let _disposeData = + ( + texture, + gl, + { + wrapSs, + wrapTs, + magFilters, + minFilters, + formats, + types, + isNeedUpdates, + flipYs, + sourceMap, + glTextureMap, + bindTextureUnitCacheMap, + needAddedSourceArray, + needInitedTextureIndexArray, + nameMap, + } as basicSourceTextureRecord, + ) => { + ...basicSourceTextureRecord, + wrapSs: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getWrapSsSize(), + BufferBasicSourceTextureService.getDefaultWrapS() + |> SourceTextureType.wrapToUint8, + wrapSs, + ), + wrapTs: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getWrapTsSize(), + BufferBasicSourceTextureService.getDefaultWrapT() + |> SourceTextureType.wrapToUint8, + wrapTs, + ), + magFilters: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getMagFiltersSize(), + BufferBasicSourceTextureService.getDefaultMagFilter() + |> SourceTextureType.filterToUint8, + magFilters, + ), + minFilters: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getMinFiltersSize(), + BufferBasicSourceTextureService.getDefaultMinFilter() + |> SourceTextureType.filterToUint8, + minFilters, + ), + formats: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getFormatsSize(), + BufferBasicSourceTextureService.getDefaultFormat() + |> SourceTextureType.formatToUint8, + formats, + ), + types: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getTypesSize(), + BufferBasicSourceTextureService.getDefaultType(), + types, + ), + isNeedUpdates: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getIsNeedUpdatesSize(), + BufferBasicSourceTextureService.getDefaultIsNeedUpdate(), + isNeedUpdates, + ), + flipYs: + DisposeTypeArrayService.deleteAndResetUint8(. + texture * BufferSourceSizeTextureService.getFlipYsSize(), + BufferBasicSourceTextureService.getDefaultFlipY(), + flipYs, + ), + sourceMap: sourceMap |> disposeSparseMapData(texture), + bindTextureUnitCacheMap: + bindTextureUnitCacheMap |> disposeSparseMapData(texture), + glTextureMap: + DisposeTextureMainService.disposeGlTextureMap(texture, gl, glTextureMap), + needAddedSourceArray: + DisposeTextureMainService.disposeNeedAddedSourceArray( + texture, + needAddedSourceArray, + ), + needInitedTextureIndexArray: + needInitedTextureIndexArray + |> Js.Array.filter(needInitedTexture => needInitedTexture !== texture), + nameMap: nameMap |> disposeSparseMapData(texture), +}; + +let handleBatchDispose = (materialData, textureArr, state) => { + let gl = DeviceManagerService.unsafeGetGl(. state.deviceManagerRecord); + + let basicSourceTextureRecord = + textureArr + |> WonderCommonlib.ArrayService.reduceOneParam( + (. basicSourceTextureRecord, texture) => { + let basicSourceTextureRecord = + GroupBasicSourceTextureService.removeMaterial( + materialData, + texture, + basicSourceTextureRecord, + ); + + GroupBasicSourceTextureService.isGroupBasicSourceTexture( + texture, + basicSourceTextureRecord, + ) ? + basicSourceTextureRecord : + { + let basicSourceTextureRecord = + basicSourceTextureRecord |> _disposeData(texture, gl); + + { + ...basicSourceTextureRecord, + disposedIndexArray: + DisposeMaterialService.addDisposeIndex( + texture, + basicSourceTextureRecord.disposedIndexArray, + ), + }; + }; + }, + RecordBasicSourceTextureMainService.getRecord(state), + ); + + {...state, basicSourceTextureRecord: Some(basicSourceTextureRecord)}; +}; \ No newline at end of file diff --git a/src/service/state/main/texture/basic_source/RecordBasicSourceTextureMainService.re b/src/service/state/main/texture/basic_source/RecordBasicSourceTextureMainService.re index 308124932e..e063272d96 100755 --- a/src/service/state/main/texture/basic_source/RecordBasicSourceTextureMainService.re +++ b/src/service/state/main/texture/basic_source/RecordBasicSourceTextureMainService.re @@ -162,6 +162,7 @@ let create = ({settingRecord} as state) => { isNeedUpdates, flipYs, nameMap: WonderCommonlib.MutableSparseMapService.createEmpty(), + materialsMap: WonderCommonlib.MutableSparseMapService.createEmpty(), sourceMap: WonderCommonlib.MutableSparseMapService.createEmpty(), glTextureMap: WonderCommonlib.MutableSparseMapService.createEmpty(), bindTextureUnitCacheMap: @@ -187,6 +188,7 @@ let deepCopyForRestore = ({settingRecord} as state) => { isNeedUpdates, flipYs, nameMap, + materialsMap, sourceMap, glTextureMap, bindTextureUnitCacheMap, @@ -242,8 +244,12 @@ let deepCopyForRestore = ({settingRecord} as state) => { index * getFlipYsSize(), ), nameMap: nameMap |> WonderCommonlib.MutableSparseMapService.copy, + /* TODO test */ + materialsMap: + materialsMap |> WonderCommonlib.MutableSparseMapService.copy, sourceMap: sourceMap |> WonderCommonlib.MutableSparseMapService.copy, - glTextureMap: glTextureMap |> WonderCommonlib.MutableSparseMapService.copy, + glTextureMap: + glTextureMap |> WonderCommonlib.MutableSparseMapService.copy, bindTextureUnitCacheMap: WonderCommonlib.MutableSparseMapService.createEmpty(), disposedIndexArray: disposedIndexArray |> Js.Array.copy,