diff --git a/.editorconfig b/.editorconfig index 1eeca6433..f1abc2e0c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,6 +13,17 @@ insert_final_newline = true [*.json] indent_style = space indent_size = 2 +max_line_length = 500 +ij_json_keep_blank_lines_in_code = 0 +ij_json_keep_indents_on_empty_lines = false +ij_json_keep_line_breaks = true +ij_json_space_after_colon = true +ij_json_space_after_comma = true +ij_json_space_before_colon = true +ij_json_space_before_comma = false +ij_json_spaces_within_braces = true +ij_json_spaces_within_brackets = false +ij_json_wrap_long_lines = false [*.java] indent_style = tab diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 281b66462..6bfcbdbef 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -59,6 +59,10 @@ body: label: Mod Version description: The version of the mod you were using when the bug occured options: + - "0.6.8" + - "0.6.7" + - "0.6.6" + - "0.6.5" - "0.6.4" - "0.6.3" - "0.6.2" @@ -77,6 +81,8 @@ body: label: Minecraft Version description: The version of Minecraft you were using when the bug occured options: + - "1.19.2" + - "1.18.2" - "1.18.1" - "1.17.1" validations: diff --git a/build.gradle b/build.gradle index 2345c1932..1386c43a8 100644 --- a/build.gradle +++ b/build.gradle @@ -71,6 +71,14 @@ dependencies { modCompileOnly 'io.vram:frex-fabric-mc118:6.0.263' } +loom { + runs { + client { + property('flw.loadRenderDoc', 'true') + } + } +} + processResources { inputs.property 'version', project.version diff --git a/gradle.properties b/gradle.properties index 4ade0fa1f..720361334 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs = -Xmx3G org.gradle.daemon = false # mod version info -mod_version = 0.6.4 +mod_version = 0.6.8 artifact_minecraft_version = 1.18.2 minecraft_version = 1.18.2 @@ -12,7 +12,7 @@ fabric_version = 0.56.1+1.18.2 # build dependency versions loom_version = 0.12-SNAPSHOT cursegradle_version = 1.4.0 -parchment_version = 2022.07.10 +parchment_version = 2022.11.06 # curseforge info projectId = 486392 diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java index eec0a4cf8..77749e8c7 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlStateTracker.java @@ -7,13 +7,12 @@ import com.mojang.blaze3d.platform.GlStateManager; * Tracks bound buffers/vbos because GlStateManager doesn't do that for us. */ public class GlStateTracker { - - private static final int[] buffers = new int[GlBufferType.values().length]; + private static final int[] BUFFERS = new int[GlBufferType.values().length]; private static int vao; private static int program; public static int getBuffer(GlBufferType type) { - return buffers[type.ordinal()]; + return BUFFERS[type.ordinal()]; } public static int getVertexArray() { @@ -24,36 +23,36 @@ public class GlStateTracker { return program; } - public static void _setBuffer(GlBufferType type, int buffer) { - buffers[type.ordinal()] = buffer; - } - - public static void _setProgram(int id) { - program = id; + public static void _setBuffer(GlBufferType type, int id) { + BUFFERS[type.ordinal()] = id; } public static void _setVertexArray(int id) { vao = id; } - public static State getRestoreState() { - return new State(buffers.clone(), vao, program); + public static void _setProgram(int id) { + program = id; } - public static record State(int[] buffers, int vao, int program) { + public static State getRestoreState() { + return new State(BUFFERS.clone(), vao, program); + } + + public record State(int[] buffers, int vao, int program) { public void restore() { + if (vao != GlStateTracker.vao) { + GlStateManager._glBindVertexArray(vao); + } + GlBufferType[] values = GlBufferType.values(); for (int i = 0; i < values.length; i++) { - if (buffers[i] != GlStateTracker.buffers[i]) { + if (buffers[i] != GlStateTracker.BUFFERS[i] && values[i] != GlBufferType.ELEMENT_ARRAY_BUFFER) { GlStateManager._glBindBuffer(values[i].glEnum, buffers[i]); } } - if (vao != GlStateTracker.vao) { - GlStateManager._glBindVertexArray(vao); - } - if (program != GlStateTracker.program) { GlStateManager._glUseProgram(program); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java index 25155a1d0..c95a398cb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBuffer.java @@ -39,7 +39,7 @@ public class MappedBuffer extends VecBuffer implements AutoCloseable { } @Override - public void close() throws Exception { + public void close() { flush(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/DrawBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/DrawBuffer.java index 4166c1e59..f9c6ab051 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/DrawBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/DrawBuffer.java @@ -2,6 +2,8 @@ package com.jozufozu.flywheel.backend.instancing; import java.nio.ByteBuffer; +import org.jetbrains.annotations.ApiStatus; + import com.jozufozu.flywheel.backend.model.BufferBuilderExtension; import com.jozufozu.flywheel.backend.model.DirectVertexConsumer; import com.mojang.blaze3d.platform.MemoryTracker; @@ -20,6 +22,7 @@ public class DrawBuffer { private ByteBuffer backingBuffer; private int expectedVertices; + @ApiStatus.Internal public DrawBuffer(RenderType parent) { this.parent = parent; } @@ -39,7 +42,10 @@ public class DrawBuffer { VertexFormat format = parent.format(); - int byteSize = format.getVertexSize() * 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 = format.getVertexSize() * (vertexCount + 1); if (backingBuffer == null) { backingBuffer = MemoryTracker.create(byteSize); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java index 1f8310d1e..7743fb0b8 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.backend.instancing; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -359,4 +360,14 @@ public abstract class InstanceManager implements InstancingEngine.OriginShift LightUpdater.get(value.world).removeListener(value); } } + + public void queueAddAll(Collection objects) { + if (!Backend.isOn() || objects.isEmpty()) { + return; + } + + synchronized (queuedAdditions) { + queuedAdditions.addAll(objects); + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java index 60ece43f5..024073635 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java @@ -6,9 +6,7 @@ import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; -import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Entity; -import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; public class EntityInstanceManager extends InstanceManager { @@ -29,18 +27,13 @@ public class EntityInstanceManager extends InstanceManager { @Override protected boolean canCreateInstance(Entity entity) { - if (!entity.isAlive()) return false; + if (!entity.isAlive()) { + return false; + } Level world = entity.level; - if (Backend.isFlywheelWorld(world)) { - BlockPos pos = entity.blockPosition(); + return Backend.isFlywheelWorld(world); - BlockGetter existingChunk = world.getChunkForCollisions(pos.getX() >> 4, pos.getZ() >> 4); - - return existingChunk != null; - } - - return false; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java index 4b469c02f..d3522ac81 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/GPUInstancer.java @@ -45,6 +45,7 @@ public class GPUInstancer extends AbstractInstancer { public void render() { if (invalid()) return; + // XXX VAO is bound and not reset or restored vao.bind(); renderSetup(); @@ -69,11 +70,13 @@ public class GPUInstancer extends AbstractInstancer { vao = new GlVertexArray(); model = modelAllocator.alloc(modelData, arenaModel -> { + // XXX VAO is bound and not reset or restored vao.bind(); arenaModel.setupState(vao); }); + // XXX VAO is bound and not reset or restored vao.bind(); vao.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount()); @@ -108,6 +111,7 @@ public class GPUInstancer extends AbstractInstancer { removeDeletedInstances(); } + // XXX ARRAY_BUFFER is bound and reset instanceVBO.bind(); if (!realloc()) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java index 2bc228ed9..b132596ca 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java @@ -68,15 +68,23 @@ public class InstancedMaterialGroup

implements MaterialG return vertexCount; } + // XXX Overriden in CrumblingGroup + // XXX Runs inside of restore state public void render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { type.setupRenderState(); - Textures.bindActiveTextures(); - renderAll(viewProjection, camX, camY, camZ, layer); + Textures.bindActiveTextures(); // XXX Changes active unit and bound textures + renderAll(viewProjection, camX, camY, camZ, layer); // XXX May change ARRAY_BUFFER binding (reset or not reset), VAO binding (not reset), shader binding (not reset), call Model.createEBO type.clearRenderState(); + // XXX Should texture bindings be reset or restored? + // XXX Should the active unit be reset or restored? + // XXX Should the VAO binding be reset or restored? + // XXX Should the ARRAY_BUFFER binding be reset or restored? + // XXX Should the shader binding be reset or restored? } + // XXX Internal GL state changes are inconsistent; sometimes bindings are reset to 0, sometimes not protected void renderAll(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { - initializeInstancers(); + initializeInstancers(); // XXX May change ARRAY_BUFFER binding (reset or not reset), VAO binding (not reset), call Model.createEBO vertexCount = 0; instanceCount = 0; @@ -88,6 +96,7 @@ public class InstancedMaterialGroup

