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

feat: add vertex attribute bindings for short and byte #4906

Merged
merged 3 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -12,10 +12,12 @@
import org.junit.Test;
import org.terasology.engine.rendering.assets.mesh.resource.GLAttributes;
import org.terasology.engine.rendering.assets.mesh.resource.VertexAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexByteAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexFloatAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexIntegerAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexResource;
import org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder;
import org.terasology.engine.rendering.assets.mesh.resource.VertexShortAttributeBinding;
import org.terasology.nui.Color;
import org.terasology.nui.Colorc;

Expand Down Expand Up @@ -74,25 +76,47 @@ public void testIntBinding() {
@Test
public void testByteBinding() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexIntegerAttributeBinding a1 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
VertexByteAttributeBinding a1 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();

a1.put(10);
a1.put(150);
a1.put(300);
a1.put(100);
a1.put((byte) 10);
a1.put((byte) 150);
a1.put((byte) 100);
a1.put((byte) 100);

assertEquals(4, a1.getPosition());
resource.writeBuffer(buffer -> {
assertEquals(4 * Byte.BYTES, buffer.limit());

assertEquals(10, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 0)));
assertEquals(150, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 1)));
assertEquals(255, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 2)));
assertEquals(100, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 2)));
assertEquals(100, Byte.toUnsignedInt(buffer.get(Byte.BYTES * 3)));
});
}

@Test
public void testShortBinding() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexShortAttributeBinding a1 = builder.add(0, GLAttributes.SHORT_1_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();

a1.put((short) 10);
a1.put((short) 150);
a1.put((short) 100);
a1.put((short) 100);

assertEquals(4, a1.getPosition());
resource.writeBuffer(buffer -> {
assertEquals(4 * Short.BYTES, buffer.limit());

assertEquals(10, Short.toUnsignedInt(buffer.getShort(Short.BYTES * 0)));
assertEquals(150, Short.toUnsignedInt(buffer.getShort(Short.BYTES * 1)));
assertEquals(100, Short.toUnsignedInt(buffer.getShort(Short.BYTES * 2)));
assertEquals(100, Short.toUnsignedInt(buffer.getShort(Short.BYTES * 3)));
});
}

