Skip to content

Commit

Permalink
Add MappedVkbBufferRange
Browse files Browse the repository at this point in the history
  • Loading branch information
knokko committed Oct 24, 2024
1 parent 5428ee5 commit aabb3e8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ tuple `(buffer, byteOffset, byteSize)`. You can obtain an instance by
calling the `range(...)` or `fullRange()` method of a `DeviceVkbBuffer` or
a `MappedVkbBuffer`.

### MappedVkbBufferRange
The `MappedVkbBufferRange` represents a range of a `MappedVkbBuffer`,
which is a tuple `(mappedBuffer, byteOffset, byteSize)`. You can obtain
an instance by calling the `mappedRange(...)` or `mappedFullRange()`
method of a `MappedVkbBuffer`. It provides `byteBuffer()`,
`shortBuffer()`, etc... methods to create Java NIO buffers whose memory
is backed by the buffer range. It also provides a `range(...)` method to
create a corresponding `VkbBufferRange`.

### Encoding/decoding images
You can use `boiler.buffers.encodeBufferedImageRGBA(...)` to encode/store a
`BufferedImage` in a `MappedVkbBuffer` in RGBA8 format. You can use this to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,21 @@
* host-visible memory, and must be mapped at <i>hostAddress</i> during its lifetime.
*/
public record MappedVkbBuffer(long vkBuffer, long vmaAllocation, long size, long hostAddress) implements VkbBuffer {

/**
* @return a <i>MappedVkbBufferRange</i> that covers this whole buffer
*/
public MappedVkbBufferRange fullMappedRange() {
return new MappedVkbBufferRange(this, 0L, size);
}

/**
* @param offset The offset, in bytes
* @param rangeSize The size of the range, in bytes
* @return a <i>MappedVkbBufferRange</i> that covers the bytes [offset, offset + rangeSize> from this buffer
*/
public MappedVkbBufferRange mappedRange(long offset, long rangeSize) {
if (offset + rangeSize > size) throw new IllegalArgumentException(offset + " + " + rangeSize + " > " + size);
return new MappedVkbBufferRange(this, offset, rangeSize);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.knokko.boiler.buffers;

import java.nio.*;

import static org.lwjgl.system.MemoryUtil.*;

/**
* Represents a range/section of a <i>MappedVkbBuffer</i>
* @param buffer The mapped buffer
* @param offset The offset into the mapped buffer, in bytes
* @param size The size of this range, in bytes
*/
public record MappedVkbBufferRange(MappedVkbBuffer buffer, long offset, long size) {

/**
* @return The start host address of this buffer range
*/
public long hostAddress() {
return buffer.hostAddress() + offset;
}

/**
* @return The size of this range <b>in bytes</b>, but represented as integer
* @throws UnsupportedOperationException When the size is larger than <i>Integer.MAX_VALUE</i>
*/
public int intSize() {
if (size > Integer.MAX_VALUE) throw new UnsupportedOperationException("Size (" + size + ") is too large");
return (int) size;
}

/**
* @return The corresponding <i>VkbBufferRange</i>
*/
public VkbBufferRange range() {
return new VkbBufferRange(buffer, offset, size);
}

/**
* @return A direct byte buffer that starts at the start of this range, and has the same capacity
*/
public ByteBuffer byteBuffer() {
return memByteBuffer(hostAddress(), intSize());
}

/**
* @return A direct short buffer that starts at the start of this range, with a capacity of <i>size / Short.BYTES</i>
*/
public ShortBuffer shortBuffer() {
return memShortBuffer(hostAddress(), intSize() / Short.BYTES);
}

/**
* @return A direct int buffer that starts at the start of this range, with a capacity of <i>size / Int.BYTES</i>
*/
public IntBuffer intBuffer() {
return memIntBuffer(hostAddress(), intSize() / Integer.BYTES);
}

/**
* @return A direct float buffer that starts at the start of this range, with a capacity of <i>size / Float.BYTES</i>
*/
public FloatBuffer floatBuffer() {
return memFloatBuffer(hostAddress(), intSize() / Float.BYTES);
}

/**
* @return A direct long buffer that starts at the start of this range, with a capacity of <i>size / Long.BYTES</i>
*/
public LongBuffer longBuffer() {
return memLongBuffer(hostAddress(), intSize() / Long.BYTES);
}

/**
* @return A direct double buffer that starts at the start of this range, with a capacity of <i>size / Double.BYTES</i>
*/
public DoubleBuffer doubleBuffer() {
return memDoubleBuffer(hostAddress(), intSize() / Double.BYTES);
}
}

0 comments on commit aabb3e8

Please sign in to comment.