67f4984507d4066508565f2caef97c5edceb6217
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / ShaderProgram.js
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.ShaderProgram = class ShaderProgram extends WI.Object
27 {
28     constructor(identifier, programType, canvas)
29     {
30         console.assert(identifier);
31         console.assert(Object.values(ShaderProgram.ProgramType).includes(programType));
32         console.assert(canvas instanceof WI.Canvas);
33         console.assert(ShaderProgram.contextTypeSupportsProgramType(canvas.contextType, programType));
34
35         super();
36
37         this._identifier = identifier;
38         this._programType = programType;
39         this._canvas = canvas;
40         this._disabled = false;
41     }
42
43     // Static
44
45     static contextTypeSupportsProgramType(contextType, programType)
46     {
47         switch (contextType) {
48         case WI.Canvas.ContextType.WebGL:
49         case WI.Canvas.ContextType.WebGL2:
50             return programType === ShaderProgram.ProgramType.Render;
51
52         case WI.Canvas.ContextType.WebGPU:
53             return programType === ShaderProgram.ProgramType.Compute
54                 || programType === ShaderProgram.ProgramType.Render;
55         }
56
57         console.assert();
58         return false;
59     }
60
61     static programTypeSupportsShaderType(programType, shaderType)
62     {
63         switch (programType) {
64         case ShaderProgram.ProgramType.Compute:
65             return shaderType === ShaderProgram.ShaderType.Compute;
66
67         case ShaderProgram.ProgramType.Render:
68             return shaderType === ShaderProgram.ShaderType.Fragment
69                 || shaderType === ShaderProgram.ShaderType.Vertex;
70         }
71
72         console.assert();
73         return false;
74     }
75
76     // Public
77
78     get identifier() { return this._identifier; }
79     get programType() { return this._programType; }
80     get canvas() { return this._canvas; }
81
82     get displayName()
83     {
84         let format = null;
85         switch (this._canvas.contextType) {
86         case WI.Canvas.ContextType.WebGL:
87         case WI.Canvas.ContextType.WebGL2:
88             format = WI.UIString("Program %d");
89             break;
90         case WI.Canvas.ContextType.WebGPU:
91             switch (this._programType) {
92             case ShaderProgram.ProgramType.Compute:
93                 format = WI.UIString("Compute Pipeline %d");
94                 break;
95             case ShaderProgram.ProgramType.Render:
96                 format = WI.UIString("Render Pipeline %d");
97                 break;
98             }
99             break;
100         }
101         console.assert(format);
102         if (!this._uniqueDisplayNumber)
103             this._uniqueDisplayNumber = this._canvas.nextShaderProgramDisplayNumberForProgramType(this._programType);
104         return format.format(this._uniqueDisplayNumber);
105     }
106
107     get disabled()
108     {
109         return this._disabled;
110     }
111
112     set disabled(disabled)
113     {
114         console.assert(this._programType === ShaderProgram.ProgramType.Render);
115         console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
116
117         if (this._canvas.contextType === WI.Canvas.ContextType.WebGPU)
118             return;
119
120         if (this._disabled === disabled)
121             return;
122
123         this._disabled = disabled;
124
125         CanvasAgent.setShaderProgramDisabled(this._identifier, disabled);
126
127         this.dispatchEventToListeners(ShaderProgram.Event.DisabledChanged);
128     }
129
130     requestShaderSource(shaderType, callback)
131     {
132         console.assert(Object.values(ShaderProgram.ShaderType).includes(shaderType));
133         console.assert(ShaderProgram.programTypeSupportsShaderType(this._programType, shaderType));
134
135         // COMPATIBILITY (iOS 13): `content` was renamed to `source`.
136         CanvasAgent.requestShaderSource(this._identifier, shaderType, (error, source) => {
137             if (error) {
138                 WI.reportInternalError(error);
139                 callback(null, null);
140                 return;
141             }
142
143             callback(source);
144         });
145     }
146
147     updateShader(shaderType, source)
148     {
149         console.assert(Object.values(ShaderProgram.ShaderType).includes(shaderType));
150         console.assert(ShaderProgram.programTypeSupportsShaderType(this._programType, shaderType));
151
152         CanvasAgent.updateShader(this._identifier, shaderType, source);
153     }
154
155     showHighlight()
156     {
157         console.assert(this._programType === ShaderProgram.ProgramType.Render);
158         console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
159
160         CanvasAgent.setShaderProgramHighlighted(this._identifier, true);
161     }
162
163     hideHighlight()
164     {
165         console.assert(this._programType === ShaderProgram.ProgramType.Render);
166         console.assert(this._canvas.contextType === WI.Canvas.ContextType.WebGL || this._canvas.contextType === WI.Canvas.ContextType.WebGL2);
167
168         CanvasAgent.setShaderProgramHighlighted(this._identifier, false);
169     }
170 };
171
172 WI.ShaderProgram.ProgramType = {
173     Compute: "compute",
174     Render: "render",
175 };
176
177 WI.ShaderProgram.ShaderType = {
178     Compute: "compute",
179     Fragment: "fragment",
180     Vertex: "vertex",
181 };
182
183 WI.ShaderProgram.Event = {
184     DisabledChanged: "shader-program-disabled-changed",
185 };