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

Translucency sort using compute shaders #963

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.lwjgl.opengl.GL20C;
import org.lwjgl.opengl.GL31C;
import org.lwjgl.opengl.GL43C;

public enum GlBufferTarget {
ARRAY_BUFFER(GL20C.GL_ARRAY_BUFFER, GL20C.GL_ARRAY_BUFFER_BINDING),
ELEMENT_BUFFER(GL20C.GL_ELEMENT_ARRAY_BUFFER, GL20C.GL_ELEMENT_ARRAY_BUFFER_BINDING),
COPY_READ_BUFFER(GL31C.GL_COPY_READ_BUFFER, GL31C.GL_COPY_READ_BUFFER),
COPY_WRITE_BUFFER(GL31C.GL_COPY_WRITE_BUFFER, GL31C.GL_COPY_WRITE_BUFFER);
COPY_WRITE_BUFFER(GL31C.GL_COPY_WRITE_BUFFER, GL31C.GL_COPY_WRITE_BUFFER),
SHADER_STORAGE_BUFFER(GL43C.GL_SHADER_STORAGE_BUFFER, GL43C.GL_SHADER_STORAGE_BUFFER_BINDING);

public static final GlBufferTarget[] VALUES = GlBufferTarget.values();
public static final int COUNT = VALUES.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.jellysquid.mods.sodium.client.gl.util.EnumBitField;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public interface CommandList extends AutoCloseable {
GlMutableBuffer createMutableBuffer();
Expand All @@ -21,10 +22,16 @@ public interface CommandList extends AutoCloseable {

void uploadData(GlMutableBuffer glBuffer, ByteBuffer byteBuffer, GlBufferUsage usage);

void bufferData(GlBufferTarget target, GlMutableBuffer glBuffer, int[] intArray, GlBufferUsage usage);

void bufferData(GlBufferTarget target, GlMutableBuffer glBuffer, IntBuffer intBuffer, GlBufferUsage usage);

void copyBufferSubData(GlBuffer src, GlBuffer dst, long readOffset, long writeOffset, long bytes);

void bindBuffer(GlBufferTarget target, GlBuffer buffer);

void bindBufferBase(GlBufferTarget target, int index, GlBuffer buffer);

void unbindVertexArray();

void allocateStorage(GlMutableBuffer buffer, long bufferSize, GlBufferUsage usage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ public void uploadData(GlMutableBuffer glBuffer, ByteBuffer byteBuffer, GlBuffer
glBuffer.setSize(byteBuffer.remaining());
}

@Override
public void bufferData(GlBufferTarget target, GlMutableBuffer glBuffer, int[] intArray, GlBufferUsage usage) {
this.bindBuffer(target, glBuffer);

GL15C.glBufferData(target.getTargetParameter(), intArray, usage.getId());
}

@Override
public void bufferData(GlBufferTarget target, GlMutableBuffer glBuffer, IntBuffer intBuffer, GlBufferUsage usage) {
this.bindBuffer(target, glBuffer);

GL15C.glBufferData(target.getTargetParameter(), intBuffer, usage.getId());
}

@Override
public void copyBufferSubData(GlBuffer src, GlBuffer dst, long readOffset, long writeOffset, long bytes) {
this.bindBuffer(GlBufferTarget.COPY_READ_BUFFER, src);
Expand All @@ -103,6 +117,11 @@ public void bindBuffer(GlBufferTarget target, GlBuffer buffer) {
}
}

@Override
public void bindBufferBase(GlBufferTarget target, int index, GlBuffer buffer) {
GL30C.glBindBufferBase(target.getTargetParameter(), index, buffer.handle());
}

@Override
public void unbindVertexArray() {
if (this.stateTracker.makeVertexArrayActive(null)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package me.jellysquid.mods.sodium.client.gl.shader;

import org.lwjgl.opengl.GL20C;
import org.lwjgl.opengl.GL43C;

/**
* An enumeration over the supported OpenGL shader types.
*/
public enum ShaderType {
VERTEX(GL20C.GL_VERTEX_SHADER),
FRAGMENT(GL20C.GL_FRAGMENT_SHADER);
FRAGMENT(GL20C.GL_FRAGMENT_SHADER),
COMPUTE(GL43C.GL_COMPUTE_SHADER);

public final int id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.lwjgl.opengl.GL32C;

public enum GlIndexType {
UNSIGNED_BYTE(GL32C.GL_UNSIGNED_BYTE, 1),
UNSIGNED_SHORT(GL32C.GL_UNSIGNED_SHORT, 2),
UNSIGNED_INT(GL32C.GL_UNSIGNED_INT, 4);

private final int id;
Expand All @@ -22,6 +20,4 @@ public int getFormatId() {
public int getStride() {
return this.stride;
}

public static final GlIndexType[] VALUES = GlIndexType.values();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import me.jellysquid.mods.sodium.client.gui.options.control.TickBoxControl;
import me.jellysquid.mods.sodium.client.gui.options.storage.MinecraftOptionsStorage;
import me.jellysquid.mods.sodium.client.gui.options.storage.SodiumOptionsStorage;
import me.jellysquid.mods.sodium.client.render.chunk.shader.ComputeShaderInterface;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.option.Option;
import net.minecraft.client.option.*;
import net.minecraft.client.util.Window;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;

Expand Down Expand Up @@ -292,6 +292,16 @@ public static OptionPage performance() {
.setFlags(OptionFlag.REQUIRES_RENDERER_UPDATE)
.build()
)
.add(OptionImpl.createBuilder(boolean.class, sodiumOpts)
.setName(new TranslatableText("sodium.options.translucent_face_sorting.name"))
.setTooltip(new TranslatableText("sodium.options.translucent_face_sorting.tooltip"))
.setControl(TickBoxControl::new)
.setImpact(OptionImpact.VARIES)
.setEnabled(ComputeShaderInterface.isSupported(RenderDevice.INSTANCE))
.setBinding((opts, value) -> opts.advanced.useTranslucentFaceSorting = value, opts -> opts.advanced.useTranslucentFaceSorting)
.setFlags(OptionFlag.REQUIRES_RENDERER_RELOAD)
.build()
)
.build());

return new OptionPage(new TranslatableText("sodium.options.pages.performance"), ImmutableList.copyOf(groups));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import me.jellysquid.mods.sodium.client.gui.options.TextProvider;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.option.GraphicsMode;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;

Expand Down Expand Up @@ -54,6 +53,7 @@ public static class AdvancedSettings {
public boolean allowDirectMemoryAccess = true;
public boolean enableMemoryTracing = false;
public boolean useAdvancedStagingBuffers = true;
public boolean useTranslucentFaceSorting = true;

public int cpuRenderAheadLimit = 3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,16 @@ public Result pop() {
return new Result(this.indices);
}

private static GlIndexType getOptimalIndexType(int count) {
if (count < 65536) {
return GlIndexType.UNSIGNED_SHORT;
} else {
return GlIndexType.UNSIGNED_INT;
}
}

public int getCount() {
return this.indices.size();
}

public static class Result {
private final IntArrayList indices;

private final int maxIndex, minIndex;
private final GlIndexType format;
private final GlIndexType format = GlIndexType.UNSIGNED_INT;

private Result(IntArrayList indices) {
this.indices = indices;

int maxIndex = Integer.MIN_VALUE;
int minIndex = Integer.MAX_VALUE;

IntIterator it = this.indices.iterator();

while (it.hasNext()) {
int i = it.nextInt();

minIndex = Math.min(minIndex, i);
maxIndex = Math.max(maxIndex, i);
}

this.minIndex = minIndex;
this.maxIndex = maxIndex;

this.format = getOptimalIndexType(this.maxIndex - this.minIndex);
}

public int writeTo(int offset, ByteBuffer buffer) {
Expand All @@ -78,14 +51,7 @@ public int writeTo(int offset, ByteBuffer buffer) {
int pointer = offset;

while (it.hasNext()) {
int value = it.nextInt() - this.minIndex;

switch (this.format) {
case UNSIGNED_BYTE -> buffer.put(pointer, (byte) value);
case UNSIGNED_SHORT -> buffer.putShort(pointer, (short) value);
case UNSIGNED_INT -> buffer.putInt(pointer, value);
}

buffer.putInt(pointer, it.nextInt());
pointer += stride;
}

Expand All @@ -101,7 +67,7 @@ public int getCount() {
}

public int getBaseVertex() {
return this.minIndex;
return 0;
}

public GlIndexType getFormat() {
Expand Down
Loading