• 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!

Shaders Question about Texture Coordinates

R

Roscoe.Zhao

Guest
Hi,
I'm trying to make a shader where I need to get some RGB values from a texture input from a surface (actually it's an output of another shader)
I tried something like this:
Code:
varying vec4 v_vColour;
varying vec2 v_vTexcoord;

uniform sampler2D Texture;
// The 'texture surface'
// This is from another shader's output, like this: texture_set_stage(uTexture,surface_get_texture(sTexture));

void main() {
    gl_FragColor =  texture2D(Texture, v_vTexcoord);
}
I tried this and expected it will draw the 'texture surface', but it gives a black screen.
I tried to draw the texture surface using draw_surface_stretched() it's normal, not empty.
Maybe I need to somehow calculate a new texture coordinate for that texture surface?
I know I could use the uvs values and v_vTexcoord to calculate the corresponding texcoords if I'm drawing a sprite, but I don't know how to deal with surfaces.
I can't find a function similar to sprite_get_uvs() for surfaces

Any help would be appreciated
 
Hi. What are you drawing? And what are you drawing onto?

Surfaces don't have a get_uvs function. The left side of a surface always has x texture coordinate of 0, right is 1. The top side of a surface has y texture coordinate 0, bottom is 1.
 
Last edited:
R

Roscoe.Zhao

Guest
In that example, I'm drawing a surface to another surface
the two surfaces actually have same size.
I don't know why but the codes above gives a black screen..
 
R

Roscoe.Zhao

Guest
Code:
// In draw event

surface_set_target(sTexture);
shader_set(shd_initial);
shader_set_uniform_f(shader_get_uniform(shd_initial,"D"),D);
draw_surface(sTexture,0,0);
shader_reset();
surface_reset_target();

// If I draw sTexture here, and comment-out the rest of code, it will draw sTexture surface normally.

surface_set_target(sUpdate);
shader_set(shd_update);
texture_set_stage(sUpdate,surface_get_texture(sTexture));
draw_surface(sUpdate,0,0);
shader_reset();
surface_reset_target();

draw_surface_stretched(sUpdate,0,0,640,480); // Draws black screen
 
You're not supposed to draw surfaces onto themselves. Not sure on the technical details, but gpus apparently have problems sampling textures they are simultaneously writing to. Or something along those lines.


I don't know if that is the cause of your problem, since as you say sTexture will show up.

EDIT:

texture_set_stage(sUpdate,surface_get_texture(sTexture));

sUpdate is not a sampler index, is it?

Look up shader_get_sampler_index
 
Last edited:
R

Roscoe.Zhao

Guest
But actually I'll need to continuously update that surface,
Can I draw it to a temporary one, then use surface_copy() to copy it back to that surface?
 
R

Roscoe.Zhao

Guest
You're not supposed to draw surfaces onto themselves. Not sure on the technical details, but gpus apparently have problems sampling textures they are simultaneously writing to. Or something along those lines.


I don't know if that is the cause of your problem, since as you say sTexture will show up.

EDIT:

texture_set_stage(sUpdate,surface_get_texture(sTexture));

sUpdate is not a sampler index, is it?

Look up shader_get_sampler_index
Thank you very much! Solved it!
 
Top