import { clContext as nodenCLContext , OpenCLProgram , KernelParams } from 'nodencl' ; import { ClJobs , JobCB , JobID } from '../clJobQueue' ; export abstract class ProcessImpl { protected readonly name : string ; protected readonly width : number ; protected readonly height : number ; readonly kernel : string ; readonly programName : string ; readonly globalWorkItems = 0 ; constructor ( name , width , height , kernel , programName ) { this . name = name ; this . width = width ; this . height = height ; this . kernel = kernel ; this . programName = programName ; } abstract init ( ) ; getName ( ) { return this . name ; } getNumBytesRGBA ( ) { return this . width * this . height * 4 * 4 ; } getGlobalWorkItems ( ) { return Uint32Array . from ( [ this . width , this . height ] ) ; } abstract getKernelParams ( params ) ; abstract releaseRefs ( ) ; } export default class ImageProcess { private readonly clContext : nodenCLContext ; private readonly processImpl : ProcessImpl ; protected readonly clJobs : ClJobs ; private program : OpenCLProgram | null = null ; constructor ( clContext , processImpl , clJobs ) { this . clContext = clContext ; this . processImpl = processImpl ; this . clJobs = clJobs ; } async init ( ) { this . program = await this . clContext . createProgram ( this . processImpl . kernel , { name : this . processImpl . programName , globalWorkItems : this . processImpl . getGlobalWorkItems ( ) } ) ; return this . processImpl . init ( ) ; } async run ( params , id , cb ) { if ( this . program == null ) throw new Error ( 'Loader.run failed with no program available' ) ; const kernelParams = await this . processImpl . getKernelParams ( params ) ; this . clJobs . add ( id , this . processImpl . getName ( ) , this . program , kernelParams , ( ) => { this . processImpl . releaseRefs ( ) ; cb ( ) ; } ) ; } finish ( ) { this . processImpl . releaseRefs ( ) ; } }