-
-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/net/coderbot/iris/gl/debug/TimerQuerier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package net.coderbot.iris.gl.debug; | ||
|
||
import it.unimi.dsi.fastutil.objects.Object2IntMap; | ||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.ObjectArraySet; | ||
import net.coderbot.iris.Iris; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import org.lwjgl.opengl.GL32C; | ||
|
||
import java.util.Set; | ||
|
||
public class TimerQuerier { | ||
private static final Object2ObjectMap<String, TimerQuery>[] queries = new Object2ObjectOpenHashMap[5]; | ||
private static int frameId = 0; | ||
|
||
static { | ||
for (int i = 0; i < 5; i++) { | ||
queries[i] = new Object2ObjectOpenHashMap<>(); | ||
} | ||
} | ||
|
||
private static final Set<TimerQuery> unusedQueries = new ObjectArraySet<>(); | ||
|
||
public static void initRenderer() { | ||
int[] queries = new int[100]; | ||
GL32C.glGenQueries(queries); | ||
for (int i = 0; i < 100; i++) { | ||
unusedQueries.add(new TimerQuery(queries[i])); | ||
} | ||
} | ||
|
||
public static TimerQuery giveQuery() { | ||
if (unusedQueries.isEmpty()) { | ||
Iris.logger.warn("Congrats, you overran the query system. Adding a new query. (If this stops after a bit, it's not an error, you just have a lot going on)"); | ||
unusedQueries.add(new TimerQuery(GL32C.glGenQueries())); | ||
} | ||
TimerQuery query = unusedQueries.iterator().next(); | ||
unusedQueries.remove(query); | ||
return query; | ||
} | ||
|
||
|
||
public static void advanceFrameAndReset() { | ||
// Advance the frame ID | ||
frameId = (frameId + 1) % 5; | ||
|
||
int frameToGet = frameId - 4; | ||
if (frameToGet < 0) frameToGet += 5; | ||
|
||
queries[frameToGet].forEach((name, query) -> { | ||
if (Screen.hasControlDown()) { | ||
Iris.logger.warn("Query result for " + name + " was " + ((float) query.returnResult() / 1000000f) + "ms"); | ||
} | ||
query.stopUsing(); | ||
unusedQueries.add(query); | ||
}); | ||
|
||
queries[frameToGet].clear(); | ||
} | ||
|
||
public static void monitorQuery(String name, TimerQuery query) { | ||
queries[frameId].put(name, query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.coderbot.iris.gl.debug; | ||
|
||
import net.coderbot.iris.Iris; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.lwjgl.opengl.GL42C; | ||
|
||
public class TimerQuery { | ||
private final int query; | ||
private boolean inUse = false; | ||
|
||
private static boolean isInQuery = false; | ||
private static String name; | ||
|
||
public TimerQuery(int query) { | ||
this.query = query; | ||
} | ||
|
||
public boolean isInUse() { | ||
return inUse; | ||
} | ||
|
||
public void startQuery(String name) { | ||
if (inUse || isInQuery) { | ||
throw new IllegalStateException("Query " + name + " already in use"); | ||
} | ||
TimerQuery.name = name; | ||
|
||
inUse = true; | ||
isInQuery = true; | ||
|
||
GL42C.glBeginQuery(GL42C.GL_TIME_ELAPSED, query); | ||
} | ||
|
||
public void startMonitoring() { | ||
if (!inUse) { | ||
throw new IllegalStateException("Not in use, giving up"); | ||
} | ||
|
||
isInQuery = false; | ||
GL42C.glEndQuery(GL42C.GL_TIME_ELAPSED); | ||
|
||
TimerQuerier.monitorQuery(name, this); | ||
} | ||
|
||
@ApiStatus.Internal | ||
public int returnResult() { | ||
return GL42C.glGetQueryObjecti(query, GL42C.GL_QUERY_RESULT); | ||
} | ||
|
||
protected void stopUsing() { | ||
if (!inUse) { | ||
throw new IllegalStateException("Query not in use"); | ||
} | ||
|
||
inUse = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters