Skip to content

Commit

Permalink
a little bit more work
Browse files Browse the repository at this point in the history
  • Loading branch information
Asek3 committed Apr 13, 2024
1 parent fd885da commit 3d54a04
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 100 deletions.
22 changes: 12 additions & 10 deletions src/main/java/net/coderbot/iris/mixin/LevelRendererAccessor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package net.coderbot.iris.mixin;

import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.util.BlockRenderLayer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
Expand All @@ -17,25 +22,22 @@
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.world.entity.Entity;

@Mixin(LevelRenderer.class)
@Mixin(RenderGlobal.class)
public interface LevelRendererAccessor {
@Accessor("entityRenderDispatcher")
EntityRenderDispatcher getEntityRenderDispatcher();
@Accessor("renderManager")
RenderManager getEntityRenderDispatcher();

@Accessor("renderChunks")
ObjectList<LevelRenderer.RenderChunkInfo> getRenderChunks();

@Invoker("renderChunkLayer")
void invokeRenderChunkLayer(RenderType terrainLayer, PoseStack modelView, double cameraX, double cameraY, double cameraZ);
@Invoker("renderBlockLayer")
void invokeRenderChunkLayer(BlockRenderLayer blockLayerIn, double partialTicks, int pass, Entity entityIn);

@Invoker("setupRender")
void invokeSetupRender(Camera camera, Frustum frustum, boolean hasForcedFrustum, int frame, boolean spectator);

@Invoker("renderEntity")
void invokeRenderEntity(Entity entity, double cameraX, double cameraY, double cameraZ, float tickDelta, PoseStack poseStack, MultiBufferSource bufferSource);

@Accessor("level")
ClientLevel getLevel();
@Accessor("world")
WorldClient getLevel();

@Accessor("frameId")
int getFrameId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.coderbot.iris.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import nanolive.utils.RenderGlobalExtended;
import net.coderbot.iris.Iris;
import net.coderbot.iris.gl.program.Program;
Expand All @@ -9,6 +10,7 @@
import net.coderbot.iris.uniforms.SystemTimeUniforms;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.renderer.culling.ICamera;
import net.minecraft.client.settings.GameSettings;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -60,6 +62,13 @@ public class MixinEntityRenderer {
Program.unbind();
}

// Setup shadow terrain & render shadows before the main terrain setup. We need to do things in this order to
// avoid breaking other mods such as Light Overlay: https://github.com/IrisShaders/Iris/issues/1356
@Inject(method = "renderWorldPass", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/RenderGlobal;setupTerrain(Lnet/minecraft/entity/Entity;DLnet/minecraft/client/renderer/culling/ICamera;IZ)V"))
private void iris$renderTerrainShadows(int pass, float partialTicks, long finishTimeNano, CallbackInfo ci, @Local ICamera icamera) {
((RenderGlobalExtended) this.mc.renderGlobal).getPipeline().renderShadows((LevelRendererAccessor) this, icamera);
}

@Redirect(method = "renderWorldPass", at = @At(value = "FIELD", target = "Lnet/minecraft/client/settings/GameSettings;renderDistanceChunks:I"),
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/culling/ICamera;setPosition(DDD)V")))
private int iris$alwaysRenderSky(GameSettings instance) {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/net/coderbot/iris/mixin/MixinLevelRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ public class MixinLevelRenderer implements RenderGlobalExtended {
@Unique
private WorldRenderingPipeline pipeline;

// Setup shadow terrain & render shadows before the main terrain setup. We need to do things in this order to
// avoid breaking other mods such as Light Overlay: https://github.com/IrisShaders/Iris/issues/1356
@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "net/minecraft/client/renderer/LevelRenderer.setupRender (Lnet/minecraft/client/Camera;Lnet/minecraft/client/renderer/culling/Frustum;ZIZ)V"))
private void iris$renderTerrainShadows(PoseStack poseStack, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f projection, CallbackInfo callback) {
pipeline.renderShadows((LevelRendererAccessor) this, camera);
}

/*@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = RENDER_SKY))
private void iris$beginSky(PoseStack poseStack, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f projection, CallbackInfo callback) {
// Use CUSTOM_SKY until levelFogColor is called as a heuristic to catch FabricSkyboxes.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package net.coderbot.iris.mixin.bettermipmaps;

import com.mojang.blaze3d.platform.NativeImage;
import nanolive.compat.NativeImage;
import net.coderbot.iris.helpers.ColorSRGB;
import net.minecraft.client.renderer.texture.TextureUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.client.renderer.texture.MipmapGenerator;

/**
* Implements a significantly enhanced mipmap downsampling filter.
*
Expand All @@ -25,15 +24,15 @@
* artifacts.</ul>
* </li>
*/
@Mixin(MipmapGenerator.class)
@Mixin(TextureUtil.class)
public class MixinMipmapGenerator {

/**
* @author coderbot
* @reason replace the vanilla blending function with our improved function
*/
@Overwrite
private static int alphaBlend(int one, int two, int three, int four, boolean checkAlpha) {
private static int blendColors(int one, int two, int three, int four, boolean checkAlpha) {
// First blend horizontally, then blend vertically.
//
// This works well for the case where our change is the most impactful (grass side overlays)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package net.coderbot.iris.mixin.shadows;

import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.chunk.RenderChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher;

@Mixin(LevelRenderer.RenderChunkInfo.class)
@Mixin(RenderGlobal.ContainerLocalRenderInformation.class)
public interface ChunkInfoAccessor {
@Accessor("chunk")
ChunkRenderDispatcher.RenderChunk getChunk();
@Accessor("renderChunk")
RenderChunk getChunk();
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package net.coderbot.iris.mixin.shadows;

import org.spongepowered.asm.mixin.Final;
import net.coderbot.iris.pipeline.ShadowRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.culling.ICamera;
import net.minecraft.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Group;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import it.unimi.dsi.fastutil.objects.ObjectList;
import net.coderbot.iris.pipeline.ShadowRenderer;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.culling.Frustum;
import java.util.List;

/**
* Prevent nearby chunks from being rebuilt on the main thread in the shadow pass. Aside from causing FPS to tank,
Expand All @@ -26,38 +25,35 @@
* if we apply with the same priority, then we'll just get a Mixin error due to the injects conflicting with the
* {@code @Overwrite}. Using {@code @Group} allows us to avoid a fragile Mixin plugin.
*/
@Mixin(value = LevelRenderer.class, priority = 1010)
@Mixin(value = RenderGlobal.class, priority = 1010)
public class MixinPreventRebuildNearInShadowPass {
@Shadow
@Final
private ObjectList<LevelRenderer.RenderChunkInfo> renderChunks;
private List<RenderGlobal.ContainerLocalRenderInformation> renderInfos;

@Group(name = "iris_MixinPreventRebuildNearInShadowPass", min = 1, max = 1)
@Inject(method = "setupRender",
@Inject(method = "setupTerrain",
at = @At(value = "INVOKE_STRING",
target = "Lnet/minecraft/util/profiling/ProfilerFiller;popPush(Ljava/lang/String;)V",
target = "Lnet/minecraft/profiler/Profiler;endStartSection(Ljava/lang/String;)V",
args = "ldc=rebuildNear"),
cancellable = true,
require = 0)
private void iris$preventRebuildNearInShadowPass(Camera camera, Frustum frustum, boolean hasForcedFrustum,
int frame, boolean spectator, CallbackInfo callback) {
private void iris$preventRebuildNearInShadowPass(Entity viewEntity, double partialTicks, ICamera camera, int frameCount, boolean playerSpectator, CallbackInfo ci) {
if (ShadowRenderer.ACTIVE) {
for (LevelRenderer.RenderChunkInfo chunk : this.renderChunks) {
ShadowRenderer.visibleBlockEntities.addAll(((ChunkInfoAccessor) chunk).getChunk().getCompiledChunk().getRenderableBlockEntities());
for (RenderGlobal.ContainerLocalRenderInformation chunk : this.renderInfos) {
ShadowRenderer.visibleBlockEntities.addAll(((ChunkInfoAccessor) chunk).getChunk().getCompiledChunk().getTileEntities());
}
Minecraft.getInstance().getProfiler().pop();
callback.cancel();
Minecraft.getMinecraft().profiler.endSection();
ci.cancel();
}
}

@Group(name = "iris_MixinPreventRebuildNearInShadowPass", min = 1, max = 1)
@Inject(method = "setupRender",
@Inject(method = "setupTerrain",
at = @At(value = "INVOKE",
target = "me/jellysquid/mods/sodium/client/gl/device/RenderDevice.enterManagedCode ()V",
remap = false),
require = 0)
private void iris$cannotInject(Camera camera, Frustum frustum, boolean hasForcedFrustum, int frame,
boolean spectator, CallbackInfo callback) {
private void iris$cannotInject(Entity viewEntity, double partialTicks, ICamera camera, int frameCount, boolean playerSpectator, CallbackInfo ci) {
// Dummy injection just to assert that either Sodium is present, or the vanilla injection passed.
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package net.coderbot.iris.mixin.sky;

import net.coderbot.iris.Iris;
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL11C;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;

import net.coderbot.iris.Iris;
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.minecraft.client.renderer.LevelRenderer;

/**
* Allows pipelines to disable the sun, moon, or both.
*/
Expand All @@ -42,10 +34,10 @@ public class MixinLevelRenderer_SunMoonToggle {
}

@Inject(method = "renderSky(FI)V",
at = @At(value = "INVOKE", target = "com/mojang/blaze3d/vertex/BufferUploader.end (Lcom/mojang/blaze3d/vertex/BufferBuilder;)V"),
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/Tessellator;draw()V"),
slice = @Slice(
from = @At(value = "FIELD", target = "net/minecraft/client/renderer/LevelRenderer.SUN_LOCATION : Lnet/minecraft/resources/ResourceLocation;"),
to = @At(value = "FIELD", target = "net/minecraft/client/renderer/LevelRenderer.MOON_LOCATION : Lnet/minecraft/resources/ResourceLocation;")),
from = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/RenderGlobal;SUN_TEXTURES:Lnet/minecraft/util/ResourceLocation;"),
to = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/RenderGlobal;MOON_PHASES_TEXTURES:Lnet/minecraft/util/ResourceLocation;")),
allow = 1)
private void iris$beforeDrawSun(float partialTicks, int pass, CallbackInfo ci) {
if (!Iris.getPipelineManager().getPipeline().map(WorldRenderingPipeline::shouldRenderSun).orElse(true)) {
Expand All @@ -54,10 +46,10 @@ public class MixinLevelRenderer_SunMoonToggle {
}

@Inject(method = "renderSky(FI)V",
at = @At(value = "INVOKE", target = "com/mojang/blaze3d/vertex/BufferUploader.end (Lcom/mojang/blaze3d/vertex/BufferBuilder;)V"),
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/Tessellator;draw()V"),
slice = @Slice(
from = @At(value = "FIELD", target = "net/minecraft/client/renderer/LevelRenderer.MOON_LOCATION : Lnet/minecraft/resources/ResourceLocation;"),
to = @At(value = "INVOKE", target = "net/minecraft/client/multiplayer/ClientLevel.getStarBrightness (F)F")),
from = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/RenderGlobal;MOON_PHASES_TEXTURES:Lnet/minecraft/util/ResourceLocation;"),
to = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/WorldClient;getStarBrightness(F)F")),
allow = 1)
private void iris$beforeDrawMoon(float partialTicks, int pass, CallbackInfo ci) {
if (!Iris.getPipelineManager().getPipeline().map(WorldRenderingPipeline::shouldRenderMoon).orElse(true)) {
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/net/coderbot/iris/pipeline/ShadowRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.culling.ICamera;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.opengl.*;
import org.lwjgl.opengl.GL20C;
Expand Down Expand Up @@ -356,7 +360,7 @@ private void copyPreTranslucentDepth() {
}

private void renderEntities(LevelRendererAccessor levelRenderer, Frustum frustum, MultiBufferSource.BufferSource bufferSource, PoseStack modelView, double cameraX, double cameraY, double cameraZ, float tickDelta) {
EntityRenderDispatcher dispatcher = levelRenderer.getEntityRenderDispatcher();
RenderManager dispatcher = levelRenderer.getEntityRenderDispatcher();

int shadowEntities = 0;

Expand All @@ -365,7 +369,7 @@ private void renderEntities(LevelRendererAccessor levelRenderer, Frustum frustum
List<Entity> renderedEntities = new ArrayList<>(32);

// TODO: I'm sure that this can be improved / optimized.
for (Entity entity : getLevel().entitiesForRendering()) {
for (Entity entity : getLevel().loadedEntityList) {
if (!dispatcher.shouldRender(entity, frustum, cameraX, cameraY, cameraZ) || (entity instanceof EntityPlayer && ((EntityPlayer) entity).isSpectator())) {
continue;
}
Expand All @@ -392,7 +396,7 @@ private void renderEntities(LevelRendererAccessor levelRenderer, Frustum frustum
}

private void renderPlayerEntity(LevelRendererAccessor levelRenderer, Frustum frustum, MultiBufferSource.BufferSource bufferSource, PoseStack modelView, double cameraX, double cameraY, double cameraZ, float tickDelta) {
EntityRenderDispatcher dispatcher = levelRenderer.getEntityRenderDispatcher();
RenderManager dispatcher = levelRenderer.getEntityRenderDispatcher();

profiler.startSection("cull");

Expand All @@ -413,8 +417,8 @@ private void renderPlayerEntity(LevelRendererAccessor levelRenderer, Frustum fru
}
}

if (player.getVehicle() != null) {
levelRenderer.invokeRenderEntity(player.getVehicle(), cameraX, cameraY, cameraZ, tickDelta, modelView, bufferSource);
if (player.getRidingEntity() != null) {
levelRenderer.invokeRenderEntity(player.getRidingEntity(), cameraX, cameraY, cameraZ, tickDelta, modelView, bufferSource);
shadowEntities++;
}

Expand Down Expand Up @@ -444,10 +448,10 @@ private void renderBlockEntities(MultiBufferSource.BufferSource bufferSource, Po
continue;
}
}
modelView.pushPose();
modelView.translate(pos.getX() - cameraX, pos.getY() - cameraY, pos.getZ() - cameraZ);
BlockEntityRenderDispatcher.instance.render(entity, tickDelta, modelView, bufferSource);
modelView.popPose();
//modelView.pushPose();
//modelView.translate(pos.getX() - cameraX, pos.getY() - cameraY, pos.getZ() - cameraZ);
TileEntityRendererDispatcher.instance.render(entity, pos.getX() - cameraX, pos.getY() - cameraY, pos.getZ() - cameraZ, tickDelta, bufferSource);
//modelView.popPose();

shadowBlockEntities++;
}
Expand Down Expand Up @@ -525,7 +529,7 @@ public void renderShadows(LevelRendererAccessor levelRenderer, ICamera playerCam
// We have to ensure that we don't regenerate clouds every frame, since that's what needsUpdate ends up doing.
// This took up to 10% of the frame time before we applied this fix! That's really bad!
boolean regenerateClouds = levelRenderer.shouldRegenerateClouds();
((LevelRenderer) levelRenderer).needsUpdate();
((RenderGlobal) levelRenderer).setDisplayListEntitiesDirty();
levelRenderer.setShouldRegenerateClouds(regenerateClouds);

// Execute the vanilla terrain setup / culling routines using our shadow frustum.
Expand All @@ -544,9 +548,9 @@ public void renderShadows(LevelRendererAccessor levelRenderer, ICamera playerCam

// Render all opaque terrain unless pack requests not to
if (shouldRenderTerrain) {
levelRenderer.invokeRenderChunkLayer(RenderType.solid(), modelView, cameraX, cameraY, cameraZ);
levelRenderer.invokeRenderChunkLayer(RenderType.cutout(), modelView, cameraX, cameraY, cameraZ);
levelRenderer.invokeRenderChunkLayer(RenderType.cutoutMipped(), modelView, cameraX, cameraY, cameraZ);
levelRenderer.invokeRenderChunkLayer(BlockRenderLayer.SOLID, modelView, cameraX, cameraY, cameraZ);
levelRenderer.invokeRenderChunkLayer(BlockRenderLayer.CUTOUT, modelView, cameraX, cameraY, cameraZ);
levelRenderer.invokeRenderChunkLayer(BlockRenderLayer.CUTOUT_MIPPED, modelView, cameraX, cameraY, cameraZ);
}

profiler.endStartSection("entities");
Expand Down Expand Up @@ -608,7 +612,7 @@ public void renderShadows(LevelRendererAccessor levelRenderer, ICamera playerCam
// It doesn't matter a ton, since this just means that they won't be sorted in the normal rendering pass.
// Just something to watch out for, however...
if (shouldRenderTranslucent) {
levelRenderer.invokeRenderChunkLayer(RenderType.translucent(), modelView, cameraX, cameraY, cameraZ);
levelRenderer.invokeRenderChunkLayer(BlockRenderLayer.TRANSLUCENT, modelView, cameraX, cameraY, cameraZ);
}

// Note: Apparently tripwire isn't rendered in the shadow pass.
Expand All @@ -618,7 +622,7 @@ public void renderShadows(LevelRendererAccessor levelRenderer, ICamera playerCam
renderBuffersExt.endLevelRendering();
}

debugStringTerrain = ((LevelRenderer) levelRenderer).getChunkStatistics();
debugStringTerrain = ((RenderGlobal) levelRenderer).getDebugInfoRenders();

profiler.endStartSection("generate mipmaps");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.coderbot.iris.mixin.LevelRendererAccessor;
import net.coderbot.iris.shaderpack.CloudSetting;
import net.coderbot.iris.uniforms.FrameUpdateNotifier;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.culling.ICamera;

public interface WorldRenderingPipeline {
Expand Down
Loading

0 comments on commit 3d54a04

Please sign in to comment.