Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum Target {
PLAYER_ITEMS,
PLAYER_BODY,
LIVE_CULLING,
LIVE_POSES,
BATCHING;

String id() {
Expand All @@ -54,6 +55,7 @@ void apply(boolean on) {
case PLAYER_ITEMS -> c.playerItemGeometry = on;
case PLAYER_BODY -> c.playerBodyGeometry = on;
case LIVE_CULLING -> c.liveViewCulling = on;
case LIVE_POSES -> c.livePoseFastPath = on;
case BATCHING -> c.featureBatching = on;
}
}
Expand All @@ -64,7 +66,8 @@ boolean unavailable() {
|| this == PLAYER_ARMOR
|| this == PLAYER_ITEMS
|| this == PLAYER_BODY
|| this == LIVE_CULLING)
|| this == LIVE_CULLING
|| this == LIVE_POSES)
&& ru.wilyfox.drawless.compat.IrisShaders.packInUse()
|| this == PLAYER_BODY && RenderModSupport.PLAYER_MODEL_MODS
|| (this == PARTICLES || this == PARTICLE_LIGHT || this == PARTICLE_COLLISION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class DrawLessConfig {
public ExtremeDisplayMode extremeDisplayMode = ExtremeDisplayMode.LIVE_CACHE;
public boolean extremeAllItemDisplays = true;
public boolean liveViewCulling = true;
public boolean livePoseFastPath = true;
public boolean modelEngineNpcBudget = false;
public int npcGroupsPerFrame = 2;
public boolean adaptiveNpcBudget = true;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/ru/wilyfox/drawless/gui/SettingsCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ static List<Option> create() {
Page.RENDERING,
() -> c().liveViewCulling,
v -> c().liveViewCulling = v));
options.add(
toggle(
"live_pose_fast_path",
Page.RENDERING,
() -> c().livePoseFastPath,
v -> c().livePoseFastPath = v));
options.add(
toggle(
"model_cache",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.wilyfox.drawless.mixin;

import net.minecraft.client.renderer.entity.DisplayRenderer;
import net.minecraft.client.renderer.entity.state.DisplayEntityRenderState;
import net.minecraft.world.entity.Display;
import org.joml.Quaternionf;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(DisplayRenderer.class)
public interface DisplayOrientationAccessor {
@Invoker("calculateOrientation")
Quaternionf drawless$orientation(
Display.RenderState render, DisplayEntityRenderState state, Quaternionf orientation);
}
138 changes: 135 additions & 3 deletions src/main/java/ru/wilyfox/drawless/performance/LiveDisplayGeometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.phys.AABB;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import ru.wilyfox.drawless.DrawLess;
import ru.wilyfox.drawless.compat.IrisShaders;
import ru.wilyfox.drawless.compat.RenderModSupport;
import ru.wilyfox.drawless.config.DrawLessConfig;
import ru.wilyfox.drawless.config.ExtremeDisplayMode;
import ru.wilyfox.drawless.mixin.DisplayOrientationAccessor;
import ru.wilyfox.drawless.observe.Observation;
import ru.wilyfox.drawless.util.ClientDisconnect;

Expand Down Expand Up @@ -72,6 +75,15 @@ public final class LiveDisplayGeometry {
private static long entityLoopStarted, entityLoopNanos, entityLoopTotal, entityLoopFrames;
private static final long MOVING_REFRESH = 50_000_000L, STABLE_REFRESH = 1_000_000_000L;
private static final double MOVING_MARGIN = 1, STABLE_MARGIN = 0.5;
private static final Matrix4f poseBase = new Matrix4f(), poseScratch = new Matrix4f();
private static final Quaternionf poseOrientation = new Quaternionf();

/** Fixtures: keep the ordinary path and compare the direct pose with it on every frame. */
static boolean verifyPoses;

private static long poseChecks, poseMismatches;
private static int poseFast, poseRejected;
private static long poseFastTotal;
private static int culled;
private static long culledTotal;

Expand Down Expand Up @@ -126,6 +138,8 @@ public static void clear() {
entityLoopStarted = entityLoopNanos = entityLoopTotal = entityLoopFrames = 0;
culled = 0;
culledTotal = 0;
poseFast = poseRejected = 0;
poseFastTotal = poseChecks = poseMismatches = 0;
pose = new PoseStack();
}

Expand Down Expand Up @@ -161,7 +175,7 @@ public static void beginFrame(Minecraft client) {
activeBatches.clear();
visibleVertices = interpolatingParts = 0;
visibleMeshCount = 0;
poseReused = poseSubmitted = 0;
poseReused = poseSubmitted = poseFast = 0;
for (var type : batches.values()) {
type.count = 0;
for (var batch : type.chunks) batch.used = 0;
Expand Down Expand Up @@ -244,6 +258,7 @@ public static void finishFrame(Minecraft client, LevelRenderState state) {
long start = System.nanoTime();
boolean sample = Observation.get().sampling();
long eligibilityNanos = 0, poseNanos = 0, groupNanos = 0;
boolean fastPoses = DrawLessConfig.get().livePoseFastPath && !verifyPoses;
try (var ignored = Observation.get().scope("render/liveDisplay/capture")) {
for (var renderState : state.entityRenderStates) {
var entity = extracted.get(renderState);
Expand All @@ -266,6 +281,11 @@ public static void finishFrame(Minecraft client, LevelRenderState state) {
reason = part.reason;
poseReused++;
poseReusedTotal++;
} else if (fastPoses && part.posable(item)) {
part.repose(client, item);
reason = Reason.READY;
poseFast++;
poseFastTotal++;
} else {
capture.reset(part);
// Capture around the entity origin. Camera/actor/passenger translation is
Expand All @@ -290,6 +310,8 @@ public static void finishFrame(Minecraft client, LevelRenderState state) {
reason,
client.getEntityRenderDispatcher().getRenderer(item).getClass()
== DisplayRenderer.ItemDisplayRenderer.class);
if (reason == Reason.READY && part.replayable) part.learnPose(client, item);
else part.poseKnown = part.poseValid = false;
poseSubmitted++;
poseSubmittedTotal++;
}
Expand Down Expand Up @@ -452,7 +474,7 @@ public static String diagnostic() {
if (reasons[r.ordinal()] > 0) reasonsText.add(r.label + ":" + reasons[r.ordinal()]);
return String.format(
Locale.ROOT,
"%s; status=%s; meshes=%d; quadSets=%d; aliases=%d; parts=%d; interpolatingParts=%d; visibleMeshes=%d; visibleVertices=%d; instances=%d; culled=%d; culledTotal=%d; quads=%d; draws=%d; fallback=%d; localBytes=%d; indexBytes=%d; meshBuilds=%d; indexRebuilds=%d; geometryUploads=%d; geometryUploadedBytes=%d; instanceUploadedBytes=%d; animatedSprites=%d; poseReused=%d; poseSubmitted=%d; poseReusedTotal=%d; poseSubmittedTotal=%d; capture=%.3fms; upload=%.3fms; drawCpu=%.3fms; framesTotal=%d; captureTotalMs=%.3f; uploadTotalMs=%.3f; drawTotalMs=%.3f; admission=%s",
"%s; status=%s; meshes=%d; quadSets=%d; aliases=%d; parts=%d; interpolatingParts=%d; visibleMeshes=%d; visibleVertices=%d; instances=%d; culled=%d; culledTotal=%d; poseFast=%d; poseFastTotal=%d; poseRejected=%d; poseChecks=%d; poseMismatches=%d; quads=%d; draws=%d; fallback=%d; localBytes=%d; indexBytes=%d; meshBuilds=%d; indexRebuilds=%d; geometryUploads=%d; geometryUploadedBytes=%d; instanceUploadedBytes=%d; animatedSprites=%d; poseReused=%d; poseSubmitted=%d; poseReusedTotal=%d; poseSubmittedTotal=%d; capture=%.3fms; upload=%.3fms; drawCpu=%.3fms; framesTotal=%d; captureTotalMs=%.3f; uploadTotalMs=%.3f; drawTotalMs=%.3f; admission=%s",
liveMode == null ? DrawLessConfig.get().extremeDisplayMode : liveMode,
enabled
? "on"
Expand All @@ -469,6 +491,11 @@ public static String diagnostic() {
instances,
culled,
culledTotal,
poseFast,
poseFastTotal,
poseRejected,
poseChecks,
poseMismatches,
quads,
draws,
fallbacks,
Expand Down Expand Up @@ -542,7 +569,8 @@ static MeshState of(ItemMeshes.Mesh mesh) {

private static final class Part {
final ArrayList<Instance> instances = new ArrayList<>();
int used, seen;
int used, seen, poseUsed;
boolean poseRejectedOnce;
long indexBytes;

/** Bounds around the entity origin at the last pose, and when they were last confirmed. */
Expand All @@ -551,6 +579,11 @@ private static final class Part {
long boundsChecked;
boolean moving;

/**
* Rest matrices were learned from a vanilla pose; poseValid once a second one confirmed them.
*/
boolean poseKnown, poseValid;

ItemStackRenderState layers;
Display.RenderState render;
float interpolation, yRot, xRot, cameraXRot, cameraYRot;
Expand Down Expand Up @@ -595,6 +628,70 @@ void remember(ItemDisplayEntityRenderState state, Reason reason, boolean replaya
}
}

/** Only the interpolated transform or the billboard changed since the vanilla pose. */
boolean posable(ItemDisplayEntityRenderState state) {
return poseValid
&& replayable
&& reason == Reason.READY
&& layers == state.item
&& render == state.renderState;
}

private static void poseBase(Minecraft client, ItemDisplayEntityRenderState state) {
var renderer =
(DisplayOrientationAccessor) client.getEntityRenderDispatcher().getRenderer(state);
var render = state.renderState;
renderer.drawless$orientation(render, state, poseOrientation.identity());
poseBase
.rotation(poseOrientation)
.mul(render.transformation().get(state.interpolationProgress).getMatrix());
}

/** The pose vanilla would produce: orientation, then transform, then the unchanged rest. */
void repose(Minecraft client, ItemDisplayEntityRenderState state) {
poseBase(client, state);
for (int i = 0; i < used; i++) {
var instance = instances.get(i);
instance.setPose(poseBase.mul(instance.rest, poseScratch));
instance.relocate();
}
interpolation = state.interpolationProgress;
yRot = state.entityYRot;
xRot = state.entityXRot;
cameraXRot = state.cameraXRot;
cameraYRot = state.cameraYRot;
measure(state);
}

/**
* Derives each instance's rest from the pose vanilla just produced. Before replacing a learned
* rest it must predict this pose; a part that fails or cannot be inverted stays on the ordinary
* path, so anything this model of the pose misses costs time, not pixels.
*/
void learnPose(Minecraft client, ItemDisplayEntityRenderState state) {
poseBase(client, state);
boolean matches = poseKnown && poseUsed == used;
if (poseKnown && poseUsed == used) {
poseChecks++;
for (int i = 0; i < used && matches; i++) matches = instances.get(i).predicts(poseBase);
if (!matches) {
poseMismatches++;
if (poseValid || !poseRejectedOnce) poseRejected++;
poseRejectedOnce = true;
}
}
poseValid = matches && !poseRejectedOnce;
float determinant = poseBase.determinant3x3();
if (!Float.isFinite(determinant) || Math.abs(determinant) < 1e-9f) {
poseKnown = poseValid = false;
return;
}
poseBase.invert(poseScratch);
for (int i = 0; i < used; i++) instances.get(i).learnRest(poseScratch);
poseUsed = used;
poseKnown = true;
}

private void measure(ItemDisplayEntityRenderState state) {
moving = state.interpolationProgress < 1;
minX = minY = minZ = Float.MAX_VALUE;
Expand Down Expand Up @@ -650,6 +747,41 @@ void set(ItemMeshes.Mesh mesh, PoseStack.Pose pose, int color, int light, int ov
localZ = transform.m32();
}

/** The pose of this instance relative to the display's orientation and transform. */
final Matrix4f rest = new Matrix4f();

private static final Matrix4f scratch = new Matrix4f();
private static final float[] expectedNormals = new float[9];

void relocate() {
localX = transform.m30();
localY = transform.m31();
localZ = transform.m32();
}

void learnRest(Matrix4f inverseBase) {
inverseBase.mul(transform, rest);
}

/** Called with the fresh vanilla pose in place, before the rest is replaced. */
boolean predicts(Matrix4f base) {
base.mul(rest, scratch);
for (int column = 0; column < 4; column++)
for (int row = 0; row < 3; row++) {
float expected = transform.get(column, row);
if (Math.abs(scratch.get(column, row) - expected)
> 1e-4f * Math.max(1, Math.abs(expected))) return false;
}
System.arraycopy(normals, 0, expectedNormals, 0, 9);
var vanilla = transform.get(new Matrix4f());
setPose(scratch);
boolean same = true;
for (int i = 0; i < 9; i++) same &= Math.abs(normals[i] - expectedNormals[i]) <= 2.5f / 127;
transform.set(vanilla);
System.arraycopy(expectedNormals, 0, normals, 0, 9);
return same;
}

void position(float x, float y, float z, int light) {
// Always derive from the original local translation; never accumulate camera deltas.
transform.m30(localX + x).m31(localY + y).m32(localZ + z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.mojang.blaze3d.vertex.PoseStack;
import java.nio.ByteBuffer;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.joml.Vector3f;

/**
Expand All @@ -12,6 +14,7 @@
*/
class RetainedInstance {
private static final Vector3f normal = new Vector3f();
private static final Matrix3f normalMatrix = new Matrix3f();

RetainedMesh mesh;
final Matrix4f transform = new Matrix4f();
Expand All @@ -38,6 +41,18 @@ void set(RetainedMesh mesh, PoseStack.Pose pose, int color, int light, int overl
}
}

/** The same result as {@link #set} for an already known pose matrix. */
void setPose(Matrix4fc pose) {
transform.set(pose);
pose.normal(normalMatrix);
for (int i = 0; i < 3; i++) {
normal.set(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0).mul(normalMatrix).normalize();
normals[i * 3] = (byte) (int) (normal.x * 127) / 127f;
normals[i * 3 + 1] = (byte) (int) (normal.y * 127) / 127f;
normals[i * 3 + 2] = (byte) (int) (normal.z * 127) / 127f;
}
}

void copy(RetainedInstance source, RetainedMesh mesh, int color, int light, int overlay) {
this.mesh = mesh;
transform.set(source.transform);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/drawless/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"drawless.setting.text_reuse.description": "Experimental. Reuses the current text/name-tag buffer within a preparation pass. Off by default.",
"drawless.setting.live_view_culling": "Skip displays outside the view",
"drawless.setting.live_view_culling.description": "Item Displays on the GPU that are outside the camera view are skipped for the frame. Ticks and animation keep running. A part moving very quickly into view may appear a moment late.",
"drawless.setting.live_pose_fast_path": "Fast poses for animated displays",
"drawless.setting.live_pose_fast_path.description": "Computes the pose of interpolating Item Display parts directly instead of running them through vanilla submission every frame. A part whose pose this does not reproduce keeps the ordinary path.",
"drawless.setting.model_cache": "Display model cache",
"drawless.setting.model_cache.description": "Reuses Item Display model layers and refreshes them within a bounded CPU budget.",
"drawless.setting.adaptive_budget": "Adaptive model budget",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/drawless/lang/ru_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"drawless.setting.text_reuse.description": "Экспериментально. Переиспользует текущий буфер текста и имён в пределах прохода подготовки. По умолчанию выключено.",
"drawless.setting.live_view_culling": "Пропускать дисплеи вне экрана",
"drawless.setting.live_view_culling.description": "Item Display на видеокарте, которые не попадают в обзор камеры, пропускаются в этом кадре. Тики и анимация продолжают работать. Часть, очень быстро влетающая в кадр, может появиться с небольшой задержкой.",
"drawless.setting.live_pose_fast_path": "Быстрые позы анимированных дисплеев",
"drawless.setting.live_pose_fast_path.description": "Считает позу интерполируемых частей Item Display напрямую, а не прогоняет их через ванильную отправку каждый кадр. Часть, чью позу так не воспроизвести, остаётся на обычном пути.",
"drawless.setting.model_cache": "Кеш моделей Display",
"drawless.setting.model_cache.description": "Переиспользует слои моделей Item Display и обновляет их в пределах бюджета CPU.",
"drawless.setting.adaptive_budget": "Адаптивный бюджет",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/drawless.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"DisplayItemStateAllocationMixin",
"DisplayModelReloadMixin",
"DisplayItemStateAccessor",
"DisplayOrientationAccessor",
"DisplayCuboidModelAccessor",
"DisplayCompositeModelAccessor",
"DisplayRangeModelAccessor",
Expand Down
Loading