Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
RenderTarget Shader wrong coordinates — Gideros Forum

RenderTarget Shader wrong coordinates

YanYan Member
edited March 2021 in Bugs and issues
There is a wrong detect of coordinates in Shader of Bitmap that uses RenderTarget. Ive write a simple shader that colors only a quarter of Bitmap, it works fine if I use Bitmap with Texture, but it fails if I use RenderTarget as Texture. To be clear I added project and GIF with explanation of this behavior.

As you can see, for different size RenderTarget proportions GLSL Shader detects 0.5 coordinate wrong. Why this happend and how to fix this ?

primitive Shader
	void main() {
		vec4 vertex = vec4(vVertex, 1.0);
		gl_Position = vMatrix*vertex;
		fTexCoord = vTexCoord;
	}
void main()
	{
		gl_FragColor = vec4(0.0, 0.0, 0.0, 0.5);
 
		if (fTexCoord.y > 0.5 && fTexCoord.x > 0.5)
		{	
			gl_FragColor = vec4(1.0, 0.5, 0.2, 0.5);
		}		
	}
image

video.gif
966 x 598 - 42K
zip
zip
RenderTargetShader.zip
3K
vk.com/yan_alex

Comments

  • i think it may be due to the shader texture being a power of two size.
    try a rendertarget of size that is a power of two, like 1024x1024. if that is fine, then this is the issue. there are some constants which can help you to get the right value even if it's not a power of two, @hgy29 knows better, he helped me out once with this, but i don't remember anymore the details (and it would take a lot of time to dig up my code now).
  • YanYan Member
    keszegh said:

    i think it may be due to the shader texture being a power of two size.
    try a rendertarget of size that is a power of two, like 1024x1024. if that is fine, then this is the issue. there are some constants which can help you to get the right value even if it's not a power of two, @hgy29 knows better, he helped me out once with this, but i don't remember anymore the details (and it would take a lot of time to dig up my code now).

    Yes, Ive already tried, and this is seems really problem of power of 2, I figured this out before started topic, but It still looks like a bug and I dont understand how to fix.

    Ive tried to multiply on ratio proportion coefficient, but I failed or this is not the solve.

    Dislikes: Yan

    vk.com/yan_alex
    +1 -1 (+0 / -1 )Share on Facebook
  • keszeghkeszegh Member
    edited March 2021
    it is not really a bug, just a technical limitation. under the hood every texture is of size power of two, and shader uses that.

    here is the computation i use to normalize:
    varying mediump vec2 fTexCoord;
    uniform mediump vec4 fTextureInfo;
     
    ....
     
    coord.y=(fTexCoord.y/fTextureInfo.y-0.5);	
    coord.x=(fTexCoord.x/fTextureInfo.x-0.5)/fTextureInfo.z*fTextureInfo.x*fTextureInfo.w/fTextureInfo.y;
    (perhaps it is overcomplicated, also it has those 0.5's as i wanted to put the (0,0) coordinate in the middle of my texture for symmetry reasons.)
  • hgy29hgy29 Maintainer
    Yes, as @keszegh said it is due to Gideros enlarging texture sizes so that they always have a power of two size, so the 'payload' of the texture doesn't always span for 0 to 1. This is something your shader code should be aware of, and it can be helped by the constant Gideros can provide to the shader. The x and y parts of this vector are the real extent of the texture.
  • YanYan Member
    edited March 2021
    hgy29 said:

    Yes, as @keszegh said it is due to Gideros enlarging texture sizes so that they always have a power of two size, so the 'payload' of the texture doesn't always span for 0 to 1. This is something your shader code should be aware of, and it can be helped by the constant Gideros can provide to the shader. The x and y parts of this vector are the real extent of the texture.

    What should I provide as a constant ? Im not quite really understand the solve

    Is this a part I need to provide ?


    shader:setConstant("fTexelSize",Shader.CFLOAT4,1,{1/texw,1/texh,0,0})
    and then multiply by fTexelSize.xy
    vk.com/yan_alex
  • hgy29hgy29 Maintainer
    Accepted Answer
    Just change your condition to:
    		if (fTexCoord.y > fTexSize.y/2.0 && fTexCoord.x > fTexSize.x/2.0)

    Likes: Yan

    +1 -1 (+1 / -0 )Share on Facebook
  • Doh, I realised everybody just use UV as coord / size
    vk.com/yan_alex
Sign In or Register to comment.