Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/main/java/dev/leadlight/render/FeatureLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ GpuTextureView open(GpuDevice device, int width, int height) {
return this.layer.getColorTextureView();
}

/**
* The layer's own image after {@link #open}, for a caller that has to point something other than
* the features redirect at it: the main target's colour, while a mod draws into it by name.
*/
TextureTarget target() {
return this.layer;
}

/** Called every frame: a resource reload empties the pipeline cache. */
boolean prepare(GpuDevice device) {
if (GraphicsApi.valid(GraphicsApi.compile(device, this.pipeline, this.source))) {
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/dev/leadlight/render/PackChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import com.mojang.blaze3d.systems.GpuDevice;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.textures.GpuTextureView;
import com.mojang.blaze3d.pipeline.TextureTarget;
import net.minecraft.client.Minecraft;
import org.jspecify.annotations.Nullable;
import net.minecraft.client.renderer.MappableRingBuffer;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
Expand Down Expand Up @@ -315,6 +317,12 @@ static void chainWanted(boolean wanted) {
/** The game's translucent features, caught and composed onto the pack's image. Null when the
* pack serves no translucent pass to compose in front of. */
private final FeatureLayer features;
/**
* What mods draw by name into the game's main target while the level is still being drawn,
* Litematica's schematic and its overlay among them, caught as {@link #features} catches the
* game's translucent features and composed onto the same attachment. Null where that one is.
*/
private final FeatureLayer modDraws;
private final int load;
final TerrainDraw terrain;
private final SkyDraw sky;
Expand Down Expand Up @@ -386,6 +394,9 @@ static void chainWanted(boolean wanted) {
*/
private boolean redirected;

/** Whether {@link #openModDraws} handed out the layer this frame, the same rule as {@link #redirected}. */
private boolean modDrawsOpen;

PackChain(PackProgram.Chain chain, PackValues values, String world, boolean seedEnabled,
OpenedPack opened, Path packPath, Map<String, OptionValue> chosen, String profile) {
// One pack's shadow map is not another's, and the arming file is read again here so a
Expand Down Expand Up @@ -430,6 +441,8 @@ static void chainWanted(boolean wanted) {
.filter(into -> !holdsIntegers(into.target()))
.map(into -> new FeatureLayer(into, this.targets.format(into.target())))
.orElse(null);
this.modDraws = this.features == null ? null
: new FeatureLayer(this.features.into(), this.targets.format(this.features.into().target()));
// Held rather than read: the terrain program is compiled against the chunk mesh format, and
// nothing knows that format until the renderer asks for its shader.
//
Expand Down Expand Up @@ -2136,6 +2149,91 @@ public static void closeFeatures() {
}
}

/**
* Hands out the layer a mod's draws into the main target are caught in, emptied, or null when
* nothing should be redirected and those draws stay where they were made. Called from the head
* of the game's outline phase, which is where Litematica draws its schematic and its overlay
* ({@code executeOutline}, HEAD and TAIL), each through a pass of its own opened on the main
* target by name. The caller points the main target's colour at the layer for the length of
* the phase and puts it back before {@link #closeModDraws()}.
* <p>
* The reason is the one {@link #openFeatures()} gives for the game's own features: what lands
* on the main target is either painted over by the final or, through the seed, shown only
* where the pack drew nothing. Under Iris these draws reach the pack's programs through
* {@code IrisApi.assignPipeline}; there is no such door here, so they are composed instead,
* flat and in front of the world, the way the features layer composes what it catches. The
* world's depth is not redirected, so they still hide behind terrain.
* <p>
* Must run outside any render pass, as the layer's emptying does.
*/
public static @Nullable TextureTarget openModDraws() {
PackChain chain = active;
GpuDevice device = RenderSystem.tryGetDevice();
Minecraft minecraft = Minecraft.getInstance();
if (disabled || chain == null || device == null || minecraft == null || !chainWanted
|| chain.modDraws == null) {
return null;
}

RenderTarget main = minecraft.gameRenderer.mainRenderTarget();

try {
if (main == null || !chain.modDraws.prepare(device) || !chain.drawable()) {
return null;
}

GraphicsApi.suspendLevelPass();
if (chain.modDraws.open(device, main.width, main.height) == null) {
return null;
}

chain.modDrawsOpen = true;

return chain.modDraws.target();
} catch (GpuDeviceLossException e) {
throw e;
} catch (RuntimeException e) {
chain.modDrawsOpen = false;
stop();
Leadlight.logger().error("Leadlight stopped drawing this pack after an error", e);
chain.release();

return null;
}
}

/**
* Composes what {@link #openModDraws()} caught onto the attachment the features layer is
* composed onto. The main target's colour has to be the game's own again before this runs.
*/
public static void closeModDraws() {
PackChain chain = active;
if (chain == null || !chain.modDrawsOpen) {
return;
}

chain.modDrawsOpen = false;

GpuDevice device = RenderSystem.tryGetDevice();
if (disabled || device == null || chain.modDraws == null) {
return;
}

try {
GraphicsApi.suspendLevelPass();
ChainPlan.Attachment into = chain.modDraws.into();
GpuTextureView view = chain.targets.view(into.target(), into.side());
chain.modDraws.compose(device.createCommandEncoder(), chain.quad, view,
chain.targets.takeClear(view));
} catch (GpuDeviceLossException e) {
throw e;
} catch (RuntimeException e) {
stop();
Leadlight.logger().error("Leadlight stopped drawing this pack after an error", e);
chain.release();
}
}

/**
* Draws the begins, on the far plane and the depths of the frame before.
* <p>
Expand Down Expand Up @@ -3310,6 +3408,9 @@ void release() {
if (this.features != null) {
this.features.release();
}
if (this.modDraws != null) {
this.modDraws.release();
}

// DH takes its far terrain back with the pack, its own frame order included: DhLods.handBack
// says why nothing else would ever return it.
Expand Down
30 changes: 18 additions & 12 deletions src/mc26.3/java/dev/leadlight/fabric/mixin/AfterLevelMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@

import dev.leadlight.platform.EngineStages;

import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LevelRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
* The end of the level on Fabric, right after the level renderer returns, which is the line
* NeoForge posts its after-level event on.
* The end of the level on Fabric: inside the level's render, once its frame graph has run and the
* model view matrix it pushed is popped again, which is the line Iris ends its own level render on
* ({@code mixin/MixinLevelRenderer.java}, {@code iris$endLevelRender} at the same
* {@code popMatrix}).
* <p>
* The 26.3 half: {@code renderLevel} takes no argument there, and the level's render takes the
* frame's state rather than the tracker and the model view matrix.
* Not after the level renderer returns, which is where this used to be. Mods draw their world
* overlays at the {@code RETURN} of this same method, MaLiLib's world-last renderers among them
* (Litematica's boxes, MiniHUD's light levels), straight into the main target and with the game's
* own programs. Behind the return, the pack's final was drawn over them and they were never seen;
* here, the final is already in the main target when they draw, so they land on the pack's image as
* they land on Iris's. Nothing between this line and the return draws: the frame's features are
* closed and the sections compiled, uploaded and culled for the next frame.
* <p>
* The 26.3 half: the level's render takes the frame's state rather than the tracker and the model
* view matrix.
*/
@Mixin(GameRenderer.class)
@Mixin(LevelRenderer.class)
public abstract class AfterLevelMixin {

@Inject(method = "renderLevel",
@Inject(method = "render",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/renderer/LevelRenderer;"
+ "render(Lcom/mojang/blaze3d/resource/GraphicsResourceAllocator;Z"
+ "Lnet/minecraft/client/renderer/state/level/CameraRenderState;"
+ "Lcom/mojang/renderpearl/api/buffers/GpuBufferSlice;"
+ "Lorg/joml/Vector4f;ZZ)V",
target = "Lorg/joml/Matrix4fStack;popMatrix()Lorg/joml/Matrix4fStack;",
shift = At.Shift.AFTER),
require = 1)
private void leadlight$afterLevel(CallbackInfo ci) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.leadlight.mixin.game;

import dev.leadlight.render.PackChain;

import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.renderpearl.api.textures.GpuTexture;
import com.mojang.renderpearl.api.textures.GpuTextureView;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.feature.FeatureRenderDispatcher;
import org.spongepowered.asm.mixin.Mixin;

/**
* Points the main target's colour at {@link PackChain#openModDraws()}'s layer for the length of
* the game's outline phase, and composes what landed there once it is the game's own again.
* <p>
* The phase itself draws nothing on the main target: the game's glowing outlines go to a target of
* their own. What draws on the main target inside it is a mod, by name: Litematica draws its
* schematic at the head of this method and its overlay at the tail, each through a pass it opens on
* the main target's colour and depth. Wrapping the whole method takes both, wherever another mod's
* injection sits in it, and nothing else. Only the colour moves; the depth is the world's, so the
* schematic still hides behind terrain and the terrain behind it.
* <p>
* Put back in a finally, before the composition: a main target left pointing at the layer would
* take the rest of the frame with it, the pack's final included.
*/
@Mixin(LevelRenderer.class)
public abstract class LevelRendererModDrawsMixin {

@WrapMethod(method = "executeOutline", require = 1)
private void leadlight$modDraws(FeatureRenderDispatcher.PreparedFrame frame,
Operation<Void> original) {
TextureTarget layer = PackChain.openModDraws();
RenderTarget main = layer == null ? null : Minecraft.getInstance().gameRenderer.mainRenderTarget();
if (main == null) {
original.call(frame);

return;
}

RenderTargetAccessor target = (RenderTargetAccessor) main;
GpuTexture texture = target.leadlight$colorTexture();
GpuTextureView view = target.leadlight$colorTextureView();
target.leadlight$setColorTexture(layer.getColorTexture());
target.leadlight$setColorTextureView(layer.getColorTextureView());
try {
original.call(frame);
} finally {
target.leadlight$setColorTexture(texture);
target.leadlight$setColorTextureView(view);
}

PackChain.closeModDraws();
}
}
24 changes: 24 additions & 0 deletions src/mc26.3/java/dev/leadlight/mixin/game/RenderTargetAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.leadlight.mixin.game;

import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.renderpearl.api.textures.GpuTexture;
import com.mojang.renderpearl.api.textures.GpuTextureView;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

/** A target's colour image, read and set, for pointing the main target at a layer for a while. */
@Mixin(RenderTarget.class)
public interface RenderTargetAccessor {

@Accessor("colorTexture")
GpuTexture leadlight$colorTexture();

@Accessor("colorTexture")
void leadlight$setColorTexture(GpuTexture texture);

@Accessor("colorTextureView")
GpuTextureView leadlight$colorTextureView();

@Accessor("colorTextureView")
void leadlight$setColorTextureView(GpuTextureView view);
}
2 changes: 2 additions & 0 deletions src/mc26.3/resources/leadlight-game.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
"GameRendererTransparencyMixin",
"LevelRendererFeaturesMixin",
"LevelRendererMainPassMixin",
"LevelRendererModDrawsMixin",
"PipelineBuilderMixin",
"PipelineCacheAccessor",
"RenderPipelinesAccessor",
"RenderSystemAccessor",
"RenderSystemMixin",
"RenderTargetAccessor",
"SkyRendererMixin",
"SPIRVModuleMixin",
"VulkanRenderPipelineAccessor"
Expand Down