SFML community forums

Help => Graphics => Topic started by: taifun93 on February 16, 2022, 10:49:40 am

Title: [SOLVED] Using shaders with sfml
Post by: taifun93 on February 16, 2022, 10:49:40 am
Hi!

I want to use this shader (https://github.com/libretro/glsl-shaders/blob/master/xbr/shaders/xbr-mlv4-multipass/xbr-mlv4-pass1.glsl) but all i get is a black image. I know the texture is loaded correctly because i can render it without the shader.

This is how i load and use the shader, which is probably not correct.

void init() {
        shader.loadFromFile("glsl-shaders-master/xbr/shaders/xbr-mlv4-multipass/xbr-mlv4-pass1.glsl", sf::Shader::Vertex);
        shader.loadFromFile("glsl-shaders-master/xbr/shaders/xbr-mlv4-multipass/xbr-mlv4-pass1.glsl", sf::Shader::Fragment);
        shader.setUniform("MVPmatrix", sf::Glsl::Mat4(bg_sprite.getTransform()));
        shader.setUniform("FrameDirection", 1);
        shader.setUniform("FrameCount", 100);
        shader.setUniform("OutputSize", sf::Vector2f(500, 500));
        shader.setUniform("TextureSize", sf::Vector2f(500, 500));
        shader.setUniform("InputSize", sf::Vector2f(500, 500));
        shader.setUniform("Texture", sf::Shader::CurrentTexture);

        //more code
}

void draw() {
        window.draw(bg_sprite, &shader);

        //more code
}

This are the errors i get:

Uniform "MVPmatrix" not found in shader
Uniform "FrameDirection" not found in shader
Uniform "FrameCount" not found in shader
Uniform "OutputSize" not found in shader
Uniform "TextureSize" not found in shader
Uniform "InputSize" not found in shader
Uniform "Texture" not found in shader
Title: Re: Using shaders with sfml
Post by: Stauricus on February 16, 2022, 11:32:05 am
I'm a complete ignorant in shaders, but have you tried anothe one, just for testing? I get this error:

Quote
Failed to compile vertex shader:
0:368(1): error: syntax error, unexpected end of file
Title: Re: Using shaders with sfml
Post by: taifun93 on February 16, 2022, 12:44:50 pm
I'm a complete ignorant in shaders, but have you tried anothe one, just for testing? I get this error:

Quote
Failed to compile vertex shader:
0:368(1): error: syntax error, unexpected end of file

I could use this simple shader which is not useful to me, it only paints some colors on top of everything. But other shaders that i downloaded do not seem to work.


uniform vec2 u_resolution;

void main() {
    vec2 st = gl_FragCoord.st/u_resolution;
    gl_FragColor = vec4(st.x, st.y, 0.0, 1);
}
 

        shader.loadFromFile("rainbow.glsl", sf::Shader::Fragment);
        shader.setUniform("u_resolution", sf::Glsl::Vec2{ 100, 100 });*/
 

Title: Re: Using shaders with sfml
Post by: eXpl0it3r on February 16, 2022, 01:19:29 pm
Since the linked shader has a big #if around the main body, I wonder whether the version check fails and thus you get no main() function which the compiler expects and as it doesn't find it until the end of the file, it errors with said message.
Title: Re: Using shaders with sfml
Post by: Stauricus on February 16, 2022, 01:49:26 pm
Since the linked shader has a big #if around the main body, I wonder whether the version check fails and thus you get no main() function which the compiler expects and as it doesn't find it until the end of the file, it errors with said message.

agreed. thats also why its not finding the variables.
Title: Re: Using shaders with sfml
Post by: taifun93 on February 18, 2022, 03:03:49 pm
I made some progress. If i add this line the the shader file i get a green texture. I also dropped all the setUniform() calls from my program as they seem to do nothing.

#define FRAGMENT
Title: Re: Using shaders with sfml
Post by: Stauricus on February 18, 2022, 04:08:56 pm
well, 'setuniform' is used to pass variables from SFML to the shader...  :P
Title: Re: Using shaders with sfml
Post by: taifun93 on February 19, 2022, 02:09:54 pm
I finally got it working! I had to separate the shader in two different files, one for the vertex shader and one for the fragment shader.

I also had to feed OutputSize, TextureSize, and InputSize uniform variables to the verex shader. Then in the main() function of the vertex shader i added these lines and delete their previous declarations.

        mat4 MVPMatrix = gl_ModelViewProjectionMatrix;
        vec4 VertexCoord = gl_Vertex;
        vec4 TexCoord = gl_TextureMatrix[0] * gl_MultiTexCoord0;