-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdescriptor.go
More file actions
622 lines (550 loc) · 16.9 KB
/
Copy pathdescriptor.go
File metadata and controls
622 lines (550 loc) · 16.9 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//go:build !rust && !(js && wasm)
package wgpu
import (
"github.com/gogpu/gputypes"
"github.com/gogpu/wgpu/hal"
)
// Extent3D is a 3D size.
type Extent3D struct {
Width uint32
Height uint32
DepthOrArrayLayers uint32
}
func (e Extent3D) toHAL() hal.Extent3D {
return hal.Extent3D{Width: e.Width, Height: e.Height, DepthOrArrayLayers: e.DepthOrArrayLayers}
}
// Origin3D is a 3D origin point.
type Origin3D struct {
X uint32
Y uint32
Z uint32
}
func (o Origin3D) toHAL() hal.Origin3D {
return hal.Origin3D{X: o.X, Y: o.Y, Z: o.Z}
}
// ImageDataLayout describes the layout of image data in a buffer.
type ImageDataLayout struct {
Offset uint64
BytesPerRow uint32
RowsPerImage uint32
}
func (l ImageDataLayout) toHAL() hal.ImageDataLayout {
return hal.ImageDataLayout{Offset: l.Offset, BytesPerRow: l.BytesPerRow, RowsPerImage: l.RowsPerImage}
}
// BufferDescriptor describes buffer creation parameters.
type BufferDescriptor struct {
Label string
Size uint64
Usage BufferUsage
MappedAtCreation bool
}
// toHAL converts a BufferDescriptor to a hal.BufferDescriptor.
func (d *BufferDescriptor) toHAL() *hal.BufferDescriptor {
return &hal.BufferDescriptor{
Label: d.Label,
Size: d.Size,
Usage: d.Usage,
MappedAtCreation: d.MappedAtCreation,
}
}
// TextureDescriptor describes texture creation parameters.
type TextureDescriptor struct {
Label string
Size Extent3D
MipLevelCount uint32
SampleCount uint32
Dimension TextureDimension
Format TextureFormat
Usage TextureUsage
ViewFormats []TextureFormat
}
// toHAL converts a TextureDescriptor to a hal.TextureDescriptor.
func (d *TextureDescriptor) toHAL() *hal.TextureDescriptor {
return &hal.TextureDescriptor{
Label: d.Label,
Size: d.Size.toHAL(),
MipLevelCount: d.MipLevelCount,
SampleCount: d.SampleCount,
Dimension: d.Dimension,
Format: d.Format,
Usage: d.Usage,
ViewFormats: d.ViewFormats,
}
}
// TextureViewDescriptor describes texture view creation parameters.
type TextureViewDescriptor struct {
Label string
Format TextureFormat
Dimension TextureViewDimension
Aspect TextureAspect
BaseMipLevel uint32
MipLevelCount uint32
BaseArrayLayer uint32
ArrayLayerCount uint32
}
// toHAL converts a TextureViewDescriptor to a hal.TextureViewDescriptor.
func (d *TextureViewDescriptor) toHAL() *hal.TextureViewDescriptor {
return &hal.TextureViewDescriptor{
Label: d.Label,
Format: d.Format,
Dimension: d.Dimension,
Aspect: d.Aspect,
BaseMipLevel: d.BaseMipLevel,
MipLevelCount: d.MipLevelCount,
BaseArrayLayer: d.BaseArrayLayer,
ArrayLayerCount: d.ArrayLayerCount,
}
}
// SamplerDescriptor describes sampler creation parameters.
type SamplerDescriptor struct {
Label string
AddressModeU AddressMode
AddressModeV AddressMode
AddressModeW AddressMode
MagFilter FilterMode
MinFilter FilterMode
MipmapFilter FilterMode
LodMinClamp float32
LodMaxClamp float32
Compare CompareFunction
Anisotropy uint16
}
// toHAL converts a SamplerDescriptor to a hal.SamplerDescriptor.
func (d *SamplerDescriptor) toHAL() *hal.SamplerDescriptor {
return &hal.SamplerDescriptor{
Label: d.Label,
AddressModeU: d.AddressModeU,
AddressModeV: d.AddressModeV,
AddressModeW: d.AddressModeW,
MagFilter: d.MagFilter,
MinFilter: d.MinFilter,
MipmapFilter: d.MipmapFilter,
LodMinClamp: d.LodMinClamp,
LodMaxClamp: d.LodMaxClamp,
Compare: d.Compare,
Anisotropy: d.Anisotropy,
}
}
// ShaderModuleDescriptor describes shader module creation parameters.
type ShaderModuleDescriptor struct {
Label string
WGSL string // WGSL source code
SPIRV []uint32 // SPIR-V bytecode (alternative to WGSL)
}
// toHAL converts a ShaderModuleDescriptor to a hal.ShaderModuleDescriptor.
func (d *ShaderModuleDescriptor) toHAL() *hal.ShaderModuleDescriptor {
return &hal.ShaderModuleDescriptor{
Label: d.Label,
Source: hal.ShaderSource{
WGSL: d.WGSL,
SPIRV: d.SPIRV,
},
}
}
// CommandEncoderDescriptor describes command encoder creation.
type CommandEncoderDescriptor struct {
Label string
}
// toHAL converts a CommandEncoderDescriptor to a hal.CommandEncoderDescriptor.
func (d *CommandEncoderDescriptor) toHAL() *hal.CommandEncoderDescriptor {
return &hal.CommandEncoderDescriptor{
Label: d.Label,
}
}
// BindGroupLayoutDescriptor describes a bind group layout.
type BindGroupLayoutDescriptor struct {
Label string
Entries []BindGroupLayoutEntry
}
// toHAL converts a BindGroupLayoutDescriptor to a hal.BindGroupLayoutDescriptor.
func (d *BindGroupLayoutDescriptor) toHAL() *hal.BindGroupLayoutDescriptor {
return &hal.BindGroupLayoutDescriptor{
Label: d.Label,
Entries: d.Entries,
}
}
// BindGroupDescriptor describes a bind group.
type BindGroupDescriptor struct {
Label string
Layout *BindGroupLayout
Entries []BindGroupEntry
}
// BindGroupEntry describes a single resource binding in a bind group.
// Exactly one of Buffer, Sampler, or TextureView must be set.
type BindGroupEntry struct {
Binding uint32
Buffer *Buffer // For buffer bindings
Offset uint64 // Buffer offset
Size uint64 // Buffer binding size (0 = rest of buffer)
Sampler *Sampler // For sampler bindings
TextureView *TextureView // For texture bindings
}
// toHAL converts a BindGroupEntry to a gputypes.BindGroupEntry.
func (e *BindGroupEntry) toHAL() gputypes.BindGroupEntry {
entry := gputypes.BindGroupEntry{
Binding: e.Binding,
}
var halView hal.TextureView
if e.TextureView != nil {
halView = e.TextureView.resolveHAL()
}
switch {
case e.Buffer != nil:
halBuf := e.Buffer.halBuffer()
if halBuf != nil {
entry.Resource = gputypes.BufferBinding{
Buffer: halBuf.NativeHandle(),
Offset: e.Offset,
Size: e.Size,
}
}
case e.Sampler != nil && e.Sampler.hal != nil:
entry.Resource = gputypes.SamplerBinding{
Sampler: e.Sampler.hal.NativeHandle(),
}
case halView != nil:
entry.Resource = gputypes.TextureViewBinding{
TextureView: halView.NativeHandle(),
}
}
return entry
}
// PipelineLayoutDescriptor describes a pipeline layout.
type PipelineLayoutDescriptor struct {
Label string
BindGroupLayouts []*BindGroupLayout
}
// StencilOperation describes a stencil operation.
// Canonical definition in gputypes (webgpu.h spec-compliant values).
type StencilOperation = gputypes.StencilOperation
// Stencil operation constants (webgpu.h spec values).
const (
StencilOperationKeep = gputypes.StencilOperationKeep
StencilOperationZero = gputypes.StencilOperationZero
StencilOperationReplace = gputypes.StencilOperationReplace
StencilOperationInvert = gputypes.StencilOperationInvert
StencilOperationIncrementClamp = gputypes.StencilOperationIncrementClamp
StencilOperationDecrementClamp = gputypes.StencilOperationDecrementClamp
StencilOperationIncrementWrap = gputypes.StencilOperationIncrementWrap
StencilOperationDecrementWrap = gputypes.StencilOperationDecrementWrap
)
// StencilFaceState describes stencil operations for a face.
type StencilFaceState struct {
Compare CompareFunction
FailOp StencilOperation
DepthFailOp StencilOperation
PassOp StencilOperation
}
func (s StencilFaceState) toHAL() hal.StencilFaceState {
return hal.StencilFaceState{Compare: s.Compare, FailOp: s.FailOp, DepthFailOp: s.DepthFailOp, PassOp: s.PassOp}
}
// DepthStencilState describes depth and stencil testing configuration.
type DepthStencilState struct {
Format TextureFormat
DepthWriteEnabled bool
DepthCompare CompareFunction
StencilFront StencilFaceState
StencilBack StencilFaceState
StencilReadMask uint32
StencilWriteMask uint32
DepthBias int32
DepthBiasSlopeScale float32
DepthBiasClamp float32
}
func (d *DepthStencilState) toHAL() *hal.DepthStencilState {
if d == nil {
return nil
}
return &hal.DepthStencilState{
Format: d.Format,
DepthWriteEnabled: d.DepthWriteEnabled,
DepthCompare: d.DepthCompare,
StencilFront: d.StencilFront.toHAL(),
StencilBack: d.StencilBack.toHAL(),
StencilReadMask: d.StencilReadMask,
StencilWriteMask: d.StencilWriteMask,
DepthBias: d.DepthBias,
DepthBiasSlopeScale: d.DepthBiasSlopeScale,
DepthBiasClamp: d.DepthBiasClamp,
}
}
// RenderPipelineDescriptor describes a render pipeline.
type RenderPipelineDescriptor struct {
Label string
Layout *PipelineLayout
Vertex VertexState
Primitive PrimitiveState
DepthStencil *DepthStencilState
Multisample MultisampleState
Fragment *FragmentState
}
// VertexState describes the vertex shader stage.
type VertexState struct {
Module *ShaderModule
EntryPoint string
Buffers []VertexBufferLayout
}
// FragmentState describes the fragment shader stage.
type FragmentState struct {
Module *ShaderModule
EntryPoint string
Targets []ColorTargetState
}
// toHAL converts a RenderPipelineDescriptor to a hal.RenderPipelineDescriptor.
func (d *RenderPipelineDescriptor) toHAL() *hal.RenderPipelineDescriptor {
halDesc := &hal.RenderPipelineDescriptor{
Label: d.Label,
Primitive: d.Primitive,
Multisample: d.Multisample,
DepthStencil: d.DepthStencil.toHAL(),
}
if d.Layout != nil {
halDesc.Layout = d.Layout.hal
}
if d.Vertex.Module != nil {
halDesc.Vertex = hal.VertexState{
Module: d.Vertex.Module.hal,
EntryPoint: d.Vertex.EntryPoint,
Buffers: d.Vertex.Buffers,
}
}
if d.Fragment != nil && d.Fragment.Module != nil {
halDesc.Fragment = &hal.FragmentState{
Module: d.Fragment.Module.hal,
EntryPoint: d.Fragment.EntryPoint,
Targets: d.Fragment.Targets,
}
}
return halDesc
}
// ComputePipelineDescriptor describes a compute pipeline.
type ComputePipelineDescriptor struct {
Label string
Layout *PipelineLayout
Module *ShaderModule
EntryPoint string
// Constants are pipeline-overridable constants (WebGPU spec).
// Keys are constant identifiers defined in the shader, values are float64 overrides.
// Nil or empty means no overrides are applied.
//
// Matches Rust wgpu PipelineConstants (wgpu-types). Each backend applies these
// during shader compilation via naga's override processing.
Constants map[string]float64
// ZeroInitializeWorkgroupMemory controls whether workgroup-scoped shared memory
// is zero-initialized before compute shader execution (WebGPU spec default: true).
//
// When nil (zero value for *bool), defaults to true per WebGPU spec.
// Set to false only for cross-platform applications that do not rely on
// zero-initialized workgroup memory and want to avoid the overhead.
//
// Matches Rust wgpu ProgrammableStage.zero_initialize_workgroup_memory.
ZeroInitializeWorkgroupMemory *bool
}
// toHAL converts a ComputePipelineDescriptor to a hal.ComputePipelineDescriptor.
func (d *ComputePipelineDescriptor) toHAL() *hal.ComputePipelineDescriptor {
halDesc := &hal.ComputePipelineDescriptor{
Label: d.Label,
}
if d.Layout != nil {
halDesc.Layout = d.Layout.hal
}
// WebGPU spec: zero_initialize_workgroup_memory defaults to true.
// When the caller does not set the field (nil), we default to true.
zeroInit := true
if d.ZeroInitializeWorkgroupMemory != nil {
zeroInit = *d.ZeroInitializeWorkgroupMemory
}
if d.Module != nil {
halDesc.Compute = hal.ComputeState{
Module: d.Module.hal,
EntryPoint: d.EntryPoint,
Constants: d.Constants,
ZeroInitializeWorkgroupMemory: zeroInit,
}
}
return halDesc
}
// RenderPassDescriptor describes a render pass.
type RenderPassDescriptor struct {
Label string
ColorAttachments []RenderPassColorAttachment
DepthStencilAttachment *RenderPassDepthStencilAttachment
}
// RenderPassColorAttachment describes a color attachment.
type RenderPassColorAttachment struct {
View *TextureView
ResolveTarget *TextureView
LoadOp LoadOp
StoreOp StoreOp
ClearValue Color
}
// RenderPassDepthStencilAttachment describes a depth/stencil attachment.
type RenderPassDepthStencilAttachment struct {
View *TextureView
DepthLoadOp LoadOp
DepthStoreOp StoreOp
DepthClearValue float32
DepthReadOnly bool
StencilLoadOp LoadOp
StencilStoreOp StoreOp
StencilClearValue uint32
StencilReadOnly bool
}
// toHAL converts a RenderPassDescriptor to a hal.RenderPassDescriptor.
func (d *RenderPassDescriptor) toHAL() *hal.RenderPassDescriptor {
halDesc := &hal.RenderPassDescriptor{
Label: d.Label,
}
for _, ca := range d.ColorAttachments {
halCA := hal.RenderPassColorAttachment{
LoadOp: ca.LoadOp,
StoreOp: ca.StoreOp,
ClearValue: ca.ClearValue,
}
if ca.View != nil {
halCA.View = ca.View.resolveHAL()
}
if ca.ResolveTarget != nil {
halCA.ResolveTarget = ca.ResolveTarget.resolveHAL()
}
halDesc.ColorAttachments = append(halDesc.ColorAttachments, halCA)
}
if d.DepthStencilAttachment != nil {
ds := d.DepthStencilAttachment
halDS := &hal.RenderPassDepthStencilAttachment{
DepthLoadOp: ds.DepthLoadOp,
DepthStoreOp: ds.DepthStoreOp,
DepthClearValue: ds.DepthClearValue,
DepthReadOnly: ds.DepthReadOnly,
StencilLoadOp: ds.StencilLoadOp,
StencilStoreOp: ds.StencilStoreOp,
StencilClearValue: ds.StencilClearValue,
StencilReadOnly: ds.StencilReadOnly,
}
if ds.View != nil {
halDS.View = ds.View.resolveHAL()
}
halDesc.DepthStencilAttachment = halDS
}
return halDesc
}
// ComputePassDescriptor describes a compute pass.
type ComputePassDescriptor struct {
Label string
}
// toHAL converts a ComputePassDescriptor to a hal.ComputePassDescriptor.
func (d *ComputePassDescriptor) toHAL() *hal.ComputePassDescriptor {
return &hal.ComputePassDescriptor{
Label: d.Label,
}
}
// SurfaceConfiguration describes surface settings.
type SurfaceConfiguration struct {
Width uint32
Height uint32
Format TextureFormat
Usage TextureUsage
PresentMode PresentMode
AlphaMode CompositeAlphaMode
}
// toHAL converts a SurfaceConfiguration to a hal.SurfaceConfiguration.
func (c *SurfaceConfiguration) toHAL() *hal.SurfaceConfiguration {
return &hal.SurfaceConfiguration{
Width: c.Width,
Height: c.Height,
Format: c.Format,
Usage: c.Usage,
PresentMode: c.PresentMode,
AlphaMode: c.AlphaMode,
}
}
// ImageCopyTexture describes a texture subresource and origin for write operations.
type ImageCopyTexture struct {
Texture *Texture
MipLevel uint32
Origin Origin3D
Aspect TextureAspect
}
func (i *ImageCopyTexture) toHAL() *hal.ImageCopyTexture {
if i == nil || i.Texture == nil {
return nil
}
return &hal.ImageCopyTexture{
Texture: i.Texture.resolveHAL(),
MipLevel: i.MipLevel,
Origin: i.Origin.toHAL(),
Aspect: i.Aspect,
}
}
// TextureCopy describes a texture-to-texture copy region.
// WebGPU spec: GPUCommandEncoder.copyTextureToTexture()
type TextureCopy struct {
Source ImageCopyTexture
Destination ImageCopyTexture
Size Extent3D
}
func (t *TextureCopy) toHAL() hal.TextureCopy {
return hal.TextureCopy{
SrcBase: *t.Source.toHAL(),
DstBase: *t.Destination.toHAL(),
Size: t.Size.toHAL(),
}
}
// TextureUsageTransition defines a texture usage state transition.
type TextureUsageTransition struct {
OldUsage TextureUsage
NewUsage TextureUsage
}
// TextureRange specifies a range of texture subresources.
type TextureRange struct {
Aspect TextureAspect
BaseMipLevel uint32
MipLevelCount uint32
BaseArrayLayer uint32
ArrayLayerCount uint32
}
// TextureBarrier defines a texture state transition for synchronization.
// Required on Vulkan for layout transitions between render pass and copy
// operations. On Metal, GLES, and software backends this is a no-op.
type TextureBarrier struct {
Texture *Texture
Range TextureRange
Usage TextureUsageTransition
}
func (b TextureBarrier) toHAL() hal.TextureBarrier {
var t hal.Texture
if b.Texture != nil {
t = b.Texture.resolveHAL()
}
return hal.TextureBarrier{
Texture: t,
Range: hal.TextureRange{
Aspect: b.Range.Aspect,
BaseMipLevel: b.Range.BaseMipLevel,
MipLevelCount: b.Range.MipLevelCount,
BaseArrayLayer: b.Range.BaseArrayLayer,
ArrayLayerCount: b.Range.ArrayLayerCount,
},
Usage: hal.TextureUsageTransition{
OldUsage: b.Usage.OldUsage,
NewUsage: b.Usage.NewUsage,
},
}
}
// BufferTextureCopy defines a buffer-texture copy region.
type BufferTextureCopy struct {
BufferLayout ImageDataLayout
TextureBase ImageCopyTexture
Size Extent3D
}
func (c BufferTextureCopy) toHAL() hal.BufferTextureCopy {
btc := hal.BufferTextureCopy{
BufferLayout: c.BufferLayout.toHAL(),
Size: c.Size.toHAL(),
}
halTex := c.TextureBase.toHAL()
if halTex != nil {
btc.TextureBase = *halTex
}
return btc
}