Browse Source

Merge pull request #6979 from JohanAR/shaderpp_concat

Add concatenation to shader preprocessor
Rémi Verschelde 1 year ago
parent
commit
adbab1482c
1 changed files with 17 additions and 2 deletions
  1. 17 2
      tutorials/shaders/shader_reference/shader_preprocessor.rst

+ 17 - 2
tutorials/shaders/shader_reference/shader_preprocessor.rst

@@ -47,8 +47,8 @@ General syntax
 Defines the identifier after that directive as a macro, and replaces all
 Defines the identifier after that directive as a macro, and replaces all
 successive occurrences of it with the replacement code given in the shader.
 successive occurrences of it with the replacement code given in the shader.
 Replacement is performed on a "whole words" basis, which means no replacement is
 Replacement is performed on a "whole words" basis, which means no replacement is
-performed if the string is part of another string (without any spaces separating
-it).
+performed if the string is part of another string (without any spaces or
+operators separating it).
 
 
 Defines with replacements may also have one or more *arguments*, which can then
 Defines with replacements may also have one or more *arguments*, which can then
 be passed when referencing the define (similar to a function call).
 be passed when referencing the define (similar to a function call).
@@ -56,6 +56,21 @@ be passed when referencing the define (similar to a function call).
 If the replacement code is not defined, the identifier may only be used with
 If the replacement code is not defined, the identifier may only be used with
 ``#ifdef`` or ``#ifndef`` directives.
 ``#ifdef`` or ``#ifndef`` directives.
 
 
+If the *concatenation* symbol (``##``) is present in the replacement code then
+it will be removed upon macro insertion, together with any space surrounding
+it, and join the surrounding words and arguments into a new token.
+
+.. code-block:: glsl
+
+    uniform sampler2D material0;
+
+    #define SAMPLE(N) vec4 tex##N = texture(material##N, UV)
+
+    void fragment() {
+        SAMPLE(0);
+        ALBEDO = tex0.rgb;
+    }
+
 Compared to constants (``const CONSTANT = value;``), ``#define`` can be used
 Compared to constants (``const CONSTANT = value;``), ``#define`` can be used
 anywhere within the shader (including in uniform hints).
 anywhere within the shader (including in uniform hints).
 ``#define`` can also be used to insert arbitrary shader code at any location,
 ``#define`` can also be used to insert arbitrary shader code at any location,