浏览代码

optimisation + adding transformMatrix as uniform buffer

Benjamin Guignabert 8 年之前
父节点
当前提交
0153625e91

文件差异内容过多而无法显示
+ 5863 - 5859
dist/preview release/babylon.d.ts


文件差异内容过多而无法显示
+ 5863 - 5859
dist/preview release/babylon.module.d.ts


+ 3 - 3
index.html

@@ -59,7 +59,7 @@
 					for (var i = 0; i < squareSize; i++) {
 					for (var i = 0; i < squareSize; i++) {
 						for (var j = 0; j < squareSize; j++) {
 						for (var j = 0; j < squareSize; j++) {
 							for (var k = 0; k < 5; k++) {
 							for (var k = 0; k < 5; k++) {
-								var sphere = BABYLON.Mesh.CreateSphere("sphere", 16, 0.5, scene);
+								var sphere = BABYLON.Mesh.CreateSphere("sphere", 4, 0.5, scene);
 
 
 								// Move the sphere upward 1/2 its height
 								// Move the sphere upward 1/2 its height
 								sphere.position.z = - squareSize / 4 + j / 2;
 								sphere.position.z = - squareSize / 4 + j / 2;
@@ -67,8 +67,8 @@
 								sphere.position.y = - squareSize / 4 + k / 2;
 								sphere.position.y = - squareSize / 4 + k / 2;
 								if (j === 0) {
 								if (j === 0) {
 									sphere.material = new BABYLON.StandardMaterial("test");
 									sphere.material = new BABYLON.StandardMaterial("test");
-									sphere.material.diffuseTexture = new BABYLON.Texture("test.jpeg", scene);
-									sphere.material.bumpTexture = new BABYLON.Texture("test.jpeg", scene);
+									// sphere.material.diffuseTexture = new BABYLON.Texture("test.jpeg", scene);
+									// sphere.material.bumpTexture = new BABYLON.Texture("test.jpeg", scene);
 									sphere.material.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
 									sphere.material.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
 									sphere.material.specularColor = new BABYLON.Color3(Math.random() < 0.8 ? 0 : 1, Math.random() < 0.8 ? 0 : 1, Math.random() < 0.8 ? 0 : 1);
 									sphere.material.specularColor = new BABYLON.Color3(Math.random() < 0.8 ? 0 : 1, Math.random() < 0.8 ? 0 : 1, Math.random() < 0.8 ? 0 : 1);
 									sphere.material.emissiveColor = new BABYLON.Color3(0, 0, 0);
 									sphere.material.emissiveColor = new BABYLON.Color3(0, 0, 0);

+ 1 - 1
src/Lights/babylon.hemisphericLight.ts

@@ -23,7 +23,7 @@
             this._uniformBuffer.addUniform("vLightData", 4);
             this._uniformBuffer.addUniform("vLightData", 4);
             this._uniformBuffer.addUniform("vLightDiffuse", 4);
             this._uniformBuffer.addUniform("vLightDiffuse", 4);
             this._uniformBuffer.addUniform("vLightSpecular", 3);
             this._uniformBuffer.addUniform("vLightSpecular", 3);
-            this._uniformBuffer.addUniform("vLightDirection", 4);
+            this._uniformBuffer.addUniform("vLightGround", 3);
             this._uniformBuffer.addUniform("shadowsInfo", 3);
             this._uniformBuffer.addUniform("shadowsInfo", 3);
         }
         }
 
 

+ 1 - 1
src/Lights/babylon.spotLight.ts

@@ -47,7 +47,7 @@
             this._uniformBuffer.addUniform("vLightData", 4);
             this._uniformBuffer.addUniform("vLightData", 4);
             this._uniformBuffer.addUniform("vLightDiffuse", 4);
             this._uniformBuffer.addUniform("vLightDiffuse", 4);
             this._uniformBuffer.addUniform("vLightSpecular", 3);
             this._uniformBuffer.addUniform("vLightSpecular", 3);
-            this._uniformBuffer.addUniform("vLightGround", 3);
+            this._uniformBuffer.addUniform("vLightDirection", 3);
             this._uniformBuffer.addUniform("shadowsInfo", 3);
             this._uniformBuffer.addUniform("shadowsInfo", 3);
         }
         }
         
         

+ 1 - 1
src/Materials/babylon.effect.ts

@@ -101,7 +101,7 @@
             this.name = baseName;
             this.name = baseName;
             this.defines = defines;
             this.defines = defines;
             // TODO
             // TODO
-            this._uniformBuffersNames = ["Material", "Light0"];
+            this._uniformBuffersNames = ["Material", "Light0", "Scene"];
 
 
             this._uniformsNames = uniformsNames.concat(samplers);
             this._uniformsNames = uniformsNames.concat(samplers);
             this._samplers = samplers;
             this._samplers = samplers;

+ 4 - 1
src/Materials/babylon.pushMaterial.ts

@@ -24,7 +24,10 @@
             return this.isReadyForSubMesh(mesh, mesh.subMeshes[0], useInstances);
             return this.isReadyForSubMesh(mesh, mesh.subMeshes[0], useInstances);
         }
         }
 
 
-        
+        public bindTransformMatrix(effect: Effect, transformMatrixBuffer: UniformBuffer): void {
+            effect.bindUniformBuffer(transformMatrixBuffer.getBuffer(), "Scene");
+        }
+
         public bindOnlyWorldMatrix(world: Matrix): void {
         public bindOnlyWorldMatrix(world: Matrix): void {
             this._activeEffect.setMatrix("world", world);
             this._activeEffect.setMatrix("world", world);
         }
         }

+ 12 - 3
src/Materials/babylon.standardMaterial.ts

@@ -715,12 +715,21 @@ module BABYLON {
                 }
                 }
                 MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, defines, this._maxSimultaneousLights);
                 MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, defines, this._maxSimultaneousLights);
 
 
