|
| 1 | +package org.dynamislight.impl.vulkan.sky; |
| 2 | + |
| 3 | +import org.dynamislight.impl.vulkan.state.VulkanBackendResources; |
| 4 | +import org.vectrix.core.Matrix4f; |
| 5 | + |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.logging.Logger; |
| 8 | + |
| 9 | +/** |
| 10 | + * Optional runtime bridge to DynamisSky via reflection. |
| 11 | + */ |
| 12 | +public final class VulkanSkyRuntimeBridge { |
| 13 | + private static final Logger LOG = Logger.getLogger(VulkanSkyRuntimeBridge.class.getName()); |
| 14 | + |
| 15 | + private Object integration; |
| 16 | + private Method updateMethod; |
| 17 | + private Method recordBackgroundMethod; |
| 18 | + private Method recordCelestialMethod; |
| 19 | + private Method getSunStateMethod; |
| 20 | + |
| 21 | + public void initialize(VulkanBackendResources resources) { |
| 22 | + if (resources == null || resources.device == null || resources.physicalDevice == null) { |
| 23 | + return; |
| 24 | + } |
| 25 | + try { |
| 26 | + Class<?> memoryOpsClass = Class.forName("org.dynamissky.vulkan.lut.LwjglGpuMemoryOps"); |
| 27 | + Object memoryOps = memoryOpsClass |
| 28 | + .getConstructor(org.lwjgl.vulkan.VkDevice.class, org.lwjgl.vulkan.VkPhysicalDevice.class) |
| 29 | + .newInstance(resources.device, resources.physicalDevice); |
| 30 | + |
| 31 | + Class<?> skyConfigClass = Class.forName("org.dynamissky.vulkan.SkyConfig"); |
| 32 | + Object builder = skyConfigClass.getMethod("builder").invoke(null); |
| 33 | + Object config = builder.getClass().getMethod("build").invoke(builder); |
| 34 | + |
| 35 | + Class<?> integrationClass = Class.forName("org.dynamissky.vulkan.integration.VulkanSkyIntegration"); |
| 36 | + integration = integrationClass |
| 37 | + .getMethod("create", long.class, long.class, Class.forName("org.dynamissky.vulkan.lut.GpuMemoryOps"), long.class, skyConfigClass) |
| 38 | + .invoke(null, resources.device.address(), resources.renderPass, memoryOps, 0L, config); |
| 39 | + |
| 40 | + Class<?> cameraStateClass = Class.forName("org.dynamissky.vulkan.lut.CameraState"); |
| 41 | + updateMethod = integrationClass.getMethod("update", long.class, cameraStateClass, float.class, int.class); |
| 42 | + recordBackgroundMethod = integrationClass.getMethod("recordBackground", long.class, Matrix4f.class, int.class); |
| 43 | + recordCelestialMethod = integrationClass.getMethod("recordCelestial", long.class, Matrix4f.class, int.class); |
| 44 | + getSunStateMethod = integrationClass.getMethod("getSunState"); |
| 45 | + } catch (Throwable t) { |
| 46 | + integration = null; |
| 47 | + updateMethod = null; |
| 48 | + recordBackgroundMethod = null; |
| 49 | + recordCelestialMethod = null; |
| 50 | + getSunStateMethod = null; |
| 51 | + LOG.fine("DynamisSky bridge unavailable: " + t.getMessage()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public boolean active() { |
| 56 | + return integration != null; |
| 57 | + } |
| 58 | + |
| 59 | + public void updateAndRecord(long commandBuffer, int frameIndex, Matrix4f viewProj, Matrix4f invViewProj) { |
| 60 | + if (!active()) { |
| 61 | + return; |
| 62 | + } |
| 63 | + try { |
| 64 | + Object cameraState = Class.forName("org.dynamissky.vulkan.lut.CameraState") |
| 65 | + .getMethod("defaultState") |
| 66 | + .invoke(null); |
| 67 | + updateMethod.invoke(integration, commandBuffer, cameraState, 1.0f / 60.0f, frameIndex); |
| 68 | + recordBackgroundMethod.invoke(integration, commandBuffer, invViewProj, frameIndex); |
| 69 | + recordCelestialMethod.invoke(integration, commandBuffer, viewProj, frameIndex); |
| 70 | + } catch (Throwable t) { |
| 71 | + LOG.fine("DynamisSky frame bridge failure: " + t.getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public float[] sunDirection() { |
| 76 | + Object sun = sunState(); |
| 77 | + if (sun == null) { |
| 78 | + return new float[]{0f, -1f, 0f}; |
| 79 | + } |
| 80 | + try { |
| 81 | + Object dir = sun.getClass().getMethod("direction").invoke(sun); |
| 82 | + float x = ((Number) dir.getClass().getMethod("x").invoke(dir)).floatValue(); |
| 83 | + float y = ((Number) dir.getClass().getMethod("y").invoke(dir)).floatValue(); |
| 84 | + float z = ((Number) dir.getClass().getMethod("z").invoke(dir)).floatValue(); |
| 85 | + return new float[]{x, y, z}; |
| 86 | + } catch (Throwable t) { |
| 87 | + return new float[]{0f, -1f, 0f}; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public float[] sunColor() { |
| 92 | + Object sun = sunState(); |
| 93 | + if (sun == null) { |
| 94 | + return new float[]{1f, 1f, 1f}; |
| 95 | + } |
| 96 | + try { |
| 97 | + Object color = sun.getClass().getMethod("color").invoke(sun); |
| 98 | + float r = ((Number) color.getClass().getMethod("r").invoke(color)).floatValue(); |
| 99 | + float g = ((Number) color.getClass().getMethod("g").invoke(color)).floatValue(); |
| 100 | + float b = ((Number) color.getClass().getMethod("b").invoke(color)).floatValue(); |
| 101 | + return new float[]{r, g, b}; |
| 102 | + } catch (Throwable t) { |
| 103 | + return new float[]{1f, 1f, 1f}; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public float sunIntensity() { |
| 108 | + Object sun = sunState(); |
| 109 | + if (sun == null) { |
| 110 | + return 1f; |
| 111 | + } |
| 112 | + try { |
| 113 | + return ((Number) sun.getClass().getMethod("intensity").invoke(sun)).floatValue(); |
| 114 | + } catch (Throwable t) { |
| 115 | + return 1f; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Object sunState() { |
| 120 | + if (!active()) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + try { |
| 124 | + return getSunStateMethod.invoke(integration); |
| 125 | + } catch (Throwable t) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments