├── .gitmodules ├── CONTRIBUTING ├── LICENSE ├── NOTICE ├── README.md ├── ShaderDSL.js ├── examples ├── LICENSE ├── cube-noglsl.html ├── orion-nebula.jpg └── texture-noglsl.html ├── gruntfile.js ├── package.json ├── src ├── genglsl.js ├── infermem.js └── typeinference.js └── third-party └── README /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third-party/rivertrail"] 2 | path = third-party/rivertrail 3 | url = https://github.com/RiverTrail/RiverTrail.git 4 | [submodule "third-party/gladder"] 5 | path = third-party/gladder 6 | url = https://github.com/ttencate/gladder.git 7 | -------------------------------------------------------------------------------- /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | Contributions to this code are covered by the Adobe contributors 2 | license agreeent. Developers must sign and submit the Adobe CLA in 3 | order to contribute to this project. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | ShaderDSL.js 2 | Copyright 2013 Adobe Systems Incorporated 3 | 4 | This software is licensed under the Apache License, Version 2.0 (see 5 | LICENSE file). 6 | 7 | This software uses the following third party libraries that may have 8 | licenses differing from that of the software itself. You can find the 9 | libraries and their respective licenses below. 10 | 11 | 12 | RiverTrail, Copyright (c) 2011, Intel Corporation 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | - Redistributions of source code must retain the above copyright notice, 19 | this list of conditions and the following disclaimer. 20 | - Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 28 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34 | THE POSSIBILITY OF SUCH DAMAGE. 35 | 36 | 37 | 38 | Gladder, Copyright (c) 2012, Thomas ten Cate 39 | All rights reserved. 40 | 41 | Redistribution and use in source and binary forms, with or without 42 | modification, are permitted provided that the following conditions are 43 | met: 44 | 45 | * Redistributions of source code must retain the above copyright 46 | notice, this list of conditions and the following disclaimer. 47 | * Redistributions in binary form must reproduce the above copyright 48 | notice, this list of conditions and the following disclaimer in the 49 | documentation and/or other materials provided with the distribution. 50 | 51 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 52 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 53 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 54 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 55 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 56 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 57 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 61 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 | 63 | 64 | glMatrix.js Copyright (c) 2010 Brandon Jones 65 | 66 | This software is provided 'as-is', without any express or implied 67 | warranty. In no event will the authors be held liable for any damages 68 | arising from the use of this software. 69 | 70 | Permission is granted to anyone to use this software for any purpose, 71 | including commercial applications, and to alter it and redistribute it 72 | freely, subject to the following restrictions: 73 | 74 | 1. The origin of this software must not be misrepresented; you must not 75 | claim that you wrote the original software. If you use this software 76 | in a product, an acknowledgment in the product documentation would be 77 | appreciated but is not required. 78 | 79 | 2. Altered source versions must be plainly marked as such, and must not 80 | be misrepresented as being the original software. 81 | 82 | 3. This notice may not be removed or altered from any source 83 | distribution. 84 | 85 | 86 | Three.js r43 - http://github.com/mrdoob/three.js 87 | The MIT License 88 | 89 | Copyright (c) 2010-2011 three.js Authors. All rights reserved. 90 | Permission is hereby granted, free of charge, to any person obtaining a copy 91 | of this software and associated documentation files (the "Software"), to deal 92 | in the Software without restriction, including without limitation the rights 93 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 94 | copies of the Software, and to permit persons to whom the Software is 95 | furnished to do so, subject to the following conditions: 96 | 97 | The above copyright notice and this permission notice shall be included in 98 | all copies or substantial portions of the Software. 99 | 100 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 101 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 102 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 103 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 104 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 105 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 106 | THE SOFTWARE. 107 | 108 | 109 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ShaderDSL.js 2 | ============ 3 | 4 | 5 | A compiler from a subset of JavaScript to GLSL. 6 | 7 | Based on the RiverTrail JIT. 8 | Some examples use the Gladder library. 9 | 10 | FAQ 11 | === 12 | 13 | 14 | **What is ShaderDSL.js?** 15 | 16 | ShaderDSL.js allows you to write GLSL shaders in plain JavaScript, 17 | instead of the C like (GLSL) language used today. 18 | 19 | **Why ShaderDSL.js?** 20 | 21 | We wanted to experiment with a more approachable way for web 22 | developers to author shaders, hence the use of JavaScript. 23 | 24 | **Do I need to enable special flags or use a specific browser to play with ShaderDSL.js?** 25 | 26 | No, ShaderDSL.js relies only on the portable, pure Javascript part of 27 | RiverTrail, and works in all the browsers with WebGL enabled (Firefox, 28 | Chrome, Safari). 29 | 30 | **What is the status of ShaderDSL.js?** 31 | 32 | The project is an experiment and not intended to be used in 33 | production. If you are interested in contributing, please do, we want 34 | this project to be a playground for new ways of authoring shaders. 35 | -------------------------------------------------------------------------------- /examples/LICENSE: -------------------------------------------------------------------------------- 1 | The orion-nebula.jpg file is an unmodified file from the gladder library, and subject to the following license 2 | 3 | Copyright (c) 2012, Thomas ten Cate 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /examples/cube-noglsl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 31 | 32 | 33 | 34 | Gladder : Cube example 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |

Gladder : Cube example

43 |

Because every OpenGL demo needs a spinning cube, right?

44 | 45 | 46 | 47 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /examples/orion-nebula.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adobe-webplatform/shaderdsl/bced56b1491ed5bd0884c6286c49785e03750710/examples/orion-nebula.jpg -------------------------------------------------------------------------------- /examples/texture-noglsl.html: -------------------------------------------------------------------------------- 1 | 2 | 30 | 31 | 32 | 33 | Gladder : Texture example 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |

Gladder : Texture example

43 |

This shows how to load textures and map them to objects.

44 |

Note: due to browser security constraints, this might not work if you view it from a file:// URL.

