@@ -646,27 +646,44 @@ export class NativeEngine extends Engine {
646646 }
647647 }
648648
649- /**
650- * @internal
651- */
652- public _isRenderingStateCompiled ( pipelineContext : IPipelineContext ) : boolean {
653- // TODO: support async shader compilcation
654- return true ;
649+ public isAsync ( pipelineContext : IPipelineContext ) : boolean {
650+ return ! ! ( pipelineContext . isAsync && this . _engine . createProgramAsync ) ;
655651 }
656652
657653 /**
658654 * @internal
659655 */
660656 public _executeWhenRenderingStateIsCompiled ( pipelineContext : IPipelineContext , action : ( ) => void ) {
661- // TODO: support async shader compilcation
662- action ( ) ;
657+ const nativePipelineContext = pipelineContext as NativePipelineContext ;
658+
659+ if ( ! this . isAsync ( pipelineContext ) ) {
660+ action ( ) ;
661+ return ;
662+ }
663+
664+ const oldHandler = nativePipelineContext . onCompiled ;
665+
666+ if ( oldHandler ) {
667+ nativePipelineContext . onCompiled = ( ) => {
668+ oldHandler ! ( ) ;
669+ action ( ) ;
670+ } ;
671+ } else {
672+ nativePipelineContext . onCompiled = action ;
673+ }
663674 }
664675
665676 public createRawShaderProgram ( ) : WebGLProgram {
666677 throw new Error ( "Not Supported" ) ;
667678 }
668679
669- public createShaderProgram ( _pipelineContext : IPipelineContext , vertexCode : string , fragmentCode : string , defines : Nullable < string > ) : WebGLProgram {
680+ public createShaderProgram ( pipelineContext : IPipelineContext , vertexCode : string , fragmentCode : string , defines : Nullable < string > ) : WebGLProgram {
681+ const nativePipelineContext = pipelineContext as NativePipelineContext ;
682+
683+ if ( nativePipelineContext . nativeProgram ) {
684+ throw new Error ( "Tried to create a second program in the same NativePipelineContext" ) ;
685+ }
686+
670687 this . onBeforeShaderCompilationObservable . notifyObservers ( this ) ;
671688
672689 const vertexInliner = new ShaderCodeInliner ( vertexCode ) ;
@@ -680,9 +697,26 @@ export class NativeEngine extends Engine {
680697 vertexCode = ThinEngine . _ConcatenateShader ( vertexCode , defines ) ;
681698 fragmentCode = ThinEngine . _ConcatenateShader ( fragmentCode , defines ) ;
682699
683- const program = this . _engine . createProgram ( vertexCode , fragmentCode ) ;
684- this . onAfterShaderCompilationObservable . notifyObservers ( this ) ;
685- return program as WebGLProgram ;
700+ const onSuccess = ( ) => {
701+ nativePipelineContext . isCompiled = true ;
702+ nativePipelineContext . onCompiled ?.( ) ;
703+ this . onAfterShaderCompilationObservable . notifyObservers ( this ) ;
704+ } ;
705+
706+ if ( this . isAsync ( pipelineContext ) ) {
707+ return this . _engine . createProgramAsync ( vertexCode , fragmentCode , onSuccess , ( error : Error ) => {
708+ nativePipelineContext . compilationError = error ;
709+ } ) as WebGLProgram ;
710+ } else {
711+ try {
712+ const program = ( nativePipelineContext . nativeProgram = this . _engine . createProgram ( vertexCode , fragmentCode ) ) ;
713+ onSuccess ( ) ;
714+ return program as WebGLProgram ;
715+ } catch ( e : any ) {
716+ const message = e ?. message ;
717+ throw new Error ( "SHADER ERROR" + ( typeof message === "string" ? "\n" + message : "" ) ) ;
718+ }
719+ }
686720 }
687721
688722 /**
0 commit comments