Skip to content

Commit

Permalink
Add notes on bit depth for ints and floats (#10028)
Browse files Browse the repository at this point in the history
* Added notes on bit depth for ints and floats

Added a small note about the bit depth of integers and floats in Godot's shading language as it is not explicitly stated anywhere.

The bit depth of integer and floats in GDscript and Godot's shading language are different, which can cause problems with lost precision in calculations when integers are set from GDscript as floats/ints in GDscript are 64 bits instead of 32 bits (the standard in GLSL ES 3.0).

While most are unlikely to run into problems due to this difference in bit depth, it can cause mathematical errors in edge cases. As stated by previous contributors, no error will be thrown if types do not match while setting a shader uniform. This includes GDscript floats being set as Godot shader floats (which may not be intuitive).
  • Loading branch information
BrianBHuynh authored Oct 4, 2024
1 parent 8af700e commit 67e37b5
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tutorials/shaders/shader_reference/shading_language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Most GLSL ES 3.0 datatypes are supported:
+----------------------+---------------------------------------------------------------------------------+
| **bvec4** | Four-component vector of booleans. |
+----------------------+---------------------------------------------------------------------------------+
| **int** | Signed scalar integer. |
| **int** | 32 bit signed scalar integer. |
+----------------------+---------------------------------------------------------------------------------+
| **ivec2** | Two-component vector of signed integers. |
+----------------------+---------------------------------------------------------------------------------+
Expand All @@ -48,7 +48,7 @@ Most GLSL ES 3.0 datatypes are supported:
+----------------------+---------------------------------------------------------------------------------+
| **uvec4** | Four-component vector of unsigned integers. |
+----------------------+---------------------------------------------------------------------------------+
| **float** | Floating-point scalar. |
| **float** | 32 bit floating-point scalar. |
+----------------------+---------------------------------------------------------------------------------+
| **vec2** | Two-component vector of floating-point values. |
+----------------------+---------------------------------------------------------------------------------+
Expand Down Expand Up @@ -227,7 +227,7 @@ variables, arguments and varyings:
lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (default)
highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (32 bit default)
Using lower precision for some operations can speed up the math involved (at the
Expand Down Expand Up @@ -938,6 +938,9 @@ table of the corresponding types:
be thrown if the type does not match. Your shader will just exhibit
undefined behavior.

.. warning::
As with the last note, no error will be thrown if the typing does not match while setting a shader uniform, this unintuitively includes setting a (GDscript) 64 bit int/float into a Godot shader language int/float (32 bit). This may lead to unintentional consequences in cases where high precision is required.

Uniforms can also be assigned default values:

.. code-block:: glsl
Expand Down

0 comments on commit 67e37b5

Please sign in to comment.