diff --git a/util/test/demos/CMakeLists.txt b/util/test/demos/CMakeLists.txt
index b8d8b98a4..d3763211b 100644
--- a/util/test/demos/CMakeLists.txt
+++ b/util/test/demos/CMakeLists.txt
@@ -80,6 +80,7 @@ set(OPENGL_SRC
gl/gl_overlay_test.cpp
gl/gl_parameter_zoo.cpp
gl/gl_per_type_tex_units.cpp
+ gl/gl_queries_in_use.cpp
gl/gl_resource_lifetimes.cpp
gl/gl_runtime_bind_prog_to_pipe.cpp
gl/gl_separable_geometry_shader.cpp
diff --git a/util/test/demos/demos.vcxproj b/util/test/demos/demos.vcxproj
index 35304baa4..52f79ce1b 100644
--- a/util/test/demos/demos.vcxproj
+++ b/util/test/demos/demos.vcxproj
@@ -236,6 +236,7 @@
+
diff --git a/util/test/demos/demos.vcxproj.filters b/util/test/demos/demos.vcxproj.filters
index 8a3a5e558..24018379f 100644
--- a/util/test/demos/demos.vcxproj.filters
+++ b/util/test/demos/demos.vcxproj.filters
@@ -553,6 +553,9 @@
D3D11\demos
+
+ OpenGL\demos
+
diff --git a/util/test/demos/gl/gl_queries_in_use.cpp b/util/test/demos/gl/gl_queries_in_use.cpp
new file mode 100644
index 000000000..65febf717
--- /dev/null
+++ b/util/test/demos/gl/gl_queries_in_use.cpp
@@ -0,0 +1,159 @@
+/******************************************************************************
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019-2020 Baldur Karlsson
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ ******************************************************************************/
+
+#include "3rdparty/fmt/core.h"
+#include "gl_test.h"
+
+RD_TEST(GL_Queries_In_Use, OpenGLGraphicsTest)
+{
+ static constexpr const char *Description =
+ "Tests that we can still fetch mesh output and queries even when the capture itself makes "
+ "uses of those features.";
+
+ int main()
+ {
+ // initialise, create window, create context, etc
+ if(!Init())
+ return 3;
+
+ GLuint vao = MakeVAO();
+ glBindVertexArray(vao);
+
+ GLuint vb = MakeBuffer();
+ glBindBuffer(GL_ARRAY_BUFFER, vb);
+ glBufferStorage(GL_ARRAY_BUFFER, sizeof(DefaultTri), DefaultTri, 0);
+
+ ConfigureDefaultVAO();
+
+ GLuint program = MakeProgram();
+
+ GLuint vs = glCreateShader(GL_VERTEX_SHADER);
+ {
+ const char *cstr = GLDefaultVertex.c_str();
+ glShaderSource(vs, 1, &cstr, NULL);
+ glCompileShader(vs);
+ }
+
+ GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
+ {
+ const char *cstr = GLDefaultPixel.c_str();
+ glShaderSource(fs, 1, &cstr, NULL);
+ glCompileShader(fs);
+ }
+
+ glAttachShader(program, vs);
+ glAttachShader(program, fs);
+
+ GLchar *posOnly = "gl_Position";
+ glTransformFeedbackVaryings(program, 1, &posOnly, GL_INTERLEAVED_ATTRIBS);
+
+ glLinkProgram(program);
+
+ glDetachShader(program, vs);
+ glDetachShader(program, fs);
+ glDeleteShader(vs);
+ glDeleteShader(fs);
+
+ GLint status;
+ char buffer[1024];
+ glGetProgramiv(program, GL_LINK_STATUS, &status);
+ if(status == 0)
+ {
+ glGetProgramInfoLog(program, 1024, NULL, buffer);
+ TEST_ERROR("Link error: %s", buffer);
+
+ glDeleteProgram(program);
+ program = 0;
+ }
+
+ GLuint xfb;
+ glGenTransformFeedbacks(1, &xfb);
+
+ GLuint xfbBuffer = MakeBuffer();
+ glNamedBufferDataEXT(xfbBuffer, 1024, NULL, GL_DYNAMIC_READ);
+ glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfbBuffer);
+
+ GLuint queries[4];
+ glGenQueries(4, queries);
+
+ while(Running())
+ {
+ float col[] = {0.2f, 0.2f, 0.2f, 1.0f};
+ glClearBufferfv(GL_COLOR, 0, col);
+
+ glBindVertexArray(vao);
+
+ glUseProgram(program);
+
+ glViewport(0, 0, GLsizei(screenWidth), GLsizei(screenHeight));
+
+ glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, xfb);
+ glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, xfbBuffer);
+ glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, queries[0]);
+
+ glBeginTransformFeedback(GL_TRIANGLES);
+
+ setMarker("XFB Draw");
+ glDrawArrays(GL_TRIANGLES, 0, 3);
+
+ glEndTransformFeedback();
+ glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
+
+ GLuint primsWritten = 0;
+ glGetQueryObjectuiv(queries[0], GL_QUERY_RESULT, &primsWritten);
+
+ Vec4f vert;
+ glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(Vec4f), &vert);
+
+ setMarker(fmt::format("XFBResult: {} prims, first vert {},{},{},{}", primsWritten, vert.x,
+ vert.y, vert.z, vert.w));
+
+ setMarker("Counters Draw");
+ glBeginQuery(GL_CLIPPING_OUTPUT_PRIMITIVES_ARB, queries[1]);
+ glBeginQuery(GL_VERTEX_SHADER_INVOCATIONS_ARB, queries[2]);
+ glBeginQuery(GL_FRAGMENT_SHADER_INVOCATIONS_ARB, queries[3]);
+
+ glDrawArrays(GL_TRIANGLES, 0, 3);
+
+ glEndQuery(GL_CLIPPING_OUTPUT_PRIMITIVES_ARB);
+ glEndQuery(GL_VERTEX_SHADER_INVOCATIONS_ARB);
+ glEndQuery(GL_FRAGMENT_SHADER_INVOCATIONS_ARB);
+
+ glGetQueryObjectuiv(queries[1], GL_QUERY_RESULT, &primsWritten);
+ glGetQueryObjectuiv(queries[2], GL_QUERY_RESULT, &vs);
+ glGetQueryObjectuiv(queries[3], GL_QUERY_RESULT, &fs);
+
+ setMarker(fmt::format("CounterResult: {} prims, {} vs {} fs", primsWritten, vs, fs));
+
+ Present();
+ }
+
+ glDeleteTransformFeedbacks(1, &xfb);
+ glDeleteQueries(4, queries);
+
+ return 0;
+ }
+};
+
+REGISTER_TEST();
diff --git a/util/test/tests/GL/GL_Queries_In_Use.py b/util/test/tests/GL/GL_Queries_In_Use.py
new file mode 100644
index 000000000..9e956fd7b
--- /dev/null
+++ b/util/test/tests/GL/GL_Queries_In_Use.py
@@ -0,0 +1,80 @@
+import renderdoc as rd
+import rdtest
+
+
+class GL_Queries_In_Use(rdtest.TestCase):
+ demos_test_name = 'GL_Queries_In_Use'
+
+ def check_capture(self):
+ last_draw: rd.DrawcallDescription = self.get_last_draw()
+
+ self.controller.SetFrameEvent(last_draw.eventId, True)
+
+ tex_details = self.get_texture(last_draw.copyDestination)
+
+ draw = self.find_draw("XFB Draw").next
+
+ self.controller.SetFrameEvent(draw.eventId, False)
+
+ postvs_data = self.get_postvs(draw, rd.MeshDataStage.VSOut, 0, draw.numIndices)
+
+ postvs_ref = {
+ 0: {
+ 'vtx': 0,
+ 'idx': 0,
+ 'gl_Position': [-0.5, -0.5, 0.0, 1.0],
+ 'v2f_block.pos': [-0.5, -0.5, 0.0, 1.0],
+ 'v2f_block.col': [0.0, 1.0, 0.0, 1.0],
+ 'v2f_block.uv': [0.0, 0.0, 0.0, 1.0],
+ },
+ 1: {
+ 'vtx': 1,
+ 'idx': 1,
+ 'gl_Position': [0.0, 0.5, 0.0, 1.0],
+ 'v2f_block.pos': [0.0, 0.5, 0.0, 1.0],
+ 'v2f_block.col': [0.0, 1.0, 0.0, 1.0],
+ 'v2f_block.uv': [0.0, 1.0, 0.0, 1.0],
+ },
+ 2: {
+ 'vtx': 2,
+ 'idx': 2,
+ 'gl_Position': [0.5, -0.5, 0.0, 1.0],
+ 'v2f_block.pos': [0.5, -0.5, 0.0, 1.0],
+ 'v2f_block.col': [0.0, 1.0, 0.0, 1.0],
+ 'v2f_block.uv': [1.0, 0.0, 0.0, 1.0],
+ },
+ }
+
+ self.check_mesh_data(postvs_ref, postvs_data)
+
+ results = self.controller.FetchCounters([rd.GPUCounter.RasterizedPrimitives, rd.GPUCounter.VSInvocations, rd.GPUCounter.FSInvocations])
+
+ draw = self.find_draw("Counters Draw").next
+
+ results = [r for r in results if r.eventId == draw.eventId]
+
+ if len(results) != 3:
+ raise rdtest.TestFailureException("Expected 3 results, got {} results".format(len(results)))
+
+ for r in results:
+ r: rd.CounterResult
+ val = r.value.u32
+ if r.counter == rd.GPUCounter.RasterizedPrimitives:
+ if not rdtest.value_compare(val, 1):
+ raise rdtest.TestFailureException("RasterizedPrimitives result {} is not as expected".format(val))
+ else:
+ rdtest.log.success("RasterizedPrimitives result is as expected")
+ elif r.counter == rd.GPUCounter.VSInvocations:
+ if not rdtest.value_compare(val, 3):
+ raise rdtest.TestFailureException("VSInvocations result {} is not as expected".format(val))
+ else:
+ rdtest.log.success("VSInvocations result is as expected")
+ elif r.counter == rd.GPUCounter.FSInvocations:
+ if val < int(0.1 * tex_details.width * tex_details.height):
+ raise rdtest.TestFailureException("FSInvocations result {} is not as expected".format(val))
+ else:
+ rdtest.log.success("FSInvocations result is as expected")
+ else:
+ raise rdtest.TestFailureException("Unexpected counter result {}".format(r.counter))
+
+ rdtest.log.success("Counter data retrieved successfully")