Skip to content

Commit 2d0a0bb

Browse files
committed
experiment with opaque material parameter layouts
1 parent 5670441 commit 2d0a0bb

10 files changed

Lines changed: 144 additions & 40 deletions

File tree

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public <E extends Struct> E bind(StructLayout layout) {
8686
public void computeOffsets() {
8787
this.size = 0;
8888
this.alignment = layout.getMinStructAlignment();
89-
for (T f : fields) if (f.isEnabled()) {
89+
for (T f : fields) {
9090
size = f.bind(this, size) + f.getSize();
9191
alignment = Math.max(alignment, f.getAlignment());
9292
}
@@ -161,7 +161,6 @@ public static class Field <T> implements StructField<T> {
161161
private Struct struct;
162162
private FieldDesc<T> description;
163163
private int offset;
164-
private boolean enabled = true;
165164

166165
public Field(T alias) {
167166
this(null, alias);
@@ -182,14 +181,12 @@ public int bind(Struct struct, int offset) {
182181

183182
@Override
184183
public void set(T value) {
185-
if (!enabled) return;
186184
assert description != null : "Struct not bound: unable to write.";
187185
description.write(struct.getLayout(), struct.getMapping(), struct.getPosition() + offset, value);
188186
}
189187

190188
@Override
191189
public T get() {
192-
if (!enabled) return alias;
193190
assert description != null : "Struct not bound: unable to read.";
194191
return alias = description.read(struct.getLayout(), struct.getMapping(), struct.getPosition() + offset, alias);
195192
}
@@ -209,16 +206,6 @@ public String getName() {
209206
return name;
210207
}
211208

212-
@Override
213-
public void enable(boolean enable) {
214-
this.enabled = enable;
215-
}
216-
217-
@Override
218-
public boolean isEnabled() {
219-
return enabled;
220-
}
221-
222209
@Override
223210
public Class getType() {
224211
return alias.getClass();

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ public interface StructField <T> {
4040
*/
4141
T alias();
4242

43-
/**
44-
* Enables or disables this field. When enabled, the field will be mapped to an offset
45-
* relative to its struct and read/write from that location. When disabled, no data
46-
* will be read or written on {@link #get()} or {@link #set()}.
47-
*
48-
* <p>If changed, the struct should be {@link Struct#computeOffsets() recomputed}.</p>
49-
*
50-
* @param enable true to enable this field
51-
*/
52-
void enable(boolean enable);
53-
5443
/**
5544
* Sets the name of this field. When created inside a {@link Struct}, the name will be
5645
* assigned reflectively if no name is assigned manually.
@@ -90,13 +79,6 @@ public interface StructField <T> {
9079
*/
9180
int getAlignment();
9281

93-
/**
94-
* Returns true if this field is {@link #enable(boolean) enabled}.
95-
*
96-
* @return true if enabled
97-
*/
98-
boolean isEnabled();
99-
10082
/**
10183
* Serializes {@link #alias()} to the proper memory address through
10284
* the bound field description.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.jme3.vulkan.buffers;
22

3-
import com.jme3.vulkan.memory.MemorySize;
4-
53
public interface Mappable {
64

75
}

jme3-core/src/main/java/com/jme3/vulkan/descriptors/DescriptorSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public DescriptorSet(LogicalDevice<?> device, DescriptorPool pool, DescriptorSet
2323
this.object = id;
2424
if (pool.getFlags().contains(DescriptorPool.Create.FreeDescriptorSets)) {
2525
ref = DisposableManager.reference(this);
26-
pool.getNativeReference().addDependent(ref);
26+
pool.getReference().addDependent(ref);
2727
}
2828
}
2929

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.jme3.vulkan.material;
2+
3+
import com.jme3.util.struct.Struct;
4+
import com.jme3.util.struct.StructSequence;
5+
6+
public class MaterialMapping implements StructSequence<> {
7+
8+
public static class StructImpl extends Struct {
9+
10+
}
11+
12+
}

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package com.jme3.vulkan.material;
22

33
import com.jme3.material.RenderState;
4+
import com.jme3.math.ColorRGBA;
5+
import com.jme3.util.struct.Struct;
6+
import com.jme3.util.struct.StructField;
7+
import com.jme3.util.struct.StructMapping;
8+
import com.jme3.util.struct.StructSequence;
49
import com.jme3.vulkan.buffers.BufferMapping;
510
import com.jme3.vulkan.buffers.DirectBufferMapping;
11+
import com.jme3.vulkan.buffers.MappableBuffer;
612
import com.jme3.vulkan.commands.CommandBuffer;
713
import com.jme3.vulkan.descriptors.*;
814
import com.jme3.vulkan.material.shader.ShaderStage;
15+
import com.jme3.vulkan.material.structs.UnshadedParams;
916
import com.jme3.vulkan.material.technique.NewTechnique;
1017
import com.jme3.vulkan.material.technique.PushConstantRange;
1118
import com.jme3.vulkan.material.technique.VulkanTechnique;
19+
import com.jme3.vulkan.material.test.ShaderParameters;
20+
import com.jme3.vulkan.material.uniforms.StructUniform;
1221
import com.jme3.vulkan.material.uniforms.Uniform;
1322
import com.jme3.vulkan.material.uniforms.VulkanUniform;
1423
import com.jme3.vulkan.pipeline.Pipeline;
@@ -108,6 +117,109 @@ public RenderState getAdditionalRenderState() {
108117
return renderState;
109118
}
110119

120+
private final Map<StructUniform, StructMapping> paramMappings = new IdentityHashMap<>();
121+
122+
public StructField<ColorRGBA> getColor() {
123+
StructUniform<MappableBuffer, UnshadedParams> s = getUniform("Parameters");
124+
return new FieldMapping<>(s, s.getStruct().color);
125+
}
126+
127+
public void flushParameters() {
128+
for (StructMapping m : paramMappings.values()) {
129+
m.close();
130+
}
131+
paramMappings.clear();
132+
}
133+
134+
private void mapUniformBuffer() {}
135+
136+
public MyParams mapParameters() {
137+
return new MyParams();
138+
}
139+
140+
public class MyParams implements ShaderParameters {
141+
142+
private final Map<MappableBuffer, StructMapping> paramMappings = new IdentityHashMap<>();
143+
144+
@Override
145+
public void close() {
146+
for (StructMapping m : paramMappings.values()) {
147+
m.close();
148+
}
149+
}
150+
151+
public StructField<ColorRGBA> getColor() {
152+
StructUniform<MappableBuffer, UnshadedParams> s = getUniform("Parameters");
153+
paramMappings.computeIfAbsent(s.get(), k -> k.mapStruct(s.getStruct()));
154+
return s.getStruct().color;
155+
}
156+
157+
}
158+
159+
private class FieldMapping <T> implements StructField<T> {
160+
161+
private final StructUniform<MappableBuffer, ? extends Struct> uniform;
162+
private final StructField<T> delegate;
163+
private StructMapping mapping;
164+
165+
public FieldMapping(StructUniform<MappableBuffer, ? extends Struct> uniform, StructField<T> delegate) {
166+
this.uniform = uniform;
167+
this.delegate = delegate;
168+
}
169+
170+
@Override
171+
public int bind(Struct struct, int offset) {
172+
return delegate.bind(struct, offset);
173+
}
174+
175+
@Override
176+
public void set(T value) {
177+
if (mapping == null) {
178+
mapping = paramMappings.computeIfAbsent(uniform, StructUniform::map);
179+
}
180+
delegate.set(value);
181+
}
182+
183+
@Override
184+
public T get() {
185+
if (mapping == null) {
186+
mapping = paramMappings.computeIfAbsent(uniform, StructUniform::map);
187+
}
188+
return delegate.get();
189+
}
190+
191+
@Override
192+
public T alias() {
193+
return delegate.alias();
194+
}
195+
196+
@Override
197+
public void setName(String name) {
198+
delegate.setName(name);
199+
}
200+
201+
@Override
202+
public String getName() {
203+
return delegate.getName();
204+
}
205+
206+
@Override
207+
public Class getType() {
208+
return delegate.getType();
209+
}
210+
211+
@Override
212+
public int getSize() {
213+
return delegate.getSize();
214+
}
215+
216+
@Override
217+
public int getAlignment() {
218+
return delegate.getAlignment();
219+
}
220+
221+
}
222+
111223
protected static class CachedDescriptorSet {
112224

113225
private final DescriptorSet set;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.jme3.vulkan.material.test;
2+
3+
public interface ShaderParameters extends AutoCloseable {
4+
5+
@Override
6+
void close();
7+
8+
}

jme3-core/src/main/java/com/jme3/vulkan/material/uniforms/StructUniform.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ public StructMapping<S> map() {
2626
return get().mapAllStructs(struct);
2727
}
2828

29+
public S getStruct() {
30+
return struct;
31+
}
32+
2933
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jme3.export.*;
44
import com.jme3.util.struct.*;
5+
import com.jme3.vulkan.buffers.Mappable;
56
import com.jme3.vulkan.buffers.MappableBuffer;
67
import com.jme3.vulkan.tmp.Final;
78
import com.jme3.vulkan.tmp.FinalWriter;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.jme3.backend.SimpleVulkanEngine;
66
import com.jme3.material.Material;
77
import com.jme3.math.ColorRGBA;
8+
import com.jme3.math.Vector3f;
9+
import com.jme3.math.Vector4f;
810
import com.jme3.renderer.Camera;
911
import com.jme3.renderer.RenderManager;
1012
import com.jme3.renderer.ViewPort;
@@ -17,6 +19,7 @@
1719
import com.jme3.util.struct.StructMapping;
1820
import com.jme3.vulkan.commands.StandardRenderSettings;
1921
import com.jme3.vulkan.material.structs.UnshadedParams;
22+
import com.jme3.vulkan.mesh.attributes.AttributeMapping;
2023
import com.jme3.vulkan.render.bucket.GeometryBucket;
2124
import com.sun.tools.javac.util.List;
2225
import org.graalvm.compiler.lir.amd64.AMD64BinaryConsumer;
@@ -37,11 +40,8 @@ public void simpleInitApp() {
3740

3841
Geometry g = new Geometry("geom_jme4", new Box(1f, 1f, 1f));
3942
Material mat = engine.createMaterial("Common/MatDefs/Misc/Unshaded.j3md");
40-
try (StructMapping<UnshadedParams> m = mat.mapStruct("Parameters")) {
41-
UnshadedParams p = m.get();
42-
p.color.set(ColorRGBA.Blue);
43-
p.glowColor.set(ColorRGBA.Blue.mult(0.2f));
44-
p.vertexColor.set(true);
43+
try (MyParams p = mat.mapParameters()) {
44+
p.getColor().set(ColorRGBA.Blue);
4545
}
4646
g.setMaterial(mat);
4747
rootNode.attachChild(g);

0 commit comments

Comments
 (0)