| 1 | package com.mojang.renderpearl.backend.vulkan.init;
|
| 2 |
|
| 3 | import java.lang.reflect.Field;
|
| 4 | import java.lang.reflect.InvocationTargetException;
|
| 5 | import java.lang.reflect.Method;
|
| 6 | import java.util.Locale;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import org.lwjgl.system.MemoryStack;
|
| 10 | import org.lwjgl.system.Pointer;
|
| 11 | import org.lwjgl.system.Struct;
|
| 12 | import org.lwjgl.vulkan.VkPhysicalDeviceFeatures;
|
| 13 | import org.lwjgl.vulkan.VkPhysicalDeviceFeatures2;
|
| 14 | import org.lwjgl.vulkan.VkPhysicalDeviceProperties2;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public record VulkanPNextStruct(Class<?> pNextStructClass, int sType, int structSize) {
|
| 18 | private static final int OFFSET_PNEXT = VkPhysicalDeviceProperties2.PNEXT;
|
| 19 | private static final int OFFSET_STYPE = VkPhysicalDeviceProperties2.STYPE;
|
| 20 |
|
| 21 | public <T extends Struct<?>> VulkanPNextStruct(final Class<T> pNextStructClass) {
|
| 22 | String structClassName = pNextStructClass.getSimpleName();
|
| 23 |
|
| 24 | Method createFunction;
|
| 25 | Method sType$DefaultFunction;
|
| 26 | Field pNextOffsetField;
|
| 27 | Field sTypeOffsetField;
|
| 28 | try {
|
| 29 | createFunction = pNextStructClass.getMethod("calloc", MemoryStack.class);
|
| 30 | sType$DefaultFunction = pNextStructClass.getMethod("sType$Default");
|
| 31 | pNextOffsetField = pNextStructClass.getField("PNEXT");
|
| 32 | sTypeOffsetField = pNextStructClass.getField("STYPE");
|
| 33 | } catch (NoSuchFieldException | NoSuchMethodException e) {
|
| 34 | throw new IllegalArgumentException("Struct class " + structClassName + " does not have required member " + e.getMessage());
|
| 35 | }
|
| 36 |
|
| 37 | int sType;
|
| 38 | int structSize;
|
| 39 | try {
|
| 40 | int pNextOffset = (Integer)pNextOffsetField.get(null);
|
| 41 | if (pNextOffset != OFFSET_PNEXT) {
|
| 42 | throw new IllegalArgumentException("Invalid pNext offset on class " + structClassName);
|
| 43 | }
|
| 44 |
|
| 45 | int sTypeOffset = (Integer)sTypeOffsetField.get(null);
|
| 46 | if (sTypeOffset != OFFSET_STYPE) {
|
| 47 | throw new IllegalArgumentException("Invalid sType offset on class " + structClassName);
|
| 48 | }
|
| 49 |
|
| 50 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 51 | Struct<?> structInstance = (Struct<?>)createFunction.invoke(null, stack);
|
| 52 | sType$DefaultFunction.invoke(structInstance);
|
| 53 | sType = sType(structInstance.address());
|
| 54 | structSize = structInstance.sizeof();
|
| 55 | }
|
| 56 | } catch (IllegalAccessException | InvocationTargetException e) {
|
| 57 | throw new IllegalArgumentException(e);
|
| 58 | }
|
| 59 |
|
| 60 | this(pNextStructClass, sType, structSize);
|
| 61 | }
|
| 62 |
|
| 63 | private static long pNext(final long pointer) {
|
| 64 | return VkPhysicalDeviceProperties2.npNext(pointer);
|
| 65 | }
|
| 66 |
|
| 67 | private static void pNext(final long pointer, final long value) {
|
| 68 | VkPhysicalDeviceProperties2.npNext(pointer, value);
|
| 69 | }
|
| 70 |
|
| 71 | private static int sType(final long pointer) {
|
| 72 | return VkPhysicalDeviceProperties2.nsType(pointer);
|
| 73 | }
|
| 74 |
|
| 75 | private static void sType(final long pointer, final int value) {
|
| 76 | VkPhysicalDeviceProperties2.nsType(pointer, value);
|
| 77 | }
|
| 78 |
|
| 79 | public long fieldOffset(final String name) {
|
| 80 | Class<?> sourceClass;
|
| 81 | int sourceClassOffset;
|
| 82 | if (this.pNextStructClass == VkPhysicalDeviceFeatures2.class) {
|
| 83 | sourceClass = VkPhysicalDeviceFeatures.class;
|
| 84 | sourceClassOffset = VkPhysicalDeviceFeatures2.FEATURES;
|
| 85 | } else {
|
| 86 | sourceClass = this.pNextStructClass;
|
| 87 | sourceClassOffset = 0;
|
| 88 | }
|
| 89 |
|
| 90 | try {
|
| 91 | Method method = sourceClass.getMethod(name);
|
| 92 | if (method.getReturnType() != boolean.class) {
|
| 93 | throw new IllegalArgumentException("Only booleans are supported for struct methods");
|
| 94 | }
|
| 95 |
|
| 96 | Field offsetField = sourceClass.getField(name.toUpperCase(Locale.ROOT));
|
| 97 | return sourceClassOffset + (Integer)offsetField.get(null);
|
| 98 | } catch (NoSuchFieldException | NoSuchMethodException e) {
|
| 99 | throw new IllegalArgumentException("Could not find field " + name + " in struct " + this.pNextStructClass.getSimpleName());
|
| 100 | } catch (IllegalAccessException e) {
|
| 101 | throw new IllegalArgumentException(e);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | public long findOrCreateStructInPNextChain(final VkPhysicalDeviceProperties2 properties2, final MemoryStack stack) {
|
| 106 | return this.findOrCreateStructInPNextChain(properties2.address(), stack);
|
| 107 | }
|
| 108 |
|
| 109 | public long findOrCreateStructInPNextChain(final VkPhysicalDeviceFeatures2 features2, final MemoryStack stack) {
|
| 110 | return this.findOrCreateStructInPNextChain(features2.address(), stack);
|
| 111 | }
|
| 112 |
|
| 113 | public long findOrCreateStructInPNextChain(final long pNextChain, final MemoryStack stack) {
|
| 114 | long foundStruct = findStructInPNextChain(pNextChain, this.sType);
|
| 115 | if (foundStruct != 0L) {
|
| 116 | return foundStruct;
|
| 117 | }
|
| 118 |
|
| 119 | long newStruct = stack.ncalloc(Pointer.POINTER_SIZE, 1, this.structSize);
|
| 120 | sType(newStruct, this.sType);
|
| 121 | pNext(newStruct, pNext(pNextChain));
|
| 122 | pNext(pNextChain, newStruct);
|
| 123 | return newStruct;
|
| 124 | }
|
| 125 |
|
| 126 | public long findStructInPNextChain(final long pNextChain) {
|
| 127 | return findStructInPNextChain(pNextChain, this.sType);
|
| 128 | }
|
| 129 |
|
| 130 | private static long findStructInPNextChain(long pNextChain, final int sType) {
|
| 131 | while (pNextChain != 0L) {
|
| 132 | if (sType(pNextChain) == sType) {
|
| 133 | return pNextChain;
|
| 134 | }
|
| 135 |
|
| 136 | pNextChain = pNext(pNextChain);
|
| 137 | }
|
| 138 |
|
| 139 | return 0L;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public String toString() {
|
| 144 | return this.pNextStructClass.getSimpleName();
|
| 145 | }
|
| 146 | }
|
| 147 |
|