Skip to content

Commit 4e1decb

Browse files
committed
improve structs again
1 parent e105554 commit 4e1decb

33 files changed

Lines changed: 272 additions & 1396 deletions

jme3-core/src/main/java/com/jme3/backend/Engine.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.jme3.material.Material;
55
import com.jme3.math.Vector2f;
66
import com.jme3.math.Vector3f;
7-
import com.jme3.renderer.ViewPortArea;
87
import com.jme3.scene.GlVertexBuffer;
98
import com.jme3.scene.Mesh;
109
import com.jme3.texture.Texture;
@@ -13,7 +12,6 @@
1312
import com.jme3.vulkan.buffers.BufferGenerator;
1413
import com.jme3.vulkan.buffers.BufferUsage;
1514
import com.jme3.vulkan.buffers.MappableBuffer;
16-
import com.jme3.vulkan.commands.RenderSetting;
1715
import com.jme3.vulkan.material.uniforms.Uniform;
1816
import com.jme3.vulkan.mesh.InputRate;
1917
import com.jme3.vulkan.mesh.attribute.Attribute;

jme3-core/src/main/java/com/jme3/backend/SimpleVulkanEngine.java

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,15 @@
8080

8181
public class SimpleVulkanEngine implements RenderEngine {
8282

83-
public static class LightData implements Struct {
83+
public static class LightData extends Struct {
8484

8585
private Light light;
86-
@Member(0) public final ColorRGBA color = new ColorRGBA();
87-
@Member(1) public final Vector4f position = new Vector4f();
88-
@Member(2) public final Vector4f direction = new Vector4f();
89-
90-
public LightData() {}
86+
public final Field<ColorRGBA> color = new Field<>(new ColorRGBA());
87+
public final Field<Vector4f> position = new Field<>(new Vector4f());
88+
public final Field<Vector4f> direction = new Field<>(new Vector4f());
9189

9290
public LightData(Light light) {
91+
addFields(color, position, direction);
9392
set(light);
9493
}
9594

@@ -105,27 +104,27 @@ public void set(Light light) {
105104

106105
public void set(DirectionalLight light) {
107106
this.light = light;
108-
color.set(light.getColor());
109-
color.a = light.getType().getId();
110-
direction.set(light.getDirection()); // this breaks protocol with jme's shaders
107+
color.get().set(light.getColor());
108+
color.get().a = light.getType().getId();
109+
direction.get().set(light.getDirection()); // this breaks protocol with jme's shaders
111110
}
112111

113112
public void set(PointLight light) {
114113
this.light = light;
115-
color.set(light.getColor());
116-
color.a = light.getType().getId();
117-
position.set(light.getPosition());
118-
position.w = light.getInvRadius();
114+
color.get().set(light.getColor());
115+
color.get().a = light.getType().getId();
116+
position.get().set(light.getPosition());
117+
position.get().w = light.getInvRadius();
119118
}
120119

121120
public void set(SpotLight light) {
122121
this.light = light;
123-
color.set(light.getColor());
124-
color.a = light.getType().getId();
125-
position.set(light.getPosition());
126-
position.w = light.getInvSpotRange();
127-
direction.set(light.getDirection());
128-
direction.w = light.getPackedAngleCos();
122+
color.get().set(light.getColor());
123+
color.get().a = light.getType().getId();
124+
position.get().set(light.getPosition());
125+
position.get().w = light.getInvSpotRange();
126+
direction.get().set(light.getDirection());
127+
direction.get().w = light.getPackedAngleCos();
129128
}
130129

131130
public Light getLight() {
@@ -134,14 +133,26 @@ public Light getLight() {
134133

135134
}
136135

137-
public static class Lighting implements Struct {
138-
@Member(0) public final List<LightData> lights = new ArrayList<>();
139-
@Member(1) public final List<Integer> indices = new ArrayList<>();
136+
public static class Lighting extends Struct {
137+
138+
public final Field<List<LightData>> lights = new Field<>(new ArrayList<>());
139+
public final Field<List<Integer>> indices = new Field<>(new ArrayList<>());
140+
141+
public Lighting() {
142+
addFields(lights, indices);
143+
}
144+
140145
}
141146

142-
public static class Transforms implements Struct {
143-
@Member(0) public final Matrix4f worldViewProjectionMatrix = new Matrix4f();
144-
@Member(1) public final Matrix4f viewProjectionMatrix = new Matrix4f();
147+
public static class Transforms extends Struct {
148+
149+
public final Field<Matrix4f> worldViewProjectionMatrix = new Field<>(new Matrix4f());
150+
public final Field<Matrix4f> viewProjectionMatrix = new Field<>(new Matrix4f());
151+
152+
public Transforms() {
153+
addFields(worldViewProjectionMatrix, viewProjectionMatrix);
154+
}
155+
145156
}
146157

147158
private final Application app;
@@ -318,12 +329,14 @@ public Mesh createMesh(int vertices, int instances) {
318329

319330
@Override
320331
public <T extends Struct> Uniform<T> createUniformBuffer(StructLayout layout, T struct) {
321-
return new StructUniform<>(Descriptor.UniformBuffer, BufferUsage.Uniform, layout, struct, bufferGen);
332+
return new StructUniform<>(Descriptor.StorageBuffer, layout, struct,
333+
size -> new StreamingBuffer(size, b -> b.setUsage(BufferUsage.Uniform)));
322334
}
323335

324336
@Override
325337
public <T extends Struct> Uniform<T> createShaderStorageUniform(StructLayout layout, T struct) {
326-
return new StructUniform<>(Descriptor.StorageBuffer, BufferUsage.Storage, layout, struct, bufferGen);
338+
return new StructUniform<>(Descriptor.StorageBuffer, layout, struct,
339+
size -> new StreamingBuffer(size, b -> b.setUsage(BufferUsage.Storage)));
327340
}
328341

329342
@Override
@@ -386,10 +399,10 @@ private void render(ViewPort vp) {
386399
if (!vp.isEnabled() || vp.getScenes().isEmpty()) {
387400
return;
388401
}
389-
lighting.lights.clear();
402+
lighting.lights.get().clear();
390403
for (Spatial scene : vp.getScenes()) for (Spatial child : scene) {
391404
for (Light l : child.getLocalLightList()) {
392-
lighting.lights.add(new LightData(l));
405+
lighting.lights.get().add(new LightData(l));
393406
}
394407
}
395408
settings.pushViewPort(vp.getArea());
@@ -408,18 +421,18 @@ private void render(ViewPort vp) {
408421
if (e.getMaterial() != currentMaterial) {
409422
(currentMaterial = e.getMaterial()).bind(graphics, currentPipeline, descriptorPool);
410423
}
411-
lighting.indices.clear();
424+
lighting.indices.get().clear();
412425
int lightIndex = 0;
413-
for (LightData l : lighting.lights) {
426+
for (LightData l : lighting.lights.get()) {
414427
if (l.getLight().intersectsVolume(e.getGeometry().getWorldBound(), vars)) {
415-
lighting.indices.add(lightIndex);
428+
lighting.indices.get().add(lightIndex);
416429
}
417430
lightIndex++;
418431
}
419-
transforms.viewProjectionMatrix.mult(e.getGeometry().getWorldMatrix(), transforms.worldViewProjectionMatrix);
432+
transforms.viewProjectionMatrix.get().mult(e.getGeometry().getWorldMatrix(), transforms.worldViewProjectionMatrix.get());
420433
e.getMaterial().set("Lighting", lighting);
421434
e.getMaterial().set("Transforms", transforms);
422-
stream.stream(graphics); // this is a problem because it can bypass wrappers
435+
graphics.uploadBuffers(stream);
423436
e.getMesh().render(graphics, currentPipeline);
424437
}
425438
b.cleanupRender(vp, settings);
@@ -449,17 +462,16 @@ public void accept(Swapchain swapchain) {
449462

450463
}
451464

452-
private class BufferGeneratorImpl implements BufferGenerator<VulkanBuffer> {
465+
private static class BufferGeneratorImpl implements BufferGenerator<VulkanBuffer> {
453466

454467
@Override
455468
public VulkanBuffer createBuffer(MemorySize size, Flag<BufferUsage> bufUsage, GlVertexBuffer.Usage dataUsage) {
456469
switch (dataUsage) {
457470
case Static: case Dynamic: {
458-
return stream.add(new StreamingBuffer(device, size, bufUsage));
471+
return new StreamingBuffer(size, true, b -> b.setUsage(bufUsage));
459472
}
460473
case Stream: {
461-
return new PersistentVulkanBuffer<>(HostVisibleBuffer.build(
462-
device, size, b -> b.setUsage(BufferUsage.Vertex)));
474+
return new PersistentVulkanBuffer<>(HostVisibleBuffer.build(size, b -> b.setUsage(BufferUsage.Vertex)));
463475
}
464476
case CpuOnly: throw new IllegalArgumentException("Cannot create cpu-only buffer for Vulkan.");
465477
default: throw new UnsupportedOperationException("Unrecognized: " + dataUsage);

jme3-core/src/main/java/com/jme3/material/Material.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,11 @@
3131
*/
3232
package com.jme3.material;
3333

34-
import com.jme3.export.Savable;
35-
import com.jme3.math.*;
3634
import com.jme3.scene.Geometry;
37-
import com.jme3.texture.Texture;
3835
import com.jme3.util.struct.Struct;
39-
import com.jme3.util.struct.StructBuffer;
40-
import com.jme3.vulkan.buffers.MappableBuffer;
4136
import com.jme3.vulkan.material.technique.NewTechnique;
4237
import com.jme3.vulkan.material.uniforms.Uniform;
4338

44-
import java.util.Objects;
45-
4639
/**
4740
* <code>Material</code> describes the rendering style for a given
4841
* {@link Geometry}.
@@ -74,4 +67,13 @@ default void set(String name, Object value) {
7467
getUniform(name).set(value);
7568
}
7669

70+
default <T> T get(String name) {
71+
Uniform<T> u = getUniform(name);
72+
return u.get();
73+
}
74+
75+
default <T> T get(String name, Class<T> type) {
76+
return get(name);
77+
}
78+
7779
}

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import com.jme3.export.*;
3535
import com.jme3.util.BufferUtils;
3636
import com.jme3.util.TempVars;
37+
import org.lwjgl.system.MemoryUtil;
3738

3839
import java.io.IOException;
39-
import java.nio.ByteBuffer;
4040
import java.nio.FloatBuffer;
4141
import java.util.logging.Logger;
4242

@@ -1462,6 +1462,48 @@ public void fromStartEndVectors(Vector3f start, Vector3f end) {
14621462
}
14631463
}
14641464

1465+
/**
1466+
* Writes this matrix directly to the memory at the address.
1467+
*
1468+
* @param address memory address
1469+
* @deprecated can lead to memory corruption
1470+
*/
1471+
@SuppressWarnings("DeprecatedIsStillUsed")
1472+
@Deprecated
1473+
public Matrix3f writeToStdMemory(long address) {
1474+
MemoryUtil.memPutFloat(address, m00);
1475+
MemoryUtil.memPutFloat(address += Float.BYTES, m10);
1476+
MemoryUtil.memPutFloat(address += Float.BYTES, m20);
1477+
MemoryUtil.memPutFloat(address += Float.BYTES << 1, m01);
1478+
MemoryUtil.memPutFloat(address += Float.BYTES, m11);
1479+
MemoryUtil.memPutFloat(address += Float.BYTES, m21);
1480+
MemoryUtil.memPutFloat(address += Float.BYTES << 1, m02);
1481+
MemoryUtil.memPutFloat(address += Float.BYTES, m12);
1482+
MemoryUtil.memPutFloat(address + Float.BYTES, m22);
1483+
return this;
1484+
}
1485+
1486+
/**
1487+
* Reads to this matrix directly to the memory at the address.
1488+
*
1489+
* @param address memory address
1490+
* @deprecated can lead to unexpected values
1491+
*/
1492+
@SuppressWarnings("DeprecatedIsStillUsed")
1493+
@Deprecated
1494+
public Matrix3f readFromStdMemory(long address) {
1495+
m00 = MemoryUtil.memGetFloat(address);
1496+
m10 = MemoryUtil.memGetFloat(address += Float.BYTES);
1497+
m20 = MemoryUtil.memGetFloat(address += Float.BYTES);
1498+
m01 = MemoryUtil.memGetFloat(address += Float.BYTES << 1);
1499+
m11 = MemoryUtil.memGetFloat(address += Float.BYTES);
1500+
m21 = MemoryUtil.memGetFloat(address += Float.BYTES);
1501+
m02 = MemoryUtil.memGetFloat(address += Float.BYTES << 1);
1502+
m12 = MemoryUtil.memGetFloat(address += Float.BYTES);
1503+
m22 = MemoryUtil.memGetFloat(address + Float.BYTES);
1504+
return this;
1505+
}
1506+
14651507
/**
14661508
* Scales each column by the corresponding element of the argument.
14671509
*

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.jme3.export.*;
3535
import com.jme3.util.BufferUtils;
3636
import com.jme3.util.TempVars;
37+
import org.lwjgl.system.MemoryUtil;
3738

3839
import java.io.IOException;
3940
import java.nio.FloatBuffer;
@@ -2413,6 +2414,61 @@ public void read(JmeImporter importer) throws IOException {
24132414
m33 = cap.readFloat("m33", 1);
24142415
}
24152416

2417+
/**
2418+
* Writes this matrix directly to the memory at the address. It is not recommended
2419+
* to use this method as it can lead to memory corruption.
2420+
*
2421+
* @param address memory address
2422+
* @deprecated can lead to memory corruption
2423+
*/
2424+
@Deprecated
2425+
public Matrix4f writeToStdMemory(long address) {
2426+
MemoryUtil.memPutFloat(address, m00);
2427+
MemoryUtil.memPutFloat(address += Float.BYTES, m10);
2428+
MemoryUtil.memPutFloat(address += Float.BYTES, m20);
2429+
MemoryUtil.memPutFloat(address += Float.BYTES, m30);
2430+
MemoryUtil.memPutFloat(address += Float.BYTES, m01);
2431+
MemoryUtil.memPutFloat(address += Float.BYTES, m11);
2432+
MemoryUtil.memPutFloat(address += Float.BYTES, m21);
2433+
MemoryUtil.memPutFloat(address += Float.BYTES, m31);
2434+
MemoryUtil.memPutFloat(address += Float.BYTES, m02);
2435+
MemoryUtil.memPutFloat(address += Float.BYTES, m12);
2436+
MemoryUtil.memPutFloat(address += Float.BYTES, m22);
2437+
MemoryUtil.memPutFloat(address += Float.BYTES, m32);
2438+
MemoryUtil.memPutFloat(address += Float.BYTES, m03);
2439+
MemoryUtil.memPutFloat(address += Float.BYTES, m13);
2440+
MemoryUtil.memPutFloat(address += Float.BYTES, m23);
2441+
MemoryUtil.memPutFloat(address + Float.BYTES, m33);
2442+
return this;
2443+
}
2444+
2445+
/**
2446+
* Reads to this matrix directly to the memory at the address.
2447+
*
2448+
* @param address memory address
2449+
* @deprecated can lead to unexpected values
2450+
*/
2451+
@Deprecated
2452+
public Matrix4f readFromStdMemory(long address) {
2453+
m00 = MemoryUtil.memGetFloat(address);
2454+
m10 = MemoryUtil.memGetFloat(address += Float.BYTES);
2455+
m20 = MemoryUtil.memGetFloat(address += Float.BYTES);
2456+
m30 = MemoryUtil.memGetFloat(address += Float.BYTES);
2457+
m01 = MemoryUtil.memGetFloat(address += Float.BYTES);
2458+
m11 = MemoryUtil.memGetFloat(address += Float.BYTES);
2459+
m21 = MemoryUtil.memGetFloat(address += Float.BYTES);
2460+
m31 = MemoryUtil.memGetFloat(address += Float.BYTES);
2461+
m02 = MemoryUtil.memGetFloat(address += Float.BYTES);
2462+
m12 = MemoryUtil.memGetFloat(address += Float.BYTES);
2463+
m22 = MemoryUtil.memGetFloat(address += Float.BYTES);
2464+
m32 = MemoryUtil.memGetFloat(address += Float.BYTES);
2465+
m03 = MemoryUtil.memGetFloat(address += Float.BYTES);
2466+
m13 = MemoryUtil.memGetFloat(address += Float.BYTES);
2467+
m23 = MemoryUtil.memGetFloat(address += Float.BYTES);
2468+
m33 = MemoryUtil.memGetFloat(address + Float.BYTES);
2469+
return this;
2470+
}
2471+
24162472
/**
24172473
* Test for exact identity.
24182474
*

0 commit comments

Comments
 (0)