| 1 | package com.mojang.renderpearl.frontend;
|
| 2 |
|
| 3 | import com.mojang.jtracy.GpuApi;
|
| 4 | import com.mojang.jtracy.GpuContext;
|
| 5 | import com.mojang.jtracy.TracyClient;
|
| 6 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 7 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 8 | import java.util.OptionalLong;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class TracyGpuProfiler {
|
| 14 | private static final int MAX_QUERIES = 1024;
|
| 15 | private static final long TIME_BETWEEN_CALIBRATION = 5000000000L;
|
| 16 | private final GpuQueryPool queries;
|
| 17 | private final GpuContext context;
|
| 18 | private int head = 0;
|
| 19 | private int tail = 0;
|
| 20 | private final double period;
|
| 21 | private final FrontendGpuDevice device;
|
| 22 | private long calibrationOffset;
|
| 23 | private long lastCalibrationTime;
|
| 24 |
|
| 25 | public TracyGpuProfiler(final FrontendGpuDevice device) {
|
| 26 | this.queries = device.createTimestampQueryPool(1024);
|
| 27 | this.context = TracyClient.createGpuContext(GpuApi.OPENGL, System.nanoTime(), 1.0F);
|
| 28 | this.period = device.getDeviceInfo().timestampPeriod();
|
| 29 | this.device = device;
|
| 30 | this.lastCalibrationTime = System.nanoTime();
|
| 31 | this.calibrationOffset = device.getTimestampCalibrationOffset();
|
| 32 | }
|
| 33 |
|
| 34 | public void close() {
|
| 35 | this.queries.close();
|
| 36 | }
|
| 37 |
|
| 38 | public void pushZone(final CommandEncoder encoder, final String name) {
|
| 39 | int queryId = this.nextQueryId();
|
| 40 | encoder.writeTimestamp(this.queries, queryId);
|
| 41 | this.context.beginZone(queryId, name, "", "", 0);
|
| 42 | }
|
| 43 |
|
| 44 | public void popZone(final CommandEncoder encoder) {
|
| 45 | int queryId = this.nextQueryId();
|
| 46 | encoder.writeTimestamp(this.queries, queryId);
|
| 47 | this.context.endZone(queryId);
|
| 48 | }
|
| 49 |
|
| 50 | public void endFrame() {
|
| 51 | if (System.nanoTime() - this.lastCalibrationTime > 5000000000L) {
|
| 52 | this.calibrationOffset = this.device.getTimestampCalibrationOffset();
|
| 53 | this.lastCalibrationTime = System.nanoTime();
|
| 54 | }
|
| 55 |
|
| 56 | if (this.head < this.tail) {
|
| 57 | OptionalLong[] timestamps = this.queries.getValues(this.tail, 1024 - this.tail);
|
| 58 |
|
| 59 | for (int i = 0; i < timestamps.length; i++) {
|
| 60 | OptionalLong timestamp = timestamps[i];
|
| 61 | if (!timestamp.isPresent()) {
|
| 62 | return;
|
| 63 | }
|
| 64 |
|
| 65 | this.context.submitQueryTimestamp(this.tail, (long)(timestamp.getAsLong() * this.period) + this.calibrationOffset);
|
| 66 | this.tail = (this.tail + 1) % 1024;
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | if (this.tail < this.head) {
|
| 71 | OptionalLong[] timestamps = this.queries.getValues(this.tail, this.head - this.tail);
|
| 72 |
|
| 73 | for (int i = 0; i < timestamps.length; i++) {
|
| 74 | OptionalLong timestamp = timestamps[i];
|
| 75 | if (!timestamp.isPresent()) {
|
| 76 | return;
|
| 77 | }
|
| 78 |
|
| 79 | this.context.submitQueryTimestamp(this.tail, (long)(timestamp.getAsLong() * this.period) + this.calibrationOffset);
|
| 80 | this.tail = (this.tail + 1) % 1024;
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | private int nextQueryId() {
|
| 86 | int id = this.head;
|
| 87 | this.head = (this.head + 1) % 1024;
|
| 88 | return id;
|
| 89 | }
|
| 90 | }
|
| 91 |
|