Skip to content

Commit

Permalink
Vec3i uniforms and time uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Nov 7, 2023
1 parent db13e88 commit 8e67526
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/net/coderbot/iris/gl/IrisRenderSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public static void uniform3f(int location, float v0, float v1, float v2) {
GL32C.glUniform3f(location, v0, v1, v2);
}

public static void uniform3i(int location, int v0, int v1, int v2) {
RenderSystem.assertOnRenderThreadOrInit();
GL32C.glUniform3i(location, v0, v1, v2);
}

public static void uniform4f(int location, float v0, float v1, float v2, float v3) {
RenderSystem.assertOnRenderThreadOrInit();
GL32C.glUniform4f(location, v0, v1, v2, v3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private static UniformType getExpectedType(int type) {
} else if (type == GL20C.GL_FLOAT_VEC3) {
return UniformType.VEC3;
} else if (type == GL20C.GL_INT_VEC3) {
return null;
return UniformType.VEC3I;
} else if (type == GL20C.GL_FLOAT_MAT2) {
return null;
} else if (type == GL20C.GL_FLOAT_VEC2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.coderbot.iris.vendored.joml.Vector2i;
import net.coderbot.iris.vendored.joml.Vector3d;
import net.coderbot.iris.vendored.joml.Vector3f;
import net.coderbot.iris.vendored.joml.Vector3i;
import net.coderbot.iris.vendored.joml.Vector4f;

import java.util.OptionalInt;
Expand Down Expand Up @@ -74,6 +75,13 @@ default LocationalUniformHolder uniform3f(UniformUpdateFrequency updateFrequency
return this;
}

@Override
default LocationalUniformHolder uniform3i(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector3i> value) {
location(name, UniformType.VEC3I).ifPresent(id -> addUniform(updateFrequency, new Vector3IntegerUniform(id, value)));

return this;
}

@Override
default LocationalUniformHolder uniformVanilla3f(UniformUpdateFrequency updateFrequency, String name, Supplier<com.mojang.math.Vector3f> value) {
location(name, UniformType.VEC3).ifPresent(id -> addUniform(updateFrequency, new VanillaVector3Uniform(id, value)));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/coderbot/iris/gl/uniform/UniformHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.coderbot.iris.vendored.joml.Vector2i;
import net.coderbot.iris.vendored.joml.Vector3d;
import net.coderbot.iris.vendored.joml.Vector3f;
import net.coderbot.iris.vendored.joml.Vector3i;
import net.coderbot.iris.vendored.joml.Vector4f;

import java.util.function.BooleanSupplier;
Expand All @@ -28,6 +29,7 @@ public interface UniformHolder {
UniformHolder uniform2i(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector2i> value);

UniformHolder uniform3f(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector3f> value);
UniformHolder uniform3i(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector3i> value);

UniformHolder uniformVanilla3f(UniformUpdateFrequency updateFrequency, String name, Supplier<com.mojang.math.Vector3f> value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum UniformType {
VEC2,
VEC2I,
VEC3,
VEC3I,
VEC4,
VEC4I
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.coderbot.iris.gl.uniform;

import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.state.ValueUpdateNotifier;
import net.coderbot.iris.vendored.joml.Vector3d;
import net.coderbot.iris.vendored.joml.Vector3f;
import net.coderbot.iris.vendored.joml.Vector3i;
import net.coderbot.iris.vendored.joml.Vector4f;

import java.util.function.Supplier;

public class Vector3IntegerUniform extends Uniform {
private final Vector3i cachedValue;
private final Supplier<Vector3i> value;

Vector3IntegerUniform(int location, Supplier<Vector3i> value) {
super(location);

this.cachedValue = new Vector3i();
this.value = value;
}

Vector3IntegerUniform(int location, Supplier<Vector3i> value, ValueUpdateNotifier notifier) {
super(location, notifier);

this.cachedValue = new Vector3i();
this.value = value;
}
@Override
public void update() {
updateValue();

if (notifier != null) {
notifier.setListener(this::updateValue);
}
}

private void updateValue() {
Vector3i newValue = value.get();

if (!newValue.equals(cachedValue)) {
cachedValue.set(newValue.x(), newValue.y(), newValue.z());
IrisRenderSystem.uniform3i(location, cachedValue.x(), cachedValue.y(), cachedValue.z());
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/net/coderbot/iris/mixin/MixinLevelRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.coderbot.iris.shadows.frustum.fallback.NonCullingFrustum;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.IrisTimeUniforms;
import net.coderbot.iris.uniforms.SystemTimeUniforms;
import net.coderbot.iris.vendored.joml.Vector3d;
import net.fabricmc.api.EnvType;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class MixinLevelRenderer {
" didn't work. This is a bug! Please report it to the Iris developers.");
}

IrisTimeUniforms.updateTime();
CapturedRenderingState.INSTANCE.setGbufferModelView(poseStack.last().pose());
CapturedRenderingState.INSTANCE.setGbufferProjection(projection);
CapturedRenderingState.INSTANCE.setTickDelta(tickDelta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static void addNonDynamicUniforms(UniformHolder uniforms, IdMap idMap, Pa
BiomeParameters.addBiomeUniforms(uniforms);
new CelestialUniforms(directives.getSunPathRotation()).addCelestialUniforms(uniforms);
IrisExclusiveUniforms.addIrisExclusiveUniforms(uniforms);
IrisTimeUniforms.addTimeUniforms(uniforms);
MatrixUniforms.addMatrixUniforms(uniforms, directives);
IdMapUniforms.addIdMapUniforms(updateNotifier, uniforms, idMap, directives.isOldHandLight());
CommonUniforms.generalCommonUniforms(uniforms, updateNotifier, directives);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/coderbot/iris/uniforms/IrisTimeUniforms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.coderbot.iris.uniforms;

import net.coderbot.iris.gl.uniform.UniformHolder;
import net.coderbot.iris.gl.uniform.UniformUpdateFrequency;
import net.coderbot.iris.vendored.joml.Vector3i;

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalField;

public class IrisTimeUniforms {
private static LocalDateTime dateTime;

public static void updateTime() {
dateTime = LocalDateTime.now();
}

public static void addTimeUniforms(UniformHolder uniforms) {
Vector3i date = new Vector3i();
Vector3i time = new Vector3i();
uniforms.uniform3i(UniformUpdateFrequency.PER_TICK, "currentDate", () -> date.set(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth()));
uniforms.uniform3i(UniformUpdateFrequency.PER_TICK, "currentTime", () -> time.set(dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public Builder uniform3f(UniformUpdateFrequency updateFrequency, String name, Su
return this.put(name, new Float3VectorCachedUniform(name, updateFrequency, value));
}

@Override
public Builder uniform3i(UniformUpdateFrequency updateFrequency, String name, Supplier<Vector3i> value) {
return this.put(name, new Int3VectorCachedUniform(name, updateFrequency, value));
}

@Override
public UniformHolder uniformVanilla3f(UniformUpdateFrequency updateFrequency, String name, Supplier<com.mojang.math.Vector3f> value) {
return this.put(name, new Float3VanillaVectorCachedUniform(name, updateFrequency, value));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.coderbot.iris.uniforms.custom.cached;

import net.coderbot.iris.gl.uniform.UniformUpdateFrequency;
import net.coderbot.iris.parsing.VectorType;
import net.coderbot.iris.vendored.joml.Vector3f;
import net.coderbot.iris.vendored.joml.Vector3i;
import org.lwjgl.opengl.GL21;

import java.util.function.Supplier;

public class Int3VectorCachedUniform extends VectorCachedUniform<Vector3i> {

public Int3VectorCachedUniform(String name, UniformUpdateFrequency updateFrequency, Supplier<Vector3i> supplier) {
super(name, updateFrequency, new Vector3i(), supplier);
}

@Override
protected void setFrom(Vector3i other) {
this.cached.set(other);
}

@Override
public void push(int location) {
GL21.glUniform3i(location, this.cached.x, this.cached.y, this.cached.z);
}

@Override
public VectorType getType() {
return VectorType.I_VEC3;
}
}

0 comments on commit 8e67526

Please sign in to comment.