Browse Source

Split Webgpu Webgl specific in Engine

Sebastien Vandenberghe 6 years ago
parent
commit
63b0ee97f8

+ 5 - 0
src/Engines/IPipelineContext.ts

@@ -1,3 +1,5 @@
+import { Nullable } from '../types';
+import { Effect } from '../Materials/effect';
 
 
 /**
 /**
  * Class used to store and describe the pipeline context associated with an effect
  * Class used to store and describe the pipeline context associated with an effect
@@ -14,4 +16,7 @@ export interface IPipelineContext {
 
 
     /** @hidden */
     /** @hidden */
     _handlesSpectorRebuildCallback(onCompiled: (compiledObject: any) => void): void;
     _handlesSpectorRebuildCallback(onCompiled: (compiledObject: any) => void): void;
+
+    /** @hidden */
+    _fillEffectInformation(effect: Effect, uniformBuffersNames: { [key: string]: number }, uniformsNames: string[], uniforms: { [key: string]: Nullable<WebGLUniformLocation> }, samplerList: string[], samplers: { [key: string]: number }, attributesNames: string[], attributes: number[]): void;
 }
 }

+ 30 - 0
src/Engines/WebGL/webGLPipelineContext.ts

@@ -1,6 +1,7 @@
 import { IPipelineContext } from '../IPipelineContext';
 import { IPipelineContext } from '../IPipelineContext';
 import { Engine } from '../engine';
 import { Engine } from '../engine';
 import { Nullable } from '../../types';
 import { Nullable } from '../../types';
+import { Effect } from '../../Materials/effect';
 
 
 /** @hidden */
 /** @hidden */
 export class WebGLPipelineContext implements IPipelineContext {
 export class WebGLPipelineContext implements IPipelineContext {
@@ -33,4 +34,33 @@ export class WebGLPipelineContext implements IPipelineContext {
             onCompiled(this.program);
             onCompiled(this.program);
         }
         }
     }
     }
+
+    public _fillEffectInformation(effect: Effect, uniformBuffersNames: { [key: string]: number }, uniformsNames: string[], uniforms: { [key: string]: Nullable<WebGLUniformLocation> }, samplerList: string[], samplers: { [key: string]: number }, attributesNames: string[], attributes: number[]) {
+        const engine = this.engine;
+        if (engine.supportsUniformBuffers) {
+            for (var name in uniformBuffersNames) {
+                effect.bindUniformBlock(name, uniformBuffersNames[name]);
+            }
+        }
+
+        const effectAvailableUniforms = this.engine.getUniforms(this, uniformsNames);
+        effectAvailableUniforms.forEach((uniform, index) => {
+            uniforms[uniformsNames[index]] = uniform;
+        });
+
+        let index: number;
+        for (index = 0; index < samplerList.length; index++) {
+            const sampler = effect.getUniform(samplerList[index]);
+            if (sampler == null) {
+                samplerList.splice(index, 1);
+                index--;
+            }
+        }
+
+        samplerList.forEach((name, index) => {
+            samplers[name] = index;
+        });
+
+        attributes.push(...engine.getAttributes(this, attributesNames));
+    }
 }
 }

+ 34 - 0
src/Engines/WebGPU/webgpuPipelineContext.ts

@@ -2,6 +2,7 @@ import { IPipelineContext } from '../IPipelineContext';
 import { Nullable } from '../../types';
 import { Nullable } from '../../types';
 import { WebGPUEngine } from '../webgpuEngine';
 import { WebGPUEngine } from '../webgpuEngine';
 import { InternalTexture } from '../../Materials/Textures/internalTexture';
 import { InternalTexture } from '../../Materials/Textures/internalTexture';
+import { Effect } from '../../Materials/effect';
 
 
 /** @hidden */
 /** @hidden */
 export interface IWebGPUPipelineContextSamplerCache {
 export interface IWebGPUPipelineContextSamplerCache {
@@ -62,4 +63,37 @@ export class WebGPUPipelineContext implements IPipelineContext {
     public _handlesSpectorRebuildCallback(onCompiled: (program: any) => void): void {
     public _handlesSpectorRebuildCallback(onCompiled: (program: any) => void): void {
         // Nothing to do yet for spector.
         // Nothing to do yet for spector.
     }
     }
+
+    public _fillEffectInformation(effect: Effect, uniformBuffersNames: { [key: string]: number }, uniformsNames: string[], uniforms: { [key: string]: Nullable<WebGLUniformLocation> }, samplerList: string[], samplers: { [key: string]: number }, attributesNames: string[], attributes: number[]) {
+        const engine = this.engine;
+
+        let effectAvailableUniforms = engine.getUniforms(this, uniformsNames);
+        effectAvailableUniforms.forEach((uniform, index) => {
+            uniforms[uniformsNames[index]] = uniform;
+        });
+
+        // TODO. Cleanup SEB.
+        // Prevent Memory Leak by reducing the number of string, refer to the string instead of copy.
+        effect._fragmentSourceCode = "";
+        effect._vertexSourceCode = "";
+        // this._fragmentSourceCodeOverride = "";
+        // this._vertexSourceCodeOverride = "";
+
+        const foundSamplers = this.availableUBOs;
+        let index: number;
+        for (index = 0; index < samplerList.length; index++) {
+            const name = samplerList[index];
+            const sampler = foundSamplers[samplerList[index]];
+
+            if (sampler == null || sampler == undefined) {
+                samplerList.splice(index, 1);
+                index--;
+            }
+            else {
+                samplers[name] = sampler;
+            }
+        }
+
+        attributes.push(...engine.getAttributes(this, attributesNames));
+    }
 }
 }

+ 13 - 54
src/Materials/effect.ts

@@ -7,7 +7,6 @@ import { Logger } from "../Misc/logger";
 import { IDisposable } from '../scene';
 import { IDisposable } from '../scene';
 import { IPipelineContext } from '../Engines/IPipelineContext';
 import { IPipelineContext } from '../Engines/IPipelineContext';
 import { DataBuffer } from '../Meshes/dataBuffer';
 import { DataBuffer } from '../Meshes/dataBuffer';
-import { WebGPUPipelineContext } from '../Engines/WebGPU/webgpuPipelineContext';
 import { ShaderProcessor } from '../Engines/Processors/shaderProcessor';
 import { ShaderProcessor } from '../Engines/Processors/shaderProcessor';
 import { ProcessingOptions } from '../Engines/Processors/shaderProcessingOptions';
 import { ProcessingOptions } from '../Engines/Processors/shaderProcessingOptions';
 
 
@@ -263,8 +262,6 @@ export class Effect implements IDisposable {
     public _key: string = "";
     public _key: string = "";
     private _indexParameters: any;
     private _indexParameters: any;
     private _fallbacks: Nullable<EffectFallbacks> = null;
     private _fallbacks: Nullable<EffectFallbacks> = null;
-    private _vertexSourceCode: string = "";
-    private _fragmentSourceCode: string = "";
     private _vertexSourceCodeOverride: string = "";
     private _vertexSourceCodeOverride: string = "";
     private _fragmentSourceCodeOverride: string = "";
     private _fragmentSourceCodeOverride: string = "";
     private _transformFeedbackVaryings: Nullable<string[]> = null;
     private _transformFeedbackVaryings: Nullable<string[]> = null;
@@ -273,6 +270,10 @@ export class Effect implements IDisposable {
      * @hidden
      * @hidden
      */
      */
     public _pipelineContext: Nullable<IPipelineContext> = null;
     public _pipelineContext: Nullable<IPipelineContext> = null;
+    /** @hidden */
+    public _vertexSourceCode: string = "";
+    /** @hidden */
+    public _fragmentSourceCode: string = "";
     private _valueCache: { [key: string]: any } = {};
     private _valueCache: { [key: string]: any } = {};
     private static _baseCache: { [key: number]: DataBuffer } = {};
     private static _baseCache: { [key: number]: DataBuffer } = {};
 
 
@@ -721,57 +722,15 @@ export class Effect implements IDisposable {
             }
             }
 
 
             engine._executeWhenRenderingStateIsCompiled(this._pipelineContext, () => {
             engine._executeWhenRenderingStateIsCompiled(this._pipelineContext, () => {
-                // TODO. Move the WebGL Part out.
-                if (engine.supportsUniformBuffers) {
-                    for (var name in this._uniformBuffersNames) {
-                        this.bindUniformBlock(name, this._uniformBuffersNames[name]);
-                    }
-                }
-
-                let uniforms = engine.getUniforms(this._pipelineContext!, this._uniformsNames);
-                uniforms.forEach((uniform, index) => {
-                    this._uniforms[this._uniformsNames[index]] = uniform;
-                });
-
-                if (!engine.isWebGPU) {
-                    let index: number;
-                    for (index = 0; index < this._samplerList.length; index++) {
-                        const sampler = this.getUniform(this._samplerList[index]);
-                        if (sampler == null) {
-                            this._samplerList.splice(index, 1);
-                            index--;
-                        }
-                    }
-
-                    this._samplerList.forEach((name, index) => {
-                        this._samplers[name] = index;
-                    });
-                }
-                else {
-                    // TODO. CLEANUP THIS STUFF (FOR SEB) !!!
-                    this._fragmentSourceCode = "";
-                    // this._fragmentSourceCodeOverride = "";
-                    this._vertexSourceCode = "";
-                    // this._vertexSourceCodeOverride = "";
-
-                    const foundSamplers = (this._pipelineContext as WebGPUPipelineContext).availableUBOs;
-                    let index: number;
-                    for (index = 0; index < this._samplerList.length; index++) {
-                        const name = this._samplerList[index];
-                        const sampler = foundSamplers[this._samplerList[index]];
-
-                        if (sampler == null || sampler == undefined) {
-                            this._samplerList.splice(index, 1);
-                            index--;
-                        }
-                        else {
-                            this._samplers[name] = sampler;
-                        }
-                    }
-                }
-
-                // Generic WebGL WebGPU part.
-                this._attributes = engine.getAttributes(this._pipelineContext!, attributesNames);
+                this._attributes = [];
+                this._pipelineContext!._fillEffectInformation(this,
+                    this._uniformBuffersNames,
+                    this._uniformsNames,
+                    this._uniforms,
+                    this._samplerList,
+                    this._samplers,
+                    attributesNames,
+                    this._attributes);
 
 
                 this._compilationError = "";
                 this._compilationError = "";
                 this._isReady = true;
                 this._isReady = true;