Skip to content

Commit

Permalink
Support new Fabric API changes and Sodium 0.5.3
Browse files Browse the repository at this point in the history
Ports the parts of FabricMC/fabric#3287 that relate to Indigo. Now uses the same renderer-api-v1 impl details that Indigo uses; if this becomes an issue the util class can be moved into Indium.

Fixes #228, fixes #232.

Co-authored-by: Technici4n <[email protected]>
  • Loading branch information
comp500 and Technici4n committed Sep 23, 2023
1 parent 794272d commit 75088b0
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 148 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ publishing {
}

// TODO: infer from fabric.mod.json?!
def supportedVersions = ["${project.minecraft_version}"]
def verName = "Indium ${project.mod_version} for Minecraft ${project.minecraft_version}/Sodium ${project.sodium_version}"
def supportedVersions = ["${project.minecraft_version}", "1.20.2"]
def verName = "Indium ${project.mod_version} for Minecraft 1.20.x/Sodium ${project.sodium_version}"

// Check version is as expected
if (System.getenv("EXPECTED_VERSION") !== null) {
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ minecraft_version=1.20.1
yarn_mappings=1.20.1+build.8
loader_version=0.14.21
# Mod Properties
mod_version=1.0.25
mod_version=1.0.26
maven_group=link.infra
archives_base_name=indium
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.84.0+1.20.1
sodium_version=0.5.2
fabric_version=0.89.0+1.20.1
sodium_version=0.5.3
sodium_minecraft_version=1.20.1
# Publishing metadata
curseforge_id=459496
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package link.infra.indium.mixin.renderer;

import link.infra.indium.renderer.render.AbstractBlockRenderContext;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.fabricmc.fabric.impl.renderer.VanillaModelEncoder;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView;
import org.spongepowered.asm.mixin.Mixin;

import java.util.function.Supplier;

@Mixin(BakedModel.class)
public interface MixinBakedModel extends FabricBakedModel {
/**
* Override the fallback path to shade vanilla quads differently.
*/
@Override
default void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
VanillaModelEncoder.emitBlockQuads((BakedModel) this, state, randomSupplier, context, ((AbstractBlockRenderContext) context).getVanillaModelEmitter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package link.infra.indium.mixin.renderer;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -24,7 +25,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import link.infra.indium.renderer.accessor.AccessItemRenderer;
import link.infra.indium.renderer.render.IndiumQuadHandler;
import link.infra.indium.renderer.render.ItemRenderContext;
import net.minecraft.client.color.item.ItemColors;
import net.minecraft.client.render.VertexConsumer;
Expand All @@ -37,22 +37,19 @@

@Mixin(ItemRenderer.class)
public abstract class MixinItemRenderer implements AccessItemRenderer {
@Shadow
protected ItemColors colors;
@Shadow @Final
private ItemColors colors;

@Unique
private final ThreadLocal<ItemRenderContext> indium_contexts = ThreadLocal.withInitial(() -> new ItemRenderContext(colors));

@Unique
private final ItemRenderContext.VanillaQuadHandler indium_vanillaHandler = new IndiumQuadHandler(this);

@Shadow
protected abstract void renderBakedItemModel(BakedModel model, ItemStack stack, int light, int overlay, MatrixStack matrixStack, VertexConsumer buffer);

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/BakedModel;isBuiltin()Z"), method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformationMode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", cancellable = true)
public void hook_renderItem(ItemStack stack, ModelTransformationMode transformMode, boolean invert, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int light, int overlay, BakedModel model, CallbackInfo ci) {
if (!model.isVanillaAdapter()) {
indium_contexts.get().renderModel(stack, transformMode, invert, matrixStack, vertexConsumerProvider, light, overlay, model, indium_vanillaHandler);
indium_contexts.get().renderModel(stack, transformMode, invert, matrixStack, vertexConsumerProvider, light, overlay, model);
matrixStack.pop();
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@

package link.infra.indium.renderer.render;

import static link.infra.indium.renderer.helper.GeometryHelper.AXIS_ALIGNED_FLAG;
import static link.infra.indium.renderer.helper.GeometryHelper.LIGHT_FACE_FLAG;

import java.util.List;

import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

import link.infra.indium.Indium;
import link.infra.indium.renderer.IndiumRenderer;
import link.infra.indium.renderer.aocalc.AoCalculator;
import link.infra.indium.renderer.aocalc.AoConfig;
import link.infra.indium.renderer.helper.ColorHelper;
Expand All @@ -36,17 +27,22 @@
import me.jellysquid.mods.sodium.client.render.chunk.terrain.material.Material;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;
import net.fabricmc.fabric.api.util.TriState;
import net.fabricmc.fabric.impl.renderer.VanillaModelEncoder;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockRenderView;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

import static link.infra.indium.renderer.helper.GeometryHelper.AXIS_ALIGNED_FLAG;
import static link.infra.indium.renderer.helper.GeometryHelper.LIGHT_FACE_FLAG;

/**
* Subclasses must set the {@link #blockInfo} and {@link #aoCalc} fields in their constructor.
Expand All @@ -67,6 +63,18 @@ public void emitDirectly() {
}
};

private final MutableQuadViewImpl vanillaModelEditorQuad = new MutableQuadViewImpl() {
{
data = new int[EncodingFormat.TOTAL_STRIDE];
clear();
}

@Override
public void emitDirectly() {
renderQuad(this, true);
}
};

private final BakedModelConsumerImpl vanillaModelConsumer = new BakedModelConsumerImpl();

protected abstract LightDataAccess getLightCache();
Expand All @@ -79,6 +87,21 @@ public QuadEmitter getEmitter() {
return editorQuad;
}

public QuadEmitter getVanillaModelEmitter() {
// Do not clear the editorQuad since it is not accessible to API users.
return vanillaModelEditorQuad;
}

@Override
public boolean isFaceCulled(@Nullable Direction face) {
return !blockInfo.shouldDrawFace(face);
}

@Override
public ModelTransformationMode itemTransformationMode() {
throw new IllegalStateException("itemTransformationMode() can only be called on an item render context.");
}

@Override
public BakedModelConsumer bakedModelConsumer() {
return vanillaModelConsumer;
Expand All @@ -89,7 +112,7 @@ private void renderQuad(MutableQuadViewImpl quad, boolean isVanilla) {
return;
}

if (!blockInfo.shouldDrawFace(quad.cullFace())) {
if (isFaceCulled(quad.cullFace())) {
return;
}

Expand Down Expand Up @@ -296,45 +319,14 @@ private static int getOffsetLightmap(LightDataAccess lightCache, BlockPos pos, D
* them through vanilla logic would require additional hooks.
*/
private class BakedModelConsumerImpl implements BakedModelConsumer {
private static final RenderMaterial MATERIAL_SHADED = IndiumRenderer.INSTANCE.materialFinder().find();
private static final RenderMaterial MATERIAL_FLAT = IndiumRenderer.INSTANCE.materialFinder().ambientOcclusion(TriState.FALSE).find();

private final MutableQuadViewImpl editorQuad = new MutableQuadViewImpl() {
{
data = new int[EncodingFormat.TOTAL_STRIDE];
clear();
}

@Override
public void emitDirectly() {
renderQuad(this, true);
}
};

@Override
public void accept(BakedModel model) {
accept(model, blockInfo.blockState);
}

@Override
public void accept(BakedModel model, @Nullable BlockState state) {
MutableQuadViewImpl editorQuad = this.editorQuad;
final RenderMaterial defaultMaterial = model.useAmbientOcclusion() ? MATERIAL_SHADED : MATERIAL_FLAT;

for (int i = 0; i <= ModelHelper.NULL_FACE_ID; i++) {
final Direction cullFace = ModelHelper.faceFromIndex(i);
final List<BakedQuad> quads = model.getQuads(state, cullFace, blockInfo.randomSupplier.get());
final int count = quads.size();

for (int j = 0; j < count; j++) {
final BakedQuad q = quads.get(j);
editorQuad.fromVanilla(q, defaultMaterial, cullFace);
// Call renderQuad directly instead of emit for efficiency
renderQuad(editorQuad, true);
}
}

// Do not clear the editorQuad since it is not accessible to API users.
VanillaModelEncoder.emitBlockQuads(model, state, blockInfo.randomSupplier, AbstractBlockRenderContext.this, vanillaModelEditorQuad);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ protected final boolean transform(MutableQuadView q) {
return activeTransform.transform(q);
}

protected boolean hasTransform() {
@Override
public boolean hasTransform() {
return activeTransform != NO_TRANSFORM;
}

Expand Down

This file was deleted.

Loading

0 comments on commit 75088b0

Please sign in to comment.