Reputation: 11
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?
Upvotes: 1
Views: 1294
Reputation: 12069
A texture is a 2D array (i.e. a single contiguous block of memory of columns and rows). You're uploading a list of 1D arrays, which are stored scattered across memory because they are each dynamically allocate separately. No idea what you are actually managing to upload here, but you're lucky it doesn't just crash.
The RGBA8 textures which are you are trying to upload store 4 separate byte values between 0 and 255 per texel (one for red, one for blue, etc). You're trying to upload a float per pixel, which therefore is in totally the wrong data format.
Your uniform for "position" is going to be constant for every pixel, so every pixel is going to look up from the same texture coordinate, which probably isn't what you wanted. You need to upload your texture coordinates as a per-vertex varying value.
Honestly there are many fundamental mistakes here - I'd suggest starting with a basic "texture a triangle" tutorial for iOS, there are plenty on the web.
Upvotes: 1