| 1 | package com.mojang.renderpearl.backend.vulkan.checkpoints;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 4 | import com.mojang.renderpearl.backend.vulkan.VulkanDevice;
|
| 5 | import com.mojang.renderpearl.backend.vulkan.VulkanGpuBuffer;
|
| 6 | import com.mojang.renderpearl.backend.vulkan.VulkanQueue;
|
| 7 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.List;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import org.lwjgl.vulkan.AMDBufferMarker;
|
| 13 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class AmdCheckpointExtension implements CheckpointExtension {
|
| 17 | private static final long[] STAGES = new long[]{1L, 8192L};
|
| 18 | private final List<AmdCheckpointExtension.AmdCheckpointStorage> storages = new ArrayList<>();
|
| 19 |
|
| 20 | @Override
|
| 21 | public CheckpointExtension.CheckpointStorage createStorage(final VulkanDevice device, final VulkanQueue queue, final int maxFramesInFlight) {
|
| 22 | AmdCheckpointExtension.AmdCheckpointStorage storage = new AmdCheckpointExtension.AmdCheckpointStorage(device, queue, maxFramesInFlight);
|
| 23 | this.storages.add(storage);
|
| 24 | return storage;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public List<CheckpointExtension.QueueCheckpoints> retrieveCheckpoints(final boolean isDeviceLost) {
|
| 29 | List<CheckpointExtension.QueueCheckpoints> result = new ArrayList<>(this.storages.size());
|
| 30 |
|
| 31 | for (AmdCheckpointExtension.AmdCheckpointStorage storage : this.storages) {
|
| 32 | result.add(storage.retrieveCheckpoints());
|
| 33 | }
|
| 34 |
|
| 35 | return result;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void close() {
|
| 40 | for (AmdCheckpointExtension.AmdCheckpointStorage storage : this.storages) {
|
| 41 | storage.close();
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | @Environment(EnvType.CLIENT)
|
| 46 | private static class AmdCheckpointStorage extends AbstractCheckpointStorage implements UncheckedAutoCloseable {
|
| 47 | private final VulkanGpuBuffer buffer;
|
| 48 | private final GpuBufferSlice.MappedView mappedView;
|
| 49 |
|
| 50 | protected AmdCheckpointStorage(final VulkanDevice device, final VulkanQueue queue, final int maxFramesInFlight) {
|
| 51 | super(queue, maxFramesInFlight);
|
| 52 | this.buffer = device.createBuffer(() -> "Internal marker storage", 9, AmdCheckpointExtension.STAGES.length * 4);
|
| 53 | this.mappedView = this.buffer.map(true, false);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected void recordCheckpoint(final VkCommandBuffer commandBuffer, final int id) {
|
| 58 | for (int i = 0; i < AmdCheckpointExtension.STAGES.length; i++) {
|
| 59 | AMDBufferMarker.vkCmdWriteBufferMarker2AMD(commandBuffer, AmdCheckpointExtension.STAGES[i], this.buffer.vkBuffer(), i * 4, id);
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | public CheckpointExtension.QueueCheckpoints retrieveCheckpoints() {
|
| 64 | List<CheckpointExtension.StageCheckpoint> stageCheckpoints = new ArrayList<>();
|
| 65 |
|
| 66 | for (int i = 0; i < AmdCheckpointExtension.STAGES.length; i++) {
|
| 67 | AbstractCheckpointStorage.Checkpoint checkpoint = this.findCheckpoint(this.mappedView.data().getInt(i * 4));
|
| 68 | if (checkpoint != null) {
|
| 69 | stageCheckpoints.add(new CheckpointExtension.StageCheckpoint(AmdCheckpointExtension.STAGES[i], checkpoint.type(), checkpoint.label()));
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return new CheckpointExtension.QueueCheckpoints(this.queue.address(), stageCheckpoints);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void close() {
|
| 78 | this.mappedView.close();
|
| 79 | this.buffer.close();
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 |
|