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