Remove some internal dumps.
[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 # values required
187
188 #  java-arg                     java parameter type and and name
189 #  invoke-arg           native value for invokeExact argument
190 #  native-arg           native parameter type and name
191 #  trampoline-arg       java value for upcall invocation
192 #  layout                       MemoryLayout
193 #  sig                          JNI signature string
194
195 # parameterisation values
196 #  type                         java type
197 #  carrier                      native (jdk.foriegn) type
198 #  length                       array length
199
200 type value {
201         java-arg        {{ {type} {name} }}
202         invoke-arg      {{ {name} }}
203
204         native-arg      {{ {carrier} {name}$ }}
205         trampoline-arg  {{ {name}$ }}
206 }
207
208 type value-array {
209         java-arg        {{ {type} {name} }}
210         invoke-arg      {{ {name} }}
211
212         native-arg      {{ {carrier} {name}$ }}
213         trampoline-arg  {{ {name}$ }}
214 }
215
216 type value-array2d {
217         java-arg        {{ {type} {name} }}
218         invoke-arg      {{ {name} }}
219
220         native-arg      {{ {carrier} {name}$ }}
221         trampoline-arg  {{ {name}$ }}
222 }
223
224 type uint8_t,char value {
225         carrier {{ byte }}
226         type    {{ byte }}
227         layout  {{ Memory.BYTE }}
228 }
229
230 type uint16_t value {
231         carrier {{ short }}
232         type    {{ short }}
233         layout  {{ Memory.SHORT }}
234 }
235
236 type uint32_t,int,int32_t value {
237         carrier {{ int }}
238         type    {{ int }}
239         layout  {{ Memory.INT }}
240         sig             {{ I }}
241 }
242
243 type uint64_t,int64_t,size_t value {
244         carrier {{ long }}
245         type    {{ long }}
246         layout  {{ Memory.LONG }}
247         sig             {{ J }}
248 }
249
250 type float value {
251         carrier {{ float }}
252         type    {{ float }}
253         layout  {{ Memory.FLOAT }}
254 }
255
256 type double value {
257         carrier {{ double }}
258         type    {{ double }}
259         layout  {{ Memory.DOUBLE }}
260 }
261
262 # ###################################################################### #
263
264 # type uint8_t[],char[] value-array {
265 #       type    {{ ByteArray }}
266 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.BYTE) }}
267 #       typei   {{ byte }}
268 # }
269
270 #type uint32_t[],int32_t[] value-array {
271 #       type    {{ IntArray }}
272 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.INT) }}
273 #       typei   {{ int }}
274 #}
275
276 # type uint64_t[] value-array {
277 #       type    {{ LongArray }}
278 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.LONG) }}
279 #       typei   {{ long }}
280 # }
281
282 # calls float[] =-> pointer
283 #type float[] value-array {
284 #       # or should it be float[] ?
285 #       type    {{ FloatArray }}
286 #       layout  {{ MemoryLayout.sequenceLayout({len1}, Memory.FLOAT) }}
287 #       typei   {{ float }}
288 #}
289
290 # type struct[] inline-array {
291 #       type    {{ {baseType} }}
292 #       layout  {{ MemoryLayout.sequenceLayout({len1}, {baseType}.LAYOUT) }}
293 # }
294
295 # type float[][] value-array2d {
296 #       type    {{ FloatArray }}
297 #       typei   {{ float }}
298 #       layout  {{ MemoryLayout.sequenceLayout({len1}, MemoryLayout.sequenceLayout({len2}, Memory.FLOAT)) }}
299 # }
300
301 # select=len?  or what?
302
303 type pointer value {
304         carrier {{ MemoryAddress }}
305         layout  {{ Memory.POINTER }}
306         type    {{ MemoryAddress }}
307         sig             {{ Ljdk/incubator/foreign/MemoryAddress; }}
308
309         invoke-arg      {{ Memory.address({name}) }}
310 }
311
312 type void {
313         type            {{ void }}
314         sig                     {{ V }}
315         java-arg        {{ }}
316 }
317
318 type void* pointer;
319
320 type funcpointer pointer {
321         type            {{ NativeSymbol }}
322 }
323
324 # FIXME: clenaup, value-pointer does nothing
325 type value-pointer pointer {
326 }
327
328 type uint8_t* value-pointer {
329         type    ByteArray;
330 }
331
332 type uint32_t*,int32_t*,int* value-pointer {
333         type    IntArray;
334 }
335
336 type uint64_t* value-pointer {
337         type    LongArray;
338 }
339
340 type float* value-pointer {
341         type    FloatArray;
342 }
343
344 type size_t* value-pointer {
345         type    LongArray;
346 }
347
348 type pointer-length pointer {
349 }
350
351 type void*-length pointer-length {
352         type    MemorySegment;
353 }
354
355 type uint8_t*-length pointer-length {
356         type    ByteArray;
357 }
358
359 type uint32_t*-length,int32_t*-length pointer-length {
360         type    IntArray;
361 }
362
363 type uint32_t[] pointer-length {
364         type    IntArray;
365         length  {{ {len1} }}
366 }
367
368 type uint64_t*-length pointer-length {
369         type    LongArray;
370 }
371
372 type float*-length pointer-length {
373         type    FloatArray;
374 }
375
376 type float[] pointer-length {
377         type    FloatArray;
378         length  {{ {len1} }}
379 }
380
381 # special handling for strings, will fail if it isn't
382 type char* pointer need-frame {
383         type            {{ String }}
384
385         invoke-arg      {{ (Addressable)frame$.allocateUtf8String({name}) }}
386
387         trampoline-arg  {{ {name}$.getUtf8String(0) }}
388
389         # this just verifies it's a string type
390         length eval     {{
391                 if ($v->{len} =~ m/null-terminated/) {
392                         1;
393                 } else {
394                         print "Warning: not sure if this is a string: $s->{name} $m->{name}\n";
395                 }
396         }}
397
398 }
399
400 # type XXchar**-length pointer-length accessor=value-alloc {
401 #       type    {{ String[] }}
402
403 #       java-set        {{ {name}$VH.set(segment, Memory.copyStringArray({name}, alloc$)); {set-length} }}
404 #       java-get        {{ Memory.copyStringArray((MemoryAddress){name}$VH.get(segment), {length}) }}
405
406 #       set-length eval {{
407 #               if ($v->{len} =~ m/(.*),null-terminated/) {
408 #                       'set'.ucfirst($1).'({name}.length)';
409 #               } else {
410 #                       die Dumper($v, $s);
411 #               }
412 #       }}
413 # }
414
415 # FIXME: wrong
416 type Xuint32_t** pointer {
417         type    {{ HandleArray<IntArray> }}
418         typei   {{ IntArray }}
419 }
420
421 type uint32_t**-length pointer {
422         type    {{ HandleArray<IntArray> }}
423         typei   {{ IntArray }}
424 }
425
426 type void** pointer {
427         type    PointerArray;
428 }
429
430 type void**-length pointer-length {
431         type    PointerArray;
432 }
433
434 type handle pointer {
435         type            {{ {baseType} }}
436 }
437
438 type handle* pointer {
439         type            {{ HandleArray<{typei}> }}
440         typei           {{ {baseType} }}
441
442         invoke-arg      {{ Memory.address({name}) }}
443 }
444
445 type handle*-length pointer-length {
446         type            {{ HandleArray<{baseType}> }}
447         typei           {{ {baseType} }}
448 }
449
450 #type struct inline {
451 #       type    {{ {baseType} }}
452 #       layout  {{ {baseType}.LAYOUT }}
453 #}
454
455 type struct* pointer trampoline-scope {
456         type            {{ {baseType} }}
457
458         trampoline-arg  {{ {baseType}.create({name}$, scope$$) }}
459 }
460
461 type struct*-length pointer-length {
462         type    {{ {baseType} }}
463         typei   {{ {baseType} }}
464 }
465
466 type struct**-length pointer-length {
467         type    {{ HandleArray<{baseType}> }}
468         typei   {{ {baseType} }}
469 }
470
471 type struct** pointer {
472 }
473
474 # xlib
475 type XID,Window,VisualID uint64_t;
476
477 type Display* handle {
478         type    xlib.XDisplay;
479 }
480
481 # xcb
482 type xcb_window_t uint32_t;
483 type xcb_visualid_t uint32_t;
484 type xcb_connection_t* handle {
485         type    xcb.Connection;
486 }
487
488 # special types for call overrides
489 type instance handle is-instance {
490         java-arg        {{ }}
491         invoke-arg      {{ (Addressable)self }}
492         native-arg      {{ }}
493 }
494
495 # A pointer type which is always ignored/supressed, e.g. VkAllocationCallbacks *
496 type struct*-ignore,void*-ignore void* {
497         java-arg        {{ }}
498         invoke-arg      {{ (Addressable)MemoryAddress.NULL }}
499         trampoline-arg  {{ }}
500 }
501
502 type void*-return void* {
503         native-result-define    {{ MemoryAddress result$; }}
504         native-result-assign    {{ result$ = (MemoryAddress) }}
505         java-result-return              {{ return result$; }}
506 }
507
508 type funcpointer-return funcpointer need-scope {
509         native-result-define    {{ MemoryAddress result$; }}
510         native-result-assign    {{ result$ = (MemoryAddress) }}
511         java-result-return              {{ return NativeSymbol.ofAddress(pName, result$, scope$); }}
512 }
513
514 type uint32_t-return uint32_t {
515         native-result-define    {{ int result$; }}
516         native-result-assign    {{ result$ = (int) }}
517         java-result-return              {{ return result$; /* uint32_t */ }}
518
519         trampoline-result-define        {{ int result$$; }}
520         trampoline-result-assign        {{ result$$ = (int) }}
521         trampoline-result-return        {{ return result$$; /* uint32_t */ }}
522 }
523
524 type uint64_t-return,size_t-return uint64_t {
525         native-result-define    {{ long result$; }}
526         native-result-assign    {{ result$ = (long) }}
527         java-result-return              {{ return result$; /* uint64_t */ }}
528 }
529
530 # for handle constructors
531 type handle*-output handle* need-frame need-scope {
532         java-arg        {{ }}
533         invoke-arg      {{ (Addressable){name} }}
534
535         java-result                     {{ {baseType} }}
536         native-init                     {{ MemorySegment {name} = frame$.allocate(Memory.POINTER); }}
537         java-result-assign      {{ {baseType} result$$ = {baseType}.create({name}.get(Memory.POINTER, 0), scope$); }}
538         java-result-return      {{ return result$$; }}
539 }
540
541 type uint32_t*-output,int*-output uint32_t* need-frame {
542         java-arg        {{ }}
543         invoke-arg      {{ (Addressable){name}.address() }}
544
545         java-result                     {{ int }}
546         native-init                     {{ MemorySegment {name} = frame$.allocate(Memory.INT); }}
547         java-result-assign      {{ int result$$ = {name}.getAtIndex(Memory.INT, 0); }}
548         java-result-return      {{ return result$$; }}
549 }
550
551 type uint64_t*-output uint64_t* need-frame {
552         java-arg        {{ }}
553         invoke-arg      {{ (Addressable){name}.address() }}
554
555         java-result                     {{ long }}
556         native-init                     {{ MemorySegment {name} = frame$.allocate(Memory.LONG); }}
557         java-result-assign      {{ long result$$ = {name}.getAtIndex(Memory.LONG, 0); }}
558         java-result-return      {{ return result$$; }}
559 }
560
561 type void*-output handle*-output need-frame {
562         java-arg        {{ }}
563         invoke-arg      {{ (Addressable){name}.address() }}
564
565         java-result                     {{ MemoryAddress }}
566         native-init                     {{ MemorySegment {name} = frame$.allocate(Memory.POINTER); }}
567         java-result-assign      {{ MemoryAddress result$$ = {name}.getAtIndex(Memory.POINTER, 0); }}
568         java-result-return      {{ return result$$; }}
569 }
570
571 type VkBool32*-output uint32_t*-output {
572         java-result                     {{ boolean }}
573         java-result-return      {{ return result$$ != 0; }}
574 }
575
576 # for handle constructors of dispatchable types
577 type dispatch*-output handle*-output {
578         java-result-assign      {{ {baseType} result$$ = {baseType}.create({name}.get(Memory.POINTER, 0), dispatch, scope$); }}
579 }
580
581 # for query and return types
582 type handle*-length-query handle*-length need-alloc {
583         java-arg        {{ }}
584         invoke-arg      {{ (Addressable){name}.address() }}
585         query-arg       {{ (Addressable)MemoryAddress.NULL }}
586
587         java-result                     {{ {type} }}
588         query-init                      {{ {type} {name} = {typei}.createArray({length}, alloc$); }}
589         java-result-return      {{ return {name}; }}
590 }
591
592 type dispatch*-length-query handle*-length-query need-scope {
593         query-init                      {{ {type} {name} = {typei}.createArray({length}, alloc$, dispatch, scope$); }}
594 }
595
596 type struct*-length-query struct*-length need-alloc {
597         java-arg        {{ }}
598         invoke-arg      {{ (Addressable){name}.address() }}
599         query-arg       {{ (Addressable)MemoryAddress.NULL }}
600
601         java-result                     {{ {type} }}
602         query-init                      {{ {type} {name} = {typei}.createArray({length}, alloc$); }}
603         java-result-return      {{ return {name}; }}
604 }
605
606 type void*-length-query void*-length need-alloc {
607         java-arg        {{ }}
608         invoke-arg      {{ (Addressable){name} }}
609         query-arg       {{ (Addressable)MemoryAddress.NULL }}
610
611         java-result                     {{ {type} }}
612         query-init                      {{ {type} {name} = alloc$.allocate({length}); }}
613         java-result-return      {{ return {name}; }}
614 }
615
616 type uint32_t*-length-query uint32_t*-length need-alloc {
617         java-arg        {{ }}
618         invoke-arg      {{ (Addressable){name}.address() }}
619         query-arg       {{ (Addressable)MemoryAddress.NULL }}
620
621         java-result                     {{ {type} }}
622         query-init                      {{ {type} {name} = IntArray.createArray({length}, alloc$); }}
623         java-result-return      {{ return {name}; }}
624 }
625
626 type uint32_t*-querylen uint32_t* need-frame {
627         type            {{ MemorySegment }}
628         java-arg        {{ }}
629         invoke-arg      {{ (Addressable){name}$ }}
630
631         native-init     {{ {type} {name}$ = alloc$.allocate(Memory.INT, 0); }}
632         query-init      {{ long {name} = {name}$.get(Memory.INT, 0); }}
633 }
634
635 type size_t*-querylen uint32_t* need-frame {
636         type            {{ MemorySegment }}
637         java-arg        {{ }}
638         invoke-arg      {{ (Addressable){name}$ }}
639
640         native-init     {{ {type} {name}$ = alloc$.allocate(Memory.LONG, 0); }}
641         query-init      {{ long {name} = {name}$.get(Memory.LONG, 0); }}
642 }
643
644 # Or should it just use an insert function?
645
646 # there's only two like this
647 type handle*-alloc handle* {
648         java-arg        {{ }}
649         java-result                     {{ {type} }}
650         java-result-return      {{ return {name}; }}
651 }
652
653 # A couple of very special cases
654 type VkCommandBuffer-alloc handle*-alloc need-scope need-alloc {
655         native-init     {{
656                 {type} {name} = {typei}.createArray(
657                         (int)VkCommandBufferAllocateInfo.commandBufferCount$VH.get(pAllocateInfo.segment),
658                         alloc$,
659                         dispatch,
660                         scope$);
661         }}
662 }
663
664 type VkDescriptorSet-alloc handle*-alloc need-alloc {
665         native-init     {{
666                 {type} {name} = {typei}.createArray(
667                         (int)VkDescriptorSetAllocateInfo.descriptorSetCount$VH.get(pAllocateInfo.segment),
668                         alloc$);
669         }}
670 }
671
672 type void**-output void** need-frame need-scope {
673         type            {{ MemorySegment }}
674
675         java-arg        {{ }}
676         invoke-arg      {{ (Addressable){name} }}
677
678         native-init     {{ MemorySegment {name} = frame$.allocate(Memory.POINTER); }}
679
680         java-result                     {{ {type} }}
681         java-result-assign      {{ {type} result$$ = MemorySegment.ofAddress({name}.get(Memory.POINTER, 0), {length}, scope$); }}
682         java-result-return      {{ return result$$; }}
683 }
684
685 type vkMapMemory-output void**-output {
686         length  {{ size }}
687 }
688
689 # some tweaks that the auto-discovery code misses/isn't worth adding to
690 override commands {
691         vkAllocateCommandBuffers device=type:instance pCommandBuffers=type:VkCommandBuffer-alloc;
692         vkAllocateDescriptorSets device=type:instance pDescriptorSets=type:VkDescriptorSet-alloc;
693
694         vkMapMemory device=type:instance ppData=type:vkMapMemory-output;
695
696         # Don't need userData
697         PFN_vkDebugUtilsMessengerCallbackEXT pUserData=type:void*-ignore;
698         PFN_vkDebugReportCallbackEXT pUserData=type:void*-ignore;
699         PFN_vkDeviceMemoryReportCallbackEXT pUserData=type:void*-ignore;
700
701         # Mapped to a NativeSymbol via 'funcpointer' type
702         PFN_vkVoidFunction ignore;
703
704         # Don't need VkAllocationCallbacks, don't need these
705         PFN_vkAllocationFunction ignore;
706         PFN_vkReallocationFunction ignore;
707         PFN_vkFreeFunction ignore;
708         PFN_vkInternalAllocationNotification ignore;
709         PFN_vkInternalFreeNotification ignore;
710 }