i965/blorp: Set the dynamic state upper bound.
[profile/ivi/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
104
105 /**
106  * CMD_STATE_BASE_ADDRESS
107  *
108  * From the Sandy Bridge PRM, Volume 1, Part 1, Table STATE_BASE_ADDRESS:
109  *     The following commands must be reissued following any change to the
110  *     base addresses:
111  *         3DSTATE_CC_POINTERS
112  *         3DSTATE_BINDING_TABLE_POINTERS
113  *         3DSTATE_SAMPLER_STATE_POINTERS
114  *         3DSTATE_VIEWPORT_STATE_POINTERS
115  *         MEDIA_STATE_POINTERS
116  */
117 void
118 gen6_blorp_emit_state_base_address(struct brw_context *brw,
119                                    const brw_blorp_params *params)
120 {
121    struct intel_context *intel = &brw->intel;
122
123    BEGIN_BATCH(10);
124    OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (10 - 2));
125    OUT_BATCH(1); /* GeneralStateBaseAddressModifyEnable */
126    /* SurfaceStateBaseAddress */
127    OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0, 1);
128    /* DynamicStateBaseAddress */
129    OUT_RELOC(intel->batch.bo, (I915_GEM_DOMAIN_RENDER |
130                                I915_GEM_DOMAIN_INSTRUCTION), 0, 1);
131    OUT_BATCH(1); /* IndirectObjectBaseAddress */
132    if (params->use_wm_prog) {
133       OUT_RELOC(brw->cache.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
134                 1); /* Instruction base address: shader kernels */
135    } else {
136       OUT_BATCH(1); /* InstructionBaseAddress */
137    }
138    OUT_BATCH(1); /* GeneralStateUpperBound */
139    /* Dynamic state upper bound.  Although the documentation says that
140     * programming it to zero will cause it to be ignored, that is a lie.
141     * If this isn't programmed to a real bound, the sampler border color
142     * pointer is rejected, causing border color to mysteriously fail.
143     */
144    OUT_BATCH(0xfffff001);
145    OUT_BATCH(1); /* IndirectObjectUpperBound*/
146    OUT_BATCH(1); /* InstructionAccessUpperBound */
147    ADVANCE_BATCH();
148 }
149
150
151 void
152 gen6_blorp_emit_vertices(struct brw_context *brw,
153                          const brw_blorp_params *params)
154 {
155    struct intel_context *intel = &brw->intel;
156    uint32_t vertex_offset;
157
158    /* Setup VBO for the rectangle primitive..
159     *
160     * A rectangle primitive (3DPRIM_RECTLIST) consists of only three
161     * vertices. The vertices reside in screen space with DirectX coordinates
162     * (that is, (0, 0) is the upper left corner).
163     *
164     *   v2 ------ implied
165     *    |        |
166     *    |        |
167     *   v0 ----- v1
168     *
169     * Since the VS is disabled, the clipper loads each VUE directly from
170     * the URB. This is controlled by the 3DSTATE_VERTEX_BUFFERS and
171     * 3DSTATE_VERTEX_ELEMENTS packets below. The VUE contents are as follows:
172     *   dw0: Reserved, MBZ.
173     *   dw1: Render Target Array Index. The HiZ op does not use indexed
174     *        vertices, so set the dword to 0.
175     *   dw2: Viewport Index. The HiZ op disables viewport mapping and
176     *        scissoring, so set the dword to 0.
177     *   dw3: Point Width: The HiZ op does not emit the POINTLIST primitive, so
178     *        set the dword to 0.
179     *   dw4: Vertex Position X.
180     *   dw5: Vertex Position Y.
181     *   dw6: Vertex Position Z.
182     *   dw7: Vertex Position W.
183     *
184     * For details, see the Sandybridge PRM, Volume 2, Part 1, Section 1.5.1
185     * "Vertex URB Entry (VUE) Formats".
186     */
187    {
188       float *vertex_data;
189
190       const float vertices[GEN6_BLORP_VBO_SIZE] = {
191          /* v0 */ 0, 0, 0, 0,     params->x0, params->y1, 0, 1,
192          /* v1 */ 0, 0, 0, 0,     params->x1, params->y1, 0, 1,
193          /* v2 */ 0, 0, 0, 0,     params->x0, params->y0, 0, 1,
194       };
195
196       vertex_data = (float *) brw_state_batch(brw, AUB_TRACE_VERTEX_BUFFER,
197                                               GEN6_BLORP_VBO_SIZE, 32,
198                                               &vertex_offset);
199       memcpy(vertex_data, vertices, GEN6_BLORP_VBO_SIZE);
200    }
201
202    /* 3DSTATE_VERTEX_BUFFERS */
203    {
204       const int num_buffers = 1;
205       const int batch_length = 1 + 4 * num_buffers;
206
207       uint32_t dw0 = GEN6_VB0_ACCESS_VERTEXDATA |
208                      (GEN6_BLORP_NUM_VUE_ELEMS * sizeof(float)) << BRW_VB0_PITCH_SHIFT;
209
210       if (intel->gen >= 7)
211          dw0 |= GEN7_VB0_ADDRESS_MODIFYENABLE;
212
213       BEGIN_BATCH(batch_length);
214       OUT_BATCH((_3DSTATE_VERTEX_BUFFERS << 16) | (batch_length - 2));
215       OUT_BATCH(dw0);
216       /* start address */
217       OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_VERTEX, 0,
218                 vertex_offset);
219       /* end address */
220       OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_VERTEX, 0,
221                 vertex_offset + GEN6_BLORP_VBO_SIZE - 1);
222       OUT_BATCH(0);
223       ADVANCE_BATCH();
224    }
225
226    /* 3DSTATE_VERTEX_ELEMENTS
227     *
228     * Fetch dwords 0 - 7 from each VUE. See the comments above where
229     * the vertex_bo is filled with data.
230     */
231    {
232       const int num_elements = 2;
233       const int batch_length = 1 + 2 * num_elements;
234
235       BEGIN_BATCH(batch_length);
236       OUT_BATCH((_3DSTATE_VERTEX_ELEMENTS << 16) | (batch_length - 2));
237       /* Element 0 */
238       OUT_BATCH(GEN6_VE0_VALID |
239                 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT |
240                 0 << BRW_VE0_SRC_OFFSET_SHIFT);
241       OUT_BATCH(BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT |
242                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_1_SHIFT |
243                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_2_SHIFT |
244                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_3_SHIFT);
245       /* Element 1 */
246       OUT_BATCH(GEN6_VE0_VALID |
247                 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT |
248                 16 << BRW_VE0_SRC_OFFSET_SHIFT);
249       OUT_BATCH(BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT |
250                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_1_SHIFT |
251                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_2_SHIFT |
252                 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_3_SHIFT);
253       ADVANCE_BATCH();
254    }
255 }
256
257
258 /* 3DSTATE_URB
259  *
260  * Assign the entire URB to the VS. Even though the VS disabled, URB space
261  * is still needed because the clipper loads the VUE's from the URB. From
262  * the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE,
263  * Dword 1.15:0 "VS Number of URB Entries":
264  *     This field is always used (even if VS Function Enable is DISABLED).
265  *
266  * The warning below appears in the PRM (Section 3DSTATE_URB), but we can
267  * safely ignore it because this batch contains only one draw call.
268  *     Because of URB corruption caused by allocating a previous GS unit
269  *     URB entry to the VS unit, software is required to send a “GS NULL
270  *     Fence” (Send URB fence with VS URB size == 1 and GS URB size == 0)
271  *     plus a dummy DRAW call before any case where VS will be taking over
272  *     GS URB space.
273  */
274 static void
275 gen6_blorp_emit_urb_config(struct brw_context *brw,
276                            const brw_blorp_params *params)
277 {
278    struct intel_context *intel = &brw->intel;
279
280    BEGIN_BATCH(3);
281    OUT_BATCH(_3DSTATE_URB << 16 | (3 - 2));
282    OUT_BATCH(brw->urb.max_vs_entries << GEN6_URB_VS_ENTRIES_SHIFT);
283    OUT_BATCH(0);
284    ADVANCE_BATCH();
285 }
286
287
288 /* BLEND_STATE */
289 uint32_t
290 gen6_blorp_emit_blend_state(struct brw_context *brw,
291                             const brw_blorp_params *params)
292 {
293    uint32_t cc_blend_state_offset;
294
295    struct gen6_blend_state *blend = (struct gen6_blend_state *)
296       brw_state_batch(brw, AUB_TRACE_BLEND_STATE,
297                       sizeof(struct gen6_blend_state), 64,
298                       &cc_blend_state_offset);
299
300    memset(blend, 0, sizeof(*blend));
301
302    // TODO: handle other formats.
303    blend->blend1.pre_blend_clamp_enable = 1;
304    blend->blend1.post_blend_clamp_enable = 1;
305    blend->blend1.clamp_range = BRW_RENDERTARGET_CLAMPRANGE_FORMAT;
306
307    blend->blend1.write_disable_r = false;
308    blend->blend1.write_disable_g = false;
309    blend->blend1.write_disable_b = false;
310    blend->blend1.write_disable_a = false;
311
312    return cc_blend_state_offset;
313 }
314
315
316 /* CC_STATE */
317 uint32_t
318 gen6_blorp_emit_cc_state(struct brw_context *brw,
319                          const brw_blorp_params *params)
320 {
321    uint32_t cc_state_offset;
322
323    struct gen6_color_calc_state *cc = (struct gen6_color_calc_state *)
324       brw_state_batch(brw, AUB_TRACE_CC_STATE,
325                       sizeof(gen6_color_calc_state), 64,
326                       &cc_state_offset);
327    memset(cc, 0, sizeof(*cc));
328
329    return cc_state_offset;
330 }
331
332
333 /**
334  * \param out_offset is relative to
335  *        CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
336  */
337 uint32_t
338 gen6_blorp_emit_depth_stencil_state(struct brw_context *brw,
339                                     const brw_blorp_params *params)
340 {
341    uint32_t depthstencil_offset;
342
343    struct gen6_depth_stencil_state *state;
344    state = (struct gen6_depth_stencil_state *)
345       brw_state_batch(brw, AUB_TRACE_DEPTH_STENCIL_STATE,
346                       sizeof(*state), 64,
347                       &depthstencil_offset);
348    memset(state, 0, sizeof(*state));
349
350    /* See the following sections of the Sandy Bridge PRM, Volume 1, Part2:
351     *   - 7.5.3.1 Depth Buffer Clear
352     *   - 7.5.3.2 Depth Buffer Resolve
353     *   - 7.5.3.3 Hierarchical Depth Buffer Resolve
354     */
355    state->ds2.depth_write_enable = 1;
356    if (params->hiz_op == GEN6_HIZ_OP_DEPTH_RESOLVE) {
357       state->ds2.depth_test_enable = 1;
358       state->ds2.depth_test_func = COMPAREFUNC_NEVER;
359    }
360
361    return depthstencil_offset;
362 }
363
364
365 /* 3DSTATE_CC_STATE_POINTERS
366  *
367  * The pointer offsets are relative to
368  * CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
369  *
370  * The HiZ op doesn't use BLEND_STATE or COLOR_CALC_STATE.
371  */
372 static void
373 gen6_blorp_emit_cc_state_pointers(struct brw_context *brw,
374                                   const brw_blorp_params *params,
375                                   uint32_t cc_blend_state_offset,
376                                   uint32_t depthstencil_offset,
377                                   uint32_t cc_state_offset)
378 {
379    struct intel_context *intel = &brw->intel;
380
381    BEGIN_BATCH(4);
382    OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (4 - 2));
383    OUT_BATCH(cc_blend_state_offset | 1); /* BLEND_STATE offset */
384    OUT_BATCH(depthstencil_offset | 1); /* DEPTH_STENCIL_STATE offset */
385    OUT_BATCH(cc_state_offset | 1); /* COLOR_CALC_STATE offset */
386    ADVANCE_BATCH();
387 }
388
389
390 /* WM push constants */
391 uint32_t
392 gen6_blorp_emit_wm_constants(struct brw_context *brw,
393                              const brw_blorp_params *params)
394 {
395    uint32_t wm_push_const_offset;
396
397    void *constants = brw_state_batch(brw, AUB_TRACE_WM_CONSTANTS,
398                                      sizeof(params->wm_push_consts),
399                                      32, &wm_push_const_offset);
400    memcpy(constants, &params->wm_push_consts,
401           sizeof(params->wm_push_consts));
402
403    return wm_push_const_offset;
404 }
405
406
407 /* SURFACE_STATE for renderbuffer or texture surface (see
408  * brw_update_renderbuffer_surface and brw_update_texture_surface)
409  */
410 static uint32_t
411 gen6_blorp_emit_surface_state(struct brw_context *brw,
412                               const brw_blorp_params *params,
413                               const brw_blorp_surface_info *surface,
414                               uint32_t read_domains, uint32_t write_domain)
415 {
416    uint32_t wm_surf_offset;
417    uint32_t width, height;
418    surface->get_miplevel_dims(&width, &height);
419    if (surface->num_samples > 0) { /* TODO: seems clumsy */
420       width /= 2;
421       height /= 2;
422    }
423    if (surface->map_stencil_as_y_tiled) {
424       width *= 2;
425       height /= 2;
426    }
427    struct intel_region *region = surface->mt->region;
428
429    /* TODO: handle other formats */
430    uint32_t format = surface->map_stencil_as_y_tiled
431       ? BRW_SURFACEFORMAT_R8_UNORM : BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
432
433    uint32_t *surf = (uint32_t *)
434       brw_state_batch(brw, AUB_TRACE_SURFACE_STATE, 6 * 4, 32,
435                       &wm_surf_offset);
436
437    surf[0] = (BRW_SURFACE_2D << BRW_SURFACE_TYPE_SHIFT |
438               BRW_SURFACE_MIPMAPLAYOUT_BELOW << BRW_SURFACE_MIPLAYOUT_SHIFT |
439               BRW_SURFACE_CUBEFACE_ENABLES |
440               format << BRW_SURFACE_FORMAT_SHIFT);
441
442    /* reloc */
443    surf[1] = region->bo->offset; /* No tile offsets needed */
444
445    surf[2] = (0 << BRW_SURFACE_LOD_SHIFT |
446               (width - 1) << BRW_SURFACE_WIDTH_SHIFT |
447               (height - 1) << BRW_SURFACE_HEIGHT_SHIFT);
448
449    uint32_t tiling = surface->map_stencil_as_y_tiled
450       ? BRW_SURFACE_TILED | BRW_SURFACE_TILED_Y
451       : brw_get_surface_tiling_bits(region->tiling);
452    uint32_t pitch_bytes = region->pitch * region->cpp;
453    if (surface->map_stencil_as_y_tiled)
454       pitch_bytes *= 2;
455    surf[3] = (tiling |
456               0 << BRW_SURFACE_DEPTH_SHIFT |
457               (pitch_bytes - 1) << BRW_SURFACE_PITCH_SHIFT);
458
459    surf[4] = brw_get_surface_num_multisamples(surface->num_samples);
460
461    surf[5] = (0 << BRW_SURFACE_X_OFFSET_SHIFT |
462               0 << BRW_SURFACE_Y_OFFSET_SHIFT |
463               (surface->mt->align_h == 4 ?
464                BRW_SURFACE_VERTICAL_ALIGN_ENABLE : 0));
465
466    /* Emit relocation to surface contents */
467    drm_intel_bo_emit_reloc(brw->intel.batch.bo,
468                            wm_surf_offset + 4,
469                            region->bo,
470                            surf[1] - region->bo->offset,
471                            read_domains, write_domain);
472
473    return wm_surf_offset;
474 }
475
476
477 /* BINDING_TABLE.  See brw_wm_binding_table(). */
478 uint32_t
479 gen6_blorp_emit_binding_table(struct brw_context *brw,
480                               const brw_blorp_params *params,
481                               uint32_t wm_surf_offset_renderbuffer,
482                               uint32_t wm_surf_offset_texture)
483 {
484    uint32_t wm_bind_bo_offset;
485    uint32_t *bind = (uint32_t *)
486       brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
487                       sizeof(uint32_t) *
488                       BRW_BLORP_NUM_BINDING_TABLE_ENTRIES,
489                       32, /* alignment */
490                       &wm_bind_bo_offset);
491    bind[BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX] =
492       wm_surf_offset_renderbuffer;
493    bind[BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX] = wm_surf_offset_texture;
494
495    return wm_bind_bo_offset;
496 }
497
498
499 /**
500  * SAMPLER_STATE.  See brw_update_sampler_state().
501  */
502 static uint32_t
503 gen6_blorp_emit_sampler_state(struct brw_context *brw,
504                               const brw_blorp_params *params)
505 {
506    uint32_t sampler_offset;
507
508    struct brw_sampler_state *sampler = (struct brw_sampler_state *)
509       brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
510                       sizeof(struct brw_sampler_state),
511                       32, &sampler_offset);
512    memset(sampler, 0, sizeof(*sampler));
513
514    sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
515    sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
516    sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
517
518    sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
519    sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
520    sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
521
522    sampler->ss0.min_mag_neq = 1;
523
524    /* Set LOD bias: 
525     */
526    sampler->ss0.lod_bias = 0;
527
528    sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
529    sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
530
531    /* Set BaseMipLevel, MaxLOD, MinLOD: 
532     *
533     * XXX: I don't think that using firstLevel, lastLevel works,
534     * because we always setup the surface state as if firstLevel ==
535     * level zero.  Probably have to subtract firstLevel from each of
536     * these:
537     */
538    sampler->ss0.base_level = U_FIXED(0, 1);
539
540    sampler->ss1.max_lod = U_FIXED(0, 6);
541    sampler->ss1.min_lod = U_FIXED(0, 6);
542
543    sampler->ss3.non_normalized_coord = 1;
544
545    sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
546       BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
547       BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
548    sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
549       BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
550       BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
551
552    return sampler_offset;
553 }
554
555
556 /**
557  * 3DSTATE_SAMPLER_STATE_POINTERS.  See upload_sampler_state_pointers().
558  */
559 static void
560 gen6_blorp_emit_sampler_state_pointers(struct brw_context *brw,
561                                        const brw_blorp_params *params,
562                                        uint32_t sampler_offset)
563 {
564    struct intel_context *intel = &brw->intel;
565
566    BEGIN_BATCH(4);
567    OUT_BATCH(_3DSTATE_SAMPLER_STATE_POINTERS << 16 |
568              VS_SAMPLER_STATE_CHANGE |
569              GS_SAMPLER_STATE_CHANGE |
570              PS_SAMPLER_STATE_CHANGE |
571              (4 - 2));
572    OUT_BATCH(0); /* VS */
573    OUT_BATCH(0); /* GS */
574    OUT_BATCH(sampler_offset);
575    ADVANCE_BATCH();
576 }
577
578
579 /* 3DSTATE_VS
580  *
581  * Disable vertex shader.
582  */
583 void
584 gen6_blorp_emit_vs_disable(struct brw_context *brw,
585                            const brw_blorp_params *params)
586 {
587    struct intel_context *intel = &brw->intel;
588
589    if (intel->gen == 6) {
590       /* From the BSpec, Volume 2a, Part 3 "Vertex Shader", Section
591        * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
592        *
593        *   [DevSNB] A pipeline flush must be programmed prior to a
594        *   3DSTATE_VS command that causes the VS Function Enable to
595        *   toggle. Pipeline flush can be executed by sending a PIPE_CONTROL
596        *   command with CS stall bit set and a post sync operation.
597        */
598       intel_emit_post_sync_nonzero_flush(intel);
599    }
600
601    BEGIN_BATCH(6);
602    OUT_BATCH(_3DSTATE_VS << 16 | (6 - 2));
603    OUT_BATCH(0);
604    OUT_BATCH(0);
605    OUT_BATCH(0);
606    OUT_BATCH(0);
607    OUT_BATCH(0);
608    ADVANCE_BATCH();
609 }
610
611
612 /* 3DSTATE_GS
613  *
614  * Disable the geometry shader.
615  */
616 void
617 gen6_blorp_emit_gs_disable(struct brw_context *brw,
618                            const brw_blorp_params *params)
619 {
620    struct intel_context *intel = &brw->intel;
621
622    BEGIN_BATCH(7);
623    OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
624    OUT_BATCH(0);
625    OUT_BATCH(0);
626    OUT_BATCH(0);
627    OUT_BATCH(0);
628    OUT_BATCH(0);
629    OUT_BATCH(0);
630    ADVANCE_BATCH();
631 }
632
633
634 /* 3DSTATE_CLIP
635  *
636  * Disable the clipper.
637  *
638  * The BLORP op emits a rectangle primitive, which requires clipping to
639  * be disabled. From page 10 of the Sandy Bridge PRM Volume 2 Part 1
640  * Section 1.3 "3D Primitives Overview":
641  *    RECTLIST:
642  *    Either the CLIP unit should be DISABLED, or the CLIP unit's Clip
643  *    Mode should be set to a value other than CLIPMODE_NORMAL.
644  *
645  * Also disable perspective divide. This doesn't change the clipper's
646  * output, but does spare a few electrons.
647  */
648 void
649 gen6_blorp_emit_clip_disable(struct brw_context *brw,
650                              const brw_blorp_params *params)
651 {
652    struct intel_context *intel = &brw->intel;
653
654    BEGIN_BATCH(4);
655    OUT_BATCH(_3DSTATE_CLIP << 16 | (4 - 2));
656    OUT_BATCH(0);
657    OUT_BATCH(GEN6_CLIP_PERSPECTIVE_DIVIDE_DISABLE);
658    OUT_BATCH(0);
659    ADVANCE_BATCH();
660 }
661
662
663 /* 3DSTATE_SF
664  *
665  * Disable ViewportTransformEnable (dw2.1)
666  *
667  * From the SandyBridge PRM, Volume 2, Part 1, Section 1.3, "3D
668  * Primitives Overview":
669  *     RECTLIST: Viewport Mapping must be DISABLED (as is typical with the
670  *     use of screen- space coordinates).
671  *
672  * A solid rectangle must be rendered, so set FrontFaceFillMode (dw2.4:3)
673  * and BackFaceFillMode (dw2.5:6) to SOLID(0).
674  *
675  * From the Sandy Bridge PRM, Volume 2, Part 1, Section
676  * 6.4.1.1 3DSTATE_SF, Field FrontFaceFillMode:
677  *     SOLID: Any triangle or rectangle object found to be front-facing
678  *     is rendered as a solid object. This setting is required when
679  *     (rendering rectangle (RECTLIST) objects.
680  */
681 static void
682 gen6_blorp_emit_sf_config(struct brw_context *brw,
683                           const brw_blorp_params *params)
684 {
685    struct intel_context *intel = &brw->intel;
686
687    BEGIN_BATCH(20);
688    OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
689    OUT_BATCH((1 - 1) << GEN6_SF_NUM_OUTPUTS_SHIFT | /* only position */
690              1 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
691              0 << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT);
692    OUT_BATCH(0); /* dw2 */
693    OUT_BATCH(params->num_samples > 0 ? GEN6_SF_MSRAST_ON_PATTERN : 0);
694    for (int i = 0; i < 16; ++i)
695       OUT_BATCH(0);
696    ADVANCE_BATCH();
697 }
698
699
700 /**
701  * Enable or disable thread dispatch and set the HiZ op appropriately.
702  */
703 static void
704 gen6_blorp_emit_wm_config(struct brw_context *brw,
705                           const brw_blorp_params *params,
706                           uint32_t prog_offset,
707                           brw_blorp_prog_data *prog_data)
708 {
709    struct intel_context *intel = &brw->intel;
710    uint32_t dw2, dw4, dw5, dw6;
711
712    /* Even when thread dispatch is disabled, max threads (dw5.25:31) must be
713     * nonzero to prevent the GPU from hanging. See the valid ranges in the
714     * BSpec, Volume 2a.11 Windower, Section 3DSTATE_WM, Dword 5.25:31
715     * "Maximum Number Of Threads".
716     *
717     * To be safe (and to minimize extraneous code) we go ahead and fully
718     * configure the WM state whether or not there is a WM program.
719     */
720
721    dw2 = dw4 = dw5 = dw6 = 0;
722    switch (params->hiz_op) {
723    case GEN6_HIZ_OP_DEPTH_CLEAR:
724       dw4 |= GEN6_WM_DEPTH_CLEAR;
725       break;
726    case GEN6_HIZ_OP_DEPTH_RESOLVE:
727       dw4 |= GEN6_WM_DEPTH_RESOLVE;
728       break;
729    case GEN6_HIZ_OP_HIZ_RESOLVE:
730       dw4 |= GEN6_WM_HIERARCHICAL_DEPTH_RESOLVE;
731       break;
732    case GEN6_HIZ_OP_NONE:
733       break;
734    default:
735       assert(0);
736       break;
737    }
738    dw4 |= GEN6_WM_STATISTICS_ENABLE;
739    dw5 |= GEN6_WM_LINE_AA_WIDTH_1_0;
740    dw5 |= GEN6_WM_LINE_END_CAP_AA_WIDTH_0_5;
741    dw5 |= (brw->max_wm_threads - 1) << GEN6_WM_MAX_THREADS_SHIFT;
742    dw6 |= 0 << GEN6_WM_BARYCENTRIC_INTERPOLATION_MODE_SHIFT; /* No interp */
743    dw6 |= 0 << GEN6_WM_NUM_SF_OUTPUTS_SHIFT; /* No inputs from SF */
744    if (params->use_wm_prog) {
745       dw2 |= 1 << GEN6_WM_SAMPLER_COUNT_SHIFT; /* Up to 4 samplers */
746       dw4 |= prog_data->first_curbe_grf << GEN6_WM_DISPATCH_START_GRF_SHIFT_0;
747       dw5 |= GEN6_WM_16_DISPATCH_ENABLE;
748       dw5 |= GEN6_WM_KILL_ENABLE; /* TODO: temporarily smash on */
749       dw5 |= GEN6_WM_DISPATCH_ENABLE; /* We are rendering */
750    }
751
752    if (params->num_samples > 0) {
753       dw6 |= GEN6_WM_MSRAST_ON_PATTERN;
754       if (prog_data && prog_data->persample_msaa_dispatch)
755          dw6 |= GEN6_WM_MSDISPMODE_PERSAMPLE;
756       else
757          dw6 |= GEN6_WM_MSDISPMODE_PERPIXEL;
758    } else {
759       dw6 |= GEN6_WM_MSRAST_OFF_PIXEL;
760       dw6 |= GEN6_WM_MSDISPMODE_PERSAMPLE;
761    }
762
763    BEGIN_BATCH(9);
764    OUT_BATCH(_3DSTATE_WM << 16 | (9 - 2));
765    OUT_BATCH(params->use_wm_prog ? prog_offset : 0);
766    OUT_BATCH(dw2);
767    OUT_BATCH(0); /* No scratch needed */
768    OUT_BATCH(dw4);
769    OUT_BATCH(dw5);
770    OUT_BATCH(dw6);
771    OUT_BATCH(0); /* No other programs */
772    OUT_BATCH(0); /* No other programs */
773    ADVANCE_BATCH();
774 }
775
776
777 static void
778 gen6_blorp_emit_constant_ps(struct brw_context *brw,
779                             const brw_blorp_params *params,
780                             uint32_t wm_push_const_offset)
781 {
782    struct intel_context *intel = &brw->intel;
783
784    /* Make sure the push constants fill an exact integer number of
785     * registers.
786     */
787    assert(sizeof(brw_blorp_wm_push_constants) % 32 == 0);
788
789    /* There must be at least one register worth of push constant data. */
790    assert(BRW_BLORP_NUM_PUSH_CONST_REGS > 0);
791
792    /* Enable push constant buffer 0. */
793    BEGIN_BATCH(5);
794    OUT_BATCH(_3DSTATE_CONSTANT_PS << 16 |
795              GEN6_CONSTANT_BUFFER_0_ENABLE |
796              (5 - 2));
797    OUT_BATCH(wm_push_const_offset + (BRW_BLORP_NUM_PUSH_CONST_REGS - 1));
798    OUT_BATCH(0);
799    OUT_BATCH(0);
800    OUT_BATCH(0);
801    ADVANCE_BATCH();
802 }
803
804
805 /**
806  * 3DSTATE_BINDING_TABLE_POINTERS
807  */
808 static void
809 gen6_blorp_emit_binding_table_pointers(struct brw_context *brw,
810                                        const brw_blorp_params *params,
811                                        uint32_t wm_bind_bo_offset)
812 {
813    struct intel_context *intel = &brw->intel;
814
815    BEGIN_BATCH(4);
816    OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 |
817              GEN6_BINDING_TABLE_MODIFY_PS |
818              (4 - 2));
819    OUT_BATCH(0); /* vs -- ignored */
820    OUT_BATCH(0); /* gs -- ignored */
821    OUT_BATCH(wm_bind_bo_offset); /* wm/ps */
822    ADVANCE_BATCH();
823 }
824
825
826 static void
827 gen6_blorp_emit_depth_stencil_config(struct brw_context *brw,
828                                      const brw_blorp_params *params)
829 {
830    struct intel_context *intel = &brw->intel;
831    uint32_t draw_x, draw_y;
832    uint32_t tile_mask_x, tile_mask_y;
833
834    gen6_blorp_compute_tile_masks(params, &tile_mask_x, &tile_mask_y);
835    params->depth.get_draw_offsets(&draw_x, &draw_y);
836
837    /* 3DSTATE_DEPTH_BUFFER */
838    {
839       uint32_t width, height;
840       params->depth.get_miplevel_dims(&width, &height);
841
842       uint32_t tile_x = draw_x & tile_mask_x;
843       uint32_t tile_y = draw_y & tile_mask_y;
844       uint32_t offset =
845          intel_region_get_aligned_offset(params->depth.mt->region,
846                                          draw_x & ~tile_mask_x,
847                                          draw_y & ~tile_mask_y);
848
849       /* According to the Sandy Bridge PRM, volume 2 part 1, pp326-327
850        * (3DSTATE_DEPTH_BUFFER dw5), in the documentation for "Depth
851        * Coordinate Offset X/Y":
852        *
853        *   "The 3 LSBs of both offsets must be zero to ensure correct
854        *   alignment"
855        *
856        * We have no guarantee that tile_x and tile_y are correctly aligned,
857        * since they are determined by the mipmap layout, which is only aligned
858        * to multiples of 4.
859        *
860        * So, to avoid hanging the GPU, just smash the low order 3 bits of
861        * tile_x and tile_y to 0.  This is a temporary workaround until we come
862        * up with a better solution.
863        */
864       tile_x &= ~7;
865       tile_y &= ~7;
866
867       intel_emit_post_sync_nonzero_flush(intel);
868       intel_emit_depth_stall_flushes(intel);
869
870       BEGIN_BATCH(7);
871       OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (7 - 2));
872       uint32_t pitch_bytes =
873          params->depth.mt->region->pitch * params->depth.mt->region->cpp;
874       OUT_BATCH((pitch_bytes - 1) |
875                 params->depth_format << 18 |
876                 1 << 21 | /* separate stencil enable */
877                 1 << 22 | /* hiz enable */
878                 BRW_TILEWALK_YMAJOR << 26 |
879                 1 << 27 | /* y-tiled */
880                 BRW_SURFACE_2D << 29);
881       OUT_RELOC(params->depth.mt->region->bo,
882                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
883                 offset);
884       OUT_BATCH(BRW_SURFACE_MIPMAPLAYOUT_BELOW << 1 |
885                 (width + tile_x - 1) << 6 |
886                 (height + tile_y - 1) << 19);
887       OUT_BATCH(0);
888       OUT_BATCH(tile_x |
889                 tile_y << 16);
890       OUT_BATCH(0);
891       ADVANCE_BATCH();
892    }
893
894    /* 3DSTATE_HIER_DEPTH_BUFFER */
895    {
896       struct intel_region *hiz_region = params->depth.mt->hiz_mt->region;
897       uint32_t hiz_offset =
898          intel_region_get_aligned_offset(hiz_region,
899                                          draw_x & ~tile_mask_x,
900                                          (draw_y & ~tile_mask_y) / 2);
901
902       BEGIN_BATCH(3);
903       OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
904       OUT_BATCH(hiz_region->pitch * hiz_region->cpp - 1);
905       OUT_RELOC(hiz_region->bo,
906                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
907                 hiz_offset);
908       ADVANCE_BATCH();
909    }
910
911    /* 3DSTATE_STENCIL_BUFFER */
912    {
913       BEGIN_BATCH(3);
914       OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
915       OUT_BATCH(0);
916       OUT_BATCH(0);
917       ADVANCE_BATCH();
918    }
919 }
920
921
922 static void
923 gen6_blorp_emit_depth_disable(struct brw_context *brw,
924                               const brw_blorp_params *params)
925 {
926    struct intel_context *intel = &brw->intel;
927
928    BEGIN_BATCH(7);
929    OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (7 - 2));
930    OUT_BATCH((BRW_DEPTHFORMAT_D32_FLOAT << 18) |
931              (BRW_SURFACE_NULL << 29));
932    OUT_BATCH(0);
933    OUT_BATCH(0);
934    OUT_BATCH(0);
935    OUT_BATCH(0);
936    OUT_BATCH(0);
937    ADVANCE_BATCH();
938 }
939
940
941 /* 3DSTATE_CLEAR_PARAMS
942  *
943  * From the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE_CLEAR_PARAMS:
944  *   [DevSNB] 3DSTATE_CLEAR_PARAMS packet must follow the DEPTH_BUFFER_STATE
945  *   packet when HiZ is enabled and the DEPTH_BUFFER_STATE changes.
946  */
947 static void
948 gen6_blorp_emit_clear_params(struct brw_context *brw,
949                              const brw_blorp_params *params)
950 {
951    struct intel_context *intel = &brw->intel;
952
953    BEGIN_BATCH(2);
954    OUT_BATCH(_3DSTATE_CLEAR_PARAMS << 16 |
955              GEN5_DEPTH_CLEAR_VALID |
956              (2 - 2));
957    OUT_BATCH(params->depth.mt ? params->depth.mt->depth_clear_value : 0);
958    ADVANCE_BATCH();
959 }
960
961
962 /* 3DSTATE_DRAWING_RECTANGLE */
963 void
964 gen6_blorp_emit_drawing_rectangle(struct brw_context *brw,
965                                   const brw_blorp_params *params)
966 {
967    struct intel_context *intel = &brw->intel;
968
969    BEGIN_BATCH(4);
970    OUT_BATCH(_3DSTATE_DRAWING_RECTANGLE << 16 | (4 - 2));
971    OUT_BATCH(0);
972    OUT_BATCH(((params->x1 - 1) & 0xffff) |
973              ((params->y1 - 1) << 16));
974    OUT_BATCH(0);
975    ADVANCE_BATCH();
976 }
977
978 /* 3DSTATE_VIEWPORT_STATE_POINTERS */
979 void
980 gen6_blorp_emit_viewport_state(struct brw_context *brw,
981                                const brw_blorp_params *params)
982 {
983    struct intel_context *intel = &brw->intel;
984    struct brw_cc_viewport *ccv;
985    uint32_t cc_vp_offset;
986
987    ccv = (struct brw_cc_viewport *)brw_state_batch(brw, AUB_TRACE_CC_VP_STATE,
988                                                    sizeof(*ccv), 32,
989                                                    &cc_vp_offset);
990
991    ccv->min_depth = 0.0;
992    ccv->max_depth = 1.0;
993
994    BEGIN_BATCH(4);
995    OUT_BATCH(_3DSTATE_VIEWPORT_STATE_POINTERS << 16 | (4 - 2) |
996              GEN6_CC_VIEWPORT_MODIFY);
997    OUT_BATCH(0); /* clip VP */
998    OUT_BATCH(0); /* SF VP */
999    OUT_BATCH(cc_vp_offset);
1000    ADVANCE_BATCH();
1001 }
1002
1003
1004 /* 3DPRIMITIVE */
1005 static void
1006 gen6_blorp_emit_primitive(struct brw_context *brw,
1007                           const brw_blorp_params *params)
1008 {
1009    struct intel_context *intel = &brw->intel;
1010
1011    BEGIN_BATCH(6);
1012    OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
1013              _3DPRIM_RECTLIST << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
1014              GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL);
1015    OUT_BATCH(3); /* vertex count per instance */
1016    OUT_BATCH(0);
1017    OUT_BATCH(1); /* instance count */
1018    OUT_BATCH(0);
1019    OUT_BATCH(0);
1020    ADVANCE_BATCH();
1021 }
1022
1023
1024 /**
1025  * \brief Execute a blit or render pass operation.
1026  *
1027  * To execute the operation, this function manually constructs and emits a
1028  * batch to draw a rectangle primitive. The batchbuffer is flushed before
1029  * constructing and after emitting the batch.
1030  *
1031  * This function alters no GL state.
1032  */
1033 void
1034 gen6_blorp_exec(struct intel_context *intel,
1035                 const brw_blorp_params *params)
1036 {
1037    struct gl_context *ctx = &intel->ctx;
1038    struct brw_context *brw = brw_context(ctx);
1039    brw_blorp_prog_data *prog_data = NULL;
1040    uint32_t cc_blend_state_offset = 0;
1041    uint32_t cc_state_offset = 0;
1042    uint32_t depthstencil_offset;
1043    uint32_t wm_push_const_offset = 0;
1044    uint32_t wm_bind_bo_offset = 0;
1045
1046    uint32_t prog_offset = params->get_wm_prog(brw, &prog_data);
1047    gen6_blorp_emit_batch_head(brw, params);
1048    gen6_emit_3dstate_multisample(brw, params->num_samples);
1049    gen6_emit_3dstate_sample_mask(brw, params->num_samples);
1050    gen6_blorp_emit_state_base_address(brw, params);
1051    gen6_blorp_emit_vertices(brw, params);
1052    gen6_blorp_emit_urb_config(brw, params);
1053    if (params->use_wm_prog) {
1054       cc_blend_state_offset = gen6_blorp_emit_blend_state(brw, params);
1055       cc_state_offset = gen6_blorp_emit_cc_state(brw, params);
1056    }
1057    depthstencil_offset = gen6_blorp_emit_depth_stencil_state(brw, params);
1058    gen6_blorp_emit_cc_state_pointers(brw, params, cc_blend_state_offset,
1059                                      depthstencil_offset, cc_state_offset);
1060    if (params->use_wm_prog) {
1061       uint32_t wm_surf_offset_renderbuffer;
1062       uint32_t wm_surf_offset_texture;
1063       uint32_t sampler_offset;
1064       wm_push_const_offset = gen6_blorp_emit_wm_constants(brw, params);
1065       wm_surf_offset_renderbuffer =
1066          gen6_blorp_emit_surface_state(brw, params, &params->dst,
1067                                        I915_GEM_DOMAIN_RENDER,
1068                                        I915_GEM_DOMAIN_RENDER);
1069       wm_surf_offset_texture =
1070          gen6_blorp_emit_surface_state(brw, params, &params->src,
1071                                        I915_GEM_DOMAIN_SAMPLER, 0);
1072       wm_bind_bo_offset =
1073          gen6_blorp_emit_binding_table(brw, params,
1074                                        wm_surf_offset_renderbuffer,
1075                                        wm_surf_offset_texture);
1076       sampler_offset = gen6_blorp_emit_sampler_state(brw, params);
1077       gen6_blorp_emit_sampler_state_pointers(brw, params, sampler_offset);
1078    }
1079    gen6_blorp_emit_vs_disable(brw, params);
1080    gen6_blorp_emit_gs_disable(brw, params);
1081    gen6_blorp_emit_clip_disable(brw, params);
1082    gen6_blorp_emit_sf_config(brw, params);
1083    if (params->use_wm_prog)
1084       gen6_blorp_emit_constant_ps(brw, params, wm_push_const_offset);
1085    gen6_blorp_emit_wm_config(brw, params, prog_offset, prog_data);
1086    if (params->use_wm_prog)
1087       gen6_blorp_emit_binding_table_pointers(brw, params, wm_bind_bo_offset);
1088    gen6_blorp_emit_viewport_state(brw, params);
1089
1090    if (params->depth.mt)
1091       gen6_blorp_emit_depth_stencil_config(brw, params);
1092    else
1093       gen6_blorp_emit_depth_disable(brw, params);
1094    gen6_blorp_emit_clear_params(brw, params);
1095    gen6_blorp_emit_drawing_rectangle(brw, params);
1096    gen6_blorp_emit_primitive(brw, params);
1097
1098    /* See comments above at first invocation of intel_flush() in
1099     * gen6_blorp_emit_batch_head().
1100     */
1101    intel_flush(ctx);
1102
1103    /* Be safe. */
1104    brw->state.dirty.brw = ~0;
1105    brw->state.dirty.cache = ~0;
1106 }
1107