i965: Always re-emit the pipeline select during invariant state emission
[mesa.git] / src / mesa / drivers / dri / i965 / brw_misc_state.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
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:
13
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.
17
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.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 */
31
32
33
34 #include "intel_batchbuffer.h"
35 #include "intel_fbo.h"
36 #include "intel_mipmap_tree.h"
37
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_defines.h"
41
42 #include "main/framebuffer.h"
43 #include "main/fbobject.h"
44 #include "main/glformats.h"
45
46 /* Constant single cliprect for framebuffer object or DRI2 drawing */
47 static void
48 upload_drawing_rect(struct brw_context *brw)
49 {
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);
54
55 BEGIN_BATCH(4);
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));
59 OUT_BATCH(0);
60 ADVANCE_BATCH();
61 }
62
63 const struct brw_tracked_state brw_drawing_rect = {
64 .dirty = {
65 .mesa = _NEW_BUFFERS,
66 .brw = BRW_NEW_CONTEXT,
67 },
68 .emit = upload_drawing_rect
69 };
70
71 /**
72 * Upload pointers to the per-stage state.
73 *
74 * The state pointers in this packet are all relative to the general state
75 * base address set by CMD_STATE_BASE_ADDRESS, which is 0.
76 */
77 static void
78 upload_pipelined_state_pointers(struct brw_context *brw)
79 {
80 if (brw->gen == 5) {
81 /* Need to flush before changing clip max threads for errata. */
82 BEGIN_BATCH(1);
83 OUT_BATCH(MI_FLUSH);
84 ADVANCE_BATCH();
85 }
86
87 BEGIN_BATCH(7);
88 OUT_BATCH(_3DSTATE_PIPELINED_POINTERS << 16 | (7 - 2));
89 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
90 brw->vs.base.state_offset);
91 if (brw->ff_gs.prog_active)
92 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
93 brw->ff_gs.state_offset | 1);
94 else
95 OUT_BATCH(0);
96 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
97 brw->clip.state_offset | 1);
98 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
99 brw->sf.state_offset);
100 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
101 brw->wm.base.state_offset);
102 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
103 brw->cc.state_offset);
104 ADVANCE_BATCH();
105
106 brw->ctx.NewDriverState |= BRW_NEW_PSP;
107 }
108
109 static void
110 upload_psp_urb_cbs(struct brw_context *brw)
111 {
112 upload_pipelined_state_pointers(brw);
113 brw_upload_urb_fence(brw);
114 brw_upload_cs_urb_state(brw);
115 }
116
117 const struct brw_tracked_state brw_psp_urb_cbs = {
118 .dirty = {
119 .mesa = 0,
120 .brw = BRW_NEW_BATCH |
121 BRW_NEW_FF_GS_PROG_DATA |
122 BRW_NEW_GEN4_UNIT_STATE |
123 BRW_NEW_STATE_BASE_ADDRESS |
124 BRW_NEW_URB_FENCE,
125 },
126 .emit = upload_psp_urb_cbs,
127 };
128
129 uint32_t
130 brw_depthbuffer_format(struct brw_context *brw)
131 {
132 struct gl_context *ctx = &brw->ctx;
133 struct gl_framebuffer *fb = ctx->DrawBuffer;
134 struct intel_renderbuffer *drb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
135 struct intel_renderbuffer *srb;
136
137 if (!drb &&
138 (srb = intel_get_renderbuffer(fb, BUFFER_STENCIL)) &&
139 !srb->mt->stencil_mt &&
140 (intel_rb_format(srb) == MESA_FORMAT_Z24_UNORM_S8_UINT ||
141 intel_rb_format(srb) == MESA_FORMAT_Z32_FLOAT_S8X24_UINT)) {
142 drb = srb;
143 }
144
145 if (!drb)
146 return BRW_DEPTHFORMAT_D32_FLOAT;
147
148 return brw_depth_format(brw, drb->mt->format);
149 }
150
151 /**
152 * Returns the mask of how many bits of x and y must be handled through the
153 * depthbuffer's draw offset x and y fields.
154 *
155 * The draw offset x/y field of the depthbuffer packet is unfortunately shared
156 * between the depth, hiz, and stencil buffers. Because it can be hard to get
157 * all 3 to agree on this value, we want to do as much drawing offset
158 * adjustment as possible by moving the base offset of the 3 buffers, which is
159 * restricted to tile boundaries.
160 *
161 * For each buffer, the remainder must be applied through the x/y draw offset.
162 * This returns the worst-case mask of the low bits that have to go into the
163 * packet. If the 3 buffers don't agree on the drawing offset ANDed with this
164 * mask, then we're in trouble.
165 */
166 void
167 brw_get_depthstencil_tile_masks(struct intel_mipmap_tree *depth_mt,
168 uint32_t depth_level,
169 uint32_t depth_layer,
170 struct intel_mipmap_tree *stencil_mt,
171 uint32_t *out_tile_mask_x,
172 uint32_t *out_tile_mask_y)
173 {
174 uint32_t tile_mask_x = 0, tile_mask_y = 0;
175
176 if (depth_mt) {
177 intel_miptree_get_tile_masks(depth_mt, &tile_mask_x, &tile_mask_y, false);
178
179 if (intel_miptree_level_has_hiz(depth_mt, depth_level)) {
180 uint32_t hiz_tile_mask_x, hiz_tile_mask_y;
181 intel_miptree_get_tile_masks(depth_mt->hiz_buf->mt,
182 &hiz_tile_mask_x, &hiz_tile_mask_y,
183 false);
184
185 /* Each HiZ row represents 2 rows of pixels */
186 hiz_tile_mask_y = hiz_tile_mask_y << 1 | 1;
187
188 tile_mask_x |= hiz_tile_mask_x;
189 tile_mask_y |= hiz_tile_mask_y;
190 }
191 }
192
193 if (stencil_mt) {
194 if (stencil_mt->stencil_mt)
195 stencil_mt = stencil_mt->stencil_mt;
196
197 if (stencil_mt->format == MESA_FORMAT_S_UINT8) {
198 /* Separate stencil buffer uses 64x64 tiles. */
199 tile_mask_x |= 63;
200 tile_mask_y |= 63;
201 } else {
202 uint32_t stencil_tile_mask_x, stencil_tile_mask_y;
203 intel_miptree_get_tile_masks(stencil_mt,
204 &stencil_tile_mask_x,
205 &stencil_tile_mask_y, false);
206
207 tile_mask_x |= stencil_tile_mask_x;
208 tile_mask_y |= stencil_tile_mask_y;
209 }
210 }
211
212 *out_tile_mask_x = tile_mask_x;
213 *out_tile_mask_y = tile_mask_y;
214 }
215
216 static struct intel_mipmap_tree *
217 get_stencil_miptree(struct intel_renderbuffer *irb)
218 {
219 if (!irb)
220 return NULL;
221 if (irb->mt->stencil_mt)
222 return irb->mt->stencil_mt;
223 return irb->mt;
224 }
225
226 void
227 brw_workaround_depthstencil_alignment(struct brw_context *brw,
228 GLbitfield clear_mask)
229 {
230 struct gl_context *ctx = &brw->ctx;
231 struct gl_framebuffer *fb = ctx->DrawBuffer;
232 bool rebase_depth = false;
233 bool rebase_stencil = false;
234 struct intel_renderbuffer *depth_irb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
235 struct intel_renderbuffer *stencil_irb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
236 struct intel_mipmap_tree *depth_mt = NULL;
237 struct intel_mipmap_tree *stencil_mt = get_stencil_miptree(stencil_irb);
238 uint32_t tile_x = 0, tile_y = 0, stencil_tile_x = 0, stencil_tile_y = 0;
239 uint32_t stencil_draw_x = 0, stencil_draw_y = 0;
240 bool invalidate_depth = clear_mask & BUFFER_BIT_DEPTH;
241 bool invalidate_stencil = clear_mask & BUFFER_BIT_STENCIL;
242
243 if (depth_irb)
244 depth_mt = depth_irb->mt;
245
246 /* Initialize brw->depthstencil to 'nop' workaround state.
247 */
248 brw->depthstencil.tile_x = 0;
249 brw->depthstencil.tile_y = 0;
250 brw->depthstencil.depth_offset = 0;
251 brw->depthstencil.stencil_offset = 0;
252 brw->depthstencil.hiz_offset = 0;
253 brw->depthstencil.depth_mt = NULL;
254 brw->depthstencil.stencil_mt = NULL;
255 if (depth_irb)
256 brw->depthstencil.depth_mt = depth_mt;
257 if (stencil_irb)
258 brw->depthstencil.stencil_mt = get_stencil_miptree(stencil_irb);
259
260 /* Gen6+ doesn't require the workarounds, since we always program the
261 * surface state at the start of the whole surface.
262 */
263 if (brw->gen >= 6)
264 return;
265
266 /* Check if depth buffer is in depth/stencil format. If so, then it's only
267 * safe to invalidate it if we're also clearing stencil, and both depth_irb
268 * and stencil_irb point to the same miptree.
269 *
270 * Note: it's not sufficient to check for the case where
271 * _mesa_get_format_base_format(depth_mt->format) == GL_DEPTH_STENCIL,
272 * because this fails to catch depth/stencil buffers on hardware that uses
273 * separate stencil. To catch that case, we check whether
274 * depth_mt->stencil_mt is non-NULL.
275 */
276 if (depth_irb && invalidate_depth &&
277 (_mesa_get_format_base_format(depth_mt->format) == GL_DEPTH_STENCIL ||
278 depth_mt->stencil_mt)) {
279 invalidate_depth = invalidate_stencil && depth_irb && stencil_irb
280 && depth_irb->mt == stencil_irb->mt;
281 }
282
283 uint32_t tile_mask_x, tile_mask_y;
284 brw_get_depthstencil_tile_masks(depth_mt,
285 depth_mt ? depth_irb->mt_level : 0,
286 depth_mt ? depth_irb->mt_layer : 0,
287 stencil_mt,
288 &tile_mask_x, &tile_mask_y);
289
290 if (depth_irb) {
291 tile_x = depth_irb->draw_x & tile_mask_x;
292 tile_y = depth_irb->draw_y & tile_mask_y;
293
294 /* According to the Sandy Bridge PRM, volume 2 part 1, pp326-327
295 * (3DSTATE_DEPTH_BUFFER dw5), in the documentation for "Depth
296 * Coordinate Offset X/Y":
297 *
298 * "The 3 LSBs of both offsets must be zero to ensure correct
299 * alignment"
300 */
301 if (tile_x & 7 || tile_y & 7)
302 rebase_depth = true;
303
304 /* We didn't even have intra-tile offsets before g45. */
305 if (!brw->has_surface_tile_offset) {
306 if (tile_x || tile_y)
307 rebase_depth = true;
308 }
309
310 if (rebase_depth) {
311 perf_debug("HW workaround: blitting depth level %d to a temporary "
312 "to fix alignment (depth tile offset %d,%d)\n",
313 depth_irb->mt_level, tile_x, tile_y);
314 intel_renderbuffer_move_to_temp(brw, depth_irb, invalidate_depth);
315 /* In the case of stencil_irb being the same packed depth/stencil
316 * texture but not the same rb, make it point at our rebased mt, too.
317 */
318 if (stencil_irb &&
319 stencil_irb != depth_irb &&
320 stencil_irb->mt == depth_mt) {
321 intel_miptree_reference(&stencil_irb->mt, depth_irb->mt);
322 intel_renderbuffer_set_draw_offset(stencil_irb);
323 }
324
325 stencil_mt = get_stencil_miptree(stencil_irb);
326
327 tile_x = depth_irb->draw_x & tile_mask_x;
328 tile_y = depth_irb->draw_y & tile_mask_y;
329 }
330
331 if (stencil_irb) {
332 stencil_mt = get_stencil_miptree(stencil_irb);
333 intel_miptree_get_image_offset(stencil_mt,
334 stencil_irb->mt_level,
335 stencil_irb->mt_layer,
336 &stencil_draw_x, &stencil_draw_y);
337 int stencil_tile_x = stencil_draw_x & tile_mask_x;
338 int stencil_tile_y = stencil_draw_y & tile_mask_y;
339
340 /* If stencil doesn't match depth, then we'll need to rebase stencil
341 * as well. (if we hadn't decided to rebase stencil before, the
342 * post-stencil depth test will also rebase depth to try to match it
343 * up).
344 */
345 if (tile_x != stencil_tile_x ||
346 tile_y != stencil_tile_y) {
347 rebase_stencil = true;
348 }
349 }
350 }
351
352 /* If we have (just) stencil, check it for ignored low bits as well */
353 if (stencil_irb) {
354 intel_miptree_get_image_offset(stencil_mt,
355 stencil_irb->mt_level,
356 stencil_irb->mt_layer,
357 &stencil_draw_x, &stencil_draw_y);
358 stencil_tile_x = stencil_draw_x & tile_mask_x;
359 stencil_tile_y = stencil_draw_y & tile_mask_y;
360
361 if (stencil_tile_x & 7 || stencil_tile_y & 7)
362 rebase_stencil = true;
363
364 if (!brw->has_surface_tile_offset) {
365 if (stencil_tile_x || stencil_tile_y)
366 rebase_stencil = true;
367 }
368 }
369
370 if (rebase_stencil) {
371 perf_debug("HW workaround: blitting stencil level %d to a temporary "
372 "to fix alignment (stencil tile offset %d,%d)\n",
373 stencil_irb->mt_level, stencil_tile_x, stencil_tile_y);
374
375 intel_renderbuffer_move_to_temp(brw, stencil_irb, invalidate_stencil);
376 stencil_mt = get_stencil_miptree(stencil_irb);
377
378 intel_miptree_get_image_offset(stencil_mt,
379 stencil_irb->mt_level,
380 stencil_irb->mt_layer,
381 &stencil_draw_x, &stencil_draw_y);
382 stencil_tile_x = stencil_draw_x & tile_mask_x;
383 stencil_tile_y = stencil_draw_y & tile_mask_y;
384
385 if (depth_irb && depth_irb->mt == stencil_irb->mt) {
386 intel_miptree_reference(&depth_irb->mt, stencil_irb->mt);
387 intel_renderbuffer_set_draw_offset(depth_irb);
388 } else if (depth_irb && !rebase_depth) {
389 if (tile_x != stencil_tile_x ||
390 tile_y != stencil_tile_y) {
391 perf_debug("HW workaround: blitting depth level %d to a temporary "
392 "to match stencil level %d alignment (depth tile offset "
393 "%d,%d, stencil offset %d,%d)\n",
394 depth_irb->mt_level,
395 stencil_irb->mt_level,
396 tile_x, tile_y,
397 stencil_tile_x, stencil_tile_y);
398
399 intel_renderbuffer_move_to_temp(brw, depth_irb, invalidate_depth);
400
401 tile_x = depth_irb->draw_x & tile_mask_x;
402 tile_y = depth_irb->draw_y & tile_mask_y;
403
404 if (stencil_irb && stencil_irb->mt == depth_mt) {
405 intel_miptree_reference(&stencil_irb->mt, depth_irb->mt);
406 intel_renderbuffer_set_draw_offset(stencil_irb);
407 }
408
409 WARN_ONCE(stencil_tile_x != tile_x ||
410 stencil_tile_y != tile_y,
411 "Rebased stencil tile offset (%d,%d) doesn't match depth "
412 "tile offset (%d,%d).\n",
413 stencil_tile_x, stencil_tile_y,
414 tile_x, tile_y);
415 }
416 }
417 }
418
419 if (!depth_irb) {
420 tile_x = stencil_tile_x;
421 tile_y = stencil_tile_y;
422 }
423
424 /* While we just tried to get everything aligned, we may have failed to do
425 * so in the case of rendering to array or 3D textures, where nonzero faces
426 * will still have an offset post-rebase. At least give an informative
427 * warning.
428 */
429 WARN_ONCE((tile_x & 7) || (tile_y & 7),
430 "Depth/stencil buffer needs alignment to 8-pixel boundaries.\n"
431 "Truncating offset, bad rendering may occur.\n");
432 tile_x &= ~7;
433 tile_y &= ~7;
434
435 /* Now, after rebasing, save off the new dephtstencil state so the hardware
436 * packets can just dereference that without re-calculating tile offsets.
437 */
438 brw->depthstencil.tile_x = tile_x;
439 brw->depthstencil.tile_y = tile_y;
440 if (depth_irb) {
441 depth_mt = depth_irb->mt;
442 brw->depthstencil.depth_mt = depth_mt;
443 brw->depthstencil.depth_offset =
444 intel_miptree_get_aligned_offset(depth_mt,
445 depth_irb->draw_x & ~tile_mask_x,
446 depth_irb->draw_y & ~tile_mask_y,
447 false);
448 if (intel_renderbuffer_has_hiz(depth_irb)) {
449 brw->depthstencil.hiz_offset =
450 intel_miptree_get_aligned_offset(depth_mt,
451 depth_irb->draw_x & ~tile_mask_x,
452 (depth_irb->draw_y & ~tile_mask_y) / 2,
453 false);
454 }
455 }
456 if (stencil_irb) {
457 stencil_mt = get_stencil_miptree(stencil_irb);
458
459 brw->depthstencil.stencil_mt = stencil_mt;
460 if (stencil_mt->format == MESA_FORMAT_S_UINT8) {
461 /* Note: we can't compute the stencil offset using
462 * intel_region_get_aligned_offset(), because stencil_region claims
463 * that the region is untiled even though it's W tiled.
464 */
465 brw->depthstencil.stencil_offset =
466 (stencil_draw_y & ~tile_mask_y) * stencil_mt->pitch +
467 (stencil_draw_x & ~tile_mask_x) * 64;
468 }
469 }
470 }
471
472 void
473 brw_emit_depthbuffer(struct brw_context *brw)
474 {
475 struct gl_context *ctx = &brw->ctx;
476 struct gl_framebuffer *fb = ctx->DrawBuffer;
477 /* _NEW_BUFFERS */
478 struct intel_renderbuffer *depth_irb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
479 struct intel_renderbuffer *stencil_irb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
480 struct intel_mipmap_tree *depth_mt = brw->depthstencil.depth_mt;
481 struct intel_mipmap_tree *stencil_mt = brw->depthstencil.stencil_mt;
482 uint32_t tile_x = brw->depthstencil.tile_x;
483 uint32_t tile_y = brw->depthstencil.tile_y;
484 bool hiz = depth_irb && intel_renderbuffer_has_hiz(depth_irb);
485 bool separate_stencil = false;
486 uint32_t depth_surface_type = BRW_SURFACE_NULL;
487 uint32_t depthbuffer_format = BRW_DEPTHFORMAT_D32_FLOAT;
488 uint32_t depth_offset = 0;
489 uint32_t width = 1, height = 1;
490
491 if (stencil_mt) {
492 separate_stencil = stencil_mt->format == MESA_FORMAT_S_UINT8;
493
494 /* Gen7 supports only separate stencil */
495 assert(separate_stencil || brw->gen < 7);
496 }
497
498 /* If there's a packed depth/stencil bound to stencil only, we need to
499 * emit the packed depth/stencil buffer packet.
500 */
501 if (!depth_irb && stencil_irb && !separate_stencil) {
502 depth_irb = stencil_irb;
503 depth_mt = stencil_mt;
504 }
505
506 if (depth_irb && depth_mt) {
507 /* When 3DSTATE_DEPTH_BUFFER.Separate_Stencil_Enable is set, then
508 * 3DSTATE_DEPTH_BUFFER.Surface_Format is not permitted to be a packed
509 * depthstencil format.
510 *
511 * Gens prior to 7 require that HiZ_Enable and Separate_Stencil_Enable be
512 * set to the same value. Gens after 7 implicitly always set
513 * Separate_Stencil_Enable; software cannot disable it.
514 */
515 if ((brw->gen < 7 && hiz) || brw->gen >= 7) {
516 assert(!_mesa_is_format_packed_depth_stencil(depth_mt->format));
517 }
518
519 /* Prior to Gen7, if using separate stencil, hiz must be enabled. */
520 assert(brw->gen >= 7 || !separate_stencil || hiz);
521
522 assert(brw->gen < 6 || depth_mt->tiling == I915_TILING_Y);
523 assert(!hiz || depth_mt->tiling == I915_TILING_Y);
524
525 depthbuffer_format = brw_depthbuffer_format(brw);
526 depth_surface_type = BRW_SURFACE_2D;
527 depth_offset = brw->depthstencil.depth_offset;
528 width = depth_irb->Base.Base.Width;
529 height = depth_irb->Base.Base.Height;
530 } else if (separate_stencil) {
531 /*
532 * There exists a separate stencil buffer but no depth buffer.
533 *
534 * The stencil buffer inherits most of its fields from
535 * 3DSTATE_DEPTH_BUFFER: namely the tile walk, surface type, width, and
536 * height.
537 *
538 * The tiled bit must be set. From the Sandybridge PRM, Volume 2, Part 1,
539 * Section 7.5.5.1.1 3DSTATE_DEPTH_BUFFER, Bit 1.27 Tiled Surface:
540 * [DevGT+]: This field must be set to TRUE.
541 */
542 assert(brw->has_separate_stencil);
543
544 depth_surface_type = BRW_SURFACE_2D;
545 width = stencil_irb->Base.Base.Width;
546 height = stencil_irb->Base.Base.Height;
547 }
548
549 if (depth_mt)
550 brw_render_cache_set_check_flush(brw, depth_mt->bo);
551 if (stencil_mt)
552 brw_render_cache_set_check_flush(brw, stencil_mt->bo);
553
554 brw->vtbl.emit_depth_stencil_hiz(brw, depth_mt, depth_offset,
555 depthbuffer_format, depth_surface_type,
556 stencil_mt, hiz, separate_stencil,
557 width, height, tile_x, tile_y);
558 }
559
560 void
561 brw_emit_depth_stencil_hiz(struct brw_context *brw,
562 struct intel_mipmap_tree *depth_mt,
563 uint32_t depth_offset, uint32_t depthbuffer_format,
564 uint32_t depth_surface_type,
565 struct intel_mipmap_tree *stencil_mt,
566 bool hiz, bool separate_stencil,
567 uint32_t width, uint32_t height,
568 uint32_t tile_x, uint32_t tile_y)
569 {
570 /* Enable the hiz bit if we're doing separate stencil, because it and the
571 * separate stencil bit must have the same value. From Section 2.11.5.6.1.1
572 * 3DSTATE_DEPTH_BUFFER, Bit 1.21 "Separate Stencil Enable":
573 * [DevIL]: If this field is enabled, Hierarchical Depth Buffer
574 * Enable must also be enabled.
575 *
576 * [DevGT]: This field must be set to the same value (enabled or
577 * disabled) as Hierarchical Depth Buffer Enable
578 */
579 bool enable_hiz_ss = hiz || separate_stencil;
580
581
582 /* 3DSTATE_DEPTH_BUFFER, 3DSTATE_STENCIL_BUFFER are both
583 * non-pipelined state that will need the PIPE_CONTROL workaround.
584 */
585 if (brw->gen == 6) {
586 brw_emit_depth_stall_flushes(brw);
587 }
588
589 unsigned int len;
590 if (brw->gen >= 6)
591 len = 7;
592 else if (brw->is_g4x || brw->gen == 5)
593 len = 6;
594 else
595 len = 5;
596
597 BEGIN_BATCH(len);
598 OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (len - 2));
599 OUT_BATCH((depth_mt ? depth_mt->pitch - 1 : 0) |
600 (depthbuffer_format << 18) |
601 ((enable_hiz_ss ? 1 : 0) << 21) | /* separate stencil enable */
602 ((enable_hiz_ss ? 1 : 0) << 22) | /* hiz enable */
603 (BRW_TILEWALK_YMAJOR << 26) |
604 ((depth_mt ? depth_mt->tiling != I915_TILING_NONE : 1)
605 << 27) |
606 (depth_surface_type << 29));
607
608 if (depth_mt) {
609 OUT_RELOC(depth_mt->bo,
610 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
611 depth_offset);
612 } else {
613 OUT_BATCH(0);
614 }
615
616 OUT_BATCH(((width + tile_x - 1) << 6) |
617 ((height + tile_y - 1) << 19));
618 OUT_BATCH(0);
619
620 if (brw->is_g4x || brw->gen >= 5)
621 OUT_BATCH(tile_x | (tile_y << 16));
622 else
623 assert(tile_x == 0 && tile_y == 0);
624
625 if (brw->gen >= 6)
626 OUT_BATCH(0);
627
628 ADVANCE_BATCH();
629
630 if (hiz || separate_stencil) {
631 /*
632 * In the 3DSTATE_DEPTH_BUFFER batch emitted above, the 'separate
633 * stencil enable' and 'hiz enable' bits were set. Therefore we must
634 * emit 3DSTATE_HIER_DEPTH_BUFFER and 3DSTATE_STENCIL_BUFFER. Even if
635 * there is no stencil buffer, 3DSTATE_STENCIL_BUFFER must be emitted;
636 * failure to do so causes hangs on gen5 and a stall on gen6.
637 */
638
639 /* Emit hiz buffer. */
640 if (hiz) {
641 struct intel_mipmap_tree *hiz_mt = depth_mt->hiz_buf->mt;
642 BEGIN_BATCH(3);
643 OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
644 OUT_BATCH(hiz_mt->pitch - 1);
645 OUT_RELOC(hiz_mt->bo,
646 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
647 brw->depthstencil.hiz_offset);
648 ADVANCE_BATCH();
649 } else {
650 BEGIN_BATCH(3);
651 OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
652 OUT_BATCH(0);
653 OUT_BATCH(0);
654 ADVANCE_BATCH();
655 }
656
657 /* Emit stencil buffer. */
658 if (separate_stencil) {
659 BEGIN_BATCH(3);
660 OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
661 /* The stencil buffer has quirky pitch requirements. From Vol 2a,
662 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
663 * The pitch must be set to 2x the value computed based on width, as
664 * the stencil buffer is stored with two rows interleaved.
665 */
666 OUT_BATCH(2 * stencil_mt->pitch - 1);
667 OUT_RELOC(stencil_mt->bo,
668 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
669 brw->depthstencil.stencil_offset);
670 ADVANCE_BATCH();
671 } else {
672 BEGIN_BATCH(3);
673 OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
674 OUT_BATCH(0);
675 OUT_BATCH(0);
676 ADVANCE_BATCH();
677 }
678 }
679
680 /*
681 * On Gen >= 6, emit clear params for safety. If using hiz, then clear
682 * params must be emitted.
683 *
684 * From Section 2.11.5.6.4.1 3DSTATE_CLEAR_PARAMS:
685 * 3DSTATE_CLEAR_PARAMS packet must follow the DEPTH_BUFFER_STATE packet
686 * when HiZ is enabled and the DEPTH_BUFFER_STATE changes.
687 */
688 if (brw->gen >= 6 || hiz) {
689 BEGIN_BATCH(2);
690 OUT_BATCH(_3DSTATE_CLEAR_PARAMS << 16 |
691 GEN5_DEPTH_CLEAR_VALID |
692 (2 - 2));
693 OUT_BATCH(depth_mt ? depth_mt->depth_clear_value : 0);
694 ADVANCE_BATCH();
695 }
696 }
697
698 const struct brw_tracked_state brw_depthbuffer = {
699 .dirty = {
700 .mesa = _NEW_BUFFERS,
701 .brw = BRW_NEW_BATCH,
702 },
703 .emit = brw_emit_depthbuffer,
704 };
705
706 /**
707 * Polygon stipple packet
708 */
709 static void
710 upload_polygon_stipple(struct brw_context *brw)
711 {
712 struct gl_context *ctx = &brw->ctx;
713 GLuint i;
714
715 /* _NEW_POLYGON */
716 if (!ctx->Polygon.StippleFlag)
717 return;
718
719 BEGIN_BATCH(33);
720 OUT_BATCH(_3DSTATE_POLY_STIPPLE_PATTERN << 16 | (33 - 2));
721
722 /* Polygon stipple is provided in OpenGL order, i.e. bottom
723 * row first. If we're rendering to a window (i.e. the
724 * default frame buffer object, 0), then we need to invert
725 * it to match our pixel layout. But if we're rendering
726 * to a FBO (i.e. any named frame buffer object), we *don't*
727 * need to invert - we already match the layout.
728 */
729 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
730 for (i = 0; i < 32; i++)
731 OUT_BATCH(ctx->PolygonStipple[31 - i]); /* invert */
732 } else {
733 for (i = 0; i < 32; i++)
734 OUT_BATCH(ctx->PolygonStipple[i]);
735 }
736 ADVANCE_BATCH();
737 }
738
739 const struct brw_tracked_state brw_polygon_stipple = {
740 .dirty = {
741 .mesa = _NEW_POLYGON |
742 _NEW_POLYGONSTIPPLE,
743 .brw = BRW_NEW_CONTEXT,
744 },
745 .emit = upload_polygon_stipple
746 };
747
748 /**
749 * Polygon stipple offset packet
750 */
751 static void
752 upload_polygon_stipple_offset(struct brw_context *brw)
753 {
754 struct gl_context *ctx = &brw->ctx;
755
756 /* _NEW_POLYGON */
757 if (!ctx->Polygon.StippleFlag)
758 return;
759
760 BEGIN_BATCH(2);
761 OUT_BATCH(_3DSTATE_POLY_STIPPLE_OFFSET << 16 | (2-2));
762
763 /* _NEW_BUFFERS
764 *
765 * If we're drawing to a system window we have to invert the Y axis
766 * in order to match the OpenGL pixel coordinate system, and our
767 * offset must be matched to the window position. If we're drawing
768 * to a user-created FBO then our native pixel coordinate system
769 * works just fine, and there's no window system to worry about.
770 */
771 if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
772 OUT_BATCH((32 - (_mesa_geometric_height(ctx->DrawBuffer) & 31)) & 31);
773 else
774 OUT_BATCH(0);
775 ADVANCE_BATCH();
776 }
777
778 const struct brw_tracked_state brw_polygon_stipple_offset = {
779 .dirty = {
780 .mesa = _NEW_BUFFERS |
781 _NEW_POLYGON,
782 .brw = BRW_NEW_CONTEXT,
783 },
784 .emit = upload_polygon_stipple_offset
785 };
786
787 /**
788 * AA Line parameters
789 */
790 static void
791 upload_aa_line_parameters(struct brw_context *brw)
792 {
793 struct gl_context *ctx = &brw->ctx;
794
795 if (!ctx->Line.SmoothFlag)
796 return;
797
798 /* Original Gen4 doesn't have 3DSTATE_AA_LINE_PARAMETERS. */
799 if (brw->gen == 4 && !brw->is_g4x)
800 return;
801
802 BEGIN_BATCH(3);
803 OUT_BATCH(_3DSTATE_AA_LINE_PARAMETERS << 16 | (3 - 2));
804 /* use legacy aa line coverage computation */
805 OUT_BATCH(0);
806 OUT_BATCH(0);
807 ADVANCE_BATCH();
808 }
809
810 const struct brw_tracked_state brw_aa_line_parameters = {
811 .dirty = {
812 .mesa = _NEW_LINE,
813 .brw = BRW_NEW_CONTEXT,
814 },
815 .emit = upload_aa_line_parameters
816 };
817
818 /**
819 * Line stipple packet
820 */
821 static void
822 upload_line_stipple(struct brw_context *brw)
823 {
824 struct gl_context *ctx = &brw->ctx;
825 GLfloat tmp;
826 GLint tmpi;
827
828 if (!ctx->Line.StippleFlag)
829 return;
830
831 BEGIN_BATCH(3);
832 OUT_BATCH(_3DSTATE_LINE_STIPPLE_PATTERN << 16 | (3 - 2));
833 OUT_BATCH(ctx->Line.StipplePattern);
834
835 if (brw->gen >= 7) {
836 /* in U1.16 */
837 tmp = 1.0f / ctx->Line.StippleFactor;
838 tmpi = tmp * (1<<16);
839 OUT_BATCH(tmpi << 15 | ctx->Line.StippleFactor);
840 } else {
841 /* in U1.13 */
842 tmp = 1.0f / ctx->Line.StippleFactor;
843 tmpi = tmp * (1<<13);
844 OUT_BATCH(tmpi << 16 | ctx->Line.StippleFactor);
845 }
846
847 ADVANCE_BATCH();
848 }
849
850 const struct brw_tracked_state brw_line_stipple = {
851 .dirty = {
852 .mesa = _NEW_LINE,
853 .brw = BRW_NEW_CONTEXT,
854 },
855 .emit = upload_line_stipple
856 };
857
858 void
859 brw_emit_select_pipeline(struct brw_context *brw, enum brw_pipeline pipeline)
860 {
861 const bool is_965 = brw->gen == 4 && !brw->is_g4x;
862 const uint32_t _3DSTATE_PIPELINE_SELECT =
863 is_965 ? CMD_PIPELINE_SELECT_965 : CMD_PIPELINE_SELECT_GM45;
864
865 /* Select the pipeline */
866 BEGIN_BATCH(1);
867 OUT_BATCH(_3DSTATE_PIPELINE_SELECT << 16 |
868 (brw->gen >= 9 ? (3 << 8) : 0) |
869 (pipeline == BRW_COMPUTE_PIPELINE ? 2 : 0));
870 ADVANCE_BATCH();
871 }
872
873 /**
874 * Misc invariant state packets
875 */
876 void
877 brw_upload_invariant_state(struct brw_context *brw)
878 {
879 const bool is_965 = brw->gen == 4 && !brw->is_g4x;
880
881 brw_emit_select_pipeline(brw, BRW_RENDER_PIPELINE);
882 brw->last_pipeline = BRW_RENDER_PIPELINE;
883
884 if (brw->gen < 6) {
885 /* Disable depth offset clamping. */
886 BEGIN_BATCH(2);
887 OUT_BATCH(_3DSTATE_GLOBAL_DEPTH_OFFSET_CLAMP << 16 | (2 - 2));
888 OUT_BATCH_F(0.0);
889 ADVANCE_BATCH();
890 }
891
892 if (brw->gen >= 8) {
893 BEGIN_BATCH(3);
894 OUT_BATCH(CMD_STATE_SIP << 16 | (3 - 2));
895 OUT_BATCH(0);
896 OUT_BATCH(0);
897 ADVANCE_BATCH();
898 } else {
899 BEGIN_BATCH(2);
900 OUT_BATCH(CMD_STATE_SIP << 16 | (2 - 2));
901 OUT_BATCH(0);
902 ADVANCE_BATCH();
903 }
904
905 const uint32_t _3DSTATE_VF_STATISTICS =
906 is_965 ? GEN4_3DSTATE_VF_STATISTICS : GM45_3DSTATE_VF_STATISTICS;
907 BEGIN_BATCH(1);
908 OUT_BATCH(_3DSTATE_VF_STATISTICS << 16 | 1);
909 ADVANCE_BATCH();
910 }
911
912 const struct brw_tracked_state brw_invariant_state = {
913 .dirty = {
914 .mesa = 0,
915 .brw = BRW_NEW_CONTEXT,
916 },
917 .emit = brw_upload_invariant_state
918 };
919
920 /**
921 * Define the base addresses which some state is referenced from.
922 *
923 * This allows us to avoid having to emit relocations for the objects,
924 * and is actually required for binding table pointers on gen6.
925 *
926 * Surface state base address covers binding table pointers and
927 * surface state objects, but not the surfaces that the surface state
928 * objects point to.
929 */
930 static void
931 upload_state_base_address(struct brw_context *brw)
932 {
933 /* FINISHME: According to section 3.6.1 "STATE_BASE_ADDRESS" of
934 * vol1a of the G45 PRM, MI_FLUSH with the ISC invalidate should be
935 * programmed prior to STATE_BASE_ADDRESS.
936 *
937 * However, given that the instruction SBA (general state base
938 * address) on this chipset is always set to 0 across X and GL,
939 * maybe this isn't required for us in particular.
940 */
941
942 if (brw->gen >= 6) {
943 uint8_t mocs = brw->gen == 7 ? GEN7_MOCS_L3 : 0;
944
945 BEGIN_BATCH(10);
946 OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (10 - 2));
947 OUT_BATCH(mocs << 8 | /* General State Memory Object Control State */
948 mocs << 4 | /* Stateless Data Port Access Memory Object Control State */
949 1); /* General State Base Address Modify Enable */
950 /* Surface state base address:
951 * BINDING_TABLE_STATE
952 * SURFACE_STATE
953 */
954 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0, 1);
955 /* Dynamic state base address:
956 * SAMPLER_STATE
957 * SAMPLER_BORDER_COLOR_STATE
958 * CLIP, SF, WM/CC viewport state
959 * COLOR_CALC_STATE
960 * DEPTH_STENCIL_STATE
961 * BLEND_STATE
962 * Push constants (when INSTPM: CONSTANT_BUFFER Address Offset
963 * Disable is clear, which we rely on)
964 */
965 OUT_RELOC(brw->batch.bo, (I915_GEM_DOMAIN_RENDER |
966 I915_GEM_DOMAIN_INSTRUCTION), 0, 1);
967
968 OUT_BATCH(1); /* Indirect object base address: MEDIA_OBJECT data */
969 OUT_RELOC(brw->cache.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
970 1); /* Instruction base address: shader kernels (incl. SIP) */
971
972 OUT_BATCH(1); /* General state upper bound */
973 /* Dynamic state upper bound. Although the documentation says that
974 * programming it to zero will cause it to be ignored, that is a lie.
975 * If this isn't programmed to a real bound, the sampler border color
976 * pointer is rejected, causing border color to mysteriously fail.
977 */
978 OUT_BATCH(0xfffff001);
979 OUT_BATCH(1); /* Indirect object upper bound */
980 OUT_BATCH(1); /* Instruction access upper bound */
981 ADVANCE_BATCH();
982 } else if (brw->gen == 5) {
983 BEGIN_BATCH(8);
984 OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (8 - 2));
985 OUT_BATCH(1); /* General state base address */
986 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0,
987 1); /* Surface state base address */
988 OUT_BATCH(1); /* Indirect object base address */
989 OUT_RELOC(brw->cache.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
990 1); /* Instruction base address */
991 OUT_BATCH(0xfffff001); /* General state upper bound */
992 OUT_BATCH(1); /* Indirect object upper bound */
993 OUT_BATCH(1); /* Instruction access upper bound */
994 ADVANCE_BATCH();
995 } else {
996 BEGIN_BATCH(6);
997 OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (6 - 2));
998 OUT_BATCH(1); /* General state base address */
999 OUT_RELOC(brw->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0,
1000 1); /* Surface state base address */
1001 OUT_BATCH(1); /* Indirect object base address */
1002 OUT_BATCH(1); /* General state upper bound */
1003 OUT_BATCH(1); /* Indirect object upper bound */
1004 ADVANCE_BATCH();
1005 }
1006
1007 /* According to section 3.6.1 of VOL1 of the 965 PRM,
1008 * STATE_BASE_ADDRESS updates require a reissue of:
1009 *
1010 * 3DSTATE_PIPELINE_POINTERS
1011 * 3DSTATE_BINDING_TABLE_POINTERS
1012 * MEDIA_STATE_POINTERS
1013 *
1014 * and this continues through Ironlake. The Sandy Bridge PRM, vol
1015 * 1 part 1 says that the folowing packets must be reissued:
1016 *
1017 * 3DSTATE_CC_POINTERS
1018 * 3DSTATE_BINDING_TABLE_POINTERS
1019 * 3DSTATE_SAMPLER_STATE_POINTERS
1020 * 3DSTATE_VIEWPORT_STATE_POINTERS
1021 * MEDIA_STATE_POINTERS
1022 *
1023 * Those are always reissued following SBA updates anyway (new
1024 * batch time), except in the case of the program cache BO
1025 * changing. Having a separate state flag makes the sequence more
1026 * obvious.
1027 */
1028
1029 brw->ctx.NewDriverState |= BRW_NEW_STATE_BASE_ADDRESS;
1030 }
1031
1032 const struct brw_tracked_state brw_state_base_address = {
1033 .dirty = {
1034 .mesa = 0,
1035 .brw = BRW_NEW_BATCH |
1036 BRW_NEW_PROGRAM_CACHE,
1037 },
1038 .emit = upload_state_base_address
1039 };