not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.06k stars 298 forks source link

Texture won't sent to a shader #697

Open optozorax opened 5 months ago

optozorax commented 5 months ago

I used this MRE:

use macroquad::prelude::*;

#[macroquad::main("Post processing")]
async fn main() {
    let material = load_material(
        ShaderSource::Glsl {
            vertex: CRT_VERTEX_SHADER,
            fragment: CRT_FRAGMENT_SHADER,
        },
        MaterialParams {
            textures: vec!["ferris_tex".to_string()],
            ..Default::default()
        }
    )
    .unwrap();

    let texture: Texture2D = load_texture("examples/ferris.png").await.unwrap();

    material.set_texture("ferris_tex", texture);

    loop {
        gl_use_material(&material);
        draw_rectangle(0., 0., screen_width(), screen_height(), WHITE);
        gl_use_default_material();

        next_frame().await;
    }
}

const CRT_FRAGMENT_SHADER: &str = r#"#version 100
precision lowp float;

varying vec4 color;
varying vec2 uv;

uniform sampler2D ferris_tex;

void main() {
    gl_FragColor = vec4(texture2D(ferris_tex, uv).rgb, 1.0);

}
"#;

const CRT_VERTEX_SHADER: &str = "#version 100
attribute vec3 position;
attribute vec2 texcoord;
attribute vec4 color0;

varying lowp vec2 uv;
varying lowp vec4 color;

uniform mat4 Model;
uniform mat4 Projection;

void main() {
    gl_Position = Projection * Model * vec4(position, 1);
    color = color0 / 255.0;
    uv = texcoord;
}
";

This example should draw ferris texture. But on current version it draws black screen.

Using git bisect I found that after this commit this example brokes.

vdrn commented 3 months ago

Fixing this requires a breaking change for both macroquad and miniquad:

In the meantime @optozorax for your example to work you need to use material.set_texture("ferris_tex", texture.clone()); (Don't worry, it won't actually clone the texture, just a smart pointer for it)