import { assert } from './auxiliaries' ; import { Bindable } from './bindable' ; import { Initializable } from './initializable' ; import { AbstractObject } from './object' ; /**
 * Wrapper around an OpenGL array or element array buffer. This buffer may be used to be attached to multiple VAOs.
 */ export class Buffer extends AbstractObject < WebGLBuffer > implements Bindable { /**
     * Default buffer, e.g., used for unbind.
     */ static readonly DEFAULT_BUFFER = undefined ; see target protected _target : GLenum | undefined = Buffer . DEFAULT_BUFFER ; /**
     * Create a buffer object on the GPU.
     */ protected create ( target ) { const gl = this . _context . gl ; this . _object = gl . createBuffer ( ) ; this . _valid = this . _object instanceof WebGLBuffer ; if ( this . _valid ) { assert ( target === gl . ARRAY_BUFFER || target === gl . ELEMENT_ARRAY_BUFFER , `either ARRAY_BUFFER or ELEMENT_ARRAY_BUFFER expected as buffer target` ) ; this . _target = target ; } return this . _object ; } /**
     * Delete the buffer object on the GPU. This should have the reverse effect of `create`.
     */ protected delete ( ) { assert ( this . _object instanceof WebGLBuffer , `expected WebGLBuffer object` ) ; this . _context . gl . deleteBuffer ( this . _object ) ; this . _object = undefined ; this . _valid = false ; this . _target = Buffer . DEFAULT_BUFFER ; } /**
     * Binds the buffer object as buffer to predefined target.
     */ @ Initializable . assert_initialized ( ) bind ( ) { assert ( this . _target === this . _context . gl . ARRAY_BUFFER || this . _target === this . _context . gl . ELEMENT_ARRAY_BUFFER , `expected either ARRAY_BUFFER or ELEMENT_ARRAY_BUFFER as buffer target` ) ; this . _context . gl . bindBuffer ( this . _target , this . _object ) ; } /**
     * Binds null as current buffer to predefined target;
     */ @ Initializable . assert_initialized ( ) unbind ( ) { this . context . gl . bindBuffer ( this . _target , Buffer . DEFAULT_BUFFER ) ; } param data param usage param bind param unbind @ Initializable . assert_initialized ( ) data ( data , usage , bind = true , unbind = true ) { const gl = this . context . gl ; if ( bind ) { this . bind ( ) ; } gl . bufferData ( this . _target , data , usage ) ; if ( unbind ) { this . unbind ( ) ; } this . _valid = gl . isBuffer ( this . _object ) && gl . getError ( ) === gl . NO_ERROR ; const byteLength = typeof data === `number` ? data : data . byteLength ; const bytes = this . _valid ? byteLength : 0 ; this . context . allocationRegister . reallocate ( this . _identifier , bytes ) ; } param dstByteOffset param srcData param srcOffset param length param bind param unbind subData ( dstByteOffset , srcData , srcOffset = 0 , length = 0 , bind = true , unbind = true ) { const gl = this . context . gl ; if ( bind ) { this . bind ( ) ; } this . context . gl2facade . bufferSubData ( this . _target ! , dstByteOffset , srcData , srcOffset , length ) ; if ( unbind ) { this . unbind ( ) ; } this . _valid = gl . getError ( ) === gl . NO_ERROR ; } param index param size param type param normalized param stride param offset param bind param unbind @ Initializable . assert_initialized ( ) attribEnable ( index , size , type , normalized = false , stride = 0 , offset = 0 , bind = true , unbind = true ) { const gl = this . context . gl ; if ( bind ) { this . bind ( ) ; } gl . vertexAttribPointer ( index , size , type , normalized , stride , offset ) ; gl . enableVertexAttribArray ( index ) ; if ( unbind ) { this . unbind ( ) ; } } param index param bind param unbind @ Initializable . assert_initialized ( ) attribDisable ( index , bind = true , unbind = true ) { const gl = this . context . gl ; if ( bind ) { this . bind ( ) ; } gl . disableVertexAttribArray ( index ) ; if ( unbind ) { this . unbind ( ) ; } } /**
     * Returns the number of bytes this object approximately allocates on the GPU.
     */ get bytes ( ) : number { this . assertInitialized ( ) ; return this . context . allocationRegister . allocated ( this . _identifier ) ; } /**
     * Target to which the buffer object is bound (either GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER).
     * Readonly access to the target (as specified on initialization) the buffer will be bound to.
     */ get target ( ) : GLenum | undefined { this . assertInitialized ( ) ; return this . _target ; } }