55 * GPU Accelerated JavaScript
66 *
77 * @version 2.9.3
8- * @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
8+ * @date Mon Aug 24 2020 13:12:42 GMT-0400 (Eastern Daylight Time)
99 *
1010 * @license MIT
1111 * The MIT License
@@ -1043,6 +1043,15 @@ class CPUFunctionNode extends FunctionNode {
10431043 this . astGeneric ( mNode . property , retArr ) ;
10441044 retArr . push ( ']' ) ;
10451045 return retArr ;
1046+ case 'fn()[][]' :
1047+ this . astGeneric ( mNode . object . object , retArr ) ;
1048+ retArr . push ( '[' ) ;
1049+ this . astGeneric ( mNode . object . property , retArr ) ;
1050+ retArr . push ( ']' ) ;
1051+ retArr . push ( '[' ) ;
1052+ this . astGeneric ( mNode . property , retArr ) ;
1053+ retArr . push ( ']' ) ;
1054+ return retArr ;
10461055 default :
10471056 throw this . astErrorOutput ( 'Unexpected expression' , mNode ) ;
10481057 }
@@ -1064,6 +1073,9 @@ class CPUFunctionNode extends FunctionNode {
10641073 case 'Array(2)' :
10651074 case 'Array(3)' :
10661075 case 'Array(4)' :
1076+ case 'Matrix(2)' :
1077+ case 'Matrix(3)' :
1078+ case 'Matrix(4)' :
10671079 case 'HTMLImageArray' :
10681080 case 'ArrayTexture(1)' :
10691081 case 'ArrayTexture(2)' :
@@ -1165,18 +1177,23 @@ class CPUFunctionNode extends FunctionNode {
11651177 }
11661178
11671179 astArrayExpression ( arrNode , retArr ) {
1180+ const returnType = this . getType ( arrNode ) ;
11681181 const arrLen = arrNode . elements . length ;
1169-
1170- retArr . push ( 'new Float32Array([' ) ;
1182+ const elements = [ ] ;
11711183 for ( let i = 0 ; i < arrLen ; ++ i ) {
1172- if ( i > 0 ) {
1173- retArr . push ( ', ' ) ;
1174- }
1175- const subNode = arrNode . elements [ i ] ;
1176- this . astGeneric ( subNode , retArr )
1184+ const element = [ ] ;
1185+ this . astGeneric ( arrNode . elements [ i ] , element ) ;
1186+ elements . push ( element . join ( '' ) ) ;
1187+ }
1188+ switch ( returnType ) {
1189+ case 'Matrix(2)' :
1190+ case 'Matrix(3)' :
1191+ case 'Matrix(4)' :
1192+ retArr . push ( `[${ elements . join ( ', ' ) } ]` ) ;
1193+ break ;
1194+ default :
1195+ retArr . push ( `new Float32Array([${ elements . join ( ', ' ) } ])` ) ;
11771196 }
1178- retArr . push ( '])' ) ;
1179-
11801197 return retArr ;
11811198 }
11821199
@@ -1189,6 +1206,7 @@ class CPUFunctionNode extends FunctionNode {
11891206module . exports = {
11901207 CPUFunctionNode
11911208} ;
1209+
11921210} , { "../function-node" :9 } ] , 6 :[ function ( require , module , exports ) {
11931211const { utils } = require ( '../../utils' ) ;
11941212
@@ -1208,6 +1226,9 @@ function constantsToString(constants, types) {
12081226 case 'Array(2)' :
12091227 case 'Array(3)' :
12101228 case 'Array(4)' :
1229+ case 'Matrix(2)' :
1230+ case 'Matrix(3)' :
1231+ case 'Matrix(4)' :
12111232 results . push ( `${ name } :new ${ constant . constructor . name } (${ JSON . stringify ( Array . from ( constant ) ) } )` ) ;
12121233 break ;
12131234 }
@@ -1347,6 +1368,9 @@ ${ header.join('\n') }
13471368 case 'Array(2)':
13481369 case 'Array(3)':
13491370 case 'Array(4)':
1371+ case 'Matrix(2)':
1372+ case 'Matrix(3)':
1373+ case 'Matrix(4)':
13501374 if (incomingConstants.hasOwnProperty(p)) {
13511375 console.warn('constant ' + p + ' of type ' + type + ' cannot be resigned');
13521376 }
@@ -2843,6 +2867,13 @@ class FunctionNode {
28432867 case 'BlockStatement' :
28442868 return this . getType ( ast . body ) ;
28452869 case 'ArrayExpression' :
2870+ const childType = this . getType ( ast . elements [ 0 ] ) ;
2871+ switch ( childType ) {
2872+ case 'Array(2)' :
2873+ case 'Array(3)' :
2874+ case 'Array(4)' :
2875+ return `Matrix(${ ast . elements . length } )` ;
2876+ }
28462877 return `Array(${ ast . elements . length } )` ;
28472878 case 'Literal' :
28482879 const literalKey = this . astKey ( ast ) ;
@@ -3774,6 +3805,7 @@ class FunctionNode {
37743805 } ;
37753806 }
37763807 case 'fn()[]' :
3808+ case 'fn()[][]' :
37773809 case '[][]' :
37783810 return {
37793811 signature : variableSignature ,
@@ -3883,6 +3915,9 @@ const typeLookupMap = {
38833915 'Array(2)' : 'Number' ,
38843916 'Array(3)' : 'Number' ,
38853917 'Array(4)' : 'Number' ,
3918+ 'Matrix(2)' : 'Number' ,
3919+ 'Matrix(3)' : 'Number' ,
3920+ 'Matrix(4)' : 'Number' ,
38863921 'Array2D' : 'Number' ,
38873922 'Array3D' : 'Number' ,
38883923 'Input' : 'Number' ,
@@ -4380,14 +4415,15 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
43804415 result . push ( context . toString ( ) ) ;
43814416 if ( kernel . renderOutput === kernel . renderTexture ) {
43824417 context . reset ( ) ;
4418+ const framebufferName = context . getContextVariableName ( kernel . framebuffer ) ;
43834419 if ( kernel . renderKernels ) {
43844420 const results = kernel . renderKernels ( ) ;
43854421 const textureName = context . getContextVariableName ( kernel . texture . texture ) ;
43864422 result . push ( ` return {
43874423 result: {
43884424 texture: ${ textureName } ,
43894425 type: '${ results . result . type } ',
4390- toArray: ${ getToArrayString ( results . result , textureName ) }
4426+ toArray: ${ getToArrayString ( results . result , textureName , framebufferName ) }
43914427 },` ) ;
43924428 const { subKernels, mappedTextures } = kernel ;
43934429 for ( let i = 0 ; i < subKernels . length ; i ++ ) {
@@ -4399,7 +4435,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
43994435 ${ subKernel . property } : {
44004436 texture: ${ subKernelTextureName } ,
44014437 type: '${ subKernelResult . type } ',
4402- toArray: ${ getToArrayString ( subKernelResult , subKernelTextureName ) }
4438+ toArray: ${ getToArrayString ( subKernelResult , subKernelTextureName , framebufferName ) }
44034439 },` ) ;
44044440 }
44054441 result . push ( ` };` ) ;
@@ -4409,7 +4445,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
44094445 result . push ( ` return {
44104446 texture: ${ textureName } ,
44114447 type: '${ rendered . type } ',
4412- toArray: ${ getToArrayString ( rendered , textureName ) }
4448+ toArray: ${ getToArrayString ( rendered , textureName , framebufferName ) }
44134449 };` ) ;
44144450 }
44154451 }
@@ -4424,7 +4460,7 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
44244460
44254461 let constantsUpload = [ ] ;
44264462 kernelConstants . forEach ( ( kernelConstant ) => {
4427- constantsUpload . push ( `${ kernelConstant . getStringValueHandler ( ) } ` ) ;
4463+ constantsUpload . push ( `${ kernelConstant . getStringValueHandler ( ) } ` ) ;
44284464 } ) ;
44294465 return `function kernel(settings) {
44304466 const { context, constants } = settings;
@@ -4468,14 +4504,17 @@ function getGetPixelsString(kernel) {
44684504 } ) ;
44694505}
44704506
4471- function getToArrayString ( kernelResult , textureName ) {
4507+ function getToArrayString ( kernelResult , textureName , framebufferName ) {
44724508 const toArray = kernelResult . toArray . toString ( ) ;
44734509 const useFunctionKeyword = ! / ^ f u n c t i o n / . test ( toArray ) ;
44744510 const flattenedFunctions = utils . flattenFunctionToString ( `${ useFunctionKeyword ? 'function ' : '' } ${ toArray } ` , {
44754511 findDependency : ( object , name ) => {
44764512 if ( object === 'utils' ) {
44774513 return `const ${ name } = ${ utils [ name ] . toString ( ) } ;` ;
44784514 } else if ( object === 'this' ) {
4515+ if ( name === 'framebuffer' ) {
4516+ return '' ;
4517+ }
44794518 return `${ useFunctionKeyword ? 'function ' : '' } ${ kernelResult [ name ] . toString ( ) } ` ;
44804519 } else {
44814520 throw new Error ( 'unhandled fromObject' ) ;
@@ -4489,17 +4528,14 @@ function getToArrayString(kernelResult, textureName) {
44894528 if ( isDeclaration ) return null ;
44904529 return 'gl' ;
44914530 }
4492- if ( property === '_framebuffer' ) {
4493- return '_framebuffer' ;
4494- }
44954531 if ( kernelResult . hasOwnProperty ( property ) ) {
44964532 return JSON . stringify ( kernelResult [ property ] ) ;
44974533 }
44984534 throw new Error ( `unhandled thisLookup ${ property } ` ) ;
44994535 }
45004536 } ) ;
45014537 return `() => {
4502- let _framebuffer ;
4538+ function framebuffer() { return ${ framebufferName } ; } ;
45034539 ${ flattenedFunctions }
45044540 return toArray();
45054541 }` ;
@@ -4542,6 +4578,7 @@ function findKernelValue(argument, kernelValues, values, context, uploadedValues
45424578module . exports = {
45434579 glKernelString
45444580} ;
4581+
45454582} , { "../../utils" :113 , "gl-wiretap" :2 } ] , 12 :[ function ( require , module , exports ) {
45464583const { Kernel } = require ( '../kernel' ) ;
45474584const { utils } = require ( '../../utils' ) ;
@@ -5801,18 +5838,12 @@ class GLTexture extends Texture {
58015838 if ( this . texture . _refs ) return ;
58025839 }
58035840 this . context . deleteTexture ( this . texture ) ;
5804- if ( this . texture . _refs === 0 && this . _framebuffer ) {
5805- this . context . deleteFramebuffer ( this . _framebuffer ) ;
5806- this . _framebuffer = null ;
5807- }
58085841 }
58095842
58105843 framebuffer ( ) {
58115844 if ( ! this . _framebuffer ) {
5812- this . _framebuffer = this . context . createFramebuffer ( ) ;
5845+ this . _framebuffer = this . kernel . getRawValueFramebuffer ( this . size [ 0 ] , this . size [ 1 ] ) ;
58135846 }
5814- this . _framebuffer . width = this . size [ 0 ] ;
5815- this . _framebuffer . height = this . size [ 1 ] ;
58165847 return this . _framebuffer ;
58175848 }
58185849}
@@ -5926,8 +5957,7 @@ class GLTextureUnsigned extends GLTexture {
59265957 }
59275958 renderRawOutput ( ) {
59285959 const { context : gl } = this ;
5929- const framebuffer = gl . createFramebuffer ( ) ;
5930- gl . bindFramebuffer ( gl . FRAMEBUFFER , framebuffer ) ;
5960+ gl . bindFramebuffer ( gl . FRAMEBUFFER , this . framebuffer ( ) ) ;
59315961 gl . framebufferTexture2D (
59325962 gl . FRAMEBUFFER ,
59335963 gl . COLOR_ATTACHMENT0 ,
@@ -7412,6 +7442,9 @@ class WebGLFunctionNode extends FunctionNode {
74127442 case 'Array(4)' :
74137443 case 'Array(3)' :
74147444 case 'Array(2)' :
7445+ case 'Matrix(2)' :
7446+ case 'Matrix(3)' :
7447+ case 'Matrix(4)' :
74157448 case 'Input' :
74167449 this . astGeneric ( ast . argument , result ) ;
74177450 break ;
@@ -7977,7 +8010,7 @@ class WebGLFunctionNode extends FunctionNode {
79778010 }
79788011 const markupType = typeMap [ type ] ;
79798012 if ( ! markupType ) {
7980- throw this . astErrorOutput ( `Markup type ${ markupType } not handled` , varDecNode ) ;
8013+ throw this . astErrorOutput ( `Markup type ${ type } not handled` , varDecNode ) ;
79818014 }
79828015 const declarationResult = [ ] ;
79838016 if ( actualType === 'Integer' && type === 'Integer' ) {
@@ -8275,6 +8308,15 @@ class WebGLFunctionNode extends FunctionNode {
82758308 retArr . push ( this . memberExpressionPropertyMarkup ( property ) ) ;
82768309 retArr . push ( ']' ) ;
82778310 return retArr ;
8311+ case 'fn()[][]' :
8312+ this . astCallExpression ( mNode . object . object , retArr ) ;
8313+ retArr . push ( '[' ) ;
8314+ retArr . push ( this . memberExpressionPropertyMarkup ( mNode . object . property ) ) ;
8315+ retArr . push ( ']' ) ;
8316+ retArr . push ( '[' ) ;
8317+ retArr . push ( this . memberExpressionPropertyMarkup ( mNode . property ) ) ;
8318+ retArr . push ( ']' ) ;
8319+ return retArr ;
82788320 case '[][]' :
82798321 this . astArrayExpression ( mNode . object , retArr ) ;
82808322 retArr . push ( '[' ) ;
@@ -8402,6 +8444,14 @@ class WebGLFunctionNode extends FunctionNode {
84028444 this . memberExpressionXYZ ( xProperty , yProperty , zProperty , retArr ) ;
84038445 retArr . push ( ')' ) ;
84048446 break ;
8447+ case 'Matrix(2)' :
8448+ case 'Matrix(3)' :
8449+ case 'Matrix(4)' :
8450+ retArr . push ( `${ markupName } [${ this . memberExpressionPropertyMarkup ( yProperty ) } ]` ) ;
8451+ if ( yProperty ) {
8452+ retArr . push ( `[${ this . memberExpressionPropertyMarkup ( xProperty ) } ]` ) ;
8453+ }
8454+ break ;
84058455 default :
84068456 throw new Error ( `unhandled member expression "${ type } "` ) ;
84078457 }
@@ -8574,9 +8624,19 @@ class WebGLFunctionNode extends FunctionNode {
85748624 }
85758625
85768626 astArrayExpression ( arrNode , retArr ) {
8627+ const returnType = this . getType ( arrNode ) ;
8628+
85778629 const arrLen = arrNode . elements . length ;
85788630
8579- retArr . push ( 'vec' + arrLen + '(' ) ;
8631+ switch ( returnType ) {
8632+ case 'Matrix(2)' :
8633+ case 'Matrix(3)' :
8634+ case 'Matrix(4)' :
8635+ retArr . push ( `mat${ arrLen } (` ) ;
8636+ break ;
8637+ default :
8638+ retArr . push ( `vec${ arrLen } (` ) ;
8639+ }
85808640 for ( let i = 0 ; i < arrLen ; ++ i ) {
85818641 if ( i > 0 ) {
85828642 retArr . push ( ', ' ) ;
@@ -8630,6 +8690,9 @@ const typeMap = {
86308690 'Array(2)' : 'vec2' ,
86318691 'Array(3)' : 'vec3' ,
86328692 'Array(4)' : 'vec4' ,
8693+ 'Matrix(2)' : 'mat2' ,
8694+ 'Matrix(3)' : 'mat3' ,
8695+ 'Matrix(4)' : 'mat4' ,
86338696 'Array2D' : 'sampler2D' ,
86348697 'Array3D' : 'sampler2D' ,
86358698 'Boolean' : 'bool' ,
@@ -10467,6 +10530,7 @@ class WebGLKernel extends GLKernel {
1046710530 this . framebuffer = gl . createFramebuffer ( ) ;
1046810531 this . framebuffer . width = texSize [ 0 ] ;
1046910532 this . framebuffer . height = texSize [ 1 ] ;
10533+ this . rawValueFramebuffers = { } ;
1047010534
1047110535 const vertices = new Float32Array ( [ - 1 , - 1 ,
1047210536 1 , - 1 , - 1 , 1 ,
@@ -10999,6 +11063,19 @@ float integerCorrectionModulo(float number, float divisor) {
1099911063 return result . join ( '' ) ;
1100011064 }
1100111065
11066+ getRawValueFramebuffer ( width , height ) {
11067+ if ( ! this . rawValueFramebuffers [ width ] ) {
11068+ this . rawValueFramebuffers [ width ] = { } ;
11069+ }
11070+ if ( ! this . rawValueFramebuffers [ width ] [ height ] ) {
11071+ const framebuffer = this . context . createFramebuffer ( ) ;
11072+ framebuffer . width = width ;
11073+ framebuffer . height = height ;
11074+ this . rawValueFramebuffers [ width ] [ height ] = framebuffer ;
11075+ }
11076+ return this . rawValueFramebuffers [ width ] [ height ] ;
11077+ }
11078+
1100211079 getKernelResultDeclaration ( ) {
1100311080 switch ( this . returnType ) {
1100411081 case 'Array(2)' :
@@ -11329,6 +11406,13 @@ float integerCorrectionModulo(float number, float divisor) {
1132911406 if ( this . framebuffer ) {
1133011407 this . context . deleteFramebuffer ( this . framebuffer ) ;
1133111408 }
11409+ for ( const width in this . rawValueFramebuffers ) {
11410+ for ( const height in this . rawValueFramebuffers [ width ] ) {
11411+ this . context . deleteFramebuffer ( this . rawValueFramebuffers [ width ] [ height ] ) ;
11412+ delete this . rawValueFramebuffers [ width ] [ height ] ;
11413+ }
11414+ delete this . rawValueFramebuffers [ width ] ;
11415+ }
1133211416 if ( this . vertShader ) {
1133311417 this . context . deleteShader ( this . vertShader ) ;
1133411418 }
@@ -14754,7 +14838,7 @@ const utils = {
1475414838 if ( ! flattened [ functionDependency ] ) {
1475514839 flattened [ functionDependency ] = true ;
1475614840 }
14757- flattenedFunctionDependencies . push ( utils . flattenFunctionToString ( functionDependency , settings ) + '\n' ) ;
14841+ functionDependency ? flattenedFunctionDependencies . push ( utils . flattenFunctionToString ( functionDependency , settings ) + '\n' ) : '' ;
1475814842 }
1475914843 return flattenedFunctionDependencies . join ( '' ) + result ;
1476014844 }
@@ -14984,5 +15068,6 @@ const _systemEndianness = utils.getSystemEndianness();
1498415068module . exports = {
1498515069 utils
1498615070} ;
15071+
1498715072} , { "./input" :109 , "./texture" :112 , "acorn" :1 } ] } , { } , [ 106 ] ) ( 106 )
1498815073} ) ;
0 commit comments