Skip to content

Commit

Permalink
Added section profiling to debug screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Dec 20, 2023
1 parent ff11a46 commit 9965111
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
3 changes: 0 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ public static Client getClient() {

public static long startTime;
public static void main(String[] args) throws IOException {
byte[] vals = new byte[Integer.MAX_VALUE/2];
startTime = System.currentTimeMillis();
argumentContainer = new ArgumentContainer(args);
gameInstance.handleArgs(args);
gameInstance.side = Side.CLIENT;
gameInstance.loadContent();

;

if(argumentContainer.getBoolean("--integratedServer", false)) {
try {
Thread thread = new Thread(() -> ServerMain.server(gameInstance, argumentContainer));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/dev/hilligans/ourcraft/client/input/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;

import static org.lwjgl.glfw.GLFW.GLFW_PRESS;
import static org.lwjgl.glfw.GLFW.GLFW_REPEAT;

public class Input implements IRegistryElement {

Expand Down Expand Up @@ -42,6 +43,7 @@ public boolean canInput(String pipeline) {
return true;
}
}
//System.out.println(pipeline);
return false;
}

Expand Down Expand Up @@ -92,7 +94,7 @@ public String getDisplay() {

public void process(InputHandler inputHandler, int input, float mode, RenderWindow window, int action, float strength) {
//TODO fix
if(action == GLFW_PRESS) {
if(action == GLFW_PRESS || action == GLFW_REPEAT) {
press(window, strength);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public OpenGLWindow createWindow() {

@Override
public void render(RenderWindow window, GraphicsContext graphicsContext) {
window.frameTracker.count();
//client.mouseLocked = client.screen == null;
//glGetError();
//GLRenderer.resetFrame();
Expand All @@ -67,10 +68,6 @@ public void render(RenderWindow window, GraphicsContext graphicsContext) {
window.renderPipeline(client, matrixStack, screenStack, graphicsContext);
}

public static int counter = 0;



@Override
public OpenGLWindow setup() {
System.setProperty("java.awt.headless", "true");
Expand Down Expand Up @@ -143,7 +140,7 @@ public Logger getLogger() {
@Override
public GraphicsContext getGraphicsContext() {
if(profiling) {
return new GraphicsContext().setSection(new ProfiledSection());
return new GraphicsContext().setSection(new ProfiledSection().setMonitorName("loop"));
}
return new GraphicsContext();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hilligans.ourcraft.client.rendering.graphics.tasks;

import dev.hilligans.ourcraft.Ourcraft;
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
Expand All @@ -10,6 +11,9 @@
import dev.hilligans.ourcraft.client.rendering.graphics.RenderTaskSource;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.world.StringRenderer;
import dev.hilligans.ourcraft.util.sections.ProfiledSection;

import static dev.hilligans.ourcraft.util.FormattedString.FSTR;

public class GUIRenderTask extends RenderTaskSource {

Expand All @@ -20,12 +24,16 @@ public GUIRenderTask() {
@Override
public RenderTask getDefaultTask() {
return new RenderTask() {

public int counter = 0;
public ProfiledSection.StackFrame monitoredFrame;

@Override
public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphicsEngine<?, ?,?> engine, Client client, MatrixStack worldStack, MatrixStack screenStack, float delta) {
IDefaultEngineImpl<?,?> impl = engine.getDefaultImpl();

StringRenderer stringRenderer = engine.getStringRenderer();

counter++;
if (client.playerData.f3) {
// stringRenderer.drawStringInternal(window,screenStack,"1",0,0,0.5f);
stringRenderer.drawStringInternal(window, screenStack, "FPS:" + window.frameTracker.getFPS(), window.getWindowWidth() / 2, 29, 0.5f);
Expand All @@ -51,6 +59,17 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
stringRenderer.drawStringInternal(window,screenStack, "X:" + client.rWindow.camera.getCameraPos().x, window.getWindowWidth() / 2, 358,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "Y:" + client.rWindow.camera.getCameraPos().y, window.getWindowWidth() / 2, 387,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "Z:" + client.rWindow.camera.getCameraPos().z, window.getWindowWidth() / 2, 416,0.5f);

if(counter % 100 == 0) {
counter = 0;
if(graphicsContext.getSection() instanceof ProfiledSection profiledSection) {
monitoredFrame = profiledSection.getMonitor();
}
}
if(monitoredFrame != null) {
//recursiveDraw(stringRenderer, window, screenStack, monitoredFrame, new int[]{0}, monitoredFrame.totalTime);
recursiveDrawTimes(stringRenderer, window, screenStack, monitoredFrame, new int[]{0});
}
}
// ItemStack stack = client.playerData.inventory.getItem(client.playerData.handSlot);
// if (stack != null && stack.item != null) {
Expand All @@ -76,6 +95,22 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
// }
}

public void recursiveDraw(StringRenderer stringRenderer, RenderWindow renderWindow, MatrixStack matrixStack, ProfiledSection.StackFrame stackFrame, int[] y, long time) {
stringRenderer.drawStringInternal(renderWindow, matrixStack, FSTR."\{stackFrame.getIndentLevel()}\{stackFrame.sectionName}: %2.2f%%\{(double)stackFrame.totalTime/time*100}\n", 0, y[0], 0.5f);
y[0] += 29;
for(ProfiledSection.StackFrame child : stackFrame.frames) {
recursiveDraw(stringRenderer, renderWindow, matrixStack, child, y, time);
}
}

public void recursiveDrawTimes(StringRenderer stringRenderer, RenderWindow renderWindow, MatrixStack matrixStack, ProfiledSection.StackFrame stackFrame, int[] y) {
stringRenderer.drawStringInternal(renderWindow, matrixStack, STR."\{stackFrame.getIndentLevel()}\{stackFrame.sectionName}: \{Ourcraft.getConvertedTime(stackFrame.totalTime)}\n", 0, y[0], 0.5f);
y[0] += 29;
for(ProfiledSection.StackFrame child : stackFrame.frames) {
recursiveDrawTimes(stringRenderer, renderWindow, matrixStack, child, y);
}
}

@Override
public PipelineState getPipelineState() {
return new PipelineState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.hilligans.ourcraft.util.sections;

import dev.hilligans.ourcraft.Ourcraft;
import dev.hilligans.ourcraft.util.FormattedString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

Expand All @@ -11,14 +11,21 @@
public class ProfiledSection implements ISection {

protected final Stack<StackFrame> stackFrames = new Stack<>();
public StackFrame head;
public StackFrame monitor;
public StackFrame monitoredTotalTime;
public String monitorName;

@Override
public SectionView startSection(String name) {
stackFrames.push(new StackFrame(name, Ourcraft.getTime()));
return new SectionView(name, this);
}

public ProfiledSection setMonitorName(String name) {
this.monitorName = name;
return this;
}

@Override
public void stopSection(@NotNull String name) {
StackFrame stackFrame = stackFrames.pop();
Expand All @@ -28,34 +35,47 @@ public void stopSection(@NotNull String name) {
processFrame(stackFrame);
}

public @Nullable StackFrame getMonitor() {
return monitor;
}

public @Nullable StackFrame getMonitoredTotalTime() {
return monitoredTotalTime;
}

private void processFrame(StackFrame stackFrame) {
stackFrame.setEndTime(Ourcraft.getTime());
StackFrame lastFrame = stackFrames.empty() ? null : stackFrames.peek();
if(lastFrame != null) {
stackFrame.setParent(lastFrame);
lastFrame.addFrame(stackFrame);
}
if(monitorName == null) {
if(lastFrame == null) {
monitor = stackFrame;
}
} else {
head = stackFrame;
if(monitorName.equals(stackFrame.sectionName)) {
monitor = stackFrame;
if(lastFrame != null) {
monitoredTotalTime = lastFrame.stackFrames.get(monitorName).copy();
}
}
}
}

@Override
public String toString() {
if(head == null) {
if(stackFrames.empty()) {
return "empty";
} else {
StackFrame f = stackFrames.peek().frames.get(0);
return f.toString1(f.totalTime);
}
if(monitor == null) {
return "empty";
} else {
return head.toString();
return monitor.toString();
}
}

public static class StackFrame {
public final String sectionName;
public final long startTime;
public long startTime;
public long endTime;
public long totalTime;
public StackFrame parent;
Expand All @@ -68,6 +88,12 @@ public StackFrame(String sectionName, long startTime) {
this.startTime = startTime;
}

public StackFrame(String sectionName, long totalTime, StackFrame parent) {
this.sectionName = sectionName;
this.totalTime = totalTime;
this.parent = parent;
}

public void setEndTime(long time) {
endTime = time;
this.addTime(getRunTime());
Expand Down Expand Up @@ -106,6 +132,16 @@ public void addFrame(StackFrame stackFrame) {
frames.add(stackFrame);
}

public StackFrame copy() {
StackFrame stackFrame = new StackFrame(sectionName, totalTime, parent);
for(StackFrame child : frames) {
child = child.copy();
stackFrame.frames.add(child);
stackFrame.stackFrames.put(child.sectionName, child);
}
return stackFrame;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand All @@ -116,7 +152,11 @@ public String toString() {
return builder.toString();
}

public String toString1(long time) {
public String getPercentagesString() {
return toString1(totalTime);
}

String toString1(long time) {
StringBuilder builder = new StringBuilder();
builder.append(FSTR."\{getIndentLevel()}\{sectionName}: %2.2f%%\{(double)totalTime/time*100}\n");
for(StackFrame frame : frames) {
Expand Down

0 comments on commit 9965111

Please sign in to comment.