Skip to content

Commit

Permalink
Fallback for if ARB_depth_buffer_float is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
aftersomemath committed Oct 3, 2023
1 parent c6961b7 commit b1e6728
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/render/render_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,13 @@ static void makeOff(mjrContext* con) {
mju_error("Could not allocate offscreen depth and stencil buffer");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil);

GLenum depth_buffer_format = mjGLAD_GL_ARB_depth_buffer_float ? GL_DEPTH32F_STENCIL8 : GL_DEPTH24_STENCIL8;
if (con->offSamples) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, GL_DEPTH32F_STENCIL8,
glRenderbufferStorageMultisample(GL_RENDERBUFFER, con->offSamples, depth_buffer_format,
con->offWidth, con->offHeight);
} else {
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, con->offDepthStencil);
Expand Down Expand Up @@ -1169,7 +1171,7 @@ static void makeOff(mjrContext* con) {
mju_error("Could not allocate offscreen depth and stencil buffer_r");
}
glBindRenderbuffer(GL_RENDERBUFFER, con->offDepthStencil_r);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, con->offWidth, con->offHeight);
glRenderbufferStorage(GL_RENDERBUFFER, depth_buffer_format, con->offWidth, con->offHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, con->offDepthStencil_r);

Expand Down Expand Up @@ -1489,9 +1491,6 @@ void mjr_makeContext_offSize(const mjModel* m, mjrContext* con, int fontscale,
if (!mjGLAD_GL_ARB_vertex_buffer_object) {
mju_error("OpenGL ARB_vertex_buffer_object required");
}
if (!mjGLAD_GL_ARB_depth_buffer_float) {
mju_error("OpenGL ARB_depth_buffer_float required");
}
con->glInitialized = 1;

// determine window availability (could be EGL-headless)
Expand Down
14 changes: 10 additions & 4 deletions src/render/render_gl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ void mjr_readPixels(unsigned char* rgb, float* depth,
int N_pixels = viewport.width * viewport.height;
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
}
else if (!mjGLAD_GL_ARB_clip_control) {
mju_warning("ARB_clip_control unavailable while mjDEPTH_ZEROFAR requested, depth accuracy will be limited");
if (con->readDepthMap == mjDEPTH_ZEROFAR && !mjGLAD_GL_ARB_clip_control) {
mju_warning("mjDEPTH_ZEROFAR requested but ARB_clip_control unavailable, depth accuracy will be limited");
}
if (con->readDepthMap == mjDEPTH_ZEROFAR) {
mju_warning("mjDEPTH_ZEROFAR requested but window depth buffer precision may be limited");
}
}
}
Expand Down Expand Up @@ -170,8 +173,11 @@ void mjr_readPixels(unsigned char* rgb, float* depth,
int N_pixels = viewport.width * viewport.height;
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
}
else if (!mjGLAD_GL_ARB_clip_control) {
mju_warning("ARB_clip_control unavailable while mjDEPTH_ZEROFAR requested, depth accuracy will be limited");
if (con->readDepthMap == mjDEPTH_ZEROFAR && !mjGLAD_GL_ARB_clip_control) {
mju_warning("mjDEPTH_ZEROFAR requested but ARB_clip_control unavailable, depth accuracy will be limited");
}
if (con->readDepthMap == mjDEPTH_ZEROFAR && !mjGLAD_GL_ARB_depth_buffer_float) {
mju_warning("mjDEPTH_ZEROFAR requested but ARB_depth_buffer_float unavailable, depth accuracy will be limited");
}
}

Expand Down

0 comments on commit b1e6728

Please sign in to comment.