4 ms·
Sure, GLSL is easy to learn, because it's basically a simplified C, which many people are already familiar with. It's intentionally bare-bones, verbose and old
by joan_kode 6y ago
Sure, GLSL is easy to learn, because it's basically a simplified C, which many people are already familiar with. It's intentionally bare-bones, verbose and old school, so that everyone can learn it and every vendor won't get bogged down writing compilers for it. In that sense, it achieves its purpose well enough.
But come on, it doesn't even have first-class functions. There are countless examples of unreadable macros and code duplication out there due to GLSL's extreme simplicity.
- BubRoss 6y agoI have never seen anyone think GLSL needed first class functions. Can you give me an example of how you would use them instead of a macro? GPUs can't jump to a function pointer as far as I know and macros can't be passed to functions.
- daeken 6y agoFunctional programming can make advanced shaders -- particularly distance field stuff -- significantly simpler. I pass first-class functions around all over the place in this example, which compiles down to GLSL: https://github.com/daeken/DaeForth/blob/master/FragmentToy/test.dfr https://github.com/daeken/DaeForth/blob/master/FragmentToy/t... A good example there is `twist-func' which takes in a world-space position, a block that hands back a twist factor, and a block that receives the transformed position (in object space). Once you start doing things that way, you end up with ridiculously rapid development, which is the whole reason I created this family of languages; started as an experiment to take APL/K, Lisp, and Forth and smash them all together, and it ended up working out well enough that I've kept it going for 7 years now.
- joan_kode 6y agoYes, I can give you examples. On this page (which is probably the most popular reference on distance functions for raymarching): https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm https://www.iquilezles.org/www/articles/distfunctions/distfu... If you scroll down to the section "Deformations and distortions", you'll notice that iq wishes so hard there were first class functions in GLSL that he pretends they exist in all the code examples (the deformation functions all take functions as parameters). As for macros specifically filling in the role of first class functions, this is sometimes done by creating a macro that takes a function as parameter. So for example, iq's functions above could be turned into crazy macros.
- fulafel 6y agoGPUs can jump to function pointers and some GPU languages support them. Have a look at "4.2 Branching" in http://developer.amd.com/wordpress/media/2013/12/AMD_GCN3_Instruction_Set_Architecture_rev1.1.pdf http://developer.amd.com/wordpress/media/2013/12/AMD_GCN3_In... Also, it's possible to compile code using function pointers down to more limited forms of flow control instructions.
- TazeTSchnitzel 6y agoGood luck with performance if it's non-uniform across the warp.