From 9e121dd10b5f1c4af9c3a43d5a4ab42b3376fdf5 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alexander.rose@weirdbyte.de>
Date: Thu, 22 Nov 2018 13:11:24 -0800
Subject: [PATCH] vao update improvements

---
 src/mol-gl/webgl/render-item.ts  | 19 ++++++++++++++-----
 src/mol-gl/webgl/vertex-array.ts | 12 +++++++++++-
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/src/mol-gl/webgl/render-item.ts b/src/mol-gl/webgl/render-item.ts
index bc7d302b1..7738c7cc3 100644
--- a/src/mol-gl/webgl/render-item.ts
+++ b/src/mol-gl/webgl/render-item.ts
@@ -216,11 +216,20 @@ export function createRenderItem(ctx: WebGLContext, drawMode: DrawMode, shaderCo
             }
 
             if (valueChanges.attributes || valueChanges.defines || valueChanges.elements) {
-                // console.log('program/defines or buffers changed, rebuild vaos')
-                Object.keys(RenderVariantDefines).forEach(k => {
-                    deleteVertexArray(ctx, vertexArrays[k])
-                    vertexArrays[k] = createVertexArray(ctx, programs[k].value, attributeBuffers, elementsBuffer)
-                })
+                // console.log('program/defines or buffers changed, update vaos')
+                const { vertexArrayObject } = ctx.extensions
+                if (vertexArrayObject) {
+                    Object.keys(RenderVariantDefines).forEach(k => {
+                        vertexArrayObject.bindVertexArray(vertexArrays[k])
+                        if (elementsBuffer && (valueChanges.defines || valueChanges.elements)) {
+                            elementsBuffer.bind()
+                        }
+                        if (valueChanges.attributes || valueChanges.defines) {
+                            programs[k].value.bindAttributes(attributeBuffers)
+                        }
+                        vertexArrayObject.bindVertexArray(null)
+                    })
+                }
             }
 
             valueChanges.textures = false
diff --git a/src/mol-gl/webgl/vertex-array.ts b/src/mol-gl/webgl/vertex-array.ts
index cb9d6d696..f6a231518 100644
--- a/src/mol-gl/webgl/vertex-array.ts
+++ b/src/mol-gl/webgl/vertex-array.ts
@@ -17,11 +17,21 @@ export function createVertexArray(ctx: WebGLContext, program: Program, attribute
         if (elementsBuffer) elementsBuffer.bind()
         program.bindAttributes(attributeBuffers)
         ctx.vaoCount += 1
-        vertexArrayObject.bindVertexArray(null!)
+        vertexArrayObject.bindVertexArray(null)
     }
     return vertexArray
 }
 
+export function updateVertexArray(ctx: WebGLContext, vertexArray: WebGLVertexArrayObject | null, program: Program, attributeBuffers: AttributeBuffers, elementsBuffer?: ElementsBuffer) {
+    const { vertexArrayObject } = ctx.extensions
+    if (vertexArrayObject && vertexArray) {
+        vertexArrayObject.bindVertexArray(vertexArray)
+        if (elementsBuffer) elementsBuffer.bind()
+        program.bindAttributes(attributeBuffers)
+        vertexArrayObject.bindVertexArray(null)
+    }
+}
+
 export function deleteVertexArray(ctx: WebGLContext, vertexArray: WebGLVertexArrayObject | null) {
     const { vertexArrayObject } = ctx.extensions
     if (vertexArrayObject && vertexArray) {
-- 
GitLab