-
-
Notifications
You must be signed in to change notification settings - Fork 631
/
HelloVulkan.java
1982 lines (1675 loc) · 83.6 KB
/
HelloVulkan.java
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
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
package org.lwjgl.demo.vulkan;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.system.*;
import org.lwjgl.vulkan.*;
import java.nio.*;
import java.util.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFWVulkan.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.vulkan.EXTDebugUtils.*;
import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
/*
* Copyright (c) 2015-2016 The Khronos Group Inc.
* Copyright (c) 2015-2016 Valve Corporation
* Copyright (c) 2015-2016 LunarG, Inc.
*
* 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.
*
* Author: Chia-I Wu <olvaffe@gmail.com>
* Author: Cody Northrop <cody@lunarg.com>
* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
* Author: Ian Elliott <ian@LunarG.com>
* Author: Jon Ashburn <jon@lunarg.com>
* Author: Piers Daniell <pdaniell@nvidia.com>
* Author: Gwan-gyeong Mun <elongbug@gmail.com>
* Porter: Camilla Berglund <elmindreda@glfw.org>
*/
/** Simple Vulkan demo. Ported from the GLFW <a href="https://github.com/glfw/glfw/blob/master/tests/vulkan.c">vulkan</a> test. */
public final class HelloVulkan {
private static final boolean VALIDATE = true;
private static final boolean USE_STAGING_BUFFER = false;
private static final int DEMO_TEXTURE_COUNT = 1;
private static final int VERTEX_BUFFER_BIND_ID = 0;
private static final ByteBuffer KHR_swapchain = memASCII(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
private static final ByteBuffer EXT_debug_utils = memASCII(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
private static final byte[] fragShaderCode = {
0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x0d, 0x00,
0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30,
0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x10, 0x00, 0x03, 0x00,
0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00,
0x02, 0x00, 0x00, 0x00, (byte)0xcc, 0x01, 0x00, 0x00, 0x04, 0x00, 0x0a, 0x00,
0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x63, 0x70,
0x70, 0x5f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x5f, 0x6c, 0x69, 0x6e, 0x65,
0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x00,
0x04, 0x00, 0x08, 0x00, 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c,
0x45, 0x5f, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x5f, 0x64, 0x69,
0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x05, 0x00, 0x04, 0x00,
0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00,
0x05, 0x00, 0x05, 0x00, 0x09, 0x00, 0x00, 0x00, 0x75, 0x46, 0x72, 0x61,
0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x00, 0x05, 0x00, 0x05, 0x00,
0x11, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64,
0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x00,
0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x47, 0x00, 0x04, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00,
0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x02, 0x00,
0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
0x3b, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x19, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x03, 0x00, 0x0b, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x17, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
0x10, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, (byte)0xf8, 0x00, 0x02, 0x00,
0x05, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00,
0x0f, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
0x57, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00,
0x09, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, (byte)0xfd, 0x00, 0x01, 0x00,
0x38, 0x00, 0x01, 0x00,
};
private static final byte[] vertShaderCode = {
0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x0d, 0x00,
0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30,
0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
0x17, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00,
(byte)0xcc, 0x01, 0x00, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x47, 0x4c, 0x5f, 0x47,
0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x63, 0x70, 0x70, 0x5f, 0x73, 0x74,
0x79, 0x6c, 0x65, 0x5f, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x64, 0x69, 0x72,
0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00,
0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x69, 0x6e,
0x63, 0x6c, 0x75, 0x64, 0x65, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74,
0x69, 0x76, 0x65, 0x00, 0x05, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00,
0x09, 0x00, 0x00, 0x00, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64,
0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
0x61, 0x74, 0x74, 0x72, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00,
0x11, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x50, 0x65, 0x72, 0x56, 0x65,
0x72, 0x74, 0x65, 0x78, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x06, 0x00,
0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x50,
0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x06, 0x00, 0x07, 0x00,
0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x50,
0x6f, 0x69, 0x6e, 0x74, 0x53, 0x69, 0x7a, 0x65, 0x00, 0x00, 0x00, 0x00,
0x06, 0x00, 0x07, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x67, 0x6c, 0x5f, 0x43, 0x6c, 0x69, 0x70, 0x44, 0x69, 0x73, 0x74, 0x61,
0x6e, 0x63, 0x65, 0x00, 0x06, 0x00, 0x07, 0x00, 0x11, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x43, 0x75, 0x6c, 0x6c, 0x44,
0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x00, 0x05, 0x00, 0x03, 0x00,
0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x03, 0x00,
0x17, 0x00, 0x00, 0x00, 0x70, 0x6f, 0x73, 0x00, 0x47, 0x00, 0x04, 0x00,
0x09, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x47, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00, 0x11, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0x00, 0x05, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00,
0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00, 0x11, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x47, 0x00, 0x03, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x47, 0x00, 0x04, 0x00, 0x17, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
0x21, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x16, 0x00, 0x03, 0x00, 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x17, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x20, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x15, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x0f, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x04, 0x00,
0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
0x1e, 0x00, 0x06, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x20, 0x00, 0x04, 0x00, 0x12, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x11, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x12, 0x00, 0x00, 0x00,
0x13, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x15, 0x00, 0x04, 0x00,
0x14, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x2b, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x16, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00,
0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x20, 0x00, 0x04, 0x00, 0x19, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x0d, 0x00, 0x00, 0x00, 0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
(byte)0xf8, 0x00, 0x02, 0x00, 0x05, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00,
0x07, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
0x3e, 0x00, 0x03, 0x00, 0x09, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x3d, 0x00, 0x04, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
0x17, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x19, 0x00, 0x00, 0x00,
0x1a, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
0x3e, 0x00, 0x03, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
(byte)0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00,
};
// buffers for handle output-params
private final IntBuffer ip = memAllocInt(1);
private final LongBuffer lp = memAllocLong(1);
private final PointerBuffer pp = memAllocPointer(1);
private PointerBuffer extension_names = memAllocPointer(64);
private VkInstance inst;
private VkPhysicalDevice gpu;
private long msg_callback;
private VkPhysicalDeviceProperties gpu_props = VkPhysicalDeviceProperties.malloc();
private VkPhysicalDeviceFeatures gpu_features = VkPhysicalDeviceFeatures.malloc();
private VkQueueFamilyProperties.Buffer queue_props;
private int width = 300;
private int height = 300;
private float depthStencil = 1.0f;
private float depthIncrement = -0.01f;
private long window;
private long surface;
private int graphics_queue_node_index;
private VkDevice device;
private VkQueue queue;
private int format;
private int color_space;
private VkPhysicalDeviceMemoryProperties memory_properties = VkPhysicalDeviceMemoryProperties.malloc();
private long cmd_pool;
private VkCommandBuffer draw_cmd;
private long swapchain;
private int swapchainImageCount;
private SwapchainBuffers[] buffers;
private int current_buffer;
private VkCommandBuffer setup_cmd;
private Depth depth = new Depth();
private TextureObject[] textures = new TextureObject[DEMO_TEXTURE_COUNT];
private Vertices vertices = new Vertices();
private long desc_layout;
private long pipeline_layout;
private long render_pass;
private long pipeline;
private long desc_pool;
private long desc_set;
private LongBuffer framebuffers;
private HelloVulkan() {
for (int i = 0; i < textures.length; i++) {
textures[i] = new TextureObject();
}
}
private final VkDebugUtilsMessengerCallbackEXT dbgFunc = VkDebugUtilsMessengerCallbackEXT.create(
(messageSeverity, messageTypes, pCallbackData, pUserData) -> {
String severity;
if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
severity = "VERBOSE";
} else if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
severity = "INFO";
} else if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
severity = "WARNING";
} else if ((messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
severity = "ERROR";
} else {
severity = "UNKNOWN";
}
String type;
if ((messageTypes & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) != 0) {
type = "GENERAL";
} else if ((messageTypes & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0) {
type = "VALIDATION";
} else if ((messageTypes & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
type = "PERFORMANCE";
} else {
type = "UNKNOWN";
}
VkDebugUtilsMessengerCallbackDataEXT data = VkDebugUtilsMessengerCallbackDataEXT.create(pCallbackData);
System.err.format(
"%s %s: [%s]\n\t%s\n",
type, severity, data.pMessageIdNameString(), data.pMessageString()
);
/*
* false indicates that layer should not bail-out of an
* API call that had validation failures. This may mean that the
* app dies inside the driver due to invalid parameter(s).
* That's what would happen without validation layers, so we'll
* keep that behavior here.
*/
return VK_FALSE;
}
);
private static void check(int errcode) {
if (errcode != 0) {
throw new IllegalStateException(String.format("Vulkan error [0x%X]", errcode));
}
}
private static void demo_init_connection() {
GLFWErrorCallback.createPrint().set();
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
if (!glfwVulkanSupported()) {
throw new IllegalStateException("Cannot find a compatible Vulkan installable client driver (ICD)");
}
}
/**
* Return true if all layer names specified in {@code check_names} can be found in given {@code layer} properties.
*/
private static PointerBuffer demo_check_layers(MemoryStack stack, VkLayerProperties.Buffer available, String... layers) {
PointerBuffer required = stack.mallocPointer(layers.length);
for (int i = 0; i < layers.length; i++) {
boolean found = false;
for (int j = 0; j < available.capacity(); j++) {
available.position(j);
if (layers[i].equals(available.layerNameString())) {
found = true;
break;
}
}
if (!found) {
System.err.format("Cannot find layer: %s\n", layers[i]);
return null;
}
required.put(i, stack.ASCII(layers[i]));
}
return required;
}
private void demo_init_vk() {
try (MemoryStack stack = stackPush()) {
PointerBuffer requiredLayers = null;
if (VALIDATE) {
check(vkEnumerateInstanceLayerProperties(ip, null));
if (ip.get(0) > 0) {
VkLayerProperties.Buffer availableLayers = VkLayerProperties.malloc(ip.get(0), stack);
check(vkEnumerateInstanceLayerProperties(ip, availableLayers));
// VulkanSDK 1.1.106+
requiredLayers = demo_check_layers(
stack, availableLayers,
"VK_LAYER_KHRONOS_validation"/*,
"VK_LAYER_LUNARG_assistant_layer"*/
);
if (requiredLayers == null) { // use alternative (deprecated) set of validation layers
requiredLayers = demo_check_layers(
stack, availableLayers,
"VK_LAYER_LUNARG_standard_validation"/*,
"VK_LAYER_LUNARG_assistant_layer"*/
);
}
if (requiredLayers == null) { // use alternative (deprecated) set of validation layers
requiredLayers = demo_check_layers(
stack, availableLayers,
"VK_LAYER_GOOGLE_threading",
"VK_LAYER_LUNARG_parameter_validation",
"VK_LAYER_LUNARG_object_tracker",
"VK_LAYER_LUNARG_core_validation",
"VK_LAYER_GOOGLE_unique_objects"/*,
"VK_LAYER_LUNARG_assistant_layer"*/
);
}
}
if (requiredLayers == null) {
throw new IllegalStateException("vkEnumerateInstanceLayerProperties failed to find required validation layer.");
}
}
PointerBuffer required_extensions = glfwGetRequiredInstanceExtensions();
if (required_extensions == null) {
throw new IllegalStateException("glfwGetRequiredInstanceExtensions failed to find the platform surface extensions.");
}
for (int i = 0; i < required_extensions.capacity(); i++) {
extension_names.put(required_extensions.get(i));
}
check(vkEnumerateInstanceExtensionProperties((String)null, ip, null));
if (ip.get(0) != 0) {
VkExtensionProperties.Buffer instance_extensions = VkExtensionProperties.malloc(ip.get(0), stack);
check(vkEnumerateInstanceExtensionProperties((String)null, ip, instance_extensions));
for (int i = 0; i < ip.get(0); i++) {
instance_extensions.position(i);
if (VK_EXT_DEBUG_UTILS_EXTENSION_NAME.equals(instance_extensions.extensionNameString())) {
if (VALIDATE) {
extension_names.put(EXT_debug_utils);
}
}
}
}
ByteBuffer APP_SHORT_NAME = stack.UTF8("tri");
VkApplicationInfo app = VkApplicationInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.pApplicationName(APP_SHORT_NAME)
.applicationVersion(0)
.pEngineName(APP_SHORT_NAME)
.engineVersion(0)
.apiVersion(VK.getInstanceVersionSupported());
extension_names.flip();
VkInstanceCreateInfo inst_info = VkInstanceCreateInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.pApplicationInfo(app)
.ppEnabledLayerNames(requiredLayers)
.ppEnabledExtensionNames(extension_names);
extension_names.clear();
VkDebugUtilsMessengerCreateInfoEXT dbgCreateInfo;
if (VALIDATE) {
dbgCreateInfo = VkDebugUtilsMessengerCreateInfoEXT.malloc(stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.messageSeverity(
/*VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |*/
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT
)
.messageType(
VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT
)
.pfnUserCallback(dbgFunc)
.pUserData(NULL);
inst_info.pNext(dbgCreateInfo.address());
}
int err = vkCreateInstance(inst_info, null, pp);
if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
throw new IllegalStateException("Cannot find a compatible Vulkan installable client driver (ICD).");
} else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
throw new IllegalStateException("Cannot find a specified extension library. Make sure your layers path is set appropriately.");
} else if (err != 0) {
throw new IllegalStateException("vkCreateInstance failed. Do you have a compatible Vulkan installable client driver (ICD) installed?");
}
inst = new VkInstance(pp.get(0), inst_info);
/* Make initial call to query gpu_count, then second call for gpu info */
check(vkEnumeratePhysicalDevices(inst, ip, null));
if (ip.get(0) > 0) {
PointerBuffer physical_devices = stack.mallocPointer(ip.get(0));
check(vkEnumeratePhysicalDevices(inst, ip, physical_devices));
/* For tri demo we just grab the first physical device */
gpu = new VkPhysicalDevice(physical_devices.get(0), inst);
} else {
throw new IllegalStateException("vkEnumeratePhysicalDevices reported zero accessible devices.");
}
/* Look for device extensions */
boolean swapchainExtFound = false;
check(vkEnumerateDeviceExtensionProperties(gpu, (String)null, ip, null));
if (ip.get(0) > 0) {
VkExtensionProperties.Buffer device_extensions = VkExtensionProperties.malloc(ip.get(0), stack);
check(vkEnumerateDeviceExtensionProperties(gpu, (String)null, ip, device_extensions));
for (int i = 0; i < ip.get(0); i++) {
device_extensions.position(i);
if (VK_KHR_SWAPCHAIN_EXTENSION_NAME.equals(device_extensions.extensionNameString())) {
swapchainExtFound = true;
extension_names.put(KHR_swapchain);
}
}
}
if (!swapchainExtFound) {
throw new IllegalStateException("vkEnumerateDeviceExtensionProperties failed to find the " + VK_KHR_SWAPCHAIN_EXTENSION_NAME + " extension.");
}
if (VALIDATE) {
err = vkCreateDebugUtilsMessengerEXT(inst, dbgCreateInfo, null, lp);
switch (err) {
case VK_SUCCESS:
msg_callback = lp.get(0);
break;
case VK_ERROR_OUT_OF_HOST_MEMORY:
throw new IllegalStateException("CreateDebugReportCallback: out of host memory");
default:
throw new IllegalStateException("CreateDebugReportCallback: unknown failure");
}
}
vkGetPhysicalDeviceProperties(gpu, gpu_props);
// Query with NULL data to get count
vkGetPhysicalDeviceQueueFamilyProperties(gpu, ip, null);
queue_props = VkQueueFamilyProperties.malloc(ip.get(0));
vkGetPhysicalDeviceQueueFamilyProperties(gpu, ip, queue_props);
if (ip.get(0) == 0) {
throw new IllegalStateException();
}
vkGetPhysicalDeviceFeatures(gpu, gpu_features);
// Graphics queue and MemMgr queue can be separate.
// TODO: Add support for separate queues, including synchronization,
// and appropriate tracking for QueueSubmit
}
}
private void demo_init() {
demo_init_connection();
demo_init_vk();
}
private void demo_create_window() {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
window = glfwCreateWindow(width, height, "The Vulkan Triangle Demo Program", NULL, NULL);
if (window == NULL) {
throw new IllegalStateException("Cannot create a window in which to draw!");
}
glfwSetWindowRefreshCallback(window, window -> demo_draw());
glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
this.width = width;
this.height = height;
if (width != 0 && height != 0) {
demo_resize();
}
});
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true);
}
});
}
private void demo_init_device() {
try (MemoryStack stack = stackPush()) {
VkDeviceQueueCreateInfo.Buffer queue = VkDeviceQueueCreateInfo.malloc(1, stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.queueFamilyIndex(graphics_queue_node_index)
.pQueuePriorities(stack.floats(0.0f));
VkPhysicalDeviceFeatures features = VkPhysicalDeviceFeatures.calloc(stack);
if (gpu_features.shaderClipDistance()) {
features.shaderClipDistance(true);
}
extension_names.flip();
VkDeviceCreateInfo device = VkDeviceCreateInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.pQueueCreateInfos(queue)
.ppEnabledLayerNames(null)
.ppEnabledExtensionNames(extension_names)
.pEnabledFeatures(features);
check(vkCreateDevice(gpu, device, null, pp));
this.device = new VkDevice(pp.get(0), gpu, device);
}
}
private void demo_init_vk_swapchain() {
// Create a WSI surface for the window:
glfwCreateWindowSurface(inst, window, null, lp);
surface = lp.get(0);
try (MemoryStack stack = stackPush()) {
// Iterate over each queue to learn whether it supports presenting:
IntBuffer supportsPresent = stack.mallocInt(queue_props.capacity());
int graphicsQueueNodeIndex;
int presentQueueNodeIndex;
for (int i = 0; i < supportsPresent.capacity(); i++) {
supportsPresent.position(i);
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, i, surface, supportsPresent);
}
// Search for a graphics and a present queue in the array of queue
// families, try to find one that supports both
graphicsQueueNodeIndex = Integer.MAX_VALUE;
presentQueueNodeIndex = Integer.MAX_VALUE;
for (int i = 0; i < supportsPresent.capacity(); i++) {
if ((queue_props.get(i).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) {
if (graphicsQueueNodeIndex == Integer.MAX_VALUE) {
graphicsQueueNodeIndex = i;
}
if (supportsPresent.get(i) == VK_TRUE) {
graphicsQueueNodeIndex = i;
presentQueueNodeIndex = i;
break;
}
}
}
if (presentQueueNodeIndex == Integer.MAX_VALUE) {
// If didn't find a queue that supports both graphics and present, then
// find a separate present queue.
for (int i = 0; i < supportsPresent.capacity(); ++i) {
if (supportsPresent.get(i) == VK_TRUE) {
presentQueueNodeIndex = i;
break;
}
}
}
// Generate error if could not find both a graphics and a present queue
if (graphicsQueueNodeIndex == Integer.MAX_VALUE || presentQueueNodeIndex == Integer.MAX_VALUE) {
throw new IllegalStateException("Could not find a graphics and a present queue");
}
// TODO: Add support for separate queues, including presentation,
// synchronization, and appropriate tracking for QueueSubmit.
// NOTE: While it is possible for an application to use a separate graphics
// and a present queues, this demo program assumes it is only using
// one:
if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
throw new IllegalStateException("Could not find a common graphics and a present queue");
}
graphics_queue_node_index = graphicsQueueNodeIndex;
demo_init_device();
vkGetDeviceQueue(device, graphics_queue_node_index, 0, pp);
queue = new VkQueue(pp.get(0), device);
// Get the list of VkFormat's that are supported:
check(vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, ip, null));
VkSurfaceFormatKHR.Buffer surfFormats = VkSurfaceFormatKHR.malloc(ip.get(0), stack);
check(vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, ip, surfFormats));
// If the format list includes just one entry of VK_FORMAT_UNDEFINED,
// the surface has no preferred format. Otherwise, at least one
// supported format will be returned.
if (ip.get(0) == 1 && surfFormats.get(0).format() == VK_FORMAT_UNDEFINED) {
format = VK_FORMAT_B8G8R8A8_UNORM;
} else {
assert ip.get(0) >= 1;
format = surfFormats.get(0).format();
}
color_space = surfFormats.get(0).colorSpace();
// Get Memory information and properties
vkGetPhysicalDeviceMemoryProperties(gpu, memory_properties);
}
}
private static class SwapchainBuffers {
long image;
VkCommandBuffer cmd;
long view;
}
private void demo_set_image_layout(long image, int aspectMask, int old_image_layout, int new_image_layout, int srcAccessMask) {
try (MemoryStack stack = stackPush()) {
if (setup_cmd == null) {
VkCommandBufferAllocateInfo cmd = VkCommandBufferAllocateInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.commandPool(cmd_pool)
.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY)
.commandBufferCount(1);
check(vkAllocateCommandBuffers(device, cmd, pp));
setup_cmd = new VkCommandBuffer(pp.get(0), device);
VkCommandBufferBeginInfo cmd_buf_info = VkCommandBufferBeginInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.pInheritanceInfo(null);
check(vkBeginCommandBuffer(setup_cmd, cmd_buf_info));
}
VkImageMemoryBarrier.Buffer image_memory_barrier = VkImageMemoryBarrier.malloc(1, stack)
.sType$Default()
.pNext(NULL)
.srcAccessMask(srcAccessMask)
.dstAccessMask(0)
.oldLayout(old_image_layout)
.newLayout(new_image_layout)
.srcQueueFamilyIndex(0)
.dstQueueFamilyIndex(0)
.image(image)
.subresourceRange(it -> it
.aspectMask(aspectMask)
.baseMipLevel(0)
.levelCount(1)
.baseArrayLayer(0)
.layerCount(1));
int src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
int dest_stages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
if (srcAccessMask == VK_ACCESS_HOST_WRITE_BIT) {
src_stages = VK_PIPELINE_STAGE_HOST_BIT;
}
switch (new_image_layout) {
case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
image_memory_barrier.dstAccessMask(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
image_memory_barrier.dstAccessMask(VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
dest_stages = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
break;
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
/* Make sure any Copy or CPU writes to image are flushed */
image_memory_barrier.dstAccessMask(VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT);
dest_stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
break;
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
image_memory_barrier.srcAccessMask(VK_ACCESS_MEMORY_READ_BIT);
/* Make sure anything that was copying from this image has completed */
image_memory_barrier.dstAccessMask(VK_ACCESS_TRANSFER_READ_BIT);
dest_stages = VK_PIPELINE_STAGE_TRANSFER_BIT;
break;
case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
image_memory_barrier.dstAccessMask(VK_ACCESS_MEMORY_READ_BIT);
break;
}
vkCmdPipelineBarrier(setup_cmd, src_stages, dest_stages, 0, null, null, image_memory_barrier);
}
}
private void demo_prepare_buffers() {
long oldSwapchain = swapchain;
try (MemoryStack stack = stackPush()) {
// Check the surface capabilities and formats
VkSurfaceCapabilitiesKHR surfCapabilities = VkSurfaceCapabilitiesKHR.malloc(stack);
check(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, surfCapabilities));
check(vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, ip, null));
IntBuffer presentModes = stack.mallocInt(ip.get(0));
check(vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, ip, presentModes));
VkExtent2D swapchainExtent = VkExtent2D.malloc(stack);
// width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// If the surface size is undefined, the size is set to the size
// of the images requested, which must fit within the minimum and
// maximum values.
swapchainExtent.width(width);
swapchainExtent.height(height);
if (swapchainExtent.width() < surfCapabilities.minImageExtent().width()) {
swapchainExtent.width(surfCapabilities.minImageExtent().width());
} else if (swapchainExtent.width() > surfCapabilities.maxImageExtent().width()) {
swapchainExtent.width(surfCapabilities.maxImageExtent().width());
}
if (swapchainExtent.height() < surfCapabilities.minImageExtent().height()) {
swapchainExtent.height(surfCapabilities.minImageExtent().height());
} else if (swapchainExtent.height() > surfCapabilities.maxImageExtent().height()) {
swapchainExtent.height(surfCapabilities.maxImageExtent().height());
}
} else {
// If the surface size is defined, the swap chain size must match
swapchainExtent.set(surfCapabilities.currentExtent());
width = surfCapabilities.currentExtent().width();
height = surfCapabilities.currentExtent().height();
}
int swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
// Determine the number of VkImage's to use in the swap chain.
// Application desires to only acquire 1 image at a time (which is
// "surfCapabilities.minImageCount").
int desiredNumOfSwapchainImages = surfCapabilities.minImageCount();
// If maxImageCount is 0, we can ask for as many images as we want;
// otherwise we're limited to maxImageCount
if ((surfCapabilities.maxImageCount() > 0) &&
(desiredNumOfSwapchainImages > surfCapabilities.maxImageCount())) {
// Application must settle for fewer images than desired:
desiredNumOfSwapchainImages = surfCapabilities.maxImageCount();
}
int preTransform;
if ((surfCapabilities.supportedTransforms() & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) != 0) {
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
} else {
preTransform = surfCapabilities.currentTransform();
}
VkSwapchainCreateInfoKHR swapchain = VkSwapchainCreateInfoKHR.calloc(stack)
.sType$Default()
.surface(surface)
.minImageCount(desiredNumOfSwapchainImages)
.imageFormat(format)
.imageColorSpace(color_space)
.imageExtent(swapchainExtent)
.imageArrayLayers(1)
.imageUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
.imageSharingMode(VK_SHARING_MODE_EXCLUSIVE)
.preTransform(preTransform)
.compositeAlpha(VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR)
.presentMode(swapchainPresentMode)
.clipped(true)
.oldSwapchain(oldSwapchain);
check(vkCreateSwapchainKHR(device, swapchain, null, lp));
this.swapchain = lp.get(0);
// If we just re-created an existing swapchain, we should destroy the old
// swapchain at this point.
// Note: destroying the swapchain also cleans up all its associated
// presentable images once the platform is done with them.
if (oldSwapchain != VK_NULL_HANDLE) {
vkDestroySwapchainKHR(device, oldSwapchain, null);
}
check(vkGetSwapchainImagesKHR(device, this.swapchain, ip, null));
swapchainImageCount = ip.get(0);
LongBuffer swapchainImages = stack.mallocLong(swapchainImageCount);
check(vkGetSwapchainImagesKHR(device, this.swapchain, ip, swapchainImages));
buffers = new SwapchainBuffers[swapchainImageCount];
for (int i = 0; i < swapchainImageCount; i++) {
buffers[i] = new SwapchainBuffers();
buffers[i].image = swapchainImages.get(i);
VkImageViewCreateInfo color_attachment_view = VkImageViewCreateInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.image(buffers[i].image)
.viewType(VK_IMAGE_VIEW_TYPE_2D)
.format(format)
.components(it -> it
.r(VK_COMPONENT_SWIZZLE_R)
.g(VK_COMPONENT_SWIZZLE_G)
.b(VK_COMPONENT_SWIZZLE_B)
.a(VK_COMPONENT_SWIZZLE_A))
.subresourceRange(it -> it
.aspectMask(VK_IMAGE_ASPECT_COLOR_BIT)
.baseMipLevel(0)
.levelCount(1)
.baseArrayLayer(0)
.layerCount(1));
check(vkCreateImageView(device, color_attachment_view, null, lp));
buffers[i].view = lp.get(0);
}
current_buffer = 0;
}
}
private static class Depth {
int format;
long image;
long mem;
long view;
}
private boolean memory_type_from_properties(int typeBits, int requirements_mask, VkMemoryAllocateInfo mem_alloc) {
// Search memtypes to find first index with those properties
for (int i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
if ((typeBits & 1) == 1) {
// Type is available, does it match user properties?
if ((memory_properties.memoryTypes().get(i).propertyFlags() & requirements_mask) == requirements_mask) {
mem_alloc.memoryTypeIndex(i);
return true;
}
}
typeBits >>= 1;
}
// No memory types matched, return failure
return false;
}
private void demo_prepare_depth() {
depth.format = VK_FORMAT_D16_UNORM;
try (MemoryStack stack = stackPush()) {
VkImageCreateInfo image = VkImageCreateInfo.calloc(stack)
.sType$Default()
.pNext(NULL)
.imageType(VK_IMAGE_TYPE_2D)
.format(depth.format)
.extent(it -> it
.width(width)
.height(height)
.depth(1))
.mipLevels(1)
.arrayLayers(1)
.samples(VK_SAMPLE_COUNT_1_BIT)
.tiling(VK_IMAGE_TILING_OPTIMAL)
.usage(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
/* create image */
check(vkCreateImage(device, image, null, lp));
depth.image = lp.get(0);
/* get memory requirements for this object */
VkMemoryRequirements mem_reqs = VkMemoryRequirements.malloc(stack);
vkGetImageMemoryRequirements(device, depth.image, mem_reqs);
/* select memory size and type */
VkMemoryAllocateInfo mem_alloc = VkMemoryAllocateInfo.malloc(stack)
.sType$Default()
.pNext(NULL)
.allocationSize(mem_reqs.size())
.memoryTypeIndex(0);
boolean pass = memory_type_from_properties(mem_reqs.memoryTypeBits(),
0, /* No requirements */
mem_alloc);
assert (pass);
/* allocate memory */
check(vkAllocateMemory(device, mem_alloc, null, lp));
depth.mem = lp.get(0);
/* bind memory */
check(vkBindImageMemory(device, depth.image, depth.mem, 0));
demo_set_image_layout(depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
0);
/* create image view */
VkImageViewCreateInfo view = VkImageViewCreateInfo.calloc(stack)
.sType$Default()
.pNext(NULL)
.flags(0)
.image(depth.image)
.viewType(VK_IMAGE_VIEW_TYPE_2D)
.format(depth.format)
.subresourceRange(it -> it
.aspectMask(VK_IMAGE_ASPECT_DEPTH_BIT)
.baseMipLevel(0)
.levelCount(1)
.baseArrayLayer(0)
.layerCount(1));
check(vkCreateImageView(device, view, null, lp));
depth.view = lp.get(0);
}
}
private static class TextureObject {
long sampler;
long image;
int imageLayout;