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