Added graphics vulkan demo.
[panamaz] / src / notzed.vulkan / gen / command-types.api
1 # -*- Mode:text; tab-width:4; electric-indent-mode: nil; indent-line-function:insert-tab; -*-
2
3 # things to check; vkCmdSetFragmentShadingRateKHR
4
5 # types for function calls
6
7 code method {
8   invoke {{
9         /* method:invoke */
10         static final MethodHandle {name}$FH = Memory.downcall("{name}",
11                 {function-descriptor});
12         /**
13         * success: {successcodes}
14         * errors: {errorcodes}
15         */
16         public {static}{java-result} {rename}(
17                 {java-arg}) {
18                 {native-result-define}
19                 try {create-frame}{
20                         {native-init}
21                         {native-result-assign}{name}$FH.invokeExact({invoke-arg});
22                         {result-test}{
23                                 {java-result-assign}
24                                 {java-result-return}
25                         }
26                 } catch (Throwable t) {
27                         throw new RuntimeException(t);
28                 }
29                 {result-throw}
30         }
31   }}
32 }
33
34 code method-query {
35   invoke {{
36         /* method-query:invoke */
37         static final MethodHandle {name}$FH = Memory.downcall("{name}",
38                 {function-descriptor});
39         /**
40         * success: {successcodes}
41         * errors: {errorcodes}
42         */
43         public {static}{java-result} {rename}(
44                 {java-arg}) {
45                 {native-result-define}
46                 try {create-frame}{
47                         {native-init}
48                         {native-result-assign}{name}$FH.invokeExact({query-arg});
49                         {result-test}{
50                                 {query-init}
51                                 {native-result-assign}{name}$FH.invokeExact({invoke-arg});
52                                 {java-result-assign}
53                                 {java-result-return}
54                         }
55                 } catch (Throwable t) {
56                         throw new RuntimeException(t);
57                 }
58                 {result-throw}
59         }
60   }}
61 }
62
63 code method-extension {
64   dispatch {{
65         final NativeSymbol {name}$NS;
66   }}
67   invoke {{
68         /* method-extension:invoke */
69         final static MethodHandle {name}$DH = Memory.downcall(
70                 {function-descriptor});
71         /**
72         * success: {successcodes}
73         * errors: {errorcodes}
74         */
75         public {static}{java-result} {rename}(
76                 {java-arg}) {
77                 {native-result-define}
78                 try {create-frame}{
79                         {native-init}
80                         {native-result-assign}{name}$DH.invokeExact(dispatch.{name}$NS, {invoke-arg});
81                         {result-test}{
82                                 {java-result-assign}
83                                 {java-result-return}
84                         }
85                 } catch (Throwable t) {
86                         throw new RuntimeException(t);
87                 }
88                 {result-throw}
89         }
90   }}
91 }
92
93 code method-extension-query {
94   dispatch {{
95         final NativeSymbol {name}$NS;
96   }}
97   invoke {{
98         /* method-extension:invoke */
99         final static MethodHandle {name}$DH = Memory.downcall(
100                 {function-descriptor});
101         /**
102         * success: {successcodes}
103         * errors: {errorcodes}
104         */
105         public {static}{java-result} {rename}(
106                 {java-arg}) {
107                 {native-result-define}
108                 try {create-frame}{
109                         {native-init}
110                         {native-result-assign}{name}$DH.invokeExact(dispatch.{name}$NS, {query-arg});
111                         {result-test}{
112                                 {query-init}
113                                 {native-result-assign}{name}$DH.invokeExact(dispatch.{name}$NS, {invoke-arg});
114                                 {java-result-assign}
115                                 {java-result-return}
116                         }
117                 } catch (Throwable t) {
118                         throw new RuntimeException(t);
119                 }
120                 {result-throw}
121         }
122   }}
123 }
124
125 # ###################################################################### #
126
127 # common parts for funcpointer
128 code funcpointer {
129   common {{
130         public {java-result} call({java-arg});
131
132         public final static FunctionDescriptor DESCRIPTOR =
133                 {function-descriptor};
134   }}
135   upcall {{
136         public static FunctionPointer<{rename}> upcall({rename} target$, ResourceScope scope$) {
137                 interface Trampoline {
138                         {native-result} call({native-arg});
139                 }
140                 Trampoline trampoline = ({native-arg}) -> {
141                         // frame?  scope?
142                         try {trampoline-scope} {
143                                 {trampoline-result-define}
144                                 {trampoline-result-assign}target$.call({trampoline-arg});
145                                 {trampoline-result-return}
146                         } catch (Throwable t) {
147                                 throw new RuntimeException(t);
148                         }
149                 };
150                 return new FunctionPointer<>(
151                         Memory.upcall(
152                                 MethodHandles.lookup(),
153                                 trampoline,
154                                 "call",
155                                 "{trampoline-signature}",
156                                 DESCRIPTOR,
157                                 scope$),
158                         target$);
159         }
160   }}
161   # vulkan has none
162   downcall {{ }}
163 }
164
165 code funcpointer-readwrite insert=funcpointer:common,funcpointer:upcall,funcpointer:downcall {
166   class {{
167         package {package};
168
169         import jdk.incubator.foreign.*;
170         import java.lang.invoke.*;
171         import au.notzed.nativez.*;
172
173         @FunctionalInterface
174         public interface {name} {
175
176                 {common}
177
178                 {upcall}
179                 {downcall}
180         }
181   }}
182 }
183
184 # ###################################################################### #
185
186 type value {
187         java-arg        {{ {type} {name} }}
188         invoke-arg      {{ {name} }}
189
190         native-arg      {{ {carrier} {name}$ }}
191         trampoline-arg  {{ {name}$ }}
192 }
193
194 type value-array {
195         java-arg        {{ {type} {name} }}
196         invoke-arg      {{ {name} }}
197
198         native-arg      {{ {carrier} {name}$ }}
199         trampoline-arg  {{ {name}$ }}
200 }
201
202 type value-array2d {
203         java-arg        {{ {type} {name} }}
204         invoke-arg      {{ {name} }}
205
206         native-arg      {{ {carrier} {name}$ }}
207         trampoline-arg  {{ {name}$ }}
208 }
209
210 type uint8_t,char value {
211         carrier {{ byte }}
212         type    {{ byte }}
213         layout  {{ Memory.BYTE }}
214 }
215
216 type uint16_t value {
217         carrier {{ short }}
218         type    {{ short }}
219         layout  {{ Memory.SHORT }}
220 }
221
222 type uint32_t,int,int32_t value {
223         carrier {{ int }}
224         type    {{ int }}
225         layout  {{ Memory.INT }}
226         sig             {{ I }}
227 }
228
229 type uint64_t,int64_t,size_t value {
230         carrier {{ long }}
231         type    {{ long }}
232         layout  {{ Memory.LONG }}
233         sig             {{ J }}
234 }
235
236 type float value {
237         carrier {{ float }}
238         type    {{ float }}
239         layout  {{ Memory.FLOAT }}
240 }
241
242 type double value {
243         carrier {{ double }}
244         type    {{ double }}
245         layout  {{ Memory.DOUBLE }}
246 }
247
248 # ###################################################################### #
249 # implied length types
250 type uint64_t-length,size_t-length uint64_t {
251         java-arg        {{ }}
252         invoke-arg      {{ Memory.length({lengthfor}) }}
253 }
254
255 type uint32_t-length uint32_t {
256         java-arg        {{ }}
257         invoke-arg      {{ (int)Memory.length({lengthfor}) }}
258 }
259
260 # ###################################################################### #
261
262 # type uint8_t[],char[] value-array {
263 #       type    {{ ByteArray }}
264 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.BYTE) }}
265 #       typei   {{ byte }}
266 # }
267
268 #type uint32_t[],int32_t[] value-array {
269 #       type    {{ IntArray }}
270 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.INT) }}
271 #       typei   {{ int }}
272 #}
273
274 # type uint64_t[] value-array {
275 #       type    {{ LongArray }}
276 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.LONG) }}
277 #       typei   {{ long }}
278 # }
279
280 # calls float[] =-> pointer
281 #type float[] value-array {
282 #       # or should it be float[] ?
283 #       type    {{ FloatArray }}
284 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.FLOAT) }}
285 #       typei   {{ float }}
286 #}
287
288 # type struct[] inline-array {
289 #       type    {{ {baseType} }}
290 #       layout  {{ MemoryLayout.sequenceLayout({len1}, {baseType}.LAYOUT) }}
291 # }
292
293 # type float[][] value-array2d {
294 #       type    {{ FloatArray }}
295 #       typei   {{ float }}
296 #       layout  {{ MemoryLayout.sequenceLayout({len1}, MemoryLayout.sequenceLayout({len2}, Memory.FLOAT)) }}
297 # }
298
299 # select=len?  or what?
300
301 type pointer value {
302         carrier {{ MemoryAddress }}
303         layout  {{ Memory.POINTER }}
304         type    {{ MemoryAddress }}
305         sig             {{ Ljdk/incubator/foreign/MemoryAddress; }}
306
307         invoke-arg      {{ Memory.address({name}) }}
308 }
309
310 type void {
311         type            {{ void }}
312         sig                     {{ V }}
313         java-arg        {{ }}
314 }
315
316 type void* pointer;
317
318 type funcpointer pointer {
319         type            {{ NativeSymbol }}
320 }
321
322 # FIXME: clenaup, value-pointer does nothing
323 type value-pointer pointer {
324 }
325
326 type uint8_t* value-pointer {
327         type    ByteArray;
328 }
329
330 type uint32_t*,int32_t*,int* value-pointer {
331         type    IntArray;
332 }
333
334 type uint64_t* value-pointer {
335         type    LongArray;
336 }
337
338 type float* value-pointer {
339         type    FloatArray;
340 }
341
342 type size_t* value-pointer {
343         type    LongArray;
344 }
345
346 type pointer-length pointer {
347 }
348
349 type void*-length pointer-length {
350         type            MemorySegment;
351         java-get        {{ MemorySegment.ofAddress({native-get}, {length}, this.segment.scope()) }}
352 }
353
354 type uint8_t*-length pointer-length {
355         type    ByteArray;
356 }
357
358 type uint32_t*-length,int32_t*-length pointer-length {
359         type    IntArray;
360 }
361
362 type uint32_t[] pointer-length {
363         type    IntArray;
364         length  {{ {len1} }}
365 }
366
367 type uint64_t*-length pointer-length {
368         type    LongArray;
369 }
370
371 type float*-length pointer-length {
372         type    FloatArray;
373 }
374
375 type float[] pointer-length {
376         type    FloatArray;
377         length  {{ {len1} }}
378 }
379
380 # special handling for strings, will fail if it isn't
381 type char* pointer need-frame {
382         type            {{ String }}
383
384         invoke-arg      {{ (Addressable)frame$.allocateUtf8String({name}) }}
385
386         trampoline-arg  {{ {name}$.getUtf8String(0) }}
387
388         # this just verifies it's a string type
389         length eval     {{
390                 if ($v->{len} =~ m/null-terminated/) {
391                         1;
392                 } else {
393                         print "Warning: not sure if this is a string: $s->{name} $m->{name}\n";
394                 }
395         }}
396
397 }
398
399 # type XXchar**-length pointer-length accessor=value-alloc {
400 #       type    {{ String[] }}
401
402 #       java-set        {{ {name}$VH.set(segment, Memory.copyStringArray({name}, alloc$)); {set-length} }}
403 #       java-get        {{ Memory.copyStringArray((MemoryAddress){name}$VH.get(segment), {length}) }}
404
405 #       set-length eval {{
406 #               if ($v->{len} =~ m/(.*),null-terminated/) {
407 #                       'set'.ucfirst($1).'({name}.length)';
408 #               } else {
409 #                       die Dumper($v, $s);
410 #               }
411 #       }}
412 # }
413
414 # FIXME: wrong
415 type Xuint32_t** pointer {
416         type    {{ HandleArray<IntArray> }}
417         typei   {{ IntArray }}
418 }
419
420 type uint32_t**-length pointer {
421         type    {{ HandleArray<IntArray> }}
422         typei   {{ IntArray }}
423 }
424
425 type void** pointer {
426         type    PointerArray;
427 }
428
429 type void**-length pointer-length {
430         type    PointerArray;
431 }
432
433 type handle pointer {
434         type            {{ {baseType} }}
435 }
436
437 type handle* pointer {
438         type            {{ HandleArray<{typei}> }}
439         typei           {{ {baseType} }}
440
441         invoke-arg      {{ Memory.address({name}) }}
442 }
443
444 type handle*-length pointer-length {
445         type            {{ HandleArray<{baseType}> }}
446         typei           {{ {baseType} }}
447 }
448
449 #type struct inline {
450 #       type    {{ {baseType} }}
451 #       layout  {{ {baseType}.LAYOUT }}
452 #}
453
454 type struct* pointer trampoline-scope {
455         type            {{ {baseType} }}
456
457         trampoline-arg  {{ {baseType}.create({name}$, scope$$) }}
458 }
459
460 type struct*-length pointer-length {
461         type    {{ {baseType} }}
462         typei   {{ {baseType} }}
463 }
464
465 type struct**-length pointer-length {
466         type    {{ HandleArray<{baseType}> }}
467         typei   {{ {baseType} }}
468 }
469
470 type struct** pointer {
471 }
472
473 # xlib
474 type XID,Window,VisualID uint64_t;
475
476 type Display* handle {
477         type    xlib.XDisplay;
478 }
479
480 # xcb
481 type xcb_window_t uint32_t;
482 type xcb_visualid_t uint32_t;
483 type xcb_connection_t* handle {
484         type    xcb.Connection;
485 }
486
487 # special types for call overrides
488 type instance handle is-instance {
489         java-arg        {{ }}
490         invoke-arg      {{ (Addressable)self }}
491         native-arg      {{ }}
492 }
493
494 # A pointer type which is always ignored/supressed, e.g. VkAllocationCallbacks *
495 type struct*-ignore,void*-ignore void* {
496         java-arg        {{ }}
497         invoke-arg      {{ (Addressable)MemoryAddress.NULL }}
498         trampoline-arg  {{ }}
499 }
500
501 type void*-return void* {
502         native-result-define    {{ MemoryAddress result$; }}
503         native-result-assign    {{ result$ = (MemoryAddress) }}
504         java-result-return              {{ return result$; }}
505 }
506
507 type funcpointer-return funcpointer need-scope {
508         native-result-define    {{ MemoryAddress result$; }}
509         native-result-assign    {{ result$ = (MemoryAddress) }}
510         java-result-return              {{ return NativeSymbol.ofAddress(pName, result$, scope$); }}
511 }
512
513 type uint32_t-return uint32_t {
514         native-result-define    {{ int result$; }}
515         native-result-assign    {{ result$ = (int) }}
516         java-result-return              {{ return result$; /* uint32_t */ }}
517
518         trampoline-result-define        {{ int result$$; }}
519         trampoline-result-assign        {{ result$$ = (int) }}
520         trampoline-result-return        {{ return result$$; /* uint32_t */ }}
521 }
522
523 type uint64_t-return,size_t-return uint64_t {
524         native-result-define    {{ long result$; }}
525         native-result-assign    {{ result$ = (long) }}
526         java-result-return              {{ return result$; /* uint64_t */ }}
527 }
528
529 # for handle constructors
530 type handle*-output handle* need-frame need-scope {
531         java-arg        {{ }}
532         invoke-arg      {{ (Addressable){name} }}
533
534         java-result                     {{ {baseType} }}
535         native-init                     {{ MemorySegment {name} = frame$.allocate(Memory.POINTER); }}
536         java-result-assign      {{ {baseType} result$$ = {baseType}.create({name}.get(Memory.POINTER, 0), scope$); }}
537         java-result-return      {{ return result$$; }}
538 }
539
540 # for handle constructors of dispatchable types
541 type dispatch*-output handle*-output {
542         java-result-assign      {{ {baseType} result$$ = {baseType}.create({name}.get(Memory.POINTER, 0), dispatch, scope$); }}
543 }
544
545 # for query and return types
546 type handle*-length-query handle*-length need-alloc {
547         java-arg        {{ }}
548         invoke-arg      {{ (Addressable){name}.address() }}
549         query-arg       {{ (Addressable)MemoryAddress.NULL }}
550
551         java-result                     {{ {type} }}
552         query-init                      {{ {type} {name} = {typei}.createArray({length}, alloc$); }}
553         java-result-return      {{ return {name}; }}
554 }
555
556 type dispatch*-length-query handle*-length-query need-scope {
557         query-init                      {{ {type} {name} = {typei}.createArray({length}, alloc$, dispatch, scope$); }}
558 }
559
560 type struct*-length-query struct*-length need-alloc {
561         java-arg        {{ }}
562         invoke-arg      {{ (Addressable){name}.address() }}
563         query-arg       {{ (Addressable)MemoryAddress.NULL }}
564
565         java-result                     {{ {type} }}
566         query-init                      {{ {type} {name} = {typei}.createArray({length}, alloc$); }}
567         java-result-return      {{ return {name}; }}
568 }
569
570 type void*-length-query void*-length need-alloc {
571         java-arg        {{ }}
572         invoke-arg      {{ (Addressable){name} }}
573         query-arg       {{ (Addressable)MemoryAddress.NULL }}
574
575         java-result                     {{ {type} }}
576         query-init                      {{ {type} {name} = alloc$.allocate({length}); }}
577         java-result-return      {{ return {name}; }}
578 }
579
580 type uint32_t*-length-query uint32_t*-length need-alloc {
581         java-arg        {{ }}
582         invoke-arg      {{ (Addressable){name}.address() }}
583         query-arg       {{ (Addressable)MemoryAddress.NULL }}
584
585         java-result                     {{ {type} }}
586         query-init                      {{ {type} {name} = IntArray.createArray({length}, alloc$); }}
587         java-result-return      {{ return {name}; }}
588 }
589
590 type uint32_t*-querylen uint32_t* need-frame {
591         type            {{ MemorySegment }}
592         java-arg        {{ }}
593         invoke-arg      {{ (Addressable){name}$ }}
594
595         native-init     {{ {type} {name}$ = alloc$.allocate(Memory.INT, 0); }}
596         query-init      {{ long {name} = {name}$.get(Memory.INT, 0); }}
597 }
598
599 type size_t*-querylen uint32_t* need-frame {
600         type            {{ MemorySegment }}
601         java-arg        {{ }}
602         invoke-arg      {{ (Addressable){name}$ }}
603
604         native-init     {{ {type} {name}$ = alloc$.allocate(Memory.LONG, 0); }}
605         query-init      {{ long {name} = {name}$.get(Memory.LONG, 0); }}
606 }
607
608 # Or should it just use an insert function?
609
610 # there's only two like this
611 type handle*-alloc handle* {
612         java-arg        {{ }}
613         java-result                     {{ {type} }}
614         java-result-return      {{ return {name}; }}
615 }
616
617 # A couple of very special cases
618 type VkCommandBuffer-alloc handle*-alloc need-scope need-alloc {
619         native-init     {{
620                 {type} {name} = {typei}.createArray(
621                         (int)VkCommandBufferAllocateInfo.commandBufferCount$VH.get(pAllocateInfo.segment),
622                         alloc$,
623                         dispatch,
624                         scope$);
625         }}
626 }
627
628 type VkDescriptorSet-alloc handle*-alloc need-alloc {
629         native-init     {{
630                 {type} {name} = {typei}.createArray(
631                         (int)VkDescriptorSetAllocateInfo.descriptorSetCount$VH.get(pAllocateInfo.segment),
632                         alloc$);
633         }}
634 }
635
636 type void**-output void** need-frame need-scope {
637         type            {{ MemorySegment }}
638
639         java-arg        {{ }}
640         invoke-arg      {{ (Addressable){name} }}
641
642         native-init     {{ MemorySegment {name} = frame$.allocate(Memory.POINTER); }}
643
644         java-result                     {{ {type} }}
645         java-result-assign      {{ {type} result$$ = MemorySegment.ofAddress({name}.get(Memory.POINTER, 0), {length}, scope$); }}
646         java-result-return      {{ return result$$; }}
647 }
648
649 type vkMapMemory-output void**-output {
650         length  {{ size }}
651 }
652
653 # some tweaks that the auto-discovery code misses/isn't worth adding to
654 override commands {
655         vkGetPhysicalDeviceQueueFamilyProperties template=method-query
656                 physicalDevice=type:instance
657                 pQueueFamilyPropertyCount=type:uint32_t*-querylen
658                 pQueueFamilyProperties=type:struct*-length-query;
659
660         vkAllocateCommandBuffers device=type:instance pCommandBuffers=type:VkCommandBuffer-alloc;
661         vkAllocateDescriptorSets device=type:instance pDescriptorSets=type:VkDescriptorSet-alloc;
662
663         vkMapMemory device=type:instance ppData=type:vkMapMemory-output;
664
665         # forces ignoring of lengths, maybe need to do it for all vkCmd
666         #vkCmdSetScissor commandBuffer=type:instance;
667         #vkCmdSetViewport commandBuffer=type:instance;
668         #vkCmdBindDescriptorSets commandBuffer=type:instance;
669
670         # Don't need userData
671         PFN_vkDebugUtilsMessengerCallbackEXT pUserData=type:void*-ignore;
672         PFN_vkDebugReportCallbackEXT pUserData=type:void*-ignore;
673         PFN_vkDeviceMemoryReportCallbackEXT pUserData=type:void*-ignore;
674
675         # Mapped to a NativeSymbol via 'funcpointer' type
676         PFN_vkVoidFunction ignore;
677
678         # Don't need VkAllocationCallbacks, don't need these
679         PFN_vkAllocationFunction ignore;
680         PFN_vkReallocationFunction ignore;
681         PFN_vkFreeFunction ignore;
682         PFN_vkInternalAllocationNotification ignore;
683         PFN_vkInternalFreeNotification ignore;
684 }