1

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?

2
  • What are you trying to do? You show the setup code for a framebuffer but the question seems to be about how to get a texture working. These two things have nothing to do with each other. Commented Dec 12, 2016 at 10:02
  • @BDL I am trying to input the CPU float array data to my fragment shader, but my set up does not work. I showed my FBO code because it gives me GL_INCOMPLETE_ATTACHMENT when I tried changed the internal format to GL_R32F, format to GL_RED, and type to GL_FLOAT. The above codes work without any error but still I get a black screen on my iOS device. Commented Dec 13, 2016 at 2:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry for my lack of explanations, but I have set position as attribute variable but I forgot to show it. This is in my c file: _aPosition = glGetAttribLocation(_program, "aPosition"); and in my vsh. : attribute vec2 aPosition; void main(void) { gl_Position = vec4(aPosition, 0., 1.); } I have also changed the CPU input data to malloc(WHsizeof(float)), and internal format to GL_R32F, format to GL_RED and type GL_FLOAT. However I still get a GL_INCOMPLETE_ATTACHMENT error. Do you have any suggestions on how to fix the issue?Many thanks for the reply!
Floating point render targets are not part of the core ES 3.0 spec, so it may be that the GPU you are using doesn't support this.
Note your question only talks about textures, but goes to a lot of effort to set up a render target too - the two are not the same thing. You can read a floating point texture (this is core in ES3.0) without it being part of an FBO; you just can't render into one.
thanks for your comments. I have managed to solve the issue now. I made a dumb mistake by setting CPU memory to be 1 channel, not 4. I fixed by using malloc(4*WHsizeof(float)). Now using your suggested format gives no error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.