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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
|
;;; Chickadee Game Toolkit
;;; Copyright © 2023 David Thompson <dthompson2@worcester.edu>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;
;; WebGPU-like abstract GPU interface.
;;
;;; Code:
(define-module (chickadee graphics gpu)
#:use-module (chickadee graphics color)
#:use-module (chickadee math vector)
#:use-module (ice-9 match)
#:use-module (ice-9 threads)
#:use-module (rnrs bytevectors)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-9 gnu)
#:export (gpu-preferred-canvas-format
gpu-shader-language-features
make-gpu-adapter
gpu-adapter?
gpu-adapter-supported-features
gpu-adapter-supported-limits
gpu-adapter-fallback?
gpu-adapter-info
gpu-adapter-request-device
make-gpu-device
gpu-device-tick
gpu-device-swap
gpu-device-gc
gpu-device-submit
current-gpu-device
define-gpu-device-backend
make-buffer
buffer?
buffer-available?
buffer-destroyed?
buffer-mapped?
buffer-size
buffer-usage
buffer-destroy!
buffer-write!
buffer-map!
buffer-unmap!
bytevector->buffer
make-extent-3d
extent-3d?
extent-3d-width
extent-3d-height
extent-3d-depth
make-texture
texture-destroy!
texture?
texture-gpu
texture-handle
texture-destroyed?
texture-width
texture-height
texture-depth
texture-mip-level-count
texture-sample-count
texture-dimension
texture-format
texture-usage
texture-view-formats
make-texture-view
texture-view-destroy!
texture-view?
texture-view-gpu
texture-view-handle
texture-view-destroyed?
texture-view-texture
texture-view-format
texture-view-dimension
texture-view-aspect
texture-view-base-mip-level
texture-view-mip-level-count
texture-view-base-layer
texture-view-layer-count
make-sampler
sampler-destroy!
sampler?
sampler-gpu
sampler-handle
sampler-destroyed?
sampler-address-mode-u
sampler-address-mode-v
sampler-address-mode-w
sampler-mag-filter
sampler-min-filter
sampler-mipmap-filter
make-buffer-binding-layout
buffer-binding-layout?
buffer-binding-layout-type
buffer-binding-layout-dynamic-offset?
buffer-binding-layout-min-binding-size
make-texture-binding-layout
texture-binding-layout?
texture-binding-layout-sample-type
texture-binding-layout-view-dimension
texture-binding-layout-multisampled?
make-storage-texture-binding-layout
storage-texture-binding-layout?
storage-texture-binding-layout-access
storage-texture-binding-layout-format
storage-texture-binding-layout-view-dimension
make-external-texture-binding-layout
external-texture-binding-layout?
make-bind-group-layout-entry
bind-group-layout-entry-binding
bind-group-layout-entry-visibility
bind-group-layout-entry-layout
make-bind-group-layout
bind-group-layout?
bind-group-layout-entries
make-bind-group-entry
bind-group-entry?
bind-group-entry-binding
bind-group-entry-resource
make-bind-group
bind-group?
bind-group-layout
bind-group-entries
make-pipeline-layout
pipeline-layout?
pipeline-layout-device
pipeline-layout-bind-group-layouts
make-shader-module
shader-module?
shader-module-vertex
shader-module-fragment
shader-module-destroyed?
make-vertex-attribute
vertex-attribute?
vertex-attribute-location
vertex-attribute-format
vertex-attribute-offset
make-vertex-buffer-layout
vertex-buffer-layout?
vertex-buffer-layout-stride
vertex-buffer-layout-step-mode
vertex-buffer-layout-attributes
make-vertex-state
vertex-state?
vertex-state-module
vertex-state-buffers
make-blend-component
blend-component?
blend-component-operation
blend-component-src-factor
blend-component-dst-factor
make-blend-state
blend-state?
blend-state-color
blend-state-alpha
make-color-target-state
color-target-state?
color-target-state-format
color-target-state-blend
color-target-state-write-mask
make-fragment-state
fragment-state?
fragment-state-module
fragment-state-targets
make-primitive-state
primitive-state?
primitive-state-topology
primitive-state-front-face
primitive-state-cull-mode
primitive-state-unclipped-depth?
make-stencil-face-state
stencil-face-state?
stencil-face-state-compare
stencil-face-state-fail-op
stencil-face-state-depth-fail-op
stencil-face-state-pass-op
make-depth-stencil-format
depth-stencil-format?
depth-stencil-state-format
depth-stencil-state-depth-write-enabled?
depth-stencil-state-compare-function
depth-stencil-state-stencil-front
depth-stencil-state-stencil-back
depth-stencil-state-stencil-read-mask
depth-stencil-state-stencil-write-mask
make-multisample-state
multisample-state?
multisample-state-count
multisample-state-mask
multisample-state-alpha-to-coverage?
make-render-pipeline
render-pipeline?
render-pipeline-vertex
render-pipeline-fragment
render-pipeline-primitive
render-pipeline-depth-stencil
render-pipeline-multisample
make-color-attachment
color-attachment?
color-attachment-view
color-attachment-resolve-target
color-attachment-clear-color
color-attachment-load-op
color-attachment-store-op
make-depth-stencil-attachment
depth-stencil-attachment?
depth-stencil-attachment-view
depth-stencil-attachment-depth-clear-value
depth-stencil-attachment-depth-load-op
depth-stencil-attachment-depth-store-op
depth-stencil-attachment-depth-read-only?
depth-stencil-attachment-stencil-clear-value
depth-stencil-attachment-stencil-load-op
depth-stencil-attachment-stencil-store-op
depth-stencil-attachment-stencil-read-only?
make-render-pass-descriptor
render-pass-descriptor?
render-pass-descriptor?-color-attachments
render-pass-descriptor?-depth-stencil-attachment
render-pass-descriptor?-max-draw-count
render-state?
render-state-pipeline
render-state-vertex-buffers
command-buffer?
command-buffer-commands
make-command-encoder
command-encoder?
command-encoder-finish
begin-render-pass
render-pass-encoder?
render-pass-encoder-pipeline
render-pass-encoder-vertex-buffers
set-render-pass-encoder-pipeline!
set-render-pass-encoder-vertex-buffer!
render-pass-draw
end-render-pass))
(define-syntax-rule (define-record-type* name
make pred
(field accessor default) ...)
(begin
(define-record-type name
(%make field ...)
pred
(field accessor) ...)
(define* (make #:key (field default) ...)
(%make field ...))))
;;;
;;; Device Queue
;;;
;;;
;;; Device
;;;
(define-syntax-rule (define-gpu-device-type name make pred
(mutex mutex-accessor)
(guardian guardian-accessor)
(internal internal-accessor)
(vfield vfield-accessor
(vfield-dispatch vfield-dispatch-args ...))
...)
(begin
(define-record-type name
(%make mutex guardian internal vfield ...)
pred
(mutex mutex-accessor)
(guardian guardian-accessor)
(internal internal-accessor)
(vfield vfield-accessor) ...)
(define* (make internal #:key vfield ...)
(%make (make-recursive-mutex) (make-guardian) internal vfield ...))
(define (vfield-dispatch gpu-device vfield-dispatch-args ...)
(with-mutex (mutex-accessor gpu-device)
((vfield-accessor gpu-device)
(internal-accessor gpu-device)
vfield-dispatch-args ...)))
...))
(define-gpu-device-type <gpu-device>
%make-gpu-device
gpu-device?
(mutex gpu-device-mutex)
(guardian gpu-device-guardian)
(internal gpu-device-internal)
(tick %gpu-device-tick (gpu-device-tick))
(swap %gpu-device-swap (gpu-device-swap))
(enqueue %gpu-device-enqueue (backend:enqueue command-buffer))
(make-buffer %gpu-device-make-buffer
(backend:make-buffer length usage))
(buffer-destroy %gpu-device-buffer-destroy
(backend:buffer-destroy buffer))
(buffer-map %gpu-device-buffer-map
(backend:buffer-map buffer mode offset sizes))
(buffer-unmap %gpu-device-buffer-unmap
(backend:buffer-unmap buffer))
(buffer-write %gpu-device-buffer-write
(backend:buffer-write buffer buffer-offset data data-offset
length))
(make-texture %gpu-device-make-texture
(backend:make-texture width height depth mip-level-count
sample-count dimension format usage
view-formats))
(texture-destroy %gpu-device-texture-destroy (backend:texture-destroy texture))
(make-texture-view %gpu-device-make-texture-view
(backend:make-texture-view texture format dimension aspect
base-mip-level mip-level-count
base-depth depth))
(texture-view-destroy %gpu-device-texture-view-destroy
(backend:texture-view-destroy view))
(make-sampler %gpu-device-make-sampler
(backend:make-sampler address-mode-u address-mode-v
address-mode-w mag-filter min-filter
mipmap-filter))
(sampler-destroy %gpu-device-sampler-destroy (backend:sampler-destroy module))
(make-shader-module %gpu-device-make-shader-module
(backend:make-shader-module vertex-source fragment-source))
(shader-module-destroy %gpu-device-shader-module-destroy
(backend:shader-module-destroy module))
(make-render-pipeline %gpu-device-make-render-pipeline
(backend:make-render-pipeline vertex fragment
primitive depth-stencil
multisample))
(render-pipeline-destroy %gpu-device-render-pipeline-destroy
(backend:render-pipeline-destroy pipeline)))
(define (gpu-device-guard! gpu-device obj)
((gpu-device-guardian gpu-device) obj))
(define *gpu-device-backends* (make-hash-table))
(define (gpu-device-backend-ref name)
(hashq-ref *gpu-device-backends* name))
(define (gpu-device-backend-set! name proc)
(hashq-set! *gpu-device-backends* name proc))
(define-syntax-rule (define-gpu-device-backend name
internal-constructor
args ...)
(gpu-device-backend-set! 'name
(lambda (window context)
(%make-gpu-device (internal-constructor window context)
args ...))))
(define current-gpu-device (make-parameter #f))
;;;
;;; Buffers
;;;
;; (define-record-type* <buffer-descriptor>
;; make-buffer-descriptor
;; buffer-descriptor?
;; (label buffer-descriptor-label "")
;; (size buffer-descriptor-size #f)
;; (mapped-at-creation? buffer-descriptor-mapped-at-creation? #f))
(define-record-type <buffer>
(%make-buffer device handle size usage state map-state)
buffer?
(device buffer-device)
(handle buffer-handle)
(size buffer-size)
(usage buffer-usage)
(state buffer-state set-buffer-state!)
(map-state buffer-map-state set-buffer-map-state!)
(mapping buffer-mapping set-buffer-mapping!))
(define (print-buffer buffer port)
(match buffer
(($ <buffer> _ handle length usage)
(format #t "#<buffer handle: ~a length: ~a usage ~a>"
handle length usage))))
(set-record-type-printer! <buffer> print-buffer)
(define (buffer-available? buffer)
(eq? (buffer-state buffer) 'available))
(define (buffer-destroyed? buffer)
(eq? (buffer-state buffer) 'destroyed))
(define (buffer-mapped? buffer)
(eq? (buffer-map-state buffer) 'mapped))
;; TODO: Validate length is > 0 and < max length.
;; TODO: Validate usage flags.
(define* (make-buffer device length #:optional (usage '(vertex)))
(let ((handle (backend:make-buffer device length usage)))
(%make-buffer device handle length usage 'available 'unmapped)))
;; TODO: Ensure buffer is unmapped first.
(define (buffer-destroy! buffer)
(unless (buffer-destroyed? buffer)
(backend:buffer-destroy (buffer-device buffer)
(buffer-handle buffer))
(set-buffer-state! buffer 'destroyed)))
(define (buffer-write! buffer at data start size)
(backend:buffer-write (buffer-device buffer)
(buffer-handle buffer)
0 data 0 size))
(define (buffer-map! buffer mode offset size)
(backend:buffer-map (buffer-device buffer)
(buffer-handle buffer)
mode offset size))
(define (buffer-unmap! buffer)
(when (buffer-mapped? buffer)
(backend:buffer-unmap (buffer-device buffer)
(buffer-handle buffer))
(set-buffer-mapping! buffer #f)))
(define* (bytevector->buffer gpu data #:optional (usage '(vertex)))
(let* ((size (bytevector-length data))
(buffer (make-buffer gpu size usage)))
(buffer-write! buffer 0 data 0 size)
buffer))
;;;
;;; Textures
;;;
(define-record-type* <extent-3d>
make-extent-3d
extent-3d?
(width extent-3d-width #f)
(height extent-3d-height 1)
(depth extent-3d-depth 1))
(define-record-type <texture>
(%make-texture device handle destroyed? width height depth mip-level-count
sample-count dimension format usage view-formats)
texture?
(device texture-device)
(handle texture-handle)
(destroyed? texture-destroyed? set-texture-destroyed!)
(width texture-width)
(height texture-height)
(depth texture-depth)
(mip-level-count texture-mip-level-count)
(sample-count texture-sample-count)
(dimension texture-dimension)
(format texture-format)
(usage texture-usage)
(view-formats texture-view-formats))
(define (print-texture texture port)
(match texture
(($ <texture> _ handle _ width height depth _ _ dimension format* usage)
(format #t "#<texture handle: ~a width: ~a height: ~a depth: ~a dimension: ~a format: ~a usage: ~a>"
handle width height depth dimension format* usage))))
(set-record-type-printer! <texture> print-texture)
(define* (make-texture device #:key
(size (make-extent-3d #:width 1))
(mip-level-count 1)
(sample-count 1)
(dimension '2d)
format usage
(view-formats '()))
(match size
(($ <extent-3d> width height depth)
(let ((handle (backend:make-texture device width height depth mip-level-count
sample-count dimension format usage
view-formats)))
(%make-texture device handle #f width height depth mip-level-count
sample-count dimension format usage view-formats)))))
(define (texture-destroy! texture)
(unless (texture-destroyed? texture)
(backend:texture-destroy (texture-device texture)
(texture-handle texture))
(set-texture-destroyed! texture #t)))
(define-record-type* <image-data-layout>
make-image-data-layout
image-data-layout?
(offset image-data-layout-offset 0)
(bytes-per-row image-data-layout-bytes-per-row #f)
(rows-per-image image-data-layout-rows-per-image #f))
(define-record-type* <image-copy-texture>
make-image-copy-texture
image-copy-texture?
(texture image-copy-texture-texture #f)
(mip-level image-copy-texture-mip-level 0)
(origin image-copy-texture-origin (vec3 0.0 0.0 0.0))
;; all, depth-only, or stencil-only
(aspect image-copy-texture-aspect 'all))
(define (texture-write! destination data data-layout size)
#t)
(define-record-type <texture-view>
(%make-texture-view device handle destroyed? texture format dimension aspect
base-mip-level mip-level-count base-layer layer-count)
texture-view?
(device texture-view-device)
(handle texture-view-handle)
(destroyed? texture-view-destroyed? set-texture-view-destroyed!)
(texture texture-view-texture)
(format texture-view-format)
(dimension texture-view-dimension) ; 2d or 3d
(aspect texture-view-aspect) ; all, stencil-only, depth-only
(base-mip-level texture-view-base-mip-level)
(mip-level-count texture-view-mip-level-count)
(base-layer texture-view-base-layer)
(layer-count texture-view-layer-count))
(define (print-texture-view view port)
(match view
(($ <texture-view> _ handle _ texture format* dimension)
(format #t "#<texture-view handle: ~a texture: ~a format: ~a dimension: ~a>"
handle texture format* dimension))))
(set-record-type-printer! <texture-view> print-texture-view)
(define* (make-texture-view device texture #:key
format
(dimension '2d)
(aspect 'all)
(base-mip-level 0)
(mip-level-count 0)
(base-layer 0)
(layer-count 0))
(let ((handle (backend:make-texture-view device texture format dimension aspect
base-mip-level mip-level-count
base-layer layer-count)))
(%make-texture-view device handle #f texture format dimension aspect
base-mip-level mip-level-count base-layer layer-count)))
(define (texture-view-destroy! view)
(unless (texture-view-destroyed? view)
(backend:texture-view-destroy (texture-view-device view)
(texture-view-handle view))
(set-texture-view-destroyed! view #t)))
;; TODO: lod, compare, anisotropy.
(define-record-type <sampler>
(%make-sampler device handle destroyed?
address-mode-u address-mode-v address-mode-w
mag-filter min-filter mipmap-filter)
sampler?
(device sampler-device)
(handle sampler-handle)
(destroyed? sampler-destroyed? set-sampler-destroyed!)
;; clamp-to-edge, repeat, or mirror-repeat
(address-mode-u sampler-address-mode-u)
(address-mode-v sampler-address-mode-v)
(address-mode-w sampler-address-mode-w)
;; nearest or linear
(mag-filter sampler-mag-filter)
(min-filter sampler-min-filter)
(mipmap-filter sampler-mipmap-filter))
(define (print-sampler view port)
(match view
(($ <sampler> _ handle _ u v w mag min mip)
(format #t "#<sampler handle: ~a address-mode-u: ~a address-mode-v: ~a address-mode-w: ~a mag-filter: ~a min-filter: ~a mipmap-filter: ~a>"
handle u v w mag min mip))))
(set-record-type-printer! <sampler> print-sampler)
(define* (make-sampler device #:key
(address-mode-u 'clamp-to-edge)
(address-mode-v 'clamp-to-edge)
(address-mode-w 'clamp-to-edge)
(mag-filter 'nearest)
(min-filter 'nearest)
(mipmap-filter 'nearest))
(let ((handle (backend:make-sampler device address-mode-u address-mode-v
address-mode-w mag-filter min-filter
mipmap-filter)))
(%make-sampler device handle #f address-mode-u address-mode-v
address-mode-w mag-filter min-filter
mipmap-filter)))
(define (sampler-destroy! sampler)
(unless (sampler-destroyed? sampler)
(backend:sampler-destroy (sampler-device sampler)
(sampler-handle sampler))
(set-sampler-destroyed! sampler #t)))
;;;
;;; Bind groups
;;;
(define-record-type* <buffer-binding-layout>
make-buffer-binding-layout
buffer-binding-layout?
;; uniform, storage, or read-only-storage
(type buffer-binding-layout-type 'uniform)
(dynamic-offset? buffer-binding-layout-dynamic-offset? #f)
(min-binding-size buffer-binding-layout-min-binding-size 0))
(define-record-type* <texture-binding-layout>
make-texture-binding-layout
texture-binding-layout?
;; float, unfilterable-float, depth, sint, or uint
(sample-type texture-binding-layout-sample-type 'float)
;; 2d or 3d
(view-dimension texture-binding-layout-view-dimension '2d)
(multisampled? texture-binding-layout-multisampled? #f))
(define-record-type* <storage-texture-binding-layout>
make-storage-texture-binding-layout
storage-texture-binding-layout?
;; write-only, read-only, or read-write
(access storage-texture-binding-layout-access 'write-only)
(format storage-texture-binding-layout-format #f)
;; 2d or 3d
(view-dimension storage-texture-binding-layout-view-dimension '2d))
(define-record-type* <external-texture-binding-layout>
make-external-texture-binding-layout
external-texture-binding-layout?)
(define-record-type* <bind-group-layout-entry>
make-bind-group-layout-entry
bind-group-layout-entry?
(binding bind-group-layout-entry-binding (error "binding required"))
;; A list of flags: vertex, fragment, compute
(visibility bind-group-layout-entry-visibility (error "visibility required"))
;; Not following the spec here which has separate object keys for
;; buffer, sampler, texture, storage texture, and external texture.
(layout bind-group-layout-entry-layout #f))
(define-record-type <bind-group-layout>
(%make-bind-group-layout entries entry-map dynamic-offset-count exclusive-pipeline)
bind-group-layout?
(entries bind-group-layout-entries)
(entry-map bind-group-layout-entry-map)
(dynamic-offset-count bind-group-layout-dynamic-offset-count)
(exclusive-pipeline bind-group-layout-exclusive-pipeline))
(define (make-bind-group-layout device entries)
(let ((entry-map
(map (match-lambda
((and layout ($ <bind-group-layout-entry> binding))
(cons binding layout)))
entries))
(dynamic-offset-count
(fold (lambda (entry sum)
(match entry
(($ <bind-group-layout-entry> _ _
($ <buffer-binding-layout> _ #t))
(+ sum 1))
(_ sum)))
0
entries)))
(%make-bind-group-layout entries entry-map dynamic-offset-count #f)))
(define-record-type* <bind-group-entry>
make-bind-group-entry
bind-group-entry?
(binding bind-group-entry-binding #f)
(resource bind-group-entry-resource #f))
(define-record-type <bind-group>
(%make-bind-group device layout entries used-resources)
bind-group?
(device bind-group-device)
(layout bind-group-layout)
(entries bind-group-entries)
(used-resources bind-group-used-resources))
(define* (make-bind-group device #:key layout (entries '()))
;; TODO: compute used resources.
(%make-bind-group device layout entries #f))
(define-record-type <pipeline-layout>
(%make-pipeline-layout device bind-group-layouts)
pipeline-layout?
(device pipeline-layout-device)
(bind-group-layouts pipeline-layout-bind-group-layouts))
(define (make-pipeline-layout device bind-group-layouts)
(%make-pipeline-layout device bind-group-layouts))
;;;
;;; Shader modules
;;;
(define-record-type <shader-module>
(%make-shader-module device handle state)
shader-module?
(device shader-module-device)
(handle shader-module-handle)
(state shader-module-state set-shader-module-state!))
(define (print-shader-module shader port)
(match shader
(($ <shader-module> _ handle)
(format #t "#<shader-module handle: ~a>" handle))))
(set-record-type-printer! <shader-module> print-shader-module)
(define (shader-module-available? shader-module)
(eq? (shader-module-state shader-module) 'available))
(define (shader-module-destroyed? shader-module)
(eq? (shader-module-state shader-module) 'destroyed))
(define* (make-shader-module gpu #:key vertex-source fragment-source)
(let ((handle (backend:make-shader-module gpu vertex-source fragment-source)))
(%make-shader-module gpu handle 'available)))
(define (shader-module-destroy! module)
(unless (shader-module-destroyed? module)
(backend:shader-module-destroy (shader-module-device module)
(shader-module-handle module))
(set-shader-module-state! module 'destroyed)))
;;;
;;; Render pipelines
;;;
(define-record-type* <vertex-attribute>
make-vertex-attribute
attribute?
(location vertex-attribute-location #f)
(format vertex-attribute-format #f)
(offset vertex-attribute-offset 0))
(define-record-type* <vertex-buffer-layout>
make-vertex-buffer-layout
vertex-buffer-layout?
(stride vertex-buffer-layout-stride 0)
(step-mode vertex-buffer-layout-step-mode 'vertex)
(attributes vertex-buffer-layout-attributes '()))
(define-record-type* <vertex-state>
make-vertex-state
vertex-state?
(module vertex-state-module #f)
(buffers vertex-state-buffers '()))
(define-record-type* <blend-component>
make-blend-component
blend-component?
(operation blend-component-operation 'add)
(src-factor blend-component-src-factor 'one)
(dst-factor blend-component-dst-factor 'zero))
(define-record-type* <blend-state>
make-blend-state
blend-state?
(color blend-state-color (make-blend-component))
(alpha blend-state-alpha (make-blend-component)))
(define-record-type* <color-target-state>
make-color-target-state
color-target-state?
(format color-target-state-format #f)
(blend color-target-state-blend #f)
(write-mask color-target-state-write-mask #xf))
(define-record-type* <fragment-state>
make-fragment-state
fragment-state?
(module fragment-state-module #f)
(targets fragment-state-targets '()))
(define-record-type* <primitive-state>
make-primitive-state
primitive-state
(topology primitive-state-topology 'triangle-list)
(front-face primitive-state-front-face 'ccw)
(cull-mode primitive-state-cull-mode 'none)
(unclipped-depth? primitive-state-unclipped-depth? #f))
(define-record-type* <stencil-face-state>
make-stencil-face-state
stencil-face-state?
(compare stencil-face-state-compare 'always)
(fail-op stencil-face-state-fail-op 'keep)
(depth-fail-op stencil-face-state-depth-fail-op 'keep)
(pass-op stencil-face-state-pass-op 'keep))
;; TODO: depth bias fields.
(define-record-type* <depth-stencil-state>
make-depth-stencil-state
depth-stencil-state?
(format depth-stencil-state-format #f)
(depth-write-enabled? depth-stencil-state-depth-write-enabled? #f)
(compare-function depth-stencil-state-compare-function #f)
(stencil-front depth-stencil-state-stencil-front (make-stencil-face-state))
(stencil-back depth-stencil-state-stencil-back (make-stencil-face-state))
(stencil-read-mask depth-stencil-state-stencil-read-mask #xffffffff)
(stencil-write-mask depth-stencil-state-stencil-write-mask #xffffffff))
(define-record-type* <multisample-state>
make-multisample-state
multisample-state?
(count multisample-state-count 1)
(mask multisample-state-mask #xffffffff)
(alpha-to-coverage? multisample-state-alpha-to-coverage? #f))
(define-record-type <render-pipeline>
(%make-render-pipeline handle vertex fragment primitive depth-stencil multisample)
render-pipeline?
(handle render-pipeline-handle)
(vertex render-pipeline-vertex)
(fragment render-pipeline-fragment)
(primitive render-pipeline-primitive)
(depth-stencil render-pipeline-depth-stencil)
(multisample render-pipeline-multisample))
(define* (make-render-pipeline gpu #:key vertex fragment
(primitive (make-primitive-state))
depth-stencil
(multisample (make-multisample-state)))
(unless (vertex-state? vertex)
(error "vertex state is required"))
(let ((handle (backend:make-render-pipeline gpu vertex fragment primitive
depth-stencil multisample)))
(%make-render-pipeline handle vertex fragment primitive depth-stencil
multisample)))
(define default-clear-color (make-color 0.0 0.0 0.0 0.0))
(define-record-type* <color-attachment>
make-color-attachment
color-attachment?
(view color-attachment-view #f)
(resolve-target color-attachment-resolve-target #f)
(clear-color color-attachment-clear-color default-clear-color)
(load-op color-attachment-load-op 'clear)
(store-op color-attachment-store-op 'store))
(define-record-type* <depth-stencil-attachment>
make-depth-stencil-attachment
depth-stencil-attachment?
(view depth-stencil-attachment-view #f)
(depth-clear-value depth-stencil-attachment-depth-clear-value 0.0)
(depth-load-op depth-stencil-attachment-depth-load-op 'clear)
(depth-store-op depth-stencil-attachment-depth-store-op 'store)
(depth-read-only? depth-stencil-attachment-depth-read-only? #f)
(stencil-clear-value depth-stencil-attachment-stencil-clear-value 0)
(stencil-load-op depth-stencil-attachment-stencil-load-op 'clear)
(stencil-store-op depth-stencil-attachment-stencil-store-op 'store)
(stencil-read-only? depth-stencil-attachment-stencil-read-only? #f))
(define-record-type* <render-pass-descriptor>
make-render-pass-descriptor
render-pass-descriptor?
(color-attachments render-pass--descriptorcolor-attachments '())
(depth-stencil-attachment render-pass-descriptor-depth-stencil-attachment #f)
(max-draw-count render-pass-descriptor-max-draw-count 50000000))
;;;
;;; Commands
;;;
(define-record-type <command-buffer>
(make-command-buffer commands)
command-buffer?
(commands command-buffer-commands))
;; A public wrapper around a command buffer. The command encoder asks
;; the GPU for a command buffer (so that the GPU can reuse command
;; buffers repeatedly to reduce allocation), fills it up with
;; commands, and then submits the buffer back to the GPU for
;; execution.
(define-record-type <command-encoder>
(%make-command-encoder state commands)
command-encoder?
(state command-encoder-state set-command-encoder-state!)
(commands command-encoder-commands set-command-encoder-commands!))
;; TODO: Don't hardcode this limit and move the limit into the GPU
;; object.
(define max-vertex-buffers 8)
(define-record-type <render-pass-encoder>
(make-render-pass-encoder state command-encoder descriptor pipeline
vertex-buffers)
render-pass-encoder?
(state render-pass-encoder-state set-render-pass-encoder-state!)
(command-encoder render-pass-encoder-command-encoder)
(descriptor render-pass-encoder-descriptor)
(pipeline render-pass-encoder-pipeline set-render-pass-encoder-pipeline!)
(vertex-buffers render-pass-encoder-vertex-buffers
set-render-pass-encoder-vertex-buffers!))
(define-record-type <render-state>
(%make-render-state pipeline vertex-buffers)
render-state?
(pipeline render-state-pipeline)
(vertex-buffers render-state-vertex-buffers))
(define-record-type <draw-command>
(make-draw-command render-state vertex-count instance-count
first-vertex first-instance)
draw-command?
(render-state draw-command-render-state)
(vertex-count draw-command-vertex-count)
(instance-count draw-command-instance-count)
(first-vertex draw-command-first-vertex)
(first-instance draw-command-first-instance))
(define (make-render-state pipeline vertex-buffers)
(%make-render-state pipeline (vector-copy vertex-buffers)))
(define (make-command-encoder)
(%make-command-encoder 'open '()))
(define (command-encoder-add! encoder command)
(set-command-encoder-commands! encoder
(cons command
(command-encoder-commands encoder))))
(define (command-encoder-finish encoder)
(make-command-buffer (reverse (command-encoder-commands encoder))))
(define (begin-render-pass command-encoder pass-descriptor)
(make-render-pass-encoder 'open command-encoder pass-descriptor
#f (make-vector max-vertex-buffers #f)))
(define (end-render-pass pass)
(set-render-pass-encoder-state! pass 'closed))
(define (render-pass-encoder->render-state pass)
(match pass
(($ <render-pass-encoder> _ _ _ pipeline vertex-buffers)
(make-render-state pipeline vertex-buffers))))
(define (set-render-pass-encoder-vertex-buffer! pass index buffer)
(vector-set! (render-pass-encoder-vertex-buffers pass) index buffer))
(define* (render-pass-draw pass vertex-count #:key
(instance-count 1)
(first-vertex 0)
(first-instance 0))
(let ((cmd (make-draw-command (render-pass-encoder->render-state pass)
vertex-count instance-count
first-vertex first-instance)))
(command-encoder-add! (render-pass-encoder-command-encoder pass) cmd)))
(define (gpu-device-submit device command-buffer)
(with-mutex (gpu-device-mutex device)
(backend:enqueue device command-buffer)))
;;;
;;; Garbage collection
;;;
(define (gpu-device-gc device)
(with-mutex (gpu-device-mutex device)
(let ((guardian (gpu-device-guardian device)))
(let loop ()
(let ((obj (guardian)))
(when obj
(match obj
((? buffer? buffer) (buffer-destroy! buffer))
((? texture? texture) (texture-destroy! texture))
((? shader-module? module) (shader-module-destroy! module)))
(loop)))))))
;;;
;;; Adapter
;;;
(define (gpu-adapter-supported-features adapter)
(error "unimplemented"))
(define (gpu-adapter-supported-limits adapter)
(error "unimplemented"))
(define (gpu-adapter-fallback? adapter)
(error "unimplemented"))
;; requestAdapterInfo
(define (gpu-adapter-info adapter)
(error "unimplemented"))
;; Feature names:
;; depth-clip-control
;; depth32float-stencil8
;; texture-compression-bc
;; texture-compression-etc2
;; texture-compression-astc
;; timestamp-query
;; indirect-first-instance
;; shader-f16
;; rg11b10ufloat-renderable
;; bgra8unorm-storage
;; float32-filterable
(define* (gpu-adapter-request-device adapter window context #:key
(required-features '())
(required-limits '())
default-queue)
;; Include module at runtime to avoid circular reference.
;;
;; TODO: Programatically discover backend modules, load them, and
;; pick the "best" one.
(module-use! (current-module)
(resolve-interface
'(chickadee graphics backend opengl)))
((gpu-device-backend-ref 'opengl) window context))
;;;
;;; GPU
;;;
(define-record-type <gpu-adapter>
(%make-gpu-adapter power-preference force-fallback?)
gpu-adapter?
(power-preference gpu-adapter-power-preference)
(force-fallback? gpu-adapter-force-fallback?))
;; navigator.gpu.requestAdapter
;;
;; Power preference can either be low-power or high-performance
(define* (make-gpu-adapter #:key power-preference force-fallback?)
(%make-gpu-adapter power-preference force-fallback?))
(define (gpu-preferred-canvas-format)
(error "unimplemented"))
;; Avoiding the official name with "wgsl" in it because Chickadee has
;; its own shader language and does not expose WGSL at all.
(define (gpu-shader-language-features)
(error "unimplemented"))
|