| 1 | package com.mojang.blaze3d.systems;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 4 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 5 | import java.util.OptionalLong;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | @Environment(EnvType.CLIENT)
|
| 11 | public class TimerQuery implements AutoCloseable {
|
| 12 | private static final int ROTATIONS = 3;
|
| 13 | private @Nullable CommandEncoder activeEncoder;
|
| 14 | private final GpuQueryPool queryPool;
|
| 15 | private int currentRotationIndex;
|
| 16 | private TimerQuery.Status status = TimerQuery.Status.NOT_RECORDING;
|
| 17 | private final long[] results = new long[3];
|
| 18 |
|
| 19 | public TimerQuery() {
|
| 20 | this.queryPool = RenderSystem.getDevice().createTimestampQueryPool(6);
|
| 21 | }
|
| 22 |
|
| 23 | public void beginProfile() {
|
| 24 | if (this.status != TimerQuery.Status.NOT_RECORDING) {
|
| 25 | throw new IllegalStateException("Current profile not ended");
|
| 26 | }
|
| 27 |
|
| 28 | this.currentRotationIndex++;
|
| 29 | this.currentRotationIndex %= 3;
|
| 30 | this.activeEncoder = RenderSystem.getDevice().createCommandEncoder();
|
| 31 | this.activeEncoder.writeTimestamp(this.queryPool, this.currentRotationIndex * 2);
|
| 32 | this.status = TimerQuery.Status.STARTED;
|
| 33 | }
|
| 34 |
|
| 35 | public void endProfile() {
|
| 36 | if (this.status == TimerQuery.Status.STARTED && this.activeEncoder != null) {
|
| 37 | this.activeEncoder.writeTimestamp(this.queryPool, this.currentRotationIndex * 2 + 1);
|
| 38 | this.activeEncoder = null;
|
| 39 | this.status = TimerQuery.Status.AWAITING_VALUES;
|
| 40 | } else {
|
| 41 | throw new IllegalStateException("endProfile called before beginProfile");
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public long get() {
|
| 46 | long average = 0L;
|
| 47 |
|
| 48 | for (int i = 0; i < this.results.length; i++) {
|
| 49 | average += this.results[i];
|
| 50 | }
|
| 51 |
|
| 52 | return average / this.results.length;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void close() {
|
| 57 | this.queryPool.close();
|
| 58 | }
|
| 59 |
|
| 60 | public TimerQuery.Status getStatus() {
|
| 61 | if (this.status == TimerQuery.Status.AWAITING_VALUES) {
|
| 62 | OptionalLong[] timestamps = this.queryPool.getValues(this.currentRotationIndex * 2, 2);
|
| 63 | OptionalLong startValue = timestamps[0];
|
| 64 | OptionalLong endValue = timestamps[1];
|
| 65 | if (startValue.isPresent() && endValue.isPresent()) {
|
| 66 | long delta = endValue.getAsLong() - startValue.getAsLong();
|
| 67 | this.results[this.currentRotationIndex] = (long)((float)delta * RenderSystem.getDevice().getDeviceInfo().timestampPeriod());
|
| 68 | this.status = TimerQuery.Status.NOT_RECORDING;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return this.status;
|
| 73 | }
|
| 74 |
|
| 75 | @Environment(EnvType.CLIENT)
|
| 76 | public enum Status {
|
| 77 | NOT_RECORDING,
|
| 78 | STARTED,
|
| 79 | AWAITING_VALUES;
|
| 80 | }
|
| 81 | }
|
| 82 |
|