al_attach_shader_source(3)

πŸ“¦
version 5.2.11.3 Β· 3 versions
, in arch rolling

πŸ’– NAME πŸ’–

okay so this is al_attach_shader_source(3) from the Allegro reference manual, and it's literally the attach + compile a shader source page bestie ✨

al_attach_shader_source β€” Allegro 5 API.

✨ SYNOPSIS ✨

#include <allegro5/allegro.h>

bool al_attach_shader_source(ALLEGRO_SHADER *shader, ALLEGRO_SHADER_TYPE type,
    const char *source)

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so what this function does: it attaches the shader's source code to the shader object and compiles it. Slay. πŸ’… If you pass NULL, it deletes the underlying (OpenGL or DirectX) shader. Also, if you'd rather grab your shader source from an external file, see al_attach_shader_source_file(3) β€” that's the file-based bestie.

If you do not use ALLEGRO_PROGRAMMABLE_PIPELINE, Allegro's graphics functions will not use any shader specific functions themselves. And in case of a system with no fixed function pipeline (like OpenGL ES 2 or OpenGL 3 or 4), this means Allegro's drawing functions cannot be used. Like... cannot. 😭

TODO: Is ALLEGRO_PROGRAMMABLE_PIPELINE set automatically in this case?

🫢 the uniform situation when ALLEGRO_PROGRAMMABLE_PIPELINE is on

When ALLEGRO_PROGRAMMABLE_PIPELINE is used, the following shader uniforms are provided by Allegro and can be accessed in your shaders:

  • al_projview_matrix β€” matrix for Allegro's orthographic projection multiplied by the al_use_transform(3) matrix. The type is mat4 in GLSL, and float4x4 in HLSL.
  • al_use_tex β€” whether or not to use the bound texture. The type is bool in both GLSL and HLSL.
  • al_tex β€” the texture if one is bound. The type is sampler2D in GLSL and texture in HLSL.
  • al_use_tex_matrix β€” whether or not to use a texture matrix (used by the primitives addon). The type is bool in both GLSL and HLSL.
  • al_tex_matrix β€” the texture matrix (used by the primitives addon). Your shader should multiply the texture coordinates by this matrix. The type is mat4 in GLSL, and float4x4 in HLSL.

🌸 GLSL alpha testing extras

With GLSL, alpha testing is done in the shader and uses these additional uniforms:

  • al_alpha_test β€” Whether to do any alpha testing. If false, the shader should render the pixel, otherwise it should interpret the values of al_alpha_func and al_alpha_test_val.
  • al_alpha_func β€” The alpha testing function used. One of the ALLEGRO_RENDER_FUNCTION(3) values. The default is ALLEGRO_RENDER_ALWAYS which means all pixels (even completely transparent ones) are rendered. The type is int. See ALLEGRO_RENDER_STATE(3).
  • al_alpha_test_val β€” If alpha testing is not ALLEGRO_RENDER_NEVER or ALLEGRO_RENDER_ALWAYS, the alpha value to compare to for alpha testing. The type is float.

πŸŽ€ GLSL vertex attributes

For GLSL shaders, the vertex attributes are passed using the following variables:

  • al_pos β€” vertex position attribute. Type is vec4.
  • al_texcoord β€” vertex texture coordinate attribute. Type is vec2.
  • al_color β€” vertex color attribute. Type is vec4.
  • al_user_attr_0 β€” The vertex attribute declared as ALLEGRO_PRIM_USER_ATTR
  • al_user_attr_1, ..., al_user_attr_9 β€” The vertex attribute declared as ALLEGRO_PRIM_USER_ATTR + X where X is an integer from 1 to 9

🧚 HLSL vertex attributes

For HLSL shaders, the vertex attributes are passed using the following semantics:

  • POSITION0 β€” vertex position attribute. Type is float4.
  • TEXCOORD0 β€” vertex texture coordinate attribute. Type is float2.
  • TEXCOORD1 β€” vertex color attribute. Type is float4.

πŸ‘‘ macro name cheat sheet

Also, each shader variable has a corresponding macro name that can be used when defining the shaders using string literals. Do not use these macros with the other shader functions β€” that will lead to undefined behavior. Not cute. πŸ’€

  • ALLEGRO_SHADER_VAR_PROJVIEW_MATRIX for "al_projview_matrix"
  • ALLEGRO_SHADER_VAR_POS for "al_pos"
  • ALLEGRO_SHADER_VAR_COLOR for "al_color"
  • ALLEGRO_SHADER_VAR_TEXCOORD for "al_texcoord"
  • ALLEGRO_SHADER_VAR_USE_TEX for "al_use_tex"
  • ALLEGRO_SHADER_VAR_TEX for "al_tex"
  • ALLEGRO_SHADER_VAR_USE_TEX_MATRIX for "al_use_tex_matrix"
  • ALLEGRO_SHADER_VAR_TEX_MATRIX for "al_tex_matrix"
  • ALLEGRO_SHADER_VAR_ALPHA_FUNCTION for "al_alpha_func"
  • ALLEGRO_SHADER_VAR_ALPHA_TEST_VALUE for "al_alpha_test_val"

Examine the output of al_get_default_shader_source(3) for an example of how to use the above uniforms and attributes. Fr fr, it's the reference example. πŸ‘€

It returns true on success and false on error, in which case the error log is updated. The error log can be retrieved with al_get_shader_log(3). So if it fails, go check the log bestie. 🫢

🌈 SINCE 🌈

5.1.0 β€” iconic since then. ⭐

πŸŽ€ SEE ALSO πŸŽ€


source/version: Allegro reference manual Β· date: not printed on this page β€” bye bestie! πŸ«ΆπŸ’–

πŸ“¦ where this page comes from
  • arch rolling: allegro 5.2.11.3-2 Β· Zlib AND BSD-3-Clause AND OFL-1.1 AND LicenseRef-Custom
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms