Skip to content

Commit

Permalink
[#515] Implemented batching for our render objects to improve the cli…
Browse files Browse the repository at this point in the history
…ent rendering performance
  • Loading branch information
SirEndii committed Aug 5, 2024
1 parent 4b61372 commit 2cc13a8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.logging.level=info
# Minecraft related
mod_id=advancedperipherals
minecraft_version=1.19.2
forge_version=43.3.13
forge_version=43.4.0
loader_version=43
mod_version=0.8r
release_type=release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@
import net.minecraftforge.client.gui.overlay.ForgeGui;
import net.minecraftforge.client.gui.overlay.IGuiOverlay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OverlayModuleOverlay implements IGuiOverlay {
public static final String ID = "overlay_module_overlay";

@Override
public void render(ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
poseStack.pushPose();

Map<Class<? extends RenderableObject>, List<RenderableObject>> batches = new HashMap<>();

for (RenderableObject object : OverlayObjectHolder.getObjects()) {
if (!object.isEnabled())
continue;
object.getRenderObject().render(object, gui, poseStack, partialTick, screenWidth, screenHeight);

Class<? extends RenderableObject> objectClass = object.getClass();

if (batches.containsKey(objectClass)) {
batches.get(objectClass).add(object);
continue;
}

List<RenderableObject> newBatchArray = new ArrayList<>();
newBatchArray.add(object);
batches.put(objectClass, newBatchArray);
}

for (List<RenderableObject> batch : batches.values()) {
batch.get(0).getRenderObject().renderBatch(batch, gui, poseStack, partialTick, screenWidth, screenHeight);
}

poseStack.popPose();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;

import java.util.List;

public class CircleRenderer implements IObjectRenderer {

@Override
public void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
Circle circle = (Circle) object;
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
for (RenderableObject object : objects) {

Circle circle = (Circle) object;

float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;
float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;

drawCircle(poseStack, circle.x, circle.y, circle.radius, 120, red, green, blue, alpha);
drawCircle(poseStack, circle.x, circle.y, circle.radius, 120, red, green, blue, alpha);
}
}

public void drawCircle(PoseStack poseStack, float cx, float cy, float r, int numSegments, float red, float green, float blue, float alpha) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraftforge.client.gui.overlay.ForgeGui;

import java.util.List;

public interface IObjectRenderer {

void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight);
void renderBatch(List<RenderableObject> object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;

import java.util.List;

public class PanelRenderer implements IObjectRenderer {

@Override
public void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;

public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
RenderSystem.setShader(GameRenderer::getPositionColorShader);
BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();
Matrix4f matrix = poseStack.last().pose();
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
bufferbuilder.vertex(matrix, (float) object.x, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.y, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.x, (float) object.y, 0f).color(red, green, blue, alpha).endVertex();

for (RenderableObject object : objects) {
float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;

bufferbuilder.vertex(matrix, (float) object.x, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.y, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.x, (float) object.y, 0f).color(red, green, blue, alpha).endVertex();
}

BufferUploader.drawWithShader(bufferbuilder.end());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraftforge.client.gui.overlay.ForgeGui;

import java.util.List;

public class TextRenderer implements IObjectRenderer {

@Override
public void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
float alpha = object.opacity;
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
/*float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;

*/

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.srendi.advancedperipherals.common.network.toclient;

import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder;
import de.srendi.advancedperipherals.common.network.base.IPacket;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
Expand Down Expand Up @@ -29,8 +28,6 @@ public void encode(FriendlyByteBuf buffer) {
}

public static RenderableObjectBulkSyncPacket decode(FriendlyByteBuf buffer) {
AdvancedPeripherals.debug("Size" + buffer.readableBytes());

int size = buffer.readInt();
RenderableObject[] objects = new RenderableObject[size];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public void update(RenderableObject object) {

public int bulkUpdate() {
int size = objectsToUpdate.size();
AdvancedPeripherals.debug("Updating " + size);
int packetCount = (int) Math.ceil((double) size / 15000);

// In some cases, if the user creates a lot of objects above 15k, the packet payload can be too big.
Expand Down

0 comments on commit 2cc13a8

Please sign in to comment.