Can GLSL macro expansion do this?

11,493

GLSL preprocessor "equals" to the standard C preprocessor. Indeed, you can reach what you want with the following preprocessor definition:

#define POW(a, b) Pow ## b ## (a)

But pay attention, since the concatenation operator (##) is available only starting from GLSL 1.30. Indeed using previous GLSL versions this macro will generate a compiler error.

Still wondering why you don't use the pow function...

Share:
11,493
metaleap
Author by

metaleap

Freelance developer since high school (1999), increasingly into pure functional programming and the Lambda calculus Since 2015 --- Haskell, Elm, PureScript Since 2012 --- OpenGL+GLSL Since 2011 --- Go (aka golang), WebGL, Node.js, CoffeeScript Since 2008 --- F#, Python, Lisp/Clojure/Scheme Since 2006 --- SharePoint Since 2001 --- C#, ASP.NET, XSLT, XPath Since 2000 --- PHP, Java Since 1999 --- SQL, ASP, VisualBasic Since 1998 --- Pascal, Basic, HTML+CSS+JS Before long --- OCaml, Erlang Elixir, Scala, R, Rust, Nemerle..

Updated on August 18, 2022

Comments

  • metaleap
    metaleap over 1 year

    Consider the following GLSL functions:

    float Pow3 (const in float f) {
        return f * f * f;
    }
    
    float Pow4 (const in float f) {
        return f * f * f * f;
    }
    
    float Pow5 (const in float f) {
        return f * f * f * f * f;
    }
    

    ... and so on. Is there a way to #define a GLSL macro that can generate n multiplications-of-f-by-itself at compile time, not using the built-in GLSL pow() function of course?