45 | 46 | 47 | 48 | 49 | 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /gruntfile.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | module.exports = function(grunt) { 18 | grunt.initConfig({ 19 | pkg: grunt.file.readJSON('package.json'), 20 | concat: { 21 | options: { 22 | stripBanners: false, 23 | separator: ';\n', 24 | banner: 25 | '/**' 26 | + ' \n' 27 | + ' Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.\n' 28 | + ' Licensed under the Apache License, Version 2.0 (the "License");\n' 29 | + ' you may not use this file except in compliance with the License.\n' 30 | + ' You may obtain a copy of the License at\n' 31 | + '\n' 32 | + ' http://www.apache.org/licenses/LICENSE-2.0\n' 33 | + '\n' 34 | + ' Unless required by applicable law or agreed to in writing, software\n' 35 | + ' distributed under the License is distributed on an "AS IS" BASIS,\n' 36 | + ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' 37 | + ' See the License for the specific language governing permissions and\n' 38 | + ' limitations under the License.\n' 39 | + '*/\n\n\n' 40 | }, 41 | dist: { 42 | src: ['third-party/rivertrail/jslib/jit/narcissus/jsdefs.js', 43 | 'third-party/rivertrail/jslib/jit/narcissus/jslex.js', 44 | 'third-party/rivertrail/jslib/jit/narcissus/jsparse.js', 45 | 'third-party/rivertrail/jslib/jit/narcissus/jsdecomp.js', 46 | 'third-party/rivertrail/jslib/jit/compiler/definitions.js', 47 | 'third-party/rivertrail/jslib/jit/compiler/helper.js', 48 | 'src/genglsl.js', 49 | 'src/typeinference.js', 50 | 'src/infermem.js' 51 | ], 52 | dest: '<%= pkg.name %>' 53 | } 54 | }, 55 | }); 56 | grunt.loadNpmTasks('grunt-contrib-concat'); 57 | grunt.registerTask('default', ['concat']); 58 | 59 | } 60 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ShaderDSL.js", 3 | "version": "0.0.1", 4 | "description": "A Domain Specific Language for GPU shaders based on Javascript", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git@git.corp.adobe.com:kpalacz/shaderdsl.git" 12 | }, 13 | "author": "Krzysztof Palacz", 14 | "license": "Apache 2.0", 15 | "gitHead": "241a2ea3cdfa3980e45ebb1d9fa385aed6e5cb5f" 16 | } 17 | -------------------------------------------------------------------------------- /src/genglsl.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | 17 | * Copyright (c) 2011, Intel Corporation 18 | * All rights reserved. 19 | * 20 | * Redistribution and use in source and binary forms, with or without 21 | * modification, are permitted provided that the following conditions are met: 22 | * 23 | * - Redistributions of source code must retain the above copyright notice, 24 | * this list of conditions and the following disclaimer. 25 | * - Redistributions in binary form must reproduce the above copyright notice, 26 | * this list of conditions and the following disclaimer in the documentation 27 | * and/or other materials provided with the distribution. 28 | * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 39 | * THE POSSIBILITY OF SUCH DAMAGE. 40 | * 41 | */ 42 | 43 | 44 | 45 | (function() { 46 | eval(Narcissus.definitions.consts); 47 | eval(RiverTrail.definitions.consts); 48 | 49 | const tokens = RiverTrail.definitions.tokens; 50 | 51 | var varyingTypes = {}; 52 | varyingTypes.__proto__ = null; 53 | 54 | var reportError = RiverTrail.Helper.reportError; 55 | var reportBug = RiverTrail.Helper.reportBug; 56 | 57 | function parseGladderArrayDecl(decl) 58 | { 59 | var match = decl.match(/vec([2-4])\[(\d)+\]/); 60 | if (match) { 61 | return { length: parseInt(match[2]), rank: parseInt(match[1]) }; 62 | } else return null; 63 | } 64 | 65 | function isGlMatrixMatSingleton(str) 66 | { 67 | return str === "mat4" || str === "mat3" || str === "mat2"; 68 | } 69 | 70 | function isGlMatrixVecSingleton(str) 71 | { 72 | return str === "vec4" || str === "vec3" || str === "vec2"; 73 | } 74 | 75 | function isGlMatrixSingleton(str) { 76 | return isGlMatrixVecSingleton(str) || isGlMatrixMatSingleton(str); 77 | } 78 | 79 | function emitAttributeDecls(args) 80 | { 81 | var s = ""; 82 | for (var k in args.attributes) { 83 | s += "attribute " + args.attributes[k] + " " + k + ";\n"; 84 | } 85 | return s; 86 | } 87 | 88 | function emitUniformDecls(args) 89 | { 90 | var s = ""; 91 | for (var k in args.uniforms) { // FIXME sanitize 92 | var decl = args.uniforms[k]; 93 | var result = parseGladderArrayDecl(decl); 94 | if (result) { 95 | // FIXME vec? 96 | var vecdec = "uniform vec" + result.rank + " " + k + "[" + result.length + "]"; 97 | s += vecdec + ";\n"; 98 | } else { 99 | s += "uniform " + decl + " " + k + ";\n"; 100 | } 101 | } 102 | return s; 103 | } 104 | 105 | function emitVaryings() 106 | { 107 | var s = ""; 108 | for (var key in varyingTypes) { 109 | var type = varyingTypes[key]; 110 | s += "varying " + type.slangTypedName(key) + ";\n"; 111 | } 112 | return s; 113 | } 114 | 115 | 116 | function genReturn (ast) { 117 | //"use strict"; // seems to trigger a timing-dependent behavior in Chrome 118 | // go over the literal, add varyings 119 | function genArrayRet(name, ast) { 120 | var iVar = varNameGen("ret"); 121 | // FIXME we are reevaluating the expression?, make sure this is just an identifier? 122 | //if (ast.value.type != IDENTIFIER) debugger; 123 | var res = glslExpression(ast.value); 124 | // FIXME this is just one depth of array, but that's all that's supported, but what if it's an array of mat? 125 | return "for (int " + iVar + " = 0; " + iVar + " < " + ast.typeInfo.properties.shape[0] + "; " + iVar + " ++) { " + name + "[" + iVar + "] = " + RENAME(res) + "[" + iVar + "];} "; 126 | } 127 | 128 | 129 | if (ast.typeInfo.isSlangArray()) { 130 | return genArrayRet("_retVal", ast); 131 | } 132 | 133 | if (ast.value) { 134 | var s = "" ; 135 | if (ast.value.type == OBJECT_INIT) 136 | { 137 | 138 | for (var i = 0; i < ast.value.children.length; i++) 139 | { 140 | var init = ast.value.children[i]; 141 | var name = init.children[0].value; 142 | varyingTypes[name] = init.children[1].typeInfo; 143 | if (varyingTypes[name].isSlangArray()) { 144 | // element-by-element assignmment 145 | s += genArrayRet(name, init.children[1]) + "\n"; 146 | } else { 147 | //console.log("inferred type of varyingTypes[" + name + "] is " + init.children[1].typeInfo); 148 | s = s + "(" + name + "= " + glslExpression(init.children[1]) + ");\n"; // no ; because ASSIGN is an expression! 149 | } 150 | } 151 | return s; 152 | } else { 153 | // FIXME is it finished? 154 | s = s + "return " + glslExpression(ast.value); 155 | return s; 156 | } 157 | } else { 158 | return "return FIXME<" + Narcissus.decompiler.pp(ast) + ">"; 159 | } 160 | } 161 | 162 | var renameEnv = {}; 163 | renameEnv.__proto__ = null; 164 | 165 | // FIXME not unique 166 | var varNameGen = function () { 167 | var counter = 0; 168 | 169 | return function varNameGen(postfix) { 170 | return "v" + (counter++) + "_" + (postfix || "nameless"); 171 | }; 172 | }(); 173 | 174 | 175 | var RENAME = function (s) { 176 | if(renameEnv[s] === undefined) { 177 | // FIXME double check that gl_ is special? 178 | return s.indexOf("gl_") == 0 ? s : RENAME.prefix + s; 179 | } else { 180 | return renameEnv[s]; 181 | } 182 | }; 183 | RENAME.prefix = "rt_"; 184 | 185 | 186 | function stripCasts(ast) { 187 | if (ast.type === CAST) { 188 | return stripCasts(ast.children[0]); 189 | } else { 190 | return ast; 191 | } 192 | } 193 | 194 | 195 | function glslStatement(ast) { 196 | //"use strict"; // seems to trigger a timing-dependent behavior in Chrome 197 | 198 | var s = ""; 199 | if (!ast) { 200 | return ""; 201 | } 202 | if (!(ast instanceof Object)) { 203 | return ast; 204 | } 205 | 206 | if (ast.parenthesized) { 207 | s += "("; 208 | } 209 | 210 | 211 | // From SEH There is a whole lot of code in here that has not be changed over to the genOCL code but at this point 212 | // it only compiles the default loader program below. 213 | // Using combine for a combinator. 214 | // var pa = new ParallelArray([1,2,3]); 215 | // var foo = function foo(iv) { return this.get(iv); }; 216 | 217 | switch (ast.type) { 218 | 219 | case SCRIPT: 220 | s=s+"SCRIPT TBD "; 221 | // retrieve the type environment for local bindings 222 | 223 | // retrieve all the function environments for other function in case we see a call. 224 | 225 | // retrieve the symbols ?? 226 | 227 | // add all local variable declarations to environment to shadow old 228 | // ones from previous scopes 229 | // ast.varDecls.forEach(function (name) { doSomething(name); }); 230 | 231 | // add all locally declared functions to the environment 232 | // strictly speaking they are not variable bindings yet they can shadow variables and be shadowed 233 | // by variables so we disallow these 234 | // ast.funDecls.forEach(function (f) { doSomething(f)}); 235 | 236 | // fallthrough to deal with the implicit SCRIPT block. 237 | case BLOCK: 238 | ast.children.forEach(function (ast) { glslStatement(ast); }); 239 | break; 240 | 241 | // 242 | // statements 243 | // 244 | case FUNCTION: 245 | // this is not an applied occurence but the declaration, so we do not do anything here 246 | // functions are picked up from the body node earlier on 247 | 248 | break; 249 | case RETURN: 250 | // FIXME check compatibility of all return values? in typeinferece? 251 | s += genReturn(ast); 252 | break; 253 | case FOR: 254 | var setupExpr = glslExpression(ast.setup); 255 | var loopIndexType = ast.setup.children[0].typeInfo.OpenCLType; 256 | var constInit = ast.setup.children[0].initializer; // FIXME check that it's a literal 257 | // if (constInit.typeInfo instanceof TLiteral) ... 258 | var existingName = ast.setup.children[0].value; 259 | var newName = varNameGen("loop_"); // FIXME non-clashing name? 260 | s += "for ( " + loopIndexType + " " + newName + " = " + glslExpression(constInit) + "; "; 261 | var saved = renameEnv; 262 | try { 263 | renameEnv = {}; 264 | renameEnv[existingName] = newName; 265 | renameEnv.__proto__ = saved; 266 | 267 | s += glslExpression(ast.condition) + ";" 268 | + glslExpression(ast.update) + ") "; 269 | s += glslStatements(ast.body, RENAME.prefix + existingName + " = " + newName + ";"); // write back from new var to old var 270 | } finally { 271 | renameEnv = saved; 272 | } 273 | break; 274 | case WHILE: 275 | s += "while ( "+glslExpression(ast.condition)+") "; 276 | s += glslStatements(ast.body); 277 | break; 278 | case DO: 279 | s += "do " + glslStatements(ast.body)+" while ("+glslExpression(ast.condition)+");"; 280 | break; 281 | case IF: 282 | s += "if ("+glslExpression(ast.condition)+") {"; 283 | s += glslStatements(ast.thenPart) + "} "; 284 | if (ast.elsePart) { 285 | s += "else {"+glslStatements(ast.elsePart) + "}"; 286 | } 287 | break; 288 | case SEMICOLON: 289 | if (ast.expression) { 290 | s += glslStatement(ast.expression)+";"; 291 | } 292 | break; 293 | case CONST: 294 | break; // const initialziers are handled at declaration time 295 | case VAR: 296 | s = s + ast.children.reduce(function (prev, ast) { 297 | if (ast.initializer) { // CONSTs are handled at initialization 298 | if (ast.initializer.typeInfo.isPseudoType()) { 299 | // we're propagating a pseudotype, codegen will go directly do the appropriate individual vars 300 | // no code 301 | return prev; 302 | } 303 | 304 | switch (ast.type) { 305 | case IDENTIFIER: 306 | // simple case of a = expr 307 | // initialisation assignments are separated by commata (which is valid C) to make them work in for statement initialisers. 308 | if (prev !== "") { 309 | prev += ", "; 310 | } 311 | ast.initializer.assignedIdentifier = ast.value; 312 | if (ast.initializer.type == ARRAY_INIT) { 313 | prev += glslExpression(ast.initializer); 314 | } else { 315 | if (ast.initializer.typeInfo.isSlangArray()) { 316 | // nope 317 | } else { 318 | prev += RENAME(ast.value) + " = "; 319 | } 320 | prev += glslExpression(ast.initializer); 321 | } 322 | 323 | break; 324 | default: 325 | reportBug("unhandled lhs in var/const"); 326 | break; 327 | } 328 | } 329 | return prev; 330 | }, ""); 331 | break; 332 | case ASSIGN: 333 | // children[0] is the left hand side, children[1] is the right hand side. 334 | // both can be expressions. 335 | if (ast.children[1].typeInfo.isPseudoType()) { 336 | // we're propagating a pseudotype, codegen will go directly do the appropriate individual vars 337 | // no code 338 | 339 | break; 340 | } 341 | switch (ast.children[0].type) { 342 | case IDENTIFIER: 343 | // simple case of a = expr 344 | if (ast.allocatedMem) { 345 | //throw new Error("a memcopy would be required to compile this code."); 346 | var s_tmp = ""; var s_decl = ""; 347 | var sourceShape = ast.children[1].typeInfo.getOpenCLShape(); 348 | var sourceType = ast.children[1].typeInfo.OpenCLType; 349 | var maxDepth = sourceShape.length; 350 | var sourceAddressSpace = (ast.children[1].typeInfo.getOpenCLAddressSpace() == "__global" ? "__global": ""); 351 | if(!(ast.children[1].typeInfo.isScalarType()) && maxDepth >= 1) { 352 | verboseDebug && console.log("Doing copy assignment to value ", ast.children[0].value, " from shape ", sourceShape); 353 | var source_tmp_name = "tmp_" + ast.memBuffers.list[0]; 354 | s_decl += "/* Copying Assignment */ " + sourceAddressSpace + " " + sourceType + " " + source_tmp_name + " = " + glslExpression(ast.children[1]) + ";" ; 355 | s_tmp += "("; 356 | var post_parens = ""; 357 | var redu = 1; var rhs = ""; var lhs = ""; post_parens = ")"; 358 | for(var i = 0 ; i < maxDepth; i++) { 359 | for(var j = 0; j < sourceShape[i]*redu; j++) { 360 | if(i===maxDepth-1) { 361 | lhs = "(" + getPointerCast(i, maxDepth, ast.typeInfo.OpenCLType) + 362 | ast.memBuffers.list[i] + ")" + "[" + j + "]"; 363 | var n = j; var idx = ""; 364 | for(var k = maxDepth-1; k >=0; k--) { 365 | idx = "[" + n % sourceShape[k] +"]" + idx; 366 | n = Math.floor(n/sourceShape[k]); 367 | } 368 | rhs = source_tmp_name + idx; 369 | } 370 | else { 371 | lhs = "(" + getPointerCast(i, maxDepth, ast.typeInfo.OpenCLType) + 372 | ast.memBuffers.list[i] + ")" + "[" + j + "]"; 373 | rhs = "&((" + getPointerCast(i+1, maxDepth, ast.typeInfo.OpenCLType) 374 | + ast.memBuffers.list[i+1]+ ")" + "[" + j*sourceShape[i+1] + "]" + ")"; 375 | } 376 | s_tmp += lhs + " = " + rhs + " ,"; 377 | } 378 | redu = redu*sourceShape[i]; 379 | } 380 | s_tmp += " (" + sourceType + ")" + ast.memBuffers.list[0] + ")"; 381 | s += s_decl + "(" + ast.children[0].value + (ast.assignOp ? tokens[ast.assignOp] : "") + "= " + s_tmp + ")"; 382 | } 383 | else if(ast.typeInfo.isScalarType()) { 384 | // Do scalars ever have memory allocated to 385 | // them ? 386 | throw new Error("Compiler bug: Memory allocated for scalar copy"); 387 | } 388 | 389 | } else if (ast.children[1].type == ARRAY_INIT) { 390 | ast.children[1].assignedIdentifier = ast.children[0].value; 391 | // do not assign 392 | s = s + "(" + glslExpression(ast.children[1]) + ")"; // no ; because ASSIGN is an expression! 393 | } else if (ast.children[1].typeInfo.isSlangArray()) { 394 | // RHS is an array? 395 | console.log('skip assignment for', Narcissus.decompiler.pp(ast.children[1])); 396 | ast.children[1].assignedIdentifier = ast.children[0].value; 397 | // don't generate the assignment, since call returns void 398 | s = s + "(" + glslExpression(ast.children[1]) + ")"; // no ; because ASSIGN is an expression! 399 | } else { 400 | s = s + "(" + RENAME(ast.children[0].value) + (ast.assignOp ? tokens[ast.assignOp] : "") + "= " + glslExpression(ast.children[1]) + ")"; // no ; because ASSIGN is an expression! 401 | } 402 | break; 403 | case INDEX: 404 | // array update [iv] = expr 405 | // make sure that is in the __private address space. We catch it this late just for 406 | // prototyping convenience. Could go anywhere after TI. 407 | // (findSelectionRoot(ast.children[0]).typeInfo.getOpenCLAddressSpace() !== "__global") || reportError("global arrays are immutable", ast); 408 | 409 | s = s + "((" + glslExpression(ast.children[0]) + ")" + (ast.assignOp ? tokens[ast.assignOp] : "") + "= " + glslExpression(ast.children[1]) + ")"; 410 | break; 411 | case DOT: 412 | // object property update. 413 | // a.b = c; 414 | // make sure that address spaces are right! 415 | s = s + "((" + glslExpression(ast.children[0].children[0]) + "->" + ast.children[0].children[1].value + ")" + (ast.assignOp ? tokens[ast.assignOp] : "") + "= " + glslExpression(ast.children[1]) + ")" ; 416 | break; 417 | default: 418 | reportBug("unhandled lhs in assignment"); 419 | break; 420 | } 421 | // leave the last type in the accu. Assignments can be expressions :) 422 | break; 423 | 424 | // 425 | // expressions 426 | // 427 | case COMMA: 428 | for (var i=0; i0) { 430 | s += ", "; 431 | } 432 | s += glslExpression(ast.children[i]); 433 | } 434 | break; 435 | case HOOK: 436 | // the hook (?) is badly designed. The first child is the condition, second child 437 | // the then expression, third child the else expression 438 | s += "("+glslExpression(ast.children[0])+"?" 439 | +glslExpression(ast.children[1])+":" 440 | +glslExpression(ast.children[2])+")"; 441 | break; 442 | 443 | // binary operations on all literals 444 | case STRICT_EQ: 445 | case STRICT_NE: 446 | // we map these to the no strict case for now 447 | ast.value = ast.value.substring(0,2); 448 | // fallthrough; 449 | 450 | case PLUS: 451 | // we do not support strings yet, so this case is the same as numbers 452 | // fallthrough 453 | 454 | // binary operators on numbers (incl bool) 455 | case BITWISE_OR: 456 | case BITWISE_XOR: 457 | case BITWISE_AND: 458 | case EQ: 459 | case NE: 460 | case LT: 461 | case LE: 462 | case GE: 463 | case GT: 464 | case LSH: 465 | case RSH: 466 | case URSH: 467 | case MINUS: 468 | case MUL: 469 | case DIV: 470 | s = s + "("+glslExpression(ast.children[0]) + ast.value + glslExpression(ast.children[1]) + ")"; 471 | break; 472 | case MOD: 473 | s = s + "(" + "fmod(" + "(" + glslExpression(ast.children[0]) + ")" + ", " + "(" + glslExpression(ast.children[1]) + ")" + ")" + ")"; 474 | break; 475 | 476 | // binary operators on bool 477 | case OR: 478 | s += "("+glslExpression(ast.children[0])+" || "+glslExpression(ast.children[1])+")"; 479 | break; 480 | case AND: 481 | s += "("+glslExpression(ast.children[0])+" && "+glslExpression(ast.children[1])+")"; 482 | break; 483 | 484 | // unary functions on all literals 485 | case NOT: 486 | case BITWISE_NOT: 487 | case UNARY_PLUS: 488 | case UNARY_MINUS: 489 | s = s + ast.value + glslExpression(ast.children[0]); 490 | break; 491 | // unary functions on numbers (incl bool) 492 | case INCREMENT: 493 | case DECREMENT: 494 | var incArg = stripCasts(ast.children[0]); 495 | var incType = ast.children[0].typeInfo.OpenCLType; 496 | if (ast.postfix) { 497 | s = s + glslExpression(ast.children[0]) + ast.value; 498 | } else { 499 | s = s + ast.value + glslExpression(ast.children[0]); 500 | } 501 | break; 502 | 503 | // literals 504 | case IDENTIFIER: 505 | s = s + RENAME(ast.value); 506 | break; 507 | case THIS: 508 | s = s + " tempThis "; // This should come from the boilerplate but that cannot be passed around easily 509 | break; 510 | case DOT: 511 | if (ast.children[0].typeInfo.isObjectType("Attributes")) { // KP: FIXME check?? 512 | s = ast.children[1].value; 513 | } else if (ast.children[0].typeInfo.isObjectType("Uniforms")) { // KP: FIXME check?? 514 | s = ast.children[1].value; 515 | } else if (ast.children[0].typeInfo.isObjectType("Varyings")) { // KP: FIXME check?? only valid for fragment shaders? 516 | s = ast.children[1].value; 517 | } else if (ast.children[0].typeInfo.isObjectType("InlineObject")) { 518 | // TypeInference would have checked if this property selection 519 | // is valid 520 | s = s + " " + RENAME(ast.children[0].value) + "->" + ast.children[1].value; 521 | } else if ((ast.children[0].typeInfo.isArrayishType()) && 522 | (ast.children[1].value === "length")) { 523 | // length property -> substitute the value 524 | s = s + ast.children[0].typeInfo.getOpenCLShape()[0]; 525 | } else { 526 | reportBug("unsupported property selection in back end", ast); 527 | } 528 | break; 529 | 530 | case CAST: 531 | case FLATTEN: 532 | case NUMBER: 533 | s += glslExpression(ast); 534 | break; 535 | case TRUE: 536 | s += "true"; 537 | break; 538 | case FALSE: 539 | s += "false"; 540 | break; 541 | 542 | // array operations 543 | case INDEX: 544 | var lhs = ast.children[0].typeInfo; 545 | if (lhs.isObjectType("ParallelArray")) { 546 | if (lhs.properties.shape.length == 1) { 547 | var rank = lhs.properties.shape[0]; 548 | var index = Number(ast.children[1].value); 549 | var succ = false; 550 | switch (index) { 551 | case 0: 552 | if (rank >= 0) { 553 | s += glslExpression(ast.children[0]) + ".x"; 554 | succ = true; 555 | } 556 | break; 557 | case 1: 558 | if (rank >= 1) { 559 | s += glslExpression(ast.children[0]) + ".y"; 560 | succ = true; 561 | } 562 | break; 563 | case 2: 564 | if (rank >= 2) { 565 | s += glslExpression(ast.children[0]) + ".z"; 566 | succ = true; 567 | } 568 | break; 569 | case 3: 570 | if (rank >= 3) { 571 | s += glslExpression(ast.children[0]) + ".w"; 572 | succ = true; 573 | } 574 | break; 575 | } 576 | if (!succ) { 577 | s += "FIXME(failed to index ParallelArray with shape " + lhs.properties.shape + " on " + ast.children[1].value + ")"; 578 | } 579 | 580 | } else { 581 | s += "FIXME(some kind of ParallelArray)"; 582 | } 583 | 584 | } else { 585 | s += compileSelectionOperation(ast, ast.children[0], ast.children[1], true); 586 | } 587 | break; 588 | 589 | case ARRAY_INIT: 590 | if (!ast.assignedIdentifier) 591 | reportError("array literals supported only in variable initializers"); 592 | s = s + "("; 593 | for (var i=0;i0) { 595 | s += ", "; 596 | } 597 | s += /*ast.allocatedMem*/ RENAME(ast.assignedIdentifier) + "[" + i + "] = " + glslExpression(ast.children[i]); 598 | } 599 | /* 600 | if (i>0) { 601 | s += ", "; 602 | } 603 | s = s + ast.allocatedMem + ")"; 604 | */ 605 | s = s + ")"; 606 | //} 607 | break; 608 | 609 | // function application 610 | case CALL: 611 | s += glslCallExpression(ast); 612 | break; 613 | // Below is the typ 614 | 615 | // argument lists 616 | case LIST: 617 | for (var i=0; i0) { 619 | s += ", "; 620 | } 621 | s += glslExpression(ast.children[i]); 622 | } 623 | break; 624 | 625 | // 626 | // unsupported stuff here 627 | // 628 | case GETTER: 629 | case SETTER: 630 | reportError("setters/getters not yet implemented", ast); 631 | break; 632 | case TRY: 633 | case THROW: 634 | reportError("try/throw/catch/finally not yet implemented", ast); 635 | break; 636 | case BREAK: 637 | //s += " break; "; 638 | //break; 639 | case CONTINUE: 640 | //s += " continue; "; 641 | //break; 642 | case LABEL: 643 | reportError("break/continure/labels not yet implemented", ast); 644 | break; 645 | case YIELD: 646 | case GENERATOR: 647 | reportError("generators/yield not yet implemented", ast); 648 | break; 649 | case FOR_IN: 650 | reportError("for .. in loops not yet implemented", ast); 651 | break; 652 | case ARRAY_COMP: 653 | case COMP_TAIL: 654 | reportError("array comprehensions not yet implemented", ast); 655 | break; 656 | case NEW: 657 | reportError("general object construction not yet implemented", ast); 658 | break; 659 | case NEW_WITH_ARGS: 660 | case OBJECT_INIT: 661 | reportError("general object construction not yet implemented", ast); 662 | break; 663 | case WITH: 664 | reportError("general objects not yet implemented", ast); 665 | break; 666 | case LET: 667 | case LET_BLOCK: 668 | reportError("let not yet implemented", ast); 669 | break; 670 | case SWITCH: 671 | reportError("switch not yet implemented", ast); 672 | break; 673 | 674 | // unsupported binary functions 675 | case INSTANCEOF: 676 | reportError("instanceof not yet implemented", ast); 677 | break; 678 | case EQ: 679 | case NE: 680 | reportError("non-strict equality not yet implemented", ast); 681 | break; 682 | case IN: 683 | reportError("in not yet implemented", ast); 684 | break; 685 | 686 | // unsupported literals 687 | case NULL: 688 | reportError("null not yet implemented", ast); 689 | break; 690 | case REGEXP: 691 | reportError("regular expressions not yet implemented", ast); 692 | break; 693 | case STRING: 694 | reportError("strings not yet implemented", ast); 695 | break; 696 | 697 | case TOINT32: 698 | if (ast.typeInfo.isNumberType()) { 699 | // we have a scalar number, so we just emit the 700 | // conversion code 701 | s = s + "((int)" + glslExpression(ast.children[0]) + ")"; 702 | } else { 703 | // this is some form of array or vector. We do not 704 | // have allocation of local temps, yet, so fail 705 | throw "TOINT32 applied to non scalar data structure"; 706 | } 707 | break; 708 | case DEBUGGER: // whatever this is... 709 | default: 710 | throw "unhandled node type in analysis: " + tokens[ast.type]; 711 | } 712 | if (ast.parenthesized) { 713 | s += ")"; 714 | } 715 | 716 | return s; 717 | 718 | } 719 | 720 | var checkBounds = false; // ?? 721 | var checkall = false; 722 | 723 | var newError = function newError(msg) { 724 | return msg; 725 | /* 726 | if (verboseErrors) { 727 | errorMsgs[errorMsgs.length] = "AT " + (calledScope.inCalledScope() || "") + ": " + msg; 728 | } 729 | return errorMsgs.length; // this is one after the index on purpose! 730 | */ 731 | }; 732 | 733 | 734 | // Creates a potentially checked array index. 735 | // If the index is statically known to be correct, this 736 | // just returns the index itself (expr). 737 | // Otherwise an expression is created that checks whether 738 | // the index is in bounds and, if the index turns out to be 739 | // out of bounds, returns 0. If the index is in bounds, 740 | // the value of expr is returned. 741 | // 742 | // The emmited code relies on a global variable 743 | // int _sel_idx_tmp 744 | // to store the intermediate result of evaluation expr. 745 | // This variable should be declared at the top-level of the 746 | // function. 747 | // 748 | // As a side effect, the global variable _FAIL is set to > 0 749 | // if a bounds check failed. 750 | function wrapIntoCheck(range, bound, expr, ast) { 751 | var postfix = ""; 752 | var result = ""; 753 | var dynCheck = false; 754 | 755 | if (bound === 0) { 756 | // we have an empty array => you cannot select from those 757 | // (yeah, yeah, I know, a real corner case :=D) 758 | throw new Error("selection from empty array encountered!"); 759 | } 760 | 761 | if (checkBounds && 762 | (checkall || 763 | (range === undefined) || 764 | (range.lb === undefined) || 765 | (range.lb < 0))) { 766 | // emit lower bound check 767 | result += "(_sel_idx_tmp < 0 ? (_FAIL ? 0 : (_FAIL = " + newError("index " + expr + " smaller than zero in " + RiverTrail.Helper.wrappedPP(ast)) + ", 0)) : "; 768 | postfix = ")" + postfix; 769 | dynCheck = true; 770 | } 771 | 772 | if (checkBounds && 773 | (checkall || 774 | (range === undefined) || 775 | (range.ub === undefined) || 776 | (range.ub >= bound))) { 777 | // emit upper bound check 778 | result += "(_sel_idx_tmp >= " + bound + " ? (_FAIL ? 0: (_FAIL = " + newError("index " + expr + " greater than upper bound " + bound + " in " + RiverTrail.Helper.wrappedPP(ast)) + ", 0)) : "; 779 | postfix = ")" + postfix; 780 | dynCheck = true; 781 | } 782 | 783 | if (dynCheck) { 784 | result = "int(_sel_idx_tmp = " + expr + ", " + result + "_sel_idx_tmp" + postfix + ")"; 785 | } else { 786 | result = "int(" + expr + ")"; 787 | } 788 | 789 | return result; 790 | } 791 | 792 | 793 | // 794 | // Given we have a source and an arrayOfIndices. If the result is 795 | // a primitive type we return it. If it is an array then we generate a 796 | // pointer to the start of that array. 797 | // 798 | 799 | var compileSelectionOperation = function (ast, source, arrayOfIndices) { 800 | "use strict"; 801 | 802 | var s = ""; 803 | var i; 804 | var elemSize; 805 | var stride; 806 | var indexLen; 807 | var dynamicSel; 808 | var rangeInfo; 809 | // If arrayOfIndices has an inferredType of an array (dimSize > 0) then it is get([x, y...]); 810 | // If that is the case then elemRank will be the sourceRank - the length of the argument. 811 | var sourceType = source.typeInfo; 812 | var sourceShape = sourceType.getOpenCLShape(); 813 | var sourceRank = sourceShape.length; 814 | var elemRank = ast.typeInfo.getOpenCLShape().length; 815 | if (sourceType.isObjectType("JSArray")) { 816 | // special treatment for JavaScript encoded arrays 817 | rangeInfo = arrayOfIndices.rangeInfo; 818 | 819 | s = s + glslExpression(source) ; 820 | s = s + "[" + wrapIntoCheck(rangeInfo, sourceShape[0], glslExpression(arrayOfIndices), ast) + "]"; 821 | 822 | 823 | /* 824 | if (elemRank === 0) { 825 | // scalar case 826 | s = s + "__JS_array_sel_S(" + glslExpression(source) + ", " + wrapIntoCheck(rangeInfo, sourceShape[0], glslExpression(arrayOfIndices), ast) + ")"; 827 | } else { 828 | s = s + "__JS_array_sel_A(" + glslExpression(source) + ", " + wrapIntoCheck(rangeInfo, sourceShape[0], glslExpression(arrayOfIndices), ast) + ", " + sourceShape[1] + ", &_FAIL)"; 829 | } 830 | */ 831 | } else { 832 | // C style arrays 833 | var isParallelArray = sourceType.isObjectType("ParallelArray"); 834 | var isGlobal = (sourceType.getOpenCLAddressSpace() === "__global"); 835 | if (elemRank !== 0) { 836 | if(isParallelArray || isGlobal) { 837 | // The result is a pointer to a sub dimension. 838 | s = s + "( &"; 839 | } 840 | else { 841 | s = s + "("; 842 | } 843 | } 844 | elemSize = ast.typeInfo.getOpenCLShape().reduce( function (p,n) { return p*n;}, 1); 845 | if(isParallelArray || isGlobal) { 846 | s = s + " ( " + glslExpression(source) + "[0 "; 847 | } 848 | else { 849 | s = s + glslExpression(source) ; 850 | } 851 | 852 | stride = elemSize; 853 | 854 | if (arrayOfIndices.type !== LIST) { 855 | // we have a single scalar index from an INDEX op 856 | rangeInfo = arrayOfIndices.rangeInfo; 857 | if(isParallelArray || isGlobal) { 858 | s = s + " + " + stride + " * ("+wrapIntoCheck(rangeInfo, sourceShape[0], glslExpression(arrayOfIndices), ast) + ")"; 859 | } 860 | else { 861 | s = s + "[" + wrapIntoCheck(rangeInfo, sourceShape[0], glslExpression(arrayOfIndices), ast) + "]"; 862 | } 863 | } else { 864 | // this is a get 865 | if (arrayOfIndices.children[0] && (arrayOfIndices.children[0].type === ARRAY_INIT)) { 866 | // We might have get([0,0]); instead of get(0,0); 867 | arrayOfIndices = arrayOfIndices.children[0]; 868 | } 869 | rangeInfo = arrayOfIndices.rangeInfo; 870 | // the first argument could be an index vector, in which case we have to produce dynamic 871 | // selection code 872 | dynamicSel = arrayOfIndices.children[0].typeInfo.getOpenCLShape().length !== 0; 873 | for (i = sourceRank - elemRank - 1; i >= 0; i--) { // Ususally only 2 or 3 dimensions so ignore complexity 874 | s = s + " + " + stride + " * (" 875 | if (dynamicSel) { 876 | s = s + wrapIntoCheck((rangeInfo ? rangeInfo.get(0).get(i) : undefined), sourceShape[i], glslExpression(arrayOfIndices.children[0], ast) + "[" + i + "]") + ")"; 877 | } else { 878 | s = s + wrapIntoCheck((rangeInfo ? rangeInfo.get(i) : undefined), sourceShape[i], glslExpression(arrayOfIndices.children[i]), ast) + ")"; 879 | } 880 | stride = stride * sourceType.getOpenCLShape()[i]; 881 | } 882 | } 883 | if(isParallelArray || isGlobal) { 884 | s = s + "])"; 885 | } 886 | 887 | if (elemRank !== 0) { 888 | // The result is a pointer to a sub dimension. 889 | s = s + " )"; 890 | } 891 | } 892 | 893 | return s; 894 | } 895 | 896 | 897 | function emitDeclarations(env, renamer) { 898 | s = ""; 899 | for (var name in env.bindings) { 900 | var type = env.bindings[name].type; 901 | // only declare variables that are actually used (and thus have a type) 902 | if (type && !type.isPseudoType()) { 903 | s = s + " "; 904 | if (env.bindings[name].constinit) { 905 | s += "const "; // FIXME restrict const expression initializers to allow reordering? currently they may use variables whose 906 | // value computation might have been reordered, due to the fact that const initializers are emitted in GLSL at 907 | // declaration location, while regular variables are not. 908 | s += type.slangTypedName((renamer ? renamer(name) : name)) 909 | + " = " + glslExpression(env.bindings[name].constinit) + ";\n"; 910 | } else { 911 | s += type.slangTypedName((renamer ? renamer(name) : name)) + ";\n"; 912 | } 913 | } 914 | } 915 | return s; 916 | }; 917 | 918 | 919 | function glslStatements(statements, extra) { 920 | //"use strict"; // seems to trigger a timing-dependent behavior in Chrome 921 | var i; 922 | var x; 923 | var s = ""; 924 | if ((statements.type === BLOCK) || (statements.type === SCRIPT)) { 925 | if (statements.symbols) { 926 | s = s + emitDeclarations(statements.symbols, RENAME); 927 | } 928 | if (statements.memVars) { 929 | s = s + statements.memVars.declare(); 930 | } 931 | 932 | for (i=0; i" + idx + "=" + 1148 | "(" + propType.OpenCLType + ")" + ast.memBuffers[idx][0] + ","; 1149 | */ 1150 | } 1151 | //s = s + RENAME(ast.children[0].dispatch) + "(" (actuals !== "" ? ", " : "") + actuals; 1152 | // s += ", (" + ast.typeInfo.OpenCLType + ")" + rootBuffer + "))"; 1153 | } 1154 | else if(!(ast.typeInfo.isScalarType()) && ast.typeInfo.getOpenCLShape().length > 1) { 1155 | // Create structure if this call is going to return a nested 1156 | // array 1157 | console.log(ast.children[0], ast.children[1]); 1158 | s += "("; // + "/*(initNestedArrayStructure(ast, false)*/)" 1159 | post_parens = ")"; 1160 | // NOTE: use renamed dispatch name here! 1161 | s = s + RENAME(ast.children[0].dispatch) + "(" + actuals; 1162 | 1163 | s = s + ", " + RENAME(ast.assignedIdentifier); 1164 | //s = s + ", (" + ast.typeInfo.OpenCLType + ") " + ast.allocatedMem; 1165 | s = s + ")"; 1166 | s = s + post_parens; // Close the comma list. 1167 | } 1168 | else { 1169 | if (ast.children[0].value === "Number") { 1170 | // the only builtin function handled. 1171 | s = s + "float(" + actuals; 1172 | // FIXME make sure redefining Number (and other intrinsics) is a no-no 1173 | } else { 1174 | // NOTE: use renamed dispatch name here! 1175 | s = s + RENAME(ast.children[0].dispatch) + "(" + actuals; 1176 | } 1177 | /* 1178 | if (!(ast.typeInfo.isScalarType())) { 1179 | s = s + ", (" + ast.typeInfo.OpenCLType + ") " + ast.allocatedMem; 1180 | } 1181 | */ 1182 | s = s + ")"; 1183 | s = s + post_parens; 1184 | } 1185 | } 1186 | return s; 1187 | }; 1188 | 1189 | 1190 | function glslExpression(ast) { 1191 | //"use strict"; // seems to trigger a timing-dependent behavior in Chrome 1192 | var s = " "; 1193 | var i, ii; 1194 | var arrayOfIndices; 1195 | var stride; 1196 | var elemSize; 1197 | var elemRank; 1198 | var sourceRank; 1199 | var indexLen; 1200 | if (ast.type === CAST) { // deals with adding things like (float *)mumble) to generated code. 1201 | if (!ast.typeInfo.isScalarType()) { 1202 | reportError("non-scalar cast encountered", ast); 1203 | } 1204 | s = "((" + ast.typeInfo.OpenCLType + ")" 1205 | + glslExpression(ast.children[0]) + ")"; 1206 | //} 1207 | } else if (ast.type === FLATTEN) { 1208 | if (ast.typeInfo.getOpenCLShape().length > 1) { 1209 | reportError("flattening of nested arrays not yet implemented.", ast.children[0]); 1210 | } 1211 | s = s + glslExpression(ast.children[0]); 1212 | } else if (ast.type === NUMBER) { 1213 | s = s + toCNumber(ast.value, ast.typeInfo); 1214 | 1215 | } else if (ast.type === THIS) { 1216 | s = s + " tempThis "; // SAH: this should come from the boilerplate but that cannot be passed around easily 1217 | 1218 | } else if (ast.type === CALL) { 1219 | return glslCallExpression(ast); 1220 | // Deal with some intrinsics if found, otherwise just make the call. 1221 | } else { 1222 | // Everything else can be dealt with according to the more straight forward translation. 1223 | s = glslStatement(ast); 1224 | } 1225 | return s; 1226 | } 1227 | 1228 | 1229 | // If you are working inside the top level of actual kernel function then scope is empty. 1230 | // If you generating code for a called function then this will be true. 1231 | var calledScope = function () { 1232 | "use strict"; 1233 | // state is private. 1234 | var state = false; 1235 | var enter = function enter(name) { 1236 | state = name || true; 1237 | }; 1238 | var exit = function exit(previous) { 1239 | state = previous; 1240 | } 1241 | var inCalledScope = function () { 1242 | return state; 1243 | }; 1244 | return {"enter": enter, "exit": exit, "inCalledScope": inCalledScope}; 1245 | } (); 1246 | 1247 | 1248 | function genFormalParams(formalsAst, construct) { 1249 | "use strict"; 1250 | if (calledScope.inCalledScope()) { 1251 | return genNonKernelFormalParams(formalsAst); 1252 | } else { 1253 | return genKernelFormalParams(formalsAst, construct); 1254 | } 1255 | } 1256 | // This is for called functions, not the top level kernel function. 1257 | function genNonKernelFormalParams(formalsAst) { 1258 | "use strict"; 1259 | var i; 1260 | var s = ""; 1261 | var formalsNames = formalsAst.params; 1262 | var formalsTypes = formalsAst.typeInfo.parameters; 1263 | 1264 | for (i = 0; i < formalsTypes.length; i++) { 1265 | if (s !== "" ) { 1266 | s = s + ", "; // leave out the , before the first parameter 1267 | } 1268 | 1269 | /* 1270 | if (formalsTypes[i].isArrayishType()) { 1271 | // array argument, so needs address space qualifier 1272 | s = s + formalsTypes[i].getOpenCLAddressSpace() + " "; 1273 | } 1274 | */ 1275 | s = s + formalsTypes[i].slangTypedName(RENAME(formalsNames[i])); 1276 | } 1277 | return s; 1278 | }; 1279 | 1280 | function genKernelFormalParams(formalsAst, construct) { 1281 | "use strict"; 1282 | var i; 1283 | var s = ""; 1284 | var formalsNames = formalsAst.params; 1285 | var formalsTypes = formalsAst.typeInfo.parameters; 1286 | if (construct === "combine") { 1287 | // Skip the extra type for this and ignore the first argument. 1288 | // the extras do not include |this| and the first formal since that is the index generated in the body. 1289 | 1290 | formalsNames = formalsNames.slice(1); // This skips the index argument 1291 | formalsTypes = formalsTypes.slice(2); // This skips this and the index argument 1292 | } else if ((construct === "comprehension") || (construct === "comprehensionScalar")) { 1293 | // ignore the first argument, the index 1294 | formalsTypes = formalsTypes.slice(1); 1295 | formalsNames = formalsNames.slice(1); 1296 | } else if (construct === "map") { 1297 | // Skip the extra type for this 1298 | // Skip the extra type for this and ignore the first argument, which is the value and is set 1299 | // explicitly based on this and the id. 1300 | // the extras do not include |this| and the first formal since that is the value generated in the body. 1301 | formalsTypes = formalsTypes.slice(2); // Skip this and the val argument. 1302 | formalsNames = formalsNames.slice(1); // Skip the val argument 1303 | } 1304 | 1305 | for (i = 0; i < formalsTypes.length; i++) { 1306 | if (s !== "" ) { 1307 | s = s + ", "; // leave out the , before the first parameter 1308 | } 1309 | 1310 | s = s + formalsTypes[i].slangTypedName(RENAME(formalsNames[i])); 1311 | } 1312 | return s; 1313 | } 1314 | 1315 | // 1316 | // This generates code for a function that is presmable called from the kernel function. 1317 | // 1318 | // 1319 | function genCalledFunctionHeader(ast) { 1320 | "use strict"; 1321 | var s = ""; 1322 | var formals = ""; 1323 | 1324 | if (ast.value != "function") { 1325 | throw "expecting function found " + ast.value; 1326 | } 1327 | 1328 | var previousCalledScope = calledScope.inCalledScope(); 1329 | calledScope.enter(ast.name); 1330 | var arrayReturn = ast.typeInfo.result.isSlangArray(); 1331 | // Need code here to deal with array values being returned. 1332 | // NOTE: use dispatched function name here 1333 | 1334 | s = s + " " + (arrayReturn ? "void" : ast.typeInfo.result.OpenCLType) + " " + RENAME(ast.dispatch); 1335 | s = s + "("; // start param list. 1336 | // add extra parameter for failure propagation 1337 | formals = genFormalParams(ast, "ignore"); 1338 | if (formals !== "") { 1339 | s = s + formals; 1340 | } // else there are no formals to output. 1341 | 1342 | var returnType = ast.typeInfo.result.OpenCLType; 1343 | 1344 | if (arrayReturn) { // This assumes it is an array. 1345 | // If the return type of the result is an Array then a pointer to it is passed in. 1346 | //s = s + ", " + returnType + " retVal"; 1347 | s += ", " + ast.typeInfo.result.slangTypedName("_retVal"); // FIXME renaming 1348 | } 1349 | 1350 | s = s + " )"; 1351 | 1352 | calledScope.exit(previousCalledScope); 1353 | 1354 | return s; 1355 | } 1356 | 1357 | function genCalledFunction(ast) { 1358 | "use strict"; 1359 | var s = ""; 1360 | 1361 | var previousCalledScope = calledScope.inCalledScope(); 1362 | calledScope.enter(ast.name); 1363 | if (ast.value != "function") { 1364 | throw "expecting function found " + ast.value; 1365 | } 1366 | var returnType = ast.typeInfo.result.OpenCLType; 1367 | s = s + genCalledFunctionHeader(ast); 1368 | //s = s + " { ";// function body 1369 | //s = s + returnType + " " + "tempResult" + ";"; // tmp var for parking result 1370 | s = s + glslStatements(ast.body); // Generate the statements; 1371 | // s = s + " }"; 1372 | s = s + "\n"; 1373 | 1374 | calledScope.exit(previousCalledScope); 1375 | return s; 1376 | } 1377 | 1378 | function declareLocalFunctions(decls) { 1379 | var s = ""; 1380 | 1381 | if (!decls) 1382 | return s; 1383 | 1384 | for (var cnt = 0; cnt < decls.length; cnt ++) { 1385 | if (decls[cnt].isBuiltin) { 1386 | continue; 1387 | } 1388 | s = s + declareLocalFunctions(decls[cnt].body.funDecls); 1389 | s = s + genCalledFunctionHeader(decls[cnt]) + ";\n"; 1390 | } 1391 | 1392 | return s; 1393 | } 1394 | 1395 | function generateLocalFunctions(decls) { 1396 | var s = ""; 1397 | 1398 | if (!decls) 1399 | return s; 1400 | 1401 | for (var cnt = 0; cnt < decls.length; cnt ++) { 1402 | if (decls[cnt].isBuiltin) { 1403 | continue; 1404 | } 1405 | s = s + generateLocalFunctions(decls[cnt].body.funDecls); 1406 | s = s + genCalledFunction(decls[cnt]); 1407 | } 1408 | 1409 | return s; 1410 | } 1411 | 1412 | function compileVertexShader(args) { 1413 | //var src = document.getElementById(func.glslElement).innerHTML; 1414 | //console.log('/*orig*/ ' + src); 1415 | var comp = "precision mediump float;\n"; 1416 | var ast = RiverTrail.Helper.parseFunction(args.vertexShader); 1417 | var paSource = undefined; 1418 | var construct = undefined; 1419 | var rank = undefined; 1420 | var lowPrecision = undefined; 1421 | 1422 | ShaderDSL.Typeinference.analyze(ast, paSource, construct, args.attributes, args.uniforms, undefined, lowPrecision); 1423 | ShaderDSL.InferMem.infer(ast); 1424 | 1425 | var body = glslStatements(ast.body); 1426 | 1427 | comp += emitUniformDecls(args); 1428 | comp += emitAttributeDecls(args); 1429 | comp += emitVaryings(); 1430 | 1431 | comp += declareLocalFunctions(ast.body.funDecls); 1432 | comp += generateLocalFunctions(ast.body.funDecls); 1433 | 1434 | comp += "void main()\n" + body; 1435 | console.log('/*xltd*/ ' + comp); 1436 | return comp; 1437 | } 1438 | 1439 | function compileFragmentShader(args) { 1440 | //var src = document.getElementById(func.glslElement).innerHTML; 1441 | //console.log('/*orig*/ ' + src); 1442 | var ast = RiverTrail.Helper.parseFunction(args.fragmentShader) 1443 | var paSource = undefined; 1444 | var construct = undefined; 1445 | var rank = undefined; 1446 | var lowPrecision = undefined; 1447 | 1448 | ShaderDSL.Typeinference.analyze(ast, paSource, construct, undefined, args.uniforms, varyingTypes, lowPrecision); 1449 | ShaderDSL.InferMem.infer(ast); 1450 | 1451 | var body = glslStatements(ast.body); 1452 | 1453 | var comp = "precision mediump float;\n"; 1454 | comp += emitUniformDecls(args); // FIXME same uniforms as vertex shader? or only referenced uniforms? or? 1455 | comp += emitVaryings(); 1456 | 1457 | comp += declareLocalFunctions(ast.body.funDecls); 1458 | comp += generateLocalFunctions(ast.body.funDecls); 1459 | 1460 | comp += "void main()\n" + body; 1461 | console.log('/*xltd*/ ' + comp); 1462 | return comp; 1463 | } 1464 | 1465 | function compileGladderProgram(args) { 1466 | function stripArrays(decls) { 1467 | // probably wrong to make gladder believe that arrays are scalars. 1468 | var result = {}; 1469 | for (var k in decls) { 1470 | if (!decls.hasOwnProperty(k)) continue; 1471 | if ((k.indexOf("vec") == 0 || k.indexOf("mat") == 0) && k.indexOf("[") >= 0) { 1472 | // FIXME that's a simplification 1473 | k = k.substring(0, 4); 1474 | } 1475 | result[k] = decls[k]; 1476 | } 1477 | return result; 1478 | } 1479 | return { 1480 | vertexShader: ShaderDSL.compileVertexShader(args), 1481 | fragmentShader: ShaderDSL.compileFragmentShader(args), 1482 | attributes: stripArrays(args.attributes), 1483 | uniforms: stripArrays(args.uniforms) 1484 | }; 1485 | } 1486 | 1487 | window.ShaderDSL = { 1488 | compileVertexShader: compileVertexShader, 1489 | compileFragmentShader: compileFragmentShader, 1490 | compileGladderProgram: compileGladderProgram, 1491 | parseGladderArrayDecl: parseGladderArrayDecl, 1492 | isGlMatrixSingleton: isGlMatrixSingleton, 1493 | isGlMatrixMatSingleton : isGlMatrixMatSingleton 1494 | }; 1495 | 1496 | })(); -------------------------------------------------------------------------------- /src/infermem.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | 17 | * Copyright (c) 2011, Intel Corporation 18 | * All rights reserved. 19 | * 20 | * Redistribution and use in source and binary forms, with or without 21 | * modification, are permitted provided that the following conditions are met: 22 | * 23 | * - Redistributions of source code must retain the above copyright notice, 24 | * this list of conditions and the following disclaimer. 25 | * - Redistributions in binary form must reproduce the above copyright notice, 26 | * this list of conditions and the following disclaimer in the documentation 27 | * and/or other materials provided with the distribution. 28 | * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 39 | * THE POSSIBILITY OF SUCH DAMAGE. 40 | * 41 | */ 42 | 43 | if (ShaderDSL === undefined) { 44 | var ShaderDSL = {}; 45 | } 46 | 47 | ShaderDSL.InferMem = function () { 48 | var definitions = Narcissus.definitions; 49 | eval(definitions.consts); 50 | eval(RiverTrail.definitions.consts); 51 | 52 | var isGlMatrixSingleton = ShaderDSL.isGlMatrixSingleton; 53 | 54 | const debug = false; 55 | 56 | // A MemSet models a set of memory variables for a given size. Optinally, 57 | // each memory name might be associated with a set of aliases that may 58 | // refer to the same memory. Such aliases are created when two memory sets 59 | // are overlayed, e.g., because they correspond to two independent control 60 | // flow paths. 61 | var MemSet = function () { 62 | var unique = {value : 0}; 63 | return function () { 64 | this._store = {}; 65 | this._store.__proto__ = null; 66 | this._unique = unique; 67 | return this; 68 | }; 69 | }(); 70 | var MSP = MemSet.prototype = {}; 71 | MSP.create = function create (name) { 72 | var memName = "_tmp_" + name + "_" + this._unique.value++; 73 | this.add(memName); 74 | return memName; 75 | } 76 | MSP.add = function add (name) { 77 | this._store[name] = null; 78 | } 79 | MSP.union = function union (other) { 80 | for (var mem in other._store) { 81 | if (other._store[mem] !== null) { 82 | // this entry has a set of aliases attached to it, so copy 83 | this._store[mem] = new MemSet(); 84 | this._store[mem].union(other._store[mem]); 85 | } else { 86 | this.add(mem); 87 | } 88 | } 89 | }; 90 | MSP.overlay = function overlap (other) { 91 | var keys = Object.keys(this._store); 92 | var keyPos = 0; 93 | for (var mem in other._store) { 94 | var ms; 95 | if (keyPos < keys.length) { 96 | if (this._store[keys[keyPos]] === null) { 97 | this._store[keys[keyPos]] = new MemSet(); 98 | } 99 | ms = this._store[keys[keyPos]]; 100 | ms.add(mem); 101 | } else { 102 | this.add(mem); 103 | ms = this._store[mem] = new MemSet(); 104 | } 105 | if (other._store[mem] !== null) { 106 | for (var alias in other._store[mem]._store) { 107 | ms.add(alias); 108 | } 109 | } 110 | keyPos++; 111 | } 112 | }; 113 | MSP.declare = function declare (kind) { 114 | var result = ""; 115 | for (var name in this._store) { 116 | result += kind + " " + name + ";"; // FIXME this is not proper decl for arrays 117 | if (this._store[name] !== null) { 118 | result += this._store[name].declareAlias(name); 119 | } 120 | }; 121 | return result; 122 | }; 123 | MSP.declareAlias = function declareAlias (alias) { 124 | result = ""; 125 | for (var name in this._store) { 126 | result += "char *" + name + " = " + alias + ";"; 127 | } 128 | return result; 129 | } 130 | 131 | // A memory list models a mapping from memory sizes to memory variables. 132 | // I sort them by sizes so that I can more easily overlay different 133 | // memory lists. 134 | var MemList = function () { 135 | this._store = []; 136 | return this; 137 | }; 138 | var MLP = MemList.prototype = {}; 139 | MLP.allocate = function allocate (type, name) { 140 | !(type instanceof ShaderDSL.Typeinference.TObject) && reportError("Wrong type " + type); 141 | var kind = type.OpenCLType; 142 | if (this._store[kind] === undefined) { 143 | this._store[kind] = new MemSet(); 144 | } 145 | return this._store[kind].create(name); 146 | }; 147 | MLP.join = function join (other) { 148 | for (var kind in other._store) { 149 | if (!other._store.hasOwnProperty(kind)) continue; 150 | if (this._store[kind] === undefined) { 151 | this._store[kind] = new MemSet(); 152 | } 153 | this._store[kind].union(other._store[kind]); 154 | } 155 | }; 156 | MLP.overlay = function overlay (other) { 157 | for (var kind in other._store) { 158 | if (!other._store.hasOwnProperty(kind)) continue; 159 | if (this._store[kind] === undefined) { 160 | this._store[kind] = new MemSet(); 161 | } 162 | this._store[kind].overlay(other._store[kind]); 163 | } 164 | }; 165 | MLP.declare = function declare () { 166 | var result = ""; 167 | for (var kind in this._store) { 168 | if (!this._store.hasOwnProperty(kind)) continue; 169 | result += this._store[kind].declare(kind); 170 | } 171 | return result; 172 | }; 173 | 174 | // 175 | // error reporting 176 | // 177 | function reportError(msg, t) { 178 | throw "Error: " + msg + " <" + (t ? RiverTrail.Helper.wrappedPP(t) : "no context") + ">"; // could be more elaborate 179 | } 180 | function reportBug(msg, t) { 181 | throw "Bug: " + msg; // could be more elaborate 182 | } 183 | 184 | var isArrayLiteral = RiverTrail.Helper.isArrayLiteral; 185 | 186 | // The code below creates a single buffer for each 187 | // dimension of the nested array. These buffers are 188 | // attached to the AST node and the backend emits code 189 | // for initializing the pointers in each of these 190 | // buffers. 191 | function allocateArrayMem(ast, prefix, memVars) { 192 | var shape = ast.typeInfo.getOpenCLShape(); 193 | var shape_len = shape.length; 194 | if(shape_len === 1) { 195 | ast.allocatedMem = memVars.allocate(ast.typeInfo, prefix); 196 | } 197 | else { 198 | ast.memBuffers = {size:0, list:[]}; 199 | /* 200 | var redu = 1; 201 | for(var i = 0; i < shape_len; i++) { 202 | //var type_size = getTypeSize(i, shape, ast.typeInfo.OpenCLType); 203 | var type_size = RiverTrail.Helper.getOpenCLSize(ast.typeInfo.OpenCLType); 204 | var allocation_size = type_size*shape[i]*redu; 205 | //debug && console.log("Allocating " + allocation_size + " bytes in " + "CALL_" 206 | // + i + " for i = " + i); 207 | var memBufferName = memVars.allocate(allocation_size, "CALL_" + i); 208 | ast.memBuffers.size +=1; 209 | ast.memBuffers.list.push(memBufferName); 210 | 211 | redu = redu*shape[i]; 212 | } 213 | */ 214 | var memBufferName = memVars.allocate(ast.typeInfo, "CALL_" + ast.typeInfo.OpenCLType + i); 215 | ast.memBuffers.size +=1; 216 | ast.memBuffers.list.push(memBufferName); 217 | 218 | // Set the primary memory buffer for this node to be the 219 | // top-level buffer 220 | ast.allocatedMem = ast.memBuffers.list[0]; 221 | //debug && console.log("Total AST allocations: ", ast.memBuffers.size, ast.memBuffers.list.length); 222 | } 223 | } 224 | 225 | 226 | // We allocate memory for the fields of the object 227 | // and for the object itself (pointer to the fields) 228 | function allocateObjMem(ast, memVars) { 229 | var fields = ast.typeInfo.properties.fields; 230 | var objSize = 0; 231 | ast.memBuffers = {__size:0, __root:null}; 232 | for(var idx in fields) { 233 | ast.memBuffers[idx] = []; 234 | if(fields[idx].isScalarType()) { 235 | var allocation_size = RiverTrail.Helper.getOpenCLSize(fields[idx].OpenCLType); 236 | objSize += allocation_size; 237 | } 238 | else if (fields[idx].name === "InlineObject") { 239 | reportError("InlineObject type properties not implemented yet"); 240 | } 241 | else if(fields[idx].name === "Array") { 242 | var shape = fields[idx].getOpenCLShape(); 243 | var shape_len = shape.length; 244 | 245 | objSize += RiverTrail.Helper.getOpenCLSize(fields[idx].OpenCLType); 246 | var redu = 1; 247 | for(var i = 0; i < shape_len; i++) { 248 | //var type_size = getTypeSize(i, shape, ast.typeInfo.OpenCLType); 249 | var type_size = RiverTrail.Helper.getOpenCLSize(fields[idx].OpenCLType); 250 | var allocation_size = type_size*shape[i]*redu; 251 | debug && console.log("Allocating " + allocation_size + " bytes in " + "FLD_" + i + " for i = " + i); 252 | var memBufferName = memVars.allocate(fields[idx], "FLD_" + i); // FIXME? 253 | ast.memBuffers.__size +=1; 254 | ast.memBuffers[idx].push(memBufferName); 255 | redu = redu*shape[i]; 256 | } 257 | } 258 | else { 259 | reportError("Unknown field type"); 260 | } 261 | } 262 | // Allocate space for the fields of the object 263 | var obj_memBufferName = memVars.allocate(objSize, "OBJ"); 264 | ast.memBuffers.size += 1; 265 | ast.memBuffers.__root = obj_memBufferName; 266 | ast.allocatedMem = obj_memBufferName; 267 | } 268 | 269 | function lvaluep(acc) { 270 | return acc && (acc.type == IDENTIFIER || acc.type == INDEX); 271 | } 272 | 273 | function infer(ast, memVars, ins, outs) { 274 | //"use strict"; // seems to trigger a timing-dependent behavior in Chrome 275 | 276 | switch (ast.type) { 277 | case SCRIPT: 278 | ast.funDecls.forEach(function (f) {infer(f.body);}); 279 | ast.memVars = new MemList(); 280 | ast.children.forEach(function (child) { infer(child, ast.memVars, null, null); }); 281 | break; 282 | 283 | case BLOCK: 284 | ast.children.forEach(function (child) { infer(child, memVars, ins, outs); }); 285 | break; 286 | 287 | // 288 | // statements 289 | // 290 | case FUNCTION: 291 | // this is not an applied occurence but the declaration, so we do not do anything here 292 | break; 293 | case RETURN: 294 | // special case: if the value is an ARRAY_INIT that only contains allocation free 295 | // expressions, we do not allocate space for the frame as it is directly written 296 | if (!isArrayLiteral(ast.value)) { 297 | infer(ast.value, memVars, ins, outs); 298 | } 299 | break; 300 | // 301 | // loops 302 | // 303 | case DO: 304 | // fallthrough; 305 | case FOR: 306 | // setup is run once 307 | if (ast.setup) { 308 | infer(ast.setup, memVars, ins, outs); 309 | } 310 | // fallthrough; 311 | case WHILE: 312 | infer(ast.condition, memVars, ins, outs); 313 | infer(ast.body, memVars, ast.ins, ast.outs); 314 | if (ast.update) { 315 | infer(ast.update, memVars, ast.ins, ast.outs); 316 | } 317 | break; 318 | case IF: 319 | infer(ast.condition, memVars, ins, outs); 320 | var thenMem = new MemList(); 321 | infer(ast.thenPart, thenMem, ins, outs); 322 | if (ast.elsePart) { 323 | var elseMem = new MemList(); 324 | infer(ast.elsePart, elseMem, ins, outs); 325 | thenMem.overlay(elseMem); 326 | } 327 | memVars.join(thenMem); 328 | break; 329 | case SEMICOLON: 330 | if (ast.expression) { 331 | infer(ast.expression, memVars, ins, outs); 332 | } 333 | break; 334 | case CONST: 335 | case VAR: 336 | ast.children.forEach(function (ast) { 337 | if (ast.initializer) { 338 | infer(ast.initializer, memVars, ins, outs); 339 | } 340 | }); 341 | break; 342 | case ASSIGN: 343 | // children[0] is the left hand side, children[1] is the right hand side. 344 | // both can be expressions. 345 | infer(ast.children[0], memVars, ins, outs); 346 | infer(ast.children[1], memVars, ins, outs); 347 | switch (ast.children[0].type) { 348 | case IDENTIFIER: 349 | // a = expr 350 | // 351 | // case 1: 352 | // If is in the __private address space, then if is an in and out var we have to copy, 353 | // as the memory we have allocated for could potentially be reused in the next iteration 354 | // of the loop before has been read. 355 | // 356 | // case 2: 357 | // If is in a different address space than , we have to copy, too. 358 | var aVar = ast.children[0]; 359 | if (((ast.children[1].typeInfo.getOpenCLAddressSpace() === "__private") && // case 1 360 | (ins && ins.contains(aVar.value) && outs && outs.contains(aVar.value))) || 361 | (aVar.typeInfo.getOpenCLAddressSpace() != ast.children[1].typeInfo.getOpenCLAddressSpace())) { // case 2 362 | if(!ast.typeInfo.isScalarType()) { 363 | var shape = ast.typeInfo.getOpenCLShape(); 364 | var shape_len = shape.length; 365 | debug && console.log("Creating memory for " + ast.children[0].value + " with shape: ", shape); 366 | ast.memBuffers = {size:0, list:[]}; 367 | var redu = 1; 368 | for(var i = 0; i < shape_len; i++) { 369 | //var type_size = getTypeSize(i, shape, ast.typeInfo.OpenCLType); 370 | var type_size = RiverTrail.Helper.getOpenCLSize(ast.typeInfo.OpenCLType); 371 | var allocation_size = type_size*shape[i]*redu; 372 | debug && console.log("Allocating " + allocation_size + " bytes in " + ast.children[0].value 373 | + "_" + i + " for i = " + i); 374 | var memBufferName = memVars.allocate(allocation_size, ast.children[0].value + "_" + i); 375 | ast.memBuffers.size +=1; 376 | ast.memBuffers.list.push(memBufferName); 377 | 378 | redu = redu*shape[i]; 379 | } 380 | // Set the primary memory buffer for this node to be the 381 | // top-level buffer 382 | ast.allocatedMem = ast.memBuffers.list[0]; 383 | debug && console.log("Total AST allocations: ", ast.memBuffers.size, ast.memBuffers.list.length); 384 | } 385 | } 386 | break; 387 | case INDEX: 388 | // case of a[iv] = expr. 389 | break; 390 | case DOT: 391 | // Support for updates on object properties. 392 | infer(ast.children[0], memVars, ins, outs); 393 | infer(ast.children[1], memVars, ins, outs); 394 | break; 395 | default: 396 | reportBug("unhandled lhs in assignment"); 397 | break; 398 | } 399 | break; 400 | 401 | // 402 | // expressions 403 | // 404 | 405 | case HOOK: 406 | // the hook (?) is badly designed. The first child is the condition, second child 407 | // the then expression, third child the else expression 408 | infer(ast.children[0], memVars, ins, outs); 409 | var thenMem = new MemList(); 410 | infer(ast.children[1], thenMem, ins, outs); 411 | var elseMem = new MemList(); 412 | infer(ast.children[2], elseMem, ins, outs); 413 | thenMem.overlay(elseMem); 414 | memVars.join(thenMem); 415 | break; 416 | 417 | // literals 418 | case IDENTIFIER: 419 | case THIS: 420 | case NUMBER: 421 | case TRUE: 422 | case FALSE: 423 | // nothing to do here 424 | break; 425 | 426 | case ARRAY_INIT: // FIMXE need allocation? 427 | // fallthru 428 | // stuff where we just look at the children 429 | case COMMA: 430 | case INCREMENT: 431 | case PLUS: 432 | case DECREMENT: 433 | case MINUS: 434 | case MUL: 435 | case EQ: 436 | case NE: 437 | case STRICT_EQ: 438 | case STRICT_NE: 439 | case LT: 440 | case LE: 441 | case GE: 442 | case GT: 443 | case BITWISE_OR: 444 | case BITWISE_XOR: 445 | case BITWISE_AND: 446 | case LSH: 447 | case RSH: 448 | case URSH: 449 | case DIV: 450 | case MOD: 451 | case AND: 452 | case OR: 453 | case NOT: 454 | case UNARY_PLUS: 455 | case UNARY_MINUS: 456 | case BITWISE_NOT: 457 | case DOT: 458 | case INDEX: 459 | case LIST: 460 | case CAST: 461 | case FLATTEN: 462 | case TOINT32: 463 | if (ast.children) { 464 | ast.children.forEach( function (child) { infer(child, memVars, ins, outs); }); 465 | } 466 | break; 467 | case CALL: 468 | if (ast.children) { 469 | if(ast.children[0].type === DOT && ast.children[0].children[0].value === "RiverTrailUtils") { 470 | switch (ast.children[0].children[1].value) { 471 | case "createArray": 472 | allocateArrayMem(ast, "createArray", memVars); 473 | break; 474 | default: 475 | reportError("Invalid method " + ast.children[0].children[1].value + " on RiverTrailUtils", ast); 476 | } 477 | } 478 | else { 479 | ast.children.forEach( function (child) { infer(child, memVars, ins, outs); }); 480 | } 481 | } 482 | 483 | if(ast.typeInfo.name === "InlineObject") { 484 | allocateObjMem(ast, memVars); 485 | } 486 | // If I am returning an Array space needs to be allocated for it in the caller and 487 | // the name of the space should be left in the CALL nodes allocatedMem field so that when 488 | // I generate the call it is available. However, if this method does return a pointer 489 | // to some existing data, like |get| on ParallelArray, the type inference will have 490 | // left an isShared annotation and no memory needs to be allocated. 491 | else if (!ast.typeInfo.isScalarType()) { 492 | // This call returns a nested array. The caller needs to allocate enough 493 | // memory for this array and initialize the pointers in 494 | // the allocated buffer to create a structure that the 495 | // callee can simply fill the leaves of. 496 | 497 | var receiver = ast.children[0].children[0]; 498 | if (receiver === undefined || !isGlMatrixSingleton(receiver.value)) { 499 | // don't bother 500 | break; 501 | } 502 | var callName = ast.children[0].children[1].value; 503 | // Recognizes calls to methods with accumulator of the form: 504 | // mat4.multiply(a, b, expr); // expr is not a lvalue 505 | // these can't be translated to expr = a * b; 506 | // so a temporary is allocated: float tmp = expr; tmp = a * b; 507 | // which could be collapsed to tmp = a * b, if it weren't for side effects in expr. 508 | 509 | switch (callName) { 510 | case "negate": 511 | case "multiply": 512 | case "subtract": 513 | case "divide": 514 | case "add": { 515 | var args = ast.children[1].children; 516 | var acc = args[args.length - 1]; 517 | if (!lvaluep(acc)) { 518 | console.log("allocateArrayMem", Narcissus.decompiler.pp(ast), memVars.declare()); 519 | allocateArrayMem(ast, callName, memVars); 520 | } 521 | } 522 | break; 523 | default: 524 | 525 | } 526 | } 527 | break; 528 | 529 | // 530 | // unsupported stuff here 531 | // 532 | case GETTER: 533 | case SETTER: 534 | reportError("setters/getters not yet implemented", ast); 535 | break; 536 | case TRY: 537 | case THROW: 538 | reportError("try/throw/catch/finally not yet implemented", ast); 539 | break; 540 | case BREAK: 541 | case CONTINUE: 542 | case LABEL: 543 | reportError("break/continure/labels not yet implemented", ast); 544 | break; 545 | case YIELD: 546 | case GENERATOR: 547 | reportError("generators/yield not yet implemented", ast); 548 | break; 549 | case FOR_IN: 550 | reportError("for .. in loops not yet implemented", ast); 551 | break; 552 | case ARRAY_COMP: 553 | case COMP_TAIL: 554 | reportError("array comprehensions not yet implemented", ast); 555 | break; 556 | case NEW: 557 | for(var idx = 0; idx < ast.children.length; idx++) { 558 | infer(ast.children[idx].children[1], memVars, ins, outs); 559 | } 560 | break; 561 | case NEW_WITH_ARGS: 562 | case OBJECT_INIT: 563 | for(var idx = 0; idx < ast.children.length; idx++) { 564 | infer(ast.children[idx].children[1], memVars, ins, outs); 565 | } 566 | break; 567 | case WITH: 568 | reportError("general objects not yet implemented", ast); 569 | break; 570 | case LET: 571 | case LET_BLOCK: 572 | reportError("let not yet implemented", ast); 573 | break; 574 | case SWITCH: 575 | reportError("switch not yet implemented", ast); 576 | break; 577 | 578 | // unsupported binary functions 579 | case INSTANCEOF: 580 | reportError("instanceof not yet implemented", ast); 581 | break; 582 | case EQ: 583 | case NE: 584 | reportError("non-strict equality not yet implemented", ast); 585 | break; 586 | case IN: 587 | reportError("in not yet implemented", ast); 588 | break; 589 | 590 | // unsupported literals 591 | case NULL: 592 | reportError("null not yet implemented", ast); 593 | break; 594 | case REGEXP: 595 | reportError("regular expressions not yet implemented", ast); 596 | break; 597 | case STRING: 598 | reportError("strings not yet implemented", ast); 599 | break; 600 | 601 | case DEBUGGER: // whatever this is... 602 | default: 603 | throw "unhandled node type in analysis: " + ast.type; 604 | } 605 | 606 | }; 607 | 608 | function doInfer (ast) { 609 | if (ast.type !== FUNCTION) { 610 | reportBug("you probaly wanted to call the inference on a function node!"); 611 | } else { 612 | infer(ast.body); 613 | } 614 | }; 615 | 616 | return { 617 | "infer" : doInfer 618 | }; 619 | }(); 620 | -------------------------------------------------------------------------------- /src/typeinference.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adobe-webplatform/shaderdsl/bced56b1491ed5bd0884c6286c49785e03750710/src/typeinference.js -------------------------------------------------------------------------------- /third-party/README: -------------------------------------------------------------------------------- 1 | 2 | 3 | --------------------------------------------------------------------------------