mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-10-25 20:22:22 +00:00
Rework DH matrix handling (#1861)
This commit is contained in:
1 parent
f92ce1d24e
commit
307c563b6d
5 files changed
+64
-84
No files matched your search
@@ -59,7 +59,10 @@ public class CompatUniformManager {
|
||||
static final int MAT_FIELDS = 5;
|
||||
static final int LOC_MAT_BASE = LOC_LIGHT_BASE + 2 * LIGHT_FIELDS; // 43
|
||||
|
||||
static final int LOC_COUNT = LOC_MAT_BASE + MAT_FIELDS; // 48
|
||||
static final int LOC_IRIS_MODELVIEW_INVERSE = LOC_MAT_BASE + MAT_FIELDS; // 48
|
||||
static final int LOC_IRIS_PROJECTION_INVERSE = LOC_IRIS_MODELVIEW_INVERSE + 1; // 49
|
||||
|
||||
static final int LOC_COUNT = LOC_IRIS_PROJECTION_INVERSE + 1; // 50
|
||||
|
||||
private static final String[] LIGHT_FIELD_NAMES = {
|
||||
"ambient", "diffuse", "specular", "position", "halfVector",
|
||||
@@ -89,6 +92,8 @@ public class CompatUniformManager {
|
||||
UNIFORM_NAMES[LOC_IRIS_NORMAL] = "iris_NormalMatrix";
|
||||
UNIFORM_NAMES[LOC_IRIS_LIGHTMAP_TEXTURE_MATRIX] = "iris_LightmapTextureMatrix";
|
||||
UNIFORM_NAMES[LOC_IRIS_TEXTURE_MATRIX] = "iris_TextureMatrix";
|
||||
UNIFORM_NAMES[LOC_IRIS_MODELVIEW_INVERSE] = "iris_ModelViewMatrixInverse";
|
||||
UNIFORM_NAMES[LOC_IRIS_PROJECTION_INVERSE] = "iris_ProjectionMatrixInverse";
|
||||
UNIFORM_NAMES[LOC_ALPHA_TEST_REF] = "angelica_currentAlphaTest";
|
||||
UNIFORM_NAMES[LOC_SCENE_COLOR] = "angelica_SceneColor";
|
||||
UNIFORM_NAMES[LOC_CLIP_PLANES] = "angelica_ClipPlane[0]";
|
||||
@@ -229,10 +234,15 @@ public class CompatUniformManager {
|
||||
}
|
||||
|
||||
// ModelView Inverse
|
||||
if (locs[LOC_MODELVIEW_INVERSE] != -1) {
|
||||
if (locs[LOC_MODELVIEW_INVERSE] != -1 || locs[LOC_IRIS_MODELVIEW_INVERSE] != -1) {
|
||||
mv.invert(scratchMatrix);
|
||||
scratchMatrix.get(mat4Buf);
|
||||
RENDER_BACKEND.uniformMatrix4(locs[LOC_MODELVIEW_INVERSE], false, mat4Buf);
|
||||
if (locs[LOC_MODELVIEW_INVERSE] != -1) {
|
||||
RENDER_BACKEND.uniformMatrix4(locs[LOC_MODELVIEW_INVERSE], false, mat4Buf);
|
||||
}
|
||||
if (locs[LOC_IRIS_MODELVIEW_INVERSE] != -1) {
|
||||
RENDER_BACKEND.uniformMatrix4(locs[LOC_IRIS_MODELVIEW_INVERSE], false, mat4Buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,10 +276,15 @@ public class CompatUniformManager {
|
||||
}
|
||||
|
||||
// Projection Inverse
|
||||
if (locs[LOC_PROJECTION_INVERSE] != -1) {
|
||||
if (locs[LOC_PROJECTION_INVERSE] != -1 || locs[LOC_IRIS_PROJECTION_INVERSE] != -1) {
|
||||
proj.invert(scratchMatrix);
|
||||
scratchMatrix.get(mat4Buf);
|
||||
RENDER_BACKEND.uniformMatrix4(locs[LOC_PROJECTION_INVERSE], false, mat4Buf);
|
||||
if (locs[LOC_PROJECTION_INVERSE] != -1) {
|
||||
RENDER_BACKEND.uniformMatrix4(locs[LOC_PROJECTION_INVERSE], false, mat4Buf);
|
||||
}
|
||||
if (locs[LOC_IRIS_PROJECTION_INVERSE] != -1) {
|
||||
RENDER_BACKEND.uniformMatrix4(locs[LOC_IRIS_PROJECTION_INVERSE], false, mat4Buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.joml.Matrix4d;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.joml.Matrix4fStack;
|
||||
import org.joml.Vector3d;
|
||||
import org.joml.Vector3f;
|
||||
@@ -3437,6 +3438,21 @@ public class GLStateManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void setModelViewMatrix(Matrix4fc m) {
|
||||
if (isCachingEnabled()) {
|
||||
modelViewMatrix.set(m);
|
||||
mvGeneration++;
|
||||
mvLinearGeneration++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setProjectionMatrix(Matrix4fc m) {
|
||||
if (isCachingEnabled()) {
|
||||
projectionMatrix.set(m);
|
||||
projGeneration++;
|
||||
}
|
||||
}
|
||||
|
||||
public static Matrix4fStack getMatrixStack() {
|
||||
switch (matrixMode.getMode()) {
|
||||
case GL11.GL_MODELVIEW -> {
|
||||
|
||||
@@ -31,7 +31,6 @@ import net.coderbot.iris.uniforms.custom.CustomUniforms;
|
||||
import net.irisshaders.iris.api.v0.IrisApi;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture;
|
||||
import org.joml.Matrix3f;
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
@@ -46,12 +45,6 @@ import java.util.Map;
|
||||
|
||||
public class IrisGenericRenderProgram implements IDhApiGenericObjectShaderProgram {
|
||||
// Uniforms
|
||||
public final int modelViewUniform;
|
||||
public final int modelViewInverseUniform;
|
||||
public final int projectionUniform;
|
||||
public final int projectionInverseUniform;
|
||||
public final int normalMatrix3fUniform;
|
||||
// Fog/Clip Uniforms
|
||||
private final int id;
|
||||
private final ProgramUniforms uniforms;
|
||||
private final CustomUniforms customUniforms;
|
||||
@@ -62,7 +55,6 @@ public class IrisGenericRenderProgram implements IDhApiGenericObjectShaderProgra
|
||||
private final Matrix4f tempModel = new Matrix4f();
|
||||
private final Matrix4f tempProj = new Matrix4f();
|
||||
private final Matrix4f tempMat4 = new Matrix4f();
|
||||
private final Matrix3f tempMat3 = new Matrix3f();
|
||||
|
||||
private final int instancedShaderOffsetChunkUniform;
|
||||
private final int instancedShaderOffsetSubChunkUniform;
|
||||
@@ -143,12 +135,6 @@ public class IrisGenericRenderProgram implements IDhApiGenericObjectShaderProgra
|
||||
GLStateManager.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
|
||||
GLStateManager.glEnableVertexAttribArray(0);
|
||||
|
||||
projectionUniform = tryGetUniformLocation2("iris_ProjectionMatrix");
|
||||
projectionInverseUniform = tryGetUniformLocation2("iris_ProjectionMatrixInverse");
|
||||
modelViewUniform = tryGetUniformLocation2("iris_ModelViewMatrix");
|
||||
modelViewInverseUniform = tryGetUniformLocation2("iris_ModelViewMatrixInverse");
|
||||
normalMatrix3fUniform = tryGetUniformLocation2("iris_NormalMatrix");
|
||||
|
||||
this.instancedShaderOffsetChunkUniform = this.tryGetUniformLocation2("uOffsetChunk");
|
||||
this.instancedShaderOffsetSubChunkUniform = this.tryGetUniformLocation2("uOffsetSubChunk");
|
||||
this.instancedShaderCameraChunkPosUniform = this.tryGetUniformLocation2("uCameraPosChunk");
|
||||
@@ -213,18 +199,6 @@ public class IrisGenericRenderProgram implements IDhApiGenericObjectShaderProgra
|
||||
}
|
||||
}
|
||||
|
||||
public void setUniform(int index, Matrix3f matrix) {
|
||||
if (index == -1) return;
|
||||
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer buffer = stack.callocFloat(9);
|
||||
matrix.get(buffer);
|
||||
buffer.rewind();
|
||||
|
||||
GLStateManager.glUniformMatrix3(index, false, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
// Override ShaderProgram.bind()
|
||||
public void bind(DhApiRenderParam renderParam) {
|
||||
GLStateManager.glBindVertexArray(va);
|
||||
@@ -238,14 +212,8 @@ public class IrisGenericRenderProgram implements IDhApiGenericObjectShaderProgra
|
||||
toJOML(tempModel, renderParam.dhModelViewMatrix);
|
||||
toJOML(tempProj, renderParam.dhProjectionMatrix);
|
||||
|
||||
setUniform(modelViewUniform, tempModel);
|
||||
setUniform(projectionUniform, tempProj);
|
||||
this.setUniform(this.instancedShaderProjectionModelViewMatrixUniform, tempMat4.set(tempProj).mul(tempModel));
|
||||
tempModel.invert();
|
||||
tempProj.invert();
|
||||
setUniform(modelViewInverseUniform, tempModel);
|
||||
setUniform(projectionInverseUniform, tempProj);
|
||||
setUniform(normalMatrix3fUniform, tempModel.transpose3x3(tempMat3));
|
||||
LodRendererEvents.pushDhMatrices(tempProj, tempModel);
|
||||
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + IrisSamplers.LIGHTMAP_TEXTURE_UNIT);
|
||||
DynamicTexture lightmapTexture = ((EntityRendererAccessor) Minecraft.getMinecraft().entityRenderer).getLightmapTexture();
|
||||
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture.getGlTextureId());
|
||||
@@ -259,6 +227,7 @@ public class IrisGenericRenderProgram implements IDhApiGenericObjectShaderProgra
|
||||
}
|
||||
|
||||
public void unbind() {
|
||||
LodRendererEvents.popDhMatrices();
|
||||
GLStateManager.glBindVertexArray(0);
|
||||
GLStateManager.glUseProgram(0);
|
||||
ProgramUniforms.clearActiveUniforms();
|
||||
|
||||
@@ -3,7 +3,6 @@ package net.coderbot.iris.compat.dh;
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.mixins.interfaces.EntityRendererAccessor;
|
||||
import com.mitchej123.lwjgl.MemoryStack;
|
||||
import com.seibel.distanthorizons.api.DhApi;
|
||||
import com.seibel.distanthorizons.api.objects.math.DhApiVec3f;
|
||||
import net.coderbot.iris.gl.blending.BlendModeOverride;
|
||||
@@ -26,15 +25,12 @@ import net.coderbot.iris.uniforms.builtin.BuiltinReplacementUniforms;
|
||||
import net.coderbot.iris.uniforms.custom.CustomUniforms;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture;
|
||||
import org.joml.Matrix3f;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL32;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -44,11 +40,6 @@ public class IrisLodRenderProgram {
|
||||
public final int modelOffsetUniform;
|
||||
public final int worldYOffsetUniform;
|
||||
public final int mircoOffsetUniform;
|
||||
public final int modelViewUniform;
|
||||
public final int modelViewInverseUniform;
|
||||
public final int projectionUniform;
|
||||
public final int projectionInverseUniform;
|
||||
public final int normalMatrix3fUniform;
|
||||
// Fog/Clip Uniforms
|
||||
public final int clipDistanceUniform;
|
||||
private final int id;
|
||||
@@ -58,9 +49,6 @@ public class IrisLodRenderProgram {
|
||||
private final ProgramImages images;
|
||||
private final BlendModeOverride blend;
|
||||
private final BufferBlendOverride[] bufferBlendOverrides;
|
||||
private final Matrix4f tempMat4a = new Matrix4f();
|
||||
private final Matrix4f tempMat4b = new Matrix4f();
|
||||
private final Matrix3f tempMat3 = new Matrix3f();
|
||||
|
||||
// This will bind AbstractVertexAttribute
|
||||
private IrisLodRenderProgram(String name, boolean isShadowPass, boolean translucent, BlendModeOverride override, BufferBlendOverride[] bufferBlendOverrides, String vertex, String tessControl, String tessEval, String geometry, String fragment, CustomUniforms customUniforms, DeferredWorldRenderingPipeline pipeline) {
|
||||
@@ -132,11 +120,6 @@ public class IrisLodRenderProgram {
|
||||
modelOffsetUniform = tryGetUniformLocation2("modelOffset");
|
||||
worldYOffsetUniform = tryGetUniformLocation2("worldYOffset");
|
||||
mircoOffsetUniform = tryGetUniformLocation2("mircoOffset");
|
||||
projectionUniform = tryGetUniformLocation2("iris_ProjectionMatrix");
|
||||
projectionInverseUniform = tryGetUniformLocation2("iris_ProjectionMatrixInverse");
|
||||
modelViewUniform = tryGetUniformLocation2("iris_ModelViewMatrix");
|
||||
modelViewInverseUniform = tryGetUniformLocation2("iris_ModelViewMatrixInverse");
|
||||
normalMatrix3fUniform = tryGetUniformLocation2("iris_NormalMatrix");
|
||||
|
||||
// Fog/Clip Uniforms
|
||||
clipDistanceUniform = tryGetUniformLocation2("clipDistance");
|
||||
@@ -176,30 +159,6 @@ public class IrisLodRenderProgram {
|
||||
return GLStateManager.glGetUniformLocation(this.id, name);
|
||||
}
|
||||
|
||||
public void setUniform(int index, Matrix4fc matrix) {
|
||||
if (index == -1 || matrix == null) return;
|
||||
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer buffer = stack.callocFloat(16);
|
||||
matrix.get(buffer);
|
||||
buffer.rewind();
|
||||
|
||||
GLStateManager.glUniformMatrix4(index, false, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void setUniform(int index, Matrix3f matrix) {
|
||||
if (index == -1) return;
|
||||
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer buffer = stack.callocFloat(9);
|
||||
matrix.get(buffer);
|
||||
buffer.rewind();
|
||||
|
||||
GLStateManager.glUniformMatrix3(index, false, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
// Override ShaderProgram.bind()
|
||||
public void bind() {
|
||||
GLStateManager.glUseProgram(id);
|
||||
@@ -227,11 +186,8 @@ public class IrisLodRenderProgram {
|
||||
GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + IrisSamplers.LIGHTMAP_TEXTURE_UNIT);
|
||||
DynamicTexture lightmapTexture = ((EntityRendererAccessor) Minecraft.getMinecraft().entityRenderer).getLightmapTexture();
|
||||
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture.getGlTextureId());
|
||||
setUniform(modelViewUniform, modelView);
|
||||
setUniform(modelViewInverseUniform, modelView.invert(tempMat4a));
|
||||
setUniform(projectionUniform, projection);
|
||||
setUniform(projectionInverseUniform, projection.invert(tempMat4b));
|
||||
setUniform(normalMatrix3fUniform, tempMat4a.set(modelView).invert().transpose3x3(tempMat3));
|
||||
|
||||
LodRendererEvents.pushDhMatrices(projection, modelView);
|
||||
|
||||
setUniform(mircoOffsetUniform, 0.01f); // 0.01 block offset
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import net.coderbot.iris.shadows.ShadowRenderingState;
|
||||
import net.coderbot.iris.uniforms.CapturedRenderingState;
|
||||
import net.irisshaders.iris.api.v0.IrisApi;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
public class LodRendererEvents {
|
||||
@@ -46,6 +47,28 @@ public class LodRendererEvents {
|
||||
private static int textureHeight;
|
||||
private static final Matrix4f tempMat4 = new Matrix4f();
|
||||
|
||||
private static boolean dhMatricesActive = false;
|
||||
private static final Matrix4f savedModelView = new Matrix4f();
|
||||
private static final Matrix4f savedProjection = new Matrix4f();
|
||||
|
||||
static void pushDhMatrices(Matrix4fc projection, Matrix4fc modelView) {
|
||||
if (!dhMatricesActive) {
|
||||
savedModelView.set(GLStateManager.getModelViewMatrix());
|
||||
savedProjection.set(GLStateManager.getProjectionMatrix());
|
||||
dhMatricesActive = true;
|
||||
}
|
||||
GLStateManager.setModelViewMatrix(modelView);
|
||||
GLStateManager.setProjectionMatrix(projection);
|
||||
}
|
||||
|
||||
static void popDhMatrices() {
|
||||
if (dhMatricesActive) {
|
||||
GLStateManager.setModelViewMatrix(savedModelView);
|
||||
GLStateManager.setProjectionMatrix(savedProjection);
|
||||
dhMatricesActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// constructor //
|
||||
|
||||
@@ -181,6 +204,7 @@ public class LodRendererEvents {
|
||||
DhApiBeforeRenderCleanupEvent beforeCleanupEvent = new DhApiBeforeRenderCleanupEvent() {
|
||||
@Override
|
||||
public void beforeCleanup(DhApiEventParam<DhApiRenderParam> event) {
|
||||
popDhMatrices();
|
||||
if (getInstance().shouldOverride) {
|
||||
if (ShadowRenderingState.areShadowsCurrentlyBeingRendered()) {
|
||||
getInstance().getShadowShader().unbind();
|
||||
|
||||
Reference in new issue
Block a user