Web Inspector: Canvas: show WebGPU shader pipelines
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / ShaderProgram.js
index 7ee7a8f..67f4984 100644 (file)
 
 WI.ShaderProgram = class ShaderProgram extends WI.Object
 {
-    constructor(identifier, canvas)
+    constructor(identifier, programType, canvas)
     {
-        super();
-
         console.assert(identifier);
+        console.assert(Object.values(ShaderProgram.ProgramType).includes(programType));
         console.assert(canvas instanceof WI.Canvas);
+        console.assert(ShaderProgram.contextTypeSupportsProgramType(canvas.contextType, programType));
+
+        super();
 
         this._identifier = identifier;
+        this._programType = programType;
         this._canvas = canvas;
-        this._uniqueDisplayNumber = canvas.nextShaderProgramDisplayNumber();
+        this._disabled = false;
+    }
+
+    // Static
+
+    static contextTypeSupportsProgramType(contextType, programType)
+    {
+        switch (contextType) {
+        case WI.Canvas.ContextType.WebGL:
+        case WI.Canvas.ContextType.WebGL2:
+            return programType === ShaderProgram.ProgramType.Render;
+
+        case WI.Canvas.ContextType.WebGPU:
+            return programType === ShaderProgram.ProgramType.Compute
+                || programType === ShaderProgram.ProgramType.Render;
+        }
+
+        console.assert();
+        return false;
+    }
+
+    static programTypeSupportsShaderType(programType, shaderType)
+    {
+        switch (programType) {
+        case ShaderProgram.ProgramType.Compute:
+            return shaderType === ShaderProgram.ShaderType.Compute;
+
+        case ShaderProgram.ProgramType.Render:
+            return shaderType === ShaderProgram.ShaderType.Fragment
+                || shaderType === ShaderProgram.ShaderType.Vertex;
+        }
+
+        console.assert();
+        return false;
     }
 
     // Public
 
     get identifier() { return this._identifier; }
+    get programType() { return this._programType; }
     get canvas() { return this._canvas; }
 
     get displayName()
     {
-        return WI.UIString("Program %d").format(this._uniqueDisplayNumber);
+        let format = null;
+        switch (this._canvas.contextType) {
+        case WI.Canvas.ContextType.WebGL:
+        case WI.Canvas.ContextType.WebGL2:
+            format = WI.UIString("Program %d");
+            break;
+        case WI.Canvas.ContextType.WebGPU:
+            switch (this._programType) {
+            case ShaderProgram.ProgramType.Compute:
+                format = WI.UIString("Compute Pipeline %d");
+                break;
+            case ShaderProgram.ProgramType.Render:
+                format = WI.UIString("Render Pipeline %d");
+                break;
+            }
+            break;
+        }
+        console.assert(format);
+        if (!this._uniqueDisplayNumber)
+            this._uniqueDisplayNumber = this._canvas.nextShaderProgramDisplayNumberForProgramType(this._programType);
+        return format.format(this._uniqueDisplayNumber);
     }
 
-    requestVertexShaderSource(callback)
+    get disabled()
     {
-        this._requestShaderSource(CanvasAgent.ShaderType.Vertex, callback);
+        return this._disabled;
     }
 
-    requestFragmentShaderSource(callback)
+    set disabled(disabled)
     {
-        this._requestShaderSource(CanvasAgent.ShaderType.Fragment, callback);
-    }
+        console.assert(this._programType === ShaderProgram.ProgramType.Render);
+        console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
+
+        if (this._canvas.contextType === WI.Canvas.ContextType.WebGPU)
+            return;
+
+        if (this._disabled === disabled)
+            return;
 
-    // Private
+        this._disabled = disabled;
 
-    _requestShaderSource(shaderType, callback)
+        CanvasAgent.setShaderProgramDisabled(this._identifier, disabled);
+
+        this.dispatchEventToListeners(ShaderProgram.Event.DisabledChanged);
+    }
+
+    requestShaderSource(shaderType, callback)
     {
-        CanvasAgent.requestShaderSource(this._identifier, shaderType, (error, content) => {
+        console.assert(Object.values(ShaderProgram.ShaderType).includes(shaderType));
+        console.assert(ShaderProgram.programTypeSupportsShaderType(this._programType, shaderType));
+
+        // COMPATIBILITY (iOS 13): `content` was renamed to `source`.
+        CanvasAgent.requestShaderSource(this._identifier, shaderType, (error, source) => {
             if (error) {
-                callback(null);
+                WI.reportInternalError(error);
+                callback(null, null);
                 return;
             }
 
-            callback(content);
+            callback(source);
         });
     }
+
+    updateShader(shaderType, source)
+    {
+        console.assert(Object.values(ShaderProgram.ShaderType).includes(shaderType));
+        console.assert(ShaderProgram.programTypeSupportsShaderType(this._programType, shaderType));
+
+        CanvasAgent.updateShader(this._identifier, shaderType, source);
+    }
+
+    showHighlight()
+    {
+        console.assert(this._programType === ShaderProgram.ProgramType.Render);
+        console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
+
+        CanvasAgent.setShaderProgramHighlighted(this._identifier, true);
+    }
+
+    hideHighlight()
+    {
+        console.assert(this._programType === ShaderProgram.ProgramType.Render);
+        console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
+
+        CanvasAgent.setShaderProgramHighlighted(this._identifier, false);
+    }
+};
+
+WI.ShaderProgram.ProgramType = {
+    Compute: "compute",
+    Render: "render",
 };
 
 WI.ShaderProgram.ShaderType = {
-    Fragment: "shader-type-fragment",
-    Vertex: "shader-type-vertex",
+    Compute: "compute",
+    Fragment: "fragment",
+    Vertex: "vertex",
 };
 
 WI.ShaderProgram.Event = {
-    ProgramLinked: "shader-program-program-linked",
-    ShaderCompiled: "shader-program-shader-compiled",
+    DisabledChanged: "shader-program-disabled-changed",
 };