OSDN Git Service

i965: Parameterize HiZ code to prepare for adding blitting.
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i965 / gen6_blorp.cpp
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include <assert.h>
25
26 #include "intel_batchbuffer.h"
27 #include "intel_fbo.h"
28 #include "intel_mipmap_tree.h"
29
30 #include "brw_context.h"
31 #include "brw_defines.h"
32 #include "brw_state.h"
33
34 #include "brw_blorp.h"
35 #include "gen6_blorp.h"
36
37 /**
38  * \name Constants for BLORP VBO
39  * \{
40  */
41 #define GEN6_BLORP_NUM_VERTICES 3
42 #define GEN6_BLORP_NUM_VUE_ELEMS 8
43 #define GEN6_BLORP_VBO_SIZE (GEN6_BLORP_NUM_VERTICES \
44                              * GEN6_BLORP_NUM_VUE_ELEMS \
45                              * sizeof(float))
46 /** \} */
47
48 void
49 gen6_blorp_emit_batch_head(struct brw_context *brw,
50                            const brw_blorp_params *params)
51 {
52    struct gl_context *ctx = &brw->intel.ctx;
53    struct intel_context *intel = &brw->intel;
54
55    /* To ensure that the batch contains only the resolve, flush the batch
56     * before beginning and after finishing emitting the resolve packets.
57     *
58     * Ideally, we would not need to flush for the resolve op. But, I suspect
59     * that it's unsafe for CMD_PIPELINE_SELECT to occur multiple times in
60     * a single batch, and there is no safe way to ensure that other than by
61     * fencing the resolve with flushes. Ideally, we would just detect if
62     * a batch is in progress and do the right thing, but that would require
63     * the ability to *safely* access brw_context::state::dirty::brw
64     * outside of the brw_upload_state() codepath.
65     */
66    intel_flush(ctx);
67
68    /* CMD_PIPELINE_SELECT
69     *
70     * Select the 3D pipeline, as opposed to the media pipeline.
71     */
72    {
73       BEGIN_BATCH(1);
74       OUT_BATCH(brw->CMD_PIPELINE_SELECT << 16);
75       ADVANCE_BATCH();
76    }
77
78    /* 3DSTATE_MULTISAMPLE */
79    {
80       int length = intel->gen == 7 ? 4 : 3;
81
82       BEGIN_BATCH(length);
83       OUT_BATCH(_3DSTATE_MULTISAMPLE << 16 | (length - 2));
84       OUT_BATCH(MS_PIXEL_LOCATION_CENTER |
85                 MS_NUMSAMPLES_1);
86       OUT_BATCH(0);
87       if (length >= 4)
88          OUT_BATCH(0);
89       ADVANCE_BATCH();
90
91    }
92
93    /* 3DSTATE_SAMPLE_MASK */
94    {
95       BEGIN_BATCH(2);
96       OUT_BATCH(_3DSTATE_SAMPLE_MASK << 16 | (2 - 2));
97       OUT_BATCH(1);
98       ADVANCE_BATCH();
99    }
100
101    /* CMD_STATE_BASE_ADDRESS
102     *
103     * From the Sandy Bridge PRM, Volume 1, Part 1, Table STATE_BASE_ADDRESS:
104     *     The following commands must be reissued following any change to the
105     *     base addresses:
106     *         3DSTATE_CC_POINTERS
107     *         3DSTATE_BINDING_TABLE_POINTERS
108     *         3DSTATE_SAMPLER_STATE_POINTERS
109     *         3DSTATE_VIEWPORT_STATE_POINTERS
110     *         MEDIA_STATE_POINTERS
111     */
112    {
113       BEGIN_BATCH(10);
114       OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (10 - 2));
115       OUT_BATCH(1); /* GeneralStateBaseAddressModifyEnable */
116       /* SurfaceStateBaseAddress */
117       OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0, 1);
118       /* DynamicStateBaseAddress */
119       OUT_RELOC(intel->batch.bo, (I915_GEM_DOMAIN_RENDER |
120                                   I915_GEM_DOMAIN_INSTRUCTION), 0, 1);
121       OUT_BATCH(1); /* IndirectObjectBaseAddress */
122       OUT_BATCH(1); /* InstructionBaseAddress */
123       OUT_BATCH(1); /* GeneralStateUpperBound */
124       OUT_BATCH(1); /* DynamicStateUpperBound */
125       OUT_BATCH(1); /* IndirectObjectUpperBound*/
126       OUT_BATCH(1); /* InstructionAccessUpperBound */
127       ADVANCE_BATCH();
128    }
129 }
130
131 void
132 gen6_blorp_emit_vertices(struct brw_context *brw,
133                          const brw_blorp_params *params)
134 {
135    struct intel_context *intel = &brw->intel;
136    uint32_t vertex_offset;
137
138    /* Setup VBO for the rectangle primitive..
139     *
140     * A rectangle primitive (3DPRIM_RECTLIST) consists of only three
141     * vertices. The vertices reside in screen space with DirectX coordinates
142     * (that is, (0, 0) is the upper left corner).
143     *
144     *   v2 ------ implied
145     *    |        |
146     *    |        |
147     *   v0 ----- v1
148     *
149     * Since the VS is disabled, the clipper loads each VUE directly from
150     * the URB. This is controlled by the 3DSTATE_VERTEX_BUFFERS and
151     * 3DSTATE_VERTEX_ELEMENTS packets below. The VUE contents are as follows:
152     *   dw0: Reserved, MBZ.
153     *   dw1: Render Target Array Index. The HiZ op does not use indexed
154     *        vertices, so set the dword to 0.
155     *   dw2: Viewport Index. The HiZ op disables viewport mapping and
156     *        scissoring, so set the dword to 0.
157     *   dw3: Point Width: The HiZ op does not emit the POINTLIST primitive, so
158     *        set the dword to 0.
159     *   dw4: Vertex Position X.
160     *   dw5: Vertex Position Y.
161     *   dw6: Vertex Position Z.
162     *   dw7: Vertex Position W.
163     *
164     * For details, see the Sandybridge PRM, Volume 2, Part 1, Section 1.5.1
165     * "Vertex URB Entry (VUE) Formats".
166     */
167    {
168       float *vertex_data;
169
170       const float vertices[GEN6_BLORP_VBO_SIZE] = {
171          /* v0 */ 0, 0, 0, 0,     params->x0, params->y1, 0, 1,
172          /* v1 */ 0, 0, 0, 0,     params->x1, params->y1, 0, 1,
173          /* v2 */ 0, 0, 0, 0,     params->x0, params->y0, 0, 1,
174       };
175
176       vertex_data = (float *) brw_state_batch(brw, AUB_TRACE_NO_TYPE,
177                                               GEN6_BLORP_VBO_SIZE, 32,
178                                               &vertex_offset);
179       memcpy(vertex_data, vertices, GEN6_BLORP_VBO_SIZE);
180    }
181
182    /* 3DSTATE_VERTEX_BUFFERS */
183    {
184       const int num_buffers = 1;
185       const int batch_length = 1 + 4 * num_buffers;
186
187       uint32_t dw0 = GEN6_VB0_ACCESS_VERTEXDATA |
188                      (GEN6_BLORP_NUM_VUE_ELEMS * sizeof(float)) << BRW_VB0_PITCH_SHIFT;
189
190       if (intel->gen >= 7)
191          dw0 |= GEN7_VB0_ADDRESS_MODIFYENABLE;
192
193       BEGIN_BATCH(batch_length);
194       OUT_BATCH((_3DSTATE_VERTEX_BUFFERS << 16) | (batch_length - 2));
195       OUT_BATCH(dw0);
196       /* start address */
197       OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_VERTEX, 0,
198                 vertex_offset);
199       /* end address */
200       OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_VERTEX, 0,
201                 vertex_offset + GEN6_BLORP_VBO_SIZE - 1);
202       OUT_BATCH(0);
203       ADVANCE_BATCH();
204    }
205
206    /* 3DSTATE_VERTEX_ELEMENTS
207     *
208     * Fetch dwords 0 - 7 from each VUE. See the comments above where
209     * the vertex_bo is filled with data.
210     */
211    {
212       const int num_elements = 2;
213       const int batch_length = 1 + 2 * num_elements;
214
215       BEGIN_BATCH(batch_length);
216       OUT_BATCH((_3DSTATE_VERTEX_ELEMENTS << 16) | (batch_length - 2));
217       /* Element 0 */
218       OUT_BATCH(GEN6_VE0_VALID |
219                 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT |
220                 0 << BRW_VE0_SRC_OFFSET_SHIFT);
221       OUT_BATCH(BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT |
222                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_1_SHIFT |
223                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_2_SHIFT |
224                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_3_SHIFT);
225       /* Element 1 */
226       OUT_BATCH(GEN6_VE0_VALID |
227                 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT |
228                 16 << BRW_VE0_SRC_OFFSET_SHIFT);
229       OUT_BATCH(BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT |
230                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_1_SHIFT |
231                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_2_SHIFT |
232                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_3_SHIFT);
233       ADVANCE_BATCH();
234    }
235 }
236
237 /**
238  * \brief Execute a blit or render pass operation.
239  *
240  * To execute the operation, this function manually constructs and emits a
241  * batch to draw a rectangle primitive. The batchbuffer is flushed before
242  * constructing and after emitting the batch.
243  *
244  * This function alters no GL state.
245  */
246 void
247 gen6_blorp_exec(struct intel_context *intel,
248                 const brw_blorp_params *params)
249 {
250    struct gl_context *ctx = &intel->ctx;
251    struct brw_context *brw = brw_context(ctx);
252    uint32_t draw_x, draw_y;
253    uint32_t tile_mask_x, tile_mask_y;
254
255    params->depth.get_draw_offsets(&draw_x, &draw_y);
256
257    /* Compute masks to determine how much of draw_x and draw_y should be
258     * performed using the fine adjustment of "depth coordinate offset X/Y"
259     * (dw5 of 3DSTATE_DEPTH_BUFFER).  See the emit_depthbuffer() function for
260     * details.
261     */
262    {
263       uint32_t depth_mask_x, depth_mask_y, hiz_mask_x, hiz_mask_y;
264       intel_region_get_tile_masks(params->depth.mt->region,
265                                   &depth_mask_x, &depth_mask_y);
266       intel_region_get_tile_masks(params->depth.mt->hiz_mt->region,
267                                   &hiz_mask_x, &hiz_mask_y);
268
269       /* Each HiZ row represents 2 rows of pixels */
270       hiz_mask_y = hiz_mask_y << 1 | 1;
271
272       tile_mask_x = depth_mask_x | hiz_mask_x;
273       tile_mask_y = depth_mask_y | hiz_mask_y;
274    }
275
276    gen6_blorp_emit_batch_head(brw, params);
277    gen6_blorp_emit_vertices(brw, params);
278
279    /* 3DSTATE_URB
280     *
281     * Assign the entire URB to the VS. Even though the VS disabled, URB space
282     * is still needed because the clipper loads the VUE's from the URB. From
283     * the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE,
284     * Dword 1.15:0 "VS Number of URB Entries":
285     *     This field is always used (even if VS Function Enable is DISABLED).
286     *
287     * The warning below appears in the PRM (Section 3DSTATE_URB), but we can
288     * safely ignore it because this batch contains only one draw call.
289     *     Because of URB corruption caused by allocating a previous GS unit
290     *     URB entry to the VS unit, software is required to send a “GS NULL
291     *     Fence” (Send URB fence with VS URB size == 1 and GS URB size == 0)
292     *     plus a dummy DRAW call before any case where VS will be taking over
293     *     GS URB space.
294     */
295    {
296       BEGIN_BATCH(3);
297       OUT_BATCH(_3DSTATE_URB << 16 | (3 - 2));
298       OUT_BATCH(brw->urb.max_vs_entries << GEN6_URB_VS_ENTRIES_SHIFT);
299       OUT_BATCH(0);
300       ADVANCE_BATCH();
301    }
302
303    /* 3DSTATE_CC_STATE_POINTERS
304     *
305     * The pointer offsets are relative to
306     * CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
307     *
308     * The HiZ op doesn't use BLEND_STATE or COLOR_CALC_STATE.
309     */
310    {
311       uint32_t depthstencil_offset;
312       gen6_blorp_emit_depth_stencil_state(brw, params, &depthstencil_offset);
313
314       BEGIN_BATCH(4);
315       OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (4 - 2));
316       OUT_BATCH(1); /* BLEND_STATE offset */
317       OUT_BATCH(depthstencil_offset | 1); /* DEPTH_STENCIL_STATE offset */
318       OUT_BATCH(1); /* COLOR_CALC_STATE offset */
319       ADVANCE_BATCH();
320    }
321
322    /* 3DSTATE_VS
323     *
324     * Disable vertex shader.
325     */
326    {
327       /* From the BSpec, Volume 2a, Part 3 "Vertex Shader", Section
328        * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
329        *   [DevSNB] A pipeline flush must be programmed prior to a 3DSTATE_VS
330        *   command that causes the VS Function Enable to toggle. Pipeline
331        *   flush can be executed by sending a PIPE_CONTROL command with CS
332        *   stall bit set and a post sync operation.
333        */
334       intel_emit_post_sync_nonzero_flush(intel);
335
336       BEGIN_BATCH(6);
337       OUT_BATCH(_3DSTATE_VS << 16 | (6 - 2));
338       OUT_BATCH(0);
339       OUT_BATCH(0);
340       OUT_BATCH(0);
341       OUT_BATCH(0);
342       OUT_BATCH(0);
343       ADVANCE_BATCH();
344    }
345
346    /* 3DSTATE_GS
347     *
348     * Disable the geometry shader.
349     */
350    {
351       BEGIN_BATCH(7);
352       OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
353       OUT_BATCH(0);
354       OUT_BATCH(0);
355       OUT_BATCH(0);
356       OUT_BATCH(0);
357       OUT_BATCH(0);
358       OUT_BATCH(0);
359       ADVANCE_BATCH();
360    }
361
362    /* 3DSTATE_CLIP
363     *
364     * Disable the clipper.
365     *
366     * The BLORP op emits a rectangle primitive, which requires clipping to
367     * be disabled. From page 10 of the Sandy Bridge PRM Volume 2 Part 1
368     * Section 1.3 "3D Primitives Overview":
369     *    RECTLIST:
370     *    Either the CLIP unit should be DISABLED, or the CLIP unit's Clip
371     *    Mode should be set to a value other than CLIPMODE_NORMAL.
372     *
373     * Also disable perspective divide. This doesn't change the clipper's
374     * output, but does spare a few electrons.
375     */
376    {
377       BEGIN_BATCH(4);
378       OUT_BATCH(_3DSTATE_CLIP << 16 | (4 - 2));
379       OUT_BATCH(0);
380       OUT_BATCH(GEN6_CLIP_PERSPECTIVE_DIVIDE_DISABLE);
381       OUT_BATCH(0);
382       ADVANCE_BATCH();
383    }
384
385    /* 3DSTATE_SF
386     *
387     * Disable ViewportTransformEnable (dw2.1)
388     *
389     * From the SandyBridge PRM, Volume 2, Part 1, Section 1.3, "3D
390     * Primitives Overview":
391     *     RECTLIST: Viewport Mapping must be DISABLED (as is typical with the
392     *     use of screen- space coordinates).
393     *
394     * A solid rectangle must be rendered, so set FrontFaceFillMode (dw2.4:3)
395     * and BackFaceFillMode (dw2.5:6) to SOLID(0).
396     *
397     * From the Sandy Bridge PRM, Volume 2, Part 1, Section
398     * 6.4.1.1 3DSTATE_SF, Field FrontFaceFillMode:
399     *     SOLID: Any triangle or rectangle object found to be front-facing
400     *     is rendered as a solid object. This setting is required when
401     *     (rendering rectangle (RECTLIST) objects.
402     */
403    {
404       BEGIN_BATCH(20);
405       OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
406       OUT_BATCH((1 - 1) << GEN6_SF_NUM_OUTPUTS_SHIFT | /* only position */
407                 1 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
408                 0 << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT);
409       for (int i = 0; i < 18; ++i)
410          OUT_BATCH(0);
411       ADVANCE_BATCH();
412    }
413
414    /* 3DSTATE_WM
415     *
416     * Disable thread dispatch (dw5.19) and enable the HiZ op.
417     *
418     * Even though thread dispatch is disabled, max threads (dw5.25:31) must be
419     * nonzero to prevent the GPU from hanging. See the valid ranges in the
420     * BSpec, Volume 2a.11 Windower, Section 3DSTATE_WM, Dword 5.25:31
421     * "Maximum Number Of Threads".
422     */
423    {
424       uint32_t dw4 = 0;
425
426       switch (params->hiz_op) {
427       case GEN6_HIZ_OP_DEPTH_CLEAR:
428          assert(!"not implemented");
429          dw4 |= GEN6_WM_DEPTH_CLEAR;
430          break;
431       case GEN6_HIZ_OP_DEPTH_RESOLVE:
432          dw4 |= GEN6_WM_DEPTH_RESOLVE;
433          break;
434       case GEN6_HIZ_OP_HIZ_RESOLVE:
435          dw4 |= GEN6_WM_HIERARCHICAL_DEPTH_RESOLVE;
436          break;
437       default:
438          assert(0);
439          break;
440       }
441
442       BEGIN_BATCH(9);
443       OUT_BATCH(_3DSTATE_WM << 16 | (9 - 2));
444       OUT_BATCH(0);
445       OUT_BATCH(0);
446       OUT_BATCH(0);
447       OUT_BATCH(dw4);
448       OUT_BATCH((brw->max_wm_threads - 1) << GEN6_WM_MAX_THREADS_SHIFT);
449       OUT_BATCH((1 - 1) << GEN6_WM_NUM_SF_OUTPUTS_SHIFT); /* only position */
450       OUT_BATCH(0);
451       OUT_BATCH(0);
452       ADVANCE_BATCH();
453    }
454
455    /* 3DSTATE_DEPTH_BUFFER */
456    {
457       uint32_t width, height;
458       params->depth.get_miplevel_dims(&width, &height);
459
460       uint32_t tile_x = draw_x & tile_mask_x;
461       uint32_t tile_y = draw_y & tile_mask_y;
462       uint32_t offset =
463          intel_region_get_aligned_offset(params->depth.mt->region,
464                                          draw_x & ~tile_mask_x,
465                                          draw_y & ~tile_mask_y);
466
467       /* According to the Sandy Bridge PRM, volume 2 part 1, pp326-327
468        * (3DSTATE_DEPTH_BUFFER dw5), in the documentation for "Depth
469        * Coordinate Offset X/Y":
470        *
471        *   "The 3 LSBs of both offsets must be zero to ensure correct
472        *   alignment"
473        *
474        * We have no guarantee that tile_x and tile_y are correctly aligned,
475        * since they are determined by the mipmap layout, which is only aligned
476        * to multiples of 4.
477        *
478        * So, to avoid hanging the GPU, just smash the low order 3 bits of
479        * tile_x and tile_y to 0.  This is a temporary workaround until we come
480        * up with a better solution.
481        */
482       tile_x &= ~7;
483       tile_y &= ~7;
484
485       intel_emit_post_sync_nonzero_flush(intel);
486       intel_emit_depth_stall_flushes(intel);
487
488       BEGIN_BATCH(7);
489       OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (7 - 2));
490       uint32_t pitch_bytes =
491          params->depth.mt->region->pitch * params->depth.mt->region->cpp;
492       OUT_BATCH((pitch_bytes - 1) |
493                 params->depth_format << 18 |
494                 1 << 21 | /* separate stencil enable */
495                 1 << 22 | /* hiz enable */
496                 BRW_TILEWALK_YMAJOR << 26 |
497                 1 << 27 | /* y-tiled */
498                 BRW_SURFACE_2D << 29);
499       OUT_RELOC(params->depth.mt->region->bo,
500                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
501                 offset);
502       OUT_BATCH(BRW_SURFACE_MIPMAPLAYOUT_BELOW << 1 |
503                 (width + tile_x - 1) << 6 |
504                 (height + tile_y - 1) << 19);
505       OUT_BATCH(0);
506       OUT_BATCH(tile_x |
507                 tile_y << 16);
508       OUT_BATCH(0);
509       ADVANCE_BATCH();
510    }
511
512    /* 3DSTATE_HIER_DEPTH_BUFFER */
513    {
514       struct intel_region *hiz_region = params->depth.mt->hiz_mt->region;
515       uint32_t hiz_offset =
516          intel_region_get_aligned_offset(hiz_region,
517                                          draw_x & ~tile_mask_x,
518                                          (draw_y & ~tile_mask_y) / 2);
519
520       BEGIN_BATCH(3);
521       OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
522       OUT_BATCH(hiz_region->pitch * hiz_region->cpp - 1);
523       OUT_RELOC(hiz_region->bo,
524                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
525                 hiz_offset);
526       ADVANCE_BATCH();
527    }
528
529    /* 3DSTATE_STENCIL_BUFFER */
530    {
531       BEGIN_BATCH(3);
532       OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
533       OUT_BATCH(0);
534       OUT_BATCH(0);
535       ADVANCE_BATCH();
536    }
537
538    /* 3DSTATE_CLEAR_PARAMS
539     *
540     * From the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE_CLEAR_PARAMS:
541     *   [DevSNB] 3DSTATE_CLEAR_PARAMS packet must follow the DEPTH_BUFFER_STATE
542     *   packet when HiZ is enabled and the DEPTH_BUFFER_STATE changes.
543     */
544    {
545       BEGIN_BATCH(2);
546       OUT_BATCH(_3DSTATE_CLEAR_PARAMS << 16 | (2 - 2));
547       OUT_BATCH(0);
548       ADVANCE_BATCH();
549    }
550
551    /* 3DSTATE_DRAWING_RECTANGLE */
552    {
553       BEGIN_BATCH(4);
554       OUT_BATCH(_3DSTATE_DRAWING_RECTANGLE << 16 | (4 - 2));
555       OUT_BATCH(0);
556       OUT_BATCH(((params->x1 - 1) & 0xffff) |
557                 ((params->y1 - 1) << 16));
558       OUT_BATCH(0);
559       ADVANCE_BATCH();
560    }
561
562    /* 3DPRIMITIVE */
563    {
564      BEGIN_BATCH(6);
565      OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
566                _3DPRIM_RECTLIST << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
567                GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL);
568      OUT_BATCH(3); /* vertex count per instance */
569      OUT_BATCH(0);
570      OUT_BATCH(1); /* instance count */
571      OUT_BATCH(0);
572      OUT_BATCH(0);
573      ADVANCE_BATCH();
574    }
575
576    /* See comments above at first invocation of intel_flush() in
577     * gen6_blorp_emit_batch_head().
578     */
579    intel_flush(ctx);
580
581    /* Be safe. */
582    brw->state.dirty.brw = ~0;
583    brw->state.dirty.cache = ~0;
584 }
585
586 /**
587  * \param out_offset is relative to
588  *        CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
589  */
590 void
591 gen6_blorp_emit_depth_stencil_state(struct brw_context *brw,
592                                     const brw_blorp_params *params,
593                                     uint32_t *out_offset)
594 {
595    struct gen6_depth_stencil_state *state;
596    state = (struct gen6_depth_stencil_state *)
597       brw_state_batch(brw, AUB_TRACE_DEPTH_STENCIL_STATE,
598                       sizeof(*state), 64,
599                       out_offset);
600    memset(state, 0, sizeof(*state));
601
602    /* See the following sections of the Sandy Bridge PRM, Volume 1, Part2:
603     *   - 7.5.3.1 Depth Buffer Clear
604     *   - 7.5.3.2 Depth Buffer Resolve
605     *   - 7.5.3.3 Hierarchical Depth Buffer Resolve
606     */
607    state->ds2.depth_write_enable = 1;
608    if (params->hiz_op == GEN6_HIZ_OP_DEPTH_RESOLVE) {
609       state->ds2.depth_test_enable = 1;
610       state->ds2.depth_test_func = COMPAREFUNC_NEVER;
611    }
612 }
613
614 /** \see intel_context::vtbl::resolve_hiz_slice */
615 void
616 gen6_resolve_hiz_slice(struct intel_context *intel,
617                        struct intel_mipmap_tree *mt,
618                        uint32_t level,
619                        uint32_t layer)
620 {
621    brw_hiz_op_params params(mt, level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
622    gen6_blorp_exec(intel, &params);
623 }
624
625 /** \see intel_context::vtbl::resolve_depth_slice */
626 void
627 gen6_resolve_depth_slice(struct intel_context *intel,
628                          struct intel_mipmap_tree *mt,
629                          uint32_t level,
630                          uint32_t layer)
631 {
632    brw_hiz_op_params params(mt, level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
633    gen6_blorp_exec(intel, &params);
634 }