Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Shaders not working properly

This thread is locked; no one can reply to it. rss feed Print
Shaders not working properly
brisingr123
Member #15,673
July 2014

Hello everyone!

I was just messing around in my game, and I across a very strange behavior with shaders. Here is the code I was using, just for reference.

    AddShader("TEMP", "Assets\\shaders\\default.vert", "Assets\\shaders\\temp.frag");
    if (KBKeyDown(ALLEGRO_KEY_N))
        UseShader("TEMP");
    al_set_target_bitmap(al_get_backbuffer(GAME_DISPLAY));
    al_clear_to_color(blackColor);
    if (KBKeyDown(ALLEGRO_KEY_B))
        al_draw_scaled_bitmap(screenBitmap, SCREEN_WIDTH * (1 - scaleInverse) / 2, SCREEN_HEIGHT * (1 - scaleInverse) / 2, SCREEN_WIDTH * scaleInverse, SCREEN_HEIGHT * scaleInverse, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
    UseShader("NONE");

1, 2 and 3 are the images showing the outputs with B (not darwing the bitmap), with B but not N (drawing it without the shader), and B and N (drawing it with shader). The shader is a simple shader that sets every pixel to (1, 0, 0). So why this strange behaviour? What's more, if I try drawing another bitmap without changing the shader, it is still drawn in it's normal form - no shaders used. I seriously have no idea what the hell is going on now ???

EDIT : images are uploaded as attachments, sorry for that.

Thomas Fjellstrom
Member #476
June 2000
avatar

Have you checked to see if your shader calls succeed? Shaders can be a bit finicky.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

brisingr123
Member #15,673
July 2014

Since I'm assuming you didn't look at the attachment, yes, I'm pretty sure the call succeeded, since part of the bitmap is red.

Thomas Fjellstrom
Member #476
June 2000
avatar

What does your shader code look like? especially UseShader.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

brisingr123
Member #15,673
July 2014

void UseShader(std::string name)
{
    if (name == "NONE")
        al_use_shader(nullptr);
    else
        al_use_shader(shaderList[name]);
}

temp.frag

uniform sampler2D al_tex;
uniform bool al_use_tex;

varying vec4 tintColor;
varying vec2 texCoord;
varying vec2 fragPos;

void main()
{
    gl_FragColor = vec4(1, 0, 0, 1);
}

SiegeLord
Member #7,827
October 2006
avatar

Are you changing your target bitmap while changing shaders? The current shader (set by al_use_shader) is local to the target bitmap.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

brisingr123
Member #15,673
July 2014

Nope, I am not. For heaven's sake, one part of the bitmap (i'm sure it's all one bitmap, since not drawing it draws a blank screen) is being drawn red, and the other half is being drawn with the proper colors! That has to proof enough that I'm not messing up with the calls!

P.S. sorry if I sound exasperated, I've been staring at this code for so long trying to figure out why the shader is not working properly.

Elias
Member #358
May 2000

Can you show more code? Like, where are you calling al_flip_display?

--
"Either help out or stop whining" - Evert

Thomas Fjellstrom
Member #476
June 2000
avatar

Nope, I am not. For heaven's sake, one part of the bitmap (i'm sure it's all one bitmap, since not drawing it draws a blank screen) is being drawn red, and the other half is being drawn with the proper colors! That has to proof enough that I'm not messing up with the calls!

I don't know about that. You are calling al_set_target_bitmap if you changed it at all before the code you showed us, that could easily be causing some issues.

Can we see your main drawing code?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

brisingr123
Member #15,673
July 2014

The code before this just draws things to screenBitmap, and this is the final thing that draws the screenBitmap to my display. After this, I draw FPS, and flip the display (no more functions calls whatsoever to allegro, except one al_draw_text to draw the FPS).

"The current shader (set by al_use_shader) is local to the target bitmap."
Just curious, does that mean that if I use bitmap A, use shader A_Shader, change my bitmap to B, use a different shader B_Shader and finally switch back to A again, that will still have A_Shader active? I guess I somehow overlooked this technicality. If so, then that might be the cause of my problem. I'll just go ahead and try playing with my code again, with this assumption.

Thomas Fjellstrom
Member #476
June 2000
avatar

Try setting the shader after you set the target.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

brisingr123
Member #15,673
July 2014

Yeah, that did it, and now I'm feeling like a dumb ass for not realizing that earlier :-X

Btw, if I'm not wrong, the last time I used vanilla openGl, the shader was constant till the time you changed it - and not dependent on the target (been a long time since I used it, so please correct me if I'm wrong). So why does Allegro bind it's shaders to target bitmaps, as opposed to having them bound till they are replaced, irrespective of the target bitmaps?

SiegeLord
Member #7,827
October 2006
avatar

So why does Allegro bind it's shaders to target bitmaps, as opposed to having them bound till they are replaced, irrespective of the target bitmaps?

No particular reason. It seems convenient to have a particular shader associated with each intermediate bitmap... but perhaps not that convenient?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Go to: