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

[TS] Delete VertexRange, use isTranslucent, fix partition tree sorting on geometry with negative dot products #2655

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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

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,45 @@ 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()) {
vertexCounts[ordinal] = -1;
douira marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

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

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

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.model.FabricBakedModel;
Expand Down Expand Up @@ -175,8 +175,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,12 +61,9 @@ public void setVertexData(int localSectionIndex,
int vertexOffset = allocation.getOffset();

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

if (vertexRange != null) {
vertexCount = vertexRange.vertexCount();
} else {
if (vertexCount == -1) {
vertexCount = 0;
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* type is determined with a heuristic based on the collected metrics. This
* determines if block face culling can be enabled.
* - Now the {@link BuiltSectionMeshParts} is generated, which yields the vertex
* ranges.
* 3. The vertex ranges and the mesh parts object are used by the collector in
* counts.
* 3. The vertex counts and the mesh parts object are used by the collector in
* the construction of the {@link TranslucentData} object. The data object
* allocates memory for the index data and performs the first (and for static
* sort types, only) sort.
Expand Down Expand Up @@ -534,7 +534,7 @@ public TranslucentData getTranslucentData(
// doesn't matter
if (this.sortType == SortType.NONE && oldData instanceof AnyOrderData oldAnyData
&& oldAnyData.getQuadCount() == this.quads.length
&& Arrays.equals(oldAnyData.getVertexRanges(), translucentMesh.getVertexRanges())) {
&& Arrays.equals(oldAnyData.getVertexCounts(), translucentMesh.getVertexCounts())) {
return oldAnyData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,17 @@ static InnerPartitionBSPNode attemptNodeReuse(BSPWorkspace workspace, IntArrayLi
return oldNode;
}

/**
* Encoding with {@link MathUtil#floatToComparableInt(float)} is necessary here to ensure negative distances are not sorted backwards. Simply converting potentially negative floats to int bits using {@link Float#floatToRawIntBits(float)} would sort negative floats backwards amongst themselves.
* <p>
* Note that negative floats convert to negative integers with this method which is ok, since it yields an overall negative long that gets sorted correctly before the longs that encode positive floats as distances.
*/
private static long encodeIntervalPoint(float distance, int quadIndex, int type) {
return ((long) Float.floatToRawIntBits(distance) << 32) | ((long) type << 30) | quadIndex;
return ((long) MathUtil.floatToComparableInt(distance) << 32) | ((long) type << 30) | quadIndex;
}

private static float decodeDistance(long encoded) {
return Float.intBitsToFloat((int) (encoded >>> 32));
return MathUtil.comparableIntToFloat((int) (encoded >>> 32));
}

private static int decodeQuadIndex(long encoded) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.data;

import net.caffeinemc.mods.sodium.client.gl.util.VertexRange;
import net.caffeinemc.mods.sodium.client.render.chunk.data.BuiltSectionMeshParts;
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.SortType;
import net.caffeinemc.mods.sodium.client.render.chunk.translucent_sorting.TQuad;
import net.caffeinemc.mods.sodium.client.util.NativeBuffer;
import net.minecraft.core.SectionPos;

/**
Expand All @@ -24,8 +22,8 @@
public class AnyOrderData extends SplitDirectionData {
private Sorter sorterOnce;

AnyOrderData(SectionPos sectionPos, VertexRange[] ranges, int quadCount) {
super(sectionPos, ranges, quadCount);
AnyOrderData(SectionPos sectionPos, int[] vertexCounts, int quadCount) {
super(sectionPos, vertexCounts, quadCount);
}

@Override
Expand All @@ -48,18 +46,18 @@ public Sorter getSorter() {
*/
public static AnyOrderData fromMesh(BuiltSectionMeshParts translucentMesh,
TQuad[] quads, SectionPos sectionPos) {
var ranges = translucentMesh.getVertexRanges();
var anyOrderData = new AnyOrderData(sectionPos, ranges, quads.length);
var vertexCounts = translucentMesh.getVertexCounts();
var anyOrderData = new AnyOrderData(sectionPos, vertexCounts, quads.length);
var sorter = new StaticSorter(quads.length);
anyOrderData.sorterOnce = sorter;
var indexBuffer = sorter.getIntBuffer();

for (var range : ranges) {
if (range == null) {
for (var vertexCount : vertexCounts) {
if (vertexCount == -1) {
continue;
}

int count = TranslucentData.vertexCountToQuadCount(range.vertexCount());
int count = TranslucentData.vertexCountToQuadCount(vertexCount);
for (int i = 0; i < count; i++) {
TranslucentData.writeQuadVertexIndexes(indexBuffer, i);
}
Expand Down
Loading
Loading