Skip to content

Commit

Permalink
Merge pull request #3 from FalsePattern/master
Browse files Browse the repository at this point in the history
polymorphic Vma buffers
  • Loading branch information
knokko authored Mar 16, 2024
2 parents 886811a + 95232ec commit 1764614
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.knokko.boiler.buffer;

public record DeviceOnlyVmaBuffer(long vkBuffer, long vmaAllocation, long size) implements VmaBuffer {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
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 record MappedVmaBuffer(long vkBuffer, long vmaAllocation, long size, long hostAddress) implements VmaBuffer {
@Deprecated
public VmaBuffer asBuffer() {
return new VmaBuffer(vkBuffer, vmaAllocation, size);
return this;
}
}
9 changes: 6 additions & 3 deletions src/main/java/com/github/knokko/boiler/buffer/VmaBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 1764614

Please sign in to comment.