2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **********************************************************************/
29 * Keith Whitwell <keithw@vmware.com>
34 #include "intel_batchbuffer.h"
35 #include "intel_fbo.h"
36 #include "intel_mipmap_tree.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_defines.h"
42 #include "main/framebuffer.h"
43 #include "main/fbobject.h"
44 #include "main/glformats.h"
46 /* Constant single cliprect for framebuffer object or DRI2 drawing */
48 upload_drawing_rect(struct brw_context
*brw
)
50 struct gl_context
*ctx
= &brw
->ctx
;
51 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
52 const unsigned int fb_width
= _mesa_geometric_width(fb
);
53 const unsigned int fb_height
= _mesa_geometric_height(fb
);
56 OUT_BATCH(_3DSTATE_DRAWING_RECTANGLE
<< 16 | (4 - 2));
57 OUT_BATCH(0); /* xmin, ymin */
58 OUT_BATCH(((fb_width
- 1) & 0xffff) | ((fb_height
- 1) << 16));
63 const struct brw_tracked_state brw_drawing_rect
= {
66 .brw
= BRW_NEW_BLORP
|
69 .emit
= upload_drawing_rect
73 * Upload pointers to the per-stage state.
75 * The state pointers in this packet are all relative to the general state
76 * base address set by CMD_STATE_BASE_ADDRESS, which is 0.
79 upload_pipelined_state_pointers(struct brw_context
*brw
)
82 /* Need to flush before changing clip max threads for errata. */
89 OUT_BATCH(_3DSTATE_PIPELINED_POINTERS
<< 16 | (7 - 2));
90 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
91 brw
->vs
.base
.state_offset
);
92 if (brw
->ff_gs
.prog_active
)
93 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
94 brw
->ff_gs
.state_offset
| 1);
97 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
98 brw
->clip
.state_offset
| 1);
99 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
100 brw
->sf
.state_offset
);
101 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
102 brw
->wm
.base
.state_offset
);
103 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
104 brw
->cc
.state_offset
);
107 brw
->ctx
.NewDriverState
|= BRW_NEW_PSP
;
111 upload_psp_urb_cbs(struct brw_context
*brw
)
113 upload_pipelined_state_pointers(brw
);
114 brw_upload_urb_fence(brw
);
115 brw_upload_cs_urb_state(brw
);
118 const struct brw_tracked_state brw_psp_urb_cbs
= {
121 .brw
= BRW_NEW_BATCH
|
123 BRW_NEW_FF_GS_PROG_DATA
|
124 BRW_NEW_GEN4_UNIT_STATE
|
125 BRW_NEW_STATE_BASE_ADDRESS
|
128 .emit
= upload_psp_urb_cbs
,
132 brw_depthbuffer_format(struct brw_context
*brw
)
134 struct gl_context
*ctx
= &brw
->ctx
;
135 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
136 struct intel_renderbuffer
*drb
= intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
137 struct intel_renderbuffer
*srb
;
140 (srb
= intel_get_renderbuffer(fb
, BUFFER_STENCIL
)) &&
141 !srb
->mt
->stencil_mt
&&
142 (intel_rb_format(srb
) == MESA_FORMAT_Z24_UNORM_S8_UINT
||
143 intel_rb_format(srb
) == MESA_FORMAT_Z32_FLOAT_S8X24_UINT
)) {
148 return BRW_DEPTHFORMAT_D32_FLOAT
;
150 return brw_depth_format(brw
, drb
->mt
->format
);
154 * Returns the mask of how many bits of x and y must be handled through the
155 * depthbuffer's draw offset x and y fields.
157 * The draw offset x/y field of the depthbuffer packet is unfortunately shared
158 * between the depth, hiz, and stencil buffers. Because it can be hard to get
159 * all 3 to agree on this value, we want to do as much drawing offset
160 * adjustment as possible by moving the base offset of the 3 buffers, which is
161 * restricted to tile boundaries.
163 * For each buffer, the remainder must be applied through the x/y draw offset.
164 * This returns the worst-case mask of the low bits that have to go into the
165 * packet. If the 3 buffers don't agree on the drawing offset ANDed with this
166 * mask, then we're in trouble.
169 brw_get_depthstencil_tile_masks(struct intel_mipmap_tree
*depth_mt
,
170 uint32_t depth_level
,
171 uint32_t depth_layer
,
172 struct intel_mipmap_tree
*stencil_mt
,
173 uint32_t *out_tile_mask_x
,
174 uint32_t *out_tile_mask_y
)
176 uint32_t tile_mask_x
= 0, tile_mask_y
= 0;
179 intel_get_tile_masks(depth_mt
->tiling
, depth_mt
->tr_mode
,
181 &tile_mask_x
, &tile_mask_y
);
183 if (intel_miptree_level_has_hiz(depth_mt
, depth_level
)) {
184 uint32_t hiz_tile_mask_x
, hiz_tile_mask_y
;
185 intel_get_tile_masks(depth_mt
->hiz_buf
->mt
->tiling
,
186 depth_mt
->hiz_buf
->mt
->tr_mode
,
187 depth_mt
->hiz_buf
->mt
->cpp
,
191 /* Each HiZ row represents 2 rows of pixels */
192 hiz_tile_mask_y
= hiz_tile_mask_y
<< 1 | 1;
194 tile_mask_x
|= hiz_tile_mask_x
;
195 tile_mask_y
|= hiz_tile_mask_y
;
200 if (stencil_mt
->stencil_mt
)
201 stencil_mt
= stencil_mt
->stencil_mt
;
203 if (stencil_mt
->format
== MESA_FORMAT_S_UINT8
) {
204 /* Separate stencil buffer uses 64x64 tiles. */
208 uint32_t stencil_tile_mask_x
, stencil_tile_mask_y
;
209 intel_get_tile_masks(stencil_mt
->tiling
,
212 &stencil_tile_mask_x
,
213 &stencil_tile_mask_y
);
215 tile_mask_x
|= stencil_tile_mask_x
;
216 tile_mask_y
|= stencil_tile_mask_y
;
220 *out_tile_mask_x
= tile_mask_x
;
221 *out_tile_mask_y
= tile_mask_y
;
224 static struct intel_mipmap_tree
*
225 get_stencil_miptree(struct intel_renderbuffer
*irb
)
229 if (irb
->mt
->stencil_mt
)
230 return irb
->mt
->stencil_mt
;
235 brw_workaround_depthstencil_alignment(struct brw_context
*brw
,
236 GLbitfield clear_mask
)
238 struct gl_context
*ctx
= &brw
->ctx
;
239 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
240 bool rebase_depth
= false;
241 bool rebase_stencil
= false;
242 struct intel_renderbuffer
*depth_irb
= intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
243 struct intel_renderbuffer
*stencil_irb
= intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
244 struct intel_mipmap_tree
*depth_mt
= NULL
;
245 struct intel_mipmap_tree
*stencil_mt
= get_stencil_miptree(stencil_irb
);
246 uint32_t tile_x
= 0, tile_y
= 0, stencil_tile_x
= 0, stencil_tile_y
= 0;
247 uint32_t stencil_draw_x
= 0, stencil_draw_y
= 0;
248 bool invalidate_depth
= clear_mask
& BUFFER_BIT_DEPTH
;
249 bool invalidate_stencil
= clear_mask
& BUFFER_BIT_STENCIL
;
252 depth_mt
= depth_irb
->mt
;
254 /* Initialize brw->depthstencil to 'nop' workaround state.
256 brw
->depthstencil
.tile_x
= 0;
257 brw
->depthstencil
.tile_y
= 0;
258 brw
->depthstencil
.depth_offset
= 0;
259 brw
->depthstencil
.stencil_offset
= 0;
260 brw
->depthstencil
.hiz_offset
= 0;
261 brw
->depthstencil
.depth_mt
= NULL
;
262 brw
->depthstencil
.stencil_mt
= NULL
;
264 brw
->depthstencil
.depth_mt
= depth_mt
;
266 brw
->depthstencil
.stencil_mt
= get_stencil_miptree(stencil_irb
);
268 /* Gen6+ doesn't require the workarounds, since we always program the
269 * surface state at the start of the whole surface.
274 /* Check if depth buffer is in depth/stencil format. If so, then it's only
275 * safe to invalidate it if we're also clearing stencil, and both depth_irb
276 * and stencil_irb point to the same miptree.
278 * Note: it's not sufficient to check for the case where
279 * _mesa_get_format_base_format(depth_mt->format) == GL_DEPTH_STENCIL,
280 * because this fails to catch depth/stencil buffers on hardware that uses
281 * separate stencil. To catch that case, we check whether
282 * depth_mt->stencil_mt is non-NULL.
284 if (depth_irb
&& invalidate_depth
&&
285 (_mesa_get_format_base_format(depth_mt
->format
) == GL_DEPTH_STENCIL
||
286 depth_mt
->stencil_mt
)) {
287 invalidate_depth
= invalidate_stencil
&& depth_irb
&& stencil_irb
288 && depth_irb
->mt
== stencil_irb
->mt
;
291 uint32_t tile_mask_x
, tile_mask_y
;
292 brw_get_depthstencil_tile_masks(depth_mt
,
293 depth_mt
? depth_irb
->mt_level
: 0,
294 depth_mt
? depth_irb
->mt_layer
: 0,
296 &tile_mask_x
, &tile_mask_y
);
299 tile_x
= depth_irb
->draw_x
& tile_mask_x
;
300 tile_y
= depth_irb
->draw_y
& tile_mask_y
;
302 /* According to the Sandy Bridge PRM, volume 2 part 1, pp326-327
303 * (3DSTATE_DEPTH_BUFFER dw5), in the documentation for "Depth
304 * Coordinate Offset X/Y":
306 * "The 3 LSBs of both offsets must be zero to ensure correct
309 if (tile_x
& 7 || tile_y
& 7)
312 /* We didn't even have intra-tile offsets before g45. */
313 if (!brw
->has_surface_tile_offset
) {
314 if (tile_x
|| tile_y
)
319 perf_debug("HW workaround: blitting depth level %d to a temporary "
320 "to fix alignment (depth tile offset %d,%d)\n",
321 depth_irb
->mt_level
, tile_x
, tile_y
);
322 intel_renderbuffer_move_to_temp(brw
, depth_irb
, invalidate_depth
);
323 /* In the case of stencil_irb being the same packed depth/stencil
324 * texture but not the same rb, make it point at our rebased mt, too.
327 stencil_irb
!= depth_irb
&&
328 stencil_irb
->mt
== depth_mt
) {
329 intel_miptree_reference(&stencil_irb
->mt
, depth_irb
->mt
);
330 intel_renderbuffer_set_draw_offset(stencil_irb
);
333 stencil_mt
= get_stencil_miptree(stencil_irb
);
335 tile_x
= depth_irb
->draw_x
& tile_mask_x
;
336 tile_y
= depth_irb
->draw_y
& tile_mask_y
;
340 stencil_mt
= get_stencil_miptree(stencil_irb
);
341 intel_miptree_get_image_offset(stencil_mt
,
342 stencil_irb
->mt_level
,
343 stencil_irb
->mt_layer
,
344 &stencil_draw_x
, &stencil_draw_y
);
345 int stencil_tile_x
= stencil_draw_x
& tile_mask_x
;
346 int stencil_tile_y
= stencil_draw_y
& tile_mask_y
;
348 /* If stencil doesn't match depth, then we'll need to rebase stencil
349 * as well. (if we hadn't decided to rebase stencil before, the
350 * post-stencil depth test will also rebase depth to try to match it
353 if (tile_x
!= stencil_tile_x
||
354 tile_y
!= stencil_tile_y
) {
355 rebase_stencil
= true;
360 /* If we have (just) stencil, check it for ignored low bits as well */
362 intel_miptree_get_image_offset(stencil_mt
,
363 stencil_irb
->mt_level
,
364 stencil_irb
->mt_layer
,
365 &stencil_draw_x
, &stencil_draw_y
);
366 stencil_tile_x
= stencil_draw_x
& tile_mask_x
;
367 stencil_tile_y
= stencil_draw_y
& tile_mask_y
;
369 if (stencil_tile_x
& 7 || stencil_tile_y
& 7)
370 rebase_stencil
= true;
372 if (!brw
->has_surface_tile_offset
) {
373 if (stencil_tile_x
|| stencil_tile_y
)
374 rebase_stencil
= true;
378 if (rebase_stencil
) {
379 perf_debug("HW workaround: blitting stencil level %d to a temporary "
380 "to fix alignment (stencil tile offset %d,%d)\n",
381 stencil_irb
->mt_level
, stencil_tile_x
, stencil_tile_y
);
383 intel_renderbuffer_move_to_temp(brw
, stencil_irb
, invalidate_stencil
);
384 stencil_mt
= get_stencil_miptree(stencil_irb
);
386 intel_miptree_get_image_offset(stencil_mt
,
387 stencil_irb
->mt_level
,
388 stencil_irb
->mt_layer
,
389 &stencil_draw_x
, &stencil_draw_y
);
390 stencil_tile_x
= stencil_draw_x
& tile_mask_x
;
391 stencil_tile_y
= stencil_draw_y
& tile_mask_y
;
393 if (depth_irb
&& depth_irb
->mt
== stencil_irb
->mt
) {
394 intel_miptree_reference(&depth_irb
->mt
, stencil_irb
->mt
);
395 intel_renderbuffer_set_draw_offset(depth_irb
);
396 } else if (depth_irb
&& !rebase_depth
) {
397 if (tile_x
!= stencil_tile_x
||
398 tile_y
!= stencil_tile_y
) {
399 perf_debug("HW workaround: blitting depth level %d to a temporary "
400 "to match stencil level %d alignment (depth tile offset "
401 "%d,%d, stencil offset %d,%d)\n",
403 stencil_irb
->mt_level
,
405 stencil_tile_x
, stencil_tile_y
);
407 intel_renderbuffer_move_to_temp(brw
, depth_irb
, invalidate_depth
);
409 tile_x
= depth_irb
->draw_x
& tile_mask_x
;
410 tile_y
= depth_irb
->draw_y
& tile_mask_y
;
412 if (stencil_irb
&& stencil_irb
->mt
== depth_mt
) {
413 intel_miptree_reference(&stencil_irb
->mt
, depth_irb
->mt
);
414 intel_renderbuffer_set_draw_offset(stencil_irb
);
417 WARN_ONCE(stencil_tile_x
!= tile_x
||
418 stencil_tile_y
!= tile_y
,
419 "Rebased stencil tile offset (%d,%d) doesn't match depth "
420 "tile offset (%d,%d).\n",
421 stencil_tile_x
, stencil_tile_y
,
428 tile_x
= stencil_tile_x
;
429 tile_y
= stencil_tile_y
;
432 /* While we just tried to get everything aligned, we may have failed to do
433 * so in the case of rendering to array or 3D textures, where nonzero faces
434 * will still have an offset post-rebase. At least give an informative
437 WARN_ONCE((tile_x
& 7) || (tile_y
& 7),
438 "Depth/stencil buffer needs alignment to 8-pixel boundaries.\n"
439 "Truncating offset, bad rendering may occur.\n");
443 /* Now, after rebasing, save off the new dephtstencil state so the hardware
444 * packets can just dereference that without re-calculating tile offsets.
446 brw
->depthstencil
.tile_x
= tile_x
;
447 brw
->depthstencil
.tile_y
= tile_y
;
449 depth_mt
= depth_irb
->mt
;
450 brw
->depthstencil
.depth_mt
= depth_mt
;
451 brw
->depthstencil
.depth_offset
=
452 intel_miptree_get_aligned_offset(depth_mt
,
453 depth_irb
->draw_x
& ~tile_mask_x
,
454 depth_irb
->draw_y
& ~tile_mask_y
,
456 if (intel_renderbuffer_has_hiz(depth_irb
)) {
457 brw
->depthstencil
.hiz_offset
=
458 intel_miptree_get_aligned_offset(depth_mt
,
459 depth_irb
->draw_x
& ~tile_mask_x
,
460 (depth_irb
->draw_y
& ~tile_mask_y
) / 2,
465 stencil_mt
= get_stencil_miptree(stencil_irb
);
467 brw
->depthstencil
.stencil_mt
= stencil_mt
;
468 if (stencil_mt
->format
== MESA_FORMAT_S_UINT8
) {
469 /* Note: we can't compute the stencil offset using
470 * intel_region_get_aligned_offset(), because stencil_region claims
471 * that the region is untiled even though it's W tiled.
473 brw
->depthstencil
.stencil_offset
=
474 (stencil_draw_y
& ~tile_mask_y
) * stencil_mt
->pitch
+
475 (stencil_draw_x
& ~tile_mask_x
) * 64;
481 brw_emit_depthbuffer(struct brw_context
*brw
)
483 struct gl_context
*ctx
= &brw
->ctx
;
484 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
486 struct intel_renderbuffer
*depth_irb
= intel_get_renderbuffer(fb
, BUFFER_DEPTH
);
487 struct intel_renderbuffer
*stencil_irb
= intel_get_renderbuffer(fb
, BUFFER_STENCIL
);
488 struct intel_mipmap_tree
*depth_mt
= brw
->depthstencil
.depth_mt
;
489 struct intel_mipmap_tree
*stencil_mt
= brw
->depthstencil
.stencil_mt
;
490 uint32_t tile_x
= brw
->depthstencil
.tile_x
;
491 uint32_t tile_y
= brw
->depthstencil
.tile_y
;
492 bool hiz
= depth_irb
&& intel_renderbuffer_has_hiz(depth_irb
);
493 bool separate_stencil
= false;
494 uint32_t depth_surface_type
= BRW_SURFACE_NULL
;
495 uint32_t depthbuffer_format
= BRW_DEPTHFORMAT_D32_FLOAT
;
496 uint32_t depth_offset
= 0;
497 uint32_t width
= 1, height
= 1;
500 separate_stencil
= stencil_mt
->format
== MESA_FORMAT_S_UINT8
;
502 /* Gen7 supports only separate stencil */
503 assert(separate_stencil
|| brw
->gen
< 7);
506 /* If there's a packed depth/stencil bound to stencil only, we need to
507 * emit the packed depth/stencil buffer packet.
509 if (!depth_irb
&& stencil_irb
&& !separate_stencil
) {
510 depth_irb
= stencil_irb
;
511 depth_mt
= stencil_mt
;
514 if (depth_irb
&& depth_mt
) {
515 /* When 3DSTATE_DEPTH_BUFFER.Separate_Stencil_Enable is set, then
516 * 3DSTATE_DEPTH_BUFFER.Surface_Format is not permitted to be a packed
517 * depthstencil format.
519 * Gens prior to 7 require that HiZ_Enable and Separate_Stencil_Enable be
520 * set to the same value. Gens after 7 implicitly always set
521 * Separate_Stencil_Enable; software cannot disable it.
523 if ((brw
->gen
< 7 && hiz
) || brw
->gen
>= 7) {
524 assert(!_mesa_is_format_packed_depth_stencil(depth_mt
->format
));
527 /* Prior to Gen7, if using separate stencil, hiz must be enabled. */
528 assert(brw
->gen
>= 7 || !separate_stencil
|| hiz
);
530 assert(brw
->gen
< 6 || depth_mt
->tiling
== I915_TILING_Y
);
531 assert(!hiz
|| depth_mt
->tiling
== I915_TILING_Y
);
533 depthbuffer_format
= brw_depthbuffer_format(brw
);
534 depth_surface_type
= BRW_SURFACE_2D
;
535 depth_offset
= brw
->depthstencil
.depth_offset
;
536 width
= depth_irb
->Base
.Base
.Width
;
537 height
= depth_irb
->Base
.Base
.Height
;
538 } else if (separate_stencil
) {
540 * There exists a separate stencil buffer but no depth buffer.
542 * The stencil buffer inherits most of its fields from
543 * 3DSTATE_DEPTH_BUFFER: namely the tile walk, surface type, width, and
546 * The tiled bit must be set. From the Sandybridge PRM, Volume 2, Part 1,
547 * Section 7.5.5.1.1 3DSTATE_DEPTH_BUFFER, Bit 1.27 Tiled Surface:
548 * [DevGT+]: This field must be set to TRUE.
550 assert(brw
->has_separate_stencil
);
552 depth_surface_type
= BRW_SURFACE_2D
;
553 width
= stencil_irb
->Base
.Base
.Width
;
554 height
= stencil_irb
->Base
.Base
.Height
;
558 brw_render_cache_set_check_flush(brw
, depth_mt
->bo
);
560 brw_render_cache_set_check_flush(brw
, stencil_mt
->bo
);
562 brw
->vtbl
.emit_depth_stencil_hiz(brw
, depth_mt
, depth_offset
,
563 depthbuffer_format
, depth_surface_type
,
564 stencil_mt
, hiz
, separate_stencil
,
565 width
, height
, tile_x
, tile_y
);
569 brw_emit_depth_stencil_hiz(struct brw_context
*brw
,
570 struct intel_mipmap_tree
*depth_mt
,
571 uint32_t depth_offset
, uint32_t depthbuffer_format
,
572 uint32_t depth_surface_type
,
573 struct intel_mipmap_tree
*stencil_mt
,
574 bool hiz
, bool separate_stencil
,
575 uint32_t width
, uint32_t height
,
576 uint32_t tile_x
, uint32_t tile_y
)
578 /* Enable the hiz bit if we're doing separate stencil, because it and the
579 * separate stencil bit must have the same value. From Section 2.11.5.6.1.1
580 * 3DSTATE_DEPTH_BUFFER, Bit 1.21 "Separate Stencil Enable":
581 * [DevIL]: If this field is enabled, Hierarchical Depth Buffer
582 * Enable must also be enabled.
584 * [DevGT]: This field must be set to the same value (enabled or
585 * disabled) as Hierarchical Depth Buffer Enable
587 bool enable_hiz_ss
= hiz
|| separate_stencil
;
590 /* 3DSTATE_DEPTH_BUFFER, 3DSTATE_STENCIL_BUFFER are both
591 * non-pipelined state that will need the PIPE_CONTROL workaround.
594 brw_emit_depth_stall_flushes(brw
);
600 else if (brw
->is_g4x
|| brw
->gen
== 5)
606 OUT_BATCH(_3DSTATE_DEPTH_BUFFER
<< 16 | (len
- 2));
607 OUT_BATCH((depth_mt
? depth_mt
->pitch
- 1 : 0) |
608 (depthbuffer_format
<< 18) |
609 ((enable_hiz_ss
? 1 : 0) << 21) | /* separate stencil enable */
610 ((enable_hiz_ss
? 1 : 0) << 22) | /* hiz enable */
611 (BRW_TILEWALK_YMAJOR
<< 26) |
612 ((depth_mt
? depth_mt
->tiling
!= I915_TILING_NONE
: 1)
614 (depth_surface_type
<< 29));
617 OUT_RELOC(depth_mt
->bo
,
618 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
624 OUT_BATCH(((width
+ tile_x
- 1) << 6) |
625 ((height
+ tile_y
- 1) << 19));
628 if (brw
->is_g4x
|| brw
->gen
>= 5)
629 OUT_BATCH(tile_x
| (tile_y
<< 16));
631 assert(tile_x
== 0 && tile_y
== 0);
638 if (hiz
|| separate_stencil
) {
640 * In the 3DSTATE_DEPTH_BUFFER batch emitted above, the 'separate
641 * stencil enable' and 'hiz enable' bits were set. Therefore we must
642 * emit 3DSTATE_HIER_DEPTH_BUFFER and 3DSTATE_STENCIL_BUFFER. Even if
643 * there is no stencil buffer, 3DSTATE_STENCIL_BUFFER must be emitted;
644 * failure to do so causes hangs on gen5 and a stall on gen6.
647 /* Emit hiz buffer. */
650 struct intel_mipmap_tree
*hiz_mt
= depth_mt
->hiz_buf
->mt
;
652 OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER
<< 16) | (3 - 2));
653 OUT_BATCH(hiz_mt
->pitch
- 1);
654 OUT_RELOC(hiz_mt
->bo
,
655 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
656 brw
->depthstencil
.hiz_offset
);
660 OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER
<< 16) | (3 - 2));
666 /* Emit stencil buffer. */
667 if (separate_stencil
) {
669 OUT_BATCH((_3DSTATE_STENCIL_BUFFER
<< 16) | (3 - 2));
670 /* The stencil buffer has quirky pitch requirements. From Vol 2a,
671 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
672 * The pitch must be set to 2x the value computed based on width, as
673 * the stencil buffer is stored with two rows interleaved.
675 OUT_BATCH(2 * stencil_mt
->pitch
- 1);
676 OUT_RELOC(stencil_mt
->bo
,
677 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
678 brw
->depthstencil
.stencil_offset
);
682 OUT_BATCH((_3DSTATE_STENCIL_BUFFER
<< 16) | (3 - 2));
690 * On Gen >= 6, emit clear params for safety. If using hiz, then clear
691 * params must be emitted.
693 * From Section 2.11.5.6.4.1 3DSTATE_CLEAR_PARAMS:
694 * 3DSTATE_CLEAR_PARAMS packet must follow the DEPTH_BUFFER_STATE packet
695 * when HiZ is enabled and the DEPTH_BUFFER_STATE changes.
697 if (brw
->gen
>= 6 || hiz
) {
699 OUT_BATCH(_3DSTATE_CLEAR_PARAMS
<< 16 |
700 GEN5_DEPTH_CLEAR_VALID
|
702 OUT_BATCH(depth_mt
? depth_mt
->depth_clear_value
: 0);
707 const struct brw_tracked_state brw_depthbuffer
= {
709 .mesa
= _NEW_BUFFERS
,
710 .brw
= BRW_NEW_BATCH
|
713 .emit
= brw_emit_depthbuffer
,
717 * Polygon stipple packet
720 upload_polygon_stipple(struct brw_context
*brw
)
722 struct gl_context
*ctx
= &brw
->ctx
;
726 if (!ctx
->Polygon
.StippleFlag
)
730 OUT_BATCH(_3DSTATE_POLY_STIPPLE_PATTERN
<< 16 | (33 - 2));
732 /* Polygon stipple is provided in OpenGL order, i.e. bottom
733 * row first. If we're rendering to a window (i.e. the
734 * default frame buffer object, 0), then we need to invert
735 * it to match our pixel layout. But if we're rendering
736 * to a FBO (i.e. any named frame buffer object), we *don't*
737 * need to invert - we already match the layout.
739 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
740 for (i
= 0; i
< 32; i
++)
741 OUT_BATCH(ctx
->PolygonStipple
[31 - i
]); /* invert */
743 for (i
= 0; i
< 32; i
++)
744 OUT_BATCH(ctx
->PolygonStipple
[i
]);
749 const struct brw_tracked_state brw_polygon_stipple
= {
751 .mesa
= _NEW_POLYGON
|
753 .brw
= BRW_NEW_CONTEXT
,
755 .emit
= upload_polygon_stipple
759 * Polygon stipple offset packet
762 upload_polygon_stipple_offset(struct brw_context
*brw
)
764 struct gl_context
*ctx
= &brw
->ctx
;
767 if (!ctx
->Polygon
.StippleFlag
)
771 OUT_BATCH(_3DSTATE_POLY_STIPPLE_OFFSET
<< 16 | (2-2));
775 * If we're drawing to a system window we have to invert the Y axis
776 * in order to match the OpenGL pixel coordinate system, and our
777 * offset must be matched to the window position. If we're drawing
778 * to a user-created FBO then our native pixel coordinate system
779 * works just fine, and there's no window system to worry about.
781 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
))
782 OUT_BATCH((32 - (_mesa_geometric_height(ctx
->DrawBuffer
) & 31)) & 31);
788 const struct brw_tracked_state brw_polygon_stipple_offset
= {
790 .mesa
= _NEW_BUFFERS
|
792 .brw
= BRW_NEW_CONTEXT
,
794 .emit
= upload_polygon_stipple_offset
801 upload_aa_line_parameters(struct brw_context
*brw
)
803 struct gl_context
*ctx
= &brw
->ctx
;
805 if (!ctx
->Line
.SmoothFlag
)
808 /* Original Gen4 doesn't have 3DSTATE_AA_LINE_PARAMETERS. */
809 if (brw
->gen
== 4 && !brw
->is_g4x
)
813 OUT_BATCH(_3DSTATE_AA_LINE_PARAMETERS
<< 16 | (3 - 2));
814 /* use legacy aa line coverage computation */
820 const struct brw_tracked_state brw_aa_line_parameters
= {
823 .brw
= BRW_NEW_CONTEXT
,
825 .emit
= upload_aa_line_parameters
829 * Line stipple packet
832 upload_line_stipple(struct brw_context
*brw
)
834 struct gl_context
*ctx
= &brw
->ctx
;
838 if (!ctx
->Line
.StippleFlag
)
842 OUT_BATCH(_3DSTATE_LINE_STIPPLE_PATTERN
<< 16 | (3 - 2));
843 OUT_BATCH(ctx
->Line
.StipplePattern
);
847 tmp
= 1.0f
/ ctx
->Line
.StippleFactor
;
848 tmpi
= tmp
* (1<<16);
849 OUT_BATCH(tmpi
<< 15 | ctx
->Line
.StippleFactor
);
852 tmp
= 1.0f
/ ctx
->Line
.StippleFactor
;
853 tmpi
= tmp
* (1<<13);
854 OUT_BATCH(tmpi
<< 16 | ctx
->Line
.StippleFactor
);
860 const struct brw_tracked_state brw_line_stipple
= {
863 .brw
= BRW_NEW_CONTEXT
,
865 .emit
= upload_line_stipple
869 brw_emit_select_pipeline(struct brw_context
*brw
, enum brw_pipeline pipeline
)
871 const bool is_965
= brw
->gen
== 4 && !brw
->is_g4x
;
872 const uint32_t _3DSTATE_PIPELINE_SELECT
=
873 is_965
? CMD_PIPELINE_SELECT_965
: CMD_PIPELINE_SELECT_GM45
;
875 if (brw
->use_resource_streamer
&& pipeline
!= BRW_RENDER_PIPELINE
) {
876 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
877 * PIPELINE_SELECT [DevBWR+]":
879 * Project: HSW, BDW, CHV, SKL, BXT
881 * Hardware Binding Tables are only supported for 3D
882 * workloads. Resource streamer must be enabled only for 3D
883 * workloads. Resource streamer must be disabled for Media and GPGPU
887 OUT_BATCH(MI_RS_CONTROL
| 0);
890 gen7_disable_hw_binding_tables(brw
);
892 /* XXX - Disable gather constant pool too when we start using it. */
895 if (brw
->gen
>= 8 && brw
->gen
< 10) {
896 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
898 * Software must clear the COLOR_CALC_STATE Valid field in
899 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
900 * with Pipeline Select set to GPGPU.
902 * The internal hardware docs recommend the same workaround for Gen9
905 if (pipeline
== BRW_COMPUTE_PIPELINE
) {
907 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS
<< 16 | (2 - 2));
911 brw
->ctx
.NewDriverState
|= BRW_NEW_CC_STATE
;
914 } else if (brw
->gen
>= 6) {
915 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
916 * PIPELINE_SELECT [DevBWR+]":
920 * Software must ensure all the write caches are flushed through a
921 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
922 * command to invalidate read only caches prior to programming
923 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode.
925 const unsigned dc_flush
=
926 brw
->gen
>= 7 ? PIPE_CONTROL_DATA_CACHE_FLUSH
: 0;
928 brw_emit_pipe_control_flush(brw
,
929 PIPE_CONTROL_RENDER_TARGET_FLUSH
|
930 PIPE_CONTROL_DEPTH_CACHE_FLUSH
|
932 PIPE_CONTROL_NO_WRITE
|
933 PIPE_CONTROL_CS_STALL
);
935 brw_emit_pipe_control_flush(brw
,
936 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
|
937 PIPE_CONTROL_CONST_CACHE_INVALIDATE
|
938 PIPE_CONTROL_STATE_CACHE_INVALIDATE
|
939 PIPE_CONTROL_INSTRUCTION_INVALIDATE
|
940 PIPE_CONTROL_NO_WRITE
);
943 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
944 * PIPELINE_SELECT [DevBWR+]":
946 * Project: PRE-DEVSNB
948 * Software must ensure the current pipeline is flushed via an
949 * MI_FLUSH or PIPE_CONTROL prior to the execution of PIPELINE_SELECT.
956 /* Select the pipeline */
958 OUT_BATCH(_3DSTATE_PIPELINE_SELECT
<< 16 |
959 (brw
->gen
>= 9 ? (3 << 8) : 0) |
960 (pipeline
== BRW_COMPUTE_PIPELINE
? 2 : 0));
963 if (brw
->gen
== 7 && !brw
->is_haswell
&&
964 pipeline
== BRW_RENDER_PIPELINE
) {
965 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
966 * PIPELINE_SELECT [DevBWR+]":
968 * Project: DEVIVB, DEVHSW:GT3:A0
970 * Software must send a pipe_control with a CS stall and a post sync
971 * operation and then a dummy DRAW after every MI_SET_CONTEXT and
972 * after any PIPELINE_SELECT that is enabling 3D mode.
974 gen7_emit_cs_stall_flush(brw
);
977 OUT_BATCH(CMD_3D_PRIM
<< 16 | (7 - 2));
978 OUT_BATCH(_3DPRIM_POINTLIST
);
987 if (brw
->use_resource_streamer
&& pipeline
== BRW_RENDER_PIPELINE
) {
988 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
989 * PIPELINE_SELECT [DevBWR+]":
991 * Project: HSW, BDW, CHV, SKL, BXT
993 * Hardware Binding Tables are only supported for 3D
994 * workloads. Resource streamer must be enabled only for 3D
995 * workloads. Resource streamer must be disabled for Media and GPGPU
999 OUT_BATCH(MI_RS_CONTROL
| 1);
1002 gen7_enable_hw_binding_tables(brw
);
1004 /* XXX - Re-enable gather constant pool here. */
1009 * Misc invariant state packets
1012 brw_upload_invariant_state(struct brw_context
*brw
)
1014 const bool is_965
= brw
->gen
== 4 && !brw
->is_g4x
;
1016 brw_emit_select_pipeline(brw
, BRW_RENDER_PIPELINE
);
1017 brw
->last_pipeline
= BRW_RENDER_PIPELINE
;
1019 if (brw
->gen
>= 8) {
1021 OUT_BATCH(CMD_STATE_SIP
<< 16 | (3 - 2));
1027 OUT_BATCH(CMD_STATE_SIP
<< 16 | (2 - 2));
1032 const uint32_t _3DSTATE_VF_STATISTICS
=
1033 is_965
? GEN4_3DSTATE_VF_STATISTICS
: GM45_3DSTATE_VF_STATISTICS
;
1035 OUT_BATCH(_3DSTATE_VF_STATISTICS
<< 16 | 1);
1039 const struct brw_tracked_state brw_invariant_state
= {
1042 .brw
= BRW_NEW_BLORP
|
1045 .emit
= brw_upload_invariant_state
1049 * Define the base addresses which some state is referenced from.
1051 * This allows us to avoid having to emit relocations for the objects,
1052 * and is actually required for binding table pointers on gen6.
1054 * Surface state base address covers binding table pointers and
1055 * surface state objects, but not the surfaces that the surface state
1059 brw_upload_state_base_address(struct brw_context
*brw
)
1061 if (brw
->batch
.state_base_address_emitted
)
1064 /* FINISHME: According to section 3.6.1 "STATE_BASE_ADDRESS" of
1065 * vol1a of the G45 PRM, MI_FLUSH with the ISC invalidate should be
1066 * programmed prior to STATE_BASE_ADDRESS.
1068 * However, given that the instruction SBA (general state base
1069 * address) on this chipset is always set to 0 across X and GL,
1070 * maybe this isn't required for us in particular.
1073 if (brw
->gen
>= 8) {
1074 uint32_t mocs_wb
= brw
->gen
>= 9 ? SKL_MOCS_WB
: BDW_MOCS_WB
;
1075 int pkt_len
= brw
->gen
>= 9 ? 19 : 16;
1077 BEGIN_BATCH(pkt_len
);
1078 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (pkt_len
- 2));
1079 /* General state base address: stateless DP read/write requests */
1080 OUT_BATCH(mocs_wb
<< 4 | 1);
1082 OUT_BATCH(mocs_wb
<< 16);
1083 /* Surface state base address: */
1084 OUT_RELOC64(brw
->batch
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0,
1086 /* Dynamic state base address: */
1087 OUT_RELOC64(brw
->batch
.bo
,
1088 I915_GEM_DOMAIN_RENDER
| I915_GEM_DOMAIN_INSTRUCTION
, 0,
1090 /* Indirect object base address: MEDIA_OBJECT data */
1091 OUT_BATCH(mocs_wb
<< 4 | 1);
1093 /* Instruction base address: shader kernels (incl. SIP) */
1094 OUT_RELOC64(brw
->cache
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
1097 /* General state buffer size */
1098 OUT_BATCH(0xfffff001);
1099 /* Dynamic state buffer size */
1100 OUT_BATCH(ALIGN(brw
->batch
.bo
->size
, 4096) | 1);
1101 /* Indirect object upper bound */
1102 OUT_BATCH(0xfffff001);
1103 /* Instruction access upper bound */
1104 OUT_BATCH(ALIGN(brw
->cache
.bo
->size
, 4096) | 1);
1105 if (brw
->gen
>= 9) {
1111 } else if (brw
->gen
>= 6) {
1112 uint8_t mocs
= brw
->gen
== 7 ? GEN7_MOCS_L3
: 0;
1115 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (10 - 2));
1116 OUT_BATCH(mocs
<< 8 | /* General State Memory Object Control State */
1117 mocs
<< 4 | /* Stateless Data Port Access Memory Object Control State */
1118 1); /* General State Base Address Modify Enable */
1119 /* Surface state base address:
1120 * BINDING_TABLE_STATE
1123 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0, 1);
1124 /* Dynamic state base address:
1126 * SAMPLER_BORDER_COLOR_STATE
1127 * CLIP, SF, WM/CC viewport state
1129 * DEPTH_STENCIL_STATE
1131 * Push constants (when INSTPM: CONSTANT_BUFFER Address Offset
1132 * Disable is clear, which we rely on)
1134 OUT_RELOC(brw
->batch
.bo
, (I915_GEM_DOMAIN_RENDER
|
1135 I915_GEM_DOMAIN_INSTRUCTION
), 0, 1);
1137 OUT_BATCH(1); /* Indirect object base address: MEDIA_OBJECT data */
1138 OUT_RELOC(brw
->cache
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
1139 1); /* Instruction base address: shader kernels (incl. SIP) */
1141 OUT_BATCH(1); /* General state upper bound */
1142 /* Dynamic state upper bound. Although the documentation says that
1143 * programming it to zero will cause it to be ignored, that is a lie.
1144 * If this isn't programmed to a real bound, the sampler border color
1145 * pointer is rejected, causing border color to mysteriously fail.
1147 OUT_BATCH(0xfffff001);
1148 OUT_BATCH(1); /* Indirect object upper bound */
1149 OUT_BATCH(1); /* Instruction access upper bound */
1151 } else if (brw
->gen
== 5) {
1153 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (8 - 2));
1154 OUT_BATCH(1); /* General state base address */
1155 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0,
1156 1); /* Surface state base address */
1157 OUT_BATCH(1); /* Indirect object base address */
1158 OUT_RELOC(brw
->cache
.bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0,
1159 1); /* Instruction base address */
1160 OUT_BATCH(0xfffff001); /* General state upper bound */
1161 OUT_BATCH(1); /* Indirect object upper bound */
1162 OUT_BATCH(1); /* Instruction access upper bound */
1166 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (6 - 2));
1167 OUT_BATCH(1); /* General state base address */
1168 OUT_RELOC(brw
->batch
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0,
1169 1); /* Surface state base address */
1170 OUT_BATCH(1); /* Indirect object base address */
1171 OUT_BATCH(1); /* General state upper bound */
1172 OUT_BATCH(1); /* Indirect object upper bound */
1176 /* According to section 3.6.1 of VOL1 of the 965 PRM,
1177 * STATE_BASE_ADDRESS updates require a reissue of:
1179 * 3DSTATE_PIPELINE_POINTERS
1180 * 3DSTATE_BINDING_TABLE_POINTERS
1181 * MEDIA_STATE_POINTERS
1183 * and this continues through Ironlake. The Sandy Bridge PRM, vol
1184 * 1 part 1 says that the folowing packets must be reissued:
1186 * 3DSTATE_CC_POINTERS
1187 * 3DSTATE_BINDING_TABLE_POINTERS
1188 * 3DSTATE_SAMPLER_STATE_POINTERS
1189 * 3DSTATE_VIEWPORT_STATE_POINTERS
1190 * MEDIA_STATE_POINTERS
1192 * Those are always reissued following SBA updates anyway (new
1193 * batch time), except in the case of the program cache BO
1194 * changing. Having a separate state flag makes the sequence more
1198 brw
->ctx
.NewDriverState
|= BRW_NEW_STATE_BASE_ADDRESS
;
1199 brw
->batch
.state_base_address_emitted
= true;