Web Inspector: Canvas: show WebGPU shader pipelines
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / ShaderProgram.js
index 12cc3ee..67f4984 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-WebInspector.ShaderProgram = class ShaderProgram extends WebInspector.Object
+WI.ShaderProgram = class ShaderProgram extends WI.Object
 {
-    constructor(id, canvas)
+    constructor(identifier, programType, canvas)
     {
+        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._id = id;
-        this._canvas = canvas || null;
+        this._identifier = identifier;
+        this._programType = programType;
+        this._canvas = canvas;
+        this._disabled = false;
     }
 
-    // Public
+    // Static
 
-    get id()
+    static contextTypeSupportsProgramType(contextType, programType)
     {
-        return this._id;
+        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;
     }
 
-    get canvas()
+    static programTypeSupportsShaderType(programType, shaderType)
     {
-        return this._canvas;
+        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 WebInspector.UIString("Program %d").format(this._id.objectId);
+        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);
     }
 
-    saveIdentityToCookie(cookie)
+    get disabled()
     {
-        cookie[WebInspector.ShaderProgram.FrameURLCookieKey] = this.canvas.parentFrame.url.hash;
-        cookie[WebInspector.ShaderProgram.CanvasNameKey] = this.canvas.name.hash;
-        cookie[WebInspector.ShaderProgram.IDCookieKey] = this._id;
+        return this._disabled;
     }
 
-    updateCanvas(canvas)
+    set disabled(disabled)
     {
-        // Called by canvas.
+        console.assert(this._programType === ShaderProgram.ProgramType.Render);
+        console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
 
-        console.assert(!this._canvas);
-        this._canvas = canvas;
+        if (this._canvas.contextType === WI.Canvas.ContextType.WebGPU)
+            return;
+
+        if (this._disabled === disabled)
+            return;
+
+        this._disabled = disabled;
+
+        CanvasAgent.setShaderProgramDisabled(this._identifier, disabled);
+
+        this.dispatchEventToListeners(ShaderProgram.Event.DisabledChanged);
+    }
+
+    requestShaderSource(shaderType, callback)
+    {
+        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) {
+                WI.reportInternalError(error);
+                callback(null, null);
+                return;
+            }
+
+            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);
     }
 };
 
-WebInspector.ShaderProgram.FrameURLCookieKey = "shader-program-url";
-WebInspector.ShaderProgram.CanvasNameKey = "shader-program-canvas-name";
-WebInspector.ShaderProgram.IDCookieKey = "shader-program-id";
+WI.ShaderProgram.ProgramType = {
+    Compute: "compute",
+    Render: "render",
+};
+
+WI.ShaderProgram.ShaderType = {
+    Compute: "compute",
+    Fragment: "fragment",
+    Vertex: "vertex",
+};
+
+WI.ShaderProgram.Event = {
+    DisabledChanged: "shader-program-disabled-changed",
+};