-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRenderPipelineEntry.ts
415 lines (362 loc) · 15.2 KB
/
RenderPipelineEntry.ts
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { PipelineEntry } from './PipelineEntry'
import { ProjectedShaderChunks, ShaderChunks } from '../shaders/ShaderChunks'
import { isRenderer, Renderer } from '../renderers/utils'
import { throwError } from '../../utils/utils'
import {
PipelineEntryShaders,
RenderPipelineEntryOptions,
RenderPipelineEntryParams,
RenderPipelineEntryPropertiesParams,
} from '../../types/PipelineEntries'
import { GPUCurtains } from '../../curtains/GPUCurtains'
import { AllowedBindGroups, BindGroupBufferBindingElement } from '../../types/BindGroups'
import { RenderMaterialAttributes, ShaderOptions } from '../../types/Materials'
/**
* Used to create a {@link PipelineEntry} specifically designed to handle {@link core/materials/RenderMaterial.RenderMaterial | RenderMaterial}.
*
* ## Shaders patching
*
* The {@link RenderPipelineEntry} uses each of its {@link RenderPipelineEntry#bindGroups | bind groups} {@link core/bindings/Binding.Binding | Binding} to patch the given compute shader before creating the {@link GPUShaderModule}.<br>
* It will prepend every {@link core/bindings/Binding.Binding | Binding} WGSL code snippets (or fragments) with the correct bind group and bindings indices.
*
* ## Pipeline compilation
*
* The {@link RenderPipelineEntry} will then create a {@link GPURenderPipeline} (asynchronously by default).
*/
export class RenderPipelineEntry extends PipelineEntry {
/** Shaders to use with this {@link RenderPipelineEntry} */
shaders: PipelineEntryShaders
/** {@link RenderMaterialAttributes | Geometry attributes} sent to the {@link RenderPipelineEntry} */
attributes: RenderMaterialAttributes
/** {@link GPURenderPipelineDescriptor | Render pipeline descriptor} based on {@link layout} and {@link shaders} */
descriptor: GPURenderPipelineDescriptor | null
/** Options used to create this {@link RenderPipelineEntry} */
options: RenderPipelineEntryOptions
/**
* RenderPipelineEntry constructor
* @param parameters - {@link RenderPipelineEntryParams | parameters} used to create this {@link RenderPipelineEntry}
*/
constructor(parameters: RenderPipelineEntryParams) {
// eslint-disable-next-line prefer-const
let { renderer, ...pipelineParams } = parameters
const { label, ...renderingOptions } = pipelineParams
// we could pass our curtains object OR our curtains renderer object
renderer = (renderer && (renderer as GPUCurtains).renderer) || (renderer as Renderer)
const type = 'RenderPipelineEntry'
isRenderer(renderer, label ? label + ' ' + type : type)
super(parameters)
this.type = type
this.shaders = {
vertex: {
head: '',
code: '',
module: null,
},
fragment: {
head: '',
code: '',
module: null,
},
full: {
head: '',
code: '',
module: null,
},
}
this.descriptor = null
this.options = {
...this.options,
...renderingOptions,
}
}
// TODO! need to chose whether we should silently add the camera bind group here
// or explicitly in the RenderMaterial class createBindGroups() method
/**
* Merge our {@link bindGroups | pipeline entry bind groups} with the {@link core/renderers/GPUCameraRenderer.GPUCameraRenderer#cameraBindGroup | camera bind group} if needed and set them
* @param bindGroups - {@link core/materials/RenderMaterial.RenderMaterial#bindGroups | bind groups} to use with this {@link RenderPipelineEntry}
*/
setPipelineEntryBindGroups(bindGroups: AllowedBindGroups[]) {
this.bindGroups =
'cameraBindGroup' in this.renderer && this.options.rendering.useProjection
? [this.renderer.cameraBindGroup, ...bindGroups]
: bindGroups
}
/**
* Set {@link RenderPipelineEntry} properties (in this case the {@link bindGroups | bind groups} and {@link attributes})
* @param parameters - the {@link core/materials/RenderMaterial.RenderMaterial#bindGroups | bind groups} and {@link core/materials/RenderMaterial.RenderMaterial#attributes | attributes} to use
*/
setPipelineEntryProperties(parameters: RenderPipelineEntryPropertiesParams) {
const { attributes, bindGroups } = parameters
this.attributes = attributes
this.setPipelineEntryBindGroups(bindGroups)
}
/* SHADERS */
/**
* Patch the shaders by appending all the necessary shader chunks, {@link bindGroups | bind groups}) and {@link attributes} WGSL code fragments to the given {@link types/PipelineEntries.PipelineEntryParams#shaders | parameter shader code}
*/
patchShaders() {
this.shaders.vertex.head = ''
this.shaders.vertex.code = ''
this.shaders.fragment.head = ''
this.shaders.fragment.code = ''
this.shaders.full.head = ''
this.shaders.full.code = ''
// first add chunks
for (const chunk in ShaderChunks.vertex) {
this.shaders.vertex.head = `${ShaderChunks.vertex[chunk]}\n${this.shaders.vertex.head}`
this.shaders.full.head = `${ShaderChunks.vertex[chunk]}\n${this.shaders.full.head}`
}
if (this.options.shaders.fragment) {
for (const chunk in ShaderChunks.fragment) {
this.shaders.fragment.head = `${ShaderChunks.fragment[chunk]}\n${this.shaders.fragment.head}`
if (this.shaders.full.head.indexOf(ShaderChunks.fragment[chunk]) === -1) {
this.shaders.full.head = `${ShaderChunks.fragment[chunk]}\n${this.shaders.full.head}`
}
}
}
if (this.options.rendering.useProjection) {
for (const chunk in ProjectedShaderChunks.vertex) {
this.shaders.vertex.head = `${ProjectedShaderChunks.vertex[chunk]}\n${this.shaders.vertex.head}`
this.shaders.full.head = `${ProjectedShaderChunks.vertex[chunk]}\n${this.shaders.full.head}`
}
if (this.options.shaders.fragment) {
for (const chunk in ProjectedShaderChunks.fragment) {
this.shaders.fragment.head = `${ProjectedShaderChunks.fragment[chunk]}\n${this.shaders.fragment.head}`
if (this.shaders.full.head.indexOf(ProjectedShaderChunks.fragment[chunk]) === -1) {
this.shaders.full.head = `${ProjectedShaderChunks.fragment[chunk]}\n${this.shaders.full.head}`
}
}
}
}
const groupsBindings = []
this.bindGroups.forEach((bindGroup) => {
let bindIndex = 0
bindGroup.bindings.forEach((binding, bindingIndex) => {
binding.wgslGroupFragment.forEach((groupFragment, groupFragmentIndex) => {
groupsBindings.push({
groupIndex: bindGroup.index,
visibility: binding.visibility,
bindIndex,
wgslStructFragment: (binding as BindGroupBufferBindingElement).wgslStructFragment,
wgslGroupFragment: groupFragment,
newLine:
bindingIndex === bindGroup.bindings.length - 1 &&
groupFragmentIndex === binding.wgslGroupFragment.length - 1,
})
bindIndex++
})
})
})
groupsBindings.forEach((groupBinding) => {
if (
groupBinding.visibility === GPUShaderStage.VERTEX ||
groupBinding.visibility === (GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE)
) {
// do not duplicate structs
if (
groupBinding.wgslStructFragment &&
this.shaders.vertex.head.indexOf(groupBinding.wgslStructFragment) === -1
) {
this.shaders.vertex.head = `\n${groupBinding.wgslStructFragment}\n${this.shaders.vertex.head}`
}
// do not duplicate struct var as well
if (this.shaders.vertex.head.indexOf(groupBinding.wgslGroupFragment) === -1) {
this.shaders.vertex.head = `${this.shaders.vertex.head}\n@group(${groupBinding.groupIndex}) @binding(${groupBinding.bindIndex}) ${groupBinding.wgslGroupFragment}`
if (groupBinding.newLine) this.shaders.vertex.head += `\n`
}
}
if (
this.options.shaders.fragment &&
(groupBinding.visibility === GPUShaderStage.FRAGMENT ||
groupBinding.visibility === (GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE))
) {
// do not duplicate structs
if (
groupBinding.wgslStructFragment &&
this.shaders.fragment.head.indexOf(groupBinding.wgslStructFragment) === -1
) {
this.shaders.fragment.head = `\n${groupBinding.wgslStructFragment}\n${this.shaders.fragment.head}`
}
// do not duplicate struct var as well
if (this.shaders.fragment.head.indexOf(groupBinding.wgslGroupFragment) === -1) {
this.shaders.fragment.head = `${this.shaders.fragment.head}\n@group(${groupBinding.groupIndex}) @binding(${groupBinding.bindIndex}) ${groupBinding.wgslGroupFragment}`
if (groupBinding.newLine) this.shaders.fragment.head += `\n`
}
}
if (groupBinding.wgslStructFragment && this.shaders.full.head.indexOf(groupBinding.wgslStructFragment) === -1) {
this.shaders.full.head = `\n${groupBinding.wgslStructFragment}\n${this.shaders.full.head}`
}
if (this.shaders.full.head.indexOf(groupBinding.wgslGroupFragment) === -1) {
this.shaders.full.head = `${this.shaders.full.head}\n@group(${groupBinding.groupIndex}) @binding(${groupBinding.bindIndex}) ${groupBinding.wgslGroupFragment}`
if (groupBinding.newLine) this.shaders.full.head += `\n`
}
})
// add attributes to vertex shader only
this.shaders.vertex.head = `${this.attributes.wgslStructFragment}\n${this.shaders.vertex.head}`
this.shaders.full.head = `${this.attributes.wgslStructFragment}\n${this.shaders.full.head}`
this.shaders.vertex.code = this.shaders.vertex.head + this.options.shaders.vertex.code
if (typeof this.options.shaders.fragment === 'object')
this.shaders.fragment.code = this.shaders.fragment.head + this.options.shaders.fragment.code
// check if its one shader string with different entry points
if (typeof this.options.shaders.fragment === 'object') {
if (
this.options.shaders.vertex.entryPoint !== this.options.shaders.fragment.entryPoint &&
this.options.shaders.vertex.code.localeCompare(this.options.shaders.fragment.code) === 0
) {
this.shaders.full.code = this.shaders.full.head + this.options.shaders.vertex.code
} else {
this.shaders.full.code =
this.shaders.full.head + this.options.shaders.vertex.code + this.options.shaders.fragment.code
}
}
}
/* SETUP */
/**
* Get whether the shaders modules have been created
* @readonly
*/
get shadersModulesReady(): boolean {
return !(!this.shaders.vertex.module || (this.options.shaders.fragment && !this.shaders.fragment.module))
}
/**
* Create the {@link shaders}: patch them and create the {@link GPUShaderModule}
*/
createShaders() {
this.patchShaders()
const isSameShader =
typeof this.options.shaders.fragment === 'object' &&
this.options.shaders.vertex.entryPoint !== this.options.shaders.fragment.entryPoint &&
this.options.shaders.vertex.code.localeCompare(this.options.shaders.fragment.code) === 0
this.shaders.vertex.module = this.createShaderModule({
code: this.shaders[isSameShader ? 'full' : 'vertex'].code,
type: 'vertex',
})
if (this.options.shaders.fragment) {
this.shaders.fragment.module = this.createShaderModule({
code: this.shaders[isSameShader ? 'full' : 'fragment'].code,
type: 'fragment',
})
}
}
/**
* Create the render pipeline {@link descriptor}
*/
createPipelineDescriptor() {
if (!this.shadersModulesReady) return
let vertexLocationIndex = -1
// we will assume our renderer alphaMode is set to 'premultiplied'
// we either disable blending if mesh if opaque
// use a custom blending if set
// or use this blend equation if mesh is transparent (see https://limnu.com/webgl-blending-youre-probably-wrong/)
const blend =
this.options.rendering.blend ??
(this.options.rendering.transparent && {
color: {
srcFactor: 'src-alpha',
dstFactor: 'one-minus-src-alpha',
},
alpha: {
srcFactor: 'one',
dstFactor: 'one-minus-src-alpha',
},
})
this.descriptor = {
label: this.options.label,
layout: this.layout,
vertex: {
module: this.shaders.vertex.module,
entryPoint: this.options.shaders.vertex.entryPoint,
buffers: this.attributes.vertexBuffers.map((vertexBuffer) => {
return {
stepMode: vertexBuffer.stepMode,
arrayStride: vertexBuffer.arrayStride * 4, // 4 bytes each
attributes: vertexBuffer.attributes.map((attribute) => {
vertexLocationIndex++
return {
shaderLocation: vertexLocationIndex,
offset: attribute.bufferOffset, // previous attribute size * 4
format: attribute.bufferFormat,
}
}),
}
}),
},
...(this.options.shaders.fragment && {
fragment: {
module: this.shaders.fragment.module,
entryPoint: (this.options.shaders.fragment as ShaderOptions).entryPoint,
targets: [
{
format: this.options.rendering.targetFormat ?? this.renderer.options.preferredFormat,
...(blend && {
blend,
}),
},
...(this.options.rendering.additionalTargets ?? []), // merge with additional targets if any
],
},
}),
primitive: {
topology: this.options.rendering.topology,
frontFace: this.options.rendering.verticesOrder,
cullMode: this.options.rendering.cullMode,
},
...(this.options.rendering.depth && {
depthStencil: {
depthWriteEnabled: this.options.rendering.depthWriteEnabled,
depthCompare: this.options.rendering.depthCompare,
format: this.options.rendering.depthFormat,
},
}),
...(this.options.rendering.sampleCount > 1 && {
multisample: {
count: this.options.rendering.sampleCount,
},
}),
} as GPURenderPipelineDescriptor
}
/**
* Create the render {@link pipeline}
*/
createRenderPipeline() {
if (!this.shadersModulesReady) return
try {
this.pipeline = this.renderer.createRenderPipeline(this.descriptor)
} catch (error) {
this.status.error = error
throwError(error)
}
}
/**
* Asynchronously create the render {@link pipeline}
* @async
* @returns - void promise result
*/
async createRenderPipelineAsync(): Promise<void> {
if (!this.shadersModulesReady) return
try {
this.pipeline = await this.renderer.createRenderPipelineAsync(this.descriptor)
this.status.compiled = true
this.status.compiling = false
this.status.error = null
} catch (error) {
this.status.error = error
throwError(error)
}
}
/**
* Call {@link PipelineEntry#compilePipelineEntry | PipelineEntry compilePipelineEntry} method, then create our render {@link pipeline}
* @async
*/
async compilePipelineEntry(): Promise<void> {
super.compilePipelineEntry()
if (this.options.useAsync) {
await this.createRenderPipelineAsync()
} else {
this.createRenderPipeline()
this.status.compiled = true
this.status.compiling = false
this.status.error = null
}
}
}