Quellcode durchsuchen

Support Primitive Model Change

Don't release loaded Effect for performance's sake
nockawa vor 9 Jahren
Ursprung
Commit
a06c4a95ce

+ 4 - 19
src/Canvas2d/babylon.ellipse2d.ts

@@ -127,15 +127,10 @@
                 this.fillIB = null;
             }
 
-            if (this.effectFill) {
-                this._engine._releaseEffect(this.effectFill);
-                this.effectFill = null;
-            }
-
-            if (this.effectFillInstanced) {
-                this._engine._releaseEffect(this.effectFillInstanced);
-                this.effectFillInstanced = null;
-            }
+            this.effectFill = null;
+            this.effectFillInstanced = null;
+            this.effectBorder = null;
+            this.effectBorderInstanced = null;
 
             if (this.borderVB) {
                 this._engine._releaseBuffer(this.borderVB);
@@ -147,16 +142,6 @@
                 this.borderIB = null;
             }
 
-            if (this.effectBorder) {
-                this._engine._releaseEffect(this.effectBorder);
-                this.effectBorder = null;
-            }
-
-            if (this.effectBorderInstanced) {
-                this._engine._releaseEffect(this.effectBorderInstanced);
-                this.effectBorderInstanced = null;
-            }
-
             return true;
         }
     }

+ 13 - 21
src/Canvas2d/babylon.lines2d.ts

@@ -126,15 +126,10 @@
                 this.fillIB = null;
             }
 
-            if (this.effectFill) {
-                this._engine._releaseEffect(this.effectFill);
-                this.effectFill = null;
-            }
-
-            if (this.effectFillInstanced) {
-                this._engine._releaseEffect(this.effectFillInstanced);
-                this.effectFillInstanced = null;
-            }
+            this.effectFill = null;
+            this.effectFillInstanced = null;
+            this.effectBorder = null;
+            this.effectBorderInstanced = null;
 
             if (this.borderVB) {
                 this._engine._releaseBuffer(this.borderVB);
@@ -146,16 +141,6 @@
                 this.borderIB = null;
             }
 
-            if (this.effectBorder) {
-                this._engine._releaseEffect(this.effectBorder);
-                this.effectBorder = null;
-            }
-
-            if (this.effectBorderInstanced) {
-                this._engine._releaseEffect(this.effectBorderInstanced);
-                this.effectBorderInstanced = null;
-            }
-
             return true;
         }
     }
@@ -233,6 +218,7 @@
 
         public set points(value: Vector2[]) {
             this._points = value;
+            this._contour = null;
             this._boundingBoxDirty();
         }
 
@@ -342,10 +328,10 @@
             }
 
             if (this._startCapTriIndices) {
-                if (capIntersect(this._startCapTriIndices, this._startCapContour)) {
+                if (this._startCapTriIndices && capIntersect(this._startCapTriIndices, this._startCapContour)) {
                     return true;
                 }
-                if (capIntersect(this._endCapTriIndices, this._endCapContour)) {
+                if (this._endCapTriIndices && capIntersect(this._endCapTriIndices, this._endCapContour)) {
                     return true;
                 }
             }
@@ -1168,11 +1154,17 @@
                 let startCapTri = Earcut.earcut(startCapContour, null, 2);
                 this._startCapTriIndices = startCapTri;
                 this._startCapContour = startCapContour;
+            } else {
+                this._startCapTriIndices = null;
+                this._startCapContour = null;
             }
             if (endCapContour.length > 0) {
                 let endCapTri = Earcut.earcut(endCapContour, null, 2);
                 this._endCapContour = endCapContour;
                 this._endCapTriIndices = endCapTri;
+            } else {
+                this._endCapContour = null;
+                this._endCapTriIndices = null;
             }
             let bs = this._boundingMax.subtract(this._boundingMin);
             this._size.width = bs.x;

+ 0 - 20
src/Canvas2d/babylon.modelRenderCache.ts

@@ -191,7 +191,6 @@
             this._modelKey = modelKey;
             this._nextKey = 1;
             this._refCounter = 1;
-            this._instancesData = new StringDictionary<InstanceDataBase[]>();
             this._partData = null;
         }
 
@@ -231,22 +230,6 @@
             return true;
         }
 
-        addInstanceDataParts(data: InstanceDataBase[]): string {
-            let key = this._nextKey.toString();
-
-            if (!this._instancesData.add(key, data)) {
-                throw Error(`Key: ${key} is already allocated`);
-            }
-
-            ++this._nextKey;
-
-            return key;
-        }
-
-        removeInstanceData(key: string) {
-            this._instancesData.remove(key);
-        }
-
         protected getPartIndexFromId(partId: number) {
             for (var i = 0; i < this._partData.length; i++) {
                 if (this._partData[i]._partId === partId) {
@@ -339,9 +322,6 @@
 
         protected _engine: Engine;
         private _modelKey: string;
-
-        _instancesData: StringDictionary<InstanceDataBase[]>;
-
         private _nextKey: number;
         private _refCounter: number;
 

+ 2 - 0
src/Canvas2d/babylon.prim2dBase.ts

@@ -1393,6 +1393,8 @@
                 this._opacity = 1;
             }
 
+            this._updateRenderMode();
+
             if (settings.childrenFlatZOrder) {
                 this._setFlags(SmartPropertyPrim.flagChildrenFlatZOrder);
             }

+ 4 - 18
src/Canvas2d/babylon.rectangle2d.ts

@@ -127,15 +127,10 @@
                 this.fillIB = null;
             }
 
-            if (this.effectFill) {
-                this._engine._releaseEffect(this.effectFill);
-                this.effectFill = null;
-            }
-
-            if (this.effectFillInstanced) {
-                this._engine._releaseEffect(this.effectFillInstanced);
-                this.effectFillInstanced = null;
-            }
+            this.effectFill = null;
+            this.effectFillInstanced = null;
+            this.effectBorder = null;
+            this.effectBorderInstanced = null;
 
             if (this.borderVB) {
                 this._engine._releaseBuffer(this.borderVB);
@@ -147,15 +142,6 @@
                 this.borderIB = null;
             }
 
-            if (this.effectBorder) {
-                this._engine._releaseEffect(this.effectBorder);
-                this.effectBorder = null;
-            }
-
-            if (this.effectBorderInstanced) {
-                this._engine._releaseEffect(this.effectBorderInstanced);
-                this.effectBorderInstanced = null;
-            }
 
             return true;
         }

+ 38 - 14
src/Canvas2d/babylon.renderablePrim2d.ts

@@ -251,7 +251,7 @@
         constructor(partId: number, dataElementCount: number) {
             this.id = partId;
             this.curElement = 0;
-            this.dataElementCount = dataElementCount;
+            this._dataElementCount = dataElementCount;
             this.renderMode = 0;
             this.arrayLengthChanged = false;
         }
@@ -321,6 +321,7 @@
             this._dataElementCount = value;
             this.allocElements();
         }
+        groupInstanceInfo: GroupInstanceInfo;
         arrayLengthChanged: boolean;
         curElement: number;
         renderMode: number;
@@ -329,7 +330,6 @@
         typeInfo: ClassTreeInfo<InstanceClassInfo, InstancePropInfo>;
 
         private _dataElementCount: number;
-
     }
 
     @className("RenderablePrim2D")
@@ -408,9 +408,8 @@
                 this._transparentPrimitiveInfo = null;
             }
 
-            if (this._modelRenderInstanceID) {
-                this._modelRenderCache.removeInstanceData(this._modelRenderInstanceID);
-                this._modelRenderInstanceID = null;
+            if (this._instanceDataParts) {
+                this._cleanupInstanceDataParts();
             }
 
             if (this._modelRenderCache) {
@@ -428,13 +427,41 @@
             return true;
         }
 
+        private _cleanupInstanceDataParts() {
+            let gii: GroupInstanceInfo = null;
+            for (let part of this._instanceDataParts) {
+                part.freeElements();
+                gii = part.groupInstanceInfo;
+            }
+            if (gii) {
+                let usedCount = 0;
+                if (gii.hasOpaqueData) {
+                    let od = gii.opaqueData[0];
+                    usedCount += od._partData.usedElementCount;
+                }
+                if (gii.hasAlphaTestData) {
+                    let atd = gii.alphaTestData[0];
+                    usedCount += atd._partData.usedElementCount;
+                }
+                if (gii.hasTransparentData) {
+                    let td = gii.transparentData[0];
+                    usedCount += td._partData.usedElementCount;
+                }
+
+                if (usedCount === 0) {
+                    this.renderGroup._renderableData._renderGroupInstancesInfo.remove(gii.modelRenderCache.modelKey);
+                }
+                gii.modelRenderCache.dispose();
+            }
+            this._instanceDataParts = null;
+        }
+
         public _prepareRenderPre(context: PrepareRender2DContext) {
             super._prepareRenderPre(context);
 
             // If the model changed and we have already an instance, we must remove this instance from the obsolete model
-            if (this._isFlagSet(SmartPropertyPrim.flagModelDirty) && this._modelRenderInstanceID) {
-                this._modelRenderCache.removeInstanceData(this._modelRenderInstanceID);
-                this._modelRenderInstanceID = null;
+            if (this._isFlagSet(SmartPropertyPrim.flagModelDirty) && this._instanceDataParts) {
+                this._cleanupInstanceDataParts();
             }
 
             // Need to create the model?
@@ -447,7 +474,7 @@
             let newInstance = false;
 
             // Need to create the instance data parts?
-            if (!this._modelRenderInstanceID) {
+            if (!this._instanceDataParts) {
                 // Yes, flag it for later, more processing will have to be done
                 newInstance = true;
                 gii = this._createModelDataParts();
@@ -535,11 +562,9 @@
                 part.dataBuffer = gipd[i]._partData;
                 part.allocElements();
                 part.renderMode = rm;
+                part.groupInstanceInfo = gii;
             }
 
-            // Add the instance data parts in the ModelRenderCache they belong, track them by storing their ID in the primitive in case we need to change the model later on, so we'll have to release the allocated instance data parts because they won't fit anymore
-            this._modelRenderInstanceID = this._modelRenderCache.addInstanceDataParts(this._instanceDataParts);
-
             return gii;
         }
 
@@ -666,7 +691,7 @@
             // For each Instance Data part, refresh it to update the data in the DynamicFloatArray
             for (let part of this._instanceDataParts) {
                 // Check if we need to allocate data elements (hidden prim which becomes visible again)
-                if (visChanged || !part.dataElements || rmChanged) {
+                if (!part.dataElements && (visChanged || rmChanged)) {
                     part.allocElements();
                 }
 
@@ -913,7 +938,6 @@
         }
 
         private _modelRenderCache: ModelRenderCache;
-        private _modelRenderInstanceID: string;
         private _transparentPrimitiveInfo: TransparentPrimitiveInfo;
 
         protected _instanceDataParts: InstanceDataBase[];

+ 3 - 1
src/Canvas2d/babylon.smartPropertyPrim.ts

@@ -404,7 +404,9 @@
 
             // Set the dirty flags
             if (!instanceDirty && (propInfo.kind === Prim2DPropInfo.PROPKIND_MODEL)) {
-                this.onPrimitivePropertyDirty(SmartPropertyPrim.flagModelDirty);
+                if (!this.isDirty) {
+                    this._setFlags(SmartPropertyPrim.flagModelDirty);
+                }
             } else if (instanceDirty || (propInfo.kind === Prim2DPropInfo.PROPKIND_INSTANCE) || (propInfo.kind === Prim2DPropInfo.PROPKIND_DYNAMIC)) {
                 this.onPrimitivePropertyDirty(propMask);
             }

+ 2 - 9
src/Canvas2d/babylon.sprite2d.ts

@@ -76,15 +76,8 @@
                 this.texture = null;
             }
 
-            if (this.effect) {
-                this._engine._releaseEffect(this.effect);
-                this.effect = null;
-            }
-
-            if (this.effectInstanced) {
-                this._engine._releaseEffect(this.effectInstanced);
-                this.effectInstanced = null;
-            }
+            this.effect = null;
+            this.effectInstanced = null;
 
             return true;
         }

+ 2 - 9
src/Canvas2d/babylon.text2d.ts

@@ -76,15 +76,8 @@
                 this.fontTexture = null;
             }
 
-            if (this.effect) {
-                this._engine._releaseEffect(this.effect);
-                this.effect = null;
-            }
-
-            if (this.effectInstanced) {
-                this._engine._releaseEffect(this.effectInstanced);
-                this.effectInstanced = null;
-            }
+            this.effect = null;
+            this.effectInstanced = null;
 
             return true;
         }