• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Shader works wrong on Mac

muddrox

Member
Hello, there everyone! I developed a shader that makes any sprite a solid color. It works wonderfully on windows. However, this shader fails to work the same way on Mac. On Mac, the shader effect produces a weird, staticy, rectangular effect over the sprites that are drawn with it. I have no idea why this is the case because it works perfect on my windows computer.

Here is my Shader:

-vertex shader

Code:
//
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
  
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}
-Fragment Shader

Code:
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform vec4 color;

void main()
{ 
    float tex_alpha = texture2D( gm_BaseTexture, v_vTexcoord ).a;
  
    if ( tex_alpha > 0.0 ) {
        gl_FragColor = color;
    }
}
Now, here is the object that actually uses the shader: As you notice in the create event, some values are initially empty. These variables are changed right after the object's create an event using another script I created called, scr_effect_flash. This script is called from within another object to initialize the variables after this object's create event. Anyways, despite what the create event initially implies, the variable, color, is immediately changed to be either c_red, c_green, or c_blue.

obj_effect_flash

-create event

Code:
color = noone;

image_speed = 0;

uni_color = shader_get_uniform(shd_solid_color, "color");

var_red   = 0;
var_green = 0;
var_blue  = 0;
var_alpha = image_alpha;

glitter = part_type_create();

part_type_shape(glitter,pt_shape_spark);
part_type_size(glitter,0.10,0.10,0,0);
part_type_scale(glitter,1,1);
part_type_alpha2(glitter,1,0);
part_type_speed(glitter,0.10,2,0,0);
part_type_gravity(glitter,0,270);
part_type_orientation(glitter,0,0,0,0,1);
part_type_blend(glitter,1);
part_type_life(glitter,30,180);

emit_glitter = part_emitter_create(global.ps_main);

dir  = noone;
emit = false;
-Step Event

Code:
if ( emit == false && dir != noone ) {
    var regSize = 30;
  
    part_type_direction(glitter, dir - 20, dir + 20, 0, 20);
    part_type_color1(glitter, color);
  
    part_emitter_region(global.ps_main,emit_glitter,x-regSize,x+regSize,y-regSize,y+regSize,ps_shape_ellipse,ps_distr_gaussian);
    part_emitter_burst(global.ps_main,emit_glitter,glitter,5);
}

if ( !instance_exists(obj_player) ){
    instance_destroy();
}

if ( image_alpha > 0 ) {
    image_alpha -= .1;
} else {
    instance_destroy();
}
-Draw Event

Code:
if ( !sprite_exists(sprite_index) ) exit;

var_red   = 1 * ( color_get_red(color)   / 255 );
var_green = 1 * ( color_get_green(color) / 255 );
var_blue  = 1 * ( color_get_blue(color)  / 255 );
var_alpha = image_alpha;

shader_set(shd_solid_color);

shader_set_uniform_f(uni_color, var_red, var_green, var_blue, var_alpha);

draw_self();

shader_reset();
I hope this is enough information to get some ideas thrown around. I have no idea what is wrong as it works perfectly on windows but not Mac. Everything else in the game works right on Mac except for that specific shader. I thank you all in advance for you help!

Update:
I added some screenshot of examples of what is going on between windows and Mac:

Windows version (working correctly):
windows capture.PNG

Mac Version (working incorrectly):

mac capture.png mac capture 2.png
 
Last edited:
What are the hardware specs on your Mac.

What version of Mac OS and GMS are you using.

Can you post a screen shot of the shader working and not working.
 

muddrox

Member
Here are the specs for the Mac:

Macbook Air (2017) 256GB

1.8GHz Processor
256GB Storage

1.8GHz dual-core Intel Core i5 processor
Turbo Boost up to 2.9GHz
8GB 1600MHz LPDDR3 memory
256GB SSD storage1
Intel HD Graphics 6000

---------------------------------

I use Game Maker 2. I am running the latest version of Mac Os. Here are screenshot examples of what the windows version looks like in comparison to the Mac version:

Windows Version:
windows capture.PNG

Mac Version:
mac capture.png mac capture 2.png
 
Last edited:

Ido-f

Member
Try logging shader_is_compiled at the start of your game.
iirc some syntax in a shader will be able to be compiled in windows but not when using mac OS.
If you get 1 for shader_is_compiled on windows and 0 on the mac that's your problem.
If that's the case I've had some success debugging shaders in here: http://evanw.github.io/glslx/
as game maker doesn't provide good error feedback for shaders (sometimes none of it, on mac).

In your case, I suspect this line:

Code:
float tex_alpha = texture2D( gm_BaseTexture, v_vTexcoord ).a;
Try breaking it down to:

Code:
vec4 tex = texture2D( gm_BaseTexture, v_vTexcoord );
if ( tex.a > 0.0 ) {
Alternatively, make sure your shader type is the default, GLSL ES (right click on the shader in the resource tree).
iirc the other two types are platform exclusive.
 
Last edited:

muddrox

Member
@Ido-f Thanks for your thorough response. shader_is_compiled(shd_solid_color) returned true. I also changed my code to look like the code snippet in your response but it produced the exact same effect, unfortunately. Here is some actual screen captures of whats happening on mac:
mac capture.png mac capture 2.png

Odd glitch to be certain. Any other ideas on what I can do to get this fixed? Thanks!
 
Can you post a screenshot without the particles?

I have a hunch that might be completely off base. I'm wondering if the problem is that you aren't providing a branch in the case that alpha == 0.

Try this and see if it makes a difference:

float tex_alpha = texture2D( gm_BaseTexture, v_vTexcoord ).a;
gl_FragColor = vec4(color.rgb, color.a * float(tex_alpha > 0.0) );

or

if (tex_alpha == 0.0) { discard; }
gl_FragColor = color;
 

muddrox

Member
@flyingsaucerinvasion Well, as it turns out, your hunch was right! Your solution fixed the problem perfectly and now the game runs like a dream on Mac. THANK YOU SO MUCH. Thank you, everyone, for your help! I would have never figured this out if it wasn't for your assistance and especially nice hunch flyingsaucer! Now how do I mark this issue as being solved lol?
 
You can edit the title of your post by adding [SOLVED] to it to mark it as such. Plus you can give a like to the person who solved your problem if you so wish :)
 
Top