Skip to content

Commit

Permalink
Fix partition tree sorting on geometry with negative dot products and…
Browse files Browse the repository at this point in the history
… some refactors (#2655)

* Remove vertex range because the vertex offset is never used, instead use integers and/or integer arrays.

* Refactor use of default terrain render passes and material to use `#isTranslucent() instead of enum equality.

* Fix partition algorithm interval encoding by using floatToComparableInt and generally fix floatToComparableInt to actually function correctly.

* Remove use of -1 as null-like vertex count, refactor translucent data objects to just deal with vertex counts instead of the mesh object.
  • Loading branch information
douira authored Aug 12, 2024
1 parent bad3763 commit 7b25a31
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 169 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import net.caffeinemc.mods.sodium.client.render.chunk.data.SectionRenderDataStorage;
import net.caffeinemc.mods.sodium.client.render.chunk.data.SectionRenderDataUnsafe;
import net.caffeinemc.mods.sodium.client.render.chunk.lists.ChunkRenderListIterable;
import net.caffeinemc.mods.sodium.client.render.chunk.lists.ChunkRenderList;
import net.caffeinemc.mods.sodium.client.render.chunk.lists.ChunkRenderListIterable;
import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion;
import net.caffeinemc.mods.sodium.client.render.chunk.shader.ChunkShaderBindingPoints;
import net.caffeinemc.mods.sodium.client.render.chunk.shader.ChunkShaderInterface;
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.DefaultTerrainRenderPasses;
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.SortBehavior;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.format.ChunkMeshAttribute;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import net.caffeinemc.mods.sodium.client.render.viewport.CameraTransform;
import net.caffeinemc.mods.sodium.client.util.BitwiseMath;
import org.lwjgl.system.MemoryUtil;

import java.util.Iterator;

public class DefaultChunkRenderer extends ShaderChunkRenderer {
Expand Down Expand Up @@ -100,8 +100,7 @@ public void render(ChunkRenderMatrices matrices,
}

private static boolean isTranslucentRenderPass(TerrainRenderPass renderPass) {
return renderPass == DefaultTerrainRenderPasses.TRANSLUCENT
&& SodiumClientMod.options().performance.getSortBehavior() != SortBehavior.OFF;
return renderPass.isTranslucent() && SodiumClientMod.options().performance.getSortBehavior() != SortBehavior.OFF;
}

private static void fillCommandBuffer(MultiDrawBatch batch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.caffeinemc.mods.sodium.client.render.chunk.compile;

import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import net.caffeinemc.mods.sodium.client.gl.util.VertexRange;
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.BakedChunkModelBuilder;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
Expand Down Expand Up @@ -61,41 +60,43 @@ public BuiltSectionMeshParts createMesh(TerrainRenderPass pass, boolean forceUna
var builder = this.builders.get(pass);

List<ByteBuffer> vertexBuffers = new ArrayList<>();
VertexRange[] vertexRanges = new VertexRange[ModelQuadFacing.COUNT];
int[] vertexCounts = new int[ModelQuadFacing.COUNT];

int vertexCount = 0;
int vertexSum = 0;

for (ModelQuadFacing facing : ModelQuadFacing.VALUES) {
var ordinal = facing.ordinal();
var buffer = builder.getVertexBuffer(facing);

if (buffer.isEmpty()) {
continue;
}

vertexBuffers.add(buffer.slice());
var bufferCount = buffer.count();
if (!forceUnassigned) {
vertexRanges[facing.ordinal()] = new VertexRange(vertexCount, buffer.count());
vertexCounts[ordinal] = bufferCount;
}

vertexCount += buffer.count();
vertexSum += bufferCount;
}

if (vertexCount == 0) {
if (vertexSum == 0) {
return null;
}

if (forceUnassigned) {
vertexRanges[ModelQuadFacing.UNASSIGNED.ordinal()] = new VertexRange(0, vertexCount);
vertexCounts[ModelQuadFacing.UNASSIGNED.ordinal()] = vertexSum;
}

var mergedBuffer = new NativeBuffer(vertexCount * this.vertexType.getVertexFormat().getStride());
var mergedBuffer = new NativeBuffer(vertexSum * this.vertexType.getVertexFormat().getStride());
var mergedBufferBuilder = mergedBuffer.getDirectBuffer();

for (var buffer : vertexBuffers) {
mergedBufferBuilder.put(buffer);
}

return new BuiltSectionMeshParts(mergedBuffer, vertexRanges);
return new BuiltSectionMeshParts(mergedBuffer, vertexCounts);
}

public void destroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package net.caffeinemc.mods.sodium.client.render.chunk.compile.buffers;

import com.mojang.blaze3d.vertex.VertexConsumer;
import net.caffeinemc.mods.sodium.api.util.ColorABGR;
import net.caffeinemc.mods.sodium.api.util.ColorARGB;
import net.caffeinemc.mods.sodium.api.util.NormI8;
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.DefaultMaterials;
import net.caffeinemc.mods.sodium.client.render.chunk.terrain.material.Material;
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.TranslucentGeometryCollector;
import net.caffeinemc.mods.sodium.client.render.chunk.vertex.format.ChunkVertexEncoder;
import net.caffeinemc.mods.sodium.api.util.ColorABGR;
import net.caffeinemc.mods.sodium.api.util.ColorARGB;
import net.caffeinemc.mods.sodium.client.render.texture.SpriteFinderCache;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.NotNull;

public class ChunkVertexConsumer implements VertexConsumer {
Expand Down Expand Up @@ -129,8 +127,8 @@ public VertexConsumer potentiallyEndVertex() {

ModelQuadFacing cullFace = ModelQuadFacing.fromPackedNormal(normal);

if (material == DefaultMaterials.TRANSLUCENT && collector != null) {
collector.appendQuad(normal, vertices, cullFace);
if (this.material.isTranslucent() && this.collector != null) {
this.collector.appendQuad(normal, this.vertices, cullFace);
}

this.modelBuilder.getVertexBuffer(cullFace).push(this.vertices, this.material);
Expand All @@ -156,21 +154,21 @@ public VertexConsumer potentiallyEndVertex() {
}

private int calculateNormal() {
final float x0 = vertices[0].x;
final float y0 = vertices[0].y;
final float z0 = vertices[0].z;
final float x0 = this.vertices[0].x;
final float y0 = this.vertices[0].y;
final float z0 = this.vertices[0].z;

final float x1 = vertices[1].x;
final float y1 = vertices[1].y;
final float z1 = vertices[1].z;
final float x1 = this.vertices[1].x;
final float y1 = this.vertices[1].y;
final float z1 = this.vertices[1].z;

final float x2 = vertices[2].x;
final float y2 = vertices[2].y;
final float z2 = vertices[2].z;
final float x2 = this.vertices[2].x;
final float y2 = this.vertices[2].y;
final float z2 = this.vertices[2].z;

final float x3 = vertices[3].x;
final float y3 = vertices[3].y;
final float z3 = vertices[3].z;
final float x3 = this.vertices[3].x;
final float y3 = this.vertices[3].y;
final float z3 = this.vertices[3].z;

final float dx0 = x2 - x0;
final float dy0 = y2 - y0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline;

import net.caffeinemc.mods.sodium.api.util.ColorARGB;
import net.caffeinemc.mods.sodium.client.model.color.ColorProvider;
import net.caffeinemc.mods.sodium.client.model.color.ColorProviderRegistry;
import net.caffeinemc.mods.sodium.client.model.light.LightMode;
Expand All @@ -20,7 +21,6 @@
import net.caffeinemc.mods.sodium.client.services.PlatformModelAccess;
import net.caffeinemc.mods.sodium.client.services.SodiumModelData;
import net.caffeinemc.mods.sodium.client.world.LevelSlice;
import net.caffeinemc.mods.sodium.api.util.ColorARGB;
import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.material.ShadeMode;
Expand Down Expand Up @@ -177,8 +177,8 @@ private void bufferQuad(MutableQuadViewImpl quad, float[] brightnesses, Material

ModelQuadFacing normalFace = quad.normalFace();

if (material == DefaultMaterials.TRANSLUCENT && collector != null) {
collector.appendQuad(quad.getFaceNormal(), vertices, normalFace);
if (material.isTranslucent() && this.collector != null) {
this.collector.appendQuad(quad.getFaceNormal(), vertices, normalFace);
}

ChunkMeshBufferBuilder vertexBuffer = modelBuilder.getVertexBuffer(normalFace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.caffeinemc.mods.sodium.client.render.chunk.compile.ChunkBuildOutput;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.executor.ChunkBuilder;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderCache;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderContext;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionInfo;
import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionMeshParts;
Expand All @@ -20,7 +19,6 @@
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.TranslucentGeometryCollector;
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.data.PresentTranslucentData;
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.data.TranslucentData;
import net.caffeinemc.mods.sodium.client.services.PlatformLevelAccess;
import net.caffeinemc.mods.sodium.client.services.PlatformLevelRenderHooks;
import net.caffeinemc.mods.sodium.client.util.task.CancellationToken;
import net.caffeinemc.mods.sodium.client.world.LevelSlice;
Expand All @@ -37,10 +35,10 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import java.util.Map;

import org.joml.Vector3dc;

import java.util.Map;

/**
* Rebuilds all the meshes of a chunk for each given render pass with non-occluded blocks. The result is then uploaded
* to graphics memory on the main thread.
Expand Down Expand Up @@ -160,8 +158,7 @@ public ChunkBuildOutput execute(ChunkBuildContext buildContext, CancellationToke
for (TerrainRenderPass pass : DefaultTerrainRenderPasses.ALL) {
// consolidate all translucent geometry into UNASSIGNED so that it's rendered
// all together if it needs to share an index buffer between the directions
boolean isTranslucent = pass == DefaultTerrainRenderPasses.TRANSLUCENT;
BuiltSectionMeshParts mesh = buffers.createMesh(pass, isTranslucent && sortType.needsDirectionMixing);
BuiltSectionMeshParts mesh = buffers.createMesh(pass, pass.isTranslucent() && sortType.needsDirectionMixing);

if (mesh != null) {
meshes.put(pass, mesh);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package net.caffeinemc.mods.sodium.client.render.chunk.data;

import net.caffeinemc.mods.sodium.client.gl.util.VertexRange;
import net.caffeinemc.mods.sodium.client.util.NativeBuffer;

public class BuiltSectionMeshParts {
private final VertexRange[] ranges;
private final int[] vertexCounts;
private final NativeBuffer buffer;

public BuiltSectionMeshParts(NativeBuffer buffer, VertexRange[] ranges) {
this.ranges = ranges;
public BuiltSectionMeshParts(NativeBuffer buffer, int[] vertexCounts) {
this.vertexCounts = vertexCounts;
this.buffer = buffer;
}

public NativeBuffer getVertexData() {
return this.buffer;
}

public VertexRange[] getVertexRanges() {
return this.ranges;
public int[] getVertexCounts() {
return this.vertexCounts;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.caffeinemc.mods.sodium.client.render.chunk.data;

import net.caffeinemc.mods.sodium.client.gl.arena.GlBufferSegment;
import net.caffeinemc.mods.sodium.client.gl.util.VertexRange;
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -47,7 +46,7 @@ public SectionRenderDataStorage(boolean storesIndices) {
}

public void setVertexData(int localSectionIndex,
GlBufferSegment allocation, VertexRange[] ranges) {
GlBufferSegment allocation, int[] vertexCounts) {
GlBufferSegment prev = this.vertexAllocations[localSectionIndex];

if (prev != null) {
Expand All @@ -62,14 +61,7 @@ public void setVertexData(int localSectionIndex,
int vertexOffset = allocation.getOffset();

for (int facingIndex = 0; facingIndex < ModelQuadFacing.COUNT; facingIndex++) {
VertexRange vertexRange = ranges[facingIndex];
int vertexCount;

if (vertexRange != null) {
vertexCount = vertexRange.vertexCount();
} else {
vertexCount = 0;
}
int vertexCount = vertexCounts[facingIndex];

SectionRenderDataUnsafe.setVertexOffset(pMeshData, facingIndex, vertexOffset);
SectionRenderDataUnsafe.setElementCount(pMeshData, facingIndex, (vertexCount >> 2) * 6);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public SectionRenderDataStorage createStorage(TerrainRenderPass pass) {
var storage = this.sectionRenderData.get(pass);

if (storage == null) {
storage = new SectionRenderDataStorage(pass == DefaultTerrainRenderPasses.TRANSLUCENT);
storage = new SectionRenderDataStorage(pass.isTranslucent());
this.sectionRenderData.put(pass, storage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void uploadResults(CommandList commandList, RenderRegion region, Collect
for (PendingSectionMeshUpload upload : uploads) {
var storage = region.createStorage(upload.pass);
storage.setVertexData(upload.section.getSectionIndex(),
upload.vertexUpload.getResult(), upload.meshData.getVertexRanges());
upload.vertexUpload.getResult(), upload.meshData.getVertexCounts());
}
}

Expand Down
Loading

0 comments on commit 7b25a31

Please sign in to comment.