0

I am using OpenGL ES 3.2 and the NVIDIA driver 361.00 on a Pixel C tablet with Tegra X1 GPU. I would like to use a compute shader to write data to a colour map and then later I will use some graphics shaders to display the image.

I already have this concept working using desktop GL and now I want to port to mobile. I am implementing the GL in Java rather than in native code. I extend GLSurfaceView and the GLSurfaceView.Renderer and then during the OnSurfaceCreated callback I initialise the shader programs and textures etc.

The compute shader compiles just fine without any errors:

#version 310 es
layout(binding = 0, rgba32f) uniform highp image2D colourMap;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;

void main()
{
    imageStore(colourMap, ivec2(gl_GlobalInvocationID.xy), vec4(1.0f, 0.0f, 0.0f, 1.0f));
}

And I initialise a texture

// Generate a 2D texture
GLES31.glGenTextures(1, colourMap, 0);
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, colourMap[0]);

// Set interpolation to nearest
GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_LINEAR);
GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_LINEAR);

// Create some dummy texture to begin with so we can see if it changes
float texData[] = new float[texWidth * texHeight * 4];
for (int j = 0; j < texHeight; j++)
{
    for (int i = 0; i < texWidth; i++)
    {
        // Set a few pixels in here...
    }
}
Buffer texDataBuffer = FloatBuffer.wrap(texData);
GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_RGBA32F, texWidth, texHeight, 0, GLES31.GL_RGBA, GLES31.GL_FLOAT, texDataBuffer);

After this I set the image unit in the shader here: EDIT: I don't do this now but just assume it will be assigned automatically when the shader program is created as per solidpixel's answer.

GLES31.glUseProgram(idComputeShaderProgram);
int loc = GLES31.glGetUniformLocation(idComputeShaderProgram, "colourMap");
if (loc == -1) Log.e("Error", "Cannot locate variable");
GLES31.glUniform1i(loc, 0);

After every call to GL I check for errors using GLES31.glGetError() -- left out here for clarity.

EDIT: When I dispatch compute I bind the image texture but first query the unit assignment:

GLES31.glUseProgram(idComputeShaderProgram);
int[] unit = new int[1];
GLES31.glGetUniformiv(idComputeShaderProgram, GLES31.glGetUniformLocation(idComputeShaderProgram, "colourMap"), unit, 0);
GLES31.glBindImageTexture(unit[0], velocityMap[0], 0, false, 0, GLES31.GL_WRITE_ONLY, GLES31.GL_RGBA32F);

This final line is the one which errors now. The error code translates to GL_INVALID_OPERATION. The shader compiles correctly and the program object is valid and active. The location of the variable is also valid. I have even used glGetActiveUniform() to get the type of the variable and it returns a type of 36941 which translates to GL_IMAGE_2D which I believe is an integer.

I still think I'm misunderstanding something here but not sure what.

4
  • Have the same problem on Samsung S7 Edge with Mali-T800 GPU, GLES 3.2 and Android 7.0. Mar 1, 2018 at 16:34
  • I am getting the same gl error now at the same line. Have you solved it?
    – Sung
    Jul 2, 2019 at 9:41
  • 1
    @Sung yes, I solved it using the answer below and the documentation I quoted in the comments to the answer relating to allocating textures. Jul 2, 2019 at 13:33
  • Yeap, it is solved by glTexStorage2D() + glTexSubImage2D() instead of glTexImage2D
    – Sung
    Jul 3, 2019 at 8:57

1 Answer 1

0

You can't assign your own unit identities for images. See OpenGL ES 3.2 specification section 7.6.

An INVALID_OPERATION error is generated if any of the following conditions occur:

  • an image uniform is loaded with any of the Uniform* commands.

You need to query the automatic unit assignment using glGetUniformiv(prog, loc, &unit) to get the unit name.

7
  • thank you for your answer. So when in the uniform assigned, when the compute shader program is created? How do I ensure that the image2D variable in the shader will access the texture I created -- do I just make sure I bind the texture to the correct target? A corrected code snippet might help? Mar 2, 2018 at 13:38
  • OK so I just deleted trying to assign a unit id to the image2D variable in the shader and instead, before calling glBindImageTexture, used your recommendation to get the unit number then used that unit number in the glBindImageTexture call. It still gives me a GL_INVALID_OPERATION error. Mar 2, 2018 at 13:53
  • I've made some edits to the question to reflect my changes. Mar 2, 2018 at 14:00
  • In terms of "self-help", does your device support the KHR_debug extensions? Android exposes this via Java bindings, and it normally gives much better error messages.
    – solidpixel
    Mar 2, 2018 at 14:34
  • In terms of reading the specification "GL_INVALID_OPERATION is generated if texture is not the name of an immutable texture object".
    – solidpixel
    Mar 2, 2018 at 14:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.