|
1 | 1 | package com.jme3.vulkan.material; |
2 | 2 |
|
3 | 3 | 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; |
4 | 9 | import com.jme3.vulkan.buffers.BufferMapping; |
5 | 10 | import com.jme3.vulkan.buffers.DirectBufferMapping; |
| 11 | +import com.jme3.vulkan.buffers.MappableBuffer; |
6 | 12 | import com.jme3.vulkan.commands.CommandBuffer; |
7 | 13 | import com.jme3.vulkan.descriptors.*; |
8 | 14 | import com.jme3.vulkan.material.shader.ShaderStage; |
| 15 | +import com.jme3.vulkan.material.structs.UnshadedParams; |
9 | 16 | import com.jme3.vulkan.material.technique.NewTechnique; |
10 | 17 | import com.jme3.vulkan.material.technique.PushConstantRange; |
11 | 18 | import com.jme3.vulkan.material.technique.VulkanTechnique; |
| 19 | +import com.jme3.vulkan.material.test.ShaderParameters; |
| 20 | +import com.jme3.vulkan.material.uniforms.StructUniform; |
12 | 21 | import com.jme3.vulkan.material.uniforms.Uniform; |
13 | 22 | import com.jme3.vulkan.material.uniforms.VulkanUniform; |
14 | 23 | import com.jme3.vulkan.pipeline.Pipeline; |
@@ -108,6 +117,109 @@ public RenderState getAdditionalRenderState() { |
108 | 117 | return renderState; |
109 | 118 | } |
110 | 119 |
|
| 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 | + |
111 | 223 | protected static class CachedDescriptorSet { |
112 | 224 |
|
113 | 225 | private final DescriptorSet set; |
|
0 commit comments