import { clContext as nodenCLContext , OpenCLBuffer , ImageDims } from 'nodencl' ; import { Loader , Saver } from './loadSave' ; import { PackImpl , Interlace } from './packer' ; import { ClJobs } from '../clJobQueue' ; export class ToRGBA { private readonly clContext : nodenCLContext ; private readonly loader : Loader ; private readonly numBytes : Array < number > ; private readonly numBytesRGBA : number ; private readonly totalBytes : number ; constructor ( clContext , colSpecRead , colSpecWrite , readImpl , clJobs ) { this . clContext = clContext ; this . loader = new Loader ( this . clContext , colSpecRead , colSpecWrite , readImpl , clJobs ) ; this . numBytes = readImpl . getNumBytes ( ) ; this . numBytesRGBA = readImpl . getNumBytesRGBA ( ) ; this . totalBytes = readImpl . getTotalBytes ( ) ; } async init ( ) { await this . loader . init ( ) ; } getNumBytes ( ) { return this . numBytes ; } getNumBytesRGBA ( ) { return this . numBytesRGBA ; } getTotalBytes ( ) { return this . totalBytes ; } async createSources ( ) { return Promise . all ( this . numBytes . map ( ( bytes ) => this . clContext . createBuffer ( bytes , 'readonly' , 'coarse' , undefined , 'ToRGBA src' ) ) ) ; } async createDest ( imageDims ) { return this . clContext . createBuffer ( this . numBytesRGBA , 'readonly' , 'coarse' , imageDims , 'ToRGBA' ) ; } async loadFrame ( input , sources , clQueue ? ) { const inputs = Array . isArray ( input ) ? input : [ input ] ; if ( sources . length !== inputs . length ) throw new Error ( `Expected buffer array of ${ sources . length } sources, found ${ inputs . length }` ) ; for ( let i = 0 ; i < inputs . length ; ++ i ) { await sources [ i ] . hostAccess ( 'writeonly' , clQueue ? clQueue : 0 , inputs [ i ] . slice ( 0 , this . numBytes [ i ] ) ) ; await sources [ i ] . hostAccess ( 'none' , clQueue ? clQueue : 0 ) ; } return Promise . resolve ( ) ; } processFrame ( sourceID , sources , dest ) { return this . loader . run ( { sources : sources , dest : dest } , { source : sourceID , timestamp : sources [ 0 ] . timestamp } , ( ) => sources . forEach ( ( s ) => s . release ( ) ) ) ; } finish ( ) { this . loader . releaseRefs ( ) ; } } export class FromRGBA { private readonly clContext : nodenCLContext ; private readonly saver : Saver ; private readonly numBytes : Array < number > ; private readonly numBytesRGBA : number ; private readonly totalBytes : number ; constructor ( clContext , colSpecRead , writeImpl , clJobs ) { this . clContext = clContext ; this . saver = new Saver ( this . clContext , colSpecRead , writeImpl , clJobs ) ; this . numBytes = writeImpl . getNumBytes ( ) ; this . numBytesRGBA = writeImpl . getNumBytesRGBA ( ) ; this . totalBytes = writeImpl . getTotalBytes ( ) ; } async init ( ) { await this . saver . init ( ) ; } getNumBytes ( ) { return this . numBytes ; } getNumBytesRGBA ( ) { return this . numBytesRGBA ; } getTotalBytes ( ) { return this . totalBytes ; } async createDests ( ) { return Promise . all ( this . numBytes . map ( ( bytes ) => this . clContext . createBuffer ( bytes , 'writeonly' , 'coarse' , undefined , 'FromRGBA' ) ) ) ; } processFrame ( sourceID , source , dests , interlace ? ) { this . saver . run ( { source : source , dests : dests , interlace : interlace } , { source : sourceID , timestamp : source . timestamp } , ( ) => source . release ( ) ) ; } async saveFrame ( output , clQueue ? ) { const outputs = Array . isArray ( output ) ? output : [ output ] ; for ( let o = 0 ; o < outputs . length ; ++ o ) await outputs [ o ] . hostAccess ( 'readonly' , clQueue ? clQueue : 0 ) ; return Promise . resolve ( ) ; } finish ( ) { this . saver . releaseRefs ( ) ; } }