Skip to content
Open
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
20 changes: 15 additions & 5 deletions src/main/java/rs117/hd/HdPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public class HdPlugin extends Plugin {
public UBOLights uboLightsCulling;

// Configs used frequently enough to be worth caching
public boolean configLegacyBrightness;
public boolean configGroundTextures;
public boolean configGroundBlending;
public boolean configModelTextures;
Expand All @@ -418,6 +419,8 @@ public class HdPlugin extends Plugin {
public boolean configTiledLighting;
public boolean configTiledLightingImageLoadStore;
public int configDetailDrawDistance;
public int configDrawDistance;
public int configBrightness;
public DynamicLights configDynamicLights;
public ShadowMode configShadowMode;
public SeasonalTheme configSeasonalTheme;
Expand Down Expand Up @@ -676,6 +679,8 @@ protected void startUp() {
}
}

HDUtils.setupThreadAllocatedBytesMonitoring();

updateCachedConfigs();
developerTools.activate();

Expand Down Expand Up @@ -1518,9 +1523,9 @@ public void prepareInterfaceTexture() {
.build(
"AsyncUICopy",
t -> {
long start = System.nanoTime();
long start = frameTimer.getTimeStamp();
pbo.mapped().intView().put(pixels, 0, uiWidth * uiHeight);
frameTimer.add(Timer.COPY_UI_ASYNC, System.nanoTime() - start);
frameTimer.add(Timer.COPY_UI_ASYNC, start);
}
)
.setExecuteAsync(!isPowerSaving)
Expand Down Expand Up @@ -1644,6 +1649,8 @@ private void updateCachedConfigs() {
configShadowMode = config.shadowMode();
configShadowsEnabled = configShadowMode != ShadowMode.OFF;
configRoofShadows = config.roofShadows();
configLegacyBrightness = config.useLegacyBrightness();
configBrightness = config.brightness();
configGroundTextures = config.groundTextures();
configGroundBlending = config.groundBlending();
configModelTextures = config.modelTextures();
Expand All @@ -1659,6 +1666,7 @@ private void updateCachedConfigs() {
configTiledLighting = config.tiledLighting();
configTiledLightingImageLoadStore = config.tiledLightingImageLoadStore();
configDetailDrawDistance = config.detailDrawDistance();
configDrawDistance = config.drawDistance();
configExpandShadowDraw = config.expandShadowDraw();
configUseFasterModelHashing = config.fasterModelHashing();
configZoneStreaming = config.zoneStreaming();
Expand Down Expand Up @@ -1963,13 +1971,13 @@ private float[] getDpiScaling() {
}

public int getDrawDistance() {
return clamp(config.drawDistance(), 0, MAX_DISTANCE);
return clamp(configDrawDistance, 0, MAX_DISTANCE);
}

public float getGammaCorrection() {
if (config.useLegacyBrightness())
if (configLegacyBrightness)
return 1;
return 100f / config.brightness();
return 100f / configBrightness;
}

public int getExpandedMapLoadingChunks() {
Expand All @@ -1991,6 +1999,8 @@ public void onBeforeRender(BeforeRender beforeRender) {
return;
}

frameTimer.end(Timer.CLIENT);

if (lastFrameTimeMillis > 0) {
deltaTime = (float) ((System.currentTimeMillis() - lastFrameTimeMillis) / 1000.);

Expand Down
56 changes: 49 additions & 7 deletions src/main/java/rs117/hd/opengl/GLState.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package rs117.hd.opengl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import lombok.Getter;
import rs117.hd.utils.collections.PrimitiveIntArray;

public abstract class GLState {
protected boolean hasValue;
Expand Down Expand Up @@ -107,6 +106,27 @@ public final void set(int... v) {
System.arraycopy(v, 0, value, 0, v.length);
}

public final void set(int a, int b) {
hasValue = true;
value[0] = a;
value[1] = b;
}

public final void set(int a, int b, int c) {
hasValue = true;
value[0] = a;
value[1] = b;
value[2] = c;
}

public final void set(int a, int b, int c, int d) {
hasValue = true;
value[0] = a;
value[1] = b;
value[2] = c;
value[3] = d;
}

@Override
protected void internalApply() {
if (!hasApplied || !Arrays.equals(value, appliedValue)) {
Expand All @@ -133,6 +153,27 @@ public final void set(boolean... v) {
System.arraycopy(v, 0, value, 0, v.length);
}

public final void set(boolean a, boolean b) {
hasValue = true;
value[0] = a;
value[1] = b;
}

public final void set(boolean a, boolean b, boolean c) {
hasValue = true;
value[0] = a;
value[1] = b;
value[2] = c;
}

public final void set(boolean a, boolean b, boolean c, boolean d) {
hasValue = true;
value[0] = a;
value[1] = b;
value[2] = c;
value[3] = d;
}

@Override
protected void internalApply() {
if (!hasApplied || !Arrays.equals(value, appliedValue)) {
Expand All @@ -145,11 +186,11 @@ protected void internalApply() {
}

public abstract static class IntSet extends GLState {
private final Set<Integer> targets = new HashSet<>();
private final PrimitiveIntArray targets = new PrimitiveIntArray();

public void add(int target) {
hasValue = true;
targets.add(target);
targets.addUnique(target);
}

public void remove(int target) {
Expand All @@ -159,14 +200,15 @@ public void remove(int target) {

@Override
protected void internalApply() {
for (int t : targets) applyTarget(t);
targets.clear();
for (int i = 0; i < targets.length; i++)
applyTarget(targets.array[i]);
targets.reset();
}

@Override
public void reset() {
super.reset();
targets.clear();
targets.reset();
}

protected abstract void applyTarget(int target);
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/rs117/hd/overlays/FrameTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.runelite.client.callback.ClientThread;
import org.lwjgl.opengl.*;
import rs117.hd.HdPlugin;
import rs117.hd.utils.HDUtils;

import static org.lwjgl.opengl.GL33C.*;

Expand All @@ -40,6 +41,8 @@ public class FrameTimer {
private final AutoTimer[] autoTimers = new AutoTimer[NUM_TIMERS];
private final boolean[] activeTimers = new boolean[NUM_TIMERS];
private final long[] timings = new long[NUM_TIMERS];
private final long[] memoryUsage = new long[NUM_TIMERS];
private final long[] allocations = new long[NUM_TIMERS];
private final int[] gpuQueries = new int[NUM_TIMERS * 2];
private final ArrayDeque<Timer> glDebugGroupStack = new ArrayDeque<>(NUM_GPU_DEBUG_GROUPS);
private final ArrayDeque<Listener> listeners = new ArrayDeque<>();
Expand Down Expand Up @@ -136,6 +139,7 @@ public void removeAllListeners() {

public void reset() {
Arrays.fill(timings, 0);
Arrays.fill(allocations, 0);
Arrays.fill(activeTimers, false);
cumulativeError = 0;
}
Expand All @@ -161,6 +165,7 @@ public AutoTimer begin(Timer timer) {
} else if (!activeTimers[index]) {
cumulativeError += errorCompensation + 1 >> 1;
timings[index] -= System.nanoTime() - cumulativeError;
memoryUsage[index] = HDUtils.getUsedMemory(timer != Timer.CLIENT);
}
activeTimers[index] = true;

Expand All @@ -185,15 +190,36 @@ public void end(Timer timer) {
glQueryCounter(gpuQueries[timer.ordinal() * 2 + 1], GL_TIMESTAMP);
// leave the GPU timer active, since it needs to be gathered at a later point
} else {
long allocated = HDUtils.getUsedMemory(timer != Timer.CLIENT) - memoryUsage[timer.ordinal()];
cumulativeError += errorCompensation >> 1;
timings[timer.ordinal()] += System.nanoTime() - cumulativeError;
activeTimers[timer.ordinal()] = false;
allocations[timer.ordinal()] += allocated > 0 ? allocated : 0;
}
}

public void add(Timer timer, long nanos) {
if (isActive)
public long getTimeStamp() { return isActive ? System.nanoTime() : 0; }

public long getUsedMemory() { return isActive ? HDUtils.getUsedMemory(true) : 0; }

public void addDuration(Timer timer, long nanos) {
if (isActive) {
timings[timer.ordinal()] += nanos;
}
}

public void add(Timer timer, long startNanos) {
if (isActive) {
timings[timer.ordinal()] += System.nanoTime() - startNanos;
}
}

public void add(Timer timer, long startNanos, long startMemory) {
if (isActive) {
long allocation = HDUtils.getUsedMemory(true) - startMemory;
timings[timer.ordinal()] += System.nanoTime() - startNanos;
allocations[timer.ordinal()] += allocation > 0 ? allocation : 0;
}
}

public void endFrameAndReset() {
Expand Down Expand Up @@ -234,14 +260,17 @@ public void endFrameAndReset() {
}

final float cpuLoad = (float) osBean.getSystemLoadAverage() / osBean.getAvailableProcessors();
var frameTimings = new FrameTimings(frameEndTimestamp, timings, cpuLoad);
var frameTimings = new FrameTimings(frameEndTimestamp, timings, allocations, cpuLoad);
for (var listener : listeners)
listener.onFrameCompletion(frameTimings);

reset();
}

private void trackGarbageCollection() {
if(!isActive)
return;

List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
if (lastGCTimes == null || lastGCTimes.length != garbageCollectors.size())
lastGCTimes = new long[garbageCollectors.size()];
Expand All @@ -259,6 +288,6 @@ private void trackGarbageCollection() {
plugin.garbageCollectionCount += gc.getCollectionCount();
}

add(Timer.GARBAGE_COLLECTION, elapsedDuration * 1_000_000L);
addDuration(Timer.GARBAGE_COLLECTION, elapsedDuration * 1_000_000L);
}
}
Loading