Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rendering)!: migrate chunk vertex attribute changes #4735

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public ChunkMeshInfo(ChunkMesh mesh) {
if (mesh.hasVertexElements()) {
for (ChunkMesh.RenderType type : ChunkMesh.RenderType.values()) {
final ChunkMesh.VertexElements element = mesh.getVertexElements(type);
vertices += element.finalVertices.limit();
indices += element.finalIndices.limit();
vertices += element.buffer.elements();
indices += element.indices.indices();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,31 @@ protected PersistedData serializeNonNull(ChunkMesh value, PersistedDataSerialize
}
List<PersistedData> data = new ArrayList<>();
for (ChunkMesh.RenderType renderType : ChunkMesh.RenderType.values()) {
data.add(serializer.serialize(asByteBuffer(value.getVertexElements(renderType).finalVertices)));
data.add(serializer.serialize(asByteBuffer(value.getVertexElements(renderType).finalIndices)));

value.getVertexElements(renderType).buffer.writeBuffer(buffer -> {
data.add(serializer.serialize(buffer));
});
value.getVertexElements(renderType).indices.writeBuffer(buffer -> {
data.add(serializer.serialize(buffer));
});
}
return serializer.serialize(data);
}

@Override
public Optional<ChunkMesh> deserialize(PersistedData data) {
List<IntBuffer> asBuffers = new ArrayList<>();
List<ByteBuffer> asBuffers = new ArrayList<>();
for (PersistedData datum : data.getAsArray()) {
IntBuffer buffer = datum.getAsByteBuffer().asIntBuffer();
IntBuffer directBuffer = BufferUtils.createIntBuffer(buffer.limit() * 4);
ByteBuffer buffer = datum.getAsByteBuffer();
ByteBuffer directBuffer = BufferUtils.createByteBuffer(buffer.limit());
directBuffer.put(buffer);
directBuffer.rewind();
asBuffers.add(directBuffer);
}
ChunkMesh result = new ChunkMesh();
for (ChunkMesh.RenderType renderType : ChunkMesh.RenderType.values()) {
result.getVertexElements(renderType).finalVertices = asBuffers.remove(0);
result.getVertexElements(renderType).finalIndices = asBuffers.remove(0);
result.getVertexElements(renderType).buffer.copyBuffer(asBuffers.remove(0));
result.getVertexElements(renderType).indices.copyBuffer(asBuffers.remove(0));
}
result.generateVBOs();
return Optional.of(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public int inSize() {
return inSize;
}

public void copyBuffer(ByteBuffer copyBuffer) {
ensureCapacity(copyBuffer.capacity());
buffer.put(copyBuffer);
}

public void copyBuffer(BufferedResource resource) {
ensureCapacity(resource.inSize);
ByteBuffer copyBuffer = resource.buffer;
Expand All @@ -34,9 +39,15 @@ public void copyBuffer(BufferedResource resource) {
this.inSize = resource.inSize;
}

protected void reserve(int inSize) {
ensureCapacity(inSize);
this.inSize = 0;
protected void reserve(int size) {
if (size > buffer.capacity()) {
int newCap = Math.max(buffer.capacity() << 1, size);
ByteBuffer newBuffer = BufferUtils.createByteBuffer(newCap);
buffer.limit(this.inSize);
buffer.position(0);
newBuffer.put(buffer);
this.buffer = newBuffer;
}
}

protected void allocate(int size) {
Expand All @@ -46,9 +57,9 @@ protected void allocate(int size) {

protected void ensureCapacity(int size) {
if (size > buffer.capacity()) {
int newCap = Math.max(this.inSize << 1, size);
int newCap = Math.max(this.buffer.capacity() << 1, size);
ByteBuffer newBuffer = BufferUtils.createByteBuffer(newCap);
buffer.limit(Math.min(size, this.inSize));
buffer.limit(this.inSize);
buffer.position(0);
newBuffer.put(buffer);
this.buffer = newBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@

public final class GLAttributes {
private GLAttributes() {

}

public static final VertexFloatAttribute FLOAT_1_VERTEX_ATTRIBUTE = new VertexFloatAttribute(new VertexFloatAttribute.AttributeConfiguration() {
@Override
public void write(float value, int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
buffer.putFloat(bufferStart, value);
}

@Override
public float read(int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
return buffer.getFloat(bufferStart);
}
}, TypeMapping.ATTR_FLOAT, 1);

public static final VertexIntegerAttribute INT_1_VERTEX_ATTRIBUTE = new VertexIntegerAttribute(new VertexIntegerAttribute.AttributeConfiguration() {

@Override
Expand All @@ -35,6 +51,23 @@ public int read(int vertIdx, int offset, VertexResource resource) {
}
}, TypeMapping.ATTR_INT, 1);

public static final VertexIntegerAttribute BYTE_1_VERTEX_ATTRIBUTE = new VertexIntegerAttribute(new VertexIntegerAttribute.AttributeConfiguration() {

@Override
public void write(int value, int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
buffer.put(bufferStart, (byte) value);
}

@Override
public int read(int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
return buffer.get(bufferStart);
}
}, TypeMapping.ATTR_BYTE, 1);


public static final VertexAttribute<Vector3fc, Vector3f> VECTOR_3_F_VERTEX_ATTRIBUTE = new VertexAttribute<Vector3fc, Vector3f>(Vector3f.class, new VertexAttribute.AttributeConfiguration<Vector3fc, Vector3f>() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.rendering.assets.mesh.resource;

public class VertexFloatAttribute extends BaseVertexAttribute {
public final VertexFloatAttribute.AttributeConfiguration configuration;

public interface AttributeConfiguration {
void write(float value, int vertIdx, int offset, VertexResource resource);

float read(int vertIdx, int offset, VertexResource resource);
}
/**
* @param mapping maps a primitive to a given supported type.
* @param count the number elements that is described by the target
*/
protected VertexFloatAttribute(VertexFloatAttribute.AttributeConfiguration attributeConfiguration,
TypeMapping mapping, int count) {
super(mapping, count);
this.configuration = attributeConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.rendering.assets.mesh.resource;

public class VertexFloatAttributeBinding extends VertexBinding {
private final VertexFloatAttribute attribute;

public VertexFloatAttributeBinding(VertexResource resource, int offset, VertexFloatAttribute attribute) {
super(resource, offset);
this.attribute = attribute;
}

@Override
public void allocate(int elements) {
resource.allocateElements(elements);
resource.mark();
}

@Override
public void reserve(int vertCount) {
resource.reserveElements(vertCount);
}

public void put(float value) {
resource.ensureElements(this.vertexIndex + 1);
attribute.configuration.write(value, this.vertexIndex, this.offset, resource);
this.vertexIndex++;
this.resource.mark();
}

public void set(int index, float value) {
attribute.configuration.write(value, index, this.offset, resource);
this.resource.mark();
}

public float get(int index) {
return attribute.configuration.read(index, this.offset, resource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package org.terasology.engine.rendering.assets.mesh.resource;

public class VertexIntegerAttribute extends BaseVertexAttribute {
public class VertexIntegerAttribute extends BaseVertexAttribute {
public final VertexIntegerAttribute.AttributeConfiguration configuration;

public interface AttributeConfiguration {
Expand All @@ -16,7 +16,8 @@ public interface AttributeConfiguration {
* @param mapping maps a primitive to a given supported type.
* @param count the number elements that is described by the target
*/
protected VertexIntegerAttribute(VertexIntegerAttribute.AttributeConfiguration attributeConfiguration, TypeMapping mapping, int count) {
protected VertexIntegerAttribute(VertexIntegerAttribute.AttributeConfiguration attributeConfiguration,
TypeMapping mapping, int count) {
super(mapping, count);
this.configuration = attributeConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public VertexIntegerAttributeBinding add(int location, VertexIntegerAttribute at
return result;
}

public VertexFloatAttributeBinding add(int location, VertexFloatAttribute attribute) {
VertexFloatAttributeBinding result = new VertexFloatAttributeBinding(resource, inStride, attribute);
this.definitions.add(new VertexResource.VertexDefinition(location, inStride, attribute));
inStride += attribute.mapping.size * attribute.count;
return result;
}


public VertexResource build() {
resource.setDefinitions(definitions.toArray(new VertexResource.VertexDefinition[]{}));
resource.allocate(0, inStride);
Expand Down
Loading