| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 4 | import java.nio.LongBuffer;
|
| 5 | import java.util.OptionalLong;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import org.lwjgl.system.MemoryStack;
|
| 9 | import org.lwjgl.vulkan.VK12;
|
| 10 | import org.lwjgl.vulkan.VkQueryPoolCreateInfo;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class VulkanQueryPool implements GpuQueryPool, Destroyable {
|
| 14 | private final VulkanDevice device;
|
| 15 | private final int size;
|
| 16 | private final long vkQueryPool;
|
| 17 |
|
| 18 | public VulkanQueryPool(final VulkanDevice device, final int size) {
|
| 19 | this.device = device;
|
| 20 | this.size = size;
|
| 21 |
|
| 22 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 23 | VkQueryPoolCreateInfo createInfo = VkQueryPoolCreateInfo.calloc(stack).sType$Default();
|
| 24 | createInfo.queryType(2);
|
| 25 | createInfo.queryCount(size);
|
| 26 | LongBuffer pointer = stack.callocLong(1);
|
| 27 | VulkanUtils.crashIfFailure(device, VK12.vkCreateQueryPool(device.vkDevice(), createInfo, null, pointer), "Cannot create query pool");
|
| 28 | this.vkQueryPool = pointer.get(0);
|
| 29 | VK12.vkResetQueryPool(device.vkDevice(), this.vkQueryPool, 0, size);
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public int size() {
|
| 35 | return this.size;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public OptionalLong getValue(final int index) {
|
| 40 | return this.getValues(index, 1)[0];
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public OptionalLong[] getValues(final int index, final int count) {
|
| 45 | if (index + count > this.size) {
|
| 46 | throw new IndexOutOfBoundsException(
|
| 47 | "getValues would read out-of-bounds for an array of " + count + " starting at " + index + ", when total size is " + this.size
|
| 48 | );
|
| 49 | }
|
| 50 |
|
| 51 | OptionalLong[] result = new OptionalLong[count];
|
| 52 |
|
| 53 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 54 | LongBuffer values = stack.callocLong(2 * count);
|
| 55 | VulkanUtils.crashIfFailure(
|
| 56 | this.device, VK12.vkGetQueryPoolResults(this.device.vkDevice(), this.vkQueryPool, index, count, values, 16L, 5), "Cannot fetch query results"
|
| 57 | );
|
| 58 |
|
| 59 | for (int i = 0; i < count; i++) {
|
| 60 | if (values.get(i * 2 + 1) != 0L) {
|
| 61 | result[i] = OptionalLong.of(values.get(i * 2));
|
| 62 | } else {
|
| 63 | result[i] = OptionalLong.empty();
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | return result;
|
| 69 | }
|
| 70 |
|
| 71 | protected long vkQueryPool() {
|
| 72 | return this.vkQueryPool;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void close() {
|
| 77 | this.device.createCommandEncoder().queueForDestroy(this);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void destroy() {
|
| 82 | VK12.vkDestroyQueryPool(this.device.vkDevice(), this.vkQueryPool, null);
|
| 83 | }
|
| 84 | }
|
| 85 |
|