/** * An index buffer stores index values into a {@link VertexBuffer}. Indexed graphical primitives * can normally utilize less memory that unindexed primitives (if vertices are shared). * * Typically, index buffers are set on {@link Mesh} objects. * * @category Graphics */ export class IndexBuffer { /** * Create a new IndexBuffer instance. * * @param {GraphicsDevice} graphicsDevice - The graphics device used to manage this index buffer. * @param {number} format - The type of each index to be stored in the index buffer. Can be: * * - {@link INDEXFORMAT_UINT8} * - {@link INDEXFORMAT_UINT16} * - {@link INDEXFORMAT_UINT32} * @param {number} numIndices - The number of indices to be stored in the index buffer. * @param {number} [usage] - The usage type of the vertex buffer. Can be: * * - {@link BUFFER_DYNAMIC} * - {@link BUFFER_STATIC} * - {@link BUFFER_STREAM} * * Defaults to {@link BUFFER_STATIC}. * @param {ArrayBuffer|ArrayBufferView} [initialData] - Initial data. Can be an * {@link ArrayBuffer} or a typed array (for example a {@link Uint16Array}). The data is stored * by reference and is not copied, so a typed array that is a view into a larger buffer is kept * as-is. If left unspecified, the index buffer will be initialized to zeros. * @param {object} [options] - Object for passing optional arguments. * @param {boolean} [options.storage] - Defines if the index buffer can be used as a storage * buffer by a compute shader. Defaults to false. Only supported on WebGPU. * @example * // Create an index buffer holding 3 16-bit indices. The buffer is marked as * // static, hinting that the buffer will never be modified. * const indices = new Uint16Array([0, 1, 2]); * const indexBuffer = new IndexBuffer(graphicsDevice, * INDEXFORMAT_UINT16, * 3, * BUFFER_STATIC, * indices); */ constructor(graphicsDevice: GraphicsDevice, format: number, numIndices: number, usage?: number, initialData?: ArrayBuffer | ArrayBufferView, options?: { storage?: boolean; }); device: GraphicsDevice; format: number; numIndices: number; usage: number; id: number; impl: any; bytesPerIndex: number; numBytes: number; storage: ArrayBuffer; /** * Frees resources associated with this index buffer. */ destroy(): void; adjustVramSizeTracking(vram: any, size: any): void; /** * Called when the rendering context was lost. It releases all context related resources. * * @ignore */ loseContext(): void; /** * Called when the rendering context is restored. Recreates the GPU buffer and uploads from * {@link IndexBuffer#lock|lock} storage. * * @ignore */ restoreContext(): void; /** * Returns the data format of the specified index buffer. * * @returns {number} The data format of the specified index buffer. Can be: * * - {@link INDEXFORMAT_UINT8} * - {@link INDEXFORMAT_UINT16} * - {@link INDEXFORMAT_UINT32} */ getFormat(): number; /** * Returns the number of indices stored in the specified index buffer. * * @returns {number} The number of indices stored in the specified index buffer. */ getNumIndices(): number; /** * Gives access to the block of memory that stores the buffer's indices. * * @returns {ArrayBuffer|ArrayBufferView} The memory that stores the buffer's indices. This * matches whatever was supplied as the initial data: an {@link ArrayBuffer} when none was * provided, otherwise the {@link ArrayBuffer} or typed array that was passed in. Use * {@link ArrayBuffer.isView} to distinguish the two before accessing it. */ lock(): ArrayBuffer | ArrayBufferView; /** * Uploads the client side copy of the index buffer to the GPU. When called without arguments, * uploads the entire buffer. An explicit range uploads only those bytes at the same GPU offset. * The first upload always initializes the entire GPU buffer, regardless of the requested range. * A zero byte length does nothing, including before the first upload. * * Partial uploads do not resize the buffer or change its CPU storage. The caller must upload * every modified range before expecting those changes on the GPU. Context restoration uploads * the entire CPU storage. Invalid ranges are ignored in all builds and report an assertion * in debug builds. * * @param {number} [byteOffset] - Offset in bytes from the start of the buffer's storage. * Defaults to 0. Must be a non-negative integer and a multiple of 4 on all graphics backends. * @param {number} [byteLength] - Number of bytes to upload. Defaults to the remaining bytes * after byteOffset. The length must be a non-negative integer and the range must fit within * the buffer. Partial ranges require a length that is a multiple of 4. Full-buffer uploads * support any byte length, whether the range is explicit or the arguments are omitted. * @example * // After modifying bytes 16 through 31 of the CPU storage: * indexBuffer.unlock(16, 16); */ unlock(byteOffset?: number, byteLength?: number): void; /** * Set preallocated data on the index buffer. * * @param {ArrayBuffer|ArrayBufferView} data - The index data to set. Can be an * {@link ArrayBuffer} or a typed array. Stored by reference, not copied. * @returns {boolean} True if the data was set successfully, false otherwise. * @ignore */ setData(data: ArrayBuffer | ArrayBufferView): boolean; /** * Copies the specified number of elements from data into index buffer. Optimized for * performance from both typed array as well as array. * * @param {Uint8Array|Uint16Array|Uint32Array|number[]} data - The data to write. * @param {number} count - The number of indices to write. * @ignore */ writeData(data: Uint8Array | Uint16Array | Uint32Array | number[], count: number): void; /** * Copies index data from index buffer into provided data array. * * @param {Uint8Array|Uint16Array|Uint32Array|number[]} data - The data array to write to. * @returns {number} The number of indices read. * @ignore */ readData(data: Uint8Array | Uint16Array | Uint32Array | number[]): number; } import type { GraphicsDevice } from './graphics-device.js';