Skip to content

Commit

Permalink
Use random seed from Sodium rendering context and BlockModelRenderer …
Browse files Browse the repository at this point in the history
…caller

Fixes #171
  • Loading branch information
comp500 committed Feb 6, 2023
1 parent fa99058 commit 0060403
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package link.infra.indium.mixin.sodium;

import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderContext;
import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import link.infra.indium.Indium;
import link.infra.indium.renderer.render.TerrainRenderContext;
import me.jellysquid.mods.sodium.client.gl.compile.ChunkBuildContext;
import me.jellysquid.mods.sodium.client.render.chunk.compile.ChunkBuildResult;
import me.jellysquid.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderContext;
import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
import me.jellysquid.mods.sodium.client.render.chunk.tasks.ChunkRenderBuildTask;
import me.jellysquid.mods.sodium.client.render.chunk.tasks.ChunkRenderRebuildTask;
import me.jellysquid.mods.sodium.client.util.task.CancellationSource;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.BlockRenderView;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

/**
* The main injection point into Sodium - here we stop Sodium from rendering FRAPI block models, and do it ourselves
Expand All @@ -46,8 +40,7 @@ public boolean onRenderBlock(BlockRenderer blockRenderer, BlockRenderContext ctx
if (!Indium.ALWAYS_TESSELLATE_INDIUM && ((FabricBakedModel) ctx.model()).isVanillaAdapter()) {
return blockRenderer.renderModel(ctx, buffers);
} else {
Vec3d modelOffset = ctx.state().getModelOffset(ctx.world(), ctx.pos());
return TerrainRenderContext.get(buildContext).tessellateBlock(ctx.state(), ctx.pos(), ctx.origin(), ctx.model(), modelOffset);
return TerrainRenderContext.get(buildContext).tessellateBlock(ctx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package link.infra.indium.renderer.render;

import java.util.function.Supplier;

import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
Expand All @@ -31,6 +29,8 @@
import net.minecraft.util.math.random.RandomSeed;
import net.minecraft.world.BlockRenderView;

import java.util.function.Supplier;

/**
* Holds, manages and provides access to the block/world related state
* needed by fallback and mesh consumers.
Expand All @@ -49,27 +49,18 @@ public class BlockRenderInfo {
RenderLayer defaultLayer;

public final Supplier<Random> randomSupplier = () -> {
final Random result = random;
long seed = this.seed;

if (seed == -1L) {
seed = blockState.getRenderingSeed(blockPos);
this.seed = seed;
}

result.setSeed(seed);
return result;
random.setSeed(this.seed);
return random;
};

public void setBlockView(BlockRenderView blockView) {
this.blockView = blockView;
}

public void prepareForBlock(BlockState blockState, BlockPos blockPos, boolean modelAO) {
public void prepareForBlock(BlockState blockState, BlockPos blockPos, boolean modelAO, long seed) {
this.blockPos = blockPos;
this.blockState = blockState;
// in the unlikely case seed actually matches this, we'll simply retrieve it more than one
seed = -1L;
this.seed = seed;
defaultAo = modelAO && MinecraftClient.isAmbientOcclusionEnabled() && blockState.getLuminance() == 0;

defaultLayer = RenderLayers.getBlockLayer(blockState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean render(BlockRenderView blockView, BakedModel model, BlockState st
this.didOutput = false;
aoCalc.clear();
blockInfo.setBlockView(blockView);
blockInfo.prepareForBlock(state, pos, model.useAmbientOcclusion());
blockInfo.prepareForBlock(state, pos, model.useAmbientOcclusion(), seed);
lightCache.reset(pos, blockView);

((FabricBakedModel) model).emitBlockQuads(blockView, state, pos, randomSupplier, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public TerrainBlockRenderInfo(BlockOcclusionCache blockOcclusionCache) {
}

@Override
public void prepareForBlock(BlockState blockState, BlockPos blockPos, boolean modelAO) {
super.prepareForBlock(blockState, blockPos, modelAO);
public void prepareForBlock(BlockState blockState, BlockPos blockPos, boolean modelAO, long seed) {
super.prepareForBlock(blockState, blockPos, modelAO, seed);
cullCompletionFlags = 0;
cullResultFlags = 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package link.infra.indium.renderer.render;

import java.util.function.Consumer;
import java.util.function.Function;

import link.infra.indium.mixin.sodium.AccessBlockRenderer;
import link.infra.indium.renderer.accessor.AccessBlockRenderCache;
import link.infra.indium.renderer.aocalc.AoCalculator;
import me.jellysquid.mods.sodium.client.gl.compile.ChunkBuildContext;
import me.jellysquid.mods.sodium.client.model.light.cache.ArrayLightDataCache;
import me.jellysquid.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderCache;
import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderContext;
import me.jellysquid.mods.sodium.client.render.occlusion.BlockOcclusionCache;
import me.jellysquid.mods.sodium.client.world.WorldSlice;
import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.crash.CrashException;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.crash.CrashReportSection;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import org.joml.Vector3f;
import org.joml.Vector3fc;

import java.util.function.Consumer;
import java.util.function.Function;

public class TerrainRenderContext extends AbstractRenderContext {
private final TerrainBlockRenderInfo blockInfo;
private final ChunkRenderInfo chunkInfo;
Expand Down Expand Up @@ -66,19 +62,19 @@ public void release() {
}

/** Called from chunk renderer hook. */
public boolean tessellateBlock(BlockState blockState, BlockPos blockPos, Vector3fc origin, final BakedModel model, Vec3d modelOffset) {
this.origin = origin;
this.modelOffset = modelOffset;
public boolean tessellateBlock(BlockRenderContext ctx) {
this.origin = ctx.origin();
this.modelOffset = ctx.state().getModelOffset(ctx.world(), ctx.pos());

try {
chunkInfo.didOutput = false;
aoCalc.clear();
blockInfo.prepareForBlock(blockState, blockPos, model.useAmbientOcclusion());
((FabricBakedModel) model).emitBlockQuads(blockInfo.blockView, blockInfo.blockState, blockInfo.blockPos, blockInfo.randomSupplier, this);
blockInfo.prepareForBlock(ctx.state(), ctx.pos(), ctx.model().useAmbientOcclusion(), ctx.seed());
((FabricBakedModel) ctx.model()).emitBlockQuads(blockInfo.blockView, blockInfo.blockState, blockInfo.blockPos, blockInfo.randomSupplier, this);
} catch (Throwable throwable) {
CrashReport crashReport = CrashReport.create(throwable, "Tessellating block in world - Indium Renderer");
CrashReportSection crashReportSection = crashReport.addElement("Block being tessellated");
CrashReportSection.addBlockInfo(crashReportSection, chunkInfo.blockView, blockPos, blockState);
CrashReportSection.addBlockInfo(crashReportSection, chunkInfo.blockView, ctx.pos(), ctx.state());
throw new CrashException(crashReport);
}

Expand Down

0 comments on commit 0060403

Please sign in to comment.