Skip to content

Commit

Permalink
Allow passing length when setting buffer data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakuna committed Dec 5, 2024
1 parent 878bc7c commit 8a3e120
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lakuna/ugl",
"version": "23.4.1",
"version": "24.0.0",
"description": "A lightweight WebGL2 library.",
"keywords": [
"front-end"
Expand Down
97 changes: 64 additions & 33 deletions src/core/buffers/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default abstract class Buffer<
* @param context - The rendering context.
* @param data - The initial data contained in this buffer or the size of this buffer's data store in bytes.
* @param usage - The intended usage of the buffer.
* @param offset - The index of the element to start reading the buffer at.
* @param offset - The index of the element to start reading the initial data at.
* @param length - The length of the initial data to read into the buffer.
* @param isHalf - Whether or not the data contains half floats if it contains floats.
* @param target - The target binding point of the buffer.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/createBuffer | createBuffer}
Expand All @@ -28,7 +29,8 @@ export default abstract class Buffer<
context: Context,
data: T | number,
usage = BufferUsage.STATIC_DRAW,
offset = 0,
offset: number | undefined = void 0,
length: number | undefined = void 0,
isHalf = false,
target = BufferTarget.ARRAY_BUFFER
) {
Expand All @@ -37,7 +39,6 @@ export default abstract class Buffer<
this.internal = this.gl.createBuffer();
this.targetCache = target;
this.usageCache = usage;
this.offsetCache = offset;
this.isHalfCache = isHalf;
if (typeof data === "number") {
this.sizeCache = data;
Expand All @@ -47,7 +48,7 @@ export default abstract class Buffer<
this.dataCache = data;
}

this.setData(data, usage, offset, isHalf);
this.setData(this.data, this.usage, offset, length, this.isHalf);
}

/**
Expand Down Expand Up @@ -121,17 +122,6 @@ export default abstract class Buffer<
return (this.typeCache ??= getDataTypeForTypedArray(this.data));
}

/**
* The element index offset at which to start reading this buffer.
* @internal
*/
private offsetCache;

/** The element index offset at which to start reading this buffer. */
public get offset(): number {
return this.offsetCache;
}

/**
* The size of this buffer's data store in bytes.
* @internal
Expand All @@ -155,68 +145,109 @@ export default abstract class Buffer<
}

/**
* Sets the data in this buffer.
* Replace the data in this buffer.
* @param data - The data to store in this buffer or the size to set this buffer's data store to in bytes.
* @param usage - The intended usage of the buffer.
* @param offset - The index of the element to start reading the supplied data at.
* @param length - The length of the supplied data to read.
* @param isHalf - Whether or not the data contains 16-bit floating-point data if it contains floating-point data.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | bufferData}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferSubData | bufferSubData}
*/
public setData(
data: T | number,
data: T,
usage?: BufferUsage,
offset?: number,
length?: number,
isHalf?: boolean
): void;

/**
* Updates a subset of the data in this buffer.
* Set the size of this buffer, clearing its data.
* @param data - The data to store in this buffer or the size to set this buffer's data store to in bytes.
* @param usage - The intended usage of the buffer.
* @param _ - An unused value.
* @param __ - An unused value.
* @param isHalf - Whether or not the data contains 16-bit floating-point data if it contains floating-point data.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | bufferData}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferSubData | bufferSubData}
*/
public setData(
data: number,
usage?: BufferUsage,
_?: unknown,
__?: unknown,
isHalf?: boolean
): void;

/**
* Update a subset of the data in this buffer.
* @param data - The data to store in this buffer.
* @param _ - An ignored value.
* @param offset - The index of the element to start reading the supplied data at.
* @param length - The length of the supplied data to read.
* @param __ - An ignored value.
* @param replaceOffset - The offset in bytes to start replacing data at.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData | bufferData}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferSubData | bufferSubData}
*/
public setData(
data: T,
_: unknown,
offset: number,
__: unknown,
replaceOffset: number
_?: unknown,
offset?: number,
length?: number,
__?: unknown,
replaceOffset?: number
): void;

