Fix GL error spam and do some cleanup

- Add flywheel$ prefix to mixin duck interfaces/accessors
 - Better chat messages for /flywheel backend command
 - Track VAO via vanilla
This commit is contained in:
Jozufozu 2021-12-24 01:45:38 -08:00
parent 5f7b17a200
commit 3ce1345d6a
20 changed files with 96 additions and 54 deletions

View file

@ -1,5 +1,6 @@
package com.jozufozu.flywheel.backend.gl;
import com.jozufozu.flywheel.mixin.BufferUploaderAccessor;
import com.jozufozu.flywheel.util.AttribUtil;
import com.mojang.blaze3d.platform.GlStateManager;
@ -9,11 +10,14 @@ public class GlVertexArray extends GlObject {
}
public void bind() {
GlStateManager._glBindVertexArray(handle());
int handle = handle();
GlStateManager._glBindVertexArray(handle);
BufferUploaderAccessor.flywheel$setLastVAO(handle);
}
public static void unbind() {
GlStateManager._glBindVertexArray(0);
BufferUploaderAccessor.flywheel$setLastVAO(0);
}
protected void deleteInternal(int handle) {

View file

@ -31,12 +31,12 @@ public abstract class GlProgram extends GlObject {
public void bind() {
int handle = handle();
ProgramManager.glUseProgram(handle);
ShaderInstanceAccessor.setLastProgramId(handle);
ShaderInstanceAccessor.flywheel$setLastProgramId(handle);
}
public void unbind() {
ProgramManager.glUseProgram(0);
ShaderInstanceAccessor.setLastProgramId(0);
ShaderInstanceAccessor.flywheel$setLastProgramId(0);
}
/**

View file

@ -20,7 +20,7 @@ public class SuperBufferSource {
public SuperBufferSource() {
scratch = new BufferBuilder(8);
((BufferBuilderHack) scratch).freeBuffer();
((BufferBuilderHack) scratch).flywheel$freeBuffer();
}
public DirectVertexConsumer getBuffer(RenderType renderType, int vertexCount) {
@ -40,7 +40,7 @@ public class SuperBufferSource {
if (builder.expectedVertices > 0) {
RenderType type = entry.getKey();
hack.hackBegin(builder.backingBuffer, type.format(), builder.expectedVertices);
hack.flywheel$hackBegin(builder.backingBuffer, type.format(), builder.expectedVertices);
type.end(scratch, 0, 0, 0);

View file

@ -10,7 +10,7 @@ import com.mojang.blaze3d.vertex.VertexFormat;
*/
public interface BufferBuilderHack {
void freeBuffer();
void flywheel$freeBuffer();
void hackBegin(ByteBuffer buffer, VertexFormat format, int vertexCount);
void flywheel$hackBegin(ByteBuffer buffer, VertexFormat format, int vertexCount);
}

View file

@ -6,6 +6,8 @@ import java.util.Map;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import com.jozufozu.flywheel.backend.Backend;
import net.minecraft.ChatFormatting;
@ -13,12 +15,14 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent;
import net.minecraftforge.fml.ModList;
public enum FlwEngine {
OFF("off", "Off", new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED)),
BATCHING("batching", "Parallel Batching", new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN)),
INSTANCING("instancing", "GL33 Instanced Arrays", new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN)),
OFF("off", "Off"),
BATCHING("batching", "Parallel Batching"),
INSTANCING("instancing", "GL33 Instanced Arrays"),
;
private static final Map<String, FlwEngine> lookup;
@ -30,14 +34,12 @@ public enum FlwEngine {
}
}
private final Component message;
private final String shortName;
private final String properName;
FlwEngine(String shortName, String properName, Component message) {
FlwEngine(String shortName, String properName) {
this.shortName = shortName;
this.properName = properName;
this.message = message;
}
public String getProperName() {
@ -55,13 +57,33 @@ public enum FlwEngine {
if (type != null) {
FlwConfig.get().client.engine.set(type);
player.displayClientMessage(type.message, false);
Component message = getMessage(type);
player.displayClientMessage(message, false);
Backend.reloadWorldRenderers();
} else {
player.displayClientMessage(FlwConfig.get().getEngine().message, false);
player.displayClientMessage(getMessage(FlwConfig.get().getEngine()), false);
}
}
private static Component getMessage(@NotNull FlwEngine type) {
return switch (type) {
case OFF -> new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED);
case INSTANCING -> new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN);
case BATCHING -> {
MutableComponent msg = new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN);
if (ModList.get()
.isLoaded("create")) {
// FIXME: batching engine contraption lighting issues
msg.append(new TextComponent("\nWARNING: May cause issues with Create Contraptions").withStyle(ChatFormatting.RED));
}
yield msg;
}
};
}
@Nullable
public static FlwEngine decode(FriendlyByteBuf buffer) {
byte b = buffer.readByte();

View file

@ -40,7 +40,7 @@ public class AtlasInfo {
* FOR USE IN MIXIN
*/
public static void _setAtlasData(ResourceLocation atlas, SheetDataAccessor accessor) {
sheetData.put(atlas, new SheetSize(accessor.getWidth(), accessor.getHeight()));
sheetData.put(atlas, new SheetSize(accessor.flywheel$getWidth(), accessor.flywheel$getHeight()));
}
public record SheetSize(int width, int height) {

View file

@ -96,7 +96,7 @@ public class CrumblingRenderer {
Int2ObjectMap<List<BlockEntity>> breakingEntities = new Int2ObjectArrayMap<>();
for (Long2ObjectMap.Entry<SortedSet<BlockDestructionProgress>> entry : ((LevelRendererAccessor) Minecraft.getInstance().levelRenderer).getDestructionProgress()
for (Long2ObjectMap.Entry<SortedSet<BlockDestructionProgress>> entry : ((LevelRendererAccessor) Minecraft.getInstance().levelRenderer).flywheel$getDestructionProgress()
.long2ObjectEntrySet()) {
BlockPos breakingPos = BlockPos.of(entry.getLongKey());

View file

@ -3,7 +3,7 @@ package com.jozufozu.flywheel.core.materials.model;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.core.materials.BasicWriterUnsafe;
import com.jozufozu.flywheel.util.WriteUnsafe;
import com.jozufozu.flywheel.util.MatrixWrite;
public class ModelWriterUnsafe extends BasicWriterUnsafe<ModelData> {
@ -16,7 +16,7 @@ public class ModelWriterUnsafe extends BasicWriterUnsafe<ModelData> {
super.writeInternal(d);
long ptr = writePointer + 6;
((WriteUnsafe) (Object) d.model).writeUnsafe(ptr);
((WriteUnsafe) (Object) d.normal).writeUnsafe(ptr + 4 * 16);
((MatrixWrite) (Object) d.model).flywheel$writeUnsafe(ptr);
((MatrixWrite) (Object) d.normal).flywheel$writeUnsafe(ptr + 4 * 16);
}
}

View file

@ -72,6 +72,6 @@ public class RenderLayerEvent extends Event {
@Override
public String toString() {
return "RenderLayerEvent{" + "world=" + world + ", type=" + type + ", stack=" + stack + ", viewProjection=" + viewProjection + ", buffers=" + buffers + ", camX=" + camX + ", camY=" + camY + ", camZ=" + camZ + ", layer=" + layer + '}';
return "RenderLayerEvent[" + layer + "][" + "world=" + world + ", type=" + type + ", stack=" + stack + ", viewProjection=" + viewProjection + ", buffers=" + buffers + ", camX=" + camX + ", camY=" + camY + ", camZ=" + camZ + ']';
}
}

View file

@ -44,7 +44,7 @@ public abstract class BufferBuilderMixin implements BufferBuilderHack {
private int vertices;
@Override
public void freeBuffer() {
public void flywheel$freeBuffer() {
if (this.buffer != null) {
MemoryUtil.memFree(this.buffer);
this.buffer = null;
@ -52,7 +52,7 @@ public abstract class BufferBuilderMixin implements BufferBuilderHack {
}
@Override
public void hackBegin(@Nonnull ByteBuffer buffer, @Nonnull VertexFormat format, int vertexCount) {
public void flywheel$hackBegin(@Nonnull ByteBuffer buffer, @Nonnull VertexFormat format, int vertexCount) {
this.building = true;
this.mode = VertexFormat.Mode.QUADS;

View file

@ -0,0 +1,16 @@
package com.jozufozu.flywheel.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import com.mojang.blaze3d.vertex.BufferUploader;
import net.minecraft.client.renderer.ShaderInstance;
@Mixin(BufferUploader.class)
public interface BufferUploaderAccessor {
@Accessor("lastVertexArrayObject")
static void flywheel$setLastVAO(int id) {
throw new AssertionError();
}
}

View file

@ -11,6 +11,6 @@ import net.minecraft.server.level.BlockDestructionProgress;
@Mixin(LevelRenderer.class)
public interface LevelRendererAccessor {
@Accessor
Long2ObjectMap<SortedSet<BlockDestructionProgress>> getDestructionProgress();
@Accessor("destructionProgress")
Long2ObjectMap<SortedSet<BlockDestructionProgress>> flywheel$getDestructionProgress();
}

View file

@ -8,7 +8,7 @@ import net.minecraft.client.renderer.ShaderInstance;
@Mixin(ShaderInstance.class)
public interface ShaderInstanceAccessor {
@Accessor("lastProgramId")
static void setLastProgramId(int id) {
static void flywheel$setLastProgramId(int id) {
throw new AssertionError();
}
}

View file

@ -9,9 +9,9 @@ import net.minecraft.client.renderer.texture.TextureAtlas;
public interface SheetDataAccessor {
@Accessor("width")
int getWidth();
int flywheel$getWidth();
@Accessor("height")
int getHeight();
int flywheel$getHeight();
}

View file

@ -5,8 +5,7 @@ import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.util.WriteSafe;
import com.jozufozu.flywheel.util.WriteUnsafe;
import com.jozufozu.flywheel.util.MatrixWrite;
import com.mojang.math.Matrix3f;
import net.minecraftforge.api.distmarker.Dist;
@ -14,7 +13,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
@Mixin(Matrix3f.class)
public abstract class Matrix3fMixin implements WriteUnsafe, WriteSafe {
public abstract class Matrix3fMixin implements MatrixWrite {
@Shadow protected float m00;
@Shadow protected float m01;
@ -27,7 +26,7 @@ public abstract class Matrix3fMixin implements WriteUnsafe, WriteSafe {
@Shadow protected float m22;
@Override
public void writeUnsafe(long ptr) {
public void flywheel$writeUnsafe(long ptr) {
MemoryUtil.memPutFloat(ptr, m00);
MemoryUtil.memPutFloat(ptr + 4, m10);
MemoryUtil.memPutFloat(ptr + 8, m20);
@ -40,7 +39,7 @@ public abstract class Matrix3fMixin implements WriteUnsafe, WriteSafe {
}
@Override
public void write(VecBuffer buffer) {
public void flywheel$write(VecBuffer buffer) {
buffer.putFloat(m00);
buffer.putFloat(m10);
buffer.putFloat(m20);

View file

@ -5,8 +5,7 @@ import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
import com.jozufozu.flywheel.util.WriteSafe;
import com.jozufozu.flywheel.util.WriteUnsafe;
import com.jozufozu.flywheel.util.MatrixWrite;
import com.mojang.math.Matrix4f;
import net.minecraftforge.api.distmarker.Dist;
@ -14,7 +13,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
@Mixin(Matrix4f.class)
public abstract class Matrix4fMixin implements WriteUnsafe, WriteSafe {
public abstract class Matrix4fMixin implements MatrixWrite {
@Shadow protected float m00;
@Shadow protected float m01;
@ -34,7 +33,7 @@ public abstract class Matrix4fMixin implements WriteUnsafe, WriteSafe {
@Shadow protected float m33;
@Override
public void writeUnsafe(long ptr) {
public void flywheel$writeUnsafe(long ptr) {
MemoryUtil.memPutFloat(ptr, m00);
MemoryUtil.memPutFloat(ptr + 4, m10);
MemoryUtil.memPutFloat(ptr + 8, m20);
@ -54,7 +53,7 @@ public abstract class Matrix4fMixin implements WriteUnsafe, WriteSafe {
}
@Override
public void write(VecBuffer buf) {
public void flywheel$write(VecBuffer buf) {
buf.putFloat(m00);
buf.putFloat(m10);
buf.putFloat(m20);

View file

@ -0,0 +1,17 @@
package com.jozufozu.flywheel.util;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
/**
* @see com.jozufozu.flywheel.mixin.matrix.Matrix3fMixin
* @see com.jozufozu.flywheel.mixin.matrix.Matrix4fMixin
*/
public interface MatrixWrite {
/**
* Write the contents of this object into sequential memory starting at the given address.
*/
void flywheel$writeUnsafe(long ptr);
void flywheel$write(VecBuffer buf);
}

View file

@ -1,7 +0,0 @@
package com.jozufozu.flywheel.util;
import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer;
public interface WriteSafe {
void write(VecBuffer buf);
}

View file

@ -1,9 +0,0 @@
package com.jozufozu.flywheel.util;
public interface WriteUnsafe {
/**
* Write the contents of this object into sequential memory starting at the given address.
*/
void writeUnsafe(long ptr);
}

View file

@ -6,6 +6,7 @@
"refmap": "flywheel.refmap.json",
"client": [
"BufferBuilderMixin",
"BufferUploaderAccessor",
"CancelEntityRenderMixin",
"ChunkRebuildHooksMixin",
"FixFabulousDepthMixin",