Material refactor pt 4

- Begin reworking layer rendering
This commit is contained in:
Jozufozu 2022-04-19 13:42:45 -07:00
parent 671ac33dda
commit 7cf653eda5
9 changed files with 126 additions and 84 deletions

View File

@ -14,6 +14,7 @@ import com.jozufozu.flywheel.util.WorldAttached;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.Sheets;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.entity.BlockEntity;
@ -101,7 +102,7 @@ public class InstancedRenderDispatcher {
ClientLevel world = event.getWorld();
if (!Backend.canUseInstancing(world)) return;
instanceWorlds.get(world).renderLayer(event.context);
instanceWorlds.get(world).renderLayer(event.context);//.withRenderType(Sheets.chestSheet()));
}
@SubscribeEvent

View File

@ -84,4 +84,13 @@ public class InstancedMaterial<D extends InstanceData> implements Material<D> {
}
}
}
public boolean anythingToRender(RenderType type) {
for (GPUInstancer<?> instancer : models.values()) {
if (instancer.shouldRenderIn(type)) {
return true;
}
}
return false;
}
}

View File

@ -87,14 +87,17 @@ public class InstancingEngine<P extends WorldProgram> implements Engine {
for (Map.Entry<Instanced<? extends InstanceData>, InstancedMaterial<?>> entry : materials.entrySet()) {
InstancedMaterial<?> material = entry.getValue();
Instanced<? extends InstanceData> instanceType = entry.getKey();
setup(type, camX, camY, camZ, viewProjection, instanceType.getProgramSpec());
//if (material.anythingToRender(type)) {
Instanced<? extends InstanceData> instanceType = entry.getKey();
instanceCount += material.getInstanceCount();
vertexCount += material.getVertexCount();
setup(type, camX, camY, camZ, viewProjection, instanceType.getProgramSpec());
material.renderIn(type);
instanceCount += material.getInstanceCount();
vertexCount += material.getVertexCount();
material.renderIn(type);
//}
}
type.clearRenderState();

View File

@ -13,6 +13,12 @@ import net.minecraft.client.renderer.RenderType;
public record RenderContext(ClientLevel level, RenderType type, PoseStack stack, Matrix4f viewProjection, RenderBuffers buffers,
double camX, double camY, double camZ) implements TransformStack {
public static RenderContext CURRENT;
public RenderContext withRenderType(RenderType renderType) {
return new RenderContext(level, renderType, stack, viewProjection, buffers, camX, camY, camZ);
}
@Override
public TransformStack multiply(Quaternion quaternion) {
return TransformStack.cast(stack).multiply(quaternion);

View File

@ -1,5 +1,7 @@
package com.jozufozu.flywheel.event;
import org.jetbrains.annotations.NotNull;
import com.jozufozu.flywheel.core.RenderContext;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
@ -13,13 +15,22 @@ import net.minecraftforge.eventbus.api.Event;
public class RenderLayerEvent extends Event {
public final RenderContext context;
public RenderLayerEvent(RenderContext context) {
this.context = context;
}
public RenderLayerEvent(ClientLevel world, RenderType type, PoseStack stack, RenderBuffers buffers, double camX, double camY, double camZ) {
var viewProjection = stack.last()
context = new RenderContext(world, type, stack, createViewProjection(stack), buffers, camX, camY, camZ);
}
@NotNull
public static Matrix4f createViewProjection(PoseStack view) {
var viewProjection = view.last()
.pose()
.copy();
viewProjection.multiplyBackward(RenderSystem.getProjectionMatrix());
context = new RenderContext(world, type, stack, viewProjection, buffers, camX, camY, camZ);
viewProjection.multiplyBackward(RenderSystem.getProjectionMatrix());
return viewProjection;
}
@Override

View File

@ -12,6 +12,7 @@ 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.RenderContext;
import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer;
import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
@ -46,49 +47,28 @@ public class LevelRendererMixin {
@Shadow
@Final
private RenderBuffers renderBuffers;
//
// @Inject(at = @At("HEAD"), method = "setupRender")
// private void setupRender(Camera camera, Frustum frustum, boolean queue, boolean isSpectator, CallbackInfo ci) {
//
// GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
// MinecraftForge.EVENT_BUS.post(new BeginFrameEvent(level, camera, frustum));
// restoreState.restore();
// }
@Inject(at = @At("HEAD"), method = "renderLevel")
private void beginRender(PoseStack pPoseStack, float pPartialTick, long pFinishNanoTime, boolean pRenderBlockOutline, Camera pCamera, GameRenderer pGameRenderer, LightTexture pLightTexture, Matrix4f pProjectionMatrix, CallbackInfo ci) {
Vec3 position = pCamera.getPosition();
RenderContext.CURRENT = new RenderContext(level, null, pPoseStack, RenderLayerEvent.createViewProjection(pPoseStack), renderBuffers, position.x, position.y, position.z);
@Inject(at = @At("HEAD"), method = "setupRender")
private void setupRender(Camera camera, Frustum frustum, boolean queue, boolean isSpectator, CallbackInfo ci) {
GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
MinecraftForge.EVENT_BUS.post(new BeginFrameEvent(level, camera, frustum));
MinecraftForge.EVENT_BUS.post(new BeginFrameEvent(level, pCamera, null));
restoreState.restore();
}
@Unique
private boolean flywheel$LayerRendered;
/**
* 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")
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;
}
/**
* This always gets injected.
*/
@Group(name = "flywheel$renderLayer")
@Inject(at = @At("TAIL"), method = "renderChunkLayer")
private void renderLayerSodium(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) {
if (!flywheel$LayerRendered) {
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 = "renderLevel")
private void endRender(PoseStack pPoseStack, float pPartialTick, long pFinishNanoTime, boolean pRenderBlockOutline, Camera pCamera, GameRenderer pGameRenderer, LightTexture pLightTexture, Matrix4f pProjectionMatrix, CallbackInfo ci) {
RenderContext.CURRENT = null;
}
@Inject(at = @At("TAIL"), method = "allChanged")

View File

@ -0,0 +1,31 @@
package com.jozufozu.flywheel.mixin;
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.CallbackInfo;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlStateTracker;
import com.jozufozu.flywheel.core.RenderContext;
import com.jozufozu.flywheel.event.RenderLayerEvent;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderBuffers;
import net.minecraft.client.renderer.RenderType;
import net.minecraftforge.common.MinecraftForge;
@Mixin(MultiBufferSource.BufferSource.class)
public class MultiBufferSourceMixin {
@Inject(method = "endBatch(Lnet/minecraft/client/renderer/RenderType;)V", at = @At("TAIL"))
private void renderLayer(RenderType renderType, CallbackInfo ci) {
if (RenderContext.CURRENT != null && Backend.isGameActive() && Backend.isOn()) {
GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
MinecraftForge.EVENT_BUS.post(new RenderLayerEvent(RenderContext.CURRENT.withRenderType(renderType)));
restoreState.restore();
}
}
}

View File

@ -34,8 +34,8 @@ import net.minecraft.world.level.block.state.properties.ChestType;
public class ChestInstance<T extends BlockEntity & LidBlockEntity> extends BlockEntityInstance<T> implements DynamicInstance {
private static final BiFunction<ChestType, Material, ModelSupplier> LID = Util.memoize((type, mat) -> new ModelSupplier(() -> createLidModel(type, mat.sprite()), RenderType.entitySolid(mat.atlasLocation())));
private static final BiFunction<ChestType, Material, ModelSupplier> BASE = Util.memoize((type, mat) -> new ModelSupplier(() -> createBaseModel(type, mat.sprite()), RenderType.entitySolid(mat.atlasLocation())));
private static final BiFunction<ChestType, Material, ModelSupplier> LID = Util.memoize((type, mat) -> new ModelSupplier(() -> createLidModel(type, mat.sprite()), Sheets.chestSheet()));
private static final BiFunction<ChestType, Material, ModelSupplier> BASE = Util.memoize((type, mat) -> new ModelSupplier(() -> createBaseModel(type, mat.sprite()), Sheets.chestSheet()));
private final OrientedData body;
private final ModelData lid;

View File

@ -1,37 +1,38 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.jozufozu.flywheel.mixin",
"compatibilityLevel": "JAVA_17",
"refmap": "flywheel.refmap.json",
"client": [
"BlockEntityRenderDispatcherAccessor",
"BlockEntityTypeMixin",
"BufferBuilderMixin",
"BufferUploaderMixin",
"CameraMixin",
"ClientLevelMixin",
"ChunkRebuildHooksMixin",
"EntityTypeMixin",
"FixFabulousDepthMixin",
"FrustumMixin",
"GlStateManagerMixin",
"InstanceAddMixin",
"InstanceRemoveMixin",
"LevelRendererAccessor",
"LevelRendererMixin",
"PausedPartialTickAccessor",
"RenderTexturesMixin",
"RenderTypeMixin",
"atlas.AtlasDataMixin",
"atlas.SheetDataAccessor",
"light.LightUpdateMixin",
"light.NetworkLightUpdateMixin",
"matrix.Matrix3fMixin",
"matrix.Matrix4fMixin",
"matrix.PoseStackMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"minVersion": "0.8",
"package": "com.jozufozu.flywheel.mixin",
"compatibilityLevel": "JAVA_17",
"refmap": "flywheel.refmap.json",
"client": [
"BlockEntityRenderDispatcherAccessor",
"BlockEntityTypeMixin",
"BufferBuilderMixin",
"BufferUploaderMixin",
"CameraMixin",
"ChunkRebuildHooksMixin",
"ClientLevelMixin",
"EntityTypeMixin",
"FixFabulousDepthMixin",
"FrustumMixin",
"GlStateManagerMixin",
"InstanceAddMixin",
"InstanceRemoveMixin",
"LevelRendererAccessor",
"LevelRendererMixin",
"MultiBufferSourceMixin",
"PausedPartialTickAccessor",
"RenderTexturesMixin",
"RenderTypeMixin",
"atlas.AtlasDataMixin",
"atlas.SheetDataAccessor",
"light.LightUpdateMixin",
"light.NetworkLightUpdateMixin",
"matrix.Matrix3fMixin",
"matrix.Matrix4fMixin",
"matrix.PoseStackMixin"
],
"injectors": {
"defaultRequire": 1
}
}