diff --git a/dependencies.gradle.kts b/dependencies.gradle.kts index 25d122c99..45a0e0fd6 100644 --- a/dependencies.gradle.kts +++ b/dependencies.gradle.kts @@ -62,6 +62,7 @@ DependencyHandlerScope.of(dependencies).apply { runtimeOnlyNonPublishable("com.github.GTNewHorizons:BetterLoadingScreen:1.7.9-GTNH:dev") runtimeOnlyNonPublishable("com.github.GTNewHorizons:BetterQuesting:3.8.72-GTNH:dev") runtimeOnlyNonPublishable("com.github.GTNewHorizons:BuildCraft:7.1.63:dev") + compileOnly("com.github.GTNewHorizons:Botania:1.13.32-GTNH:dev") { isTransitive = false } runtimeOnlyNonPublishable("com.github.GTNewHorizons:Botania:1.13.32-GTNH:dev") runtimeOnlyNonPublishable("com.github.GTNewHorizons:LogisticsPipes:1.5.32-GTNH:dev") runtimeOnlyNonPublishable("com.github.GTNewHorizons:OpenComputers:1.12.64-GTNH:dev") diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java b/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java index 38fdcb019..8efc962db 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java @@ -69,7 +69,13 @@ public final class SystemProperties { public static final boolean FLYBY_JFR = Boolean.getBoolean("angelica.flyby.jfr"); public static final float FLYBY_PITCH = parseFloat("angelica.flyby.pitch"); public static final int FLYBY_SCREENSHOTS = Integer.getInteger("angelica.flyby.screenshots", 0); - public static final boolean FLYBY_DEBUG_HUD = Boolean.getBoolean("angelica.flyby.debugHud"); + public static final Boolean FLYBY_DEBUG_HUD = parseBooleanOverride("angelica.flyby.debugHud"); + public static final String FLYBY_DEBUG_PROFILE = System.getProperty("angelica.flyby.debugProfile", ""); + public static final String FLYBY_DEBUG_ENTRIES = System.getProperty("angelica.flyby.debugEntries", ""); + public static final Boolean FLYBY_DEBUG_FLAT = parseBooleanOverride("angelica.flyby.debugFlat"); + public static final Boolean FLYBY_HUD_CACHING = parseBooleanOverride("angelica.flyby.hudCaching"); + public static final Boolean FLYBY_FPS_GRAPH = parseBooleanOverride("angelica.flyby.fpsGraph"); + public static final boolean FLYBY_DEBUG_DUMP = Boolean.getBoolean("angelica.flyby.debugDump"); public static final String FLYBY_CRASH_TEST = System.getProperty("angelica.flyby.crashTest", ""); // Debug diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java index 6741dee65..9d0d26932 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java @@ -42,6 +42,9 @@ import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetAddress; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetInt; +import static com.gtnewhorizon.gtnhlib.bytebuf.Pointer.POINTER_SIZE; import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL; /** @@ -380,7 +383,9 @@ public final class Lwjgl2GLRenderBackend extends RenderBackend { @Override public void multiDrawElementsBaseVertex(int mode, long pCount, int type, long pIndices, int drawcount, long pBaseVertex) { - throw new UnsupportedOperationException("GLRenderBackend.multiDrawElementsBaseVertex should not be called — LWJGL service handles this"); + for (int i = 0; i < drawcount; i++) { + GL32.glDrawElementsBaseVertex(mode, memGetInt(pCount + 4L * i), type, memGetAddress(pIndices + (long) i * POINTER_SIZE), memGetInt(pBaseVertex + 4L * i)); + } } @Override diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java index 3309643fd..b133f7459 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/RenderBackend.java @@ -26,6 +26,8 @@ import java.util.function.Predicate; */ public abstract class RenderBackend { + public static final int MAX_FRAMES_IN_FLIGHT = 3; + public void onPostWindowCreate(long window) {} public abstract void init(); diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/VertexShaderGenerator.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/VertexShaderGenerator.java index 59a430612..d96ecf935 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/VertexShaderGenerator.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/ffp/VertexShaderGenerator.java @@ -1,6 +1,7 @@ package com.gtnewhorizons.angelica.glsm.ffp; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; /** * Generates GLSL 330 core vertex shaders for FFP emulation. @@ -19,6 +20,9 @@ public final class VertexShaderGenerator { emitAttributes(sb, key); emitUniforms(sb, key); emitOutputs(sb, key); + if (key.lineQuads()) { + sb.append(ShaderLoader.getShaderSource("angelica:include/line_quads.glsl")).append('\n'); + } sb.append("void main() {\n"); if (key.instancing() == Instancing.WEATHER) { @@ -142,7 +146,7 @@ public final class VertexShaderGenerator { } else { sb.append(" gl_Position = u_MVPMatrix * pos4;\n"); if (key.lineQuads()) { - emitLineQuadWidening(sb); + emitLineQuadWidening(sb, key); } // Eye position needed for lighting, fog, EYE_LINEAR texgen, and clip planes @@ -156,42 +160,15 @@ public final class VertexShaderGenerator { sb.append('\n'); } - private static void emitLineQuadWidening(StringBuilder sb) { + private static void emitLineQuadWidening(StringBuilder sb, VertexKey key) { sb.append(" int lineCorner = gl_VertexID % 6;\n"); - sb.append(" bool lineStart = lineCorner == 0 || lineCorner == 1 || lineCorner == 3;\n"); + sb.append(" bool lineStart = angelica_lineStart(lineCorner);\n"); sb.append(" vec4 lineOther = u_MVPMatrix * vec4(a_LineOtherEnd, 1.0);\n"); - sb.append(" vec4 lineC0 = lineStart ? gl_Position : lineOther;\n"); - sb.append(" vec4 lineC1 = lineStart ? lineOther : gl_Position;\n"); - sb.append(" vec4 lineCenter = lineC0;\n"); - sb.append(" float lineT = 0.0;\n"); - sb.append(" float lineD0 = lineC0.z + lineC0.w;\n"); - sb.append(" float lineD1 = lineC1.z + lineC1.w;\n"); - sb.append(" if (lineD0 < 0.0 && lineD1 < 0.0) {\n"); - sb.append(" gl_Position = vec4(0.0, 0.0, 2.0, 1.0);\n"); - sb.append(" } else {\n"); - sb.append(" float lineT0 = lineD0 < 0.0 ? lineD0 / (lineD0 - lineD1) : 0.0;\n"); - sb.append(" float lineT1 = lineD1 < 0.0 ? lineD0 / (lineD0 - lineD1) : 1.0;\n"); - sb.append(" lineT = lineStart ? lineT0 : lineT1;\n"); - sb.append(" vec4 lineP0 = mix(lineC0, lineC1, lineT0);\n"); - sb.append(" vec4 lineP1 = mix(lineC0, lineC1, lineT1);\n"); - sb.append(" vec2 lineN0 = lineP0.xy / lineP0.w;\n"); - sb.append(" vec2 lineN1 = lineP1.xy / lineP1.w;\n"); - sb.append(" vec2 linePixel = 2.0 / u_ViewportSize;\n"); - sb.append(" float lineHalfWidth = 0.5 * u_LineWidth;\n"); - sb.append(" vec2 lineOffset;\n"); - sb.append(" vec2 lineShift;\n"); - sb.append(" vec2 lineSpan = abs(lineN1 - lineN0) * u_ViewportSize;\n"); - sb.append(" if (lineSpan.x > lineSpan.y) {\n"); - sb.append(" lineOffset = vec2(0.0, lineHalfWidth * linePixel.y);\n"); - sb.append(" lineShift = vec2(lineN0.x < lineN1.x ? -0.5 : 0.5, -0.125) * linePixel;\n"); - sb.append(" } else {\n"); - sb.append(" lineOffset = vec2(lineHalfWidth * linePixel.x, 0.0);\n"); - sb.append(" lineShift = vec2(0.125, lineN0.y < lineN1.y ? -0.5 : 0.5) * linePixel;\n"); - sb.append(" }\n"); - sb.append(" vec4 lineP = lineStart ? lineP0 : lineP1;\n"); - sb.append(" vec2 lineN = lineStart ? lineN0 : lineN1;\n"); - sb.append(" gl_Position = vec4((lineN + (lineCorner % 2 == 0 ? lineOffset : -lineOffset) + lineShift) * lineP.w, lineP.z, lineP.w);\n"); - sb.append(" }\n"); + if (key.lineStipple()) { + sb.append(" vec4 lineCenter = lineStart ? gl_Position : lineOther;\n"); + } + sb.append(" float lineT;\n"); + sb.append(" gl_Position = angelica_lineQuad(gl_Position, lineOther, lineCorner, u_ViewportSize, u_LineWidth, lineT);\n"); } private static void emitNormalTransform(StringBuilder sb, VertexKey key) { diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/DisplayListVBO.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/DisplayListVBO.java index 193ab7320..177c00cb8 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/DisplayListVBO.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/DisplayListVBO.java @@ -1,7 +1,6 @@ package com.gtnewhorizons.angelica.glsm.recording; import com.gtnewhorizon.gtnhlib.client.renderer.vao.IVertexArrayObject; -import com.gtnewhorizons.angelica.glsm.GLContextState; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.hooks.ImmediateExtendedAttribHandler; import com.gtnewhorizons.angelica.glsm.streaming.LineQuads; @@ -122,18 +121,14 @@ public final class DisplayListVBO { } private void renderLineQuads() { - final GLContextState glCtx = GLStateManager.ctx(); lineQuads.bind(); - VAOManager.setCurrentVertexFlags(lineQuadFlags); - final boolean culled = LineQuads.disableCulling(); - glCtx.lineQuadsActive = true; + final boolean culled = LineQuads.beginDraw(lineQuadFlags); FfpExtendedAttribs.beginInternalDraw(); try { lineQuads.draw(GL11.GL_TRIANGLES, lineQuadStart, lineQuadCount); } finally { FfpExtendedAttribs.endInternalDraw(); - glCtx.lineQuadsActive = false; - LineQuads.restoreCulling(culled); + LineQuads.endDraw(culled); } lineQuads.unbind(); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/streaming/LineQuads.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/streaming/LineQuads.java index bdfc55757..1b30770c7 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/streaming/LineQuads.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/streaming/LineQuads.java @@ -5,6 +5,7 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.ffp.VAOManager; import org.lwjgl.opengl.GL11; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memCopy; @@ -72,6 +73,18 @@ public final class LineQuads { } } + public static boolean beginDraw(int vertexFlags) { + VAOManager.setCurrentVertexFlags(vertexFlags); + final boolean culled = disableCulling(); + GLStateManager.ctx().lineQuadsActive = true; + return culled; + } + + public static void endDraw(boolean culled) { + GLStateManager.ctx().lineQuadsActive = false; + restoreCulling(culled); + } + public static VertexFormat quadFormat(VertexFormat lineFormat) { return QUAD_FORMATS[lineFormat.getVertexFlags() | VertexFlags.NORMAL_BIT]; } diff --git a/glsm/src/main/resources/assets/angelica/shaders/include/line_quads.glsl b/glsm/src/main/resources/assets/angelica/shaders/include/line_quads.glsl new file mode 100644 index 000000000..c59195887 --- /dev/null +++ b/glsm/src/main/resources/assets/angelica/shaders/include/line_quads.glsl @@ -0,0 +1,38 @@ +bool angelica_lineStart(int corner) { + return corner == 0 || corner == 1 || corner == 3; +} + +vec4 angelica_lineQuad(vec4 clipThis, vec4 clipOther, int corner, vec2 viewport, float width, out float lineT) { + bool lineStart = angelica_lineStart(corner); + vec4 lineC0 = lineStart ? clipThis : clipOther; + vec4 lineC1 = lineStart ? clipOther : clipThis; + lineT = 0.0; + float lineD0 = lineC0.z + lineC0.w; + float lineD1 = lineC1.z + lineC1.w; + if (lineD0 < 0.0 && lineD1 < 0.0) { + return vec4(0.0, 0.0, 2.0, 1.0); + } else { + float lineT0 = lineD0 < 0.0 ? lineD0 / (lineD0 - lineD1) : 0.0; + float lineT1 = lineD1 < 0.0 ? lineD0 / (lineD0 - lineD1) : 1.0; + lineT = lineStart ? lineT0 : lineT1; + vec4 lineP0 = mix(lineC0, lineC1, lineT0); + vec4 lineP1 = mix(lineC0, lineC1, lineT1); + vec2 lineN0 = lineP0.xy / lineP0.w; + vec2 lineN1 = lineP1.xy / lineP1.w; + vec2 linePixel = 2.0 / viewport; + float lineHalfWidth = 0.5 * width; + vec2 lineOffset; + vec2 lineShift; + vec2 lineSpan = abs(lineN1 - lineN0) * viewport; + if (lineSpan.x > lineSpan.y) { + lineOffset = vec2(0.0, lineHalfWidth * linePixel.y); + lineShift = vec2(lineN0.x < lineN1.x ? -0.5 : 0.5, -0.125) * linePixel; + } else { + lineOffset = vec2(lineHalfWidth * linePixel.x, 0.0); + lineShift = vec2(0.125, lineN0.y < lineN1.y ? -0.5 : 0.5) * linePixel; + } + vec4 lineP = lineStart ? lineP0 : lineP1; + vec2 lineN = lineStart ? lineN0 : lineN1; + return vec4((lineN + (corner % 2 == 0 ? lineOffset : -lineOffset) + lineShift) * lineP.w, lineP.z, lineP.w); + } +} diff --git a/scripts/flyby-capture.sh b/scripts/flyby-capture.sh index 3338e9c6a..573cce465 100755 --- a/scripts/flyby-capture.sh +++ b/scripts/flyby-capture.sh @@ -22,6 +22,8 @@ # ALSO_JFR=1 - with a .tracy output, also record a JFR alongside it (timings inflated, correlation only). # ASPROF_OPTS - comma separated async-profiler agent opts, passed through when a JFR is recorded. # +# FLYBY_FULLSCREEN=0 - keep options.txt's window mode. Default: fullscreen:true for the run (avoids vsync pacing), restored on exit. +# # The output extension selects the profiler: .tracy for Tracy, .jfr for an in-process async-profiler # JFR that starts near the end of warmup and stops when the measured run ends. # @@ -32,6 +34,9 @@ set -euo pipefail +# shellcheck source=flyby-lib.sh +. "$(dirname "$0")/flyby-lib.sh" + OUT="${1:?usage: flyby-capture.sh [route] [length] [extra gradle args...]}" ROUTE="${2:-straight}" LENGTH="${3:-0}" @@ -148,12 +153,40 @@ if [ "$MODE" = "tracy" ]; then if kill -0 "$CAPTURE_PID" 2>/dev/null; then echo "==> stopping capture" kill -INT "$CAPTURE_PID" 2>/dev/null || true + local waited=0 + while kill -0 "$CAPTURE_PID" 2>/dev/null && { { [ -e "$OUT" ] && [ "$waited" -lt 600 ]; } || [ "$waited" -lt 120 ]; }; do + sleep 1 + waited=$((waited + 1)) + done + if kill -0 "$CAPTURE_PID" 2>/dev/null; then + echo "==> capture did not stop after ${waited}s (limit 120s before output exists, 600s after), killing it" >&2 + kill -9 "$CAPTURE_PID" 2>/dev/null || true + fi wait "$CAPTURE_PID" 2>/dev/null || true fi } trap cleanup EXIT fi +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +OPTIONS_TXT="$ROOT/run/client/options.txt" +FS_DIR="$(mktemp -d)" + +on_exit() { + if [ -n "$CAPTURE_PID" ]; then + cleanup + fi + if [ "$FULLSCREEN" != 0 ]; then + flyby_fullscreen_restore "$OPTIONS_TXT" "$FS_DIR/options.txt.bak" || true + fi + rm -rf "$FS_DIR" +} +FULLSCREEN="${FLYBY_FULLSCREEN:-1}" +if [ "$FULLSCREEN" != 0 ]; then + flyby_fullscreen_on "$OPTIONS_TXT" "$FS_DIR/options.txt.bak" +fi +trap on_exit EXIT + echo "==> launching game (route=$ROUTE warmup=$WARMUP length=${LENGTH:-default} scene=${SCENE_ABS:-none})" if [ -n "$JFR_OUT" ]; then echo "==> recording JFR -> $JFR_ABS" @@ -178,7 +211,6 @@ fi echo "==> game exited, finalising capture" if [ "$MODE" = "tracy" ]; then cleanup - trap - EXIT fi if [ "$MODE" = "jfr" ]; then diff --git a/scripts/flyby-lib.sh b/scripts/flyby-lib.sh new file mode 100644 index 000000000..3f1b3f9b4 --- /dev/null +++ b/scripts/flyby-lib.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# Shared file helpers for the flyby-*.sh scripts. Sourced, not executed: . "$(dirname "$0")/flyby-lib.sh" +# +# flyby_set_key [sep] replace the first line starting with (default sep ":"), else append; creates the file +# flyby_copy copy bytes and verify with a compare +# flyby_backup_file copy to ; if is absent, remove any stale (records absence) +# flyby_restore_file flyby_backup_file with swapped arguments; prints one error line and returns 1 on failure. +# Trap callers append '|| true'. Removes if is absent: only call it after the backup was made. +# flyby_fullscreen_on backs up, sets fullscreen:true +# flyby_fullscreen_restore restores the backup; idempotent, call only after flyby_fullscreen_on +# flyby_fresh_dir remove and recreate it empty +# flyby_collect ... copy /logs/ and /screenshots/flyby-shot-*.png into +# flyby_crash_list sorted names in , nothing if it is absent +# flyby_new_crashes write the current crash list to , print how many names are not in + +flyby_set_key() { + python3 - "$1" "$2" "$3" "${4:-:}" <<'PY' +import os, re, sys +path, key, value, sep = sys.argv[1:5] +text = "" +if os.path.isfile(path): + with open(path, newline="") as f: + text = f.read() +pat = re.compile(r"^" + re.escape(key + sep) + r"[^\r\n]*", re.M) +line = key + sep + value +if pat.search(text): + text = pat.sub(lambda _: line, text, count=1) +else: + if text and not text.endswith("\n"): + text += "\n" + text += line + "\n" +with open(path, "w", newline="") as f: + f.write(text) +PY +} + +flyby_copy() { + python3 - "$1" "$2" <<'PY' +import filecmp, shutil, sys +src, dst = sys.argv[1:3] +shutil.copyfile(src, dst) +if not filecmp.cmp(src, dst, shallow=False): + sys.exit("error: copy differs: " + dst) +PY +} + +flyby_backup_file() { + python3 - "$1" "$2" <<'PY' +import filecmp, os, shutil, sys +path, backup = sys.argv[1:3] +if os.path.isfile(path): + shutil.copyfile(path, backup) + if not filecmp.cmp(path, backup, shallow=False): + sys.exit("error: backup differs: " + backup) +elif os.path.isfile(backup): + os.remove(backup) +PY +} + +flyby_restore_file() { + flyby_backup_file "$1" "$2" || { echo "error: restore failed or differs: $2" >&2; return 1; } +} + +flyby_fullscreen_on() { + flyby_backup_file "$1" "$2" + flyby_set_key "$1" fullscreen true : +} + +flyby_fullscreen_restore() { + flyby_restore_file "$2" "$1" +} + +flyby_fresh_dir() { + python3 -c 'import shutil, sys; shutil.rmtree(sys.argv[1], ignore_errors=True)' "$1" + mkdir -p "$1" +} + +flyby_collect() { + python3 - "$@" <<'PY' +import glob, os, shutil, sys +run, vdir = sys.argv[1:3] +for name in sys.argv[3:]: + src = os.path.join(run, "logs", name) + if os.path.isfile(src): + shutil.copyfile(src, os.path.join(vdir, name)) +for src in glob.glob(os.path.join(run, "screenshots", "flyby-shot-*.png")): + shutil.copyfile(src, os.path.join(vdir, os.path.basename(src))) +PY +} + +flyby_crash_list() { + if [ -d "$1" ]; then + ls -1 "$1" | sort + fi +} + +flyby_new_crashes() { + flyby_crash_list "$1" >| "$3" + comm -13 "$2" "$3" | wc -l | tr -d ' ' +} diff --git a/scripts/flyby-prism.sh b/scripts/flyby-prism.sh new file mode 100755 index 000000000..62b25ccc4 --- /dev/null +++ b/scripts/flyby-prism.sh @@ -0,0 +1,480 @@ +#!/usr/bin/env bash +# Unattended flyby run of a test Angelica jar inside the real Prism GTNH instance, one launch per variant +# +# Usage: scripts/flyby-prism.sh ... +# Variants: sdl-off | sdl-pack | gl-off | gl-pack (SDL-GPU vs OpenGL x shader pack off vs on) +# gl-pack writes .txt from the pack's profile.HIGH first (never Ultra on OpenGL) and fails before launching without it. +# Output: tmp/prism/// (fml-client-latest.log, flyby-shot-*.png, console from the checker). +# Prints one PASS/FAIL line per variant; exits non-zero if any fails. A run that never reaches the world +# (no 'Flyby: world saving is off' in the log) is FAILED and stops the batch. +# Needs a hands-off machine: the game must keep focus (waitForFocus) and runs fullscreen. +# +# PRISM_INSTANCE - instance id (default: GTNH-daily-2026-10-01+766-mmcprism-java17-26). +# PRISM_BIN - launcher binary (default: /Applications/Prism Launcher.app/Contents/MacOS/prismlauncher). +# PRISM_TIMEOUT - seconds before a variant's game is killed (default: 900). +# PRISM_KEEP_JAR - 1 leaves the test jar installed and the old angelica jar(s) disabled after the script ends (default: 0). +# PRISM_WORLD - save for QuickPlay (default: FlybyGTNH). No spaces. +# PRISM_ENTRIES - id=status,... passed as -Dangelica.flyby.debugEntries on every variant and graded with +# --require-renderers (default: all nine world renderers inOverlay). +# +# Sets fullscreen:true in the instance options.txt per variant (avoids vsync pacing). +# Restores instance.cfg, options.txt, config/shaders.properties and the pack option file on exit (cmp-verified). +# scripts/flyby-prism.sh --helper ... runs one pure step in isolation. + +set -euo pipefail + +# shellcheck source=flyby-lib.sh +. "$(dirname "$0")/flyby-lib.sh" + +HELPER="$(cat <<'PY' +import re, sys + + +def read(path): + with open(path, newline="") as f: + return f.read() + + +def write(path, text): + with open(path, "w", newline="") as f: + f.write(text) + + +def unquote(raw): + if len(raw) >= 2 and raw[0] == '"' and raw[-1] == '"': + return True, raw[1:-1].replace('\\"', '"').replace("\\\\", "\\") + return False, raw + + +def requote(value, quoted): + if quoted or re.search(r"[\s,;\"]", value): + return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"' + return value + + +def key_of(token): + return token.split("=", 1)[0] + + +def jvmargs(cfg, original, backend, extra): + src = read(original) + m = re.search(r"^JvmArgs=([^\r\n]*)", src, re.M) + quoted, value = unquote(m.group(1)) if m else (True, "") + new = ["-Dangelica.sdlgpu.enable=" + backend] + extra + for t in new: + if re.search(r"\s", t) or '"' in t: + raise SystemExit("error: jvm arg contains whitespace or a quote: " + repr(t)) + drop = {key_of(t) for t in new} + kept = [t for t in value.split() if key_of(t) not in drop] + line = "JvmArgs=" + requote(" ".join(kept + new), quoted) + text = read(cfg) + if re.search(r"^JvmArgs=[^\r\n]*", text, re.M): + text = re.sub(r"^JvmArgs=[^\r\n]*", lambda _: line, text, count=1, flags=re.M) + elif re.search(r"^\[General\][^\r\n]*", text, re.M): + text = re.sub(r"^(\[General\][^\r\n]*)(\r?\n)", lambda g: g.group(1) + g.group(2) + line + g.group(2), text, count=1, flags=re.M) + else: + text = line + "\n" + text + if re.search(r"^OverrideJavaArgs=[^\r\n]*", text, re.M): + text = re.sub(r"^OverrideJavaArgs=[^\r\n]*", "OverrideJavaArgs=true", text, count=1, flags=re.M) + else: + text = re.sub(r"^(JvmArgs=[^\r\n]*)(\r?\n|$)", lambda g: g.group(1) + (g.group(2) or "\n") + "OverrideJavaArgs=true" + g.group(2), text, count=1, flags=re.M) + write(cfg, text) + print(line) + + +def logical_lines(text): + out = [] + buf = "" + for raw in text.splitlines(): + if buf: + piece = raw.strip() + else: + piece = raw.rstrip() + if piece.endswith("\\"): + buf += piece[:-1] + " " + continue + out.append(buf + piece) + buf = "" + if buf: + out.append(buf) + return out + + +def profile_tokens(defs, name, seen): + if name in seen: + raise SystemExit("error: profile cycle at " + name) + if name not in defs: + raise SystemExit("error: profile." + name + " not defined") + out = [] + for tok in defs[name].split(): + if tok.startswith("profile."): + out.extend(profile_tokens(defs, tok[len("profile."):], seen | {name})) + else: + out.append(tok) + return out + + +def profile(props, out_txt, name): + defs = {} + for line in logical_lines(read(props)): + s = line.strip() + if s.startswith("#") or s.startswith("!"): + continue + m = re.match(r"profile\.(\w+)\s*=(.*)$", s) + if m: + defs[m.group(1)] = m.group(2) + if name not in defs: + raise SystemExit("error: profile." + name + " not found in " + props) + options = {} + for tok in profile_tokens(defs, name, frozenset()): + if "=" in tok: + k, v = tok.split("=", 1) + elif tok.startswith("!"): + k, v = tok[1:], "false" + else: + k, v = tok, "true" + if not re.match(r"^[A-Za-z_][A-Za-z0-9_.]*$", k) or v == "": + raise SystemExit("error: cannot parse profile token " + repr(tok)) + options[k] = v + if not options: + raise SystemExit("error: profile." + name + " is empty") + write(out_txt, "".join(k + "=" + v + "\n" for k, v in options.items())) + print(len(options)) + + +def main(argv): + cmd = argv[1] + if cmd == "jvmargs": + jvmargs(argv[2], argv[3], argv[4], argv[5:]) + elif cmd == "profile": + profile(argv[2], argv[3], argv[4] if len(argv) > 4 else "HIGH") + else: + raise SystemExit("error: unknown helper " + cmd) + + +main(sys.argv) +PY +)" + +helper() { + python3 -c "$HELPER" "$@" +} + +if [ "${1:-}" = "--helper" ]; then + shift + helper "$@" + exit $? +fi + +NAME="${1:?usage: flyby-prism.sh ...}" +shift +if [ $# -eq 0 ]; then + echo "usage: flyby-prism.sh ..." >&2 + exit 2 +fi + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT" + +PRISM_INSTANCE="${PRISM_INSTANCE:-GTNH-daily-2026-10-01+766-mmcprism-java17-26}" +PRISM_BIN="${PRISM_BIN:-/Applications/Prism Launcher.app/Contents/MacOS/prismlauncher}" +PRISM_TIMEOUT="${PRISM_TIMEOUT:-900}" +PRISM_KEEP_JAR="${PRISM_KEEP_JAR:-0}" +PRISM_WORLD="${PRISM_WORLD:-FlybyGTNH}" +PRISM_ENTRIES="${PRISM_ENTRIES:-minecraft:chunk_borders=inOverlay,minecraft:chunk_section_visibility=inOverlay,minecraft:visualize_block_light_levels=inOverlay,minecraft:visualize_sky_light_levels=inOverlay,minecraft:visualize_heightmap=inOverlay,minecraft:visualize_collision_boxes=inOverlay,minecraft:visualize_solid_faces=inOverlay,minecraft:visualize_water_levels=inOverlay,minecraft:visualize_entity_supporting_block=inOverlay}" + +for v in "$@"; do + case "$v" in + sdl-off|sdl-pack|gl-off|gl-pack) ;; + *) echo "error: variant must be sdl-off, sdl-pack, gl-off or gl-pack, got: $v" >&2; exit 2 ;; + esac +done +case "$PRISM_WORLD" in + *[[:space:]\"]*|"") echo "error: PRISM_WORLD must be non-empty without spaces or quotes: '$PRISM_WORLD'" >&2; exit 2 ;; +esac +case "$PRISM_ENTRIES" in + *[[:space:]\"]*) echo "error: PRISM_ENTRIES must not contain spaces or quotes" >&2; exit 2 ;; +esac + +INST="$HOME/Library/Application Support/PrismLauncher/instances/$PRISM_INSTANCE" +MC="$INST/.minecraft" +CFG="$INST/instance.cfg" +OPTS="$MC/options.txt" +SHADERS="$MC/config/shaders.properties" +for f in "$CFG" "$OPTS" "$SHADERS"; do + if [ ! -f "$f" ]; then + echo "error: missing $f" >&2 + exit 1 + fi +done +if [ ! -x "$PRISM_BIN" ]; then + echo "error: launcher not executable: $PRISM_BIN" >&2 + exit 1 +fi + +INST_TAIL="${PRISM_INSTANCE#*+}" +GAME_PAT="java.*$INST_TAIL" +if pgrep -x prismlauncher >/dev/null 2>&1; then + echo "error: Prism Launcher is already running; quit it first" >&2 + exit 1 +fi +if pgrep -f "$GAME_PAT" >/dev/null 2>&1; then + echo "error: a process matching '$GAME_PAT' (the instance's game?) is running" >&2 + exit 1 +fi +if pgrep -f 'GradleStart|retrofuturabootstrap' >/dev/null 2>&1; then + echo "error: a dev client is running" >&2 + exit 1 +fi + +./gradlew assemble +JAR="" +for f in "$ROOT"/build/libs/angelica-*.jar; do + case "$f" in + *-dev.jar|*-sources.jar|*-api.jar|*-dev-preshadow.jar|*-predowngrade.jar|*shadow-stripped.jar) continue ;; + esac + if [ -f "$f" ] && { [ -z "$JAR" ] || [ "$f" -nt "$JAR" ]; }; then + JAR="$f" + fi +done +if [ -z "$JAR" ]; then + echo "error: no production angelica jar in build/libs" >&2 + exit 1 +fi +JAR_NAME="$(basename "$JAR")" +echo "==> test jar: $JAR" + +OUT="$ROOT/tmp/prism/$NAME" +BAK="$OUT/backup" + +PACK="$(awk '/^shaderPack=/ { sub(/^[^=]*=/, ""); sub(/\r$/, ""); print; exit }' "$SHADERS")" +PACK_TXT="" +if [ -n "$PACK" ]; then + PACK_TXT="$MC/shaderpacks/$PACK.txt" +fi +for v in "$@"; do + if [ "$v" = "gl-pack" ] || [ "$v" = "sdl-pack" ]; then + if [ -z "$PACK" ] || [ ! -d "$MC/shaderpacks/$PACK" ]; then + echo "error: shaderPack '$PACK' missing from $MC/shaderpacks" >&2 + exit 1 + fi + fi +done + +STALE_BAK="" +if [ -d "$BAK" ]; then + STALE_BAK="$(find "$BAK" -mindepth 1 -maxdepth 1 | head -n 1)" +fi +STALE_DISABLED="" +for f in "$MC"/mods/angelica-*.jar.disabled; do + if [ -e "$f" ]; then + STALE_DISABLED="$STALE_DISABLED $f" + fi +done +if [ -n "$STALE_BAK" ] || [ -n "$STALE_DISABLED" ]; then + echo "error: leftovers from an interrupted run; nothing was touched" >&2 + echo "undo steps:" >&2 + echo " 1. copy back from $BAK (if present): instance.cfg -> $CFG, options.txt -> $OPTS, shaders.properties -> $SHADERS, pack.txt -> ${PACK_TXT:-n/a}" >&2 + echo " 2. remove the installed test jar from $MC/mods (originals are listed in $BAK/renamed.list)" >&2 + echo " 3. drop the .disabled suffix from:${STALE_DISABLED:- (none found)}" >&2 + echo " 4. rm -rf '$BAK'" >&2 + exit 1 +fi +mkdir -p "$BAK" + +flyby_copy "$CFG" "$BAK/instance.cfg" +flyby_copy "$OPTS" "$BAK/options.txt" +flyby_copy "$SHADERS" "$BAK/shaders.properties" +if [ -n "$PACK_TXT" ]; then + flyby_backup_file "$PACK_TXT" "$BAK/pack.txt" +fi + +RENAMED="$BAK/renamed.list" +: >| "$RENAMED" + +PRISM_PID="" +GAME_PID="" + +kill_game() { + local pids + pids="$(pgrep -f "$GAME_PAT" || true)" + if [ -n "$pids" ]; then + # shellcheck disable=SC2086 + kill $pids 2>/dev/null || true + sleep 3 + pids="$(pgrep -f "$GAME_PAT" || true)" + if [ -n "$pids" ]; then + # shellcheck disable=SC2086 + kill -9 $pids 2>/dev/null || true + fi + fi + if [ -n "$PRISM_PID" ] && kill -0 "$PRISM_PID" 2>/dev/null; then + kill "$PRISM_PID" 2>/dev/null || true + sleep 1 + kill -9 "$PRISM_PID" 2>/dev/null || true + fi +} + +RESTORED=0 +restore() { + set +e + if [ "$RESTORED" = "1" ]; then + return 0 + fi + RESTORED=1 + trap - EXIT INT TERM HUP + kill_game + local ok=1 + flyby_restore_file "$BAK/instance.cfg" "$CFG" || ok=0 + flyby_restore_file "$BAK/options.txt" "$OPTS" || ok=0 + flyby_restore_file "$BAK/shaders.properties" "$SHADERS" || ok=0 + if [ -n "$PACK_TXT" ]; then + flyby_restore_file "$BAK/pack.txt" "$PACK_TXT" || ok=0 + fi + if [ "$PRISM_KEEP_JAR" != "1" ]; then + rm -f "$MC/mods/$JAR_NAME" || { echo "warning: could not remove $MC/mods/$JAR_NAME" >&2; ok=0; } + while IFS= read -r f; do + if [ -n "$f" ] && [ -f "$f.disabled" ]; then + mv -f "$f.disabled" "$f" || { echo "warning: could not re-enable $f" >&2; ok=0; } + fi + done < "$RENAMED" + if [ "$ok" = "1" ]; then + rm -rf "$BAK" + fi + echo "==> test jar removed, original angelica jar(s) re-enabled" + else + echo "==> test jar LEFT installed: $MC/mods/$JAR_NAME" + echo "==> to undo: rm -f '$MC/mods/$JAR_NAME'; then drop the .disabled suffix from: $(tr '\n' ' ' < "$RENAMED")" + fi +} +trap restore EXIT +trap 'exit 130' INT +trap 'exit 143' TERM +trap 'exit 129' HUP + +for f in "$MC"/mods/angelica-*.jar; do + if [ -f "$f" ]; then + printf '%s\n' "$f" >> "$RENAMED" + mv -n "$f" "$f.disabled" + fi +done +flyby_copy "$JAR" "$MC/mods/$JAR_NAME" +echo "==> installed $JAR_NAME; disabled: $(tr '\n' ' ' < "$RENAMED")" + +game_pid() { + pgrep -f "$GAME_PAT" | head -n 1 || true +} + +FAILED=0 +for VARIANT in "$@"; do + VDIR="$OUT/$VARIANT" + flyby_fresh_dir "$VDIR" + + case "$VARIANT" in + sdl-*) BACKEND=true ;; + *) BACKEND=false ;; + esac + case "$VARIANT" in + *-pack) SHADERS_ON=true ;; + *) SHADERS_ON=false ;; + esac + + EXTRA=( + -Dangelica.debug.tooling=true + -Dangelica.flyby.route=static + -Dangelica.flyby.length=400 + -Dangelica.flyby.warmupTicks=200 + -Dangelica.flyby.exitWhenDone=true + -Dangelica.flyby.waitForFocus=true + -Dangelica.flyby.screenshots=4 + -Dangelica.flyby.debugHud=true + -Dangelica.flyby.debugDump=true + "-DquickPlaySingleplayer=$PRISM_WORLD" + ) + CHECK=(--modpack --shots 4 --expect-mode OVERLAY --require-group botania:botania --require-group journeymap:journeymap --forbid-dup --require-mod etfuturum) + EXTRA+=("-Dangelica.flyby.debugEntries=$PRISM_ENTRIES") + RENDERERS="$(printf '%s\n' "$PRISM_ENTRIES" | tr ',' '\n' | sed -e '/^$/d' -e 's/=[^=]*$//' | paste -sd, -)" + CHECK+=(--require-renderers "$RENDERERS") + + flyby_restore_file "$BAK/instance.cfg" "$CFG" + echo "==> $NAME/$VARIANT: JvmArgs" + helper jvmargs "$CFG" "$BAK/instance.cfg" "$BACKEND" "${EXTRA[@]}" | tee "$VDIR/jvmargs.txt" + + flyby_restore_file "$BAK/shaders.properties" "$SHADERS" + flyby_set_key "$SHADERS" enableShaders "$SHADERS_ON" = + + if [ -n "$PACK_TXT" ]; then + flyby_restore_file "$BAK/pack.txt" "$PACK_TXT" + fi + if [ "$VARIANT" = "gl-pack" ]; then + PROPS="$MC/shaderpacks/$PACK/shaders/shaders.properties" + if [ ! -f "$PROPS" ] || ! COUNT="$(helper profile "$PROPS" "$PACK_TXT" HIGH)"; then + echo "FAIL $VARIANT: pack profile.HIGH could not be read from $PROPS; not launching" + FAILED=1 + continue + fi + echo "==> $NAME/$VARIANT: wrote $COUNT HIGH options to $PACK_TXT" + fi + + flyby_restore_file "$BAK/options.txt" "$OPTS" + flyby_set_key "$OPTS" fullscreen true : + + mkdir -p "$MC/screenshots" + rm -f "$MC"/screenshots/flyby-shot-*.png "$MC/logs/fml-client-latest.log" + flyby_crash_list "$MC/crash-reports" >| "$VDIR/crashes.before" + + echo "==> $NAME/$VARIANT: launching (timeout ${PRISM_TIMEOUT}s)" + "$PRISM_BIN" -l "$PRISM_INSTANCE" >| "$VDIR/launcher.log" 2>&1 & + PRISM_PID=$! + + GAME_PID="" + WAITED=0 + while [ "$WAITED" -lt 240 ]; do + GAME_PID="$(game_pid)" + if [ -n "$GAME_PID" ]; then + break + fi + if ! kill -0 "$PRISM_PID" 2>/dev/null; then + break + fi + sleep 2 + WAITED=$((WAITED + 2)) + done + + STATUS="0" + if [ -z "$GAME_PID" ]; then + STATUS="nogame" + echo "==> $NAME/$VARIANT: no game process appeared within ${WAITED}s" + else + DEADLINE=$(( $(date +%s) + PRISM_TIMEOUT )) + while kill -0 "$GAME_PID" 2>/dev/null; do + if [ "$(date +%s)" -ge "$DEADLINE" ]; then + echo "==> $NAME/$VARIANT: timeout, killing game" + STATUS="timeout" + break + fi + sleep 2 + done + fi + kill_game + PRISM_PID="" + sleep 2 + + NEW_CRASHES="$(flyby_new_crashes "$MC/crash-reports" "$VDIR/crashes.before" "$VDIR/crashes.after")" + flyby_collect "$MC" "$VDIR" fml-client-latest.log + + if ! grep -qF "Flyby: world saving is off" "$VDIR/fml-client-latest.log" 2>/dev/null; then + echo "FAIL $VARIANT: never entered the world (no 'Flyby: world saving is off' in the log; QuickPlay did not fire?); stopping the batch" + FAILED=1 + break + fi + + if [ "$STATUS" = "nogame" ]; then + STATUS="1" + fi + if ! python3 "$ROOT/scripts/flyby-smoke-check.py" "$VDIR" "$STATUS" "$NEW_CRASHES" "${CHECK[@]}"; then + FAILED=1 + fi +done + +exit "$FAILED" diff --git a/scripts/flyby-smoke-check.py b/scripts/flyby-smoke-check.py index dc7ffe424..d8d7ce6f5 100755 --- a/scripts/flyby-smoke-check.py +++ b/scripts/flyby-smoke-check.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import argparse import glob import os import re @@ -15,8 +16,18 @@ CATCH_MARK = "Caught an exception during ISBRH rendering" BAD_LINE = re.compile( r"InvalidInjectionException|InjectionError|MixinApplyError|MixinTransformerError|" r"InvalidMixinException|MixinPrepareError|skipped: the surrounded call was removed|" - r"flyby scene command did not execute" + r"flyby scene command did not execute|Grouped debug screen failed|Failed to set up the debug screen|" + r"Debug screen entry \S+ failed and is disabled|needs the batching font renderer|" + r"Flyby debugEntries:|Flyby: debug entry list is not available|Flyby: unknown debugProfile|" + r"Debug screen compat entry for \S+ failed to register|" + r"Failed to (?:load|save|set aside the unreadable) debug screen profile|toggle listeners kept changing" ) +OVERLAY_RE = re.compile(r"Flyby debug overlay shot=(\d+) mode=(\w+)(?:.* renderers=(\S*))?") +LINE_RE = re.compile(r"Flyby debug line shot=(\d+) id=(\S+) text=(.*)") +GROUP_RE = re.compile(r"Flyby debug group shot=(\d+) id=(\S+) ") +DUP_IDS = ("angelica:other_left", "angelica:other_right") +DUP_PREFIXES = ("Allocation rate:", "Java:", "GPU:", "OpenGL:", "CPU Cores:", "OS:", "LWJGL:", "IntCache:", + "[Botania]", "[JM]", "[ChunkGen]", "hunger:", "HydroEnergy GPU RAM:", "renderDebugMode:") ANGELICA_FRAME = re.compile( r"com\.gtnewhorizons\.angelica|net\.coderbot|org\.embeddedt|org\.taumc|com\.prupe|" r"jss\.notfine|\$surround|angelica\$" @@ -44,7 +55,44 @@ def split_traces(lines): return traces -def check(variant_dir, exit_status, new_crashes): +def check_overlay(lines, opts, reasons): + overlays = [m for m in (OVERLAY_RE.search(l) for l in lines) if m] + mode = opts.get("expect_mode") + if mode: + if not overlays: + reasons.append("no 'Flyby debug overlay' line") + else: + wrong = sorted({m.group(2) for m in overlays if m.group(2) != mode}) + if wrong: + reasons.append("overlay mode " + ",".join(wrong) + " != expected " + mode) + + dbg_lines = [m for m in (LINE_RE.search(l) for l in lines) if m] + groups = [m for m in (GROUP_RE.search(l) for l in lines) if m] + for ident in opts.get("require_group") or []: + if not any(m.group(2) == ident for m in groups): + reasons.append("missing debug group " + ident) + + wanted = [r for r in (opts.get("require_renderers") or "").split(",") if r] + if wanted: + if not any(m.group(3) is not None and set(wanted) <= set(m.group(3).split(",")) for m in overlays): + reasons.append("no overlay line with renderers " + ",".join(wanted)) + + for name in opts.get("require_mod") or []: + if not any(name.lower() in l.lower() for l in lines): + reasons.append("mod " + name + " not in log") + + if opts.get("forbid_dup"): + if not dbg_lines: + reasons.append("no debug lines dumped") + dups = sorted({m.group(3).lstrip().split(":")[0] + ":" for m in dbg_lines + if m.group(2) in DUP_IDS and m.group(3).lstrip().startswith(DUP_PREFIXES)}) + if dups: + reasons.append("duplicate vanilla lines in " + "/".join(DUP_IDS) + ": " + ", ".join(dups)) + + +def check(variant_dir, exit_status, new_crashes, opts=None): + opts = opts or {} + shots_required = opts.get("shots") or SHOTS_REQUIRED variant = os.path.basename(os.path.normpath(variant_dir)) reasons = [] details = [] @@ -71,7 +119,11 @@ def check(variant_dir, exit_status, new_crashes): elif max(frames) == 0: reasons.append("flyby frames is 0") - bad = [l for l in lines if BAD_LINE.search(l)] + if opts.get("modpack"): + bad = [l for i, l in enumerate(lines) if BAD_LINE.search(l) + and any("angelica" in x.lower() for x in lines[i:i + 4])] + else: + bad = [l for l in lines if BAD_LINE.search(l)] if bad: reasons.append(str(len(bad)) + " mixin/injection/scene failure line(s)") details.extend([" " + l.strip()[:200] for l in bad[:3]]) @@ -94,12 +146,14 @@ def check(variant_dir, exit_status, new_crashes): if CATCH_MARK in l and any(CRASH_MARK in x for x in lines[i:i + 2]): caught = True break - if not caught: + if not caught and not opts.get("modpack"): reasons.append("no ISBRH catch line for crashtest") + check_overlay(lines, opts, reasons) + shots = sorted(glob.glob(os.path.join(variant_dir, "flyby-shot-*.png"))) - if len(shots) < SHOTS_REQUIRED: - reasons.append("only " + str(len(shots)) + " of " + str(SHOTS_REQUIRED) + " screenshots") + if len(shots) < shots_required: + reasons.append("only " + str(len(shots)) + " of " + str(shots_required) + " screenshots") small = [os.path.basename(p) for p in shots if os.path.getsize(p) < MIN_SHOT_BYTES] if small: reasons.append("small screenshot(s): " + ", ".join(small)) @@ -154,12 +208,56 @@ def self_test(): ("no-isbrh", [l for l in GOOD_LOG if CATCH_MARK not in l], {}, 1), ("mixin-error", GOOD_LOG + ["[10:00:05] [main/ERROR]: InvalidInjectionException: bad"], {}, 1), ("small-shot", GOOD_LOG, {"shot_size": 100}, 1), + ("bad-entries", GOOD_LOG + ["[10:00:06] [Client thread/ERROR]: Flyby debugEntries: unknown entry id 'test:nope'"], {}, 1), ] for name, log, kw, want in cases: d = make_variant(root, name, log, **kw) got = check(d, 0, 0) if got != want: failures.append(name) + no_isbrh = [l for l in GOOD_LOG if CATCH_MARK not in l] + ov = [ + "[10:00:06] [Client thread/INFO]: Flyby debug overlay shot=0 mode=OVERLAY groups=2 renderers=vanilla,angelica", + "[10:00:06] [Client thread/INFO]: Flyby debug group shot=0 id=angelica:other_left side=L x=0 y=0 w=1 h=1 facts= lines=2", + "[10:00:06] [Client thread/INFO]: Flyby debug line shot=0 id=angelica:other_left text=Allocation rate: 1 MB/s", + "[10:00:06] [Client thread/INFO]: Flyby debug line shot=0 id=angelica:other_left text=Second", + "[10:00:06] [Client thread/INFO]: Flyby debug line shot=0 id=angelica:other_right text=Free", + "[10:00:07] [main/INFO]: Loading mod Botania", + ] + ov_dup = [l for l in ov if "Allocation" not in l] + ov_dup += ["[10:00:06] [Client thread/INFO]: Flyby debug line shot=1 id=angelica:other_right text= GPU: foo"] + flag_cases = [ + ("modpack-ok", no_isbrh, {"modpack": True}, 0), + ("modpack-no-flag", no_isbrh, {}, 1), + ("modpack-foreign-bad", GOOD_LOG + ["[10:00:05] [main/ERROR]: InvalidInjectionException: x", "other", "other", "other", "angelica late"], {"modpack": True}, 0), + ("modpack-angelica-bad", no_isbrh + ["[10:00:05] [main/ERROR]: Grouped debug screen failed", "x", "angelica y"], {"modpack": True}, 1), + ("grouped-failed", GOOD_LOG + ["[10:00:05] [main/ERROR]: Grouped debug screen failed"], {}, 1), + ("setup-failed", GOOD_LOG + ["[10:00:05] [Client thread/ERROR] [Angelica]: Failed to set up the debug screen; it stays disabled for this session"], {}, 1), + ("entry-failed", GOOD_LOG + ["[10:00:05] [Client thread/ERROR] [Angelica]: Debug screen entry botania:debug failed and is disabled for the rest of this session"], {}, 1), + ("font-fallback", GOOD_LOG + ["[10:00:05] [Client thread/ERROR] [Angelica]: Grouped debug screen needs the batching font renderer on the HUD font; using the classic layout for the rest of this session"], {}, 1), + ("shots-ok", GOOD_LOG, {"shots": 3}, 0), + ("shots-short", GOOD_LOG, {"shots": 9}, 1), + ("mode-ok", GOOD_LOG + ov, {"expect_mode": "OVERLAY"}, 0), + ("mode-wrong", GOOD_LOG + ov, {"expect_mode": "FLAT"}, 1), + ("mode-none", GOOD_LOG, {"expect_mode": "FLAT"}, 1), + ("group-ok", GOOD_LOG + ov, {"require_group": ["angelica:other_left"]}, 0), + ("group-missing", GOOD_LOG + ov, {"require_group": ["angelica:nope"]}, 1), + ("renderers-ok", GOOD_LOG + ov, {"require_renderers": "angelica,vanilla"}, 0), + ("renderers-missing", GOOD_LOG + ov, {"require_renderers": "angelica,sodium"}, 1), + ("mod-ok", GOOD_LOG + ov, {"require_mod": ["botania"]}, 0), + ("mod-missing", GOOD_LOG + ov, {"require_mod": ["thaumcraft"]}, 1), + ("dup-fail", GOOD_LOG + ov, {"forbid_dup": True}, 1), + ("dup-ok", GOOD_LOG + ov_dup[:3] + ov_dup[4:5], {"forbid_dup": True}, 0), + ("dup-spaces-fail", GOOD_LOG + ov_dup, {"forbid_dup": True}, 1), + ("dup-no-dump", GOOD_LOG, {"forbid_dup": True}, 1), + ] + line = "[10:00:06] [Client thread/INFO]: Flyby debug line shot=1 id=angelica:other_left text= [Botania] x" + flag_cases.append(("dup-prefix", GOOD_LOG + ov_dup[:3] + [line], {"forbid_dup": True}, 1)) + flag_cases.append(("dup-prefix-other-id", GOOD_LOG + ov_dup[:3] + [line.replace("other_left", "other_elsewhere")], {"forbid_dup": True}, 0)) + for name, log, opts, want in flag_cases: + d = make_variant(root, name, log, shots=3 if name == "shots-ok" else 8) + if check(d, 0, 0, opts) != want: + failures.append(name) d = make_variant(root, "bad-exit", GOOD_LOG) if check(d, "timeout", 1) != 1: failures.append("bad-exit") @@ -175,11 +273,19 @@ def self_test(): def main(argv): if len(argv) == 2 and argv[1] == "--self-test": return self_test() - if len(argv) != 4: - print("usage: flyby-smoke-check.py ", file=sys.stderr) - print(" flyby-smoke-check.py --self-test", file=sys.stderr) - return 2 - return check(argv[1], argv[2], int(argv[3])) + ap = argparse.ArgumentParser(prog="flyby-smoke-check.py") + ap.add_argument("variant_dir") + ap.add_argument("exit_status") + ap.add_argument("new_crashes", type=int) + ap.add_argument("--modpack", action="store_true") + ap.add_argument("--shots", type=int) + ap.add_argument("--expect-mode") + ap.add_argument("--require-group", action="append", metavar="ID") + ap.add_argument("--require-renderers", metavar="a,b") + ap.add_argument("--require-mod", action="append", metavar="NAME") + ap.add_argument("--forbid-dup", action="store_true") + a = ap.parse_args(argv[1:]) + return check(a.variant_dir, a.exit_status, a.new_crashes, vars(a)) if __name__ == "__main__": diff --git a/scripts/flyby-smoke.sh b/scripts/flyby-smoke.sh index 7064b399e..84b95741c 100755 --- a/scripts/flyby-smoke.sh +++ b/scripts/flyby-smoke.sh @@ -11,11 +11,14 @@ # SMOKE_TIMEOUT - seconds before the client is killed (default: 600). # SMOKE_SCENE - scene command file (default: scripts/flyby-scenes/surround-smoke.txt). # SMOKE_PACK - shaderpack name for the pack variant (default: ComplementaryUnbound_r5.7.1 + EuphoriaPatches_1.8.1). -# SMOKE_DEBUG_HUD - true forces F3 on; false keeps the saved setting (default: true). +# SMOKE_DEBUG_HUD - true forces F3 on, false forces it off (default: true). # SMOKE_PITCH - camera pitch in degrees, positive looks down (default: 15). # SMOKE_WARMUP_TICKS - ticks at the first pose before the pan (default: 100). +# SMOKE_ARGS - extra -D flags for the client, word-split (default: none). +# SMOKE_CHECK_ARGS - extra flyby-smoke-check.py flags, word-split (default: none). +# SMOKE_FULLSCREEN - 0 keeps options.txt's window mode (default: 1, fullscreen for the run). # -# Runs from this script's own checkout. Restores run/client/config/shaders.properties and angelica-options.json on exit. +# Runs from this script's own checkout, fullscreen unless SMOKE_FULLSCREEN=0 (fullscreen avoids vsync pacing). Restores run/client/config/shaders.properties, angelica-options.json and options.txt on exit. set -euo pipefail @@ -27,6 +30,8 @@ fi ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" +# shellcheck source=flyby-lib.sh +. "$ROOT/scripts/flyby-lib.sh" QUICKPLAY_WORLD="${QUICKPLAY_WORLD:-Flat World - No Mods}" FLYBY_ORIGIN="${FLYBY_ORIGIN:-1509.5,-924.5,0}" @@ -35,6 +40,9 @@ SMOKE_PACK="${SMOKE_PACK:-ComplementaryUnbound_r5.7.1 + EuphoriaPatches_1.8.1}" SMOKE_DEBUG_HUD="${SMOKE_DEBUG_HUD:-true}" SMOKE_PITCH="${SMOKE_PITCH:-15}" SMOKE_WARMUP_TICKS="${SMOKE_WARMUP_TICKS:-100}" +SMOKE_ARGS="${SMOKE_ARGS:-}" +SMOKE_CHECK_ARGS="${SMOKE_CHECK_ARGS:-}" +SMOKE_FULLSCREEN="${SMOKE_FULLSCREEN:-1}" SCENE="${SMOKE_SCENE:-$ROOT/scripts/flyby-scenes/surround-smoke.txt}" case "$SCENE" in @@ -47,6 +55,7 @@ OUT="$ROOT/tmp/smoke/$NAME" BACKUP="$OUT/shaders.properties.bak" OPTIONS="$RUN/config/angelica-options.json" OPTIONS_BACKUP="$OUT/angelica-options.json.bak" +OPTIONS_TXT="$RUN/options.txt" for v in "$@"; do case "$v" in @@ -66,48 +75,26 @@ for v in "$@"; do done mkdir -p "$OUT" -python3 - "$SHADERS" "$BACKUP" <<'PY' -import shutil, sys -shutil.copyfile(sys.argv[1], sys.argv[2]) -PY -python3 - "$OPTIONS" "$OPTIONS_BACKUP" <<'PY' -import shutil, sys -shutil.copyfile(sys.argv[1], sys.argv[2]) -PY +flyby_copy "$SHADERS" "$BACKUP" +flyby_copy "$OPTIONS" "$OPTIONS_BACKUP" restore_options() { - python3 - "$OPTIONS_BACKUP" "$OPTIONS" <<'PY' || true -import shutil, sys -shutil.copyfile(sys.argv[1], sys.argv[2]) -PY + flyby_restore_file "$OPTIONS_BACKUP" "$OPTIONS" || true } restore() { - python3 - "$BACKUP" "$SHADERS" <<'PY' || true -import shutil, sys -shutil.copyfile(sys.argv[1], sys.argv[2]) -PY + flyby_restore_file "$BACKUP" "$SHADERS" || true restore_options + if [ "$SMOKE_FULLSCREEN" = 1 ]; then flyby_fullscreen_restore "$OPTIONS_TXT" "$OUT/options.txt.bak" || true; fi } +if [ "$SMOKE_FULLSCREEN" = 1 ]; then flyby_fullscreen_on "$OPTIONS_TXT" "$OUT/options.txt.bak"; fi trap restore EXIT set_shaders() { - python3 - "$SHADERS" "$1" "$2" <<'PY' -import re, sys -path, enable, pack = sys.argv[1:4] -text = open(path).read() -def put(text, key, value): - pat = re.compile(r'^' + key + r'=.*$', re.M) - if pat.search(text): - return pat.sub(lambda m: key + '=' + value, text) - if text and not text.endswith('\n'): - text += '\n' - return text + key + '=' + value + '\n' -text = put(text, 'enableShaders', enable) -if pack: - text = put(text, 'shaderPack', pack) -open(path, 'w').write(text) -PY + flyby_set_key "$SHADERS" enableShaders "$1" = + if [ -n "$2" ]; then + flyby_set_key "$SHADERS" shaderPack "$2" = + fi } set_vao() { @@ -123,20 +110,10 @@ with open(path, "w") as f: PY } -crash_list() { - if [ -d "$RUN/crash-reports" ]; then - ls "$RUN/crash-reports" | sort - fi -} - FAILED=0 for VARIANT in "$@"; do VDIR="$OUT/$VARIANT" - python3 - "$VDIR" <<'PY' -import shutil, sys -shutil.rmtree(sys.argv[1], ignore_errors=True) -PY - mkdir -p "$VDIR" + flyby_fresh_dir "$VDIR" restore_options if [ "$VARIANT" = "vao" ]; then @@ -151,7 +128,7 @@ PY mkdir -p "$RUN/screenshots" rm -f "$RUN"/screenshots/flyby-shot-*.png - crash_list >| "$VDIR/crashes.before" + flyby_crash_list "$RUN/crash-reports" >| "$VDIR/crashes.before" echo "==> $NAME/$VARIANT: launching (timeout ${SMOKE_TIMEOUT}s)" ./gradlew runClient25 --console=plain \ @@ -167,6 +144,7 @@ PY -Dangelica.flyby.crashTest=isbrh \ -Dangelica.debug.testBlocks=true \ -DquickPlaySingleplayer="$QUICKPLAY_WORLD" \ + $SMOKE_ARGS \ >| "$VDIR/console.log" 2>&1 & PID=$! @@ -190,21 +168,10 @@ PY STATUS="$RC" fi - crash_list >| "$VDIR/crashes.after" - NEW_CRASHES="$(comm -13 "$VDIR/crashes.before" "$VDIR/crashes.after" | wc -l | tr -d ' ')" + NEW_CRASHES="$(flyby_new_crashes "$RUN/crash-reports" "$VDIR/crashes.before" "$VDIR/crashes.after")" + flyby_collect "$RUN" "$VDIR" fml-client-latest.log latest.log - for f in fml-client-latest.log latest.log; do - if [ -f "$RUN/logs/$f" ]; then - cp -f "$RUN/logs/$f" "$VDIR/$f" - fi - done - for f in "$RUN"/screenshots/flyby-shot-*.png; do - if [ -f "$f" ]; then - cp -f "$f" "$VDIR/" - fi - done - - if ! python3 "$ROOT/scripts/flyby-smoke-check.py" "$VDIR" "$STATUS" "$NEW_CRASHES"; then + if ! python3 "$ROOT/scripts/flyby-smoke-check.py" "$VDIR" "$STATUS" "$NEW_CRASHES" $SMOKE_CHECK_ARGS; then FAILED=1 fi done diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java index a359e7c5d..277d486a4 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java @@ -785,7 +785,7 @@ public class SDLGPURenderBackend extends RenderBackend { device.fenceReleaser().drain(); if (frameSync != 0L) { frameSyncs.enqueue(frameSync); - while (frameSyncs.size() > Device.MAX_FRAMES_IN_FLIGHT - 1) { + while (frameSyncs.size() > RenderBackend.MAX_FRAMES_IN_FLIGHT - 1) { final long sync = frameSyncs.dequeueLong(); fenceTracker.clientWaitSync(sync, 0, Long.MAX_VALUE); fenceTracker.deleteSync(sync); diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/Device.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/Device.java index 0993c1501..5f8f73317 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/Device.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/Device.java @@ -37,9 +37,7 @@ import static org.lwjgl.system.MemoryUtil.memUTF8; public final class Device { private static final Logger LOG = LogManager.getLogger("Angelica-SDLGPU"); - public static final int MAX_FRAMES_IN_FLIGHT = 3; - - private static final int FRAMES_IN_FLIGHT = Math.min(Math.max(SystemProperties.SDL_FRAMES_IN_FLIGHT, 1), MAX_FRAMES_IN_FLIGHT); + private static final int FRAMES_IN_FLIGHT = Math.min(Math.max(SystemProperties.SDL_FRAMES_IN_FLIGHT, 1), RenderBackend.MAX_FRAMES_IN_FLIGHT); public static int framesInFlight() { return FRAMES_IN_FLIGHT; } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java index ec5c6671d..235d8f302 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java @@ -45,6 +45,7 @@ import static org.lwjgl.system.MemoryStack.*; import static org.lwjgl.system.MemoryUtil.memAlloc; import static org.lwjgl.system.MemoryUtil.memFree; +import com.gtnewhorizons.angelica.glsm.backend.RenderBackend; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.glsm.texture.TextureStaging; import com.gtnewhorizons.angelica.sdlgpu.device.Device; @@ -697,7 +698,7 @@ public final class ResourceManager { final GpuBucket b = gpuBufferPool.get(Hashing.packHiLo(usage, bucket)); if (b == null || b.handles.isEmpty()) return 0; for (int i = 0, n = b.handles.size(); i < n; i++) { - if (gpuPoolCurrentFrame - b.frames.getLong(i) >= Device.MAX_FRAMES_IN_FLIGHT) { + if (gpuPoolCurrentFrame - b.frames.getLong(i) >= RenderBackend.MAX_FRAMES_IN_FLIGHT) { final long handle = b.handles.getLong(i); final int last = n - 1; if (i != last) { @@ -1555,7 +1556,7 @@ public final class ResourceManager { for (int i = 0; i < n; i += 2) { final long handle = batchSegmentPending.getLong(i); final long stamp = batchSegmentPending.getLong(i + 1); - if (currentFrame - stamp >= Device.MAX_FRAMES_IN_FLIGHT) { + if (currentFrame - stamp >= RenderBackend.MAX_FRAMES_IN_FLIGHT) { batchSegmentFree.enqueue(handle); } else { batchSegmentPending.set(w, handle); @@ -2445,7 +2446,7 @@ public final class ResourceManager { } private static final int DUMMY_VBO_SIZE = 16 * 16; - public static final int ATTRIB_RING_BLOCKS = 2048 * Device.MAX_FRAMES_IN_FLIGHT; + public static final int ATTRIB_RING_BLOCKS = 2048 * RenderBackend.MAX_FRAMES_IN_FLIGHT; public static final int ATTRIB_RING_CHUNK_BLOCKS = 64; private static final int ATTRIB_RING_CHUNKS = ATTRIB_RING_BLOCKS / ATTRIB_RING_CHUNK_BLOCKS; private final AtomicLong attribRingCounter = new AtomicLong(); diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugContext.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugContext.java new file mode 100644 index 000000000..34aaf59ea --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugContext.java @@ -0,0 +1,24 @@ +package com.gtnewhorizons.angelica.api.debug; + +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; + +public interface DebugContext { + + World world(); + + EntityClientPlayerMP player(); + + int blockX(); + + int blockY(); + + int blockZ(); + + Chunk chunk(); + + long nanoTime(); + + boolean isSampleDue(); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntryCategory.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntryCategory.java new file mode 100644 index 000000000..056fd8bec --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntryCategory.java @@ -0,0 +1,13 @@ +package com.gtnewhorizons.angelica.api.debug; + +public enum DebugEntryCategory { + + SCREEN_TEXT("angelica.debug.options.category.text"), + RENDERER("angelica.debug.options.category.renderer"); + + public final String translationKey; + + DebugEntryCategory(String translationKey) { + this.translationKey = translationKey; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntryHandle.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntryHandle.java new file mode 100644 index 000000000..70d9b191b --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntryHandle.java @@ -0,0 +1,10 @@ +package com.gtnewhorizons.angelica.api.debug; + +public interface DebugEntryHandle { + + String id(); + + boolean isEnabled(); + + boolean isFailed(); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntrySpec.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntrySpec.java new file mode 100644 index 000000000..4b8d44a0d --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugEntrySpec.java @@ -0,0 +1,87 @@ +package com.gtnewhorizons.angelica.api.debug; + +public final class DebugEntrySpec { + + public interface DebugEntryFactory { + + DebugScreenEntry create(DebugEntryHandle entry); + } + + public interface DebugToggleListener { + + void onEnabledChanged(DebugEntryHandle entry, boolean enabled); + } + + private final String id; + private final DebugEntryCategory category; + private String name; + private DebugScreenEntryStatus defaultStatus; + private DebugScreenEntryStatus performanceStatus = DebugScreenEntryStatus.NEVER; + private DebugEntryFactory textFactory; + private DebugToggleListener toggleListener; + private boolean supportsAlways = true; + + public DebugEntrySpec(String id, DebugEntryCategory category) { + this.id = id; + this.category = category; + this.defaultStatus = category == DebugEntryCategory.SCREEN_TEXT ? DebugScreenEntryStatus.IN_OVERLAY : DebugScreenEntryStatus.NEVER; + } + + public DebugEntrySpec name(String keyOrLiteral) { + this.name = keyOrLiteral; + return this; + } + + public DebugEntrySpec defaults(DebugScreenEntryStatus defaultStatus, DebugScreenEntryStatus performanceStatus) { + this.defaultStatus = defaultStatus; + this.performanceStatus = performanceStatus; + return this; + } + + public DebugEntrySpec text(DebugEntryFactory factory) { + this.textFactory = factory; + return this; + } + + public DebugEntrySpec toggle(DebugToggleListener listener) { + this.toggleListener = listener; + return this; + } + + public DebugEntrySpec supportsAlways(boolean supportsAlways) { + this.supportsAlways = supportsAlways; + return this; + } + + public String getId() { + return id; + } + + public DebugEntryCategory getCategory() { + return category; + } + + public String getName() { + return name; + } + + public DebugScreenEntryStatus getDefaultStatus() { + return defaultStatus; + } + + public DebugScreenEntryStatus getPerformanceStatus() { + return performanceStatus; + } + + public DebugEntryFactory getTextFactory() { + return textFactory; + } + + public DebugToggleListener getToggleListener() { + return toggleListener; + } + + public boolean getSupportsAlways() { + return supportsAlways; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugFact.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugFact.java new file mode 100644 index 000000000..710791e29 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugFact.java @@ -0,0 +1,18 @@ +package com.gtnewhorizons.angelica.api.debug; + +public interface DebugFact { + + DebugFact text(CharSequence text); + + DebugFact text(char c); + + DebugFact value(CharSequence value); + + DebugFact value(int value); + + DebugFact value(long value); + + DebugFact value(double value, int decimals); + + DebugFact padded(long value, int width); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugFactLines.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugFactLines.java new file mode 100644 index 000000000..7da271e15 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugFactLines.java @@ -0,0 +1,117 @@ +package com.gtnewhorizons.angelica.api.debug; + +import java.util.Arrays; + +public final class DebugFactLines { + + private static final int MAX_LABEL = 24; + private static final String COLON = ": "; + private static final String DASH = " - "; + + private String[] sources = new String[8]; + private String[] names = new String[8]; + private String[] values = new String[8]; + private int cursor; + private int size; + private boolean open; + + public void begin() { + cursor = 0; + open = true; + } + + public void end() { + open = false; + if (cursor < size) { + Arrays.fill(sources, cursor, size, null); + Arrays.fill(names, cursor, size, null); + Arrays.fill(values, cursor, size, null); + } + size = cursor; + } + + public void fact(String name, String value) { + requireOpen(); + if (value == null) throw new IllegalArgumentException("Debug fact value is null"); + reserve(); + sources[cursor] = null; + names[cursor] = name; + values[cursor] = value; + cursor++; + } + + public void line(String line) { + fact(null, line); + } + + public void split(String line) { + requireOpen(); + if (line == null) throw new IllegalArgumentException("Debug fact line is null"); + reserve(); + if (line.equals(sources[cursor])) { + cursor++; + return; + } + sources[cursor] = line; + parse(line); + cursor++; + } + + public void emit(DebugScreenDisplayer displayer, DebugGroupHandle group) { + if (open) throw new IllegalStateException("DebugFactLines.emit() called before end()"); + for (int i = 0; i < size; i++) { + final String value = values[i]; + final String name = names[i]; + if (name == null) { + displayer.line(group).text(value); + } else { + displayer.fact(group, name).value(value); + } + } + } + + private void parse(String line) { + int at = labelEnd(line, COLON); + if (at >= 0) { + names[cursor] = name(line, at); + values[cursor] = line.substring(at + COLON.length()); + return; + } + at = labelEnd(line, DASH); + if (at >= 0) { + names[cursor] = name(line, at); + values[cursor] = line.substring(at + DASH.length()); + return; + } + names[cursor] = null; + values[cursor] = line; + } + + private String name(String line, int end) { + final String previous = names[cursor]; + return previous != null && previous.length() == end && line.startsWith(previous) ? previous : line.substring(0, end); + } + + private void requireOpen() { + if (!open) throw new IllegalStateException("DebugFactLines is not open: call begin() first"); + } + + private void reserve() { + if (cursor == sources.length) { + final int capacity = cursor * 2; + sources = Arrays.copyOf(sources, capacity); + names = Arrays.copyOf(names, capacity); + values = Arrays.copyOf(values, capacity); + } + } + + static int labelEnd(String line, String separator) { + final int at = line.indexOf(separator); + if (at <= 0 || at > MAX_LABEL) return -1; + for (int i = 0; i < at; i++) { + final char c = line.charAt(i); + if (c == '=' || c == '(') return -1; + } + return at; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugGroupHandle.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugGroupHandle.java new file mode 100644 index 000000000..845c7c22a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugGroupHandle.java @@ -0,0 +1,11 @@ +package com.gtnewhorizons.angelica.api.debug; + +public interface DebugGroupHandle { + + int LEFT = 0; + int RIGHT = 1; + + String id(); + + int side(); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugGroupSpec.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugGroupSpec.java new file mode 100644 index 000000000..14d8ead1c --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugGroupSpec.java @@ -0,0 +1,56 @@ +package com.gtnewhorizons.angelica.api.debug; + +public final class DebugGroupSpec { + + public static final int DEFAULT_ORDER = 500; + + private final String id; + private String titleKey; + private int accent = -1; + private int side = DebugGroupHandle.LEFT; + private int order = DEFAULT_ORDER; + + public DebugGroupSpec(String id) { + this.id = id; + } + + public DebugGroupSpec title(String keyOrLiteral) { + this.titleKey = keyOrLiteral; + return this; + } + + public DebugGroupSpec accent(int rgb) { + this.accent = rgb & 0xFFFFFF; + return this; + } + + public DebugGroupSpec side(int side) { + this.side = side; + return this; + } + + public DebugGroupSpec order(int order) { + this.order = order; + return this; + } + + public String getId() { + return id; + } + + public String getTitleKey() { + return titleKey; + } + + public int getAccent() { + return accent; + } + + public int getSide() { + return side; + } + + public int getOrder() { + return order; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenApi.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenApi.java new file mode 100644 index 000000000..df8bd6330 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenApi.java @@ -0,0 +1,29 @@ +package com.gtnewhorizons.angelica.api.debug; + +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; + +public final class DebugScreenApi { + + private DebugScreenApi() {} + + public static DebugGroupHandle registerGroup(DebugGroupSpec spec) { + DebugScreen.bootstrap(); + return DebugRegistry.MAIN.registerGroup(spec); + } + + public static DebugEntryHandle registerEntry(DebugEntrySpec spec) { + DebugScreen.bootstrap(); + return DebugRegistry.MAIN.registerEntry(spec); + } + + public static DebugGroupHandle group(String id) { + DebugScreen.bootstrap(); + return DebugRegistry.MAIN.groupById(id); + } + + public static DebugEntryHandle entry(String id) { + DebugScreen.bootstrap(); + return DebugRegistry.MAIN.entryById(id); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenDisplayer.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenDisplayer.java new file mode 100644 index 000000000..fa48ae1c2 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenDisplayer.java @@ -0,0 +1,8 @@ +package com.gtnewhorizons.angelica.api.debug; + +public interface DebugScreenDisplayer { + + DebugFact line(DebugGroupHandle group); + + DebugFact fact(DebugGroupHandle group, String name); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenEntry.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenEntry.java new file mode 100644 index 000000000..f13af6bfa --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenEntry.java @@ -0,0 +1,6 @@ +package com.gtnewhorizons.angelica.api.debug; + +public interface DebugScreenEntry { + + void display(DebugScreenDisplayer displayer, DebugContext context); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenEntryStatus.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenEntryStatus.java new file mode 100644 index 000000000..4ed26357f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/DebugScreenEntryStatus.java @@ -0,0 +1,23 @@ +package com.gtnewhorizons.angelica.api.debug; + +public enum DebugScreenEntryStatus { + + ALWAYS_ON("alwaysOn"), + IN_OVERLAY("inOverlay"), + NEVER("never"); + + private static final DebugScreenEntryStatus[] VALUES = values(); + + public final String serializedName; + + DebugScreenEntryStatus(String serializedName) { + this.serializedName = serializedName; + } + + public static DebugScreenEntryStatus bySerializedName(String name) { + for (final DebugScreenEntryStatus status : VALUES) { + if (status.serializedName.equals(name)) return status; + } + return null; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/debug/package-info.java b/src/main/java/com/gtnewhorizons/angelica/api/debug/package-info.java new file mode 100644 index 000000000..07d6b6d43 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/debug/package-info.java @@ -0,0 +1,4 @@ +@API(owner = "angelica", provides = "angelica|debug", apiVersion = "1") +package com.gtnewhorizons.angelica.api.debug; + +import cpw.mods.fml.common.API; diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java b/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java index 623f91631..a185344f5 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java @@ -1,6 +1,7 @@ package com.gtnewhorizons.angelica.client.font; import com.google.common.collect.ImmutableSet; +import com.gtnewhorizon.gtnhlib.bytebuf.Pointer; import com.gtnewhorizon.gtnhlib.client.renderer.vao.IndexBuffer; import com.gtnewhorizon.gtnhlib.util.font.FontRendering; import com.gtnewhorizons.angelica.config.AngelicaConfig; @@ -48,17 +49,17 @@ import java.util.Objects; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAlloc; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAllocFloat; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memCopy; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memFree; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetByte; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetFloat; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutAddress; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutByte; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutInt; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutFloat; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutShort; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memRealloc; import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.FORMATTING_CHAR; -import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.GRADIENT_PAYLOAD; -import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.SECTION_X_LENGTH; -import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.SECTION_X_PAYLOAD; import static com.gtnewhorizons.angelica.client.font.FontStrategist.INVALID_TEXTURE; import static com.gtnewhorizons.angelica.client.font.FontStrategist.bindIntTexture; @@ -150,6 +151,8 @@ public class BatchingFontRenderer { // [v, v, t, t, c, c, c, c, tb, tb, tb, tb] // v, t and tb are floats, c is bytes; 36 bytes total private static final int VERTEX_SIZE = 36; + static final int QUAD_BYTES = 4 * VERTEX_SIZE; + private static final int GLYPH_FIXED_QUADS = 4; /** {@code OpenGlHelper.lightmapTexUnit} */ private static final int LIGHTMAP_TEX_UNIT = 1; @@ -176,9 +179,29 @@ public class BatchingFontRenderer { private static int vaoBase = -1; private int batchDepth = 0; + private GLContextState batchCtx; private static int vertexDataPos = 0; private static int idxWriterIndex = 0; + private static int arenaEpoch = 0; + + private TextMesh capturing; + private int captureVertexPos; + private int captureIdx; + private int captureCmdStart; + private int captureSealedEnd; + private int captureSegmentCount; + private boolean captureBatchMatrixValid; + private int captureWatermark; + private int captureEpoch; + private final Matrix4f captureProj = new Matrix4f(); + private final Matrix4f captureModelView = new Matrix4f(); + private final TextDrawState captureDrawState = new TextDrawState(); + private boolean captureLightmapActive; + private float captureLightmapU; + private float captureLightmapV; + private int captureLightmapTexture; + private int captureLightmapPackedLight; private final ObjectArrayList batchCommands = ObjectArrayList.wrap(new FontDrawCmd[64], 0); private final ObjectArrayList batchCommandPool = ObjectArrayList.wrap(new FontDrawCmd[64], 0); @@ -213,22 +236,22 @@ public class BatchingFontRenderer { * GUI/HUD text uses an orthographic projection and must never be shaded by world lighting - * mods rely on getting back the exact color they requested (e.g. GTNHLib's ColorResource). */ - private static boolean isWorldSpaceText() { - return GLStateManager.getProjectionMatrix().m33() == 0.0f; + private boolean isWorldSpaceText() { + return batchCtx.projectionMatrix.m33() == 0.0f; } private void captureLightmapState() { - final int unitBinding = GLStateManager.getTextures().getTextureUnitBindings(LIGHTMAP_TEX_UNIT).getBinding(); + final GLContextState glCtx = batchCtx; + final int unitBinding = glCtx.textures.getTextureUnitBindings(LIGHTMAP_TEX_UNIT).getBinding(); final boolean active = isWorldSpaceText() && unitBinding != 0 - && GLStateManager.getTextures().getTextureUnitStates(LIGHTMAP_TEX_UNIT).isEnabled(); + && glCtx.textures.getTextureUnitStates(LIGHTMAP_TEX_UNIT).isEnabled(); float u = 0.0f; float v = 0.0f; int texture = 0; if (active) { - final GLContextState glCtx = GLStateManager.ctx(); - scratchLightmapUv.set(glCtx.lastBrightnessX, glCtx.lastBrightnessY, 0.0f, 1.0f).mul(GLStateManager.getTextures().getTextureUnitMatrix(LIGHTMAP_TEX_UNIT)); + scratchLightmapUv.set(glCtx.lastBrightnessX, glCtx.lastBrightnessY, 0.0f, 1.0f).mul(glCtx.textures.getTextureUnitMatrix(LIGHTMAP_TEX_UNIT)); u = scratchLightmapUv.x; v = scratchLightmapUv.y; texture = unitBinding; @@ -247,7 +270,7 @@ public class BatchingFontRenderer { } private void captureDrawState() { - liveDrawState.captureLive(); + liveDrawState.captureLive(batchCtx); if (liveDrawState.sameAs(drawState)) return; sealBatchSegment(); drawState.set(liveDrawState); @@ -270,7 +293,7 @@ public class BatchingFontRenderer { } // 16-bit EBO index range - private static final int QUAD_BUDGET = 65536 / 4; + static final int QUAD_BUDGET = 65536 / 4; private static int deferredVertexPos; private static int deferredIdxPos; @@ -283,16 +306,18 @@ public class BatchingFontRenderer { private final Matrix4f batchProj = new Matrix4f(); private final Matrix4f batchModelView = new Matrix4f(); private boolean batchMatrixValid; + private GLContextState batchMatrixCtx; + private int batchProjGeneration; + private int batchMvGeneration; private final ObjectArrayList batchSegments = ObjectArrayList.wrap(new TextSegment[16], 0); private int batchSealedEnd; private static BatchingFontRenderer arenaOwner; private void allocateBuffers() { - populateEBO(rawCapacity / VERTEX_SIZE); + populateEBO(rawCapacity / QUAD_BYTES); } - private void populateEBO(int capacity) { - final int quadCount = capacity * 6; + private void populateEBO(int quadCount) { final ByteBuffer data = memAlloc(quadCount * 6 * 2); long ptr = memAddress0(data); for (int i = 0; i < quadCount; i++) { @@ -326,6 +351,13 @@ public class BatchingFontRenderer { if (streamRing != null) { streamRing.endFrame(); } + RetainedTextStore.endFrame(); + } + + static void forgetVertexSource(int buffer) { + if (vaoBuffer == buffer) { + vaoBuffer = -1; + } } private static void uploadVertexData(int limitPos) { @@ -346,16 +378,23 @@ public class BatchingFontRenderer { pendingBase = 0; } - private void ensureCapacity() { - if (vertexDataPos + (4 * VERTEX_SIZE) > rawCapacity) { - rawCapacity *= 2; - vertexData = memRealloc(vertexData, rawCapacity); - vertexDataAddress = memAddress0(vertexData); - - allocateBuffers(); + private void ensureCapacity(int bytes) { + if ((long) vertexDataPos + bytes > rawCapacity) { + grow((long) vertexDataPos + bytes); } } + private void grow(long needed) { + long capacity = rawCapacity; + while (capacity < needed) { + capacity *= 2; + } + rawCapacity = Math.toIntExact(capacity); + vertexData = memRealloc(vertexData, rawCapacity); + vertexDataAddress = memAddress0(vertexData); + allocateBuffers(); + } + private void pushVtx(float x, float y, int rgba, float u, float v, float uMin, float uMax, float vMin, float vMax) { final long ptr = vertexDataAddress + vertexDataPos; rgba = applyLighting(rgba); @@ -385,7 +424,6 @@ public class BatchingFontRenderer { } private void pushUntexRect(float x, float y, float w, float h, int rgba) { - ensureCapacity(); pushVtx(x, y, rgba, 0, 0, 0, 0, 0, 0); pushVtx(x, y + h, rgba, 0, 0, 0, 0, 0, 0); pushVtx(x + w, y, rgba, 0, 0, 0, 0, 0, 0); @@ -396,8 +434,6 @@ public class BatchingFontRenderer { private static final float[] MAX_AA_REACH = {0f, 6f, 8f}; private void pushTexRect(float x, float y, float w, float h, float itOff, int rgba, float uStart, float vStart, float uSz, float vSz, boolean flipV) { - ensureCapacity(); - final float tBu0 = uStart; final float tBu1 = uStart + uSz; final float tBv0 = vStart; @@ -436,12 +472,17 @@ public class BatchingFontRenderer { } private void pushDrawCmd(int startIdx, int idxCount, int texture, boolean isUnicode, int layer) { + pushDrawCmd(startIdx, idxCount, texture, isUnicode, layer, null, 0, 0); + } + + private void pushDrawCmd(int startIdx, int idxCount, int texture, boolean isUnicode, int layer, TextMesh mesh, int buffer, int baseVertex) { arenaOwner = this; - // Never coalesce into a command below the watermark - it belongs to a sealed segment. - if (batchCommands.size() > batchSealedEnd) { - final FontDrawCmd lastCmd = batchCommands.get(batchCommands.size() - 1); + // Never coalesce across the sealed watermark or the capture start: both are command boundaries. + final int size = batchCommands.size(); + if (size > batchSealedEnd && (capturing == null || size > captureCmdStart)) { + final FontDrawCmd lastCmd = batchCommands.get(size - 1); final int prevEndVtx = lastCmd.startVtx + lastCmd.idxCount; - if (prevEndVtx == startIdx && lastCmd.texture == texture && lastCmd.layer == layer) { + if (prevEndVtx == startIdx && lastCmd.texture == texture && lastCmd.layer == layer && lastCmd.mesh == mesh && lastCmd.buffer == buffer && lastCmd.baseVertex == baseVertex) { // Coalesce into one lastCmd.idxCount += idxCount; return; @@ -453,10 +494,13 @@ public class BatchingFontRenderer { } } final FontDrawCmd cmd = batchCommandPool.pop(); - cmd.reset(startIdx, idxCount, texture, isUnicode, layer); + cmd.reset(startIdx, idxCount, texture, isUnicode, layer, mesh, buffer, baseVertex); + cmd.seq = pushSequence++; batchCommands.add(cmd); } + private static long pushSequence; + private static final class FontDrawCmd { public int startVtx; @@ -469,17 +513,28 @@ public class BatchingFontRenderer { * needs a lower layer rather than an earlier push. */ public int layer; + public long seq; + public TextMesh mesh; + public int buffer; + public int baseVertex; public void reset(int startVtx, int vtxCount, int texture, boolean isUnicode) { reset(startVtx, vtxCount, texture, isUnicode, LAYER_DEFAULT); } public void reset(int startVtx, int vtxCount, int texture, boolean isUnicode, int layer) { + reset(startVtx, vtxCount, texture, isUnicode, layer, null, 0, 0); + } + + public void reset(int startVtx, int vtxCount, int texture, boolean isUnicode, int layer, TextMesh mesh, int buffer, int baseVertex) { this.layer = layer; this.startVtx = startVtx; this.idxCount = vtxCount; this.texture = texture; this.isUnicode = isUnicode; + this.mesh = mesh; + this.buffer = buffer; + this.baseVertex = baseVertex; } @Override @@ -512,7 +567,7 @@ public class BatchingFontRenderer { public static final Comparator DRAW_ORDER_COMPARATOR = Comparator.comparingInt((FontDrawCmd fdc) -> fdc.layer) .thenComparingInt(fdc -> fdc.texture == 0 ? Integer.MAX_VALUE : fdc.texture) - .thenComparingInt(fdc -> fdc.startVtx); + .thenComparingLong(fdc -> fdc.seq); } /** @@ -523,10 +578,11 @@ public class BatchingFontRenderer { if (arenaOwner != null && arenaOwner != this) { arenaOwner.flushBatch(); } + batchCtx = GLStateManager.ctx(); if (batchDepth > 0) { if (batchCommands.size() == batchSealedEnd) { captureBatchMatrix(); - } else if (!batchProj.equals(GLStateManager.getProjectionMatrix(), 0.0f) || !batchModelView.equals(GLStateManager.getModelViewMatrix(), 0.0f)) { + } else if (!batchMatrixCurrent() && (!batchProj.equals(batchCtx.projectionMatrix, 0.0f) || !batchModelView.equals(batchCtx.modelViewMatrix, 0.0f))) { sealBatchSegment(); captureBatchMatrix(); } @@ -535,11 +591,18 @@ public class BatchingFontRenderer { } private void captureBatchMatrix() { - batchProj.set(GLStateManager.getProjectionMatrix()); - batchModelView.set(GLStateManager.getModelViewMatrix()); + batchProj.set(batchCtx.projectionMatrix); + batchModelView.set(batchCtx.modelViewMatrix); + batchMatrixCtx = batchCtx; + batchProjGeneration = batchCtx.projGeneration; + batchMvGeneration = batchCtx.mvGeneration; batchMatrixValid = true; } + private boolean batchMatrixCurrent() { + return batchMatrixCtx == batchCtx && batchProjGeneration == batchCtx.projGeneration && batchMvGeneration == batchCtx.mvGeneration; + } + private void resolveMvp(Matrix4f dest) { if (batchMatrixValid) { batchProj.mul(batchModelView, dest); @@ -593,10 +656,14 @@ public class BatchingFontRenderer { batchDepth--; if (batchDepth == 0) { // We finished any nested batches. - if (shouldDeferNow()) { - deferBatch(); - } else { - flushBatch(); + try { + if (shouldDeferNow()) { + deferBatch(); + } else { + flushBatch(); + } + } finally { + batchCtx = null; } } } @@ -621,6 +688,8 @@ public class BatchingFontRenderer { public void resetAfterCrash() { discardDeferredText(); batchDepth = 0; + batchCtx = null; + capturing = null; deferredCmdWatermark = 0; deferredVertexPos = 0; deferredIdxPos = 0; @@ -718,7 +787,9 @@ public class BatchingFontRenderer { } private static void flushDeferredTextInner() { - uploadVertexData(deferredVertexPos); + if (deferredVertexPos > 0) { + uploadVertexData(deferredVertexPos); + } final int d = GLStateManager.pushState(StateSet.FONT); try { @@ -766,6 +837,7 @@ public class BatchingFontRenderer { vertexDataPos = 0; idxWriterIndex = 0; arenaOwner = null; + arenaEpoch++; } private void clearDeferredCommands() { @@ -829,12 +901,14 @@ public class BatchingFontRenderer { private void flushBatchInner() { final int cmdCount = batchCommands.size(); final int segmentCount = batchSegments.size(); - if (segmentCount == 0 && (vertexDataPos == deferredVertexPos || cmdCount == deferredCmdWatermark)) { + if (segmentCount == 0 && cmdCount == deferredCmdWatermark) { truncateBatchToWatermark(); return; } - uploadVertexData(vertexDataPos); + if (vertexDataPos > deferredVertexPos) { + uploadVertexData(vertexDataPos); + } final FontDrawCmd[] cmds = batchCommands.elements(); for (int i = 0; i < segmentCount; i++) { @@ -926,17 +1000,19 @@ public class BatchingFontRenderer { } else { GLStateManager.glBindVertexArray(fontVAO); } + } - if (vaoBuffer != pendingBuffer || vaoBase != pendingBase) { - GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, pendingBuffer); + private static void bindVertexSource(int buffer, int base) { + if (vaoBuffer != buffer || vaoBase != base) { + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer); - GLStateManager.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, VERTEX_SIZE, pendingBase); - GLStateManager.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, VERTEX_SIZE, pendingBase + 8); - GLStateManager.glVertexAttribPointer(2, 4, GL11.GL_UNSIGNED_BYTE, true, VERTEX_SIZE, pendingBase + 16); - GLStateManager.glVertexAttribPointer(3, 4, GL11.GL_FLOAT, false, VERTEX_SIZE, pendingBase + 20); + GLStateManager.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, VERTEX_SIZE, base); + GLStateManager.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, VERTEX_SIZE, base + 8); + GLStateManager.glVertexAttribPointer(2, 4, GL11.GL_UNSIGNED_BYTE, true, VERTEX_SIZE, base + 16); + GLStateManager.glVertexAttribPointer(3, 4, GL11.GL_FLOAT, false, VERTEX_SIZE, base + 20); - vaoBuffer = pendingBuffer; - vaoBase = pendingBase; + vaoBuffer = buffer; + vaoBase = base; } } @@ -974,6 +1050,26 @@ public class BatchingFontRenderer { } } + private static int multiDrawCapacity = 64; + private static ByteBuffer multiDrawCounts = memAlloc(multiDrawCapacity * 4); + private static ByteBuffer multiDrawIndices = memAlloc(multiDrawCapacity * Pointer.POINTER_SIZE); + private static ByteBuffer multiDrawBaseVertices = memAlloc(multiDrawCapacity * 4); + private static long multiDrawCountsAddress = memAddress0(multiDrawCounts); + private static long multiDrawIndicesAddress = memAddress0(multiDrawIndices); + private static long multiDrawBaseVerticesAddress = memAddress0(multiDrawBaseVertices); + + private static void growMultiDraw(int needed) { + int capacity = multiDrawCapacity; + while (capacity < needed) capacity *= 2; + multiDrawCounts = memRealloc(multiDrawCounts, capacity * 4); + multiDrawIndices = memRealloc(multiDrawIndices, capacity * Pointer.POINTER_SIZE); + multiDrawBaseVertices = memRealloc(multiDrawBaseVertices, capacity * 4); + multiDrawCountsAddress = memAddress0(multiDrawCounts); + multiDrawIndicesAddress = memAddress0(multiDrawIndices); + multiDrawBaseVerticesAddress = memAddress0(multiDrawBaseVertices); + multiDrawCapacity = capacity; + } + private static void drawCommands(FontDrawCmd[] cmdsData, int from, int to, BatchingFontRenderer owner) { for (int i = from; i < to; i++) { final FontDrawCmd cmd = cmdsData[i]; @@ -988,14 +1084,42 @@ public class BatchingFontRenderer { } flushLastTexture = cmd.texture; } - GLStateManager.glDrawElements(GL11.GL_TRIANGLES, cmd.idxCount, GL11.GL_UNSIGNED_SHORT, (long) cmd.startVtx * 2L); + if (cmd.mesh == null) { + bindVertexSource(pendingBuffer, pendingBase); + GLStateManager.glDrawElements(GL11.GL_TRIANGLES, cmd.idxCount, GL11.GL_UNSIGNED_SHORT, (long) cmd.startVtx * 2L); + continue; + } + int end = i + 1; + while (end < to) { + final FontDrawCmd next = cmdsData[end]; + if (next.mesh == null || next.buffer != cmd.buffer || next.texture != cmd.texture) break; + end++; + } + bindVertexSource(cmd.buffer, 0); + // GLSM's glDrawElementsBaseVertex and glMultiDrawElementsBaseVertex skip glDrawElements' preDraw (pending program, deferred blend, entity ids, deferred attribs); drop this and those go stale. + GLStateManager.preDraw(GLStateManager.ctx(), GL11.GL_TRIANGLES); + final int runLength = end - i; + if (runLength == 1) { + GLStateManager.glDrawElementsBaseVertex(GL11.GL_TRIANGLES, cmd.idxCount, GL11.GL_UNSIGNED_SHORT, (long) cmd.startVtx * 2L, cmd.baseVertex); + } else { + if (runLength > multiDrawCapacity) growMultiDraw(runLength); + for (int k = 0; k < runLength; k++) { + final FontDrawCmd run = cmdsData[i + k]; + memPutInt(multiDrawCountsAddress + (long) k * 4L, run.idxCount); + memPutAddress(multiDrawIndicesAddress + (long) k * Pointer.POINTER_SIZE, (long) run.startVtx * 2L); + memPutInt(multiDrawBaseVerticesAddress + (long) k * 4L, run.baseVertex); + } + GLStateManager.glMultiDrawElementsBaseVertex(GL11.GL_TRIANGLES, multiDrawCountsAddress, GL11.GL_UNSIGNED_SHORT, + multiDrawIndicesAddress, runLength, multiDrawBaseVerticesAddress); + } + i = end - 1; } } private void flushBatchThroughPipeline() { final int cmdCount = batchCommands.size(); final int segmentCount = batchSegments.size(); - if (segmentCount == 0 && (vertexDataPos == deferredVertexPos || cmdCount == deferredCmdWatermark)) { + if (segmentCount == 0 && cmdCount == deferredCmdWatermark) { truncateBatchToWatermark(); return; } @@ -1093,9 +1217,11 @@ public class BatchingFontRenderer { private void emitCommand(Tessellator tessellator, FontDrawCmd cmd) { final int firstVertex = cmd.startVtx / 6 * 4; final int quadCount = cmd.idxCount / 6; + final long source = cmd.mesh != null ? cmd.mesh.address : vertexDataAddress; + if (source == 0L) return; for (int q = 0; q < quadCount; q++) { - final long base = vertexDataAddress + (long) (firstVertex + q * 4) * VERTEX_SIZE; + final long base = source + (long) (firstVertex + q * 4) * VERTEX_SIZE; emitVertex(tessellator, base); emitVertex(tessellator, base + VERTEX_SIZE); emitVertex(tessellator, base + 3L * VERTEX_SIZE); @@ -1123,6 +1249,7 @@ public class BatchingFontRenderer { batchMatrixValid = false; vertexDataPos = deferredVertexPos; idxWriterIndex = deferredIdxPos; + arenaEpoch++; resetLightmapState(); if (arenaOwner == this) { arenaOwner = null; @@ -1165,26 +1292,19 @@ public class BatchingFontRenderer { // === Actual text mesh generation - public static boolean charInRange(char what, char fromInclusive, char toInclusive) { - return (what >= fromInclusive) && (what <= toInclusive); - } - - /** - * Count visible characters from {@code start} to {@code end}, skipping format codes. - * Stops at §r, any color code (§0-f, §x), or any color effect (§q, §g). - * Style codes (§k-o) and non-terminating effects (§z wave, §v dinnerbone) are skipped. - */ + /** Visible chars up to the next color, reset, rainbow or gradient code; format payloads skipped. */ private static int countVisibleChars(CharSequence str, int start, int end) { int count = 0; for (int i = start; i < end; i++) { char ch = str.charAt(i); if (ch == FORMATTING_CHAR && i + 1 < end) { - char code = Character.toLowerCase(str.charAt(i + 1)); - if (code == 'r' || (code >= '0' && code <= '9') || (code >= 'a' && code <= 'f') - || code == 'x' || code == 'q' || code == 'g') { + final long fmt = FormatCodes.parse(str, i + 1, end); + final int kind = FormatCodes.kind(fmt); + if (kind == FormatCodes.HEX || kind == FormatCodes.COLOR || kind == FormatCodes.RAINBOW + || kind == FormatCodes.GRADIENT || kind == FormatCodes.RESET) { break; } - i++; + i += 1 + FormatCodes.extraChars(fmt); } else { count++; } @@ -1224,11 +1344,11 @@ public class BatchingFontRenderer { } public float getGlyphScaleX() { - return forceDefaults() ? 1 : (float) (FontConfig.glyphScale * Math.pow(2, FontConfig.glyphAspect)); + return forceDefaults() ? 1 : GlyphScale.current().x(); } public float getGlyphScaleY() { - return forceDefaults() ? 1 : (float) (FontConfig.glyphScale / Math.pow(2, FontConfig.glyphAspect)); + return forceDefaults() ? 1 : GlyphScale.current().y(); } public float getGlyphSpacing() { @@ -1258,13 +1378,15 @@ public class BatchingFontRenderer { : inputColor; final int shadowColor = (color & 0xfcfcfc) >> 2 | color & 0xff000000; - FontProviderMC.get(this.isSGA).charWidth = this.charWidth; - FontProviderMC.get(this.isSGA).locationFontTexture = this.locationFontTexture; + primeMcProvider(); this.beginBatch(); this.captureLightmapState(); this.captureDrawState(); - this.lightingFactorActive = isWorldSpaceText() && FFPVertexLighting.modulatesVertexColor(this.lightingFactor); + final boolean lit = updateLightingFactor(); + if (lit || recolorActive()) { + markAnimated(); + } float curX = anchorX; try { final int totalStringLength = string.length(); @@ -1274,6 +1396,9 @@ public class BatchingFontRenderer { return 0; } final int stringEnd = stringOffset + stringLength; + final int shadowCopies = FontConfig.shadowCopies; + final int boldCopies = FontConfig.boldCopies; + final int glyphReserve = (GLYPH_FIXED_QUADS + 2 * shadowCopies + boldCopies) * QUAD_BYTES; int curColor = color; int curShadowColor = shadowColor; @@ -1309,10 +1434,11 @@ public class BatchingFontRenderer { float strikethroughEndX = 0.0f; for (int charIdx = stringOffset; charIdx < stringEnd; charIdx++) { + ensureCapacity(glyphReserve); char chr = string.charAt(charIdx); if (chr == FORMATTING_CHAR && (charIdx + 1) < stringEnd) { - final char fmtCode = Character.toLowerCase(string.charAt(charIdx + 1)); - charIdx++; + final long fmt = FormatCodes.parse(string, charIdx + 1, stringEnd); + charIdx += 1 + FormatCodes.extraChars(fmt); if (curUnderline && underlineStartX != underlineEndX) { final int ulIdx = idxWriterIndex; @@ -1332,9 +1458,9 @@ public class BatchingFontRenderer { strikethroughStartX = strikethroughEndX; } - if (fmtCode == 'x' && AngelicaConfig.enableRGBColors && charIdx + SECTION_X_PAYLOAD < stringEnd) { - int rgb = ColorCodeUtils.parseHexPairs(string, charIdx + 1, 6); - if (rgb != -1) { + switch (FormatCodes.kind(fmt)) { + case FormatCodes.HEX -> { + final int rgb = FormatCodes.rgb(fmt); curRainbow = false; curGradient = false; curCustomEffects = 0L; @@ -1349,15 +1475,11 @@ public class BatchingFontRenderer { } curColor = (curColor & 0xFF000000) | (rgb & 0x00FFFFFF); curShadowColor = (curShadowColor & 0xFF000000) | ((rgb & 0xFCFCFC) >> 2); - charIdx += SECTION_X_PAYLOAD; if (darkModeRecolorEnabled) { curShadow = false; } } - } else { - final boolean is09 = charInRange(fmtCode, '0', '9'); - final boolean isAF = charInRange(fmtCode, 'a', 'f'); - if (is09 || isAF) { + case FormatCodes.COLOR -> { curRandom = false; curBold = false; curStrikethrough = false; @@ -1367,8 +1489,7 @@ public class BatchingFontRenderer { curGradient = false; curCustomEffects = 0L; // wave/dinnerbone NOT reset — they're positional effects, independent of color - - final int colorIdx = is09 ? (fmtCode - '0') : (fmtCode - 'a' + 10); + final int colorIdx = FormatCodes.colorIndex(fmt); final int rgb = this.colorCode[colorIdx]; curColor = (curColor & 0xFF000000) | (rgb & 0x00FFFFFF); final int shadowRgb = this.colorCode[colorIdx + 16]; @@ -1376,61 +1497,51 @@ public class BatchingFontRenderer { if (darkModeRecolorEnabled) { curShadow = false; } - } else if (fmtCode == 'k') { - curRandom = true; - } else if (fmtCode == 'l') { - curBold = true; - } else if (fmtCode == 'm') { + } + case FormatCodes.OBFUSCATED -> curRandom = true; + case FormatCodes.BOLD -> curBold = true; + case FormatCodes.STRIKETHROUGH -> { curStrikethrough = true; strikethroughStartX = curX - 1.0f; strikethroughEndX = strikethroughStartX; - } else if (fmtCode == 'n') { + } + case FormatCodes.UNDERLINE -> { curUnderline = true; underlineStartX = curX - 1.0f; underlineEndX = underlineStartX; - } else if (fmtCode == 'o') { - curItalic = true; - } else if (fmtCode == 'q' && AngelicaConfig.enableRainbow) { + } + case FormatCodes.ITALIC -> curItalic = true; + case FormatCodes.RAINBOW -> { curRainbow = true; curGradient = false; curCustomEffects = 0L; rainbowCharIndex = 0; - } else if (fmtCode == 'z' && AngelicaConfig.enableWaveText) { - curWave = !curWave; - } else if (fmtCode == 'v' && AngelicaConfig.enableDinnerboneText) { - curDinnerbone = !curDinnerbone; - } else if (fmtCode == 'u' && AngelicaConfig.enableDropShadow) { - int customRgb = (charIdx + SECTION_X_LENGTH < stringEnd) - ? ColorCodeUtils.parseSectionXAt(string, charIdx + 1) - : -1; - if (customRgb != -1) { - curShadow = true; - curShadowCustomColor = true; - curShadowColorOverride = customRgb; - charIdx += SECTION_X_LENGTH; - } else { - curShadow = !curShadow; - if (!curShadow) curShadowCustomColor = false; - } - } else if (fmtCode == 'g' && AngelicaConfig.enableGradients && charIdx + GRADIENT_PAYLOAD < stringEnd) { - int color1 = ColorCodeUtils.parseSectionXAt(string, charIdx + 1); - int color2 = ColorCodeUtils.parseSectionXAt(string, charIdx + 1 + SECTION_X_LENGTH); - if (color1 != -1 && color2 != -1) { - curGradient = true; - curRainbow = false; - curCustomEffects = 0L; - gradientStartRgb = color1; - gradientEndRgb = color2; - gradientCharIndex = 0; - gradientTotalChars = countVisibleChars(string, charIdx + 1 + GRADIENT_PAYLOAD, stringEnd); - gradientStep = gradientTotalChars > 1 ? 1f / (gradientTotalChars - 1) : 0f; - charIdx += GRADIENT_PAYLOAD; - } - } else if (FontEffectRegistry.isRegistered(fmtCode)) { - // Toggles, like wave and dinnerbone, so a span can be closed - // without a colour or §r. Those still clear it as before. - curCustomEffects ^= FontEffectRegistry.bit(fmtCode); - } else if (fmtCode == 'r') { + } + case FormatCodes.WAVE -> curWave = !curWave; + case FormatCodes.DINNERBONE -> curDinnerbone = !curDinnerbone; + case FormatCodes.SHADOW_COLOR -> { + curShadow = true; + curShadowCustomColor = true; + curShadowColorOverride = FormatCodes.rgb(fmt); + } + case FormatCodes.SHADOW_TOGGLE -> { + curShadow = !curShadow; + if (!curShadow) curShadowCustomColor = false; + } + case FormatCodes.GRADIENT -> { + curGradient = true; + curRainbow = false; + curCustomEffects = 0L; + gradientStartRgb = FormatCodes.gradientFrom(fmt); + gradientEndRgb = FormatCodes.gradientTo(fmt); + gradientCharIndex = 0; + gradientTotalChars = countVisibleChars(string, charIdx + 1, stringEnd); + gradientStep = gradientTotalChars > 1 ? 1f / (gradientTotalChars - 1) : 0f; + } + // Toggles, like wave and dinnerbone, so a span can be closed + // without a colour or §r. Those still clear it as before. + case FormatCodes.CUSTOM -> curCustomEffects ^= FormatCodes.customBit(fmt); + case FormatCodes.RESET -> { curRandom = false; curBold = false; curStrikethrough = false; @@ -1446,7 +1557,8 @@ public class BatchingFontRenderer { curColor = color; curShadowColor = shadowColor; } - } // close else block for non-§x codes + default -> { } + } continue; } @@ -1456,7 +1568,7 @@ public class BatchingFontRenderer { chr = FontStrategist.replaceCustomGlyph(this, chr, unicodeFlag); // ASCII space, NBSP, NNBSP, decided before obfuscation as vanilla does. - final boolean whitespace = chr == ' ' || chr == '\u00A0' || chr == '\u202F'; + final boolean whitespace = isWhitespace(chr); FontProvider fontProvider = FontStrategist.getFontProvider(this, chr, FontConfig.enableCustomFont, unicodeFlag); @@ -1464,16 +1576,16 @@ public class BatchingFontRenderer { // and to the width it was measured at, under a custom or unicode font too. if (curRandom && !whitespace) { chr = fontProvider.getRandomReplacement(chr); + markAnimated(); } - heightNorth = anchorY + (underlying.FONT_HEIGHT - 1.0f) * (0.5f - glyphScaleY * fontProvider.getYScaleMultiplier() / 2) - + fontProvider.getBaselineShift() * glyphScaleY; - float heightSouth = (underlying.FONT_HEIGHT - 1.0f) * glyphScaleY * fontProvider.getYScaleMultiplier(); + heightNorth = glyphTop(fontProvider, anchorY, underlying.FONT_HEIGHT - 1.0f, glyphScaleY); + float heightSouth = glyphHeight(fontProvider, underlying.FONT_HEIGHT - 1.0f, glyphScaleY); visibleCharIndex++; if (whitespace) { - curX += 4 * this.getWhitespaceScale() + (curBold ? 1 : 0); + curX += whitespaceStep(curBold); continue; } @@ -1496,21 +1608,22 @@ public class BatchingFontRenderer { final float uStart = fontProvider.getUStart(chr); final float vStart = fontProvider.getVStart(chr); - final float xAdvance = fontProvider.getXAdvance(chr) * glyphScaleX; final float glyphW = fontProvider.getGlyphW(chr) * glyphScaleX; final float uSz = fontProvider.getUSize(chr); final float vSz = fontProvider.getVSize(chr); final float itOff = curItalic ? glyphScaleX : 0.0F; // italic offset final float shadowOffset = fontProvider.getShadowOffset(); - final int shadowCopies = FontConfig.shadowCopies; - final int boldCopies = FontConfig.boldCopies; final int texture = fontProvider.getTexture(chr); // Wave: Y offset via sine wave float renderY = heightNorth; if (curWave) { - float time = HUDCaching.renderingCacheOverride ? 0f : (float) ((System.nanoTime() & 0xFFFFFFFFFFFFL) * WAVE_TIME_SCALE); + final boolean frozen = HUDCaching.renderingCacheOverride; + float time = frozen ? 0f : (float) ((System.nanoTime() & 0xFFFFFFFFFFFFL) * WAVE_TIME_SCALE); renderY += (float) Math.sin(visibleCharIndex * WAVE_FREQUENCY + time) * AngelicaConfig.waveAmplitude; + if (!frozen) { + markAnimated(); + } } if (darkModeRecolorEnabled) { @@ -1538,6 +1651,9 @@ public class BatchingFontRenderer { final int background = effect.backgroundColor(visibleCharIndex); if (background != 0) glyphBackground = background; } + markAnimated(); + // Effects are third-party code that may re-enter drawString and spend the reserve taken at the loop top. + ensureCapacity(glyphReserve); } // Untextured, with its own draw command, like the underline below. @@ -1587,8 +1703,7 @@ public class BatchingFontRenderer { final int vtxCount = 4 * charCount; pushDrawCmd(idxId, vtxCount / 2 * 3, texture, chr > 255); - curX += (xAdvance + (curBold ? 1.0f : 0.0f)) + getGlyphSpacing(); - if (bookMode) { curX = (int) curX; } + curX = glyphPen(curX, glyphAdvance(fontProvider, chr, curBold, glyphScaleX)); underlineEndX = curX; strikethroughEndX = curX; @@ -1609,6 +1724,7 @@ public class BatchingFontRenderer { } } + ensureCapacity(2 * QUAD_BYTES); if (curUnderline && underlineStartX != underlineEndX) { final int ulIdx = idxWriterIndex; pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, curColor); @@ -1631,11 +1747,278 @@ public class BatchingFontRenderer { return curX + (enableShadow ? 1.0f : 0.0f); } + public void beginCapture(TextMesh mesh) { + if (capturing != null) { + throw new IllegalStateException("A text mesh capture is already open"); + } + mesh.captured = false; + mesh.animated = false; + mesh.settings = GlyphSettings.current(); + beginBatch(); + try { + captureLightmapState(); + captureDrawState(); + captureProj.set(batchCtx.projectionMatrix); + captureModelView.set(batchCtx.modelViewMatrix); + captureDrawState.set(drawState); + captureLightmapActive = lightmapActive; + captureLightmapU = lightmapU; + captureLightmapV = lightmapV; + captureLightmapTexture = lightmapTextureId; + captureLightmapPackedLight = lightmapPackedLight; + captureVertexPos = vertexDataPos; + captureIdx = idxWriterIndex; + captureCmdStart = batchCommands.size(); + captureSealedEnd = batchSealedEnd; + captureSegmentCount = batchSegments.size(); + captureBatchMatrixValid = batchMatrixValid; + captureWatermark = deferredCmdWatermark; + captureEpoch = arenaEpoch; + capturing = mesh; + } catch (RuntimeException e) { + endBatch(); + throw e; + } + } + + private boolean captureIntact() { + if (arenaEpoch != captureEpoch || batchSealedEnd != captureSealedEnd || deferredCmdWatermark != captureWatermark) { + return false; + } + if (!captureProj.equals(batchCtx.projectionMatrix, 0.0f) || !captureModelView.equals(batchCtx.modelViewMatrix, 0.0f)) { + return false; + } + if (batchMatrixValid && (!captureProj.equals(batchProj, 0.0f) || !captureModelView.equals(batchModelView, 0.0f))) { + return false; + } + return drawState.sameAs(captureDrawState) + && lightmapActive == captureLightmapActive + && lightmapU == captureLightmapU + && lightmapV == captureLightmapV + && lightmapTextureId == captureLightmapTexture + && lightmapPackedLight == captureLightmapPackedLight; + } + + public void endCapture() { + final TextMesh mesh = capturing; + if (mesh == null) { + throw new IllegalStateException("No text mesh capture is open"); + } + capturing = null; + try { + RetainedTextStore.release(mesh); + if (mesh.animated || !captureIntact()) { + return; + } + final int bytes = vertexDataPos - captureVertexPos; + mesh.reserveVertexBytes(bytes); + memCopy(vertexDataAddress + captureVertexPos, mesh.address, bytes); + mesh.quadCount = bytes / QUAD_BYTES; + mesh.commands = 0; + for (int i = captureCmdStart, n = batchCommands.size(); i < n; i++) { + final FontDrawCmd cmd = batchCommands.get(i); + mesh.addCommand(cmd.startVtx - captureIdx, cmd.idxCount, cmd.texture, cmd.isUnicode, cmd.layer); + } + mesh.captured = true; + } finally { + endBatch(); + } + } + + public void abortCapture() { + final TextMesh mesh = capturing; + if (mesh == null) { + throw new IllegalStateException("No text mesh capture is open"); + } + capturing = null; + try { + mesh.captured = false; + RetainedTextStore.release(mesh); + if (arenaEpoch != captureEpoch || deferredCmdWatermark != captureWatermark) { + return; + } + for (int i = batchSegments.size() - 1; i >= captureSegmentCount; i--) { + final TextSegment segment = batchSegments.get(i); + segment.owner = null; + deferredSegmentPool.add(segment); + } + batchSegments.size(captureSegmentCount); + batchSealedEnd = captureSealedEnd; + for (int i = batchCommands.size() - 1; i >= captureCmdStart; i--) { + batchCommandPool.add(batchCommands.get(i)); + } + batchCommands.size(captureCmdStart); + vertexDataPos = captureVertexPos; + idxWriterIndex = captureIdx; + drawState.set(captureDrawState); + lightmapActive = captureLightmapActive; + lightmapU = captureLightmapU; + lightmapV = captureLightmapV; + lightmapTextureId = captureLightmapTexture; + lightmapPackedLight = captureLightmapPackedLight; + batchMatrixValid = captureBatchMatrixValid; + if (batchMatrixValid) { + batchProj.set(captureProj); + batchModelView.set(captureModelView); + batchMatrixCtx = null; + } + } finally { + endBatch(); + } + } + + public boolean replay(TextMesh mesh, GlyphSettings live) { + EntityShadowReplay.rejectForeignDraw(); + if (mesh.animated || !mesh.isValid(live)) { + return false; + } + if (mesh.quadCount == 0) { + return true; + } + beginBatch(); + try { + if (updateLightingFactor()) { + return false; + } + captureLightmapState(); + captureDrawState(); + if (capturing == null && !shouldDrawThroughPipeline() && RetainedTextStore.ensure(mesh)) { + final int buffer = RetainedTextStore.buffer(); + final int baseVertex = mesh.regionStart * 4; + for (int i = 0, n = mesh.commands; i < n; i++) { + pushDrawCmd(mesh.commandStart[i], mesh.commandCount[i], mesh.commandTexture[i], mesh.commandUnicode[i], mesh.commandLayer[i], mesh, buffer, baseVertex); + } + return true; + } + final int bytes = mesh.quadCount * QUAD_BYTES; + ensureCapacity(bytes); + memCopy(mesh.address, vertexDataAddress + vertexDataPos, bytes); + final int idxBase = idxWriterIndex; + vertexDataPos += bytes; + idxWriterIndex += mesh.quadCount * 6; + for (int i = 0, n = mesh.commands; i < n; i++) { + pushDrawCmd(idxBase + mesh.commandStart[i], mesh.commandCount[i], mesh.commandTexture[i], mesh.commandUnicode[i], mesh.commandLayer[i]); + } + } finally { + endBatch(); + } + return true; + } + + public void fillRect(float x1, float y1, float x2, float y2, int argb) { + EntityShadowReplay.rejectForeignDraw(); + beginBatch(); + try { + captureLightmapState(); + captureDrawState(); + if (updateLightingFactor()) { + markAnimated(); + } + ensureCapacity(QUAD_BYTES); + final int idx = idxWriterIndex; + pushUntexRect(x1, y1, x2 - x1, y2 - y1, argb); + pushDrawCmd(idx, 6, 0, false, LAYER_BACKGROUND); + } finally { + endBatch(); + } + } + + public FontProvider glyphProvider(char chr, boolean unicodeFlag) { + primeMcProvider(); + return FontStrategist.getFontProvider(this, chr, FontConfig.enableCustomFont, unicodeFlag); + } + + public void bindGlyphTexture(int texture) { + bindIntTexture(this, texture); + } + + private boolean updateLightingFactor() { + return lightingFactorActive = isWorldSpaceText() && FFPVertexLighting.modulatesVertexColor(lightingFactor); + } + + static boolean recolorActive() { + return darkModeRecolorEnabled || buttonSectionActive; + } + + private void markAnimated() { + if (capturing != null) { + capturing.animated = true; + } + } + + private void primeMcProvider() { + final FontProviderMC mc = FontProviderMC.get(this.isSGA); + mc.charWidth = this.charWidth; + mc.locationFontTexture = this.locationFontTexture; + } + + private static boolean isWhitespace(char chr) { + return chr == ' ' || chr == '\u00A0' || chr == '\u202F'; + } + + private float whitespaceStep(boolean bold) { + return 4 * this.getWhitespaceScale() + (bold ? 1 : 0); + } + + public static float glyphTop(FontProvider provider, float anchorY, float lineHeight, float glyphScaleY) { + return anchorY + lineHeight * (0.5f - glyphScaleY * provider.getYScaleMultiplier() / 2) + provider.getBaselineShift() * glyphScaleY; + } + + public static float glyphHeight(FontProvider provider, float lineHeight, float glyphScaleY) { + return lineHeight * glyphScaleY * provider.getYScaleMultiplier(); + } + + public float glyphAdvance(FontProvider provider, char chr, boolean bold, float glyphScaleX) { + return (provider.getXAdvance(chr) * glyphScaleX + (bold ? 1.0f : 0.0f)) + getGlyphSpacing(); + } + + private float glyphPen(float curX, float step) { + curX += step; + return bookMode ? (int) curX : curX; + } + + private float glyphStep(char chr, boolean bold, boolean unicodeFlag, float glyphScaleX) { + if (chr == ColorCodeUtils.ESCAPED_AMPERSAND) { chr = '&'; } + chr = FontStrategist.replaceCustomGlyph(this, chr, unicodeFlag); + if (isWhitespace(chr)) { + return whitespaceStep(bold); + } + return glyphAdvance(FontStrategist.getFontProvider(this, chr, FontConfig.enableCustomFont, unicodeFlag), chr, bold, glyphScaleX); + } + + public float advance(char chr, boolean bold, boolean unicodeFlag) { + primeMcProvider(); + return glyphStep(chr, bold, unicodeFlag, getGlyphScaleX()); + } + + public float measure(CharSequence text, int start, int end, boolean unicodeFlag) { + primeMcProvider(); + final float glyphScaleX = getGlyphScaleX(); + float curX = 0.0f; + boolean bold = false; + for (int i = start; i < end; i++) { + char chr = text.charAt(i); + if (chr == FORMATTING_CHAR && i + 1 < end) { + final long fmt = FormatCodes.parse(text, i + 1, end); + i += 1 + FormatCodes.extraChars(fmt); + if (FormatCodes.kind(fmt) == FormatCodes.BOLD) { + bold = true; + } else if (FormatCodes.endsBold(fmt)) { + bold = false; + } + continue; + } + // Pen rounding is exact on whitespace only while book mode keeps whitespaceScale at 1 and curX starts at 0. + curX = glyphPen(curX, glyphStep(chr, bold, unicodeFlag, glyphScaleX)); + } + return curX; + } + public float getCharWidthFine(char chr) { if (chr == ColorCodeUtils.ESCAPED_AMPERSAND) { chr = '&'; } if (chr == FORMATTING_CHAR) { return -1; } - if (chr == ' ' || chr == '\u00A0' || chr == '\u202F') { + if (isWhitespace(chr)) { return 4 * this.getWhitespaceScale(); } diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistry.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistry.java index 5ff00cd02..0ce705a53 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistry.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistry.java @@ -48,10 +48,6 @@ public final class FontEffectRegistry { return anyRegistered && lowerCode >= 'a' && lowerCode <= 'z' && EFFECTS[lowerCode - 'a'] != null; } - static long bit(char lowerCode) { - return 1L << (lowerCode - 'a'); - } - static CustomGlyphEffect get(int bitIndex) { return EFFECTS[bitIndex]; } diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderBitmap.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderBitmap.java index bead77fbb..043e12462 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderBitmap.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderBitmap.java @@ -31,15 +31,19 @@ public final class FontProviderBitmap implements FontProvider { private static final ResourceLocation DEFINITION = new ResourceLocation("angelica", "font/modern.json"); private static final int DEFAULT_HEIGHT = 8; - private static final FontProviderBitmap[] NONE = new FontProviderBitmap[0]; + static final FontProviderBitmap[] NONE = new FontProviderBitmap[0]; private static volatile FontProviderBitmap[] loaded; public static void registerReloadListener() { - ((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(manager -> loaded = null); + ((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(manager -> { + loaded = null; + FontProviderCache.invalidate(); + }); } private final ResourceLocation sheet; + private int texture; private final int columns; private final float uSize; private final float vSize; @@ -60,7 +64,7 @@ public final class FontProviderBitmap implements FontProvider { return null; } - private static FontProviderBitmap[] providers() { + static FontProviderBitmap[] providers() { FontProviderBitmap[] local = loaded; if (local == null) { final IResourceManager resources = Minecraft.getMinecraft().getResourceManager(); @@ -229,7 +233,12 @@ public final class FontProviderBitmap implements FontProvider { @Override public int getTexture(char chr) { - return FontStrategist.getIntFromResourceLocation(this.sheet); + int texture = this.texture; + if (texture == 0) { + texture = FontStrategist.getIntFromResourceLocation(this.sheet); + this.texture = texture; + } + return texture; } @Override diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCache.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCache.java new file mode 100644 index 000000000..691447ef6 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCache.java @@ -0,0 +1,98 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizons.angelica.config.FontConfig; + +final class FontProviderCache { + + private static final byte UNRESOLVED = 0; + private static final byte MC = 1; + private static final byte UNICODE = 2; + private static final byte CUSTOM_PRIMARY = 3; + private static final byte CUSTOM_FALLBACK = 4; + private static final byte BITMAP = 5; + private static final int MAX_BITMAPS = Byte.MAX_VALUE - BITMAP; + private static final int VARIANT_COUNT = 8; + + private static FontProviderCache[] variants = new FontProviderCache[VARIANT_COUNT]; + private static int generation; + + private final boolean custom; + private final boolean unicode; + private final FontProviderBitmap[] bitmaps; + private final byte[][] pages = new byte[256][]; + + private FontProviderCache(boolean custom, boolean unicode, boolean modern) { + this.custom = custom; + this.unicode = unicode; + this.bitmaps = modern && !unicode ? FontProviderBitmap.providers() : FontProviderBitmap.NONE; + } + + static FontProvider lookup(char chr, boolean custom, boolean unicode) { + final int variant = (custom ? 4 : 0) | (unicode ? 2 : 0) | (FontConfig.enableModernFont ? 1 : 0); + final FontProviderCache[] tables = variants; + FontProviderCache table = tables[variant]; + if (table == null) { + table = new FontProviderCache(custom, unicode, (variant & 1) != 0); + tables[variant] = table; + } + return table.bitmaps.length > MAX_BITMAPS ? null : table.get(chr); + } + + static void invalidate() { + generation++; + variants = new FontProviderCache[VARIANT_COUNT]; + } + + static int generation() { + return generation; + } + + private FontProvider get(char chr) { + final int hi = chr >>> 8; + byte[] page = pages[hi]; + if (page == null) { + page = new byte[256]; + pages[hi] = page; + } + final int lo = chr & 0xFF; + byte id = page[lo]; + if (id == UNRESOLVED) { + id = resolve(chr); + page[lo] = id; + } + return switch (id) { + case MC -> FontProviderMC.get(false); + case UNICODE -> FontProviderUnicode.get(); + case CUSTOM_PRIMARY -> FontProviderCustom.getPrimary(); + case CUSTOM_FALLBACK -> FontProviderCustom.getFallback(); + default -> bitmaps[id - BITMAP]; + }; + } + + private byte resolve(char chr) { + if (custom) { + if (FontProviderCustom.getPrimary().isGlyphAvailable(chr)) { + return CUSTOM_PRIMARY; + } + if (FontProviderCustom.getFallback().isGlyphAvailable(chr)) { + return CUSTOM_FALLBACK; + } + final int bitmap = bitmapIndex(chr); + return bitmap >= 0 ? (byte) (BITMAP + bitmap) : UNICODE; + } + final int bitmap = bitmapIndex(chr); + if (bitmap >= 0) { + return (byte) (BITMAP + bitmap); + } + return !unicode && FontProviderMC.get(false).isGlyphAvailable(chr) ? MC : UNICODE; + } + + private int bitmapIndex(char chr) { + for (int i = 0; i < bitmaps.length; i++) { + if (bitmaps[i].isGlyphAvailable(chr)) { + return i; + } + } + return -1; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCustom.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCustom.java index 9eb73ca20..21fc2a785 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCustom.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderCustom.java @@ -69,6 +69,7 @@ public final class FontProviderCustom implements FontProvider { this.atlasTextures[i] = 0; } } + FontProviderCache.invalidate(); } public void reloadFont(int fontID) { diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderMC.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderMC.java index d21f31c83..5f3086ffd 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderMC.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderMC.java @@ -1,10 +1,12 @@ package com.gtnewhorizons.angelica.client.font; import com.gtnewhorizons.angelica.config.FontConfig; -import it.unimi.dsi.fastutil.chars.Char2ShortOpenHashMap; import jss.util.RandomXoshiro256StarStar; import net.minecraft.util.ResourceLocation; +import java.util.Arrays; +import java.util.Objects; + public final class FontProviderMC implements FontProvider { /** @@ -22,30 +24,38 @@ public final class FontProviderMC implements FontProvider { return (isSGA ? InstLoader.instanceSGA : InstLoader.instance); } - private static final Char2ShortOpenHashMap MCFONT_ASCII_MAP = new Char2ShortOpenHashMap(); + private static final short[] MCFONT_SLOTS; ResourceLocation locationFontTexture = null; + private record TextureSlot(ResourceLocation location, int id) {} + + private TextureSlot textureSlot = new TextureSlot(null, 0); private final RandomXoshiro256StarStar fontRandom = new RandomXoshiro256StarStar(); public int[] charWidth; static { - MCFONT_ASCII_MAP.defaultReturnValue((short) -1); + int maxChar = 0; + for (int i = 0; i < MCFONT_CHARS.length(); i++) { + maxChar = Math.max(maxChar, MCFONT_CHARS.charAt(i)); + } + MCFONT_SLOTS = new short[maxChar + 1]; + Arrays.fill(MCFONT_SLOTS, (short) -1); for (short i = 0; i < MCFONT_CHARS.length(); i++) { - MCFONT_ASCII_MAP.put(MCFONT_CHARS.charAt(i), i); + MCFONT_SLOTS[MCFONT_CHARS.charAt(i)] = i; } } public int lookupMcFontPosition(char ch) { - return MCFONT_ASCII_MAP.get(ch); + return ch < MCFONT_SLOTS.length ? MCFONT_SLOTS[ch] : -1; } @Override public boolean isGlyphAvailable(char chr) { - return MCFONT_ASCII_MAP.containsKey(chr); + return lookupMcFontPosition(chr) != -1; } /** * A random glyph of the same width, for {@code §k}. Only slots the reverse lookup - * agrees with are eligible: {@link #MCFONT_CHARS} repeats {@code U+0000} and the map + * agrees with are eligible: {@link #MCFONT_CHARS} repeats {@code U+0000} and the table * keeps the last, so other slots would measure from a different one. Bounded, since * a resource pack may ship widths where nothing matches. */ @@ -61,7 +71,7 @@ public final class FontProviderMC implements FontProvider { continue; } final char replacement = MCFONT_CHARS.charAt(candidate); - if (MCFONT_ASCII_MAP.get(replacement) == candidate) { + if (lookupMcFontPosition(replacement) == candidate) { return replacement; } } @@ -110,7 +120,13 @@ public final class FontProviderMC implements FontProvider { @Override public int getTexture(char chr) { - return FontStrategist.getIntFromResourceLocation(locationFontTexture); + final ResourceLocation location = locationFontTexture; + TextureSlot slot = textureSlot; + if (!Objects.equals(slot.location(), location)) { + slot = new TextureSlot(location, FontStrategist.getIntFromResourceLocation(location)); + textureSlot = slot; + } + return slot.id(); } @Override diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderUnicode.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderUnicode.java index 96588dbfa..f4d434c0c 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderUnicode.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontProviderUnicode.java @@ -34,6 +34,7 @@ public final class FontProviderUnicode implements FontProvider, IResourceManager public static FontProviderUnicode get() { return FontProviderUnicode.InstLoader.instance; } private static final ResourceLocation[] unicodePageLocations = new ResourceLocation[256]; + private static final int[] unicodePageTextures = new int[256]; private final byte[] glyphWidth = new byte[65536]; private final RandomXoshiro256StarStar fontRandom = new RandomXoshiro256StarStar(); @@ -129,8 +130,12 @@ public final class FontProviderUnicode implements FontProvider, IResourceManager @Override public int getTexture(char chr) { final int uniPage = chr / 256; - ResourceLocation rl = getUnicodePageLocation(uniPage); - return FontStrategist.getIntFromResourceLocation(rl); + int texture = unicodePageTextures[uniPage]; + if (texture == 0) { + texture = FontStrategist.getIntFromResourceLocation(getUnicodePageLocation(uniPage)); + unicodePageTextures[uniPage] = texture; + } + return texture; } @Override diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontStrategist.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontStrategist.java index fdf49c961..4dfa1b0b7 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontStrategist.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontStrategist.java @@ -161,6 +161,14 @@ public class FontStrategist { if (me.bookMode) { return FontProviderUnicode.get(); } + if (!me.isSplash && GLStateManager.isMainThread()) { + final FontProvider cached = FontProviderCache.lookup(chr, customFontEnabled, forceUnicode); + if (cached != null) { return cached; } + } + return searchFontProvider(me, chr, customFontEnabled, forceUnicode); + } + + static FontProvider searchFontProvider(BatchingFontRenderer me, char chr, boolean customFontEnabled, boolean forceUnicode) { if (customFontEnabled && !me.isSplash) { FontProvider fp; fp = FontProviderCustom.getPrimary(); @@ -235,23 +243,30 @@ public class FontStrategist { static final int INVALID_TEXTURE = Integer.MIN_VALUE; private static int remap(int n) { return -1 - n; } // involution + private static final Object RESOURCE_LOCK = new Object(); private static final List intToResourceLocation = new ArrayList<>(); private static final Map resourceLocationToInt = new HashMap<>(); static int getIntFromResourceLocation(ResourceLocation rl) { - Integer i = resourceLocationToInt.get(rl); - if (i != null) { return i; } - int idx = remap(intToResourceLocation.size()); - resourceLocationToInt.put(rl, idx); - intToResourceLocation.add(rl); - return idx; + synchronized (RESOURCE_LOCK) { + Integer i = resourceLocationToInt.get(rl); + if (i != null) { return i; } + int idx = remap(intToResourceLocation.size()); + resourceLocationToInt.put(rl, idx); + intToResourceLocation.add(rl); + return idx; + } } static void bindIntTexture(BatchingFontRenderer owner, int texture) { if (texture > 0) { GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); } else if (texture < 0) { - ((FontRendererAccessor) owner.underlying).angelica$bindTexture(intToResourceLocation.get(remap(texture))); + final ResourceLocation location; + synchronized (RESOURCE_LOCK) { + location = intToResourceLocation.get(remap(texture)); + } + ((FontRendererAccessor) owner.underlying).angelica$bindTexture(location); } } } diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FormatCodes.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FormatCodes.java new file mode 100644 index 000000000..2fd28792a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FormatCodes.java @@ -0,0 +1,108 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizon.gtnhlib.util.font.FontRendering; +import com.gtnewhorizons.angelica.config.AngelicaConfig; + +import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.GRADIENT_PAYLOAD; +import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.SECTION_X_LENGTH; +import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.SECTION_X_PAYLOAD; + +public final class FormatCodes { + + public static final int NONE = 0; + public static final int HEX = 1; + public static final int COLOR = 2; + public static final int OBFUSCATED = 3; + public static final int BOLD = 4; + public static final int STRIKETHROUGH = 5; + public static final int UNDERLINE = 6; + public static final int ITALIC = 7; + public static final int RAINBOW = 8; + public static final int WAVE = 9; + public static final int DINNERBONE = 10; + public static final int SHADOW_COLOR = 11; + public static final int SHADOW_TOGGLE = 12; + public static final int GRADIENT = 13; + public static final int CUSTOM = 14; + public static final int RESET = 15; + + private static final int EXTRA_SHIFT = 8; + private static final int PAYLOAD_SHIFT = 16; + private static final long RGB_MASK = 0xFFFFFFL; + + private FormatCodes() {} + + public static long parse(CharSequence s, int codeIndex, int end) { + final char code = Character.toLowerCase(s.charAt(codeIndex)); + return switch (code) { + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> pack(COLOR, 0, code - '0'); + case 'a', 'b', 'c', 'd', 'e', 'f' -> pack(COLOR, 0, code - 'a' + 10); + case 'k' -> OBFUSCATED; + case 'l' -> BOLD; + case 'm' -> STRIKETHROUGH; + case 'n' -> UNDERLINE; + case 'o' -> ITALIC; + case 'r' -> RESET; + case 'q' -> AngelicaConfig.enableRainbow ? RAINBOW : NONE; + case 'z' -> AngelicaConfig.enableWaveText ? WAVE : NONE; + case 'v' -> AngelicaConfig.enableDinnerboneText ? DINNERBONE : NONE; + case 'x' -> { + if (!AngelicaConfig.enableRGBColors || codeIndex + SECTION_X_PAYLOAD >= end) yield NONE; + final int rgb = ColorCodeUtils.parseHexPairs(s, codeIndex + 1, 6); + yield rgb == -1 ? NONE : pack(HEX, SECTION_X_PAYLOAD, rgb); + } + case 'u' -> { + if (!AngelicaConfig.enableDropShadow) yield NONE; + final int rgb = codeIndex + SECTION_X_LENGTH < end ? ColorCodeUtils.parseSectionXAt(s, codeIndex + 1) : -1; + yield rgb == -1 ? SHADOW_TOGGLE : pack(SHADOW_COLOR, SECTION_X_LENGTH, rgb); + } + case 'g' -> { + if (!AngelicaConfig.enableGradients || codeIndex + GRADIENT_PAYLOAD >= end) yield NONE; + final int from = ColorCodeUtils.parseSectionXAt(s, codeIndex + 1); + final int to = ColorCodeUtils.parseSectionXAt(s, codeIndex + 1 + SECTION_X_LENGTH); + yield from == -1 || to == -1 ? NONE : pack(GRADIENT, GRADIENT_PAYLOAD, ((long) from << 24) | to); + } + default -> FontEffectRegistry.isRegistered(code) ? pack(CUSTOM, 0, code - 'a') : NONE; + }; + } + + private static long pack(int kind, int extraChars, long payload) { + return kind | ((long) extraChars << EXTRA_SHIFT) | (payload << PAYLOAD_SHIFT); + } + + public static int kind(long code) { + return (int) (code & 0xFF); + } + + public static int extraChars(long code) { + return (int) ((code >>> EXTRA_SHIFT) & 0xFF); + } + + public static int rgb(long code) { + return (int) ((code >>> PAYLOAD_SHIFT) & RGB_MASK); + } + + public static int colorIndex(long code) { + return (int) ((code >>> PAYLOAD_SHIFT) & 0xF); + } + + public static int gradientFrom(long code) { + return (int) ((code >>> (PAYLOAD_SHIFT + 24)) & RGB_MASK); + } + + public static int gradientTo(long code) { + return rgb(code); + } + + public static long customBit(long code) { + return 1L << ((code >>> PAYLOAD_SHIFT) & 0x1F); + } + + public static boolean endsBold(long code) { + return switch (kind(code)) { + case COLOR, RESET -> true; + case HEX -> FontRendering.hexColorResetsStyles(); + default -> false; + }; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/GlyphScale.java b/src/main/java/com/gtnewhorizons/angelica/client/font/GlyphScale.java new file mode 100644 index 000000000..81b300dea --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/GlyphScale.java @@ -0,0 +1,24 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizons.angelica.config.FontConfig; + +record GlyphScale(int scaleBits, int aspectBits, float x, float y) { + + private static GlyphScale current = of(FontConfig.glyphScale, FontConfig.glyphAspect); + + private static GlyphScale of(float scale, float aspect) { + final double pow = Math.pow(2, aspect); + return new GlyphScale(Float.floatToRawIntBits(scale), Float.floatToRawIntBits(aspect), (float) (scale * pow), (float) (scale / pow)); + } + + static GlyphScale current() { + final float scale = FontConfig.glyphScale; + final float aspect = FontConfig.glyphAspect; + GlyphScale holder = current; + if (holder.scaleBits != Float.floatToRawIntBits(scale) || holder.aspectBits != Float.floatToRawIntBits(aspect)) { + holder = of(scale, aspect); + current = holder; + } + return holder; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/GlyphSettings.java b/src/main/java/com/gtnewhorizons/angelica/client/font/GlyphSettings.java new file mode 100644 index 000000000..6ba971ef9 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/GlyphSettings.java @@ -0,0 +1,87 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizon.gtnhlib.util.font.FontRendering; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.config.FontConfig; +import com.gtnewhorizons.angelica.hudcaching.HUDCaching; + +public record GlyphSettings(int generation, int fontAAMode, int fontAAStrength, int shadowCopies, int boldCopies, int fontShadowOffset, int fontShadowOffsetUC, + int glyphScale, int glyphAspect, int glyphSpacing, int whitespaceScale, int customFontScale, int waveAmplitude, int flags) { + + private static final int CUSTOM_FONT = 1; + private static final int MODERN_FONT = 1 << 1; + private static final int GLYPH_REPLACEMENTS = 1 << 2; + private static final int RGB_COLORS = 1 << 3; + private static final int GRADIENTS = 1 << 4; + private static final int RAINBOW = 1 << 5; + private static final int WAVE = 1 << 6; + private static final int DINNERBONE = 1 << 7; + private static final int DROP_SHADOW = 1 << 8; + private static final int HEX_RESETS_STYLES = 1 << 9; + private static final int CACHE_OVERRIDE = 1 << 10; + private static final int METRIC_FLAGS = CUSTOM_FONT | MODERN_FONT | GLYPH_REPLACEMENTS | RGB_COLORS | GRADIENTS | DROP_SHADOW | HEX_RESETS_STYLES; + + // One slot per renderingCacheOverride value: the HUD-cache redraw flips it every refresh, and a single slot would allocate on each flip. + private static final GlyphSettings[] CURRENT = new GlyphSettings[2]; + + public static GlyphSettings current() { + final boolean cacheOverride = HUDCaching.renderingCacheOverride; + final int generation = FontProviderCache.generation(); + final int fontAAMode = FontConfig.fontAAMode; + final int fontAAStrength = FontConfig.fontAAStrength; + final int shadowCopies = FontConfig.shadowCopies; + final int boldCopies = FontConfig.boldCopies; + final int fontShadowOffset = Float.floatToIntBits(FontConfig.fontShadowOffset); + final int fontShadowOffsetUC = Float.floatToIntBits(FontConfig.fontShadowOffsetUC); + final int glyphScale = Float.floatToIntBits(FontConfig.glyphScale); + final int glyphAspect = Float.floatToIntBits(FontConfig.glyphAspect); + final int glyphSpacing = Float.floatToIntBits(FontConfig.glyphSpacing); + final int whitespaceScale = Float.floatToIntBits(FontConfig.whitespaceScale); + final int customFontScale = Float.floatToIntBits(FontConfig.customFontScale); + final int waveAmplitude = Float.floatToIntBits(AngelicaConfig.waveAmplitude); + final int flags = (FontConfig.enableCustomFont ? CUSTOM_FONT : 0) + | (FontConfig.enableModernFont ? MODERN_FONT : 0) + | (FontConfig.enableGlyphReplacements ? GLYPH_REPLACEMENTS : 0) + | (AngelicaConfig.enableRGBColors ? RGB_COLORS : 0) + | (AngelicaConfig.enableGradients ? GRADIENTS : 0) + | (AngelicaConfig.enableRainbow ? RAINBOW : 0) + | (AngelicaConfig.enableWaveText ? WAVE : 0) + | (AngelicaConfig.enableDinnerboneText ? DINNERBONE : 0) + | (AngelicaConfig.enableDropShadow ? DROP_SHADOW : 0) + | (FontRendering.hexColorResetsStyles() ? HEX_RESETS_STYLES : 0) + | (cacheOverride ? CACHE_OVERRIDE : 0); + final int slot = cacheOverride ? 1 : 0; + final GlyphSettings cached = CURRENT[slot]; + if (cached != null + && cached.generation == generation + && cached.fontAAMode == fontAAMode + && cached.fontAAStrength == fontAAStrength + && cached.shadowCopies == shadowCopies + && cached.boldCopies == boldCopies + && cached.fontShadowOffset == fontShadowOffset + && cached.fontShadowOffsetUC == fontShadowOffsetUC + && cached.glyphScale == glyphScale + && cached.glyphAspect == glyphAspect + && cached.glyphSpacing == glyphSpacing + && cached.whitespaceScale == whitespaceScale + && cached.customFontScale == customFontScale + && cached.waveAmplitude == waveAmplitude + && cached.flags == flags) { + return cached; + } + final GlyphSettings live = new GlyphSettings(generation, fontAAMode, fontAAStrength, shadowCopies, boldCopies, fontShadowOffset, fontShadowOffsetUC, + glyphScale, glyphAspect, glyphSpacing, whitespaceScale, customFontScale, waveAmplitude, flags); + CURRENT[slot] = live; + return live; + } + + public boolean sameMetricsAs(GlyphSettings other) { + return generation == other.generation + && glyphScale == other.glyphScale + && glyphAspect == other.glyphAspect + && glyphSpacing == other.glyphSpacing + && whitespaceScale == other.whitespaceScale + && customFontScale == other.customFontScale + && (flags & METRIC_FLAGS) == (other.flags & METRIC_FLAGS); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/QuadRegionAllocator.java b/src/main/java/com/gtnewhorizons/angelica/client/font/QuadRegionAllocator.java new file mode 100644 index 000000000..06f15323a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/QuadRegionAllocator.java @@ -0,0 +1,106 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizons.angelica.glsm.backend.RenderBackend; + +import java.util.Arrays; + +final class QuadRegionAllocator { + + private int[] freeStart = new int[16]; + private int[] freeLength = new int[16]; + private int freeCount; + + private int[] retiredStart = new int[16]; + private int[] retiredLength = new int[16]; + private long[] retiredFrame = new long[16]; + private int retiredCount; + + QuadRegionAllocator(int capacity) { + reset(capacity); + } + + void reset(int capacity) { + freeCount = 0; + retiredCount = 0; + if (capacity > 0) { + freeStart[0] = 0; + freeLength[0] = capacity; + freeCount = 1; + } + } + + int allocate(int quads) { + for (int i = 0; i < freeCount; i++) { + final int length = freeLength[i]; + if (length < quads) continue; + final int start = freeStart[i]; + if (length == quads) { + System.arraycopy(freeStart, i + 1, freeStart, i, freeCount - i - 1); + System.arraycopy(freeLength, i + 1, freeLength, i, freeCount - i - 1); + freeCount--; + } else { + freeStart[i] = start + quads; + freeLength[i] = length - quads; + } + return start; + } + return -1; + } + + void retire(int start, int quads, long frame) { + if (retiredCount == retiredStart.length) { + final int n = retiredCount * 2; + retiredStart = Arrays.copyOf(retiredStart, n); + retiredLength = Arrays.copyOf(retiredLength, n); + retiredFrame = Arrays.copyOf(retiredFrame, n); + } + retiredStart[retiredCount] = start; + retiredLength[retiredCount] = quads; + retiredFrame[retiredCount] = frame; + retiredCount++; + } + + void advance(long frame) { + final long reusableUpTo = frame - RenderBackend.MAX_FRAMES_IN_FLIGHT; + int kept = 0; + for (int i = 0; i < retiredCount; i++) { + if (retiredFrame[i] <= reusableUpTo) { + free(retiredStart[i], retiredLength[i]); + } else { + retiredStart[kept] = retiredStart[i]; + retiredLength[kept] = retiredLength[i]; + retiredFrame[kept] = retiredFrame[i]; + kept++; + } + } + retiredCount = kept; + } + + private void free(int start, int quads) { + int at = 0; + while (at < freeCount && freeStart[at] < start) at++; + final boolean joinsPrev = at > 0 && freeStart[at - 1] + freeLength[at - 1] == start; + final boolean joinsNext = at < freeCount && start + quads == freeStart[at]; + if (joinsPrev && joinsNext) { + freeLength[at - 1] += quads + freeLength[at]; + System.arraycopy(freeStart, at + 1, freeStart, at, freeCount - at - 1); + System.arraycopy(freeLength, at + 1, freeLength, at, freeCount - at - 1); + freeCount--; + } else if (joinsPrev) { + freeLength[at - 1] += quads; + } else if (joinsNext) { + freeStart[at] = start; + freeLength[at] += quads; + } else { + if (freeCount == freeStart.length) { + freeStart = Arrays.copyOf(freeStart, freeCount * 2); + freeLength = Arrays.copyOf(freeLength, freeCount * 2); + } + System.arraycopy(freeStart, at, freeStart, at + 1, freeCount - at); + System.arraycopy(freeLength, at, freeLength, at + 1, freeCount - at); + freeStart[at] = start; + freeLength[at] = quads; + freeCount++; + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/RetainedTextStore.java b/src/main/java/com/gtnewhorizons/angelica/client/font/RetainedTextStore.java new file mode 100644 index 000000000..745ff2058 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/RetainedTextStore.java @@ -0,0 +1,162 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizons.angelica.glsm.FeedbackManager; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.RenderBackend; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.lwjgl.opengl.GL15; + +import java.nio.ByteBuffer; +import java.util.Arrays; + +final class RetainedTextStore { + + private static final int INITIAL_QUADS = 4096; + private static final long EVICT_AFTER_NANOS = 5_000_000_000L; + + // Retired regions and buffers wait MAX_FRAMES_IN_FLIGHT frames: SDL-GPU runs a frame's uploads before its draws, so reusing one sooner overwrites bytes an in-flight draw still reads. + private static final QuadRegionAllocator allocator = new QuadRegionAllocator(0); + private static final ObjectArrayList resident = new ObjectArrayList<>(); + private static int buffer; + private static int capacityQuads = INITIAL_QUADS; + private static int generation; + private static long frame; + private static long frameNanos = System.nanoTime(); + private static int[] retiredBuffers = new int[4]; + private static long[] retiredBufferFrames = new long[4]; + private static int retiredBufferCount; + + private RetainedTextStore() {} + + static int buffer() { + return buffer; + } + + static boolean ensure(TextMesh mesh) { + if (!GLStateManager.isMainThread() || mesh.quadCount > BatchingFontRenderer.QUAD_BUDGET || GLStateManager.isRecordingDisplayList() || FeedbackManager.isFeedbackMode()) { + return false; + } + if (mesh.regionStart >= 0 && mesh.regionGeneration == generation) { + mesh.lastUsedNanos = frameNanos; + return true; + } + final int quads = mesh.quadCount; + if (buffer == 0) { + create(Math.max(capacityQuads, quads)); + } + int start = allocator.allocate(quads); + if (start < 0) { + int capacity = capacityQuads * 2; + while (capacity < quads) capacity *= 2; + retireBuffer(); + create(capacity); + start = allocator.allocate(quads); + } + upload(mesh, start); + mesh.regionStart = start; + mesh.regionQuads = quads; + mesh.regionGeneration = generation; + mesh.lastUsedNanos = frameNanos; + resident.add(mesh); + return true; + } + + static void release(TextMesh mesh) { + if (mesh.regionStart >= 0 && mesh.regionGeneration == generation) { + final int i = resident.indexOf(mesh); + if (i >= 0) { + evict(i); + return; + } + } + mesh.regionStart = -1; + mesh.regionQuads = 0; + } + + static void endFrame() { + frame++; + final long now = System.nanoTime(); + frameNanos = now; + allocator.advance(frame); + if (retiredBufferCount > 0) { + deleteExpiredBuffers(); + } + if (resident.isEmpty()) { + retireBuffer(); + return; + } + for (int i = resident.size() - 1; i >= 0; i--) { + if (now - resident.get(i).lastUsedNanos > EVICT_AFTER_NANOS) { + evict(i); + } + } + if (resident.isEmpty()) { + retireBuffer(); + } + } + + private static void evict(int index) { + final TextMesh mesh = resident.get(index); + allocator.retire(mesh.regionStart, mesh.regionQuads, frame); + final TextMesh last = resident.pop(); + if (index < resident.size()) { + resident.set(index, last); + } + mesh.regionStart = -1; + mesh.regionQuads = 0; + } + + private static void create(int capacity) { + final int previous = GLStateManager.ctx().boundVBO; + buffer = GLStateManager.glGenBuffers(); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer); + GLStateManager.glBufferData(GL15.GL_ARRAY_BUFFER, (long) capacity * BatchingFontRenderer.QUAD_BYTES, GL15.GL_DYNAMIC_DRAW); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, previous); + capacityQuads = capacity; + allocator.reset(capacity); + resident.clear(); + generation++; + } + + private static void upload(TextMesh mesh, int start) { + final int previous = GLStateManager.ctx().boundVBO; + final ByteBuffer vertices = mesh.vertices; + vertices.position(0); + vertices.limit(mesh.quadCount * BatchingFontRenderer.QUAD_BYTES); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer); + GLStateManager.glBufferSubData(GL15.GL_ARRAY_BUFFER, (long) start * BatchingFontRenderer.QUAD_BYTES, vertices); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, previous); + vertices.clear(); + } + + private static void retireBuffer() { + if (buffer == 0) return; + if (retiredBufferCount == retiredBuffers.length) { + retiredBuffers = Arrays.copyOf(retiredBuffers, retiredBufferCount * 2); + retiredBufferFrames = Arrays.copyOf(retiredBufferFrames, retiredBufferCount * 2); + } + retiredBuffers[retiredBufferCount] = buffer; + retiredBufferFrames[retiredBufferCount] = frame; + retiredBufferCount++; + buffer = 0; + resident.clear(); + generation++; + } + + private static void deleteExpiredBuffers() { + final long deletableUpTo = frame - RenderBackend.MAX_FRAMES_IN_FLIGHT; + int kept = 0; + for (int i = 0; i < retiredBufferCount; i++) { + final int id = retiredBuffers[i]; + if (retiredBufferFrames[i] <= deletableUpTo) { + BatchingFontRenderer.forgetVertexSource(id); + GLStateManager.glDeleteBuffers(id); + } else { + retiredBuffers[kept] = id; + retiredBufferFrames[kept] = retiredBufferFrames[i]; + kept++; + } + } + retiredBufferCount = kept; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/TextDrawState.java b/src/main/java/com/gtnewhorizons/angelica/client/font/TextDrawState.java index cd3ba9652..d8e5a15e1 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/TextDrawState.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/TextDrawState.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.client.font; +import com.gtnewhorizons.angelica.glsm.GLContextState; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.states.ColorMask; import com.gtnewhorizons.angelica.glsm.states.PolygonState; @@ -28,23 +29,23 @@ final class TextDrawState { float offsetUnits; float alphaRef; - void captureLive() { - depthTest = GLStateManager.getDepthTest().isEnabled(); - depthFunc = GLStateManager.getDepthState().getFunc(); - depthMask = GLStateManager.isEffectiveDepthMaskEnabled(); - final ColorMask colorMask = GLStateManager.getEffectiveColorMask(colorMaskScratch); + void captureLive(GLContextState ctx) { + depthTest = ctx.depthTest.isEnabled(); + depthFunc = ctx.depthState.getFunc(); + depthMask = ctx.depthState.isEffectiveMaskEnabled(); + final ColorMask colorMask = ctx.colorMask.readEffective(colorMaskScratch); colorRed = colorMask.red; colorGreen = colorMask.green; colorBlue = colorMask.blue; colorAlpha = colorMask.alpha; - final PolygonState polygon = GLStateManager.getPolygonState(); - cull = GLStateManager.getCullState().isEnabled(); + final PolygonState polygon = ctx.polygonState; + cull = ctx.cullState.isEnabled(); cullFace = polygon.getCullFaceMode(); frontFace = polygon.getFrontFace(); - polygonOffset = GLStateManager.getPolygonOffsetFillState().isEnabled(); + polygonOffset = ctx.polygonOffsetFillState.isEnabled(); offsetFactor = polygon.getOffsetFactor(); offsetUnits = polygon.getOffsetUnits(); - alphaRef = GLStateManager.getAlphaState().getReference(); + alphaRef = ctx.alphaState.getReference(); } boolean sameAs(TextDrawState other) { diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/TextMesh.java b/src/main/java/com/gtnewhorizons/angelica/client/font/TextMesh.java new file mode 100644 index 000000000..622ebfc2a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/TextMesh.java @@ -0,0 +1,75 @@ +package com.gtnewhorizons.angelica.client.font; + +import java.nio.ByteBuffer; +import java.util.Arrays; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAlloc; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memFree; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memRealloc; + +public final class TextMesh { + + private static final int INITIAL_COMMANDS = 8; + + GlyphSettings settings; + ByteBuffer vertices; + long address; + int quadCount; + int[] commandStart = new int[INITIAL_COMMANDS]; + int[] commandCount = new int[INITIAL_COMMANDS]; + int[] commandTexture = new int[INITIAL_COMMANDS]; + int[] commandLayer = new int[INITIAL_COMMANDS]; + boolean[] commandUnicode = new boolean[INITIAL_COMMANDS]; + int commands; + boolean captured; + boolean animated; + int regionStart = -1; + int regionQuads; + int regionGeneration; + long lastUsedNanos; + + public boolean isValid(GlyphSettings live) { + return captured && !BatchingFontRenderer.recolorActive() && live.equals(settings); + } + + public boolean isAnimated() { + return animated; + } + + void reserveVertexBytes(int bytes) { + if (vertices == null) { + vertices = memAlloc(Math.max(bytes, 1)); + } else if (vertices.capacity() < bytes) { + vertices = memRealloc(vertices, Math.max(bytes, vertices.capacity() * 2)); + } else { + return; + } + address = memAddress0(vertices); + } + + public void free() { + RetainedTextStore.release(this); + memFree(vertices); + vertices = null; + address = 0L; + captured = false; + } + + void addCommand(int start, int count, int texture, boolean unicode, int layer) { + if (commands == commandStart.length) { + final int n = commands * 2; + commandStart = Arrays.copyOf(commandStart, n); + commandCount = Arrays.copyOf(commandCount, n); + commandTexture = Arrays.copyOf(commandTexture, n); + commandLayer = Arrays.copyOf(commandLayer, n); + commandUnicode = Arrays.copyOf(commandUnicode, n); + } + commandStart[commands] = start; + commandCount[commands] = count; + commandTexture[commands] = texture; + commandLayer[commands] = layer; + commandUnicode[commands] = unicode; + commands++; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/commands/AngelicaCommand.java b/src/main/java/com/gtnewhorizons/angelica/commands/AngelicaCommand.java index ae1f31133..f814a974b 100644 --- a/src/main/java/com/gtnewhorizons/angelica/commands/AngelicaCommand.java +++ b/src/main/java/com/gtnewhorizons/angelica/commands/AngelicaCommand.java @@ -4,7 +4,13 @@ package com.gtnewhorizons.angelica.commands; import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.config.SystemProperties; -import com.gtnewhorizons.angelica.debug.ChunkDebugMinimap; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugFactImpl; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntryList; +import com.gtnewhorizons.angelica.debug.screen.entries.AngelicaEntries; import com.gtnewhorizons.angelica.debug.flyby.FlybyRoute; import com.gtnewhorizons.angelica.debug.flyby.FlybyRunner; import com.gtnewhorizons.angelica.debug.profiling.AsprofRecorder; @@ -13,7 +19,6 @@ import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.glsm.profiling.TracyBackend; import com.gtnewhorizons.angelica.rendering.IsbrhTestRenderer; import com.gtnewhorizons.angelica.rendering.RenderRecovery; -import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasDebugScreenHandler; import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; @@ -38,7 +43,7 @@ public class AngelicaCommand extends CommandBase { final Map help = new LinkedHashMap<>(); if (SystemProperties.debugTooling()) { help.put("wireframe", helpLine("wireframe", "Toggle wireframe rendering")); - help.put("fog", helpLine("fog", "Toggle fog debug on F3")); + help.put("fog", helpLine("fog", "Toggle always-on fog debug info")); help.put("minimap", helpLine("minimap", "Toggle chunk debug overlay")); help.put("flyby", helpLine("flyby <" + FlybyRoute.ids() + ">", "Run a deterministic benchmark route")); help.put("profile", helpLine("profile ", "Control async-profiler (JFR) recording")); @@ -154,19 +159,33 @@ public class AngelicaCommand extends CommandBase { } private void handleFog(ICommandSender sender) { - CeleritasDebugScreenHandler.showFogDebug = !CeleritasDebugScreenHandler.showFogDebug; - final String f3State = CeleritasDebugScreenHandler.showFogDebug ? "ON" : "OFF"; - - sender.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + "[Angelica] " + EnumChatFormatting.WHITE + "Fog debug (F3): " + f3State)); - sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + " " + CeleritasDebugScreenHandler.getFogDebugString())); + if (debugScreenUnavailable(sender)) return; + final boolean on = toggleAlways(DebugScreenEntries.ANGELICA_FOG); + sender.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + "[Angelica] " + EnumChatFormatting.WHITE + "Fog debug: " + (on ? "ON" : "OFF"))); + final StringBuilder fogText = new StringBuilder(); + AngelicaEntries.writeFog(new DebugFactImpl().intoPlain(fogText)); + sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GRAY + " " + fogText)); } private void handleMinimap(ICommandSender sender) { - ChunkDebugMinimap.toggle(); - final String state = ChunkDebugMinimap.isEnabled() ? "ON" : "OFF"; + if (debugScreenUnavailable(sender)) return; + final String state = toggleAlways(DebugScreenEntries.ANGELICA_CHUNK_MINIMAP) ? "ON" : "OFF"; sender.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + "[Angelica] " + EnumChatFormatting.WHITE + "Chunk debug minimap: " + state)); } + private static boolean debugScreenUnavailable(ICommandSender sender) { + if (DebugScreen.ENTRIES != null) return false; + sender.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + "[Angelica] " + EnumChatFormatting.RED + "The debug screen failed to start; see the log")); + return true; + } + + private static boolean toggleAlways(DebugEntryDef def) { + final DebugScreenEntryList entries = DebugScreen.ENTRIES; + final boolean on = entries.effectiveStatus(def) != DebugScreenEntryStatus.ALWAYS_ON; + entries.overrideForSession(def, on ? DebugScreenEntryStatus.ALWAYS_ON : DebugScreenEntryStatus.NEVER); + return on; + } + private void handleCrashTest(ICommandSender sender, String[] args) { if (args.length >= 2 && "isbrh".equalsIgnoreCase(args[1])) { IsbrhTestRenderer.armCrashTest(); diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/debug/BotaniaDebugEntry.java b/src/main/java/com/gtnewhorizons/angelica/compat/debug/BotaniaDebugEntry.java new file mode 100644 index 000000000..81e994e90 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/compat/debug/BotaniaDebugEntry.java @@ -0,0 +1,107 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugEntryHandle; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenApi; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.world.World; +import org.lwjgl.opengl.ContextCapabilities; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL20; +import vazkii.botania.client.fx.ParticleRenderDispatcher; +import vazkii.botania.common.core.handler.ConfigHandler; +import vazkii.botania.common.core.handler.ManaNetworkHandler; +import vazkii.botania.common.lib.LibMisc; + +public final class BotaniaDebugEntry implements DebugScreenEntry { + + static final String GROUP_ID = "botania:botania"; + static final String ENTRY_ID = "botania:debug"; + + private static DebugEntryHandle handle; + + private final DebugGroupHandle group; + private final String version = displayVersion(runtimeVersion()); + private int collectors; + private int pools; + private String glVersion; + private String glRenderer; + private String glslVersion; + + private BotaniaDebugEntry(DebugGroupHandle group) { + this.group = group; + } + + static void register() { + handle = DebugCompat.register(GROUP_ID, ENTRY_ID, "Botania", 0x00AA00, entry -> new BotaniaDebugEntry(DebugScreenApi.group(GROUP_ID))); + } + + public static boolean active() { + return DebugCompat.active(handle); + } + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (!ConfigHandler.showF3Debug) return; + if (context.isSampleDue()) { + final World world = context.world(); + collectors = ManaNetworkHandler.instance.getAllCollectorsInWorld(world).size(); + pools = ManaNetworkHandler.instance.getAllPoolsInWorld(world).size(); + } + writeFacts(displayer, group, ParticleRenderDispatcher.sparkleFxCount, ParticleRenderDispatcher.fakeSparkleFxCount, ParticleRenderDispatcher.wispFxCount, ParticleRenderDispatcher.depthIgnoringWispFxCount, ParticleRenderDispatcher.lightningCount, collectors, pools, version); + if (!GuiScreen.isCtrlKeyDown() || !GuiScreen.isShiftKeyDown()) { + writeContextHint(displayer, group, Minecraft.isRunningOnMac); + return; + } + if (glVersion == null) { + glVersion = GLStateManager.glGetString(GL11.GL_VERSION); + glRenderer = GLStateManager.glGetString(GL11.GL_RENDERER); + glslVersion = GLStateManager.glGetString(GL20.GL_SHADING_LANGUAGE_VERSION); + } + final ContextCapabilities caps = GLStateManager.capabilities; + writeContext(displayer, group, ConfigHandler.useShaders, ConfigHandler.glSecondaryTextureUnit, glVersion, glRenderer, glslVersion, GLStateManager.MAX_TEXTURE_UNITS, caps.GL_ARB_multitexture, caps.GL_ARB_texture_non_power_of_two, caps.OpenGL13); + } + + private static String runtimeVersion() { + try { + return (String) LibMisc.class.getField("VERSION").get(null); + } catch (ReflectiveOperationException e) { + return null; + } + } + + static String displayVersion(String version) { + return version == null || version.contains("GRADLE") ? "N/A" : version; + } + + static void writeFacts(DebugScreenDisplayer displayer, DebugGroupHandle group, int sparkles, int fakeSparkles, int wisps, int depthIgnoringWisps, int lightning, int collectors, int pools, String version) { + displayer.fact(group, "Sparkles").value(sparkles).text(" (").value(fakeSparkles).text(" fake)"); + displayer.fact(group, "Wisps").value(wisps).text(" (").value(depthIgnoringWisps).text(" depth-ignoring)"); + displayer.fact(group, "Lightning").value(lightning); + displayer.fact(group, "Mana Network").value(collectors).text(" collectors, ").value(pools).text(" pools"); + displayer.fact(group, "Version").value(version); + } + + static void writeContextHint(DebugScreenDisplayer displayer, DebugGroupHandle group, boolean mac) { + displayer.line(group).text(mac ? "SHIFT+CMD for context" : "SHIFT+CTRL for context"); + } + + static void writeContext(DebugScreenDisplayer displayer, DebugGroupHandle group, boolean useShaders, int secondaryUnit, String glVersion, String glRenderer, String glslVersion, int maxTextureUnits, boolean multitexture, boolean nonPowerOfTwo, boolean openGl13) { + displayer.line(group).text("Config Context"); + displayer.fact(group, "shaders.enabled").value(String.valueOf(useShaders)); + displayer.fact(group, "shaders.secondaryUnit").value(secondaryUnit); + displayer.line(group).text("OpenGL Context"); + displayer.fact(group, "GL_VERSION").value(String.valueOf(glVersion)); + displayer.fact(group, "GL_RENDERER").value(String.valueOf(glRenderer)); + displayer.fact(group, "GL_SHADING_LANGUAGE_VERSION").value(String.valueOf(glslVersion)); + displayer.fact(group, "GL_MAX_TEXTURE_IMAGE_UNITS_ARB").value(maxTextureUnits); + displayer.fact(group, "GL_ARB_multitexture").value(String.valueOf(multitexture)); + displayer.fact(group, "GL_ARB_texture_non_power_of_two").value(String.valueOf(nonPowerOfTwo)); + displayer.fact(group, "OpenGL13").value(String.valueOf(openGl13)); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/debug/DebugCompat.java b/src/main/java/com/gtnewhorizons/angelica/compat/debug/DebugCompat.java new file mode 100644 index 000000000..2488e03d9 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/compat/debug/DebugCompat.java @@ -0,0 +1,48 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntryHandle; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec.DebugEntryFactory; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugGroupSpec; +import com.gtnewhorizons.angelica.api.debug.DebugScreenApi; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import cpw.mods.fml.common.Loader; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public final class DebugCompat { + + private static final Logger LOGGER = LogManager.getLogger("Angelica"); + + private DebugCompat() {} + + public static void registerMissing() { + if (Loader.isModLoaded("Botania")) registerMissing("botania", BotaniaDebugEntry::register); + if (Loader.isModLoaded("journeymap")) registerMissing("journeymap", JourneyMapDebugEntry::register); + if (Loader.isModLoaded("hodgepodge")) registerMissing("hodgepodge", HodgepodgeDebugEntry::register); + } + + static void registerMissing(String namespace, Runnable register) { + if (DebugRegistry.MAIN.hasNamespace(namespace)) return; + try { + register.run(); + } catch (RuntimeException | LinkageError e) { + LOGGER.error("Debug screen compat entry for {} failed to register", namespace, e); + } + } + + static boolean active(DebugEntryHandle handle) { + return handle != null && !handle.isFailed(); + } + + static DebugEntryHandle register(String groupId, String entryId, String name, int accent, DebugEntryFactory factory) { + DebugScreenApi.registerGroup(new DebugGroupSpec(groupId).title(name).accent(accent).side(DebugGroupHandle.LEFT)); + return DebugScreenApi.registerEntry(new DebugEntrySpec(entryId, DebugEntryCategory.SCREEN_TEXT) + .name(name) + .defaults(DebugScreenEntryStatus.IN_OVERLAY, DebugScreenEntryStatus.NEVER) + .text(factory)); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/debug/HodgepodgeDebugEntry.java b/src/main/java/com/gtnewhorizons/angelica/compat/debug/HodgepodgeDebugEntry.java new file mode 100644 index 000000000..5c97d2471 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/compat/debug/HodgepodgeDebugEntry.java @@ -0,0 +1,85 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugEntryHandle; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenApi; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.mitchej123.hodgepodge.config.DebugConfig; +import com.mitchej123.hodgepodge.mixins.hooks.ChunkGenScheduler; +import com.mitchej123.hodgepodge.mixins.hooks.ChunkGenStats; +import net.minecraftforge.classloading.FMLForgePlugin; + +public final class HodgepodgeDebugEntry implements DebugScreenEntry { + + static final String GROUP_ID = "hodgepodge:chunkgen"; + static final String ENTRY_ID = "hodgepodge:chunkgen"; + + private static DebugEntryHandle handle; + + private final DebugGroupHandle group; + private boolean scheduled; + private double budgetUsedMs; + private double budgetAvailMs; + private double terrainGenEmaMs; + private double populateEmaMs; + private int genQueue; + private int popQueue; + private int pipelineBacklog; + private int terrainGenCount; + private int popCount; + private int blockedLoads; + private int stuckCount; + private int neighborFills; + private double debtMs; + + private HodgepodgeDebugEntry(DebugGroupHandle group) { + this.group = group; + } + + static void register() { + if (!DebugConfig.showChunkGenDebug && FMLForgePlugin.RUNTIME_DEOBF) return; + handle = DebugCompat.register(GROUP_ID, ENTRY_ID, "Chunk Generation", 0xFFAA00, entry -> new HodgepodgeDebugEntry(DebugScreenApi.group(GROUP_ID))); + } + + public static boolean active() { + return DebugCompat.active(handle); + } + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (context.isSampleDue()) sample(context); + if (!scheduled) return; + writeFacts(displayer, group, budgetUsedMs, budgetAvailMs, terrainGenEmaMs, populateEmaMs, genQueue, popQueue, pipelineBacklog, terrainGenCount, popCount, blockedLoads, stuckCount, neighborFills, debtMs); + } + + private void sample(DebugContext context) { + final ChunkGenScheduler scheduler = ChunkGenScheduler.getForDimension(context.player().dimension); + scheduled = scheduler != null; + if (scheduler == null) return; + final ChunkGenStats stats = scheduler.getStats(); + budgetUsedMs = stats.getSnapBudgetUsedMs(); + budgetAvailMs = stats.getSnapBudgetAvailMs(); + terrainGenEmaMs = stats.getTerrainGenEmaMs(); + populateEmaMs = stats.getPopulateEmaMs(); + genQueue = stats.getSnapGenQueue(); + popQueue = stats.getSnapPopQueue(); + pipelineBacklog = stats.getSnapPipelineBacklog(); + terrainGenCount = stats.getSnapTerrainGenCount(); + popCount = stats.getSnapPopCount(); + blockedLoads = stats.getSnapBlockedLoads(); + stuckCount = stats.getSnapStuckCount(); + neighborFills = stats.getSnapNeighborFills(); + debtMs = stats.getDebtMs(); + } + + static void writeFacts(DebugScreenDisplayer displayer, DebugGroupHandle group, double budgetUsedMs, double budgetAvailMs, double terrainGenEmaMs, double populateEmaMs, int genQueue, int popQueue, int pipelineBacklog, int terrainGenCount, int popCount, int blockedLoads, int stuckCount, int neighborFills, double debtMs) { + displayer.fact(group, "Budget").value(budgetUsedMs, 1).text(" / ").value(budgetAvailMs, 1).text(" ms"); + displayer.fact(group, "Average").text("terrain ").value(terrainGenEmaMs, 1).text(" ms, populate ").value(populateEmaMs, 1).text(" ms"); + displayer.fact(group, "Queues").text("gen ").value(genQueue).text(", populate ").value(popQueue).text(", pipeline ").value(pipelineBacklog); + displayer.fact(group, "Active").text("gen ").value(terrainGenCount).text(", populate ").value(popCount); + displayer.fact(group, "Blocked").value(blockedLoads).text(" blocked, ").value(stuckCount).text(" stuck, ").value(neighborFills).text(" fills"); + displayer.fact(group, "Debt").value(debtMs, 1).text(" ms"); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/debug/JourneyMapDebugEntry.java b/src/main/java/com/gtnewhorizons/angelica/compat/debug/JourneyMapDebugEntry.java new file mode 100644 index 000000000..da2b4e33d --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/compat/debug/JourneyMapDebugEntry.java @@ -0,0 +1,103 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugEntryHandle; +import com.gtnewhorizons.angelica.api.debug.DebugFactLines; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenApi; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.I18n; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +public final class JourneyMapDebugEntry implements DebugScreenEntry { + + static final String GROUP_ID = "journeymap:journeymap"; + static final String ENTRY_ID = "journeymap:debug"; + + private static final String MAPPING_DISABLED_KEY = "jm.common.enable_mapping_false_text"; + private static final long REPORT_INTERVAL_NS = TimeUnit.SECONDS.toNanos(3); + + private static DebugEntryHandle handle; + + private final DebugGroupHandle group; + private final DebugFactLines facts = new DebugFactLines(); + private MethodHandle stats; + private MethodHandle mappingEnabled; + private MethodHandle timerReport; + private List report = Collections.emptyList(); + private long reportAt; + private boolean reported; + + private JourneyMapDebugEntry(DebugGroupHandle group) { + this.group = group; + } + + static void register() { + handle = DebugCompat.register(GROUP_ID, ENTRY_ID, "JourneyMap", 0x55FFFF, entry -> new JourneyMapDebugEntry(DebugScreenApi.group(GROUP_ID))); + } + + public static boolean active() { + return DebugCompat.active(handle); + } + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (context.isSampleDue()) sample(context.nanoTime()); + facts.emit(displayer, group); + } + + @SuppressWarnings("unchecked") + private void sample(long now) { + try { + if (stats == null) resolve(); + final boolean mapping = ((AtomicBoolean) mappingEnabled.invokeExact()).get(); + final boolean profiler = Minecraft.getMinecraft().gameSettings.showDebugProfilerChart; + if (profiler && (!reported || now - reportAt > REPORT_INTERVAL_NS)) { + report = (List) timerReport.invokeExact("", ""); + reportAt = now; + reported = true; + } + writeFacts(facts, mapping ? (String[]) stats.invokeExact() : null, mapping ? null : I18n.format(MAPPING_DISABLED_KEY), profiler ? report : null); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable t) { + throw new IllegalStateException("JourneyMap debug stats are unavailable", t); + } + } + + private void resolve() throws ReflectiveOperationException { + final MethodHandles.Lookup lookup = MethodHandles.publicLookup(); + final Class properties = Class.forName("journeymap.client.properties.CoreProperties"); + final MethodHandle coreProperties = lookup.findStatic(Class.forName("journeymap.client.JourneymapClient"), "getCoreProperties", MethodType.methodType(properties)); + mappingEnabled = MethodHandles.filterReturnValue(coreProperties, lookup.findGetter(properties, "mappingEnabled", AtomicBoolean.class)); + timerReport = lookup.findStatic(Class.forName("journeymap.client.log.StatTimer"), "getReportByTotalTime", MethodType.methodType(List.class, String.class, String.class)); + stats = lookup.findStatic(Class.forName("journeymap.client.task.multi.MapPlayerTask"), "getDebugStats", MethodType.methodType(String[].class)); + } + + static void writeFacts(DebugFactLines facts, String[] stats, String mappingDisabled, List report) { + facts.begin(); + if (mappingDisabled != null) facts.line(mappingDisabled); + if (stats != null) { + for (int i = 0; i < stats.length; i++) { + final String line = stats[i]; + if (line != null) facts.split(line); + } + } + if (report != null) { + for (int i = 0, n = report.size(); i < n; i++) { + final String line = report.get(i); + if (line != null) facts.line(line); + } + } + facts.end(); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java index 009db7b85..0574b4816 100644 --- a/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java +++ b/src/main/java/com/gtnewhorizons/angelica/config/AngelicaConfig.java @@ -108,10 +108,6 @@ public class AngelicaConfig { @Config.DefaultBoolean(true) public static boolean modernizeF3Screen; - @Config.Comment("Show block registry name and meta value in F3, similar to 1.8+. [From ArchaicFix]") - @Config.DefaultBoolean(true) - public static boolean showBlockDebugInfo; - @Config.DefaultBoolean(true) @Config.Comment("Hide downloading terrain screen. [From ArchaicFix]") public static boolean hideDownloadingTerrainScreen; @@ -460,13 +456,14 @@ public class AngelicaConfig { @Config.DefaultBoolean(false) public static boolean useVanillaChunkTracking; - @Config.Comment("Disables additional F3 information added by Angelica.") - @Config.DefaultBoolean(false) - public static boolean disableF3Additions; - - @Config.Comment("Shows developer counters (FFP, streaming, TESR, transfer) on the F3 screen. Toggle in game with F3+V.") - @Config.DefaultBoolean(false) - public static boolean verboseF3; + @Config.Comment({"Size of the F3 text.", + "-1 = same as your GUI Scale (default, the size vanilla F3 uses)", + "0 = Auto: half the largest size that fits the window", + "1-16 = that size", + "F3 is never drawn larger than your GUI Scale."}) + @Config.DefaultInt(-1) + @Config.RangeInt(min = -1, max = 16) + public static int debugGuiScale; @Config.Comment("Replaces various FFP uploads with statically allocated VBO's.") @Config.DefaultBoolean(true) diff --git a/src/main/java/com/gtnewhorizons/angelica/config/CompatConfig.java b/src/main/java/com/gtnewhorizons/angelica/config/CompatConfig.java index f81846a5c..59306bacf 100644 --- a/src/main/java/com/gtnewhorizons/angelica/config/CompatConfig.java +++ b/src/main/java/com/gtnewhorizons/angelica/config/CompatConfig.java @@ -50,4 +50,13 @@ public class CompatConfig { @Config.RequiresMcRestart public static boolean fixNTMSpace; + @Config.Comment({"Rules for F3 lines that other mods add without Angelica's F3 API, one rule per entry.", + "A rule matches lines that start with the quoted text (color codes ignored), in the left or right column.", + " drop left \"Text: \" hides the line", + " fact left \"Text: \" group= moves the line into that F3 panel; add name=\"Label\" to rename it", + "Panel ids are listed under Panels in the F3+F6 screen; entry ids show when you hover an entry there.", + "Add when= to apply a rule only while that F3 entry is enabled."}) + @Config.DefaultStringList({}) + public static String[] debugLineRules; + } diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/ChunkDebugMinimap.java b/src/main/java/com/gtnewhorizons/angelica/debug/ChunkDebugMinimap.java index 525812590..b20dcd70d 100644 --- a/src/main/java/com/gtnewhorizons/angelica/debug/ChunkDebugMinimap.java +++ b/src/main/java/com/gtnewhorizons/angelica/debug/ChunkDebugMinimap.java @@ -22,33 +22,48 @@ package com.gtnewhorizons.angelica.debug; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.debug.render.DebugDrawScope; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.DebugProgram; +import com.gtnewhorizons.angelica.debug.render.DebugPrograms; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; import com.gtnewhorizons.angelica.rendering.celeritas.AngelicaRenderSectionManager; import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer; import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import lombok.Getter; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ChunkProviderClient; -import net.minecraft.client.renderer.Tessellator; import net.minecraft.world.chunk.EmptyChunk; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; +import org.joml.Matrix4f; import org.lwjgl.opengl.GL11; /** - * Debug overlay that renders a minimap showing chunk loading and rendering state. Toggle via /angelica minimap + * Debug overlay that renders a minimap showing chunk loading and rendering state. */ public class ChunkDebugMinimap { - private static final ChunkDebugMinimap INSTANCE = new ChunkDebugMinimap(); - @Getter - private static volatile boolean enabled = false; - private static final int RANGE = 32; // Chunks in each direction from player - public static synchronized void toggle() { - if (enabled) disable(); - else enable(); - } + private static final int FLAGS = VertexFlags.COLOR_BIT; + private static final VertexFormat FORMAT = DefaultVertexFormat.ALL_FORMATS[FLAGS]; + private static final int STRIDE = FORMAT.getVertexSize(); + private static final int COLOR_OFFSET = DebugGeometry.offsetOf(FORMAT, VertexFormatElement.Usage.COLOR); + private static final int CHUNKS = RANGE * 2 * RANGE * 2; + private static final int ALPHA = (int) (0.8F * 255.0F); + private static final int BYTES = CHUNKS * 4 * STRIDE; + private static final ChunkDebugMinimap INSTANCE = new ChunkDebugMinimap(); + private static volatile boolean enabled = false; + + private final DebugDrawScope scope = new DebugDrawScope(); + private final NativeScratch scratch = new NativeScratch(BYTES); + private final Matrix4f mvp = new Matrix4f(); + private long cursor; public static synchronized void enable() { if (enabled) return; @@ -60,6 +75,7 @@ public class ChunkDebugMinimap { if (!enabled) return; enabled = false; MinecraftForge.EVENT_BUS.unregister(INSTANCE); + INSTANCE.scratch.free(); } @SubscribeEvent @@ -84,39 +100,31 @@ public class ChunkDebugMinimap { final int playerChunkX = ((int) Math.floor(pX)) >> 4; final int playerChunkZ = ((int) Math.floor(pZ)) >> 4; - // Save GL state - GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); - GLStateManager.glPushMatrix(); - try { - // Setup rendering - GLStateManager.glScaled(2, 2, 1); - GLStateManager.glTranslated(32, 32, 0); + scratch.ensure(BYTES); + cursor = scratch.address(); + final ChunkProviderClient chunkProvider = (ChunkProviderClient) mc.theWorld.getChunkProvider(); - GLStateManager.disableLighting(); - GLStateManager.disableTexture(); - GLStateManager.disableDepthTest(); - GLStateManager.enableBlend(); - GLStateManager.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); - - final Tessellator tess = Tessellator.instance; - tess.startDrawingQuads(); - - final ChunkProviderClient chunkProvider = (ChunkProviderClient) mc.theWorld.getChunkProvider(); - - for (int x = playerChunkX - RANGE; x < playerChunkX + RANGE; x++) { - for (int z = playerChunkZ - RANGE; z < playerChunkZ + RANGE; z++) { - drawChunk(tess, x, z, playerChunkX, playerChunkZ, manager, chunkProvider); - } + for (int x = playerChunkX - RANGE; x < playerChunkX + RANGE; x++) { + for (int z = playerChunkZ - RANGE; z < playerChunkZ + RANGE; z++) { + drawChunk(x, z, playerChunkX, playerChunkZ, manager, chunkProvider); } + } - tess.draw(); + GLStateManager.getProjectionMatrix().mul(GLStateManager.getModelViewMatrix(), mvp).scale(2, 2, 1).translate(32, 32, 0); + + scope.beginHud(); + try { + final DebugProgram color = DebugPrograms.color(); + color.use(); + color.mvp(mvp); + final int bytes = (int) (cursor - scratch.address()); + TessellatorStreamingDrawer.drawPacked(scratch.window(bytes), GL11.GL_QUADS, FLAGS, bytes / STRIDE); } finally { - GLStateManager.glPopMatrix(); - GLStateManager.glPopAttrib(); + scope.end(); } } - private void drawChunk(Tessellator tess, int chunkX, int chunkZ, int playerChunkX, int playerChunkZ, AngelicaRenderSectionManager manager, ChunkProviderClient chunkProvider) { + private void drawChunk(int chunkX, int chunkZ, int playerChunkX, int playerChunkZ, AngelicaRenderSectionManager manager, ChunkProviderClient chunkProvider) { boolean initialized = false; float r = 0, g = 0, b = 0; @@ -142,16 +150,23 @@ public class ChunkDebugMinimap { } } - tess.setColorRGBA_F(r, g, b, 0.8f); + final int argb = ALPHA << 24 | (int) (r * 255) << 16 | (int) (g * 255) << 8 | (int) (b * 255); final int xStart = chunkX - playerChunkX; final int zStart = chunkZ - playerChunkZ; final int xEnd = xStart + 1; final int zEnd = zStart + 1; - tess.addVertex(xStart, zStart, 0); - tess.addVertex(xStart, zEnd, 0); - tess.addVertex(xEnd, zEnd, 0); - tess.addVertex(xEnd, zStart, 0); + vertex(xStart, zStart, argb); + vertex(xStart, zEnd, argb); + vertex(xEnd, zEnd, argb); + vertex(xEnd, zStart, argb); + } + + private void vertex(float x, float y, int argb) { + final long p = cursor; + DebugGeometry.putPosition(p, x, y, 0.0F); + DebugGeometry.putColor(p + COLOR_OFFSET, argb); + cursor = p + STRIDE; } } diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/DebugF3Toggle.java b/src/main/java/com/gtnewhorizons/angelica/debug/DebugF3Toggle.java new file mode 100644 index 000000000..291dad330 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/DebugF3Toggle.java @@ -0,0 +1,49 @@ +package com.gtnewhorizons.angelica.debug; + +import org.lwjgl.input.Keyboard; + +public final class DebugF3Toggle { + + private boolean armed; + private boolean comboUsed; + private boolean shift; + private boolean alt; + + public void arm(boolean shift, boolean alt) { + armed = true; + comboUsed = false; + this.shift = shift; + this.alt = alt; + } + + public void disarm() { + armed = false; + comboUsed = false; + } + + public boolean isArmed() { + return armed; + } + + public boolean shift() { + return shift; + } + + public boolean alt() { + return alt; + } + + public void onKeyPress(int key) { + if (!armed) return; + switch (key) { + case Keyboard.KEY_F3, Keyboard.KEY_LSHIFT, Keyboard.KEY_RSHIFT, Keyboard.KEY_LMENU, Keyboard.KEY_RMENU -> {} + default -> comboUsed = true; + } + } + + public boolean onF3Release() { + final boolean toggle = armed && !comboUsed; + disarm(); + return toggle; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/DebugKeyHandler.java b/src/main/java/com/gtnewhorizons/angelica/debug/DebugKeyHandler.java index 623c3ad14..7ef7c2b49 100644 --- a/src/main/java/com/gtnewhorizons/angelica/debug/DebugKeyHandler.java +++ b/src/main/java/com/gtnewhorizons/angelica/debug/DebugKeyHandler.java @@ -1,28 +1,70 @@ package com.gtnewhorizons.angelica.debug; import com.gtnewhorizon.gtnhlib.GTNHLib; -import com.gtnewhorizon.gtnhlib.config.ConfigurationManager; -import com.gtnewhorizons.angelica.config.AngelicaConfig; -import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasDebugScreenHandler; +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugOptionsScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; +import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent; import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.util.StatCollector; import org.lwjgl.input.Keyboard; -/** F3+V toggles {@link AngelicaConfig#verboseF3} */ public class DebugKeyHandler { + public static final DebugF3Toggle F3_TOGGLE = new DebugF3Toggle(); + @SubscribeEvent public void onKey(InputEvent.KeyInputEvent event) { - if (!Keyboard.getEventKeyState() || Keyboard.getEventKey() != Keyboard.KEY_V || !Keyboard.isKeyDown(Keyboard.KEY_F3) - || Minecraft.getMinecraft().currentScreen != null) { + final int key = Keyboard.getEventKey(); + final Minecraft mc = Minecraft.getMinecraft(); + + if (mc.currentScreen != null) { + if (mc.currentScreen instanceof DebugOptionsScreen && key == Keyboard.KEY_F6 && Keyboard.getEventKeyState() + && !Keyboard.isRepeatEvent() && Keyboard.isKeyDown(Keyboard.KEY_F3)) { + mc.displayGuiScreen(null); + } + F3_TOGGLE.disarm(); return; } - AngelicaConfig.verboseF3 = !AngelicaConfig.verboseF3; - ConfigurationManager.save(AngelicaConfig.class); - CeleritasDebugScreenHandler.invalidate(); - GTNHLib.proxy.addDebugToChat(StatCollector.translateToLocal(AngelicaConfig.verboseF3 ? "angelica.debug.verbose.on" : "angelica.debug.verbose.off")); + if (!Keyboard.getEventKeyState()) { + if (key == Keyboard.KEY_F3 && F3_TOGGLE.onF3Release()) { + mc.gameSettings.showDebugInfo = !mc.gameSettings.showDebugInfo; + mc.gameSettings.showDebugProfilerChart = F3_TOGGLE.shift(); + ((IGameSettingsExt) mc.gameSettings).angelica$setShowFpsGraph(F3_TOGGLE.alt()); + } + if (key == Keyboard.KEY_F3) DebugScreen.syncOverlayVisible(); + return; + } + + F3_TOGGLE.onKeyPress(key); + + if (!Keyboard.isKeyDown(Keyboard.KEY_F3)) { + return; + } + if (!Keyboard.isRepeatEvent()) { + switch (key) { + case Keyboard.KEY_F6 -> DebugOptionsScreen.open(mc); + case Keyboard.KEY_V -> toggleAndReport(DebugScreenEntries.ANGELICA_RENDERER_VERBOSE, "angelica.debug.verbose"); + case Keyboard.KEY_G -> toggleAndReport(DebugScreenEntries.CHUNK_BORDERS, "angelica.debug.chunk_borders"); + case Keyboard.KEY_B -> { + if (DebugScreen.ENTRIES != null) DebugScreen.ENTRIES.toggleStatus(DebugScreenEntries.ENTITY_HITBOXES); + } + default -> {} + } + } + if (key == Keyboard.KEY_B && DebugScreen.ENTRIES != null) { + RenderManager.debugBoundingBox = DebugScreenEntries.ENTITY_HITBOXES.isEnabled(); + } + } + + private static void toggleAndReport(DebugEntryDef def, String key) { + if (DebugScreen.ENTRIES == null) return; + final boolean on = DebugScreen.ENTRIES.toggleStatus(def); + GTNHLib.proxy.addDebugToChat(StatCollector.translateToLocal(key + (on ? ".on" : ".off"))); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/F3Direction.java b/src/main/java/com/gtnewhorizons/angelica/debug/F3Direction.java index efc60b5e9..597637e25 100644 --- a/src/main/java/com/gtnewhorizons/angelica/debug/F3Direction.java +++ b/src/main/java/com/gtnewhorizons/angelica/debug/F3Direction.java @@ -1,19 +1,44 @@ package com.gtnewhorizons.angelica.debug; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.debug.render.DebugDrawScope; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.DebugPrograms; +import com.gtnewhorizons.angelica.debug.render.DebugProgram; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.ViewportState; +import com.gtnewhorizons.angelica.glsm.streaming.LineQuads; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.renderer.Tessellator; import net.minecraft.entity.Entity; import net.minecraftforge.client.event.RenderGameOverlayEvent; +import org.joml.Matrix4f; import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; public class F3Direction { + private static final float AXIS_LENGTH = 10.0F; + private static final float LINE_WIDTH = 2.0F; + private static final int SEGMENTS = 3; + private static final int LINE_VERTICES = SEGMENTS * 2; + private static final int QUAD_VERTICES = SEGMENTS * 6; + private static final int FLAGS = VertexFlags.COLOR_BIT; + private static final VertexFormat FORMAT = DefaultVertexFormat.ALL_FORMATS[FLAGS]; + private static final VertexFormat QUAD_FORMAT = LineQuads.quadFormat(FORMAT); + private static final int STRIDE = FORMAT.getVertexSize(); + private static final int COLOR_OFFSET = DebugGeometry.offsetOf(FORMAT, VertexFormatElement.Usage.COLOR); + + private static final DebugDrawScope SCOPE = new DebugDrawScope(); + private static final Matrix4f MVP = new Matrix4f(); + private static int vao; + public static void renderWorldDirectionsEvent(Minecraft mc, RenderGameOverlayEvent.Pre event) { - if (mc.gameSettings.showDebugInfo && mc.gameSettings.thirdPersonView == 0) { - final ScaledResolution scaledresolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - final int width = scaledresolution.getScaledWidth(); - final int height = scaledresolution.getScaledHeight(); + if (mc.gameSettings.thirdPersonView == 0) { + final int width = event.resolution.getScaledWidth(); + final int height = event.resolution.getScaledHeight(); GLStateManager.glPushMatrix(); @@ -30,33 +55,57 @@ public class F3Direction { GLStateManager.glPopMatrix(); } } + public static void renderWorldDirections() { - GLStateManager.disableTexture(); - GLStateManager.glDepthMask(false); - final Tessellator tessellator = Tessellator.instance; + GLStateManager.getProjectionMatrix().mul(GLStateManager.getModelViewMatrix(), MVP); + final ViewportState viewport = GLStateManager.getViewportState(); - GLStateManager.glLineWidth(2.0F); - tessellator.startDrawing(GL11.GL_LINES); + SCOPE.beginHud(); + try { + if (vao == 0) build(); + final DebugProgram lines = DebugPrograms.lines(); + lines.use(); + lines.mvp(MVP); + lines.lineWidth(LINE_WIDTH); + lines.viewport(viewport.width, viewport.height); + GLStateManager.glBindVertexArray(vao); + GLStateManager.glDrawArrays(GL11.GL_TRIANGLES, 0, QUAD_VERTICES); + } finally { + SCOPE.end(); + } + } - //X - tessellator.setColorRGBA_F(255, 0, 0, 1); - tessellator.addVertex(0.0D, 0.0D, 0.0D); - tessellator.addVertex(10, 0.0D, 0.0D); + private static void build() { + final int quadBytes = QUAD_VERTICES * QUAD_FORMAT.getVertexSize(); + final NativeScratch source = new NativeScratch(LINE_VERTICES * STRIDE); + final NativeScratch expanded = new NativeScratch(quadBytes); + try { + source.ensure(LINE_VERTICES * STRIDE); + expanded.ensure(quadBytes); + long p = source.address(); + p = putAxis(p, AXIS_LENGTH, 0.0F, 0.0F, 0xFFFF0000); + p = putAxis(p, 0.0F, 0.0F, AXIS_LENGTH, 0xFF0000FF); + putAxis(p, 0.0F, AXIS_LENGTH, 0.0F, 0xFF00FF00); + LineQuads.expandWithOtherEnd(source.address(), FORMAT, GL11.GL_LINES, LINE_VERTICES, expanded.address()); - //Z - tessellator.setColorRGBA_F(0, 0, 255, 1); - tessellator.addVertex(0.0D, 0.0D, 0.0D); - tessellator.addVertex(0.0D, 0.0D, 10); + final int vbo = GLStateManager.glGenBuffers(); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + GLStateManager.glBufferData(GL15.GL_ARRAY_BUFFER, expanded.window(quadBytes), GL15.GL_STATIC_DRAW); + vao = DebugGeometry.createVao(vbo, QUAD_FORMAT); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); + } finally { + source.free(); + expanded.free(); + } + } - //Y - tessellator.setColorRGBA_F(0, 255, 0, 1); - tessellator.addVertex(0.0D, 0.0D, 0.0D); - tessellator.addVertex(0.0D, 10, 0.0D); - - tessellator.draw(); - - GLStateManager.glLineWidth(1.0F); - GLStateManager.glDepthMask(true); - GLStateManager.enableTexture(); + private static long putAxis(long address, float x, float y, float z, int argb) { + long p = address; + DebugGeometry.putPosition(p, 0.0F, 0.0F, 0.0F); + DebugGeometry.putColor(p + COLOR_OFFSET, argb); + p += STRIDE; + DebugGeometry.putPosition(p, x, y, z); + DebugGeometry.putColor(p + COLOR_OFFSET, argb); + return p + STRIDE; } } diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/F3Graph.java b/src/main/java/com/gtnewhorizons/angelica/debug/F3Graph.java index 98a5b0974..03b1c61a2 100644 --- a/src/main/java/com/gtnewhorizons/angelica/debug/F3Graph.java +++ b/src/main/java/com/gtnewhorizons/angelica/debug/F3Graph.java @@ -1,21 +1,31 @@ package com.gtnewhorizons.angelica.debug; import com.gtnewhorizon.gtnhlib.bytebuf.MemoryStack; -import com.gtnewhorizon.gtnhlib.client.renderer.shader.ShaderProgram; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.debug.render.DebugDrawScope; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.DebugPrograms; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; +import com.gtnewhorizons.angelica.debug.screen.DebugNumbers; import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.client.renderer.Tessellator; import net.minecraft.util.ResourceLocation; +import org.embeddedt.embeddium.impl.gl.shader.ShaderConstants; +import org.joml.Matrix4f; import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; import java.nio.FloatBuffer; import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryStack.stackPush; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutFloat; import static org.lwjgl.opengl.GL11.GL_FLOAT; -import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA; -import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA; import static org.lwjgl.opengl.GL11.GL_TRIANGLE_STRIP; import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; @@ -29,14 +39,25 @@ public abstract class F3Graph { private static final int HEIGHT = 60; private static final int WIDTH = NUM_SAMPLES; private static final int FONT_COLOR = 0xFFE0E0E0; + private static final int BACKGROUND_ARGB = 0x90505050; + private static final int FOREGROUND_ARGB = 0xFFFFFFFF; + private static final int COLOR_FLAGS = VertexFlags.COLOR_BIT; + private static final int TEXTURED_FLAGS = VertexFlags.TEXTURE_BIT | VertexFlags.COLOR_BIT; + private static final VertexFormat COLOR_FORMAT = DefaultVertexFormat.ALL_FORMATS[COLOR_FLAGS]; + private static final VertexFormat TEXTURED_FORMAT = DefaultVertexFormat.ALL_FORMATS[TEXTURED_FLAGS]; + private static final int COLOR_STRIDE = COLOR_FORMAT.getVertexSize(); + private static final int TEXTURED_STRIDE = TEXTURED_FORMAT.getVertexSize(); + private static final int TEXTURED_UV_OFFSET = DebugGeometry.offsetOf(TEXTURED_FORMAT, VertexFormatElement.Usage.PRIMARY_UV); + private static final int TEXTURED_COLOR_OFFSET = DebugGeometry.offsetOf(TEXTURED_FORMAT, VertexFormatElement.Usage.COLOR); + private static final int COLOR_COLOR_OFFSET = DebugGeometry.offsetOf(COLOR_FORMAT, VertexFormatElement.Usage.COLOR); + private static final int QUAD_BYTES = 4 * Math.max(COLOR_STRIDE, TEXTURED_STRIDE); // Due to GLSL 120 limitations, it's just easier to use floats private final FloatBuffer sampleBuf = BufferUtils.createFloatBuffer(NUM_SAMPLES); private final long[] samples = new long[NUM_SAMPLES]; // long version for calculations // Circular buffer holding the last 240 samples, in nanoseconds private int samplesHead = 0; // one ahead of the position of the last sample private boolean initialized = false; - private ShaderProgram shader; - private int aPos; + private int program; private int uFBWidth; private int uFBHeight; private int uScaleFactor; @@ -44,10 +65,20 @@ public abstract class F3Graph { private int uSamples; private int vertBuf; private int vao; + private final DebugDrawScope scope = new DebugDrawScope(); + private final NativeScratch scratch = new NativeScratch(QUAD_BYTES); + private final Matrix4f mvp = new Matrix4f(); private final ResourceLocation texture; private final float pxPerNs; private final boolean left; private long sum; + private final StringBuilder label = new StringBuilder(16); + private String minString; + private String avgString; + private String maxString; + private int labelTick = Integer.MIN_VALUE; + private int avgWidth; + private int maxWidth; protected F3Graph(ResourceLocation texture, float pxPerNs, boolean left) { this.texture = texture; @@ -65,6 +96,10 @@ public abstract class F3Graph { samplesHead = (samplesHead + 1) % NUM_SAMPLES; } + public static int reservedHeight(FontRenderer fontRenderer) { + return HEIGHT + BORDER * 2 + fontRenderer.FONT_HEIGHT; + } + private int getVertX(ScaledResolution sres, int idx) { if (left) { return switch (idx) { @@ -92,23 +127,17 @@ public abstract class F3Graph { } private void init() { - shader = new ShaderProgram( - "angelica", - "shaders/debug_graph.vert.glsl", - "shaders/debug_graph.frag.glsl"); - shader.use(); + final int handle = DebugPrograms.link("angelica:debug_graph", "angelica:debug_graph.vert.glsl", "angelica:debug_graph.frag.glsl", ShaderConstants.EMPTY, new String[]{"pos"}, new int[]{0}); + program = handle; + GLStateManager.glUseProgram(handle); - // Attribute location is declared in shader via layout(location = 0) - aPos = 0; - - // Register uniforms - uFBWidth = shader.getUniformLocation("fbWidth"); - uFBHeight = shader.getUniformLocation("fbHeight"); - uScaleFactor = shader.getUniformLocation("scaleFactor"); - uSamples = shader.getUniformLocation("samples"); - uHeadIdx = shader.getUniformLocation("headIdx"); - final int uPxPerNs = shader.getUniformLocation("pxPerNs"); - final int uLeft = shader.getUniformLocation("left"); + uFBWidth = GLStateManager.glGetUniformLocation(handle, "fbWidth"); + uFBHeight = GLStateManager.glGetUniformLocation(handle, "fbHeight"); + uScaleFactor = GLStateManager.glGetUniformLocation(handle, "scaleFactor"); + uSamples = GLStateManager.glGetUniformLocation(handle, "samples"); + uHeadIdx = GLStateManager.glGetUniformLocation(handle, "headIdx"); + final int uPxPerNs = GLStateManager.glGetUniformLocation(handle, "pxPerNs"); + final int uLeft = GLStateManager.glGetUniformLocation(handle, "left"); vao = GLStateManager.glGenVertexArrays(); GLStateManager.glBindVertexArray(vao); @@ -131,33 +160,17 @@ public abstract class F3Graph { } // Configure vertex attribute in the VAO - GLStateManager.glVertexAttribPointer(aPos, VERT_FLOATS, GL_FLOAT, false, VERT_FLOATS * 4, 0); - GLStateManager.glEnableVertexAttribArray(aPos); + GLStateManager.glVertexAttribPointer(0, VERT_FLOATS, GL_FLOAT, false, VERT_FLOATS * 4, 0); + GLStateManager.glEnableVertexAttribArray(0); GLStateManager.glBindBuffer(GL_ARRAY_BUFFER, 0); GLStateManager.glBindVertexArray(0); - final Minecraft mc = Minecraft.getMinecraft(); - final ScaledResolution sr = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - - // Load initial value for uniforms - GLStateManager.glUniform1f(uFBWidth, mc.displayWidth); - GLStateManager.glUniform1f(uFBHeight, mc.displayHeight); - GLStateManager.glUniform1f(uScaleFactor, sr.getScaleFactor()); - GLStateManager.glUniform1i(uHeadIdx, samplesHead); - GLStateManager.glUniform1(uSamples, sampleBuf); GLStateManager.glUniform1f(uPxPerNs, pxPerNs); - GLStateManager.glUniform1i(uLeft, left ? 1 : 0); // this is how you load bool uniforms - - ShaderProgram.clear(); + GLStateManager.glUniform1i(uLeft, left ? 1 : 0); } - public void render() { - if (!initialized) { - init(); - initialized = true; - } - + public void render(ScaledResolution sr, int tick) { /* * We try to copy modern vanilla's tracker. * It is 240 (FRAME_TIMES) wide by 60 tall, including the 1px borders. @@ -171,88 +184,98 @@ public abstract class F3Graph { */ final Minecraft mc = Minecraft.getMinecraft(); - final ScaledResolution sr = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - // Setup GL state - final Tessellator tess = Tessellator.instance; - GLStateManager.disableDepthTest(); - GLStateManager.enableAlphaTest(); - GLStateManager.enableBlend(); - GLStateManager.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GLStateManager.getProjectionMatrix().mul(GLStateManager.getModelViewMatrix(), mvp); - // Draw background - GLStateManager.disableTexture(); - GLStateManager.glColor4f(0x50 / 255.0F, 0x50 / 255.0F, 0x50 / 255.0F, 0x90 / 255.0F); - tess.startDrawingQuads(); - for (int i = 0; i < 4; ++i) { - tess.addVertex(getVertX(sr, i), getVertY(sr, i), 0); + scope.beginHud(); + try { + if (!initialized) { + init(); + initialized = true; + } + drawGraph(mc, sr); + } finally { + scope.end(); } - tess.draw(); - GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - GLStateManager.enableTexture(); - // Draw samples - shader.use(); + // Draw text + final FontRenderer fr = mc.fontRenderer; + if (tick != labelTick) { + labelTick = tick; + updateLabels(fr); + } + final int top = sr.getScaledHeight() - reservedHeight(fr); + final int scaledWidth = sr.getScaledWidth(); + final int minX; + final int avgX; + final int maxX; + if (left) { + minX = BORDER * 2; + avgX = BORDER + WIDTH / 2 - avgWidth / 2; + maxX = BORDER * 2 + WIDTH - maxWidth; + } else { + minX = scaledWidth - (BORDER * 2 + WIDTH); + avgX = scaledWidth - (BORDER + WIDTH / 2 + avgWidth / 2); + maxX = scaledWidth - (BORDER * 2 + maxWidth); + } + fr.drawString(minString, minX, top, FONT_COLOR, true); + fr.drawString(avgString, avgX, top, FONT_COLOR, true); + fr.drawString(maxString, maxX, top, FONT_COLOR, true); + } - // Load uniforms + private void drawGraph(Minecraft mc, ScaledResolution sr) { + scratch.ensure(QUAD_BYTES); + long p = scratch.address(); + for (int i = 0; i < 4; ++i, p += COLOR_STRIDE) { + DebugGeometry.putPosition(p, getVertX(sr, i), getVertY(sr, i), 0.0F); + DebugGeometry.putColor(p + COLOR_COLOR_OFFSET, BACKGROUND_ARGB); + } + final var color = DebugPrograms.color(); + color.use(); + color.mvp(mvp); + TessellatorStreamingDrawer.drawPacked(scratch.window(4 * COLOR_STRIDE), GL11.GL_QUADS, COLOR_FLAGS, 4); + + GLStateManager.glUseProgram(program); GLStateManager.glUniform1f(uFBWidth, mc.displayWidth); GLStateManager.glUniform1f(uFBHeight, mc.displayHeight); GLStateManager.glUniform1f(uScaleFactor, sr.getScaleFactor()); GLStateManager.glUniform1i(uHeadIdx, samplesHead); GLStateManager.glUniform1(uSamples, sampleBuf); - GLStateManager.glBindVertexArray(vao); - - // Left side graphs are inverted, so temporarily disable culling - if (!left) GLStateManager.disableCull(); - GLStateManager.glDrawArrays(GL_TRIANGLE_STRIP, 0, VERT_COUNT); - if (!left) GLStateManager.enableCull(); - - GLStateManager.glBindVertexArray(0); - - ShaderProgram.clear(); - - // Draw foreground - mc.getTextureManager().bindTexture(texture); - tess.startDrawingQuads(); - tess.addVertexWithUV(getVertX(sr, 0), getVertY(sr, 0), 0.0D, 1, 0); - tess.addVertexWithUV(getVertX(sr, 1), getVertY(sr, 1), 0.0D, 0, 0); - tess.addVertexWithUV(getVertX(sr, 2), getVertY(sr, 2), 0.0D, 0, 1); - tess.addVertexWithUV(getVertX(sr, 3), getVertY(sr, 3), 0.0D, 1, 1); - tess.draw(); + p = scratch.address(); + for (int i = 0; i < 4; ++i, p += TEXTURED_STRIDE) { + DebugGeometry.putPosition(p, getVertX(sr, i), getVertY(sr, i), 0.0F); + memPutFloat(p + TEXTURED_UV_OFFSET, (i == 0 || i == 3) ? 1.0F : 0.0F); + memPutFloat(p + TEXTURED_UV_OFFSET + 4, i >= 2 ? 1.0F : 0.0F); + DebugGeometry.putColor(p + TEXTURED_COLOR_OFFSET, FOREGROUND_ARGB); + } + final var textured = DebugPrograms.textured(); + textured.use(); + textured.mvp(mvp); + TessellatorStreamingDrawer.drawPacked(scratch.window(4 * TEXTURED_STRIDE), GL11.GL_QUADS, TEXTURED_FLAGS, 4); + } - // Reset GL state - GLStateManager.enableDepthTest(); - GLStateManager.disableAlphaTest(); - GLStateManager.disableBlend(); - - // Draw text - // Ensure running counters are reset on first sample + private void updateLabels(FontRenderer fr) { long min = Long.MAX_VALUE; long max = Long.MIN_VALUE; for (int i = 0; i < NUM_SAMPLES; ++i) { min = Math.min(min, samples[i]); max = Math.max(max, samples[i]); } - String minStr = String.format("%.1f ms min", min / 1_000_000D); - String avgStr = String.format("%.1f ms avg", sum / 1_000_000 / (double) NUM_SAMPLES); - String maxStr = String.format("%.1f ms max", max / 1_000_000D); - final FontRenderer fr = mc.fontRenderer; - final int top = sr.getScaledHeight() - HEIGHT - BORDER * 2 - fr.FONT_HEIGHT; - final int scaledWidth = sr.getScaledWidth(); + minString = label(min / 1_000_000D, " ms min"); + avgString = label(sum / 1_000_000 / (double) NUM_SAMPLES, " ms avg"); + maxString = label(max / 1_000_000D, " ms max"); + avgWidth = fr.getStringWidth(avgString); + maxWidth = fr.getStringWidth(maxString); + } - if (left) { - fr.drawString(minStr, BORDER * 2, top, FONT_COLOR, true); - fr.drawString(avgStr, BORDER + WIDTH / 2 - fr.getStringWidth(avgStr) / 2, top, FONT_COLOR, true); - fr.drawString(maxStr, BORDER * 2 + WIDTH - fr.getStringWidth(maxStr), top, FONT_COLOR, true); - } else { - fr.drawString(minStr, scaledWidth - (BORDER * 2 + WIDTH), top, FONT_COLOR, true); - fr.drawString(avgStr, scaledWidth - (BORDER + WIDTH / 2 + fr.getStringWidth(avgStr) / 2), top, FONT_COLOR, true); - fr.drawString(maxStr, scaledWidth - (BORDER * 2 + fr.getStringWidth(maxStr)), top, FONT_COLOR, true); - } + private String label(double millis, String suffix) { + label.setLength(0); + DebugNumbers.appendFixed(label, millis, 1); + return label.append(suffix).toString(); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyDebugOverrides.java b/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyDebugOverrides.java new file mode 100644 index 000000000..bae7a00df --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyDebugOverrides.java @@ -0,0 +1,138 @@ +package com.gtnewhorizons.angelica.debug.flyby; + +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntryList; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenProfile; +import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt; +import net.minecraft.client.Minecraft; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; + +import static com.gtnewhorizons.angelica.config.SystemProperties.FLYBY_DEBUG_ENTRIES; +import static com.gtnewhorizons.angelica.config.SystemProperties.FLYBY_DEBUG_FLAT; +import static com.gtnewhorizons.angelica.config.SystemProperties.FLYBY_DEBUG_HUD; +import static com.gtnewhorizons.angelica.config.SystemProperties.FLYBY_DEBUG_PROFILE; +import static com.gtnewhorizons.angelica.config.SystemProperties.FLYBY_FPS_GRAPH; +import static com.gtnewhorizons.angelica.config.SystemProperties.FLYBY_HUD_CACHING; + +final class FlybyDebugOverrides { + private static final Logger LOGGER = LogManager.getLogger("Angelica/Flyby"); + + record Pair(String id, DebugScreenEntryStatus status) {} + + private Boolean hudSaved; + private Boolean flatSaved; + private Boolean cachingSaved; + private Boolean fpsGraphSaved; + private boolean sessionOverridden; + + static List parseEntries(String raw) { + final List pairs = new ArrayList<>(); + if (raw == null) return pairs; + for (final String part : raw.split(",")) { + final String pair = part.trim(); + if (pair.isEmpty()) continue; + final int eq = pair.lastIndexOf('='); + if (eq <= 0 || eq == pair.length() - 1) { + LOGGER.error("Flyby debugEntries: malformed pair '{}', expected id=status", pair); + continue; + } + final String id = pair.substring(0, eq).trim(); + final String name = pair.substring(eq + 1).trim(); + final DebugScreenEntryStatus status = DebugScreenEntryStatus.bySerializedName(name); + if (id.isEmpty() || status == null) { + LOGGER.error("Flyby debugEntries: bad {} '{}' in '{}'", id.isEmpty() ? "id" : "status", id.isEmpty() ? id : name, pair); + continue; + } + pairs.add(new Pair(id, status)); + } + return pairs; + } + + void apply(Minecraft mc) { + if (FLYBY_DEBUG_HUD != null && this.hudSaved == null) { + this.hudSaved = mc.gameSettings.showDebugInfo; + mc.gameSettings.showDebugInfo = FLYBY_DEBUG_HUD; + } + if (FLYBY_DEBUG_FLAT != null && this.flatSaved == null) { + this.flatSaved = AngelicaConfig.modernizeF3Screen; + AngelicaConfig.modernizeF3Screen = !FLYBY_DEBUG_FLAT; + } + if (FLYBY_HUD_CACHING != null && this.cachingSaved == null) { + this.cachingSaved = AngelicaConfig.hudCachingActive; + AngelicaConfig.hudCachingActive = FLYBY_HUD_CACHING; + } + if (FLYBY_FPS_GRAPH != null && this.fpsGraphSaved == null) { + final IGameSettingsExt settings = (IGameSettingsExt) mc.gameSettings; + this.fpsGraphSaved = settings.angelica$showFpsGraph(); + settings.angelica$setShowFpsGraph(FLYBY_FPS_GRAPH); + } + this.applySession(); + } + + private void applySession() { + final boolean wantProfile = !FLYBY_DEBUG_PROFILE.isEmpty(); + final boolean wantEntries = !FLYBY_DEBUG_ENTRIES.isEmpty(); + if (!wantProfile && !wantEntries) return; + + final DebugScreenEntryList entries = DebugScreen.ENTRIES; + if (entries == null) { + LOGGER.error("Flyby: debug entry list is not available, ignoring debugProfile and debugEntries"); + return; + } + final DebugRegistry registry = DebugRegistry.MAIN; + this.sessionOverridden = true; + + if (wantProfile) { + final DebugScreenProfile profile = DebugScreenProfile.bySerializedName(FLYBY_DEBUG_PROFILE); + if (profile == null) { + LOGGER.error("Flyby: unknown debugProfile '{}', expected default or performance", FLYBY_DEBUG_PROFILE); + } else { + for (int i = 0; i < registry.entryCount(); i++) { + final DebugEntryDef def = registry.entryAt(i); + entries.overrideForSession(def, def.profileStatus(profile)); + } + } + } + + for (final Pair pair : parseEntries(FLYBY_DEBUG_ENTRIES)) { + final DebugEntryDef def = registry.entryById(pair.id()); + if (def == null) { + LOGGER.error("Flyby debugEntries: unknown entry id '{}'", pair.id()); + continue; + } + entries.overrideForSession(def, pair.status()); + } + } + + void restore(Minecraft mc) { + if (this.hudSaved != null) { + mc.gameSettings.showDebugInfo = this.hudSaved; + this.hudSaved = null; + } + if (this.flatSaved != null) { + AngelicaConfig.modernizeF3Screen = this.flatSaved; + this.flatSaved = null; + } + if (this.cachingSaved != null) { + AngelicaConfig.hudCachingActive = this.cachingSaved; + this.cachingSaved = null; + } + if (this.fpsGraphSaved != null) { + ((IGameSettingsExt) mc.gameSettings).angelica$setShowFpsGraph(this.fpsGraphSaved); + this.fpsGraphSaved = null; + } + if (this.sessionOverridden) { + final DebugScreenEntryList entries = DebugScreen.ENTRIES; + if (entries != null) entries.clearSessionOverrides(); + this.sessionOverridden = false; + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java b/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java index 0297aa955..000037626 100644 --- a/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java +++ b/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java @@ -5,6 +5,7 @@ import com.gtnewhorizons.angelica.config.SystemProperties.FlybyPacing; import com.gtnewhorizons.angelica.debug.profiling.AsprofRecorder; import com.gtnewhorizons.angelica.debug.profiling.AsprofRecorder.StopResult; import com.gtnewhorizons.angelica.debug.profiling.AsprofRecorder.StopStatus; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenOverlay; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.backend.VSyncMode; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; @@ -14,6 +15,7 @@ import com.gtnewhorizons.angelica.rendering.FramePacer; import com.gtnewhorizons.angelica.rendering.IsbrhTestRenderer; import com.sun.management.OperatingSystemMXBean; import com.sun.management.ThreadMXBean; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import net.minecraft.client.Minecraft; @@ -92,8 +94,7 @@ public final class FlybyRunner { private boolean pauseOnLostFocusSaved; private boolean pauseOnLostFocusOverridden; private boolean vsyncOverridden; - private boolean showDebugInfoSaved; - private boolean showDebugInfoOverridden; + private final FlybyDebugOverrides debugOverrides = new FlybyDebugOverrides(); private int cameraSaved; private boolean cameraOverridden; private int shotEvery; @@ -180,8 +181,8 @@ public final class FlybyRunner { this.fixedOrigin = origin; this.waitForTracy = SystemProperties.FLYBY_WAIT_FOR_TRACY; if (this.waitForTracy && !Tracy.ENABLED) { - LOGGER.warn("Flyby: waitForTracy requested but Tracy is disabled in this build"); - this.waitForTracy = false; + LOGGER.error("Flyby: a Tracy capture was requested but Tracy is not available (native library missing or init failed, see the Tracy log lines above); exiting instead of running unprofiled"); + FMLCommonHandler.instance().exitJava(1, false); } this.waitForFocus = SystemProperties.FLYBY_WAIT_FOR_FOCUS; this.exitWhenDone = SystemProperties.FLYBY_EXIT_WHEN_DONE; @@ -339,6 +340,7 @@ public final class FlybyRunner { final Minecraft mc = Minecraft.getMinecraft(); final IChatComponent result = ScreenShotHelper.saveScreenshot(mc.mcDataDir, "flyby-shot-" + this.shot + ".png", mc.displayWidth, mc.displayHeight, mc.getFramebuffer()); LOGGER.info("Flyby screenshot {}", result.getUnformattedText()); + if (SystemProperties.FLYBY_DEBUG_DUMP) DebugScreenOverlay.INSTANCE.describe(LOGGER, this.shot); this.shot++; this.nextShot += this.shotEvery; } @@ -401,11 +403,7 @@ public final class FlybyRunner { this.buildPath(); this.suppressPacing(); - if (SystemProperties.FLYBY_DEBUG_HUD && !this.showDebugInfoOverridden) { - this.showDebugInfoSaved = mc.gameSettings.showDebugInfo; - this.showDebugInfoOverridden = true; - mc.gameSettings.showDebugInfo = true; - } + this.debugOverrides.apply(mc); if (!this.cameraOverridden) { this.cameraSaved = mc.gameSettings.thirdPersonView; this.cameraOverridden = true; @@ -682,6 +680,7 @@ public final class FlybyRunner { ThreadedFileIOBase.threadedIOInstance.waitForFinish(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); + LOGGER.error("Flyby: interrupted while draining chunk IO before discarding changes", e); } worldChangesDiscarded = true; @@ -878,7 +877,7 @@ public final class FlybyRunner { this.returnToOrigin(player); this.restorePauseOnLostFocus(mc); - this.restoreShowDebugInfo(mc); + this.debugOverrides.restore(mc); this.restoreCamera(mc); this.armed = false; sceneGuarded = false; @@ -906,12 +905,6 @@ public final class FlybyRunner { this.pauseOnLostFocusOverridden = false; } - private void restoreShowDebugInfo(Minecraft mc) { - if (!this.showDebugInfoOverridden) return; - mc.gameSettings.showDebugInfo = this.showDebugInfoSaved; - this.showDebugInfoOverridden = false; - } - private void restoreCamera(Minecraft mc) { if (!this.cameraOverridden) return; mc.gameSettings.thirdPersonView = this.cameraSaved; @@ -1067,7 +1060,7 @@ public final class FlybyRunner { FramePacer.endStats(); final Minecraft mc = Minecraft.getMinecraft(); this.restorePauseOnLostFocus(mc); - this.restoreShowDebugInfo(mc); + this.debugOverrides.restore(mc); this.restoreCamera(mc); this.armed = false; this.stopRecording(mc); diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugDrawScope.java b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugDrawScope.java new file mode 100644 index 000000000..14fb8a86f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugDrawScope.java @@ -0,0 +1,58 @@ +package com.gtnewhorizons.angelica.debug.render; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.StateSet; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL15; + +public final class DebugDrawScope { + + private static final StateSet SET = StateSet.forMask(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_ENABLE_BIT | GL11.GL_POLYGON_BIT | GL11.GL_SCISSOR_BIT | GL11.GL_TEXTURE_BIT | GL11.GL_LINE_BIT); + + private int depth; + private int program; + private int vao; + private int vbo; + + public void beginHud() { + capture(); + GLStateManager.disableDepthTest(); + common(); + } + + public void beginWorld() { + capture(); + GLStateManager.enableDepthTest(); + GLStateManager.glDepthFunc(GL11.GL_LEQUAL); + GLStateManager.glCullFace(GL11.GL_BACK); + GLStateManager.glFrontFace(GL11.GL_CCW); + common(); + } + + private void capture() { + depth = GLStateManager.pushState(SET); + program = GLStateManager.getActiveProgram(); + vao = GLStateManager.getBoundVAO(); + vbo = GLStateManager.getBoundVBO(); + } + + public void end() { + GLStateManager.glUseProgram(program); + GLStateManager.glBindVertexArray(vao); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + GLStateManager.popStateTo(depth); + } + + private static void common() { + GLStateManager.glDepthMask(false); + GLStateManager.enableBlend(); + GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO); + GLStateManager.disableCull(); + GLStateManager.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL); + GLStateManager.disableScissorTest(); + GLStateManager.glColorMask(true, true, true, true); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glLineWidth(1.0F); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugGeometry.java b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugGeometry.java new file mode 100644 index 000000000..f32fc5fe1 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugGeometry.java @@ -0,0 +1,44 @@ +package com.gtnewhorizons.angelica.debug.render; + +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.lwjgl.opengl.GL15; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutByte; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutFloat; + +public final class DebugGeometry { + + private DebugGeometry() {} + + public static int createVao(int vbo, VertexFormat format) { + final int vao = GLStateManager.glGenVertexArrays(); + GLStateManager.glBindVertexArray(vao); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + format.setupBufferState(0L); + return vao; + } + + public static int offsetOf(VertexFormat format, VertexFormatElement.Usage usage) { + int offset = 0; + for (final VertexFormatElement element : format.elementsArray) { + if (element.getUsage() == usage) return offset; + offset += element.getByteSize(); + } + throw new IllegalArgumentException("Vertex format has no " + usage); + } + + public static void putColor(long address, int argb) { + memPutByte(address, (byte) (argb >> 16)); + memPutByte(address + 1, (byte) (argb >> 8)); + memPutByte(address + 2, (byte) argb); + memPutByte(address + 3, (byte) (argb >>> 24)); + } + + public static void putPosition(long address, float x, float y, float z) { + memPutFloat(address, x); + memPutFloat(address + 4, y); + memPutFloat(address + 8, z); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugProgram.java b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugProgram.java new file mode 100644 index 000000000..31fa9c6a9 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugProgram.java @@ -0,0 +1,40 @@ +package com.gtnewhorizons.angelica.debug.render; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.joml.Matrix4fc; +import org.lwjgl.BufferUtils; + +import java.nio.FloatBuffer; + +public final class DebugProgram { + + private final int handle; + private final int mvpLocation; + private final int lineWidthLocation; + private final int viewportLocation; + private final FloatBuffer matrix = BufferUtils.createFloatBuffer(16); + + DebugProgram(int handle) { + this.handle = handle; + this.mvpLocation = GLStateManager.glGetUniformLocation(handle, "u_MVP"); + this.lineWidthLocation = GLStateManager.glGetUniformLocation(handle, "u_LineWidth"); + this.viewportLocation = GLStateManager.glGetUniformLocation(handle, "u_ViewportSize"); + } + + public void use() { + GLStateManager.glUseProgram(handle); + } + + public void mvp(Matrix4fc mvp) { + mvp.get(matrix); + GLStateManager.glUniformMatrix4(mvpLocation, false, matrix); + } + + public void lineWidth(float pixels) { + GLStateManager.glUniform1f(lineWidthLocation, pixels); + } + + public void viewport(int width, int height) { + GLStateManager.glUniform2f(viewportLocation, width, height); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugPrograms.java b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugPrograms.java new file mode 100644 index 000000000..f8ae25cf2 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/render/DebugPrograms.java @@ -0,0 +1,66 @@ +package com.gtnewhorizons.angelica.debug.render; + +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import org.embeddedt.embeddium.impl.gl.shader.GlProgram; +import org.embeddedt.embeddium.impl.gl.shader.GlShader; +import org.embeddedt.embeddium.impl.gl.shader.ShaderConstants; +import org.embeddedt.embeddium.impl.gl.shader.ShaderType; +import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; + +public final class DebugPrograms { + + private static DebugProgram color; + private static DebugProgram textured; + private static DebugProgram lines; + + private DebugPrograms() {} + + public static DebugProgram color() { + if (color == null) color = link("angelica:debug_color", "angelica:debug.vert", ShaderConstants.EMPTY, false, false); + return color; + } + + public static DebugProgram textured() { + if (textured == null) textured = link("angelica:debug_textured", "angelica:debug.vert", ShaderConstants.builder().add("TEXTURED").build(), true, false); + return textured; + } + + public static DebugProgram lines() { + if (lines == null) lines = link("angelica:debug_lines", "angelica:debug_lines.vert", ShaderConstants.EMPTY, false, true); + return lines; + } + + private static DebugProgram link(String name, String vertexSource, ShaderConstants constants, boolean texCoord, boolean otherEnd) { + final int count = 2 + (texCoord ? 1 : 0) + (otherEnd ? 1 : 0); + final String[] attributes = new String[count]; + final int[] locations = new int[count]; + attributes[0] = "a_Position"; + locations[0] = VertexFormatElement.Usage.POSITION.getAttributeLocation(); + attributes[1] = "a_Color"; + locations[1] = VertexFormatElement.Usage.COLOR.getAttributeLocation(); + int next = 2; + if (texCoord) { + attributes[next] = "a_TexCoord0"; + locations[next++] = VertexFormatElement.Usage.PRIMARY_UV.getAttributeLocation(); + } + if (otherEnd) { + attributes[next] = "a_LineOtherEnd"; + locations[next] = VertexFormatElement.Usage.NORMAL.getAttributeLocation(); + } + return new DebugProgram(link(name, vertexSource, "angelica:debug.frag", constants, attributes, locations)); + } + + public static int link(String name, String vertexSource, String fragmentSource, ShaderConstants constants, String[] attributes, int[] locations) { + final GlShader vert = ShaderLoader.loadShader(ShaderType.VERTEX, vertexSource, constants); + final GlShader frag = ShaderLoader.loadShader(ShaderType.FRAGMENT, fragmentSource, constants); + try { + final GlProgram.Builder builder = GlProgram.builder(name).attachShader(vert).attachShader(frag); + for (int i = 0; i < attributes.length; i++) builder.bindAttribute(attributes[i], locations[i]); + final GlProgram program = builder.link(ctx -> null); + return program.handle(); + } finally { + vert.delete(); + frag.delete(); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/render/NativeScratch.java b/src/main/java/com/gtnewhorizons/angelica/debug/render/NativeScratch.java new file mode 100644 index 000000000..2d7e0d916 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/render/NativeScratch.java @@ -0,0 +1,45 @@ +package com.gtnewhorizons.angelica.debug.render; + +import java.nio.ByteBuffer; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memFree; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memRealloc; + +public final class NativeScratch { + + private final int initial; + private ByteBuffer buffer; + private long address; + private int capacity; + + public NativeScratch(int initial) { + this.initial = initial; + } + + public long address() { + return address; + } + + public void ensure(int bytes) { + if (bytes <= capacity) return; + int grown = Math.max(1, Math.max(initial, capacity)); + while (grown < bytes) grown <<= 1; + buffer = memRealloc(buffer, grown); + address = memAddress0(buffer); + capacity = buffer.capacity(); + } + + public ByteBuffer window(int bytes) { + buffer.clear(); + buffer.limit(bytes); + return buffer; + } + + public void free() { + memFree(buffer); + buffer = null; + address = 0L; + capacity = 0; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugContextImpl.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugContextImpl.java new file mode 100644 index 000000000..6d34663ec --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugContextImpl.java @@ -0,0 +1,106 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.util.MathHelper; +import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; + +public final class DebugContextImpl implements DebugContext { + + private static final long SAMPLE_INTERVAL_NS = 500_000_000L; + + private World world; + private EntityClientPlayerMP player; + private Chunk chunk; + private int blockX; + private int blockY; + private int blockZ; + private long nanoTime; + private boolean sampleDue; + private boolean sampled; + private long sampledVersion; + private long sampledNanos; + private DebugEntryDef entry; + private boolean entryFirstSample; + + public void prepare(Minecraft mc, long nanoTime, long entriesVersion) { + final World w = mc.theWorld; + final EntityClientPlayerMP p = mc.thePlayer; + world = w; + player = p; + chunk = null; + if (w != null && p != null) { + blockX = MathHelper.floor_double(p.posX); + blockY = MathHelper.floor_double(p.boundingBox.minY); + blockZ = MathHelper.floor_double(p.posZ); + final Chunk column = w.getChunkFromChunkCoords(blockX >> 4, blockZ >> 4); + if (column != null && !column.isEmpty()) chunk = column; + } + this.nanoTime = nanoTime; + sampleDue = !sampled || entriesVersion != sampledVersion || nanoTime - sampledNanos >= SAMPLE_INTERVAL_NS; + if (sampleDue) { + sampled = true; + sampledVersion = entriesVersion; + sampledNanos = nanoTime; + } + } + + void beginEntry(DebugEntryDef def) { + entry = def; + entryFirstSample = false; + } + + public void release() { + world = null; + player = null; + chunk = null; + entry = null; + } + + @Override + public World world() { + return world; + } + + @Override + public EntityClientPlayerMP player() { + return player; + } + + @Override + public int blockX() { + return blockX; + } + + @Override + public int blockY() { + return blockY; + } + + @Override + public int blockZ() { + return blockZ; + } + + @Override + public Chunk chunk() { + return chunk; + } + + @Override + public long nanoTime() { + return nanoTime; + } + + @Override + public boolean isSampleDue() { + final DebugEntryDef def = entry; + if (def != null && !def.sampleOffered) { + def.sampleOffered = true; + entryFirstSample = true; + } + return sampleDue || entryFirstSample; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugEntryDef.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugEntryDef.java new file mode 100644 index 000000000..3f0726f3a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugEntryDef.java @@ -0,0 +1,94 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntryHandle; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec.DebugEntryFactory; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec.DebugToggleListener; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; + +public final class DebugEntryDef implements DebugEntryHandle { + + public final String id; + public final String namespace; + public final String path; + public final String name; + public final DebugEntryCategory category; + public final DebugScreenEntryStatus defaultStatus; + public final DebugScreenEntryStatus performanceStatus; + public final int index; + public final int kind; + public final int rendererIndex; + public final boolean supportsAlways; + public final boolean flat; + + final DebugRegistry registry; + final DebugEntryFactory textFactory; + final DebugToggleListener toggleListener; + + boolean enabled; + boolean alwaysOn; + boolean failed; + boolean sampleOffered; + private DebugScreenEntry entry; + + DebugEntryDef(DebugRegistry registry, DebugEntrySpec spec, int index, int kind, int rendererIndex, boolean flat) { + this.registry = registry; + this.id = spec.getId(); + final int colon = id.indexOf(':'); + this.namespace = id.substring(0, colon); + this.path = id.substring(colon + 1); + this.name = spec.getName(); + this.category = spec.getCategory(); + this.defaultStatus = spec.getDefaultStatus(); + this.performanceStatus = spec.getPerformanceStatus(); + this.index = index; + this.kind = kind; + this.rendererIndex = rendererIndex; + this.supportsAlways = spec.getSupportsAlways(); + this.flat = flat; + this.textFactory = spec.getTextFactory(); + this.toggleListener = spec.getToggleListener(); + } + + @Override + public String id() { + return id; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + public boolean isAlwaysOn() { + return alwaysOn; + } + + @Override + public boolean isFailed() { + return failed; + } + + public boolean hasText() { + return textFactory != null; + } + + public boolean isScreenText() { + return category == DebugEntryCategory.SCREEN_TEXT && hasText(); + } + + public DebugScreenEntryStatus profileStatus(DebugScreenProfile profile) { + return profile == DebugScreenProfile.PERFORMANCE ? performanceStatus : defaultStatus; + } + + public DebugScreenEntry entryOrCreate() { + DebugScreenEntry created = entry; + if (created == null && textFactory != null) { + created = textFactory.create(this); + entry = created; + } + return created; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugFactImpl.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugFactImpl.java new file mode 100644 index 000000000..de9b28929 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugFactImpl.java @@ -0,0 +1,75 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugFact; + +public final class DebugFactImpl implements DebugFact { + + private static final String VALUE_ON = "\u00a7f"; + private static final String VALUE_OFF = "\u00a7r"; + + private static final String PLAIN = ""; + + private StringBuilder out; + private String valueOn = VALUE_ON; + private String valueOff = VALUE_OFF; + + public DebugFactImpl into(StringBuilder out) { + this.out = out; + valueOn = VALUE_ON; + valueOff = VALUE_OFF; + return this; + } + + public DebugFactImpl intoPlain(StringBuilder out) { + this.out = out; + valueOn = PLAIN; + valueOff = PLAIN; + return this; + } + + @Override + public DebugFact text(CharSequence text) { + out.append(text); + return this; + } + + @Override + public DebugFact text(char c) { + out.append(c); + return this; + } + + @Override + public DebugFact value(CharSequence value) { + out.append(valueOn).append(value).append(valueOff); + return this; + } + + @Override + public DebugFact value(int value) { + out.append(valueOn).append(value).append(valueOff); + return this; + } + + @Override + public DebugFact value(long value) { + out.append(valueOn).append(value).append(valueOff); + return this; + } + + @Override + public DebugFact value(double value, int decimals) { + out.append(valueOn); + DebugNumbers.appendFixed(out, value, decimals); + out.append(valueOff); + return this; + } + + @Override + public DebugFact padded(long value, int width) { + out.append(valueOn); + DebugNumbers.appendPadded(out, value, width); + out.append(valueOff); + return this; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGroup.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGroup.java new file mode 100644 index 000000000..f57d0d3ae --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGroup.java @@ -0,0 +1,78 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugGroupSpec; + +public final class DebugGroup implements DebugGroupHandle { + + public static final String PRIORITY_LEFT_ID = "angelica:priority_left"; + + public static final DebugGroup PRIORITY_LEFT = builtin("priority_left", null, -1, LEFT, 0); + public static final DebugGroup RENDERING = builtin("rendering", "rendering", 0xF0B0A0, LEFT, 100); + public static final DebugGroup POSITION = builtin("position", "position", 0xFFFFFF, LEFT, 200); + public static final DebugGroup SHADERS = builtin("shaders", "shaders", 0xFF55FF, LEFT, 300); + public static final DebugGroup OTHER_LEFT = builtin("other_left", "other", -1, LEFT, 800); + public static final DebugGroup HELP = builtin("help", "help", -1, LEFT, 1000); + public static final DebugGroup MEMORY = builtin("memory", "memory", 0xFF9B00, RIGHT, 100); + public static final DebugGroup SYSTEM_SPECS = builtin("system_specs", "system_specs", 0xFF0000, RIGHT, 200); + public static final DebugGroup FORGE = builtin("forge", "forge", 0x5555FF, RIGHT, 300); + public static final DebugGroup ANGELICA = builtin("angelica", "angelica", 0x55FF55, RIGHT, 400); + public static final DebugGroup OTHER_RIGHT = builtin("other_right", "other", -1, RIGHT, 800); + public static final DebugGroup LOOKING_AT_BLOCK = builtin("looking_at_block", "looking_at_block", 0xCC00FF, RIGHT, 900); + + public final String id; + public final String titleKey; + public final int accentColor; + public final int side; + public final int order; + int index = -1; + + DebugGroup(String id, String titleKey, int accentColor, int side, int order) { + this.id = id; + this.titleKey = titleKey; + this.accentColor = accentColor; + this.side = side; + this.order = order; + } + + private static DebugGroup builtin(String path, String title, int accent, int side, int order) { + final DebugGroupSpec spec = new DebugGroupSpec("angelica:" + path).side(side).order(order); + if (title != null) spec.title("angelica.debug.group." + title); + if (accent >= 0) spec.accent(accent); + return DebugRegistry.MAIN.registerGroup(spec); + } + + static void bootstrap() {} + + static DebugGroup of(DebugGroupHandle handle) { + if (handle instanceof DebugGroup group) return group; + throw new IllegalArgumentException("Not a debug group handle from DebugScreenApi: " + handle); + } + + static String requireName(String name) { + if (name == null) throw new IllegalArgumentException("Debug fact name is null"); + return name; + } + + @Override + public String id() { + return id; + } + + @Override + public int side() { + return side; + } + + public int index() { + return index; + } + + public boolean hasTitle() { + return titleKey != null; + } + + public boolean hasAccent() { + return accentColor >= 0; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGroupContents.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGroupContents.java new file mode 100644 index 000000000..333e2d47b --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGroupContents.java @@ -0,0 +1,286 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import java.util.Arrays; + +public final class DebugGroupContents { + + public static final int TEXT = 1; + public static final int SIZE = 2; + + public static final int ROW_HEIGHT = 9; + private static final int SHRINK_HOLD_TICKS = 40; + public static final int MARGIN = 3; + static final int TITLE_PADDING = 3; + static final int FACT_PADDING = 5; + static final int FACT_LINE_GAP = 2; + static final int MIN_VALUE_BUDGET = 32; + + static final int TITLE_BAR_COLOR = 0x90303030; + static final int BODY_COLOR = 0x90505050; + static final int TITLE_COLOR = 0xFFFFFFFF; + static final int NAME_COLOR = 0xFFE0E0E0; + static final int VALUE_COLOR = 0xFFD0D0D0; + static final int LINE_COLOR = 0xFFE0E0E0; + + private static final Slot[] NO_SLOTS = new Slot[0]; + + public static final class Slot { + + StringBuilder text = new StringBuilder(); + private StringBuilder previous = new StringBuilder(); + final StringBuilder wrapped = new StringBuilder(); + int[] rowStart = new int[1]; + int[] rowEnd = new int[1]; + int rowCount; + int width; + String name; + String label; + int nameWidth; + private int wrapBudget = -1; + private boolean isWrapped; + private boolean dirty; + + void begin() { + final StringBuilder swap = previous; + previous = text; + text = swap; + text.setLength(0); + dirty = true; + } + + private boolean sameAsPrevious() { + return CharSequence.compare(text, previous) == 0; + } + + boolean measure(DebugText renderer, int budget, boolean force) { + boolean textChanged = false; + if (dirty) { + dirty = false; + textChanged = !sameAsPrevious(); + } + if (!force && !textChanged && budget == wrapBudget) return false; + wrapBudget = budget; + rowCount = 0; + width = 0; + final int len = text.length(); + final float full = renderer.measure(text, 0, len); + if (full <= budget) { + isWrapped = false; + addRow(0, len, full); + } else { + isWrapped = true; + renderer.wrap(this, budget); + } + return true; + } + + void addRow(int start, int end, float rowWidth) { + if (rowCount == rowStart.length) { + final int n = rowCount * 2; + rowStart = Arrays.copyOf(rowStart, n); + rowEnd = Arrays.copyOf(rowEnd, n); + } + rowStart[rowCount] = start; + rowEnd[rowCount] = end; + rowCount++; + final int w = (int) Math.ceil(rowWidth); + if (w > width) width = w; + } + + CharSequence rowText() { + return isWrapped ? wrapped : text; + } + } + + public final DebugGroup group; + private Slot[] facts = NO_SLOTS; + private int factCount; + private Slot[] lines = NO_SLOTS; + private int lineCount; + private int measuredFactCount; + private int measuredLineCount; + private String title = ""; + private int titleWidth; + private boolean titleDirty; + private int factNameWidth; + private int fullWidth; + private int fullHeight; + private boolean wasEmpty = true; + private int widthHeldSince; + private int heightHeldSince; + private boolean holdReleased; + + public DebugGroupContents(DebugGroup group) { + this.group = group; + } + + public void reset() { + factCount = 0; + lineCount = 0; + } + + public boolean isEmpty() { + return factCount == 0 && lineCount == 0; + } + + public int lineCount() { + return lineCount; + } + + public int factCount() { + return factCount; + } + + public Slot fact(int i) { + return facts[i]; + } + + public Slot line(int i) { + return lines[i]; + } + + public int fullWidth() { + return fullWidth; + } + + public int fullHeight() { + return fullHeight; + } + + void releaseSizeHold() { + holdReleased = true; + } + + void setTitle(String title, int width) { + if (this.title.equals(title) && titleWidth == width) return; + this.title = title; + this.titleWidth = width; + titleDirty = true; + } + + private static Slot[] grow(Slot[] slots, int count) { + if (count < slots.length) return slots; + final int n = Math.max(4, count * 2); + final Slot[] grown = Arrays.copyOf(slots, n); + for (int i = count; i < n; i++) grown[i] = new Slot(); + return grown; + } + + Slot addFact(String name) { + facts = grow(facts, factCount); + final Slot slot = facts[factCount++]; + if (slot.name != name) { + slot.name = name; + slot.label = name.concat(":"); + slot.nameWidth = -1; + } + slot.begin(); + return slot; + } + + Slot addLine() { + lines = grow(lines, lineCount); + final Slot slot = lines[lineCount++]; + slot.begin(); + return slot; + } + + int measure(DebugText renderer, int lineBudget, boolean force, int tick) { + boolean text = force || titleDirty || factCount != measuredFactCount || lineCount != measuredLineCount; + titleDirty = false; + measuredFactCount = factCount; + measuredLineCount = lineCount; + int nameWidth = 0; + for (int i = 0; i < factCount; i++) { + final Slot slot = facts[i]; + if (force || slot.nameWidth < 0) { + slot.nameWidth = renderer.width(slot.name, 0, slot.name.length()); + text = true; + } + if (slot.nameWidth > nameWidth) nameWidth = slot.nameWidth; + } + if (nameWidth != factNameWidth) text = true; + factNameWidth = nameWidth; + final int valueBudget = Math.max(lineBudget - nameWidth - FACT_PADDING, MIN_VALUE_BUDGET); + int rows = 0; + int width = 0; + for (int i = 0; i < factCount; i++) { + final Slot slot = facts[i]; + text |= slot.measure(renderer, valueBudget, force); + rows += slot.rowCount; + final int w = nameWidth + FACT_PADDING + slot.width; + if (w > width) width = w; + } + for (int i = 0; i < lineCount; i++) { + final Slot slot = lines[i]; + text |= slot.measure(renderer, lineBudget, force); + rows += slot.rowCount; + if (slot.width > width) width = slot.width; + } + if (titleWidth > 0 && titleWidth + TITLE_PADDING > width) width = titleWidth + TITLE_PADDING; + int height = rows * ROW_HEIGHT; + if (titleWidth > 0) height += ROW_HEIGHT; + if (factCount > 0 && lineCount > 0) height += FACT_LINE_GAP; + final boolean empty = isEmpty(); + final boolean free = force || holdReleased; + holdReleased = false; + if (free || empty || width >= fullWidth || tick - widthHeldSince >= SHRINK_HOLD_TICKS) { + widthHeldSince = tick; + } else { + width = fullWidth; + } + if (free || empty || height >= fullHeight || tick - heightHeldSince >= SHRINK_HOLD_TICKS) { + heightHeldSince = tick; + } else { + height = fullHeight; + } + int flags = text ? TEXT : 0; + if (width != fullWidth || height != fullHeight || empty != wasEmpty) flags |= SIZE; + fullWidth = width; + fullHeight = height; + wasEmpty = empty; + return flags; + } + + void emitRects(DebugText renderer, int left, int top, int side, int screenWidth) { + int y = top; + if (titleWidth > 0) { + renderer.rect(left - 1, top - 1, left + fullWidth + 1, top + ROW_HEIGHT - 1, TITLE_BAR_COLOR); + y = top + ROW_HEIGHT; + } + renderer.rect(left - 1, y - 1, left + fullWidth + 1, top + fullHeight + 1, BODY_COLOR); + if (group.hasAccent()) { + final int x = side == DebugGroup.LEFT ? 0 : screenWidth - 1; + renderer.rect(x, top - 1, x + 1, top + fullHeight + 1, 0xFF000000 | group.accentColor); + } + } + + void drawText(DebugText renderer, int left, int top) { + int y = top; + if (titleWidth > 0) { + renderer.draw(title, 0, title.length(), left + TITLE_PADDING, top, TITLE_COLOR); + y += ROW_HEIGHT; + } + final int valueX = left + factNameWidth + FACT_PADDING; + for (int i = 0; i < factCount; i++) { + final Slot slot = facts[i]; + renderer.draw(slot.label, 0, slot.label.length(), left + (factNameWidth - slot.nameWidth), y, NAME_COLOR); + y = drawRows(renderer, slot, valueX, y, VALUE_COLOR); + } + if (factCount > 0 && lineCount > 0) y += FACT_LINE_GAP; + for (int i = 0; i < lineCount; i++) { + y = drawRows(renderer, lines[i], left, y, LINE_COLOR); + } + } + + private static int drawRows(DebugText renderer, Slot slot, int x, int y, int color) { + final CharSequence text = slot.rowText(); + for (int row = 0; row < slot.rowCount; row++) { + final int start = slot.rowStart[row]; + final int end = slot.rowEnd[row]; + if (end > start) renderer.draw(text, start, end, x, y, color); + y += ROW_HEIGHT; + } + return y; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGuiScale.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGuiScale.java new file mode 100644 index 000000000..c2e5d8735 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugGuiScale.java @@ -0,0 +1,36 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import net.minecraft.client.Minecraft; + +public final class DebugGuiScale { + + public static final int UNCHANGED = -1; + public static final int AUTO = 0; + + private DebugGuiScale() {} + + public static boolean evenForUnicode(Minecraft mc) { + return mc.func_152349_b() && !AngelicaConfig.removeUnicodeEvenScaling; + } + + public static int maxScale(int displayWidth, int displayHeight, boolean evenForUnicode) { + int scale = 1; + while (displayWidth / (scale + 1) >= 320 && displayHeight / (scale + 1) >= 240) { + scale++; + } + if (evenForUnicode && scale % 2 != 0 && scale != 1) scale--; + return scale; + } + + public static int offeredMax(int maxScale, int gameScale) { + return Math.max(1, Math.min(maxScale, gameScale)); + } + + public static int resolve(int setting, int gameScale, int displayWidth, int displayHeight, boolean evenForUnicode) { + if (setting == UNCHANGED) return gameScale; + final int max = maxScale(displayWidth, displayHeight, evenForUnicode); + final int wanted = setting == AUTO ? Math.max(1, max / 2) : Math.min(setting, max); + return Math.max(1, Math.min(wanted, gameScale)); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugLayout.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugLayout.java new file mode 100644 index 000000000..ac3e356c4 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugLayout.java @@ -0,0 +1,84 @@ +package com.gtnewhorizons.angelica.debug.screen; + +public final class DebugLayout { + + public static final int MARGIN_TOP = 2; + public static final int GROUP_SPACING = 9; + + public final int[] height; + public final int[] side; + public final int[] top; + public final int[] columnHeight = new int[2]; + + private final int[] maxHeight = new int[2]; + private final int[] homeSide; + private final int count; + private final int priority; + + public DebugLayout(DebugRegistry registry) { + count = registry.groupCount(); + height = new int[count]; + side = new int[count]; + top = new int[count]; + homeSide = new int[count]; + for (int g = 0; g < count; g++) { + homeSide[g] = registry.groupAt(g).side; + } + final DebugGroup priorityGroup = registry.groupById(DebugGroup.PRIORITY_LEFT_ID); + priority = priorityGroup == null ? -1 : priorityGroup.index; + } + + private void place(int group, int column) { + top[group] = columnHeight[column]; + side[group] = column; + columnHeight[column] += height[group] + GROUP_SPACING; + } + + private boolean isFull(int column) { + return columnHeight[column] >= maxHeight[column]; + } + + private boolean fits(int group, int column) { + return columnHeight[column] + height[group] <= maxHeight[column]; + } + + public void assign(int top, int leftMaxHeight, int rightMaxHeight) { + columnHeight[DebugGroup.LEFT] = top; + columnHeight[DebugGroup.RIGHT] = top; + maxHeight[DebugGroup.LEFT] = leftMaxHeight; + maxHeight[DebugGroup.RIGHT] = rightMaxHeight; + for (int g = 0; g < count; g++) { + side[g] = -1; + } + if (priority >= 0 && height[priority] > 0) place(priority, homeSide[priority]); + for (int g = 0; g < count; g++) { + if (height[g] <= 0 || side[g] >= 0) continue; + final int column = homeSide[g]; + if (fits(g, column)) place(g, column); + } + for (int g = 0; g < count; g++) { + if (height[g] <= 0 || side[g] >= 0) continue; + final int column = 1 - homeSide[g]; + if (fits(g, column)) place(g, column); + } + for (int g = 0; g < count; g++) { + if (height[g] <= 0 || side[g] >= 0) continue; + final int column = homeSide[g]; + if (!isFull(column)) place(g, column); + else if (!isFull(1 - column)) place(g, 1 - column); + } + } + + public void assignSingleColumn(int column, int columnTop, int columnMaxHeight) { + columnHeight[column] = columnTop; + maxHeight[column] = columnMaxHeight; + for (int g = 0; g < count; g++) { + side[g] = -1; + } + if (priority >= 0 && height[priority] > 0) place(priority, column); + for (int g = 0; g < count; g++) { + if (height[g] <= 0 || side[g] >= 0) continue; + if (!isFull(column)) place(g, column); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugLineRules.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugLineRules.java new file mode 100644 index 000000000..5e71da153 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugLineRules.java @@ -0,0 +1,141 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.AngelicaMod; + +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public final class DebugLineRules { + + private static final int MAX_PREFIX = 64; + private static final Pattern RULE = Pattern.compile("\\s*(drop|fact)\\s+(left|right)\\s+\"([^\"]*)\"((?:\\s+\\w+=(?:\"[^\"]*\"|[^\\s\"]+))*)\\s*"); + private static final Pattern OPTION = Pattern.compile("(\\w+)=(?:\"([^\"]*)\"|([^\\s\"]+))"); + + static final String[] DEFAULTS = { + "drop right \"Allocation rate: \" when=minecraft:memory", + "drop right \"Java: \" when=minecraft:system_specs", + "drop right \"GPU: \" when=minecraft:system_specs", + "drop right \"OpenGL: \" when=minecraft:system_specs", + "drop right \"CPU Cores: \" when=minecraft:system_specs", + "fact right \"IntCache: \" group=angelica:memory when=minecraft:memory", + "fact right \"OS: \" group=angelica:system_specs when=minecraft:system_specs", + "fact right \"LWJGL: \" group=angelica:system_specs when=minecraft:system_specs", + "fact left \"Cube X: \" group=angelica:position when=minecraft:player_position", + "fact left \"Cube Y: \" group=angelica:position when=minecraft:player_position", + "fact left \"Cube Z: \" group=angelica:position when=minecraft:player_position", + "fact left \"Cube: \" group=angelica:position when=minecraft:player_position", + "fact left \"hunger: \" group=angelica:position when=minecraft:player_position name=\"Hunger\"", + "fact right \"HydroEnergy GPU RAM: \" group=angelica:memory when=minecraft:memory", + "fact right \"renderDebugMode: \" group=angelica:rendering when=minecraft:chunk_render_stats name=\"Render Debug Mode\"", + }; + + public record Rule(String prefix, DebugEntryDef when, DebugGroup group, String name) { + + int payloadStart(String line) { + int i = matchPrefix(line, skipCodes(line, 0), prefix); + while (i < line.length() && line.charAt(i) == ' ') i++; + return i; + } + } + + private final Rule[][] rules; + + private DebugLineRules(Rule[][] rules) { + this.rules = rules; + } + + static DebugLineRules compile(DebugRegistry registry, String[] configRules) { + final ArrayList left = new ArrayList<>(); + final ArrayList right = new ArrayList<>(); + if (configRules != null) { + for (String text : configRules) add(registry, text, left, right); + } + for (String text : DEFAULTS) add(registry, text, left, right); + return new DebugLineRules(new Rule[][] { left.toArray(new Rule[0]), right.toArray(new Rule[0]) }); + } + + private static void add(DebugRegistry registry, String text, ArrayList left, ArrayList right) { + final Matcher matcher = text == null ? null : RULE.matcher(text); + final Rule rule = matcher != null && matcher.matches() ? parse(registry, matcher) : null; + if (rule == null) { + AngelicaMod.LOGGER.warn("invalid debug line rule: {}", text); + } else { + (matcher.group(2).equals("left") ? left : right).add(rule); + } + } + + private static Rule parse(DebugRegistry registry, Matcher rule) { + final String prefix = stripCodes(rule.group(3)); + if (prefix.isEmpty() || prefix.length() > MAX_PREFIX) return null; + String groupId = null; + String name = null; + String whenId = null; + final Matcher option = OPTION.matcher(rule.group(4)); + while (option.find()) { + final String value = option.group(2) != null ? option.group(2) : option.group(3); + switch (option.group(1)) { + case "group" -> groupId = value; + case "name" -> name = value; + case "when" -> whenId = value; + default -> { + return null; + } + } + } + final DebugEntryDef when = whenId == null ? null : registry.entryById(whenId); + if (whenId != null && when == null) return null; + if (rule.group(1).equals("drop")) return groupId == null && name == null ? new Rule(prefix, when, null, null) : null; + final DebugGroup group = groupId == null ? null : registry.groupById(groupId); + if (name == null) name = defaultName(prefix); + return group == null || name.isEmpty() ? null : new Rule(prefix, when, group, name); + } + + private static String stripCodes(String s) { + if (s.indexOf(DebugText.FORMAT) < 0) return s; + final StringBuilder sb = new StringBuilder(s.length()); + DebugText.appendStripped(sb, s); + return sb.toString(); + } + + private static String defaultName(String prefix) { + int end = prefix.length(); + while (end > 0 && prefix.charAt(end - 1) == ' ') end--; + if (end > 0 && prefix.charAt(end - 1) == ':') end--; + return prefix.substring(0, end); + } + + static int skipCodes(String s, int from) { + final int last = s.length() - 1; + int i = from; + while (i < last && s.charAt(i) == DebugText.FORMAT) i += 2; + return i; + } + + static int matchPrefix(String line, int rawStart, String prefix) { + final int length = line.length(); + int i = rawStart; + for (int p = 0, n = prefix.length(); p < n; p++) { + i = skipCodes(line, i); + if (i >= length || line.charAt(i) != prefix.charAt(p)) return -1; + i++; + } + return i; + } + + static boolean isBlank(String line) { + final int length = line.length(); + for (int i = skipCodes(line, 0); i < length; i = skipCodes(line, i + 1)) { + if (line.charAt(i) > ' ') return false; + } + return true; + } + + public Rule match(int side, String line) { + final int start = skipCodes(line, 0); + for (Rule rule : rules[side]) { + if ((rule.when == null || rule.when.isEnabled()) && matchPrefix(line, start, rule.prefix) >= 0) return rule; + } + return null; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugNumbers.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugNumbers.java new file mode 100644 index 000000000..6dddf2860 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugNumbers.java @@ -0,0 +1,38 @@ +package com.gtnewhorizons.angelica.debug.screen; + +public final class DebugNumbers { + + private static final long[] POW10 = { 1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L }; + private static final double MAX_SCALED = 9.0e18; + + private DebugNumbers() {} + + public static void appendFixed(StringBuilder out, double value, int decimals) { + final int places = Math.max(0, Math.min(decimals, POW10.length - 1)); + final long scale = POW10[places]; + final double magnitude = Math.abs(value) * scale; + if (Double.isNaN(value) || Double.isInfinite(value) || magnitude >= MAX_SCALED) { + out.append(value); + return; + } + final long scaled = Math.round(magnitude); + if (value < 0 && scaled != 0) out.append('-'); + out.append(scaled / scale); + if (places == 0) return; + out.append('.'); + appendPadded(out, scaled % scale, places); + } + + public static void appendPadded(StringBuilder out, long value, int width) { + if (value >= 0) { + int digits = 1; + for (long rest = value / 10; rest > 0; rest /= 10) { + digits++; + } + for (int i = digits; i < width; i++) { + out.append('0'); + } + } + out.append(value); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugOptionsScreen.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugOptionsScreen.java new file mode 100644 index 000000000..f0bb318e9 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugOptionsScreen.java @@ -0,0 +1,847 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizon.gtnhlib.config.ConfigurationManager; +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.mixins.interfaces.FontRendererAccessor; +import net.minecraft.client.Minecraft; +import net.minecraft.client.audio.PositionedSoundRecord; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.StatCollector; +import org.lwjgl.input.Keyboard; +import org.lwjgl.input.Mouse; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; + +public class DebugOptionsScreen extends GuiScreen { + + private static final int ROW_H = 20; + private static final int TOOLTIP_WRAP_W = 200; + private static final int ROW_BUTTON_W = 60; + private static final int ROW_BUTTON_H = 16; + private static final int ROW_BUTTON_GAP = 2; + private static final int ARROW_W = 20; + private static final int LIST_MAX_W = 350; + private static final int SCROLLBAR_W = 6; + private static final int SCROLLBAR_MIN_THUMB = 10; + private static final int HEADER_H = 28; + private static final int HEADER_LINE_H = 10; + private static final int FOOTER_W = 120; + private static final int FOOTER_H = 20; + private static final int FOOTER_GAP = 4; + private static final int WHEEL_ROWS = 3; + private static final int TOOLTIP_OFFSET = 12; + + private static final ResourceLocation BUTTON_PRESS = new ResourceLocation("gui.button.press"); + private static final Comparator BY_ID = Comparator.comparing(d -> d.id); + private static final Comparator GROUPS_BY_ID = Comparator.comparing(g -> g.id); + + private enum RowKind { HEADER, ENTRY, SCALE, PANEL } + + private static final int NORMAL_COLOR = 0xFFFFFF; + private static final int WARNING_COLOR = 0xFF5555; + private static final int INACTIVE_COLOR = 0x909090; + private static final int NOTICE_COLOR = 0xFFFF55; + private static final int DISABLED_COLOR = 0x505050; + private static final int BUTTON_COLOR = DebugGroupContents.BODY_COLOR; + private static final int BUTTON_LIT_COLOR = DebugGroupContents.TITLE_BAR_COLOR; + private static final int OUTLINE_COLOR = 0xFFA0A0A0; + private static final int PLACEHOLDER_COLOR = 0x707070; + private static final int FIELD_BOX_COLOR = 0xFF000000; + private static final int FIELD_TEXT_COLOR = 0xE0E0E0; + private static final int FIELD_CURSOR_COLOR = 0xFFD0D0D0; + private static final int FIELD_SELECTION_COLOR = 0xFF0000FF; + private static final int TOOLTIP_BACKGROUND = 0xF0100010; + private static final int TOOLTIP_BORDER = 0x505000FF; + private static final int TOOLTIP_TEXT_COLOR = 0xFFFFFFFF; + + private record Choice(DebugScreenEntryStatus status, String labelKey, int activeColor) {} + + private static final Choice[] CHOICES = { + new Choice(DebugScreenEntryStatus.NEVER, "options.off", 0xFFFFFF), + new Choice(DebugScreenEntryStatus.IN_OVERLAY, "angelica.debug.options.status.overlay", 0xFFFF55), + new Choice(DebugScreenEntryStatus.ALWAYS_ON, "angelica.debug.options.status.always", 0xDF5050) + }; + private static final int ROW_BUTTONS_W = ROW_BUTTON_W * CHOICES.length + ROW_BUTTON_GAP * (CHOICES.length - 1); + + private final class Button { + final int id; + final int w; + final int h; + final String label; + final int labelW; + int x; + int y; + boolean enabled = true; + boolean selected; + boolean pressable = true; + int activeColor; + + Button(int id, int w, int h, String label) { + this.id = id; + this.w = w; + this.h = h; + this.label = label; + this.labelW = fontRendererObj.getStringWidth(label); + } + + void setSelected(boolean selected, boolean pressable, int activeColor) { + this.selected = selected; + this.pressable = pressable; + this.activeColor = activeColor; + } + + boolean contains(int mouseX, int mouseY) { + return mouseX >= x && mouseY >= y && mouseX < x + w && mouseY < y + h; + } + + boolean pressed(int mouseX, int mouseY) { + return enabled && pressable && contains(mouseX, mouseY); + } + + void draw(boolean hovered) { + rect(x, y, x + w, y + h, selected || hovered && enabled ? BUTTON_LIT_COLOR : BUTTON_COLOR); + if (selected) { + rect(x, y, x + w, y + 1, OUTLINE_COLOR); + rect(x, y + h - 1, x + w, y + h, OUTLINE_COLOR); + rect(x, y + 1, x + 1, y + h - 1, OUTLINE_COLOR); + rect(x + w - 1, y + 1, x + w, y + h - 1, OUTLINE_COLOR); + } + drawText(label, x + (w - labelW) / 2, y + (h - 8) / 2, !enabled ? DISABLED_COLOR : selected ? activeColor : INACTIVE_COLOR); + } + } + + private final class SearchField extends GuiTextField { + private int blink; + private String measuredText; + private int measuredScroll = -1; + private int measuredCursor = -1; + private int measuredSelection = -1; + private String shown; + private int cursor; + private int selection; + private boolean cursorShown; + private int prefixW; + private int selectionW; + private boolean cursorPending; + private boolean cursorInsert; + private int pendingX; + private int pendingY; + + SearchField(int x, int y, int w, int h) { + super(fontRendererObj, x, y, w, h); + } + + @Override + public void updateCursorCounter() { + blink++; + } + + @Override + public void setFocused(boolean focused) { + if (focused && !isFocused()) blink = 0; + super.setFocused(focused); + } + + private void measure(String text) { + measuredText = text; + measuredScroll = lineScrollOffset; + measuredCursor = getCursorPosition(); + measuredSelection = getSelectionEnd(); + shown = fontRendererObj.trimStringToWidth(text.substring(lineScrollOffset), getWidth()); + cursor = measuredCursor - lineScrollOffset; + selection = Math.min(measuredSelection - lineScrollOffset, shown.length()); + cursorShown = cursor >= 0 && cursor <= shown.length(); + prefixW = fontRendererObj.getStringWidth(cursorShown ? shown.substring(0, cursor) : shown); + selectionW = fontRendererObj.getStringWidth(shown.substring(0, selection)); + } + + @Override + public void drawTextBox() { + cursorPending = false; + if (!getVisible()) return; + rect(xPosition - 1, yPosition - 1, xPosition + width + 1, yPosition + height + 1, OUTLINE_COLOR); + rect(xPosition, yPosition, xPosition + width, yPosition + height, FIELD_BOX_COLOR); + final String text = getText(); + if (text != measuredText || lineScrollOffset != measuredScroll || getCursorPosition() != measuredCursor || getSelectionEnd() != measuredSelection) measure(text); + + final int textX = xPosition + 4; + final int textY = yPosition + (height - 8) / 2; + final int end = shown.isEmpty() ? textX : textX + prefixW + 1; + final boolean insert = measuredCursor < text.length() || text.length() >= getMaxStringLength(); + int cursorX = end; + if (!cursorShown) { + cursorX = cursor > 0 ? textX + width : textX; + } else if (insert) { + cursorX = end - 1; + } + if (selection != cursor) { + final int right = xPosition + width; + final int selectionX = textX + selectionW - 1; + rect(Math.min(Math.min(cursorX, selectionX), right), textY - 1, Math.min(Math.max(cursorX, selectionX), right), textY + 1 + fontRendererObj.FONT_HEIGHT, FIELD_SELECTION_COLOR); + } + if (!shown.isEmpty()) drawText(shown, textX, textY, FIELD_TEXT_COLOR); + cursorPending = isFocused() && blink / 6 % 2 == 0 && cursorShown; + cursorInsert = insert; + pendingX = cursorX; + pendingY = textY; + } + + void drawCursor() { + if (!cursorPending) return; + cursorPending = false; + if (cursorInsert) { + rect(pendingX, pendingY - 1, pendingX + 1, pendingY + 1 + fontRendererObj.FONT_HEIGHT, FIELD_CURSOR_COLOR); + } else { + drawText("_", pendingX, pendingY, FIELD_TEXT_COLOR); + } + } + } + + private static final class Row { + final RowKind kind; + final DebugEntryDef def; + final String plain; + final String normalText; + final List normalTooltip; + final Button[] buttons; + String text; + int textW; + List tooltip; + String value; + int valueW; + int color = NORMAL_COLOR; + boolean failed; + + Row(RowKind kind, DebugEntryDef def, String plain, String normalText, List normalTooltip, Button[] buttons) { + this.kind = kind; + this.def = def; + this.plain = plain; + this.normalText = normalText; + this.normalTooltip = normalTooltip; + this.buttons = buttons; + this.text = normalText; + this.tooltip = normalTooltip; + } + } + + public static void open(Minecraft mc) { + if (DebugScreen.ENTRIES == null) return; + mc.displayGuiScreen(new DebugOptionsScreen()); + } + + private final ArrayList rows = new ArrayList<>(); + private final ArrayList thirdParty = new ArrayList<>(); + private final ArrayList panels = new ArrayList<>(); + private final Button[] profileButtons = new Button[DebugScreenProfile.VALUES.length]; + private Button doneButton; + + private FontRendererAccessor fonts; + private BatchingFontRenderer batcher; + private DebugText.Batched painter; + + private int[] headerLineX; + private List measuredTooltip; + private int measuredTooltipW; + + private String search = ""; + private int scrollRow; + private int visibleRows; + private boolean dragging; + private int dragOffset; + private boolean grouped; + private long lastVersion; + private boolean configDirty; + private int scaleMax; + + private SearchField searchField; + + private String title; + private String searchPlaceholder; + private int cursorW; + private List failedTooltip; + private List headerLines; + private int headerColor; + + private int titleX; + private int listX; + private int listW; + private int nameX; + private int nameMaxW; + private int buttonsX; + private int viewTop; + private int viewH; + private int footerTop; + + @Override + public void initGui() { + if (DebugScreen.ENTRIES == null) { + mc.displayGuiScreen(null); + return; + } + Keyboard.enableRepeatEvents(true); + dragging = false; + if (fontRendererObj instanceof FontRendererAccessor accessor) { + fonts = accessor; + batcher = accessor.angelica$getBatcher(); + painter = new DebugText.Batched(fontRendererObj, batcher); + } else { + fonts = null; + batcher = null; + painter = null; + } + measuredTooltip = null; + + title = translate("angelica.debug.options.title"); + searchPlaceholder = translate("angelica.debug.options.search"); + failedTooltip = wrap(translate("angelica.debug.options.failed.tooltip")); + + final boolean even = DebugGuiScale.evenForUnicode(mc); + final int gameScale = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight).getScaleFactor(); + scaleMax = DebugGuiScale.offeredMax(DebugGuiScale.maxScale(mc.displayWidth, mc.displayHeight, even), gameScale); + + listW = Math.min(LIST_MAX_W, width - 20); + listX = (width - listW) / 2; + nameX = listX + 4; + buttonsX = listX + listW - SCROLLBAR_W - 4 - ROW_BUTTONS_W; + nameMaxW = buttonsX - 4 - nameX; + + final int titleW = fontRendererObj.getStringWidth(title); + titleX = (width - titleW) / 2; + final int searchX = titleX + titleW + 8; + searchField = new SearchField(searchX, 6, Math.max(40, Math.min(120, width - searchX - 8)), 18); + searchField.setMaxStringLength(32); + searchField.setText(search); + searchField.setFocused(true); + cursorW = fontRendererObj.getStringWidth("_"); + searchPlaceholder = fontRendererObj.trimStringToWidth(searchPlaceholder, searchField.getWidth() - cursorW); + + initFooter(); + layoutView(); + buildRows(); + } + + private void initFooter() { + final int count = profileButtons.length + 1; + final int buttonW = Math.min(FOOTER_W, (width - 8 - (count - 1) * FOOTER_GAP) / count); + final int rowW = count * buttonW + (count - 1) * FOOTER_GAP; + final int left = (width - rowW) / 2; + footerTop = height - 6 - FOOTER_H; + for (int i = 0; i < profileButtons.length; i++) { + final Button button = new Button(i, buttonW, FOOTER_H, translate(DebugScreenProfile.VALUES[i].translationKey)); + button.x = left + i * (buttonW + FOOTER_GAP); + button.y = footerTop; + profileButtons[i] = button; + } + doneButton = new Button(profileButtons.length, buttonW, FOOTER_H, translate("gui.done")); + doneButton.x = left + profileButtons.length * (buttonW + FOOTER_GAP); + doneButton.y = footerTop; + } + + private void layoutView() { + grouped = DebugScreen.isGroupedActive(); + final String header = translate(grouped ? "angelica.debug.options.warning" : "angelica.debug.options.flat_notice"); + headerLines = fontRendererObj.listFormattedStringToWidth(header, Math.min(width - 20, 400)); + headerLineX = new int[headerLines.size()]; + for (int i = 0; i < headerLineX.length; i++) { + headerLineX[i] = width / 2 - fontRendererObj.getStringWidth(headerLines.get(i)) / 2; + } + headerColor = grouped ? WARNING_COLOR : NOTICE_COLOR; + viewTop = HEADER_H + headerLines.size() * HEADER_LINE_H + 2; + visibleRows = Math.max(1, (footerTop - 6 - viewTop) / ROW_H); + viewH = visibleRows * ROW_H; + } + + private List wrap(String text) { + return fontRendererObj.listFormattedStringToWidth(text, TOOLTIP_WRAP_W); + } + + private static boolean isBuiltin(String namespace) { + return "minecraft".equals(namespace) || "forge".equals(namespace) || "angelica".equals(namespace); + } + + private Row headerRow(String label) { + final Row row = new Row(RowKind.HEADER, null, null, EnumChatFormatting.BOLD + "" + EnumChatFormatting.UNDERLINE + label, null, null); + row.textW = fontRendererObj.getStringWidth(row.text); + return row; + } + + private void buildRows() { + rows.clear(); + final String query = search.toLowerCase(Locale.ROOT); + final DebugRegistry registry = DebugRegistry.MAIN; + final DebugEntryCategory[] categories = DebugEntryCategory.values(); + for (int c = 0; c < categories.length; c++) { + final DebugEntryCategory category = categories[c]; + final int start = rows.size(); + thirdParty.clear(); + for (int i = 0, n = registry.entryCount(); i < n; i++) { + final DebugEntryDef def = registry.entryAt(i); + if (def.category != category) continue; + if (!grouped && category == DebugEntryCategory.SCREEN_TEXT && !def.flat) continue; + if (def == DebugScreenEntries.THREE_DIMENSIONAL_CROSSHAIR && !AngelicaConfig.modernizeF3Screen) continue; + final boolean builtin = isBuiltin(def.namespace); + if (!query.isEmpty() && !(builtin ? def.path : def.id).contains(query) && !(def.name != null && translate(def.name).toLowerCase(Locale.ROOT).contains(query))) continue; + if (builtin) { + rows.add(entryRow(def, false)); + } else { + thirdParty.add(def); + } + } + thirdParty.sort(BY_ID); + for (int i = 0, n = thirdParty.size(); i < n; i++) { + rows.add(entryRow(thirdParty.get(i), true)); + } + if (rows.size() > start) rows.add(start, headerRow(translate(category.translationKey))); + } + if (grouped) { + final int start = rows.size(); + addLayoutRow(RowKind.SCALE, translate("angelica.debug.options.text_size"), wrap(translate("angelica.debug.options.text_size.tooltip")), "<", ">", query); + if (rows.size() > start) rows.add(start, headerRow(translate("angelica.debug.options.category.layout"))); + addPanelRows(registry, query); + } + thirdParty.clear(); + refreshStates(); + scrollTo(scrollRow); + } + + private void addLayoutRow(RowKind kind, String label, List tooltip, String decrease, String increase, String query) { + if (!query.isEmpty() && !label.toLowerCase(Locale.ROOT).contains(query)) return; + final String trimmed = fontRendererObj.trimStringToWidth(label, nameMaxW); + final List shownTooltip = tooltip != null ? tooltip : trimmed.equals(label) ? null : wrap(label); + final Button[] buttons = { + new Button(0, ARROW_W, ROW_BUTTON_H, decrease), + new Button(1, ARROW_W, ROW_BUTTON_H, increase) + }; + final Row row = new Row(kind, null, label, trimmed, shownTooltip, buttons); + refreshLayoutValue(row); + rows.add(row); + } + + private void addPanelRows(DebugRegistry registry, String query) { + for (int i = 0, n = registry.groupCount(); i < n; i++) panels.add(registry.groupAt(i)); + panels.sort(GROUPS_BY_ID); + final int start = rows.size(); + for (int i = 0, n = panels.size(); i < n; i++) { + final DebugGroup group = panels.get(i); + final String title = group.hasTitle() ? translate(group.titleKey) : group.id; + if (!query.isEmpty() && !group.id.contains(query) && !title.toLowerCase(Locale.ROOT).contains(query)) continue; + final List tooltip = wrap(StatCollector.translateToLocalFormatted("angelica.debug.options.panels.tooltip", group.id)); + final Row row = new Row(RowKind.PANEL, null, title, fontRendererObj.trimStringToWidth(title, nameMaxW), tooltip, null); + row.value = fontRendererObj.trimStringToWidth(group.id, ROW_BUTTONS_W); + row.valueW = fontRendererObj.getStringWidth(row.value); + rows.add(row); + } + panels.clear(); + if (rows.size() > start) rows.add(start, headerRow(translate("angelica.debug.options.category.panels"))); + } + + private Row entryRow(DebugEntryDef def, boolean external) { + final boolean named = def.name != null; + final String full = named ? translate(def.name) : external ? def.id : def.path; + final String trimmed = fontRendererObj.trimStringToWidth(full, nameMaxW); + final String normalText; + if (external && !named) { + final int split = Math.min(trimmed.length(), def.namespace.length() + 1); + normalText = EnumChatFormatting.GRAY + trimmed.substring(0, split) + EnumChatFormatting.WHITE + trimmed.substring(split); + } else { + normalText = trimmed; + } + final List tooltip; + final String helpKey = named ? def.name + ".tooltip" : null; + if (helpKey != null && StatCollector.canTranslate(helpKey)) { + final List lines = new ArrayList<>(wrap(translate(helpKey))); + lines.addAll(wrap(EnumChatFormatting.DARK_GRAY + def.id)); + tooltip = lines; + } else { + tooltip = !named && trimmed.equals(full) ? null : wrap(def.id); + } + final boolean always = def.supportsAlways && (grouped || def.category == DebugEntryCategory.RENDERER); + final Button[] buttons = new Button[always ? CHOICES.length : CHOICES.length - 1]; + for (int i = 0; i < buttons.length; i++) { + buttons[i] = new Button(i, ROW_BUTTON_W, ROW_BUTTON_H, translate(CHOICES[i].labelKey)); + } + return new Row(RowKind.ENTRY, def, trimmed, normalText, tooltip, buttons); + } + + private void refreshStates() { + final DebugScreenEntryList entries = DebugScreen.ENTRIES; + lastVersion = entries.version(); + for (int i = 0, n = rows.size(); i < n; i++) { + final Row row = rows.get(i); + if (row.kind != RowKind.ENTRY) continue; + final boolean failed = row.def.isFailed(); + if (failed != row.failed) { + row.failed = failed; + if (failed) { + row.text = EnumChatFormatting.STRIKETHROUGH + row.plain; + row.tooltip = failedTooltip; + row.color = WARNING_COLOR; + } else { + row.text = row.normalText; + row.tooltip = row.normalTooltip; + row.color = NORMAL_COLOR; + } + } + final DebugScreenEntryStatus current = entries.effectiveStatus(row.def); + final DebugScreenEntryStatus stored = entries.getStatus(row.def); + final DebugScreenEntryStatus shown = current == DebugScreenEntryStatus.ALWAYS_ON && row.buttons.length < CHOICES.length ? DebugScreenEntryStatus.IN_OVERLAY : current; + for (final Button button : row.buttons) { + final Choice choice = CHOICES[button.id]; + button.enabled = !failed; + button.setSelected(choice.status == shown, choice.status != shown || choice.status != stored, choice.activeColor); + } + } + for (int i = 0; i < profileButtons.length; i++) { + final boolean using = entries.isUsingProfile(DebugScreenProfile.VALUES[i]); + profileButtons[i].setSelected(using, !using, NORMAL_COLOR); + } + } + + private void refreshLayoutValue(Row row) { + switch (row.kind) { + case SCALE -> { + final int scale = Math.min(AngelicaConfig.debugGuiScale, scaleMax); + row.value = scale <= DebugGuiScale.UNCHANGED ? translate("angelica.debug.options.text_size.match") + : scale == DebugGuiScale.AUTO ? translate("angelica.debug.options.text_size.auto") : Integer.toString(scale); + } + default -> {} + } + if (row.value != null) row.valueW = fontRendererObj.getStringWidth(row.value); + } + + private void stepLayout(Row row, int direction) { + switch (row.kind) { + case SCALE -> { + int next = Math.max(DebugGuiScale.UNCHANGED, Math.min(AngelicaConfig.debugGuiScale, scaleMax)) + direction; + if (next > scaleMax) next = DebugGuiScale.UNCHANGED; + else if (next < DebugGuiScale.UNCHANGED) next = scaleMax; + AngelicaConfig.debugGuiScale = next; + } + default -> {} + } + configDirty = true; + refreshLayoutValue(row); + } + + private int maxScroll() { + return Math.max(0, rows.size() - visibleRows); + } + + private int shownEnd() { + return Math.min(rows.size(), scrollRow + visibleRows); + } + + private void scrollTo(int row) { + scrollRow = Math.max(0, Math.min(row, maxScroll())); + for (int i = scrollRow, end = shownEnd(); i < end; i++) { + final Row shown = rows.get(i); + if (shown.buttons == null) continue; + final int y = viewTop + (i - scrollRow) * ROW_H + (ROW_H - ROW_BUTTON_H) / 2; + if (shown.kind == RowKind.ENTRY) { + for (final Button button : shown.buttons) { + button.x = buttonsX + button.id * (ROW_BUTTON_W + ROW_BUTTON_GAP); + button.y = y; + } + } else { + shown.buttons[0].x = buttonsX; + shown.buttons[0].y = y; + shown.buttons[1].x = buttonsX + ROW_BUTTONS_W - ARROW_W; + shown.buttons[1].y = y; + } + } + } + + private int thumbHeight() { + return Math.max(SCROLLBAR_MIN_THUMB, viewH * visibleRows / rows.size()); + } + + private void dragTo(int mouseY) { + final int max = maxScroll(); + if (max == 0) return; + final int track = viewH - thumbHeight(); + if (track <= 0) return; + scrollTo(((mouseY - dragOffset - viewTop) * max + track / 2) / track); + } + + private static String translate(String key) { + return StatCollector.translateToLocal(key); + } + + @Override + protected void keyTyped(char typedChar, int keyCode) { + final int page = Math.max(1, visibleRows - 1); + final int step = switch (keyCode) { + case Keyboard.KEY_UP -> -1; + case Keyboard.KEY_DOWN -> 1; + case Keyboard.KEY_PRIOR -> -page; + case Keyboard.KEY_NEXT -> page; + default -> 0; + }; + if (step != 0) { + scrollTo(scrollRow + step); + return; + } + if (searchField.textboxKeyTyped(typedChar, keyCode)) { + final String text = searchField.getText(); + if (!text.equals(search)) { + search = text; + scrollRow = 0; + buildRows(); + } + return; + } + super.keyTyped(typedChar, keyCode); + } + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { + searchField.mouseClicked(mouseX, mouseY, mouseButton); + searchField.setFocused(true); + if (mouseButton != 0) return; + + if (doneButton.pressed(mouseX, mouseY)) { + playPressSound(); + mc.displayGuiScreen(null); + return; + } + for (final Button button : profileButtons) { + if (!button.pressed(mouseX, mouseY)) continue; + playPressSound(); + DebugScreen.ENTRIES.loadProfile(DebugScreenProfile.VALUES[button.id]); + DebugScreen.ENTRIES.save(); + refreshStates(); + return; + } + + final int barX = listX + listW - SCROLLBAR_W; + if (mouseX >= barX && mouseX < barX + SCROLLBAR_W && mouseY >= viewTop && mouseY < viewTop + viewH) { + final int max = maxScroll(); + if (max == 0) return; + final int thumbH = thumbHeight(); + final int thumbY = viewTop + (viewH - thumbH) * scrollRow / max; + dragging = true; + if (mouseY >= thumbY && mouseY < thumbY + thumbH) { + dragOffset = mouseY - thumbY; + } else { + dragOffset = thumbH / 2; + dragTo(mouseY); + } + return; + } + + for (int i = scrollRow, end = shownEnd(); i < end; i++) { + final Row row = rows.get(i); + if (row.buttons == null) continue; + for (final Button button : row.buttons) { + if (!button.pressed(mouseX, mouseY)) continue; + playPressSound(); + if (row.kind == RowKind.ENTRY) { + DebugScreen.ENTRIES.setStatus(row.def, CHOICES[button.id].status); + refreshStates(); + } else { + stepLayout(row, button.id == 0 ? -1 : 1); + } + return; + } + } + } + + @Override + protected void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) { + if (dragging && clickedMouseButton == 0) dragTo(mouseY); + } + + @Override + protected void mouseMovedOrUp(int mouseX, int mouseY, int state) { + if (state == 0) dragging = false; + super.mouseMovedOrUp(mouseX, mouseY, state); + } + + @Override + public void handleMouseInput() { + super.handleMouseInput(); + final int wheel = Mouse.getEventDWheel(); + if (wheel != 0) scrollTo(scrollRow - Integer.signum(wheel) * WHEEL_ROWS); + } + + @Override + public void updateScreen() { + searchField.updateCursorCounter(); + if (DebugScreen.isGroupedActive() != grouped) { + layoutView(); + buildRows(); + } else if (DebugScreen.ENTRIES.version() != lastVersion) { + refreshStates(); + } + } + + @Override + public void onGuiClosed() { + Keyboard.enableRepeatEvents(false); + if (configDirty) { + configDirty = false; + ConfigurationManager.save(AngelicaConfig.class); + } + } + + @Override + public boolean doesGuiPauseGame() { + return false; + } + + private void playPressSound() { + mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(BUTTON_PRESS, 1.0F)); + } + + private void rect(int x1, int y1, int x2, int y2, int argb) { + if (painter != null) { + painter.rect(x1, y1, x2, y2, argb); + } else { + Gui.drawRect(x1, y1, x2, y2, argb); + } + } + + private void drawText(String text, int x, int y, int color) { + if (fonts != null) { + fonts.angelica$drawStringBatched(text, x, y, color, true); + } else { + fontRendererObj.drawStringWithShadow(text, x, y, color); + } + } + + private void beginLayer() { + if (painter == null) return; + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + batcher.beginBatch(); + painter.beginRects(); + } + + private void endLayer() { + if (painter == null) return; + try { + painter.endRects(); + } finally { + batcher.endBatch(); + } + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + final List tooltip; + try { + beginLayer(); + tooltip = drawContents(mouseX, mouseY); + } finally { + endLayer(); + } + if (tooltip == null && !searchField.cursorPending) return; + try { + beginLayer(); + searchField.drawCursor(); + if (tooltip != null) drawTooltip(tooltip, mouseX, mouseY); + } finally { + endLayer(); + } + } + + private List drawContents(int mouseX, int mouseY) { + rect(0, 0, width, viewTop, 0x60000000); + rect(0, footerTop - 4, width, height, 0x60000000); + rect(listX, viewTop, listX + listW, viewTop + viewH, 0x40000000); + + drawText(title, titleX, 11, NORMAL_COLOR); + for (int i = 0; i < headerLineX.length; i++) { + drawText(headerLines.get(i), headerLineX[i], HEADER_H + i * HEADER_LINE_H, headerColor); + } + + searchField.drawTextBox(); + if (search.isEmpty()) { + drawText(searchPlaceholder, searchField.xPosition + 4 + cursorW, searchField.yPosition + 5, PLACEHOLDER_COLOR); + } + + List tooltip = null; + for (int i = scrollRow, end = shownEnd(); i < end; i++) { + final Row row = rows.get(i); + final int y = viewTop + (i - scrollRow) * ROW_H; + if (row.kind == RowKind.HEADER) { + drawText(row.text, listX + listW / 2 - row.textW / 2, y + 6, NORMAL_COLOR); + continue; + } + drawText(row.text, nameX, y + 6, row.color); + if (row.kind == RowKind.PANEL) { + drawText(row.value, buttonsX + ROW_BUTTONS_W - row.valueW, y + 6, INACTIVE_COLOR); + } else { + if (row.value != null) drawText(row.value, buttonsX + ROW_BUTTONS_W / 2 - row.valueW / 2, y + 6, NORMAL_COLOR); + for (final Button button : row.buttons) { + button.draw(button.contains(mouseX, mouseY)); + } + } + final int hoverRight = row.buttons == null ? buttonsX + ROW_BUTTONS_W : buttonsX; + if (row.tooltip != null && mouseX >= nameX && mouseX < hoverRight && mouseY >= y && mouseY < y + ROW_H) { + tooltip = row.tooltip; + } + } + + final int max = maxScroll(); + if (max > 0) { + final int thumbH = thumbHeight(); + final int thumbY = viewTop + (viewH - thumbH) * scrollRow / max; + final int barX = listX + listW - SCROLLBAR_W; + rect(barX, viewTop, barX + SCROLLBAR_W, viewTop + viewH, 0x60000000); + rect(barX, thumbY, barX + SCROLLBAR_W, thumbY + thumbH, dragging ? 0xFFFFFFFF : OUTLINE_COLOR); + } + + for (final Button button : profileButtons) { + button.draw(button.contains(mouseX, mouseY)); + } + doneButton.draw(doneButton.contains(mouseX, mouseY)); + return tooltip; + } + + private void drawTooltip(List lines, int mouseX, int mouseY) { + if (lines != measuredTooltip) { + int widest = 0; + for (int i = 0, n = lines.size(); i < n; i++) { + widest = Math.max(widest, fontRendererObj.getStringWidth(lines.get(i))); + } + measuredTooltip = lines; + measuredTooltipW = widest; + } + final int w = measuredTooltipW; + final int h = lines.size() > 1 ? 10 + (lines.size() - 1) * 10 : 8; + int x = mouseX + TOOLTIP_OFFSET; + int y = mouseY - TOOLTIP_OFFSET; + if (x + w > width) x -= 28 + w; + x = Math.max(4, x); + if (y + h + 6 > height) y = height - h - 6; + y = Math.max(4, y); + + rect(x - 3, y - 4, x + w + 3, y - 3, TOOLTIP_BACKGROUND); + rect(x - 3, y + h + 3, x + w + 3, y + h + 4, TOOLTIP_BACKGROUND); + rect(x - 3, y - 3, x + w + 3, y + h + 3, TOOLTIP_BACKGROUND); + rect(x - 4, y - 3, x - 3, y + h + 3, TOOLTIP_BACKGROUND); + rect(x + w + 3, y - 3, x + w + 4, y + h + 3, TOOLTIP_BACKGROUND); + rect(x - 3, y - 2, x - 2, y + h + 2, TOOLTIP_BORDER); + rect(x + w + 2, y - 2, x + w + 3, y + h + 2, TOOLTIP_BORDER); + rect(x - 3, y - 3, x + w + 3, y - 2, TOOLTIP_BORDER); + rect(x - 3, y + h + 2, x + w + 3, y + h + 3, TOOLTIP_BORDER); + + for (int i = 0, n = lines.size(); i < n; i++) { + drawText(lines.get(i), x, y, TOOLTIP_TEXT_COLOR); + y += i == 0 ? 12 : 10; + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugRegistry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugRegistry.java new file mode 100644 index 000000000..bb72c979e --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugRegistry.java @@ -0,0 +1,197 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugGroupSpec; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; + +public final class DebugRegistry { + + private static final Logger LOGGER = LogManager.getLogger("Angelica"); + private static final DebugGroup[] NO_GROUPS = new DebugGroup[0]; + private static final DebugEntryDef[] NO_ENTRIES = new DebugEntryDef[0]; + + public static final DebugRegistry MAIN = new DebugRegistry(); + + private final ArrayList openGroups = new ArrayList<>(); + private final ArrayList openEntries = new ArrayList<>(); + private final HashMap groupsById = new HashMap<>(); + private final HashMap entriesById = new HashMap<>(); + private DebugGroup[] groups = NO_GROUPS; + private DebugEntryDef[] entries = NO_ENTRIES; + private DebugEntryDef[] flat = NO_ENTRIES; + private DebugEntryDef[] renderers = NO_ENTRIES; + private int rendererSequence; + private boolean frozen; + private boolean failuresPending; + + static boolean isValidId(String id) { + if (id == null) return false; + final int colon = id.indexOf(':'); + final int length = id.length(); + if (colon <= 0 || colon == length - 1) return false; + for (int i = 0; i < length; i++) { + if (i == colon) continue; + final char c = id.charAt(i); + if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_' || c == '.' || c == '-') continue; + if (c == '/' && i > colon) continue; + return false; + } + return true; + } + + private void requireOpen() { + if (frozen) throw new IllegalStateException("The debug screen registry is frozen; register before the first client tick"); + } + + public DebugGroup registerGroup(DebugGroupSpec spec) { + requireOpen(); + final String id = spec.getId(); + if (!isValidId(id)) throw new IllegalArgumentException("Invalid debug group id: " + id); + if (groupsById.containsKey(id)) throw new IllegalArgumentException("Duplicate debug group id: " + id); + final int side = spec.getSide(); + if (side != DebugGroupHandle.LEFT && side != DebugGroupHandle.RIGHT) throw new IllegalArgumentException("Invalid side for debug group " + id + ": " + side); + final DebugGroup group = new DebugGroup(id, spec.getTitleKey(), spec.getAccent(), side, spec.getOrder()); + openGroups.add(group); + groupsById.put(id, group); + return group; + } + + public DebugEntryDef registerEntry(DebugEntrySpec spec) { + return register(spec, 0, spec.getCategory() == DebugEntryCategory.SCREEN_TEXT); + } + + DebugEntryDef registerBuiltin(DebugEntrySpec spec, int kind, boolean flat) { + return register(spec, kind, flat); + } + + private DebugEntryDef register(DebugEntrySpec spec, int kind, boolean isFlat) { + requireOpen(); + final String id = spec.getId(); + if (!isValidId(id)) throw new IllegalArgumentException("Invalid debug entry id: " + id); + if (entriesById.containsKey(id)) throw new IllegalArgumentException("Duplicate debug entry id: " + id); + final DebugEntryCategory category = spec.getCategory(); + if (category == null || spec.getDefaultStatus() == null || spec.getPerformanceStatus() == null) { + throw new IllegalArgumentException("Debug entry " + id + " needs a category and both profile statuses"); + } + if (category == DebugEntryCategory.RENDERER && spec.getTextFactory() != null) { + throw new IllegalArgumentException("Renderer debug entry " + id + " must not have a text factory"); + } + final int rendererIndex = category == DebugEntryCategory.RENDERER ? rendererSequence++ : -1; + final DebugEntryDef def = new DebugEntryDef(this, spec, openEntries.size(), kind, rendererIndex, isFlat); + openEntries.add(def); + entriesById.put(id, def); + return def; + } + + public void freeze() { + if (frozen) return; + frozen = true; + final int groupCount = openGroups.size(); + final DebugGroup[] sorted = openGroups.toArray(new DebugGroup[groupCount]); + Arrays.sort(sorted, Comparator.comparingInt((DebugGroup g) -> g.side).thenComparingInt(g -> g.order)); + for (int i = 0; i < groupCount; i++) { + sorted[i].index = i; + } + groups = sorted; + entries = openEntries.toArray(new DebugEntryDef[openEntries.size()]); + int flatCount = 0; + for (final DebugEntryDef def : entries) { + if (isFlatText(def)) flatCount++; + } + flat = new DebugEntryDef[flatCount]; + renderers = new DebugEntryDef[rendererSequence]; + int f = 0; + for (final DebugEntryDef def : entries) { + if (isFlatText(def)) flat[f++] = def; + if (def.rendererIndex >= 0) renderers[def.rendererIndex] = def; + } + openGroups.clear(); + openGroups.trimToSize(); + openEntries.clear(); + openEntries.trimToSize(); + } + + private static boolean isFlatText(DebugEntryDef def) { + return def.flat && def.isScreenText(); + } + + public void failAll() { + for (final DebugEntryDef def : entriesById.values()) def.failed = true; + } + + public boolean hasNamespace(String namespace) { + final String prefix = namespace + ':'; + for (final String id : groupsById.keySet()) { + if (id.startsWith(prefix)) return true; + } + for (final String id : entriesById.keySet()) { + if (id.startsWith(prefix)) return true; + } + return false; + } + + public boolean isFrozen() { + return frozen; + } + + public int entryCount() { + return entries.length; + } + + public DebugEntryDef entryAt(int index) { + return entries[index]; + } + + public DebugEntryDef entryById(String id) { + return entriesById.get(id); + } + + public int groupCount() { + return groups.length; + } + + public DebugGroup groupAt(int index) { + return groups[index]; + } + + public DebugGroup groupById(String id) { + return groupsById.get(id); + } + + public int flatCount() { + return flat.length; + } + + public DebugEntryDef flatAt(int index) { + return flat[index]; + } + + public int rendererCount() { + return renderers.length; + } + + public DebugEntryDef rendererAt(int index) { + return renderers[index]; + } + + public void fail(DebugEntryDef def, Throwable cause) { + if (def.failed) return; + def.failed = true; + failuresPending = true; + LOGGER.error("Debug screen entry {} failed and is disabled for the rest of this session", def.id, cause); + } + + public boolean consumeFailures() { + final boolean pending = failuresPending; + failuresPending = false; + return pending; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreen.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreen.java new file mode 100644 index 000000000..a04be149a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreen.java @@ -0,0 +1,140 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.AngelicaMod; +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.compat.debug.DebugCompat; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.config.SystemProperties; +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugRenderers; +import cpw.mods.fml.common.Loader; +import net.minecraft.client.Minecraft; + +import java.io.File; + +public final class DebugScreen { + + public static DebugScreenEntryList ENTRIES; + + private static final DebugContextImpl CONTEXT = new DebugContextImpl(); + + private static boolean overlayVisible; + private static int clientTick; + private static boolean groupedFailed; + private static boolean freezeAttempted; + + private DebugScreen() {} + + public static void bootstrap() { + DebugGroup.bootstrap(); + DebugScreenEntries.bootstrap(); + } + + private static void freeze(File configDir) { + bootstrap(); + DebugCompat.registerMissing(); + DebugRegistry.MAIN.freeze(); + final DebugScreenEntryList entries = new DebugScreenEntryList(DebugRegistry.MAIN); + entries.setFile(new File(configDir, "angelica-debug-profile.json")); + entries.setListener(DebugRenderers::onEnabledChanged); + ENTRIES = entries; + entries.load(); + entries.setOverlayVisible(overlayVisible); + } + + public static void syncOverlayVisible() { + final boolean visible = Minecraft.getMinecraft().gameSettings.showDebugInfo; + overlayVisible = visible; + final DebugScreenEntryList entries = ENTRIES; + if (entries != null && entries.isOverlayVisible() != visible) entries.setOverlayVisible(visible); + } + + public static void onClientTickStart() { + if (!freezeAttempted) { + freezeAttempted = true; + try { + freeze(Loader.instance().getConfigDir()); + } catch (RuntimeException | LinkageError e) { + ENTRIES = null; + DebugRegistry.MAIN.failAll(); + AngelicaMod.LOGGER.error("Failed to set up the debug screen; it stays disabled for this session", e); + } + } + syncOverlayVisible(); + final DebugScreenEntryList entries = ENTRIES; + if (entries != null) entries.refresh(); + } + + public static void onClientTickEnd() { + clientTick++; + } + + public static boolean isOverlayVisible() { + return overlayVisible; + } + + public static int clientTick() { + return clientTick; + } + + public static boolean isRendererVerbose() { + return (SystemProperties.DEBUG_F3_DETAIL && DebugScreenEntries.ANGELICA_RENDERER.isEnabled()) || DebugScreenEntries.ANGELICA_RENDERER_VERBOSE.isEnabled(); + } + + public static boolean isOverlayIdle() { + final DebugScreenEntryList entries = ENTRIES; + return !overlayVisible && (entries == null || !entries.anyTextEnabled()); + } + + public static boolean isGroupedActive() { + return AngelicaConfig.modernizeF3Screen && !groupedFailed && AngelicaConfig.enableFontRenderer; + } + + public static void disableGrouped() { + groupedFailed = true; + } + + public static boolean isAlwaysOnTextAtTopLeft() { + final DebugScreenEntryList entries = ENTRIES; + return entries != null && isGroupedActive() && entries.anyTextAlwaysOn() && Minecraft.getMinecraft().theWorld != null; + } + + public static void fail(DebugEntryDef def, Throwable cause) { + def.registry.fail(def, cause); + } + + static void display(DebugEntryDef def, DebugScreenDisplayer displayer, DebugContext context) { + if (context.world() == null || context.player() == null) return; + try { + def.entryOrCreate().display(displayer, context); + } catch (RuntimeException | LinkageError e) { + fail(def, e); + } + } + + static void runEntries(DebugScreenDisplayer displayer, Minecraft mc, long version, DebugRegistry flatFrom, boolean alwaysOnOnly) { + final DebugContextImpl context = CONTEXT; + context.prepare(mc, System.nanoTime(), version); + try { + if (flatFrom != null) { + for (int i = 0, n = flatFrom.flatCount(); i < n; i++) { + final DebugEntryDef def = flatFrom.flatAt(i); + if (!def.isEnabled()) continue; + context.beginEntry(def); + display(def, displayer, context); + } + return; + } + final DebugScreenEntryList entries = ENTRIES; + for (int i = 0, n = entries.enabledCount(); i < n; i++) { + final DebugEntryDef def = entries.enabledAt(i); + if (!def.isScreenText()) continue; + if (alwaysOnOnly && !def.isAlwaysOn()) continue; + context.beginEntry(def); + display(def, displayer, context); + } + } finally { + context.release(); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntries.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntries.java new file mode 100644 index 000000000..910a3ce97 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntries.java @@ -0,0 +1,173 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntryHandle; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.ChunkDebugMinimap; +import com.gtnewhorizons.angelica.debug.screen.entries.AngelicaEntries; +import com.gtnewhorizons.angelica.debug.screen.entries.AngelicaRendererEntry; +import com.gtnewhorizons.angelica.debug.screen.entries.AngelicaShadersEntry; +import com.gtnewhorizons.angelica.debug.screen.entries.ForgeBrandingsEntry; +import com.gtnewhorizons.angelica.debug.screen.entries.HeaderEntries; +import com.gtnewhorizons.angelica.debug.screen.entries.LookingAtBlockEntry; +import com.gtnewhorizons.angelica.debug.screen.entries.MemoryEntry; +import com.gtnewhorizons.angelica.debug.screen.entries.PositionEntries; +import com.gtnewhorizons.angelica.debug.screen.entries.RenderingEntries; +import com.gtnewhorizons.angelica.debug.screen.entries.SystemSpecsEntry; +import net.minecraft.client.renderer.entity.RenderManager; + +import static com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus.ALWAYS_ON; +import static com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus.IN_OVERLAY; +import static com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus.NEVER; + +public final class DebugScreenEntries { + + public static final int KIND_GAME_VERSION = 1; + public static final int KIND_FPS = 2; + public static final int KIND_TPS = 3; + public static final int KIND_PLAYER_POSITION = 4; + public static final int KIND_PLAYER_SECTION_POSITION = 5; + public static final int KIND_PLAYER_SPEED = 6; + public static final int KIND_BIOME = 7; + public static final int KIND_LIGHT_LEVELS = 8; + public static final int KIND_HEIGHTMAP = 9; + public static final int KIND_CHUNK_RENDER_STATS = 10; + public static final int KIND_ENTITY_RENDER_STATS = 11; + public static final int KIND_PARTICLE_RENDER_STATS = 12; + public static final int KIND_CHUNK_SOURCE_STATS = 13; + public static final int KIND_MEMORY = 14; + public static final int KIND_SYSTEM_SPECS = 15; + public static final int KIND_LOOKING_AT_BLOCK = 16; + public static final int KIND_FORGE_BRANDINGS = 17; + public static final int KIND_ANGELICA_RENDERER = 18; + public static final int KIND_ANGELICA_SHADERS = 20; + public static final int KIND_ANGELICA_DYNAMIC_LIGHTS = 21; + public static final int KIND_ANGELICA_FOG = 22; + public static final int KIND_ANGELICA_ANIMATIONS = 23; + public static final int KIND_HELP = 26; + public static final int KIND_ENTITY_HITBOXES = 28; + public static final int KIND_CHUNK_BORDERS = 29; + public static final int KIND_CHUNK_SECTION_VISIBILITY = 30; + public static final int KIND_VISUALIZE_BLOCK_LIGHT_LEVELS = 31; + public static final int KIND_VISUALIZE_SKY_LIGHT_LEVELS = 32; + public static final int KIND_VISUALIZE_HEIGHTMAP = 33; + public static final int KIND_VISUALIZE_COLLISION_BOXES = 34; + public static final int KIND_VISUALIZE_SOLID_FACES = 35; + public static final int KIND_VISUALIZE_WATER_LEVELS = 36; + public static final int KIND_VISUALIZE_ENTITY_SUPPORTING_BLOCK = 37; + public static final int KIND_ANGELICA_CHUNK_MINIMAP = 38; + public static final int KIND_POST_EFFECT = 39; + + private static final String NAME_KEY_PREFIX = "angelica.debug.entry."; + + public static final DebugEntryDef GAME_VERSION = add(text("minecraft:game_version", IN_OVERLAY, NEVER), KIND_GAME_VERSION, false); + public static final DebugEntryDef FPS = add(text("minecraft:fps", IN_OVERLAY, ALWAYS_ON), KIND_FPS, false); + public static final DebugEntryDef TPS = add(text("minecraft:tps", IN_OVERLAY, IN_OVERLAY), KIND_TPS, true); + public static final DebugEntryDef PLAYER_POSITION = add(text("minecraft:player_position", IN_OVERLAY, NEVER), KIND_PLAYER_POSITION, false); + public static final DebugEntryDef PLAYER_SECTION_POSITION = add(text("minecraft:player_section_position", IN_OVERLAY, NEVER), KIND_PLAYER_SECTION_POSITION, false); + public static final DebugEntryDef PLAYER_SPEED = add(text("minecraft:player_speed", IN_OVERLAY, NEVER), KIND_PLAYER_SPEED, false); + public static final DebugEntryDef BIOME = add(text("minecraft:biome", IN_OVERLAY, NEVER), KIND_BIOME, false); + public static final DebugEntryDef LIGHT_LEVELS = add(text("minecraft:light_levels", IN_OVERLAY, NEVER), KIND_LIGHT_LEVELS, false); + public static final DebugEntryDef HEIGHTMAP = add(text("minecraft:heightmap", IN_OVERLAY, NEVER), KIND_HEIGHTMAP, false); + public static final DebugEntryDef CHUNK_RENDER_STATS = add(text("minecraft:chunk_render_stats", IN_OVERLAY, NEVER), KIND_CHUNK_RENDER_STATS, false); + public static final DebugEntryDef ENTITY_RENDER_STATS = add(text("minecraft:entity_render_stats", IN_OVERLAY, NEVER), KIND_ENTITY_RENDER_STATS, false); + public static final DebugEntryDef PARTICLE_RENDER_STATS = add(text("minecraft:particle_render_stats", IN_OVERLAY, NEVER), KIND_PARTICLE_RENDER_STATS, false); + public static final DebugEntryDef POST_EFFECT = add(text("minecraft:post_effect", IN_OVERLAY, NEVER), KIND_POST_EFFECT, false); + public static final DebugEntryDef CHUNK_SOURCE_STATS = add(text("minecraft:chunk_source_stats", IN_OVERLAY, NEVER), KIND_CHUNK_SOURCE_STATS, false); + public static final DebugEntryDef MEMORY = add(text("minecraft:memory", IN_OVERLAY, IN_OVERLAY), KIND_MEMORY, false); + public static final DebugEntryDef SYSTEM_SPECS = add(text("minecraft:system_specs", IN_OVERLAY, NEVER), KIND_SYSTEM_SPECS, false); + public static final DebugEntryDef LOOKING_AT_BLOCK = add(text("minecraft:looking_at_block", IN_OVERLAY, NEVER), KIND_LOOKING_AT_BLOCK, true); + public static final DebugEntryDef FORGE_BRANDINGS = add(text("forge:brandings", IN_OVERLAY, NEVER), KIND_FORGE_BRANDINGS, false); + public static final DebugEntryDef ANGELICA_RENDERER = add(text("angelica:renderer", IN_OVERLAY, IN_OVERLAY), KIND_ANGELICA_RENDERER, true); + public static final DebugEntryDef ANGELICA_RENDERER_VERBOSE = add(gate("angelica:renderer_verbose", NEVER, NEVER).supportsAlways(false), 0, false); + + static { + if (AngelicaConfig.enableIris) { + add(text("angelica:shaders", IN_OVERLAY, NEVER), KIND_ANGELICA_SHADERS, true); + } + } + + public static final DebugEntryDef ANGELICA_DYNAMIC_LIGHTS = add(text("angelica:dynamic_lights", IN_OVERLAY, NEVER), KIND_ANGELICA_DYNAMIC_LIGHTS, true); + public static final DebugEntryDef ANGELICA_FOG = add(text("angelica:fog", NEVER, NEVER), KIND_ANGELICA_FOG, true); + public static final DebugEntryDef ANGELICA_ANIMATIONS = add(text("angelica:animations", IN_OVERLAY, NEVER), KIND_ANGELICA_ANIMATIONS, true); + public static final DebugEntryDef OTHER_LEFT = add(gate("angelica:other_left", IN_OVERLAY, NEVER).supportsAlways(false), 0, false); + public static final DebugEntryDef OTHER_RIGHT = add(gate("angelica:other_right", IN_OVERLAY, NEVER).supportsAlways(false), 0, false); + public static final DebugEntryDef HELP = add(text("angelica:help", IN_OVERLAY, NEVER).supportsAlways(false), KIND_HELP, false); + public static final DebugEntryDef THREE_DIMENSIONAL_CROSSHAIR = add(renderer("minecraft:3d_crosshair", IN_OVERLAY, NEVER), 0, false); + public static final DebugEntryDef ENTITY_HITBOXES = add(renderer("minecraft:entity_hitboxes", NEVER, NEVER).toggle(DebugScreenEntries::onToggle), KIND_ENTITY_HITBOXES, false); + public static final DebugEntryDef CHUNK_BORDERS = add(renderer("minecraft:chunk_borders", NEVER, NEVER), KIND_CHUNK_BORDERS, false); + public static final DebugEntryDef CHUNK_SECTION_VISIBILITY = add(renderer("minecraft:chunk_section_visibility", NEVER, NEVER), KIND_CHUNK_SECTION_VISIBILITY, false); + public static final DebugEntryDef VISUALIZE_BLOCK_LIGHT_LEVELS = add(renderer("minecraft:visualize_block_light_levels", NEVER, NEVER), KIND_VISUALIZE_BLOCK_LIGHT_LEVELS, false); + public static final DebugEntryDef VISUALIZE_SKY_LIGHT_LEVELS = add(renderer("minecraft:visualize_sky_light_levels", NEVER, NEVER), KIND_VISUALIZE_SKY_LIGHT_LEVELS, false); + public static final DebugEntryDef VISUALIZE_HEIGHTMAP = add(renderer("minecraft:visualize_heightmap", NEVER, NEVER), KIND_VISUALIZE_HEIGHTMAP, false); + public static final DebugEntryDef VISUALIZE_COLLISION_BOXES = add(renderer("minecraft:visualize_collision_boxes", NEVER, NEVER), KIND_VISUALIZE_COLLISION_BOXES, false); + public static final DebugEntryDef VISUALIZE_SOLID_FACES = add(renderer("minecraft:visualize_solid_faces", NEVER, NEVER), KIND_VISUALIZE_SOLID_FACES, false); + public static final DebugEntryDef VISUALIZE_WATER_LEVELS = add(renderer("minecraft:visualize_water_levels", NEVER, NEVER), KIND_VISUALIZE_WATER_LEVELS, false); + public static final DebugEntryDef VISUALIZE_ENTITY_SUPPORTING_BLOCK = add(renderer("minecraft:visualize_entity_supporting_block", NEVER, NEVER), KIND_VISUALIZE_ENTITY_SUPPORTING_BLOCK, false); + public static final DebugEntryDef ANGELICA_CHUNK_MINIMAP = add(renderer("angelica:chunk_minimap", NEVER, NEVER).toggle(DebugScreenEntries::onToggle), KIND_ANGELICA_CHUNK_MINIMAP, false); + + private DebugScreenEntries() {} + + static void bootstrap() {} + + private static DebugScreenEntry create(DebugEntryHandle entry) { + final DebugEntryDef def = (DebugEntryDef) entry; + return switch (def.kind) { + case KIND_GAME_VERSION -> HeaderEntries::gameVersion; + case KIND_FPS -> HeaderEntries::fps; + case KIND_TPS -> RenderingEntries::tps; + case KIND_PLAYER_POSITION -> new PositionEntries.PlayerPosition(); + case KIND_PLAYER_SECTION_POSITION -> PositionEntries::sectionPosition; + case KIND_PLAYER_SPEED -> PositionEntries::speed; + case KIND_BIOME -> new PositionEntries.Biome(); + case KIND_LIGHT_LEVELS -> PositionEntries::lightLevels; + case KIND_HEIGHTMAP -> PositionEntries::heightmap; + case KIND_CHUNK_RENDER_STATS, KIND_ENTITY_RENDER_STATS, KIND_PARTICLE_RENDER_STATS, KIND_CHUNK_SOURCE_STATS -> new RenderingEntries.RenderStats(def.kind); + case KIND_POST_EFFECT -> new RenderingEntries.PostEffect(); + case KIND_MEMORY -> new MemoryEntry(); + case KIND_SYSTEM_SPECS -> new SystemSpecsEntry(); + case KIND_LOOKING_AT_BLOCK -> new LookingAtBlockEntry(); + case KIND_FORGE_BRANDINGS -> new ForgeBrandingsEntry(); + case KIND_ANGELICA_RENDERER -> new AngelicaRendererEntry(); + case KIND_ANGELICA_SHADERS -> new AngelicaShadersEntry(); + case KIND_ANGELICA_DYNAMIC_LIGHTS -> AngelicaEntries::dynamicLights; + case KIND_ANGELICA_FOG -> AngelicaEntries::fog; + case KIND_ANGELICA_ANIMATIONS -> AngelicaEntries::animations; + case KIND_HELP -> new HeaderEntries.Help(); + default -> throw new IllegalArgumentException(def.id); + }; + } + + private static void onToggle(DebugEntryHandle entry, boolean enabled) { + switch (((DebugEntryDef) entry).kind) { + case KIND_ENTITY_HITBOXES -> RenderManager.debugBoundingBox = enabled; + case KIND_ANGELICA_CHUNK_MINIMAP -> { + if (enabled) { + ChunkDebugMinimap.enable(); + } else { + ChunkDebugMinimap.disable(); + } + } + default -> {} + } + } + + private static DebugEntrySpec text(String id, DebugScreenEntryStatus defaultStatus, DebugScreenEntryStatus performanceStatus) { + return new DebugEntrySpec(id, DebugEntryCategory.SCREEN_TEXT).defaults(defaultStatus, performanceStatus).text(DebugScreenEntries::create); + } + + private static DebugEntrySpec gate(String id, DebugScreenEntryStatus defaultStatus, DebugScreenEntryStatus performanceStatus) { + return new DebugEntrySpec(id, DebugEntryCategory.SCREEN_TEXT).defaults(defaultStatus, performanceStatus); + } + + private static DebugEntrySpec renderer(String id, DebugScreenEntryStatus defaultStatus, DebugScreenEntryStatus performanceStatus) { + return new DebugEntrySpec(id, DebugEntryCategory.RENDERER).defaults(defaultStatus, performanceStatus); + } + + private static DebugEntryDef add(DebugEntrySpec spec, int kind, boolean flat) { + return DebugRegistry.MAIN.registerBuiltin(spec.name(NAME_KEY_PREFIX + spec.getId().replace(':', '.')), kind, flat); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntryList.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntryList.java new file mode 100644 index 000000000..864b25885 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntryList.java @@ -0,0 +1,389 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + +public final class DebugScreenEntryList { + + private static final Logger LOGGER = LogManager.getLogger("Angelica"); + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + static final int FORMAT_VERSION = 2; + static final int MAX_REBUILD_PASSES = 8; + private static final String KEY_VERSION = "version"; + private static final String KEY_PROFILE = "profile"; + private static final String KEY_CUSTOM = "custom"; + + private final DebugRegistry registry; + private final int count; + private final DebugScreenEntryStatus[] statuses; + private final DebugScreenEntryStatus[] overrides; + private final DebugEntryDef[] enabledOrder; + private final DebugEntryDef[] flipped; + private final LinkedHashMap unknownEntries = new LinkedHashMap<>(); + private final LinkedHashMap unknownMembers = new LinkedHashMap<>(); + private int enabledCount; + private int flipCount; + private int overrideCount; + private boolean anyTextEnabled; + private boolean anyTextAlwaysOn; + private boolean overlayVisible; + private boolean rebuilding; + private boolean rebuildAgain; + private DebugScreenProfile profile; + private File file; + private Runnable listener; + private long version; + + public DebugScreenEntryList(DebugRegistry registry) { + if (!registry.isFrozen()) throw new IllegalStateException("The debug screen registry must be frozen first"); + this.registry = registry; + this.count = registry.entryCount(); + this.statuses = new DebugScreenEntryStatus[count]; + this.overrides = new DebugScreenEntryStatus[count]; + this.enabledOrder = new DebugEntryDef[count]; + this.flipped = new DebugEntryDef[count]; + resetToProfile(DebugScreenProfile.DEFAULT); + } + + public void setFile(File file) { + this.file = file; + } + + public void setListener(Runnable listener) { + this.listener = listener; + } + + private static DebugScreenEntryStatus clamp(DebugEntryDef def, DebugScreenEntryStatus status) { + return status == DebugScreenEntryStatus.ALWAYS_ON && !def.supportsAlways ? DebugScreenEntryStatus.IN_OVERLAY : status; + } + + private void resetToDefault() { + unknownMembers.clear(); + resetToProfile(DebugScreenProfile.DEFAULT); + rebuildCurrentList(); + } + + public void load() { + unknownEntries.clear(); + unknownMembers.clear(); + if (file == null) { + resetToDefault(); + return; + } + if (!file.exists()) { + resetToDefault(); + save(); + return; + } + try (Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { + final JsonElement root = new JsonParser().parse(reader); + if (root == null || !root.isJsonObject()) throw new IllegalStateException("Expected a JSON object"); + applyJson(root.getAsJsonObject()); + } catch (IOException | RuntimeException e) { + LOGGER.error("Failed to load debug screen profile from {}, resetting to default", file, e); + resetToDefault(); + quarantine(); + save(); + return; + } + rebuildCurrentList(); + } + + private void quarantine() { + final Path source = file.toPath(); + try { + Files.move(source, source.resolveSibling(file.getName() + ".corrupt"), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + LOGGER.error("Failed to set aside the unreadable debug screen profile {}", file, e); + } + } + + private static DebugScreenEntryStatus statusOf(JsonElement value) { + return DebugScreenEntryStatus.bySerializedName(stringOf(value)); + } + + private static String stringOf(JsonElement value) { + return value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isString() ? value.getAsString() : null; + } + + private void applyJson(JsonObject json) { + final JsonElement customElement = json.get(KEY_CUSTOM); + if (customElement != null && !customElement.isJsonObject()) throw new IllegalStateException("Expected custom to be an object"); + final JsonObject custom = customElement == null ? null : customElement.getAsJsonObject(); + for (final Map.Entry member : json.entrySet()) { + final String key = member.getKey(); + if (KEY_VERSION.equals(key) || KEY_PROFILE.equals(key) || KEY_CUSTOM.equals(key)) continue; + unknownMembers.put(key, member.getValue()); + } + final JsonElement profileElement = json.get(KEY_PROFILE); + if (profileElement != null || custom == null) { + final DebugScreenProfile selected = DebugScreenProfile.bySerializedName(stringOf(profileElement)); + resetToProfile(selected == null ? DebugScreenProfile.DEFAULT : selected); + return; + } + for (final Map.Entry member : custom.entrySet()) { + if (registry.entryById(member.getKey()) == null) unknownEntries.put(member.getKey(), member.getValue()); + } + for (int i = 0; i < count; i++) { + final DebugEntryDef def = registry.entryAt(i); + final DebugScreenEntryStatus status = statusOf(custom.get(def.id)); + statuses[i] = clamp(def, status == null ? def.defaultStatus : status); + } + reattachProfile(); + } + + public void save() { + if (file == null) return; + final JsonObject json = new JsonObject(); + json.addProperty(KEY_VERSION, FORMAT_VERSION); + if (profile != null) { + json.addProperty(KEY_PROFILE, profile.serializedName); + } else { + final JsonObject custom = new JsonObject(); + for (int i = 0; i < count; i++) { + final DebugEntryDef def = registry.entryAt(i); + if (statuses[i] != clamp(def, def.defaultStatus)) custom.addProperty(def.id, statuses[i].serializedName); + } + for (final Map.Entry member : unknownEntries.entrySet()) { + custom.add(member.getKey(), member.getValue()); + } + json.add(KEY_CUSTOM, custom); + } + for (final Map.Entry member : unknownMembers.entrySet()) { + json.add(member.getKey(), member.getValue()); + } + final Path target = file.toPath(); + final Path temp = target.resolveSibling(file.getName() + ".tmp"); + try { + final File parent = file.getParentFile(); + if (parent != null) Files.createDirectories(parent.toPath()); + Files.write(temp, GSON.toJson(json).getBytes(StandardCharsets.UTF_8)); + try { + Files.move(temp, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + } catch (AtomicMoveNotSupportedException e) { + Files.move(temp, target, StandardCopyOption.REPLACE_EXISTING); + } + } catch (IOException e) { + LOGGER.error("Failed to save debug screen profile to {}", file, e); + try { + Files.deleteIfExists(temp); + } catch (IOException ignored) { + } + } + } + + private void resetToProfile(DebugScreenProfile profile) { + this.profile = profile; + unknownEntries.clear(); + dropOverrides(); + for (int i = 0; i < count; i++) { + final DebugEntryDef def = registry.entryAt(i); + statuses[i] = clamp(def, def.profileStatus(profile)); + } + } + + public void loadProfile(DebugScreenProfile profile) { + resetToProfile(profile); + rebuildCurrentList(); + } + + public boolean isUsingProfile(DebugScreenProfile profile) { + return this.profile == profile; + } + + public DebugScreenEntryStatus getStatus(DebugEntryDef def) { + return statuses[def.index]; + } + + public DebugScreenEntryStatus effectiveStatus(DebugEntryDef def) { + final DebugScreenEntryStatus override = overrides[def.index]; + return override == null ? statuses[def.index] : override; + } + + public void setStatus(DebugEntryDef def, DebugScreenEntryStatus status) { + final int index = def.index; + statuses[index] = clamp(def, status); + if (overrides[index] != null) { + overrides[index] = null; + overrideCount--; + } + reattachProfile(); + rebuildCurrentList(); + save(); + } + + private void reattachProfile() { + profile = matchingProfile(); + if (profile != null) unknownEntries.clear(); + } + + private DebugScreenProfile matchingProfile() { + for (final DebugScreenProfile candidate : DebugScreenProfile.VALUES) { + if (matches(candidate)) return candidate; + } + return null; + } + + private boolean matches(DebugScreenProfile candidate) { + for (int i = 0; i < count; i++) { + final DebugEntryDef def = registry.entryAt(i); + if (statuses[i] != clamp(def, def.profileStatus(candidate))) return false; + } + return true; + } + + public boolean toggleStatus(DebugEntryDef def) { + if (def.category == DebugEntryCategory.RENDERER && def.supportsAlways) { + overrideForSession(def, def.isEnabled() ? DebugScreenEntryStatus.NEVER : DebugScreenEntryStatus.ALWAYS_ON); + return effectiveStatus(def) != DebugScreenEntryStatus.NEVER; + } + final DebugScreenEntryStatus next = switch (effectiveStatus(def)) { + case ALWAYS_ON -> DebugScreenEntryStatus.NEVER; + case IN_OVERLAY -> overlayVisible || !def.supportsAlways ? DebugScreenEntryStatus.NEVER : DebugScreenEntryStatus.ALWAYS_ON; + default -> overlayVisible ? DebugScreenEntryStatus.IN_OVERLAY : DebugScreenEntryStatus.ALWAYS_ON; + }; + overrideForSession(def, next); + return effectiveStatus(def) != DebugScreenEntryStatus.NEVER; + } + + public void overrideForSession(DebugEntryDef def, DebugScreenEntryStatus status) { + final DebugScreenEntryStatus previous = overrides[def.index]; + final DebugScreenEntryStatus next = status == null ? null : clamp(def, status); + if (previous == next) return; + if (previous == null) overrideCount++; + if (next == null) overrideCount--; + overrides[def.index] = next; + rebuildCurrentList(); + } + + public void clearSessionOverrides() { + if (overrideCount == 0) return; + dropOverrides(); + rebuildCurrentList(); + } + + private void dropOverrides() { + if (overrideCount == 0) return; + Arrays.fill(overrides, 0, count, null); + overrideCount = 0; + } + + public int enabledCount() { + return enabledCount; + } + + public DebugEntryDef enabledAt(int index) { + return enabledOrder[index]; + } + + public boolean anyTextEnabled() { + return anyTextEnabled; + } + + public boolean anyTextAlwaysOn() { + return anyTextAlwaysOn; + } + + public boolean isOverlayVisible() { + return overlayVisible; + } + + public void setOverlayVisible(boolean visible) { + if (overlayVisible != visible) { + overlayVisible = visible; + rebuildCurrentList(); + } + } + + public long version() { + return version; + } + + public void refresh() { + if (registry.consumeFailures()) rebuildCurrentList(); + } + + public void rebuildCurrentList() { + if (rebuilding) { + rebuildAgain = true; + return; + } + rebuilding = true; + try { + int passes = 0; + do { + rebuildAgain = false; + rebuildPass(); + } while (rebuildAgain && ++passes < MAX_REBUILD_PASSES); + if (rebuildAgain) failUnsettledListeners(); + } finally { + rebuilding = false; + } + } + + private void failUnsettledListeners() { + rebuildAgain = false; + LOGGER.error("Debug screen toggle listeners kept changing entry statuses after {} passes; failing the {} entries that flipped last", MAX_REBUILD_PASSES, flipCount); + final IllegalStateException cause = new IllegalStateException("Toggle listener did not settle within " + MAX_REBUILD_PASSES + " rebuild passes"); + for (int i = 0; i < flipCount; i++) { + registry.fail(flipped[i], cause); + } + } + + private void rebuildPass() { + Arrays.fill(flipped, 0, flipCount, null); + enabledCount = 0; + anyTextEnabled = false; + anyTextAlwaysOn = false; + int flips = 0; + for (int i = 0; i < count; i++) { + final DebugEntryDef def = registry.entryAt(i); + final DebugScreenEntryStatus status = effectiveStatus(def); + final boolean always = !def.failed && status == DebugScreenEntryStatus.ALWAYS_ON; + final boolean on = always || !def.failed && overlayVisible && status == DebugScreenEntryStatus.IN_OVERLAY; + def.alwaysOn = always; + if (def.enabled != on) { + def.enabled = on; + if (def.toggleListener != null) flipped[flips++] = def; + } + if (on) { + enabledOrder[enabledCount++] = def; + if (def.isScreenText()) { + anyTextEnabled = true; + anyTextAlwaysOn |= always; + } + } + } + version++; + flipCount = flips; + for (int i = 0; i < flips; i++) { + final DebugEntryDef def = flipped[i]; + try { + def.toggleListener.onEnabledChanged(def, def.enabled); + } catch (RuntimeException | LinkageError e) { + registry.fail(def, e); + } + } + if (listener != null) listener.run(); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenOverlay.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenOverlay.java new file mode 100644 index 000000000..39d333d1a --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenOverlay.java @@ -0,0 +1,445 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.AngelicaMod; +import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; +import com.gtnewhorizons.angelica.client.font.GlyphSettings; +import com.gtnewhorizons.angelica.client.font.TextMesh; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.F3Graph; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.profiling.Tracy; +import com.gtnewhorizons.angelica.mixins.interfaces.FontRendererAccessor; +import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.util.StatCollector; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.Arrays; + +public final class DebugScreenOverlay { + + public static final DebugScreenOverlay INSTANCE = new DebugScreenOverlay(); + + public enum Mode { OVERLAY, ALWAYS_ON, FLAT } + + private static final int COLUMN_BUDGET_INSET = 8; + private static final int FORGE_ROW_HEIGHT = 10; + private static final int CHROME = -1; + private static final Tracy.ZoneId Z_ENTRIES = Tracy.zoneId("f3Entries", Tracy.COLOR_CLIENT); + private static final Tracy.ZoneId Z_MEASURE = Tracy.zoneId("f3Measure", Tracy.COLOR_CLIENT); + private static final Tracy.ZoneId Z_DRAW = Tracy.zoneId("f3Draw", Tracy.COLOR_CLIENT); + private static final Tracy.ZoneId Z_CAPTURE = Tracy.zoneId("f3Capture", Tracy.COLOR_CLIENT); + + private final DebugRegistry registry = DebugRegistry.MAIN; + private final TextMesh chrome = new TextMesh(); + private final ForgeLineFilter.Sink externalSink = this::appendExternal; + private final StringBuilder dump = new StringBuilder(); + private GlyphSettings textSettings; + private GlyphSettings frameSettings; + + private int groupCount = -1; + private ForgeLineFilter filter; + private GroupedDebugDisplayer displayer; + private FlatDebugDisplayer flat; + private DebugLayout layout; + private TextMesh[] meshes; + private boolean[] groupDirty; + private boolean[] placed; + private int[] placedX; + private int[] placedY; + private int placedCount; + + private DebugText.Batched text; + private FontRenderer textFont; + private boolean textUnicode; + + private long titlesTranslatedAt = -1L; + private int lastTick = Integer.MIN_VALUE; + private long lastVersion = -1L; + private Mode lastMode; + private int lastHeight = -1; + private boolean chromeDirty = true; + + private boolean scaled; + private int scale; + private int gameScale; + private int screenWidth; + + private boolean ran; + private boolean skipped; + private Mode lastDraw; + + private DebugScreenOverlay() {} + + public void afterDebugBlock(boolean ran, boolean skipped) { + this.ran = ran; + this.skipped = skipped; + } + + public boolean debugBlockRan() { + return ran; + } + + private void ensureSized() { + if (groupCount >= 0) return; + final int n = registry.groupCount(); + filter = new ForgeLineFilter(registry); + displayer = new GroupedDebugDisplayer(registry); + flat = new FlatDebugDisplayer(registry); + layout = new DebugLayout(registry); + meshes = new TextMesh[n]; + for (int g = 0; g < n; g++) meshes[g] = new TextMesh(); + groupDirty = new boolean[n]; + placed = new boolean[n]; + placedX = new int[n]; + placedY = new int[n]; + groupCount = n; + } + + public void addFlatLines(Minecraft mc, ArrayList left, ArrayList right) { + final DebugScreenEntryList entries = DebugScreen.ENTRIES; + if (entries == null) return; + ensureSized(); + flat.rebuild(mc, DebugScreen.clientTick(), entries.version()); + flat.addTo(left, right); + lastDraw = Mode.FLAT; + } + + public boolean onTextEvent(RenderGameOverlayEvent.Text event, FontRenderer font, int width, int height, boolean canceled) { + if (canceled) return true; + final DebugScreenEntryList entries = DebugScreen.ENTRIES; + if (entries == null || !DebugScreen.isGroupedActive()) return canceled; + if (!(font instanceof FontRendererAccessor)) { + AngelicaMod.LOGGER.error("Grouped debug screen needs the batching font renderer on the HUD font; using the classic layout for the rest of this session"); + DebugScreen.disableGrouped(); + return skipped; + } + final Mode mode = DebugScreen.isOverlayVisible() && ran ? Mode.OVERLAY : Mode.ALWAYS_ON; + lastDraw = mode; + ensureSized(); + if (mode == Mode.ALWAYS_ON && !entries.anyTextAlwaysOn()) { + clearPlacement(); + return canceled; + } + try { + prepare(mode, Minecraft.getMinecraft(), event, font, width, height, entries); + draw(); + } catch (RuntimeException | LinkageError e) { + AngelicaMod.LOGGER.error("Grouped debug screen failed; using the classic layout for the rest of this session", e); + DebugScreen.disableGrouped(); + return skipped; + } + return mode == Mode.OVERLAY; + } + + private void clearPlacement() { + if (placedCount == 0) return; + placedCount = 0; + Arrays.fill(placed, false); + } + + private void markAllDirty() { + chromeDirty = true; + Arrays.fill(groupDirty, true); + } + + private void prepare(Mode mode, Minecraft mc, RenderGameOverlayEvent.Text event, FontRenderer font, int width, int height, DebugScreenEntryList entries) { + final int gameScale = event.resolution.getScaleFactor(); + final boolean even = DebugGuiScale.evenForUnicode(mc); + final int scale = DebugGuiScale.resolve(AngelicaConfig.debugGuiScale, gameScale, mc.displayWidth, mc.displayHeight, even); + final boolean scaled = scale > 0 && scale < gameScale; + final int screenWidth = scaled ? width * gameScale / scale : width; + final int screenHeight = scaled ? height * gameScale / scale : height; + final boolean sizeChanged = screenWidth != this.screenWidth || screenHeight != lastHeight || scale != this.scale || gameScale != this.gameScale; + this.scaled = scaled; + this.scale = scale; + this.gameScale = gameScale; + this.screenWidth = screenWidth; + if (sizeChanged) { + lastHeight = screenHeight; + releaseSizeHolds(); + markAllDirty(); + } + final boolean fontChanged = selectText(font); + if (fontChanged) markAllDirty(); + final boolean titlesChanged = refreshTitles(fontChanged); + final boolean refreshed = refreshContent(mc, mode, event, entries); + if (refreshed || fontChanged || titlesChanged || sizeChanged) { + if (Tracy.ENABLED) Tracy.beginZone(Z_MEASURE); + final int flags; + try { + flags = measureAll(screenWidth / 2 - COLUMN_BUDGET_INSET, fontChanged); + } finally { + if (Tracy.ENABLED) Tracy.endZone(); + } + if ((flags & DebugGroupContents.SIZE) != 0) chromeDirty = true; + } + + fillHeights(); + if (mode == Mode.OVERLAY) { + int leftMax = screenHeight; + int rightMax = screenHeight; + if (((IGameSettingsExt) mc.gameSettings).angelica$showFpsGraph()) { + int strip = F3Graph.reservedHeight(font); + if (scaled) strip = strip * gameScale / scale; + leftMax -= strip; + if (mc.isSingleplayer()) rightMax -= strip; + } + layout.assign(DebugLayout.MARGIN_TOP, leftMax, rightMax); + } else { + final int forgeRows = event.left.size(); + int forgeBlock = forgeRows > 0 ? forgeRows * FORGE_ROW_HEIGHT + DebugLayout.MARGIN_TOP : 0; + if (scaled) forgeBlock = forgeBlock * gameScale / scale; + layout.assignSingleColumn(DebugGroup.LEFT, DebugLayout.MARGIN_TOP + forgeBlock, screenHeight); + } + comparePlacement(screenWidth); + } + + private void comparePlacement(int screenWidth) { + int count = 0; + for (int g = 0; g < groupCount; g++) { + final boolean p = layout.side[g] >= 0 && layout.height[g] > 0; + int x = 0; + int y = 0; + if (p) { + x = panelLeft(displayer.contents(g), layout.side[g], screenWidth); + y = layout.top[g]; + count++; + } + if (p != placed[g] || x != placedX[g] || y != placedY[g]) { + placed[g] = p; + placedX[g] = x; + placedY[g] = y; + chromeDirty = true; + groupDirty[g] = true; + } + } + placedCount = count; + } + + private boolean selectText(FontRenderer font) { + final boolean unicode = font.getUnicodeFlag(); + final GlyphSettings settings = GlyphSettings.current(); + frameSettings = settings; + if (text != null && font == textFont && unicode == textUnicode && settings.sameMetricsAs(textSettings)) { + return false; + } + if (text == null || font != textFont) { + text = new DebugText.Batched(font, ((FontRendererAccessor) font).angelica$getBatcher()); + textFont = font; + } + textUnicode = unicode; + textSettings = settings; + return true; + } + + private boolean refreshTitles(boolean fontChanged) { + final long translationUpdate = StatCollector.getLastTranslationUpdateTimeInMilliseconds(); + if (!fontChanged && translationUpdate == titlesTranslatedAt) return false; + titlesTranslatedAt = translationUpdate; + for (int g = 0; g < groupCount; g++) { + final DebugGroup group = registry.groupAt(g); + final DebugGroupContents contents = displayer.contents(g); + if (!group.hasTitle()) { + contents.setTitle("", 0); + continue; + } + final String title = StatCollector.translateToLocal(group.titleKey); + contents.setTitle(title, text.width(title, 0, title.length())); + } + return true; + } + + private boolean refreshContent(Minecraft mc, Mode mode, RenderGameOverlayEvent.Text event, DebugScreenEntryList entries) { + final int tick = DebugScreen.clientTick(); + final long version = entries.version(); + if (tick == lastTick && version == lastVersion && mode == lastMode) return false; + if (mode != lastMode) { + releaseSizeHolds(); + } + lastTick = tick; + lastVersion = version; + lastMode = mode; + if (Tracy.ENABLED) Tracy.beginZone(Z_ENTRIES); + try { + displayer.reset(); + DebugScreen.runEntries(displayer, mc, version, null, mode == Mode.ALWAYS_ON); + if (mode == Mode.OVERLAY) filter.sample(event.left, event.right, externalSink); + } finally { + if (Tracy.ENABLED) Tracy.endZone(); + } + return true; + } + + private void releaseSizeHolds() { + for (int g = 0; g < groupCount; g++) displayer.contents(g).releaseSizeHold(); + } + + private void appendExternal(int group, String source, int start, String name) { + final DebugGroupContents contents = displayer.contents(group); + final DebugGroupContents.Slot slot = name != null ? contents.addFact(name) : contents.addLine(); + if (source != null) slot.text.append(source, start, source.length()); + } + + private int measureAll(int lineBudget, boolean force) { + final int tick = DebugScreen.clientTick(); + int all = 0; + for (int g = 0; g < groupCount; g++) { + final int flags = displayer.contents(g).measure(text, lineBudget, force, tick); + if ((flags & DebugGroupContents.TEXT) != 0) groupDirty[g] = true; + all |= flags; + } + return all; + } + + private void fillHeights() { + for (int g = 0; g < groupCount; g++) { + final DebugGroupContents contents = displayer.contents(g); + layout.height[g] = contents.isEmpty() ? 0 : contents.fullHeight(); + } + } + + private static int panelLeft(DebugGroupContents contents, int side, int screenWidth) { + return side == DebugGroup.LEFT + ? DebugGroupContents.MARGIN + : screenWidth - DebugGroupContents.MARGIN - contents.fullWidth(); + } + + private void draw() { + if (placedCount == 0) return; + if (Tracy.ENABLED) Tracy.beginZone(Z_DRAW); + if (scaled) { + GLStateManager.glPushMatrix(); + final float factor = scale / (float) gameScale; + GLStateManager.glScalef(factor, factor, 1.0f); + } + try { + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + text.batcher.beginBatch(); + try { + drawChrome(); + drawGroups(); + } finally { + text.batcher.endBatch(); + } + } finally { + if (scaled) GLStateManager.glPopMatrix(); + if (Tracy.ENABLED) Tracy.endZone(); + } + } + + private void drawChrome() { + text.beginRects(); + try { + if (!chromeDirty && text.batcher.replay(chrome, frameSettings)) return; + capture(chrome, CHROME); + chromeDirty = false; + } finally { + text.endRects(); + } + } + + private void drawGroups() { + final BatchingFontRenderer batcher = text.batcher; + for (int g = 0; g < groupCount; g++) { + if (!placed[g]) continue; + final TextMesh mesh = meshes[g]; + if (!groupDirty[g] && batcher.replay(mesh, frameSettings)) continue; + capture(mesh, g); + groupDirty[g] = false; + } + } + + private void capture(TextMesh mesh, int group) { + final BatchingFontRenderer batcher = text.batcher; + if (Tracy.ENABLED) Tracy.beginZone(Z_CAPTURE); + batcher.beginCapture(mesh); + boolean ok = false; + try { + if (group == CHROME) { + emitChrome(); + } else { + displayer.contents(group).drawText(text, placedX[group], placedY[group]); + } + ok = true; + } finally { + if (ok) { + batcher.endCapture(); + } else { + batcher.abortCapture(); + } + if (Tracy.ENABLED) Tracy.endZone(); + } + } + + private void emitChrome() { + for (int g = 0; g < groupCount; g++) { + if (!placed[g]) continue; + displayer.contents(g).emitRects(text, placedX[g], placedY[g], layout.side[g], screenWidth); + } + } + + public void describe(Logger log, int shot) { + if (DebugScreen.ENTRIES == null) return; + ensureSized(); + final Mode mode = lastDraw != null ? lastDraw : DebugScreen.isGroupedActive() ? Mode.ALWAYS_ON : Mode.FLAT; + final int groups = mode != Mode.FLAT ? placedCount + : (flat.lineCount(DebugGroup.LEFT) > 0 ? 1 : 0) + (flat.lineCount(DebugGroup.RIGHT) > 0 ? 1 : 0); + final StringBuilder out = begin("overlay", shot, null).append(" mode=").append(mode.name()).append(" groups=").append(groups).append(" renderers="); + boolean first = true; + for (int i = 0, n = registry.rendererCount(); i < n; i++) { + final DebugEntryDef def = registry.rendererAt(i); + if (!def.isEnabled()) continue; + if (!first) out.append(','); + out.append(def.id); + first = false; + } + log.info(out.toString()); + if (mode == Mode.FLAT) { + describeFlat(log, shot, DebugGroup.LEFT, "flat_left"); + describeFlat(log, shot, DebugGroup.RIGHT, "flat_right"); + return; + } + for (int g = 0; g < groupCount; g++) { + if (!placed[g]) continue; + final DebugGroupContents contents = displayer.contents(g); + final String id = contents.group.id; + log.info(begin("group", shot, id) + .append(" side=").append(layout.side[g] == DebugGroup.LEFT ? 'L' : 'R') + .append(" x=").append(placedX[g]).append(" y=").append(placedY[g]) + .append(" w=").append(contents.fullWidth()).append(" h=").append(contents.fullHeight()) + .append(" facts=").append(contents.factCount()).append(" lines=").append(contents.lineCount()).toString()); + for (int i = 0, n = contents.factCount(); i < n; i++) { + final DebugGroupContents.Slot fact = contents.fact(i); + DebugText.appendStripped(begin("line", shot, id).append(" text="), fact.name); + dump.append(": "); + DebugText.appendStripped(dump, fact.text); + log.info(dump.toString()); + } + for (int i = 0, n = contents.lineCount(); i < n; i++) { + DebugText.appendStripped(begin("line", shot, id).append(" text="), contents.line(i).text); + log.info(dump.toString()); + } + } + } + + private void describeFlat(Logger log, int shot, int side, String id) { + final int n = flat.lineCount(side); + if (n == 0) return; + log.info(begin("group", shot, id).append(" side=").append(side == DebugGroup.LEFT ? 'L' : 'R').append(" x=0 y=0 w=0 h=0 facts=0 lines=").append(n).toString()); + for (int i = 0; i < n; i++) { + final String line = flat.lineAt(side, i); + DebugText.appendStripped(begin("line", shot, id).append(" text="), line == null ? "" : line); + log.info(dump.toString()); + } + } + + private StringBuilder begin(String kind, int shot, String id) { + dump.setLength(0); + dump.append("Flyby debug ").append(kind).append(" shot=").append(shot); + return id == null ? dump : dump.append(" id=").append(id); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenProfile.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenProfile.java new file mode 100644 index 000000000..b99eacace --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenProfile.java @@ -0,0 +1,24 @@ +package com.gtnewhorizons.angelica.debug.screen; + +public enum DebugScreenProfile { + + DEFAULT("default", "angelica.debug.options.profile.default"), + PERFORMANCE("performance", "angelica.debug.options.profile.performance"); + + public static final DebugScreenProfile[] VALUES = values(); + + public final String serializedName; + public final String translationKey; + + DebugScreenProfile(String serializedName, String translationKey) { + this.serializedName = serializedName; + this.translationKey = translationKey; + } + + public static DebugScreenProfile bySerializedName(String name) { + for (final DebugScreenProfile profile : VALUES) { + if (profile.serializedName.equals(name)) return profile; + } + return null; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugText.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugText.java new file mode 100644 index 000000000..e79af7c44 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/DebugText.java @@ -0,0 +1,172 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; +import com.gtnewhorizons.angelica.client.font.FormatCodes; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import net.minecraft.client.gui.FontRenderer; + +public abstract class DebugText { + + static final char FORMAT = '\u00a7'; + + static final class Format { + + int colorStart; + int colorLength; + int styles; + + void reset() { + colorStart = 0; + colorLength = 0; + styles = 0; + } + + void copyFrom(Format other) { + colorStart = other.colorStart; + colorLength = other.colorLength; + styles = other.styles; + } + } + + private final Format format = new Format(); + private final Format atSpace = new Format(); + + static int applyFormat(CharSequence text, int index, int end, Format state) { + final long fmt = FormatCodes.parse(text, index + 1, end); + final int length = 2 + FormatCodes.extraChars(fmt); + switch (FormatCodes.kind(fmt)) { + case FormatCodes.COLOR, FormatCodes.HEX -> { + state.colorStart = index; + state.colorLength = length; + } + case FormatCodes.RESET -> state.colorLength = 0; + case FormatCodes.OBFUSCATED, FormatCodes.BOLD, FormatCodes.STRIKETHROUGH, FormatCodes.UNDERLINE, FormatCodes.ITALIC -> state.styles |= 1 << FormatCodes.kind(fmt); + default -> {} + } + if (FormatCodes.endsBold(fmt)) state.styles = 0; + return length; + } + + protected abstract float advance(char c, boolean bold); + + public abstract float measure(CharSequence text, int start, int end); + + public abstract void draw(CharSequence text, int start, int end, float x, float y, int argb); + + public abstract void rect(int x1, int y1, int x2, int y2, int argb); + + static void appendStripped(StringBuilder out, CharSequence text) { + final int last = text.length() - 1; + for (int i = 0; i <= last; i++) { + final char c = text.charAt(i); + if (c == FORMAT && i < last) { + i++; + continue; + } + out.append(c); + } + } + + public final int width(CharSequence text, int start, int end) { + return (int) Math.ceil(measure(text, start, end)); + } + + final void wrap(DebugGroupContents.Slot slot, int budget) { + final CharSequence src = slot.text; + final StringBuilder out = slot.wrapped; + final int len = src.length(); + final Format state = format; + final Format spaceState = atSpace; + state.reset(); + out.setLength(0); + int rowStart = 0; + float pen = 0.0f; + boolean rowHasContent = false; + int spaceOut = -1; + int spaceSrc = -1; + float penAtSpace = 0.0f; + int i = 0; + while (i < len) { + final char c = src.charAt(i); + if (c == FORMAT && i + 1 < len) { + final int n = applyFormat(src, i, len, state); + out.append(src, i, i + n); + i += n; + continue; + } + final float adv = advance(c, (state.styles & 1 << FormatCodes.BOLD) != 0); + if (rowHasContent && pen + adv > budget) { + if (c == ' ') { + slot.addRow(rowStart, out.length(), pen); + i++; + } else if (spaceOut >= 0) { + slot.addRow(rowStart, spaceOut, penAtSpace); + out.setLength(spaceOut); + state.copyFrom(spaceState); + i = spaceSrc + 1; + } else { + slot.addRow(rowStart, out.length(), pen); + } + rowStart = out.length(); + if (state.colorLength > 0) out.append(src, state.colorStart, state.colorStart + state.colorLength); + for (int k = FormatCodes.OBFUSCATED; k <= FormatCodes.ITALIC; k++) if ((state.styles & 1 << k) != 0) out.append(FORMAT).append((char) ('k' + k - FormatCodes.OBFUSCATED)); + pen = 0.0f; + rowHasContent = false; + spaceOut = -1; + continue; + } + if (c == ' ' && rowHasContent) { + spaceOut = out.length(); + spaceSrc = i; + penAtSpace = pen; + spaceState.copyFrom(state); + } + out.append(c); + pen += adv; + rowHasContent = true; + i++; + } + if (rowHasContent || slot.rowCount == 0) slot.addRow(rowStart, out.length(), pen); + } + + public static final class Batched extends DebugText { + + private final FontRenderer font; + final BatchingFontRenderer batcher; + private boolean depthWasEnabled; + + public Batched(FontRenderer font, BatchingFontRenderer batcher) { + this.font = font; + this.batcher = batcher; + } + + @Override + protected float advance(char c, boolean bold) { + return batcher.advance(c, bold, font.getUnicodeFlag()); + } + + @Override + public float measure(CharSequence text, int start, int end) { + return batcher.measure(text, start, end, font.getUnicodeFlag()); + } + + @Override + public void draw(CharSequence text, int start, int end, float x, float y, int argb) { + batcher.drawString(x, y, argb, false, font.getUnicodeFlag(), text, start, end - start); + } + + public void beginRects() { + depthWasEnabled = GLStateManager.getDepthTest().isEnabled(); + GLStateManager.disableDepthTest(); + } + + @Override + public void rect(int x1, int y1, int x2, int y2, int argb) { + batcher.fillRect(x1, y1, x2, y2, argb); + } + + public void endRects() { + if (depthWasEnabled) GLStateManager.enableDepthTest(); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/FlatDebugDisplayer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/FlatDebugDisplayer.java new file mode 100644 index 000000000..6eb7f1bc8 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/FlatDebugDisplayer.java @@ -0,0 +1,126 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugFact; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import net.minecraft.client.Minecraft; + +import java.util.ArrayList; +import java.util.Arrays; + +public final class FlatDebugDisplayer implements DebugScreenDisplayer { + + private static final String SPACER = null; + private static final String SEPARATOR = ": "; + + private static final class Side { + + StringBuilder[] builders = new StringBuilder[8]; + String[] strings = new String[8]; + int count; + DebugGroupHandle lastGroup; + + Side() { + for (int i = 0; i < builders.length; i++) builders[i] = new StringBuilder(); + } + + StringBuilder next() { + if (count == builders.length) { + final int n = count * 2; + builders = Arrays.copyOf(builders, n); + for (int i = count; i < n; i++) builders[i] = new StringBuilder(); + strings = Arrays.copyOf(strings, n); + } + final StringBuilder builder = builders[count++]; + builder.setLength(0); + return builder; + } + + void finish() { + for (int i = 0; i < count; i++) { + final StringBuilder builder = builders[i]; + final String current = strings[i]; + if (builder.length() == 0) { + strings[i] = SPACER; + } else if (current == null || !current.contentEquals(builder)) { + strings[i] = builder.toString(); + } + } + } + + void addTo(ArrayList target) { + if (count == 0) return; + target.ensureCapacity(target.size() + count + 1); + if (!target.isEmpty()) target.add(SPACER); + for (int i = 0; i < count; i++) target.add(strings[i]); + } + } + + private final DebugRegistry registry; + private final DebugFactImpl fact = new DebugFactImpl(); + private final Side[] sides = { new Side(), new Side() }; + private int lastTick = Integer.MIN_VALUE; + private long lastVersion = -1L; + + public FlatDebugDisplayer(DebugRegistry registry) { + this.registry = registry; + } + + void beginLines() { + for (int s = 0; s < 2; s++) { + sides[s].count = 0; + sides[s].lastGroup = null; + } + } + + void endLines() { + sides[0].finish(); + sides[1].finish(); + } + + public void rebuild(Minecraft mc, int tick, long version) { + if (tick == lastTick && version == lastVersion) return; + lastTick = tick; + lastVersion = version; + beginLines(); + try { + DebugScreen.runEntries(this, mc, version, registry, false); + } finally { + endLines(); + } + } + + public void addTo(ArrayList left, ArrayList right) { + sides[DebugGroupHandle.LEFT].addTo(left); + sides[DebugGroupHandle.RIGHT].addTo(right); + } + + public int lineCount(int side) { + return sides[side].count; + } + + public String lineAt(int side, int i) { + return sides[side].strings[i]; + } + + private DebugFact open(DebugGroupHandle handle) { + final DebugGroup group = DebugGroup.of(handle); + final Side side = sides[group.side()]; + if (side.lastGroup != group) { + if (side.count > 0) side.next(); + side.lastGroup = group; + } + return fact.into(side.next()); + } + + @Override + public DebugFact line(DebugGroupHandle group) { + return open(group); + } + + @Override + public DebugFact fact(DebugGroupHandle group, String name) { + DebugGroup.requireName(name); + return open(group).text(name).text(SEPARATOR); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/ForgeDebugPlaceholders.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/ForgeDebugPlaceholders.java new file mode 100644 index 000000000..6993fd697 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/ForgeDebugPlaceholders.java @@ -0,0 +1,90 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import cpw.mods.fml.common.FMLCommonHandler; + +import java.util.ArrayList; +import java.util.List; + +public final class ForgeDebugPlaceholders { + + static final String VERSION = new String("Minecraft 1.7.10 (0 fps)"); + static final String RENDERS = new String("C: 0/0. F: 0, O: 0, E: 0"); + static final String ENTITIES = new String("E: 0/0. B: 0, I: 0"); + static final String PARTICLES = new String("P: 0. T: 0"); + static final String WORLD = new String("Overworld"); + static final String USED_MEMORY = new String("Used memory: 0% (0MB) of 0MB"); + static final String ALLOCATED_MEMORY = new String("Allocated memory: 0% (0MB)"); + static final String X = new String("x: 0.00000 (0) // c: 0 (0)"); + static final String Y = new String("y: 0.000 (feet pos, 0.000 eyes pos)"); + static final String Z = new String("z: 0.00000 (0) // c: 0 (0)"); + static final String FACING = new String("f: 0 (South) / 0.000000"); + static final String CHUNK = new String("lc: 0 b: Plains bl: 0 sl: 0 rl: 0"); + static final String SPEED = new String("ws: 0.000, fs: 0.000, g: false, fl: 0"); + static final String SHADER = new String("shader: none"); + + private static final int LEFT_LINES = 13; + private static final int RIGHT_LINES = 3; + + private static final int[] nullsAdded = new int[2]; + private static List lastBrandings; + private static List forgeBrandings; + + private ForgeDebugPlaceholders() {} + + public static boolean isPlaceholder(String line) { + if (line == VERSION || line == RENDERS || line == ENTITIES || line == PARTICLES || line == WORLD + || line == USED_MEMORY || line == ALLOCATED_MEMORY || line == X || line == Y || line == Z + || line == FACING || line == CHUNK || line == SPEED || line == SHADER) { + return true; + } + final List brandings = lastBrandings; + if (line == null || brandings == null) return false; + for (int i = 0, n = brandings.size(); i < n; i++) { + if (brandings.get(i) == line) return true; + } + return false; + } + + public static int nullsAdded(int side) { + return nullsAdded[side]; + } + + public static List brandings() { + List brandings = forgeBrandings; + if (brandings == null) { + brandings = FMLCommonHandler.instance().getBrandings(false); + forgeBrandings = brandings; + } + return brandings; + } + + public static void add(ArrayList left, ArrayList right, boolean chunkLoaded, boolean shaderActive) { + add(left, right, chunkLoaded, shaderActive, brandings()); + } + + static void add(ArrayList left, ArrayList right, boolean chunkLoaded, boolean shaderActive, List brandings) { + lastBrandings = brandings; + nullsAdded[0] = chunkLoaded ? 1 : 2; + nullsAdded[1] = 1; + final int brandingCount = brandings.size(); + left.ensureCapacity(left.size() + LEFT_LINES); + right.ensureCapacity(right.size() + RIGHT_LINES + brandingCount); + left.add(VERSION); + left.add(RENDERS); + left.add(ENTITIES); + left.add(PARTICLES); + left.add(WORLD); + left.add(null); + right.add(USED_MEMORY); + right.add(ALLOCATED_MEMORY); + left.add(X); + left.add(Y); + left.add(Z); + left.add(FACING); + left.add(chunkLoaded ? CHUNK : null); + left.add(SPEED); + if (shaderActive) left.add(SHADER); + right.add(null); + for (int i = 0; i < brandingCount; i++) right.add(brandings.get(i)); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/ForgeLineFilter.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/ForgeLineFilter.java new file mode 100644 index 000000000..58039e743 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/ForgeLineFilter.java @@ -0,0 +1,79 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.config.CompatConfig; + +import java.util.ArrayList; + +public final class ForgeLineFilter { + + public interface Sink { + + void add(int group, String source, int start, String name); + } + + private final DebugRegistry registry; + private final DebugEntryDef[] gates; + private final DebugGroup[] others; + private DebugLineRules rules; + private String[] rulesConfig; + + ForgeLineFilter(DebugRegistry registry) { + this.registry = registry; + gates = new DebugEntryDef[] { registry.entryById(DebugGroup.OTHER_LEFT.id), registry.entryById(DebugGroup.OTHER_RIGHT.id) }; + others = new DebugGroup[] { registry.groupById(DebugGroup.OTHER_LEFT.id), registry.groupById(DebugGroup.OTHER_RIGHT.id) }; + } + + public void sample(ArrayList left, ArrayList right, Sink sink) { + final String[] config = CompatConfig.debugLineRules; + if (rules == null || config != rulesConfig) { + rules = DebugLineRules.compile(registry, config); + rulesConfig = config; + } + sampleSide(DebugGroupHandle.LEFT, left, sink); + sampleSide(DebugGroupHandle.RIGHT, right, sink); + } + + private void sampleSide(int side, ArrayList list, Sink sink) { + final DebugEntryDef gate = gates[side]; + final DebugGroup other = others[side]; + if (gate == null || other == null) return; + final boolean otherEnabled = gate.isEnabled(); + final int size = list.size(); + int nullBudget = 0; + for (int i = 0; i < size; i++) { + if (ForgeDebugPlaceholders.isPlaceholder(list.get(i))) { + nullBudget = ForgeDebugPlaceholders.nullsAdded(side); + break; + } + } + boolean otherEmitted = false; + boolean separatorPending = false; + for (int i = 0; i < size; i++) { + final String s = list.get(i); + if (s == null && nullBudget > 0) { + nullBudget--; + continue; + } + if (s == null || DebugLineRules.isBlank(s)) { + separatorPending |= otherEmitted; + continue; + } + if (ForgeDebugPlaceholders.isPlaceholder(s)) continue; + final DebugLineRules.Rule rule = rules.match(side, s); + final DebugGroup group = rule == null ? other : rule.group(); + if (group == null) continue; + if (group == other) { + if (!otherEnabled) continue; + if (separatorPending) sink.add(other.index(), null, 0, null); + separatorPending = false; + otherEmitted = true; + } + if (rule == null) { + sink.add(group.index(), s, 0, null); + } else { + sink.add(group.index(), s, rule.payloadStart(s), rule.name()); + } + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/GroupedDebugDisplayer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/GroupedDebugDisplayer.java new file mode 100644 index 000000000..de4d706b3 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/GroupedDebugDisplayer.java @@ -0,0 +1,38 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugFact; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; + +public final class GroupedDebugDisplayer implements DebugScreenDisplayer { + + private final DebugGroupContents[] groups; + private final DebugFactImpl fact = new DebugFactImpl(); + + public GroupedDebugDisplayer(DebugRegistry registry) { + groups = new DebugGroupContents[registry.groupCount()]; + for (int i = 0; i < groups.length; i++) { + groups[i] = new DebugGroupContents(registry.groupAt(i)); + } + } + + public DebugGroupContents contents(int index) { + return groups[index]; + } + + public void reset() { + for (int i = 0; i < groups.length; i++) { + groups[i].reset(); + } + } + + @Override + public DebugFact line(DebugGroupHandle group) { + return fact.into(groups[DebugGroup.of(group).index].addLine().text); + } + + @Override + public DebugFact fact(DebugGroupHandle group, String name) { + return fact.into(groups[DebugGroup.of(group).index].addFact(DebugGroup.requireName(name)).text); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaEntries.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaEntries.java new file mode 100644 index 000000000..7ae708e0b --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaEntries.java @@ -0,0 +1,68 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugFact; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.FogState; +import com.gtnewhorizons.angelica.proxy.ClientProxy; +import org.joml.Vector3d; +import org.lwjgl.opengl.GL11; + +public final class AngelicaEntries { + + private AngelicaEntries() {} + + public static void animations(DebugScreenDisplayer displayer, DebugContext context) { + if (!AngelicaConfig.speedupAnimations) return; + displayer.fact(DebugGroup.ANGELICA, "Animations").value(ClientProxy.animationsMode.toString()); + } + + public static void dynamicLights(DebugScreenDisplayer displayer, DebugContext context) { + if (!DynamicLights.configEnabled || !DynamicLights.isEnabled()) return; + final DynamicLights lights = DynamicLights.get(); + final DebugFact fact = displayer.fact(DebugGroup.ANGELICA, "Dynamic Lights"); + fact.value(lights.getLightSourcesCount()).text(" sources, ").value(lights.getLastUpdateCount()).text(" updated"); + if (DynamicLights.FrustumCullingEnabled) { + fact.text(", ").value(lights.getChunkRebuildManager().getPendingCount()).text(" pending"); + } + } + + public static void fog(DebugScreenDisplayer displayer, DebugContext context) { + writeFog(displayer.fact(DebugGroup.ANGELICA, "Fog")); + } + + public static void writeFog(DebugFact fact) { + final FogState fog = GLStateManager.getFogState(); + final Vector3d color = fog.getFogColor(); + fact + .value(GLStateManager.getFogMode().isEnabled() ? "ON" : "OFF") + .text(' ') + .value(fogModeName(fog.getFogMode())) + .text(' ') + .value(fog.getStart(), 0) + .text('-') + .value(fog.getEnd(), 0) + .text(" d=") + .value(fog.getDensity(), 2) + .text(" (") + .value(color.x, 2) + .text(", ") + .value(color.y, 2) + .text(", ") + .value(color.z, 2) + .text(')'); + } + + private static String fogModeName(int mode) { + return switch (mode) { + case GL11.GL_LINEAR -> "LINEAR"; + case GL11.GL_EXP -> "EXP"; + case GL11.GL_EXP2 -> "EXP2"; + default -> "?"; + }; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaRendererEntry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaRendererEntry.java new file mode 100644 index 000000000..3a849be12 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaRendererEntry.java @@ -0,0 +1,89 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.Tags; +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugFactLines; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; +import com.gtnewhorizons.angelica.glsm.profiling.Tracy; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; +import com.gtnewhorizons.angelica.profiling.BailClassCounts; +import com.gtnewhorizons.angelica.profiling.RenderClassTimings; +import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer; +import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher; +import com.gtnewhorizons.angelica.rendering.tesr.TesrBatchRenderer; + +import static com.gtnewhorizons.angelica.glsm.backend.BackendManager.RENDER_BACKEND; + +public final class AngelicaRendererEntry implements DebugScreenEntry { + + private static final String BUFFERS_PREFIX = "G: "; + private static final String BUFFERS_SPLIT = ", I: "; + + private final DebugFactLines facts = new DebugFactLines(); + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + final CeleritasWorldRenderer renderer = CeleritasWorldRenderer.getInstanceOrNull(); + if (renderer == null) return; + if (context.isSampleDue()) rebuild(renderer); + facts.emit(displayer, DebugGroup.ANGELICA); + } + + private void rebuild(CeleritasWorldRenderer renderer) { + facts.begin(); + facts.fact("Version", Tags.VERSION); + facts.fact("Renderer", "Celeritas"); + facts.fact("Backend", RENDER_BACKEND.getName()); + for (final String line : renderer.getDebugStrings()) { + addRendererLine(facts, line); + } + if (DebugScreen.isRendererVerbose()) rebuildVerbose(); + facts.end(); + } + + private void rebuildVerbose() { + final ShaderManager sm = ShaderManager.getInstance(); + if (sm != null) { + addIfPresent(sm.getDebugInfo()); + } + addIfPresent(TessellatorStreamingDrawer.getDebugInfo()); + addIfPresent(TesrBatchRenderer.INSTANCE.getDebugSummaryLine()); + addIfPresent(RENDER_BACKEND.getTransferDebugInfo()); + for (final String line : TesrBatchRenderer.INSTANCE.getDebugDetailStrings()) { + addRendererLine(facts, line); + } + addIfPresent(ModelPartBatcher.INSTANCE.getDebugString()); + if (Tracy.ENABLED) { + addIfPresent(RenderClassTimings.TESR.label(), RenderClassTimings.TESR.debugValue()); + addIfPresent(RenderClassTimings.ENTITY.label(), RenderClassTimings.ENTITY.debugValue()); + addIfPresent(RenderClassTimings.SHADOW_ENTITY.label(), RenderClassTimings.SHADOW_ENTITY.debugValue()); + addIfPresent(BailClassCounts.MATERIAL.label(), BailClassCounts.MATERIAL.debugValue()); + addIfPresent(BailClassCounts.TEMPLATE.label(), BailClassCounts.TEMPLATE.debugValue()); + addIfPresent(BailClassCounts.PARTICLE_SPILL.label(), BailClassCounts.PARTICLE_SPILL.debugValue()); + } + } + + private void addIfPresent(String line) { + if (line != null && !line.isEmpty()) addRendererLine(facts, line); + } + + private void addIfPresent(String label, String value) { + if (value != null && !value.isEmpty()) facts.fact(label, value); + } + + static void addRendererLine(DebugFactLines out, String line) { + if (line.startsWith(BUFFERS_PREFIX)) { + final int at = line.indexOf(BUFFERS_SPLIT); + if (at > 0) { + out.fact("Geometry", line.substring(BUFFERS_PREFIX.length(), at) + " MiB"); + out.fact("Index", line.substring(at + BUFFERS_SPLIT.length())); + return; + } + } + out.split(line); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaShadersEntry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaShadersEntry.java new file mode 100644 index 000000000..ca15637b0 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaShadersEntry.java @@ -0,0 +1,63 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugFactLines; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import net.coderbot.iris.Iris; +import net.coderbot.iris.pipeline.WorldRenderingPipeline; +import net.coderbot.iris.shaderpack.ShaderPack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public final class AngelicaShadersEntry implements DebugScreenEntry { + + private static final String TAG = "[" + Iris.MODNAME; + private static final String SHADERPACK = "Shaderpack"; + + private final DebugFactLines facts = new DebugFactLines(); + private final List scratch = new ArrayList<>(); + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (context.isSampleDue()) rebuild(); + facts.emit(displayer, DebugGroup.SHADERS); + } + + private void rebuild() { + facts.begin(); + if (Iris.getIrisConfig().areShadersEnabled()) { + final String name = Iris.getCurrentPackName(); + if (name != null) facts.fact(SHADERPACK, Iris.isFallback() ? name + " (fallback)" : name); + final Optional pack = Iris.getCurrentPack(); + if (pack.isPresent()) facts.split(pack.get().getProfileInfo()); + } else { + facts.fact(SHADERPACK, "(off)"); + } + final Optional pipeline = Iris.getPipelineManager().getPipeline(); + if (pipeline.isPresent()) { + scratch.clear(); + pipeline.get().addDebugText(scratch); + for (int i = 0, n = scratch.size(); i < n; i++) { + final String line = stripTag(scratch.get(i)); + if (!line.isEmpty()) facts.split(line); + } + } + facts.end(); + } + + static String stripTag(String line) { + if (line == null) return ""; + String rest = line; + if (line.startsWith(TAG)) { + final int close = line.indexOf(']'); + if (close > 0) rest = line.substring(close + 1); + } + int start = 0; + while (start < rest.length() && rest.charAt(start) == ' ') start++; + return start == 0 ? rest : rest.substring(start); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/ForgeBrandingsEntry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/ForgeBrandingsEntry.java new file mode 100644 index 000000000..a38390f86 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/ForgeBrandingsEntry.java @@ -0,0 +1,61 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import com.gtnewhorizons.angelica.debug.screen.ForgeDebugPlaceholders; +import cpw.mods.fml.common.Loader; + +import java.util.List; + +public final class ForgeBrandingsEntry implements DebugScreenEntry { + + private static final String[] PREFIXES = { "MCP v", "FML v", "Minecraft Forge " }; + private static final String[] NAMES = { "MCP", "FML", "Forge" }; + + private String[] names = new String[0]; + private String[] values = new String[0]; + private int modsLoaded; + private int modsActive; + private boolean resolved; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (!resolved) resolve(); + for (int i = 0; i < names.length; i++) { + if (names[i] != null) displayer.fact(DebugGroup.FORGE, names[i]).value(values[i]); + } + displayer.fact(DebugGroup.FORGE, "Mods").value(modsLoaded).text(" loaded, ").value(modsActive).text(" active"); + for (int i = 0; i < names.length; i++) { + if (names[i] == null) displayer.line(DebugGroup.FORGE).text(values[i]); + } + } + + private void resolve() { + resolved = true; + final List brandings = ForgeDebugPlaceholders.brandings(); + final int count = Math.max(0, brandings.size() - 1); + names = new String[count]; + values = new String[count]; + for (int i = 0; i < count; i++) { + final String line = brandings.get(i); + final int kind = prefixIndex(line); + if (kind >= 0) { + names[i] = NAMES[kind]; + values[i] = line.substring(PREFIXES[kind].length()); + } else { + values[i] = line; + } + } + modsLoaded = Loader.instance().getModList().size(); + modsActive = Loader.instance().getActiveModList().size(); + } + + static int prefixIndex(String line) { + for (int i = 0; i < PREFIXES.length; i++) { + if (line.startsWith(PREFIXES[i])) return i; + } + return -1; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/HeaderEntries.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/HeaderEntries.java new file mode 100644 index 000000000..37181672f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/HeaderEntries.java @@ -0,0 +1,55 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import net.minecraft.client.Minecraft; +import net.minecraft.util.StatCollector; +import net.minecraftforge.common.MinecraftForge; + +public final class HeaderEntries { + + private static final String GAME_VERSION = "Minecraft " + MinecraftForge.MC_VERSION; + + private HeaderEntries() {} + + public static void gameVersion(DebugScreenDisplayer displayer, DebugContext context) { + displayer.line(DebugGroup.PRIORITY_LEFT).text(GAME_VERSION); + } + + public static void fps(DebugScreenDisplayer displayer, DebugContext context) { + displayer.line(DebugGroup.PRIORITY_LEFT).text(Minecraft.getMinecraft().debug); + } + + public static final class Help implements DebugScreenEntry { + + private static final String[] KEYS = { + "angelica.debug.help.options", + "angelica.debug.help.hitboxes", + "angelica.debug.help.chunk_borders", + "angelica.debug.help.verbose", + "angelica.debug.help.graphs" + }; + + private String[] translated; + private long translatedAt; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + String[] lines = translated; + final long translationUpdate = StatCollector.getLastTranslationUpdateTimeInMilliseconds(); + if (lines == null || translationUpdate != translatedAt) { + translatedAt = translationUpdate; + lines = new String[KEYS.length]; + for (int i = 0; i < KEYS.length; i++) { + lines[i] = StatCollector.translateToLocal(KEYS[i]); + } + translated = lines; + } + for (int i = 0; i < lines.length; i++) { + displayer.line(DebugGroup.HELP).text(lines[i]); + } + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/LookingAtBlockEntry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/LookingAtBlockEntry.java new file mode 100644 index 000000000..90a269d1f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/LookingAtBlockEntry.java @@ -0,0 +1,27 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; +import net.minecraft.util.MovingObjectPosition; + +public final class LookingAtBlockEntry implements DebugScreenEntry { + + private final Minecraft mc = Minecraft.getMinecraft(); + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + final MovingObjectPosition hit = mc.objectMouseOver; + if (hit == null || hit.typeOfHit != MovingObjectPosition.MovingObjectType.BLOCK) return; + final int x = hit.blockX; + final int y = hit.blockY; + final int z = hit.blockZ; + final Block block = context.world().getBlock(x, y, z); + displayer.fact(DebugGroup.LOOKING_AT_BLOCK, "Coordinates").value(x).text(", ").value(y).text(", ").value(z); + displayer.fact(DebugGroup.LOOKING_AT_BLOCK, "Type").value(Block.blockRegistry.getNameForObject(block)); + displayer.fact(DebugGroup.LOOKING_AT_BLOCK, "Meta").value(context.world().getBlockMetadata(x, y, z)); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/MemoryEntry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/MemoryEntry.java new file mode 100644 index 000000000..82a72f0e9 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/MemoryEntry.java @@ -0,0 +1,91 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; + +import java.lang.management.BufferPoolMXBean; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.List; + +public final class MemoryEntry implements DebugScreenEntry { + + private static final String MIB = "MiB"; + + static final class AllocationRate { + + private static final long MIN_INTERVAL_MS = 500L; + private static final long MAX_INTERVAL_MS = 5000L; + + private long lastTime; + private long lastHeapUsage = -1L; + private long lastGcCounts = -1L; + private long lastRate; + + long rate() { + return lastRate; + } + + void sample(long timeMs, long heapUsage, long gcCounts) { + final long dt = timeMs - lastTime; + if (lastTime != 0L && dt < MIN_INTERVAL_MS) return; + if (lastTime != 0L && dt <= MAX_INTERVAL_MS && gcCounts == lastGcCounts) { + lastRate = Math.round((heapUsage - lastHeapUsage) * (1000.0 / dt)); + } + lastTime = timeMs; + lastHeapUsage = heapUsage; + lastGcCounts = gcCounts; + } + } + + private final AllocationRate allocationRate = new AllocationRate(); + private List collectors; + private BufferPoolMXBean directPool; + private boolean poolResolved; + private long directBytes = -1L; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + final Runtime runtime = Runtime.getRuntime(); + final long max = runtime.maxMemory(); + final long total = runtime.totalMemory(); + final long used = total - runtime.freeMemory(); + if (context.isSampleDue()) { + allocationRate.sample(context.nanoTime() / 1_000_000L, used, gcCounts()); + if (!poolResolved) { + poolResolved = true; + directPool = findDirectPool(); + } + if (directPool != null) directBytes = directPool.getMemoryUsed(); + } + displayer.fact(DebugGroup.MEMORY, "Used").value(used * 100L / max).text("% ").padded(toMib(used), 3).text('/').padded(toMib(max), 3).text(MIB); + displayer.fact(DebugGroup.MEMORY, "Allocated").value(total * 100L / max).text("% ").padded(toMib(total), 3).text(MIB); + displayer.fact(DebugGroup.MEMORY, "Alloc rate").padded(toMib(allocationRate.rate()), 3).text(MIB).text("/s"); + if (directBytes >= 0L) { + displayer.fact(DebugGroup.MEMORY, "Direct").padded(toMib(directBytes), 3).text(MIB); + } + } + + private long gcCounts() { + if (collectors == null) collectors = ManagementFactory.getGarbageCollectorMXBeans(); + long sum = 0L; + for (int i = 0, n = collectors.size(); i < n; i++) { + sum += collectors.get(i).getCollectionCount(); + } + return sum; + } + + private static BufferPoolMXBean findDirectPool() { + final List pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); + for (int i = 0, n = pools.size(); i < n; i++) { + if (pools.get(i).getName().equals("direct")) return pools.get(i); + } + return null; + } + + private static long toMib(long bytes) { + return bytes / 1024L / 1024L; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/PositionEntries.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/PositionEntries.java new file mode 100644 index 000000000..4c92d8a8c --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/PositionEntries.java @@ -0,0 +1,117 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugFact; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.compat.mojang.CompatMathHelper; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import com.gtnewhorizons.angelica.helpers.LoadControllerHelper; +import cpw.mods.fml.common.ModContainer; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.util.Direction; +import net.minecraft.util.MathHelper; +import net.minecraft.world.EnumSkyBlock; +import net.minecraft.world.WorldProvider; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraft.world.chunk.Chunk; + +import java.util.Locale; + +public final class PositionEntries { + + private PositionEntries() {} + + public static final class PlayerPosition implements DebugScreenEntry { + + private static final String SEPARATOR = " / "; + private static final String[] DIRECTION_NAMES = new String[4]; + private static final String[] TOWARDS = { "Towards positive Z", "Towards negative X", "Towards negative Z", "Towards positive X" }; + + static { + for (int i = 0; i < DIRECTION_NAMES.length; i++) { + DIRECTION_NAMES[i] = Direction.directions[i].toLowerCase(Locale.ROOT); + } + } + + private WorldProvider cachedProvider; + private String dimensionName = ""; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + final EntityClientPlayerMP player = context.player(); + final double feetY = player.boundingBox.minY; + final int bx = context.blockX(); + final int by = context.blockY(); + final int bz = context.blockZ(); + displayer.fact(DebugGroup.POSITION, "XYZ").value(player.posX, 3).text(SEPARATOR).value(feetY, 5).text(SEPARATOR).value(player.posZ, 3); + displayer.fact(DebugGroup.POSITION, "Block").value(bx).text(' ').value(by).text(' ').value(bz); + displayer.fact(DebugGroup.POSITION, "Chunk").value(bx >> 4).text(' ').value(by >> 4).text(' ').value(bz >> 4); + final int heading = MathHelper.floor_double((double) (player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3; + final DebugFact facing = displayer.fact(DebugGroup.POSITION, "Facing"); + facing.value(DIRECTION_NAMES[heading]).text(" (").text(TOWARDS[heading]).text(") ("); + facing.value(MathHelper.wrapAngleTo180_float(player.rotationYaw), 1).text(SEPARATOR).value(MathHelper.wrapAngleTo180_float(player.rotationPitch), 1).text(')'); + final WorldProvider provider = context.world().provider; + if (provider != cachedProvider) { + cachedProvider = provider; + dimensionName = provider.getDimensionName(); + } + displayer.fact(DebugGroup.POSITION, "Dimension").value(dimensionName).text(" (").value(provider.dimensionId).text(')'); + } + } + + public static final class Biome implements DebugScreenEntry { + + private BiomeGenBase biome; + private String label; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + final Chunk chunk = context.chunk(); + if (chunk == null) return; + final BiomeGenBase current = context.world().getBiomeGenForCoords(context.blockX(), context.blockZ()); + if (current != biome) { + final ModContainer mod = LoadControllerHelper.getOwningMod(current.getClass()); + label = mod == null ? current.biomeName : current.biomeName + " [" + mod.getName() + "]"; + biome = current; + } + displayer.fact(DebugGroup.POSITION, "Biome").value(label); + } + } + + public static void sectionPosition(DebugScreenDisplayer displayer, DebugContext context) { + displayer.fact(DebugGroup.POSITION, "Section-Relative").padded(context.blockX() & 15, 2).text(' ').padded(context.blockY() & 15, 2).text(' ').padded(context.blockZ() & 15, 2); + } + + public static void speed(DebugScreenDisplayer displayer, DebugContext context) { + final EntityClientPlayerMP player = context.player(); + final double dx = player.posX - player.prevPosX; + final double dy = player.posY - player.prevPosY; + final double dz = player.posZ - player.prevPosZ; + displayer.fact(DebugGroup.POSITION, "Speed").value(Math.sqrt(dx * dx + dy * dy + dz * dz), 3).text(" blocks/tick"); + displayer.fact(DebugGroup.POSITION, "Walk/Fly").value(player.capabilities.getWalkSpeed(), 3).text(" / ").value(player.capabilities.getFlySpeed(), 3); + displayer.fact(DebugGroup.POSITION, "On Ground").value(player.onGround ? "true" : "false"); + } + + public static void lightLevels(DebugScreenDisplayer displayer, DebugContext context) { + final Chunk chunk = context.chunk(); + if (chunk == null) return; + final int x = context.blockX() & 15; + final int z = context.blockZ() & 15; + final int cy = CompatMathHelper.clampHeight(context.blockY()); + displayer.fact(DebugGroup.POSITION, "Light") + .value(chunk.getBlockLightValue(x, cy, z, 0)) + .text(" (") + .value(chunk.getSavedLightValue(EnumSkyBlock.Sky, x, cy, z)) + .text(" sky, ") + .value(chunk.getSavedLightValue(EnumSkyBlock.Block, x, cy, z)) + .text(" block)"); + } + + public static void heightmap(DebugScreenDisplayer displayer, DebugContext context) { + final Chunk chunk = context.chunk(); + if (chunk == null) return; + displayer.fact(DebugGroup.POSITION, "Top Section").value(chunk.getTopFilledSegment() + 15); + displayer.fact(DebugGroup.POSITION, "Height").value(context.world().getHeightValue(context.blockX(), context.blockZ())); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/RenderingEntries.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/RenderingEntries.java new file mode 100644 index 000000000..d64c3ff35 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/RenderingEntries.java @@ -0,0 +1,107 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import com.gtnewhorizons.angelica.proxy.ClientProxy; +import com.gtnewhorizons.angelica.rendering.celeritas.AngelicaRenderSectionManager; +import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.renderer.RenderGlobal; + +import static com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries.KIND_CHUNK_RENDER_STATS; +import static com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries.KIND_ENTITY_RENDER_STATS; +import static com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries.KIND_PARTICLE_RENDER_STATS; + +public final class RenderingEntries { + + private static final String SPRITES_PREFIX = "A: "; + private static final String COLON = ": "; + + private RenderingEntries() {} + + public static void tps(DebugScreenDisplayer displayer, DebugContext context) { + if (Minecraft.getMinecraft().getIntegratedServer() == null) return; + displayer.fact(DebugGroup.RENDERING, "Server").value("Integrated").text(" @ ").value(ClientProxy.integratedTickTime(), 0).text(" ms ticks"); + } + + public static final class RenderStats implements DebugScreenEntry { + + private final Minecraft mc = Minecraft.getMinecraft(); + private final int kind; + private String sampled; + private int visibleChunks; + private int totalSections; + + public RenderStats(int kind) { + this.kind = kind; + } + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (mc.renderGlobal == null) return; + switch (kind) { + case KIND_CHUNK_RENDER_STATS -> displayChunks(displayer, context); + case KIND_ENTITY_RENDER_STATS -> displayEntities(displayer, context); + case KIND_PARTICLE_RENDER_STATS -> { + if (context.isSampleDue()) sampled = mc.effectRenderer.getStatistics(); + displayer.fact(DebugGroup.RENDERING, "Particles").value(sampled); + } + default -> { + if (context.isSampleDue()) sampled = chunkCacheValue(mc.getWorldProviderName()); + displayer.fact(DebugGroup.RENDERING, "Chunk Cache").value(sampled); + } + } + } + + private void displayChunks(DebugScreenDisplayer displayer, DebugContext context) { + final CeleritasWorldRenderer renderer = CeleritasWorldRenderer.getInstanceOrNull(); + if (renderer == null) return; + if (context.isSampleDue()) { + final AngelicaRenderSectionManager sections = renderer.getRenderSectionManager(); + visibleChunks = renderer.getVisibleChunkCount(); + totalSections = sections.getTotalSections(); + sampled = spritesValue(sections.getTickerDebugString()); + } + displayer.fact(DebugGroup.RENDERING, "Chunks").value(visibleChunks).text('/').value(totalSections); + displayer.fact(DebugGroup.RENDERING, "Render Distance").value(renderer.getEffectiveRenderDistance()); + if (sampled != null) displayer.fact(DebugGroup.RENDERING, "Animated Sprites").value(sampled); + } + + private void displayEntities(DebugScreenDisplayer displayer, DebugContext context) { + final RenderGlobal renderGlobal = mc.renderGlobal; + final int total = context.world().loadedEntityList.size(); + final int rendered = renderGlobal.countEntitiesRendered; + final int hidden = renderGlobal.countEntitiesHidden; + displayer.fact(DebugGroup.RENDERING, "Entities").value(rendered).text('/').value(total).text(", ").value(hidden).text(" hidden, ").value(Math.max(0, total - hidden - rendered)).text(" culled"); + displayer.fact(DebugGroup.RENDERING, "Loaded Entities").value(total); + } + } + + public static final class PostEffect implements DebugScreenEntry { + + private final Minecraft mc = Minecraft.getMinecraft(); + private String name; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (context.isSampleDue()) { + final EntityRenderer renderer = mc.entityRenderer; + name = renderer != null && renderer.isShaderActive() ? renderer.getShaderGroup().getShaderGroupName() : null; + } + if (name != null) displayer.fact(DebugGroup.RENDERING, "Post Effect").value(name); + } + } + + static String spritesValue(String ticker) { + if (ticker == null || !ticker.startsWith(SPRITES_PREFIX)) return null; + return ticker.substring(SPRITES_PREFIX.length()); + } + + static String chunkCacheValue(String provider) { + final int at = provider.indexOf(COLON); + return at < 0 ? provider : provider.substring(at + COLON.length()); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/SystemSpecsEntry.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/SystemSpecsEntry.java new file mode 100644 index 000000000..0c74edf99 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/entries/SystemSpecsEntry.java @@ -0,0 +1,43 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntry; +import com.gtnewhorizons.angelica.debug.screen.DebugGroup; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import net.minecraft.client.Minecraft; +import org.lwjgl.opengl.GL11; + +public final class SystemSpecsEntry implements DebugScreenEntry { + + private static final String UNKNOWN = "Unknown"; + + private final Minecraft mc = Minecraft.getMinecraft(); + private boolean resolved; + private String java; + private int cores; + private String gpu; + private String openGl; + + @Override + public void display(DebugScreenDisplayer displayer, DebugContext context) { + if (!resolved) resolve(); + displayer.fact(DebugGroup.SYSTEM_SPECS, "Java").value(java); + displayer.fact(DebugGroup.SYSTEM_SPECS, "CPU").value(cores).text('x'); + displayer.fact(DebugGroup.SYSTEM_SPECS, "Display").value(mc.displayWidth).text('x').value(mc.displayHeight); + displayer.fact(DebugGroup.SYSTEM_SPECS, "GPU").value(gpu); + displayer.fact(DebugGroup.SYSTEM_SPECS, "OpenGL").value(openGl); + } + + private void resolve() { + resolved = true; + java = System.getProperty("java.version", UNKNOWN) + " (" + System.getProperty("java.vendor", UNKNOWN) + ")"; + cores = Runtime.getRuntime().availableProcessors(); + gpu = orUnknown(GLStateManager.glGetString(GL11.GL_RENDERER)); + openGl = orUnknown(GLStateManager.glGetString(GL11.GL_VERSION)); + } + + private static String orUnknown(String value) { + return value == null ? UNKNOWN : value; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/ChunkBordersRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/ChunkBordersRenderer.java new file mode 100644 index 000000000..295c1f002 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/ChunkBordersRenderer.java @@ -0,0 +1,64 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugMeshData.LineWidth; + +final class ChunkBordersRenderer implements DebugWorldRenderer { + + private static final int YELLOW = 0xFFFFFF00; + private static final int CELL_BORDER = 0xFF009B9B; + private static final int MAJOR = 0xFF3F3FFF; + private static final int NEIGHBOR = 0x7FFF0000; + private static final double MIN_Y = 0.0; + private static final double MAX_Y = 256.0; + + @Override + public Granularity viewerGranularity() { + return Granularity.SECTION; + } + + @Override + public int minIntervalMillis() { + return 0; + } + + @Override + public int fallbackIntervalMillis() { + return -1; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + final double x0 = DebugRenderSlots.anchor(scan.viewerX()); + final double z0 = DebugRenderSlots.anchor(scan.viewerZ()); + final double sectionY = DebugRenderSlots.anchor(scan.viewerY()); + final double x1 = x0 + 16.0; + final double z1 = z0 + 16.0; + + for (int x = -16; x <= 32; x += 16) { + for (int z = -16; z <= 32; z += 16) { + mesh.line(LineWidth.THICK, x0 + x, MIN_Y, z0 + z, x0 + x, MAX_Y, z0 + z, NEIGHBOR); + } + } + for (int i = 2; i < 16; i += 2) { + final int color = i % 4 == 0 ? CELL_BORDER : YELLOW; + mesh.line(LineWidth.THIN, x0 + i, MIN_Y, z0, x0 + i, MAX_Y, z0, color); + mesh.line(LineWidth.THIN, x0 + i, MIN_Y, z1, x0 + i, MAX_Y, z1, color); + mesh.line(LineWidth.THIN, x0, MIN_Y, z0 + i, x0, MAX_Y, z0 + i, color); + mesh.line(LineWidth.THIN, x1, MIN_Y, z0 + i, x1, MAX_Y, z0 + i, color); + } + for (int y = 0; y <= 256; y += 2) { + mesh.ring(LineWidth.THIN, x0, y, z0, x1, z1, y % 8 == 0 ? CELL_BORDER : YELLOW); + } + for (int x = 0; x <= 16; x += 16) { + for (int z = 0; z <= 16; z += 16) { + mesh.line(LineWidth.THICK, x0 + x, MIN_Y, z0 + z, x0 + x, MAX_Y, z0 + z, MAJOR); + } + } + mesh.alwaysOnTop(true); + mesh.lineBox(LineWidth.THIN, x0, sectionY, z0, x1, sectionY + 16.0, z1, MAJOR); + mesh.alwaysOnTop(false); + for (int y = 0; y <= 256; y += 16) { + mesh.ring(LineWidth.THICK, x0, y, z0, x1, z1, MAJOR); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/CollisionBoxRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/CollisionBoxRenderer.java new file mode 100644 index 000000000..b9ba27221 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/CollisionBoxRenderer.java @@ -0,0 +1,40 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugMeshData.LineWidth; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MathHelper; + +import java.util.List; + +final class CollisionBoxRenderer implements DebugWorldRenderer { + + private static final int MAX_BOXES = 2048; + private static final double RANGE = 6.0; + private static final int COLOR = 0xFFFFFFFF; + + @Override + public int minIntervalMillis() { + return 100; + } + + @Override + public int fallbackIntervalMillis() { + return 100; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + final EntityLivingBase viewer = scan.viewer(); + final AxisAlignedBB query = viewer.boundingBox.expand(RANGE, RANGE, RANGE); + scan.watch( + MathHelper.floor_double(query.minX) - 1, MathHelper.floor_double(query.minY) - 1, MathHelper.floor_double(query.minZ) - 1, + MathHelper.floor_double(query.maxX) + 1, MathHelper.floor_double(query.maxY) + 1, MathHelper.floor_double(query.maxZ) + 1); + final List boxes = scan.world().getCollidingBoundingBoxes(viewer, query); + final int n = Math.min(boxes.size(), MAX_BOXES); + for (int i = 0; i < n; i++) { + final AxisAlignedBB box = boxes.get(i); + mesh.lineBox(LineWidth.MEDIUM, box.minX, box.minY, box.minZ, box.maxX, box.maxY, box.maxZ, COLOR); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/CubeWalker.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/CubeWalker.java new file mode 100644 index 000000000..0a1752da1 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/CubeWalker.java @@ -0,0 +1,37 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +final class CubeWalker { + + interface Sink { + + boolean cell(int x, int y, int z); + } + + private static final int MIN_Y = 0; + private static final int MAX_Y = 255; + + private CubeWalker() {} + + static void walk(int cx, int cy, int cz, int radius, Sink sink) { + for (int r = 0; r <= radius; r++) { + final int low = cy - r; + final int high = cy + r; + final boolean lowIn = low >= MIN_Y && low <= MAX_Y; + final boolean highIn = high >= MIN_Y && high <= MAX_Y; + final int from = Math.max(low, MIN_Y); + final int to = Math.min(high, MAX_Y); + for (int dx = -r; dx <= r; dx++) { + for (int dz = -r; dz <= r; dz++) { + if (dx == -r || dx == r || dz == -r || dz == r) { + for (int y = from; y <= to; y++) { + if (!sink.cell(cx + dx, y, cz + dz)) return; + } + } else { + if (lowIn && !sink.cell(cx + dx, low, cz + dz)) return; + if (highIn && !sink.cell(cx + dx, high, cz + dz)) return; + } + } + } + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabelBatch.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabelBatch.java new file mode 100644 index 000000000..dd66690b8 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabelBatch.java @@ -0,0 +1,306 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; +import com.gtnewhorizons.angelica.client.font.FontProvider; +import com.gtnewhorizons.angelica.client.font.GlyphSettings; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.DebugProgram; +import com.gtnewhorizons.angelica.debug.render.DebugPrograms; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.Color4; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; +import com.gtnewhorizons.angelica.mixins.interfaces.FontRendererAccessor; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.OpenGlHelper; +import org.joml.Matrix4f; +import org.joml.Matrix4fc; +import org.lwjgl.opengl.GL11; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutFloat; + +final class DebugLabelBatch { + + static final int MAX_LABELS = 8192; + static final int MAX_FALLBACK_LABELS = 256; + static final int MAX_VALUE = 99; + static final float SCALE = 0.02F; + static final float NEAR = 0.05F; + static final float MIN_PIXELS = 4.0F; + static final float FRUSTUM_MARGIN = 0.1F; + + private static final int DIGITS = 10; + private static final int FLAGS = VertexFlags.TEXTURE_BIT | VertexFlags.COLOR_BIT; + private static final VertexFormat FORMAT = DefaultVertexFormat.ALL_FORMATS[FLAGS]; + private static final int STRIDE = FORMAT.getVertexSize(); + private static final int TEXTURE_OFFSET = DebugGeometry.offsetOf(FORMAT, VertexFormatElement.Usage.PRIMARY_UV); + private static final int COLOR_OFFSET = DebugGeometry.offsetOf(FORMAT, VertexFormatElement.Usage.COLOR); + private static final int QUAD_BYTES = 4 * STRIDE; + private static final int INITIAL_BYTES = 0x4000; + private static final String[] NUMBERS = numbers(); + + private final float[] glyphU = new float[DIGITS]; + private final float[] glyphV = new float[DIGITS]; + private final float[] glyphUSize = new float[DIGITS]; + private final float[] glyphVSize = new float[DIGITS]; + private final float[] glyphWidth = new float[DIGITS]; + private final float[] glyphTop = new float[DIGITS]; + private final float[] glyphHeight = new float[DIGITS]; + private final float[] glyphAdvance = new float[DIGITS]; + private final int[] glyphBucket = new int[DIGITS]; + private final int[] bucketTexture = new int[DIGITS]; + private final NativeScratch[] bucketScratch = new NativeScratch[DIGITS]; + private final int[] bucketBytes = new int[DIGITS]; + private final Matrix4f labelMatrix = new Matrix4f(); + private final Matrix4f projection = new Matrix4f(); + private final Matrix4f fallbackModelView = new Matrix4f(); + private final float[] fallbackColor = new float[4]; + private GlyphSettings metrics; + private boolean metricsValid; + private boolean metricsUnicode; + private float metricsLineHeight; + private int bucketCount; + private int visible; + private int limit; + private float projectionX; + private float projectionY; + private int viewportHeight; + private float lineHeight; + private FontRenderer font; + private BatchingFontRenderer batcher; + + DebugLabelBatch() { + for (int i = 0; i < DIGITS; i++) bucketScratch[i] = new NativeScratch(INITIAL_BYTES); + } + + private static String[] numbers() { + final String[] out = new String[MAX_VALUE + 1]; + for (int i = 0; i <= MAX_VALUE; i++) { + out[i] = Integer.toString(i); + } + return out; + } + + static int digitCount(int value) { + return value >= 10 ? 2 : 1; + } + + static int digitAt(int value, int index) { + if (value < 10) return value; + return index == 0 ? value / 10 : value % 10; + } + + static float stringWidth(int value, float[] advance) { + return value >= 10 ? advance[value / 10] + advance[value % 10] : advance[value]; + } + + static float digitLeft(int value, int index, float[] advance) { + final float left = -stringWidth(value, advance) * 0.5F; + return index == 0 ? left : left + advance[value / 10]; + } + + static boolean isVisible(float eyeX, float eyeY, float eyeZ, float halfWidth, float height, float projectionX, float projectionY, int viewportHeight) { + final float depth = -eyeZ; + if (depth < NEAR) return false; + if (height * projectionY * viewportHeight * 0.5F < MIN_PIXELS * depth) return false; + final float reach = depth * (1.0F + FRUSTUM_MARGIN); + final float limitX = reach / projectionX + halfWidth; + if (eyeX > limitX || eyeX < -limitX) return false; + final float limitY = reach / projectionY + height; + return eyeY <= limitY && eyeY >= -limitY; + } + + void begin(FontRenderer fontRenderer, Matrix4fc projection, int viewportHeight) { + this.font = fontRenderer; + this.projection.set(projection); + this.projectionX = projection.m00(); + this.projectionY = projection.m11(); + this.viewportHeight = viewportHeight; + this.lineHeight = fontRenderer.FONT_HEIGHT - 1.0F; + this.visible = 0; + this.batcher = AngelicaConfig.enableFontRenderer && fontRenderer instanceof FontRendererAccessor accessor ? accessor.angelica$getBatcher() : null; + if (batcher == null) { + limit = MAX_FALLBACK_LABELS; + metricsValid = false; + bucketCount = 0; + for (int d = 0; d < DIGITS; d++) { + glyphAdvance[d] = fontRenderer.getCharWidth((char) ('0' + d)); + } + pinFallback(); + return; + } + limit = MAX_LABELS; + final boolean unicode = fontRenderer.getUnicodeFlag(); + final GlyphSettings settings = GlyphSettings.current(); + if (metricsValid && unicode == metricsUnicode && lineHeight == metricsLineHeight && settings.sameMetricsAs(metrics)) return; + metrics = settings; + metricsValid = false; + bucketCount = 0; + final float scaleX = batcher.getGlyphScaleX(); + final float scaleY = batcher.getGlyphScaleY(); + for (int d = 0; d < DIGITS; d++) { + final char chr = (char) ('0' + d); + final FontProvider provider = batcher.glyphProvider(chr, unicode); + glyphU[d] = provider.getUStart(chr); + glyphV[d] = provider.getVStart(chr); + glyphUSize[d] = provider.getUSize(chr); + glyphVSize[d] = provider.getVSize(chr); + glyphWidth[d] = provider.getGlyphW(chr) * scaleX - 1.0F; + glyphTop[d] = BatchingFontRenderer.glyphTop(provider, 0.0F, lineHeight, scaleY); + glyphHeight[d] = BatchingFontRenderer.glyphHeight(provider, lineHeight, scaleY); + glyphAdvance[d] = batcher.glyphAdvance(provider, chr, false, scaleX); + glyphBucket[d] = bucketFor(provider.getTexture(chr)); + } + metricsUnicode = unicode; + metricsLineHeight = lineHeight; + metricsValid = true; + } + + private int bucketFor(int texture) { + for (int i = 0; i < bucketCount; i++) { + if (bucketTexture[i] == texture) return i; + } + final int bucket = bucketCount++; + bucketTexture[bucket] = texture; + bucketBytes[bucket] = 0; + return bucket; + } + + void add(DebugLabels labels, Matrix4fc modelView) { + if (visible >= limit) return; + final float[] positions = labels.pos; + final int[] values = labels.value; + final int[] colors = labels.color; + final int count = labels.count; + final float m00 = modelView.m00(); + final float m01 = modelView.m01(); + final float m02 = modelView.m02(); + final float m10 = modelView.m10(); + final float m11 = modelView.m11(); + final float m12 = modelView.m12(); + final float m20 = modelView.m20(); + final float m21 = modelView.m21(); + final float m22 = modelView.m22(); + final float m30 = modelView.m30(); + final float m31 = modelView.m31(); + final float m32 = modelView.m32(); + final float[] advance = glyphAdvance; + final float height = lineHeight * SCALE; + for (int i = 0, p = 0; i < count; i++, p += 3) { + final float x = positions[p]; + final float y = positions[p + 1]; + final float z = positions[p + 2]; + final float eyeZ = m02 * x + m12 * y + m22 * z + m32; + if (-eyeZ < NEAR) continue; + final float eyeX = m00 * x + m10 * y + m20 * z + m30; + final float eyeY = m01 * x + m11 * y + m21 * z + m31; + final int value = values[i]; + final float halfWidth = stringWidth(value, advance) * SCALE * 0.5F; + if (!isVisible(eyeX, eyeY, eyeZ, halfWidth, height, projectionX, projectionY, viewportHeight)) continue; + if (batcher == null) { + drawFallback(value, colors[i], eyeX, eyeY, eyeZ); + } else { + final int digits = digitCount(value); + for (int k = 0; k < digits; k++) { + glyph(digitAt(value, k), eyeX + digitLeft(value, k, advance) * SCALE, eyeY, eyeZ, colors[i]); + } + } + if (++visible >= limit) return; + } + } + + private void glyph(int digit, float left, float anchorY, float z, int argb) { + final int bucket = glyphBucket[digit]; + final int start = bucketBytes[bucket]; + final int end = start + QUAD_BYTES; + final NativeScratch scratch = bucketScratch[bucket]; + scratch.ensure(end); + bucketBytes[bucket] = end; + final long p = scratch.address() + start; + final float right = left + glyphWidth[digit] * SCALE; + final float top = anchorY - glyphTop[digit] * SCALE; + final float bottom = top - glyphHeight[digit] * SCALE; + final float u0 = glyphU[digit]; + final float v0 = glyphV[digit]; + final float u1 = u0 + glyphUSize[digit]; + final float v1 = v0 + glyphVSize[digit]; + vertex(p, left, top, z, u0, v0, argb); + vertex(p + STRIDE, left, bottom, z, u0, v1, argb); + vertex(p + 2L * STRIDE, right, bottom, z, u1, v1, argb); + vertex(p + 3L * STRIDE, right, top, z, u1, v0, argb); + } + + private static void vertex(long p, float x, float y, float z, float u, float v, int argb) { + DebugGeometry.putPosition(p, x, y, z); + memPutFloat(p + TEXTURE_OFFSET, u); + memPutFloat(p + TEXTURE_OFFSET + 4, v); + DebugGeometry.putColor(p + COLOR_OFFSET, argb); + } + + private void drawFallback(int value, int argb, float eyeX, float eyeY, float eyeZ) { + final String text = NUMBERS[value]; + labelMatrix.translation(eyeX, eyeY, eyeZ).scale(SCALE, -SCALE, SCALE); + GLStateManager.setModelViewMatrix(labelMatrix); + font.drawString(text, -Math.round(stringWidth(value, glyphAdvance) * 0.5F), 0, argb); + } + + private void pinFallback() { + fallbackModelView.set(GLStateManager.getModelViewMatrix()); + final Color4 color = GLStateManager.getColor(); + fallbackColor[0] = color.getRed(); + fallbackColor[1] = color.getGreen(); + fallbackColor[2] = color.getBlue(); + fallbackColor[3] = color.getAlpha(); + GLStateManager.glUseProgram(0); + GLStateManager.disableLighting(); + GLStateManager.disableFog(); + GLStateManager.disableAlphaTest(); + GLStateManager.glActiveTexture(OpenGlHelper.lightmapTexUnit); + GLStateManager.disableTexture(); + GLStateManager.glActiveTexture(OpenGlHelper.defaultTexUnit); + GLStateManager.enableTexture(); + GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + } + + void end() { + try { + if (batcher != null) { + boolean bound = false; + for (int i = 0; i < bucketCount; i++) { + final int bytes = bucketBytes[i]; + if (bytes == 0) continue; + if (!bound) { + final DebugProgram textured = DebugPrograms.textured(); + textured.use(); + textured.mvp(projection); + bound = true; + } + batcher.bindGlyphTexture(bucketTexture[i]); + TessellatorStreamingDrawer.drawPacked(bucketScratch[i].window(bytes), GL11.GL_QUADS, FLAGS, bytes / STRIDE); + } + } + } finally { + if (batcher == null && font != null) { + GLStateManager.setModelViewMatrix(fallbackModelView); + GLStateManager.glColor4f(fallbackColor[0], fallbackColor[1], fallbackColor[2], fallbackColor[3]); + } + for (int i = 0; i < bucketCount; i++) bucketBytes[i] = 0; + batcher = null; + font = null; + } + } + + void free() { + for (int i = 0; i < DIGITS; i++) { + bucketScratch[i].free(); + bucketBytes[i] = 0; + } + bucketCount = 0; + metricsValid = false; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabels.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabels.java new file mode 100644 index 000000000..313e3cb09 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabels.java @@ -0,0 +1,51 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import java.util.Arrays; + +final class DebugLabels { + + private static final int INITIAL = 64; + private static final float[] NO_FLOATS = new float[0]; + private static final int[] NO_INTS = new int[0]; + + float[] pos = NO_FLOATS; + int[] value = NO_INTS; + int[] color = NO_INTS; + int count; + + void add(float x, float y, float z, int v, int argb) { + final int n = count; + if (n == value.length) { + final int grown = Math.max(INITIAL, n * 2); + pos = Arrays.copyOf(pos, grown * 3); + value = Arrays.copyOf(value, grown); + color = Arrays.copyOf(color, grown); + } + pos[n * 3] = x; + pos[n * 3 + 1] = y; + pos[n * 3 + 2] = z; + value[n] = v; + color[n] = argb; + count = n + 1; + } + + void copyFrom(DebugLabels from) { + final int n = from.count; + if (n > value.length) { + pos = new float[from.pos.length]; + value = new int[from.value.length]; + color = new int[from.color.length]; + } + System.arraycopy(from.pos, 0, pos, 0, n * 3); + System.arraycopy(from.value, 0, value, 0, n); + System.arraycopy(from.color, 0, color, 0, n); + count = n; + } + + void free() { + pos = NO_FLOATS; + value = NO_INTS; + color = NO_INTS; + count = 0; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMesh.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMesh.java new file mode 100644 index 000000000..e952a5c28 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMesh.java @@ -0,0 +1,185 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.DebugProgram; +import com.gtnewhorizons.angelica.debug.render.DebugPrograms; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.ViewportState; +import com.gtnewhorizons.angelica.glsm.streaming.LineQuads; +import org.joml.Matrix4f; +import org.joml.Matrix4fc; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; + +final class DebugMesh { + + private static final VertexFormat WIDE = LineQuads.quadFormat(DebugMeshData.PLAIN); + private static final int WIDE_STRIDE = WIDE.getVertexSize(); + private static final DebugMeshData.LineWidth[] WIDTHS = DebugMeshData.LineWidth.values(); + + private final int[] lineFirst = new int[DebugMeshData.BUCKET_COUNT]; + private final int[] lineCount = new int[DebugMeshData.BUCKET_COUNT]; + private final boolean[] lineWide = new boolean[DebugMeshData.BUCKET_COUNT]; + private final Matrix4f mvp = new Matrix4f(); + private int plainVao; + private int plainVbo; + private int wideVao; + private int wideVbo; + private int fillCount; + private int anchorX; + private int anchorY; + private int anchorZ; + final DebugLabels labels = new DebugLabels(); + + void upload(DebugMeshData data, NativeScratch scratch) { + anchorX = data.anchorX(); + anchorY = data.anchorY(); + anchorZ = data.anchorZ(); + labels.copyFrom(data.labels); + + int plainBytes = data.fillBytes(); + int wideVertices = 0; + fillCount = data.fillVertexCount(); + for (int b = 0; b < DebugMeshData.BUCKET_COUNT; b++) { + final int vertices = data.lineVertexCount(b); + final boolean wide = WIDTHS[b % DebugMeshData.WIDTH_COUNT].pixels > 1.0F; + lineWide[b] = wide; + if (vertices == 0) { + lineFirst[b] = 0; + lineCount[b] = 0; + } else if (wide) { + final int corners = LineQuads.segmentCount(GL11.GL_LINES, vertices) * LineQuads.VERTICES_PER_SEGMENT; + lineFirst[b] = wideVertices; + lineCount[b] = corners; + wideVertices += corners; + } else { + lineFirst[b] = plainBytes / DebugMeshData.STRIDE; + lineCount[b] = vertices; + plainBytes += data.lineBytes(b); + } + } + + if (plainBytes > 0) { + if (plainVao == 0) { + plainVbo = GLStateManager.glGenBuffers(); + plainVao = DebugGeometry.createVao(plainVbo, DebugMeshData.PLAIN); + } else { + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, plainVbo); + } + scratch.ensure(plainBytes); + long destination = scratch.address(); + final int fillBytes = data.fillBytes(); + if (fillBytes > 0) { + data.copySection(DebugMeshData.FILL_SECTION, destination); + destination += fillBytes; + } + for (int b = 0; b < DebugMeshData.BUCKET_COUNT; b++) { + if (lineWide[b] || lineCount[b] == 0) continue; + data.copySection(DebugMeshData.lineSection(b), destination); + destination += data.lineBytes(b); + } + GLStateManager.glBufferData(GL15.GL_ARRAY_BUFFER, scratch.window(plainBytes), GL15.GL_DYNAMIC_DRAW); + } + + if (wideVertices > 0) { + final int wideBytes = wideVertices * WIDE_STRIDE; + scratch.ensure(wideBytes); + for (int b = 0; b < DebugMeshData.BUCKET_COUNT; b++) { + if (!lineWide[b] || lineCount[b] == 0) continue; + LineQuads.expandWithOtherEnd(data.lineAddress(b), DebugMeshData.PLAIN, GL11.GL_LINES, data.lineVertexCount(b), scratch.address() + (long) lineFirst[b] * WIDE_STRIDE); + } + if (wideVao == 0) { + wideVbo = GLStateManager.glGenBuffers(); + wideVao = DebugGeometry.createVao(wideVbo, WIDE); + } else { + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, wideVbo); + } + GLStateManager.glBufferData(GL15.GL_ARRAY_BUFFER, scratch.window(wideBytes), GL15.GL_DYNAMIC_DRAW); + } + } + + static DebugProgram useColor() { + final DebugProgram color = DebugPrograms.color(); + color.use(); + return color; + } + + static DebugProgram useLines() { + final DebugProgram lines = DebugPrograms.lines(); + lines.use(); + final ViewportState viewport = GLStateManager.getViewportState(); + lines.viewport(viewport.width, viewport.height); + return lines; + } + + void prepare(Matrix4fc projBase, double camX, double camY, double camZ) { + mvp.set(projBase).translate((float) (anchorX - camX), (float) (anchorY - camY), (float) (anchorZ - camZ)); + } + + boolean has(boolean wide, boolean onTop) { + if (!wide && !onTop && fillCount > 0) return true; + final int from = onTop ? DebugMeshData.WIDTH_COUNT : 0; + final int to = from + DebugMeshData.WIDTH_COUNT; + for (int b = from; b < to; b++) { + if (lineWide[b] == wide && lineCount[b] > 0) return true; + } + return false; + } + + void drawPlain(boolean onTop, DebugProgram color) { + if (!has(false, onTop)) return; + color.mvp(mvp); + GLStateManager.glBindVertexArray(plainVao); + if (!onTop && fillCount > 0) { + GLStateManager.enableCull(); + try { + GLStateManager.glDrawArrays(GL11.GL_QUADS, 0, fillCount); + } finally { + GLStateManager.disableCull(); + } + } + final int from = onTop ? DebugMeshData.WIDTH_COUNT : 0; + final int to = from + DebugMeshData.WIDTH_COUNT; + for (int b = from; b < to; b++) { + if (lineWide[b] || lineCount[b] == 0) continue; + GLStateManager.glDrawArrays(GL11.GL_LINES, lineFirst[b], lineCount[b]); + } + } + + void drawWide(boolean onTop, DebugProgram lines) { + if (!has(true, onTop)) return; + lines.mvp(mvp); + GLStateManager.glBindVertexArray(wideVao); + final int from = onTop ? DebugMeshData.WIDTH_COUNT : 0; + final int to = from + DebugMeshData.WIDTH_COUNT; + for (int b = from; b < to; b++) { + if (!lineWide[b] || lineCount[b] == 0) continue; + lines.lineWidth(WIDTHS[b % DebugMeshData.WIDTH_COUNT].pixels); + GLStateManager.glDrawArrays(GL11.GL_TRIANGLES, lineFirst[b], lineCount[b]); + } + } + + void modelView(Matrix4f base, double camX, double camY, double camZ, Matrix4f out) { + out.set(base).translate((float) (anchorX - camX), (float) (anchorY - camY), (float) (anchorZ - camZ)); + } + + void delete() { + if (plainVbo != 0) GLStateManager.glDeleteBuffers(plainVbo); + if (plainVao != 0) GLStateManager.glDeleteVertexArrays(plainVao); + if (wideVbo != 0) GLStateManager.glDeleteBuffers(wideVbo); + if (wideVao != 0) GLStateManager.glDeleteVertexArrays(wideVao); + plainVbo = 0; + plainVao = 0; + wideVbo = 0; + wideVao = 0; + fillCount = 0; + for (int b = 0; b < DebugMeshData.BUCKET_COUNT; b++) { + lineFirst[b] = 0; + lineCount[b] = 0; + lineWide[b] = false; + } + labels.free(); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshData.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshData.java new file mode 100644 index 000000000..296c5bc2b --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshData.java @@ -0,0 +1,226 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; + +import java.util.Arrays; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memCopy; + +final class DebugMeshData { + + enum LineWidth { + + THIN(1), + MEDIUM(2), + THICK(4); + + final float pixels; + + LineWidth(float pixels) { + this.pixels = pixels; + } + } + + static final VertexFormat PLAIN = DefaultVertexFormat.ALL_FORMATS[VertexFlags.COLOR_BIT]; + static final int STRIDE = PLAIN.getVertexSize(); + static final int WIDTH_COUNT = LineWidth.values().length; + static final int BUCKET_COUNT = 2 * WIDTH_COUNT; + static final int FILL_SECTION = 0; + + private static final int INITIAL_BYTES = 4096; + private static final int SECTION_COUNT = 1 + BUCKET_COUNT; + private static final long FNV_OFFSET = 0xCBF29CE484222325L; + private static final long FNV_PRIME = 0x100000001B3L; + private static final int TAG_QUAD = 0x51554144; + private static final int TAG_LINE = 0x4C494E45; + private static final int TAG_LABEL = 0x4C41424C; + + private final NativeScratch[] sections = new NativeScratch[SECTION_COUNT]; + private final int[] used = new int[SECTION_COUNT]; + final DebugLabels labels = new DebugLabels(); + private int anchorX; + private int anchorY; + private int anchorZ; + private boolean onTop; + private long hash = FNV_OFFSET; + + DebugMeshData() { + for (int i = 0; i < SECTION_COUNT; i++) sections[i] = new NativeScratch(INITIAL_BYTES); + } + + static int bucket(LineWidth width, boolean onTop) { + return width.ordinal() + (onTop ? WIDTH_COUNT : 0); + } + + static int lineSection(int bucket) { + return 1 + bucket; + } + + void begin(int anchorX, int anchorY, int anchorZ) { + this.anchorX = anchorX; + this.anchorY = anchorY; + this.anchorZ = anchorZ; + Arrays.fill(used, 0); + labels.count = 0; + onTop = false; + hash = FNV_OFFSET; + mix(anchorX); + mix(anchorY); + mix(anchorZ); + } + + void free() { + for (int i = 0; i < SECTION_COUNT; i++) { + sections[i].free(); + used[i] = 0; + } + labels.free(); + } + + void line(LineWidth width, double x0, double y0, double z0, double x1, double y1, double z1, int argb) { + final int bucket = bucket(width, onTop); + final long p = reserve(lineSection(bucket), 2 * STRIDE); + mix(TAG_LINE); + mix(bucket); + vertex(p, x0, y0, z0, argb); + vertex(p + STRIDE, x1, y1, z1, argb); + } + + void quad(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3, int argb) { + final long p = reserve(FILL_SECTION, 4 * STRIDE); + mix(TAG_QUAD); + vertex(p, x0, y0, z0, argb); + vertex(p + STRIDE, x1, y1, z1, argb); + vertex(p + 2 * STRIDE, x2, y2, z2, argb); + vertex(p + 3 * STRIDE, x3, y3, z3, argb); + } + + void label(double x, double y, double z, int value, int argb) { + final float fx = (float) (x - anchorX); + final float fy = (float) (y - anchorY); + final float fz = (float) (z - anchorZ); + final int clamped = value < 0 ? 0 : Math.min(value, DebugLabelBatch.MAX_VALUE); + labels.add(fx, fy, fz, clamped, argb); + mix(TAG_LABEL); + mix(Float.floatToRawIntBits(fx)); + mix(Float.floatToRawIntBits(fy)); + mix(Float.floatToRawIntBits(fz)); + mix(clamped); + mix(argb); + } + + void alwaysOnTop(boolean onTop) { + this.onTop = onTop; + } + + void lineBox(LineWidth width, double x0, double y0, double z0, double x1, double y1, double z1, int argb) { + line(width, x0, y0, z0, x1, y0, z0, argb); + line(width, x1, y0, z0, x1, y0, z1, argb); + line(width, x1, y0, z1, x0, y0, z1, argb); + line(width, x0, y0, z1, x0, y0, z0, argb); + line(width, x0, y1, z0, x1, y1, z0, argb); + line(width, x1, y1, z0, x1, y1, z1, argb); + line(width, x1, y1, z1, x0, y1, z1, argb); + line(width, x0, y1, z1, x0, y1, z0, argb); + line(width, x0, y0, z0, x0, y1, z0, argb); + line(width, x1, y0, z0, x1, y1, z0, argb); + line(width, x1, y0, z1, x1, y1, z1, argb); + line(width, x0, y0, z1, x0, y1, z1, argb); + } + + void ring(LineWidth width, double x0, double y, double z0, double x1, double z1, int argb) { + line(width, x0, y, z0, x0, y, z1, argb); + line(width, x0, y, z1, x1, y, z1, argb); + line(width, x1, y, z1, x1, y, z0, argb); + line(width, x1, y, z0, x0, y, z0, argb); + } + + void fillBox(double x0, double y0, double z0, double x1, double y1, double z1, int argb) { + face(0, x0, y0, z0, x1, y1, z1, 0.0, argb); + face(1, x0, y0, z0, x1, y1, z1, 0.0, argb); + face(4, x0, y0, z0, x1, y1, z1, 0.0, argb); + face(5, x0, y0, z0, x1, y1, z1, 0.0, argb); + face(2, x0, y0, z0, x1, y1, z1, 0.0, argb); + face(3, x0, y0, z0, x1, y1, z1, 0.0, argb); + } + + void face(int side, double x0, double y0, double z0, double x1, double y1, double z1, double offset, int argb) { + switch (side) { + case 0 -> quad(x1, y0 - offset, z0, x1, y0 - offset, z1, x0, y0 - offset, z1, x0, y0 - offset, z0, argb); + case 1 -> quad(x0, y1 + offset, z0, x0, y1 + offset, z1, x1, y1 + offset, z1, x1, y1 + offset, z0, argb); + case 2 -> quad(x0, y0, z0 - offset, x0, y1, z0 - offset, x1, y1, z0 - offset, x1, y0, z0 - offset, argb); + case 3 -> quad(x1, y0, z1 + offset, x1, y1, z1 + offset, x0, y1, z1 + offset, x0, y0, z1 + offset, argb); + case 4 -> quad(x0 - offset, y0, z0, x0 - offset, y0, z1, x0 - offset, y1, z1, x0 - offset, y1, z0, argb); + default -> quad(x1 + offset, y1, z0, x1 + offset, y1, z1, x1 + offset, y0, z1, x1 + offset, y0, z0, argb); + } + } + + int anchorX() { + return anchorX; + } + + int anchorY() { + return anchorY; + } + + int anchorZ() { + return anchorZ; + } + + void copySection(int section, long destination) { + memCopy(sections[section].address(), destination, used[section]); + } + + int fillBytes() { + return used[FILL_SECTION]; + } + + int fillVertexCount() { + return used[FILL_SECTION] / STRIDE; + } + + long lineAddress(int bucket) { + return sections[lineSection(bucket)].address(); + } + + int lineBytes(int bucket) { + return used[lineSection(bucket)]; + } + + int lineVertexCount(int bucket) { + return used[lineSection(bucket)] / STRIDE; + } + + long hash() { + return hash; + } + + private long reserve(int section, int bytes) { + final int start = used[section]; + final int end = start + bytes; + final NativeScratch scratch = sections[section]; + scratch.ensure(end); + used[section] = end; + return scratch.address() + start; + } + + private void vertex(long p, double x, double y, double z, int argb) { + final float fx = (float) (x - anchorX); + final float fy = (float) (y - anchorY); + final float fz = (float) (z - anchorZ); + DebugGeometry.putPosition(p, fx, fy, fz); + DebugGeometry.putColor(p + 12, argb); + mix(Float.floatToRawIntBits(fx)); + mix(Float.floatToRawIntBits(fy)); + mix(Float.floatToRawIntBits(fz)); + mix(argb); + } + + private void mix(int value) { + hash = (hash ^ (value & 0xFFFFFFFFL)) * FNV_PRIME; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderSlots.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderSlots.java new file mode 100644 index 000000000..86f572c34 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderSlots.java @@ -0,0 +1,214 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; + +final class DebugRenderSlots { + + private static final long NANOS_PER_MILLI = 1_000_000L; + + final int count; + final DebugWorldChanges changes; + final ScanState scan; + private final DebugEntryDef[] defs; + private final DebugWorldRenderer[] renderers; + private final boolean[] enabled; + private final boolean[] fresh; + private final boolean[] drawable; + private final boolean[] built; + private final boolean[] pendingDiscard; + private final boolean[] uploadPending; + private final DebugWorldRenderer.Granularity[] granularity; + private final int[] keyX; + private final int[] keyY; + private final int[] keyZ; + private final long[] minNanos; + private final long[] fallbackNanos; + private final long[] lastBuild; + private final long[] hash; + final DebugMeshData build = new DebugMeshData(); + private int enabledCount; + private boolean live; + + DebugRenderSlots(DebugRegistry registry) { + final int n = registry.rendererCount(); + this.count = n; + this.changes = new DebugWorldChanges(n); + this.scan = new ScanState(changes); + this.defs = new DebugEntryDef[n]; + this.renderers = new DebugWorldRenderer[n]; + this.enabled = new boolean[n]; + this.fresh = new boolean[n]; + this.drawable = new boolean[n]; + this.built = new boolean[n]; + this.pendingDiscard = new boolean[n]; + this.uploadPending = new boolean[n]; + this.granularity = new DebugWorldRenderer.Granularity[n]; + this.keyX = new int[n]; + this.keyY = new int[n]; + this.keyZ = new int[n]; + this.minNanos = new long[n]; + this.fallbackNanos = new long[n]; + this.lastBuild = new long[n]; + this.hash = new long[n]; + for (int i = 0; i < n; i++) { + final DebugEntryDef def = registry.rendererAt(i); + if (DebugRenderers.isWorldRenderer(def)) defs[i] = def; + } + } + + static boolean isDue(long ageNanos, long minNanos, long fallbackNanos, boolean viewerMoved, boolean dirty) { + if (ageNanos < minNanos) return false; + return viewerMoved || dirty || fallbackNanos >= 0L && ageNanos >= fallbackNanos; + } + + static int key(DebugWorldRenderer.Granularity granularity, int coordinate) { + return granularity == DebugWorldRenderer.Granularity.BLOCK ? coordinate : coordinate >> 4; + } + + static int keyY(DebugWorldRenderer.Granularity granularity, int y) { + return granularity == DebugWorldRenderer.Granularity.CHUNK ? 0 : key(granularity, y); + } + + static int anchor(int coordinate) { + return coordinate >> 4 << 4; + } + + static int anchorY(DebugWorldRenderer.Granularity granularity, int y) { + return granularity == DebugWorldRenderer.Granularity.CHUNK ? 0 : anchor(y); + } + + boolean isLive() { + return live; + } + + int enabledCount() { + return enabledCount; + } + + boolean isDrawable(int slot) { + return drawable[slot]; + } + + void setEnabled(int slot, boolean on) { + final DebugEntryDef def = defs[slot]; + if (def == null || enabled[slot] == on) return; + if (!on) { + disable(slot); + return; + } + DebugWorldRenderer renderer = renderers[slot]; + if (renderer == null) { + renderer = DebugRenderers.create(def); + renderers[slot] = renderer; + } + final int fallback = renderer.fallbackIntervalMillis(); + granularity[slot] = renderer.viewerGranularity(); + minNanos[slot] = renderer.minIntervalMillis() * NANOS_PER_MILLI; + fallbackNanos[slot] = fallback < 0 ? -1L : fallback * NANOS_PER_MILLI; + pendingDiscard[slot] = false; + enabled[slot] = true; + fresh[slot] = true; + enabledCount++; + live = true; + } + + private void disable(int slot) { + enabled[slot] = false; + enabledCount--; + retire(slot); + } + + private void retire(int slot) { + drawable[slot] = false; + built[slot] = false; + fresh[slot] = true; + changes.clearBox(slot); + uploadPending[slot] = false; + pendingDiscard[slot] = true; + } + + void fail(int slot, Throwable cause) { + DebugScreen.fail(defs[slot], cause); + if (enabled[slot]) disable(slot); + } + + void resetWorld() { + for (int i = 0; i < count; i++) { + if (renderers[i] != null && (enabled[i] || built[i])) retire(i); + } + } + + boolean isPendingDiscard(int slot) { + return pendingDiscard[slot]; + } + + void clearPendingDiscard(int slot) { + pendingDiscard[slot] = false; + } + + void releaseIfIdle() { + if (enabledCount == 0) { + build.free(); + live = false; + } + } + + boolean takeUpload(int slot) { + final boolean pending = uploadPending[slot]; + uploadPending[slot] = false; + return pending; + } + + int pickDue(long now, int viewerX, int viewerY, int viewerZ) { + int best = -1; + long bestOverdue = Long.MIN_VALUE; + for (int i = 0; i < count; i++) { + if (!enabled[i]) continue; + if (fresh[i]) return i; + final DebugWorldRenderer.Granularity g = granularity[i]; + final long age = now - lastBuild[i]; + final boolean moved = keyX[i] != key(g, viewerX) || keyY[i] != keyY(g, viewerY) || keyZ[i] != key(g, viewerZ); + if (!isDue(age, minNanos[i], fallbackNanos[i], moved, changes.isDirty(i))) continue; + final long overdue = age - minNanos[i]; + if (overdue > bestOverdue) { + best = i; + bestOverdue = overdue; + } + } + return best; + } + + int update(long now, int viewerX, int viewerY, int viewerZ) { + final int slot = pickDue(now, viewerX, viewerY, viewerZ); + if (slot >= 0) rebuild(slot, now, viewerX, viewerY, viewerZ); + return slot; + } + + private void rebuild(int slot, long now, int viewerX, int viewerY, int viewerZ) { + final DebugWorldRenderer.Granularity g = granularity[slot]; + keyX[slot] = key(g, viewerX); + keyY[slot] = keyY(g, viewerY); + keyZ[slot] = key(g, viewerZ); + lastBuild[slot] = now; + fresh[slot] = false; + changes.clearBox(slot); + scan.slot = slot; + final DebugMeshData data = build; + data.begin(anchor(viewerX), anchorY(g, viewerY), anchor(viewerZ)); + try { + renderers[slot].rebuild(scan, data); + } catch (RuntimeException | LinkageError e) { + fail(slot, e); + return; + } + final long h = data.hash(); + if (!built[slot] || hash[slot] != h) { + uploadPending[slot] = true; + hash[slot] = h; + built[slot] = true; + } + drawable[slot] = true; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderers.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderers.java new file mode 100644 index 000000000..b71d942f5 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderers.java @@ -0,0 +1,69 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; +import net.minecraft.client.Minecraft; +import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; + +public final class DebugRenderers { + + private static DebugWorldRenderHandler handler; + private static boolean listening; + + private DebugRenderers() {} + + static DebugWorldRenderer create(DebugEntryDef def) { + return switch (def.kind) { + case DebugScreenEntries.KIND_CHUNK_BORDERS -> new ChunkBordersRenderer(); + case DebugScreenEntries.KIND_CHUNK_SECTION_VISIBILITY -> new SectionVisibilityRenderer(); + case DebugScreenEntries.KIND_VISUALIZE_BLOCK_LIGHT_LEVELS -> new LightLevelRenderer(false); + case DebugScreenEntries.KIND_VISUALIZE_SKY_LIGHT_LEVELS -> new LightLevelRenderer(true); + case DebugScreenEntries.KIND_VISUALIZE_HEIGHTMAP -> new HeightmapRenderer(); + case DebugScreenEntries.KIND_VISUALIZE_COLLISION_BOXES -> new CollisionBoxRenderer(); + case DebugScreenEntries.KIND_VISUALIZE_SOLID_FACES -> new SolidFaceRenderer(); + case DebugScreenEntries.KIND_VISUALIZE_WATER_LEVELS -> new WaterLevelRenderer(); + case DebugScreenEntries.KIND_VISUALIZE_ENTITY_SUPPORTING_BLOCK -> new SupportBlockRenderer(); + default -> throw new IllegalArgumentException(def.id); + }; + } + + static boolean isWorldRenderer(DebugEntryDef def) { + return def.kind >= DebugScreenEntries.KIND_CHUNK_BORDERS && def.kind <= DebugScreenEntries.KIND_VISUALIZE_ENTITY_SUPPORTING_BLOCK; + } + + public static void onEnabledChanged() { + final DebugRegistry registry = DebugRegistry.MAIN; + final int n = registry.rendererCount(); + if (handler == null) { + if (!anyEnabled(registry, n)) return; + handler = new DebugWorldRenderHandler(registry); + MinecraftForge.EVENT_BUS.register(handler); + } + final DebugRenderSlots slots = handler.slots(); + for (int i = 0; i < n; i++) { + final DebugEntryDef def = registry.rendererAt(i); + if (isWorldRenderer(def)) slots.setEnabled(i, def.isEnabled()); + } + final boolean any = slots.enabledCount() > 0; + if (!any) handler.processReleases(); + if (any == listening) return; + listening = any; + final World world = Minecraft.getMinecraft().theWorld; + if (world == null) return; + if (any) { + world.addWorldAccess(slots.changes); + } else { + world.removeWorldAccess(slots.changes); + } + } + + private static boolean anyEnabled(DebugRegistry registry, int n) { + for (int i = 0; i < n; i++) { + final DebugEntryDef def = registry.rendererAt(i); + if (isWorldRenderer(def) && def.isEnabled()) return true; + } + return false; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldChanges.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldChanges.java new file mode 100644 index 000000000..2a59c4795 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldChanges.java @@ -0,0 +1,99 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.IWorldAccess; + +final class DebugWorldChanges implements IWorldAccess { + + final int[] box; + final boolean[] dirty; + + DebugWorldChanges(int slots) { + box = new int[slots * 6]; + dirty = new boolean[slots]; + for (int i = 0; i < slots; i++) clearBox(i); + } + + void watch(int slot, int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { + if (minX > maxX || minY > maxY || minZ > maxZ) return; + final int o = slot * 6; + if (minX < box[o]) box[o] = minX; + if (minY < box[o + 1]) box[o + 1] = minY; + if (minZ < box[o + 2]) box[o + 2] = minZ; + if (maxX > box[o + 3]) box[o + 3] = maxX; + if (maxY > box[o + 4]) box[o + 4] = maxY; + if (maxZ > box[o + 5]) box[o + 5] = maxZ; + } + + void clearBox(int slot) { + final int o = slot * 6; + box[o] = Integer.MAX_VALUE; + box[o + 1] = Integer.MAX_VALUE; + box[o + 2] = Integer.MAX_VALUE; + box[o + 3] = Integer.MIN_VALUE; + box[o + 4] = Integer.MIN_VALUE; + box[o + 5] = Integer.MIN_VALUE; + dirty[slot] = false; + } + + boolean isDirty(int slot) { + return dirty[slot]; + } + + void touch(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { + final int n = dirty.length; + for (int i = 0, o = 0; i < n; i++, o += 6) { + if (box[o] > box[o + 3]) continue; + if (maxX < box[o] || minX > box[o + 3]) continue; + if (maxY < box[o + 1] || minY > box[o + 4]) continue; + if (maxZ < box[o + 2] || minZ > box[o + 5]) continue; + dirty[i] = true; + } + } + + @Override + public void markBlockForUpdate(int x, int y, int z) { + touch(x, y, z, x, y, z); + } + + @Override + public void markBlockForRenderUpdate(int x, int y, int z) { + touch(x, y, z, x, y, z); + } + + @Override + public void markBlockRangeForRenderUpdate(int x0, int y0, int z0, int x1, int y1, int z1) { + touch(Math.min(x0, x1), Math.min(y0, y1), Math.min(z0, z1), Math.max(x0, x1), Math.max(y0, y1), Math.max(z0, z1)); + } + + @Override + public void playSound(String name, double x, double y, double z, float volume, float pitch) {} + + @Override + public void playSoundToNearExcept(EntityPlayer player, String name, double x, double y, double z, float volume, float pitch) {} + + @Override + public void spawnParticle(String name, double x, double y, double z, double motionX, double motionY, double motionZ) {} + + @Override + public void onEntityCreate(Entity entity) {} + + @Override + public void onEntityDestroy(Entity entity) {} + + @Override + public void playRecord(String name, int x, int y, int z) {} + + @Override + public void broadcastSound(int id, int x, int y, int z, int data) {} + + @Override + public void playAuxSFX(EntityPlayer player, int id, int x, int y, int z, int data) {} + + @Override + public void destroyBlockPartially(int breakerId, int x, int y, int z, int progress) {} + + @Override + public void onStaticEntitiesChanged() {} +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldRenderHandler.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldRenderHandler.java new file mode 100644 index 000000000..fb1ffd264 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldRenderHandler.java @@ -0,0 +1,246 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.render.DebugDrawScope; +import com.gtnewhorizons.angelica.debug.render.DebugProgram; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.glsm.GLContextState; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.profiling.Tracy; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.world.World; +import net.minecraftforge.client.event.RenderWorldLastEvent; +import net.minecraftforge.event.world.WorldEvent; +import org.joml.Matrix4f; +import org.lwjgl.opengl.GL15; + +public final class DebugWorldRenderHandler { + + private static final Tracy.ZoneId Z_DEBUG_REBUILD = Tracy.zoneId("debugWorldRebuild", Tracy.COLOR_CLIENT); + private static final Tracy.ZoneId Z_DEBUG_UPLOAD = Tracy.zoneId("debugWorldUpload", Tracy.COLOR_CLIENT); + private static final Tracy.ZoneId Z_DEBUG_RENDER = Tracy.zoneId("debugWorldRender", Tracy.COLOR_CLIENT); + private static final Tracy.ZoneId Z_DEBUG_LABELS = Tracy.zoneId("debugWorldLabels", Tracy.COLOR_CLIENT); + + private static final int SCRATCH_BYTES = 0x4000; + + private final DebugRenderSlots slots; + private final DebugMesh[] meshes; + private final DebugLabelBatch labels = new DebugLabelBatch(); + private final NativeScratch scratch = new NativeScratch(SCRATCH_BYTES); + private final DebugDrawScope scope = new DebugDrawScope(); + private final Matrix4f base = new Matrix4f(); + private final Matrix4f projBase = new Matrix4f(); + private final Matrix4f scratchMatrix = new Matrix4f(); + + DebugWorldRenderHandler(DebugRegistry registry) { + slots = new DebugRenderSlots(registry); + meshes = new DebugMesh[slots.count]; + } + + DebugRenderSlots slots() { + return slots; + } + + void processReleases() { + for (int i = 0; i < slots.count; i++) { + if (!slots.isPendingDiscard(i)) continue; + slots.clearPendingDiscard(i); + final DebugMesh mesh = meshes[i]; + if (mesh != null) mesh.delete(); + } + slots.releaseIfIdle(); + if (!slots.isLive()) freeScratch(); + } + + @SubscribeEvent + public void onWorldLoad(WorldEvent.Load event) { + if (!event.world.isRemote) return; + if (slots.enabledCount() > 0) event.world.addWorldAccess(slots.changes); + } + + @SubscribeEvent + public void onWorldUnload(WorldEvent.Unload event) { + if (!event.world.isRemote) return; + if (event.world != Minecraft.getMinecraft().theWorld) return; + event.world.removeWorldAccess(slots.changes); + slots.resetWorld(); + processReleases(); + } + + @SubscribeEvent + public void onRenderWorldLast(RenderWorldLastEvent event) { + if (!slots.isLive()) return; + processReleases(); + if (!slots.isLive()) return; + final Minecraft mc = Minecraft.getMinecraft(); + final EntityLivingBase viewer = mc.renderViewEntity; + final World world = mc.theWorld; + if (viewer == null || world == null) return; + + final float partialTicks = event.partialTicks; + final double camX = viewer.lastTickPosX + (viewer.posX - viewer.lastTickPosX) * partialTicks; + final double camY = viewer.lastTickPosY + (viewer.posY - viewer.lastTickPosY) * partialTicks; + final double camZ = viewer.lastTickPosZ + (viewer.posZ - viewer.lastTickPosZ) * partialTicks; + final long now = System.nanoTime(); + + final GLContextState glCtx = GLStateManager.ctx(); + final ScanState scan = slots.scan; + final int slot; + scan.begin(world, viewer); + if (Tracy.ENABLED) Tracy.beginZone(Z_DEBUG_REBUILD); + try { + slot = slots.update(now, scan.viewerX(), scan.viewerY(), scan.viewerZ()); + } finally { + if (Tracy.ENABLED) Tracy.endZone(); + scan.end(); + } + if (slot >= 0 && slots.takeUpload(slot)) { + if (Tracy.ENABLED) Tracy.beginZone(Z_DEBUG_UPLOAD); + try { + upload(glCtx, slot); + } finally { + if (Tracy.ENABLED) Tracy.endZone(); + } + } + + if (Tracy.ENABLED) Tracy.beginZone(Z_DEBUG_RENDER); + try { + render(mc, glCtx, camX, camY, camZ); + } finally { + if (Tracy.ENABLED) Tracy.endZone(); + } + } + + private void upload(GLContextState glCtx, int slot) { + final int vao = glCtx.boundVAO; + final int vbo = glCtx.boundVBO; + try { + DebugMesh mesh = meshes[slot]; + if (mesh == null) { + mesh = new DebugMesh(); + meshes[slot] = mesh; + } + mesh.upload(slots.build, scratch); + } catch (RuntimeException | LinkageError e) { + slots.fail(slot, e); + } finally { + GLStateManager.glBindVertexArray(vao); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + } + } + + private int nextDrawable(int from) { + final int n = slots.count; + for (int i = from; i < n; i++) { + if (slots.isDrawable(i) && meshes[i] != null) return i; + } + return n; + } + + private void render(Minecraft mc, GLContextState glCtx, double camX, double camY, double camZ) { + final int n = slots.count; + if (nextDrawable(0) == n) return; + + base.set(GLStateManager.getModelViewMatrix()); + projBase.set(GLStateManager.getProjectionMatrix()).mul(base); + scope.beginWorld(); + try { + for (int i = nextDrawable(0); i < n; i = nextDrawable(i + 1)) { + meshes[i].prepare(projBase, camX, camY, camZ); + } + + drawPass(false); + + boolean anyLabels = false; + for (int i = nextDrawable(0); i < n; i = nextDrawable(i + 1)) { + anyLabels |= meshes[i].labels.count > 0; + } + if (anyLabels) { + if (Tracy.ENABLED) Tracy.beginZone(Z_DEBUG_LABELS); + try { + drawLabels(mc, glCtx, camX, camY, camZ); + } finally { + if (Tracy.ENABLED) Tracy.endZone(); + } + } + + GLStateManager.disableDepthTest(); + drawPass(true); + } finally { + scope.end(); + } + } + + private void drawPass(boolean onTop) { + final int n = slots.count; + final DebugProgram color = useProgram(false, onTop); + if (color != null) { + for (int i = nextDrawable(0); i < n; i = nextDrawable(i + 1)) { + try { + meshes[i].drawPlain(onTop, color); + } catch (RuntimeException | LinkageError e) { + slots.fail(i, e); + } + } + } + final DebugProgram lines = useProgram(true, onTop); + if (lines != null) { + for (int i = nextDrawable(0); i < n; i = nextDrawable(i + 1)) { + try { + meshes[i].drawWide(onTop, lines); + } catch (RuntimeException | LinkageError e) { + slots.fail(i, e); + } + } + } + } + + private DebugProgram useProgram(boolean wide, boolean onTop) { + final int n = slots.count; + int first = nextDrawable(0); + while (first < n && !meshes[first].has(wide, onTop)) first = nextDrawable(first + 1); + if (first == n) return null; + try { + return wide ? DebugMesh.useLines() : DebugMesh.useColor(); + } catch (RuntimeException | LinkageError e) { + for (int i = first; i < n; i = nextDrawable(i + 1)) { + if (meshes[i].has(wide, onTop)) slots.fail(i, e); + } + return null; + } + } + + private void drawLabels(Minecraft mc, GLContextState glCtx, double camX, double camY, double camZ) { + final int n = slots.count; + try { + labels.begin(mc.fontRenderer, GLStateManager.getProjectionMatrix(), glCtx.viewportState.height); + for (int i = nextDrawable(0); i < n; i = nextDrawable(i + 1)) { + final DebugMesh mesh = meshes[i]; + if (mesh.labels.count == 0) continue; + try { + mesh.modelView(base, camX, camY, camZ, scratchMatrix); + labels.add(mesh.labels, scratchMatrix); + } catch (RuntimeException | LinkageError e) { + slots.fail(i, e); + } + } + labels.end(); + } catch (RuntimeException | LinkageError e) { + failLabelled(e); + } + } + + private void failLabelled(Throwable cause) { + final int n = slots.count; + for (int i = nextDrawable(0); i < n; i = nextDrawable(i + 1)) { + if (meshes[i].labels.count > 0) slots.fail(i, cause); + } + } + + private void freeScratch() { + labels.free(); + scratch.free(); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldRenderer.java new file mode 100644 index 000000000..0778f2be3 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldRenderer.java @@ -0,0 +1,25 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +interface DebugWorldRenderer { + + enum Granularity { + + BLOCK, + SECTION, + CHUNK + } + + void rebuild(ScanState scan, DebugMeshData mesh); + + default Granularity viewerGranularity() { + return Granularity.BLOCK; + } + + default int minIntervalMillis() { + return 250; + } + + default int fallbackIntervalMillis() { + return 2000; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/HeightmapRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/HeightmapRenderer.java new file mode 100644 index 000000000..3942a1356 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/HeightmapRenderer.java @@ -0,0 +1,49 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +final class HeightmapRenderer implements DebugWorldRenderer { + + private static final int CHUNK_DIST = 2; + private static final double BOX_HEIGHT = 0.09375; + private static final int COLOR = 0xFF00B200; + + @Override + public Granularity viewerGranularity() { + return Granularity.CHUNK; + } + + @Override + public int minIntervalMillis() { + return 500; + } + + @Override + public int fallbackIntervalMillis() { + return -1; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + final int chunkX = scan.viewerX() >> 4; + final int chunkZ = scan.viewerZ() >> 4; + int minHeight = Integer.MAX_VALUE; + for (int dx = -CHUNK_DIST; dx <= CHUNK_DIST; dx++) { + for (int dz = -CHUNK_DIST; dz <= CHUNK_DIST; dz++) { + final int baseX = (chunkX + dx) << 4; + final int baseZ = (chunkZ + dz) << 4; + if (!scan.isLoaded(baseX, baseZ)) continue; + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + final double wx = baseX + x; + final double wz = baseZ + z; + final int height = scan.getHeight(baseX + x, baseZ + z); + if (height < minHeight) minHeight = height; + final double y = height + BOX_HEIGHT; + mesh.fillBox(wx + 0.25, y, wz + 0.25, wx + 0.75, y + BOX_HEIGHT, wz + 0.75, COLOR); + } + } + } + } + final int watchMinY = minHeight == Integer.MAX_VALUE ? 0 : minHeight - 1; + scan.watch((chunkX - CHUNK_DIST) << 4, watchMinY, (chunkZ - CHUNK_DIST) << 4, ((chunkX + CHUNK_DIST) << 4) + 15, 255, ((chunkZ + CHUNK_DIST) << 4) + 15); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/LightLevelRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/LightLevelRenderer.java new file mode 100644 index 000000000..7960a0f36 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/LightLevelRenderer.java @@ -0,0 +1,63 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import net.minecraft.world.EnumSkyBlock; + +final class LightLevelRenderer implements DebugWorldRenderer, ScanState.CellVisitor { + + private static final int LEVELS = 16; + private static final int RADIUS = 10; + private static final int MAX_CELLS = 4096; + + private final EnumSkyBlock type; + private final int unlit; + private final int[] colors; + private final double labelY; + + LightLevelRenderer(boolean sky) { + this.type = sky ? EnumSkyBlock.Sky : EnumSkyBlock.Block; + this.unlit = sky ? 15 : 0; + this.colors = sky ? ramp(0x0000FF, 0x00FFFF) : ramp(0xAA0000, 0xFFFF00); + this.labelY = sky ? 0.25 : 0.5; + } + + private static int lerp(float t, int from, int to) { + final int r = channel(t, from >> 16, to >> 16); + final int g = channel(t, from >> 8, to >> 8); + final int b = channel(t, from, to); + return (r << 16) | (g << 8) | b; + } + + private static int channel(float t, int from, int to) { + final int a = from & 0xFF; + final int b = to & 0xFF; + return a + Math.round((b - a) * t); + } + + private static int[] ramp(int from, int to) { + final int[] out = new int[LEVELS]; + for (int i = 0; i < LEVELS; i++) { + out[i] = lerp(i / 15.0F, from, to); + } + return out; + } + + @Override + public int fallbackIntervalMillis() { + return -1; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + if (type == EnumSkyBlock.Sky && scan.world().provider.hasNoSky) return; + scan.scanCube(RADIUS, MAX_CELLS, this, mesh); + } + + @Override + public boolean visitCell(ScanState scan, DebugMeshData mesh, int x, int y, int z) { + final int level = scan.getSavedLight(type, x, y, z); + if (level == unlit) return false; + if (scan.getBlock(x, y, z).isOpaqueCube()) return false; + mesh.label(x + 0.5, y + labelY, z + 0.5, level, 0xFF000000 | colors[level]); + return true; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/ScanState.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/ScanState.java new file mode 100644 index 000000000..7d0d98152 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/ScanState.java @@ -0,0 +1,150 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import net.minecraft.block.Block; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.init.Blocks; +import net.minecraft.util.MathHelper; +import net.minecraft.world.EnumSkyBlock; +import net.minecraft.world.World; +import net.minecraft.world.chunk.Chunk; + +final class ScanState implements CubeWalker.Sink { + + interface CellVisitor { + + boolean visitCell(ScanState scan, DebugMeshData mesh, int x, int y, int z); + } + + private final DebugWorldChanges changes; + private final Chunk[] chunks = new Chunk[4]; + private final int[] chunkX = new int[4]; + private final int[] chunkZ = new int[4]; + private Chunk column; + private int columnX; + private int columnZ; + private World world; + private EntityLivingBase viewer; + private CellVisitor visitor; + private DebugMeshData mesh; + private int budget; + private int viewerX; + private int viewerY; + private int viewerZ; + int slot; + + ScanState(DebugWorldChanges changes) { + this.changes = changes; + } + + void begin(World world, EntityLivingBase viewer) { + this.world = world; + this.viewer = viewer; + this.viewerX = MathHelper.floor_double(viewer.posX); + this.viewerY = MathHelper.floor_double(viewer.posY); + this.viewerZ = MathHelper.floor_double(viewer.posZ); + } + + void end() { + world = null; + viewer = null; + visitor = null; + mesh = null; + column = null; + chunks[0] = null; + chunks[1] = null; + chunks[2] = null; + chunks[3] = null; + } + + private Chunk chunk(int x, int z) { + if (column != null && x == columnX && z == columnZ) return column; + final int cx = x >> 4; + final int cz = z >> 4; + final int i = (cx & 1) << 1 | (cz & 1); + Chunk chunk = chunks[i]; + if (chunk == null || chunkX[i] != cx || chunkZ[i] != cz) { + chunk = world.getChunkFromChunkCoords(cx, cz); + chunks[i] = chunk; + chunkX[i] = cx; + chunkZ[i] = cz; + } + return chunk; + } + + World world() { + return world; + } + + EntityLivingBase viewer() { + return viewer; + } + + int viewerX() { + return viewerX; + } + + int viewerY() { + return viewerY; + } + + int viewerZ() { + return viewerZ; + } + + boolean isLoaded(int x, int z) { + return !chunk(x, z).isEmpty(); + } + + Block getBlock(int x, int y, int z) { + if (y < 0 || y > 255) return Blocks.air; + final Chunk chunk = chunk(x, z); + if (chunk.isEmpty()) return Blocks.air; + return chunk.getBlock(x & 15, y, z & 15); + } + + int getBlockMetadata(int x, int y, int z) { + if (y < 0 || y > 255) return 0; + final Chunk chunk = chunk(x, z); + if (chunk.isEmpty()) return 0; + return chunk.getBlockMetadata(x & 15, y, z & 15); + } + + int getSavedLight(EnumSkyBlock type, int x, int y, int z) { + if (y < 0 || y > 255) return 0; + final Chunk chunk = chunk(x, z); + if (chunk.isEmpty()) return 0; + return chunk.getSavedLightValue(type, x & 15, y, z & 15); + } + + int getHeight(int x, int z) { + final Chunk chunk = chunk(x, z); + if (chunk.isEmpty()) return 0; + return chunk.getHeightValue(x & 15, z & 15); + } + + void scanCube(int radius, int budget, CellVisitor visitor, DebugMeshData mesh) { + final int cx = viewerX; + final int cy = viewerY; + final int cz = viewerZ; + watch(cx - radius - 1, cy - radius - 1, cz - radius - 1, cx + radius + 1, cy + radius + 1, cz + radius + 1); + this.visitor = visitor; + this.mesh = mesh; + this.budget = budget; + CubeWalker.walk(cx, cy, cz, radius, this); + } + + @Override + public boolean cell(int x, int y, int z) { + final Chunk chunk = chunk(x, z); + if (chunk.isEmpty()) return true; + column = chunk; + columnX = x; + columnZ = z; + if (!visitor.visitCell(this, mesh, x, y, z)) return true; + return --budget > 0; + } + + void watch(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { + changes.watch(slot, minX, Math.max(0, minY), minZ, maxX, Math.min(255, maxY), maxZ); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SectionVisibilityRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SectionVisibilityRenderer.java new file mode 100644 index 000000000..50ca28d87 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SectionVisibilityRenderer.java @@ -0,0 +1,80 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugMeshData.LineWidth; +import com.gtnewhorizons.angelica.rendering.celeritas.AngelicaRenderSectionManager; +import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer; +import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex; +import org.embeddedt.embeddium.impl.render.chunk.RenderSection; +import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderList; +import org.embeddedt.embeddium.impl.render.chunk.lists.SortedRenderLists; +import org.embeddedt.embeddium.impl.render.chunk.occlusion.GraphDirection; +import org.embeddedt.embeddium.impl.render.chunk.occlusion.GraphDirectionSet; +import org.embeddedt.embeddium.impl.render.chunk.occlusion.VisibilityEncoding; +import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; + +final class SectionVisibilityRenderer implements DebugWorldRenderer { + + private static final int RANGE = 3; + private static final int MAX_LINES = 8192; + private static final int LINE = 0xFFFF0000; + private static final int FILL = 0x33E5E500; + private static final double INSET = 0.5; + + @Override + public int fallbackIntervalMillis() { + return 250; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + final CeleritasWorldRenderer renderer = CeleritasWorldRenderer.getInstanceOrNull(); + if (renderer == null) return; + final AngelicaRenderSectionManager manager = renderer.getRenderSectionManager(); + if (manager == null) return; + final SortedRenderLists lists = manager.getRenderLists(); + final int camX = scan.viewerX() >> 4; + final int camY = scan.viewerY() >> 4; + final int camZ = scan.viewerZ() >> 4; + final int regions = lists.getNumRegions(); + int lines = 0; + for (int r = 0; r < regions; r++) { + final ChunkRenderList list = lists.getRegion(r); + final RenderRegion region = list.getRegion(); + final byte[] ids = list.getSectionsWithGeometry(); + final int count = list.getSectionsWithGeometryCount(); + for (int k = 0; k < count; k++) { + final int id = ids[k] & 0xFF; + final int sx = region.getChunkX() + LocalSectionIndex.unpackX(id); + final int sy = region.getChunkY() + LocalSectionIndex.unpackY(id); + final int sz = region.getChunkZ() + LocalSectionIndex.unpackZ(id); + if (Math.abs(sx - camX) > RANGE || Math.abs(sy - camY) > RANGE || Math.abs(sz - camZ) > RANGE) continue; + final RenderSection section = region.getSection(id); + if (section == null) continue; + final long data = section.getVisibilityData(); + final double x0 = sx << 4; + final double y0 = sy << 4; + final double z0 = sz << 4; + final double ox = x0 + 8.0; + final double oy = y0 + 8.0; + final double oz = z0 + 8.0; + boolean disconnected = false; + for (int from = 0; from < GraphDirection.COUNT; from++) { + final int reachable = VisibilityEncoding.getConnections(data, GraphDirectionSet.of(from)); + for (int to = from + 1; to < GraphDirection.COUNT; to++) { + if (GraphDirectionSet.contains(reachable, to)) continue; + if (lines >= MAX_LINES) return; + lines++; + disconnected = true; + mesh.line(LineWidth.THIN, + ox + GraphDirection.x(from) * 8.0, oy + GraphDirection.y(from) * 8.0, oz + GraphDirection.z(from) * 8.0, + ox + GraphDirection.x(to) * 8.0, oy + GraphDirection.y(to) * 8.0, oz + GraphDirection.z(to) * 8.0, + LINE); + } + } + if (disconnected) { + mesh.fillBox(x0 + INSET, y0 + INSET, z0 + INSET, x0 + 16.0 - INSET, y0 + 16.0 - INSET, z0 + 16.0 - INSET, FILL); + } + } + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SolidFaceRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SolidFaceRenderer.java new file mode 100644 index 000000000..8b3777622 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SolidFaceRenderer.java @@ -0,0 +1,40 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import net.minecraft.block.Block; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +final class SolidFaceRenderer implements DebugWorldRenderer, ScanState.CellVisitor { + + private static final int RADIUS = 6; + private static final int MAX_CELLS = 8192; + private static final double OFFSET = 0.002; + private static final int COLOR = 0x80FF0000; + + @Override + public int fallbackIntervalMillis() { + return -1; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + scan.scanCube(RADIUS, MAX_CELLS, this, mesh); + } + + @Override + public boolean visitCell(ScanState scan, DebugMeshData mesh, int x, int y, int z) { + final World world = scan.world(); + final Block block = scan.getBlock(x, y, z); + if (block.isAir(world, x, y, z)) return false; + final ForgeDirection[] dirs = ForgeDirection.VALID_DIRECTIONS; + boolean emitted = false; + for (int d = 0; d < 6; d++) { + final ForgeDirection dir = dirs[d]; + if (!block.isSideSolid(world, x, y, z, dir)) continue; + if (scan.getBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ).isOpaqueCube()) continue; + mesh.face(d, x, y, z, x + 1.0, y + 1.0, z + 1.0, OFFSET, COLOR); + emitted = true; + } + return emitted; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SupportBlockRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SupportBlockRenderer.java new file mode 100644 index 000000000..63c2ab1c9 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/SupportBlockRenderer.java @@ -0,0 +1,55 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugMeshData.LineWidth; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.MathHelper; + +import java.util.List; + +final class SupportBlockRenderer implements DebugWorldRenderer { + + private static final double RADIUS_SQ = 16.0 * 16.0; + private static final double GROW = 0.02; + private static final int MAX_BLOCKS = 256; + private static final int PLAYER = 0x66FF0000; + private static final int OTHER = 0x6600FF00; + + @Override + public int minIntervalMillis() { + return 100; + } + + @Override + public int fallbackIntervalMillis() { + return 100; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + final EntityLivingBase viewer = scan.viewer(); + final Entity player = Minecraft.getMinecraft().thePlayer; + final List entities = scan.world().loadedEntityList; + final int n = entities.size(); + int blocks = 0; + for (int i = 0; i < n && blocks < MAX_BLOCKS; i++) { + final Entity e = entities.get(i); + if (!e.onGround) continue; + final double dx = e.posX - viewer.posX; + final double dy = e.posY - viewer.posY; + final double dz = e.posZ - viewer.posZ; + if (dx * dx + dy * dy + dz * dz > RADIUS_SQ) continue; + final int bx = MathHelper.floor_double(e.posX); + int by = MathHelper.floor_double(e.boundingBox.minY - 0.2); + final int bz = MathHelper.floor_double(e.posZ); + final int belowType = scan.getBlock(bx, by - 1, bz).getRenderType(); + if (belowType == 11 || belowType == 32 || belowType == 21) by--; + final double x = bx; + final double y = by; + final double z = bz; + mesh.lineBox(LineWidth.MEDIUM, x - GROW, y - GROW, z - GROW, x + 1.0 + GROW, y + 1.0 + GROW, z + 1.0 + GROW, e == player ? PLAYER : OTHER); + blocks++; + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/WaterLevelRenderer.java b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/WaterLevelRenderer.java new file mode 100644 index 000000000..f6889c77f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/debug/screen/renderers/WaterLevelRenderer.java @@ -0,0 +1,39 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockLiquid; +import net.minecraft.block.material.Material; + +final class WaterLevelRenderer implements DebugWorldRenderer, ScanState.CellVisitor { + + private static final int RADIUS = 10; + private static final int MAX_CELLS = 8192; + private static final int FILL = 0x2600FF00; + private static final int LABEL = 0xFF000000; + + static int level(int meta) { + return meta >= 8 ? 8 : 8 - meta; + } + + @Override + public int fallbackIntervalMillis() { + return -1; + } + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + scan.scanCube(RADIUS, MAX_CELLS, this, mesh); + } + + @Override + public boolean visitCell(ScanState scan, DebugMeshData mesh, int x, int y, int z) { + final Block block = scan.getBlock(x, y, z); + if (!(block instanceof BlockLiquid) || block.getMaterial() != Material.water) return false; + final int meta = scan.getBlockMetadata(x, y, z); + final double height = scan.getBlock(x, y + 1, z).getMaterial() == Material.water ? 1.0 : 1.0 - BlockLiquid.getLiquidHeightPercent(meta); + final double top = y + height; + mesh.fillBox(x + 0.01, y + 0.01, z + 0.01, x + 0.99, top, z + 0.99, FILL); + mesh.label(x + 0.5, top, z + 0.5, level(meta), LABEL); + return true; + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java index efd8e1ecf..00b92420f 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java @@ -96,6 +96,24 @@ public enum Mixins implements IMixins { .addClientMixins("client.railcraft.MixinRenderBlockLamp") ), + BOTANIA_DEBUG_SCREEN(new MixinBuilder("Move Botania's F3 text into its own debug screen group") + .setPhase(Phase.LATE) + .addRequiredMod(TargetedMod.BOTANIA) + .addClientMixins("client.botania.MixinDebugHandler") + ), + + JOURNEYMAP_DEBUG_SCREEN(new MixinBuilder("Move JourneyMap's F3 text into its own debug screen group") + .setPhase(Phase.LATE) + .addRequiredMod(TargetedMod.JOURNEYMAP) + .addClientMixins("client.journeymap.MixinMiniMapOverlayHandler") + ), + + HODGEPODGE_DEBUG_SCREEN(new MixinBuilder("Move Hodgepodge's ChunkGen F3 text into its own debug screen group") + .setPhase(Phase.LATE) + .addRequiredMod(TargetedMod.HODGEPODGE) + .addClientMixins("client.hodgepodge.MixinChunkGenDebugHandler") + ), + ANGELICA_ENTITY_OVERLAYS(new MixinBuilder() .setPhase(Phase.EARLY) .setApplyIf(() -> AngelicaConfig.entityOverlayFixes) @@ -402,6 +420,14 @@ public enum Mixins implements IMixins { ) ), + ANGELICA_DEBUG_KEY(new MixinBuilder() + .addExcludedMod(TargetedMod.ET_FUTURUM_REQUIEM) + .setPhase(Phase.EARLY) + .addClientMixins( + "angelica.debug.MixinMinecraft_DebugKey" + ) + ), + IRIS_STARTUP(new MixinBuilder() .setPhase(Phase.EARLY) .setApplyIf(() -> AngelicaConfig.enableIris) diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/TargetedMod.java b/src/main/java/com/gtnewhorizons/angelica/mixins/TargetedMod.java index 9d220d568..f52076c8d 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/TargetedMod.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/TargetedMod.java @@ -9,6 +9,7 @@ public enum TargetedMod implements ITargetMod { ARCHAICFIX("org.embeddedt.archaicfix.ArchaicCore", "archaicfix"), BACKHAND("xonin.backhand.coremod.BackhandLoadingPlugin", "backhand"), + BOTANIA(null, "Botania"), CAMPFIRE_BACKPORT(null, "campfirebackport"), CHISEL(null, "chisel"), COFHCORE( "cofh.asm.LoadingPlugin", "CoFHCore"), @@ -17,14 +18,16 @@ public enum TargetedMod implements ITargetMod { DYNAMIC_SURROUNDINGS_ORIGINAL("org.blockartistry.mod.DynSurround.asm.TransformLoader", "dsurround"), DRAGON_API("Reika.DragonAPI.Auxiliary.DragonAPIASMHandler", "DragonAPI"), DRACONIC_EVOLUTION(null, "DraconicEvolution"), - ET_FUTURUM_REQUIEM(null, "etfuturum"), + ET_FUTURUM_REQUIEM("ganymedes01.etfuturum.mixinplugin.EtFuturumEarlyMixins", "etfuturum"), EXTRAUTILS(null, "ExtraUtilities"), FARSEEK("farseek.core.FarseekCoreMod", "farseek"), + HODGEPODGE("com.mitchej123.hodgepodge.core.HodgepodgeCore", "hodgepodge"), FALSETWEAKS("com.falsepattern.falsetweaks.asm.CoreLoadingPlugin", "falsetweaks"), LUMI("com.falsepattern.lumi.internal.asm.ASMLoadingPlugin", "lumi"), MINEFACTORY_RELOADED(null, "MineFactoryReloaded"), NTM_SPACE("com.hbm.dim.SolarSystem"), IC2("ic2.core.coremod.IC2core", "IC2"), + JOURNEYMAP(null, "journeymap"), NATURA(null, "Natura"), OPENBLOCKS(null, "OpenBlocks"), RAILCRAFT(null, "Railcraft"), diff --git a/src/main/java/com/gtnewhorizons/angelica/profiling/BailClassCounts.java b/src/main/java/com/gtnewhorizons/angelica/profiling/BailClassCounts.java index 7fdd7764f..d0a8ffa21 100644 --- a/src/main/java/com/gtnewhorizons/angelica/profiling/BailClassCounts.java +++ b/src/main/java/com/gtnewhorizons/angelica/profiling/BailClassCounts.java @@ -1,11 +1,11 @@ package com.gtnewhorizons.angelica.profiling; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import it.unimi.dsi.fastutil.objects.Reference2IntMap; import it.unimi.dsi.fastutil.objects.Reference2IntMaps; import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import net.minecraft.client.Minecraft; public final class BailClassCounts { public static final BailClassCounts MATERIAL = new BailClassCounts("tesr.bailCls.mat.", "BailMat"); @@ -16,7 +16,7 @@ public final class BailClassCounts { private final String label; private final Reference2IntOpenHashMap> counts = new Reference2IntOpenHashMap<>(); private final Reference2ObjectOpenHashMap, String> plotNames = new Reference2ObjectOpenHashMap<>(); - private String debugLine = ""; + private String debugValue = ""; private BailClassCounts(String plotPrefix, String label) { this.plotPrefix = plotPrefix; @@ -29,12 +29,12 @@ public final class BailClassCounts { public void flushFrame(boolean plot) { if (counts.isEmpty()) { - debugLine = ""; + debugValue = ""; return; } - final boolean debugScreen = Minecraft.getMinecraft().gameSettings.showDebugInfo; + final boolean debugScreen = DebugScreen.isRendererVerbose(); if (!plot && !debugScreen) { - debugLine = ""; + debugValue = ""; counts.clear(); return; } @@ -57,13 +57,13 @@ public final class BailClassCounts { } } if (!debugScreen) { - debugLine = ""; + debugValue = ""; } else { - final StringBuilder sb = new StringBuilder(label).append(": "); + final StringBuilder sb = new StringBuilder(); appendEntry(sb, a, an); if (b != null) appendEntry(sb.append(", "), b, bn); if (c != null) appendEntry(sb.append(", "), c, cn); - debugLine = sb.toString(); + debugValue = sb.toString(); } counts.clear(); } @@ -81,7 +81,11 @@ public final class BailClassCounts { sb.append(cls.getSimpleName()).append(" x").append(count); } - public String debugLine() { - return debugLine; + public String label() { + return label; + } + + public String debugValue() { + return debugValue; } } diff --git a/src/main/java/com/gtnewhorizons/angelica/profiling/RenderClassTimings.java b/src/main/java/com/gtnewhorizons/angelica/profiling/RenderClassTimings.java index 132d97deb..d43b7cba7 100644 --- a/src/main/java/com/gtnewhorizons/angelica/profiling/RenderClassTimings.java +++ b/src/main/java/com/gtnewhorizons/angelica/profiling/RenderClassTimings.java @@ -1,12 +1,12 @@ package com.gtnewhorizons.angelica.profiling; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2LongMap; import it.unimi.dsi.fastutil.objects.Reference2LongMaps; import it.unimi.dsi.fastutil.objects.Reference2LongOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import net.minecraft.client.Minecraft; public final class RenderClassTimings { public static final RenderClassTimings ENTITY = new RenderClassTimings("entity.cls.", "Entity"); @@ -20,7 +20,7 @@ public final class RenderClassTimings { private final Reference2LongOpenHashMap> nanos = new Reference2LongOpenHashMap<>(); private final Reference2IntOpenHashMap> counts = new Reference2IntOpenHashMap<>(); private final Reference2ObjectOpenHashMap, String> plotNames = new Reference2ObjectOpenHashMap<>(); - private String debugLine = ""; + private String debugValue = ""; private RenderClassTimings(String plotPrefix, String label) { this.plotPrefix = plotPrefix; @@ -34,12 +34,12 @@ public final class RenderClassTimings { public void flushFrame(boolean plot) { if (nanos.isEmpty()) { - debugLine = ""; + debugValue = ""; return; } - final boolean debugScreen = Minecraft.getMinecraft().gameSettings.showDebugInfo; + final boolean debugScreen = DebugScreen.isRendererVerbose(); if (!plot && !debugScreen) { - debugLine = ""; + debugValue = ""; nanos.clear(); counts.clear(); return; @@ -65,13 +65,13 @@ public final class RenderClassTimings { } } if (a == null || !debugScreen) { - debugLine = ""; + debugValue = ""; } else { - final StringBuilder sb = new StringBuilder(label).append(": "); + final StringBuilder sb = new StringBuilder(); appendEntry(sb, a, an); if (b != null) appendEntry(sb.append(", "), b, bn); if (c != null) appendEntry(sb.append(", "), c, cn); - debugLine = sb.toString(); + debugValue = sb.toString(); } nanos.clear(); counts.clear(); @@ -90,7 +90,11 @@ public final class RenderClassTimings { sb.append(cls.getSimpleName()).append(' ').append(ns / 1000).append("us x").append(counts.getInt(cls)); } - public String debugLine() { - return debugLine; + public String label() { + return label; + } + + public String debugValue() { + return debugValue; } } diff --git a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java index 01b846400..e2fe06c58 100644 --- a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java +++ b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java @@ -1,12 +1,14 @@ package com.gtnewhorizons.angelica.proxy; import com.gtnewhorizons.angelica.debug.DebugKeyHandler; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenOverlay; import com.gtnewhorizons.angelica.rendering.culling.GpuCulling; import static com.gtnewhorizons.angelica.AngelicaMod.MOD_ID; import java.io.IOException; import java.lang.management.ManagementFactory; -import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; import net.minecraft.block.Block; @@ -14,14 +16,8 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiVideoSettings; -import net.minecraft.client.network.NetHandlerPlayClient; import net.minecraft.client.resources.IReloadableResourceManager; import net.minecraft.server.integrated.IntegratedServer; -import net.minecraft.util.Direction; -import net.minecraft.util.MathHelper; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.world.EnumSkyBlock; -import net.minecraft.world.chunk.Chunk; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.client.event.EntityViewRenderEvent; import net.minecraftforge.client.event.FOVUpdateEvent; @@ -35,7 +31,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import biomesoplenty.api.content.BOPCBlocks; -import com.google.common.base.Objects; import com.gtnewhorizon.gtnhlib.client.model.loading.ModelRegistry; import com.gtnewhorizon.gtnhlib.client.renderer.vao.VAOManager; import com.gtnewhorizons.angelica.client.font.DarkModeUtils; @@ -46,7 +41,6 @@ import com.gtnewhorizons.angelica.common.BlockError; import com.gtnewhorizons.angelica.common.BlockIsbrhTest; import com.gtnewhorizons.angelica.compat.ModStatus; import com.gtnewhorizons.angelica.compat.bettercrashes.BetterCrashesCompat; -import com.gtnewhorizons.angelica.compat.mojang.CompatMathHelper; import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.rendering.TileEntityRenderBoundsRegistry; import com.gtnewhorizons.angelica.rendering.tesr.AngelicaTesrMeshCache; @@ -80,7 +74,6 @@ import com.gtnewhorizons.angelica.rendering.FpsReducer; import com.gtnewhorizons.angelica.rendering.FramePacer; import com.gtnewhorizons.angelica.rendering.IsbrhTestRenderer; import com.gtnewhorizons.angelica.rendering.TextureSamplers; -import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasDebugScreenHandler; import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasSetup; import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer; import com.gtnewhorizons.angelica.rendering.celeritas.threading.ChunkTaskRegistry; @@ -109,7 +102,6 @@ import me.flashyreese.mods.reeses_sodium_options.client.gui.ReeseSodiumVideoOpti import me.jellysquid.mods.sodium.client.gui.FrameRateOptions; import me.jellysquid.mods.sodium.client.gui.SodiumGameOptions; import net.coderbot.iris.Iris; -import net.coderbot.iris.client.IrisDebugScreenHandler; public final class ClientProxy extends CommonProxy { @@ -182,10 +174,8 @@ public final class ClientProxy extends CommonProxy { final CeleritasWorldRenderer renderer = CeleritasWorldRenderer.getInstanceOrNull(); if (renderer != null) renderer.runFrameIdleWork(deadlineNanos); }); - MinecraftForge.EVENT_BUS.register(CeleritasDebugScreenHandler.INSTANCE); if (AngelicaConfig.enableIris) { IrisGLSMBridge.register(); - MinecraftForge.EVENT_BUS.register(IrisDebugScreenHandler.INSTANCE); Iris.INSTANCE.fmlInitEvent(); FMLCommonHandler.instance().bus().register(Iris.INSTANCE); @@ -285,7 +275,11 @@ public final class ClientProxy extends CommonProxy { GpuCulling.onWorldUnload(); } - float lastIntegratedTickTime; + private static float lastIntegratedTickTime; + + public static float integratedTickTime() { + return lastIntegratedTickTime; + } @SubscribeEvent public void onTick(TickEvent.ServerTickEvent event) { @@ -300,8 +294,7 @@ public final class ClientProxy extends CommonProxy { @SubscribeEvent(priority = EventPriority.LOWEST) public void onRenderOverlay(RenderGameOverlayEvent.Pre event) { - if (!mc.gameSettings.showDebugInfo) return; - if (AngelicaConfig.modernizeF3Screen && event.type == RenderGameOverlayEvent.ElementType.CROSSHAIRS) { + if (AngelicaConfig.modernizeF3Screen && event.type == RenderGameOverlayEvent.ElementType.CROSSHAIRS && DebugScreenEntries.THREE_DIMENSIONAL_CROSSHAIR.isEnabled()) { F3Direction.renderWorldDirectionsEvent(mc, event); event.setCanceled(true); } @@ -309,81 +302,13 @@ public final class ClientProxy extends CommonProxy { @SubscribeEvent(priority = EventPriority.LOWEST) public void onRenderOverlay(RenderGameOverlayEvent.Text event) { - final Minecraft mc = Minecraft.getMinecraft(); - if (event.isCanceled() || !mc.gameSettings.showDebugInfo || event.left.isEmpty()) return; - - final NetHandlerPlayClient cl = mc.getNetHandler(); - if (cl != null) { - final IntegratedServer srv = mc.getIntegratedServer(); - - if (srv != null) { - final String s = String.format("Integrated server @ %.0f ms ticks", lastIntegratedTickTime); - event.left.add(Math.min(event.left.size(), 1), s); - } - } - - if (AngelicaConfig.showBlockDebugInfo && mc.objectMouseOver != null && mc.objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) { - if (!event.right.isEmpty() && !Objects.firstNonNull(event.right.get(event.right.size() - 1), "").isEmpty()) event.right.add(""); - final Block block = mc.theWorld.getBlock(mc.objectMouseOver.blockX, mc.objectMouseOver.blockY, mc.objectMouseOver.blockZ); - final int meta = mc.theWorld.getBlockMetadata(mc.objectMouseOver.blockX, mc.objectMouseOver.blockY, mc.objectMouseOver.blockZ); - event.right.add(Block.blockRegistry.getNameForObject(block)); - event.right.add("meta: " + meta); - } - - if (DynamicLights.isEnabled()) { - final var builder = new StringBuilder("Dynamic Light Sources: "); - final DynamicLights dl = DynamicLights.get(); - builder.append(dl.getLightSourcesCount()).append(" (U: ").append(dl.getLastUpdateCount()).append(')'); - - event.right.add(builder.toString()); - } - - if (AngelicaConfig.modernizeF3Screen) { - boolean hasReplacedXYZ = false; - for (int i = 0; i < event.left.size() - 3; i++) { - /* These checks should not be inefficient as most of the time the first one will already fail */ - if (!hasReplacedXYZ && Objects.firstNonNull(event.left.get(i), "").startsWith("x:") && Objects.firstNonNull(event.left.get(i + 1), "") - .startsWith("y:") && Objects.firstNonNull(event.left.get(i + 2), "").startsWith("z:") && Objects.firstNonNull(event.left.get(i + 3), "") - .startsWith("f:")) { - hasReplacedXYZ = true; - final int heading = MathHelper.floor_double((double) (mc.thePlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3; - final String heading_str = switch (heading) { - case 0 -> "Towards positive Z"; - case 1 -> "Towards negative X"; - case 2 -> "Towards negative Z"; - case 3 -> "Towards positive X"; - default -> throw new RuntimeException(); - }; - event.left.set(i, String.format("XYZ: %.3f / %.5f / %.3f", mc.thePlayer.posX, mc.thePlayer.boundingBox.minY, mc.thePlayer.posZ)); - final int bX = MathHelper.floor_double(mc.thePlayer.posX); - final int bY = MathHelper.floor_double(mc.thePlayer.boundingBox.minY); - final int bZ = MathHelper.floor_double(mc.thePlayer.posZ); - event.left.set(i + 1, String.format("Block: %d %d %d [%d %d %d]", bX, bY, bZ, bX & 15, bY & 15, bZ & 15)); - event.left.set(i + 2, String.format("Chunk: %d %d %d", bX >> 4, bY >> 4, bZ >> 4)); - event.left.set( - i + 3, String.format( - "Facing: %s (%s) (%.1f / %.1f)", - Direction.directions[heading].toLowerCase(Locale.ROOT), - heading_str, - MathHelper.wrapAngleTo180_float(mc.thePlayer.rotationYaw), - MathHelper.wrapAngleTo180_float(mc.thePlayer.rotationPitch))); - - final Chunk chunk = this.mc.theWorld.getChunkFromBlockCoords(bX, bZ); - event.left.set(i + 4, String.format( - "lc: %d b: %s bl: %d sl: %d rl: %d", - chunk.getTopFilledSegment() + 15, - this.mc.theWorld.getBiomeGenForCoords(bX, bZ).biomeName, - chunk.getSavedLightValue(EnumSkyBlock.Block, bX & 15, CompatMathHelper.clampHeight(bY), bZ & 15), - chunk.getSavedLightValue(EnumSkyBlock.Sky, bX & 15, CompatMathHelper.clampHeight(bY), bZ & 15), - chunk.getBlockLightValue(bX & 15, CompatMathHelper.clampHeight(bY), bZ & 15, 0))); - } - } - // Draw a frametime graph - if (((IGameSettingsExt) mc.gameSettings).angelica$showFpsGraph()) { - frametimeGraph.render(); - if (Minecraft.getMinecraft().isSingleplayer()) { - tpsGraph.render(); - } + if (event.isCanceled() || !DebugScreen.isOverlayVisible() || !DebugScreenOverlay.INSTANCE.debugBlockRan()) return; + if (!DebugScreen.isGroupedActive()) DebugScreenOverlay.INSTANCE.addFlatLines(mc, event.left, event.right); + if (AngelicaConfig.modernizeF3Screen && ((IGameSettingsExt) mc.gameSettings).angelica$showFpsGraph()) { + final int tick = DebugScreen.clientTick(); + frametimeGraph.render(event.resolution, tick); + if (mc.isSingleplayer()) { + tpsGraph.render(event.resolution, tick); } } } @@ -423,11 +348,13 @@ public final class ClientProxy extends CommonProxy { @SubscribeEvent public void onTick(TickEvent.ClientTickEvent event) { - if (event.phase == TickEvent.Phase.END) { - TesrLightCache.onClientTick(); + if (event.phase == TickEvent.Phase.START) { + DebugScreen.onClientTickStart(); + return; } - - if (event.phase == TickEvent.Phase.END && mc.theWorld != null) { + DebugScreen.onClientTickEnd(); + TesrLightCache.onClientTick(); + if (mc.theWorld != null) { CloudRenderer.getCloudRenderer().checkSettings(); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/FpsReducer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/FpsReducer.java index 58145c5aa..31b59152e 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/FpsReducer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/FpsReducer.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.rendering; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; import com.gtnewhorizons.angelica.rendering.ReducerStateMachine.State; import com.gtnewhorizons.angelica.AngelicaMod; @@ -167,7 +168,7 @@ public final class FpsReducer { private static void drawHud(boolean requireNoWorld) { final Minecraft mc = Minecraft.getMinecraft(); - if (mc.gameSettings.showDebugInfo) return; + if (DebugScreen.isOverlayVisible() || DebugScreen.isAlwaysOnTextAtTopLeft()) return; if (requireNoWorld && mc.theWorld != null) return; mc.fontRenderer.drawStringWithShadow(hudText, 2, 2, 0xFFFFFF); } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java index e3c6c2e24..b8373fe7b 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java @@ -1,5 +1,6 @@ package com.gtnewhorizons.angelica.rendering.celeritas; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; import com.cardinalstar.cubicchunks.world.ICubicWorld; import com.gtnewhorizons.angelica.client.rendering.AngelicaFogService; import com.gtnewhorizons.angelica.compat.ModStatus; @@ -16,7 +17,6 @@ import com.gtnewhorizons.angelica.rendering.celeritas.world.WorldSlice; import com.gtnewhorizons.angelica.rendering.celeritas.world.cloned.ClonedChunkSectionCache; import it.unimi.dsi.fastutil.longs.LongIterator; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; -import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.world.chunk.Chunk; @@ -118,7 +118,7 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { @Override protected boolean isDebugInfoShown() { - return Minecraft.getMinecraft().gameSettings.showDebugInfo; + return DebugScreenEntries.ANGELICA_RENDERER.isEnabled(); } @Override diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasDebugScreenHandler.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasDebugScreenHandler.java deleted file mode 100644 index e161f46b4..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasDebugScreenHandler.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.gtnewhorizons.angelica.rendering.celeritas; - -import com.gtnewhorizons.angelica.Tags; -import com.gtnewhorizons.angelica.config.AngelicaConfig; -import com.gtnewhorizons.angelica.config.SystemProperties; -import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.glsm.profiling.Tracy; -import com.gtnewhorizons.angelica.profiling.BailClassCounts; -import com.gtnewhorizons.angelica.profiling.RenderClassTimings; -import static com.gtnewhorizons.angelica.glsm.backend.BackendManager.RENDER_BACKEND; -import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; -import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; -import com.gtnewhorizons.angelica.glsm.states.FogState; -import com.gtnewhorizons.angelica.rendering.FramePacer; -import com.gtnewhorizons.angelica.rendering.tesr.ModelPartBatcher; -import com.gtnewhorizons.angelica.rendering.tesr.TesrBatchRenderer; -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import net.minecraft.client.Minecraft; -import net.minecraft.util.EnumChatFormatting; -import net.minecraftforge.client.event.RenderGameOverlayEvent; -import org.lwjgl.opengl.GL11; - -import java.util.ArrayList; -import java.util.List; - -public class CeleritasDebugScreenHandler { - public static final CeleritasDebugScreenHandler INSTANCE = new CeleritasDebugScreenHandler(); - - /** Toggle for fog debug display on F3 screen */ - public static boolean showFogDebug = false; - - private static final long REBUILD_INTERVAL_NS = 500_000_000L; - private List cachedRight; - private long lastBuildNanos; - - static boolean isVerbose() { - return AngelicaConfig.verboseF3 || SystemProperties.DEBUG_F3_DETAIL; - } - - /** Drops the cached right-hand lines so a verbose toggle shows on the next frame. */ - public static void invalidate() { - INSTANCE.cachedRight = null; - } - - @SubscribeEvent(priority = EventPriority.LOW) - public void onRenderGameOverlayTextEvent(RenderGameOverlayEvent.Text event) { - final Minecraft mc = Minecraft.getMinecraft(); - if (!mc.gameSettings.showDebugInfo || AngelicaConfig.disableF3Additions) { - return; - } - - final CeleritasWorldRenderer renderer = CeleritasWorldRenderer.getInstance(); - if (renderer == null) { - return; - } - - // Fog debug on left side (when enabled) - if (showFogDebug) { - event.left.add(getFogDebugString()); - } - - if (DynamicLights.configEnabled && DynamicLights.isEnabled()) { - event.left.add(getDynamicLightsDebugString()); - } - - final long now = System.nanoTime(); - if (cachedRight == null || now - lastBuildNanos >= REBUILD_INTERVAL_NS) { - cachedRight = buildRightLines(renderer); - lastBuildNanos = now; - } - event.right.addAll(cachedRight); - } - - private static List buildRightLines(CeleritasWorldRenderer renderer) { - final List lines = new ArrayList<>(); - lines.add(""); - lines.add(EnumChatFormatting.GREEN + "Angelica " + Tags.VERSION + " [Celeritas Renderer] [" + RENDER_BACKEND.getName() + "]"); - lines.addAll(renderer.getDebugStrings()); - if (isVerbose()) { - final ShaderManager sm = ShaderManager.getInstance(); - if (sm != null) { - lines.add(sm.getDebugInfo()); - } - lines.add(TessellatorStreamingDrawer.getDebugInfo()); - lines.add(TesrBatchRenderer.INSTANCE.getDebugSummaryLine()); - final String transferInfo = RENDER_BACKEND.getTransferDebugInfo(); - if (transferInfo != null) { - lines.add(transferInfo); - } - lines.addAll(TesrBatchRenderer.INSTANCE.getDebugDetailStrings()); - lines.add(ModelPartBatcher.INSTANCE.getDebugString()); - if (Tracy.ENABLED) { - addIfPresent(lines, RenderClassTimings.TESR.debugLine()); - addIfPresent(lines, RenderClassTimings.ENTITY.debugLine()); - addIfPresent(lines, RenderClassTimings.SHADOW_ENTITY.debugLine()); - addIfPresent(lines, BailClassCounts.MATERIAL.debugLine()); - addIfPresent(lines, BailClassCounts.TEMPLATE.debugLine()); - addIfPresent(lines, BailClassCounts.PARTICLE_SPILL.debugLine()); - } - } - lines.add(""); - return lines; - } - - private static void addIfPresent(List lines, String line) { - if (!line.isEmpty()) { - lines.add(line); - } - } - - public static String getFogDebugString() { - final boolean enabled = GLStateManager.getFogMode().isEnabled(); - final FogState fog = GLStateManager.getFogState(); - final String modeName = switch (fog.getFogMode()) { - case GL11.GL_LINEAR -> "LINEAR"; - case GL11.GL_EXP -> "EXP"; - case GL11.GL_EXP2 -> "EXP2"; - default -> "?"; - }; - return String.format("Fog: %s %s %.0f-%.0f d=%.2f (%.2f, %.2f, %.2f)", - enabled ? "ON" : "OFF", - modeName, - fog.getStart(), - fog.getEnd(), - fog.getDensity(), - fog.getFogColor().x, - fog.getFogColor().y, - fog.getFogColor().z); - } - - public static String getDynamicLightsDebugString() { - final DynamicLights dl = DynamicLights.get(); - int sources = dl.getLightSourcesCount(); - int updated = dl.getLastUpdateCount(); - - if (DynamicLights.FrustumCullingEnabled) { - int pending = dl.getChunkRebuildManager().getPendingCount(); - return String.format("DynLights: %d src, %d upd, %d pending", sources, updated, pending); - } - return String.format("DynLights: %d sources, %d updated", sources, updated); - } -} diff --git a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java index c9db93d55..edcfa7a0e 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumGameOptionPages.java @@ -842,18 +842,6 @@ public class SodiumGameOptionPages { .setFlags(OptionFlag.REQUIRES_RENDERER_RELOAD) .setEnabled(NotFineConfig.allowToggle3DAnaglyph) .build()) - .add(OptionImpl.createBuilder(boolean.class, angelicaOpts) - .setName(I18n.format("options.angelica.disablef3")) - .setTooltip(I18n.format("options.angelica.disablef3.tooltip")) - .setControl(TickBoxControl::new) - .setBinding((opts, value) -> AngelicaConfig.disableF3Additions = value, opts -> AngelicaConfig.disableF3Additions) - .build()) - .add(OptionImpl.createBuilder(boolean.class, angelicaOpts) - .setName(I18n.format("options.angelica.verbosef3")) - .setTooltip(I18n.format("options.angelica.verbosef3.tooltip")) - .setControl(TickBoxControl::new) - .setBinding((opts, value) -> AngelicaConfig.verboseF3 = value, opts -> AngelicaConfig.verboseF3) - .build()) .build()); if (AngelicaConfig.enableZoom) { diff --git a/src/main/java/net/coderbot/iris/Iris.java b/src/main/java/net/coderbot/iris/Iris.java index fddc3d0c6..20578a176 100644 --- a/src/main/java/net/coderbot/iris/Iris.java +++ b/src/main/java/net/coderbot/iris/Iris.java @@ -4,6 +4,7 @@ import com.google.common.base.Throwables; import com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager; import com.gtnewhorizons.angelica.Tags; import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; import com.gtnewhorizons.angelica.glsm.RenderSystem; import com.gtnewhorizons.angelica.glsm.backend.BackendManager; import com.gtnewhorizons.angelica.glsm.hooks.GLSMHooks; @@ -251,7 +252,7 @@ public class Iris { public void keyUp(InputEvent.KeyInputEvent event) { final int key = Keyboard.getEventKey(); final boolean released = !Keyboard.getEventKeyState(); - if (Minecraft.getMinecraft().gameSettings.showDebugInfo && GuiScreen.isShiftKeyDown() && GuiScreen.isCtrlKeyDown() && released) { + if (DebugScreen.isOverlayVisible() && GuiScreen.isShiftKeyDown() && GuiScreen.isCtrlKeyDown() && released) { if (key == Keyboard.KEY_N) { ClientProxy.animationsMode.next(); } diff --git a/src/main/java/net/coderbot/iris/client/IrisDebugScreenHandler.java b/src/main/java/net/coderbot/iris/client/IrisDebugScreenHandler.java deleted file mode 100644 index b1ae1ed1d..000000000 --- a/src/main/java/net/coderbot/iris/client/IrisDebugScreenHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -package net.coderbot.iris.client; - -import com.gtnewhorizons.angelica.config.AngelicaConfig; -import com.gtnewhorizons.angelica.proxy.ClientProxy; -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import net.coderbot.iris.Iris; -import net.minecraft.client.Minecraft; -import net.minecraftforge.client.event.RenderGameOverlayEvent; - -import java.lang.management.BufferPoolMXBean; -import java.lang.management.ManagementFactory; -import java.text.CharacterIterator; -import java.text.StringCharacterIterator; -import java.util.List; -import java.util.Objects; - -public class IrisDebugScreenHandler { - public static final IrisDebugScreenHandler INSTANCE = new IrisDebugScreenHandler(); - private static final List iris$pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); - - private static final BufferPoolMXBean iris$directPool; - - static { - BufferPoolMXBean found = null; - - for (BufferPoolMXBean pool : iris$pools) { - if (pool.getName().equals("direct")) { - found = pool; - break; - } - } - - iris$directPool = Objects.requireNonNull(found); - } - - @SubscribeEvent(priority = EventPriority.LOW) - public void onRenderGameOverlayTextEvent(RenderGameOverlayEvent.Text event) { - final Minecraft mc = Minecraft.getMinecraft(); - if (mc.gameSettings.showDebugInfo && !AngelicaConfig.disableF3Additions) { - event.right.add(Math.min(event.right.size(), 2), "Direct Buffers: +" + iris$humanReadableByteCountBin(iris$directPool.getMemoryUsed())); - - if (Iris.getIrisConfig().areShadersEnabled()) { - event.right.add("[" + Iris.MODNAME + "] Shaderpack: " + Iris.getCurrentPackName() + (Iris.isFallback() ? " (fallback)" : "")); - Iris.getCurrentPack().ifPresent(pack -> event.right.add("[" + Iris.MODNAME + "] " + pack.getProfileInfo())); - } else { - event.right.add("[" + Iris.MODNAME + "] Shaders are disabled"); - } - if (AngelicaConfig.speedupAnimations) { - event.right.add(Math.min(event.right.size(), 9), "animationsMode: " + ClientProxy.animationsMode); - } - - Iris.getPipelineManager().getPipeline().ifPresent(pipeline -> pipeline.addDebugText(event.left)); - - } - } - - private static String iris$humanReadableByteCountBin(long bytes) { - long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); - if (absB < 1024) { - return bytes + " B"; - } - long value = absB; - CharacterIterator ci = new StringCharacterIterator("KMGTPE"); - for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) { - value >>= 10; - ci.next(); - } - value *= Long.signum(bytes); - return String.format("%.3f %ciB", value / 1024.0, ci.current()); - } - -} diff --git a/src/main/resources/META-INF/angelica_at.cfg b/src/main/resources/META-INF/angelica_at.cfg index dcd95dff4..9d8dee371 100644 --- a/src/main/resources/META-INF/angelica_at.cfg +++ b/src/main/resources/META-INF/angelica_at.cfg @@ -124,3 +124,4 @@ public net.minecraft.client.particle.EntityNoteFX field_70585_a # noteParticleSc public net.minecraft.client.particle.EntityReddustFX field_70570_a # reddustParticleScale public net.minecraft.client.particle.EntitySnowShovelFX field_70588_a # snowDigParticleScale public net.minecraft.client.particle.EntityFireworkSparkFX field_92048_ay +public net.minecraft.client.gui.GuiTextField field_146225_q # lineScrollOffset diff --git a/src/main/resources/assets/angelica/lang/en_US.lang b/src/main/resources/assets/angelica/lang/en_US.lang index 55c7754f0..b27096ed6 100644 --- a/src/main/resources/assets/angelica/lang/en_US.lang +++ b/src/main/resources/assets/angelica/lang/en_US.lang @@ -394,12 +394,121 @@ sodium.options.use_render_pass_optimization.tooltip=Analyzes sprite transparency options.angelica.aggressiveChunkLoading=Aggressive Chunk Loading options.angelica.aggressiveChunkLoading.tooltip=Renders chunks immediately when loaded, even if neighbors aren't ready yet. Improves perceived loading speed at render distance edges. Chunks rebuild automatically when neighbors load. Useful for low render distance servers. -options.angelica.disablef3=Disable F3 Additions -options.angelica.disablef3.tooltip=Disables Angelica's additions to the F3 debug screen. -options.angelica.verbosef3=Verbose F3 -options.angelica.verbosef3.tooltip=Shows developer counters (fixed-function shaders, streaming, TESR batching, GPU transfer) on the F3 screen. Toggle in game with F3+V. angelica.debug.verbose.on=Verbose F3: shown angelica.debug.verbose.off=Verbose F3: hidden +angelica.debug.group.rendering=Rendering +angelica.debug.group.position=Position +angelica.debug.group.looking_at_block=Looking At Block +angelica.debug.group.memory=Memory +angelica.debug.group.system_specs=System Specs +angelica.debug.group.forge=Forge +angelica.debug.group.angelica=Angelica +angelica.debug.group.shaders=Shaders +angelica.debug.group.other=Other +angelica.debug.group.help=Help +angelica.debug.options.title=Debug Options +angelica.debug.options.warning=These options are for testing purposes only. They may slow down your computer, crash the game, or eat your pet rock. +angelica.debug.options.search=Search... +angelica.debug.options.category.text=Debug Screen Text +angelica.debug.options.category.renderer=Debug Renderers +angelica.debug.options.category.layout=Layout +angelica.debug.options.profile.default=Default profile +angelica.debug.options.profile.performance=Performance profile +angelica.debug.options.failed.tooltip=This entry threw an error and is disabled until restart. See the log. +angelica.debug.options.flat_notice=Classic F3 layout is active (the grouped screen is turned off, or Angelica's font renderer is disabled): only these entries apply. +angelica.debug.options.text_size=F3 Text Size +angelica.debug.options.text_size.match=Match GUI +angelica.debug.options.text_size.auto=Auto +angelica.debug.options.text_size.tooltip=Match GUI: same size as your GUI Scale. Auto: half the largest size that fits the window. Never larger than your GUI Scale. +angelica.debug.options.category.panels=Panels +angelica.debug.options.panels.tooltip=Use group=%s in F3 line rules (Angelica compat config) to move a line into this panel. +angelica.debug.options.status.overlay=In Overlay +angelica.debug.options.status.always=Always +angelica.debug.entry.minecraft.game_version=Game Version +angelica.debug.entry.minecraft.fps=FPS +angelica.debug.entry.minecraft.tps=Server Tick Time +angelica.debug.entry.minecraft.player_position=Player Position +angelica.debug.entry.minecraft.player_section_position=Section Position +angelica.debug.entry.minecraft.player_speed=Player Speed +angelica.debug.entry.minecraft.biome=Biome +angelica.debug.entry.minecraft.light_levels=Light Levels +angelica.debug.entry.minecraft.heightmap=Heightmap +angelica.debug.entry.minecraft.chunk_render_stats=Chunk Render Stats +angelica.debug.entry.minecraft.entity_render_stats=Entity Render Stats +angelica.debug.entry.minecraft.particle_render_stats=Particle Stats +angelica.debug.entry.minecraft.chunk_source_stats=Chunk Cache Stats +angelica.debug.entry.minecraft.post_effect=Post Effect +angelica.debug.entry.minecraft.memory=Memory +angelica.debug.entry.minecraft.system_specs=System Specs +angelica.debug.entry.minecraft.looking_at_block=Looking At Block +angelica.debug.entry.forge.brandings=Forge and Mod Versions +angelica.debug.entry.angelica.renderer=Angelica Renderer +angelica.debug.entry.angelica.renderer_verbose=Verbose Renderer Info +angelica.debug.entry.angelica.shaders=Shader Pack +angelica.debug.entry.angelica.dynamic_lights=Dynamic Lights +angelica.debug.entry.angelica.fog=Fog +angelica.debug.entry.angelica.animations=Texture Animations +angelica.debug.entry.angelica.other_left=Other Mods (Left) +angelica.debug.entry.angelica.other_right=Other Mods (Right) +angelica.debug.entry.angelica.help=Key Help +angelica.debug.entry.minecraft.3d_crosshair=3D Crosshair +angelica.debug.entry.minecraft.entity_hitboxes=Entity Hitboxes +angelica.debug.entry.minecraft.chunk_borders=Chunk Borders +angelica.debug.entry.minecraft.chunk_section_visibility=Section Visibility +angelica.debug.entry.minecraft.visualize_block_light_levels=Block Light Levels +angelica.debug.entry.minecraft.visualize_sky_light_levels=Sky Light Levels +angelica.debug.entry.minecraft.visualize_heightmap=Heightmap Markers +angelica.debug.entry.minecraft.visualize_collision_boxes=Collision Boxes +angelica.debug.entry.minecraft.visualize_solid_faces=Solid Faces +angelica.debug.entry.minecraft.visualize_water_levels=Water Levels +angelica.debug.entry.minecraft.visualize_entity_supporting_block=Entity Supporting Block +angelica.debug.entry.angelica.chunk_minimap=Chunk Minimap +angelica.debug.entry.minecraft.game_version.tooltip=Minecraft version line at the top of the debug screen +angelica.debug.entry.minecraft.fps.tooltip=Frames per second, the active frame cap and chunk updates, shown at the top left +angelica.debug.entry.minecraft.tps.tooltip=Integrated server tick time in milliseconds; singleplayer only +angelica.debug.entry.minecraft.player_position.tooltip=Your exact position, block, chunk, facing direction and dimension +angelica.debug.entry.minecraft.player_section_position.tooltip=Your block position relative to the 16x16x16 section you are in +angelica.debug.entry.minecraft.player_speed.tooltip=Your speed in blocks per tick, walk and fly speed, and whether you are on the ground +angelica.debug.entry.minecraft.biome.tooltip=The biome at your position, with the owning mod for modded biomes +angelica.debug.entry.minecraft.light_levels.tooltip=Light level at your position, split into sky and block light +angelica.debug.entry.minecraft.heightmap.tooltip=Height of the top block at your position and the top filled section of your chunk +angelica.debug.entry.minecraft.chunk_render_stats.tooltip=Visible and total render sections, render distance and animated sprite counts +angelica.debug.entry.minecraft.entity_render_stats.tooltip=How many entities were drawn, hidden or culled this frame out of those loaded +angelica.debug.entry.minecraft.particle_render_stats.tooltip=Particle count from the vanilla particle manager +angelica.debug.entry.minecraft.chunk_source_stats.tooltip=Client chunk cache statistics from the world provider +angelica.debug.entry.minecraft.post_effect.tooltip=Name of the active vanilla post-processing shader group +angelica.debug.entry.minecraft.memory.tooltip=Java heap use, allocated heap, allocation rate and direct buffer memory +angelica.debug.entry.minecraft.system_specs.tooltip=Java version, CPU core count, display size, GPU and OpenGL version +angelica.debug.entry.minecraft.looking_at_block.tooltip=Coordinates, registry name and metadata of the block under the crosshair +angelica.debug.entry.forge.brandings.tooltip=MCP, FML and Forge versions plus the number of loaded and active mods +angelica.debug.entry.angelica.renderer.tooltip=Angelica version, renderer backend and terrain buffer sizes +angelica.debug.entry.angelica.renderer_verbose.tooltip=Adds detailed renderer timings to the Angelica panel, like F3+V +angelica.debug.entry.angelica.shaders.tooltip=Active shader pack and its pipeline details; shows off when shaders are disabled +angelica.debug.entry.angelica.dynamic_lights.tooltip=Dynamic light source count, updates this frame and pending chunk rebuilds +angelica.debug.entry.angelica.fog.tooltip=Fog state: on or off, mode, start and end distance, density and color +angelica.debug.entry.angelica.animations.tooltip=Texture animation mode; hidden unless animation speedup is enabled +angelica.debug.entry.angelica.other_left.tooltip=Lines other mods add to the left side of the debug screen +angelica.debug.entry.angelica.other_right.tooltip=Lines other mods add to the right side of the debug screen +angelica.debug.entry.angelica.help.tooltip=Reminder of the F3 key combinations, shown while the debug screen is open +angelica.debug.entry.minecraft.3d_crosshair.tooltip=Replaces the crosshair with colored X, Y and Z axis lines that follow your view +angelica.debug.entry.minecraft.entity_hitboxes.tooltip=Shows entity hitboxes, like F3+B +angelica.debug.entry.minecraft.chunk_borders.tooltip=Outlines the chunk and section you are in, like F3+G +angelica.debug.entry.minecraft.chunk_section_visibility.tooltip=Marks nearby sections whose faces do not connect, which cull terrain behind them +angelica.debug.entry.minecraft.visualize_block_light_levels.tooltip=Draws the block light value on each nearby block that is not fully dark +angelica.debug.entry.minecraft.visualize_sky_light_levels.tooltip=Draws the sky light value on each nearby block that is not at full sky light +angelica.debug.entry.minecraft.visualize_heightmap.tooltip=Marks the top block of every column in nearby chunks +angelica.debug.entry.minecraft.visualize_collision_boxes.tooltip=Outlines the colliding boxes within a few blocks of you +angelica.debug.entry.minecraft.visualize_solid_faces.tooltip=Tints solid block faces that are exposed to a non-opaque neighbor, near you +angelica.debug.entry.minecraft.visualize_water_levels.tooltip=Shows nearby water volumes with their flow level from 1 to 8 +angelica.debug.entry.minecraft.visualize_entity_supporting_block.tooltip=Outlines the block each nearby grounded entity stands on; yours is red, others green +angelica.debug.entry.angelica.chunk_minimap.tooltip=Small map of chunk render states in the corner +angelica.debug.chunk_borders.on=Chunk borders: shown +angelica.debug.chunk_borders.off=Chunk borders: hidden +angelica.debug.help.options=F3 + F6 = Edit debug options +angelica.debug.help.hitboxes=F3 + B = Show hitboxes +angelica.debug.help.chunk_borders=F3 + G = Show chunk borders +angelica.debug.help.verbose=F3 + V = Verbose renderer info +angelica.debug.help.graphs=Alt + F3 = FPS and TPS graphs options.angelica.texteffects.rgb_colors=RGB Colors options.angelica.texteffects.rgb_colors.tooltip=Enable hex colors via %s syntax options.angelica.texteffects.gradients=Gradients diff --git a/src/main/resources/assets/angelica/lang/ja_JP.lang b/src/main/resources/assets/angelica/lang/ja_JP.lang index 83ed38a05..a25f7d3aa 100644 --- a/src/main/resources/assets/angelica/lang/ja_JP.lang +++ b/src/main/resources/assets/angelica/lang/ja_JP.lang @@ -387,10 +387,6 @@ sodium.options.use_render_pass_optimization.tooltip=スプライトの透明度 options.angelica.aggressiveChunkLoading=積極的チャンクロード options.angelica.aggressiveChunkLoading.tooltip=隣接するチャンクの準備がまだ整っていなくても、チャンクが読み込まれるとすぐに描画することで、描画距離の端での体感的なチャンクロード速度が向上します。描画距離の短いサーバーで役立ちます。 -options.angelica.disablef3=F3画面追加情報の無効化 -options.angelica.disablef3.tooltip=Angelicaに関するF3画面の情報の表示を無効にします。 -options.angelica.verbosef3=冗長なF3画面 -options.angelica.verbosef3.tooltip=F3画面に開発者用カウンター(固定機能シェーダー、ストリーミング、TESR同時処理、GPU転送)を表示します。F3+Vキーで切り替え可能です。 angelica.debug.verbose.on=冗長なF3画面:表示する angelica.debug.verbose.off=冗長なF3画面:表示しない options.angelica.texteffects.rgb_colors=RGBカラー diff --git a/src/main/resources/assets/angelica/lang/uk_UA.lang b/src/main/resources/assets/angelica/lang/uk_UA.lang index aacbdd7c6..8b9c6fd97 100644 --- a/src/main/resources/assets/angelica/lang/uk_UA.lang +++ b/src/main/resources/assets/angelica/lang/uk_UA.lang @@ -268,8 +268,6 @@ sodium.options.use_render_pass_optimization.tooltip=Аналізує прозо options.angelica.aggressiveChunkLoading=Агресивне завантаження чанків options.angelica.aggressiveChunkLoading.tooltip=Візуалізує чанки відразу після їх завантаження, навіть якщо сусідні чанки ще не готові. Покращує видиму швидкість завантаження на межах дистанції промальовування. Чанки автоматично перебудовуються, коли завантажуються сусідні. Корисно для серверів із низькою дистанцією рендерингу. -options.angelica.disablef3=Вимкнути доповнення F3 -options.angelica.disablef3.tooltip=Вимкнути додаткову інформацію від Angelica на екрані налагодження F3. options.angelica.texteffects.rgb_colors=Кольори RGB options.angelica.texteffects.rgb_colors.tooltip=Увімкнути hex-кольори через синтаксис %s options.angelica.texteffects.gradients=Градієнти diff --git a/src/main/resources/assets/angelica/lang/zh_CN.lang b/src/main/resources/assets/angelica/lang/zh_CN.lang index 56d4a18f4..396c84db2 100644 --- a/src/main/resources/assets/angelica/lang/zh_CN.lang +++ b/src/main/resources/assets/angelica/lang/zh_CN.lang @@ -222,8 +222,6 @@ sodium.options.use_render_pass_optimization.tooltip=分析贴图透明度,当 options.angelica.aggressiveChunkLoading=更激进地区块加载 options.angelica.aggressiveChunkLoading.tooltip=区块加载后立即渲染,即使相邻区块尚未加载完毕。将提升最大渲染距离处区块的加载速度。在相邻区块加载后会自动重建。适用于渲染距离较低的服务器。 -options.angelica.disablef3=禁用F3附加信息显示 -options.angelica.disablef3.tooltip=禁用Angelica在F3调试界面的附加信息。 sodium.options.multidraw_mode.name=批量绘制模式 sodium.options.multidraw_mode.tooltip=控制地形渲染的绘制命令的调度方式。§l直接§r使用glMultiDrawElementsBaseVertex(默认,GL 3.2+)。§l间接§r使用GPU间接渲染(GL 4.3+),可能在某些硬件上减少CPU开销。§l单独§r发出单独的绘制调用。 diff --git a/src/main/resources/assets/angelica/shaders/debug.frag b/src/main/resources/assets/angelica/shaders/debug.frag new file mode 100644 index 000000000..94e58daee --- /dev/null +++ b/src/main/resources/assets/angelica/shaders/debug.frag @@ -0,0 +1,17 @@ +#version 330 core + +in vec4 v_Color; +#ifdef TEXTURED +in vec2 v_TexCoord0; +uniform sampler2D u_Texture; +#endif + +out vec4 fragColor; + +void main() { +#ifdef TEXTURED + fragColor = v_Color * texture(u_Texture, v_TexCoord0); +#else + fragColor = v_Color; +#endif +} diff --git a/src/main/resources/assets/angelica/shaders/debug.vert b/src/main/resources/assets/angelica/shaders/debug.vert new file mode 100644 index 000000000..6cd3e03fc --- /dev/null +++ b/src/main/resources/assets/angelica/shaders/debug.vert @@ -0,0 +1,20 @@ +#version 330 core + +in vec3 a_Position; +in vec4 a_Color; +#ifdef TEXTURED +in vec2 a_TexCoord0; +out vec2 v_TexCoord0; +#endif + +uniform mat4 u_MVP; + +out vec4 v_Color; + +void main() { + gl_Position = u_MVP * vec4(a_Position, 1.0); + v_Color = a_Color; +#ifdef TEXTURED + v_TexCoord0 = a_TexCoord0; +#endif +} diff --git a/src/main/resources/assets/angelica/shaders/debug_lines.vert b/src/main/resources/assets/angelica/shaders/debug_lines.vert new file mode 100644 index 000000000..7f50ed84c --- /dev/null +++ b/src/main/resources/assets/angelica/shaders/debug_lines.vert @@ -0,0 +1,19 @@ +#version 330 core + +#import + +in vec3 a_Position; +in vec4 a_Color; +in vec3 a_LineOtherEnd; + +uniform mat4 u_MVP; +uniform vec2 u_ViewportSize; +uniform float u_LineWidth; + +out vec4 v_Color; + +void main() { + float lineT; + gl_Position = angelica_lineQuad(u_MVP * vec4(a_Position, 1.0), u_MVP * vec4(a_LineOtherEnd, 1.0), gl_VertexID % 6, u_ViewportSize, u_LineWidth, lineT); + v_Color = a_Color; +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java index 5db559bb3..ebda5f411 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java @@ -2,6 +2,8 @@ package com.gtnewhorizons.angelica.mixins.early.angelica; import com.gtnewhorizons.angelica.AngelicaMod; import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.DebugKeyHandler; import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; @@ -110,9 +112,20 @@ public abstract class MixinMinecraft { @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiScreen;isShiftKeyDown()Z", shift = At.Shift.AFTER)) private void angelica$setShowFpsGraph(CallbackInfo ci) { + if (AngelicaConfig.modernizeF3Screen && DebugKeyHandler.F3_TOGGLE.isArmed()) return; ((IGameSettingsExt) gameSettings).angelica$setShowFpsGraph(Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU)); } + @Inject(method = "func_152348_aa", at = @At("HEAD"), cancellable = true, require = 1) + private void angelica$disableStreamKeys(CallbackInfo ci) { + if (!Keyboard.getEventKeyState() || !Keyboard.isKeyDown(Keyboard.KEY_F3)) return; + final int key = Keyboard.getEventKey(); + if (key == gameSettings.field_152396_an.getKeyCode() || key == gameSettings.field_152397_ao.getKeyCode() + || key == gameSettings.field_152398_ap.getKeyCode() || key == gameSettings.field_152399_aq.getKeyCode()) { + ci.cancel(); + } + } + @Inject(method = "runGameLoop", at = @At("RETURN")) private void angelica$streamingBufferEndFrame(CallbackInfo ci) { if (Tracy.ENABLED) Tracy.beginZone(angelica$Z_STREAMING_END_FRAME); diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_DebugKey.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_DebugKey.java new file mode 100644 index 000000000..c812e8844 --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_DebugKey.java @@ -0,0 +1,39 @@ +package com.gtnewhorizons.angelica.mixins.early.angelica.debug; + +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.DebugKeyHandler; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.settings.GameSettings; +import org.lwjgl.input.Keyboard; +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(Minecraft.class) +public class MixinMinecraft_DebugKey { + + @WrapOperation( + method = "runTick", + at = @At(value = "FIELD", opcode = Opcodes.PUTFIELD, target = "Lnet/minecraft/client/settings/GameSettings;showDebugInfo:Z") + ) + private void angelica$deferDebugInfoToggle(GameSettings instance, boolean value, Operation original) { + if (!AngelicaConfig.modernizeF3Screen) { + original.call(instance, value); + return; + } + DebugKeyHandler.F3_TOGGLE.arm(GuiScreen.isShiftKeyDown(), Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU)); + } + + @WrapOperation( + method = "runTick", + at = @At(value = "FIELD", opcode = Opcodes.PUTFIELD, target = "Lnet/minecraft/client/settings/GameSettings;showDebugProfilerChart:Z") + ) + private void angelica$deferDebugProfilerChart(GameSettings instance, boolean value, Operation original) { + if (!AngelicaConfig.modernizeF3Screen) { + original.call(instance, value); + } + } +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_FPSCap.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_FPSCap.java index 6e750ab96..dac515a7e 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_FPSCap.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/debug/MixinMinecraft_FPSCap.java @@ -1,9 +1,10 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.debug; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; import com.gtnewhorizons.angelica.rendering.FpsReducer; import com.gtnewhorizons.angelica.rendering.FramePacer; import net.minecraft.client.Minecraft; -import net.minecraft.client.settings.GameSettings; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -15,11 +16,9 @@ public class MixinMinecraft_FPSCap { @Shadow public String debug; - @Shadow public GameSettings gameSettings; - @Inject(method = "runGameLoop", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;debug:Ljava/lang/String;", shift = At.Shift.AFTER, ordinal = 0)) private void angelica$appendFPSCapInfo(CallbackInfo ci) { - if (!this.gameSettings.showDebugInfo) return; + if (!DebugScreen.isOverlayVisible() && !DebugScreenEntries.FPS.isEnabled()) return; final String indicator = FramePacer.debugIndicator(); final String tag = FpsReducer.debugTag(); if (indicator == null && tag == null) return; diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/gui/MixinGuiIngameForge_ModernF3.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/gui/MixinGuiIngameForge_ModernF3.java index a5aeb6389..0789ddc8c 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/gui/MixinGuiIngameForge_ModernF3.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/gui/MixinGuiIngameForge_ModernF3.java @@ -1,14 +1,115 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.gui; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenOverlay; +import com.gtnewhorizons.angelica.debug.screen.ForgeDebugPlaceholders; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.Gui; +import net.minecraft.util.MathHelper; import net.minecraftforge.client.GuiIngameForge; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; +import org.spongepowered.asm.lib.Opcodes; 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.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.ArrayList; @Mixin(GuiIngameForge.class) -public class MixinGuiIngameForge_ModernF3 { +public abstract class MixinGuiIngameForge_ModernF3 { + + @Shadow(remap = false) + private FontRenderer fontrenderer; + + @Shadow(remap = false) + private boolean pre(ElementType type) { + return false; + } + + @Shadow(remap = false) + private void post(ElementType type) {} + + @Unique + private boolean angelica$forgeLinesSkipped; + + @Inject(method = "renderHUDText", remap = false, at = @At("HEAD")) + private void angelica$resetDebugLatch(int width, int height, CallbackInfo ci) { + angelica$forgeLinesSkipped = false; + DebugScreen.syncOverlayVisible(); + } + + @ModifyExpressionValue( + method = "renderHUDText", + remap = false, + require = 1, + at = @At( + value = "FIELD", + target = "Lnet/minecraft/client/settings/GameSettings;showDebugInfo:Z", + opcode = Opcodes.GETFIELD, + ordinal = 0, + remap = true)) + private boolean angelica$skipForgeDebugLines(boolean showDebugInfo) { + final boolean show = showDebugInfo; + angelica$forgeLinesSkipped = show && DebugScreen.isGroupedActive() && DebugScreen.ENTRIES != null; + return show && !angelica$forgeLinesSkipped; + } + + @Inject( + method = "renderHUDText", + remap = false, + require = 1, + at = @At( + value = "INVOKE", + target = "net/minecraftforge/client/event/RenderGameOverlayEvent$Text.(Lnet/minecraftforge/client/event/RenderGameOverlayEvent;Ljava/util/ArrayList;Ljava/util/ArrayList;)V", + remap = false)) + private void angelica$afterDebugBlock(int width, int height, CallbackInfo ci, @Local(ordinal = 0) ArrayList left, @Local(ordinal = 1) ArrayList right) { + final boolean skipped = angelica$forgeLinesSkipped; + final boolean ran; + if (skipped) { + ran = !pre(ElementType.DEBUG); + if (ran) { + final Minecraft mc = Minecraft.getMinecraft(); + mc.mcProfiler.startSection("debug"); + ForgeDebugPlaceholders.add(left, right, angelica$chunkLoaded(mc), mc.entityRenderer != null && mc.entityRenderer.isShaderActive()); + mc.mcProfiler.endSection(); + post(ElementType.DEBUG); + } + } else { + ran = DebugScreen.isOverlayVisible() && !left.isEmpty(); + } + if (DebugScreen.isOverlayIdle()) return; + DebugScreenOverlay.INSTANCE.afterDebugBlock(ran, skipped); + } + + @Unique + private static boolean angelica$chunkLoaded(Minecraft mc) { + if (mc.theWorld == null) return false; + final int x = MathHelper.floor_double(mc.thePlayer.posX); + final int y = MathHelper.floor_double(mc.thePlayer.posY); + final int z = MathHelper.floor_double(mc.thePlayer.posZ); + return mc.theWorld.blockExists(x, y, z); + } + + @ModifyExpressionValue( + method = "renderHUDText", + remap = false, + require = 1, + at = @At( + value = "INVOKE", + target = "Lcpw/mods/fml/common/eventhandler/EventBus;post(Lcpw/mods/fml/common/eventhandler/Event;)Z", + remap = false)) + private boolean angelica$drawDebugScreen(boolean canceled, @Local RenderGameOverlayEvent.Text event, @Local(argsOnly = true, ordinal = 0) int width, @Local(argsOnly = true, ordinal = 1) int height) { + if (DebugScreen.isOverlayIdle()) return canceled; + return DebugScreenOverlay.INSTANCE.onTextEvent(event, fontrenderer, width, height, canceled); + } @Redirect( method = "renderHUDText", diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/botania/MixinDebugHandler.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/botania/MixinDebugHandler.java new file mode 100644 index 000000000..00d6822ae --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/botania/MixinDebugHandler.java @@ -0,0 +1,25 @@ +package com.gtnewhorizons.angelica.mixins.late.client.botania; + +import com.gtnewhorizons.angelica.compat.debug.BotaniaDebugEntry; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Pseudo +@Mixin(targets = "vazkii.botania.client.core.handler.DebugHandler", remap = false) +public class MixinDebugHandler { + + @Inject( + method = "onDrawDebugText(Lnet/minecraftforge/client/event/RenderGameOverlayEvent$Text;)V", + at = @At("HEAD"), + cancellable = true, + remap = false, + require = 0, + expect = 0) + private void angelica$skipLegacyDebugText(RenderGameOverlayEvent.Text event, CallbackInfo ci) { + if (BotaniaDebugEntry.active()) ci.cancel(); + } +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/hodgepodge/MixinChunkGenDebugHandler.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/hodgepodge/MixinChunkGenDebugHandler.java new file mode 100644 index 000000000..060ab1090 --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/hodgepodge/MixinChunkGenDebugHandler.java @@ -0,0 +1,25 @@ +package com.gtnewhorizons.angelica.mixins.late.client.hodgepodge; + +import com.gtnewhorizons.angelica.compat.debug.HodgepodgeDebugEntry; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Pseudo +@Mixin(targets = "com.mitchej123.hodgepodge.client.ChunkGenDebugHandler", remap = false) +public class MixinChunkGenDebugHandler { + + @Inject( + method = "onRenderGameOverlayText(Lnet/minecraftforge/client/event/RenderGameOverlayEvent$Text;)V", + at = @At("HEAD"), + cancellable = true, + remap = false, + require = 0, + expect = 0) + private void angelica$skipLegacyDebugText(RenderGameOverlayEvent.Text event, CallbackInfo ci) { + if (HodgepodgeDebugEntry.active()) ci.cancel(); + } +} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/journeymap/MixinMiniMapOverlayHandler.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/journeymap/MixinMiniMapOverlayHandler.java new file mode 100644 index 000000000..b029b4c25 --- /dev/null +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/late/client/journeymap/MixinMiniMapOverlayHandler.java @@ -0,0 +1,25 @@ +package com.gtnewhorizons.angelica.mixins.late.client.journeymap; + +import com.gtnewhorizons.angelica.compat.debug.JourneyMapDebugEntry; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Pseudo +@Mixin(targets = "journeymap.client.forge.event.MiniMapOverlayHandler", remap = false) +public class MixinMiniMapOverlayHandler { + + @Inject( + method = "onRenderOverlayDebug(Lnet/minecraftforge/client/event/RenderGameOverlayEvent$Text;)V", + at = @At("HEAD"), + cancellable = true, + remap = false, + require = 0, + expect = 0) + private void angelica$skipLegacyDebugText(RenderGameOverlayEvent.Text event, CallbackInfo ci) { + if (JourneyMapDebugEntry.active()) ci.cancel(); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/api/debug/DebugFactLinesTest.java b/src/test/java/com/gtnewhorizons/angelica/api/debug/DebugFactLinesTest.java new file mode 100644 index 000000000..7bcffe7e4 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/api/debug/DebugFactLinesTest.java @@ -0,0 +1,94 @@ +package com.gtnewhorizons.angelica.api.debug; + +import com.gtnewhorizons.angelica.debug.screen.RecordingDisplayer; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class DebugFactLinesTest { + + private static RecordingDisplayer record(DebugFactLines facts) { + return RecordingDisplayer.of(facts); + } + + private static RecordingDisplayer split(DebugFactLines facts, String line) { + facts.begin(); + facts.split(line); + facts.end(); + return record(facts); + } + + @ParameterizedTest + @CsvSource(delimiter = '|', value = { + "Chunk Workers: 0/10 busy|Chunk Workers|0/10 busy", + "cutout_mipped - 26 sections, 5.042 us|cutout_mipped|26 sections, 5.042 us", + "abcdefghijklmnopqrstuvwx: x|abcdefghijklmnopqrstuvwx|x", + "U=00 (P0=000): x|PLAIN|U=00 (P0=000): x", + "abcdefghijklmnopqrstuvwxy: x|PLAIN|abcdefghijklmnopqrstuvwxy: x", + "Shadow Pass|PLAIN|Shadow Pass", + ": x|PLAIN|: x" }) + void splitsLinesIntoFactsOrLeavesThemPlain(String line, String name, String value) { + final RecordingDisplayer recorder = split(new DebugFactLines(), line); + assertEquals(name.equals("PLAIN") ? Collections.singletonList(null) : List.of(name), recorder.names); + assertEquals(value, recorder.text(0)); + } + + @Test + void equalSourceReusesInstancesAndChangedSourceResplits() { + final DebugFactLines facts = new DebugFactLines(); + final String[] values = Reflect.get(facts, "values"); + final String[] names = Reflect.get(facts, "names"); + final RecordingDisplayer first = split(facts, "Queue: 12"); + final String firstValue = values[0]; + final String firstName = names[0]; + final RecordingDisplayer same = split(facts, new String("Queue: 12")); + assertSame(first.names.get(0), same.names.get(0)); + assertSame(firstValue, values[0]); + final RecordingDisplayer changed = split(facts, "Queue: 13"); + assertEquals("13", changed.text(0)); + assertNotSame(firstValue, values[0]); + assertSame(firstName, names[0]); + } + + @Test + void endTruncatesStaleSlotsAndFactAfterSplitReplacesSlot() { + final DebugFactLines facts = new DebugFactLines(); + facts.begin(); + facts.fact("A", "1"); + facts.fact("B", "2"); + facts.end(); + facts.begin(); + facts.fact("A", "1"); + facts.end(); + assertEquals(List.of("A"), record(facts).names); + split(facts, "Queue: 12"); + facts.begin(); + facts.fact("Other", "x"); + facts.end(); + assertEquals(List.of("Other"), record(facts).names); + } + + @Test + void misuseThrowsAtTheCall() { + final DebugFactLines facts = new DebugFactLines(); + assertThrows(IllegalStateException.class, () -> facts.fact("A", "1")); + assertThrows(IllegalStateException.class, () -> facts.split("A: 1")); + facts.begin(); + facts.fact("A", "1"); + assertThrows(IllegalArgumentException.class, () -> facts.fact("B", null)); + assertThrows(IllegalArgumentException.class, () -> facts.split(null)); + assertThrows(IllegalStateException.class, () -> record(facts)); + facts.end(); + assertThrows(IllegalStateException.class, () -> facts.fact("C", "3")); + assertEquals(List.of("A"), record(facts).names); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistryTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistryTest.java index eed28baaa..956a352ca 100644 --- a/src/test/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistryTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/FontEffectRegistryTest.java @@ -37,7 +37,7 @@ class FontEffectRegistryTest { assertFalse(FontEffectRegistry.register('s', NO_OP), "duplicate registration accepted"); assertTrue(FontEffectRegistry.isRegistered('s')); assertTrue(FontEffectRegistry.hasAny()); - assertSame(effect, FontEffectRegistry.get(Long.numberOfTrailingZeros(FontEffectRegistry.bit('s')))); + assertSame(effect, FontEffectRegistry.get('s' - 'a')); } @Test diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderCacheTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderCacheTest.java new file mode 100644 index 000000000..1304770fc --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderCacheTest.java @@ -0,0 +1,93 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizons.angelica.config.FontConfig; +import com.gtnewhorizons.angelica.glsm.GLCoreTest; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.awt.Font; +import java.nio.file.Path; +import java.util.concurrent.CompletableFuture; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +@GLCoreTest +class FontProviderCacheTest { + + private static FontTestFixture fixture; + private static BatchingFontRenderer renderer; + private static Font primaryBefore; + private static Font fallbackBefore; + + @TempDir + static Path tempDir; + + @BeforeAll + static void setUp() throws Exception { + fixture = new FontTestFixture(tempDir); + renderer = mock(BatchingFontRenderer.class); + primaryBefore = Reflect.get(FontProviderCustom.getPrimary(), "font"); + fallbackBefore = Reflect.get(FontProviderCustom.getFallback(), "font"); + FontProviderCustom.getPrimary().setFont(new Font(Font.SERIF, Font.PLAIN, 16) { + @Override + public boolean canDisplay(char c) { + return (c < '0' || c > '9') && super.canDisplay(c); + } + }); + FontProviderCustom.getFallback().setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16)); + } + + @AfterAll + static void tearDown() { + FontProviderCustom.getPrimary().setFont(primaryBefore); + FontProviderCustom.getFallback().setFont(fallbackBefore); + FontProviderCache.invalidate(); + fixture.close(); + } + + @ParameterizedTest(name = "custom={0} unicode={1} modern={2}") + @CsvSource({ + "false, false, true", "false, true, true", "false, false, false", "false, true, false", + "true, false, true", "true, true, true", "true, false, false", "true, true, false" }) + void cacheMatchesTheUncachedSearchAcrossTheBmp(boolean custom, boolean unicode, boolean modern) { + FontConfig.enableModernFont = modern; + FontProviderCache.invalidate(); + int primary = 0; + int fallback = 0; + for (int c = 0; c <= Character.MAX_VALUE; c++) { + final char chr = (char) c; + final FontProvider expected = FontStrategist.searchFontProvider(renderer, chr, custom, unicode); + if (expected == FontProviderCustom.getPrimary()) primary++; + if (expected == FontProviderCustom.getFallback()) fallback++; + assertSame(expected, FontProviderCache.lookup(chr, custom, unicode), "U+" + Integer.toHexString(c)); + assertSame(expected, FontStrategist.getFontProvider(renderer, chr, custom, unicode), "resolved U+" + Integer.toHexString(c)); + } + assertTrue(!custom || primary > 0, "no glyph resolved to the primary custom font"); + assertTrue(!custom || fallback > 0, "no glyph resolved to the fallback custom font"); + } + + @Test + void offMainThreadLookupsNeverReturnTheModernFont() throws Exception { + FontConfig.enableModernFont = true; + FontProviderCache.invalidate(); + char bitmapChar = 0; + for (int c = 1; c <= Character.MAX_VALUE && bitmapChar == 0; c++) { + if (FontStrategist.searchFontProvider(renderer, (char) c, false, false) instanceof FontProviderBitmap) bitmapChar = (char) c; + } + assertNotEquals(0, bitmapChar, "no glyph resolves to a modern font sheet"); + final char chr = bitmapChar; + final FontProvider offThread = CompletableFuture.supplyAsync(() -> FontStrategist.getFontProvider(renderer, chr, false, false)).get(); + assertNotNull(offThread); + assertFalse(offThread instanceof FontProviderBitmap, "U+" + Integer.toHexString(chr) + " resolved to " + offThread); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderMCTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderMCTest.java index b154cff29..0dc3ebe8c 100644 --- a/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderMCTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/FontProviderMCTest.java @@ -1,5 +1,7 @@ package com.gtnewhorizons.angelica.client.font; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import it.unimi.dsi.fastutil.chars.Char2ShortOpenHashMap; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -59,4 +61,19 @@ class FontProviderMCTest { void charactersOutsideTheFontAreLeftAlone() { assertEquals('Я', font.getRandomReplacement('Я')); } + + @Test + void slotTableMatchesTheCharMap() { + final String chars = Reflect.getStatic(FontProviderMC.class, "MCFONT_CHARS"); + final Char2ShortOpenHashMap map = new Char2ShortOpenHashMap(); + map.defaultReturnValue((short) -1); + for (short i = 0; i < chars.length(); i++) { + map.put(chars.charAt(i), i); + } + for (int c = 0; c <= Character.MAX_VALUE; c++) { + final char chr = (char) c; + assertEquals(map.get(chr), font.lookupMcFontPosition(chr), "slot of U+" + Integer.toHexString(c)); + assertEquals(map.containsKey(chr), font.isGlyphAvailable(chr), "availability of U+" + Integer.toHexString(c)); + } + } } diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/FontTestFixture.java b/src/test/java/com/gtnewhorizons/angelica/client/font/FontTestFixture.java new file mode 100644 index 000000000..1307b9c9b --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/FontTestFixture.java @@ -0,0 +1,147 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizon.gtnhlib.util.font.FontRendering; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.config.FontConfig; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.glsm.testutil.TestPaths; +import com.gtnewhorizons.angelica.mixins.interfaces.FontRendererAccessor; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.resources.DefaultResourcePack; +import net.minecraft.client.resources.IReloadableResourceManager; +import net.minecraft.client.resources.IResource; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class FontTestFixture implements AutoCloseable { + + public static final class TestFont extends FontRenderer implements FontRendererAccessor { + + public BatchingFontRenderer batcher; + int texture; + + public TestFont() { + super(null, null, null, false); + } + + @Override + public int angelica$drawStringBatched(String text, int x, int y, int argb, boolean dropShadow) { + return 0; + } + + @Override + public BatchingFontRenderer angelica$getBatcher() { + return batcher; + } + + @Override + public void angelica$bindTexture(ResourceLocation location) { + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); + } + } + + public static final class HexResets implements FontRendering.TextPreprocessor { + + @Override + public String apply(String s) { + return s; + } + + public boolean hexColorResetsStyles() { + return true; + } + } + + public static final ResourceLocation ASCII = new ResourceLocation("textures/font/ascii.png"); + + public final TestFont font = Reflect.allocate(TestFont.class); + public final Minecraft minecraft = mock(Minecraft.class); + private final List configFields = new ArrayList<>(); + private final List configValues = new ArrayList<>(); + private final Object preprocessor = Reflect.getStatic(FontRendering.class, "textPreprocessor"); + private final Minecraft savedMinecraft = Reflect.getStatic(Minecraft.class, "theMinecraft"); + + public FontTestFixture(Path dir) throws IOException { + for (Class config : new Class[] { FontConfig.class, AngelicaConfig.class }) { + for (Field field : config.getFields()) { + if (!Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) continue; + configFields.add(field); + configValues.add(Reflect.getStatic(config, field.getName())); + } + } + FontConfig.enableCustomFont = false; + FontConfig.enableModernFont = false; + FontConfig.enableGlyphReplacements = false; + FontConfig.fontAAMode = 0; + FontConfig.glyphScale = 1.0f; + FontConfig.glyphAspect = 0.0f; + AngelicaConfig.enableRGBColors = true; + FontRendering.setTextPreprocessor(null); + + final byte[] glyphSizes = new byte[65536]; + for (int i = 0; i < glyphSizes.length; i++) glyphSizes[i] = (byte) (0x07 + (i % 5) * 0x11); + final IReloadableResourceManager resources = mock(IReloadableResourceManager.class); + when(resources.getResourceDomains()).thenReturn(Set.of("angelica")); + when(resources.getResource(any(ResourceLocation.class))).thenAnswer(invocation -> { + final String path = invocation.getArgument(0).getResourcePath(); + final byte[] bytes = path.equals("font/glyph_sizes.bin") ? glyphSizes : Files.readAllBytes(TestPaths.resolve("src/main/resources/assets/angelica/" + path)); + final IResource resource = mock(IResource.class); + when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(bytes)); + return resource; + }); + Reflect.set(minecraft, "mcDataDir", Files.createDirectories(dir.resolve("mc")).toFile()); + Reflect.set(minecraft, "mcDefaultResourcePack", mock(DefaultResourcePack.class)); + when(minecraft.getResourceManager()).thenReturn(resources); + Reflect.setStatic(Minecraft.class, "theMinecraft", minecraft); + font.FONT_HEIGHT = 9; + } + + public static int[] charWidths() { + final int[] widths = new int[256]; + for (int i = 0; i < widths.length; i++) widths[i] = 4 + i % 5; + return widths; + } + + public BatchingFontRenderer batcher() { + if (font.batcher == null) font.batcher = new BatchingFontRenderer(font, charWidths(), new int[32], ASCII); + if (font.texture == 0) { + font.texture = GL11.glGenTextures(); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, font.texture); + final ByteBuffer pixels = BufferUtils.createByteBuffer(4 * 4 * 4); + while (pixels.hasRemaining()) pixels.put((byte) 0xFF); + pixels.flip(); + GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 4, 4, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + } + return font.batcher; + } + + @Override + public void close() { + if (font.batcher != null) font.batcher.resetAfterCrash(); + if (font.texture != 0) GL11.glDeleteTextures(font.texture); + Reflect.setStatic(Minecraft.class, "theMinecraft", savedMinecraft); + for (int i = 0; i < configFields.size(); i++) Reflect.setStatic(configFields.get(i).getDeclaringClass(), configFields.get(i).getName(), configValues.get(i)); + Reflect.setStatic(FontRendering.class, "textPreprocessor", preprocessor); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/FormatCodesTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/FormatCodesTest.java new file mode 100644 index 000000000..f1a088fe1 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/FormatCodesTest.java @@ -0,0 +1,204 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizon.gtnhlib.util.font.FontRendering; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.config.FontConfig; +import com.gtnewhorizons.angelica.glsm.GLCoreTest; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@GLCoreTest +class FormatCodesTest { + + private static final String HEX1 = "&x&1&2&3&4&5&6"; + private static final String HEX2 = "&x&a&b&c&d&e&f"; + private static final String BAD_HEX = "&x&g&g&1&1&1&1"; + private static final char CUSTOM_CODE = 'h'; + private static final int[] SCRAMBLED_WIDTHS = new int[256]; + + static { + Arrays.fill(SCRAMBLED_WIDTHS, 9); + } + + private static FontTestFixture fixture; + private static BatchingFontRenderer batcher; + + @TempDir + static Path tempDir; + + @BeforeAll + static void setUp() throws Exception { + fixture = new FontTestFixture(tempDir); + FontConfig.glyphSpacing = 0.5f; + FontConfig.whitespaceScale = 1.25f; + batcher = fixture.batcher(); + if (!FontEffectRegistry.isRegistered(CUSTOM_CODE)) assertTrue(FontEffectRegistry.register(CUSTOM_CODE, (argb, shadowPass, glyphIndex) -> argb)); + } + + @AfterAll + static void tearDown() { + fixture.close(); + } + + @AfterEach + void reset() { + AngelicaConfig.enableRGBColors = true; + AngelicaConfig.enableRainbow = true; + AngelicaConfig.enableWaveText = true; + AngelicaConfig.enableDinnerboneText = true; + AngelicaConfig.enableDropShadow = true; + AngelicaConfig.enableGradients = true; + FontRendering.setTextPreprocessor(null); + } + + private static String codes(String s) { + return s.replace('&', '\u00a7'); + } + + private static long value(long code) { + return switch (FormatCodes.kind(code)) { + case FormatCodes.COLOR -> FormatCodes.colorIndex(code); + case FormatCodes.HEX, FormatCodes.SHADOW_COLOR -> FormatCodes.rgb(code); + case FormatCodes.GRADIENT -> (long) FormatCodes.gradientFrom(code) << 24 | FormatCodes.gradientTo(code); + case FormatCodes.CUSTOM -> FormatCodes.customBit(code); + default -> 0L; + }; + } + + @ParameterizedTest(name = "{0}") + @CsvSource(delimiter = '|', value = { + HEX1 + " | HEX | 12 | 0x123456 | rgb", + "&X&A&B&C&D&E&F | HEX | 12 | 0xABCDEF | rgb", + BAD_HEX + " | NONE | 0 | 0 |", + "&x&1&2&3&4&5 | NONE | 0 | 0 |", + "&x&h&1&2&3&4&5 | NONE | 0 | 0 |", + "&h | CUSTOM | 0 | 0x80 |", + "&H | CUSTOM | 0 | 0x80 |", + "&y | NONE | 0 | 0 |", + "&9 | COLOR | 0 | 9 |", + "&a | COLOR | 0 | 10 |", + "&F | COLOR | 0 | 15 |", + "&k | OBFUSCATED | 0 | 0 |", + "&L | BOLD | 0 | 0 |", + "&m | STRIKETHROUGH | 0 | 0 |", + "&n | UNDERLINE | 0 | 0 |", + "&o | ITALIC | 0 | 0 |", + "&r | RESET | 0 | 0 |", + "&& | NONE | 0 | 0 |", + "&\u4e2d | NONE | 0 | 0 |", + "&q | RAINBOW | 0 | 0 | rainbow", + "&z | WAVE | 0 | 0 | wave", + "&v | DINNERBONE | 0 | 0 | dinnerbone", + "&uabc | SHADOW_TOGGLE | 0 | 0 | shadow", + "&u" + HEX1 + " | SHADOW_COLOR | 14 | 0x123456 | shadow", + "&u" + BAD_HEX + "| SHADOW_TOGGLE | 0 | 0 |", + "&g" + HEX1 + HEX2 + "x | GRADIENT | 28 | 0x123456ABCDEF | gradient", + "&g" + HEX1 + BAD_HEX + " | NONE | 0 | 0 |", + "&g" + BAD_HEX + HEX2 + " | NONE | 0 | 0 |", + "&g | NONE | 0 | 0 |" }) + void parse(String input, String kind, int extra, String value, String disabledBy) throws ReflectiveOperationException { + final String s = codes(input); + final long code = FormatCodes.parse(s, 1, s.length()); + assertEquals(FormatCodes.class.getField(kind).getInt(null), FormatCodes.kind(code), "kind"); + assertEquals(extra, FormatCodes.extraChars(code), "extra chars"); + assertEquals(Long.decode(value), value(code), "payload"); + if (disabledBy == null) return; + switch (disabledBy) { + case "rgb" -> AngelicaConfig.enableRGBColors = false; + case "rainbow" -> AngelicaConfig.enableRainbow = false; + case "wave" -> AngelicaConfig.enableWaveText = false; + case "dinnerbone" -> AngelicaConfig.enableDinnerboneText = false; + case "shadow" -> AngelicaConfig.enableDropShadow = false; + default -> AngelicaConfig.enableGradients = false; + } + assertEquals(FormatCodes.NONE, FormatCodes.kind(FormatCodes.parse(s, 1, s.length())), "kind with " + disabledBy + " off"); + } + + @ParameterizedTest(name = "{0}") + @CsvSource(delimiter = '|', value = { + "ab&u" + HEX1 + "cd | true | 4", + "ab&qcd | false | 4", + "ab&qcd | true | 2", + "ab&ncd | true | 4", + "ab" + HEX1 + "cd | true | 2", + "ab&acd | true | 2", + "ab&rcd | true | 2" }) + void countVisibleCharsStopsOnlyOnColorTerminators(String input, boolean rainbow, int expected) { + AngelicaConfig.enableRainbow = rainbow; + final String s = codes(input); + final int count = Reflect.invokeStatic(BatchingFontRenderer.class, "countVisibleChars", new Class[] { CharSequence.class, int.class, int.class }, s, 0, s.length()); + assertEquals(expected, count); + } + + private static final String[] PIECES = { + "abc", "XYZ", "019", " ", " ", "\u00a0", "\u202f", "\u4e2d\u6587", "\u00c4", String.valueOf(ColorCodeUtils.ESCAPED_AMPERSAND), + "&l", "&L", "&r", "&a", "&F", "&k", "&m", "&n", "&o", "&q", "&z", "&v", "&u", "&?", "&" + CUSTOM_CODE, "&y", + HEX1, HEX2, BAD_HEX, "&u" + HEX1, "&g" + HEX1 + HEX2, "&g" + HEX1 + BAD_HEX, "&x&1", "&" }; + + private static float drawn(String s, int start, int end, boolean unicode) { + batcher.beginBatch(); + try { + return batcher.drawString(0.0f, 0.0f, 0xFFFFFFFF, false, unicode, s, start, end - start); + } finally { + batcher.resetAfterCrash(); + } + } + + private static void scramble() { + FontProviderMC.get(false).charWidth = SCRAMBLED_WIDTHS; + } + + private static float measured(String s, int start, int end, boolean unicode) { + scramble(); + return batcher.measure(s, start, end, unicode); + } + + private static float advanced(char c, boolean bold, boolean unicode) { + scramble(); + return batcher.advance(c, bold, unicode); + } + + private static float provided(char c, boolean unicode) { + scramble(); + return batcher.glyphAdvance(batcher.glyphProvider(c, unicode), c, false, batcher.getGlyphScaleX()); + } + + @ParameterizedTest(name = "unicode={0} hexResets={1}") + @CsvSource({ "false, false", "true, false", "false, true", "true, true" }) + void measureAndAdvanceMatchTheDrawnPen(boolean unicode, boolean hexResets) { + if (hexResets) FontRendering.setTextPreprocessor(new FontTestFixture.HexResets()); + final Random random = new Random(0x5EED); + int positive = 0; + for (int i = 0; i < 200; i++) { + final StringBuilder sb = new StringBuilder(); + for (int p = 1 + random.nextInt(12); p > 0; p--) sb.append(PIECES[random.nextInt(PIECES.length)]); + final String s = codes(sb.toString()); + final float pen = drawn(s, 0, s.length(), unicode); + if (pen > 0.0f) positive++; + assertEquals(pen, measured(s, 0, s.length(), unicode), 0.0f, s); + final int start = random.nextInt(s.length()); + final int end = start + 1 + random.nextInt(s.length() - start); + assertEquals(drawn(s, start, end, unicode), measured(s, start, end, unicode), 0.0f, s + " [" + start + ", " + end + ")"); + } + assertTrue(positive > 150, "corpus is mostly zero-width: " + positive); + for (char c : "aZ0 \u00a0\u4e2d\u00a7".toCharArray()) { + assertEquals(drawn(String.valueOf(c), 0, 1, unicode), advanced(c, false, unicode), 0.0f, "'" + c + "'"); + assertEquals(drawn("\u00a7l" + c, 0, 3, unicode), advanced(c, true, unicode), 0.0f, "bold '" + c + "'"); + } + for (char c : "aZ0\u4e2d".toCharArray()) { + assertEquals(drawn(String.valueOf(c), 0, 1, unicode), provided(c, unicode), 0.0f, "provider '" + c + "'"); + } + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/GlyphSettingsTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/GlyphSettingsTest.java new file mode 100644 index 000000000..b4a74fbeb --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/GlyphSettingsTest.java @@ -0,0 +1,97 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizon.gtnhlib.util.font.FontRendering; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.config.FontConfig; +import com.gtnewhorizons.angelica.hudcaching.HUDCaching; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.nio.file.Path; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; + +class GlyphSettingsTest { + + private static boolean cacheOverride; + private static FontTestFixture fixture; + + @TempDir + static Path dir; + + @BeforeAll + static void setUp() throws Exception { + cacheOverride = HUDCaching.renderingCacheOverride; + fixture = new FontTestFixture(dir); + } + + @AfterAll + static void tearDown() { + fixture.close(); + HUDCaching.renderingCacheOverride = cacheOverride; + } + + private static Arguments input(String name, boolean metric, Runnable change) { + return Arguments.of(Named.of(name, change), metric); + } + + static Stream inputs() { + return Stream.of( + input("generation", true, FontProviderCache::invalidate), + input("fontAAMode", false, () -> FontConfig.fontAAMode++), + input("fontAAStrength", false, () -> FontConfig.fontAAStrength++), + input("shadowCopies", false, () -> FontConfig.shadowCopies++), + input("boldCopies", false, () -> FontConfig.boldCopies++), + input("fontShadowOffset", false, () -> FontConfig.fontShadowOffset += 0.5f), + input("fontShadowOffsetUC", false, () -> FontConfig.fontShadowOffsetUC += 0.5f), + input("glyphScale", true, () -> FontConfig.glyphScale += 0.5f), + input("glyphAspect", true, () -> FontConfig.glyphAspect += 0.5f), + input("glyphSpacing", true, () -> FontConfig.glyphSpacing += 0.5f), + input("whitespaceScale", true, () -> FontConfig.whitespaceScale += 0.5f), + input("customFontScale", true, () -> FontConfig.customFontScale += 0.5f), + input("waveAmplitude", false, () -> AngelicaConfig.waveAmplitude += 0.5f), + input("enableCustomFont", true, () -> FontConfig.enableCustomFont ^= true), + input("enableModernFont", true, () -> FontConfig.enableModernFont ^= true), + input("enableGlyphReplacements", true, () -> FontConfig.enableGlyphReplacements ^= true), + input("enableRGBColors", true, () -> AngelicaConfig.enableRGBColors ^= true), + input("enableGradients", true, () -> AngelicaConfig.enableGradients ^= true), + input("enableRainbow", false, () -> AngelicaConfig.enableRainbow ^= true), + input("enableWaveText", false, () -> AngelicaConfig.enableWaveText ^= true), + input("enableDinnerboneText", false, () -> AngelicaConfig.enableDinnerboneText ^= true), + input("enableDropShadow", true, () -> AngelicaConfig.enableDropShadow ^= true), + input("hexColorResetsStyles", true, () -> FontRendering.setTextPreprocessor(new FontTestFixture.HexResets())), + input("renderingCacheOverride", false, () -> HUDCaching.renderingCacheOverride ^= true)); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("inputs") + void everyInputIsCaptured(Runnable change, boolean metric) { + final GlyphSettings before = GlyphSettings.current(); + assertSame(before, GlyphSettings.current()); + change.run(); + final GlyphSettings after = GlyphSettings.current(); + assertNotEquals(before, after, "not captured"); + assertEquals(metric, !before.sameMetricsAs(after), "metric relevance"); + } + + @Test + void eachCacheOverrideStateKeepsItsInstance() { + final GlyphSettings outside = GlyphSettings.current(); + HUDCaching.renderingCacheOverride ^= true; + final GlyphSettings inside = GlyphSettings.current(); + HUDCaching.renderingCacheOverride ^= true; + assertSame(outside, GlyphSettings.current()); + HUDCaching.renderingCacheOverride ^= true; + assertSame(inside, GlyphSettings.current()); + HUDCaching.renderingCacheOverride ^= true; + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/QuadRegionAllocatorTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/QuadRegionAllocatorTest.java new file mode 100644 index 000000000..4911c5d5d --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/QuadRegionAllocatorTest.java @@ -0,0 +1,69 @@ +package com.gtnewhorizons.angelica.client.font; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static com.gtnewhorizons.angelica.glsm.backend.RenderBackend.MAX_FRAMES_IN_FLIGHT; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class QuadRegionAllocatorTest { + + @Test + void allocatesTheFirstHoleThatFits() { + final QuadRegionAllocator allocator = new QuadRegionAllocator(16); + assertEquals(0, allocator.allocate(4)); + assertEquals(4, allocator.allocate(4)); + assertEquals(8, allocator.allocate(4)); + allocator.retire(4, 4, 0); + allocator.advance(MAX_FRAMES_IN_FLIGHT); + assertEquals(4, allocator.allocate(2)); + assertEquals(12, allocator.allocate(4)); + assertEquals(6, allocator.allocate(2)); + assertEquals(-1, allocator.allocate(1)); + } + + @ParameterizedTest(name = "retire order {0}") + @ValueSource(strings = { "012", "021", "102", "120", "201", "210" }) + void reclaimedNeighborsCoalesce(String order) { + final QuadRegionAllocator allocator = new QuadRegionAllocator(12); + for (int i = 0; i < 3; i++) assertEquals(4 * i, allocator.allocate(4)); + for (int i = 0; i < 3; i++) { + allocator.retire(4 * (order.charAt(i) - '0'), 4, i); + allocator.advance(i + MAX_FRAMES_IN_FLIGHT); + } + assertEquals(0, allocator.allocate(12)); + } + + @Test + void retiredRegionsWaitForTheFramesInFlight() { + final QuadRegionAllocator allocator = new QuadRegionAllocator(4); + assertEquals(0, allocator.allocate(4)); + allocator.retire(0, 4, 10); + for (int frame = 10; frame < 10 + MAX_FRAMES_IN_FLIGHT; frame++) { + allocator.advance(frame); + assertEquals(-1, allocator.allocate(4), "frame " + frame); + } + allocator.advance(10 + MAX_FRAMES_IN_FLIGHT); + assertEquals(0, allocator.allocate(4)); + } + + @Test + void aFullAllocatorRefuses() { + final QuadRegionAllocator allocator = new QuadRegionAllocator(8); + assertEquals(-1, allocator.allocate(9)); + assertEquals(0, allocator.allocate(8)); + assertEquals(-1, allocator.allocate(1)); + } + + @Test + void resetRestoresTheWholeCapacityAndDropsRetiredRegions() { + final QuadRegionAllocator allocator = new QuadRegionAllocator(8); + assertEquals(0, allocator.allocate(4)); + allocator.retire(0, 4, 0); + allocator.reset(32); + assertEquals(0, allocator.allocate(32)); + allocator.advance(MAX_FRAMES_IN_FLIGHT); + assertEquals(-1, allocator.allocate(1)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/TextDrawStateGLTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/TextDrawStateGLTest.java index d1fc39ae8..e6cbd9610 100644 --- a/src/test/java/com/gtnewhorizons/angelica/client/font/TextDrawStateGLTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/TextDrawStateGLTest.java @@ -59,7 +59,7 @@ class TextDrawStateGLTest { private static TextDrawState capture() { final TextDrawState state = new TextDrawState(); - state.captureLive(); + state.captureLive(GLStateManager.ctx()); return state; } diff --git a/src/test/java/com/gtnewhorizons/angelica/client/font/TextMeshGLTest.java b/src/test/java/com/gtnewhorizons/angelica/client/font/TextMeshGLTest.java new file mode 100644 index 000000000..b10fbed00 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/client/font/TextMeshGLTest.java @@ -0,0 +1,368 @@ +package com.gtnewhorizons.angelica.client.font; + +import com.gtnewhorizons.angelica.config.FontConfig; +import com.gtnewhorizons.angelica.glsm.GLCoreTest; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.RenderBackend; +import com.gtnewhorizons.angelica.glsm.ffp.FFPVertexLighting; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.joml.Vector3f; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; + +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@GLCoreTest +class TextMeshGLTest { + + private static final String[] LINES = { "Hello, world! 0123456789", "\u00a7lBold \u00a7nunder\u00a7r plain \u00a7mstrike\u00a7r", "x: 1.00000 (1) // c: 0 (1)", "\u00a7x\u00a7f\u00a7f\u00a70\u00a70\u00a7a\u00a7argb \u4e2d\u6587 mixed" }; + private static final int VIEW_W = 256; + private static final int VIEW_H = 64; + + private static final List meshes = new ArrayList<>(); + private static FontTestFixture fixture; + private static BatchingFontRenderer batcher; + private static BatchingFontRenderer other; + private static boolean shadow; + + @TempDir + static Path tempDir; + + @BeforeAll + static void setUp() throws Exception { + fixture = new FontTestFixture(tempDir); + FontConfig.shadowCopies = 1; + FontConfig.boldCopies = 2; + batcher = fixture.batcher(); + other = new BatchingFontRenderer(fixture.font, FontTestFixture.charWidths(), new int[32], FontTestFixture.ASCII); + } + + @AfterAll + static void tearDown() { + fixture.close(); + } + + @BeforeEach + void projection() { + shadow = false; + GLStateManager.glViewport(0, 0, VIEW_W, VIEW_H); + GLStateManager.glMatrixMode(GL11.GL_PROJECTION); + GLStateManager.glLoadIdentity(); + GLStateManager.glOrtho(0.0, VIEW_W, VIEW_H, 0.0, -1.0, 1.0); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glLoadIdentity(); + } + + @AfterEach + void reset() { + batcher.resetAfterCrash(); + other.resetAfterCrash(); + meshes.forEach(TextMesh::free); + meshes.clear(); + endFrames(); + BatchingFontRenderer.exitButtonSection(false); + BatchingFontRenderer.exitRecolorSection(false); + GLStateManager.glMatrixMode(GL11.GL_PROJECTION); + GLStateManager.glLoadIdentity(); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glLoadIdentity(); + GLStateManager.disableDepthTest(); + GLStateManager.disableLighting(); + GLStateManager.disableColorMaterial(); + GLStateManager.glColorMask(true, true, true, true); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + } + + private static void draw(String s, float y) { + batcher.drawString(2.0f, y, 0xFFE0E0E0, shadow, false, s, 0, s.length()); + } + + private static void drawLines() { + batcher.fillRect(0.0f, 40.0f, 100.0f, 50.0f, 0xFF00FF00); + for (int i = 0; i < LINES.length; i++) draw(LINES[i], 2.0f + 9.0f * i); + } + + private static void drawOther(String s, float y) { + other.drawString(2.0f, y, 0xFFE0E0E0, false, false, s, 0, s.length()); + } + + private static void endFrames() { + for (int i = 0; i < RenderBackend.MAX_FRAMES_IN_FLIGHT; i++) BatchingFontRenderer.endFrame(); + } + + private static TextMesh mesh() { + final TextMesh mesh = new TextMesh(); + meshes.add(mesh); + return mesh; + } + + private static TextMesh capture(Runnable body) { + final TextMesh mesh = mesh(); + batcher.beginBatch(); + batcher.beginCapture(mesh); + body.run(); + batcher.endCapture(); + batcher.resetAfterCrash(); + return mesh; + } + + private static byte[] frame(Runnable body) { + GLStateManager.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + GLStateManager.glClear(GL11.GL_COLOR_BUFFER_BIT); + body.run(); + final ByteBuffer pixels = BufferUtils.createByteBuffer(VIEW_W * VIEW_H * 4); + GL11.glReadPixels(0, 0, VIEW_W, VIEW_H, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels); + final byte[] out = new byte[pixels.capacity()]; + pixels.get(out); + return out; + } + + private static List contents(TextMesh mesh) { + final int n = mesh.commands; + final int quadBytes = Reflect.getStatic(BatchingFontRenderer.class, "QUAD_BYTES"); + final ByteBuffer vertices = mesh.vertices.duplicate(); + vertices.limit(mesh.quadCount * quadBytes); + return List.of(vertices, mesh.quadCount, Arrays.toString(Arrays.copyOf(mesh.commandStart, n)), Arrays.toString(Arrays.copyOf(mesh.commandCount, n)), + Arrays.toString(Arrays.copyOf(mesh.commandTexture, n)), Arrays.toString(Arrays.copyOf(mesh.commandLayer, n)), Arrays.toString(Arrays.copyOf(mesh.commandUnicode, n))); + } + + @Test + void replayEmitsTheCapturedBytesWhereverItLands() { + final TextMesh direct = capture(TextMeshGLTest::drawLines); + assertTrue(direct.isValid(GlyphSettings.current())); + batcher.beginBatch(); + draw("prefix line", 100.0f); + final TextMesh afterPrefix = capture(TextMeshGLTest::drawLines); + batcher.beginBatch(); + draw("prefix line", 100.0f); + final TextMesh replayed = capture(() -> assertTrue(batcher.replay(direct, GlyphSettings.current()))); + assertTrue(direct.quadCount > 0); + assertEquals(contents(direct), contents(afterPrefix)); + assertEquals(contents(direct), contents(replayed)); + batcher.beginBatch(); + assertTrue(batcher.replay(direct, GlyphSettings.current())); + final int arenaBytes = Reflect.getStatic(BatchingFontRenderer.class, "vertexDataPos"); + assertEquals(0, arenaBytes); + batcher.endBatch(); + direct.free(); + assertFalse(direct.isValid(GlyphSettings.current())); + assertFalse(batcher.replay(direct, GlyphSettings.current())); + } + + @ParameterizedTest(name = "shadow={0}") + @ValueSource(booleans = { false, true }) + void replayDrawsTheSamePixelsAsTheDirectDraw(boolean shadowed) { + shadow = shadowed; + final byte[] direct = frame(() -> { + batcher.beginBatch(); + drawLines(); + batcher.endBatch(); + }); + final TextMesh mesh = capture(TextMeshGLTest::drawLines); + final byte[] uploaded = frame(() -> assertTrue(batcher.replay(mesh, GlyphSettings.current()))); + assertTrue(mesh.regionStart >= 0); + final byte[] resident = frame(() -> assertTrue(batcher.replay(mesh, GlyphSettings.current()))); + assertFalse(Arrays.equals(direct, frame(() -> {}))); + assertArrayEquals(direct, uploaded); + assertArrayEquals(direct, resident); + assertEquals(GL11.GL_NO_ERROR, GL11.glGetError()); + } + + @Test + void replayedAndDirectTextKeepTheirPushOrder() { + final String text = "overlap"; + final Runnable red = () -> batcher.drawString(2.0f, 2.0f, 0x80FF0000, false, false, text, 0, text.length()); + final Runnable green = () -> batcher.drawString(2.0f, 2.0f, 0x8000FF00, false, false, text, 0, text.length()); + final byte[] direct = frame(() -> { + batcher.beginBatch(); + red.run(); + green.run(); + batcher.endBatch(); + }); + final TextMesh mesh = capture(red); + final byte[] mixed = frame(() -> { + batcher.beginBatch(); + assertTrue(batcher.replay(mesh, GlyphSettings.current())); + green.run(); + batcher.endBatch(); + }); + assertTrue(mesh.regionStart >= 0); + assertArrayEquals(direct, mixed); + } + + @Test + void freedAndRecapturedMeshesReuploadAfterTheFrameDelay() { + endFrames(); + final byte[] direct = frame(() -> { + batcher.beginBatch(); + drawLines(); + batcher.endBatch(); + }); + final TextMesh resident = capture(() -> draw("resident", 2.0f)); + assertTrue(batcher.replay(resident, GlyphSettings.current())); + final TextMesh freed = capture(TextMeshGLTest::drawLines); + assertTrue(batcher.replay(freed, GlyphSettings.current())); + final int freedStart = freed.regionStart; + freed.free(); + final TextMesh sameFrame = capture(TextMeshGLTest::drawLines); + assertArrayEquals(direct, frame(() -> assertTrue(batcher.replay(sameFrame, GlyphSettings.current())))); + assertNotEquals(freedStart, sameFrame.regionStart); + sameFrame.free(); + final TextMesh recaptured = capture(TextMeshGLTest::drawLines); + endFrames(); + assertArrayEquals(direct, frame(() -> assertTrue(batcher.replay(recaptured, GlyphSettings.current())))); + assertEquals(freedStart, recaptured.regionStart); + assertEquals(GL11.GL_NO_ERROR, GL11.glGetError()); + } + + static List> abortedCaptures() { + return List.of( + Named.of("plain lines", TextMeshGLTest::drawLines), + Named.of("matrix change seals a segment", () -> { + draw("before", 2.0f); + GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); + draw("after", 11.0f); + }), + Named.of("draw state change seals a segment", () -> { + draw("before", 2.0f); + GLStateManager.glColorMask(false, false, false, false); + draw("after", 11.0f); + })); + } + + @ParameterizedTest + @MethodSource("abortedCaptures") + void abortRollsBackEverythingSinceBeginCapture(Runnable body) { + final byte[] prefixOnly = frame(() -> draw("prefix line", 30.0f)); + final TextMesh mesh = mesh(); + final byte[] aborted = frame(() -> { + batcher.beginBatch(); + draw("prefix line", 30.0f); + batcher.beginCapture(mesh); + body.run(); + batcher.abortCapture(); + assertThrows(IllegalStateException.class, batcher::abortCapture); + batcher.endBatch(); + }); + assertFalse(mesh.isValid(GlyphSettings.current())); + assertArrayEquals(prefixOnly, aborted); + } + + @Test + void rejectedCaptureCallsLeaveTheBatchDepthBalanced() { + final byte[] direct = frame(() -> draw("depth", 2.0f)); + final byte[] after = frame(() -> { + batcher.beginBatch(); + batcher.beginCapture(mesh()); + assertThrows(IllegalStateException.class, () -> batcher.beginCapture(mesh())); + batcher.endCapture(); + assertThrows(IllegalStateException.class, batcher::endCapture); + draw("depth", 2.0f); + batcher.endBatch(); + }); + assertArrayEquals(direct, after); + } + + private static Arguments refused(String name, boolean animated, Runnable body) { + return Arguments.of(Named.of(name, body), animated); + } + + static List refusedCaptures() { + return List.of( + refused("matrix change mid-capture", false, () -> { + draw("before", 2.0f); + GLStateManager.glTranslatef(1.0f, 0.0f, 0.0f); + draw("after", 11.0f); + }), + refused("draw state change before the first glyph", false, () -> { + GLStateManager.enableDepthTest(); + draw("depth", 2.0f); + }), + refused("another renderer flushes mid-capture", false, () -> { + draw("before", 2.0f); + drawOther("other", 11.0f); + }), + refused("obfuscated text", true, () -> draw("\u00a7kobfuscated", 2.0f))); + } + + @ParameterizedTest + @MethodSource("refusedCaptures") + void captureIsRefusedWhenTheMeshCouldNotReplayFaithfully(Runnable body, boolean animated) { + final TextMesh mesh = capture(body); + assertFalse(mesh.isValid(GlyphSettings.current())); + assertEquals(animated, mesh.isAnimated()); + batcher.beginBatch(); + assertFalse(batcher.replay(mesh, GlyphSettings.current())); + } + + @Test + void abortAfterAForeignFlushKeepsTheBatchUsable() { + final byte[] expected = frame(() -> { + draw("prefix", 2.0f); + drawOther("other", 20.0f); + draw("after", 38.0f); + }); + final byte[] actual = frame(() -> { + batcher.beginBatch(); + draw("prefix", 2.0f); + batcher.beginCapture(mesh()); + drawOther("other", 20.0f); + batcher.abortCapture(); + draw("after", 38.0f); + batcher.endBatch(); + }); + assertArrayEquals(expected, actual); + } + + @ParameterizedTest(name = "{0} fillRect={1}") + @CsvSource({ "none, false", "lit, false", "lit, true", "button, false", "darkMode, false" }) + void replayIsRefusedWhereADirectDrawWouldDiffer(String context, boolean rect) { + GLStateManager.glMatrixMode(GL11.GL_PROJECTION); + GLStateManager.glLoadIdentity(); + GLStateManager.glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 100.0); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + final Runnable body = rect ? () -> batcher.fillRect(0.0f, 0.0f, 4.0f, 4.0f, 0xFF00FF00) : () -> draw("text", 2.0f); + final TextMesh outside = capture(body); + assertTrue(outside.isValid(GlyphSettings.current())); + switch (context) { + case "lit" -> { + GLStateManager.enableLighting(); + GLStateManager.enableColorMaterial(); + assertTrue(FFPVertexLighting.modulatesVertexColor(new Vector3f())); + assertTrue(capture(body).isAnimated()); + } + case "button" -> BatchingFontRenderer.enterButtonSection(0, 0, 0); + case "darkMode" -> { + BatchingFontRenderer.enterRecolorSection(true); + // Whitespace only: a visible glyph would run DarkModeUtils' resource-pack setup in the test JVM. + assertTrue(capture(() -> draw(" ", 2.0f)).isAnimated()); + } + default -> { } + } + batcher.beginBatch(); + assertEquals(context.equals("none"), batcher.replay(outside, GlyphSettings.current())); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/compat/debug/BotaniaDebugCompatTest.java b/src/test/java/com/gtnewhorizons/angelica/compat/debug/BotaniaDebugCompatTest.java new file mode 100644 index 000000000..3714e67a9 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/compat/debug/BotaniaDebugCompatTest.java @@ -0,0 +1,42 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.debug.screen.RecordingDisplayer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class BotaniaDebugCompatTest { + + private static RecordingDisplayer write() { + final RecordingDisplayer displayer = new RecordingDisplayer(); + BotaniaDebugEntry.writeFacts(displayer, RecordingDisplayer.GROUP, 12, 3, 40, 5, 2, 7, 9, "1.13.32-GTNH"); + return displayer; + } + + @Test + void writesFiveFactsFromTheCounters() { + final RecordingDisplayer displayer = write(); + assertEquals("[Sparkles, Wisps, Lightning, Mana Network, Version]", displayer.names.toString()); + assertEquals("12 (3 fake)", displayer.text(0)); + assertEquals("40 (5 depth-ignoring)", displayer.text(1)); + assertEquals("2", displayer.text(2)); + assertEquals("7 collectors, 9 pools", displayer.text(3)); + assertEquals("1.13.32-GTNH", displayer.text(4)); + } + + @Test + void contextBlockMatchesTheLegacyConfigAndGlLines() { + final RecordingDisplayer displayer = new RecordingDisplayer(); + BotaniaDebugEntry.writeContext(displayer, RecordingDisplayer.GROUP, true, 7, "3.3.0", "Test GPU", "3.30", 16, true, false, true); + assertEquals("[null, shaders.enabled, shaders.secondaryUnit, null, GL_VERSION, GL_RENDERER, GL_SHADING_LANGUAGE_VERSION, GL_MAX_TEXTURE_IMAGE_UNITS_ARB, GL_ARB_multitexture, GL_ARB_texture_non_power_of_two, OpenGL13]", displayer.names.toString()); + assertEquals("[Config Context, true, 7, OpenGL Context, 3.3.0, Test GPU, 3.30, 16, true, false, true]", displayer.texts.toString()); + } + + @ParameterizedTest + @CsvSource(value = { "1.13.32-GTNH,1.13.32-GTNH", "GRADLETOKEN_VERSION,N/A", "NIL,N/A" }, nullValues = "NIL") + void displayVersionIsNotAvailableForTokensAndMissingValues(String version, String expected) { + assertEquals(expected, BotaniaDebugEntry.displayVersion(version)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/compat/debug/DebugCompatTest.java b/src/test/java/com/gtnewhorizons/angelica/compat/debug/DebugCompatTest.java new file mode 100644 index 000000000..f00d73d24 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/compat/debug/DebugCompatTest.java @@ -0,0 +1,42 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.debug.screen.DebugScreen; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugCompatTest { + + private int runs; + + @Test + void skipsNamespaceThatIsAlreadyRegistered() { + DebugScreen.bootstrap(); + DebugCompat.registerMissing("minecraft", () -> runs++); + assertEquals(0, runs); + } + + @Test + void shimStandsDownWhenTheDebugScreenFailedToStart() { + final DebugRegistry registry = new DebugRegistry(); + final DebugEntryDef def = registry.registerEntry(new DebugEntrySpec("compattest:entry", DebugEntryCategory.SCREEN_TEXT)); + assertTrue(DebugCompat.active(def)); + registry.failAll(); + assertFalse(DebugCompat.active(def)); + } + + @Test + void failingRegistrationDoesNotStopTheNext() { + DebugCompat.registerMissing("compattest", () -> { + throw new NoClassDefFoundError("vazkii/botania/Missing"); + }); + DebugCompat.registerMissing("compattest", () -> runs++); + assertEquals(1, runs); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/compat/debug/HodgepodgeDebugCompatTest.java b/src/test/java/com/gtnewhorizons/angelica/compat/debug/HodgepodgeDebugCompatTest.java new file mode 100644 index 000000000..5b030c755 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/compat/debug/HodgepodgeDebugCompatTest.java @@ -0,0 +1,27 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.debug.screen.RecordingDisplayer; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class HodgepodgeDebugCompatTest { + + private static RecordingDisplayer write() { + final RecordingDisplayer displayer = new RecordingDisplayer(); + HodgepodgeDebugEntry.writeFacts(displayer, RecordingDisplayer.GROUP, 3.26, 20.0, 1.5, 12.04, 140, 32, 9, 4, 1, 2, 0, 17, 105.75); + return displayer; + } + + @Test + void writesSixFactsFromTheStats() { + final RecordingDisplayer displayer = write(); + assertEquals("[Budget, Average, Queues, Active, Blocked, Debt]", displayer.names.toString()); + assertEquals("3.3 / 20.0 ms", displayer.text(0)); + assertEquals("terrain 1.5 ms, populate 12.0 ms", displayer.text(1)); + assertEquals("gen 140, populate 32, pipeline 9", displayer.text(2)); + assertEquals("gen 4, populate 1", displayer.text(3)); + assertEquals("2 blocked, 0 stuck, 17 fills", displayer.text(4)); + assertEquals("105.8 ms", displayer.text(5)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/compat/debug/JourneyMapDebugCompatTest.java b/src/test/java/com/gtnewhorizons/angelica/compat/debug/JourneyMapDebugCompatTest.java new file mode 100644 index 000000000..c10e907f7 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/compat/debug/JourneyMapDebugCompatTest.java @@ -0,0 +1,41 @@ +package com.gtnewhorizons.angelica.compat.debug; + +import com.gtnewhorizons.angelica.api.debug.DebugFactLines; +import com.gtnewhorizons.angelica.debug.screen.RecordingDisplayer; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class JourneyMapDebugCompatTest { + + private static RecordingDisplayer display(String... stats) { + return display(stats, null, null); + } + + private static RecordingDisplayer display(String[] stats, String mappingDisabled, List report) { + final DebugFactLines facts = new DebugFactLines(); + JourneyMapDebugEntry.writeFacts(facts, stats, mappingDisabled, report); + return RecordingDisplayer.of(facts); + } + + @Test + void statLinesBecomeNamedFactsAndOthersStayPlain() { + final RecordingDisplayer displayer = display("Surface: 4 (69) + 5 (28) = 97 chunks in 36ms (avg 0.4ms)", "Caves: 3 = 12 chunks in 5ms", "no stats yet"); + assertEquals("[Surface, Caves, null]", displayer.names.toString()); + assertEquals("4 (69) + 5 (28) = 97 chunks in 36ms (avg 0.4ms)", displayer.text(0)); + assertEquals("3 = 12 chunks in 5ms", displayer.text(1)); + assertEquals("no stats yet", displayer.text(2)); + } + + @Test + void disabledMappingShowsItsNoticeAndProfilerReportLinesStayPlain() { + final RecordingDisplayer displayer = display(null, "Mapping disabled.", Arrays.asList("MapPlayerTask: count 3 avg 1.5ms", "Render: count 9")); + assertEquals("[null, null, null]", displayer.names.toString()); + assertEquals("Mapping disabled.", displayer.text(0)); + assertEquals("MapPlayerTask: count 3 avg 1.5ms", displayer.text(1)); + assertEquals("Render: count 9", displayer.text(2)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/DebugF3ToggleTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/DebugF3ToggleTest.java new file mode 100644 index 000000000..cc5fa683b --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/DebugF3ToggleTest.java @@ -0,0 +1,45 @@ +package com.gtnewhorizons.angelica.debug; + +import org.junit.jupiter.api.Test; +import org.lwjgl.input.Keyboard; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugF3ToggleTest { + + @Test + void armedReleaseTogglesOnceAndModifiersDoNotUseTheCombo() { + final DebugF3Toggle toggle = new DebugF3Toggle(); + assertFalse(toggle.onF3Release()); + toggle.arm(false, false); + toggle.onKeyPress(Keyboard.KEY_LSHIFT); + toggle.onKeyPress(Keyboard.KEY_RMENU); + toggle.onKeyPress(Keyboard.KEY_F3); + assertTrue(toggle.onF3Release()); + assertFalse(toggle.onF3Release()); + } + + @Test + void otherKeyUsesTheComboUntilRearmed() { + final DebugF3Toggle toggle = new DebugF3Toggle(); + toggle.onKeyPress(Keyboard.KEY_B); + toggle.arm(false, false); + toggle.onKeyPress(Keyboard.KEY_B); + assertFalse(toggle.onF3Release()); + toggle.arm(false, false); + assertTrue(toggle.onF3Release()); + } + + @Test + void modifiersAreCapturedAtArmAndDisarmCancels() { + final DebugF3Toggle toggle = new DebugF3Toggle(); + toggle.arm(true, false); + assertTrue(toggle.onF3Release()); + assertTrue(toggle.shift()); + assertFalse(toggle.alt()); + toggle.arm(false, true); + toggle.disarm(); + assertFalse(toggle.onF3Release()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/flyby/FlybyDebugOverridesTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/flyby/FlybyDebugOverridesTest.java new file mode 100644 index 000000000..c6f8329f7 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/flyby/FlybyDebugOverridesTest.java @@ -0,0 +1,27 @@ +package com.gtnewhorizons.angelica.debug.flyby; + +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class FlybyDebugOverridesTest { + + @Test + void parsesPairsToleratingWhitespace() { + final List pairs = FlybyDebugOverrides.parseEntries(" a:b = alwaysOn , c:d=never,e:f=inOverlay "); + assertEquals(List.of("a:b", "c:d", "e:f"), pairs.stream().map(FlybyDebugOverrides.Pair::id).toList()); + assertEquals(List.of(DebugScreenEntryStatus.ALWAYS_ON, DebugScreenEntryStatus.NEVER, DebugScreenEntryStatus.IN_OVERLAY), pairs.stream().map(FlybyDebugOverrides.Pair::status).toList()); + } + + @Test + void skipsMalformedPairsAndKeepsTheRest() { + final List pairs = FlybyDebugOverrides.parseEntries("x,a:b=bogus,=never,e:f=inOverlay"); + assertEquals(1, pairs.size()); + assertEquals("e:f", pairs.get(0).id()); + assertTrue(FlybyDebugOverrides.parseEntries("").isEmpty()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/BuiltinEntriesTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/BuiltinEntriesTest.java new file mode 100644 index 000000000..6ff42559a --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/BuiltinEntriesTest.java @@ -0,0 +1,77 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugRenderers; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BuiltinEntriesTest { + + private static final DebugRegistry REGISTRY = TestRegistry.main(); + + @Test + void idsAreStableBecauseProfilesPersistThem() { + final List expected = new ArrayList<>(List.of( + "minecraft:game_version", "minecraft:fps", "minecraft:tps", "minecraft:player_position", "minecraft:player_section_position", + "minecraft:player_speed", "minecraft:biome", "minecraft:light_levels", "minecraft:heightmap", "minecraft:chunk_render_stats", + "minecraft:entity_render_stats", "minecraft:particle_render_stats", "minecraft:post_effect", "minecraft:chunk_source_stats", "minecraft:memory", + "minecraft:system_specs", "minecraft:looking_at_block", "forge:brandings", "angelica:renderer", "angelica:renderer_verbose", + "angelica:dynamic_lights", "angelica:fog", "angelica:animations", "angelica:other_left", "angelica:other_right", "angelica:help", + "minecraft:3d_crosshair", "minecraft:entity_hitboxes", "minecraft:chunk_borders", "minecraft:chunk_section_visibility", + "minecraft:visualize_block_light_levels", "minecraft:visualize_sky_light_levels", "minecraft:visualize_heightmap", + "minecraft:visualize_collision_boxes", "minecraft:visualize_solid_faces", "minecraft:visualize_water_levels", + "minecraft:visualize_entity_supporting_block", "angelica:chunk_minimap")); + if (AngelicaConfig.enableIris) expected.add("angelica:shaders"); + final Set actual = new HashSet<>(); + for (int i = 0; i < REGISTRY.entryCount(); i++) actual.add(REGISTRY.entryAt(i).id); + assertTrue(actual.containsAll(expected)); + } + + @Test + void everyEntryHasANameAndTooltipInTheEnglishLangFile() throws IOException { + final Set keys = new HashSet<>(); + try (InputStream in = BuiltinEntriesTest.class.getResourceAsStream("/assets/angelica/lang/en_US.lang")) { + assertNotNull(in); + for (final String line : new String(in.readAllBytes(), StandardCharsets.UTF_8).split("\\R")) { + final int equals = line.indexOf('='); + if (equals > 0 && equals < line.length() - 1) keys.add(line.substring(0, equals)); + } + } + for (int i = 0; i < REGISTRY.entryCount(); i++) { + final DebugEntryDef def = REGISTRY.entryAt(i); + assertTrue(keys.contains(def.name), def.name); + assertTrue(keys.contains(def.name + ".tooltip"), def.name + ".tooltip"); + } + } + + @Test + void everyKindIsUniqueAndHasAFactory() { + final Set kinds = new HashSet<>(); + final Class[] signature = { DebugEntryDef.class }; + for (int i = 0; i < REGISTRY.entryCount(); i++) { + final DebugEntryDef def = REGISTRY.entryAt(i); + if (def.kind <= 0) continue; + assertTrue(kinds.add(def.kind), def.id); + if (def.category == DebugEntryCategory.SCREEN_TEXT) { + assertNotNull(def.entryOrCreate(), def.id); + } else if (Reflect.invokeStatic(DebugRenderers.class, "isWorldRenderer", signature, def)) { + assertNotNull(Reflect.invokeStatic(DebugRenderers.class, "create", signature, def), def.id); + } else { + assertNotNull(def.toggleListener, def.id); + } + } + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugContextImplTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugContextImplTest.java new file mode 100644 index 000000000..218eb2d17 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugContextImplTest.java @@ -0,0 +1,52 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import net.minecraft.client.Minecraft; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +class DebugContextImplTest { + + private static final long MS = 1_000_000L; + + private final Minecraft mc = mock(Minecraft.class); + private final DebugContextImpl context = new DebugContextImpl(); + + @ParameterizedTest(name = "{0}") + @CsvSource({ + "steady 2 Hz, 1000:5:true 1050:5:false 1499:5:false 1500:5:true 1999:5:false 2000:5:true", + "entry list change, 1000:5:true 1050:6:true 1100:6:false 1549:6:false 1550:6:true" }) + void samplesAreDueTwicePerSecondOrWhenTheEntryListChanges(String name, String steps) { + for (final String step : steps.split(" ")) { + final String[] parts = step.split(":"); + context.prepare(mc, Long.parseLong(parts[0]) * MS, Long.parseLong(parts[1])); + assertEquals(Boolean.parseBoolean(parts[2]), context.isSampleDue(), step); + } + } + + @Test + void sampleIsDueForTheDisplayInWhichAnEntryFirstAsks() { + final DebugRegistry registry = new DebugRegistry(); + final DebugEntryDef early = registry.registerEntry(TestRegistry.text("test:early")); + final DebugEntryDef late = registry.registerEntry(TestRegistry.text("test:late")); + context.prepare(mc, 1000L * MS, 5L); + context.beginEntry(early); + assertTrue(context.isSampleDue()); + context.prepare(mc, 1050L * MS, 5L); + context.beginEntry(early); + assertFalse(context.isSampleDue()); + context.beginEntry(late); + assertTrue(context.isSampleDue()); + assertTrue(context.isSampleDue()); + context.beginEntry(early); + assertFalse(context.isSampleDue()); + context.prepare(mc, 1100L * MS, 5L); + context.beginEntry(late); + assertFalse(context.isSampleDue()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugEntryRunnerTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugEntryRunnerTest.java new file mode 100644 index 000000000..35203c4fc --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugEntryRunnerTest.java @@ -0,0 +1,148 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugContext; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.World; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DebugEntryRunnerTest { + + private final DebugRegistry registry = new DebugRegistry(); + private final DebugContext context = mock(DebugContext.class); + private final int[] displayed = new int[5]; + private final int[] toggled = new int[2]; + private DebugEntryDef[] text; + private DebugEntryDef throwingToggle; + private DebugEntryDef goodToggle; + private DebugScreenEntryList list; + private DebugScreenEntryList savedEntries; + private Minecraft mc; + + @BeforeEach + void setUp() { + text = new DebugEntryDef[] { + registry.registerEntry(TestRegistry.text("test:good").text(handle -> (displayer, ctx) -> displayed[0]++)), + registry.registerEntry(TestRegistry.text("test:throwing_display").text(handle -> (displayer, ctx) -> { + displayed[1]++; + throw new IllegalStateException("display"); + })), + registry.registerEntry(TestRegistry.text("test:throwing_factory").text(handle -> { + displayed[2]++; + throw new IllegalStateException("factory"); + })), + registry.registerEntry(TestRegistry.text("test:linkage").text(handle -> (displayer, ctx) -> { + displayed[3]++; + throw new NoClassDefFoundError("gone/Clazz"); + })), + registry.registerEntry(TestRegistry.text("test:last").text(handle -> (displayer, ctx) -> displayed[4]++)) }; + throwingToggle = registry.registerEntry(TestRegistry.renderer("test:throwing_toggle").defaults(DebugScreenEntryStatus.IN_OVERLAY, DebugScreenEntryStatus.NEVER).toggle((handle, enabled) -> { + toggled[0]++; + throw new IllegalStateException("toggle"); + })); + goodToggle = registry.registerEntry(TestRegistry.renderer("test:good_toggle").defaults(DebugScreenEntryStatus.IN_OVERLAY, DebugScreenEntryStatus.NEVER).toggle((handle, enabled) -> toggled[1]++)); + registry.freeze(); + list = new DebugScreenEntryList(registry); + savedEntries = DebugScreen.ENTRIES; + DebugScreen.ENTRIES = list; + mc = Reflect.allocate(Minecraft.class); + mc.theWorld = mock(WorldClient.class); + final EntityClientPlayerMP player = Reflect.allocate(EntityClientPlayerMP.class); + Reflect.set(player, "boundingBox", AxisAlignedBB.getBoundingBox(0, 0, 0, 1, 1, 1)); + mc.thePlayer = player; + } + + @AfterEach + void tearDown() { + DebugScreen.ENTRIES = savedEntries; + } + + private void runEnabledText() { + DebugScreen.runEntries(null, mc, 0L, null, false); + } + + @Test + void throwingEntriesDisableOnlyThemselves() { + list.setOverlayVisible(true); + runEnabledText(); + assertArrayEquals(new int[] { 1, 1, 1, 1, 1 }, displayed); + for (int i = 0; i < text.length; i++) assertEquals(i >= 1 && i <= 3, text[i].isFailed(), text[i].id); + list.refresh(); + runEnabledText(); + assertArrayEquals(new int[] { 2, 1, 1, 1, 2 }, displayed); + for (int i = 0; i < text.length; i++) assertEquals(i == 0 || i == 4, text[i].isEnabled(), text[i].id); + } + + @Test + void throwingToggleDisablesOnlyItsEntry() { + list.setOverlayVisible(true); + assertTrue(throwingToggle.isFailed()); + assertTrue(goodToggle.isEnabled()); + list.refresh(); + assertFalse(throwingToggle.isEnabled()); + list.setOverlayVisible(false); + assertArrayEquals(new int[] { 2, 2 }, toggled); + } + + @Test + void nullWorldOrPlayerCallsNothing() { + list.setOverlayVisible(true); + final WorldClient world = mc.theWorld; + mc.theWorld = null; + runEnabledText(); + mc.theWorld = world; + mc.thePlayer = null; + runEnabledText(); + assertArrayEquals(new int[5], displayed); + } + + @Test + void alwaysOnOnlyRunsOnlyAlwaysOnText() { + list.setOverlayVisible(true); + list.overrideForSession(text[0], DebugScreenEntryStatus.ALWAYS_ON); + DebugScreen.runEntries(null, mc, 0L, null, true); + assertArrayEquals(new int[] { 1, 0, 0, 0, 0 }, displayed); + } + + @Test + void nullFactNameOrGroupFailsOnlyThatEntry() { + when(context.world()).thenReturn(mock(World.class)); + when(context.player()).thenReturn(mock(EntityClientPlayerMP.class)); + final DebugRegistry other = new DebugRegistry(); + final DebugGroup group = TestRegistry.group(other, "test:group", DebugGroupHandle.LEFT, 100); + final DebugEntryDef nullName = other.registerEntry(TestRegistry.text("test:null_name").text(handle -> (displayer, ctx) -> displayer.fact(group, null).value(1))); + final DebugEntryDef nullGroup = other.registerEntry(TestRegistry.text("test:null_group").text(handle -> (displayer, ctx) -> displayer.fact(null, "Lost").value(1))); + final DebugEntryDef after = other.registerEntry(TestRegistry.text("test:after").text(handle -> (displayer, ctx) -> displayer.fact(group, "After").value(2))); + other.freeze(); + final GroupedDebugDisplayer grouped = new GroupedDebugDisplayer(other); + final FlatDebugDisplayer flat = new FlatDebugDisplayer(other); + flat.beginLines(); + for (int i = 0, n = other.entryCount(); i < n; i++) { + DebugScreen.display(other.entryAt(i), grouped, context); + DebugScreen.display(other.entryAt(i), flat, context); + } + flat.endLines(); + assertTrue(nullName.isFailed()); + assertTrue(nullGroup.isFailed()); + assertFalse(after.isFailed()); + final DebugGroupContents contents = grouped.contents(group.index()); + assertEquals(1, contents.factCount()); + assertEquals("After", contents.fact(0).name); + assertEquals(1, flat.lineCount(DebugGroupHandle.LEFT)); + assertEquals("After: \u00a7f2\u00a7r", flat.lineAt(DebugGroupHandle.LEFT, 0)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugGroupContentsTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugGroupContentsTest.java new file mode 100644 index 000000000..70a522ebf --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugGroupContentsTest.java @@ -0,0 +1,71 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DebugGroupContentsTest { + + private static final int TEXT = DebugGroupContents.TEXT; + private static final int SIZE = DebugGroupContents.SIZE; + private static final int ROW = DebugGroupContents.ROW_HEIGHT; + + private final FixedWidthText text = new FixedWidthText(); + private final DebugGroupContents contents = new DebugGroupContents(DebugGroup.POSITION); + + private int measure(int tick, String... lines) { + contents.reset(); + for (final String line : lines) { + if (line.contains("=")) { + contents.addFact(line.substring(0, line.indexOf('=')).intern()).text.append(line, line.indexOf('=') + 1, line.length()); + } else if (!line.isEmpty()) { + contents.addLine().text.append(line); + } + } + return contents.measure(text, 200, false, tick); + } + + @ParameterizedTest(name = "{0} -> {1}") + @CsvSource({ + "alpha|beta, alpha|beta, 0", + "alpha|beta, gamma|beta, 1", + "alpha|beta, alpha, 1", + "alpha, alpha|beta, 3", + "alpha, '', 3", + "Name=value, Name=value, 0", + "Name=value, Nome=value, 1" }) + void changeFlagsFollowTheContent(String first, String second, int flags) { + assertEquals(TEXT | SIZE, measure(0, first.split("\\|"))); + assertEquals(flags, measure(1, second.split("\\|"))); + } + + @Test + void shrinkIsHeldForFortyTicksUnlessReleased() { + measure(0, "alpha", "beta"); + assertEquals(TEXT, measure(1, "alpha")); + for (int tick = 2; tick < 40; tick++) assertEquals(0, measure(tick, "alpha"), "tick " + tick); + assertEquals(2 * ROW, contents.fullHeight()); + assertEquals(SIZE, measure(40, "alpha")); + assertEquals(ROW, contents.fullHeight()); + + measure(41, "alpha", "beta"); + assertEquals(TEXT, measure(42, "alpha")); + contents.releaseSizeHold(); + assertEquals(SIZE, measure(43, "alpha")); + assertEquals(ROW, contents.fullHeight()); + } + + @Test + void titleAndForcedRemeasureAreText() { + measure(0, "Name=value"); + contents.setTitle("Title", 3); + assertEquals(TEXT | SIZE, measure(1, "Name=value")); + contents.setTitle("Title", 3); + assertEquals(0, measure(2, "Name=value")); + contents.reset(); + contents.addFact("Name").text.append("value"); + assertEquals(TEXT, contents.measure(text, 200, true, 3)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugGuiScaleTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugGuiScaleTest.java new file mode 100644 index 000000000..7cb2f1597 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugGuiScaleTest.java @@ -0,0 +1,31 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DebugGuiScaleTest { + + @ParameterizedTest(name = "{0}x{1} even={2}") + @CsvSource({ "854, 480, false, 2", "854, 480, true, 2", "1280, 720, false, 3", "1280, 720, true, 2", "1920, 1080, false, 4", "2560, 1440, false, 6", "3840, 2160, false, 9", "3840, 2160, true, 8", "100, 100, false, 1" }) + void maxScale(int width, int height, boolean even, int expected) { + assertEquals(expected, DebugGuiScale.maxScale(width, height, even)); + } + + @ParameterizedTest(name = "setting={0} game={1} {2}x{3}") + @CsvSource({ + DebugGuiScale.UNCHANGED + ", 7, 3840, 2160, 7", DebugGuiScale.AUTO + ", 9, 3840, 2160, 4", DebugGuiScale.AUTO + ", 2, 854, 480, 1", + DebugGuiScale.AUTO + ", 3, 3840, 2160, 3", DebugGuiScale.AUTO + ", 1, 100, 100, 1", + "3, 4, 1920, 1080, 3", "8, 4, 1920, 1080, 4", "8, 9, 854, 480, 2", "4, 2, 1920, 1080, 2", "1, 1, 854, 480, 1" }) + void resolveCapsAtTheDisplayMaxAndTheGameScale(int setting, int gameScale, int width, int height, int expected) { + assertEquals(expected, DebugGuiScale.resolve(setting, gameScale, width, height, false)); + } + + @ParameterizedTest(name = "max={0} game={1}") + @CsvSource({ "4, 2, 2", "4, 4, 4", "4, 9, 4", "1, 3, 1", "4, 0, 1" }) + void offeredMaxIsCappedByGameScaleAndResolvesToItself(int max, int gameScale, int expected) { + assertEquals(expected, DebugGuiScale.offeredMax(max, gameScale)); + if (gameScale > 0) assertEquals(expected, DebugGuiScale.resolve(expected, gameScale, 3840, 2160, false)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugLayoutTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugLayoutTest.java new file mode 100644 index 000000000..364fe3f11 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugLayoutTest.java @@ -0,0 +1,94 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugLayoutTest { + + private static final int S = DebugLayout.GROUP_SPACING; + private static final DebugRegistry REGISTRY = TestRegistry.main(); + + private final DebugLayout layout = new DebugLayout(REGISTRY); + + private static DebugGroup group(String name) throws ReflectiveOperationException { + return (DebugGroup) DebugGroup.class.getField(name).get(null); + } + + private void present(DebugGroup... groups) { + Arrays.fill(layout.height, 0); + for (final DebugGroup group : groups) layout.height[group.index] = 10; + } + + private int top(DebugGroup group) { + return layout.top[group.index]; + } + + @ParameterizedTest(name = "{0} max={1},{2}") + @CsvSource({ + "RENDERING SHADERS, 1000, 1000, RENDERING:L:2 SHADERS:L:21 POSITION:-", + "RENDERING POSITION MEMORY, 15, 1000, RENDERING:L:2 MEMORY:R:2 POSITION:R:21", + "RENDERING POSITION MEMORY, 25, 25, RENDERING:L:2 MEMORY:R:2 POSITION:L:21", + "MEMORY, 1000, 0, MEMORY:L:2", + "POSITION MEMORY, 0, 0, POSITION:- MEMORY:-", + "PRIORITY_LEFT, 0, 0, PRIORITY_LEFT:L:2" }) + void groupsOverflowToTheOtherColumnThenClip(String groups, int leftMax, int rightMax, String expected) throws ReflectiveOperationException { + final String[] names = groups.split(" "); + final DebugGroup[] present = new DebugGroup[names.length]; + for (int i = 0; i < names.length; i++) present[i] = group(names[i]); + present(present); + layout.assign(2, leftMax, rightMax); + for (final String placement : expected.split(" ")) { + final String[] parts = placement.split(":"); + final DebugGroup group = group(parts[0]); + assertEquals(parts[1].equals("-") ? -1 : parts[1].equals("L") ? DebugGroup.LEFT : DebugGroup.RIGHT, layout.side[group.index], placement); + if (parts.length > 2) assertEquals(Integer.parseInt(parts[2]), top(group), placement); + } + } + + @Test + void allGroupsStackInRegistryOrderOnTheirHomeSide() { + final DebugGroup[] all = new DebugGroup[REGISTRY.groupCount()]; + for (int i = 0; i < all.length; i++) all[i] = REGISTRY.groupAt(i); + present(all); + layout.assign(2, 1000, 1000); + final int[] next = { 2, 2 }; + for (final DebugGroup group : all) { + assertEquals(group.side, layout.side[group.index], group.id); + assertEquals(next[group.side], top(group), group.id); + next[group.side] += 10 + S; + } + assertEquals(2, top(DebugGroup.PRIORITY_LEFT)); + assertEquals(next[DebugGroup.LEFT] - S - 10, top(DebugGroup.HELP)); + assertEquals(next[DebugGroup.RIGHT] - S - 10, top(DebugGroup.LOOKING_AT_BLOCK)); + } + + @Test + void appearingGroupDoesNotMoveEarlierGroups() { + present(DebugGroup.MEMORY, DebugGroup.ANGELICA); + layout.assign(2, 1000, 1000); + final int memory = top(DebugGroup.MEMORY); + final int angelica = top(DebugGroup.ANGELICA); + present(DebugGroup.MEMORY, DebugGroup.ANGELICA, DebugGroup.LOOKING_AT_BLOCK); + layout.assign(2, 1000, 1000); + assertEquals(memory, top(DebugGroup.MEMORY)); + assertEquals(angelica, top(DebugGroup.ANGELICA)); + assertTrue(top(DebugGroup.LOOKING_AT_BLOCK) > angelica); + } + + @Test + void singleColumnTakesEveryGroupInOrderUntilFull() { + present(DebugGroup.PRIORITY_LEFT, DebugGroup.RENDERING, DebugGroup.POSITION, DebugGroup.MEMORY); + layout.assignSingleColumn(DebugGroup.RIGHT, 2, 50); + assertEquals(DebugGroup.RIGHT, layout.side[DebugGroup.PRIORITY_LEFT.index]); + assertEquals(2, top(DebugGroup.PRIORITY_LEFT)); + assertEquals(2 + 10 + S, top(DebugGroup.RENDERING)); + assertEquals(2 + 2 * (10 + S), top(DebugGroup.POSITION)); + assertEquals(-1, layout.side[DebugGroup.MEMORY.index]); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugLineRulesTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugLineRulesTest.java new file mode 100644 index 000000000..5176ccf39 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugLineRulesTest.java @@ -0,0 +1,121 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +class DebugLineRulesTest { + + private static final int LEFT = DebugGroupHandle.LEFT; + private static final int RIGHT = DebugGroupHandle.RIGHT; + + private final DebugRegistry registry = new DebugRegistry(); + private DebugGroup memoryGroup; + private DebugEntryDef memory; + private DebugEntryDef specs; + + @BeforeEach + void setUp() { + memoryGroup = TestRegistry.group(registry, "angelica:memory", RIGHT, 100); + TestRegistry.group(registry, "angelica:position", LEFT, 200); + memory = registry.registerEntry(TestRegistry.text("minecraft:memory")); + specs = registry.registerEntry(TestRegistry.text("minecraft:system_specs")); + registry.freeze(); + memory.enabled = true; + specs.enabled = true; + } + + private DebugLineRules compile(String... config) { + return DebugLineRules.compile(registry, config); + } + + @Test + void everyDefaultParsesAgainstTheBuiltinRegistry() { + final DebugRegistry main = TestRegistry.main(); + final boolean[] saved = new boolean[main.entryCount()]; + for (int i = 0; i < saved.length; i++) { + saved[i] = main.entryAt(i).enabled; + main.entryAt(i).enabled = true; + } + try { + final DebugLineRules rules = DebugLineRules.compile(main, null); + for (String text : DebugLineRules.DEFAULTS) { + final String prefix = text.substring(text.indexOf('"') + 1, text.indexOf('"', text.indexOf('"') + 1)); + final DebugLineRules.Rule rule = rules.match(text.split(" ")[1].equals("left") ? LEFT : RIGHT, prefix + "1"); + assertNotNull(rule, text); + assertEquals(text.startsWith("fact"), rule.group() != null, text); + assertNotNull(rule.when(), text); + } + } finally { + for (int i = 0; i < saved.length; i++) main.entryAt(i).enabled = saved[i]; + } + } + + @ParameterizedTest + @ValueSource(ints = { DebugGroupHandle.LEFT, DebugGroupHandle.RIGHT }) + void dropAndFactMatchOnlyTheirSideAndCase(int side) { + final String word = side == LEFT ? "left" : "right"; + final DebugLineRules rules = compile("drop " + word + " \"Spam: \"", "fact " + word + " \"Battery: \" group=angelica:memory"); + assertNull(rules.match(side, "Spam: 1").group()); + assertNull(rules.match(1 - side, "Spam: 1")); + assertSame(memoryGroup, rules.match(side, "Battery: 80%").group()); + assertNull(rules.match(1 - side, "Battery: 80%")); + assertNull(rules.match(side, "battery: 80%")); + } + + @ParameterizedTest(name = "{0}") + @CsvSource(delimiter = '|', value = { + "\"Battery: \" | Battery: 80% | Battery | 11", + "\"NoColon\" | NoColon 1 | NoColon | 8", + "\"[JM] Waypoints: \" name=\"Way points\" | [JM] Waypoints: 3 | Way points | 16", + "\"; \" name=Semi | ; 1 | Semi | 2", + "\"&b[JM] &r\" name=JM | &b[J&lM] &rwaypoints | JM | 9" }) + void factNameAndPayloadIgnoreColorCodes(String rule, String line, String name, int payloadStart) { + final String text = line.replace('&', '\u00a7'); + final DebugLineRules.Rule matched = compile(("fact left " + rule + " group=angelica:position").replace('&', '\u00a7')).match(LEFT, text); + assertEquals(name, matched.name()); + assertEquals(payloadStart, matched.payloadStart(text)); + } + + @Test + void whenGatesTheRuleAndAnUnsatisfiedRuleFallsThrough() { + final DebugLineRules rules = compile("drop right \"IntCache: \" when=minecraft:system_specs"); + assertNull(rules.match(RIGHT, "IntCache: 1").group()); + specs.enabled = false; + assertSame(memoryGroup, rules.match(RIGHT, "IntCache: 1").group()); + memory.enabled = false; + assertNull(rules.match(RIGHT, "IntCache: 1")); + } + + @ParameterizedTest + @ValueSource(strings = { + "", "erase left \"Bad: \"", "drop any \"Bad: \"", "drop left", "drop left \"Bad: ", "drop left \"\"", "drop left \"\u00a7a\u00a7r\"", + "drop left \"Bad: 0123456789012345678901234567890123456789012345678901234567890123456789\"", "drop left \"Bad: \" color=red", + "drop left \"Bad: \" when=minecraft:missing", "drop left \"Bad: \" group=angelica:memory", "drop left \"Bad: \" name=x", + "fact left \"Bad: \"", "fact left \"Bad: \" group=angelica:missing", "fact left \"Bad: \" group=angelica:memory name=\"\"", + "fact left \": \" group=angelica:memory" }) + void invalidRuleIsSkippedWithoutAffectingOthers(String invalid) { + final DebugLineRules rules = compile("drop left \"Before: \"", invalid, null, "drop left \"After: \""); + assertNull(rules.match(LEFT, "Bad: 1")); + assertNull(rules.match(LEFT, ": 1")); + assertNotNull(rules.match(LEFT, "Before: 1")); + assertNotNull(rules.match(LEFT, "After: 1")); + } + + @Test + void configRulesWinOverDefaultsAndEarlierRulesWin() { + assertSame(memoryGroup, compile().match(RIGHT, "IntCache: 1").group()); + final DebugLineRules rules = compile("drop right \"IntCache: \"", "fact right \"Allocation rate: \" group=angelica:memory", "drop right \"Allocation\""); + assertNull(rules.match(RIGHT, "IntCache: 1").group()); + assertSame(memoryGroup, rules.match(RIGHT, "Allocation rate: 1").group()); + assertNull(rules.match(RIGHT, "Allocations: 1").group()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugNumbersTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugNumbersTest.java new file mode 100644 index 000000000..f058fa13c --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugNumbersTest.java @@ -0,0 +1,31 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DebugNumbersTest { + + @ParameterizedTest(name = "{0} at {1} -> {2}") + @CsvSource({ + "0.5, 0, 1", "2.5, 0, 3", "-2.5, 0, -3", "0.125, 2, 0.13", "-0.125, 2, -0.13", "0.99999, 2, 1.00", + "-0.0004, 3, 0.000", "-0.0, 3, 0.000", "-0.4, 0, 0", + "7, 3, 7.000", "0.00001, 5, 0.00001", "-12.5, 2, -12.50", + "30000000.12345, 5, 30000000.12345", "-30000000.5, 5, -30000000.50000", + "1.2, -3, 1", "1.2, 9, 1.200000", + "NaN, 2, NaN", "Infinity, 2, Infinity", "-Infinity, 2, -Infinity" }) + void appendFixed(double value, int decimals, String expected) { + final StringBuilder out = new StringBuilder(); + DebugNumbers.appendFixed(out, value, decimals); + assertEquals(expected, out.toString()); + } + + @ParameterizedTest(name = "{0} width {1} -> {2}") + @CsvSource({ "5, 3, 005", "0, 3, 000", "0, 1, 0", "1234, 2, 1234", "12, 0, 12" }) + void appendPadded(long value, int width, String expected) { + final StringBuilder out = new StringBuilder(); + DebugNumbers.appendPadded(out, value, width); + assertEquals(expected, out.toString()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugOptionsScreenGLTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugOptionsScreenGLTest.java new file mode 100644 index 000000000..df6d71b99 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugOptionsScreenGLTest.java @@ -0,0 +1,241 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.client.font.FontTestFixture; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.glsm.GLCoreTest; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt; +import net.minecraft.client.audio.SoundHandler; +import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.settings.GameSettings; +import net.minecraft.util.StatCollector; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.lwjgl.opengl.GL11; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; + +@GLCoreTest +class DebugOptionsScreenGLTest { + + private static final int WIDTH = 320; + private static final int HEIGHT = 240; + private static final int ALWAYS_BUTTON = 2; + + private static FontTestFixture fixture; + private static DebugScreenEntryList savedEntries; + private static boolean savedModernize; + private static boolean savedFontRenderer; + private static Path profileFile; + private static DebugScreenEntryList entries; + + @TempDir + static Path tempDir; + + private DebugOptionsScreen screen; + + @BeforeAll + static void setUp() throws Exception { + fixture = new FontTestFixture(tempDir); + fixture.batcher(); + fixture.font.setUnicodeFlag(false); + Reflect.set(fixture.font, "charWidth", FontTestFixture.charWidths()); + Reflect.set(fixture.font, "glyphWidth", new byte[65536]); + fixture.minecraft.fontRenderer = fixture.font; + fixture.minecraft.gameSettings = mock(GameSettings.class, withSettings().extraInterfaces(IGameSettingsExt.class)); + fixture.minecraft.displayWidth = WIDTH * 2; + fixture.minecraft.displayHeight = HEIGHT * 2; + when(fixture.minecraft.getTextureManager()).thenReturn(mock(TextureManager.class)); + when(fixture.minecraft.getSoundHandler()).thenReturn(mock(SoundHandler.class)); + savedModernize = AngelicaConfig.modernizeF3Screen; + savedFontRenderer = AngelicaConfig.enableFontRenderer; + savedEntries = DebugScreen.ENTRIES; + profileFile = tempDir.resolve("debug-profile.json"); + entries = new DebugScreenEntryList(TestRegistry.main()); + entries.setFile(profileFile.toFile()); + DebugScreen.ENTRIES = entries; + } + + @AfterAll + static void tearDown() { + AngelicaConfig.modernizeF3Screen = savedModernize; + AngelicaConfig.enableFontRenderer = savedFontRenderer; + DebugScreen.ENTRIES = savedEntries; + fixture.close(); + } + + @BeforeEach + void openScreen() { + AngelicaConfig.modernizeF3Screen = true; + AngelicaConfig.enableFontRenderer = true; + Reflect.setStatic(DebugScreen.class, "groupedFailed", false); + screen = new DebugOptionsScreen(); + fixture.minecraft.currentScreen = screen; + screen.setWorldAndResolution(fixture.minecraft, WIDTH, HEIGHT); + } + + private List rows() { + return Reflect.get(screen, "rows"); + } + + private int shownRows() { + return rows().size(); + } + + private GuiTextField searchField() { + return Reflect.get(screen, "searchField"); + } + + private void type(String text) { + for (final char c : text.toCharArray()) { + screen.keyTyped(c, 0); + } + } + + @Test + void statusButtonPersistsTheChoice() throws Exception { + final int first = Reflect.get(screen, "scrollRow"); + final int visible = Reflect.get(screen, "visibleRows"); + DebugEntryDef target = null; + Object button = null; + for (int i = first; i < Math.min(rows().size(), first + visible) && button == null; i++) { + final Object row = rows().get(i); + final DebugEntryDef def = Reflect.get(row, "def"); + final Object[] buttons = Reflect.get(row, "buttons"); + if (def == null || buttons.length <= ALWAYS_BUTTON || entries.getStatus(def) == DebugScreenEntryStatus.ALWAYS_ON) continue; + target = def; + button = buttons[ALWAYS_BUTTON]; + } + assertNotNull(button); + final int x = Reflect.get(button, "x"); + final int y = Reflect.get(button, "y"); + final int w = Reflect.get(button, "w"); + final int h = Reflect.get(button, "h"); + + screen.mouseClicked(x + w / 2, y + h / 2, 0); + + assertEquals(DebugScreenEntryStatus.ALWAYS_ON, entries.getStatus(target)); + final String saved = Files.readString(profileFile).replaceAll("\\s", ""); + assertTrue(saved.contains("\"" + target.id + "\":\"alwaysOn\"")); + } + + @Test + void twoButtonRowShowingAlwaysOnSelectsOverlay() { + AngelicaConfig.modernizeF3Screen = false; + screen = new DebugOptionsScreen(); + fixture.minecraft.currentScreen = screen; + screen.setWorldAndResolution(fixture.minecraft, WIDTH, HEIGHT); + DebugEntryDef target = null; + Object[] buttons = null; + for (final Object row : rows()) { + final DebugEntryDef def = Reflect.get(row, "def"); + final Object[] candidate = Reflect.get(row, "buttons"); + if (def != null && def.supportsAlways && candidate.length == 2) { + target = def; + buttons = candidate; + break; + } + } + assertNotNull(target); + entries.overrideForSession(target, DebugScreenEntryStatus.ALWAYS_ON); + try { + screen.updateScreen(); + assertEquals(DebugScreenEntryStatus.ALWAYS_ON, entries.effectiveStatus(target)); + final boolean neverSelected = Reflect.get(buttons[0], "selected"); + final boolean overlaySelected = Reflect.get(buttons[1], "selected"); + assertTrue(overlaySelected); + assertTrue(!neverSelected); + } finally { + entries.overrideForSession(target, null); + } + } + + @Test + void searchFiltersRowsAndKeepsFocusAfterAClick() { + final int all = shownRows(); + final Object last = rows().get(all - 1); + final DebugEntryDef def = Reflect.get(last, "def"); + final String filter = def != null ? def.path : "size"; + + type(filter); + final int filtered = shownRows(); + assertTrue(filtered < all); + assertEquals(filter, searchField().getText()); + + screen.mouseClicked(1, 1, 0); + type("x"); + + assertEquals(filter + "x", searchField().getText()); + } + + @Test + void panelsListShowsGroupIdsAndIsSearchable() { + type("angelica:position"); + Object panel = null; + int panels = 0; + for (final Object row : rows()) { + if (!"PANEL".equals(Reflect.get(row, "kind").toString())) continue; + panels++; + panel = row; + } + assertEquals(1, panels); + assertEquals("angelica:position", Reflect.get(panel, "value")); + assertEquals(StatCollector.translateToLocal("angelica.debug.group.position"), Reflect.get(panel, "plain")); + } + + @Test + void drawScreenLeavesNoGlError() { + final int firstVisible = Reflect.get(screen, "scrollRow"); + String filter = null; + for (int i = firstVisible; i < rows().size() && filter == null; i++) { + final DebugEntryDef def = Reflect.get(rows().get(i), "def"); + if (def != null && Reflect.get(rows().get(i), "tooltip") != null) filter = def.path.substring(0, Math.min(3, def.path.length())); + } + assertNotNull(filter); + type(filter); + searchField().setSelectionPos(0); + assertTrue(shownRows() > 0); + final int first = Reflect.get(screen, "scrollRow"); + final int visible = Reflect.get(screen, "visibleRows"); + int hovered = -1; + for (int i = first; i < Math.min(rows().size(), first + visible) && hovered < 0; i++) { + if (Reflect.get(rows().get(i), "def") != null && Reflect.get(rows().get(i), "tooltip") != null) hovered = i; + } + assertTrue(hovered >= 0); + final int viewTop = Reflect.get(screen, "viewTop"); + final int nameX = Reflect.get(screen, "nameX"); + final int rowH = Reflect.getStatic(DebugOptionsScreen.class, "ROW_H"); + final boolean blend = GLStateManager.getBlendMode().isEnabled(); + final boolean depth = GLStateManager.getDepthTest().isEnabled(); + GLStateManager.enableDepthTest(); + try { + GL11.glGetError(); + + screen.drawScreen(nameX + 1, viewTop + (hovered - first) * rowH + rowH / 2, 0.0f); + + assertEquals(GL11.GL_NO_ERROR, GL11.glGetError()); + assertTrue(GLStateManager.getDepthTest().isEnabled()); + assertEquals(blend, GLStateManager.getBlendMode().isEnabled()); + final Object batcher = Reflect.get(screen, "batcher"); + assertEquals(0, (int) Reflect.get(batcher, "batchDepth")); + assertNotNull(Reflect.get(screen, "measuredTooltip")); + } finally { + if (!depth) GLStateManager.disableDepthTest(); + } + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugOverlayFallbackGLTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugOverlayFallbackGLTest.java new file mode 100644 index 000000000..6cd655824 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugOverlayFallbackGLTest.java @@ -0,0 +1,153 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.client.font.BatchingFontRenderer; +import com.gtnewhorizons.angelica.client.font.FontTestFixture; +import com.gtnewhorizons.angelica.client.font.TextMesh; +import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.glsm.GLCoreTest; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.settings.GameSettings; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; + +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.withSettings; + +@GLCoreTest +class DebugOverlayFallbackGLTest { + + private static FontTestFixture fixture; + private static ThrowingBatcher batcher; + private static DebugScreenEntryList savedEntries; + private static boolean savedModernize; + private static boolean savedFontRenderer; + + @TempDir + static Path tempDir; + + static final class ThrowingBatcher extends BatchingFontRenderer { + + boolean armed; + boolean threw; + + ThrowingBatcher(FontRenderer font) { + super(font, FontTestFixture.charWidths(), new int[32], FontTestFixture.ASCII); + } + + @Override + public float drawString(float x, float y, int argb, boolean shadow, boolean unicode, CharSequence text, int start, int length) { + if (armed) { + threw = true; + throw new IllegalStateException("draw"); + } + return super.drawString(x, y, argb, shadow, unicode, text, start, length); + } + } + + @BeforeAll + static void setUp() throws Exception { + fixture = new FontTestFixture(tempDir); + batcher = new ThrowingBatcher(fixture.font); + fixture.font.batcher = batcher; + fixture.batcher(); + fixture.minecraft.gameSettings = mock(GameSettings.class, withSettings().extraInterfaces(IGameSettingsExt.class)); + savedModernize = AngelicaConfig.modernizeF3Screen; + savedFontRenderer = AngelicaConfig.enableFontRenderer; + savedEntries = DebugScreen.ENTRIES; + final DebugScreenEntryList entries = new DebugScreenEntryList(TestRegistry.main()); + entries.setOverlayVisible(true); + DebugScreen.ENTRIES = entries; + } + + @AfterAll + static void tearDown() { + AngelicaConfig.modernizeF3Screen = savedModernize; + AngelicaConfig.enableFontRenderer = savedFontRenderer; + DebugScreen.ENTRIES = savedEntries; + fixture.close(); + } + + @BeforeEach + void enableGrouped() { + AngelicaConfig.modernizeF3Screen = true; + AngelicaConfig.enableFontRenderer = true; + Reflect.setStatic(DebugScreen.class, "groupedFailed", false); + Reflect.setStatic(DebugScreen.class, "overlayVisible", true); + DebugScreenOverlay.INSTANCE.afterDebugBlock(true, false); + } + + @AfterEach + void restore() { + batcher.armed = false; + batcher.resetAfterCrash(); + Reflect.setStatic(DebugScreen.class, "groupedFailed", false); + Reflect.setStatic(DebugScreen.class, "overlayVisible", false); + DebugScreenOverlay.INSTANCE.afterDebugBlock(false, false); + } + + private static RenderGameOverlayEvent.Text event(String... left) { + final ScaledResolution resolution = Reflect.allocate(ScaledResolution.class); + Reflect.set(resolution, "scaleFactor", 1); + final RenderGameOverlayEvent.Text event = Reflect.allocate(RenderGameOverlayEvent.Text.class); + Reflect.set(event, "resolution", resolution); + Reflect.set(event, "left", new ArrayList<>(List.of(left))); + Reflect.set(event, "right", new ArrayList()); + return event; + } + + private static boolean anyLitAfterDirectDraw() { + GLStateManager.glViewport(0, 0, 128, 32); + GLStateManager.glMatrixMode(GL11.GL_PROJECTION); + GLStateManager.glLoadIdentity(); + GLStateManager.glOrtho(0.0, 128, 32, 0.0, -1.0, 1.0); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glLoadIdentity(); + GLStateManager.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + GLStateManager.glClear(GL11.GL_COLOR_BUFFER_BIT); + batcher.drawString(2.0f, 2.0f, 0xFFFFFFFF, false, false, "balanced", 0, 8); + final ByteBuffer pixels = BufferUtils.createByteBuffer(128 * 32 * 4); + GL11.glReadPixels(0, 0, 128, 32, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels); + for (int i = 0; i < pixels.capacity(); i += 4) { + if (pixels.get(i) != 0) return true; + } + return false; + } + + @Test + void fontWithoutTheBatcherFallsBackToTheClassicLayout() { + assertTrue(DebugScreen.isGroupedActive()); + assertFalse(DebugScreenOverlay.INSTANCE.onTextEvent(event("mod line"), Reflect.allocate(FontRenderer.class), 320, 240, false)); + assertFalse(DebugScreen.isGroupedActive()); + } + + @Test + void throwingDrawFallsBackToTheClassicLayoutWithTheBatchBalanced() { + batcher.armed = true; + DebugScreenOverlay.INSTANCE.afterDebugBlock(true, true); + assertTrue(DebugScreenOverlay.INSTANCE.onTextEvent(event("mod line"), fixture.font, 320, 240, false)); + assertTrue(batcher.threw); + assertFalse(DebugScreen.isGroupedActive()); + batcher.armed = false; + batcher.beginCapture(new TextMesh()); + batcher.abortCapture(); + assertTrue(anyLitAfterDirectDraw()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugRegistryTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugRegistryTest.java new file mode 100644 index 000000000..95d9b267f --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugRegistryTest.java @@ -0,0 +1,87 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugGroupSpec; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugRegistryTest { + + private final DebugRegistry registry = new DebugRegistry(); + + @ParameterizedTest + @NullSource + @ValueSource(strings = { "", "noseparator", ":path", "ns:", "Ns:path", "ns:Path", "ns:pa th", "ns/x:path", "ns:a:b", "ns:p\u00e4th" }) + void badIdsAreRejected(String id) { + assertThrows(IllegalArgumentException.class, () -> registry.registerEntry(TestRegistry.text(id))); + } + + @Test + void malformedRegistrationsAreRejected() { + registry.registerEntry(TestRegistry.text("some-mod.x:dir/entry_1.b")); + registry.registerGroup(new DebugGroupSpec("test:g")); + assertThrows(IllegalArgumentException.class, () -> registry.registerEntry(TestRegistry.renderer("some-mod.x:dir/entry_1.b"))); + assertThrows(IllegalArgumentException.class, () -> registry.registerGroup(new DebugGroupSpec("test:g"))); + assertThrows(IllegalArgumentException.class, () -> registry.registerEntry(TestRegistry.text("test:a").defaults(null, DebugScreenEntryStatus.NEVER))); + assertThrows(IllegalArgumentException.class, () -> registry.registerEntry(new DebugEntrySpec("test:b", null))); + assertThrows(IllegalArgumentException.class, () -> registry.registerGroup(new DebugGroupSpec("test:h").side(2))); + assertThrows(IllegalArgumentException.class, () -> registry.registerEntry(TestRegistry.renderer("test:c").text(entry -> null))); + } + + @Test + void registrationAfterFreezeThrows() { + registry.freeze(); + assertThrows(IllegalStateException.class, () -> registry.registerEntry(TestRegistry.text("test:a"))); + assertThrows(IllegalStateException.class, () -> registry.registerBuiltin(TestRegistry.text("test:b"), 1, false)); + assertThrows(IllegalStateException.class, () -> registry.registerGroup(new DebugGroupSpec("test:g"))); + } + + @Test + void groupsSortBySideThenOrderThenRegistration() { + final DebugGroup rightLate = TestRegistry.group(registry, "test:right_late", DebugGroupHandle.RIGHT, 900); + final DebugGroup leftDefault = registry.registerGroup(new DebugGroupSpec("test:left_default")); + final DebugGroup leftFirst = TestRegistry.group(registry, "test:left_first", DebugGroupHandle.LEFT, 0); + final DebugGroup rightEarly = TestRegistry.group(registry, "test:right_early", DebugGroupHandle.RIGHT, 100); + final DebugGroup leftDefaultSecond = TestRegistry.group(registry, "test:left_default_second", DebugGroupHandle.LEFT, DebugGroupSpec.DEFAULT_ORDER); + registry.freeze(); + final DebugGroup[] sorted = new DebugGroup[registry.groupCount()]; + for (int i = 0; i < sorted.length; i++) sorted[i] = registry.groupAt(i); + assertArrayEquals(new DebugGroup[] { leftFirst, leftDefault, leftDefaultSecond, rightEarly, rightLate }, sorted); + } + + @Test + void flatListHoldsThirdPartyAndFlatBuiltinTextEntries() { + final DebugEntryDef thirdParty = registry.registerEntry(TestRegistry.text("test:third").text(handle -> (displayer, context) -> {})); + registry.registerEntry(TestRegistry.text("test:gate")); + registry.registerBuiltin(TestRegistry.text("test:grouped").text(handle -> (displayer, context) -> {}), 7, false); + final DebugEntryDef builtinFlat = registry.registerBuiltin(TestRegistry.text("test:flat").text(handle -> (displayer, context) -> {}), 8, true); + registry.registerEntry(TestRegistry.renderer("test:renderer")); + registry.freeze(); + assertEquals(2, registry.flatCount()); + assertSame(thirdParty, registry.flatAt(0)); + assertSame(builtinFlat, registry.flatAt(1)); + } + + @Test + void failuresAreLatchedOncePerEntry() { + final DebugEntryDef def = registry.registerEntry(TestRegistry.text("test:a")); + assertFalse(registry.consumeFailures()); + registry.fail(def, new IllegalStateException("boom")); + assertTrue(def.isFailed()); + assertTrue(registry.consumeFailures()); + assertFalse(registry.consumeFailures()); + registry.fail(def, new IllegalStateException("again")); + assertFalse(registry.consumeFailures()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntryListTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntryListTest.java new file mode 100644 index 000000000..cc574c1af --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugScreenEntryListTest.java @@ -0,0 +1,347 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.time.Duration; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugScreenEntryListTest { + + private static final DebugScreenEntryStatus NEVER = DebugScreenEntryStatus.NEVER; + private static final DebugScreenEntryStatus IN_OVERLAY = DebugScreenEntryStatus.IN_OVERLAY; + private static final DebugScreenEntryStatus ALWAYS_ON = DebugScreenEntryStatus.ALWAYS_ON; + + private final DebugRegistry registry = new DebugRegistry(); + private final int[] toggles = new int[2]; + private DebugEntryDef gameVersion; + private DebugEntryDef fps; + private DebugEntryDef tps; + private DebugEntryDef memory; + private DebugEntryDef help; + private DebugEntryDef hitboxes; + private DebugScreenEntryList list; + + @TempDir + File dir; + + @BeforeEach + void setUp() { + gameVersion = registry.registerEntry(TestRegistry.text("minecraft:game_version")); + fps = registry.registerEntry(TestRegistry.text("minecraft:fps").text(entry -> null).defaults(IN_OVERLAY, ALWAYS_ON)); + tps = registry.registerEntry(TestRegistry.text("minecraft:tps").text(entry -> null).defaults(IN_OVERLAY, IN_OVERLAY)); + memory = registry.registerEntry(TestRegistry.text("minecraft:memory").defaults(ALWAYS_ON, NEVER)); + help = registry.registerEntry(TestRegistry.text("test:help").defaults(IN_OVERLAY, ALWAYS_ON).supportsAlways(false)); + hitboxes = registry.registerEntry(TestRegistry.renderer("minecraft:entity_hitboxes").toggle((handle, enabled) -> toggles[enabled ? 1 : 0]++)); + registry.freeze(); + list = new DebugScreenEntryList(registry); + } + + private File file() { + return new File(dir, "sub/debug-profile.json"); + } + + private File existingFile(String content) throws IOException { + Files.createDirectories(file().getParentFile().toPath()); + Files.writeString(file().toPath(), content); + return file(); + } + + private DebugScreenEntryList reload() { + return reload(registry); + } + + private DebugScreenEntryList reload(DebugRegistry source) { + final DebugScreenEntryList other = new DebugScreenEntryList(source); + other.setFile(file()); + other.load(); + return other; + } + + private String read() throws IOException { + return Files.readString(file().toPath()).replaceAll("\\s", ""); + } + + private String[] siblings() { + final String[] names = file().getParentFile().list(); + Arrays.sort(names); + return names; + } + + @ParameterizedTest(name = "{0} {1} overlay={2}") + @CsvSource({ + "fps, ALWAYS_ON, true, NEVER, false", "fps, ALWAYS_ON, false, NEVER, false", "fps, IN_OVERLAY, true, NEVER, false", + "fps, IN_OVERLAY, false, ALWAYS_ON, true", "fps, NEVER, true, IN_OVERLAY, true", "fps, NEVER, false, ALWAYS_ON, true", + "help, NEVER, false, IN_OVERLAY, true", "help, IN_OVERLAY, false, NEVER, false", + "hitboxes, NEVER, true, ALWAYS_ON, true", "hitboxes, NEVER, false, ALWAYS_ON, true", "hitboxes, ALWAYS_ON, true, NEVER, false", + "hitboxes, IN_OVERLAY, true, NEVER, false", "hitboxes, IN_OVERLAY, false, ALWAYS_ON, true" }) + void toggleStatus(String entry, DebugScreenEntryStatus from, boolean overlayVisible, DebugScreenEntryStatus to, boolean result) { + final DebugEntryDef def = switch (entry) { + case "fps" -> fps; + case "help" -> help; + default -> hitboxes; + }; + list.setOverlayVisible(overlayVisible); + list.setStatus(def, from); + list.setFile(file()); + assertEquals(result, list.toggleStatus(def)); + assertEquals(to, list.effectiveStatus(def)); + assertEquals(from, list.getStatus(def)); + assertFalse(file().exists()); + } + + @Test + void enabledSetFollowsOverlayVisibilityAndTextFlags() { + list.setStatus(fps, IN_OVERLAY); + list.setStatus(tps, ALWAYS_ON); + list.setStatus(memory, ALWAYS_ON); + list.setOverlayVisible(false); + assertFalse(fps.isEnabled()); + assertTrue(tps.isAlwaysOn()); + assertTrue(memory.isAlwaysOn()); + assertTrue(list.anyTextAlwaysOn()); + list.setStatus(tps, IN_OVERLAY); + assertFalse(list.anyTextAlwaysOn()); + assertFalse(list.anyTextEnabled()); + list.setOverlayVisible(true); + assertTrue(fps.isEnabled()); + assertTrue(list.anyTextEnabled()); + final long version = list.version(); + list.setOverlayVisible(true); + assertEquals(version, list.version()); + } + + @Test + void toggleListenerFiresOncePerFlipAndMayChangeStatuses() { + list.setStatus(hitboxes, IN_OVERLAY); + assertEquals(0, toggles[1]); + list.setOverlayVisible(true); + list.setStatus(hitboxes, ALWAYS_ON); + assertEquals(1, toggles[1]); + list.setStatus(hitboxes, NEVER); + assertEquals(1, toggles[0]); + + final DebugRegistry other = new DebugRegistry(); + final DebugScreenEntryList[] holder = new DebugScreenEntryList[1]; + final DebugEntryDef[] followers = new DebugEntryDef[2]; + final int[] flips = new int[1]; + final DebugEntryDef leader = other.registerEntry(TestRegistry.renderer("test:leader").toggle((handle, enabled) -> { + if (!enabled) { + holder[0].clearSessionOverrides(); + return; + } + for (final DebugEntryDef follower : followers) holder[0].overrideForSession(follower, ALWAYS_ON); + })); + final DebugEntryDef bystander = other.registerEntry(TestRegistry.renderer("test:bystander").toggle((handle, enabled) -> flips[0]++)); + followers[0] = other.registerEntry(TestRegistry.renderer("test:follower_a").toggle((handle, enabled) -> flips[0]++)); + followers[1] = other.registerEntry(TestRegistry.renderer("test:follower_b").toggle((handle, enabled) -> flips[0]++)); + other.freeze(); + holder[0] = new DebugScreenEntryList(other); + holder[0].setStatus(leader, IN_OVERLAY); + holder[0].setStatus(bystander, IN_OVERLAY); + holder[0].setOverlayVisible(true); + assertTrue(followers[0].isEnabled()); + assertTrue(followers[1].isEnabled()); + holder[0].setOverlayVisible(false); + assertFalse(followers[0].isEnabled()); + assertFalse(followers[1].isEnabled()); + assertFalse(bystander.isEnabled()); + assertFalse(leader.isFailed()); + assertEquals(6, flips[0]); + } + + @Test + void listenerThatKeepsFlippingItsOwnStatusFailsInsteadOfHanging() { + final DebugRegistry other = new DebugRegistry(); + final DebugScreenEntryList[] holder = new DebugScreenEntryList[1]; + final int[] flips = new int[1]; + final DebugEntryDef[] self = new DebugEntryDef[1]; + self[0] = other.registerEntry(TestRegistry.renderer("test:flipper").toggle((handle, enabled) -> holder[0].overrideForSession(self[0], enabled ? NEVER : ALWAYS_ON))); + final DebugEntryDef bystander = other.registerEntry(TestRegistry.renderer("test:bystander").toggle((handle, enabled) -> flips[0]++)); + other.freeze(); + holder[0] = new DebugScreenEntryList(other); + assertTimeoutPreemptively(Duration.ofSeconds(10), () -> holder[0].setStatus(self[0], ALWAYS_ON)); + assertTrue(self[0].isFailed()); + holder[0].refresh(); + holder[0].setStatus(bystander, ALWAYS_ON); + assertFalse(self[0].isEnabled()); + assertFalse(bystander.isFailed()); + assertEquals(1, flips[0]); + } + + @Test + void loadAppliesSavedStatusesWithoutFlippingDefaultsFirst() throws IOException { + final DebugRegistry other = new DebugRegistry(); + final int[] shown = new int[1]; + other.registerEntry(TestRegistry.renderer("test:shown_by_default").defaults(ALWAYS_ON, NEVER).toggle((handle, enabled) -> shown[0]++)); + other.freeze(); + existingFile("{\"version\":2,\"custom\":{\"test:shown_by_default\":\"never\"}}"); + reload(other); + assertEquals(0, shown[0]); + } + + @Test + void failedEntryTellsItsListenerItIsHidden() { + final DebugRegistry other = new DebugRegistry(); + final boolean[] shown = new boolean[1]; + final DebugEntryDef def = other.registerEntry(TestRegistry.text("test:failing").text(handle -> null).toggle((handle, enabled) -> shown[0] = enabled)); + other.freeze(); + final DebugScreenEntryList entries = new DebugScreenEntryList(other); + entries.setStatus(def, ALWAYS_ON); + assertTrue(shown[0]); + other.fail(def, new IllegalStateException("boom")); + entries.refresh(); + assertFalse(def.isEnabled()); + assertFalse(shown[0]); + } + + @Test + void sessionOverrideIsNeverSavedAndAlwaysIsClampedWhenUnsupported() throws IOException { + list.setFile(file()); + list.save(); + final String saved = read(); + list.overrideForSession(gameVersion, ALWAYS_ON); + list.overrideForSession(help, ALWAYS_ON); + assertTrue(gameVersion.isEnabled()); + assertFalse(help.isAlwaysOn()); + assertTrue(list.isUsingProfile(DebugScreenProfile.DEFAULT)); + list.save(); + assertEquals(saved, read()); + list.setStatus(help, ALWAYS_ON); + assertEquals(IN_OVERLAY, list.getStatus(help)); + assertEquals(saved, read()); + list.clearSessionOverrides(); + assertFalse(gameVersion.isEnabled()); + existingFile("{\"version\":2,\"custom\":{\"test:help\":\"alwaysOn\"}}"); + assertEquals(IN_OVERLAY, reload().getStatus(help)); + list.overrideForSession(gameVersion, ALWAYS_ON); + list.loadProfile(DebugScreenProfile.PERFORMANCE); + assertEquals(NEVER, list.effectiveStatus(gameVersion)); + } + + @Test + void profileAndCustomStatusesRoundTrip() throws IOException { + list.setFile(file()); + list.loadProfile(DebugScreenProfile.PERFORMANCE); + assertFalse(file().exists()); + list.save(); + assertTrue(read().contains("\"profile\":\"performance\"")); + assertTrue(reload().isUsingProfile(DebugScreenProfile.PERFORMANCE)); + assertEquals(ALWAYS_ON, reload().getStatus(fps)); + + list.setStatus(fps, NEVER); + list.setStatus(tps, ALWAYS_ON); + assertFalse(read().contains("profile")); + final DebugScreenEntryList other = reload(); + assertFalse(other.isUsingProfile(DebugScreenProfile.PERFORMANCE)); + for (int i = 0; i < registry.entryCount(); i++) assertEquals(list.getStatus(registry.entryAt(i)), other.getStatus(registry.entryAt(i))); + assertArrayEquals(new String[] { "debug-profile.json" }, siblings()); + } + + @Test + void customSaveHoldsOnlyChangedEntriesAndUntouchedOnesFollowNewDefaults() throws IOException { + list.setFile(file()); + list.setStatus(fps, NEVER); + assertEquals("{\"version\":2,\"custom\":{\"minecraft:fps\":\"never\"}}", read()); + + final DebugRegistry changed = new DebugRegistry(); + final DebugEntryDef changedFps = changed.registerEntry(TestRegistry.text("minecraft:fps").defaults(IN_OVERLAY, ALWAYS_ON)); + final DebugEntryDef changedMemory = changed.registerEntry(TestRegistry.text("minecraft:memory").defaults(NEVER, NEVER)); + changed.freeze(); + final DebugScreenEntryList loaded = reload(changed); + assertEquals(NEVER, loaded.getStatus(changedFps)); + assertEquals(NEVER, loaded.getStatus(changedMemory)); + } + + @Test + void settingAnEntryBackToItsProfileValueReattachesTheProfile() throws IOException { + list.setFile(file()); + list.loadProfile(DebugScreenProfile.PERFORMANCE); + list.setStatus(fps, NEVER); + assertFalse(list.isUsingProfile(DebugScreenProfile.PERFORMANCE)); + list.setStatus(fps, ALWAYS_ON); + assertTrue(list.isUsingProfile(DebugScreenProfile.PERFORMANCE)); + assertTrue(read().contains("\"profile\":\"performance\"")); + } + + @Test + void missingFileIsWrittenWithTheDefaultProfile() throws IOException { + list.setFile(file()); + list.loadProfile(DebugScreenProfile.PERFORMANCE); + list.load(); + assertTrue(list.isUsingProfile(DebugScreenProfile.DEFAULT)); + assertTrue(read().contains("\"profile\":\"default\"")); + } + + @ParameterizedTest + @ValueSource(strings = { "this is { not json", "[1, 2]", "{\"version\":2,\"custom\":\"nope\",\"keep\":1}" }) + void corruptFileIsSetAsideAndRewritten(String content) throws IOException { + existingFile(content); + list.setFile(file()); + list.loadProfile(DebugScreenProfile.PERFORMANCE); + list.load(); + assertTrue(list.isUsingProfile(DebugScreenProfile.DEFAULT)); + assertTrue(read().contains("\"default\"")); + assertFalse(read().contains("keep")); + assertEquals(content, Files.readString(new File(file().getParentFile(), "debug-profile.json.corrupt").toPath())); + assertArrayEquals(new String[] { "debug-profile.json", "debug-profile.json.corrupt" }, siblings()); + } + + @ParameterizedTest + @ValueSource(strings = { "{\"version\":2, \"profile\":\"bogus\"}", "{\"later\":true}", "{\"version\":2,\"custom\":{}}" }) + void unrecognizedButValidFileIsDefaultAndUntouched(String content) throws IOException { + existingFile(content); + list.setFile(file()); + list.loadProfile(DebugScreenProfile.PERFORMANCE); + list.load(); + assertTrue(list.isUsingProfile(DebugScreenProfile.DEFAULT)); + assertEquals(content, Files.readString(file().toPath())); + assertArrayEquals(new String[] { "debug-profile.json" }, siblings()); + } + + @Test + void customEntriesMissingOrInvalidTakeTheirDefault() throws IOException { + existingFile("{\"version\":2,\"custom\":{\"minecraft:fps\":\"alwaysOn\",\"minecraft:tps\":\"weird\",\"minecraft:game_version\":7}}"); + final DebugScreenEntryList loaded = reload(); + assertFalse(loaded.isUsingProfile(DebugScreenProfile.DEFAULT)); + assertEquals(ALWAYS_ON, loaded.getStatus(fps)); + assertEquals(IN_OVERLAY, loaded.getStatus(tps)); + assertEquals(IN_OVERLAY, loaded.getStatus(gameVersion)); + assertEquals(ALWAYS_ON, loaded.getStatus(memory)); + assertEquals(NEVER, loaded.getStatus(hitboxes)); + } + + @Test + void unknownIdsAndMembersSurviveSaves() throws IOException { + existingFile("{\"custom\":{\"gone:entry\":\"alwaysOn\",\"minecraft:fps\":\"never\",\"gone:odd\":{\"a\":[1,2]}},\"future\":{\"x\":1}}"); + final DebugScreenEntryList loaded = reload(); + assertEquals(NEVER, loaded.getStatus(fps)); + loaded.setStatus(tps, ALWAYS_ON); + assertTrue(read().contains("\"gone:entry\":\"alwaysOn\"")); + assertTrue(read().contains("\"gone:odd\":{\"a\":[1,2]}")); + assertTrue(read().contains("\"minecraft:tps\":\"alwaysOn\"")); + assertTrue(read().contains("\"future\":{\"x\":1}")); + assertTrue(read().contains("\"version\":2")); + loaded.loadProfile(DebugScreenProfile.PERFORMANCE); + loaded.save(); + assertTrue(read().contains("\"future\":{\"x\":1}")); + assertFalse(read().contains("gone:entry")); + final DebugScreenEntryList again = reload(); + again.setStatus(fps, IN_OVERLAY); + assertTrue(read().contains("\"future\":{\"x\":1}")); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugTextWrapTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugTextWrapTest.java new file mode 100644 index 000000000..43f1ce553 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/DebugTextWrapTest.java @@ -0,0 +1,32 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DebugTextWrapTest { + + @ParameterizedTest(name = "\"{0}\" in {1}") + @CsvSource(delimiter = ';', value = { + "short line; 20; short line", + "aaa bbb ccc ddd; 8; aaa bbb|ccc ddd", + "abcdefghij; 4; abcd|efgh|ij", + "&a&lab cd&r ef gh; 6; &a&lab|&a&lcd&r|ef gh", + "&a&o&lab cd; 6; &a&o&lab|&a&l&ocd", + "abc&cdef; 3; abc&c|&cdef", + "' abcdef'; 4; ' abc|def'", + "''; 10; ''" }) + void wrapsAtSpacesAndCarriesFormatOntoContinuationRows(String text, int budget, String rows) { + final DebugGroupContents.Slot slot = new DebugGroupContents.Slot(); + slot.begin(); + slot.text.append(text.replace('&', '\u00a7')); + slot.measure(new FixedWidthText(), budget, false); + final List actual = new ArrayList<>(); + for (int i = 0; i < slot.rowCount; i++) actual.add(slot.rowText().subSequence(slot.rowStart[i], slot.rowEnd[i]).toString().replace('\u00a7', '&')); + assertEquals(List.of(rows.split("\\|", -1)), actual); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/FixedWidthText.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/FixedWidthText.java new file mode 100644 index 000000000..ec57b0253 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/FixedWidthText.java @@ -0,0 +1,38 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.client.font.FormatCodes; + +final class FixedWidthText extends DebugText { + + private final Format format = new Format(); + + @Override + protected float advance(char c, boolean bold) { + return bold ? 2.0f : 1.0f; + } + + @Override + public float measure(CharSequence text, int start, int end) { + final Format state = format; + state.reset(); + float x = 0.0f; + int i = start; + while (i < end) { + if (text.charAt(i) == FORMAT && i + 1 < end) { + i += applyFormat(text, i, end, state); + continue; + } + x += advance(text.charAt(i), (state.styles & 1 << FormatCodes.BOLD) != 0); + i++; + } + return x; + } + + @Override + public void draw(CharSequence text, int start, int end, float x, float y, int argb) { + } + + @Override + public void rect(int x1, int y1, int x2, int y2, int argb) { + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/FlatDebugDisplayerTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/FlatDebugDisplayerTest.java new file mode 100644 index 000000000..3e24ba384 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/FlatDebugDisplayerTest.java @@ -0,0 +1,48 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class FlatDebugDisplayerTest { + + private final FlatDebugDisplayer displayer = new FlatDebugDisplayer(TestRegistry.main()); + private final ArrayList left = new ArrayList<>(); + private final ArrayList right = new ArrayList<>(); + + @Test + void groupsRouteBySideWithSpacersBetweenNonEmptyBlocks() { + left.add("forge left"); + right.add("forge right"); + displayer.beginLines(); + displayer.line(DebugGroup.POSITION).text("pos"); + displayer.fact(DebugGroup.SHADERS, "Server").value(20).text(" ms"); + displayer.line(DebugGroup.SHADERS).text("shader line"); + displayer.line(DebugGroup.ANGELICA).text("renderer"); + displayer.fact(DebugGroup.ANGELICA, "Fog").value(1.5, 1); + displayer.line(DebugGroup.MEMORY).text("mem"); + displayer.endLines(); + displayer.addTo(left, right); + assertEquals(Arrays.asList("forge left", null, "pos", null, "Server: \u00a7f20\u00a7r ms", "shader line"), left); + assertEquals(Arrays.asList("forge right", null, "renderer", "Fog: \u00a7f1.5\u00a7r", null, "mem"), right); + } + + @Test + void priorityGoesFirstWithoutALeadingSpacerAndNoLinesAddNothing() { + displayer.beginLines(); + displayer.line(DebugGroup.PRIORITY_LEFT).text("Minecraft"); + displayer.line(DebugGroup.SHADERS).text("pack"); + displayer.endLines(); + displayer.addTo(left, right); + assertEquals(Arrays.asList("Minecraft", null, "pack"), left); + assertEquals(List.of(), right); + displayer.beginLines(); + displayer.endLines(); + displayer.addTo(left, right); + assertEquals(3, left.size()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/ForgeDebugPlaceholdersTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/ForgeDebugPlaceholdersTest.java new file mode 100644 index 000000000..e971ec25f --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/ForgeDebugPlaceholdersTest.java @@ -0,0 +1,62 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ForgeDebugPlaceholdersTest { + + private static final List BRANDINGS = Arrays.asList("MCP 9.05", "Minecraft Forge 10.13.4.1614", "42 mods loaded"); + private static final String[] LEFT = { "Minecraft ", "C: ", "E: ", "P: ", "Overworld", null, "x: ", "y: ", "z: ", "f: ", "lc: ", "ws: ", "shader: " }; + + private final ArrayList left = new ArrayList<>(); + private final ArrayList right = new ArrayList<>(); + + private static long nulls(List lines) { + return lines.stream().filter(s -> s == null).count(); + } + + @ParameterizedTest(name = "chunkLoaded={0} shader={1}") + @CsvSource({ "true, false", "false, false", "true, true" }) + void listsKeepTheVanillaShapeSoModIndicesStillLine(boolean chunkLoaded, boolean shader) { + ForgeDebugPlaceholders.add(left, right, chunkLoaded, shader, BRANDINGS); + assertEquals(shader ? 13 : 12, left.size()); + for (int i = 0; i < left.size(); i++) { + if (LEFT[i] == null || i == 10 && !chunkLoaded) { + assertNull(left.get(i), "left " + i); + } else { + assertTrue(left.get(i).startsWith(LEFT[i]), "left " + i + " = " + left.get(i)); + assertTrue(ForgeDebugPlaceholders.isPlaceholder(left.get(i)), "left " + i); + } + } + assertTrue(right.get(0).startsWith("Used memory: ")); + assertTrue(right.get(1).startsWith("Allocated memory: ")); + assertNull(right.get(2)); + assertEquals(BRANDINGS, right.subList(3, right.size())); + assertEquals(nulls(left), ForgeDebugPlaceholders.nullsAdded(0)); + assertEquals(nulls(right), ForgeDebugPlaceholders.nullsAdded(1)); + } + + @Test + void onlyTheAddedInstancesArePlaceholders() { + final String oldBranding = new String("Old branding"); + ForgeDebugPlaceholders.add(left, right, true, false, List.of(oldBranding)); + assertTrue(ForgeDebugPlaceholders.isPlaceholder(oldBranding)); + ForgeDebugPlaceholders.add(new ArrayList<>(), new ArrayList<>(), true, false, BRANDINGS); + assertFalse(ForgeDebugPlaceholders.isPlaceholder(oldBranding)); + assertTrue(ForgeDebugPlaceholders.isPlaceholder(BRANDINGS.get(0))); + assertFalse(ForgeDebugPlaceholders.isPlaceholder(new String(BRANDINGS.get(0)))); + assertFalse(ForgeDebugPlaceholders.isPlaceholder(new String(ForgeDebugPlaceholders.VERSION))); + assertFalse(ForgeDebugPlaceholders.isPlaceholder("JourneyMap")); + assertFalse(ForgeDebugPlaceholders.isPlaceholder(null)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/ForgeLineFilterTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/ForgeLineFilterTest.java new file mode 100644 index 000000000..59980e757 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/ForgeLineFilterTest.java @@ -0,0 +1,153 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.config.CompatConfig; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ForgeLineFilterTest { + + private static final List BRANDINGS = Arrays.asList("MCP 9.05", "FML v7.10.99", "Minecraft Forge 10.13.4.1614", "42 mods loaded"); + private static final String[] HODGEPODGE = { "Allocation rate: 3MB/s", "IntCache: 1, 2, 3", "LWJGL: 3.3.3", "Java: 21 64bit", "GPU: Apple M1", "OpenGL: 3.3", "CPU Cores: 8", "OS: Mac OS X" }; + + private final ArrayList left = new ArrayList<>(); + private final ArrayList right = new ArrayList<>(); + private final HashMap> out = new HashMap<>(); + private final ForgeLineFilter.Sink sink = (group, source, start, name) -> out.computeIfAbsent(group, g -> new ArrayList<>()) + .add(source == null ? "" : name == null ? source.substring(start) : name + "=" + source.substring(start)); + + private final DebugRegistry registry = new DebugRegistry(); + private ForgeLineFilter filter; + private DebugGroup memoryGroup; + private DebugGroup specsGroup; + private DebugGroup positionGroup; + private DebugGroup otherLeft; + private DebugGroup otherRight; + private DebugEntryDef memory; + private DebugEntryDef specs; + private DebugEntryDef gateRight; + private String[] savedRules; + + @BeforeEach + void setUp() { + savedRules = CompatConfig.debugLineRules; + CompatConfig.debugLineRules = new String[0]; + memoryGroup = TestRegistry.group(registry, "angelica:memory", DebugGroupHandle.RIGHT, 100); + specsGroup = TestRegistry.group(registry, "angelica:system_specs", DebugGroupHandle.RIGHT, 200); + otherRight = TestRegistry.group(registry, "angelica:other_right", DebugGroupHandle.RIGHT, 800); + positionGroup = TestRegistry.group(registry, "angelica:position", DebugGroupHandle.LEFT, 200); + otherLeft = TestRegistry.group(registry, "angelica:other_left", DebugGroupHandle.LEFT, 800); + memory = registry.registerEntry(TestRegistry.text("minecraft:memory")); + specs = registry.registerEntry(TestRegistry.text("minecraft:system_specs")); + registry.registerEntry(TestRegistry.text("minecraft:player_position")); + registry.registerEntry(TestRegistry.text("angelica:other_left")); + gateRight = registry.registerEntry(TestRegistry.text("angelica:other_right")); + registry.freeze(); + for (int i = 0; i < registry.entryCount(); i++) registry.entryAt(i).enabled = true; + filter = new ForgeLineFilter(registry); + ForgeDebugPlaceholders.add(left, right, true, false, BRANDINGS); + } + + @AfterEach + void restoreRules() { + CompatConfig.debugLineRules = savedRules; + } + + private void sample() { + out.clear(); + filter.sample(left, right, sink); + } + + private List in(DebugGroup group) { + return out.getOrDefault(group.index(), Collections.emptyList()); + } + + private static List list(String... lines) { + return Arrays.asList(lines); + } + + @Test + void placeholdersAloneProduceNothingButEqualContentCopiesAreThirdParty() { + sample(); + assertTrue(out.isEmpty()); + final String copy = new String(ForgeDebugPlaceholders.X); + left.set(left.indexOf(ForgeDebugPlaceholders.X), copy); + sample(); + assertEquals(list(copy), in(otherLeft)); + } + + @Test + void defaultsDropAndRouteWhileTheirEntriesAreEnabled() { + right.addAll(Arrays.asList(HODGEPODGE)); + sample(); + assertEquals(list("IntCache=1, 2, 3"), in(memoryGroup)); + assertEquals(list("LWJGL=3.3.3", "OS=Mac OS X"), in(specsGroup)); + assertEquals(2, out.size()); + specs.enabled = false; + memory.enabled = false; + sample(); + assertEquals(list(HODGEPODGE), in(otherRight)); + assertEquals(1, out.size()); + } + + @Test + void otherGatesDropOnlyOtherLinesAndKeepRoutedFacts() { + left.addAll(List.of("Cube X: 4", "mod a")); + right.addAll(List.of("IntCache: 1", "mod r")); + gateRight.enabled = false; + sample(); + assertEquals(list("Cube X=4"), in(positionGroup)); + assertEquals(list("mod a"), in(otherLeft)); + assertEquals(list("IntCache=1"), in(memoryGroup)); + assertEquals(Collections.emptyList(), in(otherRight)); + } + + @Test + void forgeNullsAreConsumedAndOtherBlanksSeparate() { + CompatConfig.debugLineRules = new String[] { "drop right \"dropped\"" }; + left.add(1, "pack 1.0"); + left.addAll(Arrays.asList("mod a", null, "mod b")); + right.addAll(Arrays.asList(null, "", "a", null, "dropped 1", " ", "\u00a7r", "b", "IntCache: 1", "", "dropped 2", null)); + sample(); + assertEquals(list("pack 1.0", "mod a", "", "mod b"), in(otherLeft)); + assertEquals(list("a", "", "b"), in(otherRight)); + assertEquals(list("IntCache=1"), in(memoryGroup)); + left.clear(); + left.addAll(Arrays.asList("mod a", null, "mod b")); + sample(); + assertEquals(list("mod a", "", "mod b"), in(otherLeft)); + } + + @Test + void groupGetsTheLeftListThenTheRightListInListOrder() { + CompatConfig.debugLineRules = new String[] { "fact right \"R\" group=angelica:position name=Right", "fact left \"L\" group=angelica:position name=Left" }; + right.addAll(List.of("R1", "R2")); + left.addAll(List.of("L1", "L2")); + sample(); + assertEquals(list("Left=1", "Left=2", "Right=1", "Right=2"), in(positionGroup)); + } + + @Test + void replacingTheConfigArrayRecompiles() { + left.addAll(List.of("mod a", "mod b")); + sample(); + assertEquals(list("mod a", "mod b"), in(otherLeft)); + CompatConfig.debugLineRules = new String[] { "drop left \"mod a\"", "fact left \"mod \" group=angelica:position name=Mod" }; + sample(); + assertEquals(Collections.emptyList(), in(otherLeft)); + assertEquals(list("Mod=b"), in(positionGroup)); + CompatConfig.debugLineRules = new String[0]; + sample(); + assertEquals(list("mod a", "mod b"), in(otherLeft)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/RecordingDisplayer.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/RecordingDisplayer.java new file mode 100644 index 000000000..aff6a275d --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/RecordingDisplayer.java @@ -0,0 +1,52 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugFact; +import com.gtnewhorizons.angelica.api.debug.DebugFactLines; +import com.gtnewhorizons.angelica.api.debug.DebugGroupHandle; +import com.gtnewhorizons.angelica.api.debug.DebugScreenDisplayer; + +import java.util.ArrayList; +import java.util.List; + +public final class RecordingDisplayer implements DebugScreenDisplayer { + + public static final DebugGroupHandle GROUP = new DebugGroupHandle() { + + @Override + public String id() { + return "test:group"; + } + + @Override + public int side() { + return LEFT; + } + }; + + public final List names = new ArrayList<>(); + public final List texts = new ArrayList<>(); + private final DebugFactImpl fact = new DebugFactImpl(); + + @Override + public DebugFact line(DebugGroupHandle group) { + return fact(group, null); + } + + @Override + public DebugFact fact(DebugGroupHandle group, String name) { + final StringBuilder text = new StringBuilder(); + names.add(name); + texts.add(text); + return fact.intoPlain(text); + } + + public static RecordingDisplayer of(DebugFactLines facts) { + final RecordingDisplayer displayer = new RecordingDisplayer(); + facts.emit(displayer, GROUP); + return displayer; + } + + public String text(int index) { + return texts.get(index).toString(); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/TestRegistry.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/TestRegistry.java new file mode 100644 index 000000000..869288c79 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/TestRegistry.java @@ -0,0 +1,29 @@ +package com.gtnewhorizons.angelica.debug.screen; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugGroupSpec; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; + +final class TestRegistry { + + private TestRegistry() {} + + static synchronized DebugRegistry main() { + DebugScreen.bootstrap(); + DebugRegistry.MAIN.freeze(); + return DebugRegistry.MAIN; + } + + static DebugEntrySpec text(String id) { + return new DebugEntrySpec(id, DebugEntryCategory.SCREEN_TEXT).defaults(DebugScreenEntryStatus.IN_OVERLAY, DebugScreenEntryStatus.NEVER); + } + + static DebugEntrySpec renderer(String id) { + return new DebugEntrySpec(id, DebugEntryCategory.RENDERER).defaults(DebugScreenEntryStatus.NEVER, DebugScreenEntryStatus.NEVER); + } + + static DebugGroup group(DebugRegistry registry, String id, int side, int order) { + return registry.registerGroup(new DebugGroupSpec(id).side(side).order(order)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AllocationRateTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AllocationRateTest.java new file mode 100644 index 000000000..71ab128a2 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AllocationRateTest.java @@ -0,0 +1,40 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AllocationRateTest { + + @Test + void gcCountChangeKeepsPreviousRate() { + final MemoryEntry.AllocationRate rate = new MemoryEntry.AllocationRate(); + rate.sample(1000L, 1_000L, 3L); + rate.sample(1500L, 2_000L, 3L); + rate.sample(2000L, 500L, 4L); + assertEquals(2_000L, rate.rate()); + rate.sample(2500L, 1_000L, 4L); + assertEquals(1_000L, rate.rate()); + } + + @Test + void sampleCloserThanHalfASecondChangesNothing() { + final MemoryEntry.AllocationRate rate = new MemoryEntry.AllocationRate(); + rate.sample(1000L, 1_000L, 3L); + rate.sample(1200L, 9_000L, 3L); + assertEquals(0L, rate.rate()); + rate.sample(1500L, 2_000L, 3L); + assertEquals(2_000L, rate.rate()); + } + + @Test + void gapOverFiveSecondsResetsTheBaselineWithoutARate() { + final MemoryEntry.AllocationRate rate = new MemoryEntry.AllocationRate(); + rate.sample(1000L, 1_000L, 3L); + rate.sample(1500L, 2_000L, 3L); + rate.sample(9000L, 90_000L, 3L); + assertEquals(2_000L, rate.rate()); + rate.sample(9500L, 92_000L, 3L); + assertEquals(4_000L, rate.rate()); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaRendererEntryTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaRendererEntryTest.java new file mode 100644 index 000000000..9e553a567 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaRendererEntryTest.java @@ -0,0 +1,32 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import com.gtnewhorizons.angelica.api.debug.DebugFactLines; +import com.gtnewhorizons.angelica.debug.screen.RecordingDisplayer; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AngelicaRendererEntryTest { + + private static List render(String line) { + final DebugFactLines facts = new DebugFactLines(); + facts.begin(); + AngelicaRendererEntry.addRendererLine(facts, line); + facts.end(); + final RecordingDisplayer displayer = RecordingDisplayer.of(facts); + final List rendered = new ArrayList<>(); + for (int i = 0; i < displayer.texts.size(); i++) { + final String name = displayer.names.get(i); + rendered.add(name == null ? displayer.text(i) : name + ": " + displayer.text(i)); + } + return rendered; + } + + @Test + void bufferLineSplitsIntoGeometryAndIndex() { + assertEquals(List.of("Geometry: 17/28 MiB", "Index: 0/0 MiB (10 buffers)"), render("G: 17/28, I: 0/0 MiB (10 buffers)")); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaShadersEntryTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaShadersEntryTest.java new file mode 100644 index 000000000..5579adcfb --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/entries/AngelicaShadersEntryTest.java @@ -0,0 +1,20 @@ +package com.gtnewhorizons.angelica.debug.screen.entries; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AngelicaShadersEntryTest { + + @ParameterizedTest + @CsvSource(delimiter = '|', nullValues = "NULL", value = { + "[AngelicaShaders] Shadow Maps: not used|Shadow Maps: not used", + "[AngelicaShaders - Shadow Pass]|''", + " Shadow Entities: 3|Shadow Entities: 3", + "Profile: HIGH|Profile: HIGH", + "NULL|''"}) + void stripTag(String line, String expected) { + assertEquals(expected, AngelicaShadersEntry.stripTag(line)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/CubeWalkerTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/CubeWalkerTest.java new file mode 100644 index 000000000..ab57c5379 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/CubeWalkerTest.java @@ -0,0 +1,54 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CubeWalkerTest { + + private static List reference(int cx, int cy, int cz, int radius, int minY, int maxY) { + final List out = new ArrayList<>(); + for (int r = 0; r <= radius; r++) { + for (int dx = -r; dx <= r; dx++) { + for (int dz = -r; dz <= r; dz++) { + final boolean edge = dx == -r || dx == r || dz == -r || dz == r; + for (int dy = -r; dy <= r; dy++) { + if (!edge && dy != -r && dy != r) continue; + if (cy + dy < minY || cy + dy > maxY) continue; + out.add(cx + dx); + out.add(cy + dy); + out.add(cz + dz); + } + } + } + } + return out; + } + + private static List walk(int cx, int cy, int cz, int radius, int stopAfter) { + final List out = new ArrayList<>(); + CubeWalker.walk(cx, cy, cz, radius, (x, y, z) -> { + out.add(x); + out.add(y); + out.add(z); + return out.size() / 3 != stopAfter; + }); + return out; + } + + @ParameterizedTest + @CsvSource({"10,100,-7,3", "0,1,0,3", "0,254,0,3", "5,6,7,0", "0,300,0,50"}) + void matchesShellOrderAndClampsToWorldHeight(int cx, int cy, int cz, int radius) { + assertEquals(reference(cx, cy, cz, radius, 0, 255), walk(cx, cy, cz, radius, -1)); + } + + @Test + void stopsWhenSinkReturnsFalse() { + assertEquals(10, walk(0, 64, 0, 3, 10).size() / 3); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabelBatchTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabelBatchTest.java new file mode 100644 index 000000000..5a928d44f --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugLabelBatchTest.java @@ -0,0 +1,38 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DebugLabelBatchTest { + + @ParameterizedTest + @CsvSource({ + "0,0,-5,true", + "0,0,5,false", + "0,0,-30,true", + "0,0,-31,false", + "3.9,0,-5,true", + "4.1,0,-5,false", + "-4.1,0,-5,false", + "0,-4,-5,true", + "0,4.2,-5,false"}) + void visibility(float x, float y, float z, boolean expected) { + assertEquals(expected, DebugLabelBatch.isVisible(x, y, z, 0.06F, 0.16F, 1.4F, 1.4F, 1080)); + } + + @Test + void digitsAreCenteredUsingPerDigitAdvance() { + final float[] advance = new float[10]; + Arrays.fill(advance, 6.0F); + advance[1] = 4.0F; + assertEquals(10.0F, DebugLabelBatch.stringWidth(15, advance), 0.0F); + assertEquals(-5.0F, DebugLabelBatch.digitLeft(15, 0, advance), 0.0F); + assertEquals(-1.0F, DebugLabelBatch.digitLeft(15, 1, advance), 0.0F); + assertEquals(-3.0F, DebugLabelBatch.digitLeft(7, 0, advance), 0.0F); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshDataTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshDataTest.java new file mode 100644 index 000000000..6fec518c9 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshDataTest.java @@ -0,0 +1,173 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.debug.render.NativeScratch; +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugMeshData.LineWidth; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetByte; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memGetFloat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugMeshDataTest { + + private static final int ANCHOR = 30_000_000; + private static final int THIN = LineWidth.THIN.ordinal(); + + private final DebugMeshData mesh = new DebugMeshData(); + private final DebugMeshData other = new DebugMeshData(); + + @AfterEach + void release() { + mesh.free(); + other.free(); + } + + private static void emit(DebugMeshData target, double x, LineWidth width) { + target.begin(ANCHOR, 64, ANCHOR); + target.quad(ANCHOR, 64, ANCHOR, ANCHOR + 1, 64, ANCHOR, ANCHOR + 1, 65, ANCHOR, ANCHOR, 65, ANCHOR, 0x80FF0000); + target.line(width, x, 64.5, ANCHOR, ANCHOR + 2, 66, ANCHOR + 3, 0xFF00FF00); + target.label(ANCHOR + 0.5, 64.25, ANCHOR + 0.5, 7, 0xFFFFFF00); + } + + @Test + void farCoordinatesKeepSubBlockPrecision() { + mesh.begin(ANCHOR, 64, ANCHOR); + mesh.line(LineWidth.THIN, 30_000_005.002, 64, ANCHOR + 0.002, ANCHOR - 0.3, 64, ANCHOR, -1); + final long p = mesh.lineAddress(THIN); + assertEquals(5.002F, memGetFloat(p), 1e-6F); + assertEquals(0.0F, memGetFloat(p + 4), 0.0F); + assertEquals(0.002F, memGetFloat(p + 8), 1e-7F); + assertEquals(-0.3F, memGetFloat(p + 16), 1e-7F); + } + + @Test + void identicalEmissionGivesIdenticalHash() { + emit(mesh, ANCHOR + 0.25, LineWidth.THIN); + emit(other, ANCHOR + 0.25, LineWidth.THIN); + assertEquals(mesh.hash(), other.hash()); + } + + private static void emitVariant(DebugMeshData target, int anchorX, double x, LineWidth width, boolean onTop, int color, boolean label) { + target.begin(anchorX, 0, 0); + target.alwaysOnTop(onTop); + target.line(width, x, 0, 0, 1, 1, 1, color); + if (label) target.label(1, 2, 3, 7, -1); + } + + static Stream hashVariants() { + return Stream.of( + Arguments.of(0, 0.375, LineWidth.THIN, false, 0xFF00FF00, false, "coordinate"), + Arguments.of(0, 0.25, LineWidth.THIN, false, 0xFF0000FF, false, "color"), + Arguments.of(0, 0.25, LineWidth.THICK, false, 0xFF00FF00, false, "width bucket"), + Arguments.of(0, 0.25, LineWidth.THIN, true, 0xFF00FF00, false, "on-top bucket"), + Arguments.of(0, 0.25, LineWidth.THIN, false, 0xFF00FF00, true, "label"), + Arguments.of(1, 0.25, LineWidth.THIN, false, 0xFF00FF00, false, "anchor")); + } + + @ParameterizedTest(name = "{6}") + @MethodSource("hashVariants") + void changedEmissionChangesHash(int anchorX, double x, LineWidth width, boolean onTop, int color, boolean label, String what) { + emitVariant(mesh, 0, 0.25, LineWidth.THIN, false, 0xFF00FF00, false); + emitVariant(other, anchorX, x, width, onTop, color, label); + assertNotEquals(mesh.hash(), other.hash()); + } + + @Test + void alwaysOnTopRoutesLinesOnly() { + mesh.begin(0, 0, 0); + mesh.alwaysOnTop(true); + mesh.line(LineWidth.THIN, 0, 0, 0, 1, 1, 1, -1); + mesh.quad(0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, -1); + assertEquals(0, mesh.lineVertexCount(THIN)); + assertEquals(2, mesh.lineVertexCount(THIN + DebugMeshData.WIDTH_COUNT)); + assertEquals(32, mesh.lineBytes(THIN + DebugMeshData.WIDTH_COUNT)); + assertEquals(4, mesh.fillVertexCount()); + mesh.alwaysOnTop(false); + mesh.line(LineWidth.MEDIUM, 0, 0, 0, 1, 1, 1, -1); + assertEquals(2, mesh.lineVertexCount(LineWidth.MEDIUM.ordinal())); + assertEquals(0, mesh.lineVertexCount(LineWidth.MEDIUM.ordinal() + DebugMeshData.WIDTH_COUNT)); + } + + @ParameterizedTest(name = "side {0}") + @CsvSource({"0,0,-1,0", "1,0,1,0", "2,0,0,-1", "3,0,0,1", "4,-1,0,0", "5,1,0,0"}) + void faceWindsCounterClockwiseSeenFromOutside(int side, int nx, int ny, int nz) { + mesh.begin(0, 0, 0); + mesh.face(side, 0, 0, 0, 1, 1, 1, 0.0, -1); + final NativeScratch out = new NativeScratch(4 * DebugMeshData.STRIDE); + try { + out.ensure(4 * DebugMeshData.STRIDE); + mesh.copySection(DebugMeshData.FILL_SECTION, out.address()); + final float[] v = new float[12]; + for (int i = 0; i < 12; i++) v[i] = memGetFloat(out.address() + (long) (i / 3) * DebugMeshData.STRIDE + (i % 3) * 4L); + assertTrue(outward(v, 0, 1, 2, nx, ny, nz), "first triangle faces inward"); + assertTrue(outward(v, 0, 2, 3, nx, ny, nz), "second triangle faces inward"); + } finally { + out.free(); + } + } + + private static boolean outward(float[] v, int a, int b, int c, int nx, int ny, int nz) { + final float ex = v[b * 3] - v[a * 3], ey = v[b * 3 + 1] - v[a * 3 + 1], ez = v[b * 3 + 2] - v[a * 3 + 2]; + final float fx = v[c * 3] - v[b * 3], fy = v[c * 3 + 1] - v[b * 3 + 1], fz = v[c * 3 + 2] - v[b * 3 + 2]; + return (ey * fz - ez * fy) * nx + (ez * fx - ex * fz) * ny + (ex * fy - ey * fx) * nz > 0.0F; + } + + @Test + void labelsAreAnchorRelativeAndClamped() { + mesh.begin(ANCHOR, 64, ANCHOR); + mesh.label(ANCHOR + 0.5, 64.25, ANCHOR - 0.5, 150, 0xFF112233); + mesh.label(ANCHOR, 64, ANCHOR, -5, 0xFF445566); + mesh.label(ANCHOR, 64, ANCHOR, 42, 0xFF778899); + assertEquals(3, mesh.labels.count); + assertEquals(0.5F, mesh.labels.pos[0], 0.0F); + assertEquals(0.25F, mesh.labels.pos[1], 0.0F); + assertEquals(-0.5F, mesh.labels.pos[2], 0.0F); + assertEquals(99, mesh.labels.value[0]); + assertEquals(0, mesh.labels.value[1]); + assertEquals(42, mesh.labels.value[2]); + assertEquals(0xFF112233, mesh.labels.color[0]); + assertEquals(0xFF778899, mesh.labels.color[2]); + } + + @Test + void beginResetsCountsAndOnTop() { + emit(mesh, ANCHOR + 0.25, LineWidth.THIN); + mesh.alwaysOnTop(true); + mesh.begin(1, 2, 3); + assertEquals(1, mesh.anchorX()); + assertEquals(2, mesh.anchorY()); + assertEquals(3, mesh.anchorZ()); + assertEquals(0, mesh.fillVertexCount()); + assertEquals(0, mesh.fillBytes()); + assertEquals(0, mesh.labels.count); + for (int i = 0; i < DebugMeshData.BUCKET_COUNT; i++) assertEquals(0, mesh.lineVertexCount(i)); + mesh.line(LineWidth.THIN, 0, 0, 0, 1, 1, 1, -1); + assertEquals(2, mesh.lineVertexCount(THIN)); + } + + @Test + void growthPreservesEarlierVertices() { + mesh.begin(0, 0, 0); + for (int i = 0; i < 1000; i++) mesh.line(LineWidth.THICK, i, 1, 2, i, 3, 4, i); + final int bucket = LineWidth.THICK.ordinal(); + final long p = mesh.lineAddress(bucket); + assertEquals(2000, mesh.lineVertexCount(bucket)); + assertEquals(32000, mesh.lineBytes(bucket)); + assertEquals(0.0F, memGetFloat(p), 0.0F); + assertEquals(1.0F, memGetFloat(p + 4), 0.0F); + assertEquals(3.0F, memGetFloat(p + 16 + 4), 0.0F); + assertEquals(127.0F, memGetFloat(p + 127L * 32), 0.0F); + assertEquals(128.0F, memGetFloat(p + 128L * 32), 0.0F); + assertEquals(999.0F, memGetFloat(p + 999L * 32), 0.0F); + assertEquals((byte) 231, memGetByte(p + 999L * 32 + 14)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshGLTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshGLTest.java new file mode 100644 index 000000000..6136e006c --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugMeshGLTest.java @@ -0,0 +1,326 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFlags; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; +import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement; +import com.gtnewhorizons.angelica.debug.render.DebugDrawScope; +import com.gtnewhorizons.angelica.debug.render.DebugGeometry; +import com.gtnewhorizons.angelica.debug.render.DebugProgram; +import com.gtnewhorizons.angelica.debug.render.DebugPrograms; +import com.gtnewhorizons.angelica.debug.render.NativeScratch; +import com.gtnewhorizons.angelica.debug.screen.renderers.DebugMeshData.LineWidth; +import com.gtnewhorizons.angelica.glsm.GLCoreTest; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.GlTestSupport; +import com.gtnewhorizons.angelica.glsm.ffp.FfpFixture; +import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; +import com.gtnewhorizons.angelica.glsm.states.BlendState; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; +import org.joml.Matrix4f; +import org.lwjgl.BufferUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL15; +import org.lwjgl.opengl.GL20; +import org.lwjgl.opengl.GL30; + +import java.nio.ByteBuffer; + +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutFloat; +import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memPutInt; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@GLCoreTest +class DebugMeshGLTest { + + private static final int SIZE = 64; + private static final int BACKGROUND = 0xFF000000; + private static final int WHITE = 0xFFFFFFFF; + + private final DebugMeshData data = new DebugMeshData(); + private final DebugMesh mesh = new DebugMesh(); + private final Matrix4f base = new Matrix4f(); + private final NativeScratch uploadScratch = new NativeScratch(0x4000); + private final DebugDrawScope scope = new DebugDrawScope(); + private int vao; + private int vbo; + + @BeforeEach + void setUp() { + FfpFixture.resetFfpState(); + ShaderManager.enable(); + ShaderManager.getInstance().activate(); + GLStateManager.glViewport(0, 0, SIZE, SIZE); + GLStateManager.glMatrixMode(GL11.GL_PROJECTION); + GLStateManager.glLoadIdentity(); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glLoadIdentity(); + } + + @AfterEach + void tearDown() { + GLStateManager.glBindVertexArray(0); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); + mesh.delete(); + data.free(); + uploadScratch.free(); + if (vbo != 0) GLStateManager.glDeleteBuffers(vbo); + if (vao != 0) GLStateManager.glDeleteVertexArrays(vao); + FfpFixture.resetFfpState(); + } + + private static float pixelCenter(int pixel) { + return (pixel + 0.5F) * 2.0F / SIZE - 1.0F; + } + + private static int litInColumn(int[] pixels, int x) { + int lit = 0; + for (int row = 0; row < SIZE; row++) { + if (pixels[row * SIZE + x] != BACKGROUND) lit++; + } + return lit; + } + + private void drawMeshPasses(boolean onTop, double camX) { + mesh.prepare(base, camX, 0.0, 0.0); + mesh.drawPlain(onTop, DebugMesh.useColor()); + mesh.drawWide(onTop, DebugMesh.useLines()); + } + + private void drawMesh(boolean onTop, double camX) { + scope.beginWorld(); + try { + drawMeshPasses(onTop, camX); + } finally { + scope.end(); + } + } + + private int[] draw(boolean onTop) { + FfpFixture.clear(); + drawMesh(onTop, 0.0); + return FfpFixture.readRegion(SIZE); + } + + @ParameterizedTest + @CsvSource({"THICK,4", "MEDIUM,2", "THIN,1"}) + void lineWidthSetsPixelRows(LineWidth width, int rows) { + final double y = pixelCenter(16); + data.begin(0, 0, 0); + data.line(width, -0.8, y, 0.0, 0.8, y, 0.0, WHITE); + mesh.upload(data, uploadScratch); + assertEquals(rows, litInColumn(draw(false), SIZE / 2)); + } + + @Test + void anchorIsSubtractedFromTheCamera() { + data.begin(1000, 0, 0); + data.line(LineWidth.THICK, 999.2, 0.0, 0.0, 1000.8, 0.0, 0.0, WHITE); + mesh.upload(data, uploadScratch); + FfpFixture.clear(); + drawMesh(false, 1000.0); + assertEquals(4, litInColumn(FfpFixture.readRegion(SIZE), SIZE / 2)); + } + + @Test + void fillColorBytesAreRedGreenBlueAlpha() { + data.begin(0, 0, 0); + data.quad(-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, 0xFFFF0000); + mesh.upload(data, uploadScratch); + FfpFixture.clear(); + drawMesh(false, 0.0); + assertArrayEquals(new int[] { 255, 0, 0, 255 }, FfpFixture.readPixel(SIZE / 2, SIZE / 2)); + assertArrayEquals(new int[] { 0, 0, 0, 255 }, FfpFixture.readPixel(2, 2)); + } + + @Test + void fillCullsQuadsWoundClockwise() { + data.begin(0, 0, 0); + data.quad(-0.5, 0.5, 0.0, 0.5, 0.5, 0.0, 0.5, -0.5, 0.0, -0.5, -0.5, 0.0, 0xFFFF0000); + mesh.upload(data, uploadScratch); + FfpFixture.clear(); + drawMesh(false, 0.0); + assertArrayEquals(new int[] { 0, 0, 0, 255 }, FfpFixture.readPixel(SIZE / 2, SIZE / 2)); + } + + @Test + void onTopGeometryDrawsOnlyInTheOnTopPass() { + data.begin(0, 0, 0); + data.quad(-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, 0xFFFF0000); + data.alwaysOnTop(true); + data.line(LineWidth.THICK, -0.8, 0.75, 0.0, 0.8, 0.75, 0.0, WHITE); + mesh.upload(data, uploadScratch); + final int[] normal = draw(false); + final int[] top = draw(true); + assertEquals(SIZE / 2, litInColumn(normal, SIZE / 2)); + assertEquals(4, litInColumn(top, SIZE / 2)); + } + + @Test + void reuploadReplacesTheGeometry() { + data.begin(0, 0, 0); + data.line(LineWidth.THICK, -0.8, 0.0, 0.0, 0.8, 0.0, 0.0, WHITE); + data.line(LineWidth.THICK, -0.8, 0.5, 0.0, 0.8, 0.5, 0.0, WHITE); + mesh.upload(data, uploadScratch); + assertEquals(8, litInColumn(draw(false), SIZE / 2)); + data.begin(0, 0, 0); + data.line(LineWidth.THICK, -0.8, 0.0, 0.0, 0.8, 0.0, 0.0, WHITE); + mesh.upload(data, uploadScratch); + assertEquals(4, litInColumn(draw(false), SIZE / 2)); + data.begin(0, 0, 0); + mesh.upload(data, uploadScratch); + assertEquals(0, litInColumn(draw(false), SIZE / 2)); + } + + @Test + void scopeRestoresCallerState() { + data.begin(0, 0, 0); + data.quad(-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, 0xFFFF0000); + data.line(LineWidth.THICK, -0.8, 0.0, 0.0, 0.8, 0.0, 0.0, WHITE); + data.line(LineWidth.THIN, -0.8, 0.5, 0.0, 0.8, 0.5, 0.0, WHITE); + mesh.upload(data, uploadScratch); + + final int program = GlTestSupport.program(GlTestSupport.TRIVIAL_VS, GlTestSupport.TRIVIAL_FS); + final int ambient = GLStateManager.glGenTextures(); + final int glyphs = GLStateManager.glGenTextures(); + vao = GLStateManager.glGenVertexArrays(); + GLStateManager.glBindVertexArray(vao); + vbo = GLStateManager.glGenBuffers(); + GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + try { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, ambient); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + GLStateManager.enableDepthTest(); + GLStateManager.glDepthFunc(GL11.GL_ALWAYS); + GLStateManager.glDepthMask(true); + GLStateManager.disableBlend(); + GLStateManager.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE, GL11.GL_ZERO, GL11.GL_ONE); + GLStateManager.enableCull(); + GLStateManager.glCullFace(GL11.GL_FRONT); + GLStateManager.glFrontFace(GL11.GL_CW); + GLStateManager.enableScissorTest(); + GLStateManager.glScissor(0, 0, SIZE, SIZE); + GLStateManager.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL); + GLStateManager.glPolygonMode(GL11.GL_BACK, GL11.GL_LINE); + GLStateManager.glLineWidth(3.0F); + GLStateManager.glUseProgram(program); + + final long drawCalls = GLStateManager.drawCalls; + scope.beginWorld(); + try { + drawMeshPasses(false, 0.0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, glyphs); + } finally { + scope.end(); + } + + assertTrue(GLStateManager.drawCalls > drawCalls); + assertTrue(GLStateManager.getDepthTest().isEnabled()); + assertEquals(GL11.GL_ALWAYS, GLStateManager.getDepthState().getFunc()); + assertTrue(GLStateManager.getDepthState().isEnabled()); + assertFalse(GLStateManager.getBlendMode().isEnabled()); + final BlendState blend = GLStateManager.getBlendState(); + assertEquals(GL11.GL_ONE, blend.getSrcRgb()); + assertEquals(GL11.GL_ONE, blend.getDstRgb()); + assertEquals(GL11.GL_ZERO, blend.getSrcAlpha()); + assertEquals(GL11.GL_ONE, blend.getDstAlpha()); + assertTrue(GLStateManager.getCullState().isEnabled()); + assertEquals(GL11.GL_FRONT, GLStateManager.getPolygonState().getCullFaceMode()); + assertEquals(GL11.GL_CW, GLStateManager.getPolygonState().getFrontFace()); + assertTrue(GLStateManager.getScissorTest().isEnabled()); + assertEquals(GL11.GL_FILL, GLStateManager.getPolygonState().getFrontMode()); + assertEquals(GL11.GL_LINE, GLStateManager.getPolygonState().getBackMode()); + assertEquals(3.0F, GLStateManager.getLineState().getWidth()); + assertEquals(1, GLStateManager.getActiveTextureUnit()); + assertEquals(ambient, GLStateManager.getTextures().getTextureUnitBindings(0).getBinding()); + assertEquals(ambient, GlTestSupport.driverTextureBinding(0)); + assertEquals(program, GLStateManager.getActiveProgram()); + assertEquals(program, GL11.glGetInteger(GL20.GL_CURRENT_PROGRAM)); + assertEquals(vao, GLStateManager.getBoundVAO()); + assertEquals(vbo, GLStateManager.getBoundVBO()); + assertEquals(vao, GL11.glGetInteger(GL30.GL_VERTEX_ARRAY_BINDING)); + assertEquals(vbo, GL11.glGetInteger(GL15.GL_ARRAY_BUFFER_BINDING)); + assertEquals(GL11.GL_NO_ERROR, GL11.glGetError()); + } finally { + GLStateManager.glUseProgram(0); + GLStateManager.glDeleteProgram(program); + GLStateManager.glLineWidth(1.0F); + GLStateManager.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL); + GLStateManager.disableScissorTest(); + GLStateManager.glCullFace(GL11.GL_BACK); + GLStateManager.glFrontFace(GL11.GL_CCW); + GLStateManager.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ZERO, GL11.GL_ONE, GL11.GL_ZERO); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + GLStateManager.glDeleteTextures(ambient); + GLStateManager.glDeleteTextures(glyphs); + } + } + + @Test + void texturedProgramSamplesUnitZero() { + final int flags = VertexFlags.TEXTURE_BIT | VertexFlags.COLOR_BIT; + final VertexFormat format = DefaultVertexFormat.ALL_FORMATS[flags]; + final int stride = format.getVertexSize(); + final int uvOffset = DebugGeometry.offsetOf(format, VertexFormatElement.Usage.PRIMARY_UV); + final int colorOffset = DebugGeometry.offsetOf(format, VertexFormatElement.Usage.COLOR); + final ByteBuffer texels = BufferUtils.createByteBuffer(2 * 2 * 4); + texels.put(new byte[] { (byte) 255, 0, 0, (byte) 255, 0, (byte) 255, 0, (byte) 255, 0, 0, (byte) 255, (byte) 255, (byte) 255, (byte) 255, 0, (byte) 255 }).flip(); + final int texture = GLStateManager.glGenTextures(); + final NativeScratch vertices = new NativeScratch(4 * stride); + try { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 2, 2, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, texels); + vertices.ensure(4 * stride); + final long p = vertices.address(); + texturedVertex(p, -1.0F, -1.0F, 0.0F, 0.0F, uvOffset, colorOffset); + texturedVertex(p + stride, 1.0F, -1.0F, 1.0F, 0.0F, uvOffset, colorOffset); + texturedVertex(p + 2L * stride, 1.0F, 1.0F, 1.0F, 1.0F, uvOffset, colorOffset); + texturedVertex(p + 3L * stride, -1.0F, 1.0F, 0.0F, 1.0F, uvOffset, colorOffset); + + FfpFixture.clear(); + scope.beginWorld(); + try { + final DebugProgram textured = DebugPrograms.textured(); + textured.use(); + textured.mvp(base.identity()); + TessellatorStreamingDrawer.drawPacked(vertices.window(4 * stride), GL11.GL_QUADS, flags, 4); + } finally { + scope.end(); + } + + assertArrayEquals(new int[] { 255, 0, 0, 255 }, FfpFixture.readPixel(SIZE / 4, SIZE / 4)); + assertArrayEquals(new int[] { 0, 255, 0, 255 }, FfpFixture.readPixel(3 * SIZE / 4, SIZE / 4)); + assertArrayEquals(new int[] { 0, 0, 255, 255 }, FfpFixture.readPixel(SIZE / 4, 3 * SIZE / 4)); + assertArrayEquals(new int[] { 255, 255, 0, 255 }, FfpFixture.readPixel(3 * SIZE / 4, 3 * SIZE / 4)); + assertEquals(GL11.GL_NO_ERROR, GL11.glGetError()); + } finally { + vertices.free(); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + GLStateManager.glDeleteTextures(texture); + } + } + + private static void texturedVertex(long p, float x, float y, float u, float v, int uvOffset, int colorOffset) { + memPutFloat(p, x); + memPutFloat(p + 4, y); + memPutFloat(p + 8, 0.0F); + memPutFloat(p + uvOffset, u); + memPutFloat(p + uvOffset + 4, v); + memPutInt(p + colorOffset, -1); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderSlotsTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderSlotsTest.java new file mode 100644 index 000000000..bd8519824 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugRenderSlotsTest.java @@ -0,0 +1,208 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import com.gtnewhorizons.angelica.api.debug.DebugEntryCategory; +import com.gtnewhorizons.angelica.api.debug.DebugEntrySpec; +import com.gtnewhorizons.angelica.api.debug.DebugScreenEntryStatus; +import com.gtnewhorizons.angelica.debug.screen.DebugEntryDef; +import com.gtnewhorizons.angelica.debug.screen.DebugRegistry; +import com.gtnewhorizons.angelica.debug.screen.DebugScreenEntries; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DebugRenderSlotsTest { + + private static final long MILLI = 1_000_000L; + private static final int[] KINDS = { DebugScreenEntries.KIND_CHUNK_BORDERS, DebugScreenEntries.KIND_VISUALIZE_HEIGHTMAP, DebugScreenEntries.KIND_VISUALIZE_SOLID_FACES }; + + private static final class FakeRenderer implements DebugWorldRenderer { + + int rebuilds; + int min = 250; + int fallback = 2000; + int content = -1; + boolean watches; + boolean fails; + Granularity granularity = Granularity.BLOCK; + + @Override + public void rebuild(ScanState scan, DebugMeshData mesh) { + rebuilds++; + if (fails) throw new IllegalStateException("rebuild failed"); + mesh.line(DebugMeshData.LineWidth.THIN, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, content); + if (watches) scan.watch(0, 0, 0, 15, 15, 15); + } + + @Override + public Granularity viewerGranularity() { + return granularity; + } + + @Override + public int minIntervalMillis() { + return min; + } + + @Override + public int fallbackIntervalMillis() { + return fallback; + } + } + + private static final Class[] REGISTER_BUILTIN = { DebugEntrySpec.class, int.class, boolean.class }; + + private final FakeRenderer[] renderers = { new FakeRenderer(), new FakeRenderer(), new FakeRenderer() }; + private final DebugEntryDef[] defs = new DebugEntryDef[3]; + private DebugRenderSlots slots; + private int uploads; + + private DebugRenderSlots slots() { + final DebugRegistry registry = new DebugRegistry(); + registry.registerEntry(new DebugEntrySpec("test:gate", DebugEntryCategory.RENDERER).defaults(DebugScreenEntryStatus.NEVER, DebugScreenEntryStatus.NEVER)); + for (int i = 0; i < 3; i++) { + final DebugEntrySpec spec = new DebugEntrySpec("test:renderer_" + i, DebugEntryCategory.RENDERER).defaults(DebugScreenEntryStatus.NEVER, DebugScreenEntryStatus.NEVER); + defs[i] = Reflect.invoke(registry, "registerBuiltin", REGISTER_BUILTIN, spec, KINDS[i], false); + } + registry.freeze(); + slots = new DebugRenderSlots(registry); + final DebugWorldRenderer[] installed = Reflect.get(slots, "renderers"); + for (int i = 0; i < 3; i++) installed[defs[i].rendererIndex] = renderers[i]; + return slots; + } + + private int update(DebugRenderSlots s, long now, int x, int y, int z) { + final int slot = s.update(now, x, y, z); + if (slot >= 0 && s.takeUpload(slot)) uploads++; + return slot; + } + + private int slot(int i) { + return defs[i].rendererIndex; + } + + @AfterEach + void release() { + if (slots == null) return; + for (int i = 0; i < slots.count; i++) slots.setEnabled(i, false); + slots.releaseIfIdle(); + } + + @Test + void disableMarksTheSlotForDiscardAndLiveEndsWhenIdle() { + final DebugRenderSlots s = slots(); + s.setEnabled(slot(0), true); + assertEquals(slot(0), s.update(0L, 0, 0, 0)); + s.setEnabled(slot(0), false); + assertTrue(s.isPendingDiscard(slot(0))); + assertTrue(s.isLive()); + s.releaseIfIdle(); + assertFalse(s.isLive()); + } + + @Test + void dirtySlotWaitsForItsMinimumInterval() { + final DebugRenderSlots s = slots(); + renderers[0].watches = true; + s.setEnabled(slot(0), true); + assertEquals(slot(0), s.update(0L, 0, 0, 0)); + s.changes.markBlockForUpdate(1, 1, 1); + assertEquals(-1, s.update(250 * MILLI - 1, 0, 0, 0)); + assertEquals(slot(0), s.update(250 * MILLI, 0, 0, 0)); + assertFalse(s.changes.isDirty(slot(0))); + } + + @Test + void fallbackIntervalFiresAndNegativeFallbackNeverDoes() { + final DebugRenderSlots s = slots(); + renderers[1].fallback = -1; + s.setEnabled(slot(0), true); + s.setEnabled(slot(1), true); + assertEquals(slot(0), s.update(0L, 0, 0, 0)); + assertEquals(slot(1), s.update(0L, 0, 0, 0)); + assertEquals(-1, s.update(2000 * MILLI - 1, 0, 0, 0)); + assertEquals(slot(0), s.update(2000 * MILLI, 0, 0, 0)); + assertEquals(slot(0), s.update(3_000_000 * MILLI, 0, 0, 0)); + assertEquals(1, renderers[1].rebuilds); + } + + @Test + void viewerMovementTriggersByGranularity() { + final DebugRenderSlots s = slots(); + renderers[1].granularity = DebugWorldRenderer.Granularity.SECTION; + renderers[2].granularity = DebugWorldRenderer.Granularity.CHUNK; + for (int i = 0; i < 3; i++) { + renderers[i].fallback = -1; + s.setEnabled(slot(i), true); + s.update(0L, 0, 0, 0); + } + assertEquals(-1, s.update(300 * MILLI, 0, 0, 0)); + assertEquals(slot(0), s.update(300 * MILLI, 1, 0, 0)); + assertEquals(-1, s.update(300 * MILLI, 1, 0, 0)); + assertEquals(slot(1), s.update(600 * MILLI, 1, 17, 0)); + assertEquals(slot(0), s.update(600 * MILLI, 1, 17, 0)); + assertEquals(-1, s.update(600 * MILLI, 1, 17, 0)); + assertEquals(slot(2), s.update(900 * MILLI, 16, 17, 0)); + } + + @Test + void rebuildWithAnUnchangedHashDoesNotUpload() { + final DebugRenderSlots s = slots(); + final FakeRenderer renderer = renderers[0]; + renderer.min = 0; + renderer.fallback = 0; + s.setEnabled(slot(0), true); + update(s, 0L, 0, 0, 0); + update(s, 1L, 0, 0, 0); + assertEquals(2, renderer.rebuilds); + assertEquals(1, uploads); + renderer.content = 0xFF00FF00; + update(s, 2L, 0, 0, 0); + assertEquals(2, uploads); + } + + @Test + void blockStepInsideASectionWithIdenticalEmissionDoesNotUpload() { + final DebugRenderSlots s = slots(); + renderers[0].fallback = -1; + s.setEnabled(slot(0), true); + assertEquals(slot(0), update(s, 0L, 3, 70, 5)); + assertEquals(slot(0), update(s, 300 * MILLI, 4, 70, 5)); + assertEquals(slot(0), update(s, 600 * MILLI, 4, 71, 6)); + assertEquals(3, renderers[0].rebuilds); + assertEquals(1, uploads); + assertEquals(slot(0), update(s, 900 * MILLI, 16, 71, 6)); + assertEquals(2, uploads); + } + + @Test + void throwingRendererIsDisabledAndTheOthersKeepRunning() { + final DebugRenderSlots s = slots(); + renderers[0].fails = true; + s.setEnabled(slot(0), true); + s.setEnabled(slot(1), true); + assertEquals(slot(0), s.update(0L, 0, 0, 0)); + assertEquals(1, s.enabledCount()); + assertTrue(defs[0].isFailed()); + assertFalse(s.takeUpload(slot(0))); + assertEquals(slot(1), s.update(1L, 0, 0, 0)); + assertFalse(defs[1].isFailed()); + } + + @Test + void worldResetDiscardsMeshesAndReuploadsOnNextUpdate() { + final DebugRenderSlots s = slots(); + s.setEnabled(slot(0), true); + update(s, 0L, 0, 0, 0); + s.resetWorld(); + assertFalse(s.isDrawable(slot(0))); + assertEquals(1, s.enabledCount()); + assertTrue(s.isPendingDiscard(slot(0))); + s.clearPendingDiscard(slot(0)); + assertEquals(slot(0), update(s, 1L, 0, 0, 0)); + assertEquals(2, uploads); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldChangesTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldChangesTest.java new file mode 100644 index 000000000..2c86b2ffd --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/DebugWorldChangesTest.java @@ -0,0 +1,49 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class DebugWorldChangesTest { + + private static DebugWorldChanges create() { + final DebugWorldChanges changes = new DebugWorldChanges(4); + changes.watch(0, 0, 0, 0, 15, 15, 15); + changes.watch(2, 100, 60, 100, 120, 80, 120); + changes.watch(3, 10, 0, 10, 30, 15, 30); + return changes; + } + + private static String dirty(DebugWorldChanges changes) { + final StringBuilder out = new StringBuilder(); + for (int i = 0; i < 4; i++) out.append(changes.isDirty(i) ? '1' : '0'); + return out.toString(); + } + + @ParameterizedTest + @CsvSource({ + "110,70,110,110,70,110,0010", + "120,80,120,120,80,120,0010", + "121,80,120,121,80,120,0000", + "500,70,500,500,70,500,0000", + "12,5,12,14,6,14,1001", + "130,90,130,115,75,115,0010", + "-30000000,0,-30000000,30000000,255,30000000,1011"}) + void rangeDirtiesOverlappingBoxesOnly(int x0, int y0, int z0, int x1, int y1, int z1, String expected) { + final DebugWorldChanges changes = create(); + changes.markBlockRangeForRenderUpdate(x0, y0, z0, x1, y1, z1); + assertEquals(expected, dirty(changes)); + } + + @Test + void clearedBoxStopsMatching() { + final DebugWorldChanges changes = create(); + changes.markBlockForUpdate(110, 70, 110); + changes.clearBox(2); + changes.markBlockForUpdate(110, 70, 110); + assertFalse(changes.isDirty(2)); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/WaterLevelRendererTest.java b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/WaterLevelRendererTest.java new file mode 100644 index 000000000..cba870ad4 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/debug/screen/renderers/WaterLevelRendererTest.java @@ -0,0 +1,15 @@ +package com.gtnewhorizons.angelica.debug.screen.renderers; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class WaterLevelRendererTest { + + @ParameterizedTest + @CsvSource({"0,8", "1,7", "7,1", "8,8"}) + void level(int meta, int expected) { + assertEquals(expected, WaterLevelRenderer.level(meta)); + } +}