Skip to content

Commit adb4e01

Browse files
committed
add uniforms; fix native crash and memory leaks
1 parent 5aa391e commit adb4e01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1732
-1955
lines changed

hs_err_pid78128.log

Lines changed: 0 additions & 1520 deletions
This file was deleted.

jme3-core/src/main/java/com/jme3/math/Matrix4f.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,4 +2560,16 @@ public Matrix4f clone() {
25602560
throw new AssertionError(); // can not happen
25612561
}
25622562
}
2563+
2564+
/**
2565+
* Flips the sign of the Y scalar component (m11) to make Vulkan renderings
2566+
* appear upright in clip space.
2567+
*
2568+
* @return this instance
2569+
*/
2570+
public Matrix4f flipYScalarForVulkan() {
2571+
m11 = -m11;
2572+
return this;
2573+
}
2574+
25632575
}

jme3-core/src/main/java/com/jme3/renderer/Camera.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,6 @@ public void updateViewProjection() {
12481248
if (overrideProjection) {
12491249
viewProjectionMatrix.set(projectionMatrixOverride).multLocal(viewMatrix);
12501250
} else {
1251-
//viewProjectionMatrix.set(viewMatrix).multLocal(projectionMatrix);
12521251
viewProjectionMatrix.set(projectionMatrix).multLocal(viewMatrix);
12531252
}
12541253
}
@@ -1358,7 +1357,9 @@ public void onFrustumChange() {
13581357

13591358
projectionMatrix.fromFrustum(frustumNear, frustumFar, frustumLeft, frustumRight,
13601359
frustumTop, frustumBottom, parallelProjection);
1361-
// projectionMatrix.transposeLocal();
1360+
1361+
// for Vulkan rendering
1362+
projectionMatrix.flipYScalarForVulkan();
13621363

13631364
// The frame is affected by the frustum values
13641365
// update it as well

jme3-core/src/main/java/com/jme3/renderer/vulkan/VulkanUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jme3.renderer.vulkan;
22

3+
import com.jme3.util.natives.Native;
34
import org.lwjgl.PointerBuffer;
45
import org.lwjgl.system.MemoryStack;
56
import org.lwjgl.system.MemoryUtil;
@@ -127,6 +128,10 @@ public static boolean isBitSet(int n, int bit) {
127128
return (n & bit) > 0;
128129
}
129130

131+
public static int sharingMode(boolean concurrent) {
132+
return concurrent ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE;
133+
}
134+
130135
public static <T extends StructBuffer<E, T>, E extends Struct<E>> T accumulate(Collection<E> collection, IntFunction<T> allocate) {
131136
T buffer = allocate.apply(collection.size());
132137
for (E e : collection) {
@@ -135,6 +140,26 @@ public static <T extends StructBuffer<E, T>, E extends Struct<E>> T accumulate(C
135140
return buffer.rewind();
136141
}
137142

143+
public static LongBuffer accumulate(MemoryStack stack, Native<Long>... natives) {
144+
LongBuffer buf = stack.mallocLong(natives.length);
145+
for (int i = 0; i < buf.limit(); i++) {
146+
buf.put(i, natives[i].getNativeObject());
147+
}
148+
return buf;
149+
}
150+
151+
public static int[] getTransferArguments(int srcLayout, int dstLayout) {
152+
// output array format: {srcAccessMask, dstAccessMask, srcStage, dstStage}
153+
if (srcLayout == VK_IMAGE_LAYOUT_UNDEFINED && dstLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
154+
return new int[] {0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT};
155+
} else if (srcLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && dstLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
156+
return new int[] {VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT,
157+
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT};
158+
} else {
159+
throw new UnsupportedOperationException("Unsupported layer transitions.");
160+
}
161+
}
162+
138163
public static class NativeIterator <T> implements Iterable<T>, Iterator<T> {
139164

140165
private final PointerBuffer pointers;

jme3-core/src/main/java/com/jme3/util/natives/BasicNativeManager.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import java.lang.ref.PhantomReference;
44
import java.lang.ref.ReferenceQueue;
55
import java.lang.ref.WeakReference;
6-
import java.util.ArrayList;
7-
import java.util.Collection;
8-
import java.util.Map;
9-
import java.util.Objects;
6+
import java.util.*;
107
import java.util.concurrent.ConcurrentHashMap;
118
import java.util.concurrent.atomic.AtomicBoolean;
129
import java.util.concurrent.atomic.AtomicLong;
@@ -39,16 +36,20 @@ public int flush() {
3936
int flushed = 0;
4037
for (NativeRef ref; (ref = (NativeRef)unreachable.poll()) != null;) {
4138
ref.destroy();
39+
refMap.remove(ref.id);
4240
flushed++;
4341
}
42+
if (flushed > 0) {
43+
refMap.values().removeIf(NativeRef::isDestroyed);
44+
}
4445
return flushed;
4546
}
4647

4748
@Override
4849
public int clear() {
4950
int size = refMap.size();
5051
for (NativeRef ref : refMap.values()) {
51-
ref.destroyNoRemove();
52+
ref.destroy();
5253
}
5354
refMap.clear();
5455
return size;
@@ -57,10 +58,10 @@ public int clear() {
5758
public class NativeRef extends WeakReference<Native> implements NativeReference {
5859

5960
private final long id;
60-
private final Runnable destroyer;
6161
private final WeakReference<Native> weakRef;
6262
private final AtomicBoolean active = new AtomicBoolean(true);
6363
private final Collection<NativeReference> dependents = new ArrayList<>();
64+
private Runnable destroyer;
6465

6566
private NativeRef(long id, Native referent, ReferenceQueue<Native> q) {
6667
super(referent, q);
@@ -72,33 +73,43 @@ private NativeRef(long id, Native referent, ReferenceQueue<Native> q) {
7273
@Override
7374
public void destroy() {
7475
if (active.getAndSet(false)) {
76+
for (NativeReference ref : dependents) {
77+
ref.destroy();
78+
}
7579
dependents.clear();
76-
destroyNoRemove();
77-
refMap.remove(id, this);
80+
destroyer.run();
81+
Native referent = weakRef.get();
82+
if (referent != null) {
83+
referent.prematureNativeDestruction();
84+
}
7885
}
7986
}
8087

8188
@Override
8289
public void addDependent(NativeReference reference) {
90+
if (isDestroyed()) {
91+
throw new IllegalStateException("Cannot add dependent to destroyed resource.");
92+
}
8393
dependents.add(reference);
8494
}
8595

86-
private long getId() {
87-
return id;
96+
@Override
97+
public boolean isDestroyed() {
98+
return !active.get();
8899
}
89100

90-
private void destroyNoRemove() {
91-
for (NativeReference ref : dependents) {
92-
ref.destroy();
93-
}
94-
dependents.clear();
95-
destroyer.run();
96-
Native referent = weakRef.get();
97-
if (referent != null) {
98-
referent.prematureNativeDestruction();
101+
@Override
102+
public void refresh() {
103+
Native obj = weakRef.get();
104+
if (obj != null) {
105+
destroyer = obj.createNativeDestroyer();
99106
}
100107
}
101108

109+
private long getId() {
110+
return id;
111+
}
112+
102113
}
103114

104115
}

jme3-core/src/main/java/com/jme3/util/natives/NativeReference.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public interface NativeReference {
66

77
void addDependent(NativeReference reference);
88

9+
boolean isDestroyed();
10+
11+
void refresh();
12+
913
}

0 commit comments

Comments
 (0)