+ assert(brw->gen < 6);
+
+ switch (query->Base.Target) {
+ case GL_TIME_ELAPSED_EXT:
+ /* For timestamp queries, we record the starting time right away so that
+ * we measure the full time between BeginQuery and EndQuery. There's
+ * some debate about whether this is the right thing to do. Our decision
+ * is based on the following text from the ARB_timer_query extension:
+ *
+ * "(5) Should the extension measure total time elapsed between the full
+ * completion of the BeginQuery and EndQuery commands, or just time
+ * spent in the graphics library?
+ *
+ * RESOLVED: This extension will measure the total time elapsed
+ * between the full completion of these commands. Future extensions
+ * may implement a query to determine time elapsed at different stages
+ * of the graphics pipeline."
+ *
+ * We write a starting timestamp now (at index 0). At EndQuery() time,
+ * we'll write a second timestamp (at index 1), and subtract the two to
+ * obtain the time elapsed. Notably, this includes time elapsed while
+ * the system was doing other work, such as running other applications.
+ */
+ drm_intel_bo_unreference(query->bo);
+ query->bo = drm_intel_bo_alloc(brw->bufmgr, "timer query", 4096, 4096);
+ brw_write_timestamp(brw, query->bo, 0);
+ break;
+
+ case GL_ANY_SAMPLES_PASSED:
+ case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
+ case GL_SAMPLES_PASSED_ARB:
+ /* For occlusion queries, we delay taking an initial sample until the
+ * first drawing occurs in this batch. See the reasoning in the comments
+ * for brw_emit_query_begin() below.
+ *
+ * Since we're starting a new query, we need to be sure to throw away
+ * any previous occlusion query results.
+ */
+ drm_intel_bo_unreference(query->bo);
+ query->bo = NULL;
+ query->last_index = -1;
+
+ brw->query.obj = query;
+
+ /* Depth statistics on Gen4 require strange workarounds, so we try to
+ * avoid them when necessary. They're required for occlusion queries,
+ * so turn them on now.
+ */
+ brw->stats_wm++;
+ brw->ctx.NewDriverState |= BRW_NEW_STATS_WM;
+ break;
+
+ default:
+ unreachable("Unrecognized query target in brw_begin_query()");
+ }