1 # -*- Mode:text; tab-width:4; electric-indent-mode: nil; indent-line-function:insert-tab; -*-
3 # types for structs and unions
5 # ###################################################################### #
7 #accessor=code template
19 public {type} get{Name}() {
25 public void set{Name}({type} {name}) {
30 public {type} get{Name}AtIndex(long i$) {
35 public void set{Name}AtIndex(long i$, {type} {name}) {
40 # Initialise the sType field if it has one, also include sub-
42 if ($tempname =~ m/write/) {
43 my $init = "{name}\$VH.set(self\$.segment, Vulkan.{values});\n";
44 foreach my $x (@{$s->{items}}) {
45 if ($x->{deref} eq 'struct') {
46 my $y = $api->{types}->{$x->{baseType}};
47 if ($#{$y->{items}} >= 0 && $y->{items}->[0]->{values}) {
48 my $z = $y->{items}->[0];
49 $init .= "self\$.get$x->{Name}().set$z->{Name}(Vulkan.$z->{values});\n";
60 if ($tempname =~ m/write.*array/n) {
61 my $init = "{name}\$AH.set(self\$.segment, i, Vulkan.{values});\n";
62 foreach my $x (@{$s->{items}}) {
63 if ($x->{deref} eq 'struct') {
64 my $y = $api->{types}->{$x->{baseType}};
65 if ($#{$y->{items}} >= 0 && $y->{items}->[0]->{values}) {
66 my $z = $y->{items}->[0];
67 $init .= "self\$.get$x->{Name}AtIndex(i).set$z->{Name}(Vulkan.$z->{values});\n";
72 "for (long i=0; i<length; i++) {\n\t$init\n\t}\n";
78 # for complex constructors
79 #setall-arg {{ {type} {name} }}
80 setall-arg {{ {type} {name} }}
81 setall {{ self$.set{Name}({name}); }}
86 /* value-array {deref} */
87 public {type} get{Name}() {
90 } catch (Throwable t) {
91 throw new RuntimeException(t);
94 public {typei} get{Name}Element(int i$) {
97 public void set{Name}Element(int i$, {typei} {name}) {
103 if ($m->{len1} <= 4) {
104 join ", ", map { '{typei} {name}$'.$_ } (0 .. $m->{len1}-1);
106 "{typei}[] {name} /* $m->{deref} $m->{len1} */";
110 if ($m->{len1} <= 4) {
111 join "\n\t", map { "self\$.set{Name}Element($_, {name}\$$_);" } (0 .. $m->{len1}-1);
114 if ({name} != null) {
116 ((MemorySegment){name}\$SH.invokeExact(self\$.segment)).copyFrom(MemorySegment.ofArray({name}));
117 } catch (Throwable t) {
118 System.err.println("Copy error: " + t.getLocalizedMessage());
126 # don't like having to pass scope here, not sure what else to do though
129 /* value-array {deref} */
130 public {type} get{Name}(VkInstance instance$, ResourceScope scope$) {
133 } catch (Throwable t) {
134 throw new RuntimeException(t);
137 public {typei} get{Name}Element(int i$, VkInstance instance$, ResourceScope scope$) {
140 public void set{Name}Element(int i$, {typei} {name}) {
148 /* value-array2d {deref} */
149 public {type} get{Name}() {
152 } catch (Throwable t) {
153 throw new RuntimeException(t);
156 public {typei} get{Name}Element(int i$, int j$) {
159 public void set{Name}Element(int i$, int j$, {typei} {name}) {
168 public {type} get{Name}() {
171 } catch (Throwable t) {
172 throw new RuntimeException(t);
178 public {type} get{Name}AtIndex(long i$) {
181 } catch (Throwable t) {
182 throw new RuntimeException(t);
188 # value with a SegmentAllocator passed to set()
189 code value-alloc value {
192 public void set{Name}({type} {name}, SegmentAllocator alloc$) {
198 public void set{Name}AtIndex(long i$, {type} {name}, SegmentAllocator alloc$) {
203 setall {{ self$.set{Name}({name}, alloc$); }}
206 # implied accessors are ignored in constructors
207 code value-implied value {
209 setall {{ self$.set{Name}(({type})Memory.length({lengthfor})); }}
212 # ###################################################################### #
217 public class Vulkan {
219 public static int VK_MAKE_API_VERSION(int variant, int major, int minor, int patch) {
220 return (variant << 29) | (major << 22) | (minor << 12) | patch;
228 VK_API_VERSION_1_0 {{ public final static int VK_API_VERSION_1_0 = VK_MAKE_API_VERSION(0, 1, 0, 0); }}
229 VK_API_VERSION_1_1 {{ public final static int VK_API_VERSION_1_1 = VK_MAKE_API_VERSION(0, 1, 1, 0); }}
230 VK_API_VERSION_1_2 {{ public final static int VK_API_VERSION_1_2 = VK_MAKE_API_VERSION(0, 1, 2, 0); }}
231 VK_API_VERSION_1_3 {{ public final static int VK_API_VERSION_1_3 = VK_MAKE_API_VERSION(0, 1, 3, 0); }}
236 // template: dispatch:class
238 import jdk.incubator.foreign.*;
239 import java.lang.invoke.*;
240 import au.notzed.nativez.*;
245 {Name}(VkInstance instance$, ResourceScope scope$) {
250 field-init {{ final NativeSymbol {name}$NS; }}
251 init {{ {name}$NS = instance$.vkGetInstanceProcAddr("{name}", scope$); }}
255 # non-dispatchable handle
258 // template: handle:class
260 import jdk.incubator.foreign.*;
261 import java.lang.invoke.*;
262 import au.notzed.nativez.*;
264 public class {name} implements Pointer {
265 final NativeSymbol self;
267 private {name}(MemoryAddress address, ResourceScope scope) {
268 this.self = NativeSymbol.ofAddress("{name}", address, scope);
272 public static {name} create(MemoryAddress address, ResourceScope scope) {
273 return new {name}(address, scope);
276 public static HandleArray<{name}> createArray(long length, SegmentAllocator alloc) {
277 return HandleArray.createArray(length, alloc, {name}::create);
280 public MemoryAddress address() {
281 return self.address();
284 public ResourceScope scope() {
292 code handle-instance {
294 // template: handle-instance:class
296 import jdk.incubator.foreign.*;
297 import java.lang.invoke.*;
298 import au.notzed.nativez.*;
300 public class {name} implements Pointer {
301 final NativeSymbol self;
302 final DispatchInstance dispatch;
304 private {name}(MemoryAddress address, ResourceScope scope) {
305 this.self = NativeSymbol.ofAddress("{name}", address, scope);
306 this.dispatch = new DispatchInstance(this, scope);
310 public static {name} create(MemoryAddress address, ResourceScope scope) {
311 return new {name}(address, scope);
314 public MemoryAddress address() {
315 return self.address();
318 public ResourceScope scope() {
327 # dispatchable handle
328 code handle-dispatch {
330 // template: handle-dispatch:class
332 import jdk.incubator.foreign.*;
333 import java.lang.invoke.*;
334 import au.notzed.nativez.*;
336 public class {name} implements Pointer {
337 final NativeSymbol self;
338 final DispatchInstance dispatch;
340 private {name}(MemoryAddress address, DispatchInstance dispatch, ResourceScope scope) {
341 this.self = NativeSymbol.ofAddress("{name}", address, scope);
342 this.dispatch = dispatch;
346 public static {name} create(MemoryAddress address, DispatchInstance dispatch, ResourceScope scope) {
347 return new {name}(address, dispatch, scope);
350 // TODO: evaluate how scope fits here
351 public static HandleArray<{name}> createArray(long length, SegmentAllocator alloc, DispatchInstance dispatch, ResourceScope scope) {
352 return HandleArray.createArray(length, alloc, (a, s) -> create(a, dispatch, s), scope);
355 public static HandleArray<{name}> createArray(long length, SegmentAllocator alloc, VkInstance instance, ResourceScope scope) {
356 return HandleArray.createArray(length, alloc, (a, s) -> create(a, instance.dispatch, s), scope);
359 public MemoryAddress address() {
360 return self.address();
363 public ResourceScope scope() {
372 # ###################################################################### #
374 # shared struct components
377 public MemorySegment segment;
379 public static final GroupLayout LAYOUT = {layout};
381 private {Name}(MemorySegment segment) {
382 this.segment = segment;
385 public static {Name} create(MemorySegment segment) {
386 return new {Name}(segment);
389 public static {Name} create(MemoryAddress address, ResourceScope scope) {
390 return create(MemorySegment.ofAddress(address, LAYOUT.byteSize(), scope));
393 public static {Name} create(SegmentAllocator alloc) {
394 var self$ = create(alloc.allocate(LAYOUT));
401 public MemoryAddress address() {
402 return segment.address();
407 public ResourceScope scope() {
408 return segment.scope();
412 public String toString() {
413 return Memory.toString(segment, LAYOUT);
417 public static {Name} {create}({java-setall-arguments}) {
418 {Name} self$ = create(alloc$);
426 public void init({java-setall-arguments}) {
433 static {Name} createArray(MemoryAddress addr, long length, ResourceScope scope) {
434 return create(MemorySegment.ofAddress(addr, length * LAYOUT.byteSize(), scope));
437 public static {Name} createArray(long length, SegmentAllocator alloc) {
438 var self$ = create(alloc.allocateArray(LAYOUT, length));
443 public final static MethodHandle LAYOUT$SH = MemoryLayout.sequenceLayout(LAYOUT).sliceHandle(MemoryLayout.PathElement.sequenceElement());
447 public long length() {
448 return segment.byteSize() / LAYOUT.byteSize();
453 public {Name} getAtIndex(long index$) {
455 return create((MemorySegment)LAYOUT$SH.invokeExact(this.segment, index$));
456 } catch (Throwable t) {
457 throw new RuntimeException(t);
463 # default - writeonly struct
464 code struct-writeonly insert=struct:header,struct:create-all
465 fields=init,set,getorset,handle,java-setall {
467 // template: struct-writeonly:class
469 import jdk.incubator.foreign.*;
470 import java.lang.invoke.*;
471 import au.notzed.nativez.*;
473 public class {Name} implements Pointer {
486 code struct-writeonly-array insert=struct:header,struct:create-all,struct:array
487 fields=init,init-array,set,getorset,setat,getorsetat,handle,handleat,java-setall {
489 // template: struct-writeonly-array:class
491 import jdk.incubator.foreign.*;
492 import java.lang.invoke.*;
493 import au.notzed.nativez.*;
495 public class {Name} implements Pointer, Array<{Name}> {
513 code struct-readonly insert=struct:header
514 fields=init,get,getorset,handle {
516 // template: struct-readonly:class
518 import jdk.incubator.foreign.*;
519 import java.lang.invoke.*;
520 import au.notzed.nativez.*;
522 public class {Name} implements Pointer {
533 code struct-readonly-array insert=struct:header,struct:array
534 fields=init,init-array,get,getorset,getat,getorsetat,handle,handleat {
536 // template: struct-readonly-array:class
538 import jdk.incubator.foreign.*;
539 import java.lang.invoke.*;
540 import au.notzed.nativez.*;
542 public class {Name} implements Pointer, Array<{Name}> {
558 code struct-readwrite insert=struct:header,struct:create-all
559 fields=init,get,set,getorset,handle,java-setall {
561 // template: struct-readwrite:class
563 import jdk.incubator.foreign.*;
564 import java.lang.invoke.*;
565 import au.notzed.nativez.*;
567 public class {Name} implements Pointer {
581 code struct-readwrite-array insert=struct:header,struct:create-all,struct:array
582 fields=init,init-array,get,getat,set,setat,getorset,getorsetat,handle,handleat,java-setall {
584 // template: struct-readwrite-array:class
586 import jdk.incubator.foreign.*;
587 import java.lang.invoke.*;
588 import au.notzed.nativez.*;
590 public class {Name} implements Pointer,Array<{Name}> {
610 # ###################################################################### #
612 # Basic value-based accessor
613 type value accessor=value {
614 native-value {{ {name} }}
616 native-get {{ ({type}){name}$VH.get(this.segment) }}
617 handle {{ final static VarHandle {name}$VH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("{name}")); }}
619 java-get {{ {native-get} }}
620 java-set {{ {name}$VH.set(this.segment, {native-value}) }}
622 native-getat {{ ({type}){name}$AH.get(this.segment, i$) }}
623 native-setat {{ {name}$AH.set(this.segment, i$, {native-value}) }}
624 handleat {{ final static VarHandle {name}$AH = MemoryLayout.sequenceLayout(LAYOUT).varHandle(MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.groupElement("{name}")); }}
626 java-getat {{ {native-getat} }}
627 java-setat {{ {native-setat} }}
630 type value-array accessor=value-array {
631 native-get {{ (MemorySegment){name}$SH.invokeExact(this.segment) }}
632 java-get {{ {type}.create({native-get}) }}
634 native-geti {{ {name}$EH.get(this.segment, i$) }}
636 java-geti {{ ({typei}){native-geti} }}
637 java-seti {{ {name}$EH.set(this.segment, i$, {name}) }}
639 final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));
640 final static VarHandle {name}$EH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("{name}"), MemoryLayout.PathElement.sequenceElement());
644 type value-array2d accessor=value-array2d {
645 native-get {{ (MemorySegment){name}$SH.invokeExact(this.segment) }}
646 java-get {{ {type}.create({native-get}) }}
647 java-geti {{ ({typei}){name}$EH.get(this.segment, i$, j$) }}
648 java-seti {{ {name}$EH.set(this.segment, i$, j$, {name}) }}
650 final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));
651 final static VarHandle {name}$EH = LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("{name}"), MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.sequenceElement());
655 type inline accessor=inline {
656 native-get {{ (MemorySegment){name}$SH.invokeExact(this.segment) }}
657 java-get {{ {type}.create({native-get}) }}
658 handle {{ final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}")); }}
660 native-getat {{ (MemorySegment){name}$SA.invokeExact(this.segment, i$) }}
661 java-getat {{ {type}.create({native-getat}) }}
662 handleat {{ final static MethodHandle {name}$SA = MemoryLayout.sequenceLayout(LAYOUT).sliceHandle(MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.groupElement("{name}")); }}
665 type inline-array accessor=inline {
666 native-get {{ (MemorySegment){name}$SH.invokeExact(this.segment) }}
667 java-get {{ {type}.create({native-get}) }}
668 handle {{ final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}")); }}
670 native-getat {{ (MemorySegment){name}$SA.invokeExact(this.segment, i$) }}
671 java-getat {{ {type}.create({native-getat}) }}
672 handleat {{ final static MethodHandle {name}$SA = MemoryLayout.sequenceLayout(LAYOUT).sliceHandle(MemoryLayout.PathElement.sequenceElement(), MemoryLayout.PathElement.groupElement("{name}")); }}
675 type uint8_t,char value {
677 layout {{ Memory.BYTE }}
680 type uint16_t value {
682 layout {{ Memory.SHORT }}
685 type uint32_t,int,int32_t value {
687 layout {{ Memory.INT }}
690 type uint64_t,int64_t,size_t value {
692 layout {{ Memory.LONG }}
697 layout {{ Memory.FLOAT }}
702 layout {{ Memory.DOUBLE }}
705 # ###################################################################### #
706 # implied length types
707 type uint64_t-implied,size_t-implied uint64_t accessor=value-implied {
710 type uint32_t-implied uint32_t accessor=value-implied {
713 # ###################################################################### #
715 type uint8_t[],char[] value-array {
717 layout {{ MemoryLayout.sequenceLayout({len1}, Memory.BYTE) }}
721 type uint32_t[],int32_t[] value-array {
723 layout {{ MemoryLayout.sequenceLayout({len1}, Memory.INT) }}
727 type uint64_t[] value-array {
729 layout {{ MemoryLayout.sequenceLayout({len1}, Memory.LONG) }}
733 type float[] value-array {
734 type {{ FloatArray }}
735 layout {{ MemoryLayout.sequenceLayout({len1}, Memory.FLOAT) }}
739 type struct[] inline-array {
740 type {{ {baseType} }}
741 layout {{ MemoryLayout.sequenceLayout({len1}, {baseType}.LAYOUT) }}
744 type float[][] value-array2d {
745 type {{ FloatArray }}
747 layout {{ MemoryLayout.sequenceLayout({len1}, MemoryLayout.sequenceLayout({len2}, Memory.FLOAT)) }}
750 # select=len? or what?
753 layout {{ Memory.POINTER }}
754 type {{ MemoryAddress }}
756 native-value {{ Memory.address({name}) }}
757 native-get {{ (MemoryAddress){name}$VH.get(this.segment) }}
759 # java-get {{ {native-get} }}
760 # java-set {{ {name}$VH.set(this.segment, Memory.address({name})) }}
765 type value-pointer pointer {
766 java-get {{ {type}.create((MemoryAddress){name}$VH.get(this.segment), segment.scope()) }}
769 type uint8_t* value-pointer {
773 type uint32_t*,int32_t*,int* value-pointer {
777 type uint64_t* value-pointer {
781 type float* value-pointer {
785 type size_t* value-pointer {
789 type pointer-length pointer {
790 java-get {{ {type}.createArray({native-get}, {length}, this.segment.scope()) }}
793 type void*-length pointer-length {
795 java-get {{ MemorySegment.ofAddress({native-get}, {length}, this.segment.scope()) }}
798 type uint8_t*-length pointer-length {
802 type uint32_t*-length,int32_t*-length pointer-length {
806 type uint64_t*-length pointer-length {
810 type float*-length pointer-length {
814 # special handling for strings, will fail if it isn't
815 type char* pointer accessor=value-alloc {
818 java-get {{ ({native-get}).getUtf8String(0) }}
819 native-value {{ alloc$.allocateUtf8String({name}).address() }}
821 # this just verifies it's a string type
823 if ($v->{len} =~ m/null-terminated/) {
832 type char**-length pointer-length accessor=value-alloc {
835 java-set {{ {name}$VH.set(this.segment, Memory.copyStringArray({name}, alloc$).address()); }}
836 java-get {{ Memory.copyStringArray((MemoryAddress){name}$VH.get(this.segment), {length}) }}
839 type funcpointer pointer {
840 type {{ FunctionPointer<{baseType}> }}
841 typei {{ {baseType} }}
842 java-get {{ {baseType}.downcall({native-get}, this.segment.scope()) }}
845 type void** pointer {
848 type void**-length pointer-length {
852 type handle pointer {
853 type {{ {baseType} }}
854 java-get {{ {type}.create({native-get}, this.segment.scope()) }}
857 type handle[] value-array accessor=handle-array {
858 type {{ HandleArray<{typei}> }}
859 typei {{ {baseType} }}
860 layout {{ MemoryLayout.sequenceLayout({len1}, Memory.POINTER) }}
861 java-get {{ HandleArray.create({native-get}, (a, s) -> {typei}.create(a, instance$.dispatch, s), scope$) }}
863 java-geti {{ {typei}.create((MemoryAddress){native-geti}, instance$.dispatch, scope$) }}
866 type handle* pointer {
867 type {{ HandleArray<{typei}> }}
868 typei {{ {baseType} }}
869 java-get {{ HandleArray.create({native-get}, {typei}::create) }}
872 type handle*-length pointer-length {
873 type {{ HandleArray<{baseType}> }}
874 typei {{ {baseType} }}
875 java-get {{ HandleArray.createArray({native-get}, {length}, {typei}::create, this.segment.scope()) }}
876 java-set {{ {name}$VH.set(this.segment, Memory.address({name})); }}
880 type {{ {baseType} }}
881 layout {{ {baseType}.LAYOUT }}
884 type struct* pointer {
885 type {{ {baseType} }}
886 java-get {{ {baseType}.create({native-get}, this.segment.scope()) }}
889 type struct*-length pointer-length {
890 type {{ {baseType} }}
891 typei {{ {baseType} }}
894 type struct** pointer {
898 type XID,Window,VisualID uint64_t;
900 type Display* handle {
905 type xcb_window_t uint32_t;
906 type xcb_visualid_t uint32_t;
907 type xcb_connection_t* handle {
912 VkInstance template=handle-instance;
913 VkPhysicalDevice template=handle-dispatch;
914 VkDevice template=handle-dispatch;
915 VkCommandBuffer template=handle-dispatch;
916 VkQueue template=handle-dispatch;
918 VkAllocationCallbacks ignore;
920 # We want 'set(all fields) for these types
921 #VkOffset2D template=struct-readwrite-all;
922 #VkOffset3D template=struct-readwrite-all-array;
923 #VkExtent2D template=struct-readwrite-all;
924 #VkExtent3D template=struct-readwrite-all;
925 #VkRect2D template=struct-readwrite-all;
927 # Override default read/write
928 VkDebugUtilsMessengerCallbackDataEXT template=struct-readonly;
929 VkDebugUtilsLabelEXT template=struct-readonly-array;
930 VkDebugUtilsObjectNameInfoEXT template=struct-readonly-array;
932 # override default array-length behaviours, some of these are independent of the array pointer
933 VkAccelerationStructureBuildGeometryInfoKHR geometryCount=type:uint32_t;
934 VkDescriptorSetLayoutBinding pImmutableSamplers=type:handle* descriptorCount=type:uint32_t;
935 VkPipelineViewportStateCreateInfo scissorCount=type:uint32_t viewportCount=type:uint32_t;
937 # can't really work out what this is it's a void ** but it stays it's a pointer to uint8_t * in the spec
938 VkCuLaunchInfoNVX pParams=type:pointer pExtras=type:pointer;