1c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt/*
2c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * Copyright © 2008 Intel Corporation
3c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt *
4c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
5c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * copy of this software and associated documentation files (the "Software"),
6c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * to deal in the Software without restriction, including without limitation
7c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the
9c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * Software is furnished to do so, subject to the following conditions:
10c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt *
11c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * The above copyright notice and this permission notice (including the next
12c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * paragraph) shall be included in all copies or substantial portions of the
13c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * Software.
14c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt *
15c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * IN THE SOFTWARE.
22c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt *
23c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * Authors:
24c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt *    Eric Anholt <eric@anholt.net>
25c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt *
26c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt */
27c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
287540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt/** @file brw_queryobj.c
297540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt *
307540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * Support for query objects (GL_ARB_occlusion_query, GL_ARB_timer_query,
317540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * GL_EXT_transform_feedback, and friends).
327540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt *
337540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * The hardware provides a PIPE_CONTROL command that can report the number of
347540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * fragments that passed the depth test, or the hardware timer.  They are
357540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * appropriately synced with the stage of the pipeline for our extensions'
367540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * needs.
377540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt *
387540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * To avoid getting samples from another context's rendering in our results,
397540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * we capture the counts at the start and end of every batchbuffer while the
407540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * query is active, and sum up the differences.  (We should do so for
417540f25a345ed8d35d97ba4fbf66227a281eff6aEric Anholt * GL_TIME_ELAPSED as well, but don't).
42c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt */
43c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt#include "main/imports.h"
44c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
45c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt#include "brw_context.h"
46f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt#include "brw_defines.h"
4759b2c2adbbece27ccf54e58b598ea29cb3a5aa85Eric Anholt#include "brw_state.h"
48c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt#include "intel_batchbuffer.h"
49c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt#include "intel_reg.h"
50c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
5103f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunkestatic void
5203f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunkewrite_timestamp(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
5303f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke{
5403f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke   if (intel->gen >= 6) {
55a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke      /* Emit workaround flushes: */
56a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke      if (intel->gen == 6) {
57a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         /* The timestamp write below is a non-zero post-sync op, which on
58a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke          * Gen6 necessitates a CS stall.  CS stalls need stall at scoreboard
59a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke          * set.  See the comments for intel_emit_post_sync_nonzero_flush().
60a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke          */
61a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         BEGIN_BATCH(4);
62a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
63a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         OUT_BATCH(PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD);
64a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         OUT_BATCH(0);
65a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         OUT_BATCH(0);
66a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke         ADVANCE_BATCH();
67a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke      }
68a2cdd5ada8ae315acbc4225398b7cbf78181f610Kenneth Graunke
69c4c78c275abffe8d1014b1355f02239859d6aa2bKenneth Graunke      BEGIN_BATCH(5);
70c4c78c275abffe8d1014b1355f02239859d6aa2bKenneth Graunke      OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
7103f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_BATCH(PIPE_CONTROL_WRITE_TIMESTAMP);
7203f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_RELOC(query_bo,
7303f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
7403f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                PIPE_CONTROL_GLOBAL_GTT_WRITE |
7503f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                idx * sizeof(uint64_t));
7603f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_BATCH(0);
77c4c78c275abffe8d1014b1355f02239859d6aa2bKenneth Graunke      OUT_BATCH(0);
7803f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      ADVANCE_BATCH();
7903f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke   } else {
8003f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      BEGIN_BATCH(4);
8103f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
8203f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                PIPE_CONTROL_WRITE_TIMESTAMP);
8303f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_RELOC(query_bo,
8403f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
8503f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                PIPE_CONTROL_GLOBAL_GTT_WRITE |
8603f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke                idx * sizeof(uint64_t));
8703f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_BATCH(0);
8803f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      OUT_BATCH(0);
8903f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      ADVANCE_BATCH();
9003f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke   }
9103f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke}
9203f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke
9320c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunkestatic void
9420c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunkewrite_depth_count(struct intel_context *intel, drm_intel_bo *query_bo, int idx)
9520c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke{
9620c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke   if (intel->gen >= 6) {
975deb1d1a1f9e3354597569032af9bdf27d629ccaKenneth Graunke      /* Emit Sandybridge workaround flush: */
985deb1d1a1f9e3354597569032af9bdf27d629ccaKenneth Graunke      if (intel->gen == 6)
995deb1d1a1f9e3354597569032af9bdf27d629ccaKenneth Graunke         intel_emit_post_sync_nonzero_flush(intel);
10020c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke
1015deb1d1a1f9e3354597569032af9bdf27d629ccaKenneth Graunke      BEGIN_BATCH(5);
102e45a9ce474c3562f16c8a773260752d77a4fed5cKenneth Graunke      OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
10320c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_BATCH(PIPE_CONTROL_DEPTH_STALL |
10420c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                PIPE_CONTROL_WRITE_DEPTH_COUNT);
10520c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_RELOC(query_bo,
10620c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
10720c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                PIPE_CONTROL_GLOBAL_GTT_WRITE |
10820c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                (idx * sizeof(uint64_t)));
10920c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_BATCH(0);
110e45a9ce474c3562f16c8a773260752d77a4fed5cKenneth Graunke      OUT_BATCH(0);
11120c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      ADVANCE_BATCH();
11220c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke   } else {
11320c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      BEGIN_BATCH(4);
11420c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
11520c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                PIPE_CONTROL_DEPTH_STALL |
11620c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                PIPE_CONTROL_WRITE_DEPTH_COUNT);
11720c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      /* This object could be mapped cacheable, but we don't have an exposed
11820c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke       * mechanism to support that.  Since it's going uncached, tell GEM that
11920c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke       * we're writing to it.  The usual clflush should be all that's required
12020c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke       * to pick up the results.
12120c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke       */
12220c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_RELOC(query_bo,
12320c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
12420c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                PIPE_CONTROL_GLOBAL_GTT_WRITE |
12520c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke                (idx * sizeof(uint64_t)));
12620c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_BATCH(0);
12720c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      OUT_BATCH(0);
12820c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke      ADVANCE_BATCH();
12920c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke   }
13020c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke}
13120c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke
132c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt/** Waits on the query object's BO and totals the results for this query */
133c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtstatic void
1347457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan haibrw_queryobj_get_results(struct gl_context *ctx,
1357457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai			 struct brw_query_object *query)
136c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
1377457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai   struct intel_context *intel = intel_context(ctx);
1387457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai
139c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   int i;
140c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   uint64_t *results;
141c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
142c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   if (query->bo == NULL)
143c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt      return;
144c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
1450e723b135bfd59868c92c3ae243f1adaedaec3a5Eric Anholt   if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
1460e723b135bfd59868c92c3ae243f1adaedaec3a5Eric Anholt      if (drm_intel_bo_busy(query->bo)) {
1470e723b135bfd59868c92c3ae243f1adaedaec3a5Eric Anholt         perf_debug("Stalling on the GPU waiting for a query object.\n");
1480e723b135bfd59868c92c3ae243f1adaedaec3a5Eric Anholt      }
1490e723b135bfd59868c92c3ae243f1adaedaec3a5Eric Anholt   }
1500e723b135bfd59868c92c3ae243f1adaedaec3a5Eric Anholt
1512e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   drm_intel_bo_map(query->bo, false);
152c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   results = query->bo->virtual;
153c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   switch (query->Base.Target) {
154c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   case GL_TIME_ELAPSED_EXT:
1557457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai      if (intel->gen >= 6)
1567457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai	 query->Base.Result += 80 * (results[1] - results[0]);
1577457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai      else
1587457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai	 query->Base.Result += 1000 * ((results[1] >> 32) - (results[0] >> 32));
159c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
160c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry
161f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   case GL_TIMESTAMP:
162f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      if (intel->gen >= 6) {
163f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt         /* Our timer is a clock that increments every 80ns (regardless of
164f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * other clock scaling in the system).  The timestamp register we can
165f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * read for glGetTimestamp() masks out the top 32 bits, so we do that
166f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * here too to let the two counters be compared against each other.
167f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          *
168f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * If we just multiplied that 32 bits of data by 80, it would roll
169f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * over at a non-power-of-two, so an application couldn't use
170f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * GL_QUERY_COUNTER_BITS to handle rollover correctly.  Instead, we
171f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * report 36 bits and truncate at that (rolling over 5 times as often
172f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * as the HW counter), and when the 32-bit counter rolls over, it
173f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * happens to also be at a rollover in the reported value from near
174f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * (1<<36) to 0.
175f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          *
176f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * The low 32 bits rolls over in ~343 seconds.  Our 36-bit result
177f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          * rolls over every ~69 seconds.
178f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt          */
179f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt	 query->Base.Result = 80 * (results[1] & 0xffffffff);
180f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt         query->Base.Result &= (1ull << 36) - 1;
181f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      } else {
182f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt	 query->Base.Result = 1000 * (results[1] >> 32);
183f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      }
184f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
185f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      break;
186f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
187c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   case GL_SAMPLES_PASSED_ARB:
1883b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      /* Map and count the pixels from the current query BO */
1893b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      for (i = query->first_index; i <= query->last_index; i++) {
1903b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt	 query->Base.Result += results[i * 2 + 1] - results[i * 2];
1913b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      }
192c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
193c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry
194ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt   case GL_ANY_SAMPLES_PASSED:
195ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt      /* Set true if any of the sub-queries passed. */
196ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt      for (i = query->first_index; i <= query->last_index; i++) {
197ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt	 if (results[i * 2 + 1] != results[i * 2]) {
198ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt            query->Base.Result = GL_TRUE;
199ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt            break;
200ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt         }
201ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt      }
202ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt      break;
203ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt
204c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry   case GL_PRIMITIVES_GENERATED:
205c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry   case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
206c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* We don't actually query the hardware for this value, so query->bo
207c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * should always be NULL and execution should never reach here.
208c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
209c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      assert(!"Unreachable");
210c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      break;
211c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
212c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   default:
213c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      assert(!"Unrecognized query target in brw_queryobj_get_results()");
214c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
215c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   }
21634474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt   drm_intel_bo_unmap(query->bo);
217c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
21834474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt   drm_intel_bo_unreference(query->bo);
219c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   query->bo = NULL;
220c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
221c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
222c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtstatic struct gl_query_object *
223f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergbrw_new_query_object(struct gl_context *ctx, GLuint id)
224c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
225c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_query_object *query;
226c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
22732f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg   query = calloc(1, sizeof(struct brw_query_object));
228c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
229c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   query->Base.Id = id;
230c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   query->Base.Result = 0;
2312e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   query->Base.Active = false;
2322e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   query->Base.Ready = true;
233c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
234c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   return &query->Base;
235c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
236c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
237c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtstatic void
238f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergbrw_delete_query(struct gl_context *ctx, struct gl_query_object *q)
239c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
240c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_query_object *query = (struct brw_query_object *)q;
241c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
24234474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt   drm_intel_bo_unreference(query->bo);
24332f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg   free(query);
244c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
245c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
246c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtstatic void
247f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergbrw_begin_query(struct gl_context *ctx, struct gl_query_object *q)
248c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
249c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_context *brw = brw_context(ctx);
250c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct intel_context *intel = intel_context(ctx);
251c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_query_object *query = (struct brw_query_object *)q;
252c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
253c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   switch (query->Base.Target) {
254c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   case GL_TIME_ELAPSED_EXT:
25534474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt      drm_intel_bo_unreference(query->bo);
25603f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query", 4096, 4096);
25703f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      write_timestamp(intel, query->bo, 0);
258c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
259c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry
260ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt   case GL_ANY_SAMPLES_PASSED:
261c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   case GL_SAMPLES_PASSED_ARB:
2623b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      /* Reset our driver's tracking of query state. */
26334474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt      drm_intel_bo_unreference(query->bo);
2643b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      query->bo = NULL;
2653b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      query->first_index = -1;
2663b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      query->last_index = -1;
2673b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt
2683b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      brw->query.obj = query;
2693b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      intel->stats_wm++;
270c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
271c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry
272c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry   case GL_PRIMITIVES_GENERATED:
273c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* We don't actually query the hardware for this value; we keep track of
274c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * it a software counter.  So just reset the counter.
275c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
276c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      brw->sol.primitives_generated = 0;
2778aa78c104a6fa9497cba558b54c41f6f2292bd74Jordan Justen      brw->sol.counting_primitives_generated = true;
278c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      break;
279c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
280c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry   case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
281c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* We don't actually query the hardware for this value; we keep track of
282c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * it a software counter.  So just reset the counter.
283c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
284c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      brw->sol.primitives_written = 0;
2858aa78c104a6fa9497cba558b54c41f6f2292bd74Jordan Justen      brw->sol.counting_primitives_written = true;
286c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      break;
287c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
288c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   default:
289c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      assert(!"Unrecognized query target in brw_begin_query()");
290c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
2913b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt   }
292c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
293c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
294c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt/**
295c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt * Begin the ARB_occlusion_query query on a query object.
296c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt */
297c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtstatic void
298f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergbrw_end_query(struct gl_context *ctx, struct gl_query_object *q)
299c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
300c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_context *brw = brw_context(ctx);
301c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct intel_context *intel = intel_context(ctx);
302c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_query_object *query = (struct brw_query_object *)q;
303c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
304c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   switch (query->Base.Target) {
305f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   case GL_TIMESTAMP:
306f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      drm_intel_bo_unreference(query->bo);
307f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      query->bo = drm_intel_bo_alloc(intel->bufmgr, "timer query",
308f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt				     4096, 4096);
309f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt      /* FALLTHROUGH */
310f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
311c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   case GL_TIME_ELAPSED_EXT:
31203f14664b6b12bc7853866fe613d8af350e51e08Kenneth Graunke      write_timestamp(intel, query->bo, 1);
3138d68a90e225d831a395ba788e425cb717eec1f9aChris Wilson      intel_batchbuffer_flush(intel);
314c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
315c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry
316ebd4883a0c53a3584f6da78042dbc38da779291bEric Anholt   case GL_ANY_SAMPLES_PASSED:
317c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   case GL_SAMPLES_PASSED_ARB:
3183b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      /* Flush the batchbuffer in case it has writes to our query BO.
3193b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt       * Have later queries write to a new query BO so that further rendering
3203b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt       * doesn't delay the collection of our results.
3213b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt       */
3223b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      if (query->bo) {
3233b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt	 brw_emit_query_end(brw);
3248d68a90e225d831a395ba788e425cb717eec1f9aChris Wilson	 intel_batchbuffer_flush(intel);
3253b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt
32634474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt	 drm_intel_bo_unreference(brw->query.bo);
3273b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt	 brw->query.bo = NULL;
3283b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      }
3293b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt
3303b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      brw->query.obj = NULL;
3313b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt
3323b68b6c83e491d7f85baff6a31dcceee28622ae0Eric Anholt      intel->stats_wm--;
333c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
334c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry
335c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry   case GL_PRIMITIVES_GENERATED:
336c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* We don't actually query the hardware for this value; we keep track of
337c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * it in a software counter.  So just read the counter and store it in
338c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * the query object.
339c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
340c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      query->Base.Result = brw->sol.primitives_generated;
3418aa78c104a6fa9497cba558b54c41f6f2292bd74Jordan Justen      brw->sol.counting_primitives_generated = false;
342c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
343c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* And set brw->query.obj to NULL so that this query won't try to wait
344c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * for any rendering to complete.
345c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
346c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      query->bo = NULL;
347c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      break;
348c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
349c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry   case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
350c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* We don't actually query the hardware for this value; we keep track of
351c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * it in a software counter.  So just read the counter and store it in
352c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * the query object.
353c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
354c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      query->Base.Result = brw->sol.primitives_written;
3558aa78c104a6fa9497cba558b54c41f6f2292bd74Jordan Justen      brw->sol.counting_primitives_written = false;
356c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
357c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      /* And set brw->query.obj to NULL so that this query won't try to wait
358c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       * for any rendering to complete.
359c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry       */
360c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      query->bo = NULL;
361c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry      break;
362c59393b7069f59ca2a13bfb6500f2a5360c38031Paul Berry
363c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry   default:
364c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      assert(!"Unrecognized query target in brw_end_query()");
365c5e17a84983d7799fd842a62daaece3d97a670bePaul Berry      break;
366c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   }
367c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
368c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
369f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergstatic void brw_wait_query(struct gl_context *ctx, struct gl_query_object *q)
370c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
371c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_query_object *query = (struct brw_query_object *)q;
372c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
3737457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai   brw_queryobj_get_results(ctx, query);
3742e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   query->Base.Ready = true;
375c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
376c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
377f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergstatic void brw_check_query(struct gl_context *ctx, struct gl_query_object *q)
378c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
379c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct brw_query_object *query = (struct brw_query_object *)q;
380c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
3813c1172f06636ff4aec24a6c68df937844da73a53Eric Anholt   if (query->bo == NULL || !drm_intel_bo_busy(query->bo)) {
3827457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai      brw_queryobj_get_results(ctx, query);
3832e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      query->Base.Ready = true;
384c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   }
385c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
386c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
387c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt/** Called to set up the query BO and account for its aperture space */
388c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtvoid
389c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtbrw_prepare_query_begin(struct brw_context *brw)
390c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
391c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct intel_context *intel = &brw->intel;
392c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
393c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   /* Skip if we're not doing any queries. */
394df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt   if (!brw->query.obj)
395c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt      return;
396c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
397c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   /* Get a new query BO if we're going to need it. */
398c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   if (brw->query.bo == NULL ||
399c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt       brw->query.index * 2 + 1 >= 4096 / sizeof(uint64_t)) {
40034474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt      drm_intel_bo_unreference(brw->query.bo);
401c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt      brw->query.bo = NULL;
402c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
40334474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt      brw->query.bo = drm_intel_bo_alloc(intel->bufmgr, "query", 4096, 1);
404689aca782216d2666486dea02206cbc9c4162e0dZhenyu Wang
405689aca782216d2666486dea02206cbc9c4162e0dZhenyu Wang      /* clear target buffer */
4062e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke      drm_intel_bo_map(brw->query.bo, true);
407689aca782216d2666486dea02206cbc9c4162e0dZhenyu Wang      memset((char *)brw->query.bo->virtual, 0, 4096);
408689aca782216d2666486dea02206cbc9c4162e0dZhenyu Wang      drm_intel_bo_unmap(brw->query.bo);
409689aca782216d2666486dea02206cbc9c4162e0dZhenyu Wang
410c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt      brw->query.index = 0;
411c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   }
412c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
413c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
414c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt/** Called just before primitive drawing to get a beginning PS_DEPTH_COUNT. */
415c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtvoid
416c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtbrw_emit_query_begin(struct brw_context *brw)
417c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
418c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct intel_context *intel = &brw->intel;
4197457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai   struct gl_context *ctx = &intel->ctx;
420df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt   struct brw_query_object *query = brw->query.obj;
421c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
422c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   /* Skip if we're not doing any queries, or we've emitted the start. */
423df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt   if (!query || brw->query.active)
424c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt      return;
425c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
42620c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke   write_depth_count(intel, brw->query.bo, brw->query.index * 2);
427c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
428df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt   if (query->bo != brw->query.bo) {
429df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt      if (query->bo != NULL)
4307457da5edd4a33c2581f10608ce5bcf0e254c5f9Zou Nan hai	 brw_queryobj_get_results(ctx, query);
43134474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt      drm_intel_bo_reference(brw->query.bo);
432df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt      query->bo = brw->query.bo;
433df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt      query->first_index = brw->query.index;
434c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   }
435df3590f570cb88eb9695b443208d7576b5867fd1Eric Anholt   query->last_index = brw->query.index;
4362e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   brw->query.active = true;
437c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
438c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
439c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt/** Called at batchbuffer flush to get an ending PS_DEPTH_COUNT */
440c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtvoid
441c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtbrw_emit_query_end(struct brw_context *brw)
442c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
443c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   struct intel_context *intel = &brw->intel;
444c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
445c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   if (!brw->query.active)
446c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt      return;
447c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
44820c09b82d0520843f1c168adaf9bd5d17a572085Kenneth Graunke   write_depth_count(intel, brw->query.bo, brw->query.index * 2 + 1);
449c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
4502e5a1a254ed81b1d3efa6064f48183eefac784d0Kenneth Graunke   brw->query.active = false;
451c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   brw->query.index++;
452c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
453c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt
454f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholtstatic uint64_t
455f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholtbrw_get_timestamp(struct gl_context *ctx)
456f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt{
457f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   struct intel_context *intel = intel_context(ctx);
458f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   uint64_t result = 0;
459f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
460f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   drm_intel_reg_read(intel->bufmgr, TIMESTAMP, &result);
461f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
462f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   /* See logic in brw_queryobj_get_results() */
463f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   result = result >> 32;
464f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   result *= 80;
465f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   result &= (1ull << 36) - 1;
466f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
467f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   return result;
468f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt}
469f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt
470c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholtvoid brw_init_queryobj_functions(struct dd_function_table *functions)
471c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt{
472c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   functions->NewQueryObject = brw_new_query_object;
473c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   functions->DeleteQuery = brw_delete_query;
474c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   functions->BeginQuery = brw_begin_query;
475c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   functions->EndQuery = brw_end_query;
476c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   functions->CheckQuery = brw_check_query;
477c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt   functions->WaitQuery = brw_wait_query;
478f0159018d7709b57d9916575512d75cb3f2fb395Eric Anholt   functions->GetTimestamp = brw_get_timestamp;
479c157cfc6376f7469ab272b18868183e5ff9ac754Eric Anholt}
480