Skip to content

Commit

Permalink
Attempt to improve entity render performance
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Feb 2, 2024
1 parent a3f561b commit 3b17b9a
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 10 deletions.
6 changes: 3 additions & 3 deletions buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

public class Buildscript extends SimpleFabricProject {
static final boolean SODIUM = true;
static final boolean CUSTOM_SODIUM = true;
static final boolean CUSTOM_SODIUM = false;
static final String MC_VERSION = "1.20.4";
static final String customSodiumName = "sodium-fabric-mc1.20.3-0.5.6git.7a62284.jar";

Expand Down Expand Up @@ -114,10 +114,10 @@ public void getModDependencies(ModDependencyCollector d) {
if (CUSTOM_SODIUM) {
d.add(new JavaJarDependency(getProjectDir().resolve("custom_sodium").resolve(customSodiumName).toAbsolutePath(), null, new MavenId("me.jellysquid.mods", "sodium-fabric", customSodiumName.replace("sodium-fabric-", ""))), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
} else {
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.3-0.5.5"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.4-0.5.8"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
}
} else {
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.3-0.5.5"), ModDependencyFlag.COMPILE);
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.4-0.5.8"), ModDependencyFlag.COMPILE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.VertexConsumer;
import me.jellysquid.mods.sodium.client.render.vertex.buffer.ExtendedBufferBuilder;
import me.jellysquid.mods.sodium.client.render.vertex.buffer.SodiumBufferBuilder;
import net.coderbot.batchedentityrendering.impl.ordering.GraphTranslucencyRenderOrderManager;
import net.coderbot.batchedentityrendering.impl.ordering.RenderOrderManager;
import net.coderbot.iris.fantastic.WrappingMultiBufferSource;
Expand Down Expand Up @@ -90,7 +92,16 @@ public VertexConsumer getBuffer(RenderType renderType) {
affinities.put(renderType, affinity);
}

return builders[affinity].getBuffer(renderType);
VertexConsumer consumer = builders[affinity].getBuffer(renderType);

if (consumer instanceof ExtendedBufferBuilder extendedBufferBuilder) {
SodiumBufferBuilder replacement = extendedBufferBuilder.sodium$getDelegate();
if (replacement != null) {
consumer = replacement;
}
}

return consumer;
}

private void removeReady() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ public void endBlock() {
this.currentLocalPosZ = 0;
}

@Override
public short getCurrentBlock() {
return currentBlock;
}

@Override
public VertexFormat.Mode getMode() {
return mode;
}

@Override
public int getStride() {
return format.getVertexSize();
}

@Unique
private void putInt(int i, int value) {
this.buffer.putInt(this.nextElementByte + i, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package net.coderbot.iris.vertices;

import com.mojang.blaze3d.vertex.VertexFormat;

public interface BlockSensitiveBufferBuilder {
void beginBlock(short block, short renderType, int localPosX, int localPosY, int localPosZ);

void endBlock();
short getCurrentBlock();

VertexFormat.Mode getMode();

int getStride();
}
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"id": "iris",
"version": "1.6.14-development-environment",
"version": "1.6.15-development-environment",

"name": "Iris",
"description": "A modern shaders mod for Minecraft intended to be compatible with existing OptiFine shader packs",
Expand Down Expand Up @@ -46,7 +46,7 @@
"depends": {
"fabricloader": ">=0.12.3",
"minecraft": ["1.20.3", "1.20.4"],
"sodium": "0.5.6"
"sodium": "0.5.8"
},

"breaks": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.coderbot.iris.compat.sodium.impl.vertex_format;

import net.caffeinemc.mods.sodium.api.memory.MemoryIntrinsics;
import net.caffeinemc.mods.sodium.api.util.NormI8;
import net.caffeinemc.mods.sodium.api.vertex.format.common.ModelVertex;
import net.caffeinemc.mods.sodium.api.vertex.serializer.VertexSerializer;
Expand Down Expand Up @@ -29,7 +30,7 @@ public void serialize(long src, long dst, int vertexCount) {
midV /= 4;

for (int j = 0; j < 4; j++) {
MemoryUtil.memCopy(src, dst, 24);
MemoryIntrinsics.copyMemory(src, dst, 24);
MemoryUtil.memPutInt(dst + 24, MemoryUtil.memGetInt(src + 28L));
MemoryUtil.memPutInt(dst + 28, normal);
MemoryUtil.memPutShort(dst + 32, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedEntity());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.coderbot.iris.compat.sodium.impl.vertex_format;

import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import net.caffeinemc.mods.sodium.api.memory.MemoryIntrinsics;
import net.caffeinemc.mods.sodium.api.vertex.serializer.VertexSerializer;
import net.coderbot.iris.compat.sodium.impl.vertex_format.entity_xhfp.QuadViewEntity;
import net.coderbot.iris.uniforms.CapturedRenderingState;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void serialize(long src, long dst, int vertexCount) {
uSum += u;
vSum += v;

MemoryUtil.memCopy(src, dst, 28);
MemoryIntrinsics.copyMemory(src, dst, 28);

MemoryUtil.memPutShort(dst + 32, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedEntity());
MemoryUtil.memPutShort(dst + 34, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedBlockEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class IrisCommonVertexAttributes {
public static CommonVertexAttribute BLOCK_ID;
public static CommonVertexAttribute ENTITY_ID;
public static CommonVertexAttribute MID_BLOCK;
public static CommonVertexAttribute PADDING2;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.coderbot.iris.compat.sodium.impl.vertex_format;

import net.caffeinemc.mods.sodium.api.memory.MemoryIntrinsics;
import net.caffeinemc.mods.sodium.api.util.NormI8;
import net.caffeinemc.mods.sodium.api.vertex.format.common.ModelVertex;
import net.caffeinemc.mods.sodium.api.vertex.serializer.VertexSerializer;
Expand Down Expand Up @@ -30,7 +31,7 @@ public void serialize(long src, long dst, int vertexCount) {
midV /= 4;

for (int j = 0; j < 4; j++) {
MemoryUtil.memCopy(src, dst, 36);
MemoryIntrinsics.copyMemory(src, dst, 36);
MemoryUtil.memPutShort(dst + 36, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedEntity());
MemoryUtil.memPutShort(dst + 38, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedBlockEntity());
MemoryUtil.memPutShort(dst + 40, (short) CapturedRenderingState.INSTANCE.getCurrentRenderedItem());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.coderbot.iris.compat.sodium.impl.vertex_format;

import net.coderbot.iris.vertices.QuadView;
import net.coderbot.iris.vertices.TriView;

public class SodiumTriView implements QuadView {
private long address;
private int stride;
private int textureOffset;

public void setInfo(long address, int stride, int textureOffset) {
this.address = address;
this.stride = stride;
this.textureOffset = textureOffset;
}

@Override
public float x(int index) {
return address - stride * (3L - index);
}

@Override
public float y(int index) {
return address + 4 - stride * (3L - index);
}

@Override
public float z(int index) {
return address + 8 - stride * (3L - index);
}

@Override
public float u(int index) {
return address + textureOffset - stride * (3L - index);
}

@Override
public float v(int index) {
return address + (textureOffset + 4
) - stride * (3L - index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ public class MixinCommonVertexAttributes {
= CommonVertexAttributeAccessor.createCommonVertexElement("ENTITY_ID", baseOrdinal + 3, IrisVertexFormats.ENTITY_ID_ELEMENT);
IrisCommonVertexAttributes.MID_BLOCK
= CommonVertexAttributeAccessor.createCommonVertexElement("MID_BLOCK", baseOrdinal + 4, IrisVertexFormats.MID_BLOCK_ELEMENT);
IrisCommonVertexAttributes.PADDING2
= CommonVertexAttributeAccessor.createCommonVertexElement("PADDING2", baseOrdinal + 5, IrisVertexFormats.PADDING_SHORT);

$VALUES = ArrayUtils.addAll($VALUES,
IrisCommonVertexAttributes.TANGENT,
IrisCommonVertexAttributes.MID_TEX_COORD,
IrisCommonVertexAttributes.BLOCK_ID,
IrisCommonVertexAttributes.ENTITY_ID,
IrisCommonVertexAttributes.MID_BLOCK);
IrisCommonVertexAttributes.MID_BLOCK,
IrisCommonVertexAttributes.PADDING2);

COUNT = $VALUES.length;
}
Expand Down
Loading

0 comments on commit 3b17b9a

Please sign in to comment.