@Test
public void testVector3fBinding() {
VertexResourceBuilder builder = new VertexResourceBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

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

import com.google.common.primitives.UnsignedBytes;
import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.junit.Test;
import org.terasology.engine.rendering.assets.mesh.resource.GLAttributes;
import org.terasology.engine.rendering.assets.mesh.resource.VertexAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexByteAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexIntegerAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexResource;
import org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder;
Expand Down Expand Up @@ -53,7 +55,7 @@ public void testReserveVertexResource() {
public void testAllocation() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexAttributeBinding<Vector3fc, Vector3f> a1 = builder.add(0, GLAttributes.VECTOR_3_F_VERTEX_ATTRIBUTE);
VertexIntegerAttributeBinding a2 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
VertexByteAttributeBinding a2 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();
a1.allocate(10);
int stride = (Float.BYTES * 3) + Byte.BYTES;
Expand All @@ -67,12 +69,12 @@ public void testAllocation() {
public void testInterleave() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexAttributeBinding<Vector3fc, Vector3f> a1 = builder.add(0, GLAttributes.VECTOR_3_F_VERTEX_ATTRIBUTE);
VertexIntegerAttributeBinding a2 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
VertexByteAttributeBinding a2 = builder.add(0, GLAttributes.BYTE_1_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();

a1.put(new Vector3f(10, 0, -4));
a2.put(2);
a2.put(10);
a2.put(UnsignedBytes.checkedCast(2));
a2.put(UnsignedBytes.checkedCast(10));

assertEquals(2, a2.getPosition());
assertEquals(1, a1.getPosition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

import org.joml.Math;
import org.joml.Vector2f;
import org.joml.Vector2fc;
import org.joml.Vector3f;
Expand Down Expand Up @@ -55,18 +54,35 @@ 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() {

public static final VertexShortAttribute SHORT_1_VERTEX_ATTRIBUTE =
new VertexShortAttribute(new VertexShortAttribute.AttributeConfiguration() {
@Override
public void write(short value, int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
buffer.putShort(bufferStart, value);
}

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

public static final VertexByteAttribute BYTE_1_VERTEX_ATTRIBUTE =
new VertexByteAttribute(new VertexByteAttribute.AttributeConfiguration() {
@Override
public void write(int value, int vertIdx, int offset, VertexResource resource) {
public void write(byte value, int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
buffer.put(bufferStart, ((byte) Math.clamp(0, 255, value)));
buffer.put(bufferStart, value);
}

@Override
public int read(int vertIdx, int offset, VertexResource resource) {
public byte read(int vertIdx, int offset, VertexResource resource) {
int bufferStart = vertIdx * resource.inStride() + offset;
ByteBuffer buffer = resource.buffer();
return buffer.get(bufferStart);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

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

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

/**
* @param mapping maps a primitive to a given supported type.
* @param count the number elements that is described by the target
*/
protected VertexByteAttribute(VertexByteAttribute.AttributeConfiguration attributeConfiguration,
TypeMapping mapping, int count) {
super(mapping, count);
this.configuration = attributeConfiguration;
}

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

byte read(int vertIdx, int offset, VertexResource resource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

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

public class VertexByteAttributeBinding extends VertexBinding {

private final VertexByteAttribute attribute;

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

public int elements() {
return getResource().elements();
}

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

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

public void put(byte 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, byte value) {
attribute.configuration.write(value, index, this.offset, resource);
this.resource.mark();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public VertexFloatAttributeBinding add(int location, VertexFloatAttribute attrib
return result;
}

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

public VertexShortAttributeBinding add(int location, VertexShortAttribute attribute) {
VertexShortAttributeBinding result = new VertexShortAttributeBinding(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[]{}));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

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

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

/**
* @param mapping maps a primitive to a given supported type.
* @param count the number elements that is described by the target
*/
protected VertexShortAttribute(VertexShortAttribute.AttributeConfiguration attributeConfiguration,
TypeMapping mapping, int count) {
super(mapping, count);
this.configuration = attributeConfiguration;
}

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

short read(int vertIdx, int offset, VertexResource resource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

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

public class VertexShortAttributeBinding extends VertexBinding {

private final VertexShortAttribute attribute;

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

public int elements() {
return getResource().elements();
}

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

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

public void put(short 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, short value) {
attribute.configuration.write(value, index, this.offset, resource);
this.resource.mark();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.terasology.engine.rendering.assets.mesh.resource.GLAttributes;
import org.terasology.engine.rendering.assets.mesh.resource.IndexResource;
import org.terasology.engine.rendering.assets.mesh.resource.VertexAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexByteAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexFloatAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexIntegerAttributeBinding;
import org.terasology.engine.rendering.assets.mesh.resource.VertexResource;
import org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder;
import org.terasology.gestalt.module.sandbox.API;
Expand Down Expand Up @@ -321,8 +321,8 @@ public static class VertexElements {

public final VertexAttributeBinding<Colorc, Color> color;

public final VertexIntegerAttributeBinding flags;
public final VertexIntegerAttributeBinding frames;
public final VertexByteAttributeBinding flags;
public final VertexByteAttributeBinding frames;

public final VertexFloatAttributeBinding sunlight; // this could be changed to a single byte
public final VertexFloatAttributeBinding blockLight; // this could be changed to a single byte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public void appendTo(ChunkMesh chunk, ChunkView chunkView, int offsetX, int offs
elements.color.put(colorOffset);
elements.position.put(pos.set(vertices[vIdx]).add(offsetX, offsetY, offsetZ));
elements.normals.put(normals[vIdx]);
elements.flags.put(flags.getValue());
elements.frames.put(texFrames - 1);
elements.flags.put((byte) (flags.getValue()));
elements.frames.put((byte) (texFrames - 1));
Comment on lines +97 to +98
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can verify this by starting CoreWorlds and giving a lava block and verifying the animations play correctly.

float[] lightingData = calcLightingValuesForVertexPos(chunkView, vertices[vIdx].add(offsetX, offsetY, offsetZ,
new Vector3f()), normals[vIdx]);
elements.sunlight.put(lightingData[0]);
Expand Down