Skip to content

Commit e34c5c1

Browse files
committed
Use GuiGraphics blit rendering methods instead of directly creating vertex data
1 parent 65c42ad commit e34c5c1

File tree

9 files changed

+92
-136
lines changed

9 files changed

+92
-136
lines changed

Common/src/main/java/mezz/jei/common/gui/elements/DrawableResource.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package mezz.jei.common.gui.elements;
22

3-
import com.mojang.blaze3d.vertex.VertexConsumer;
43
import mezz.jei.api.gui.drawable.IDrawableStatic;
5-
import net.minecraft.client.Minecraft;
64
import net.minecraft.client.gui.GuiGraphics;
75
import net.minecraft.client.renderer.RenderType;
86
import net.minecraft.resources.ResourceLocation;
9-
import org.joml.Matrix4f;
107

118
public class DrawableResource implements IDrawableStatic {
129

@@ -56,30 +53,20 @@ public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset) {
5653

5754
@Override
5855
public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset, int maskTop, int maskBottom, int maskLeft, int maskRight) {
59-
RenderType rendertype = RenderType.guiTextured(this.resourceLocation);
60-
VertexConsumer bufferBuilder = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(rendertype);
56+
int uWidth = this.width - (maskRight + maskLeft);
57+
int vHeight = this.height - (maskBottom + maskTop);
6158

62-
int x = xOffset + this.paddingLeft + maskLeft;
63-
int y = yOffset + this.paddingTop + maskTop;
64-
int u = this.u + maskLeft;
65-
int v = this.v + maskTop;
66-
int width = this.width - maskRight - maskLeft;
67-
int height = this.height - maskBottom - maskTop;
68-
float f = 1.0F / this.textureWidth;
69-
float f1 = 1.0F / this.textureHeight;
70-
71-
Matrix4f matrix = guiGraphics.pose().last().pose();
72-
bufferBuilder.addVertex(matrix, x, y + height, 0)
73-
.setColor(255, 255, 255, 255)
74-
.setUv(u * f, (v + (float) height) * f1);
75-
bufferBuilder.addVertex(matrix, x + width, y + height, 0)
76-
.setColor(255, 255, 255, 255)
77-
.setUv((u + (float) width) * f, (v + (float) height) * f1);
78-
bufferBuilder.addVertex(matrix, x + width, y, 0)
79-
.setColor(255, 255, 255, 255)
80-
.setUv((u + (float) width) * f, v * f1);
81-
bufferBuilder.addVertex(matrix, x, y, 0)
82-
.setColor(255, 255, 255, 255)
83-
.setUv(u * f, v * f1);
59+
guiGraphics.blit(
60+
RenderType::guiTextured,
61+
this.resourceLocation,
62+
xOffset + maskLeft,
63+
yOffset + maskTop,
64+
u + maskLeft,
65+
v + maskTop,
66+
uWidth,
67+
vHeight,
68+
this.textureWidth,
69+
this.textureHeight
70+
);
8471
}
8572
}

Common/src/main/java/mezz/jei/common/gui/elements/DrawableSprite.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package mezz.jei.common.gui.elements;
22

3-
import com.mojang.blaze3d.vertex.VertexConsumer;
43
import mezz.jei.api.gui.drawable.IDrawableStatic;
5-
import mezz.jei.common.Constants;
64
import mezz.jei.common.gui.textures.JeiGuiSpriteManager;
7-
import net.minecraft.client.Minecraft;
5+
import mezz.jei.common.platform.IPlatformRenderHelper;
6+
import mezz.jei.common.platform.Services;
87
import net.minecraft.client.gui.GuiGraphics;
98
import net.minecraft.client.renderer.RenderType;
109
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
1110
import net.minecraft.resources.ResourceLocation;
12-
import org.joml.Matrix4f;
1311

1412
public class DrawableSprite implements IDrawableStatic {
1513
private final JeiGuiSpriteManager spriteManager;
@@ -54,41 +52,28 @@ public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset) {
5452
@Override
5553
public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset, int maskTop, int maskBottom, int maskLeft, int maskRight) {
5654
TextureAtlasSprite sprite = spriteManager.getSprite(location);
57-
int textureWidth = this.width;
58-
int textureHeight = this.height;
59-
60-
RenderType rendertype = RenderType.guiTextured(Constants.LOCATION_JEI_GUI_TEXTURE_ATLAS);
61-
VertexConsumer bufferBuilder = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(rendertype);
6255

6356
maskTop += trimTop;
6457
maskBottom += trimBottom;
6558
maskLeft += trimLeft;
6659
maskRight += trimRight;
6760

68-
int x = xOffset + maskLeft;
69-
int y = yOffset + maskTop;
70-
int width = textureWidth - maskRight - maskLeft;
71-
int height = textureHeight - maskBottom - maskTop;
72-
float uSize = sprite.getU1() - sprite.getU0();
73-
float vSize = sprite.getV1() - sprite.getV0();
74-
75-
float minU = sprite.getU0() + uSize * (maskLeft / (float) textureWidth);
76-
float minV = sprite.getV0() + vSize * (maskTop / (float) textureHeight);
77-
float maxU = sprite.getU1() - uSize * (maskRight / (float) textureWidth);
78-
float maxV = sprite.getV1() - vSize * (maskBottom / (float) textureHeight);
61+
int uWidth = this.width - (maskRight + maskLeft);
62+
int vHeight = this.height - (maskBottom + maskTop);
7963

80-
Matrix4f matrix = guiGraphics.pose().last().pose();
81-
bufferBuilder.addVertex(matrix, x, y + height, 0)
82-
.setColor(255, 255, 255, 255)
83-
.setUv(minU, maxV);
84-
bufferBuilder.addVertex(matrix, x + width, y + height, 0)
85-
.setColor(255, 255, 255, 255)
86-
.setUv(maxU, maxV);
87-
bufferBuilder.addVertex(matrix, x + width, y, 0)
88-
.setColor(255, 255, 255, 255)
89-
.setUv(maxU, minV);
90-
bufferBuilder.addVertex(matrix, x, y, 0)
91-
.setColor(255, 255, 255, 255)
92-
.setUv(minU, minV);
64+
IPlatformRenderHelper renderHelper = Services.PLATFORM.getRenderHelper();
65+
renderHelper.blitSprite(
66+
guiGraphics,
67+
RenderType::guiTextured,
68+
sprite,
69+
this.width,
70+
this.height,
71+
maskLeft,
72+
maskTop,
73+
xOffset + maskLeft,
74+
yOffset + maskTop,
75+
uWidth,
76+
vHeight
77+
);
9378
}
9479
}

Common/src/main/java/mezz/jei/common/gui/elements/ScalableDrawable.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset, int width, i
4040
xOffset,
4141
yOffset,
4242
width,
43-
height
43+
height,
44+
-1
4445
);
4546
}
4647
case GuiSpriteScaling.NineSlice nineSliceScaling -> {
@@ -56,14 +57,16 @@ public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset, int width, i
5657
height
5758
);
5859
}
59-
default -> guiGraphics.blitSprite(
60-
RenderType::guiTextured,
61-
location,
62-
xOffset,
63-
yOffset,
64-
width,
65-
height
66-
);
60+
default -> {
61+
guiGraphics.blitSprite(
62+
RenderType::guiTextured,
63+
sprite,
64+
xOffset,
65+
yOffset,
66+
width,
67+
height
68+
);
69+
}
6770
}
6871
}
6972
}

Common/src/main/java/mezz/jei/common/platform/IPlatformRenderHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public interface IPlatformRenderHelper {
3939
@Nullable
4040
TextureAtlasSprite getTextureAtlasSprite(BlockState blockState);
4141

42+
void blitSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, int textureWidth, int textureHeight, int uPosition, int vPosition, int x, int y, int uWidth, int vHeight);
43+
4244
void blitNineSlicedSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.NineSlice scaling, int xOffset, int yOffset, int width, int height);
4345

