OSDN Git Service

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