mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2024-12-27 07:26:48 +01:00
Drew buffer
- Remove DrawBuffer and related mixins.
This commit is contained in:
parent
2dbdf83ec2
commit
744199217c
6 changed files with 0 additions and 275 deletions
|
@ -11,7 +11,6 @@ import com.jozufozu.flywheel.backend.Backends;
|
|||
import com.jozufozu.flywheel.backend.ShaderIndices;
|
||||
import com.jozufozu.flywheel.backend.compile.FlwPrograms;
|
||||
import com.jozufozu.flywheel.backend.engine.UniformBuffer;
|
||||
import com.jozufozu.flywheel.backend.engine.batching.DrawBuffer;
|
||||
import com.jozufozu.flywheel.config.BackendArgument;
|
||||
import com.jozufozu.flywheel.config.FlwCommands;
|
||||
import com.jozufozu.flywheel.config.FlwConfig;
|
||||
|
@ -96,7 +95,6 @@ public class Flywheel {
|
|||
|
||||
forgeEventBus.addListener(FlwCommands::registerClientCommands);
|
||||
|
||||
forgeEventBus.addListener(DrawBuffer::onReloadRenderers);
|
||||
forgeEventBus.addListener(UniformBuffer::onReloadLevelRenderer);
|
||||
|
||||
forgeEventBus.addListener(LightUpdater::onClientTick);
|
||||
|
|
|
@ -1,183 +0,0 @@
|
|||
package com.jozufozu.flywheel.backend.engine.batching;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.jozufozu.flywheel.api.event.ReloadLevelRendererEvent;
|
||||
import com.jozufozu.flywheel.api.event.RenderStage;
|
||||
import com.jozufozu.flywheel.api.vertex.VertexView;
|
||||
import com.jozufozu.flywheel.api.vertex.VertexViewProvider;
|
||||
import com.jozufozu.flywheel.extension.BufferBuilderExtension;
|
||||
import com.jozufozu.flywheel.extension.RenderTypeExtension;
|
||||
import com.jozufozu.flywheel.lib.memory.MemoryBlock;
|
||||
import com.mojang.blaze3d.vertex.VertexFormat;
|
||||
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
import net.minecraft.util.Mth;
|
||||
|
||||
/**
|
||||
* A byte buffer that can be used to draw vertices through multiple {@link VertexView}s.
|
||||
* <br>
|
||||
* Note: The number of vertices needs to be known ahead of time.
|
||||
*/
|
||||
public class DrawBuffer {
|
||||
private static final List<DrawBuffer> ALL = new ArrayList<>();
|
||||
|
||||
private final RenderType renderType;
|
||||
private final VertexFormat format;
|
||||
private final int stride;
|
||||
private final boolean sortOnUpload;
|
||||
private final VertexViewProvider provider;
|
||||
|
||||
private MemoryBlock data;
|
||||
private ByteBuffer buffer;
|
||||
|
||||
private boolean prepared;
|
||||
private int vertexCount;
|
||||
private int verticesToDraw;
|
||||
|
||||
public DrawBuffer(RenderType renderType, VertexFormat format, int stride, boolean sortOnUpload, VertexViewProvider provider) {
|
||||
this.renderType = renderType;
|
||||
this.format = format;
|
||||
this.stride = stride;
|
||||
this.sortOnUpload = sortOnUpload;
|
||||
this.provider = provider;
|
||||
|
||||
ALL.add(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares this buffer by initializing a block of memory.
|
||||
* @param vertexCount The number of vertices to reserve memory for.
|
||||
* @throws IllegalStateException If the buffer is already in use.
|
||||
*/
|
||||
public void prepare(int vertexCount) {
|
||||
if (prepared) {
|
||||
throw new IllegalStateException("Cannot prepare DrawBuffer twice!");
|
||||
}
|
||||
|
||||
this.vertexCount = vertexCount;
|
||||
verticesToDraw = this.vertexCount;
|
||||
|
||||
// Add one extra vertex to uphold the vanilla assumption that BufferBuilders have at least
|
||||
// enough buffer space for one more vertex. Rubidium checks for this extra space when popNextBuffer
|
||||
// is called and reallocates the buffer if there is not space for one more vertex.
|
||||
int byteSize = stride * (vertexCount + 1);
|
||||
|
||||
// We'll need to allocate space for the index buffer if this render type needs sorting.
|
||||
if (sortOnUpload) {
|
||||
int i = renderType.mode()
|
||||
.indexCount(vertexCount);
|
||||
VertexFormat.IndexType indexType = VertexFormat.IndexType.least(i);
|
||||
int extraBytes = Mth.roundToward(i * indexType.bytes, 4);
|
||||
byteSize += extraBytes;
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
data = MemoryBlock.malloc(byteSize);
|
||||
buffer = data.asBuffer();
|
||||
} else if (byteSize > data.size()) {
|
||||
data = data.realloc(byteSize);
|
||||
buffer = data.asBuffer();
|
||||
}
|
||||
|
||||
prepared = true;
|
||||
}
|
||||
|
||||
public VertexView slice(int startVertex, int vertexCount) {
|
||||
if (!prepared) {
|
||||
throw new IllegalStateException("Cannot slice DrawBuffer that is not prepared!");
|
||||
}
|
||||
|
||||
if (startVertex + vertexCount > this.vertexCount) {
|
||||
throw new IndexOutOfBoundsException("Vertex count greater than allocated: " + startVertex + " + " + vertexCount + " > " + this.vertexCount);
|
||||
}
|
||||
|
||||
VertexView vertexView = provider.createVertexView();
|
||||
vertexView.ptr(ptrForVertex(startVertex));
|
||||
vertexView.vertexCount(vertexCount);
|
||||
return vertexView;
|
||||
}
|
||||
|
||||
public long ptrForVertex(long startVertex) {
|
||||
return data.ptr() + startVertex * stride;
|
||||
}
|
||||
|
||||
public void verticesToDraw(int verticesToDraw) {
|
||||
this.verticesToDraw = verticesToDraw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects the backing buffer into the given builder and prepares it for rendering.
|
||||
*
|
||||
* @param bufferBuilder The buffer builder to inject into.
|
||||
*/
|
||||
public void inject(BufferBuilderExtension bufferBuilder) {
|
||||
if (!prepared) {
|
||||
throw new IllegalStateException("Cannot inject DrawBuffer that is not prepared!");
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
bufferBuilder.flywheel$injectForRender(buffer, format, verticesToDraw);
|
||||
}
|
||||
|
||||
public RenderType getRenderType() {
|
||||
return renderType;
|
||||
}
|
||||
|
||||
public VertexFormat getVertexFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public boolean isPrepared() {
|
||||
return prepared;
|
||||
}
|
||||
|
||||
public int getVertexCount() {
|
||||
return vertexCount;
|
||||
}
|
||||
|
||||
public int getVerticesToDraw() {
|
||||
return verticesToDraw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@code true} if the buffer has any vertices to draw.
|
||||
*/
|
||||
public boolean hasVertices() {
|
||||
return verticesToDraw > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the draw buffer to have no vertices.<p>
|
||||
*
|
||||
* Does not clear the backing buffer.
|
||||
*/
|
||||
public void reset() {
|
||||
prepared = false;
|
||||
vertexCount = 0;
|
||||
verticesToDraw = 0;
|
||||
}
|
||||
|
||||
public void free() {
|
||||
reset();
|
||||
|
||||
if (data == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
data.free();
|
||||
data = null;
|
||||
buffer = null;
|
||||
}
|
||||
|
||||
public static void onReloadRenderers(ReloadLevelRendererEvent event) {
|
||||
ALL.forEach(DrawBuffer::free);
|
||||
}
|
||||
|
||||
public static DrawBuffer get(RenderType renderType, RenderStage stage) {
|
||||
return RenderTypeExtension.getDrawBufferSet(renderType)
|
||||
.getBuffer(stage);
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.jozufozu.flywheel.backend.engine.batching;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.jozufozu.flywheel.api.event.RenderStage;
|
||||
import com.jozufozu.flywheel.api.vertex.VertexViewProvider;
|
||||
import com.jozufozu.flywheel.api.vertex.VertexViewProviderRegistry;
|
||||
import com.mojang.blaze3d.vertex.VertexFormat;
|
||||
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
|
||||
public class DrawBufferSet {
|
||||
private final RenderType renderType;
|
||||
private final VertexFormat format;
|
||||
private final boolean sortOnUpload;
|
||||
private final int stride;
|
||||
private final VertexViewProvider provider;
|
||||
private final Map<RenderStage, DrawBuffer> buffers = new EnumMap<>(RenderStage.class);
|
||||
|
||||
public DrawBufferSet(RenderType renderType, boolean sortOnUpload) {
|
||||
this.renderType = renderType;
|
||||
this.sortOnUpload = sortOnUpload;
|
||||
format = renderType.format();
|
||||
stride = format.getVertexSize();
|
||||
provider = VertexViewProviderRegistry.getProvider(format);
|
||||
}
|
||||
|
||||
public DrawBuffer getBuffer(RenderStage stage) {
|
||||
return buffers.computeIfAbsent(stage, renderStage -> new DrawBuffer(renderType, format, stride, sortOnUpload, provider));
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package com.jozufozu.flywheel.extension;
|
||||
|
||||
import com.jozufozu.flywheel.backend.engine.batching.DrawBufferSet;
|
||||
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
|
||||
/**
|
||||
* Duck interface to make RenderType store a DrawBufferSet.
|
||||
*
|
||||
* @see RenderType
|
||||
*/
|
||||
public interface RenderTypeExtension {
|
||||
/**
|
||||
* @return The DrawBufferSet associated with this RenderType.
|
||||
*/
|
||||
DrawBufferSet flywheel$getDrawBufferSet();
|
||||
|
||||
/**
|
||||
* Helper function to cast a RenderType to a RenderTypeExtension and get its DrawBufferSet.
|
||||
* @param type The RenderType to get the DrawBufferSet from.
|
||||
* @return The DrawBufferSet associated with the given RenderType.
|
||||
*/
|
||||
static DrawBufferSet getDrawBufferSet(RenderType type) {
|
||||
return ((RenderTypeExtension) type).flywheel$getDrawBufferSet();
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.jozufozu.flywheel.mixin;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
|
||||
import com.jozufozu.flywheel.backend.engine.batching.DrawBufferSet;
|
||||
import com.jozufozu.flywheel.extension.RenderTypeExtension;
|
||||
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
|
||||
@Mixin(RenderType.class)
|
||||
abstract class RenderTypeMixin implements RenderTypeExtension {
|
||||
@Shadow
|
||||
@Final
|
||||
private boolean sortOnUpload;
|
||||
|
||||
@Unique
|
||||
private DrawBufferSet flywheel$drawBufferSet;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public DrawBufferSet flywheel$getDrawBufferSet() {
|
||||
if (flywheel$drawBufferSet == null) {
|
||||
flywheel$drawBufferSet = new DrawBufferSet((RenderType) (Object) this, sortOnUpload);
|
||||
}
|
||||
return flywheel$drawBufferSet;
|
||||
}
|
||||
}
|
|
@ -16,7 +16,6 @@
|
|||
"LightUpdateMixin",
|
||||
"MinecraftMixin",
|
||||
"PoseStackMixin",
|
||||
"RenderTypeMixin",
|
||||
"VertexFormatMixin",
|
||||
"fix.FixFabulousDepthMixin",
|
||||
"fix.FixNormalScalingMixin",
|
||||
|
|
Loading…
Reference in a new issue