-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathWebGPUUtils.js
More file actions
255 lines (171 loc) · 5.69 KB
/
WebGPUUtils.js
File metadata and controls
255 lines (171 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { HalfFloatType, UnsignedByteType } from '../../../constants.js';
import { GPUPrimitiveTopology, GPUTextureFormat } from './WebGPUConstants.js';
/**
* A WebGPU backend utility module with common helpers.
*
* @private
*/
class WebGPUUtils {
/**
* Constructs a new utility object.
*
* @param {WebGPUBackend} backend - The WebGPU backend.
*/
constructor( backend ) {
/**
* A reference to the WebGPU backend.
*
* @type {WebGPUBackend}
*/
this.backend = backend;
}
/**
* Returns the depth/stencil GPU format for the given render context.
*
* @param {RenderContext} renderContext - The render context.
* @return {string} The depth/stencil GPU texture format.
*/
getCurrentDepthStencilFormat( renderContext ) {
let format;
if ( renderContext.depthTexture !== null ) {
format = this.getTextureFormatGPU( renderContext.depthTexture );
} else if ( renderContext.depth && renderContext.stencil ) {
format = GPUTextureFormat.Depth24PlusStencil8;
} else if ( renderContext.depth ) {
format = GPUTextureFormat.Depth24Plus;
}
return format;
}
/**
* Returns the GPU format for the given texture.
*
* @param {Texture} texture - The texture.
* @return {string} The GPU texture format.
*/
getTextureFormatGPU( texture ) {
return this.backend.get( texture ).format;
}
/**
* Returns an object that defines the multi-sampling state of the given texture.
*
* @param {Texture} texture - The texture.
* @return {Object} The multi-sampling state.
*/
getTextureSampleData( texture ) {
let samples;
if ( texture.isFramebufferTexture ) {
samples = 1;
} else if ( texture.isDepthTexture && ! texture.renderTarget ) {
const renderer = this.backend.renderer;
const renderTarget = renderer.getRenderTarget();
samples = renderTarget ? renderTarget.samples : renderer.currentSamples;
} else if ( texture.renderTarget ) {
samples = texture.renderTarget.samples;
}
samples = samples || 1;
const isMSAA = samples > 1 && texture.renderTarget !== null && ( texture.isDepthTexture !== true && texture.isFramebufferTexture !== true );
const primarySamples = isMSAA ? 1 : samples;
return { samples, primarySamples, isMSAA };
}
/**
* Returns the default color attachment's GPU format of the current render context.
*
* @param {RenderContext} renderContext - The render context.
* @return {string} The GPU texture format of the default color attachment.
*/
getCurrentColorFormat( renderContext ) {
let format;
if ( renderContext.textures !== null ) {
format = this.getTextureFormatGPU( renderContext.textures[ 0 ] );
} else {
format = this.getPreferredCanvasFormat(); // default context format
}
return format;
}
/**
* Returns the GPU formats of all color attachments of the current render context.
*
* @param {RenderContext} renderContext - The render context.
* @return {Array<string>} The GPU texture formats of all color attachments.
*/
getCurrentColorFormats( renderContext ) {
if ( renderContext.textures !== null ) {
return renderContext.textures.map( t => this.getTextureFormatGPU( t ) );
} else {
return [ this.getPreferredCanvasFormat() ]; // default context format
}
}
/**
* Returns the output color space of the current render context.
*
* @param {RenderContext} renderContext - The render context.
* @return {string} The output color space.
*/
getCurrentColorSpace( renderContext ) {
if ( renderContext.textures !== null ) {
return renderContext.textures[ 0 ].colorSpace;
}
return this.backend.renderer.outputColorSpace;
}
/**
* Returns GPU primitive topology for the given object and material.
*
* @param {Object3D} object - The 3D object.
* @param {Material} material - The material.
* @return {string} The GPU primitive topology.
*/
getPrimitiveTopology( object, material ) {
if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
else if ( object.isLineSegments || ( object.isMesh && material.wireframe === true ) ) return GPUPrimitiveTopology.LineList;
else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
else if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
}
/**
* Returns a modified sample count from the given sample count value.
*
* That is required since WebGPU only supports either 1 or 4.
*
* @param {number} sampleCount - The input sample count.
* @return {number} The (potentially updated) output sample count.
*/
getSampleCount( sampleCount ) {
return sampleCount >= 4 ? 4 : 1;
}
/**
* Returns the sample count of the given render context.
*
* @param {RenderContext} renderContext - The render context.
* @return {number} The sample count.
*/
getSampleCountRenderContext( renderContext ) {
if ( renderContext.textures !== null ) {
return this.getSampleCount( renderContext.sampleCount );
}
return this.getSampleCount( this.backend.renderer.currentSamples );
}
/**
* Returns the preferred canvas format.
*
* There is a separate method for this so it's possible to
* honor edge cases for specific devices.
*
* @return {string} The GPU texture format of the canvas.
*/
getPreferredCanvasFormat() {
const parameters = this.backend.parameters;
let bufferType = parameters.outputBufferType;
if ( bufferType === undefined ) {
bufferType = parameters.outputType;
}
if ( bufferType === undefined ) {
return navigator.gpu.getPreferredCanvasFormat();
} else if ( bufferType === UnsignedByteType ) {
return GPUTextureFormat.BGRA8Unorm;
} else if ( bufferType === HalfFloatType ) {
return GPUTextureFormat.RGBA16Float;
} else {
throw new Error( 'Unsupported output buffer type.' );
}
}
}
export default WebGPUUtils;