Jelajahi Sumber

Add a GLFence wrapper object

scott 2 minggu lalu
induk
melakukan
14c9c3ce12

+ 3 - 3
jme3-core/src/main/java/com/jme3/renderer/opengl/GL4.java

@@ -171,7 +171,7 @@ public interface GL4 extends GL3 {
      * @param flags     must be 0
      * @param flags     must be 0
      * @return the sync object handle
      * @return the sync object handle
      */
      */
-    public long glFenceSync(int condition, int flags);
+    public GLFence glFenceSync(int condition, int flags);
 
 
     /**
     /**
      * <p><a target="_blank" href="http://docs.gl/gl4/glClientWaitSync">Reference Page</a></p>
      * <p><a target="_blank" href="http://docs.gl/gl4/glClientWaitSync">Reference Page</a></p>
@@ -184,7 +184,7 @@ public interface GL4 extends GL3 {
      * @return one of {@link #GL_ALREADY_SIGNALED}, {@link #GL_TIMEOUT_EXPIRED},
      * @return one of {@link #GL_ALREADY_SIGNALED}, {@link #GL_TIMEOUT_EXPIRED},
      *         {@link #GL_CONDITION_SATISFIED}, or {@link #GL_WAIT_FAILED}
      *         {@link #GL_CONDITION_SATISFIED}, or {@link #GL_WAIT_FAILED}
      */
      */
-    public int glClientWaitSync(long sync, int flags, long timeout);
+    public int glClientWaitSync(GLFence sync, int flags, long timeout);
 
 
     /**
     /**
      * <p><a target="_blank" href="http://docs.gl/gl4/glDeleteSync">Reference Page</a></p>
      * <p><a target="_blank" href="http://docs.gl/gl4/glDeleteSync">Reference Page</a></p>
@@ -193,6 +193,6 @@ public interface GL4 extends GL3 {
      *
      *
      * @param sync the sync object to delete
      * @param sync the sync object to delete
      */
      */
-    public void glDeleteSync(long sync);
+    public void glDeleteSync(GLFence sync);
 
 
 }
 }

+ 93 - 0
jme3-core/src/main/java/com/jme3/renderer/opengl/GLFence.java

@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2009-2026 jMonkeyEngine
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.renderer.opengl;
+
+/**
+ * Wrapper for an OpenGL sync object (fence).
+ * <p><a target="_blank" href="https://registry.khronos.org/OpenGL-Refpages/gl4/html/glFenceSync.xhtml">See here.</a></p>
+ * <p>
+ * A fence is a synchronization primitive that can be used to coordinate
+ * work between the CPU and GPU. Once inserted into the command stream,
+ * the GPU will signal the fence when all preceding commands have completed.
+ * <p>
+ * This class wraps the native sync handle (either a long address or a GLSync)
+ *
+ * @see GL4#glFenceSync(int, int)
+ * @see GL4#glClientWaitSync(GLFence, int, long)
+ * @see GL4#glDeleteSync(GLFence)
+ */
+public class GLFence {
+
+    private final long handle;
+    private Object nativeSync;
+
+    /**
+     * Creates a new fence wrapper with the given handle.
+     *
+     * @param handle the native sync object handle (pointer)
+     */
+    public GLFence(long handle) {
+        this.handle = handle;
+    }
+
+    /**
+     * Creates a new fence wrapper with the given handle and native sync object.
+     *
+     * @param handle the native sync object handle (pointer)
+     * @param nativeSync the backend-specific sync object (e.g., LWJGL2's GLSync)
+     */
+    public GLFence(long handle, Object nativeSync) {
+        this.handle = handle;
+        this.nativeSync = nativeSync;
+    }
+
+    /**
+     * Returns the native sync object handle.
+     *
+     * @return the sync handle
+     */
+    public long getHandle() {
+        return handle;
+    }
+
+    /**
+     * Returns the backend-specific native sync object, if set.
+     * <p>
+     * This is used by LWJGL2 that require their own GLSync
+     * object type rather than a raw pointer.
+     *
+     * @return the native sync object, or null if not set
+     */
+    public Object getNativeSync() {
+        return nativeSync;
+    }
+}

+ 6 - 5
jme3-core/src/main/java/com/jme3/shadow/SdsmFitter.java

@@ -41,6 +41,7 @@ import com.jme3.renderer.RendererException;
 import com.jme3.renderer.TextureUnitException;
 import com.jme3.renderer.TextureUnitException;
 import com.jme3.renderer.opengl.ComputeShader;
 import com.jme3.renderer.opengl.ComputeShader;
 import com.jme3.renderer.opengl.GL4;
 import com.jme3.renderer.opengl.GL4;
+import com.jme3.renderer.opengl.GLFence;
 import com.jme3.renderer.opengl.ShaderStorageBufferObject;
 import com.jme3.renderer.opengl.ShaderStorageBufferObject;
 import com.jme3.texture.Texture;
 import com.jme3.texture.Texture;
 
 
