Browse Source

Update to glslang and Update to latest Canary API
;

Sebastien Vandenberghe 5 years ago
parent
commit
075be4260c

File diff suppressed because it is too large
+ 126 - 0
dist/preview release/glslang/glslang.js


BIN
dist/preview release/glslang/glslang.wasm


+ 5 - 5
localDevWebGPU/index.html

@@ -127,17 +127,17 @@
                         });
                     }
 
-                    var shadercOptions = { 
-                        jsPath: "/dist/preview release/shaderc/shaderc.js",
-                        wasmPath: "/dist/preview release/shaderc/shaderc.wasm"
+                    var glslangOptions = { 
+                        jsPath: "/dist/preview release/glslang/glslang.js",
+                        wasmPath: "/dist/preview release/glslang/glslang.wasm"
                     };
 
                     if (typeof createEngine !== "undefined") {
                         engine = createEngine();
-                        engine.initAsync(shadercOptions).then(() => onLoad(engine));
+                        engine.initAsync(glslangOptions).then(() => onLoad(engine));
                     } else {
                         engine = new BABYLON.WebGPUEngine(canvas);
-                        engine.initAsync(shadercOptions).then(() => onLoad(engine));
+                        engine.initAsync(glslangOptions).then(() => onLoad(engine));
                     }
 
                     BABYLONDEVTOOLS.Loader.debugShortcut(engine);

+ 1 - 0
src/Engines/WebGPU/webgpuConstants.ts

@@ -121,6 +121,7 @@ export class WebGPUConstants {
     public static readonly GPUBufferUsage_VERTEX = 32;
     public static readonly GPUBufferUsage_UNIFORM = 64;
     public static readonly GPUBufferUsage_STORAGE = 128;
+    public static readonly GPUBufferUsage_INDIRECT = 256;
 
     public static readonly GPUColorWriteBits_NONE = 0;
     public static readonly GPUColorWriteBits_RED = 1;

+ 1 - 1
src/Engines/WebGPU/webgpuPipelineContext.ts

@@ -47,7 +47,7 @@ export class WebGPUPipelineContext implements IPipelineContext {
     public availableSamplers: { [key: string]: { setIndex: number, bindingIndex: number} };
 
     public orderedAttributes: string[];
-    public orderedUBOsAndSamplers: { name: string, isSampler: boolean }[][];
+    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, textureDimension?: GPUTextureViewDimension }[][];
 
     public leftOverUniforms: { name: string, type: string, length: number }[];
     public leftOverUniformsByName: { [name: string]: string };

+ 1 - 1
src/Engines/WebGPU/webgpuShaderProcessingContext.ts

@@ -21,7 +21,7 @@ export class WebGPUShaderProcessingContext implements ShaderProcessingContext {
     public leftOverUniforms: { name: string, type: string, length: number }[];
 
     public orderedAttributes: string[];
-    public orderedUBOsAndSamplers: { name: string, isSampler: boolean }[][];
+    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, textureDimension?: GPUTextureViewDimension }[][];
 
     constructor() {
         this.attributeNextLocation = 0;

+ 12 - 1
src/Engines/WebGPU/webgpuShaderProcessors.ts

@@ -2,6 +2,7 @@ import { Nullable } from '../../types';
 import { IShaderProcessor } from '../Processors/iShaderProcessor';
 import { ShaderProcessingContext } from "../Processors/shaderProcessingOptions";
 import { WebGPUShaderProcessingContext } from './webgpuShaderProcessingContext';
+import { WebGPUConstants } from './webgpuConstants';
 
 const _knownUBOs: { [key: string]: { setIndex: number, bindingIndex: number} } = {
     "Scene": { setIndex: 0, bindingIndex: 0 },
@@ -33,6 +34,11 @@ const _textureTypeByWebGLSamplerType: { [key: string]: string } = {
     "samplerCube": "textureCube"
 };
 
+const _gpuTextureViewDimensionByWebGPUTextureType: { [key: string]: GPUTextureViewDimension } = {
+    "textureCube": WebGPUConstants.GPUTextureViewDimension_cube,
+    "texture2D": WebGPUConstants.GPUTextureViewDimension_2d,
+};
+
 /** @hidden */
 export class WebGPUShaderProcessor implements IShaderProcessor {
 
@@ -98,6 +104,7 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 const samplerBindingIndex = samplerInfo.bindingIndex + 1;
                 const samplerFunction = _samplerFunctionByWebGLSamplerType[uniformType];
                 const textureType = _textureTypeByWebGLSamplerType[uniformType];
+                const textureDimension = _gpuTextureViewDimensionByWebGPUTextureType[textureType];
 
                 // Manage textures and samplers.
                 uniform = `layout(set = ${setIndex}, binding = ${textureBindingIndex}) uniform ${textureType} ${name}Texture;
@@ -108,7 +115,11 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 if (!webgpuProcessingContext.orderedUBOsAndSamplers[setIndex]) {
                     webgpuProcessingContext.orderedUBOsAndSamplers[setIndex] = [];
                 }
-                webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][textureBindingIndex] = { isSampler: true, name };
+                webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][textureBindingIndex] = {
+                    isSampler: true,
+                    textureDimension,
+                    name,
+                };
             }
             else {
                 // Check the size of the uniform array in case of array.

+ 34 - 47
src/Engines/webgpuEngine.ts

@@ -25,19 +25,19 @@ import { WebGPUShaderProcessingContext } from "./WebGPU/webgpuShaderProcessingCo
 import { Tools } from "../Misc/tools";
 
 /**
- * Options to load the associated Shaderc library
+ * Options to load the associated Glslang library
  */
-export interface ShadercOptions {
+export interface GlslangOptions {
     /**
-     * Defines an existing instance of shaderC (usefull in modules who do not access the global instance).
+     * Defines an existing instance of Glslang (usefull in modules who do not access the global instance).
      */
-    shaderc?: any;
+    glslang?: any;
     /**
-     * Defines the URL of the shaderc JS File.
+     * Defines the URL of the glslang JS File.
      */
     jsPath?: string;
     /**
-     * Defines the URL of the shaderc WASM File.
+     * Defines the URL of the glslang WASM File.
      */
     wasmPath?: string;
 }
@@ -97,10 +97,10 @@ export interface WebGPUEngineOptions extends GPURequestAdapterOptions {
  * The web GPU engine class provides support for WebGPU version of babylon.js.
  */
 export class WebGPUEngine extends Engine {
-    // Default shaderc options.
-    private static readonly _shadercDefaultOptions: ShadercOptions = {
-        jsPath: "https://preview.babylonjs.com/shaderc/shaderc.js",
-        wasmPath: "https://preview.babylonjs.com/shaderc/shaderc.wasm"
+    // Default glslang options.
+    private static readonly _glslangDefaultOptions: GlslangOptions = {
+        jsPath: "https://preview.babylonjs.com/glslang/glslang.js",
+        wasmPath: "https://preview.babylonjs.com/glslang/glslang.wasm"
     };
 
     // Page Life cycle and constants
@@ -113,7 +113,7 @@ export class WebGPUEngine extends Engine {
     // Engine Life Cycle
     private _canvas: HTMLCanvasElement;
     private _options: WebGPUEngineOptions;
-    private _shaderc: any = null;
+    private _glslang: any = null;
     private _adapter: GPUAdapter;
     private _device: GPUDevice;
     private _context: GPUCanvasContext;
@@ -234,13 +234,13 @@ export class WebGPUEngine extends Engine {
 
     /**
      * Initializes the WebGPU context and dependencies.
-     * @param shadercOptions Defines the ShaderC compiler options if necessary
+     * @param glslangOptions Defines the GLSLang compiler options if necessary
      * @returns a promise notifying the readiness of the engine.
      */
-    public initAsync(shadercOptions?: ShadercOptions): Promise<void> {
-        return this._initShaderc(shadercOptions)
-            .then((shaderc: any) => {
-                this._shaderc = shaderc;
+    public initAsync(glslangOptions?: GlslangOptions): Promise<void> {
+        return this._initGlslang(glslangOptions)
+            .then((glslang: any) => {
+                this._glslang = glslang;
                 return navigator.gpu!.requestAdapter(this._options);
             })
             .then((adapter: GPUAdapter) => {
@@ -261,33 +261,29 @@ export class WebGPUEngine extends Engine {
             });
     }
 
-    private _initShaderc(shadercOptions?: ShadercOptions): Promise<any> {
-        shadercOptions = shadercOptions || { };
-        shadercOptions = {
-            ...WebGPUEngine._shadercDefaultOptions,
-            ...shadercOptions
+    private _initGlslang(glslangOptions?: GlslangOptions): Promise<any> {
+        glslangOptions = glslangOptions || { };
+        glslangOptions = {
+            ...WebGPUEngine._glslangDefaultOptions,
+            ...glslangOptions
         };
 
-        if (shadercOptions.shaderc) {
-            return Promise.resolve(shadercOptions.shaderc);
+        if (glslangOptions.glslang) {
+            return Promise.resolve(glslangOptions.glslang);
         }
 
-        const wasmOptions = {
-            wasmBinaryFile: shadercOptions!.wasmPath
-        };
-
-        if ((window as any).Shaderc) {
-            return (window as any).Shaderc(wasmOptions);
+        if ((window as any).glslang) {
+            return (window as any).glslang(glslangOptions!.wasmPath);
         }
 
-        if (shadercOptions.jsPath && shadercOptions.wasmPath) {
-            return Tools.LoadScriptAsync(shadercOptions.jsPath)
+        if (glslangOptions.jsPath && glslangOptions.wasmPath) {
+            return Tools.LoadScriptAsync(glslangOptions.jsPath)
                 .then(() => {
-                    return (window as any).Shaderc(wasmOptions);
+                    return (window as any).glslang(glslangOptions!.wasmPath);
                 });
         }
 
-        return Promise.reject("shaderc is not available.");
+        return Promise.reject("gslang is not available.");
     }
 
     private _initializeLimits(): void {
@@ -781,20 +777,7 @@ export class WebGPUEngine extends Engine {
     }
 
     private _compileRawShaderToSpirV(source: string, type: string): Uint32Array {
-        const Shaderc = this._shaderc;
-        const compiler = new Shaderc.Compiler();
-        const opts = new Shaderc.CompileOptions();
-        const result = compiler.CompileGlslToSpv(source,
-            type === "fragment" ? Shaderc.shader_kind.fragment :
-                type === "vertex" ? Shaderc.shader_kind.vertex :
-                    type === "compute" ? Shaderc.shader_kind.compute : null,
-            "tempCompilation.glsl", "main", opts);
-        const error = result.GetErrorMessage();
-        if (error) {
-            Logger.Error(error);
-            throw new Error("Something went wrong while compile the shader.");
-        }
-        return result.GetBinary().slice();
+        return this._glslang.compileGLSL(source, type);
     }
 
     private _compileShaderToSpirV(source: string, type: string, defines: Nullable<string>, shaderVersion: string): Uint32Array {
@@ -2028,12 +2011,16 @@ export class WebGPUEngine extends Engine {
                         binding: j,
                         visibility: WebGPUConstants.GPUShaderStageBit_VERTEX | WebGPUConstants.GPUShaderStageBit_FRAGMENT,
                         type: WebGPUConstants.GPUBindingType_sampledTexture,
+                        textureDimension: bindingDefinition.textureDimension,
+                        // TODO WEBGPU. Handle texture component type properly.
+                        // textureComponentType?: GPUTextureComponentType,
                     }, {
                         // TODO WEBGPU. No Magic + 1 (coming from current 1 texture 1 sampler startegy).
                         binding: j + 1,
                         visibility: WebGPUConstants.GPUShaderStageBit_VERTEX | WebGPUConstants.GPUShaderStageBit_FRAGMENT,
                         type: WebGPUConstants.GPUBindingType_sampler
                     });
+                    console.log(bindingDefinition.textureDimension, bindingDefinition.name);
                 }
                 else {
                     bindings.push({

+ 12 - 3
src/LibDeclarations/webgpu.d.ts

@@ -1,4 +1,4 @@
-// https://github.com/gpuweb/gpuweb/blob/767e515eba9306840ce3fdf57ef9d73ae5d24296/spec/index.bs
+// https://github.com/gpuweb/gpuweb/blob/0a2bb28a584aa1c6adabaec1f841ed29de400626/spec/index.bs
 // except #280 setSubData (TODO)
 
 interface GPUColorDict {
@@ -233,7 +233,7 @@ interface GPUBindGroupLayoutBinding {
   textureDimension?: GPUTextureViewDimension;
   textureComponentType?: GPUTextureComponentType;
   multisampled?: boolean;
-  dynamic?: boolean;
+  hasDynamicOffset?: boolean;
 }
 
 interface GPUBindGroupLayoutDescriptor
@@ -341,6 +341,13 @@ interface GPUVertexInputDescriptor {
 
 interface GPULimits {
   maxBindGroups?: number;
+  maxDynamicUniformBuffersPerPipelineLayout?: number;
+  maxDynamicStorageBuffersPerPipelineLayout?: number;
+  maxSampledTexturesPerShaderStage?: number;
+  maxSamplersPerShaderStage?: number;
+  maxStorageBuffersPerPipelineLayout?: number;
+  maxStorageTexturesPerShaderStage?: number;
+  maxUniformBuffersPerShaderStage?: number;
 }
 
 interface GPUPipelineDescriptorBase {
@@ -668,7 +675,9 @@ interface GPURenderBundleDescriptor extends GPUObjectDescriptorBase {}
 
 interface GPURenderBundle extends GPUObjectBase {}
 
-interface GPURenderBundleEncoder extends GPURenderEncoderBase {}
+interface GPURenderBundleEncoder extends GPURenderEncoderBase {
+  finish(descriptor?: GPURenderBundleDescriptor): GPURenderBundle;
+}
 
 interface GPURenderBundleEncoderDescriptor
   extends GPUObjectDescriptorBase {