Skip to content

Commit 1e2926d

Browse files
committed
setup vulkan test for rendering from the scene graph
1 parent 49cc7f2 commit 1e2926d

File tree

7 files changed

+76
-69
lines changed

7 files changed

+76
-69
lines changed

jme3-core/src/main/java/com/jme3/scene/Geometry.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@
4242
import com.jme3.material.Material;
4343
import com.jme3.math.Matrix4f;
4444
import com.jme3.renderer.Camera;
45-
import com.jme3.scene.VertexBuffer.Type;
4645
import com.jme3.scene.mesh.MorphTarget;
4746
import com.jme3.util.TempVars;
4847
import com.jme3.util.clone.Cloner;
4948
import com.jme3.util.clone.IdentityCloneFunction;
49+
import com.jme3.vulkan.buffers.GpuBuffer;
50+
import com.jme3.vulkan.commands.CommandBuffer;
51+
import com.jme3.vulkan.material.MatrixTransformMaterial;
5052
import com.jme3.vulkan.material.NewMaterial;
5153
import com.jme3.vulkan.mesh.NewMesh;
52-
import com.jme3.vulkan.scene.NotSpatial;
54+
import com.jme3.vulkan.pipelines.Pipeline;
5355

5456
import java.io.IOException;
57+
import java.nio.FloatBuffer;
5558
import java.util.Queue;
5659
import java.util.logging.Level;
5760
import java.util.logging.Logger;
@@ -72,6 +75,7 @@ public class Geometry extends Spatial {
7275
protected NewMesh mesh;
7376
protected transient int lodLevel = 0;
7477
protected NewMaterial material;
78+
protected MatrixTransformMaterial transforms; // stores the matrices unique to this geometry
7579
/**
7680
* When true, the geometry's transform will not be applied.
7781
*/
@@ -140,6 +144,11 @@ public Geometry(String name, NewMesh mesh) {
140144
this.mesh = mesh;
141145
}
142146

147+
public Geometry(String name, NewMesh mesh, MatrixTransformMaterial transforms) {
148+
this(name, mesh);
149+
this.transforms = transforms;
150+
}
151+
143152
/**
144153
* Create a geometry node with mesh data and material.
145154
*
@@ -152,6 +161,20 @@ public Geometry(String name, NewMesh mesh, NewMaterial material) {
152161
setMaterial(material);
153162
}
154163

164+
public void updateTransformMaterial(Camera cam) {
165+
Matrix4f worldViewProjection = cam.getViewProjectionMatrix().mult(worldTransform.toTransformMatrix());
166+
GpuBuffer matBuffer = transforms.getTransforms().getResource().get();
167+
worldViewProjection.fillFloatBuffer(matBuffer.mapFloats(), true);
168+
matBuffer.unmap();
169+
}
170+
171+
public void draw(CommandBuffer cmd, Pipeline pipeline) {
172+
int offset = transforms.bind(cmd, pipeline);
173+
material.bind(cmd, pipeline, offset);
174+
mesh.bind(cmd);
175+
mesh.draw(cmd);
176+
}
177+
155178
@Override
156179
public boolean checkCulling(Camera cam) {
157180
if (isGrouped()) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.jme3.vulkan.material;
2+
3+
import com.jme3.vulkan.descriptors.Descriptor;
4+
import com.jme3.vulkan.descriptors.DescriptorPool;
5+
import com.jme3.vulkan.material.uniforms.BufferUniform;
6+
import com.jme3.vulkan.shader.ShaderStage;
7+
8+
/**
9+
* Material specifically for storing matrix transforms for a geometry.
10+
*/
11+
public class MatrixTransformMaterial extends NewMaterial {
12+
13+
private final BufferUniform transforms = new BufferUniform("Transforms",
14+
Descriptor.UniformBuffer, 0, ShaderStage.Vertex);
15+
16+
public MatrixTransformMaterial(DescriptorPool pool) {
17+
super(pool);
18+
addSet(transforms);
19+
}
20+
21+
public BufferUniform getTransforms() {
22+
return transforms;
23+
}
24+
25+
}

jme3-core/src/main/java/com/jme3/vulkan/material/NewMaterial.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public void update(CommandBuffer cmd) {
3131
}
3232
}
3333

