Shaders not working properly
brisingr123

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

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

brisingr123

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

What does your shader code look like? especially UseShader.

brisingr123
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

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

brisingr123

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

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

Thomas Fjellstrom

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?

brisingr123

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

Try setting the shader after you set the target.

brisingr123

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

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?

Thread #615075. Printed from Allegro.cc