public setData(
data: T | number,
usage: BufferUsage = this.usage,
offset: number = this.offset,
offset: number | undefined = void 0,
length: number | undefined = void 0,
isHalf: boolean = this.isHalf,
replaceOffset: number | undefined = void 0
) {
// Update regardless of cached value because the data in the `ArrayBufferView` might have changed.
this.bind();

// Set size of buffer (empty data).
if (typeof data === "number") {
this.gl.bufferData(this.target, data, usage);
this.dataCache = new Uint8Array(data) as unknown as T;
this.typeCache = DataType.UNSIGNED_BYTE;
this.sizeCache = data;
this.usageCache = usage;
this.isHalfCache = isHalf;
} else if (typeof replaceOffset === "number") {
this.gl.bufferSubData(this.target, replaceOffset, data, offset);
return;
}

// Update a portion of the buffer.
if (typeof replaceOffset === "number") {
this.gl.bufferSubData(
this.target,
replaceOffset,
data,
offset as unknown as number,
length
);
delete this.dataCache;
} else {
this.gl.bufferData(this.target, data, usage, offset);
this.dataCache = data;
this.typeCache = getDataTypeForTypedArray(data);
this.sizeCache = data.byteLength;
this.usageCache = usage;
this.isHalfCache = isHalf;
return;
}

this.offsetCache = offset;
// Replace the data in the buffer.
this.gl.bufferData(
this.target,
data,
usage,
offset as unknown as number,
length
);
this.dataCache = data;
this.typeCache = getDataTypeForTypedArray(data);
this.sizeCache = data.byteLength;
this.usageCache = usage;
this.isHalfCache = isHalf;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/core/buffers/ElementBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ export default class ElementBuffer extends Buffer<
* @param context - The rendering context.
* @param data - The initial data contained in this buffer or the size of this buffer's data store in bytes.
* @param usage - The intended usage of the buffer.
* @param offset - The index of the element to start reading the buffer at.
* @param offset - The index of the element to start reading the initial data at.
* @param length - The length of the initial data to read into the buffer.
* @throws {@link UnsupportedOperationError} if a buffer cannot be created.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/createBuffer | createBuffer}
*/
public constructor(
context: Context,
data: Uint8Array | Uint16Array | Uint32Array | number,
usage: BufferUsage = BufferUsage.STATIC_DRAW,
offset = 0
offset: number | undefined = void 0,
length: number | undefined = void 0
) {
// Ensure that the indices for a VAO aren't overwritten. Overwriting the indices of the default VAO is fine since μGL doesn't support using the default VAO anyway.
VertexArray.unbindGl(context);
Expand All @@ -131,6 +133,7 @@ export default class ElementBuffer extends Buffer<
data,
usage,
offset,
length,
false,
BufferTarget.ELEMENT_ARRAY_BUFFER
);
Expand Down
16 changes: 13 additions & 3 deletions src/core/buffers/VertexBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export default class VertexBuffer extends Buffer<ArrayBufferView> {
* @param context - The rendering context.
* @param data - The initial data contained in this buffer or the size of this buffer's data store in bytes.
* @param usage - The intended usage of the buffer.
* @param offset - The index of the element to start reading the buffer at.
* @param offset - The index of the element to start reading the initial data at.
* @param length - The length of the initial data to read into the buffer.
* @param isHalf - Whether or not the data contains half floats if it contains floats.
* @throws {@link UnsupportedOperationError} if a buffer cannot be created.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/createBuffer | createBuffer}
Expand All @@ -150,10 +151,19 @@ export default class VertexBuffer extends Buffer<ArrayBufferView> {
context: Context,
data: ArrayBufferView | number,
usage: BufferUsage = BufferUsage.STATIC_DRAW,
offset = 0,
offset: number | undefined = void 0,
length: number | undefined = void 0,
isHalf = false
) {
super(context, data, usage, offset, isHalf, BufferTarget.ARRAY_BUFFER);
super(
context,
data,
usage,
offset,
length,
isHalf,
BufferTarget.ARRAY_BUFFER
);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/utility/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export default function debug(
return `Uint8Array${stringify([...value], isEnum)}`;
}

if (value instanceof Uint8ClampedArray) {
return `Uint8ClampedArray${stringify([...value], isEnum)}`;
}

if (value instanceof Int16Array) {
return `Int16Array${stringify([...value], isEnum)}`;
}
Expand All @@ -204,6 +208,10 @@ export default function debug(
return `Int32Array${stringify([...value], isEnum)}`;
}

if (value instanceof Uint32Array) {
return `Uint32Array${stringify([...value], isEnum)}`;
}

if (value instanceof Float32Array) {
return `Float32Array${stringify([...value], isEnum)}`;
}
Expand All @@ -212,6 +220,14 @@ export default function debug(
return `Float64Array${stringify([...value], isEnum)}`;
}

if (value instanceof BigInt64Array) {
return `BigInt64Array${stringify([...value], isEnum)}`;
}

if (value instanceof BigUint64Array) {
return `BigUint64Array${stringify([...value], isEnum)}`;
}

if (Symbol.iterator in value) {
try {
return `Iterable${stringify([...(value as Iterable<unknown>)], isEnum)}`;
Expand Down

0 comments on commit 8a3e120

Please sign in to comment.