This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi-proposal.html
More file actions
921 lines (714 loc) · 26 KB
/
Copy pathapi-proposal.html
File metadata and controls
921 lines (714 loc) · 26 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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
<!DOCTYPE html>
<html>
<head>
<title>WebGPU API</title>
<meta name="viewport" content="width=device-width">
<link href="third-party/prism.css" rel="stylesheet" />
<style>
body {
font-size: 14px;
line-height: 1.5;
font-family: -apple-system, Helvetica;
background-color: #eee;
padding: 30px 20px;
max-width: 1100px;
}
pre {
font-family: Menlo, monospace;
border: 1px solid #ccc;
font-size: 80%;
}
code.example {
padding-left: 3em !important;
}
h1 {
font-size: 45px;
font-weight: bold;
border-bottom: 1px solid #aac;
margin-bottom: 30px;
}
h2 {
font-size: 35px;
font-weight: bold;
border-bottom: 1px solid #ddd;
margin-bottom: 30px;
}
h3 {
font-size: 18px;
font-weight: normal;
}
header h3 {
text-align: right;
}
li {
margin: 0.5em 0;
}
a {
color: #500;
}
.note {
margin: 20px;
padding: 20px;
background-color: #E9ADAD;
border: 1px solid #FF3C2F;
border-left-width: 5px;
}
</style>
<!-- <link rel="stylesheet" href="highlight/github-gist.css">
<script src="highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script> -->
</head>
<body>
<script src="third-party/prism.js"></script>
<header>
<h1>WebGPU API Proposal</h1>
<h3>30 Jan 2017 - <a href="mailto:dino@apple.com">dino@apple.com</a></h3>
<h2>Contents</h2>
<ol>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#todo">To Do</a></li>
<li><a href="#api">API</a></li>
<li><a href="#shadinglanguage">Shading Language</a></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#changes">Change Log</a></li>
</ol>
</header>
<h2 id="introduction">1. Introduction</h2>
<p>
This is Apple's draft proposal for the WebGPU API. It started
as a mapping of Metal to JavaScript, but that won't be where it
ends up. Not only are there some things in Metal that don't
quite fit with Vulkan and D3D12, we also don't
want to be tied to the Metal API.
So please consider this a work in progress.
There is a small source code example at
the end (although it is a bit out of date).
</p>
<p>
The main differences from the traditional native GPU APIs are:
</p>
<ul>
<li>Since we always need to validate shader code, you can only create a library
by passing a string. At some point we'll need an entry point for a
ByteArray, which hold the raw IR format (e.g. SPIR-V for WebGPU).</li>
<li>The Array types are exposed as native JavaScript Arrays.</li>
<li>Constants are exposed as strings rather than numbers. I'm
not sure what to do about the bitmasks yet - it's not something you typically
see in JavaScript.</li>
<li>No parallel render command encoders.</li>
</ul>
<h2 id="todo">2. To Do</h2>
<ul>
<li>Missing APIs for reflection (e.g. vertex attribute information)</li>
<li>I'm still trying to work out what the best way to display the rendered
output is, i.e. the equivalent of presentDrawable. This is really important,
but I haven't thought of a nice Web-ish way to do it yet.</li>
<li>We probably need to be a lot more smart about threading/Workers.
Many people will want to have the renderer running on a separate Worker, and
signalling back to the main thread that the canvas should be displayed.</li>
<li>Come up with some less Metal-like names for some things where possible.
Vulkan uses Graphics rather than Render, and DMA rather than Blit.</li>
<li>The Descriptor objects should take JS dictionaries as optional arguments in
the constructor, which would allow for one step configuration. For example,
WebGPUTextureDescriptor should have an WebGPUTextureDescriptorInit dictionary
type that can be used for construction.</li>
<li>There are a lot of questions in comments e.g. how to expose a color</li>
<li>Add support for compressed textures. ASTC is a good choice on mobile, but
not supported on desktop.</li>
</ul>
<h2 id="api">3. API</h2>
<pre>
<code class="language-clike">
interface WebGPURenderingContext : CanvasRenderingContext {
// -- Identification and feature detection
readonly attribute DOMString name;
boolean supportsFeatureSet(WebGPUFeatureSet featureSet);
// -- Library creation
WebGPULibrary createLibrary(DOMString sourceCode);
// -- Command queue creation
WebGPUCommandQueue createCommandQueue();
// @@ Do we need createCommandQueueWithMaxCommandBufferCount?
// -- Resources
WebGPUBuffer createBuffer(ArrayBufferView data);
WebGPUTexture createTexture(WebGPUTextureDescriptor descriptor);
WebGPUSamplerState createSamplerState(WebGPUSamplerDescriptor descriptor);
// -- Rendering objects
WebGPUDepthStencilState createDepthStencilState(WebGPUDepthStencilDescriptor descriptor);
WebGPURenderPipelineState createRenderPipelineState(WebGPURenderPipelineDescriptor
descriptor);
// @@ Do we need the completion block versions?
// -- Compute
WebGPUComputePipelineState createComputePipelineState(WebGPUFunction function);
WebGPUComputePipelineState createComputePipelineState(WebGPUComputePipelineDescriptor
descriptor);
// -- Getting the rendering destination
WebGPUDrawable nextDrawable();
// @@ Come up with a better way to do this.
};
interface WebGPUCommandQueue {
attribute DOMString label;
WebGPUCommandBuffer createCommandBuffer();
};
interface WebGPUCommandBuffer {
// -- Command Encoders
WebGPURenderCommandEncoder createRenderCommandEncoder(WebGPURenderPassDescriptor
descriptor);
WebGPUBlitCommandEncoder createBlitCommandEncoder();
WebGPUComputeCommandEncoder createComputeCommandEncoder();
// -- Status
readonly attribute WebGPUStatus status;
readonly attribute DOMString error; // @@ need enum
// - Execution
void commit();
void presentDrawable(WebGPUDrawable drawable);
readonly attribute Promise scheduled;
readonly attribute Promise completed;
// @@ Should the completed promise be the return value of commit()?
// @@ Maybe add enqueue or invent some way to offload filling the buffer
// to workers?
};
interface WebGPUDrawable {
readonly attribute WebGPUTexture texture; // @@ Only the framebuffer should have this.
void present();
};
interface WebGPUCommandEncoder {
readonly attribute DOMString label;
void endEncoding();
// @@ Debugging helpers?
}
interface WebGPURenderCommandEncoder : WebGPUCommandEncoder {
// -- State
// @@ Should these be attributes?
void setBlendColor(float red, float green, float blue, float alpha);
void setCullMode(WebGPUCullMode mode);
void setDepthBias(float bias, float scale, float clamp);
void setDepthClipMode(WebGPUDepthClipMode mode);
void setDepthStencilState(WebGPUDepthStencilState depthStencilState);
void setFrontFacingWinding(WebGPUWinding mode);
void setRenderPipelineState(WebGPURenderPipelineState pipelineState);
// @@ Use Geometry interfaces?
void setScissorRect(float x, float y, float width, float height);
void setStencilReferenceValue(unsigned long value);
// @@ Check if we can overload here
void setStencilReferenceValue(unsigned long front, unsigned long back);
void setTriangleFillMode(WebGPUTriangleFill mode);
void setViewport(WebGPUViewportDictionary viewport);
void setVisibilityResultMode(WebGPUVisibilityResultMode mode, unsigned long offset);
// -- Resources
void setVertexBuffer(WebGPUBuffer buffer, unsigned long offset, unsigned long index);
void setVertexBuffers(WebGPUBuffer[] buffers, unsigned long[] offsets,
unsigned long startIndex, unsigned long count);
// @@ need setVertexBytes?
void setVertexSamplerState(WebGPUSamplerState samplerState, unsigned long index);
void setVertexSamplerStates(WebGPUSamplerState[] samplerState, unsigned long startIndex,
unsigned long count);
void setVertexTexture(WebGPUTexture texture, unsigned long index);
void setVertexTextures(WebGPUTexture[] textures, unsigned long startIndex,
unsigned long count);
void setFragmentBuffer(WebGPUBuffer buffer, unsigned long offset, unsigned long index);
void setFragmentBuffers(WebGPUBuffer[] buffers, unsigned long[] offsets,
unsigned long startIndex, unsigned long count);
// @@ need setFragmentBytes?
void setFragmentSamplerState(WebGPUSamplerState samplerState, unsigned long index);
void setFragmentSamplerStates(WebGPUSamplerState[] samplerState, unsigned long startIndex,
unsigned long count);
void setFragmentTexture(WebGPUTexture texture, unsigned long index);
void setFragmentTextures(WebGPUTexture[] textures, unsigned long startIndex,
unsigned long count);
// -- Drawing
void drawPrimitives(unsigned long type, unsigned long start, unsigned long count);
// @@ add drawInstanced and drawIndexed
};
interface WebGPUBlitCommandEncoder : WebGPUCommandEncoder {
// -- Copying Data Between Two Buffers
void copyFromBufferToBuffer(WebGPUBuffer source, unsigned long sourceOffset,
WebGPUBuffer destination,
unsigned long destinationOffset, unsigned long size);
// -- Copying Data From a Buffer to a Texture
void copyFromBufferToTexture(WebGPUBuffer buffer, unsigned long sourceOffset,
unsigned long sourceBytesPerRow,
unsigned long sourceBytesPerImage, WebGPUSize size,
WebGPUTexture texture,
unsigned long destinationSlice, unsigned long destinationLevel,
WebGPUOrigin origin);
// -- Copying Data Between Two Textures
void copyFromTextureToTexture();
// -- Copying Data from a Texture to a Buffer
void copyFromTextureToBuffer();
// - Image Operations
void fillBuffer(WebGPUBuffer buffer, unsigned long start,
unsigned long count, unsigned long value);
void generateMipmapsForTexture();
}
interface WebGPUComputeCommandEncoder : WebGPUCommandEncoder {
// - Specifying the Compute Pipeline State
void setComputePipelineState(WebGPUComputePipelineState state);
// -- Buffers and Textures
void setBuffer(WebGPUBuffer buffer, unsigned long offset, unsigned long index);
void setBuffers(WebGPUBuffer[] buffers, unsigned long[] offsets,
unsigned long startIndex, unsigned long count);
void setBuffer(ArrayBufferView bufferView, unsigned long index);
void setTexture(WebGPUTexture texture, unsigned long index);
void setTextures(WebGPUTexture[] textures, unsigned long startIndex, unsigned long count);
void setSamplerState(WebGPUSamplerState samplerState, unsigned long index);
void setSamplerStates(WebGPUSamplerState[] samplerStates, unsigned long startIndex,
unsigned long count);
// @@ do we need setThreadgroupMemoryLength?
// -- Executing
void dispatch(WebGPUSize threadgroupsPerGrid, WebGPUSize threadsPerThreadgroup);
// @@ dispatchThreadgroupsWithIndirectBuffer?
};
interface WebGPUViewport {
attribute double originX;
attribute double originY;
attribute double width;
attribute double height;
attribute double znear;
attribute double zfar;
};
[ Constructor ]
interface WebGPURenderPipelineDescriptor {
attribute DOMString label;
attribute WebGPUFunction vertexFunction;
attribute WebGPUVertexDescriptor vertexDescriptor;
attribute WebGPUFunction fragmentFunction;
attribute unsigned long sampleCount;
attribute boolean alphaToCoverageEnabled;
attribute boolean alphaToOneEnabled;
attribute boolean rasterizationEnabled;
readonly attribute WebGPURenderPipelineColorAttachmentDescriptor[] colorAttachments;
attribute WebGPUPixelFormat depthAttachmentPixelFormat;
attribute WebGPUPixelFormat stencilAttachmentPixelFormat;
void reset();
};
interface WebGPURenderPipelineState {
attribute DOMString label;
};
[ Constructor ]
interface WebGPUComputePipelineDescriptor {
attribute DOMString label;
attribute WebGPUFunction computeFunction;
attribute boolean threadGroupSizeIsMultipleOfThreadExecutionWidth;
void reset();
};
interface WebGPUComputePipelineState {
attribute unsigned long maxTotalThreadsPerThreadgroup;
attribute unsigned long threadExecutionWidth;
};
[ Constructor ]
interface WebGPUDepthStencilDescriptor {
attribute DOMString label;
attribute WebGPUCompareFunction depthCompareFunction;
attribute boolean depthWriteEnabled;
attribute WebGPUStencilDescriptor backFaceStencil;
attribute WebGPUStencilDescriptor frontFaceStencil;
};
[ Constructor ]
interface WebGPUStencilDescriptor {
// -- Specifying Stencil Functions and Operations
attribute WebGPUStencilOperation stencilFailureOperation;
attribute WebGPUStencilOperation depthFailureOperation;
attribute WebGPUStencilOperation depthStencilPassOperation;
attribute WebGPUCompareFunction stencilCompareFunction;
// -- Specifying Stencil Bit Mask Properties
attribute unsigned int readMask;
attribute unsigned int writeMask;
}
interface WebGPUDepthStencilState {
attribute DOMString label;
};
interface WebGPUSamplerDescriptor {
attribute WebGPUSamplerAddressMode rAddressMode;
attribute WebGPUSamplerAddressMode sAddressMode;
attribute WebGPUSamplerAddressMode tAddressMode;
attribute WebGPUSamplerMinMagFilter minFilter;
attribute WebGPUSamplerMinMagFilter magFilter;
attribute WebGPUSamplerMipFilter mipFilter;
attribute float lodMinClamp;
attribute float lodMaxClamp;
attribute boolean lodAverage;
attribute unsigned long maxAnisotropy;
attribute boolean normalizedCoordinates;
attribute WebGPUCompareFunction compareFunction;
attribute DOMString label;
}
interface WebGPUSamplerState {
attribute DOMString label;
};
interface WebGPURenderPassAttachmentDescriptor {
// -- Texture
attribute WebGPUTexture texture;
attribute unsigned long level;
attribute unsigned long slice;
attribute unsigned long depthPlane;
// -- Rendering Pass Actions
attribute WebGPULoadAction loadAction;
attribute WebGPUStoreAction storeAction;
// -- Specifying the Texture to Resolve Multisample Data
attribute WebGPUTexture resolveTexture;
attribute unsigned long resolveLevel;
attribute unsigned long resolveSlice;
attribute unsigned long resolveDepthPlane;
};
interface WebGPURenderPassColorAttachmentDescriptor : WebGPURenderPassAttachmentDescriptor {
attribute float[] clearColor; // @@ should color be a type?
};
interface WebGPURenderPassDepthAttachmentDescriptor : WebGPURenderPassAttachmentDescriptor {
attribute double clearDepth;
attribute WebGPUMultisampleDepthResolveFilter depthResolveFilter;
};
interface WebGPURenderPassStencilAttachmentDescriptor : WebGPURenderPassAttachmentDescriptor {
attribute unsigned long clearStencil;
};
[ Constructor ]
interface WebGPURenderPassDescriptor {
readonly attribute WebGPURenderPassColorAttachmentDescriptor[] colorAttachments;
attribute WebGPURenderPassDepthAttachmentDescriptor depthAttachment;
attribute WebGPURenderPassStencilAttachmentDescriptor stencilAttachment;
attribute WebGPUBuffer visibilityResultBuffer;
};
interface WebGPURenderPipelineColorAttachmentDescriptor {
// -- Pipeline state
attribute WebGPUPixelFormat pixelFormat;
attribute WebGPUColorWriteMask writeMask;
// -- Blending
attribute boolean blendingEnabled;
attribute WebGPUBlendOperation rgbBlendOperation;
attribute WebGPUBlendOperation alphaBlendOperation;
// -- Blend Factors
attribute WebGPUBlendFactor sourceRGBBlendFactor;
attribute WebGPUBlendFactor destinationRGBBlendFactor;
attribute WebGPUBlendFactor sourceAlphaBlendFactor;
attribute WebGPUBlendFactor destinationAlphaBlendFactor;
};
interface WebGPUResource {
readonly attribute WebGPUCPUCacheMode cpuCacheMode;
readonly attribute WebGPUStorageMode storageMode;
readonly attribute DOMString label;
void setPurgeableState(DOMString state);
}
interface WebGPUOrigin {
attribute unsigned long x;
attribute unsigned long y;
attribute unsigned long z;
};
interface WebGPUSize {
attribute unsigned long width;
attribute unsigned long height;
attribute unsigned long depth;
};
interface WebGPURegion {
attribute WebGPUOrigin origin;
attribute WebGPUSize size;
}
interface WebGPUTexture : WebGPUResource {
// @@ need API to provide data from <img>, <canvas>, <video> etc
// -- Copying Data into a Texture Image
void replaceRegion(WebGPURegion region, unsigned long mipmapLevel, unsigned long slice,
ArrayBufferView bytes, unsigned long bytesPerRow,
unsigned long bytesPerImage);
void replaceRegion(WebGPURegion region, unsigned long mipmapLevel,
ArrayBufferView bytes, unsigned long bytesPerRow);
// -- Copying Data from a Texture Image
ArrayBufferView getBytes(unsigned long bytesPerRow, unsigned long bytesPerImage,
WebGPURegion region, unsigned long mipmapLevel, unsigned long slice);
// -- Creating Textures by Reusing Image Data
WebGPUTexture newTextureView(DOMString pixelFormat, DOMString textureType,
WebGPURange levelRange, WebGPURange sliceRange);
// -- Querying Texture Attributes
readonly attribute DOMString textureType;
readonly attribute DOMString pixelFormat;
readonly attribute unsigned long width;
readonly attribute unsigned long height;
readonly attribute unsigned long depth;
readonly attribute unsigned long mipmapLevelCount;
readonly attribute unsigned long arrayLength;
readonly attribute unsigned long sampleCount;
readonly attribute boolean framebufferOnly;
readonly attribute WebGPUResource rootResource;
readonly attribute WebGPUTextureUsage usage;
// -- Querying Parent Texture Attributes
readonly attribute WebGPUTexture parentTexture;
readonly attribute unsigned long parentRelativeLevel;
readonly attribute unsigned long parentRelativeSlice;
// -- Querying Source Buffer Attributes
readonly attribute WebGPUBuffer buffer;
readonly attribute unsigned long bufferOffset;
readonly attribute unsigned long bufferBytesPerRow;
};
[ Constructor ]
interface WebGPUTextureDescriptor {
attribute DOMString textureType;
attribute DOMString pixelFormat;
attribute unsigned long width;
attribute unsigned long height;
attribute unsigned long depth;
attribute unsigned long mipmapLevelCount;
attribute unsigned long sampleCount;
attribute unsigned long arrayCount;
attribute WebGPUResourceOptions resourceOptions;
attribute WebGPUCPUCacheMode cpuCacheMode;
attribute WebGPUStorageMode storageMode;
attribute WebGPUTextureUsage usage;
};
interface WebGPUBuffer : WebGPUResource {
WebGPUTexture createTexture(WebGPUTextureDescriptor descriptor,
unsigned long offset, unsigned long bytesPerRow);
readonly attribute unsigned long length;
readonly attribute ArrayBufferView contents;
};
interface WebGPULibrary {
readonly attribute DOMString sourceCode;
attribute DOMString label;
readonly attribute DOMString[] functionNames;
WebGPUFunction functionWithName(DOMString name);
};
interface WebGPUFunction {
readonly attribute DOMString name;
readonly attribute WebGPUFunctionType functionType;
readonly attribute WebGPUVertexAttributes[] vertexAttributes;
};
enum WebGPUCompareFunction {
"never",
"less",
"equal",
"lessequal",
"greater",
"notequal",
"greaterequal",
"always"
};
enum WebGPUPixelFormat {
"BGRA8Unorm",
etc
};
enum WebGPULoadAction {
"dontcare",
"load",
"clear"
};
enum WebGPUStoreAction {
"dontcare",
"store",
"multisampleresolve"
};
enum WebGPUPrimitiveType {
"point",
"line",
"linestrip",
"triangle",
"trianglestrip"
};
enum WebGPUFunctionType {
"fragment",
"vertex"
};
enum WebGPUStencilOperation {
"keep",
"zero",
"replace",
"incrementclamp",
"decrementclamp",
"invert",
"incrementwrap",
"decrementwrap"
};
enum WebGPUStatus {
"notenqueued",
"enqueued",
"committed",
"scheduled",
"completed",
"error"
};
enum WebGPUSamplerAddressMode {
"clamptoedge",
"mirrorclamptoedge",
"repeat",
"mirrorrepeat",
"clamptozero"
};
enum WebGPUSamplerMinMagFilter {
"nearest",
"linear"
};
enum WebGPUSamplerMipFilter {
"notmipmapped",
"nearest",
"linear"
};
enum WebGPUCullMode {
"none",
"front",
"back"
};
enum WebGPUIndexType {
"uint16",
"uint32"
};
enum WebGPUVisibilityResultMode {
"disabled",
"boolean",
"counting"
};
enum WebGPUWinding {
"clockwise",
"counterclockwise"
};
enum WebGPUDepthClipMode {
"clip",
"clamp"
};
enum WebGPUTriangleFillMode {
"fill",
"lines"
};
enum WebGPUCPUCacheMode {
"defaultcache",
"writecombined"
};
enum WebGPUStorageMode {
"shared",
"managed",
"private"
};
enum WebGPUResourceOptions {
"cpucachemodedefaultcache",
"cpucachemodewritecombined",
"storagemodeshared",
"storagemodemanaged",
"storagemodeprivate",
"optioncpucachemodedefaultcache",
"optioncpucachemodewritecombined"
};
enum WebGPUTextureUsage {
"unknown",
"shaderread",
"shaderwrite",
"rendertarget",
"pixelformatview"
};
enum WebGPUBlendOperation {
"add",
"subtract",
"reversesubtract",
"min",
"max"
};
enum WebGPUBlendFactor {
"zero",
"one",
"sourcecolor",
"oneminussourcecolor",
"sourcealpha",
"oneminussourcealpha",
"destinationcolor",
"oneminusdestinationcolor",
"destinationalpha",
"oneminusdestinationalpha",
"sourcealphasaturated",
"blendcolor",
"oneminusblendcolor",
"blendalpha",
"oneminusblendalpha",
};
enum WebGPUColorWriteMask {
// This is a mask, so the calling site
// should take an array of them.
"none",
"red",
"green",
"blue",
"alpha",
"all"
}
enum WebGPUMultisampleDepthResolveFilter {
"sample0",
"min",
"max"
};
enum WebGPUFeatureSet {
// some names like...
"level1",
"level2"
};
</code>
</pre>
<h2 id="shadinglanguage">4. Shading Language</h2>
<p>WebGPU accepts a variant of the <a
href="https://developer.apple.com/library/ios/documentation/Metal/Reference/Meta
lShadingLanguageGuide/Introduction/Introduction.html">Metal Shading
Language</a> called the WebGPU Shading Language.
<p>The high-level changes from the standard Metal to WebGPU language are:</p>
<ul>
<li>Built-in types and functions are in the "WebGPU" namespace</li>
<li>Buffers/Arrays are not exposed via raw pointers. Instead, the shader must
use the built-in <code>buffer<T></code> type from the "WebGPU"
namespace. This type acts like an array, but overrides the index
access to forbid out-of-bounds access.</li>
<li>Similarly, texture reads and writes are bounds-checked, and arrays
of textures/samplers.</li>
</ul>
<p class="note">Ultimately, WebGPU will accept a shader in
some IR format, similar to DXIL or SPIR-V. It's not clear
yet if there will be a human-readable shading format.</p>
<h2 id="examples">Examples</h2>
<h3>Example 1</h3>
<p>Drawing a triangle</p>
<pre><code class="language-js">
let canvas = document.querySelector("canvas");
canvas.width = 500;
canvas.height = 500;
let gpu = canvas.getContext("Webgpu");
let library = gpu.createLibrary(loadShaderFromScript("library"));
library.label = "Example label";
let vertexF = library.functionWithName("vertex_main");
let fragmentF = library.functionWithName("fragment_main");
let vertexData = new Float32Array([
// x y z 1 r g b 1
0, 0.75, 0, 1, 1, 0, 0, 1,
-0.75, -0.75, 0, 1, 0, 1, 0, 1,
0.75, -0.75, 0, 1, 0, 0, 1, 1
]);
let vertexBuffer = gpu.createBuffer(vertexData);
let pipelineDescriptor = new WebGPURenderPipelineDescriptor();
pipelineDescriptor.vertexFunction = vertexF;
pipelineDescriptor.fragmentFunction = fragmentF;
pipelineDescriptor.colorAttachments[0].pixelFormat = "BGRA8Unorm";
let pipelineState = gpu.createRenderPipelineState(pipelineDescriptor);
let drawable = gpu.nextDrawable();
let passDescriptor = new WebGPURenderPassDescriptor();
passDescriptor.colorAttachments[0].loadAction = "clear";
passDescriptor.colorAttachments[0].storeAction = "store";
passDescriptor.colorAttachments[0].clearColor = [0.8, 0.8, 0.8, 1.0];
passDescriptor.colorAttachments[0].texture = drawable.texture;
let commandQueue = gpu.createCommandQueue();
let commandBuffer = commandQueue.createCommandBuffer();
let commandEncoder = commandBuffer.createRenderCommandEncoder(passDescriptor);
commandEncoder.setRenderPipelineState(pipelineState);
commandEncoder.setVertexBuffer(vertexBuffer, 0, 0);
commandEncoder.drawPrimitives("triangle", 0, 3);
commandEncoder.endEncoding();
commandBuffer.presentDrawable(drawable);
commandBuffer.commit();
</code></pre>
<h3>Example 2</h3>
<p>Updating uniforms</p>
<pre><code class="language-js">
// Setup
let uniformData = new Float32Array([ 0.5 ]);
let uniformBuffer = gpu.createBuffer(uniformData);
// Each frame
let uniformBufferView = new Float32Array(uniformBuffer.contents);
uniformBufferView[0] = frameId * 10; // Some per-frame value
let commandEncoder = commandBuffer.createRenderCommandEncoder(passDescriptor);
commandEncoder.setRenderPipelineState(pipelineState);
commandEncoder.setVertexBuffer(vertexBuffer, 0, 0);
commandEncoder.setVertexBuffer(uniformBuffer, 0, 1);
commandEncoder.drawPrimitives("triangle", 0, 3);
commandEncoder.endEncoding();
commandBuffer.presentDrawable(drawable);
commandBuffer.commit();
</code></pre>
</body>
</html>