fixup! Initial data/struct format refactor

This commit is contained in:
Jozufozu 2021-09-14 13:52:52 -07:00
parent 613c933206
commit ee7f6fd5e2
5 changed files with 33 additions and 8 deletions

View file

@ -3,11 +3,26 @@ package com.jozufozu.flywheel.backend.struct;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
/**
* A StructType contains metadata for a specific instance struct that Flywheel can interface with.
* @param <S> The java representation of the instance struct.
*/
public interface StructType<S> {
/**
* @return A new, zeroed instance of the struct.
*/
S create();
/**
* @return The format descriptor of the struct type.
*/
VertexFormat format();
/**
* Create a {@link StructWriter} that will consume instances of S and write them to the given buffer.
*
* @param backing The buffer that the StructWriter will write to.
*/
StructWriter<S> getWriter(VecBuffer backing);
}

View file

@ -1,8 +1,17 @@
package com.jozufozu.flywheel.backend.struct;
/**
* StructWriters can quickly consume many instances of S and write them to some backing buffer.
*/
public interface StructWriter<S> {
/**
* Write the given struct to the backing array.
*/
void write(S struct);
/**
* Seek to the given position. The next write will occur there.
*/
void seek(int pos);
}

View file

@ -5,19 +5,21 @@ import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
/**
* This class copied/adapted from jellysquid's
*
* An unsafe {@link BufferWriter} implementation which uses direct memory operations to enable fast blitting of
* data into memory buffers. Only available on JVMs which support {@link sun.misc.Unsafe}, but generally produces much
* better optimized code than other implementations. The implementation does not check for invalid memory accesses,
* meaning that errors can corrupt process memory.
*/
public abstract class BufferWriterUnsafe<S> extends BufferWriter<S> {
public abstract class UnsafeBufferWriter<S> extends BufferWriter<S> {
/**
* The write pointer into the buffer storage. This is advanced by the vertex stride every time
* {@link BufferWriterUnsafe#advance()} is called.
* {@link UnsafeBufferWriter#advance()} is called.
*/
protected long writePointer;
protected BufferWriterUnsafe(VecBuffer backingBuffer, StructType<S> vertexType) {
protected UnsafeBufferWriter(VecBuffer backingBuffer, StructType<S> vertexType) {
super(backingBuffer, vertexType);
acquireWritePointer();

View file

@ -3,12 +3,11 @@ package com.jozufozu.flywheel.core.materials.model.writer;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.struct.BufferWriterUnsafe;
import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.backend.struct.BufferWriter;
import com.jozufozu.flywheel.core.materials.model.ModelData;
public class UnsafeModelWriter extends BufferWriterUnsafe<ModelData> {
public class UnsafeModelWriter extends UnsafeBufferWriter<ModelData> {
public UnsafeModelWriter(VecBuffer backingBuffer, StructType<ModelData> vertexType) {
super(backingBuffer, vertexType);

View file

@ -3,11 +3,11 @@ package com.jozufozu.flywheel.core.materials.oriented.writer;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.backend.struct.BufferWriterUnsafe;
import com.jozufozu.flywheel.backend.struct.UnsafeBufferWriter;
import com.jozufozu.flywheel.backend.struct.StructType;
import com.jozufozu.flywheel.core.materials.oriented.OrientedData;
public class UnsafeOrientedWriter extends BufferWriterUnsafe<OrientedData> {
public class UnsafeOrientedWriter extends UnsafeBufferWriter<OrientedData> {
public UnsafeOrientedWriter(VecBuffer backingBuffer, StructType<OrientedData> vertexType) {
super(backingBuffer, vertexType);
}