Skip to content

Commit

Permalink
Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Aug 11, 2023
1 parent 6c44fd0 commit c63cfd6
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/main/java/kroppeb/stareval/function/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static UniformType convert(Type type) {
else if (type == VectorType.VEC2) return UniformType.VEC2;
else if (type == VectorType.VEC3) return UniformType.VEC3;
else if (type == VectorType.VEC4) return UniformType.VEC4;
else if (type == VectorType.I_VEC2) return UniformType.VEC2I;
else if (type == VectorType.I_VEC2) return UniformType.IVEC2;
else if (type == MatrixType.MAT4) return UniformType.MAT4;
else throw new IllegalArgumentException("Unsupported custom uniform type: " + type);
}
Expand Down
128 changes: 128 additions & 0 deletions src/main/java/net/coderbot/iris/gl/buffer/IrisUniformBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package net.coderbot.iris.gl.buffer;

import it.unimi.dsi.fastutil.Pair;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.uniform.LocationalUniformHolder;
import net.coderbot.iris.gl.uniform.Uniform;
import net.coderbot.iris.gl.uniform.UniformHolder;
import net.coderbot.iris.gl.uniform.UniformType;
import net.coderbot.iris.gl.uniform.UniformUpdateFrequency;
import net.coderbot.iris.uniforms.SystemTimeUniforms;
import net.minecraft.client.Minecraft;
import org.lwjgl.opengl.GL46C;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;

