From d242f238abd341a3be5da29fb588e083d82122fa Mon Sep 17 00:00:00 2001 From: slprime Date: Thu, 19 Dec 2024 23:20:33 +0200 Subject: [PATCH] Created Grid Rendering Cache Mode --- .../java/codechicken/nei/BookmarkPanel.java | 7 +- src/main/java/codechicken/nei/ItemsGrid.java | 199 ++++++++++++------ .../java/codechicken/nei/NEIClientConfig.java | 10 +- src/main/resources/assets/nei/lang/en_US.lang | 7 +- src/main/resources/assets/nei/lang/ru_RU.lang | 3 - src/main/resources/assets/nei/lang/zh_CN.lang | 3 - 6 files changed, 148 insertions(+), 81 deletions(-) diff --git a/src/main/java/codechicken/nei/BookmarkPanel.java b/src/main/java/codechicken/nei/BookmarkPanel.java index a951b2e8f..4dafa9882 100644 --- a/src/main/java/codechicken/nei/BookmarkPanel.java +++ b/src/main/java/codechicken/nei/BookmarkPanel.java @@ -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); } @@ -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 diff --git a/src/main/java/codechicken/nei/ItemsGrid.java b/src/main/java/codechicken/nei/ItemsGrid.java index e518ef39d..6280a4554 100644 --- a/src/main/java/codechicken/nei/ItemsGrid.java +++ b/src/main/java/codechicken/nei/ItemsGrid.java @@ -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; @@ -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; @@ -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 getItems() { return realItems; @@ -152,7 +261,9 @@ public void shiftPage(int shift) { } protected void onGridChanged() { - refreshBuffer = true; + if (this.screenCapture != null) { + this.screenCapture.refreshBuffer(); + } this.gridMask = null; } @@ -280,34 +391,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(); @@ -321,8 +404,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) { @@ -331,36 +414,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(); } @@ -382,8 +451,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) { @@ -412,8 +481,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); } diff --git a/src/main/java/codechicken/nei/NEIClientConfig.java b/src/main/java/codechicken/nei/NEIClientConfig.java index df7808d9f..26eeb390b 100644 --- a/src/main/java/codechicken/nei/NEIClientConfig.java +++ b/src/main/java/codechicken/nei/NEIClientConfig.java @@ -345,8 +345,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); @@ -1000,8 +1002,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() { diff --git a/src/main/resources/assets/nei/lang/en_US.lang b/src/main/resources/assets/nei/lang/en_US.lang index ab968623f..284503f05 100644 --- a/src/main/resources/assets/nei/lang/en_US.lang +++ b/src/main/resources/assets/nei/lang/en_US.lang @@ -297,9 +297,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 diff --git a/src/main/resources/assets/nei/lang/ru_RU.lang b/src/main/resources/assets/nei/lang/ru_RU.lang index e36108ad2..ec7d64a1f 100644 --- a/src/main/resources/assets/nei/lang/ru_RU.lang +++ b/src/main/resources/assets/nei/lang/ru_RU.lang @@ -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=Включено diff --git a/src/main/resources/assets/nei/lang/zh_CN.lang b/src/main/resources/assets/nei/lang/zh_CN.lang index d8c84368e..e814ef0e5 100644 --- a/src/main/resources/assets/nei/lang/zh_CN.lang +++ b/src/main/resources/assets/nei/lang/zh_CN.lang @@ -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=游戏模式