Skip to content

Commit 14c9c3c

Browse files
author
scott
committed
Add a GLFence wrapper object
1 parent 2e5628e commit 14c9c3c

File tree

5 files changed

+117
-31
lines changed

5 files changed

+117
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public interface GL4 extends GL3 {
171171
* @param flags must be 0
172172
* @return the sync object handle
173173
*/
174-
public long glFenceSync(int condition, int flags);
174+
public GLFence glFenceSync(int condition, int flags);
175175

176176
/**
177177
* <p><a target="_blank" href="http://docs.gl/gl4/glClientWaitSync">Reference Page</a></p>
@@ -184,7 +184,7 @@ public interface GL4 extends GL3 {
184184
* @return one of {@link #GL_ALREADY_SIGNALED}, {@link #GL_TIMEOUT_EXPIRED},
185185
* {@link #GL_CONDITION_SATISFIED}, or {@link #GL_WAIT_FAILED}
186186
*/
187-
public int glClientWaitSync(long sync, int flags, long timeout);
187+
public int glClientWaitSync(GLFence sync, int flags, long timeout);
188188

189189
/**
190190
* <p><a target="_blank" href="http://docs.gl/gl4/glDeleteSync">Reference Page</a></p>
@@ -193,6 +193,6 @@ public interface GL4 extends GL3 {
193193
*
194194
* @param sync the sync object to delete
195195
*/
196-
public void glDeleteSync(long sync);
196+
public void glDeleteSync(GLFence sync);
197197

198198
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.renderer.opengl;
33+
34+
/**
35+
* Wrapper for an OpenGL sync object (fence).
36+
* <p><a target="_blank" href="https://registry.khronos.org/OpenGL-Refpages/gl4/html/glFenceSync.xhtml">See here.</a></p>
37+
* <p>
38+
* A fence is a synchronization primitive that can be used to coordinate
39+
* work between the CPU and GPU. Once inserted into the command stream,
40+
* the GPU will signal the fence when all preceding commands have completed.
41+
* <p>
42+
* This class wraps the native sync handle (either a long address or a GLSync)
43+
*
44+
* @see GL4#glFenceSync(int, int)
45+
* @see GL4#glClientWaitSync(GLFence, int, long)
46+
* @see GL4#glDeleteSync(GLFence)
47+
*/
48+
public class GLFence {
49+
50+
private final long handle;
51+
private Object nativeSync;
52+
53+
/**
54+
* Creates a new fence wrapper with the given handle.
55+
*
56+
* @param handle the native sync object handle (pointer)
57+
*/
58+
public GLFence(long handle) {
59+
this.handle = handle;
60+
}
61+
62+
/**
63+
* Creates a new fence wrapper with the given handle and native sync object.
64+
*
65+
* @param handle the native sync object handle (pointer)
66+
* @param nativeSync the backend-specific sync object (e.g., LWJGL2's GLSync)
67+
*/
68+
public GLFence(long handle, Object nativeSync) {
69+
this.handle = handle;
70+
this.nativeSync = nativeSync;
71+
}
72+
73+
/**
74+
* Returns the native sync object handle.
75+
*
76+
* @return the sync handle
77+
*/
78+
public long getHandle() {
79+
return handle;
80+
}
81+
82+
/**
83+
* Returns the backend-specific native sync object, if set.
84+
* <p>
85+
* This is used by LWJGL2 that require their own GLSync
86+
* object type rather than a raw pointer.
87+
*
88+
* @return the native sync object, or null if not set
89+
*/
90+
public Object getNativeSync() {
91+
return nativeSync;
92+
}
93+
}

jme3-core/src/main/java/com/jme3/shadow/SdsmFitter.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.jme3.renderer.TextureUnitException;
4242
import com.jme3.renderer.opengl.ComputeShader;
4343
import com.jme3.renderer.opengl.GL4;
44+
import com.jme3.renderer.opengl.GLFence;
4445
import com.jme3.renderer.opengl.ShaderStorageBufferObject;
4546
import com.jme3.texture.Texture;
4647

@@ -225,25 +226,25 @@ private class SdsmResultHolder {
225226
ShaderStorageBufferObject minMaxDepthSsbo;
226227
ShaderStorageBufferObject fitFrustumSsbo;
227228
FitParameters parameters;
228-
long fence = -1;
229+
GLFence fence;
229230

230231
SdsmResultHolder() {
231232
this.minMaxDepthSsbo = new ShaderStorageBufferObject(gl4);
232233
this.fitFrustumSsbo = new ShaderStorageBufferObject(gl4);
233234
}
234235

235236
boolean isReady(boolean wait) {
236-
if (fence == -1L) {
237+
if (fence == null) {
237238
return true;
238239
}
239240
int status = gl4.glClientWaitSync(fence, 0, wait ? -1 : 0);
240241
return status == GL4.GL_ALREADY_SIGNALED || status == GL4.GL_CONDITION_SATISFIED;
241242
}
242243

243244
SplitFitResult extract() {
244-
if (fence >= 0) {
245+
if (fence != null) {
245246
gl4.glDeleteSync(fence);
246-
fence = -1;
247+
fence = null;
247248
}
248249
SplitFit fit = extractFit();
249250
return new SplitFitResult(parameters, fit);
@@ -298,7 +299,7 @@ private SplitFit extractFit() {
298299
void cleanup() {
299300
minMaxDepthSsbo.delete();
300301
fitFrustumSsbo.delete();
301-
if (fence >= 0) {
302+
if (fence != null) {
302303
gl4.glDeleteSync(fence);
303304
}
304305
}

jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import com.jme3.renderer.opengl.GL2;
66
import com.jme3.renderer.opengl.GL3;
77
import com.jme3.renderer.opengl.GL4;
8+
import com.jme3.renderer.opengl.GLFence;
89
import com.jme3.util.BufferUtils;
910
import org.lwjgl.opengl.*;
1011

11-
import java.lang.reflect.Constructor;
1212
import java.nio.*;
1313

1414
public final class LwjglGL implements GL, GL2, GL3, GL4 {
@@ -79,28 +79,19 @@ public void glMemoryBarrier(final int barriers) {
7979
}
8080

8181
@Override
82-
public long glFenceSync(final int condition, final int flags) {
83-
return GL32.glFenceSync(condition, flags).getPointer();
82+
public GLFence glFenceSync(final int condition, final int flags) {
83+
GLSync nativeSync = GL32.glFenceSync(condition, flags);
84+
return new GLFence(nativeSync.getPointer(), nativeSync);
8485
}
8586

86-
private Constructor<GLSync> constructor = null;
87-
private GLSync makeGLSync(final long sync){
88-
try {
89-
if(constructor == null){
90-
constructor = GLSync.class.getDeclaredConstructor(long.class);
91-
constructor.setAccessible(true);
92-
}
93-
return constructor.newInstance(sync);
94-
} catch(Exception e){ throw new RuntimeException(e); }
95-
}
9687
@Override
97-
public int glClientWaitSync(final long sync, final int flags, final long timeout) {
98-
return GL32.glClientWaitSync(makeGLSync(sync), flags, timeout);
88+
public int glClientWaitSync(final GLFence sync, final int flags, final long timeout) {
89+
return GL32.glClientWaitSync((GLSync) sync.getNativeSync(), flags, timeout);
9990
}
10091

10192
@Override
102-
public void glDeleteSync(final long sync) {
103-
GL32.glDeleteSync(makeGLSync(sync));
93+
public void glDeleteSync(final GLFence sync) {
94+
GL32.glDeleteSync((GLSync) sync.getNativeSync());
10495
}
10596

10697
@Override

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.jme3.renderer.opengl.GL2;
3636
import com.jme3.renderer.opengl.GL3;
3737
import com.jme3.renderer.opengl.GL4;
38+
import com.jme3.renderer.opengl.GLFence;
3839
import org.lwjgl.opengl.*;
3940

4041
import java.nio.ByteBuffer;
@@ -99,18 +100,18 @@ public void glMemoryBarrier(final int barriers) {
99100
}
100101

101102
@Override
102-
public long glFenceSync(final int condition, final int flags) {
103-
return GL32.glFenceSync(condition, flags);
103+
public GLFence glFenceSync(final int condition, final int flags) {
104+
return new GLFence(GL32.glFenceSync(condition, flags));
104105
}
105106

106107
@Override
107-
public int glClientWaitSync(final long sync, final int flags, final long timeout) {
108-
return GL32.glClientWaitSync(sync, flags, timeout);
108+
public int glClientWaitSync(final GLFence sync, final int flags, final long timeout) {
109+
return GL32.glClientWaitSync(sync.getHandle(), flags, timeout);
109110
}
110111

111112
@Override
112-
public void glDeleteSync(final long sync) {
113-
GL32.glDeleteSync(sync);
113+
public void glDeleteSync(final GLFence sync) {
114+
GL32.glDeleteSync(sync.getHandle());
114115
}
115116

116117
@Override

0 commit comments

Comments
 (0)