Skip to content

Commit

Permalink
Merge remote-tracking branch 'slprime/fix_item_rendering_cache' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Master committed Jan 9, 2025
2 parents 4dace1f + 60c94f4 commit 12c4995
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 81 deletions.
7 changes: 4 additions & 3 deletions src/main/java/codechicken/nei/BookmarkPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ public void addItem(ItemStack stackA, ItemStackMetadata meta, boolean animate) {
this.realItems.add(stackA);
this.metadata.add(meta);

if (animate && !NEIClientConfig.shouldCacheItemRendering() && NEIClientConfig.areBookmarksAnimated()) {
if (animate && NEIClientConfig.getGridRenderingCacheMode() == 0 && NEIClientConfig.areBookmarksAnimated()) {
this.animation.put(stackA, 0f);
}

Expand Down Expand Up @@ -1040,8 +1040,9 @@ protected void moveItem(SortableItem sortableItem, int slotIndex, int groupId, b
}
}

protected boolean shouldCacheItemRendering() {
return NEIClientConfig.shouldCacheItemRendering() && this.focusedGroupId == -1;
@Override
protected int getGridRenderingCacheMode() {
return this.focusedGroupId == -1 ? NEIClientConfig.getGridRenderingCacheMode() : 0;
}

@Override
Expand Down
199 changes: 134 additions & 65 deletions src/main/java/codechicken/nei/ItemsGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.item.ItemStack;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL30;

import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.ItemPanel.ItemPanelSlot;
Expand All @@ -26,6 +27,117 @@

public class ItemsGrid {

private static class ScreenCapture {

private long nextCacheRefresh = 0;
private Framebuffer framebuffer;

public ScreenCapture() {
this.framebuffer = new Framebuffer(1, 1, true);
this.framebuffer.setFramebufferColor(0.0F, 0.0F, 0.0F, 0.0F);
Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(false);
}

protected int getGridRenderingCacheFPS(int mode) {
return mode == 1 ? NEIClientConfig.getIntSetting("inventory.gridRenderingCacheFPS") : 0;
}

public boolean needRefresh(int mode) {
return this.nextCacheRefresh == 0
|| getGridRenderingCacheFPS(mode) > 0 && this.nextCacheRefresh < System.currentTimeMillis();
}

public void refreshBuffer() {
this.nextCacheRefresh = 0;
}

public void captureScreen(Runnable callback, int mode) {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);

resetFramebuffer();
this.framebuffer.bindFramebuffer(false);

/* Set up some rendering state needed for items to work correctly */
GL11.glDisable(GL11.GL_BLEND);
GL11.glDepthMask(true);
OpenGlHelper.glBlendFunc(
GL11.GL_SRC_ALPHA,
GL11.GL_ONE_MINUS_SRC_ALPHA,
GL11.GL_SRC_ALPHA,
GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

callback.run();

GL11.glPopAttrib();

Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(false);
this.nextCacheRefresh = System.currentTimeMillis() + (1000 / Math.max(1, getGridRenderingCacheFPS(mode)));
}

private void resetFramebuffer() {
final Minecraft minecraft = Minecraft.getMinecraft();

if (this.framebuffer.framebufferWidth != minecraft.displayWidth
|| framebuffer.framebufferHeight != minecraft.displayHeight) {
this.framebuffer.createBindFramebuffer(minecraft.displayWidth, minecraft.displayHeight);
this.framebuffer.setFramebufferFilter(GL11.GL_NEAREST);
} else {
this.framebuffer.framebufferClear();
}

// copy depth buffer from MC (fix Angelica)
OpenGlHelper.func_153171_g(GL30.GL_READ_FRAMEBUFFER, minecraft.getFramebuffer().framebufferObject);
OpenGlHelper.func_153171_g(GL30.GL_DRAW_FRAMEBUFFER, this.framebuffer.framebufferObject);
GL30.glBlitFramebuffer(
0,
0,
minecraft.displayWidth,
minecraft.displayHeight,
0,
0,
minecraft.displayWidth,
minecraft.displayHeight,
GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT,
GL11.GL_NEAREST);
}

public void renderCapturedScreen() {
this.framebuffer.bindFramebufferTexture();

final Tessellator tessellator = Tessellator.instance;
final Minecraft minecraft = Minecraft.getMinecraft();
final ScaledResolution scaledresolution = new ScaledResolution(
minecraft,
minecraft.displayWidth,
minecraft.displayHeight);

/* Set up some rendering state needed for items to work correctly */
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper
.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glEnable(GL11.GL_TEXTURE_2D);

tessellator.startDrawingQuads();

tessellator.addVertexWithUV(0, scaledresolution.getScaledHeight_double(), 0.0, 0, 0);
tessellator.addVertexWithUV(
scaledresolution.getScaledWidth_double(),
scaledresolution.getScaledHeight_double(),
0.0,
1,
0);
tessellator.addVertexWithUV(scaledresolution.getScaledWidth_double(), 0, 0.0, 1, 1);
tessellator.addVertexWithUV(0, 0, 0, 0, 1);

tessellator.draw();

this.framebuffer.unbindFramebufferTexture();
}

}

public static final int SLOT_SIZE = 18;

protected int width;
Expand All @@ -49,10 +161,7 @@ public class ItemsGrid {

protected Label messageLabel = new Label(getMessageOnEmpty(), true);

@Nullable
private Framebuffer framebuffer = null;

protected boolean refreshBuffer = true;
protected ScreenCapture screenCapture = null;

public ArrayList<ItemStack> getItems() {
return realItems;
Expand Down Expand Up @@ -156,7 +265,9 @@ public void shiftPage(int shift) {
}

protected void onGridChanged() {
refreshBuffer = true;
if (this.screenCapture != null) {
this.screenCapture.refreshBuffer();
}
this.gridMask = null;
}

Expand Down Expand Up @@ -284,34 +395,6 @@ protected void beforeDrawSlot(@Nullable ItemPanelSlot focused, int slotIdx, Rect

protected void afterDrawSlot(@Nullable ItemPanelSlot focused, int slotIdx, Rectangle4i rect) {}

private void blitExistingBuffer() {
Minecraft minecraft = Minecraft.getMinecraft();
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
getFrameBuffer().bindFramebufferTexture();
GL11.glEnable(GL11.GL_TEXTURE_2D);
ScaledResolution res = new ScaledResolution(minecraft, minecraft.displayWidth, minecraft.displayHeight);
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(0, res.getScaledHeight_double(), 0.0, 0, 0);
tessellator.addVertexWithUV(res.getScaledWidth_double(), res.getScaledHeight_double(), 0.0, 1, 0);
tessellator.addVertexWithUV(res.getScaledWidth_double(), 0, 0.0, 1, 1);
tessellator.addVertexWithUV(0, 0, 0, 0, 1);
tessellator.draw();
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
}

private Framebuffer getFrameBuffer() {

if (framebuffer == null) {
framebuffer = new Framebuffer(1, 1, true);
framebuffer.setFramebufferColor(0.0F, 0.0F, 0.0F, 0.0F);
}

return framebuffer;
}

private void drawItems() {
GuiContainerManager.enableMatrixStackLogging();

Expand All @@ -325,8 +408,8 @@ private void drawItems() {
GuiContainerManager.disableMatrixStackLogging();
}

protected boolean shouldCacheItemRendering() {
return NEIClientConfig.shouldCacheItemRendering();
protected int getGridRenderingCacheMode() {
return NEIClientConfig.getGridRenderingCacheMode();
}

public void draw(int mousex, int mousey) {
Expand All @@ -335,36 +418,22 @@ public void draw(int mousex, int mousey) {
}

final ItemPanelSlot focused = getSlotMouseOver(mousex, mousey);
final int gridRenderingCacheMode = getGridRenderingCacheMode();

beforeDrawItems(mousex, mousey, focused);

if (gridRenderingCacheMode > 0) {

if (this.screenCapture == null) {
this.screenCapture = new ScreenCapture();
}

if (shouldCacheItemRendering()) {

if (refreshBuffer) {
Minecraft minecraft = Minecraft.getMinecraft();
Framebuffer framebuffer = getFrameBuffer();
framebuffer.createBindFramebuffer(minecraft.displayWidth, minecraft.displayHeight);
framebuffer.framebufferClear();
framebuffer.bindFramebuffer(false);

/* Set up some rendering state needed for items to work correctly */
GL11.glDisable(GL11.GL_BLEND);
GL11.glDepthMask(true);
OpenGlHelper.glBlendFunc(
GL11.GL_SRC_ALPHA,
GL11.GL_ONE_MINUS_SRC_ALPHA,
GL11.GL_SRC_ALPHA,
GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

drawItems();

Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(false);
refreshBuffer = false;
if (this.screenCapture.needRefresh(gridRenderingCacheMode)) {
this.screenCapture.captureScreen(this::drawItems, gridRenderingCacheMode);
}

beforeDrawItems(mousex, mousey, focused);
blitExistingBuffer();
this.screenCapture.renderCapturedScreen();
} else {
beforeDrawItems(mousex, mousey, focused);
drawItems();
}

Expand All @@ -386,8 +455,8 @@ public ItemPanelSlot getSlotMouseOver(int mousex, int mousey) {
return null;
}

final int overRow = (int) ((mousey - marginTop) / SLOT_SIZE);
final int overColumn = (int) ((mousex - marginLeft - paddingLeft) / SLOT_SIZE);
final int overRow = (mousey - marginTop) / SLOT_SIZE;
final int overColumn = (mousex - marginLeft - paddingLeft) / SLOT_SIZE;
final int slt = columns * overRow + overColumn;

if (overRow >= rows || overColumn >= columns) {
Expand Down Expand Up @@ -416,8 +485,8 @@ public boolean contains(int px, int py) {
return false;
}

final int r = (int) ((py - marginTop) / SLOT_SIZE);
final int c = (int) ((px - marginLeft - paddingLeft) / SLOT_SIZE);
final int r = (py - marginTop) / SLOT_SIZE;
final int c = (px - marginLeft - paddingLeft) / SLOT_SIZE;

return !isInvalidSlot(columns * r + c);
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ public boolean isEnabled() {
}
});

tag.getTag("inventory.cacheItemRendering").getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.cacheItemRendering", true));
tag.getTag("inventory.gridRenderingCacheFPS").getIntValue(8);

tag.getTag("inventory.gridRenderingCacheMode").getIntValue(0);
API.addOption(new OptionCycled("inventory.gridRenderingCacheMode", 3, true));

tag.getTag("itemLoadingTimeout").getIntValue(500);

Expand Down Expand Up @@ -1043,8 +1045,8 @@ public static boolean showHistoryPanelWidget() {
return getBooleanSetting("inventory.history.enabled");
}

public static boolean shouldCacheItemRendering() {
return getBooleanSetting("inventory.cacheItemRendering") && OpenGlHelper.framebufferSupported;
public static int getGridRenderingCacheMode() {
return OpenGlHelper.framebufferSupported ? getIntSetting("inventory.gridRenderingCacheMode") : 0;
}

public static boolean enableCollapsibleItems() {
Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/assets/nei/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,10 @@ nei.options.inventory.disableMouseScrollTransfer.false=False
nei.options.inventory.invertMouseScrollTransfer=Invert Mouse Wheel Transfer Direction
nei.options.inventory.invertMouseScrollTransfer.true=True
nei.options.inventory.invertMouseScrollTransfer.false=False
nei.options.inventory.cacheItemRendering=Cache Item Rendering
nei.options.inventory.cacheItemRendering.true=True
nei.options.inventory.cacheItemRendering.false=False
nei.options.inventory.gridRenderingCacheMode=Grid Rendering Cache Mode
nei.options.inventory.gridRenderingCacheMode.0=Off
nei.options.inventory.gridRenderingCacheMode.1=Low FPS
nei.options.inventory.gridRenderingCacheMode.2=Freeze
nei.options.inventory.collapsibleItems=Collapsible Items
nei.options.inventory.collapsibleItems.enabled=Collapsible Items
nei.options.inventory.collapsibleItems.enabled.true=Enabled
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/assets/nei/lang/ru_RU.lang
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,6 @@ nei.options.inventory.disableMouseScrollTransfer.false=Нет
nei.options.inventory.invertMouseScrollTransfer=Инвертировать направление переноса колесиком мыши
nei.options.inventory.invertMouseScrollTransfer.true=Да
nei.options.inventory.invertMouseScrollTransfer.false=Нет
nei.options.inventory.cacheItemRendering=Кэширование рендеринга предметов
nei.options.inventory.cacheItemRendering.true=Да
nei.options.inventory.cacheItemRendering.false=Нет
nei.options.inventory.collapsibleItems=Группирование предметов
nei.options.inventory.collapsibleItems.enabled=Группирование предметов
nei.options.inventory.collapsibleItems.enabled.true=Включено
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/assets/nei/lang/zh_CN.lang
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ nei.options.inventory.disableMouseScrollTransfer.false=关闭
nei.options.inventory.invertMouseScrollTransfer=反转鼠标滚轮方向
nei.options.inventory.invertMouseScrollTransfer.true=开启
nei.options.inventory.invertMouseScrollTransfer.false=关闭
nei.options.inventory.cacheItemRendering=缓存物品渲染
nei.options.inventory.cacheItemRendering.true=开启
nei.options.inventory.cacheItemRendering.false=关闭

nei.options.command=指令
nei.options.command.creative=游戏模式
Expand Down

0 comments on commit 12c4995

Please sign in to comment.