From 1d4bb972b91bb34c4812548442a7304ee514ace9 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 8 Nov 2021 20:48:02 -0800 Subject: [PATCH] Named models --- .../jozufozu/flywheel/backend/gl/error/GlError.java | 9 +++++++++ .../flywheel/backend/instancing/GPUInstancer.java | 13 +++++++++++-- .../backend/material/MaterialGroupImpl.java | 3 +++ .../com/jozufozu/flywheel/config/BooleanConfig.java | 12 ++++++------ .../flywheel/core/model/BakedModelModel.java | 5 +++++ .../jozufozu/flywheel/core/model/BlockModel.java | 10 ++++++++-- .../com/jozufozu/flywheel/core/model/IModel.java | 5 +++++ .../com/jozufozu/flywheel/core/model/ModelPart.java | 13 ++++++++++--- .../jozufozu/flywheel/core/model/PartBuilder.java | 6 ++++-- .../jozufozu/flywheel/core/model/WorldModel.java | 9 ++++++++- .../com/jozufozu/flywheel/vanilla/BellInstance.java | 2 +- .../jozufozu/flywheel/vanilla/ChestInstance.java | 12 ++++++------ .../jozufozu/flywheel/vanilla/MinecartInstance.java | 2 +- .../flywheel/vanilla/ShulkerBoxInstance.java | 4 ++-- 14 files changed, 79 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java index f96cfd207..2461fad51 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/error/GlError.java @@ -1,5 +1,7 @@ package com.jozufozu.flywheel.backend.gl.error; +import java.util.function.Supplier; + import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; @@ -35,4 +37,11 @@ public enum GlError { public static GlError poll() { return errorLookup.get(GL20.glGetError()); } + + public static void pollAndThrow(Supplier context) { + GlError poll = GlError.poll(); + if (poll != null) { + throw new GlException(poll, context.get()); + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java index 1e9d85dcd..364d406c6 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java @@ -9,6 +9,7 @@ import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; +import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.model.IBufferedModel; import com.jozufozu.flywheel.backend.model.ModelAllocator; import com.jozufozu.flywheel.backend.struct.StructType; @@ -73,14 +74,22 @@ public class GPUInstancer implements Instancer { if (invalid()) return; vao.bind(); - renderSetup(); + GlError.pollAndThrow(() -> modelData.name() + "_bind"); - if (glInstanceCount > 0) model.drawInstances(glInstanceCount); + renderSetup(); + GlError.pollAndThrow(() -> modelData.name() + "_setup"); + + if (glInstanceCount > 0) { + model.drawInstances(glInstanceCount); + GlError.pollAndThrow(() -> modelData.name() + "_draw"); + } // persistent mapping sync point instanceVBO.doneForThisFrame(); vao.unbind(); + + GlError.pollAndThrow(() -> modelData.name() + "_unbind"); } private boolean invalid() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java index 8cea7ca24..504b3fcd2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialGroupImpl.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import com.jozufozu.flywheel.backend.gl.error.GlError; import com.jozufozu.flywheel.backend.instancing.InstanceData; import com.jozufozu.flywheel.backend.state.IRenderState; import com.jozufozu.flywheel.core.shader.WorldProgram; @@ -45,6 +46,8 @@ public class MaterialGroupImpl

implements MaterialGroup public void render(Matrix4f viewProjection, double camX, double camY, double camZ) { for (MaterialRenderer

renderer : renderers) { renderer.render(viewProjection, camX, camY, camZ); + + GlError.pollAndThrow(renderer.material.spec.name::toString); } } diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java index ff85c8cfc..31f4d1297 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java @@ -37,7 +37,7 @@ public enum BooleanConfig { if (player == null || state == null) return; if (state == BooleanDirective.DISPLAY) { - Component text = new TextComponent("Flywheel renderer is currently: ").append(boolToText(FlwConfig.get().client.enabled.get())); + Component text = new TextComponent("Flywheel renderer is currently: ").append(boolToText(FlwConfig.get().enabled())); player.displayClientMessage(text, false); return; } @@ -47,7 +47,7 @@ public enum BooleanConfig { FlwConfig.get().client.enabled.set(enabled); - Component text = boolToText(FlwConfig.get().client.enabled.get()).append(new TextComponent(" Flywheel renderer").withStyle(ChatFormatting.WHITE)); + Component text = boolToText(FlwConfig.get().enabled()).append(new TextComponent(" Flywheel renderer").withStyle(ChatFormatting.WHITE)); Component error = new TextComponent("Flywheel renderer does not support Optifine Shaders").withStyle(ChatFormatting.RED); player.displayClientMessage(cannotUse ? error : text, false); @@ -60,14 +60,14 @@ public enum BooleanConfig { if (player == null || state == null) return; if (state == BooleanDirective.DISPLAY) { - Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(FlwConfig.get().client.debugNormals.get())); + Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(FlwConfig.get().debugNormals())); player.displayClientMessage(text, false); return; } FlwConfig.get().client.debugNormals.set(state.get()); - Component text = boolToText(FlwConfig.get().client.debugNormals.get()).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE)); + Component text = boolToText(FlwConfig.get().debugNormals()).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE)); player.displayClientMessage(text, false); } @@ -78,14 +78,14 @@ public enum BooleanConfig { if (player == null || state == null) return; if (state == BooleanDirective.DISPLAY) { - Component text = new TextComponent("Chunk caching is currently: ").append(boolToText(FlwConfig.get().client.chunkCaching.get())); + Component text = new TextComponent("Chunk caching is currently: ").append(boolToText(FlwConfig.get().chunkCaching())); player.displayClientMessage(text, false); return; } FlwConfig.get().client.chunkCaching.set(state.get()); - Component text = boolToText(FlwConfig.get().client.chunkCaching.get()).append(new TextComponent(" chunk caching").withStyle(ChatFormatting.WHITE)); + Component text = boolToText(FlwConfig.get().chunkCaching()).append(new TextComponent(" chunk caching").withStyle(ChatFormatting.WHITE)); player.displayClientMessage(text, false); Backend.reloadWorldRenderers(); diff --git a/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java b/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java index 314cf9092..bcc5ae06e 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BakedModelModel.java @@ -56,6 +56,11 @@ public class BakedModelModel implements IModel { this.numQuads = numQuads; } + @Override + public String name() { + return model.toString(); + } + @Override public void buffer(VecBuffer buffer) { diff --git a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java index 7cd27db3b..2c8c62107 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java @@ -2,8 +2,6 @@ package com.jozufozu.flywheel.core.model; import java.util.Arrays; -import org.lwjgl.opengl.GL11; - import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.Formats; @@ -30,6 +28,8 @@ public class BlockModel implements IModel { private final BufferBuilderReader reader; + private final String name; + public BlockModel(BlockState state) { this(Minecraft.getInstance() .getBlockRenderer() @@ -42,6 +42,12 @@ public class BlockModel implements IModel { public BlockModel(BakedModel model, BlockState referenceState, PoseStack ms) { reader = new BufferBuilderReader(getBufferBuilder(model, referenceState, ms)); + name = referenceState.toString(); + } + + @Override + public String name() { + return name; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/model/IModel.java b/src/main/java/com/jozufozu/flywheel/core/model/IModel.java index b7ffd1a1b..75c1be74d 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/IModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/IModel.java @@ -27,6 +27,11 @@ import com.jozufozu.flywheel.core.QuadConverter; */ public interface IModel { + /** + * A name uniquely identifying this model. + */ + String name(); + /** * Copy this model into the given buffer. */ diff --git a/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java b/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java index 6ccb583e3..3574019a6 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java @@ -10,9 +10,11 @@ public class ModelPart implements IModel { private final List cuboids; private int vertices; + private final String name; - public ModelPart(List cuboids) { + public ModelPart(List cuboids, String name) { this.cuboids = cuboids; + this.name = name; vertices = 0; @@ -21,8 +23,13 @@ public class ModelPart implements IModel { } } - public static PartBuilder builder(int sizeU, int sizeV) { - return new PartBuilder(sizeU, sizeV); + public static PartBuilder builder(String name, int sizeU, int sizeV) { + return new PartBuilder(name, sizeU, sizeV); + } + + @Override + public String name() { + return name; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java index 96a1a033d..0e8bff487 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java @@ -23,8 +23,10 @@ public class PartBuilder { private TextureAtlasSprite sprite; private final List cuboids = new ArrayList<>(); + private final String name; - public PartBuilder(int sizeU, int sizeV) { + public PartBuilder(String name, int sizeU, int sizeV) { + this.name = name; this.sizeU = (float) sizeU; this.sizeV = (float) sizeV; } @@ -39,7 +41,7 @@ public class PartBuilder { } public ModelPart build() { - return new ModelPart(cuboids); + return new ModelPart(cuboids, name); } private PartBuilder addCuboid(CuboidBuilder builder) { diff --git a/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java b/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java index 938d5cdca..32939ff22 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java @@ -15,9 +15,16 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemp public class WorldModel implements IModel { private final BufferBuilderReader reader; + private final String name; - public WorldModel(BlockAndTintGetter renderWorld, RenderType layer, Collection blocks) { + public WorldModel(BlockAndTintGetter renderWorld, RenderType layer, Collection blocks, String name) { reader = new BufferBuilderReader(ModelUtil.getBufferBuilderFromTemplate(renderWorld, layer, blocks)); + this.name = name; + } + + @Override + public String name() { + return name; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java index cddfddec7..b9cfa9c28 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java @@ -64,7 +64,7 @@ public class BellInstance extends TileEntityInstance implements } private static ModelPart createBellModel() { - return ModelPart.builder(32, 32) + return ModelPart.builder("bell", 32, 32) .sprite(BellRenderer.BELL_RESOURCE_LOCATION.sprite()) .cuboid() .start(5.0F, 6.0F, 5.0F) diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index 905f86fee..1ee1e6485 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -137,7 +137,7 @@ public class ChestInstance extends TileE switch (chestType) { case LEFT: - return ModelPart.builder(64, 64) + return ModelPart.builder("chest_base_left", 64, 64) .sprite(renderMaterial.sprite()) .cuboid() .textureOffset(0, 19) @@ -146,7 +146,7 @@ public class ChestInstance extends TileE .endCuboid() .build(); case RIGHT: - return ModelPart.builder(64, 64) + return ModelPart.builder("chest_base_right", 64, 64) .sprite(renderMaterial.sprite()) .cuboid() .textureOffset(0, 19) @@ -156,7 +156,7 @@ public class ChestInstance extends TileE .build(); } - return ModelPart.builder(64, 64) + return ModelPart.builder("chest_base", 64, 64) .sprite(renderMaterial.sprite()) .cuboid() .textureOffset(0, 19) @@ -170,7 +170,7 @@ public class ChestInstance extends TileE switch (chestType) { case LEFT: - return ModelPart.builder(64, 64) + return ModelPart.builder("chest_lid_left", 64, 64) .sprite(renderMaterial.sprite()) .cuboid() .textureOffset(0, 0) @@ -183,7 +183,7 @@ public class ChestInstance extends TileE .endCuboid() .build(); case RIGHT: - return ModelPart.builder(64, 64) + return ModelPart.builder("chest_lid_right", 64, 64) .sprite(renderMaterial.sprite()) .cuboid() .textureOffset(0, 0) @@ -197,7 +197,7 @@ public class ChestInstance extends TileE .build(); } - return ModelPart.builder(64, 64) + return ModelPart.builder("chest_lid", 64, 64) .sprite(renderMaterial.sprite()) .cuboid() .textureOffset(0, 0) diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java index 820b4d364..c5d675a54 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java @@ -155,7 +155,7 @@ public class MinecartInstance extends EntityInstance private IModel getBodyModel() { int y = -3; - return ModelPart.builder(64, 32) + return ModelPart.builder("minecart", 64, 32) .cuboid().invertYZ().start(-10, -8, -y).size(20, 16, 2).textureOffset(0, 10).rotateX(((float)Math.PI / 2F)).endCuboid() .cuboid().invertYZ().start(-8, y, -10).size(16, 8, 2).rotateY(((float)Math.PI * 1.5F)).endCuboid() .cuboid().invertYZ().start(-8, y, -10).size(16, 8, 2).rotateY(((float)Math.PI / 2F)).endCuboid() diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java index 334778b89..29ac1c79e 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java @@ -101,7 +101,7 @@ public class ShulkerBoxInstance extends TileEntityInstance