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