@@ -225,7 +226,7 @@ public class SdsmFitter {
         ShaderStorageBufferObject minMaxDepthSsbo;
         ShaderStorageBufferObject minMaxDepthSsbo;
         ShaderStorageBufferObject fitFrustumSsbo;
         ShaderStorageBufferObject fitFrustumSsbo;
         FitParameters parameters;
         FitParameters parameters;
-        long fence = -1;
+        GLFence fence;
 
 
         SdsmResultHolder() {
         SdsmResultHolder() {
             this.minMaxDepthSsbo = new ShaderStorageBufferObject(gl4);
             this.minMaxDepthSsbo = new ShaderStorageBufferObject(gl4);
@@ -233,7 +234,7 @@ public class SdsmFitter {
         }
         }
 
 
         boolean isReady(boolean wait) {
         boolean isReady(boolean wait) {
-            if (fence == -1L) {
+            if (fence == null) {
                 return true;
                 return true;
             }
             }
             int status = gl4.glClientWaitSync(fence, 0, wait ? -1 : 0);
             int status = gl4.glClientWaitSync(fence, 0, wait ? -1 : 0);
@@ -241,9 +242,9 @@ public class SdsmFitter {
         }
         }
 
 
         SplitFitResult extract() {
         SplitFitResult extract() {
-            if (fence >= 0) {
+            if (fence != null) {
                 gl4.glDeleteSync(fence);
                 gl4.glDeleteSync(fence);
-                fence = -1;
+                fence = null;
             }
             }
             SplitFit fit = extractFit();
             SplitFit fit = extractFit();
             return new SplitFitResult(parameters, fit);
             return new SplitFitResult(parameters, fit);
@@ -298,7 +299,7 @@ public class SdsmFitter {
         void cleanup() {
         void cleanup() {
             minMaxDepthSsbo.delete();
             minMaxDepthSsbo.delete();
             fitFrustumSsbo.delete();
             fitFrustumSsbo.delete();
-            if (fence >= 0) {
+            if (fence != null) {
                 gl4.glDeleteSync(fence);
                 gl4.glDeleteSync(fence);
             }
             }
         }
         }

+ 8 - 17
jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java

@@ -5,10 +5,10 @@ import com.jme3.renderer.opengl.GL;
 import com.jme3.renderer.opengl.GL2;
 import com.jme3.renderer.opengl.GL2;
 import com.jme3.renderer.opengl.GL3;
 import com.jme3.renderer.opengl.GL3;
 import com.jme3.renderer.opengl.GL4;
 import com.jme3.renderer.opengl.GL4;
+import com.jme3.renderer.opengl.GLFence;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.BufferUtils;
 import org.lwjgl.opengl.*;
 import org.lwjgl.opengl.*;
 
 
-import java.lang.reflect.Constructor;
 import java.nio.*;
 import java.nio.*;
 
 
 public final class LwjglGL implements GL, GL2, GL3, GL4 {
 public final class LwjglGL implements GL, GL2, GL3, GL4 {
@@ -79,28 +79,19 @@ public final class LwjglGL implements GL, GL2, GL3, GL4 {
     }
     }
 
 
     @Override
     @Override
-    public long glFenceSync(final int condition, final int flags) {
-        return GL32.glFenceSync(condition, flags).getPointer();
+    public GLFence glFenceSync(final int condition, final int flags) {
+        GLSync nativeSync = GL32.glFenceSync(condition, flags);
+        return new GLFence(nativeSync.getPointer(), nativeSync);
     }
     }
 
 
-    private Constructor<GLSync> constructor = null;
-    private GLSync makeGLSync(final long sync){
-        try {
-            if(constructor == null){
-                constructor = GLSync.class.getDeclaredConstructor(long.class);
-                constructor.setAccessible(true);
-            }
-            return constructor.newInstance(sync);
-        } catch(Exception e){ throw new RuntimeException(e); }
-    }
     @Override
     @Override
-    public int glClientWaitSync(final long sync, final int flags, final long timeout) {
-        return GL32.glClientWaitSync(makeGLSync(sync), flags, timeout);
+    public int glClientWaitSync(final GLFence sync, final int flags, final long timeout) {
+        return GL32.glClientWaitSync((GLSync) sync.getNativeSync(), flags, timeout);
     }
     }
 
 
     @Override
     @Override
-    public void glDeleteSync(final long sync) {
-        GL32.glDeleteSync(makeGLSync(sync));
+    public void glDeleteSync(final GLFence sync) {
+        GL32.glDeleteSync((GLSync) sync.getNativeSync());
     }
     }
     
     
     @Override
     @Override

+ 7 - 6
jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java

@@ -35,6 +35,7 @@ import com.jme3.renderer.opengl.GL;
 import com.jme3.renderer.opengl.GL2;
 import com.jme3.renderer.opengl.GL2;
 import com.jme3.renderer.opengl.GL3;
 import com.jme3.renderer.opengl.GL3;
 import com.jme3.renderer.opengl.GL4;
 import com.jme3.renderer.opengl.GL4;
+import com.jme3.renderer.opengl.GLFence;
 import org.lwjgl.opengl.*;
 import org.lwjgl.opengl.*;
 
 
 import java.nio.ByteBuffer;
 import java.nio.ByteBuffer;
@@ -99,18 +100,18 @@ public class LwjglGL extends LwjglRender implements GL, GL2, GL3, GL4 {
     }
     }
 
 
     @Override
     @Override
-    public long glFenceSync(final int condition, final int flags) {
-        return GL32.glFenceSync(condition, flags);
+    public GLFence glFenceSync(final int condition, final int flags) {
+        return new GLFence(GL32.glFenceSync(condition, flags));
     }
     }
 
 
     @Override
     @Override
-    public int glClientWaitSync(final long sync, final int flags, final long timeout) {
-        return GL32.glClientWaitSync(sync, flags, timeout);
+    public int glClientWaitSync(final GLFence sync, final int flags, final long timeout) {
+        return GL32.glClientWaitSync(sync.getHandle(), flags, timeout);
     }
     }
 
 
     @Override
     @Override
-    public void glDeleteSync(final long sync) {
-        GL32.glDeleteSync(sync);
+    public void glDeleteSync(final GLFence sync) {
+        GL32.glDeleteSync(sync.getHandle());
     }
     }
 
 
     @Override
     @Override