breakin 4 роки тому
батько
коміт
1a2c00846d
4 змінених файлів з 36 додано та 25 видалено
  1. 6 2
      src/Engines/thinEngine.ts
  2. 7 10
      src/Meshes/geometry.ts
  3. 9 12
      src/Meshes/instancedMesh.ts
  4. 14 1
      src/Meshes/mesh.ts

+ 6 - 2
src/Engines/thinEngine.ts

@@ -1780,13 +1780,17 @@ export class ThinEngine {
 
             if (order >= 0) {
                 var ai = attributes[index];
-                var vertexBuffer = vertexBuffers[ai];
+                var vertexBuffer: Nullable<VertexBuffer> = null;
 
-                if (!vertexBuffer && extraVertexBuffers) {
+                if (extraVertexBuffers) {
                     vertexBuffer = extraVertexBuffers[ai];
                 }
 
                 if (!vertexBuffer) {
+                    vertexBuffer = vertexBuffers[ai];
+                }
+
+                if (!vertexBuffer) {
                     continue;
                 }
 

+ 7 - 10
src/Meshes/geometry.ts

@@ -216,13 +216,6 @@ export class Geometry implements IGetSetVerticesData {
             let vertexBuffer = <VertexBuffer>this._vertexBuffers[key];
             vertexBuffer._rebuild();
         }
-
-        // Invalidate MESH VAO
-        const meshes = this.meshes
-        const numMeshes = meshes.length
-        for (let i = 0; i < numMeshes; i++) {
-            meshes[i].invalidateVAO(true);
-        }
     }
 
     /**
@@ -302,7 +295,7 @@ export class Geometry implements IGetSetVerticesData {
 
         var meshes = this._meshes;
         for (var index = 0; index < numOfMeshes; index++) {
-            meshes[index].invalidateVAO(false);
+            meshes[index].invalidateInstanceVertexArrayObject(false);
         }
 
         if (this._vertexArrayObjects) {
@@ -930,9 +923,13 @@ export class Geometry implements IGetSetVerticesData {
                 this._engine.releaseVertexArrayObject(this._vertexArrayObjects[kind]);
             }
             this._vertexArrayObjects = {};
-        }
 
-        // TODO: Do the meshes too?
+            const meshes = this.meshes
+            const numMeshes = meshes.length
+            for (let i = 0; i < numMeshes; i++) {
+                meshes[i].invalidateInstanceVertexArrayObject(true);
+            }
+        }
     }
 
     /**

+ 9 - 12
src/Meshes/instancedMesh.ts

@@ -490,7 +490,7 @@ declare module "./mesh" {
          */
         registerInstancedBuffer(kind: string, stride: number): void;
 
-        invalidateVAO(lostContext: boolean): void;
+        invalidateInstanceVertexArrayObject(): void;
 
         /**
          * true to use the edge renderer for all instances of this mesh
@@ -503,7 +503,7 @@ declare module "./mesh" {
             sizes: {[key: string]: number},
             vertexBuffers: {[key: string]: Nullable<VertexBuffer>},
             strides: {[key: string]: number},
-            vertexArrayObjects: {[key: string]: WebGLVertexArrayObject}
+            vertexArrayObjects?: {[key: string]: WebGLVertexArrayObject}
         };
     }
 }
@@ -539,7 +539,7 @@ Mesh.prototype.registerInstancedBuffer = function(kind: string, stride: number):
             vertexBuffers: {},
             strides: {},
             sizes: {},
-            vertexArrayObjects: {}
+            vertexArrayObjects: (this.getEngine().getCaps().vertexArrayObject) ? {} : undefined
         };
     }
 
@@ -555,7 +555,7 @@ Mesh.prototype.registerInstancedBuffer = function(kind: string, stride: number):
         instance.instancedBuffers[kind] = null;
     }
 
-    this.invalidateVAO(false);
+    this.invalidateInstanceVertexArrayObject();
 };
 
 Mesh.prototype._processInstancedBuffers = function(visibleInstances: InstancedMesh[], renderSelf: boolean) {
@@ -614,26 +614,23 @@ Mesh.prototype._processInstancedBuffers = function(visibleInstances: InstancedMe
         // Update vertex buffer
         if (!this._userInstancedBuffersStorage.vertexBuffers[kind]) {
             this._userInstancedBuffersStorage.vertexBuffers[kind] = new VertexBuffer(this.getEngine(), this._userInstancedBuffersStorage.data[kind], kind, true, false, stride, true);
-            this.invalidateVAO(false);
+            this.invalidateInstanceVertexArrayObject();
         } else {
             this._userInstancedBuffersStorage.vertexBuffers[kind]!.updateDirectly(data, 0);
         }
     }
 };
 
-Mesh.prototype.invalidateVAO = function(lostContext: boolean) {
-    if (!this._userInstancedBuffersStorage) {
+Mesh.prototype.invalidateInstanceVertexArrayObject = function() {
+    if (!this._userInstancedBuffersStorage || this._userInstancedBuffersStorage.vertexArrayObjects === undefined) {
         return;
     }
 
-    if (!lostContext) {
-        for (var kind in this._userInstancedBuffersStorage.vertexArrayObjects) {
-            this.getEngine().releaseVertexArrayObject(this._userInstancedBuffersStorage.vertexArrayObjects[kind]);
-        }
+    for (var kind in this._userInstancedBuffersStorage.vertexArrayObjects) {
+        this.getEngine().releaseVertexArrayObject(this._userInstancedBuffersStorage.vertexArrayObjects[kind]);
     }
 
     this._userInstancedBuffersStorage.vertexArrayObjects = {}
-    console.log('Invalidate mesh VAO')
 }
 
 Mesh.prototype._disposeInstanceSpecificData = function() {

+ 14 - 1
src/Meshes/mesh.ts

@@ -1688,7 +1688,7 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
                     vertexBuffers: {},
                     strides: {},
                     sizes: {},
-                    vertexArrayObjects: {}
+                    vertexArrayObjects: (this.getEngine().getCaps().vertexArrayObject) ? {} : undefined
                 };
             }
 
@@ -1792,6 +1792,19 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
             this._instanceDataStorage.instancesBuffer.dispose();
             this._instanceDataStorage.instancesBuffer = null;
         }
+        if (this._userInstancedBuffersStorage) {
+            for (var kind in this._userInstancedBuffersStorage.vertexBuffers) {
+                var buffer = this._userInstancedBuffersStorage.vertexBuffers[kind];
+                if (buffer) {
+                    // Dispose instance buffer to be recreated in _renderWithInstances when rendered
+                    buffer.dispose();
+                    this._userInstancedBuffersStorage.vertexBuffers[kind] = null;
+                }
+            }
+            if (this._userInstancedBuffersStorage.vertexArrayObjects) {
+                this._userInstancedBuffersStorage.vertexArrayObjects = {}
+            }
+        }
         super._rebuild();
     }