34-
public void bind(CommandBuffer cmd, Pipeline pipeline) {
35-
bind(cmd, pipeline, 0);
34+
public int bind(CommandBuffer cmd, Pipeline pipeline) {
35+
return bind(cmd, pipeline, 0);
3636
}
3737

38-
public void bind(CommandBuffer cmd, Pipeline pipeline, int offset) {
38+
public int bind(CommandBuffer cmd, Pipeline pipeline, int offset) {
3939
LinkedList<DescriptorSetLayout> availableLayouts = new LinkedList<>();
4040
Collections.addAll(availableLayouts, pipeline.getLayout().getDescriptorSetLayouts());
4141
try (MemoryStack stack = MemoryStack.stackPush()) {
@@ -47,6 +47,7 @@ public void bind(CommandBuffer cmd, Pipeline pipeline, int offset) {
4747
vkCmdBindDescriptorSets(cmd.getBuffer(), pipeline.getBindPoint().getVkEnum(),
4848
pipeline.getLayout().getNativeObject(), offset, setBuf, null);
4949
}
50+
return uniformSets.size(); // number of descriptor slots filled
5051
}
5152

5253
public DescriptorSetLayout[] createLayouts(LogicalDevice<?> device) {

jme3-core/src/main/java/com/jme3/vulkan/material/TestMaterial.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
package com.jme3.vulkan.material;
22

3-
import com.jme3.vulkan.descriptors.Descriptor;
43
import com.jme3.vulkan.descriptors.DescriptorPool;
54
import com.jme3.vulkan.images.VulkanImage;
6-
import com.jme3.vulkan.material.uniforms.BufferUniform;
75
import com.jme3.vulkan.material.uniforms.TextureUniform;
86
import com.jme3.vulkan.shader.ShaderStage;
97

108
public class TestMaterial extends NewMaterial {
119

12-
private final BufferUniform matrices = new BufferUniform(
13-
"Matrices", Descriptor.UniformBuffer, 0, ShaderStage.Vertex);
1410
private final TextureUniform baseColorMap = new TextureUniform(
1511
"BaseColorMap", VulkanImage.Layout.ShaderReadOnlyOptimal, 1, ShaderStage.Fragment);
1612

1713
public TestMaterial(DescriptorPool pool) {
1814
super(pool);
19-
addSet(matrices, baseColorMap);
20-
}
21-
22-
public BufferUniform getMatrices() {
23-
return matrices;
15+
addSet(baseColorMap);
2416
}
2517

2618
public TextureUniform getBaseColorMap() {

jme3-core/src/main/java/com/jme3/vulkan/mesh/AdaptiveMesh.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected enum VertexMode {
3737
private final List<VertexBuffer> vertexBuffers = new ArrayList<>();
3838
protected final List<VersionedResource<? extends GpuBuffer>> indexBuffers = new ArrayList<>();
3939
private GpuBuffer boundIndexBuffer;
40-
protected BoundingVolume volume;
40+
protected BoundingVolume volume = new BoundingBox();
4141
private int vertices;
4242
private CollisionData collisionTree;
4343

jme3-core/src/main/java/com/jme3/vulkan/mesh/MyCustomMesh.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected VersionedResource<? extends GpuBuffer> createDynamicBuffer(MemorySize
8080

8181
@Override
8282
protected VersionedResource<? extends GpuBuffer> createStaticBuffer(MemorySize size) {
83-
return updateStaticBuffers.add(new SingleCommand<>(new StaticBuffer(
83+
return updateStaticBuffers.add(new SingleCommand<>(new BackedStaticBuffer(
8484
device, size, BufferUsage.Vertex, MemoryProp.DeviceLocal, false)));
8585
}
8686

jme3-examples/src/main/java/jme3test/vulkan/VulkanHelperTest.java

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.jme3.app.FlyCamAppState;
44
import com.jme3.app.SimpleApplication;
5-
import com.jme3.math.Matrix4f;
6-
import com.jme3.math.Quaternion;
75
import com.jme3.math.Transform;
86
import com.jme3.math.Vector3f;
97
import com.jme3.scene.Geometry;
@@ -17,7 +15,6 @@
1715
import com.jme3.vulkan.Format;
1816
import com.jme3.vulkan.VulkanInstance;
1917
import com.jme3.vulkan.buffers.BufferUsage;
20-
import com.jme3.vulkan.buffers.GpuBuffer;
2118
import com.jme3.vulkan.buffers.PersistentBuffer;
2219
import com.jme3.vulkan.buffers.StageableBuffer;
2320
import com.jme3.vulkan.commands.CommandBuffer;
@@ -31,6 +28,7 @@
3128
import com.jme3.vulkan.frames.UpdateFrame;
3229
import com.jme3.vulkan.frames.UpdateFrameManager;
3330
import com.jme3.vulkan.images.*;
31+
import com.jme3.vulkan.material.MatrixTransformMaterial;
3432
import com.jme3.vulkan.material.TestMaterial;
3533
import com.jme3.vulkan.memory.MemoryProp;
3634
import com.jme3.vulkan.memory.MemorySize;
@@ -79,32 +77,6 @@ public class VulkanHelperTest extends SimpleApplication implements SwapchainUpda
7977
private boolean swapchainResizeFlag = false;
8078
private boolean applicationStopped = false;
8179

82-
// mesh
83-
private NewMesh mesh;
84-
private final FloatBuffer vertexData = BufferUtils.createFloatBuffer(
85-
-0.5f, -0.5f, 0f, 1f, 0f, 0f, 1f, 0f,
86-
0.5f, -0.5f, 0f, 0f, 1f, 0f, 0f, 0f,
87-
0.5f, 0.5f, 0f, 0f, 0f, 1f, 0f, 1f,
88-
-0.5f, 0.5f, 0f, 1f, 1f, 1f, 1f, 1f,
89-
90-
-0.5f, -0.5f, -0.5f, 1f, 0f, 0f, 1f, 0f,
91-
0.5f, -0.5f, -0.5f, 0f, 1f, 0f, 0f, 0f,
92-
0.5f, 0.5f, -0.5f, 0f, 0f, 1f, 0f, 1f,
93-
-0.5f, 0.5f, -0.5f, 1f, 1f, 1f, 1f, 1f
94-
);
95-
private final IntBuffer indexData = BufferUtils.createIntBuffer(
96-
0, 1, 2, 2, 3, 0,
97-
4, 5, 6, 6, 7, 4);
98-
99-
// material
100-
private TestMaterial material;
101-
102-
// geometry
103-
private final Transform modelTransform = new Transform();
104-
105-
// material
106-
private Texture texture;
107-
10880
// framebuffer
10981
private ImageView depthView;
11082

@@ -261,7 +233,8 @@ public void simpleInitApp() {
261233
try (ImageView.Builder i = imgView.build()) {
262234
i.setAspect(VulkanImage.Aspect.Color);
263235
}
264-
texture = new Texture(device, imgView);
236+
// material
237+
Texture texture = new Texture(device, imgView);
265238
try (Sampler.Builder t = texture.build()) {
266239
t.setMinMagFilters(Filter.Linear, Filter.Linear);
267240
t.setEdgeModes(AddressMode.Repeat);
@@ -273,14 +246,17 @@ public void simpleInitApp() {
273246
sharedData = new BasicCommandBatch();
274247
sharedDataFence = new Fence(device, true);
275248

276-
// mesh
277-
mesh = new MyCustomMesh(device, frames, meshDesc, sharedData,
278-
Vector3f.UNIT_Z, Vector3f.UNIT_Y, 1f, 1f, 0.5f, 0.5f);
249+
TestMaterial material = new TestMaterial(descriptorPool);
250+
material.getBaseColorMap().setResource(new SingleResource<>(texture));
279251

280-
material = new TestMaterial(descriptorPool);
281-
material.getMatrices().setResource(frames.perFrame(n ->
252+
NewMesh m = new MyCustomMesh(device, frames, meshDesc, sharedData,
253+
Vector3f.UNIT_Z, Vector3f.UNIT_Y, 1f, 1f, 0.5f, 0.5f);
254+
MatrixTransformMaterial t = new MatrixTransformMaterial(descriptorPool);
255+
t.getTransforms().setResource(frames.perFrame(n ->
282256
new PersistentBuffer(device, MemorySize.floats(16), BufferUsage.Uniform, false)));
283-
material.getBaseColorMap().setResource(new SingleResource<>(texture));
257+
Geometry geometry = new Geometry("geometry", m, t);
258+
geometry.setMaterial(material);
259+
rootNode.attachChild(geometry);
284260

285261
}
286262

@@ -382,15 +358,10 @@ public void update(UpdateFrameManager frames, float tpf) {
382358
renderManager.setCamera(cam, false);
383359

384360
// update matrix uniform (geometry)
385-
{
386-
// compute geometry states
387-
modelTransform.getRotation().multLocal(new Quaternion().fromAngleAxis(tpf, Vector3f.UNIT_Y));
388-
Matrix4f worldViewProjection = cam.getViewProjectionMatrix().mult(modelTransform.toTransformMatrix());
389-
390-
// update material uniform
391-
GpuBuffer matrixBuffer = material.getMatrices().getResource().get();
392-
worldViewProjection.fillFloatBuffer(matrixBuffer.mapFloats(), true);
393-
matrixBuffer.unmap();
361+
for (Spatial s : rootNode) {
362+
if (s instanceof Geometry) {
363+
((Geometry)s).updateTransformMaterial(cam);
364+
}
394365
}
395366

396367
// update shared data
@@ -436,25 +407,20 @@ public void update(UpdateFrameManager frames, float tpf) {
436407
// run graphics commands via CommandBatch
437408
graphics.run(graphicsCommands, frames.getCurrentFrame());
438409

439-
// material.bind(graphicsCommands, pipeline);
440-
// mesh.bind(graphicsCommands);
441-
// mesh.draw(graphicsCommands);
442-
443410
// draw all geometries in the rootNode
444411
for (Spatial s : rootNode) {
445412
if (s instanceof Geometry) {
446413
Geometry g = (Geometry)s;
447-
g.getMaterial().bind(graphicsCommands, pipeline);
448-
g.getMesh().bind(graphicsCommands);
449-
g.getMesh().draw(graphicsCommands);
414+
g.draw(graphicsCommands, pipeline);
450415
}
451416
}
452417

453418
// material
454419
renderPass.end(graphicsCommands);
455420

456421
// render manager
457-
graphicsCommands.endAndSubmit(new SyncGroup(new Semaphore[] {imageAvailable, perFrameDataFinished, sharedDataFinished}, renderFinished, inFlight));
422+
graphicsCommands.endAndSubmit(new SyncGroup(new Semaphore[]
423+
{imageAvailable, perFrameDataFinished, sharedDataFinished}, renderFinished, inFlight));
458424
swapchain.present(device.getPhysicalDevice().getPresent(), image, renderFinished.toGroupWait());
459425

460426
}

0 commit comments

Comments
 (0)