Direct State AccessGL 4.5

glNamedFramebufferTexture

Attaches a texture to a framebuffer, so drawing into the framebuffer writes into the texture.

Signature

void glNamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
                               GLuint texture, GLint level);

How it works

This is how render-to-texture works: attach a texture as GL_COLOR_ATTACHMENT0, draw, then sample that texture in a later pass (post-processing, shadow maps, picking IDs).

There is no texture target parameter: the function takes the type from the texture. For a cubemap or an array texture it attaches every layer at once (a layered attachment, for a geometry shader that picks gl_Layer); to attach a single layer or face use glNamedFramebufferTextureLayer.

Parameters

framebufferGLuint

The framebuffer (not 0).

attachmentGLenum

Where to attach.

Common values

GL_COLOR_ATTACHMENTi
Colour output number i (GL_COLOR_ATTACHMENT0, 1, …).
GL_DEPTH_ATTACHMENT
The depth buffer.
GL_STENCIL_ATTACHMENT
The stencil buffer.
GL_DEPTH_STENCIL_ATTACHMENT
Both at once, for a GL_DEPTH24_STENCIL8 image.
textureGLuint

The texture, or 0 to detach.

levelGLint

Mip level to render into, usually 0.

Tips & pitfalls

  • Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.

  • Do not sample a texture in the same draw that renders into it: the result is undefined (a feedback loop).

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_OPERATION
framebuffer is 0 or does not exist, or texture is not a valid texture.

Example

example.cpp
1glNamedFramebufferTexture(fbo, GL_COLOR_ATTACHMENT0, colorTex, 0);
2glNamedFramebufferTexture(fbo, GL_DEPTH_ATTACHMENT,  depthTex, 0);
Official Khronos documentation