]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/renderers/RenderBatch.java
General performance improvements
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / renderers / RenderBatch.java
1 package com.irtimaled.bbor.client.renderers;
2
3 import com.irtimaled.bbor.client.Camera;
4 import com.irtimaled.bbor.client.models.Point;
5 import com.mojang.blaze3d.systems.RenderSystem;
6 import net.minecraft.client.render.BufferBuilder;
7 import net.minecraft.client.render.BufferRenderer;
8 import net.minecraft.client.render.GameRenderer;
9 import net.minecraft.client.render.VertexFormat;
10 import net.minecraft.client.render.VertexFormats;
11 import net.minecraft.client.util.math.MatrixStack;
12 import net.minecraft.util.math.Box;
13
14 import java.awt.*;
15 import java.util.concurrent.atomic.AtomicLong;
16
17 public class RenderBatch {
18
19     private static final BufferBuilder quadBufferBuilderNonMasked = new BufferBuilder(2097152);
20     private static final BufferBuilder quadBufferBuilderMasked = new BufferBuilder(2097152);
21     private static final BufferBuilder lineBufferBuilder = new BufferBuilder(2097152);
22
23     private static final Object mutex = new Object();
24     private static final AtomicLong quadNonMaskedCount = new AtomicLong(0L);
25     private static final AtomicLong quadMaskedCount = new AtomicLong(0L);
26     private static final AtomicLong lineCount = new AtomicLong(0L);
27     private static final AtomicLong quadNonMaskedCountLast = new AtomicLong(0L);
28     private static final AtomicLong quadMaskedCountLast = new AtomicLong(0L);
29     private static final AtomicLong lineCountLast = new AtomicLong(0L);
30
31     private static final AtomicLong lastDurationNanos = new AtomicLong(0L);
32
33     static void beginBatch() {
34         quadBufferBuilderMasked.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
35         quadBufferBuilderNonMasked.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
36         lineBufferBuilder.begin(VertexFormat.DrawMode.DEBUG_LINES, VertexFormats.POSITION_COLOR);
37     }
38
39     public static void drawSolidBox(MatrixStack.Entry matrixEntry, Box box, Color color, int alpha, boolean mask) {
40         final float minX = (float) box.minX;
41         final float minY = (float) box.minY;
42         final float minZ = (float) box.minZ;
43         final float maxX = (float) box.maxX;
44         final float maxY = (float) box.maxY;
45         final float maxZ = (float) box.maxZ;
46         final int red = color.getRed();
47         final int green = color.getGreen();
48         final int blue = color.getBlue();
49
50         final BufferBuilder bufferBuilder = mask ? RenderBatch.quadBufferBuilderMasked : RenderBatch.quadBufferBuilderNonMasked;
51
52         if (minX != maxX && minZ != maxZ) {
53             if (mask) quadMaskedCount.getAndIncrement();
54             else quadNonMaskedCount.getAndIncrement();
55             bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, minZ).color(red, green, blue, alpha).next();
56             bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, minZ).color(red, green, blue, alpha).next();
57             bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, maxZ).color(red, green, blue, alpha).next();
58             bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, maxZ).color(red, green, blue, alpha).next();
59         }
60
61         if (minX != maxX && minZ != maxZ) {
62             if (mask) quadMaskedCount.getAndIncrement();
63             else quadNonMaskedCount.getAndIncrement();
64             bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, minZ).color(red, green, blue, alpha).next();
65             bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, maxZ).color(red, green, blue, alpha).next();
66             bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, maxZ).color(red, green, blue, alpha).next();
67             bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, minZ).color(red, green, blue, alpha).next();
68         }
69
70         if (minX != maxX && minY != maxY) {
71             if (mask) quadMaskedCount.getAndIncrement();
72             else quadNonMaskedCount.getAndIncrement();
73             bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, minZ).color(red, green, blue, alpha).next();
74             bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, minZ).color(red, green, blue, alpha).next();
75             bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, minZ).color(red, green, blue, alpha).next();
76             bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, minZ).color(red, green, blue, alpha).next();
77         }
78
79         if (minY != maxY && minZ != maxZ) {
80             if (mask) quadMaskedCount.getAndIncrement();
81             else quadNonMaskedCount.getAndIncrement();
82             bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, minZ).color(red, green, blue, alpha).next();
83             bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, minZ).color(red, green, blue, alpha).next();
84             bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, maxZ).color(red, green, blue, alpha).next();
85             bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, maxZ).color(red, green, blue, alpha).next();
86         }
87
88         if (minX != maxX && minY != maxY) {
89             if (mask) quadMaskedCount.getAndIncrement();
90             else quadNonMaskedCount.getAndIncrement();
91             bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, maxZ).color(red, green, blue, alpha).next();
92             bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, maxZ).color(red, green, blue, alpha).next();
93             bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, maxZ).color(red, green, blue, alpha).next();
94             bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, maxZ).color(red, green, blue, alpha).next();
95         }
96
97         if (minY != maxY && minZ != maxZ) {
98             if (mask) quadMaskedCount.getAndIncrement();
99             else quadNonMaskedCount.getAndIncrement();
100             bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, minZ).color(red, green, blue, alpha).next();
101             bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, maxZ).color(red, green, blue, alpha).next();
102             bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, maxZ).color(red, green, blue, alpha).next();
103             bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, minZ).color(red, green, blue, alpha).next();
104         }
105     }
106
107     static void drawLine(MatrixStack.Entry matrixEntry, Point startPoint, Point endPoint, Color color, int alpha) {
108         int regionX = (((int) Camera.getX()) >> 9) * 512;
109         int regionZ = (((int) Camera.getZ()) >> 9) * 512;
110
111         lineCount.getAndIncrement();
112
113         lineBufferBuilder
114                 .vertex(matrixEntry.getModel(),
115                         (float) startPoint.getX() - regionX,
116                         (float) startPoint.getY(),
117                         (float) startPoint.getZ() - regionZ)
118                 .color(color.getRed(), color.getGreen(), color.getBlue(), alpha)
119                 .next();
120         lineBufferBuilder
121                 .vertex(matrixEntry.getModel(),
122                         (float) endPoint.getX() - regionX,
123                         (float) endPoint.getY(),
124                         (float) endPoint.getZ() - regionZ)
125                 .color(color.getRed(), color.getGreen(), color.getBlue(), alpha)
126                 .next();
127     }
128
129     static void endBatch() {
130         RenderSystem.setShader(GameRenderer::getPositionColorShader);
131         long startTime = System.nanoTime();
132         quadBufferBuilderMasked.end();
133         quadBufferBuilderNonMasked.end();
134         lineBufferBuilder.end();
135
136         synchronized (mutex) {
137             quadMaskedCountLast.set(quadMaskedCount.get());
138             quadNonMaskedCountLast.set(quadNonMaskedCount.get());
139             lineCountLast.set(lineCount.get());
140             quadMaskedCount.set(0);
141             quadNonMaskedCount.set(0);
142             lineCount.set(0);
143         }
144
145         RenderSystem.depthMask(true);
146         BufferRenderer.draw(quadBufferBuilderMasked);
147         BufferRenderer.draw(lineBufferBuilder);
148
149         RenderSystem.depthMask(false);
150         BufferRenderer.draw(quadBufferBuilderNonMasked);
151         lastDurationNanos.set(System.nanoTime() - startTime);
152
153         RenderSystem.depthMask(true);
154     }
155
156     public static String debugString() {
157         return String.format("[BBOR] Statistics: Filled faces: %d,%d Lines: %d @ %.2fms", quadMaskedCountLast.get(), quadNonMaskedCountLast.get(), lineCountLast.get(), lastDurationNanos.get() / 1_000_000.0);
158     }
159
160 }