mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-01 01:23:12 +00:00

The VBO renderer needs to generate a buffer with two quads for each cell, and then transfer it to the GPU. For large monitors, generating this buffer can get quite slow. Most of the issues come from IVertexBuilder (VertexConsumer under MojMap) having a lot of overhead. By emitting a ByteBuffer directly (and doing so with Unsafe to avoid bounds checks), we can improve performance 10 fold, going from 3fps/300ms for 120 monitors to 111fps/9ms. See 41fa95bce4408239bc59e032bfa9fdc7418bcb19 and #1065 for some more context and other exploratory work. The key thing to note is we _need_ a separate version of FWFR for emitting to a ByteBuffer, as introducing polymorphism to it comes with a significant performance hit.
80 lines
2.0 KiB
Java
80 lines
2.0 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
package dan200.computercraft.client.util;
|
|
|
|
import com.mojang.blaze3d.systems.RenderSystem;
|
|
import net.minecraft.client.renderer.vertex.VertexBuffer;
|
|
import net.minecraft.client.renderer.vertex.VertexFormat;
|
|
import net.minecraft.util.math.vector.Matrix4f;
|
|
import org.lwjgl.opengl.GL11;
|
|
import org.lwjgl.opengl.GL15;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
/**
|
|
* A version of {@link VertexBuffer} which allows uploading {@link ByteBuffer}s directly.
|
|
*/
|
|
public class DirectVertexBuffer implements AutoCloseable
|
|
{
|
|
private int vertextBufferId;
|
|
private int indexCount;
|
|
private VertexFormat format;
|
|
|
|
public DirectVertexBuffer()
|
|
{
|
|
vertextBufferId = DirectBuffers.createBuffer();
|
|
}
|
|
|
|
public void upload( int vertexCount, VertexFormat format, ByteBuffer buffer )
|
|
{
|
|
RenderSystem.assertThread( RenderSystem::isOnGameThread );
|
|
|
|
DirectBuffers.setBufferData( GL15.GL_ARRAY_BUFFER, vertextBufferId, buffer, GL15.GL_STATIC_DRAW );
|
|
|
|
this.format = format;
|
|
indexCount = vertexCount;
|
|
}
|
|
|
|
public void draw( Matrix4f matrix, int indexCount )
|
|
{
|
|
bind();
|
|
format.setupBufferState( 0 );
|
|
|
|
RenderSystem.pushMatrix();
|
|
RenderSystem.loadIdentity();
|
|
RenderSystem.multMatrix( matrix );
|
|
RenderSystem.drawArrays( GL11.GL_QUADS, 0, indexCount );
|
|
RenderSystem.popMatrix();
|
|
|
|
unbind();
|
|
}
|
|
|
|
public int getIndexCount()
|
|
{
|
|
return indexCount;
|
|
}
|
|
|
|
@Override
|
|
public void close()
|
|
{
|
|
if( vertextBufferId >= 0 )
|
|
{
|
|
RenderSystem.glDeleteBuffers( vertextBufferId );
|
|
vertextBufferId = -1;
|
|
}
|
|
}
|
|
|
|
private void bind()
|
|
{
|
|
RenderSystem.glBindBuffer( GL15.GL_ARRAY_BUFFER, () -> vertextBufferId );
|
|
}
|
|
|
|
private static void unbind()
|
|
{
|
|
RenderSystem.glBindBuffer( GL15.GL_ARRAY_BUFFER, () -> 0 );
|
|
}
|
|
}
|