public class IrisUniformBuffer implements LocationalUniformHolder {
private Map<UniformUpdateFrequency, List<Uniform>> uniformList = new HashMap<>();
private int size;
private ArrayList<Pair<UniformUpdateFrequency, Uniform>> uniformOrder;
private long lastTick;
private UniformBuffer buffer;

private int align(int bufferSize, int alignment) {
return (((bufferSize - 1) + alignment) & -alignment);
}

public void finish() {
this.uniformOrder = new ArrayList<>();
for (UniformUpdateFrequency frequency : uniformList.keySet()) {
for (Uniform uniform : uniformList.get(frequency)) {
uniformOrder.add(Pair.of(frequency, uniform));
}
}

uniformOrder.sort(Comparator.comparing(Pair::first));

for (Pair<UniformUpdateFrequency, Uniform> uniform : uniformOrder) {
size = align(size, uniform.second().getAlignment());
uniform.second().setBufferIndex(size);
Iris.logger.warn("Uniform " + uniform.second().getType().name() + " added at " + size);
size += uniform.second().getByteSize();
}

Iris.logger.warn("Final size: " + size);

Iris.logger.warn(getLayout());

buffer = new UniformBuffer(size);
}

public String getLayout() {
StringBuilder builder = new StringBuilder();

builder.append("layout (std140, binding = 1) uniform CommonUniforms {\n");
uniformOrder.forEach(uniformInformation -> builder.append(uniformInformation.second().getType().name().toLowerCase()).append(" ").append(uniformInformation.second().getName()).append(";").append("\n"));


builder.append("};");

return builder.toString();
}

private static long getCurrentTick() {
if (Minecraft.getInstance().level == null) {
return 0L;
} else {
return Minecraft.getInstance().level.getGameTime();
}
}

private boolean hasUpdatedOnce = false;

public void updateUniforms() {
boolean updateOnce = false;
boolean updateTickUniforms = false;

if (!hasUpdatedOnce) {
updateOnce = true;
hasUpdatedOnce = true;
}

long currentTick = getCurrentTick();

if (lastTick != currentTick) {
lastTick = currentTick;
updateTickUniforms = true;
}

boolean finalUpdateOnce = updateOnce;
boolean finalUpdateTickUniforms = updateTickUniforms;
uniformOrder.forEach((pair) -> {
switch (pair.first()) {
case ONCE -> {
if (!finalUpdateOnce) return;
}
case PER_TICK -> {
if (!finalUpdateTickUniforms) return;
}
}

pair.second().updateBuffer(buffer.getWriteAddressForFrame());
});

buffer.updateFrame();

}

@Override
public UniformHolder externallyManagedUniform(String name, UniformType type) {
return this;
}

@Override
public LocationalUniformHolder addUniform(UniformUpdateFrequency updateFrequency, Uniform uniform) {
uniformList.computeIfAbsent(updateFrequency, a -> new ArrayList<>()).add(uniform);
return this;
}

@Override
public OptionalInt location(String name, UniformType type) {
return OptionalInt.of(1);
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/coderbot/iris/gl/buffer/UniformBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ public void updateFrame() {
if (frameId == 3) {
frameId = 0;
}

GL46C.glBindBufferRange(GL46C.GL_UNIFORM_BUFFER, 2, buffer, (size * lastFrameId), size);
}

public void destroy() {
GL46C.glUnmapNamedBuffer(buffer);
GL46C.glDeleteBuffers(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private static UniformType getExpectedType(int type) {
} else if (type == GL20C.GL_FLOAT_VEC4) {
return UniformType.VEC4;
} else if (type == GL20C.GL_INT_VEC4) {
return UniformType.VEC4I;
return UniformType.IVEC4;
} else if (type == GL20C.GL_FLOAT_MAT3) {
return UniformType.MAT3;
} else if (type == GL20C.GL_FLOAT_VEC3) {
Expand All @@ -329,7 +329,7 @@ private static UniformType getExpectedType(int type) {
} else if (type == GL20C.GL_FLOAT_VEC2) {
return UniformType.VEC2;
} else if (type == GL20C.GL_INT_VEC2) {
return UniformType.VEC2I;
return UniformType.IVEC2;
} else if (type == GL20C.GL_SAMPLER_3D) {
return UniformType.INT;
} else if (type == GL20C.GL_SAMPLER_2D) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.function.BooleanSupplier;

public class BooleanUniform extends IntUniform {
BooleanUniform(int location, BooleanSupplier value) {
super(location, () -> value.getAsBoolean() ? 1 : 0);
BooleanUniform(String name, int location, BooleanSupplier value) {
super(name, location, () -> value.getAsBoolean() ? 1 : 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,67 @@ public interface DynamicLocationalUniformHolder extends LocationalUniformHolder,
DynamicLocationalUniformHolder addDynamicUniform(Uniform uniform, ValueUpdateNotifier notifier);

default DynamicLocationalUniformHolder uniform1f(String name, FloatSupplier value, ValueUpdateNotifier notifier) {
location(name, UniformType.FLOAT).ifPresent(id -> addDynamicUniform(new FloatUniform(id, value, notifier), notifier));
location(name, UniformType.FLOAT).ifPresent(id -> addDynamicUniform(new FloatUniform(name, id, value, notifier), notifier));

return this;
}

default DynamicLocationalUniformHolder uniform1f(String name, IntSupplier value, ValueUpdateNotifier notifier) {
location(name, UniformType.FLOAT).ifPresent(id -> addDynamicUniform(new FloatUniform(id, () -> (float) value.getAsInt(), notifier), notifier));
location(name, UniformType.FLOAT).ifPresent(id -> addDynamicUniform(new FloatUniform(name, id, () -> (float) value.getAsInt(), notifier), notifier));

return this;
}

default DynamicLocationalUniformHolder uniform1f(String name, DoubleSupplier value, ValueUpdateNotifier notifier) {
location(name, UniformType.FLOAT).ifPresent(id -> addDynamicUniform(new FloatUniform(id, () -> (float) value.getAsDouble(), notifier), notifier));
location(name, UniformType.FLOAT).ifPresent(id -> addDynamicUniform(new FloatUniform(name, id, () -> (float) value.getAsDouble(), notifier), notifier));

return this;
}

default DynamicLocationalUniformHolder uniform1i(String name, IntSupplier value, ValueUpdateNotifier notifier) {
location(name, UniformType.INT).ifPresent(id -> addDynamicUniform(new IntUniform(id, value, notifier), notifier));
location(name, UniformType.INT).ifPresent(id -> addDynamicUniform(new IntUniform(name, id, value, notifier), notifier));

return this;
}

default DynamicLocationalUniformHolder uniform2f(String name, Supplier<Vector2f> value, ValueUpdateNotifier notifier) {
location(name, UniformType.VEC2).ifPresent(id -> addDynamicUniform(new Vector2Uniform(id, value, notifier), notifier));
location(name, UniformType.VEC2).ifPresent(id -> addDynamicUniform(new Vector2Uniform(name, id, value, notifier), notifier));

return this;
}

default DynamicLocationalUniformHolder uniform2i(String name, Supplier<Vector2i> value, ValueUpdateNotifier notifier) {
location(name, UniformType.VEC2I).ifPresent(id -> addDynamicUniform(new Vector2IntegerJomlUniform(id, value, notifier), notifier));
location(name, UniformType.IVEC2).ifPresent(id -> addDynamicUniform(new Vector2IntegerJomlUniform(name, id, value, notifier), notifier));

return this;
}

default DynamicUniformHolder uniform3f(String name, Supplier<Vector3f> value, ValueUpdateNotifier notifier) {
location(name, UniformType.VEC3).ifPresent(id -> addDynamicUniform(new Vector3Uniform(id, value, notifier), notifier));
location(name, UniformType.VEC3).ifPresent(id -> addDynamicUniform(new Vector3Uniform(name, id, value, notifier), notifier));

return this;
}

default DynamicUniformHolder uniform4f(String name, Supplier<Vector4f> value, ValueUpdateNotifier notifier) {
location(name, UniformType.VEC4).ifPresent(id -> addDynamicUniform(new Vector4Uniform(id, value, notifier), notifier));
location(name, UniformType.VEC4).ifPresent(id -> addDynamicUniform(new Vector4Uniform(name, id, value, notifier), notifier));

return this;
}

default DynamicUniformHolder uniform4fArray(String name, Supplier<float[]> value, ValueUpdateNotifier notifier) {
location(name, UniformType.VEC4).ifPresent(id -> addDynamicUniform(new Vector4ArrayUniform(id, value, notifier), notifier));
location(name, UniformType.VEC4).ifPresent(id -> addDynamicUniform(new Vector4ArrayUniform(name, id, value, notifier), notifier));

return this;
}

default DynamicUniformHolder uniform4i(String name, Supplier<Vector4i> value, ValueUpdateNotifier notifier) {
location(name, UniformType.VEC4I).ifPresent(id -> addDynamicUniform(new Vector4IntegerJomlUniform(id, value, notifier), notifier));
location(name, UniformType.IVEC4).ifPresent(id -> addDynamicUniform(new Vector4IntegerJomlUniform(name, id, value, notifier), notifier));

return this;
}

default DynamicUniformHolder uniformMatrix(String name, Supplier<Matrix4f> value, ValueUpdateNotifier notifier) {
location(name, UniformType.MAT4).ifPresent(id -> addDynamicUniform(new MatrixUniform(id, value, notifier), notifier));
location(name, UniformType.MAT4).ifPresent(id -> addDynamicUniform(new MatrixUniform(name, id, value, notifier), notifier));

return this;
}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/net/coderbot/iris/gl/uniform/FloatUniform.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.state.ValueUpdateNotifier;
import org.lwjgl.system.MemoryUtil;

public class FloatUniform extends Uniform {
private float cachedValue;
private final FloatSupplier value;

FloatUniform(int location, FloatSupplier value) {
this(location, value, null);
FloatUniform(String name, int location, FloatSupplier value) {
this(name, location, value, null);
}

FloatUniform(int location, FloatSupplier value, ValueUpdateNotifier notifier) {
super(location, notifier);
FloatUniform(String name, int location, FloatSupplier value, ValueUpdateNotifier notifier) {
super(name, location, notifier);

this.cachedValue = 0;
this.value = value;
Expand Down Expand Up @@ -42,6 +43,16 @@ public UniformType getType() {
return UniformType.FLOAT;
}

@Override
public void updateBuffer(long address) {
float newValue = value.getAsFloat();

if (cachedValue != newValue) {
cachedValue = newValue;
MemoryUtil.memPutFloat(address + bufferIndex, newValue);
}
}

private void updateValue() {
float newValue = value.getAsFloat();

Expand Down
19 changes: 15 additions & 4 deletions src/main/java/net/coderbot/iris/gl/uniform/IntUniform.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import com.mojang.blaze3d.systems.RenderSystem;
import net.coderbot.iris.gl.state.ValueUpdateNotifier;
import org.lwjgl.system.MemoryUtil;

import java.util.function.IntSupplier;

public class IntUniform extends Uniform {
private int cachedValue;
private final IntSupplier value;

IntUniform(int location, IntSupplier value) {
this(location, value, null);
IntUniform(String name, int location, IntSupplier value) {
this(name, location, value, null);
}

IntUniform(int location, IntSupplier value, ValueUpdateNotifier notifier) {
super(location, notifier);
IntUniform(String name, int location, IntSupplier value, ValueUpdateNotifier notifier) {
super(name, location, notifier);

this.cachedValue = 0;
this.value = value;
Expand All @@ -39,6 +40,16 @@ public int getAlignment() {
return 4;
}

@Override
public void updateBuffer(long address) {
int newValue = value.getAsInt();

if (cachedValue != newValue) {
cachedValue = newValue;
MemoryUtil.memPutInt(address + bufferIndex, newValue);
}
}

@Override
public UniformType getType() {
return UniformType.INT;
Expand Down
Loading

0 comments on commit c63cfd6

Please sign in to comment.