Skip to content

Commit

Permalink
Additional cleanup
Browse files Browse the repository at this point in the history
Also improves DrawBuffers class cache line alignment (32 bytes instead of 40)
  • Loading branch information
thr3343 committed Oct 24, 2024
1 parent d1bdc32 commit 0769d67
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TerrainRenderType, AreaBuffer> vertexBuffers = new EnumMap<>(TerrainRenderType.class);

//Need ugly minHeight Parameter to fix custom world heights (exceeding 384 Blocks in total)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -179,8 +177,6 @@ public void releaseBuffers() {
if (this.indexBuffer != null)
this.indexBuffer.freeBuffer();
this.indexBuffer = null;

this.allocated = false;
}

public boolean isAllocated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/vulkan/memory/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/vulkan/memory/MemoryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/net/vulkanmod/vulkan/memory/MemoryTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/vulkan/memory/VertexBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/vulkanmod/vulkan/queue/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Expand All @@ -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();
Expand All @@ -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()) {

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/vulkan/shader/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/vulkanmod/vulkan/texture/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/vulkanmod/vulkan/texture/VulkanImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/net/vulkanmod/vulkan/util/VUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 0769d67

Please sign in to comment.