+                var onCompiled = function(effect) {
+                    if (this.onCompiled) {
+                        this.onCompiled(effect);
+                    }
+
+                    this.bindTransformMatrix(effect, scene.getTransformMatrixBuffer());
+                }.bind(this);
+
                 subMesh.setEffect(scene.getEngine().createEffect(shaderName,
                 subMesh.setEffect(scene.getEngine().createEffect(shaderName,
                     attribs, uniforms, samplers,
                     attribs, uniforms, samplers,
-                    join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this._maxSimultaneousLights - 1 }), defines);
+                    join, fallbacks, onCompiled, this.onError, { maxSimultaneousLights: this._maxSimultaneousLights - 1 }), defines);
 
 
                 this.buildUniformLayout();
                 this.buildUniformLayout();
 
 
+
             }
             }
 
 
             if (!subMesh.effect.isReady()) {
             if (!subMesh.effect.isReady()) {
@@ -800,9 +809,9 @@ module BABYLON {
             // Bones
             // Bones
             MaterialHelper.BindBonesParameters(mesh, effect);
             MaterialHelper.BindBonesParameters(mesh, effect);
             if (this._mustRebind(scene, effect)) {
             if (this._mustRebind(scene, effect)) {
-                effect.setMatrix("viewProjection", scene.getTransformMatrix());
-
                 if (!this.isFrozen || !this._uniformBuffer.isSync) {
                 if (!this.isFrozen || !this._uniformBuffer.isSync) {
+                    effect.setMatrix("viewProjection", scene.getTransformMatrix());
+
                     if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
                     if (StandardMaterial.FresnelEnabled && defines.FRESNEL) {
                         // Fresnel
                         // Fresnel
                         if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
                         if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {

+ 30 - 15
src/Materials/babylon.uniformBuffer.ts

@@ -3,6 +3,7 @@ module BABYLON {
         private _engine: Engine;
         private _engine: Engine;
         private _buffer: WebGLBuffer;
         private _buffer: WebGLBuffer;
         private _data: number[];
         private _data: number[];
+        private _bufferData: Float32Array;
         private _dynamic: boolean;
         private _dynamic: boolean;
         private _uniformName: string;
         private _uniformName: string;
         private _uniformLocations: { [key:string]:number; };
         private _uniformLocations: { [key:string]:number; };
@@ -35,8 +36,8 @@ module BABYLON {
             return this._dynamic;
             return this._dynamic;
         }
         }
 
 
-        public getData(): number[] {
-            return this._data;
+        public getData(): Float32Array {
+            return this._bufferData;
         }
         }
 
 
         public getBuffer(): WebGLBuffer {
         public getBuffer(): WebGLBuffer {
@@ -141,12 +142,12 @@ module BABYLON {
                 return; // nothing to do
                 return; // nothing to do
             }
             }
 
 
-            var data = this._data;
+            this._bufferData = new Float32Array(this._data);
 
 
             if (this._dynamic) {
             if (this._dynamic) {
-                this._buffer = this._engine.createDynamicUniformBuffer(data);
+                this._buffer = this._engine.createDynamicUniformBuffer(this._bufferData);
             } else {
             } else {
-                this._buffer = this._engine.createUniformBuffer(data);
+                this._buffer = this._engine.createUniformBuffer(this._bufferData);
             }
             }
 
 
             this._needSync = false;
             this._needSync = false;
@@ -158,11 +159,11 @@ module BABYLON {
                 return;
                 return;
             }
             }
 
 
-            if (!this._needSync) {
+            if (!this._dynamic && !this._needSync) {
                 return;
                 return;
             }
             }
 
 
-            this._engine.updateUniformBuffer(this._buffer, this._data);
+            this._engine.updateUniformBuffer(this._buffer, this._bufferData);
 
 
             this._needSync = false;
             this._needSync = false;
         }
         }
@@ -171,22 +172,36 @@ module BABYLON {
 
 
             var location = this._uniformLocations[uniformName];
             var location = this._uniformLocations[uniformName];
             if (location === undefined) {
             if (location === undefined) {
-                return;
+                if (this._buffer) {
+                    // Cannot add an uniform if the buffer is already created
+                    Tools.Error("Uniform buffer overflow.");
+                    return;
+                }
+                this.addUniform(uniformName, size);
+                location = this._uniformLocations[uniformName];
             }
             }
 
 
             if (!this._buffer) {
             if (!this._buffer) {
                 this.create();
                 this.create();
             }
             }
 
 
-            var changed = false;
-            for (var i = 0; i < size; i++) {
-                if (this._data[location + i] !== data[i]) {
-                   changed = true;
-                    this._data[location + i] = data[i];
+            if (!this._dynamic) {
+                // Cache for static uniform buffers
+                var changed = false;
+                for (var i = 0; i < size; i++) {
+                    if (this._bufferData[location + i] !== data[i]) {
+                       changed = true;
+                        this._bufferData[location + i] = data[i];
+                    }
                 }
                 }
-            }
 
 
-            this._needSync = this._needSync || changed;
+                this._needSync = this._needSync || changed;
+            } else {
+                // No cache for dynamic
+                for (var i = 0; i < size; i++) {
+                    this._bufferData[location + i] = data[i];
+                }
+            }
         }
         }
 
 
         public updateFloat(name: string, x: number) {
         public updateFloat(name: string, x: number) {

+ 6 - 2
src/Shaders/default.vertex.fx

@@ -36,6 +36,11 @@ uniform Material
 	float pointSize;
 	float pointSize;
 } uMaterial;
 } uMaterial;
 
 
+uniform mat4 viewProjection;
+uniform Scene {
+	mat4 viewProjection;
+} uScene;
+
 // Attributes
 // Attributes
 attribute vec3 position;
 attribute vec3 position;
 #ifdef NORMAL
 #ifdef NORMAL
@@ -60,7 +65,6 @@ attribute vec4 color;
 #include<instancesDeclaration>
 #include<instancesDeclaration>
 
 
 uniform mat4 view;
 uniform mat4 view;
-uniform mat4 viewProjection;
 
 
 #ifdef DIFFUSE
 #ifdef DIFFUSE
 varying vec2 vDiffuseUV;
 varying vec2 vDiffuseUV;
@@ -127,7 +131,7 @@ void main(void) {
 #include<instancesVertex>
 #include<instancesVertex>
 #include<bonesVertex>
 #include<bonesVertex>
 
 
-	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
+	gl_Position = uScene.viewProjection * finalWorld * vec4(position, 1.0);
 
 
 	vec4 worldPos = finalWorld * vec4(position, 1.0);
 	vec4 worldPos = finalWorld * vec4(position, 1.0);
 	vPositionW = vec3(worldPos);
 	vPositionW = vec3(worldPos);

+ 11 - 0
src/babylon.scene.ts

@@ -669,6 +669,8 @@
         public _activeAnimatables = new Array<Animatable>();
         public _activeAnimatables = new Array<Animatable>();
 
 
         private _transformMatrix = Matrix.Zero();
         private _transformMatrix = Matrix.Zero();
+        private _transformMatrixBuffer: UniformBuffer;
+
         private _pickWithRayInverseMatrix: Matrix;
         private _pickWithRayInverseMatrix: Matrix;
 
 
         private _edgesRenderers = new SmartArray<EdgesRenderer>(16);
         private _edgesRenderers = new SmartArray<EdgesRenderer>(16);
@@ -737,6 +739,9 @@
 
 
             //collision coordinator initialization. For now legacy per default.
             //collision coordinator initialization. For now legacy per default.
             this.workerCollisions = false;//(!!Worker && (!!BABYLON.CollisionWorker || BABYLON.WorkerIncluded));
             this.workerCollisions = false;//(!!Worker && (!!BABYLON.CollisionWorker || BABYLON.WorkerIncluded));
+
+            // Transform Buffer
+            this._transformMatrixBuffer = new UniformBuffer(this._engine, null, true);
         }
         }
 
 
         // Properties
         // Properties
@@ -1666,6 +1671,12 @@
             } else {
             } else {
                 Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
                 Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
             }
             }
+            this._transformMatrixBuffer.updateMatrix("viewProjection", this._transformMatrix);
+            this._transformMatrixBuffer.update();
+        }
+
+        public getTransformMatrixBuffer(): UniformBuffer {
+            return this._transformMatrixBuffer;
         }
         }
 
 
         // Methods
         // Methods