shader_preprocessor.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. .. _doc_shader_preprocessor:
  2. Shader Preprocessor
  3. ===================
  4. The shader preprocessor is an optional step before shader compilation of text shaders in Godot.
  5. If you don't need it, you may ignore it, but it contains some useful macros which may speed up your productivity.
  6. #define
  7. ^^^^^^^
  8. \ **Syntax:** `#define identifier <replacement_code>`.
  9. Defines the identifier after that directive as a macro, and replaces all successive occurrence of it with the replacement code given in the shader.
  10. If the replacement code is not defined, it may only be used within `#ifdef` or `#ifndef` directives.
  11. .. code-block:: glsl
  12. shader_type spatial;
  13. #define USE_MY_COLOR
  14. #define MY_COLOR vec3(1, 0, 0)
  15. void fragment() {
  16. #ifdef USE_MY_COLOR
  17. ALBEDO = MY_COLOR;
  18. #endif
  19. }
  20. #if
  21. ^^^
  22. \ **Syntax:** `#if condition`.
  23. The `#if` directive checks the condition and if it evaluates to a non-zero value, the code block is included, otherwise it is skipped.
  24. In order to evaluate, the condition must be an expression giving a simple floating-point, integer or boolean result. There may be multiple condition blocks connected by `||` or `&&` operators.
  25. It may be continued by a `#else` block, but must be ended with the `#endif` directive.
  26. .. code-block:: glsl
  27. #define VAR 3
  28. #define USE_LIGHT 0 // evaluates to false
  29. #define USE_COLOR 1 // evaluates to true
  30. #if VAR == 3 && (USE_LIGHT || USE_COLOR)
  31. #endif
  32. #ifdef
  33. ^^^^^^
  34. \ **Syntax:** `#ifdef identifier`.
  35. Checks whether the passed identifier is defined by `#define` before that directive. Useful for creating multiple shader versions in the same file.
  36. It may be continued by a `#else` block, but must be ended with the `#endif` directive.
  37. .. code-block:: glsl
  38. #define USE_LIGHT
  39. #ifdef USE_LIGHT
  40. #endif
  41. #ifndef
  42. ^^^^^^^
  43. \ **Syntax:** `#ifndef identifier`.
  44. Similar to `#ifdef` but checks whether the passed identifier is not defined by `#define` before that directive.
  45. #else
  46. ^^^^^
  47. \ **Syntax:** `#else`.
  48. Defines the optional block which is included when the previously defined `#if`, `#ifdef` or `#ifndef` directive evaluates to false.
  49. .. code-block:: glsl
  50. shader_type spatial;
  51. #define MY_COLOR vec3(1, 0, 0)
  52. void fragment() {
  53. #ifndef MY_COLOR
  54. ALBEDO = MY_COLOR;
  55. #else
  56. ALBEDO = vec3(0, 0, 1);
  57. #endif
  58. }
  59. #endif
  60. ^^^^^^
  61. \ **Syntax:** `#endif`.
  62. Used as terminator for the `#if`, `#ifdef`, `#ifndef` or subsequent `#else` directives.
  63. #undef
  64. ^^^^^^
  65. \ **Syntax:** `#undef identifier`.
  66. The `#undef` directive may be used to cancel the previously defined `#define` directive:
  67. .. code-block:: glsl
  68. #define MY_COLOR vec3(1, 0, 0)
  69. vec3 get_red_color() {
  70. return MY_COLOR;
  71. }
  72. #undef MY_COLOR
  73. #define MY_COLOR vec3(0, 1, 0)
  74. vec3 get_green_color() {
  75. return MY_COLOR;
  76. }
  77. Without `#undef` in that case there will be a macro redefinition error.
  78. #include
  79. ^^^^^^^^
  80. \ **Syntax:** `#include "path"`.
  81. The `#include` directive includes the content of shader include to a shader. It may be used in any place, but is recommended at the beginning of the shader file,
  82. after the `shader_type` to prevent possible errors. The shader include may be created by using a `File → Create Shader Include` menu option of the shader editor.
  83. .. code-block:: glsl
  84. #include "my_shader_inc.gdshaderinc"
  85. #pragma
  86. ^^^^^^^
  87. \ **Syntax:** `#pragma value`.
  88. The `#pragma` directive provides additional information to the preprocessor or compiler.
  89. Currently, it may have only one value: `disable_preprocessor`.
  90. If you don't need the preprocessor, use that directive, and it will speed up the shader compilation by excluding the preprocessor step.
  91. .. code-block:: glsl
  92. #pragma disable_preprocessor