OSDN Git Service

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