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