From 0769d67babe9541740cd25782fbb332586ff8f7b Mon Sep 17 00:00:00 2001 From: thr3343 <125277899+thr3343@users.noreply.github.com> Date: Sat, 19 Oct 2024 10:43:01 +0100 Subject: [PATCH] Additional cleanup Also improves DrawBuffers class cache line alignment (32 bytes instead of 40) --- .../render/chunk/buffer/DrawBuffers.java | 8 ++------ .../render/chunk/buffer/UploadManager.java | 7 ++++--- .../net/vulkanmod/vulkan/memory/Buffer.java | 4 ++-- .../vulkanmod/vulkan/memory/MemoryType.java | 4 ++-- .../vulkanmod/vulkan/memory/MemoryTypes.java | 18 +++++++++--------- .../vulkanmod/vulkan/memory/VertexBuffer.java | 4 ++-- .../java/net/vulkanmod/vulkan/queue/Queue.java | 8 ++++---- .../net/vulkanmod/vulkan/shader/Pipeline.java | 4 ++-- .../vulkanmod/vulkan/texture/ImageUtil.java | 14 +++++++------- .../vulkanmod/vulkan/texture/VulkanImage.java | 6 +++--- .../java/net/vulkanmod/vulkan/util/VUtil.java | 16 ++++++++-------- 11 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/main/java/net/vulkanmod/render/chunk/buffer/DrawBuffers.java b/src/main/java/net/vulkanmod/render/chunk/buffer/DrawBuffers.java index 34345b441..02f3c0229 100644 --- a/src/main/java/net/vulkanmod/render/chunk/buffer/DrawBuffers.java +++ b/src/main/java/net/vulkanmod/render/chunk/buffer/DrawBuffers.java @@ -26,8 +26,7 @@ public class DrawBuffers { private final Vector3i origin; private final int minHeight; - private boolean allocated = false; - AreaBuffer indexBuffer; + private AreaBuffer indexBuffer; private final EnumMap vertexBuffers = new EnumMap<>(TerrainRenderType.class); //Need ugly minHeight Parameter to fix custom world heights (exceeding 384 Blocks in total) @@ -64,7 +63,6 @@ public void upload(RenderSection section, UploadBuffer buffer, TerrainRenderType } private AreaBuffer getAreaBufferOrAlloc(TerrainRenderType renderType) { - this.allocated = true; int initialSize = switch (renderType) { case SOLID, CUTOUT -> 100000; @@ -170,7 +168,7 @@ public void bindBuffers(VkCommandBuffer commandBuffer, Pipeline pipeline, Terrai } public void releaseBuffers() { - if (!this.allocated) + if (this.vertexBuffers.isEmpty()) return; this.vertexBuffers.values().forEach(AreaBuffer::freeBuffer); @@ -179,8 +177,6 @@ public void releaseBuffers() { if (this.indexBuffer != null) this.indexBuffer.freeBuffer(); this.indexBuffer = null; - - this.allocated = false; } public boolean isAllocated() { diff --git a/src/main/java/net/vulkanmod/render/chunk/buffer/UploadManager.java b/src/main/java/net/vulkanmod/render/chunk/buffer/UploadManager.java index 2dd0b02e8..25e191a96 100644 --- a/src/main/java/net/vulkanmod/render/chunk/buffer/UploadManager.java +++ b/src/main/java/net/vulkanmod/render/chunk/buffer/UploadManager.java @@ -6,6 +6,7 @@ import net.vulkanmod.vulkan.memory.Buffer; import net.vulkanmod.vulkan.memory.StagingBuffer; import net.vulkanmod.vulkan.queue.CommandPool; +import net.vulkanmod.vulkan.util.VUtil; import org.lwjgl.vulkan.VkCommandBuffer; import java.nio.ByteBuffer; @@ -36,13 +37,13 @@ public void submitUploads() { this.dstBuffers.clear(); } - public void recordUpload(Buffer buffer, long dstOffset, long bufferSize, ByteBuffer src) { + public void recordUpload(Buffer buffer, int dstOffset, int bufferSize, ByteBuffer src) { beginCommands(); VkCommandBuffer commandBuffer = this.commandBuffer.getHandle(); StagingBuffer stagingBuffer = Vulkan.getStagingBuffer(); - stagingBuffer.copyBuffer((int) bufferSize, src); + stagingBuffer.copyBuffer(bufferSize, src); if (!this.dstBuffers.add(buffer.getId())) { //Use BufferBarrier + granular QueueFamilyIndex @@ -71,7 +72,7 @@ public void copyBuffer(Buffer src, int srcOffset, Buffer dst, int dstOffset, int TransferQueue.BufferBarrier(commandBuffer, src.getId(), - VK_WHOLE_SIZE, 0, + -1, 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, diff --git a/src/main/java/net/vulkanmod/vulkan/memory/Buffer.java b/src/main/java/net/vulkanmod/vulkan/memory/Buffer.java index b0a558f41..36d9834c2 100644 --- a/src/main/java/net/vulkanmod/vulkan/memory/Buffer.java +++ b/src/main/java/net/vulkanmod/vulkan/memory/Buffer.java @@ -37,9 +37,9 @@ public void freeBuffer() { public long getAllocation() { return allocation; } - public long getUsedBytes() { return usedBytes; } + public int getUsedBytes() { return usedBytes; } - public long getOffset() { return offset; } + public int getOffset() { return offset; } public long getId() { return id; } diff --git a/src/main/java/net/vulkanmod/vulkan/memory/MemoryType.java b/src/main/java/net/vulkanmod/vulkan/memory/MemoryType.java index 91380c0d6..ecbc2fc3f 100644 --- a/src/main/java/net/vulkanmod/vulkan/memory/MemoryType.java +++ b/src/main/java/net/vulkanmod/vulkan/memory/MemoryType.java @@ -18,9 +18,9 @@ public abstract class MemoryType { abstract void createBuffer(Buffer buffer, int size); - abstract void copyToBuffer(Buffer buffer, long bufferSize, ByteBuffer byteBuffer); + abstract void copyToBuffer(Buffer buffer, int bufferSize, ByteBuffer byteBuffer); - abstract void copyFromBuffer(Buffer buffer, long bufferSize, ByteBuffer byteBuffer); + abstract void copyFromBuffer(Buffer buffer, int bufferSize, ByteBuffer byteBuffer); abstract boolean mappable(); diff --git a/src/main/java/net/vulkanmod/vulkan/memory/MemoryTypes.java b/src/main/java/net/vulkanmod/vulkan/memory/MemoryTypes.java index 300a813b5..c06f862c0 100644 --- a/src/main/java/net/vulkanmod/vulkan/memory/MemoryTypes.java +++ b/src/main/java/net/vulkanmod/vulkan/memory/MemoryTypes.java @@ -70,15 +70,15 @@ void createBuffer(Buffer buffer, int size) { } @Override - void copyToBuffer(Buffer buffer, long bufferSize, ByteBuffer byteBuffer) { + void copyToBuffer(Buffer buffer, int bufferSize, ByteBuffer byteBuffer) { StagingBuffer stagingBuffer = Vulkan.getStagingBuffer(); - stagingBuffer.copyBuffer((int) bufferSize, byteBuffer); + stagingBuffer.copyBuffer(bufferSize, byteBuffer); DeviceManager.getTransferQueue().copyBufferCmd(stagingBuffer.id, stagingBuffer.offset, buffer.getId(), buffer.getUsedBytes(), bufferSize); } @Override - void copyFromBuffer(Buffer buffer, long bufferSize, ByteBuffer byteBuffer) { + void copyFromBuffer(Buffer buffer, int bufferSize, ByteBuffer byteBuffer) { // TODO } @@ -103,13 +103,13 @@ static abstract class MappableMemory extends MemoryType { } @Override - void copyToBuffer(Buffer buffer, long bufferSize, ByteBuffer byteBuffer) { - VUtil.memcpy(byteBuffer, buffer.data.getByteBuffer(0, (int) buffer.bufferSize), (int) bufferSize, buffer.getUsedBytes()); + void copyToBuffer(Buffer buffer, int bufferSize, ByteBuffer byteBuffer) { + VUtil.memcpy(byteBuffer, buffer.data.getByteBuffer(0, buffer.bufferSize), bufferSize, buffer.getUsedBytes()); } @Override - void copyFromBuffer(Buffer buffer, long bufferSize, ByteBuffer byteBuffer) { - VUtil.memcpy(buffer.data.getByteBuffer(0, (int) buffer.bufferSize), byteBuffer, 0); + void copyFromBuffer(Buffer buffer, int bufferSize, ByteBuffer byteBuffer) { + VUtil.memcpy(buffer.data.getByteBuffer(0, buffer.bufferSize), byteBuffer, 0); } @Override @@ -132,8 +132,8 @@ void createBuffer(Buffer buffer, int size) { VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT); } - void copyToBuffer(Buffer buffer, long dstOffset, long bufferSize, ByteBuffer byteBuffer) { - VUtil.memcpy(byteBuffer, buffer.data.getByteBuffer((int) 0, (int) buffer.bufferSize), (int) bufferSize, dstOffset); + void copyToBuffer(Buffer buffer, int dstOffset, int bufferSize, ByteBuffer byteBuffer) { + VUtil.memcpy(byteBuffer, buffer.data.getByteBuffer(0, buffer.bufferSize), bufferSize, dstOffset); } void copyBuffer(Buffer src, Buffer dst) { diff --git a/src/main/java/net/vulkanmod/vulkan/memory/VertexBuffer.java b/src/main/java/net/vulkanmod/vulkan/memory/VertexBuffer.java index 37f3ffc7f..c880fc0a3 100644 --- a/src/main/java/net/vulkanmod/vulkan/memory/VertexBuffer.java +++ b/src/main/java/net/vulkanmod/vulkan/memory/VertexBuffer.java @@ -16,8 +16,8 @@ public VertexBuffer(int size, MemoryType type) { } - public void copyToVertexBuffer(long vertexSize, long vertexCount, ByteBuffer byteBuffer) { - int bufferSize = (int) (vertexSize * vertexCount); + public void copyToVertexBuffer(int vertexSize, int vertexCount, ByteBuffer byteBuffer) { + int bufferSize = vertexSize * vertexCount; // long bufferSize = byteBuffer.limit(); if(bufferSize > this.bufferSize - this.usedBytes) { diff --git a/src/main/java/net/vulkanmod/vulkan/queue/Queue.java b/src/main/java/net/vulkanmod/vulkan/queue/Queue.java index efaf67971..a5cfd75d5 100644 --- a/src/main/java/net/vulkanmod/vulkan/queue/Queue.java +++ b/src/main/java/net/vulkanmod/vulkan/queue/Queue.java @@ -54,7 +54,7 @@ public void waitIdle() { } - public long copyBufferCmd(long srcBuffer, long srcOffset, long dstBuffer, long dstOffset, long size) { + public long copyBufferCmd(long srcBuffer, int srcOffset, long dstBuffer, long dstOffset, int size) { try(MemoryStack stack = stackPush()) { @@ -74,7 +74,7 @@ public long copyBufferCmd(long srcBuffer, long srcOffset, long dstBuffer, long d } } - public void uploadBufferImmediate(long srcBuffer, long srcOffset, long dstBuffer, long dstOffset, long size) { + public void uploadBufferImmediate(long srcBuffer, int srcOffset, long dstBuffer, int dstOffset, int size) { try(MemoryStack stack = stackPush()) { CommandPool.CommandBuffer commandBuffer = this.beginCommands(); @@ -93,7 +93,7 @@ public void uploadBufferImmediate(long srcBuffer, long srcOffset, long dstBuffer } } - public void uploadBufferCmd(VkCommandBuffer commandBuffer, long srcBuffer, long srcOffset, long dstBuffer, long dstOffset, long size) { + public void uploadBufferCmd(VkCommandBuffer commandBuffer, long srcBuffer, int srcOffset, long dstBuffer, int dstOffset, int size) { try(MemoryStack stack = stackPush()) { @@ -142,7 +142,7 @@ public void fillBuffer(long id, int bufferSize, int qNaN) { vkCmdFillBuffer(this.getCommandBuffer().getHandle(), id, 0, bufferSize, qNaN); } - public void BufferBarrier(VkCommandBuffer commandBuffer, long bufferhdle, long size_t, long offset, int srcAccess, int dstAccess, int srcStage, int dstStage { + public void BufferBarrier(VkCommandBuffer commandBuffer, long bufferhdle, int size_t, int offset, int srcAccess, int dstAccess, int srcStage, int dstStage) { try(MemoryStack stack = MemoryStack.stackPush()) { VkBufferMemoryBarrier.Buffer memBarrier = VkBufferMemoryBarrier.calloc(1, stack) diff --git a/src/main/java/net/vulkanmod/vulkan/shader/Pipeline.java b/src/main/java/net/vulkanmod/vulkan/shader/Pipeline.java index f574b8087..b9f0ba157 100644 --- a/src/main/java/net/vulkanmod/vulkan/shader/Pipeline.java +++ b/src/main/java/net/vulkanmod/vulkan/shader/Pipeline.java @@ -107,7 +107,7 @@ protected void createDescriptorSetLayout() { samplerLayoutBinding.binding(imageDescriptor.getBinding()); samplerLayoutBinding.descriptorCount(1); samplerLayoutBinding.descriptorType(imageDescriptor.getType()); - samplerLayoutBinding.pImmutableSamplers(imageDescriptor.getStages()==VK_SHADER_STAGE_VERTEX_BIT ? immutableSampler : null); + samplerLayoutBinding.pImmutableSamplers(imageDescriptor.getStages() == VK_SHADER_STAGE_VERTEX_BIT ? immutableSampler : null); samplerLayoutBinding.stageFlags(imageDescriptor.getStages()); } @@ -266,7 +266,7 @@ private void updateUniforms(UniformBuffer globalUB) { boolean useOwnUB = ubo.getUniformBuffer() != null; UniformBuffer ub = useOwnUB ? ubo.getUniformBuffer() : globalUB; - int currentOffset = (int) ub.getUsedBytes(); + int currentOffset = ub.getUsedBytes(); this.dynamicOffsets.put(i, currentOffset); // TODO: non mappable memory diff --git a/src/main/java/net/vulkanmod/vulkan/texture/ImageUtil.java b/src/main/java/net/vulkanmod/vulkan/texture/ImageUtil.java index 151969d06..997bd6ce4 100644 --- a/src/main/java/net/vulkanmod/vulkan/texture/ImageUtil.java +++ b/src/main/java/net/vulkanmod/vulkan/texture/ImageUtil.java @@ -28,7 +28,7 @@ public static void copyBufferToImageCmd(VkCommandBuffer commandBuffer, long buff region.imageSubresource().baseArrayLayer(0); region.imageSubresource().layerCount(1); region.imageOffset().set(xOffset, yOffset, 0); - region.imageExtent(VkExtent3D.calloc(stack).set(width, height, 1)); + region.imageExtent().set(width, height, 1); vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, region); } @@ -40,7 +40,7 @@ public static void downloadTexture(VulkanImage image, long ptr) { CommandPool.CommandBuffer commandBuffer = DeviceManager.getGraphicsQueue().beginCommands(); image.transitionImageLayout(stack, commandBuffer.getHandle(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); - long imageSize = (long) image.width * image.height * image.formatSize; + int imageSize = image.width * image.height * image.formatSize; LongBuffer pStagingBuffer = stack.mallocLong(1); PointerBuffer pStagingAllocation = stack.pointers(0L); @@ -57,7 +57,7 @@ public static void downloadTexture(VulkanImage image, long ptr) { vkWaitForFences(DeviceManager.vkDevice, fence, true, VUtil.UINT64_MAX); MemoryManager.MapAndCopy(pStagingAllocation.get(0), - (data) -> VUtil.memcpy(data.getByteBuffer(0, (int) imageSize), ptr) + (data) -> VUtil.memcpy(data.getByteBuffer(0, imageSize), ptr) ); MemoryManager.freeBuffer(pStagingBuffer.get(0), pStagingAllocation.get(0)); @@ -122,16 +122,16 @@ public static void generateMipmaps(VulkanImage image) { prevLevel = level - 1; VkImageBlit.Buffer blit = VkImageBlit.calloc(1, stack); - blit.srcOffsets(0, VkOffset3D.calloc(stack).set(0, 0, 0)); - blit.srcOffsets(1, VkOffset3D.calloc(stack).set(image.width >> prevLevel, image.height >> prevLevel, 1)); + blit.srcOffsets(0).set(0, 0, 0); + blit.srcOffsets(1).set(image.width >> prevLevel, image.height >> prevLevel, 1); blit.srcSubresource() .aspectMask(VK_IMAGE_ASPECT_COLOR_BIT) .mipLevel(prevLevel) .baseArrayLayer(0) .layerCount(1); - blit.dstOffsets(0, VkOffset3D.calloc(stack).set(0, 0, 0)); - blit.dstOffsets(1, VkOffset3D.calloc(stack).set(image.width >> level, image.height >> level, 1)); + blit.dstOffsets(0).set(0, 0, 0); + blit.dstOffsets(1).set(image.width >> level, image.height >> level, 1); blit.dstSubresource() .aspectMask(VK_IMAGE_ASPECT_COLOR_BIT) .mipLevel(level) diff --git a/src/main/java/net/vulkanmod/vulkan/texture/VulkanImage.java b/src/main/java/net/vulkanmod/vulkan/texture/VulkanImage.java index f58d9fbf4..65a51d8de 100644 --- a/src/main/java/net/vulkanmod/vulkan/texture/VulkanImage.java +++ b/src/main/java/net/vulkanmod/vulkan/texture/VulkanImage.java @@ -192,7 +192,7 @@ public static long createImageView(long image, int format, int aspectFlags, int } public void uploadSubTextureAsync(int mipLevel, int width, int height, int xOffset, int yOffset, int unpackSkipRows, int unpackSkipPixels, int unpackRowLength, ByteBuffer buffer) { - long imageSize = buffer.limit(); + int imageSize = buffer.limit(); CommandPool.CommandBuffer commandBuffer = DeviceManager.getGraphicsQueue().getCommandBuffer(); try (MemoryStack stack = stackPush()) { @@ -202,10 +202,10 @@ public void uploadSubTextureAsync(int mipLevel, int width, int height, int xOffs StagingBuffer stagingBuffer = Vulkan.getStagingBuffer(); stagingBuffer.align(this.formatSize); - stagingBuffer.copyBuffer((int) imageSize, buffer); + stagingBuffer.copyBuffer(imageSize, buffer); ImageUtil.copyBufferToImageCmd(commandBuffer.getHandle(), stagingBuffer.getId(), id, mipLevel, width, height, xOffset, yOffset, - (int) (stagingBuffer.getOffset() + (unpackRowLength * unpackSkipRows + unpackSkipPixels) * this.formatSize), unpackRowLength, height); + stagingBuffer.getOffset() + (unpackRowLength * unpackSkipRows + unpackSkipPixels) * this.formatSize, unpackRowLength, height); long fence = DeviceManager.getGraphicsQueue().endIfNeeded(commandBuffer); if (fence != VK_NULL_HANDLE) diff --git a/src/main/java/net/vulkanmod/vulkan/util/VUtil.java b/src/main/java/net/vulkanmod/vulkan/util/VUtil.java index 46ffadeb3..5483fe2e7 100644 --- a/src/main/java/net/vulkanmod/vulkan/util/VUtil.java +++ b/src/main/java/net/vulkanmod/vulkan/util/VUtil.java @@ -52,8 +52,8 @@ public static void memcpy(ByteBuffer buffer, short[] indices) { buffer.rewind(); } - public static void memcpy(ByteBuffer buffer, short[] indices, long offset) { - buffer.position((int) offset); + public static void memcpy(ByteBuffer buffer, short[] indices, int offset) { + buffer.position(offset); for(short index : indices) { buffer.putShort(index); @@ -71,15 +71,15 @@ public static void memcpy(ByteBuffer src, long dstPtr) { MemoryUtil.memCopy(MemoryUtil.memAddress0(src), dstPtr, src.capacity()); } - public static void memcpy(ByteBuffer src, ByteBuffer dst, long offset) { - dst.position((int)offset); + public static void memcpy(ByteBuffer src, ByteBuffer dst, int offset) { + dst.position(offset); MemoryUtil.memCopy(src, dst); src.limit(src.capacity()).rewind(); } - public static void memcpy(ByteBuffer src, ByteBuffer dst, int size, long offset) { - dst.position((int)offset); + public static void memcpy(ByteBuffer src, ByteBuffer dst, int size, int offset) { + dst.position(offset); src.limit(size); MemoryUtil.memCopy(src, dst); @@ -104,8 +104,8 @@ public static void memcpy(ByteBuffer buffer, FloatBuffer floatBuffer) { floatBuffer.position(0); } - public static void memcpy(ByteBuffer buffer, FloatBuffer floatBuffer, long offset) { - buffer.position((int) offset); + public static void memcpy(ByteBuffer buffer, FloatBuffer floatBuffer, int offset) { + buffer.position(offset); while(floatBuffer.hasRemaining()) { float f = floatBuffer.get(); buffer.putFloat(f);