@@ -646,27 +646,44 @@ export class NativeEngine extends Engine {
646
646
}
647
647
}
648
648
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 ) ;
655
651
}
656
652
657
653
/**
658
654
* @internal
659
655
*/
660
656
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
+ }
663
674
}
664
675
665
676
public createRawShaderProgram ( ) : WebGLProgram {
666
677
throw new Error ( "Not Supported" ) ;
667
678
}
668
679
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
+
670
687
this . onBeforeShaderCompilationObservable . notifyObservers ( this ) ;
671
688
672
689
const vertexInliner = new ShaderCodeInliner ( vertexCode ) ;
@@ -680,9 +697,26 @@ export class NativeEngine extends Engine {
680
697
vertexCode = ThinEngine . _ConcatenateShader ( vertexCode , defines ) ;
681
698
fragmentCode = ThinEngine . _ConcatenateShader ( fragmentCode , defines ) ;
682
699
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
+ }
686
720
}
687
721
688
722
/**
0 commit comments