Skip to content

Commit 218bd55

Browse files
committed
experiment with material parameter handling
1 parent 2d0a0bb commit 218bd55

47 files changed

Lines changed: 723 additions & 463 deletions

Some content is hidden

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

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33

44
import com.jme3.material.Material;
55
import com.jme3.renderer.ViewPort;
6-
import com.jme3.texture.Texture;
7-
import com.jme3.util.struct.Struct;
8-
import com.jme3.util.struct.StructLayout;
9-
import com.jme3.vulkan.buffers.saving.BufferAllocator;
10-
import com.jme3.vulkan.material.uniforms.Uniform;
116

127
import java.util.Collection;
138

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

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@
3232
package com.jme3.material;
3333

3434
import com.jme3.scene.Geometry;
35-
import com.jme3.util.struct.Struct;
36-
import com.jme3.util.struct.StructMapping;
37-
import com.jme3.vulkan.buffers.MappableBuffer;
38-
import com.jme3.vulkan.material.technique.NewTechnique;
39-
import com.jme3.vulkan.material.uniforms.BufferUniform;
40-
import com.jme3.vulkan.material.uniforms.StructUniform;
41-
import com.jme3.vulkan.material.uniforms.Uniform;
35+
import com.jme3.vulkan.material.experimental.ShaderInterface;
4236

4337
/**
4438
* <code>Material</code> describes the rendering style for a given
@@ -53,49 +47,14 @@
5347
*/
5448
public interface Material {
5549

56-
void selectTechnique(String name);
50+
void setParameter(String name, Object value);
5751

58-
void setTechnique(String name, NewTechnique technique);
52+
<P> P getParameter(String name);
5953

60-
NewTechnique getActiveTechnique();
54+
void enableInterface(Class<? extends ShaderInterface> interfaceType, boolean enable);
6155

62-
NewTechnique getTechnique(String name);
63-
64-
<T extends Uniform> T setUniform(String name, T uniform);
65-
66-
<T extends Uniform> T getUniform(String name);
56+
boolean isInterfaceEnabled(Class<? extends ShaderInterface> interfaceType);
6757

6858
RenderState getAdditionalRenderState();
6959

70-
default int getSortId() {
71-
return 0;
72-
}
73-
74-
default <T extends Uniform> T getUniform(String name, Class<T> type) {
75-
return getUniform(name);
76-
}
77-
78-
default void set(String name, Object value) {
79-
getUniform(name).set(value);
80-
}
81-
82-
default <T> T get(String name) {
83-
Uniform<T> u = getUniform(name);
84-
return u.get();
85-
}
86-
87-
default <T> T get(String name, Class<T> type) {
88-
return get(name);
89-
}
90-
91-
default <T extends Struct> StructMapping<T> mapStruct(String name, T struct) {
92-
BufferUniform u = getUniform(name);
93-
return u.map(struct);
94-
}
95-
96-
default <T extends Struct> StructMapping<T> mapStruct(String name) {
97-
StructUniform<MappableBuffer, T> u = getUniform(name);
98-
return u.map();
99-
}
100-
10160
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.jme3.util;
2+
3+
public interface SafeCloseable extends AutoCloseable {
4+
5+
@Override
6+
void close();
7+
8+
}

jme3-core/src/main/java/com/jme3/util/struct/Struct.java

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package 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.*;
64
import com.jme3.math.FastMath;
75
import com.jme3.vulkan.buffers.BufferMapping;
86

97
import java.io.IOException;
108
import 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,
@@ -28,7 +28,7 @@
2828
*/
2929
public 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.";

jme3-core/src/main/java/com/jme3/util/struct/StructField.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.jme3.util.struct;
22

3+
import java.util.Objects;
4+
35
/**
46
* Struct field member that serializes and deserializes values to native memory
57
* relative to the bound struct's memory address.
@@ -40,29 +42,13 @@ public interface StructField <T> {
4042
*/
4143
T alias();
4244

43-
/**
44-
* Sets the name of this field. When created inside a {@link Struct}, the name will be
45-
* assigned reflectively if no name is assigned manually.
46-
*
47-
* @param name field name
48-
*/
49-
void setName(String name);
50-
5145
/**
5246
* Gets the name of this field.
5347
*
5448
* @return name of this field
5549
*/
5650
String getName();
5751

58-
/**
59-
* Gets the object type represented by this field. This is used for selecting
60-
* the field description to bind to this field.
61-
*
62-
* @return object type
63-
*/
64-
Class getType();
65-
6652
/**
6753
* Gets the size in bytes of this field. The managing struct
6854
* must be bound.
@@ -95,4 +81,13 @@ default String requireName() {
9581
return n;
9682
}
9783

84+
default boolean compareAndSet(T value) {
85+
T current = get();
86+
if (!Objects.equals(current, value)) {
87+
set(value);
88+
return true;
89+
}
90+
return false;
91+
}
92+
9893
}

0 commit comments

Comments
 (0)