Skip to content

Commit

Permalink
Add support for full-precision vertex format
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Oct 8, 2023
1 parent f92a389 commit b26c536
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public class SodiumClientMod {

public static boolean flywheelLoaded = false;
public static boolean cclLoaded = false;
public static boolean oculusLoaded = false;
public static boolean immersiveLoaded = FMLLoader.getLoadingModList().getModFileById("immersiveengineering") != null;

public SodiumClientMod() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
MinecraftForge.EVENT_BUS.addListener(this::registerReloadListener);
MOD_VERSION = ModList.get().getModContainerById(MODID).get().getModInfo().getVersion().toString();

oculusLoaded = ModList.get().isLoaded("oculus");

ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY, (a, b) -> true));
}

Expand Down Expand Up @@ -106,4 +109,8 @@ public static String getVersion() {
public static boolean isDirectMemoryAccessEnabled() {
return options().advanced.allowDirectMemoryAccess;
}

public static boolean canUseVanillaVertices() {
return !SodiumClientMod.options().performance.useCompactVertexFormat && !oculusLoaded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public record GlVertexAttributeFormat(int typeId, int size) {
public static final GlVertexAttributeFormat FLOAT = new GlVertexAttributeFormat(GL20C.GL_FLOAT, 4);
public static final GlVertexAttributeFormat UNSIGNED_SHORT = new GlVertexAttributeFormat(GL20C.GL_UNSIGNED_SHORT, 2);
public static final GlVertexAttributeFormat UNSIGNED_BYTE = new GlVertexAttributeFormat(GL20C.GL_UNSIGNED_BYTE, 1);
public static final GlVertexAttributeFormat UNSIGNED_INT = new GlVertexAttributeFormat(GL20C.GL_UNSIGNED_INT, 4);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import me.jellysquid.mods.sodium.client.gl.buffer.GlBuffer;
import me.jellysquid.mods.sodium.client.gl.buffer.GlBufferTarget;

import java.util.Objects;

public record TessellationBinding(GlBufferTarget target,
GlBuffer buffer,
GlVertexAttributeBinding[] attributeBindings) {
public static TessellationBinding forVertexBuffer(GlBuffer buffer, GlVertexAttributeBinding[] attributes) {
Objects.requireNonNull(attributes);
return new TessellationBinding(GlBufferTarget.ARRAY_BUFFER, buffer, attributes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ public static OptionPage performance() {
.setFlags(OptionFlag.REQUIRES_RENDERER_UPDATE)
.build()
)
.add(OptionImpl.createBuilder(boolean.class, sodiumOpts)
.setName(new TranslatableText("sodium.options.use_compact_vertex_format.name"))
.setTooltip(new TranslatableText("sodium.options.use_compact_vertex_format.tooltip"))
.setControl(TickBoxControl::new)
.setImpact(OptionImpact.MEDIUM)
.setBinding((opts, value) -> {
opts.performance.useCompactVertexFormat = value;
}, opts -> opts.performance.useCompactVertexFormat)
.setFlags(OptionFlag.REQUIRES_RENDERER_RELOAD)
.build()
)
.add(OptionImpl.createBuilder(boolean.class, sodiumOpts)
.setName(new TranslatableText("sodium.options.use_fog_occlusion.name"))
.setTooltip(new TranslatableText("sodium.options.use_fog_occlusion.tooltip"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static class PerformanceSettings {
public boolean useParticleCulling = true;
public boolean useFogOcclusion = true;
public boolean useBlockFaceCulling = true;
public boolean useCompactVertexFormat = true;
}

public static class AdvancedSettings {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package me.jellysquid.mods.sodium.client.model.vertex.type;

import me.jellysquid.mods.sodium.client.render.chunk.format.ChunkMeshAttribute;
import me.jellysquid.mods.sodium.client.render.chunk.format.ModelVertexSink;

public interface ChunkVertexType extends BlittableVertexType<ModelVertexSink>, CustomVertexType<ModelVertexSink, ChunkMeshAttribute> {
public interface ChunkVertexType<A extends Enum<A>> extends BlittableVertexType<ModelVertexSink>, CustomVertexType<ModelVertexSink, A> {
/**
* @return The scale to be applied to vertex coordinates
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public interface ChunkRenderer {
/**
* Returns the vertex format used by this chunk render backend for rendering meshes.
*/
ChunkVertexType getVertexType();
ChunkVertexType<?> getVertexType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import me.jellysquid.mods.sodium.client.SodiumClientMod;
import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexAttributeBinding;
import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexFormat;
import me.jellysquid.mods.sodium.client.gl.buffer.GlBufferUsage;
import me.jellysquid.mods.sodium.client.gl.buffer.GlMutableBuffer;
import me.jellysquid.mods.sodium.client.gl.device.CommandList;
Expand All @@ -18,6 +19,8 @@
import me.jellysquid.mods.sodium.client.model.vertex.type.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.data.ChunkRenderBounds;
import me.jellysquid.mods.sodium.client.render.chunk.format.ChunkMeshAttribute;
import me.jellysquid.mods.sodium.client.render.chunk.format.ChunkModelVertexFormats;
import me.jellysquid.mods.sodium.client.render.chunk.format.VanillaLikeChunkMeshAttribute;
import me.jellysquid.mods.sodium.client.render.chunk.passes.BlockRenderPass;
import me.jellysquid.mods.sodium.client.render.chunk.region.RenderRegion;
import me.jellysquid.mods.sodium.client.render.chunk.shader.ChunkShaderBindingPoints;
Expand All @@ -37,19 +40,38 @@ public class RegionChunkRenderer extends ShaderChunkRenderer {
private final GlMutableBuffer chunkInfoBuffer;
private final boolean isBlockFaceCullingEnabled = SodiumClientMod.options().performance.useBlockFaceCulling;

public RegionChunkRenderer(RenderDevice device, ChunkVertexType vertexType) {
private GlVertexAttributeBinding[] getBindingsForType() {
if(this.vertexType != ChunkModelVertexFormats.VANILLA_LIKE) {
GlVertexFormat<ChunkMeshAttribute> compactFormat = (GlVertexFormat<ChunkMeshAttribute>)this.vertexFormat;
return new GlVertexAttributeBinding[] {
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_POSITION_ID,
compactFormat.getAttribute(ChunkMeshAttribute.POSITION_ID)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_COLOR,
compactFormat.getAttribute(ChunkMeshAttribute.COLOR)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_BLOCK_TEXTURE,
compactFormat.getAttribute(ChunkMeshAttribute.BLOCK_TEXTURE)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_LIGHT_TEXTURE,
compactFormat.getAttribute(ChunkMeshAttribute.LIGHT_TEXTURE))
};
} else {
GlVertexFormat<VanillaLikeChunkMeshAttribute> vanillaFormat = (GlVertexFormat<VanillaLikeChunkMeshAttribute>)this.vertexFormat;
return new GlVertexAttributeBinding[] {
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_POSITION_ID,
vanillaFormat.getAttribute(VanillaLikeChunkMeshAttribute.POSITION)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_COLOR,
vanillaFormat.getAttribute(VanillaLikeChunkMeshAttribute.COLOR)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_BLOCK_TEXTURE,
vanillaFormat.getAttribute(VanillaLikeChunkMeshAttribute.BLOCK_TEX_ID)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_LIGHT_TEXTURE,
vanillaFormat.getAttribute(VanillaLikeChunkMeshAttribute.LIGHT)),
};
}
}

public RegionChunkRenderer(RenderDevice device, ChunkVertexType<?> vertexType) {
super(device, vertexType);

this.vertexAttributeBindings = new GlVertexAttributeBinding[] {
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_POSITION_ID,
this.vertexFormat.getAttribute(ChunkMeshAttribute.POSITION_ID)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_COLOR,
this.vertexFormat.getAttribute(ChunkMeshAttribute.COLOR)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_BLOCK_TEXTURE,
this.vertexFormat.getAttribute(ChunkMeshAttribute.BLOCK_TEXTURE)),
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_LIGHT_TEXTURE,
this.vertexFormat.getAttribute(ChunkMeshAttribute.LIGHT_TEXTURE))
};
this.vertexAttributeBindings = getBindingsForType();

try (CommandList commandList = device.createCommandList()) {
this.chunkInfoBuffer = commandList.createMutableBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import me.jellysquid.mods.sodium.client.compat.immersive.ImmersiveEmptyChunkChecker;
import me.jellysquid.mods.sodium.client.gl.device.CommandList;
import me.jellysquid.mods.sodium.client.gl.device.RenderDevice;
import me.jellysquid.mods.sodium.client.model.vertex.type.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer;
import me.jellysquid.mods.sodium.client.render.chunk.compile.ChunkBuildResult;
import me.jellysquid.mods.sodium.client.render.chunk.compile.ChunkBuilder;
Expand Down Expand Up @@ -103,12 +104,14 @@ public class RenderSectionManager {
private final ChunkTracker tracker;

public RenderSectionManager(SodiumWorldRenderer worldRenderer, BlockRenderPassManager renderPassManager, ClientWorld world, int renderDistance, CommandList commandList) {
this.chunkRenderer = new RegionChunkRenderer(RenderDevice.INSTANCE, ChunkModelVertexFormats.DEFAULT);
ChunkVertexType vertexType = SodiumClientMod.canUseVanillaVertices() ? ChunkModelVertexFormats.VANILLA_LIKE : ChunkModelVertexFormats.DEFAULT;

this.chunkRenderer = new RegionChunkRenderer(RenderDevice.INSTANCE, vertexType);

this.worldRenderer = worldRenderer;
this.world = world;

this.builder = new ChunkBuilder(ChunkModelVertexFormats.DEFAULT);
this.builder = new ChunkBuilder(vertexType);
this.builder.init(world, renderPassManager);

this.needsUpdate = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
public abstract class ShaderChunkRenderer implements ChunkRenderer {
private final Map<ChunkShaderOptions, GlProgram<ChunkShaderInterface>> programs = new Object2ObjectOpenHashMap<>();

protected final ChunkVertexType vertexType;
protected final GlVertexFormat<ChunkMeshAttribute> vertexFormat;
protected final ChunkVertexType<?> vertexType;
protected final GlVertexFormat<?> vertexFormat;

protected final RenderDevice device;

protected GlProgram<ChunkShaderInterface> activeProgram;

public ShaderChunkRenderer(RenderDevice device, ChunkVertexType vertexType) {
public ShaderChunkRenderer(RenderDevice device, ChunkVertexType<?> vertexType) {
this.device = device;
this.vertexType = vertexType;
this.vertexFormat = vertexType.getCustomVertexFormat();
Expand Down Expand Up @@ -88,7 +88,7 @@ public void delete() {
}

@Override
public ChunkVertexType getVertexType() {
public ChunkVertexType<?> getVertexType() {
return this.vertexType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class ChunkBuildBuffers {
private final VertexBufferBuilder[] vertexBuffers;
private final IndexBufferBuilder[][] indexBuffers;

private final ChunkVertexType vertexType;
private final ChunkVertexType<?> vertexType;

private final BlockRenderPassManager renderPassManager;

public ChunkBuildBuffers(ChunkVertexType vertexType, BlockRenderPassManager renderPassManager) {
public ChunkBuildBuffers(ChunkVertexType<?> vertexType, BlockRenderPassManager renderPassManager) {
this.vertexType = vertexType;
this.renderPassManager = renderPassManager;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.jellysquid.mods.sodium.client.render.chunk.format;

/**
* The attributes used by the compact chunk vertex format.
*/
public enum ChunkMeshAttribute {
POSITION_ID,
COLOR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.jellysquid.mods.sodium.client.render.chunk.format;

import me.jellysquid.mods.sodium.client.render.chunk.format.full.VanillaModelVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.format.sfp.ModelVertexType;

public class ChunkModelVertexFormats {
public static final ModelVertexType DEFAULT = new ModelVertexType();
public static final VanillaModelVertexType VANILLA_LIKE = new VanillaModelVertexType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.jellysquid.mods.sodium.client.render.chunk.format;

public enum VanillaLikeChunkMeshAttribute {
POSITION,
COLOR,
BLOCK_TEX_ID,
LIGHT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.jellysquid.mods.sodium.client.render.chunk.format.full;

import me.jellysquid.mods.sodium.client.model.vertex.buffer.VertexBufferView;
import me.jellysquid.mods.sodium.client.model.vertex.buffer.VertexBufferWriterNio;
import me.jellysquid.mods.sodium.client.render.chunk.format.ChunkModelVertexFormats;
import me.jellysquid.mods.sodium.client.render.chunk.format.ModelVertexSink;

import java.nio.ByteBuffer;

public class VanillaModelVertexBufferWriterNio extends VertexBufferWriterNio implements ModelVertexSink {
public VanillaModelVertexBufferWriterNio(VertexBufferView backingBuffer) {
super(backingBuffer, ChunkModelVertexFormats.VANILLA_LIKE);
}

@Override
public void writeVertex(float posX, float posY, float posZ, int color, float u, float v, int light, int chunkId) {
int i = this.writeOffset;

ByteBuffer buffer = this.byteBuffer;
buffer.putFloat(i + 0, posX);
buffer.putFloat(i + 4, posY);
buffer.putFloat(i + 8, posZ);
buffer.putInt(i + 12, color);

buffer.putShort(i + 16, VanillaModelVertexType.encodeBlockTexture(u));
buffer.putShort(i + 18, VanillaModelVertexType.encodeBlockTexture(v));
buffer.putShort(i + 20, (short) chunkId);

buffer.putInt(i + 24, VanillaModelVertexType.encodeLightMapTexCoord(light));

this.advance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.jellysquid.mods.sodium.client.render.chunk.format.full;

import me.jellysquid.mods.sodium.client.model.vertex.buffer.VertexBufferView;
import me.jellysquid.mods.sodium.client.model.vertex.buffer.VertexBufferWriterUnsafe;
import me.jellysquid.mods.sodium.client.render.chunk.format.ChunkModelVertexFormats;
import me.jellysquid.mods.sodium.client.render.chunk.format.ModelVertexSink;
import org.lwjgl.system.MemoryUtil;

public class VanillaModelVertexBufferWriterUnsafe extends VertexBufferWriterUnsafe implements ModelVertexSink {
public VanillaModelVertexBufferWriterUnsafe(VertexBufferView backingBuffer) {
super(backingBuffer, ChunkModelVertexFormats.VANILLA_LIKE);
}

@Override
public void writeVertex(float posX, float posY, float posZ, int color, float u, float v, int light, int chunkId) {
long i = this.writePointer;

MemoryUtil.memPutFloat(i + 0, posX);
MemoryUtil.memPutFloat(i + 4, posY);
MemoryUtil.memPutFloat(i + 8, posZ);
MemoryUtil.memPutInt(i + 12, color);

MemoryUtil.memPutShort(i + 16, VanillaModelVertexType.encodeBlockTexture(u));
MemoryUtil.memPutShort(i + 18, VanillaModelVertexType.encodeBlockTexture(v));
MemoryUtil.memPutShort(i + 20, (short) chunkId);

MemoryUtil.memPutInt(i + 24, VanillaModelVertexType.encodeLightMapTexCoord(light));

this.advance();
}
}
Loading

0 comments on commit b26c536

Please sign in to comment.