@@ -4,6 +4,7 @@ const { Texture } = require('../../texture');
4
4
5
5
function toStringWithoutUtils ( fn ) {
6
6
return fn . toString ( )
7
+ . replace ( '=>' , '' )
7
8
. replace ( / ^ f u n c t i o n / , '' )
8
9
. replace ( / u t i l s [ . ] / g, '/*utils.*/' ) ;
9
10
}
@@ -12,7 +13,7 @@ function toStringWithoutUtils(fn) {
12
13
*
13
14
* @param {Kernel } Kernel
14
15
* @param {KernelVariable[] } args
15
- * @param {IKernel } originKernel
16
+ * @param {Kernel } originKernel
16
17
* @param {string } [setupContextString]
17
18
* @param {string } [destroyContextString]
18
19
* @returns {string }
@@ -68,26 +69,12 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
68
69
result . push ( `function ${ toStringWithoutUtils ( utils . isArray ) } ` ) ;
69
70
if ( kernel . renderOutput === kernel . renderTexture ) {
70
71
result . push ( Texture . toString ( ) ) ;
71
- result . push (
72
- ` const renderOutput = function ${
73
- toStringWithoutUtils ( kernel . renderOutput . toString ( ) )
74
- . replace ( `this.outputTexture` , 'null' )
75
- . replace ( 'this.texSize' , `new Int32Array(${ JSON . stringify ( Array . from ( kernel . texSize ) ) } )` )
76
- . replace ( 'this.threadDim' , `new Int32Array(${ JSON . stringify ( Array . from ( kernel . threadDim ) ) } )` )
77
- . replace ( 'this.output' , `new Int32Array(${ JSON . stringify ( this . output ) } )` )
78
- . replace ( 'this.context' , 'gl' )
79
- . replace ( 'this.gpu' , 'null' )
80
- . replace ( 'this.getReturnTextureType()' , `'${ kernel . getReturnTextureType ( ) } '` )
81
- } ;`
82
- ) ;
72
+ if ( kernel . TextureConstructor !== Texture ) {
73
+ result . push ( kernel . TextureConstructor . toString ( ) ) ;
74
+ }
83
75
} else {
84
76
result . push (
85
- ` const renderOutput = function ${ toStringWithoutUtils ( kernel . renderOutput . toString ( ) )
86
- . replace ( '() {' , '(pixels) {' )
87
- . replace ( ' const pixels = this.readFloatPixelsToFloat32Array();\n' , '' )
88
- . replace ( 'this.readPackedPixelsToFloat32Array()' , 'new Float32Array(pixels.buffer)' )
89
- . replace ( 'this.output;' , JSON . stringify ( kernel . output ) + ';' )
90
- } ;`
77
+ ` const renderOutput = function ${ toStringWithoutUtils ( kernel . formatValues ) } ;`
91
78
) ;
92
79
}
93
80
kernel . kernelArguments . forEach ( kernelArgument => {
@@ -108,7 +95,12 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
108
95
} ) ;
109
96
result . push ( '/** end setup uploads for kernel values **/' ) ;
110
97
result . push ( context . toString ( ) ) ;
111
- result . push ( ` ${ destroyContextString ? '\n' + destroyContextString + ' ' : '' } return renderOutput(${ context . getReadPixelsVariableName } );` ) ;
98
+ result . push ( ` ${ destroyContextString ? '\n' + destroyContextString + ' ' : '' } ` ) ;
99
+ if ( context . getReadPixelsVariableName ) {
100
+ result . push ( ` return renderOutput(${ kernel . precision === 'single' ? context . getReadPixelsVariableName : `new Float32Array(${ context . getReadPixelsVariableName } .buffer)` } , ${ kernel . output [ 0 ] } , ${ kernel . output [ 1 ] } , ${ kernel . output [ 2 ] } );` ) ;
101
+ } else {
102
+ result . push ( ` return null;` ) ;
103
+ }
112
104
result . push ( ' };' ) ;
113
105
return `function kernel(context = null) {
114
106
${ setupContextString ? setupContextString : '' } ${ result . join ( '\n' ) } }` ;
0 commit comments