Hello,
I’m a beginner in programming GPU with opengl es 2.0 and I’m trying to use my GPU to accelerate calculation. The purpose is to develop a program that takes for example a matrix of values, performs some calculation with these values and then returns back the results. I have written a program that takes a matrix and defines it as a texture (width,height) using “glTexture2D”. Then, it applies this texture to a rectangle and finally draws the result in a window defined by “glViewPort(0,0, width, height)”. When I read the results using “glReadPixels”, I’m waiting to see the same values that I have injected the first time as texture. However, I get another result. When I have added “glDisable(GL_DITHER)”, I have got better results but not what I want.
For example, when I insert a matrix as an RGBA texture containing values like that:
[51 51 51 255 51 51 51 255 …]
[51 51 51 255 51 51 51 255 …]
I get this result:
[49 52 49 255 49 52 49 255 …]
[49 52 49 255 49 52 49 255 …]
It looks like it is making a sampling algorithm. So, I wonder if there is a way to fix this problem and get the good result.
Here I give you the code of vertex shader and fragment shader:
Vertex shader:
precision highp float;
attribute vec4 av4position;
attribute vec2 av2texcoord;
varying vec2 vv2texcoord;
void main()
{
vv2texcoord = av2texcoord;
gl_Position = av4position;
}
Fragment shader:
precision highp float;
varying vec2 vv2texcoord;
uniform sampler2D texture;
void main()
{
vec4 color = texture2D(texture,vv2texcoord);
gl_FragColor = color;
}
Thanks.
[ul]
[li] OpenGL ES does not require the fragment shader to support high precision, only medium and low.
[/li][li] High precision has a relative error of 2-16.
[/li][li] Medium precision has a relative error of 2-10.
[/li][li] Textures aren’t required to use 8 bits per component; 5:6:5, 5:5:5 and 4:4:4:4 are all acceptable (and fairly common on devices using OpenGL ES). There isn’t even a way to request an 8:8:8 texture without using extensions. So values stored in textures may have a relative error of 2-4.
[/li][/ul]
In short, OpenGL ES isn’t particularly suited for GPGPU computing.
Thank you for your answer,
Actually, what I did is that I inserted the matrix as texture in the format of GL_RGBA and the type GL_UNSIGNED_BYTE using “glTexture2D”. Then I used the same format and type when reading data from framebuffer with “glReadPixels”. So, what are you saying is there is no way to get exactly the same values that I have injected as texture?
[QUOTE=Lfahem;1287254]I’m a beginner in programming GPU with opengl es 2.0 and I’m trying to use my GPU to accelerate calculation.
The purpose is to develop a program that takes for example a matrix of values, performs some calculation with these values and then returns back the results. … read the results using “glReadPixels”[/QUOTE]
To add more wood to that fire, you did say you wanted to accelerate calculation. Mobile GPUs run the fragment pipe a frame late. If you’re VSynced to 60Hz, then it could be 16-32ms or more before you can get your result back. That’s a long time compared to how long it’d take you to compute this on the CPU. Further, a readback is going to trigger a full pipeline flush on the GPU which is horribly inefficient on mobile and can generate rendering artifacts under some configurations.
[QUOTE=Lfahem;1287258]Thank you for your answer,
Actually, what I did is that I inserted the matrix as texture in the format of GL_RGBA and the type GL_UNSIGNED_BYTE using “glTexture2D”. Then I used the same format and type when reading data from framebuffer with “glReadPixels”. So, what are you saying is there is no way to get exactly the same values that I have injected as texture?[/QUOTE]
From the documentation:
type may be used as a hint to specify how much precision is desired, but a GL implementation may choose to store the texture array at any internal resolution it chooses.
Bottom-line - even though you specify GL_RGBA/GL_UNSIGNED_BYTE, your GL ES implementation is allowed to convert that down to a lower-precision type. You’ve chosen an inappropriate tool for the job you want to do; sorry.
Thank you for your answers,
I think I don’t have a chance. In fact the GPU that I have supports only openGL es 2.0 et openVG 1.1. So, it is made only for graphics processing not for making general purpose calculations.