Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize JEI multiblock preview allocations #2629

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import codechicken.lib.vec.Vector3;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

Expand All @@ -33,8 +35,6 @@
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;

import javax.vecmath.Vector3f;
Expand All @@ -61,7 +61,7 @@ public abstract class WorldSceneRenderer {
.order(ByteOrder.nativeOrder()).asFloatBuffer();

public final World world;
public final Map<Collection<BlockPos>, ISceneRenderHook> renderedBlocksMap;
public final Collection<BlockPos> renderedBlocks = new ObjectOpenHashSet<>();
private Consumer<WorldSceneRenderer> beforeRender;
private Consumer<WorldSceneRenderer> afterRender;
private Consumer<RayTraceResult> onLookingAt;
Expand All @@ -73,7 +73,6 @@ public abstract class WorldSceneRenderer {

public WorldSceneRenderer(World world) {
this.world = world;
renderedBlocksMap = new LinkedHashMap<>();
}

public WorldSceneRenderer setBeforeWorldRender(Consumer<WorldSceneRenderer> callback) {
Expand All @@ -86,9 +85,9 @@ public WorldSceneRenderer setAfterWorldRender(Consumer<WorldSceneRenderer> callb
return this;
}

public WorldSceneRenderer addRenderedBlocks(Collection<BlockPos> blocks, ISceneRenderHook renderHook) {
public WorldSceneRenderer addRenderedBlocks(@Nullable Collection<BlockPos> blocks) {
if (blocks != null) {
this.renderedBlocksMap.put(blocks, renderHook);
this.renderedBlocks.addAll(blocks);
}
return this;
}
Expand Down Expand Up @@ -240,31 +239,24 @@ protected void drawWorld() {
for (BlockRenderLayer layer : BlockRenderLayer.values()) {
ForgeHooksClient.setRenderLayer(layer);
int pass = layer == BlockRenderLayer.TRANSLUCENT ? 1 : 0;
setDefaultPassRenderState(pass);

renderedBlocksMap.forEach((renderedBlocks, hook) -> {
if (hook != null) {
hook.apply(false, pass, layer);
} else {
setDefaultPassRenderState(pass);
}
BufferBuilder buffer = Tessellator.getInstance().getBuffer();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
BlockRendererDispatcher blockrendererdispatcher = mc.getBlockRendererDispatcher();

BufferBuilder buffer = Tessellator.getInstance().getBuffer();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
BlockRendererDispatcher blockrendererdispatcher = mc.getBlockRendererDispatcher();

for (BlockPos pos : renderedBlocks) {
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block == Blocks.AIR) continue;
state = state.getActualState(world, pos);
if (block.canRenderInLayer(state, layer)) {
blockrendererdispatcher.renderBlock(state, pos, world, buffer);
}
for (BlockPos pos : renderedBlocks) {
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block == Blocks.AIR) continue;
state = state.getActualState(world, pos);
if (block.canRenderInLayer(state, layer)) {
blockrendererdispatcher.renderBlock(state, pos, world, buffer);
}
}

Tessellator.getInstance().draw();
Tessellator.getInstance().getBuffer().setTranslation(0, 0, 0);
});
Tessellator.getInstance().draw();
Tessellator.getInstance().getBuffer().setTranslation(0, 0, 0);
}
} finally {
ForgeHooksClient.setRenderLayer(oldRenderLayer);
Expand All @@ -276,22 +268,15 @@ protected void drawWorld() {
// render TESR
for (int pass = 0; pass < 2; pass++) {
ForgeHooksClient.setRenderPass(pass);
int finalPass = pass;
renderedBlocksMap.forEach((renderedBlocks, hook) -> {
if (hook != null) {
hook.apply(true, finalPass, null);
} else {
setDefaultPassRenderState(finalPass);
}
for (BlockPos pos : renderedBlocks) {
TileEntity tile = world.getTileEntity(pos);
if (tile != null) {
if (tile.shouldRenderInPass(finalPass)) {
TileEntityRendererDispatcher.instance.render(tile, pos.getX(), pos.getY(), pos.getZ(), 0);
}
setDefaultPassRenderState(pass);
for (BlockPos pos : renderedBlocks) {
TileEntity tile = world.getTileEntity(pos);
if (tile != null) {
if (tile.shouldRenderInPass(pass)) {
TileEntityRendererDispatcher.instance.render(tile, pos.getX(), pos.getY(), pos.getZ(), 0);
}
}
});
}
}
ForgeHooksClient.setRenderPass(-1);
GlStateManager.enableDepth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void createWorldScene() {
TrackedDummyWorld dummyWorld = new TrackedDummyWorld(world);
dummyWorld.setRenderFilter(pos -> validPos.contains(pos));
worldSceneRenderer = new FBOWorldSceneRenderer(dummyWorld, FBO);
worldSceneRenderer.addRenderedBlocks(validPos, null);
worldSceneRenderer.addRenderedBlocks(validPos);
center = new Vector3f((minX + maxX) / 2f + 0.5f, (minY + maxY) / 2f + 0.5f, (minZ + maxZ) / 2f + 0.5f);
worldSceneRenderer.setCameraLookAt(center, 10 / scale, Math.toRadians(rotationPitch),
Math.toRadians(rotationYaw));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private void setNextLayer(int newLayer) {
if (renderer != null) {
TrackedDummyWorld world = ((TrackedDummyWorld) renderer.world);
resetCenter(world);
renderer.renderedBlocksMap.clear();
renderer.renderedBlocks.clear();
int minY = (int) world.getMinPos().getY();
Collection<BlockPos> renderBlocks;
if (newLayer == -1) {
Expand All @@ -226,7 +226,7 @@ private void setNextLayer(int newLayer) {
renderBlocks = world.renderedBlocks.stream().filter(pos -> pos.getY() - minY == newLayer)
.collect(Collectors.toSet());
}
renderer.addRenderedBlocks(renderBlocks, null);
renderer.addRenderedBlocks(renderBlocks);
}
}

Expand Down Expand Up @@ -593,7 +593,7 @@ private MBPattern initializePattern(@NotNull MultiblockShapeInfo shapeInfo, @Not
Vector3f minPos = world.getMinPos();
center = new Vector3f(minPos.x + size.x / 2, minPos.y + size.y / 2, minPos.z + size.z / 2);

worldSceneRenderer.addRenderedBlocks(world.renderedBlocks, null);
worldSceneRenderer.addRenderedBlocks(world.renderedBlocks);
worldSceneRenderer.setOnLookingAt(ray -> {});

worldSceneRenderer.setAfterWorldRender(renderer -> {
Expand All @@ -607,8 +607,7 @@ private MBPattern initializePattern(@NotNull MultiblockShapeInfo shapeInfo, @Not
renderBlockOverLay(selected, 255, 0, 0);
});
world.updateEntities();
world.setRenderFilter(
pos -> worldSceneRenderer.renderedBlocksMap.keySet().stream().anyMatch(c -> c.contains(pos)));
world.setRenderFilter(worldSceneRenderer.renderedBlocks::contains);

Map<BlockPos, TraceabilityPredicate> predicateMap = new HashMap<>();
if (controllerBase != null) {
Expand Down