How to get a binding point of an image variable in OpenGLES

风流意气都作罢 提交于 2019-12-02 03:59:15

问题


I am trying to obtain a binding point of an image variable in my GLES shader. I can do this for uniforms or shader storage blocks using that code:

GLenum Prop = GL_BUFFER_BINDING;
GLint Binding = -1;
GLint ValuesWritten = 0;
glGetProgramResourceiv( GLProgram, GL_UNIFORM_BLOCK, i, 1, &Prop, 1, &ValuesWritten, &Binding );

Unfortunately, there is no such thing as GL_IMAGE_BINDING. In desktop GL, I am just getting the location of the image uniform using GetUniformLocation and then bind it to an image slot using glProgramUniform1i. Unfortunately in OpenGLES, glProgramUniform1i can be used for sampler uniforms only and does not work for image uniforms. The reason I need this binding point is because I am doing automatic resource binding. My resources can be associated with a uniform variable name. I want them to be automatically assigned to the right image slot. That works fine so far for all resources except for images on GLES.


回答1:


The image unit is the value of the image uniform variable. While the ES 3.1 spec does not allow setting values for these variables with glProgramUniform1i(), there's nothing I can see that prevents you from getting the value that was set in the GLSL code with the binding=... layout qualifier.

This is based on section 7.10 "Images" of the spec. On page 112, it says:

The value of an image uniform is an integer specifying the image unit accessed.

and in the same section on page 113:

The location of an image variable is queried with GetUniformLocation, just like any uniform variable.

Therefore, to get the image unit for variable "MyImage" in program prog:

GLint loc = glGetUniformLocation(prog, "MyImage");
GLint imgUnit = -1;
glGetUniformiv(prog, loc, &imgUnit);

This gives you the image unit that the image variable in the shader is bound to. If you wanted the image name, you can continue the querying:

GLint imgName = -1;
glGetIntegeri_v(GL_IMAGE_BINDING_NAME, imgUnit, &imgName);


来源:https://stackoverflow.com/questions/27409680/how-to-get-a-binding-point-of-an-image-variable-in-opengles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!