Porting from all directions

- Diagonal-port restore state changes from 1.18/next
- Back-port buffer uploader changes and RenderLayerEvent dispatch point
change from 1.19/dev
- Make CrumblingRenderer return earlier if there is nothing to render
- Bump version
This commit is contained in:
PepperCode1 2022-09-21 15:34:58 -07:00
parent 738514b86b
commit 552c132512
7 changed files with 37 additions and 70 deletions

View File

@ -2,7 +2,7 @@ org.gradle.jvmargs = -Xmx3G
org.gradle.daemon = false
# mod version info
mod_version = 0.6.5
mod_version = 0.6.6
artifact_minecraft_version = 1.18.2
minecraft_version = 1.18.2

View File

@ -7,13 +7,12 @@ import com.mojang.blaze3d.platform.GlStateManager;
* Tracks bound buffers/vbos because GlStateManager doesn't do that for us.
*/
public class GlStateTracker {
private static final int[] buffers = new int[GlBufferType.values().length];
private static final int[] BUFFERS = new int[GlBufferType.values().length];
private static int vao;
private static int program;
public static int getBuffer(GlBufferType type) {
return buffers[type.ordinal()];
return BUFFERS[type.ordinal()];
}
public static int getVertexArray() {
@ -24,36 +23,36 @@ public class GlStateTracker {
return program;
}
public static void _setBuffer(GlBufferType type, int buffer) {
buffers[type.ordinal()] = buffer;
}
public static void _setProgram(int id) {
program = id;
public static void _setBuffer(GlBufferType type, int id) {
BUFFERS[type.ordinal()] = id;
}
public static void _setVertexArray(int id) {
vao = id;
}
public static void _setProgram(int id) {
program = id;
}
public static State getRestoreState() {
return new State(buffers.clone(), vao, program);
return new State(BUFFERS.clone(), vao, program);
}
public static record State(int[] buffers, int vao, int program) {
public void restore() {
if (vao != GlStateTracker.vao) {
GlStateManager._glBindVertexArray(vao);
}
GlBufferType[] values = GlBufferType.values();
for (int i = 0; i < values.length; i++) {
if (buffers[i] != GlStateTracker.buffers[i]) {
if (buffers[i] != GlStateTracker.BUFFERS[i]) {
GlStateManager._glBindBuffer(values[i].glEnum, buffers[i]);
}
}
if (vao != GlStateTracker.vao) {
GlStateManager._glBindVertexArray(vao);
}
if (program != GlStateTracker.program) {
GlStateManager._glUseProgram(program);
}

View File

@ -10,6 +10,7 @@ import javax.annotation.Nullable;
import com.jozufozu.flywheel.api.MaterialGroup;
import com.jozufozu.flywheel.backend.RenderLayer;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.backend.instancing.Engine;
import com.jozufozu.flywheel.backend.instancing.TaskEngine;
import com.jozufozu.flywheel.core.compile.ProgramCompiler;
@ -73,6 +74,8 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
*/
@Override
public void render(TaskEngine taskEngine, RenderLayerEvent event) {
GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
double camX;
double camY;
double camZ;
@ -92,6 +95,8 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
}
getGroupsToRender(event.getLayer()).forEach(group -> group.render(viewProjection, camX, camY, camZ, event.getLayer()));
restoreState.restore();
}
private Stream<InstancedMaterialGroup<P>> getGroupsToRender(@Nullable RenderLayer layer) {

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.SortedSet;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.backend.gl.GlTextureUnit;
import com.jozufozu.flywheel.backend.instancing.InstanceManager;
import com.jozufozu.flywheel.backend.instancing.SerialTaskEngine;
@ -16,6 +17,7 @@ import com.jozufozu.flywheel.mixin.LevelRendererAccessor;
import com.jozufozu.flywheel.util.Lazy;
import com.jozufozu.flywheel.util.Pair;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
@ -30,6 +32,7 @@ import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.BlockDestructionProgress;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@ -54,13 +57,20 @@ public class CrumblingRenderer {
INVALIDATOR = state.second();
}
public static void renderBreaking(RenderLayerEvent event) {
if (!Backend.canUseInstancing(event.getWorld())) return;
Int2ObjectMap<List<BlockEntity>> activeStages = getActiveStageBlockEntities(event.getWorld());
public static void render(ClientLevel level, Camera camera, PoseStack stack) {
if (!Backend.canUseInstancing(level)) return;
Int2ObjectMap<List<BlockEntity>> activeStages = getActiveStageBlockEntities(level);
if (activeStages.isEmpty()) return;
Vec3 cameraPos = camera.getPosition();
GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
CrumblingRenderer.renderBreaking(activeStages, new RenderLayerEvent(level, null, stack, null, cameraPos.x, cameraPos.y, cameraPos.z));
restoreState.restore();
}
private static void renderBreaking(Int2ObjectMap<List<BlockEntity>> activeStages, RenderLayerEvent event) {
State state = STATE.get();
InstanceManager<BlockEntity> instanceManager = state.instanceManager;
InstancingEngine<CrumblingProgram> materials = state.materialManager;

View File

@ -1,29 +0,0 @@
package com.jozufozu.flywheel.mixin;
import javax.annotation.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.VertexFormat;
@Mixin(BufferUploader.class)
public class BufferUploaderMixin {
@Shadow
@Nullable
private static VertexFormat lastFormat;
@Inject(method = "reset", at = @At("HEAD"))
private static void stopBufferUploaderFromClearingBufferStateIfNothingIsBound(CallbackInfo ci) {
// Trust our tracker over BufferUploader's.
if (GlStateTracker.getVertexArray() == 0) {
lastFormat = null;
}
}
}

View File

@ -10,13 +10,11 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer;
import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
import com.jozufozu.flywheel.event.RenderLayerEvent;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Matrix4f;
@ -30,7 +28,6 @@ import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
@ -58,7 +55,7 @@ public class LevelRendererMixin {
* This only gets injected if renderChunkLayer is not Overwritten
*/
@Group(name = "flywheel$renderLayer", min = 1, max = 2)
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/ShaderInstance;clear()V"), method = "renderChunkLayer")
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiling/ProfilerFiller;pop()V", ordinal = 1), method = "renderChunkLayer")
private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) {
flywheel$renderLayer(type, stack, camX, camY, camZ);
flywheel$LayerRendered = true;
@ -74,18 +71,11 @@ public class LevelRendererMixin {
flywheel$renderLayer(type, stack, camX, camY, camZ);
}
flywheel$LayerRendered = false;
BufferUploader.reset();
}
@Unique
private void flywheel$renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ) {
RenderBuffers renderBuffers = this.renderBuffers;
GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
MinecraftForge.EVENT_BUS.post(new RenderLayerEvent(level, type, stack, renderBuffers, camX, camY, camZ));
restoreState.restore();
}
@Inject(at = @At("TAIL"), method = "allChanged")
@ -95,17 +85,10 @@ public class LevelRendererMixin {
MinecraftForge.EVENT_BUS.post(new ReloadRenderersEvent(level));
}
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;checkPoseStack(Lcom/mojang/blaze3d/vertex/PoseStack;)V", ordinal = 2 // after the game renders the breaking overlay normally
), method = "renderLevel")
private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera info, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) {
if (!Backend.isOn()) return;
Vec3 cameraPos = info.getPosition();
GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
CrumblingRenderer.renderBreaking(new RenderLayerEvent(level, null, stack, null, cameraPos.x, cameraPos.y, cameraPos.z));
restoreState.restore();
private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) {
CrumblingRenderer.render(level, camera, stack);
}
// Instancing

View File

@ -8,7 +8,6 @@
"BlockEntityRenderDispatcherAccessor",
"BlockEntityTypeMixin",
"BufferBuilderMixin",
"BufferUploaderMixin",
"CameraMixin",
"ClientLevelMixin",
"ChunkRebuildHooksMixin",