Skip to content

Commit

Permalink
Update to support FRAPI 3.4.0 (#335)
Browse files Browse the repository at this point in the history
- Copy changes from FabricMC/fabric#3937
- Copy Fabric impl VanillaModelEncoder class to avoid issues
- Initialize VanillaAoHelper statically as mods may create their own BlockModelRenderers
- Fix issues with buildscript
  • Loading branch information
PepperCode1 authored Aug 15, 2024
1 parent 3fba1eb commit f9034c1
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 192 deletions.
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ plugins {
id 'maven-publish'

id "com.github.breadmoirai.github-release" version "2.4.1"
id "org.ajoberstar.grgit" version "4.1.0"
id "org.ajoberstar.grgit" version "4.1.1"
id "com.modrinth.minotaur" version "2.+"
id "com.matthewprenger.cursegradle" version "1.4.0"
}

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

String getGitVersion(Project project) {
if (grgit != null) {
var dirty = grgit.status().clean ? "" : "-dirty"
Expand All @@ -36,10 +33,13 @@ String getChangelog(String githubUrl) {
}.join("\n") + (githubUrl == null ? "" : "\n\nSee the full changes on Github: ${githubUrl}commits/${getGitVersion(project)}")
}

archivesBaseName = project.archives_base_name
version = getGitVersion(project)
group = project.maven_group

base {
archivesName = project.archives_base_name
}

repositories {
maven {
url = "https://api.modrinth.com/maven"
Expand All @@ -59,9 +59,6 @@ dependencies {
// For testing in dev environment
modRuntimeOnly "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.

modImplementation "maven.modrinth:sodium:mc${project.sodium_minecraft_version}-${project.sodium_version}"
}

Expand Down Expand Up @@ -89,6 +86,9 @@ java {
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ maven_group=link.infra
archives_base_name=indium
# Dependencies
# check this on https://fabricmc.net/develop/
fabric_version=0.100.1+1.21
fabric_version=0.102.0+1.21
sodium_version=0.5.11
sodium_minecraft_version=1.21
# Publishing metadata
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import link.infra.indium.renderer.accessor.AccessBlockModelRenderer;
import link.infra.indium.renderer.aocalc.VanillaAoHelper;
import link.infra.indium.renderer.render.NonTerrainBlockRenderContext;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.VertexConsumer;
Expand Down Expand Up @@ -55,11 +54,6 @@ private void hookRender(BlockRenderView blockView, BakedModel model, BlockState
}
}

@Inject(at = @At("RETURN"), method = "<init>*")
private void onInit(CallbackInfo ci) {
VanillaAoHelper.initialize((BlockModelRenderer) (Object) this);
}

@Override
public void indium$getQuadDimensions(BlockRenderView blockView, BlockState blockState, BlockPos pos, int[] vertexData, Direction face, float[] aoData, BitSet controlBits) {
getQuadDimensions(blockView, blockState, pos, vertexData, face, aoData, controlBits);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/link/infra/indium/renderer/IndiumRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;

import link.infra.indium.renderer.material.MaterialFinderImpl;
import link.infra.indium.renderer.material.RenderMaterialImpl;
import link.infra.indium.renderer.mesh.MeshBuilderImpl;
import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.fabric.api.renderer.v1.material.MaterialFinder;
Expand Down Expand Up @@ -59,7 +60,7 @@ public boolean registerMaterial(Identifier id, RenderMaterial material) {
if (materialMap.containsKey(id)) return false;

// cast to prevent acceptance of impostor implementations
materialMap.put(id, material);
materialMap.put(id, (RenderMaterialImpl) material);
return true;
}
}
54 changes: 19 additions & 35 deletions src/main/java/link/infra/indium/renderer/aocalc/AoCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import link.infra.indium.mixin.renderer.AccessAmbientOcclusionCalculator;
import link.infra.indium.renderer.aocalc.AoFace.WeightFunction;
import link.infra.indium.renderer.mesh.EncodingFormat;
import link.infra.indium.renderer.mesh.MutableQuadViewImpl;
import link.infra.indium.renderer.mesh.QuadViewImpl;
import link.infra.indium.renderer.render.BlockRenderInfo;
import me.jellysquid.mods.sodium.client.model.light.data.LightDataAccess;
Expand Down Expand Up @@ -88,7 +87,7 @@ public class AoCalculator {
public AoCalculator(BlockRenderInfo blockInfo, LightDataAccess lightCache) {
this.blockInfo = blockInfo;
this.lightCache = lightCache;
this.vanillaCalc = VanillaAoHelper.get();
this.vanillaCalc = VanillaAoHelper.createCalc();

for (int i = 0; i < 24; i++) {
faceData[i] = new AoFaceData();
Expand All @@ -100,41 +99,21 @@ public void clear() {
completionFlags = 0;
}

public void compute(MutableQuadViewImpl quad, boolean isVanilla) {
public void compute(QuadViewImpl quad, boolean vanillaShade) {
final AoConfig config = Indium.AMBIENT_OCCLUSION_MODE;

switch (config) {
case VANILLA:
// prevent NPE in error case of failed reflection for vanilla calculator access
if (vanillaCalc == null) {
calcFastVanilla(quad);
} else {
calcVanilla(quad);
}

break;

case EMULATE:
calcFastVanilla(quad);
break;

case HYBRID:
default:
if (isVanilla) {
case VANILLA -> calcVanilla(quad);
case EMULATE -> calcFastVanilla(quad);
case HYBRID -> {
if (vanillaShade) {
calcFastVanilla(quad);
} else {
calcEnhanced(quad);
}

break;

case ENHANCED:
calcEnhanced(quad);
}
}

private void calcVanilla(MutableQuadViewImpl quad) {
calcVanilla(quad, ao, light);
case ENHANCED -> calcEnhanced(quad);
}
}

// These are what vanilla AO calc wants, per its usage in vanilla code
Expand All @@ -144,19 +123,24 @@ private void calcVanilla(MutableQuadViewImpl quad) {
private final BitSet vanillaAoControlBits = new BitSet(3);
private final int[] vertexData = new int[EncodingFormat.QUAD_STRIDE];

private void calcVanilla(MutableQuadViewImpl quad, float[] aoDest, int[] lightDest) {
private void calcVanilla(QuadViewImpl quad) {
if (vanillaCalc == null) {
calcFastVanilla(quad);
return;
}

vanillaAoControlBits.clear();
final Direction lightFace = quad.lightFace();
quad.toVanilla(vertexData, 0);

VanillaAoHelper.getQuadDimensions(blockInfo.blockView, blockInfo.blockState, blockInfo.blockPos, vertexData, lightFace, vanillaAoData, vanillaAoControlBits);
vanillaCalc.indium$apply(blockInfo.blockView, blockInfo.blockState, blockInfo.blockPos, lightFace, vanillaAoData, vanillaAoControlBits, quad.hasShade());

System.arraycopy(vanillaCalc.indium$brightness(), 0, aoDest, 0, 4);
System.arraycopy(vanillaCalc.indium$light(), 0, lightDest, 0, 4);
System.arraycopy(vanillaCalc.indium$brightness(), 0, ao, 0, 4);
System.arraycopy(vanillaCalc.indium$light(), 0, light, 0, 4);
}

private void calcFastVanilla(MutableQuadViewImpl quad) {
private void calcFastVanilla(QuadViewImpl quad) {
int flags = quad.geometryFlags();

// force to block face if shape is full cube - matches vanilla logic
Expand All @@ -171,7 +155,7 @@ private void calcFastVanilla(MutableQuadViewImpl quad) {
}
}

private void calcEnhanced(MutableQuadViewImpl quad) {
private void calcEnhanced(QuadViewImpl quad) {
switch (quad.geometryFlags()) {
case AXIS_ALIGNED_FLAG | CUBIC_FLAG | LIGHT_FACE_FLAG:
case AXIS_ALIGNED_FLAG | LIGHT_FACE_FLAG:
Expand Down Expand Up @@ -246,7 +230,7 @@ private void blendedPartialFace(QuadViewImpl quad, Direction lightFace, boolean
/** used exclusively in irregular face to avoid new heap allocations each call. */
private final Vector3f vertexNormal = new Vector3f();

private void irregularFace(MutableQuadViewImpl quad, boolean shade) {
private void irregularFace(QuadViewImpl quad, boolean shade) {
final Vector3f faceNorm = quad.faceNormal();
Vector3f normal;
final float[] w = this.w;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,36 @@
import link.infra.indium.renderer.accessor.AccessBlockModelRenderer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.block.BlockModelRenderer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockRenderView;

public class VanillaAoHelper {
private static Supplier<AccessAmbientOcclusionCalculator> factory;

// Renderer method we call isn't declared as static, but uses no
// instance data and is called from multiple threads in vanilla also.
private static AccessBlockModelRenderer blockRenderer;

public static void initialize(BlockModelRenderer instance) {
blockRenderer = (AccessBlockModelRenderer) instance;
private static final AccessBlockModelRenderer BLOCK_RENDERER = (AccessBlockModelRenderer) MinecraftClient.getInstance().getBlockRenderManager().getModelRenderer();
@Nullable
private static final Supplier<AccessAmbientOcclusionCalculator> FACTORY;

static {
final String target = FabricLoader.getInstance().getMappingResolver()
.mapClassName("intermediary", "net.minecraft.class_778$class_780");

Supplier<AccessAmbientOcclusionCalculator> factory = null;

for (Class<?> innerClass : BlockModelRenderer.class.getDeclaredClasses()) {
if (innerClass.getName().equals(target)) {
Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
constructor.setAccessible(true);

factory = new Supplier<AccessAmbientOcclusionCalculator>() {
@Override
public AccessAmbientOcclusionCalculator get() {
try {
return (AccessAmbientOcclusionCalculator) constructor.newInstance();
} catch (Exception e) {
Indium.LOGGER.warn("[Indium] Exception accessing vanilla smooth lighter", e);
return null;
}
factory = () -> {
try {
return (AccessAmbientOcclusionCalculator) constructor.newInstance();
} catch (Exception e) {
Indium.LOGGER.warn("[Indium] Exception creating vanilla smooth lighter", e);
return null;
}
};
break;
Expand All @@ -72,14 +70,16 @@ public AccessAmbientOcclusionCalculator get() {
if (factory == null) {
Indium.LOGGER.warn("[Indium] Vanilla smooth lighter unavailable. Indium lighter will be used even if not configured.");
}

FACTORY = factory;
}

@Nullable
public static AccessAmbientOcclusionCalculator get() {
return factory == null ? null : factory.get();
public static AccessAmbientOcclusionCalculator createCalc() {
return FACTORY == null ? null : FACTORY.get();
}

public static void getQuadDimensions(BlockRenderView blockRenderView, BlockState blockState, BlockPos pos, int[] vertexData, Direction face, float[] aoData, BitSet controlBits) {
blockRenderer.indium$getQuadDimensions(blockRenderView, blockState, pos, vertexData, face, aoData, controlBits);
BLOCK_RENDERER.indium$getQuadDimensions(blockRenderView, blockState, pos, vertexData, face, aoData, controlBits);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package link.infra.indium.renderer.helper;

import java.util.List;
import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.material.ShadeMode;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.fabricmc.fabric.api.util.TriState;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;

/**
* Routines for adaptation of vanilla {@link BakedModel}s to FRAPI pipelines.
* Copy of {@link net.fabricmc.fabric.impl.renderer.VanillaModelEncoder} to avoid issues if the Fabric impl class receives changes.
*/
public class VanillaModelEncoder {
private static final Renderer RENDERER = RendererAccess.INSTANCE.getRenderer();
private static final RenderMaterial STANDARD_MATERIAL = RENDERER.materialFinder().shadeMode(ShadeMode.VANILLA).find();
private static final RenderMaterial NO_AO_MATERIAL = RENDERER.materialFinder().shadeMode(ShadeMode.VANILLA).ambientOcclusion(TriState.FALSE).find();

public static void emitBlockQuads(BakedModel model, @Nullable BlockState state, Supplier<Random> randomSupplier, RenderContext context) {
QuadEmitter emitter = context.getEmitter();
final RenderMaterial defaultMaterial = model.useAmbientOcclusion() ? STANDARD_MATERIAL : NO_AO_MATERIAL;

for (int i = 0; i <= ModelHelper.NULL_FACE_ID; i++) {
final Direction cullFace = ModelHelper.faceFromIndex(i);

if (!context.hasTransform() && context.isFaceCulled(cullFace)) {
// Skip entire quad list if possible.
continue;
}

final List<BakedQuad> quads = model.getQuads(state, cullFace, randomSupplier.get());
final int count = quads.size();

for (int j = 0; j < count; j++) {
final BakedQuad q = quads.get(j);
emitter.fromVanilla(q, defaultMaterial, cullFace);
emitter.emit();
}
}
}

public static void emitItemQuads(BakedModel model, @Nullable BlockState state, Supplier<Random> randomSupplier, RenderContext context) {
QuadEmitter emitter = context.getEmitter();

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

for (int j = 0; j < count; j++) {
final BakedQuad q = quads.get(j);
emitter.fromVanilla(q, STANDARD_MATERIAL, cullFace);
emitter.emit();
}
}
}
}

Loading

0 comments on commit f9034c1

Please sign in to comment.