11package com .jme3 .util .struct ;
22
3- import com .jme3 .export .JmeExporter ;
4- import com .jme3 .export .JmeImporter ;
5- import com .jme3 .export .Savable ;
3+ import com .jme3 .export .*;
64import com .jme3 .math .FastMath ;
75import com .jme3 .vulkan .buffers .BufferMapping ;
86
97import java .io .IOException ;
108import java .util .*;
9+ import java .util .logging .Level ;
10+ import java .util .logging .Logger ;
1111
1212/**
1313 * Defines the layout of properties in native memory. In contrast to traditional struct patterns,
2828 */
2929public abstract class Struct <T extends StructField > implements Savable {
3030
31- private static final Map < Class , List < java . lang . reflect . Field >> structFieldCache = new HashMap <>( );
31+ protected static final Logger logger = Logger . getLogger ( Struct . class . getName () );
3232
3333 private final List <T > fields = new LinkedList <>();
3434 protected StructLayout layout ;
@@ -37,10 +37,26 @@ public abstract class Struct <T extends StructField> implements Savable {
3737 private int size , alignment ;
3838
3939 @ Override
40- public void write (JmeExporter ex ) throws IOException {}
40+ public void write (JmeExporter ex ) throws IOException {
41+ if (layout != null ) {
42+ OutputCapsule out = ex .getCapsule (this );
43+ out .write (layout .getIdentifier (), "layoutId" , null );
44+ }
45+ }
4146
4247 @ Override
43- public void read (JmeImporter im ) throws IOException {}
48+ public void read (JmeImporter im ) throws IOException {
49+ InputCapsule in = im .getCapsule (this );
50+ String layoutId = in .readString ("layoutId" , null );
51+ if (layoutId != null ) {
52+ StructLayout l = StructLayout .getLayoutByIdentifier (layoutId );
53+ if (l != null ) {
54+ bind (l );
55+ } else {
56+ logger .log (Level .WARNING , "Layout \" {0}\" is unknown. No layout assigned to struct." , layoutId );
57+ }
58+ }
59+ }
4460
4561 @ SafeVarargs
4662 protected final void addFields (T ... fields ) {
@@ -54,6 +70,9 @@ public void bind(StructLayout layout, BufferMapping mapping, int position) {
5470 }
5571
5672 public void bind (BufferMapping mapping , int position ) {
73+ if (layout == null ) {
74+ bind (StructLayout .packed );
75+ }
5776 this .mapping = mapping ;
5877 this .position = position ;
5978 }
@@ -66,17 +85,32 @@ public void bind(Struct struct) {
6685 * Binds this struct with a layout defined by {@code layout}. If the layout
6786 * is changed, this struct is {@link #computeOffsets() recomputed}.
6887 *
69- * @param <E> struct type to return
7088 * @param layout layout
71- * @return this instance, as a builder convenience
7289 */
73- public < E extends Struct > E bind (StructLayout layout ) {
90+ public void bind (StructLayout layout ) {
7491 if (this .layout == layout ) {
75- return ( E ) this ;
92+ return ;
7693 }
7794 this .layout = layout ;
7895 computeOffsets ();
79- return (E )this ;
96+ }
97+
98+ /**
99+ * Reformats the currently bound buffer region according to the {@code layout},
100+ * if this struct is not already bound to {@code layout}.
101+ *
102+ * @param layout new layout
103+ */
104+ public void reformat (StructLayout layout ) {
105+ if (this .layout != null && this .layout != layout ) {
106+ for (T f : fields ) {
107+ f .get (); // read to alias
108+ }
109+ bind (layout );
110+ for (T f : fields ) {
111+ f .set (); // write from alias
112+ }
113+ }
80114 }
81115
82116 /**
@@ -175,7 +209,7 @@ public Field(String name, T alias) {
175209 @ Override
176210 public int bind (Struct struct , int offset ) {
177211 this .struct = struct ;
178- this .description = struct .getLayout ().getFieldDescription (getType ());
212+ this .description = struct .getLayout ().getFieldDescription (alias . getClass ());
179213 return this .offset = FastMath .toMultipleOf (offset , getAlignment ());
180214 }
181215
@@ -196,21 +230,11 @@ public T alias() {
196230 return alias ;
197231 }
198232
199- @ Override
200- public void setName (String name ) {
201- this .name = name ;
202- }
203-
204233 @ Override
205234 public String getName () {
206235 return name ;
207236 }
208237
209- @ Override
210- public Class getType () {
211- return alias .getClass ();
212- }
213-
214238 @ Override
215239 public int getSize () {
216240 assert description != null : "Struct not bound to a layout: size unknown." ;
0 commit comments