implements MaterialG P program = owner.context.getProgram(ProgramContext.create(entry.getKey() .getProgramSpec(), Formats.POS_TEX_NORMAL, layer)); + // XXX Shader is bound and not reset or restored program.bind(); program.uploadViewProjection(viewProjection); program.uploadCameraPos(camX, camY, camZ); @@ -95,7 +104,7 @@ public class InstancedMaterialGroup

implements MaterialG setup(program); for (GPUInstancer instancer : material.getAllInstancers()) { - instancer.render(); + instancer.render(); // XXX May change VAO binding (not reset), ARRAY_BUFFER binding (reset) vertexCount += instancer.getVertexCount(); instanceCount += instancer.getInstanceCount(); } @@ -103,19 +112,19 @@ public class InstancedMaterialGroup

implements MaterialG } private void initializeInstancers() { - ModelAllocator allocator = getModelAllocator(); + ModelAllocator allocator = getModelAllocator(); // XXX May change ARRAY_BUFFER binding (not reset) // initialize all uninitialized instancers... for (InstancedMaterial material : materials.values()) { for (GPUInstancer instancer : material.uninitialized) { - instancer.init(allocator); + instancer.init(allocator); // XXX May change VAO binding (not reset), ARRAY_BUFFER binding (not reset), call Model.createEBO } material.uninitialized.clear(); } if (allocator instanceof ModelPool pool) { // ...and then flush the model arena in case anything was marked for upload - pool.flush(); + pool.flush(); // XXX May change ARRAY_BUFFER binding (reset) } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index f0a696f02..59e9ff50c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -10,6 +10,7 @@ import javax.annotation.Nullable; import com.jozufozu.flywheel.api.MaterialGroup; import com.jozufozu.flywheel.backend.RenderLayer; +import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.instancing.Engine; import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.core.compile.ProgramCompiler; @@ -74,6 +75,9 @@ public class InstancingEngine

implements Engine { */ @Override public void render(TaskEngine taskEngine, RenderLayerEvent event) { + // XXX Restore state + GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); + double camX; double camY; double camZ; @@ -93,6 +97,8 @@ public class InstancingEngine

implements Engine { } getGroupsToRender(event.getLayer()).forEach(group -> group.render(viewProjection, camX, camY, camZ, event.getLayer())); + + restoreState.restore(); } private Stream> getGroupsToRender(@Nullable RenderLayer layer) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ElementBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/model/ElementBuffer.java index 8227ad019..07ce772d3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ElementBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ElementBuffer.java @@ -1,25 +1,29 @@ package com.jozufozu.flywheel.backend.model; -import com.jozufozu.flywheel.backend.gl.GlNumericType; -import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; +import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; +import com.mojang.blaze3d.vertex.VertexFormat; public class ElementBuffer { - private final GlBuffer buffer; - public final int elementCount; - public final GlNumericType eboIndexType; + protected final int elementCount; + protected final VertexFormat.IndexType eboIndexType; + private final int glBuffer; - public ElementBuffer(GlBuffer backing, int elementCount, GlNumericType indexType) { - this.buffer = backing; - this.eboIndexType = indexType; + public ElementBuffer(int backing, int elementCount, VertexFormat.IndexType indexType) { this.elementCount = elementCount; + this.eboIndexType = indexType; + this.glBuffer = backing; } public void bind() { - buffer.bind(); + GlBufferType.ELEMENT_ARRAY_BUFFER.bind(glBuffer); } - public void unbind() { - buffer.unbind(); + public int getElementCount() { + return elementCount; + } + + public VertexFormat.IndexType getEboIndexType() { + return eboIndexType; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java b/src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java index 7520afff2..42d1a67df 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/FallbackAllocator.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend.model; +import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.model.Model; public enum FallbackAllocator implements ModelAllocator { @@ -7,7 +8,7 @@ public enum FallbackAllocator implements ModelAllocator { @Override public BufferedModel alloc(Model model, Callback allocationCallback) { - IndexedModel out = new IndexedModel(model); + IndexedModel out = new IndexedModel(model, Formats.POS_TEX_NORMAL); allocationCallback.onAlloc(out); return out; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java b/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java index 7a372b0c8..580ded8b5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java @@ -20,6 +20,7 @@ import com.jozufozu.flywheel.core.model.Model; */ public class IndexedModel implements BufferedModel { + protected final VertexType type; protected final Model model; protected final GlPrimitive primitiveMode; protected ElementBuffer ebo; @@ -27,6 +28,11 @@ public class IndexedModel implements BufferedModel { protected boolean deleted; public IndexedModel(Model model) { + this(model, model.getType()); + } + + public IndexedModel(Model model, VertexType type) { + this.type = type; this.model = model; this.primitiveMode = GlPrimitive.TRIANGLES; @@ -38,7 +44,8 @@ public class IndexedModel implements BufferedModel { // mirror it in system memory, so we can write to it, and upload our model. try (MappedBuffer buffer = vbo.getBuffer()) { - model.writeInto(buffer.unwrap()); + type.createWriter(buffer.unwrap()) + .writeVertexList(model.getReader()); } catch (Exception e) { Flywheel.LOGGER.error(String.format("Error uploading model '%s':", model.name()), e); } @@ -52,27 +59,26 @@ public class IndexedModel implements BufferedModel { * The VBO/VAO should be bound externally. */ public void setupState(GlVertexArray vao) { + // XXX ARRAY_BUFFER is bound and not reset or restored vbo.bind(); vao.enableArrays(getAttributeCount()); vao.bindAttributes(0, getType().getLayout()); + ebo.bind(); } @Override public void drawCall() { - ebo.bind(); - GL20.glDrawElements(primitiveMode.glEnum, ebo.elementCount, ebo.eboIndexType.getGlEnum(), 0); + GL20.glDrawElements(primitiveMode.glEnum, ebo.getElementCount(), ebo.getEboIndexType().asGLType, 0); } /** - * Draws many instances of this model, assuming the appropriate state is already bound. - */ + * Draws many instances of this model, assuming the appropriate state is already bound. + */ @Override public void drawInstances(int instanceCount) { if (!valid()) return; - ebo.bind(); - - GL31.glDrawElementsInstanced(primitiveMode.glEnum, ebo.elementCount, ebo.eboIndexType.getGlEnum(), 0, instanceCount); + GL31.glDrawElementsInstanced(primitiveMode.glEnum, ebo.getElementCount(), ebo.getEboIndexType().asGLType, 0, instanceCount); } public boolean isDeleted() { @@ -81,7 +87,7 @@ public class IndexedModel implements BufferedModel { @Override public VertexType getType() { - return model.getType(); + return type; } public int getVertexCount() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java b/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java index 5c1d426c7..3d7d330e5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ModelPool.java @@ -42,6 +42,7 @@ public class ModelPool implements ModelAllocator { vbo = new MappedGlBuffer(GlBufferType.ARRAY_BUFFER); + // XXX ARRAY_BUFFER is bound and not reset or restored vbo.bind(); vbo.setGrowthMargin(stride * 64); } @@ -68,6 +69,7 @@ public class ModelPool implements ModelAllocator { if (dirty) { if (anyToRemove) processDeletions(); + // XXX ARRAY_BUFFER is bound and reset vbo.bind(); if (realloc()) { uploadAll(); @@ -182,25 +184,25 @@ public class ModelPool implements ModelAllocator { @Override public void setupState(GlVertexArray vao) { + // XXX ARRAY_BUFFER is bound and not reset or restored vbo.bind(); vao.enableArrays(getAttributeCount()); vao.bindAttributes(0, vertexType.getLayout()); + ebo.bind(); } @Override public void drawCall() { - GL32.glDrawElementsBaseVertex(GlPrimitive.TRIANGLES.glEnum, ebo.elementCount, ebo.eboIndexType.getGlEnum(), 0, first); + GL32.glDrawElementsBaseVertex(GlPrimitive.TRIANGLES.glEnum, ebo.getElementCount(), ebo.getEboIndexType().asGLType, 0, first); } @Override public void drawInstances(int instanceCount) { if (!valid()) return; - ebo.bind(); - //Backend.log.info(StringUtil.args("drawElementsInstancedBaseVertex", GlPrimitive.TRIANGLES, ebo.elementCount, ebo.eboIndexType, 0, instanceCount, first)); - GL32.glDrawElementsInstancedBaseVertex(GlPrimitive.TRIANGLES.glEnum, ebo.elementCount, ebo.eboIndexType.getGlEnum(), 0, instanceCount, first); + GL32.glDrawElementsInstancedBaseVertex(GlPrimitive.TRIANGLES.glEnum, ebo.getElementCount(), ebo.getEboIndexType().asGLType, 0, instanceCount, first); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/VBOModel.java b/src/main/java/com/jozufozu/flywheel/backend/model/VBOModel.java index 54dccbd28..e51832a63 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/VBOModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/VBOModel.java @@ -58,6 +58,7 @@ public class VBOModel implements BufferedModel { * The VBO/VAO should be bound externally. */ public void setupState(GlVertexArray vao) { + // XXX ARRAY_BUFFER is bound and not reset or restored vbo.bind(); vao.enableArrays(getAttributeCount()); vao.bindAttributes(0, getLayout()); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java index e8832841b..18f484ef5 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java @@ -35,7 +35,7 @@ public class FlwConfig { protected final Map> optionMapView = Collections.unmodifiableMap(optionMap); /** Select the backend to use. */ - public final EnumOption backend = addOption(new EnumOption<>("engine", BackendType.INSTANCING)); + public final EnumOption backend = addOption(new EnumOption<>("backend", BackendType.INSTANCING)); /** Enable or disable a debug overlay that colors pixels by their normal. */ public final BooleanOption debugNormals = addOption(new BooleanOption("debugNormals", false)); /** Enable or disable instance update limiting with distance. */ diff --git a/src/main/java/com/jozufozu/flywheel/core/PartialModel.java b/src/main/java/com/jozufozu/flywheel/core/PartialModel.java index 3e1fd338f..a59317f8f 100644 --- a/src/main/java/com/jozufozu/flywheel/core/PartialModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/PartialModel.java @@ -20,12 +20,12 @@ import net.minecraft.server.packs.resources.ResourceManagerReloadListener; * A helper class for loading and accessing json models. *
* Creating a PartialModel will make the associated modelLocation automatically load. - * PartialModels must be initialized during {@link net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent FMLClientSetupEvent}. + * PartialModels must be initialized the mod class constructor. *
* Once {@link ModelBakeEvent} finishes, all PartialModels (with valid modelLocations) * will have their bakedModel fields populated. *
- * Attempting to create a PartialModel after ModelRegistryEvent will cause an error. + * Attempting to create a PartialModel after {@link ModelRegistryEvent} will cause an error. */ public class PartialModel { diff --git a/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java b/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java index 9cb1fa0b1..266f15489 100644 --- a/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java +++ b/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java @@ -1,37 +1,32 @@ package com.jozufozu.flywheel.core; -import java.nio.Buffer; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.EnumMap; -import java.util.Map; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import org.lwjgl.system.MemoryStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.lwjgl.opengl.GL32; +import org.lwjgl.opengl.GL32C; import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.backend.gl.GlNumericType; -import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; -import com.jozufozu.flywheel.backend.gl.buffer.MappedGlBuffer; +import com.jozufozu.flywheel.backend.gl.buffer.GlBufferUsage; import com.jozufozu.flywheel.backend.model.ElementBuffer; import com.jozufozu.flywheel.event.ReloadRenderersEvent; +import com.mojang.blaze3d.vertex.VertexFormat; + +import it.unimi.dsi.fastutil.ints.Int2ReferenceArrayMap; +import it.unimi.dsi.fastutil.ints.Int2ReferenceMap; /** * A class to manage EBOs that index quads as triangles. */ public class QuadConverter { - public static final int STARTING_CAPACITY = 42; // 255 / 6 = 42 - private static QuadConverter INSTANCE; - @Nonnull + @NotNull public static QuadConverter getInstance() { if (INSTANCE == null) { - INSTANCE = new QuadConverter(STARTING_CAPACITY); + INSTANCE = new QuadConverter(); } return INSTANCE; @@ -42,129 +37,79 @@ public class QuadConverter { return INSTANCE; } - Map ebos; - int[] capacities; + private final Int2ReferenceMap cache = new Int2ReferenceArrayMap<>(); + private final int ebo; + private int quadCapacity; - public QuadConverter(int initialCapacity) { - this.ebos = new EnumMap<>(GlNumericType.class); - initCapacities(); - - fillBuffer(initialCapacity); + public QuadConverter() { + this.ebo = GL32.glGenBuffers(); + this.quadCapacity = 0; } public ElementBuffer quads2Tris(int quads) { - int indexCount = quads * 6; - GlNumericType type = getSmallestIndexType(indexCount); - - if (quads > getCapacity(type)) { - fillBuffer(quads, indexCount, type); + if (quads > quadCapacity) { + grow(quads * 2); } - return new ElementBuffer(getBuffer(type), indexCount, type); + return cache.computeIfAbsent(quads, this::createElementBuffer); } - private void initCapacities() { - this.capacities = new int[GlNumericType.values().length]; + @NotNull + private ElementBuffer createElementBuffer(int quads) { + return new ElementBuffer(ebo, quads * 6, VertexFormat.IndexType.INT); } - private int getCapacity(GlNumericType type) { - return capacities[type.ordinal()]; - } + private void grow(int quads) { + int byteSize = quads * 6 * GlNumericType.UINT.getByteWidth(); + final long ptr = MemoryUtil.nmemAlloc(byteSize); - private void updateCapacity(GlNumericType type, int capacity) { - if (getCapacity(type) < capacity) { - capacities[type.ordinal()] = capacity; - } + fillBuffer(ptr, quads); + + // XXX ARRAY_BUFFER is bound and reset + final var bufferType = GlBufferType.ARRAY_BUFFER; + final int oldBuffer = bufferType.getBoundBuffer(); + bufferType.bind(ebo); + GL32C.nglBufferData(bufferType.glEnum, byteSize, ptr, GlBufferUsage.STATIC_DRAW.glEnum); + bufferType.bind(oldBuffer); + + MemoryUtil.nmemFree(ptr); + + this.quadCapacity = quads; } public void delete() { - ebos.values() - .forEach(GlBuffer::delete); - ebos.clear(); - initCapacities(); + GL32.glDeleteBuffers(ebo); + this.cache.clear(); + this.quadCapacity = 0; } - private void fillBuffer(int quads) { - int indexCount = quads * 6; + private void fillBuffer(long ptr, int quads) { + int numVertices = 4 * quads; + int baseVertex = 0; + while (baseVertex < numVertices) { + writeQuadIndicesUnsafe(ptr, baseVertex); - fillBuffer(quads, indexCount, getSmallestIndexType(indexCount)); - } - - private void fillBuffer(int quads, int indexCount, GlNumericType type) { - MemoryStack stack = MemoryStack.stackPush(); - int bytes = indexCount * type.getByteWidth(); - - ByteBuffer indices; - if (bytes > stack.getSize()) { - indices = MemoryUtil.memAlloc(bytes); // not enough space on the preallocated stack - } else { - stack.push(); - indices = stack.malloc(bytes); + baseVertex += 4; + ptr += 6 * 4; } - - indices.order(ByteOrder.nativeOrder()); - - fillBuffer(indices, type, quads); - - GlBuffer buffer = getBuffer(type); - - buffer.bind(); - buffer.upload(indices); - buffer.unbind(); - - if (bytes > stack.getSize()) { - MemoryUtil.memFree(indices); - } else { - stack.pop(); - } - - updateCapacity(type, quads); } - private void fillBuffer(ByteBuffer indices, GlNumericType type, int quads) { - for (int i = 0, max = 4 * quads; i < max; i += 4) { - // triangle a - type.castAndBuffer(indices, i); - type.castAndBuffer(indices, i + 1); - type.castAndBuffer(indices, i + 2); - // triangle b - type.castAndBuffer(indices, i); - type.castAndBuffer(indices, i + 2); - type.castAndBuffer(indices, i + 3); - } - ((Buffer) indices).flip(); + private void writeQuadIndicesUnsafe(long ptr, int baseVertex) { + // triangle a + MemoryUtil.memPutInt(ptr, baseVertex); + MemoryUtil.memPutInt(ptr + 4, baseVertex + 1); + MemoryUtil.memPutInt(ptr + 8, baseVertex + 2); + // triangle b + MemoryUtil.memPutInt(ptr + 12, baseVertex); + MemoryUtil.memPutInt(ptr + 16, baseVertex + 2); + MemoryUtil.memPutInt(ptr + 20, baseVertex + 3); } - private GlBuffer getBuffer(GlNumericType type) { - return ebos.computeIfAbsent(type, $ -> new MappedGlBuffer(GlBufferType.ELEMENT_ARRAY_BUFFER)); - } - - /** - * Given the needed number of indices, what is the smallest bit width type that can index everything?
- * - *

-	 * | indexCount   | type  |
-	 * |--------------|-------|
-	 * | [0, 255)     | byte  |
-	 * | [256, 65536)	| short	|
-	 * | [65537, )	| int	|
-	 * 
- */ - private static GlNumericType getSmallestIndexType(int indexCount) { -// indexCount = indexCount >>> 8; -// if (indexCount == 0) { -// return GlNumericType.UBYTE; -// } -// indexCount = indexCount >>> 8; -// if (indexCount == 0) { -// return GlNumericType.USHORT; -// } - - return GlNumericType.UINT; - } - - // make sure this gets reset first so it has a chance to repopulate + // make sure this gets reset first, so it has a chance to repopulate public static void onRendererReload(ReloadRenderersEvent event) { - if (INSTANCE != null) INSTANCE.delete(); + if (INSTANCE != null) { + INSTANCE.delete(); + INSTANCE = null; + } } } diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java index ee113de17..0378b6ee6 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -18,6 +18,7 @@ public class CrumblingGroup

extends InstancedMateria super(owner, type); } + // XXX See notes of overriden method @Override public void render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { type.setupRenderState(); diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java index 9986ee120..d524ee38b 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingRenderer.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.SortedSet; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.GlTextureUnit; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.SerialTaskEngine; @@ -16,6 +17,7 @@ import com.jozufozu.flywheel.mixin.LevelRendererAccessor; import com.jozufozu.flywheel.util.Lazy; import com.jozufozu.flywheel.util.Pair; import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -30,6 +32,7 @@ import net.minecraft.client.resources.model.ModelBakery; import net.minecraft.core.BlockPos; import net.minecraft.server.level.BlockDestructionProgress; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.phys.Vec3; /** * Responsible for rendering the block breaking overlay for instanced block entities. @@ -48,13 +51,21 @@ public class CrumblingRenderer { INVALIDATOR = state.second(); } - public static void renderBreaking(RenderLayerEvent event) { - if (!Backend.canUseInstancing(event.getWorld())) return; - - Int2ObjectMap> activeStages = getActiveStageBlockEntities(event.getWorld()); + public static void render(ClientLevel level, Camera camera, PoseStack stack) { + if (!Backend.canUseInstancing(level)) return; + Int2ObjectMap> activeStages = getActiveStageBlockEntities(level); if (activeStages.isEmpty()) return; + Vec3 cameraPos = camera.getPosition(); + + // XXX Restore state + GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); + CrumblingRenderer.renderBreaking(activeStages, new RenderLayerEvent(level, null, stack, null, cameraPos.x, cameraPos.y, cameraPos.z)); + restoreState.restore(); + } + + private static void renderBreaking(Int2ObjectMap> activeStages, RenderLayerEvent event) { State state = STATE.get(); InstanceManager instanceManager = state.instanceManager; InstancingEngine materials = state.materialManager; @@ -71,6 +82,7 @@ public class CrumblingRenderer { instanceManager.beginFrame(SerialTaskEngine.INSTANCE, info); + // XXX Each call applies another restore state even though we are already inside of a restore state materials.render(SerialTaskEngine.INSTANCE, event); instanceManager.invalidate(); @@ -78,6 +90,9 @@ public class CrumblingRenderer { } + // XXX Inconsistent GL state cleanup + // If texture binding and active unit need to be restored, store them in variables before GL state is changed + // instead of guessing that unit 0 and crumbling tex 0 are correct GlTextureUnit.T0.makeActive(); AbstractTexture breaking = textureManager.getTexture(ModelBakery.BREAKING_LOCATIONS.get(0)); if (breaking != null) RenderSystem.bindTexture(breaking.getId()); diff --git a/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java b/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java index 67bdeb798..25546213e 100644 --- a/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java +++ b/src/main/java/com/jozufozu/flywheel/core/hardcoded/ModelPart.java @@ -3,6 +3,7 @@ package com.jozufozu.flywheel.core.hardcoded; import java.util.List; import com.jozufozu.flywheel.api.vertex.VertexList; +import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.model.Model; import com.jozufozu.flywheel.core.vertex.PosTexNormalWriterUnsafe; @@ -51,4 +52,20 @@ public class ModelPart implements Model { public VertexList getReader() { return reader; } + + @Override + public VertexType getType() { + return Formats.POS_TEX_NORMAL; + } + + @Override + public void delete() { + if (reader instanceof AutoCloseable closeable) { + try { + closeable.close(); + } catch (Exception e) { + // + } + } + } } 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 8c527bea6..76e23da8f 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/BlockModel.java @@ -1,6 +1,7 @@ package com.jozufozu.flywheel.core.model; import com.jozufozu.flywheel.api.vertex.VertexList; +import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.core.Formats; import com.mojang.blaze3d.vertex.PoseStack; @@ -12,10 +13,8 @@ import net.minecraft.world.level.block.state.BlockState; * A model of a single block. */ public class BlockModel implements Model { - private static final PoseStack IDENTITY = new PoseStack(); private final VertexList reader; - private final String name; public BlockModel(BlockState state) { @@ -25,12 +24,21 @@ public class BlockModel implements Model { } public BlockModel(BakedModel model, BlockState referenceState) { - this(model, referenceState, IDENTITY); + this(new BakedModelBuilder(model).withReferenceState(referenceState), referenceState.toString()); } public BlockModel(BakedModel model, BlockState referenceState, PoseStack ms) { - reader = Formats.BLOCK.createReader(ModelUtil.getBufferBuilder(model, referenceState, ms)); - name = referenceState.toString(); + this(new BakedModelBuilder(model).withReferenceState(referenceState) + .withPoseStack(ms), referenceState.toString()); + } + + public BlockModel(Bufferable bufferable, String name) { + this(bufferable.build(), name); + } + + public BlockModel(ShadeSeparatedBufferBuilder bufferBuilder, String name) { + this.name = name; + reader = Formats.BLOCK.createReader(bufferBuilder); } @Override @@ -47,4 +55,20 @@ public class BlockModel implements Model { public VertexList getReader() { return reader; } + + @Override + public VertexType getType() { + return Formats.BLOCK; + } + + @Override + public void delete() { + if (reader instanceof AutoCloseable closeable) { + try { + closeable.close(); + } catch (Exception e) { + // + } + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/Model.java b/src/main/java/com/jozufozu/flywheel/core/model/Model.java index eacfd3394..b0d31a15f 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/Model.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/Model.java @@ -5,7 +5,6 @@ import java.nio.ByteBuffer; import com.jozufozu.flywheel.api.vertex.VertexList; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.model.ElementBuffer; -import com.jozufozu.flywheel.core.Formats; import com.jozufozu.flywheel.core.QuadConverter; /** @@ -42,10 +41,9 @@ public interface Model { */ int vertexCount(); - default VertexType getType() { - return Formats.POS_TEX_NORMAL; - } + VertexType getType(); + // XXX Since this is public API (technically) we cannot make assumptions about what GL state this method can use or modify unless a contract is established. /** * Create an element buffer object that indexes the vertices of this model. * @@ -61,6 +59,8 @@ public interface Model { .quads2Tris(vertexCount() / 4); } + void delete(); + /** * The size in bytes that this model's data takes up. */ diff --git a/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java b/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java deleted file mode 100644 index c144f43c6..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/model/WorldModel.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.jozufozu.flywheel.core.model; - -import com.jozufozu.flywheel.api.vertex.VertexList; -import com.jozufozu.flywheel.api.vertex.VertexType; -import com.jozufozu.flywheel.core.Formats; -import com.mojang.blaze3d.vertex.BufferBuilder; - -public class WorldModel implements Model { - - private final VertexList reader; - private final String name; - - public WorldModel(BufferBuilder bufferBuilder, String name) { - this.reader = Formats.BLOCK.createReader(bufferBuilder); - this.name = name; - } - - @Override - public String name() { - return name; - } - - @Override - public VertexType getType() { - return Formats.BLOCK; - } - - @Override - public int vertexCount() { - return reader.getVertexCount(); - } - - @Override - public VertexList getReader() { - return reader; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java index 83f760618..db7313c16 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/WorldModelBuilder.java @@ -86,7 +86,7 @@ public final class WorldModelBuilder implements Bufferable { return this; } - public WorldModel intoMesh(String name) { - return new WorldModel(ModelUtil.getBufferBuilder(this), name); + public BlockModel intoMesh(String name) { + return new BlockModel(ModelUtil.getBufferBuilder(this), name); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java b/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java index a68e0c943..97475da4c 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/AbstractVertexList.java @@ -7,7 +7,6 @@ import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.api.vertex.VertexList; import com.mojang.blaze3d.platform.MemoryTracker; -import com.mojang.blaze3d.vertex.BufferBuilder; public abstract class AbstractVertexList implements VertexList, AutoCloseable { @@ -22,15 +21,6 @@ public abstract class AbstractVertexList implements VertexList, AutoCloseable { init(copyFrom); } - public AbstractVertexList(BufferBuilder builder) { - var pair = builder.popNextBuffer(); - ByteBuffer copyFrom = pair.getSecond(); - this.contents = MemoryTracker.create(copyFrom.capacity()); - this.vertexCount = pair.getFirst().vertexCount(); - this.base = MemoryUtil.memAddress(this.contents); - init(copyFrom); - } - private void init(ByteBuffer copyFrom) { this.contents.order(copyFrom.order()); this.contents.put(copyFrom); diff --git a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java index 2955b5dc2..ef46bdf36 100644 --- a/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java +++ b/src/main/java/com/jozufozu/flywheel/core/vertex/BlockVertexList.java @@ -1,19 +1,17 @@ package com.jozufozu.flywheel.core.vertex; +import java.nio.ByteBuffer; + import com.jozufozu.flywheel.api.vertex.ShadedVertexList; -import com.jozufozu.flywheel.core.model.ShadeSeparatedBufferBuilder; -import com.jozufozu.flywheel.fabric.helper.BufferBuilderHelper; import com.jozufozu.flywheel.util.RenderMath; -import com.mojang.blaze3d.vertex.BufferBuilder; public class BlockVertexList extends AbstractVertexList { private final int stride; - public BlockVertexList(BufferBuilder builder) { - super(builder); - this.stride = BufferBuilderHelper.getVertexFormat(builder) - .getVertexSize(); + public BlockVertexList(ByteBuffer copyFrom, int vertexCount, int stride) { + super(copyFrom, vertexCount); + this.stride = stride; } @Override @@ -94,9 +92,9 @@ public class BlockVertexList extends AbstractVertexList { private final int unshadedStartVertex; - public Shaded(ShadeSeparatedBufferBuilder builder) { - super(builder); - unshadedStartVertex = builder.getUnshadedStartVertex(); + public Shaded(ByteBuffer copyFrom, int vertexCount, int stride, int unshadedStartVertex) { + super(copyFrom, vertexCount, stride); + this.unshadedStartVertex = unshadedStartVertex; } @Override diff --git a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java index 557721b19..df3fe4873 100644 --- a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java @@ -13,7 +13,6 @@ import net.minecraft.client.multiplayer.ClientLevel; public class ForgeEvents { public static void addToDebugScreen(List right) { - InstancedRenderDispatcher.getDebugString(right); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java deleted file mode 100644 index 7cd63581c..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/BufferUploaderMixin.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.jozufozu.flywheel.mixin; - -import javax.annotation.Nullable; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.jozufozu.flywheel.backend.gl.GlStateTracker; -import com.mojang.blaze3d.vertex.BufferUploader; -import com.mojang.blaze3d.vertex.VertexFormat; - -@Mixin(BufferUploader.class) -public class BufferUploaderMixin { - - @Shadow - @Nullable - private static VertexFormat lastFormat; - - @Inject(method = "reset", at = @At("HEAD")) - private static void stopBufferUploaderFromClearingBufferStateIfNothingIsBound(CallbackInfo ci) { - // Trust our tracker over BufferUploader's. - if (GlStateTracker.getVertexArray() == 0) { - lastFormat = null; - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/ClientMainMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/ClientMainMixin.java new file mode 100644 index 000000000..98e26e2ed --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/ClientMainMixin.java @@ -0,0 +1,28 @@ +package com.jozufozu.flywheel.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.main.Main; + +@Mixin(value = Main.class, remap = false) +public class ClientMainMixin { + + @Inject(method = "main", at = @At("HEAD")) + private static void injectRenderDoc(CallbackInfo ci) { + // Only try to load RenderDoc if a system property is set. + if (System.getProperty("flw.loadRenderDoc") == null) { + return; + } + + try { + System.loadLibrary("renderdoc"); + } catch (Throwable ignored) { + // Oh well, we tried. + // On Windows, RenderDoc installs to "C:\Program Files\RenderDoc\" + System.err.println("Is RenderDoc in your PATH?"); + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java index e51bc2127..022925705 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java @@ -3,21 +3,17 @@ package com.jozufozu.flywheel.mixin; 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 org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Group; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.core.crumbling.CrumblingRenderer; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.fabric.event.FlywheelEvents; -import com.mojang.blaze3d.vertex.BufferUploader; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; @@ -31,7 +27,6 @@ import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.culling.Frustum; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.Vec3; @Mixin(value = LevelRenderer.class, priority = 1001) // Higher priority to go after sodium public class LevelRendererMixin { @@ -48,41 +43,9 @@ public class LevelRendererMixin { FlywheelEvents.BEGIN_FRAME.invoker().handleEvent(new BeginFrameEvent(level, camera, frustum)); } - @Unique - private boolean flywheel$LayerRendered; - - /** - * This only gets injected if renderChunkLayer is not Overwritten - */ - @Group(name = "flywheel$renderLayer", min = 1, max = 2) - @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/ShaderInstance;clear()V"), method = "renderChunkLayer") - private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) { - flywheel$renderLayer(type, stack, camX, camY, camZ); - flywheel$LayerRendered = true; - } - - /** - * This always gets injected. - */ - @Group(name = "flywheel$renderLayer") @Inject(at = @At("TAIL"), method = "renderChunkLayer") - private void renderLayerSodium(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f p_172999_, CallbackInfo ci) { - if (!flywheel$LayerRendered) { - flywheel$renderLayer(type, stack, camX, camY, camZ); - } - flywheel$LayerRendered = false; - BufferUploader.reset(); - } - - @Unique - private void flywheel$renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ) { - RenderBuffers renderBuffers = this.renderBuffers; - - GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); - + private void renderLayer(RenderType type, PoseStack stack, double camX, double camY, double camZ, Matrix4f projection, CallbackInfo ci) { FlywheelEvents.RENDER_LAYER.invoker().handleEvent(new RenderLayerEvent(level, type, stack, renderBuffers, camX, camY, camZ)); - - restoreState.restore(); } @Inject(at = @At("TAIL"), method = "allChanged") @@ -92,17 +55,10 @@ public class LevelRendererMixin { FlywheelEvents.RELOAD_RENDERERS.invoker().handleEvent(new ReloadRenderersEvent(level)); } - @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;checkPoseStack(Lcom/mojang/blaze3d/vertex/PoseStack;)V", ordinal = 2 // after the game renders the breaking overlay normally ), method = "renderLevel") - private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera info, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) { - if (!Backend.isOn()) return; - - Vec3 cameraPos = info.getPosition(); - - GlStateTracker.State restoreState = GlStateTracker.getRestoreState(); - CrumblingRenderer.renderBreaking(new RenderLayerEvent(level, null, stack, null, cameraPos.x, cameraPos.y, cameraPos.z)); - restoreState.restore(); + private void renderBlockBreaking(PoseStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) { + CrumblingRenderer.render(level, camera, stack); } // Instancing diff --git a/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java index c3a3d7b5d..d31b71899 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/RenderTypeMixin.java @@ -14,11 +14,14 @@ import net.minecraft.client.renderer.RenderType; public class RenderTypeMixin implements RenderTypeExtension { @Unique - private final DrawBuffer flywheel$drawBuffer = new DrawBuffer((RenderType) (Object) this); + private DrawBuffer flywheel$drawBuffer; @Override @Nonnull public DrawBuffer flywheel$getDrawBuffer() { + if (flywheel$drawBuffer == null) { + flywheel$drawBuffer = new DrawBuffer((RenderType) (Object) this); + } return flywheel$drawBuffer; } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/ChunkRebuildHooksMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java similarity index 95% rename from src/main/java/com/jozufozu/flywheel/mixin/ChunkRebuildHooksMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java index a12f4ad07..e6b0f404a 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/ChunkRebuildHooksMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.mixin; +package com.jozufozu.flywheel.mixin.instancemanage; import java.util.Set; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/InstanceAddMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java similarity index 95% rename from src/main/java/com/jozufozu/flywheel/mixin/InstanceAddMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java index 1a0552aa5..0010aa106 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/InstanceAddMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.mixin; +package com.jozufozu.flywheel.mixin.instancemanage; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/InstanceRemoveMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java similarity index 96% rename from src/main/java/com/jozufozu/flywheel/mixin/InstanceRemoveMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java index 0ee0fd088..1d115cfda 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/InstanceRemoveMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.mixin; +package com.jozufozu.flywheel.mixin.instancemanage; import javax.annotation.Nullable; diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 7669c0194..4be4fc254 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -7,16 +7,13 @@ "BlockEntityRenderDispatcherAccessor", "BlockEntityTypeMixin", "BufferBuilderMixin", - "BufferUploaderMixin", "CameraMixin", "ClientLevelMixin", - "ChunkRebuildHooksMixin", + "ClientMainMixin", "EntityTypeMixin", "FixFabulousDepthMixin", "FrustumMixin", "GlStateManagerMixin", - "InstanceAddMixin", - "InstanceRemoveMixin", "LevelRendererAccessor", "LevelRendererMixin", "PausedPartialTickAccessor", @@ -24,6 +21,9 @@ "RenderTypeMixin", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor", + "instancemanage.ChunkRebuildHooksMixin", + "instancemanage.InstanceAddMixin", + "instancemanage.InstanceRemoveMixin", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin", "matrix.Matrix3fMixin",