Create LodVertexBuffer

This commit is contained in:
James Seibel
2021-11-13 14:13:46 -06:00
parent f70c4f74ac
commit 75c0889ac5
6 changed files with 134 additions and 250 deletions
@@ -48,6 +48,7 @@ import com.seibel.lod.objects.lod.LodDimension;
import com.seibel.lod.objects.lod.LodRegion;
import com.seibel.lod.objects.lod.RegionPos;
import com.seibel.lod.objects.opengl.LodBufferBuilder;
import com.seibel.lod.objects.opengl.LodVertexBuffer;
import com.seibel.lod.proxy.GlProxy;
import com.seibel.lod.render.LodRenderer;
import com.seibel.lod.util.DataPointUtil;
@@ -60,12 +61,10 @@ import com.seibel.lod.wrappers.MinecraftWrapper;
import com.seibel.lod.wrappers.Block.BlockPosWrapper;
import com.seibel.lod.wrappers.Chunk.ChunkPosWrapper;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.renderer.vertex.VertexBuffer;
import net.minecraft.util.Direction;
/**
* This object creates the geometry that is
* This object creates the buffers that are
* rendered by the LodRenderer.
*
* @author James Seibel
@@ -111,9 +110,9 @@ public class LodBufferBuilderFactory
public int[][][] drawableStorageBufferIds;
/** Used when building new VBOs */
public volatile VertexBuffer[][][] buildableVbos;
public volatile LodVertexBuffer[][][] buildableVbos;
/** VBOs that are sent over to the LodNodeRenderer */
public volatile VertexBuffer[][][] drawableVbos;
public volatile LodVertexBuffer[][][] drawableVbos;
/**
* if this is true the LOD buffers are currently being
@@ -532,8 +531,8 @@ public class LodBufferBuilderFactory
numberOfBuffersPerRegion = new int[numbRegionsWide][numbRegionsWide];
buildableBuffers = new LodBufferBuilder[numbRegionsWide][numbRegionsWide][];
buildableVbos = new VertexBuffer[numbRegionsWide][numbRegionsWide][];
drawableVbos = new VertexBuffer[numbRegionsWide][numbRegionsWide][];
buildableVbos = new LodVertexBuffer[numbRegionsWide][numbRegionsWide][];
drawableVbos = new LodVertexBuffer[numbRegionsWide][numbRegionsWide][];
if (glProxy.bufferStorageSupported)
{
@@ -558,8 +557,8 @@ public class LodBufferBuilderFactory
regionMemoryRequired = LodUtil.MAX_ALLOCATABLE_DIRECT_MEMORY;
numberOfBuffersPerRegion[x][z] = numberOfBuffers;
buildableBuffers[x][z] = new LodBufferBuilder[numberOfBuffers];
buildableVbos[x][z] = new VertexBuffer[numberOfBuffers];
drawableVbos[x][z] = new VertexBuffer[numberOfBuffers];
buildableVbos[x][z] = new LodVertexBuffer[numberOfBuffers];
drawableVbos[x][z] = new LodVertexBuffer[numberOfBuffers];
if (glProxy.bufferStorageSupported)
{
@@ -572,8 +571,8 @@ public class LodBufferBuilderFactory
// we only need one buffer for this region
numberOfBuffersPerRegion[x][z] = 1;
buildableBuffers[x][z] = new LodBufferBuilder[1];
buildableVbos[x][z] = new VertexBuffer[1];
drawableVbos[x][z] = new VertexBuffer[1];
buildableVbos[x][z] = new LodVertexBuffer[1];
drawableVbos[x][z] = new LodVertexBuffer[1];
if (glProxy.bufferStorageSupported)
{
@@ -587,8 +586,8 @@ public class LodBufferBuilderFactory
{
buildableBuffers[x][z][i] = new LodBufferBuilder((int) regionMemoryRequired);
buildableVbos[x][z][i] = new VertexBuffer(DefaultVertexFormats.POSITION_COLOR); //LodUtil.LOD_VERTEX_FORMAT);
drawableVbos[x][z][i] = new VertexBuffer(DefaultVertexFormats.POSITION_COLOR); //(LodUtil.LOD_VERTEX_FORMAT);
buildableVbos[x][z][i] = new LodVertexBuffer();
drawableVbos[x][z][i] = new LodVertexBuffer();
// create the initial mapped buffers (system memory)
@@ -810,7 +809,7 @@ public class LodBufferBuilderFactory
}
/** Uploads the uploadBuffer so the GPU can use it. */
private void vboUpload(VertexBuffer vbo, int storageBufferId, ByteBuffer uploadBuffer,
private void vboUpload(LodVertexBuffer vbo, int storageBufferId, ByteBuffer uploadBuffer,
boolean allowBufferExpansion, GpuUploadMethod uploadMethod)
{
// this shouldn't happen, but just to be safe
@@ -935,7 +934,7 @@ public class LodBufferBuilderFactory
// since this is called on the main render thread
if (bufferLock.tryLock())
{
VertexBuffer[][][] tmpVbo = drawableVbos;
LodVertexBuffer[][][] tmpVbo = drawableVbos;
drawableVbos = buildableVbos;
buildableVbos = tmpVbo;
@@ -956,11 +955,11 @@ public class LodBufferBuilderFactory
/** A simple container to pass multiple objects back in the getVertexBuffers method. */
public static class VertexBuffersAndOffset
{
public final VertexBuffer[][][] vbos;
public final LodVertexBuffer[][][] vbos;
public final int[][][] storageBufferIds;
public final ChunkPosWrapper drawableCenterChunkPos;
public VertexBuffersAndOffset(VertexBuffer[][][] newVbos, int[][][] newStorageBufferIds, ChunkPosWrapper newDrawableCenterChunkPos)
public VertexBuffersAndOffset(LodVertexBuffer[][][] newVbos, int[][][] newStorageBufferIds, ChunkPosWrapper newDrawableCenterChunkPos)
{
vbos = newVbos;
storageBufferIds = newStorageBufferIds;
@@ -11,12 +11,12 @@ import com.google.common.collect.ImmutableList;
*/
public class DefaultLodVertexFormats
{
public static final LodVertexFormatElement ELEMENT_POSITION = new LodVertexFormatElement(0, LodVertexFormatElement.Type.FLOAT, 3);
public static final LodVertexFormatElement ELEMENT_COLOR = new LodVertexFormatElement(0, LodVertexFormatElement.Type.UBYTE, 4);
public static final LodVertexFormatElement ELEMENT_UV = new LodVertexFormatElement(0, LodVertexFormatElement.Type.FLOAT, 2);
public static final LodVertexFormatElement ELEMENT_LIGHT_MAP_UV = new LodVertexFormatElement(1, LodVertexFormatElement.Type.SHORT, 2);
public static final LodVertexFormatElement ELEMENT_NORMAL = new LodVertexFormatElement(0, LodVertexFormatElement.Type.BYTE, 3);
public static final LodVertexFormatElement ELEMENT_PADDING = new LodVertexFormatElement(0, LodVertexFormatElement.Type.BYTE, 1);
public static final LodVertexFormatElement ELEMENT_POSITION = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.FLOAT, 3);
public static final LodVertexFormatElement ELEMENT_COLOR = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.UBYTE, 4);
public static final LodVertexFormatElement ELEMENT_UV = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.FLOAT, 2);
public static final LodVertexFormatElement ELEMENT_LIGHT_MAP_UV = new LodVertexFormatElement(1, LodVertexFormatElement.DataType.SHORT, 2);
public static final LodVertexFormatElement ELEMENT_NORMAL = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.BYTE, 3);
public static final LodVertexFormatElement ELEMENT_PADDING = new LodVertexFormatElement(0, LodVertexFormatElement.DataType.BYTE, 1);
public static final LodVertexFormat POSITION = new LodVertexFormat(ImmutableList.<LodVertexFormatElement>builder().add(ELEMENT_POSITION).build());
@@ -39,12 +39,6 @@ public class LodBufferBuilder
private LodVertexFormat format;
private boolean building;
private boolean defaultColorSet = false;
private int defaultR = 255;
private int defaultG = 255;
private int defaultB = 255;
private int defaultA = 255;
@@ -54,17 +48,6 @@ public class LodBufferBuilder
}
// Part of MC BufferBuilder's parent DefaultColorVertexBuilder
public void defaultColor(int red, int green, int blue, int alpha)
{
this.defaultR = red;
this.defaultG = green;
this.defaultB = blue;
this.defaultA = alpha;
this.defaultColorSet = true;
}
/** originally from MC's GLAllocation class */
private ByteBuffer allocateByteBuffer(int bufferSizeInBytes)
@@ -72,6 +55,7 @@ public class LodBufferBuilder
return ByteBuffer.allocateDirect(bufferSizeInBytes).order(ByteOrder.nativeOrder());
}
/** originally from MC's GLAllocation class */
@SuppressWarnings("unused")
private FloatBuffer allocateFloatBuffer(int bufferSizeInBytes)
{
return allocateByteBuffer(bufferSizeInBytes).asFloatBuffer();
@@ -337,38 +321,27 @@ public class LodBufferBuilder
public LodBufferBuilder color(int red, int green, int blue, int alpha)
{
if (this.defaultColorSet)
LodVertexFormatElement LodVertexFormatelement = this.currentElement();
if (LodVertexFormatelement.getType() != LodVertexFormatElement.DataType.UBYTE)
{
throw new IllegalStateException();
throw new IllegalStateException("Color must be stored as a UBYTE");
}
else
{
LodVertexFormatElement LodVertexFormatelement = this.currentElement();
// if (LodVertexFormatelement.getUsage() != LodVertexFormatElement.Usage.COLOR)
// {
// return this;
// }
// else if (LodVertexFormatelement.getType() != LodVertexFormatElement.Type.UBYTE)
// {
// throw new IllegalStateException();
// }
// else
// {
this.putByte(0, (byte) red);
this.putByte(1, (byte) green);
this.putByte(2, (byte) blue);
this.putByte(3, (byte) alpha);
this.nextElement();
return this;
// }
this.putByte(0, (byte) red);
this.putByte(1, (byte) green);
this.putByte(2, (byte) blue);
this.putByte(3, (byte) alpha);
this.nextElement();
return this;
}
}
public LodBufferBuilder vertex(float x, float y, float z)
{
if (this.currentElement().getType() != LodVertexFormatElement.Type.FLOAT)
if (this.currentElement().getType() != LodVertexFormatElement.DataType.FLOAT)
{
throw new IllegalStateException();
throw new IllegalStateException("Position verticies must be stored as a FLOAT");
}
else
{
@@ -534,18 +507,6 @@ public class LodBufferBuilder
}
}
public static class State
{
private final ByteBuffer data;
private final LodVertexFormat format;
private State(ByteBuffer newByteBuffer, LodVertexFormat newLodVertexFormat)
{
data = newByteBuffer;
format = newLodVertexFormat;
}
}
// Forge added methods
@@ -0,0 +1,41 @@
package com.seibel.lod.objects.opengl;
import org.lwjgl.opengl.GL15;
import com.seibel.lod.enums.rendering.GlProxyContext;
import com.seibel.lod.proxy.GlProxy;
/**
* a (almost) exact copy of MC's
* VertexBuffer object.
*
* @author James Seibel
* @version 11-13-2021
*/
public class LodVertexBuffer implements AutoCloseable
{
public int id;
public int vertexCount;
public LodVertexBuffer()
{
if (GlProxy.getInstance().getGlContext() == GlProxyContext.NONE)
throw new IllegalStateException("Thread [" +Thread.currentThread().getName() + "] tried to create a [" + LodVertexBuffer.class.getSimpleName() + "] outside a OpenGL contex.");
this.id = GL15.glGenBuffers();
}
@Override
public void close()
{
if (this.id >= 0)
{
if (GlProxy.getInstance().getGlContext() == GlProxyContext.NONE)
throw new IllegalStateException("Thread [" +Thread.currentThread().getName() + "] tried to close the [" + LodVertexBuffer.class.getSimpleName() + "] with id [" + this.id + "] outside a OpenGL contex.");
GL15.glDeleteBuffers(this.id);
this.id = -1;
}
}
}
@@ -13,125 +13,44 @@ import org.lwjgl.opengl.GL11;
*/
public class LodVertexFormatElement
{
// private static final Logger LOGGER = LogManager.getLogger();
private final LodVertexFormatElement.Type type;
// private final LodVertexFormatElement.Usage usage;
private final LodVertexFormatElement.DataType dataType;
/** James isn't sure what index is for */
private final int index;
private final int count;
private final int byteSize;
// public LodVertexFormatElement(int p_i46096_1_, LodVertexFormatElement.Type p_i46096_2_, LodVertexFormatElement.Usage p_i46096_3_, int p_i46096_4_)
public LodVertexFormatElement(int newIndex, LodVertexFormatElement.Type newType, int newCount)
public LodVertexFormatElement(int newIndex, LodVertexFormatElement.DataType newType, int newCount)
{
// if (this.supportsUsage(p_i46096_1_, p_i46096_3_))
// {
// this.usage = p_i46096_3_;
// }
// else
// {
// LOGGER.warn("Multiple vertex elements of the same type other than UVs are not supported. Forcing type to UV.");
// this.usage = LodVertexFormatElement.Usage.UV;
// }
this.type = newType;
this.dataType = newType;
this.index = newIndex;
this.count = newCount;
this.byteSize = newType.getSize() * this.count;
}
// private boolean supportsUsage(int p_177372_1_, LodVertexFormatElement.Usage p_177372_2_)
// {
// return p_177372_1_ == 0 || p_177372_2_ == LodVertexFormatElement.Usage.UV;
// }
public final LodVertexFormatElement.Type getType()
public final LodVertexFormatElement.DataType getType()
{
return this.type;
return this.dataType;
}
// public final LodVertexFormatElement.Usage getUsage()
// {
// return this.usage;
// }
public final int getIndex()
{
return this.index;
}
@Override
public String toString()
{
// return this.count + "," + this.usage.getName() + "," + this.type.getName();
return this.count + "," + this.type.getName();
}
public final int getByteSize()
{
return this.byteSize;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj != null && this.getClass() == obj.getClass())
{
LodVertexFormatElement LodVertexFormatElement = (LodVertexFormatElement) obj;
if (this.count != LodVertexFormatElement.count)
{
return false;
}
else if (this.index != LodVertexFormatElement.index)
{
return false;
}
else if (this.type != LodVertexFormatElement.type)
{
return false;
}
else
{
// return this.usage == LodVertexFormatElement.usage;
return false;
}
}
else
{
return false;
}
}
@Override
public int hashCode()
{
int i = this.type.hashCode();
// i = 31 * i + this.usage.hashCode();
i = 31 * i + this.index;
return 31 * i + this.count;
}
// public void setupBufferState(long p_227897_1_, int p_227897_3_)
// {
// this.usage.setupBufferState(this.count, this.type.getGlType(), p_227897_3_, p_227897_1_, this.index);
// }
// public void clearBufferState()
// {
// this.usage.clearBufferState(this.index);
// }
//Forge Start
// added by Forge
public int getElementCount()
{
return count;
}
//Forge End
public static enum Type
public static enum DataType
{
FLOAT(4, "Float", GL11.GL_FLOAT),
UBYTE(1, "Unsigned Byte", GL11.GL_UNSIGNED_BYTE),
@@ -145,7 +64,7 @@ public class LodVertexFormatElement
private final String name;
private final int glType;
private Type(int sizeInBytes, String newName, int openGlDataType)
private DataType(int sizeInBytes, String newName, int openGlDataType)
{
this.size = sizeInBytes;
this.name = newName;
@@ -170,88 +89,52 @@ public class LodVertexFormatElement
// public static enum Usage
// {
// POSITION("Position", (p_227914_0_, p_227914_1_, p_227914_2_, p_227914_3_, p_227914_5_) ->
// {
// GlStateManager._vertexPointer(p_227914_0_, p_227914_1_, p_227914_2_, p_227914_3_);
// GlStateManager._enableClientState(32884);
// }, (p_227912_0_) ->
// {
// GlStateManager._disableClientState(32884);
// }),
// NORMAL("Normal", (p_227913_0_, p_227913_1_, p_227913_2_, p_227913_3_, p_227913_5_) ->
// {
// GlStateManager._normalPointer(p_227913_1_, p_227913_2_, p_227913_3_);
// GlStateManager._enableClientState(32885);
// }, (p_227910_0_) ->
// {
// GlStateManager._disableClientState(32885);
// }),
// COLOR("Vertex Color", (p_227911_0_, p_227911_1_, p_227911_2_, p_227911_3_, p_227911_5_) ->
// {
// GlStateManager._colorPointer(p_227911_0_, p_227911_1_, p_227911_2_, p_227911_3_);
// GlStateManager._enableClientState(32886);
// }, (p_227908_0_) ->
// {
// GlStateManager._disableClientState(32886);
// GlStateManager._clearCurrentColor();
// }),
// UV("UV", (p_227909_0_, p_227909_1_, p_227909_2_, p_227909_3_, p_227909_5_) ->
// {
// GlStateManager._glClientActiveTexture('\u84c0' + p_227909_5_);
// GlStateManager._texCoordPointer(p_227909_0_, p_227909_1_, p_227909_2_, p_227909_3_);
// GlStateManager._enableClientState(32888);
// GlStateManager._glClientActiveTexture(33984);
// }, (p_227906_0_) ->
// {
// GlStateManager._glClientActiveTexture('\u84c0' + p_227906_0_);
// GlStateManager._disableClientState(32888);
// GlStateManager._glClientActiveTexture(33984);
// }),
// PADDING("Padding", (p_227907_0_, p_227907_1_, p_227907_2_, p_227907_3_, p_227907_5_) ->
// {
// }, (p_227904_0_) ->
// {
// }),
// GENERIC("Generic", (p_227905_0_, p_227905_1_, p_227905_2_, p_227905_3_, p_227905_5_) ->
// {
// GlStateManager._enableVertexAttribArray(p_227905_5_);
// GlStateManager._vertexAttribPointer(p_227905_5_, p_227905_0_, p_227905_1_, false, p_227905_2_, p_227905_3_);
// }, GlStateManager::_disableVertexAttribArray);
//
// private final String name;
// private final LodVertexFormatElement.Usage.ISetupState setupState;
// private final IntConsumer clearState;
//
// private Usage(String p_i225912_3_, LodVertexFormatElement.Usage.ISetupState p_i225912_4_, IntConsumer p_i225912_5_)
// {
// this.name = p_i225912_3_;
// this.setupState = p_i225912_4_;
// this.clearState = p_i225912_5_;
// }
//
// private void setupBufferState(int p_227902_1_, int p_227902_2_, int p_227902_3_, long p_227902_4_, int p_227902_6_)
// {
// this.setupState.setupBufferState(p_227902_1_, p_227902_2_, p_227902_3_, p_227902_4_, p_227902_6_);
// }
//
// public void clearBufferState(int p_227901_1_)
// {
// this.clearState.accept(p_227901_1_);
// }
//
// public String getName()
// {
// return this.name;
// }
//
// @OnlyIn(Dist.CLIENT)
// interface ISetupState
// {
// void setupBufferState(int p_setupBufferState_1_, int p_setupBufferState_2_, int p_setupBufferState_3_, long p_setupBufferState_4_, int p_setupBufferState_6_);
// }
// }
@Override
public int hashCode()
{
int i = this.dataType.hashCode();
i = 31 * i + this.index;
return 31 * i + this.count;
}
@Override
public String toString()
{
return this.count + "," + this.dataType.getName();
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj != null && this.getClass() == obj.getClass())
{
LodVertexFormatElement LodVertexFormatElement = (LodVertexFormatElement) obj;
if (this.count != LodVertexFormatElement.count)
{
return false;
}
else if (this.index != LodVertexFormatElement.index)
{
return false;
}
else if (this.dataType != LodVertexFormatElement.dataType)
{
return false;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
@@ -38,6 +38,7 @@ import com.seibel.lod.handlers.ReflectionHandler;
import com.seibel.lod.lodApi.ApiShared;
import com.seibel.lod.objects.lod.LodDimension;
import com.seibel.lod.objects.lod.RegionPos;
import com.seibel.lod.objects.opengl.LodVertexBuffer;
import com.seibel.lod.objects.rending.Mat4f;
import com.seibel.lod.objects.rending.NearFarFogSettings;
import com.seibel.lod.proxy.GlProxy;
@@ -52,7 +53,6 @@ import com.seibel.lod.wrappers.Chunk.ChunkPosWrapper;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.vertex.VertexBuffer;
import net.minecraft.potion.Effects;
import net.minecraft.profiler.IProfiler;
import net.minecraft.util.math.ChunkPos;
@@ -90,7 +90,7 @@ public class LodRenderer
private final LodBufferBuilderFactory lodBufferBuilderFactory;
/** Each VertexBuffer represents 1 region */
private VertexBuffer[][][] vbos;
private LodVertexBuffer[][][] vbos;
/**
* the OpenGL IDs for the vbos of the same indices.
* These have to be separate because we can't override the