44-
void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.Tile scaling, int xOffset, int yOffset, int width, int height);
46+
void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.Tile scaling, int xOffset, int yOffset, int width, int height, int color);
4547
}

Fabric/src/main/java/mezz/jei/fabric/platform/RenderHelper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ public TextureAtlasSprite getTextureAtlasSprite(BlockState blockState) {
5757
return textureAtlasSprite;
5858
}
5959

60+
@Override
61+
public void blitSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, int textureWidth, int textureHeight, int uPosition, int vPosition, int x, int y, int uWidth, int vHeight) {
62+
guiGraphics.blitSprite(renderType, sprite, textureWidth, textureHeight, uPosition, vPosition, x, y, uWidth, vHeight, -1);
63+
}
64+
6065
@Override
6166
public void blitNineSlicedSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.NineSlice scaling, int xOffset, int yOffset, int width, int height) {
6267
guiGraphics.blitNineSlicedSprite(renderType, sprite, scaling, xOffset, yOffset, width, height, -1);
6368
}
6469

6570
@Override
66-
public void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.Tile scaling, int xOffset, int yOffset, int width, int height) {
71+
public void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.Tile scaling, int xOffset, int yOffset, int width, int height, int color) {
6772
guiGraphics.blitTiledSprite(
6873
renderType,
6974
sprite,
@@ -77,7 +82,7 @@ public void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation,
7782
scaling.height(),
7883
scaling.width(),
7984
scaling.height(),
80-
-1
85+
color
8186
);
8287
}
8388

Fabric/src/main/resources/jei.accesswidener

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ accessible method net/minecraft/world/item/alchemy/PotionBrewing$Mix ingredient
3131

3232
accessible method net/minecraft/client/gui/GuiGraphics renderTooltipInternal (Lnet/minecraft/client/gui/Font;Ljava/util/List;IILnet/minecraft/client/gui/screens/inventory/tooltip/ClientTooltipPositioner;Lnet/minecraft/resources/ResourceLocation;)V
3333
accessible field net/minecraft/client/gui/GuiGraphics bufferSource Lnet/minecraft/client/renderer/MultiBufferSource$BufferSource;
34+
accessible method net/minecraft/client/gui/GuiGraphics blitSprite (Ljava/util/function/Function;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;IIIIIIIII)V
3435
accessible method net/minecraft/client/gui/GuiGraphics blitNineSlicedSprite (Ljava/util/function/Function;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;Lnet/minecraft/client/resources/metadata/gui/GuiSpriteScaling$NineSlice;IIIII)V
3536
accessible method net/minecraft/client/gui/GuiGraphics blitTiledSprite (Ljava/util/function/Function;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;IIIIIIIIIII)V
3637

Library/src/main/java/mezz/jei/library/render/FluidTankRenderer.java

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
package mezz.jei.library.render;
22

33
import com.google.common.base.Preconditions;
4-
import com.mojang.blaze3d.vertex.VertexConsumer;
54
import mezz.jei.api.ingredients.IIngredientRenderer;
65
import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes;
76
import mezz.jei.common.platform.IPlatformFluidHelperInternal;
7+
import mezz.jei.common.platform.IPlatformRenderHelper;
8+
import mezz.jei.common.platform.Services;
89
import net.minecraft.ChatFormatting;
9-
import net.minecraft.client.Minecraft;
1010
import net.minecraft.client.gui.GuiGraphics;
1111
import net.minecraft.client.renderer.RenderType;
12-
import net.minecraft.client.renderer.texture.TextureAtlas;
12+
import net.minecraft.client.renderer.texture.SpriteContents;
1313
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
14+
import net.minecraft.client.resources.metadata.gui.GuiSpriteScaling;
1415
import net.minecraft.network.chat.Component;
1516
import net.minecraft.network.chat.MutableComponent;
1617
import net.minecraft.world.item.TooltipFlag;
1718
import net.minecraft.world.level.material.Fluid;
1819
import net.minecraft.world.level.material.Fluids;
19-
import org.joml.Matrix4f;
2020

