Shaders

This section explains the functions and constants used to create shaders in GameMaker: Studio.

Shaders are an incredibly powerful tool for manipulating what and how things are rendered to the screen by the graphics card. Since these tiny programs are actually run on the card itself, this means that they are extremely fast to process, freeing up valuable CPU cycles for more game logic.

To create a shader you will need to have written both a Vertex Shader and a Fragment Shader (also know as a Pixel Shader) using the Shader Editor, and even if (for example) you only wish to change the vertex positions for an instance being drawn, or if you only want to change the colour values for the pixels, you will still need both programs for a complete shader to work.

NOTE: Shaders do not permit you to change the value of any variables that you pass into them, and so these will be called shader constants in all the documentation that refers to them.

For a complete overview of the available GLSL ES functions and variables that you can use to program the shaders themselves, please refer to the OpenGL ES Shading Language (GLSL ES) Reference Pages.

Using Shaders

Once you have your shader created, using it is very simple and only requires a couple of lines of code to get the most basic of use from it:

shader_set(myShader);
draw_self();
shader_reset();

As you can see, they are used in a similar manner to blend modes and surfaces, where you first select (set) the shader, draw what you want using it, then reset the draw target again afterwards. If you wish to render the full screen through a shader, and not just a single sprite or background, you will need to set up a surface to catch the current view, and then pass that through to the shader (see Surfaces for more information).

If the shader you are using has input values, these are set using the uniform functions. You would first get the uniform handle (which is essentially an ID value for the uniform to be set) using the function shader_get_uniform() in the Create Event of the instance using the shader, and then store these handles in variables, something like this:

colour_to_find = shader_get_uniform(sShaderDemo5, "f_Colour1");
colour_to_set = shader_get_uniform(sShaderDemo5, "f_Colour2");

Once you have the uniform handles, they can then be set in the shader code for the Draw Event like this:

shader_set(sShaderDemo5);
shader_set_uniform_f(colour_to_find, 1,1,1 );
shader_set_uniform_f(colour_to_set, 1,0,0 );
draw_sprite(sprite_index,image_index,x+24, y);
shader_reset();

One final thing to note is that although shaders are accepted across all platforms, they are still device specific and if the hardware or software of the device cannot use shaders then you will get an error. Therefore you are recommended to check that the shader has been compiled before setting uniforms or using the shader itself, like this:

if (shader_is_compiled(myShader))
   {
   shader_set(myShader);
   draw_self();
   shader_reset();
   }
else show_debug_message("Shader failed");

As an extra check you can also call the function shaders_are_supported to see if the hardware even supports shaders. In general you'd do these checks on game start and store the results as a global variable to then check later.

It is important to note that GameMaker: Studio 1.4 also supports some conditional compile Macros which can be used within GLSL ES shaders so they can perform alternative code on specific supported platforms. The macros and the platforms they will be generated on are shown in the table below:

Shader Macro Value Target Platform
_YY_GLSLES_ 1 All target platforms
_YY_GLSL_ 1 Mac and Ubuntu (Linux)
_YY_HLSL9_ 1 Windows 32
_YY_HLSL11_ 1 UWP, XboxOne
_YY_PSSL_ 1 PlayStation 4
_YY_CG_PS3_ 1 PlayStation 3
_YY_CG_PSVITA_ 1 PS Vita

When you compile your GameMaker Studio 2 project on any one of the listed platforms using a GLSL ES format shader, one of the above macros will be generated which can then be used checked in the shader code like this:

#ifdef _YY_HLSL11_
// HLSL shader code here
#else
// GLSL shader code here
#endif




NOTE: Shaders, like anything related to drawing, can only be used in the draw event. It is also worth noting that if you are trying to use a colour value in a shader and the object has no texture, the results will turn out black.



The following functions are available for drawing shaders:

  1. shader_set
  2. shader_get_uniform
  3. shader_get_sampler_index
  4. shader_set_uniform_f
  5. shader_set_uniform_f_array
  6. shader_set_uniform_i
  7. shader_set_uniform_i_array
  8. shader_set_uniform_matrix
  9. shader_set_uniform_matrix_array
  10. shader_reset
  11. shader_is_compiled
  12. shaders_are_supported
  13. shader_current

We also have a special function which defines a global state for all shaders:

  1. shader_enable_corner_id

Textures

When working with texture samplers in shaders you will need information about the texture being used, in which case you can use the following functions:

  1. background_get_texture
  2. background_get_uvs
  3. sprite_get_texture
  4. sprite_get_uvs
  5. font_get_texture
  6. font_get_uvs
  7. texture_get_width
  8. texture_get_height
  9. texture_get_texel_width
  10. texture_get_texel_height
  11. texture_set_stage
  12. texture_set_interpolation_ext
  13. texture_set_repeat_ext

GameMaker: Studio Shader Constants

While this manual will not cover any of the Open GL shader functions and variables, it does contain a list of the ones that are unique to GameMaker: Studio. These constants are not part of the Open GL specification for shaders and are supplied to simplify the integration of shaders within your projects.

  1. Shader Constants

Vertex Formats and Custom Primitives

Finally, GameMaker: Studio permits you to define your own Vertex Formats from which you can create your own custom primitives. This can greatly speed up shader operations or can be used to extend their capabilities and create surprising effects. You can find information on this in the following sections:

  1. Vertex Formats
  2. Primitive Building


Back: Reference
© Copyright YoYo Games Ltd. 2018 All Rights Reserved