Skip to content

Instantly share code, notes, and snippets.

@jdolan
Created January 10, 2023 14:29
Show Gist options
  • Save jdolan/a9918543b85210b4c81d0a0d26967178 to your computer and use it in GitHub Desktop.
Save jdolan/a9918543b85210b4c81d0a0d26967178 to your computer and use it in GitHub Desktop.
Decoupling renderer framerate from occlusion query framerate.
/**
* @brief Updates and re-draws active occlusion queries for the current frame.
*/
void R_UpdateOcclusionQueries(r_view_t *view) {
if (!r_occlude->integer) {
return;
}
if (view->flags & VIEW_FLAG_NO_DELTA) {
view->num_occlusion_queries = r_occlusion.num_queries = 0;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glUseProgram(r_depth_pass_program.name);
glBindVertexArray(r_occlusion.vertex_array);
glEnableVertexAttribArray(r_depth_pass_program.in_position);
glBindBuffer(GL_ARRAY_BUFFER, r_occlusion.vertex_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, r_occlusion.elements_buffer);
const GLsizei size = sizeof(r_occlusion.vertexes[0]) * r_occlusion.num_queries;
glBufferSubData(GL_ARRAY_BUFFER, 0, size, r_occlusion.vertexes);
for (int32_t i = 0; i < view->num_occlusion_queries; i++) {
r_occlusion_query_t *q = view->occlusion_queries[i];
if (!q->available) {
glGetQueryObjectiv(q->name, GL_QUERY_RESULT_AVAILABLE, &q->available);
if (q->available || r_occlude->integer == 2) {
glGetQueryObjectiv(q->name, GL_QUERY_RESULT, &q->result);
}
}
if (q->available) {
glBeginQuery(GL_ANY_SAMPLES_PASSED, q->name);
glDrawElementsBaseVertex(GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL, q->index * 8);
glEndQuery(GL_ANY_SAMPLES_PASSED);
q->available = 0;
}
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
R_GetError(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment