I am trying to do some GPGPU programming on iOS devices, but I cant find any resources on how to set up the openGL ES 3.0 environment. I understand how to do calculations in the fragment shader but I cannot pass my float array to a uniform sampler variable to the fragment shader.
This is how I set up the FBO and allocate CPU memory:
float** dens = (float**) malloc(W*sizeof(float*));
for (int i = 0; i < W; i++) {
dens[i] = (float *)malloc(H*sizeof(float));
}
// input value
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
dens[i][j] = 1.0f;
}
}
GLuint FBO;
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
GLuint dens_gl = load_texture(W, H, GL_RGBA, 0, dens);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Here is my code for load_texture:
GLuint load_texture(const GLsizei width, const GLsizei height, const GLenum type, int tex_number, const float** data) {
GLuint texture_object_id;
glGenTextures(1, &texture_object_id);
assert(texture_object_id != 0);
glActiveTexture(GL_TEXTURE0 + tex_number);
glBindTexture(GL_TEXTURE_2D, texture_object_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, data);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + tex_number, GL_TEXTURE_2D, texture_object_id, 0);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
NSLog(@"failed to make complete framebuffer objects %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
glBindTexture(GL_TEXTURE_2D, 0);
return texture_object_id;
}
and my fragmentshader.fsh:
uniform vec2 uResolution;
uniform vec2 aPosition;
uniform sampler2D dens_gl;
void main(void) {
vec2 uv = aPosition.xy / uResolution.xy;
gl_FragColor = texture2D(dens_gl, uv);
}
There are no error code, but I can only see black screen and we are expecting white screen due to I set the value to 1 in CPU memory. Where did I go wrong?