andersch.dev

<2024-07-05 Fri>

Render pass

A render pass is a term used in the context of multipass rendering techniques. A render pass takes a series of attachments (such as color, depth or stencil buffers) and forms operations as defined by a pipeline and a series of commands and outputs the rendered images to a frame buffer. It typically includes operations such as clearing the frame buffer, setting up render targets, binding shaders, and drawing geometry.

Example of a Render pass in OpenGL

/* 1. Create and bind a framebuffer object (FBO) */
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

/* 2) Attach color and depth/stencil buffers to the FBO */
GLuint colorTexture;
glGenTextures(1, &colorTexture);
glBindTexture(GL_TEXTURE_2D, colorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);

GLuint depthBuffer;
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

/* 3) Set up the viewport and clear the buffers */
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* 4) Bind shaders, set uniforms, and draw geometry */
glUseProgram(shaderProgram);
/* Set uniforms */
/* Draw geometry */

/* 5) End render pass and unbind the framebuffer */
glBindFramebuffer(GL_FRAMEBUFFER, 0);