OSDN Git Service

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