Vertex pulling #48

Manually merged
embeddedt merged 8 commits from vertex-pulling into stonecutter 2026-10-30 15:00:42 -04:00
17 changed files with 202 additions and 95 deletions
Showing only changes of commit e4c6c2be77 - Show all commits

No files matched your search

@@ -17,6 +17,7 @@ import org.embeddedt.embeddium.impl.render.chunk.multidraw.BatchAssembler;
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderInterface;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import org.embeddedt.embeddium.impl.render.viewport.CameraTransform;
import java.util.Iterator;
@@ -77,7 +78,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
Iterator<ChunkRenderList> iterator = renderLists.iterator(renderPass.isReverseOrder());
this.currentRenderPass = renderPass;
this.currentVertexFormat = this.renderPassConfiguration.getVertexTypeForPass(this.currentRenderPass).getVertexFormat();
this.currentVertexFormat = ChunkVertexLayout.requireAttributes(this.renderPassConfiguration.getVertexTypeForPass(this.currentRenderPass).getLayout());
this.configureShaderInterface(shader);
@@ -11,6 +11,7 @@ import org.embeddedt.embeddium.impl.render.chunk.fog.FogService;
import org.embeddedt.embeddium.impl.render.chunk.shader.*;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import org.embeddedt.embeddium.impl.render.shader.ShaderLoader;
import org.jetbrains.annotations.Nullable;
@@ -74,7 +75,7 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer {
var builder = GlProgram.builder("sodium:chunk_shader");
loadedShaders.forEach(builder::attachShader);
int i = 0;
for (var attr : options.pass().vertexType().getVertexFormat().getAttributes()) {
for (var attr : ChunkVertexLayout.requireAttributes(options.pass().vertexType().getLayout()).getAttributes()) {
builder.bindAttribute(attr.getName(), i++);
}
if (!this.enableLegacyGLPatches) {
@@ -56,7 +56,7 @@ public final class ChunkBuildBuffers {
private ChunkModelBuilder createBuilder(TerrainRenderPass pass) {
var vertexType = this.renderPassConfiguration.getVertexTypeForPass(pass);
var builder = new BakedChunkModelBuilder(vertexType.createEncoder(), vertexType.getVertexFormat().getStride(), pass);
var builder = new BakedChunkModelBuilder(vertexType.createEncoder(), pass);
Objects.requireNonNull(renderData, "Buffers have not been started");
builder.begin(renderData, sectionIndex);
this.builders.put(pass, builder);
@@ -112,7 +112,8 @@ public final class ChunkBuildBuffers {
var vertexType = renderPassConfiguration.getVertexTypeForPass(pass);
var primitiveType = renderPassConfiguration.getPrimitiveTypeForPass(pass);
var mergedBuffer = new NativeBuffer(vertexCount * vertexType.getVertexFormat().getStride());
int numPrimitives = vertexCount / primitiveType.getVerticesPerPrimitive();
var mergedBuffer = new NativeBuffer(numPrimitives * vertexType.getLayout().bytesPerPrimitive(primitiveType));
var mergedBufferBuilder = mergedBuffer.getDirectBuffer();
for (var buffer : vertexBuffers) {
@@ -124,7 +125,6 @@ public final class ChunkBuildBuffers {
NativeBuffer mergedIndexBuffer;
if (pass.isSorted()) {
int numPrimitives = vertexCount / primitiveType.getVerticesPerPrimitive();
// Generate the canonical index buffer
mergedIndexBuffer = new NativeBuffer(primitiveType.getIndexBufferSize(numPrimitives));
@@ -17,11 +17,14 @@ public class BakedChunkModelBuilder implements ChunkModelBuilder {
private BuiltRenderSectionData renderData;
public BakedChunkModelBuilder(ChunkVertexEncoder encoder, int stride, TerrainRenderPass pass) {
public BakedChunkModelBuilder(ChunkVertexEncoder encoder, TerrainRenderPass pass) {
var vertexBuffers = new ChunkMeshBufferBuilder[ModelQuadFacing.COUNT];
var primitiveType = pass.primitiveType();
int bytesPerPrimitive = pass.vertexType().getLayout().bytesPerPrimitive(primitiveType);
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
vertexBuffers[facing] = new ChunkMeshBufferBuilder(encoder, stride, 64 * 1024, pass.isSorted() && facing == ModelQuadFacing.UNASSIGNED.ordinal());
vertexBuffers[facing] = new ChunkMeshBufferBuilder(encoder, bytesPerPrimitive, primitiveType.getVerticesPerPrimitive(),
64 * 1024, pass.isSorted() && facing == ModelQuadFacing.UNASSIGNED.ordinal());
}
this.encoder = encoder;
@@ -162,15 +162,17 @@ public class RenderRegion {
var storage = this.sectionRenderData.get(pass);
if (storage == null) {
int stride = renderPassConfiguration.getVertexTypeForPass(pass).getVertexFormat().getStride();
var primitiveType = renderPassConfiguration.getPrimitiveTypeForPass(pass);
int bytesPerPrimitive = renderPassConfiguration.getVertexTypeForPass(pass).getLayout().bytesPerPrimitive(primitiveType);
if (this.commonVertexStride % stride != 0) {
throw new IllegalStateException("Pass " + pass + " uses a vertex stride of " + stride
if (this.commonVertexStride % bytesPerPrimitive != 0) {
throw new IllegalStateException("Pass " + pass + " uses " + bytesPerPrimitive + " bytes per primitive"
+ ", which does not divide the region's common vertex stride of " + this.commonVertexStride
+ "; its vertex format was not declared by the render pass configuration");
}
this.sectionRenderData.put(pass, storage = new SectionRenderDataStorage(renderPassConfiguration.getPrimitiveTypeForPass(pass), pass.isSorted(), this.commonVertexStride / stride));
int verticesPerArenaElement = (this.commonVertexStride / bytesPerPrimitive) * primitiveType.getVerticesPerPrimitive();
this.sectionRenderData.put(pass, storage = new SectionRenderDataStorage(primitiveType, pass.isSorted(), verticesPerArenaElement));
this.passSetUpdateCount++;
}
@@ -45,7 +45,7 @@ public class RenderRegionManager {
*/
private static int computeCommonVertexStride(RenderPassConfiguration<?> renderPassConfiguration) {
return renderPassConfiguration.getAllKnownRenderPasses()
.mapToInt(pass -> pass.vertexType().getVertexFormat().getStride() * pass.primitiveType().getVerticesPerPrimitive())
.mapToInt(pass -> pass.vertexType().getLayout().bytesPerPrimitive(pass.primitiveType()))
.reduce(1, MathUtil::lcm);
}
@@ -5,9 +5,9 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import lombok.experimental.Accessors;
import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveType;
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
import org.jetbrains.annotations.NotNull;
@@ -57,7 +57,7 @@ public class TerrainRenderPass {
private final @NotNull ChunkPrimitiveType primitiveType;
private final @NotNull ChunkVertexType vertexType;
public record TessellationKey(GlVertexFormat vertexFormat, ChunkPrimitiveType primitiveType, boolean sorted) {}
public record TessellationKey(ChunkVertexLayout vertexLayout, ChunkPrimitiveType primitiveType, boolean sorted) {}
@Getter
@EqualsAndHashCode.Exclude
@@ -89,7 +89,7 @@ public class TerrainRenderPass {
this.hasNoLightmap = hasNoLightmap;
this.primitiveType = primitiveType;
this.vertexType = vertexType;
this.tessellationKey = new TessellationKey(vertexType.getVertexFormat(), primitiveType, useTranslucencySorting);
this.tessellationKey = new TessellationKey(vertexType.getLayout(), primitiveType, useTranslucencySorting);
this.extraDefines = Map.copyOf(extraDefines);
}
@@ -10,7 +10,8 @@ import java.nio.ByteBuffer;
public class ChunkMeshBufferBuilder {
private final ChunkVertexEncoder encoder;
private final int stride;
private final int bytesPerPrimitive;
private final int verticesPerPrimitive;
private final int initialCapacity;
private final TranslucentQuadRecorder analyzer;
@@ -20,9 +21,10 @@ public class ChunkMeshBufferBuilder {
private int capacity;
private int sectionIndex;
public ChunkMeshBufferBuilder(ChunkVertexEncoder encoder, int stride, int initialCapacity, boolean collectSortState) {
public ChunkMeshBufferBuilder(ChunkVertexEncoder encoder, int bytesPerPrimitive, int verticesPerPrimitive, int initialCapacity, boolean collectSortState) {
this.encoder = encoder;
this.stride = stride;
this.bytesPerPrimitive = bytesPerPrimitive;
this.verticesPerPrimitive = verticesPerPrimitive;
this.buffer = null;
@@ -33,18 +35,21 @@ public class ChunkMeshBufferBuilder {
}
public void push(ChunkVertexEncoder.Vertex[] vertices, Material material) {
if (vertices.length != this.verticesPerPrimitive) {
throw new IllegalArgumentException("Expected one primitive of " + this.verticesPerPrimitive + " vertices, got " + vertices.length);
}
if (this.encoder.supportsBilinearCorrection()) {
postprocessVertices(vertices);
}
var vertexStart = this.count * this.stride;
var vertexSize = vertices.length * this.stride;
var primitiveStart = this.byteSize();
if (vertexStart + vertexSize >= this.capacity) {
this.grow(vertexSize);
if (primitiveStart + this.bytesPerPrimitive >= this.capacity) {
this.grow(this.bytesPerPrimitive);
}
long ptr = LWJGL.memAddress(this.buffer, vertexStart);
long ptr = LWJGL.memAddress(this.buffer, primitiveStart);
if (this.analyzer != null) {
for (ChunkVertexEncoder.Vertex vertex : vertices) {
@@ -52,9 +57,7 @@ public class ChunkMeshBufferBuilder {
}
}
for (ChunkVertexEncoder.Vertex vertex : vertices) {
ptr = this.encoder.write(ptr, material, vertex, this.sectionIndex);
}
this.encoder.writePrimitive(ptr, material, vertices, this.sectionIndex);
this.count += vertices.length;
}
@@ -137,10 +140,14 @@ public class ChunkMeshBufferBuilder {
throw new IllegalStateException("No vertex data in buffer");
}
return LWJGL.memSlice(this.buffer, 0, this.stride * this.count);
return LWJGL.memSlice(this.buffer, 0, this.byteSize());
}
public int count() {
return this.count;
}
private int byteSize() {
return (this.count / this.verticesPerPrimitive) * this.bytesPerPrimitive;
}
}
@@ -2,8 +2,15 @@ package org.embeddedt.embeddium.impl.render.chunk.vertex.format;
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
@FunctionalInterface
public interface ChunkVertexEncoder {
long write(long ptr, Material material, Vertex vertex, int sectionIndex);
/**
* Writes one primitive starting at {@code ptr}.
*
* @return the pointer just past the primitive, which is {@code ptr} plus the layout's
* {@link ChunkVertexLayout#bytesPerPrimitive bytesPerPrimitive}
*/
long writePrimitive(long ptr, Material material, Vertex[] vertices, int sectionIndex);
default boolean supportsBilinearCorrection() {
return true;
@@ -0,0 +1,58 @@
package org.embeddedt.embeddium.impl.render.chunk.vertex.format;
import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveType;
/**
* Describes how the shader fetches a vertex type's data, and how much space one primitive takes in the geometry buffer.
*/
public sealed interface ChunkVertexLayout permits ChunkVertexLayout.Attributes, ChunkVertexLayout.Pulled {
/**
* {@return the number of bytes one primitive of the given type occupies in the geometry buffer}
*/
int bytesPerPrimitive(ChunkPrimitiveType primitiveType);
/**
* {@return the attribute format of the given layout}
* @throws UnsupportedOperationException if the layout is not fetched through vertex attributes
*/
static GlVertexFormat requireAttributes(ChunkVertexLayout layout) {
if (layout instanceof Attributes attributes) {
return attributes.format();
}
throw new UnsupportedOperationException("Vertex pulling is not supported: " + layout);
}
/**
* Vertices fetched through vertex attributes.
*/
record Attributes(GlVertexFormat format) implements ChunkVertexLayout {
@Override
public int bytesPerPrimitive(ChunkPrimitiveType primitiveType) {
return this.format.getStride() * primitiveType.getVerticesPerPrimitive();
}
}
/**
* One record per primitive, read by the vertex shader from a texture buffer using {@code gl_VertexID}. The layout
* within the record is defined by the vertex type's GLSL decoder.
*/
record Pulled(int verticesPerRecord, int bytesPerRecord) implements ChunkVertexLayout {
public Pulled {
if (bytesPerRecord % 4 != 0) {
throw new IllegalArgumentException("Records are fetched as 32-bit words, but are " + bytesPerRecord + " bytes");
}
}
@Override
public int bytesPerPrimitive(ChunkPrimitiveType primitiveType) {
if (primitiveType.getVerticesPerPrimitive() != this.verticesPerRecord) {
throw new IllegalArgumentException("Layout holds " + this.verticesPerRecord + " vertices per record, but "
+ primitiveType + " has " + primitiveType.getVerticesPerPrimitive());
}
return this.bytesPerRecord;
}
}
}
@@ -1,6 +1,5 @@
package org.embeddedt.embeddium.impl.render.chunk.vertex.format;
import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.HashMap;
@@ -22,7 +21,7 @@ public interface ChunkVertexType {
*/
float getTextureScale();
GlVertexFormat getVertexFormat();
ChunkVertexLayout getLayout();
/**
* {@return a newly constructed instance of a vertex encoder for the given vertex type}
@@ -4,6 +4,7 @@ import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeFormat;
import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
@@ -21,6 +22,8 @@ public class CompactChunkVertex implements ChunkVertexType {
public static final int STRIDE = 24;
public static final ChunkVertexLayout LAYOUT = new ChunkVertexLayout.Attributes(VERTEX_FORMAT);
private static final int POSITION_BITS = 21;
private static final int POSITION_LOW_BITS = 5;
private static final int POSITION_MASK = (1 << POSITION_BITS) - 1;
@@ -61,39 +64,46 @@ public class CompactChunkVertex implements ChunkVertexType {
}
@Override
public GlVertexFormat getVertexFormat() {
return VERTEX_FORMAT;
public ChunkVertexLayout getLayout() {
return LAYOUT;
}
@Override
public ChunkVertexEncoder createEncoder() {
return (ptr, material, vertex, sectionIndex) -> {
int x = encodePosition(vertex.x);
int y = encodePosition(vertex.y);
int z = encodePosition(vertex.z);
return (ptr, material, vertices, sectionIndex) -> {
for (var vertex : vertices) {
writeVertex(ptr, material, vertex, sectionIndex);
ptr += STRIDE;
}
LWJGL.memPutShort(ptr + 0, (short) (x >>> POSITION_LOW_BITS));
LWJGL.memPutShort(ptr + 2, (short) (y >>> POSITION_LOW_BITS));
LWJGL.memPutShort(ptr + 4, (short) (z >>> POSITION_LOW_BITS));
LWJGL.memPutShort(ptr + 6, (short) ((x & POSITION_LOW_MASK)
| ((y & POSITION_LOW_MASK) << POSITION_LOW_BITS)
| ((z & POSITION_LOW_MASK) << (POSITION_LOW_BITS * 2))));
LWJGL.memPutInt(ptr + 8, vertex.color);
int u = encodeTexture(vertex.u);
int v = encodeTexture(vertex.v);
LWJGL.memPutShort(ptr + 12, (short) (u >>> TEXTURE_LOW_BITS));
LWJGL.memPutShort(ptr + 14, (short) (v >>> TEXTURE_LOW_BITS));
LWJGL.memPutInt(ptr + 16, (encodeDrawParameters(material, sectionIndex, u, v) << 0) | (encodeLight(vertex.light) << 16));
LWJGL.memPutInt(ptr + 20, vertex.rdhFactor);
return ptr + STRIDE;
return ptr;
};
}
private static void writeVertex(long ptr, Material material, ChunkVertexEncoder.Vertex vertex, int sectionIndex) {
int x = encodePosition(vertex.x);
int y = encodePosition(vertex.y);
int z = encodePosition(vertex.z);
LWJGL.memPutShort(ptr + 0, (short) (x >>> POSITION_LOW_BITS));
LWJGL.memPutShort(ptr + 2, (short) (y >>> POSITION_LOW_BITS));
LWJGL.memPutShort(ptr + 4, (short) (z >>> POSITION_LOW_BITS));
LWJGL.memPutShort(ptr + 6, (short) ((x & POSITION_LOW_MASK)
| ((y & POSITION_LOW_MASK) << POSITION_LOW_BITS)
| ((z & POSITION_LOW_MASK) << (POSITION_LOW_BITS * 2))));
LWJGL.memPutInt(ptr + 8, vertex.color);
int u = encodeTexture(vertex.u);
int v = encodeTexture(vertex.v);
LWJGL.memPutShort(ptr + 12, (short) (u >>> TEXTURE_LOW_BITS));
LWJGL.memPutShort(ptr + 14, (short) (v >>> TEXTURE_LOW_BITS));
LWJGL.memPutInt(ptr + 16, (encodeDrawParameters(material, sectionIndex, u, v) << 0) | (encodeLight(vertex.light) << 16));
LWJGL.memPutInt(ptr + 20, vertex.rdhFactor);
}
@Override
public Map<String, String> getDefines() {
var map = ChunkVertexType.super.getDefines();
@@ -4,6 +4,7 @@ import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeFormat;
import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
@@ -22,6 +23,8 @@ public class VanillaLikeChunkVertex implements ChunkVertexType {
.addElement("a_RdhFactor", 28, GlVertexAttributeFormat.BYTE, 4, true, false)
.build();
public static final ChunkVertexLayout LAYOUT = new ChunkVertexLayout.Attributes(VERTEX_FORMAT);
@Override
public float getPositionScale() {
return 1f;
@@ -38,26 +41,33 @@ public class VanillaLikeChunkVertex implements ChunkVertexType {
}
@Override
public GlVertexFormat getVertexFormat() {
return VERTEX_FORMAT;
public ChunkVertexLayout getLayout() {
return LAYOUT;
}
@Override
public ChunkVertexEncoder createEncoder() {
return (ptr, material, vertex, sectionIndex) -> {
LWJGL.memPutFloat(ptr + 0, vertex.x);
LWJGL.memPutFloat(ptr + 4, vertex.y);
LWJGL.memPutFloat(ptr + 8, vertex.z);
LWJGL.memPutInt(ptr + 12, vertex.color);
LWJGL.memPutFloat(ptr + 16, encodeTexture(vertex.u));
LWJGL.memPutFloat(ptr + 20, encodeTexture(vertex.v));
LWJGL.memPutInt(ptr + 24, (encodeDrawParameters(material, sectionIndex) << 0) | (encodeLight(vertex.light) << 16));
LWJGL.memPutInt(ptr + 28, vertex.rdhFactor);
return (ptr, material, vertices, sectionIndex) -> {
for (var vertex : vertices) {
writeVertex(ptr, material, vertex, sectionIndex);
ptr += STRIDE;
}
return ptr + STRIDE;
return ptr;
};
}
public static void writeVertex(long ptr, Material material, ChunkVertexEncoder.Vertex vertex, int sectionIndex) {
LWJGL.memPutFloat(ptr + 0, vertex.x);
LWJGL.memPutFloat(ptr + 4, vertex.y);
LWJGL.memPutFloat(ptr + 8, vertex.z);
LWJGL.memPutInt(ptr + 12, vertex.color);
LWJGL.memPutFloat(ptr + 16, encodeTexture(vertex.u));
LWJGL.memPutFloat(ptr + 20, encodeTexture(vertex.v));
LWJGL.memPutInt(ptr + 24, (encodeDrawParameters(material, sectionIndex) << 0) | (encodeLight(vertex.light) << 16));
LWJGL.memPutInt(ptr + 28, vertex.rdhFactor);
}
private static int encodeDrawParameters(Material material, int sectionIndex) {
return (((sectionIndex & 0xFF) << 8) | ((material.bits() & 0xFF) << 0));
}
@@ -189,12 +189,7 @@ public class ImmersiveConnectionRenderer implements ResourceManagerReloadListene
int color,
boolean lightForStart
) {
static final ThreadLocal<ChunkVertexEncoder.Vertex[]> vertexHolder = ThreadLocal.withInitial(() -> new ChunkVertexEncoder.Vertex[1]);
void write(
ChunkMeshBufferBuilder vertexSink, Material material, int offX, int offY, int offZ, int lightStart, int lightEnd
) {
ChunkVertexEncoder.Vertex sodiumVertex = new ChunkVertexEncoder.Vertex();
void copyTo(ChunkVertexEncoder.Vertex sodiumVertex, int offX, int offY, int offZ, int lightStart, int lightEnd) {
sodiumVertex.x = offX + posX;
sodiumVertex.y = offY + posY;
sodiumVertex.z = offZ + posZ;
@@ -202,28 +197,32 @@ public class ImmersiveConnectionRenderer implements ResourceManagerReloadListene
sodiumVertex.u = texU;
sodiumVertex.v = texV;
sodiumVertex.light = lightForStart ? lightStart : lightEnd;
ChunkVertexEncoder.Vertex[] array = vertexHolder.get();
array[0] = sodiumVertex;
vertexSink.push(array, material);
}
}
private record Quad(Vertex v0, Vertex v1, Vertex v2, Vertex v3) {
static final ThreadLocal<ChunkVertexEncoder.Vertex[]> vertexHolder = ThreadLocal.withInitial(ChunkVertexEncoder.Vertex::uninitializedQuad);
void write(
ChunkModelBuilder out, Material material, int offX, int offY, int offZ, int lightStart, int lightEnd
) {
var vertexSink = out.getVertexBuffer(ModelQuadFacing.UNASSIGNED);
int quadStart = vertexSink.count();
var vertices = vertexHolder.get();
// clockwise
v0.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
v1.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
v2.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
v3.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
push(vertexSink, vertices, material, v0, v1, v2, v3, offX, offY, offZ, lightStart, lightEnd);
// counter-clockwise
v0.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
v3.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
v2.write(vertexSink ,material, offX, offY, offZ, lightStart, lightEnd);
v1.write(vertexSink, material, offX, offY, offZ, lightStart, lightEnd);
push(vertexSink, vertices, material, v0, v3, v2, v1, offX, offY, offZ, lightStart, lightEnd);
}
private static void push(
ChunkMeshBufferBuilder vertexSink, ChunkVertexEncoder.Vertex[] vertices, Material material,
Vertex a, Vertex b, Vertex c, Vertex d, int offX, int offY, int offZ, int lightStart, int lightEnd
) {
a.copyTo(vertices[0], offX, offY, offZ, lightStart, lightEnd);
b.copyTo(vertices[1], offX, offY, offZ, lightStart, lightEnd);
c.copyTo(vertices[2], offX, offY, offZ, lightStart, lightEnd);
d.copyTo(vertices[3], offX, offY, offZ, lightStart, lightEnd);
vertexSink.push(vertices, material);
}
}
@@ -10,6 +10,7 @@ import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration;
import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkFogMode;
import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderOptions;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.gl.blending.BlendModeOverride;
import net.irisshaders.iris.gl.blending.BufferBlendOverride;
@@ -91,7 +92,7 @@ public class IrisChunkProgramOverrides {
int i = 0;
var vertexType = configuration.getVertexTypeForPass(pass.toTerrainPass(configuration));
for (var attr : vertexType.getVertexFormat().getAttributes()) {
for (var attr : ChunkVertexLayout.requireAttributes(vertexType.getLayout()).getAttributes()) {
builder.bindAttribute(attr.getName(), i++);
}
return builder
@@ -4,6 +4,7 @@ import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeFormat;
import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkMeshFormats;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
/**
@@ -15,7 +16,7 @@ public class XHFPModelVertexType implements ChunkVertexType {
public static final int STRIDE = 48;
public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE)
// Exclude the rdh factor as shaders cannot use it
.addElements(BASE_VERTEX_TYPE.getVertexFormat().getAttributes().stream().filter(a -> !a.getName().equals("a_RdhFactor")).toList())
.addElements(ChunkVertexLayout.requireAttributes(BASE_VERTEX_TYPE.getLayout()).getAttributes().stream().filter(a -> !a.getName().equals("a_RdhFactor")).toList())
.addElement("mc_midTexCoord", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
.addElement("at_tangent", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 4, true, false)
.addElement("iris_Normal", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 3, true, false)
@@ -23,6 +24,8 @@ public class XHFPModelVertexType implements ChunkVertexType {
.addElement("at_midBlock", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 4, false, false)
.build();
public static final ChunkVertexLayout LAYOUT = new ChunkVertexLayout.Attributes(VERTEX_FORMAT);
private static final int TEXTURE_MAX_VALUE = 32768;
public static final float MID_TEX_SCALE = 1.0f / TEXTURE_MAX_VALUE;
@@ -49,8 +52,8 @@ public class XHFPModelVertexType implements ChunkVertexType {
}
@Override
public GlVertexFormat getVertexFormat() {
return VERTEX_FORMAT;
public ChunkVertexLayout getLayout() {
return LAYOUT;
}
@Override
@@ -14,6 +14,7 @@ import org.embeddedt.embeddium.impl.modern.render.chunk.ContextAwareChunkVertexE
import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.BlockRenderContext;
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder;
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.impl.VanillaLikeChunkVertex;
import net.irisshaders.iris.vertices.ExtendedDataHelper;
import net.irisshaders.iris.vertices.NormI8;
import net.irisshaders.iris.vertices.NormalHelper;
@@ -56,8 +57,6 @@ public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkV
private final FallbackTextureMaterials fallbackMaterials = WorldRenderingSettings.INSTANCE.getFallbackTextureMaterialMapping();
private final ChunkVertexEncoder baseEncoder = XHFPModelVertexType.BASE_VERTEX_TYPE.createEncoder();
private static final int MID_TEX_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("mc_midTexCoord").getPointer();
private static final int MC_ENTITY_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("mc_Entity").getPointer();
private static final int MID_BLOCK_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("at_midBlock").getPointer();
@@ -149,15 +148,22 @@ public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkV
return false;
}
@Override
public long write(long ptr,
Material material, Vertex vertex, int chunkId) {
@Override
public long writePrimitive(long ptr, Material material, Vertex[] vertices, int chunkId) {
for (Vertex vertex : vertices) {
ptr = this.writeVertex(ptr, material, vertex, chunkId);
}
return ptr;
}
private long writeVertex(long ptr, Material material, Vertex vertex, int chunkId) {
uSum += vertex.u;
vSum += vertex.v;
vertexCount++;
vertex.rdhFactor = 0;
this.baseEncoder.write(ptr, material, vertex, chunkId);
VanillaLikeChunkVertex.writeVertex(ptr, material, vertex, chunkId);
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET + 2, renderType);
MemoryUtil.memPutInt(ptr + MID_BLOCK_OFFSET, ignoreMidBlock ? 0 : ExtendedDataHelper.computeMidBlock(vertex.x, vertex.y, vertex.z, localPosX, localPosY, localPosZ));