Bilinear interpolation for quad vertex data via geometry shader #12

Closed
embeddedt wants to merge 18 commits from geotesting into stonecutter
pull from: geotesting
3 changed files with 17 additions and 7 deletions
Showing only changes of commit 8dd94526e4 - Show all commits

No files matched your search

@@ -3,6 +3,7 @@ package org.embeddedt.embeddium.impl.render.chunk.data;
import org.embeddedt.embeddium.impl.gl.arena.GlBufferSegment; import org.embeddedt.embeddium.impl.gl.arena.GlBufferSegment;
import org.embeddedt.embeddium.impl.gl.util.VertexRange; import org.embeddedt.embeddium.impl.gl.util.VertexRange;
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing; import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveType;
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -13,14 +14,16 @@ public class SectionRenderDataStorage {
private final GlBufferSegment[] indexAllocations = new GlBufferSegment[RenderRegion.REGION_SIZE]; private final GlBufferSegment[] indexAllocations = new GlBufferSegment[RenderRegion.REGION_SIZE];
private final long pMeshDataArray; private final long pMeshDataArray;
private final ChunkPrimitiveType primitiveType;
private int numAllocations; private int numAllocations;
public SectionRenderDataStorage() { public SectionRenderDataStorage(ChunkPrimitiveType primitiveType) {
this.pMeshDataArray = SectionRenderDataUnsafe.allocateHeap(RenderRegion.REGION_SIZE); this.pMeshDataArray = SectionRenderDataUnsafe.allocateHeap(RenderRegion.REGION_SIZE);
if (this.pMeshDataArray == 0) { if (this.pMeshDataArray == 0) {
throw new OutOfMemoryError("Failed to allocate mesh data array"); throw new OutOfMemoryError("Failed to allocate mesh data array");
} }
this.primitiveType = primitiveType;
} }
public boolean isEmpty() { public boolean isEmpty() {
@@ -50,6 +53,9 @@ public class SectionRenderDataStorage {
int vertexOffset = allocation.getOffset(); int vertexOffset = allocation.getOffset();
int indexOffset = indexAllocation != null ? indexAllocation.getOffset() * 4 : 0; int indexOffset = indexAllocation != null ? indexAllocation.getOffset() * 4 : 0;
int elementsPerPrimitive = primitiveType.getIndexBufferElementsPerPrimitive();
int verticesPerPrimitive = primitiveType.getVerticesPerPrimitive();
for (int facingIndex = 0; facingIndex < ModelQuadFacing.COUNT; facingIndex++) { for (int facingIndex = 0; facingIndex < ModelQuadFacing.COUNT; facingIndex++) {
VertexRange vertexRange = ranges[facingIndex]; VertexRange vertexRange = ranges[facingIndex];
int vertexCount; int vertexCount;
@@ -60,7 +66,7 @@ public class SectionRenderDataStorage {
vertexCount = 0; vertexCount = 0;
} }
int indexCount = (vertexCount >> 2) * 6; int indexCount = (vertexCount / verticesPerPrimitive) * elementsPerPrimitive;
SectionRenderDataUnsafe.setVertexOffset(pMeshData, facingIndex, vertexOffset); SectionRenderDataUnsafe.setVertexOffset(pMeshData, facingIndex, vertexOffset);
SectionRenderDataUnsafe.setElementCount(pMeshData, facingIndex, indexCount); SectionRenderDataUnsafe.setElementCount(pMeshData, facingIndex, indexCount);
@@ -133,12 +139,15 @@ public class SectionRenderDataStorage {
var data = this.getDataPointer(sectionIndex); var data = this.getDataPointer(sectionIndex);
int elementsPerPrimitive = primitiveType.getIndexBufferElementsPerPrimitive();
int verticesPerPrimitive = primitiveType.getVerticesPerPrimitive();
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
SectionRenderDataUnsafe.setVertexOffset(data, facing, vertexOffset); SectionRenderDataUnsafe.setVertexOffset(data, facing, vertexOffset);
SectionRenderDataUnsafe.setIndexOffset(data, facing, indexOffset); SectionRenderDataUnsafe.setIndexOffset(data, facing, indexOffset);
var indexCount = SectionRenderDataUnsafe.getElementCount(data, facing); var indexCount = SectionRenderDataUnsafe.getElementCount(data, facing);
vertexOffset += (indexCount / 6) * 4; // convert elements back into vertices vertexOffset += (indexCount / elementsPerPrimitive) * verticesPerPrimitive; // convert elements back into vertices
indexOffset += indexCount * 4; indexOffset += indexCount * 4;
} }
} }
@@ -9,6 +9,7 @@ import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
import org.embeddedt.embeddium.impl.gl.buffer.GlBuffer; import org.embeddedt.embeddium.impl.gl.buffer.GlBuffer;
import org.embeddedt.embeddium.impl.gl.device.CommandList; import org.embeddedt.embeddium.impl.gl.device.CommandList;
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation; import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration;
import org.embeddedt.embeddium.impl.render.chunk.RenderSection; import org.embeddedt.embeddium.impl.render.chunk.RenderSection;
import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataStorage; import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataStorage;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
@@ -120,11 +121,11 @@ public class RenderRegion {
return this.sectionRenderData.get(pass); return this.sectionRenderData.get(pass);
} }
public SectionRenderDataStorage createStorage(TerrainRenderPass pass) { public SectionRenderDataStorage createStorage(TerrainRenderPass pass, RenderPassConfiguration<?> renderPassConfiguration) {
var storage = this.sectionRenderData.get(pass); var storage = this.sectionRenderData.get(pass);
if (storage == null) { if (storage == null) {
this.sectionRenderData.put(pass, storage = new SectionRenderDataStorage()); this.sectionRenderData.put(pass, storage = new SectionRenderDataStorage(renderPassConfiguration.getPrimitiveTypeForPass(pass)));
} }
return storage; return storage;
@@ -117,7 +117,7 @@ public class RenderRegionManager {
// Collect the upload results // Collect the upload results
for (var uploads : uploadsByFormat.values()) { for (var uploads : uploadsByFormat.values()) {
for (PendingSectionUpload upload : uploads) { for (PendingSectionUpload upload : uploads) {
var storage = region.createStorage(upload.pass); var storage = region.createStorage(upload.pass, renderPassConfiguration);
storage.setMeshes(upload.section.getSectionIndex(), storage.setMeshes(upload.section.getSectionIndex(),
upload.vertexUpload.getResult(), upload.indexUpload != null ? upload.indexUpload.getResult() : null, upload.meshData.getVertexRanges()); upload.vertexUpload.getResult(), upload.indexUpload != null ? upload.indexUpload.getResult() : null, upload.meshData.getVertexRanges());
} }
@@ -172,7 +172,7 @@ public class RenderRegionManager {
// Collect the upload results // Collect the upload results
for (var uploads : uploadsByFormat.values()) { for (var uploads : uploadsByFormat.values()) {
for (PendingResortUpload upload : uploads) { for (PendingResortUpload upload : uploads) {
var storage = region.createStorage(upload.pass); var storage = region.createStorage(upload.pass, renderPassConfiguration);
storage.replaceIndexBuffer(upload.section.getSectionIndex(), upload.indexUpload.getResult()); storage.replaceIndexBuffer(upload.section.getSectionIndex(), upload.indexUpload.getResult());
} }
} }