8080
8181public 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 );
0 commit comments