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