import { log } from '@luma.gl/core' ; import GL from '@luma.gl/constants' ; import WebGLDebug from 'webgl-debug' ; globalThis . WebGLDebugUtils = WebGLDebug ; function getContextData ( gl ) { gl . luma = gl . luma || { } ; return gl . luma ; } deprecated export function makeDebugContext ( gl , { debug = true , throwOnError = false , break : breakpoints = false } = { } ) { if ( ! gl ) { return null ; } return debug ? getDebugContext ( gl , { throwOnError , break : breakpoints } ) : getRealContext ( gl ) ; } function getRealContext ( gl ) { const data = getContextData ( gl ) ; return data . realContext ? data . realContext : gl ; } function getDebugContext ( gl , opts ) { const data = getContextData ( gl ) ; if ( data . debugContext ) { return data . debugContext ; } for ( const key in GL ) { if ( ! ( key in gl ) && typeof GL [ key ] === 'number' ) { gl [ key ] = GL [ key ] ; } } class WebGLDebugContext { } const debugContext = WebGLDebug . makeDebugContext ( gl , onGLError . bind ( null , opts ) , onValidateGLFunc . bind ( null , opts ) ) ; Object . assign ( WebGLDebugContext . prototype , debugContext ) ; data . realContext = gl ; data . debugContext = debugContext ; debugContext . debug = true ; log . info ( 'debug context actived.' ) ( ) ; return debugContext ; } function getFunctionString ( functionName , functionArgs ) { let args = WebGLDebug . glFunctionArgsToString ( functionName , functionArgs ) ; args = `${ args . slice ( 0 , 100 ) }${ args . length > 100 ? '...' : '' }` ; return `gl.${ functionName }(${ args })` ; } function onGLError ( opts , err , functionName , args ) { const errorMessage = WebGLDebug . glEnumToString ( err ) ; const functionArgs = WebGLDebug . glFunctionArgsToString ( functionName , args ) ; const message = `${ errorMessage } in gl.${ functionName }(${ functionArgs })` ; if ( opts . throwOnError ) { throw new Error ( message ) ; } else { log . error ( message ) ( ) ; debugger ; } } function onValidateGLFunc ( opts , functionName , functionArgs ) { let functionString ; if ( log . level >= 4 ) { functionString = getFunctionString ( functionName , functionArgs ) ; log . log ( 4 , functionString ) ( ) ; } if ( opts . break ) { functionString = functionString || getFunctionString ( functionName , functionArgs ) ; const isBreakpoint = opts . break && opts . break . every ( ( breakOn ) => functionString . indexOf ( breakOn ) !== - 1 ) ; if ( isBreakpoint ) { debugger ; } } for ( const arg of functionArgs ) { if ( arg === undefined ) { functionString = functionString || getFunctionString ( functionName , functionArgs ) ; if ( opts . throwOnError ) { throw new Error ( `Undefined argument: ${ functionString }` ) ; } else { log . error ( `Undefined argument: ${ functionString }` ) ( ) ; debugger ; } } } }