From ef659583a00a809b48d85fbd0c724656b802d119 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Sat, 16 Mar 2024 00:21:10 +0100 Subject: [PATCH 1/3] polymorphic Vma buffers --- .../github/knokko/boiler/buffer/BoilerBuffers.java | 4 ++-- .../knokko/boiler/buffer/DeviceOnlyVmaBuffer.java | 4 ++++ .../github/knokko/boiler/buffer/MappedVmaBuffer.java | 12 +----------- .../com/github/knokko/boiler/buffer/VmaBuffer.java | 9 ++++++--- 4 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/github/knokko/boiler/buffer/DeviceOnlyVmaBuffer.java diff --git a/src/main/java/com/github/knokko/boiler/buffer/BoilerBuffers.java b/src/main/java/com/github/knokko/boiler/buffer/BoilerBuffers.java index b6a723b..64d7a50 100644 --- a/src/main/java/com/github/knokko/boiler/buffer/BoilerBuffers.java +++ b/src/main/java/com/github/knokko/boiler/buffer/BoilerBuffers.java @@ -19,7 +19,7 @@ public BoilerBuffers(BoilerInstance instance) { this.instance = instance; } - public VmaBuffer create(long size, int usage, String name) { + public DeviceOnlyVmaBuffer create(long size, int usage, String name) { try (var stack = stackPush()) { var ciBuffer = VkBufferCreateInfo.calloc(stack); ciBuffer.sType$Default(); @@ -38,7 +38,7 @@ public VmaBuffer create(long size, int usage, String name) { instance.vmaAllocator(), ciBuffer, ciAllocation, pBuffer, pAllocation, null ), "CreateBuffer", name); instance.debug.name(stack, pBuffer.get(0), VK_OBJECT_TYPE_BUFFER, name); - return new VmaBuffer(pBuffer.get(0), pAllocation.get(0), size); + return new DeviceOnlyVmaBuffer(pBuffer.get(0), pAllocation.get(0), size); } } diff --git a/src/main/java/com/github/knokko/boiler/buffer/DeviceOnlyVmaBuffer.java b/src/main/java/com/github/knokko/boiler/buffer/DeviceOnlyVmaBuffer.java new file mode 100644 index 0000000..0bfd614 --- /dev/null +++ b/src/main/java/com/github/knokko/boiler/buffer/DeviceOnlyVmaBuffer.java @@ -0,0 +1,4 @@ +package com.github.knokko.boiler.buffer; + +public record DeviceOnlyVmaBuffer(long vkBuffer, long vmaAllocation, long size) implements VmaBuffer { +} diff --git a/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java b/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java index 3b9aefe..0546811 100644 --- a/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java +++ b/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java @@ -1,14 +1,4 @@ package com.github.knokko.boiler.buffer; -import static org.lwjgl.util.vma.Vma.vmaDestroyBuffer; - -public record MappedVmaBuffer(long vkBuffer, long vmaAllocation, long size, long hostAddress) { - - public void destroy(long vmaAllocator) { - vmaDestroyBuffer(vmaAllocator, vkBuffer, vmaAllocation); - } - - public VmaBuffer asBuffer() { - return new VmaBuffer(vkBuffer, vmaAllocation, size); - } +public record MappedVmaBuffer(long vkBuffer, long vmaAllocation, long size, long hostAddress) implements VmaBuffer { } diff --git a/src/main/java/com/github/knokko/boiler/buffer/VmaBuffer.java b/src/main/java/com/github/knokko/boiler/buffer/VmaBuffer.java index aac8059..019faf8 100644 --- a/src/main/java/com/github/knokko/boiler/buffer/VmaBuffer.java +++ b/src/main/java/com/github/knokko/boiler/buffer/VmaBuffer.java @@ -2,9 +2,12 @@ import static org.lwjgl.util.vma.Vma.vmaDestroyBuffer; -public record VmaBuffer(long vkBuffer, long vmaAllocation, long size) { +public sealed interface VmaBuffer permits DeviceOnlyVmaBuffer, MappedVmaBuffer { + long vkBuffer(); + long vmaAllocation(); + long size(); - void destroy(long vmaAllocator) { - vmaDestroyBuffer(vmaAllocator, vkBuffer, vmaAllocation); + default void destroy(long vmaAllocator) { + vmaDestroyBuffer(vmaAllocator, vkBuffer(), vmaAllocation()); } } From 13cbd68ac3469424e1522be66471c3bba1157a10 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Sat, 16 Mar 2024 13:02:18 +0100 Subject: [PATCH 2/3] remove asBuffer usages in samples and tests --- .../src/main/java/com/github/knokko/boiler/samples/HelloXR.java | 2 +- .../com/github/knokko/boiler/samples/TerrainPlayground.java | 2 +- .../com/github/knokko/boiler/compute/TestComputePipelines.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/src/main/java/com/github/knokko/boiler/samples/HelloXR.java b/samples/src/main/java/com/github/knokko/boiler/samples/HelloXR.java index 4756cba..111635e 100644 --- a/samples/src/main/java/com/github/knokko/boiler/samples/HelloXR.java +++ b/samples/src/main/java/com/github/knokko/boiler/samples/HelloXR.java @@ -218,7 +218,7 @@ public static void main(String[] args) throws InterruptedException { matrixWrite.dstArrayElement(0); matrixWrite.descriptorCount(1); matrixWrite.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); - matrixWrite.pBufferInfo(boiler.descriptors.bufferInfo(stack, matrixBuffer.asBuffer())); + matrixWrite.pBufferInfo(boiler.descriptors.bufferInfo(stack, matrixBuffer)); vkUpdateDescriptorSets(boiler.vkDevice(), descriptorWrites, null); diff --git a/samples/src/main/java/com/github/knokko/boiler/samples/TerrainPlayground.java b/samples/src/main/java/com/github/knokko/boiler/samples/TerrainPlayground.java index 3ba25e8..b3f3fd5 100644 --- a/samples/src/main/java/com/github/knokko/boiler/samples/TerrainPlayground.java +++ b/samples/src/main/java/com/github/knokko/boiler/samples/TerrainPlayground.java @@ -440,7 +440,7 @@ public static void main(String[] args) throws InterruptedException { uniformWrite.dstArrayElement(0); uniformWrite.descriptorCount(1); uniformWrite.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); - uniformWrite.pBufferInfo(boiler.descriptors.bufferInfo(stack, uniformBuffer.asBuffer())); + uniformWrite.pBufferInfo(boiler.descriptors.bufferInfo(stack, uniformBuffer)); var heightMapWrite = descriptorWrites.get(1); heightMapWrite.sType$Default(); heightMapWrite.dstSet(descriptorSet); diff --git a/src/test/java/com/github/knokko/boiler/compute/TestComputePipelines.java b/src/test/java/com/github/knokko/boiler/compute/TestComputePipelines.java index 436c2b6..7217252 100644 --- a/src/test/java/com/github/knokko/boiler/compute/TestComputePipelines.java +++ b/src/test/java/com/github/knokko/boiler/compute/TestComputePipelines.java @@ -83,7 +83,7 @@ public void testSimpleComputeShader() { descriptorWrites.dstArrayElement(0); descriptorWrites.descriptorCount(1); descriptorWrites.descriptorType(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - descriptorWrites.pBufferInfo(boiler.descriptors.bufferInfo(stack, buffer.asBuffer())); + descriptorWrites.pBufferInfo(boiler.descriptors.bufferInfo(stack, buffer)); vkUpdateDescriptorSets(boiler.vkDevice(), descriptorWrites, null); From 95232ec529cc13e77fbdbfe3ed67246bb0de30a2 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Sat, 16 Mar 2024 13:03:22 +0100 Subject: [PATCH 3/3] re-add the asBuffer method, marked as deprecated (API backwards compatibility) --- .../java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java b/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java index 0546811..081a5a3 100644 --- a/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java +++ b/src/main/java/com/github/knokko/boiler/buffer/MappedVmaBuffer.java @@ -1,4 +1,8 @@ package com.github.knokko.boiler.buffer; public record MappedVmaBuffer(long vkBuffer, long vmaAllocation, long size, long hostAddress) implements VmaBuffer { + @Deprecated + public VmaBuffer asBuffer() { + return this; + } }