2121
import java.text.NumberFormat;
2222
import java.util.ArrayList;
2323
import java.util.List;
2424

2525
public class FluidTankRenderer<T> implements IIngredientRenderer<T> {
2626
private static final NumberFormat nf = NumberFormat.getIntegerInstance();
27-
private static final int TEXTURE_SIZE = 16;
2827
private static final int MIN_FLUID_HEIGHT = 1; // ensure tiny amounts of fluid are still visible
2928

3029
private final IPlatformFluidHelperInternal<T> fluidHelper;
@@ -76,68 +75,36 @@ public void render(GuiGraphics guiGraphics, T ingredient, int posX, int posY) {
7675
int fluidColor = fluidHelper.getColorTint(ingredient);
7776

7877
long amount = fluidHelper.getAmount(ingredient);
79-
long scaledAmount = (amount * height) / capacity;
80-
if (amount > 0 && scaledAmount < MIN_FLUID_HEIGHT) {
81-
scaledAmount = MIN_FLUID_HEIGHT;
78+
if (amount > 0) {
79+
long longScaledAmount = (amount * height) / capacity;
80+
int scaledAmount = Math.clamp(longScaledAmount, MIN_FLUID_HEIGHT, height);
81+
drawTiledSprite(guiGraphics, width, height, fluidColor, scaledAmount, fluidStillSprite, posX, posY);
8282
}
83-
if (scaledAmount > height) {
84-
scaledAmount = height;
85-
}
86-
87-
drawTiledSprite(guiGraphics, width, height, fluidColor, scaledAmount, fluidStillSprite, posX, posY);
8883
});
8984
}
9085

