Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL ES 2.x: Bind both `GL_TEXTURE_2D` and `GL_TEXTURE_CUBE_MAP` in the same texture image unit?

What happens if you bind (different textures) to both GL_TEXTURE_2D and GL_TEXTURE_CUBE_MAP in the same texture image unit?

For example, suppose I bind one texture to GL_TEXTURE0's GL_TEXTURE_2D target and another texture to the same texture unit's GL_TEXTURE_CUBE_MAP target. Can I then have two uniform variables, one a sampler2D and the other a samplerCube and set both to 0 (to refer to GL_TEXTURE0)?

I suspect the answer is "no" (or that the result is undefined) but I haven't found anything in the spec that specifically prohibits using multiple texture targets in the same texture image unit.

like image 668
Laurence Gonsalves Avatar asked Mar 31 '12 00:03

Laurence Gonsalves


People also ask

What is OpenGL es texture?

The texture is an OpenGL object that contains one or more images that all have the same image format. To draw texture in a square shape, we need to tell each vertex of the square which part of the texture it corresponds to. So each vertex of the shape has a texture coordinate associated with them.

How to apply texture on cube OpenGL?

Load the file from your system and convert it into a format OpenGL ES can understand. Generate a texture object and activate the desired texture unit. Load the texture data into the texture unit using glTexImage2D. Set the sampling methods with glTexParameteri.

How does glTexImage2D work?

glteximage2d is the function that actually moves the texture data across to the gpu. you will need to create the texture object first using glGenTextures() and then bind it using glBindTexture(). you can then use this texture with a VBO.

How many texture units are provided by OpenGL?

OpenGL 3. x defines the minimum number for the per-stage limit to be 16, so hardware cannot have fewer than 16 textures-per-stage. It can have a higher limit, but you know you get at least 16.


2 Answers

I haven't found anything that describes if you can bind a 2D texture and a cube map texture in the same texture unit, but (or so) I guess this is perfectly possible. It makes sense to allow it, since all texture modification functions require you to specify the texture target to operate on, anyway.

But the OpenGL ES 2 spec explicitly disallows to use both at the same time in a shader, as chapter 2.10 says:

It is not allowed to have variables of different sampler types pointing to the same texture image unit within a program object. This situation can only be detected at the next rendering command issued, and an INVALID_OPERATION error will then be generated.

So you cannot use both a sampler2D and a samplerCube referring to the same texture unit to bend your implementation's texture unit limits.

like image 94
Christian Rau Avatar answered Dec 10 '22 09:12

Christian Rau


For Chrome, I'm getting an error trying to perform such an operation.

var gl = document.getElementById("canv00").getContext("webgl");
const texture = gl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, texture)
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture)
gl.getParameter(gl.TEXTURE_BINDING_2D) // texture
gl.getParameter(gl.TEXTURE_BINDING_CUBE_MAP) // null
gl.getError()  // returns 1282 error

var gl = document.getElementById("canv00").getContext("webgl");
const texture = gl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, texture)
// gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture)
gl.getParameter(gl.TEXTURE_BINDING_2D) // texture
gl.getParameter(gl.TEXTURE_BINDING_CUBE_MAP) // null
gl.getError()  // no error

var gl = document.getElementById("canv00").getContext("webgl");
const texture = gl.createTexture()
// gl.bindTexture(gl.TEXTURE_2D, texture)
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture)
gl.getParameter(gl.TEXTURE_BINDING_2D) // null
gl.getParameter(gl.TEXTURE_BINDING_CUBE_MAP) // texture
gl.getError()  // no error
like image 27
NateW Avatar answered Dec 10 '22 07:12

NateW