Skip to content

Commit

Permalink
Fix crash with canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Sep 12, 2020
1 parent fb57d4b commit fa38e2c
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 99 deletions.

This file was deleted.

22 changes: 20 additions & 2 deletions src/main/java/ladysnake/satin/api/util/RenderLayerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*/
package ladysnake.satin.api.util;

import ladysnake.satin.impl.BlockRenderLayerRegistry;
import ladysnake.satin.impl.RenderLayerDuplicator;
import ladysnake.satin.mixin.client.render.RenderPhaseAccessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.RenderPhase;
import org.apiguardian.api.API;
Expand Down Expand Up @@ -56,8 +58,8 @@ public static String getName(RenderPhase phase) {
* builder -> builder.target(someShaderEnablingTarget)
* );}</pre>
*
* @param existing the render layer to duplicate
* @param newName the name of the new render layer
* @param existing the render layer to duplicate
* @param newName the name of the new render layer
* @param phaseTransform a transformation operation to apply to the new layer's {@linkplain RenderLayer.MultiPhaseParameters.Builder parameter builder}
* @return a {@link RenderLayer} with the same base parameters as {@code existing} but modified according to {@code phaseTransform}
* @throws IllegalArgumentException if {@code existing} is not a {@code MultiPhase} render layer
Expand All @@ -71,4 +73,20 @@ public static RenderLayer copy(RenderLayer existing, String newName, Consumer<Re
public static RenderLayer.MultiPhaseParameters copyPhaseParameters(RenderLayer existing, Consumer<RenderLayer.MultiPhaseParameters.Builder> phaseTransform) {
return RenderLayerDuplicator.copyPhaseParameters(existing, phaseTransform);
}

/**
* Register a custom RenderLayer for block rendering.
*
* <p><strong>Custom block render layers are usually not supported by alternative renderers.</strong>
* Calling this method will have no visible effect if one of those is active.
*
* @throws IllegalStateException if this method is called after {@link MinecraftClient#getBufferBuilders()
* buffer builders} have been initialized.
* @deprecated this feature is likely to cause incompatibilities. Use at your own discretion.
*/
@Deprecated
@API(status = EXPERIMENTAL, since = "1.5.0")
public static void registerBlockRenderLayer(RenderLayer layer) {
BlockRenderLayerRegistry.INSTANCE.registerRenderLayer(layer);
}
}
47 changes: 47 additions & 0 deletions src/main/java/ladysnake/satin/impl/BlockRenderLayerRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Satin
* Copyright (C) 2019-2020 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package ladysnake.satin.impl;

import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import net.minecraft.client.render.RenderLayer;

import java.util.Set;

public final class BlockRenderLayerRegistry {
public static final BlockRenderLayerRegistry INSTANCE = new BlockRenderLayerRegistry();
private final Set<RenderLayer> renderLayers = new ObjectArraySet<>(); // ArraySet for faster iteration
private volatile boolean registryLocked = false;

private BlockRenderLayerRegistry(){}

public void registerRenderLayer(RenderLayer layer) {
if(registryLocked){
throw new IllegalStateException(String.format(
"RenderLayer %s was added too late.",
layer.toString()
));
}

renderLayers.add(layer);
}

public Set<RenderLayer> getLayers() {
registryLocked = true;
return renderLayers;
}
}

This file was deleted.

82 changes: 82 additions & 0 deletions src/main/java/ladysnake/satin/mixin/SatinMixinPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Satin
* Copyright (C) 2019-2020 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package ladysnake.satin.mixin;

import ladysnake.satin.Satin;
import net.fabricmc.loader.api.FabricLoader;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.util.List;
import java.util.Set;

public final class SatinMixinPlugin implements IMixinConfigPlugin {
private static final boolean ALLOW_RENDER_LAYER_MIXINS;

static {
FabricLoader loader = FabricLoader.getInstance();
if (loader.isModLoaded("canvas")) {
Satin.LOGGER.warn("[Satin] Canvas is present, custom block renders will not work");
ALLOW_RENDER_LAYER_MIXINS = false;
} else {
if (loader.isModLoaded("sodium")) {
Satin.LOGGER.warn("[Satin] Sodium is present, custom block renders may not work");
}
ALLOW_RENDER_LAYER_MIXINS = true;
}
}

@Override
public void onLoad(String mixinPackage) {
// NO-OP
}

@Override
public String getRefMapperConfig() {
return null;
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (targetClassName.startsWith("blockrenderlayer")) {
return ALLOW_RENDER_LAYER_MIXINS;
}
return true;
}

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
// NO-OP
}

@Override
public List<String> getMixins() {
return null;
}

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
// NO-OP
}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
// NO-OP
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Satin
* Copyright (C) 2019-2020 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package ladysnake.satin.mixin.client.blockrenderlayer;

import com.google.common.collect.ImmutableList;
import ladysnake.satin.impl.BlockRenderLayerRegistry;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.RenderPhase;
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.CallbackInfoReturnable;

@Mixin(RenderLayer.class)
public abstract class RenderLayerMixin extends RenderPhase {
private RenderLayerMixin() {
super(null, null, null);
}

@Inject(
method = "getBlockLayers",
at = @At("RETURN"),
cancellable = true
)
private static void getBlockLayers(CallbackInfoReturnable<ImmutableList<Object>> info) {
info.setReturnValue(
ImmutableList.builder()
.addAll(info.getReturnValue())
.addAll(BlockRenderLayerRegistry.INSTANCE.getLayers()
).build());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package ladysnake.satin.mixin.client.render;
/*
* Satin
* Copyright (C) 2019-2020 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package ladysnake.satin.mixin.client.blockrenderlayer;

import ladysnake.satin.impl.BlockRenderLayerRegistryImpl;
import ladysnake.satin.impl.BlockRenderLayerRegistry;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.Frustum;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -21,7 +37,7 @@
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(WorldRenderer.class)
public abstract class WorldRendererMixin implements SynchronousResourceReloadListener, AutoCloseable {
public abstract class WorldRendererMixin {
@Shadow protected abstract void renderLayer(RenderLayer layer, MatrixStack matrix, double x, double y, double z);

@Inject(
Expand All @@ -42,15 +58,15 @@ public abstract class WorldRendererMixin implements SynchronousResourceReloadLis
target = "Lnet/minecraft/client/render/SkyProperties;isDarkened()Z"
)
),
locals = LocalCapture.CAPTURE_FAILHARD
locals = LocalCapture.CAPTURE_FAILSOFT
)
private void renderCustom(
MatrixStack matrix, float delta, long time, boolean renderOutlines, Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightManager, Matrix4f frustumMatrix,
CallbackInfo info,
Profiler profiler, Vec3d vec3d, double x, double y, double z
) {
// Render all the custom ones
for(RenderLayer layer : BlockRenderLayerRegistryImpl.INSTANCE.getLayers()) {
for(RenderLayer layer : BlockRenderLayerRegistry.INSTANCE.getLayers()) {
renderLayer(layer, matrix, x, y, z);
}
}
Expand Down

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/resources/mixins.satin.client.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"required": true,
"compatibilityLevel": "JAVA_8",
"package": "ladysnake.satin.mixin.client",
"plugin": "ladysnake.satin.mixin.SatinMixinPlugin",
"mixins": [
"AccessiblePassesShaderEffect",
"event.GameRendererMixin",
Expand All @@ -14,9 +15,9 @@
"gl.Matrix4fMixin",
"render.MultiPhaseParametersAccessor",
"render.RenderLayerAccessor",
"render.RenderLayerMixin",
"blockrenderlayer.RenderLayerMixin",
"render.RenderPhaseAccessor",
"render.WorldRendererMixin"
"blockrenderlayer.WorldRendererMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Loading

0 comments on commit fa38e2c

Please sign in to comment.