91-
private static void drawTiledSprite(GuiGraphics guiGraphics, final int tiledWidth, final int tiledHeight, int color, long scaledAmount, TextureAtlasSprite sprite, int posX, int posY) {
92-
@SuppressWarnings("deprecation")
93-
RenderType rendertype = RenderType.guiTextured(TextureAtlas.LOCATION_BLOCKS);
94-
VertexConsumer bufferBuilder = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(rendertype);
95-
96-
Matrix4f matrix = guiGraphics.pose().last().pose();
97-
98-
final int xTileCount = tiledWidth / TEXTURE_SIZE;
99-
final int xRemainder = tiledWidth - (xTileCount * TEXTURE_SIZE);
100-
final long yTileCount = scaledAmount / TEXTURE_SIZE;
101-
final long yRemainder = scaledAmount - (yTileCount * TEXTURE_SIZE);
102-
103-
final int yStart = tiledHeight + posY;
104-
105-
for (int xTile = 0; xTile <= xTileCount; xTile++) {
106-
for (int yTile = 0; yTile <= yTileCount; yTile++) {
107-
int width = (xTile == xTileCount) ? xRemainder : TEXTURE_SIZE;
108-
long height = (yTile == yTileCount) ? yRemainder : TEXTURE_SIZE;
109-
int x = posX + (xTile * TEXTURE_SIZE);
110-
int y = yStart - ((yTile + 1) * TEXTURE_SIZE);
111-
if (width > 0 && height > 0) {
112-
long maskTop = TEXTURE_SIZE - height;
113-
int maskRight = TEXTURE_SIZE - width;
114-
115-
drawTextureWithMasking(bufferBuilder, matrix, x, y, sprite, color, maskTop, maskRight, 100);
116-
}
117-
}
86+
private static void drawTiledSprite(GuiGraphics guiGraphics, final int tiledWidth, final int tiledHeight, int color, int scaledAmount, TextureAtlasSprite sprite, int posX, int posY) {
87+
IPlatformRenderHelper renderHelper = Services.PLATFORM.getRenderHelper();
88+
SpriteContents spriteContents = sprite.contents();
89+
GuiSpriteScaling.Tile tileScaling = new GuiSpriteScaling.Tile(spriteContents.width(), spriteContents.height());
90+
91+
posY = posY + tiledHeight - scaledAmount;
92+
93+
guiGraphics.enableScissor(posX, posY, posX + tiledWidth, posY + scaledAmount);
94+
{
95+
renderHelper.blitTiledSprite(
96+
guiGraphics,
97+
RenderType::guiTextured,
98+
sprite,
99+
tileScaling,
100+
posX,
101+
posY,
102+
tiledWidth,
103+
scaledAmount,
104+
color
105+
);
118106
}
119-
}
120-
121-
private static void drawTextureWithMasking(VertexConsumer bufferBuilder, Matrix4f matrix, float xCoord, float yCoord, TextureAtlasSprite textureSprite, int color, long maskTop, long maskRight, float zLevel) {
122-
float uMin = textureSprite.getU0();
123-
float uMax = textureSprite.getU1();
124-
float vMin = textureSprite.getV0();
125-
float vMax = textureSprite.getV1();
126-
uMax = uMax - (maskRight / 16F * (uMax - uMin));
127-
vMax = vMax - (maskTop / 16F * (vMax - vMin));
128-
129-
bufferBuilder.addVertex(matrix, xCoord, yCoord + 16, zLevel)
130-
.setColor(color)
131-
.setUv(uMin, vMax);
132-
bufferBuilder.addVertex(matrix, xCoord + 16 - maskRight, yCoord + 16, zLevel)
133-
.setColor(color)
134-
.setUv(uMax, vMax);
135-
bufferBuilder.addVertex(matrix, xCoord + 16 - maskRight, yCoord + maskTop, zLevel)
136-
.setColor(color)
137-
.setUv(uMax, vMin);
138-
bufferBuilder.addVertex(matrix, xCoord, yCoord + maskTop, zLevel)
139-
.setColor(color)
140-
.setUv(uMin, vMin);
107+
guiGraphics.disableScissor();
141108
}
142109

143110
@Override

NeoForge/src/main/java/mezz/jei/neoforge/platform/RenderHelper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,18 @@ public TextureAtlasSprite getTextureAtlasSprite(BlockState blockState) {
9191
return textureAtlasSprite;
9292
}
9393

94+
@Override
95+
public void blitSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, int textureWidth, int textureHeight, int uPosition, int vPosition, int x, int y, int uWidth, int vHeight) {
96+
guiGraphics.blitSprite(renderType, sprite, textureWidth, textureHeight, uPosition, vPosition, x, y, uWidth, vHeight, -1);
97+
}
98+
9499
@Override
95100
public void blitNineSlicedSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.NineSlice scaling, int xOffset, int yOffset, int width, int height) {
96101
guiGraphics.blitNineSlicedSprite(renderType, sprite, scaling, xOffset, yOffset, width, height, -1);
97102
}
98103

99104
@Override
100-
public void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.Tile scaling, int xOffset, int yOffset, int width, int height) {
105+
public void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation, RenderType> renderType, TextureAtlasSprite sprite, GuiSpriteScaling.Tile scaling, int xOffset, int yOffset, int width, int height, int color) {
101106
guiGraphics.blitTiledSprite(
102107
renderType,
103108
sprite,
@@ -111,7 +116,7 @@ public void blitTiledSprite(GuiGraphics guiGraphics, Function<ResourceLocation,
111116
scaling.height(),
112117
scaling.width(),
113118
scaling.height(),
114-
-1
119+
color
115120
);
116121
}
117122
}

NeoForge/src/main/resources/META-INF/accesstransformer.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ public net.minecraft.client.gui.components.toasts.ToastManager visibleToasts
4141

4242
# Rendering
4343
public net.minecraft.client.gui.GuiGraphics bufferSource
44+
public net.minecraft.client.gui.GuiGraphics blitSprite(Ljava/util/function/Function;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;IIIIIIIII)V
4445
public net.minecraft.client.gui.GuiGraphics blitNineSlicedSprite(Ljava/util/function/Function;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;Lnet/minecraft/client/resources/metadata/gui/GuiSpriteScaling$NineSlice;IIIII)V
4546
public net.minecraft.client.gui.GuiGraphics blitTiledSprite(Ljava/util/function/Function;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;IIIIIIIIIII)V

0 commit comments

Comments
 (0)