2 * Copyright © 2011 Intel Corporation
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:
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
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
26 #include "intel_batchbuffer.h"
27 #include "intel_fbo.h"
28 #include "intel_mipmap_tree.h"
30 #include "brw_context.h"
31 #include "brw_defines.h"
32 #include "brw_state.h"
34 #include "brw_blorp.h"
35 #include "gen6_blorp.h"
38 * \name Constants for BLORP VBO
41 #define GEN6_BLORP_NUM_VERTICES 3
42 #define GEN6_BLORP_NUM_VUE_ELEMS 8
43 #define GEN6_BLORP_VBO_SIZE (GEN6_BLORP_NUM_VERTICES \
44 * GEN6_BLORP_NUM_VUE_ELEMS \
49 gen6_blorp_emit_batch_head(struct brw_context *brw,
50 const brw_blorp_params *params)
52 struct gl_context *ctx = &brw->intel.ctx;
53 struct intel_context *intel = &brw->intel;
55 /* To ensure that the batch contains only the resolve, flush the batch
56 * before beginning and after finishing emitting the resolve packets.
58 * Ideally, we would not need to flush for the resolve op. But, I suspect
59 * that it's unsafe for CMD_PIPELINE_SELECT to occur multiple times in
60 * a single batch, and there is no safe way to ensure that other than by
61 * fencing the resolve with flushes. Ideally, we would just detect if
62 * a batch is in progress and do the right thing, but that would require
63 * the ability to *safely* access brw_context::state::dirty::brw
64 * outside of the brw_upload_state() codepath.
68 /* CMD_PIPELINE_SELECT
70 * Select the 3D pipeline, as opposed to the media pipeline.
74 OUT_BATCH(brw->CMD_PIPELINE_SELECT << 16);
78 /* 3DSTATE_MULTISAMPLE */
80 int length = intel->gen == 7 ? 4 : 3;
83 OUT_BATCH(_3DSTATE_MULTISAMPLE << 16 | (length - 2));
84 OUT_BATCH(MS_PIXEL_LOCATION_CENTER |
93 /* 3DSTATE_SAMPLE_MASK */
96 OUT_BATCH(_3DSTATE_SAMPLE_MASK << 16 | (2 - 2));
101 /* CMD_STATE_BASE_ADDRESS
103 * From the Sandy Bridge PRM, Volume 1, Part 1, Table STATE_BASE_ADDRESS:
104 * The following commands must be reissued following any change to the
106 * 3DSTATE_CC_POINTERS
107 * 3DSTATE_BINDING_TABLE_POINTERS
108 * 3DSTATE_SAMPLER_STATE_POINTERS
109 * 3DSTATE_VIEWPORT_STATE_POINTERS
110 * MEDIA_STATE_POINTERS
114 OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (10 - 2));
115 OUT_BATCH(1); /* GeneralStateBaseAddressModifyEnable */
116 /* SurfaceStateBaseAddress */
117 OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0, 1);
118 /* DynamicStateBaseAddress */
119 OUT_RELOC(intel->batch.bo, (I915_GEM_DOMAIN_RENDER |
120 I915_GEM_DOMAIN_INSTRUCTION), 0, 1);
121 OUT_BATCH(1); /* IndirectObjectBaseAddress */
122 OUT_BATCH(1); /* InstructionBaseAddress */
123 OUT_BATCH(1); /* GeneralStateUpperBound */
124 OUT_BATCH(1); /* DynamicStateUpperBound */
125 OUT_BATCH(1); /* IndirectObjectUpperBound*/
126 OUT_BATCH(1); /* InstructionAccessUpperBound */
132 gen6_blorp_emit_vertices(struct brw_context *brw,
133 const brw_blorp_params *params)
135 struct intel_context *intel = &brw->intel;
136 uint32_t vertex_offset;
138 /* Setup VBO for the rectangle primitive..
140 * A rectangle primitive (3DPRIM_RECTLIST) consists of only three
141 * vertices. The vertices reside in screen space with DirectX coordinates
142 * (that is, (0, 0) is the upper left corner).
149 * Since the VS is disabled, the clipper loads each VUE directly from
150 * the URB. This is controlled by the 3DSTATE_VERTEX_BUFFERS and
151 * 3DSTATE_VERTEX_ELEMENTS packets below. The VUE contents are as follows:
152 * dw0: Reserved, MBZ.
153 * dw1: Render Target Array Index. The HiZ op does not use indexed
154 * vertices, so set the dword to 0.
155 * dw2: Viewport Index. The HiZ op disables viewport mapping and
156 * scissoring, so set the dword to 0.
157 * dw3: Point Width: The HiZ op does not emit the POINTLIST primitive, so
158 * set the dword to 0.
159 * dw4: Vertex Position X.
160 * dw5: Vertex Position Y.
161 * dw6: Vertex Position Z.
162 * dw7: Vertex Position W.
164 * For details, see the Sandybridge PRM, Volume 2, Part 1, Section 1.5.1
165 * "Vertex URB Entry (VUE) Formats".
170 const float vertices[GEN6_BLORP_VBO_SIZE] = {
171 /* v0 */ 0, 0, 0, 0, params->x0, params->y1, 0, 1,
172 /* v1 */ 0, 0, 0, 0, params->x1, params->y1, 0, 1,
173 /* v2 */ 0, 0, 0, 0, params->x0, params->y0, 0, 1,
176 vertex_data = (float *) brw_state_batch(brw, AUB_TRACE_NO_TYPE,
177 GEN6_BLORP_VBO_SIZE, 32,
179 memcpy(vertex_data, vertices, GEN6_BLORP_VBO_SIZE);
182 /* 3DSTATE_VERTEX_BUFFERS */
184 const int num_buffers = 1;
185 const int batch_length = 1 + 4 * num_buffers;
187 uint32_t dw0 = GEN6_VB0_ACCESS_VERTEXDATA |
188 (GEN6_BLORP_NUM_VUE_ELEMS * sizeof(float)) << BRW_VB0_PITCH_SHIFT;
191 dw0 |= GEN7_VB0_ADDRESS_MODIFYENABLE;
193 BEGIN_BATCH(batch_length);
194 OUT_BATCH((_3DSTATE_VERTEX_BUFFERS << 16) | (batch_length - 2));
197 OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_VERTEX, 0,
200 OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_VERTEX, 0,
201 vertex_offset + GEN6_BLORP_VBO_SIZE - 1);
206 /* 3DSTATE_VERTEX_ELEMENTS
208 * Fetch dwords 0 - 7 from each VUE. See the comments above where
209 * the vertex_bo is filled with data.
212 const int num_elements = 2;
213 const int batch_length = 1 + 2 * num_elements;
215 BEGIN_BATCH(batch_length);
216 OUT_BATCH((_3DSTATE_VERTEX_ELEMENTS << 16) | (batch_length - 2));
218 OUT_BATCH(GEN6_VE0_VALID |
219 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT |
220 0 << BRW_VE0_SRC_OFFSET_SHIFT);
221 OUT_BATCH(BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT |
222 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_1_SHIFT |
223 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_2_SHIFT |
224 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_3_SHIFT);
226 OUT_BATCH(GEN6_VE0_VALID |
227 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_VE0_FORMAT_SHIFT |
228 16 << BRW_VE0_SRC_OFFSET_SHIFT);
229 OUT_BATCH(BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_0_SHIFT |
230 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_1_SHIFT |
231 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_2_SHIFT |
232 BRW_VE1_COMPONENT_STORE_SRC << BRW_VE1_COMPONENT_3_SHIFT);
238 * \brief Execute a blit or render pass operation.
240 * To execute the operation, this function manually constructs and emits a
241 * batch to draw a rectangle primitive. The batchbuffer is flushed before
242 * constructing and after emitting the batch.
244 * This function alters no GL state.
247 gen6_blorp_exec(struct intel_context *intel,
248 const brw_blorp_params *params)
250 struct gl_context *ctx = &intel->ctx;
251 struct brw_context *brw = brw_context(ctx);
252 uint32_t draw_x, draw_y;
253 uint32_t tile_mask_x, tile_mask_y;
255 params->depth.get_draw_offsets(&draw_x, &draw_y);
257 /* Compute masks to determine how much of draw_x and draw_y should be
258 * performed using the fine adjustment of "depth coordinate offset X/Y"
259 * (dw5 of 3DSTATE_DEPTH_BUFFER). See the emit_depthbuffer() function for
263 uint32_t depth_mask_x, depth_mask_y, hiz_mask_x, hiz_mask_y;
264 intel_region_get_tile_masks(params->depth.mt->region,
265 &depth_mask_x, &depth_mask_y);
266 intel_region_get_tile_masks(params->depth.mt->hiz_mt->region,
267 &hiz_mask_x, &hiz_mask_y);
269 /* Each HiZ row represents 2 rows of pixels */
270 hiz_mask_y = hiz_mask_y << 1 | 1;
272 tile_mask_x = depth_mask_x | hiz_mask_x;
273 tile_mask_y = depth_mask_y | hiz_mask_y;
276 gen6_blorp_emit_batch_head(brw, params);
277 gen6_blorp_emit_vertices(brw, params);
281 * Assign the entire URB to the VS. Even though the VS disabled, URB space
282 * is still needed because the clipper loads the VUE's from the URB. From
283 * the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE,
284 * Dword 1.15:0 "VS Number of URB Entries":
285 * This field is always used (even if VS Function Enable is DISABLED).
287 * The warning below appears in the PRM (Section 3DSTATE_URB), but we can
288 * safely ignore it because this batch contains only one draw call.
289 * Because of URB corruption caused by allocating a previous GS unit
290 * URB entry to the VS unit, software is required to send a “GS NULL
291 * Fence” (Send URB fence with VS URB size == 1 and GS URB size == 0)
292 * plus a dummy DRAW call before any case where VS will be taking over
297 OUT_BATCH(_3DSTATE_URB << 16 | (3 - 2));
298 OUT_BATCH(brw->urb.max_vs_entries << GEN6_URB_VS_ENTRIES_SHIFT);
303 /* 3DSTATE_CC_STATE_POINTERS
305 * The pointer offsets are relative to
306 * CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
308 * The HiZ op doesn't use BLEND_STATE or COLOR_CALC_STATE.
311 uint32_t depthstencil_offset;
312 gen6_blorp_emit_depth_stencil_state(brw, params, &depthstencil_offset);
315 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (4 - 2));
316 OUT_BATCH(1); /* BLEND_STATE offset */
317 OUT_BATCH(depthstencil_offset | 1); /* DEPTH_STENCIL_STATE offset */
318 OUT_BATCH(1); /* COLOR_CALC_STATE offset */
324 * Disable vertex shader.
327 /* From the BSpec, Volume 2a, Part 3 "Vertex Shader", Section
328 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
329 * [DevSNB] A pipeline flush must be programmed prior to a 3DSTATE_VS
330 * command that causes the VS Function Enable to toggle. Pipeline
331 * flush can be executed by sending a PIPE_CONTROL command with CS
332 * stall bit set and a post sync operation.
334 intel_emit_post_sync_nonzero_flush(intel);
337 OUT_BATCH(_3DSTATE_VS << 16 | (6 - 2));
348 * Disable the geometry shader.
352 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
364 * Disable the clipper.
366 * The BLORP op emits a rectangle primitive, which requires clipping to
367 * be disabled. From page 10 of the Sandy Bridge PRM Volume 2 Part 1
368 * Section 1.3 "3D Primitives Overview":
370 * Either the CLIP unit should be DISABLED, or the CLIP unit's Clip
371 * Mode should be set to a value other than CLIPMODE_NORMAL.
373 * Also disable perspective divide. This doesn't change the clipper's
374 * output, but does spare a few electrons.
378 OUT_BATCH(_3DSTATE_CLIP << 16 | (4 - 2));
380 OUT_BATCH(GEN6_CLIP_PERSPECTIVE_DIVIDE_DISABLE);
387 * Disable ViewportTransformEnable (dw2.1)
389 * From the SandyBridge PRM, Volume 2, Part 1, Section 1.3, "3D
390 * Primitives Overview":
391 * RECTLIST: Viewport Mapping must be DISABLED (as is typical with the
392 * use of screen- space coordinates).
394 * A solid rectangle must be rendered, so set FrontFaceFillMode (dw2.4:3)
395 * and BackFaceFillMode (dw2.5:6) to SOLID(0).
397 * From the Sandy Bridge PRM, Volume 2, Part 1, Section
398 * 6.4.1.1 3DSTATE_SF, Field FrontFaceFillMode:
399 * SOLID: Any triangle or rectangle object found to be front-facing
400 * is rendered as a solid object. This setting is required when
401 * (rendering rectangle (RECTLIST) objects.
405 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
406 OUT_BATCH((1 - 1) << GEN6_SF_NUM_OUTPUTS_SHIFT | /* only position */
407 1 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
408 0 << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT);
409 for (int i = 0; i < 18; ++i)
416 * Disable thread dispatch (dw5.19) and enable the HiZ op.
418 * Even though thread dispatch is disabled, max threads (dw5.25:31) must be
419 * nonzero to prevent the GPU from hanging. See the valid ranges in the
420 * BSpec, Volume 2a.11 Windower, Section 3DSTATE_WM, Dword 5.25:31
421 * "Maximum Number Of Threads".
426 switch (params->hiz_op) {
427 case GEN6_HIZ_OP_DEPTH_CLEAR:
428 assert(!"not implemented");
429 dw4 |= GEN6_WM_DEPTH_CLEAR;
431 case GEN6_HIZ_OP_DEPTH_RESOLVE:
432 dw4 |= GEN6_WM_DEPTH_RESOLVE;
434 case GEN6_HIZ_OP_HIZ_RESOLVE:
435 dw4 |= GEN6_WM_HIERARCHICAL_DEPTH_RESOLVE;
443 OUT_BATCH(_3DSTATE_WM << 16 | (9 - 2));
448 OUT_BATCH((brw->max_wm_threads - 1) << GEN6_WM_MAX_THREADS_SHIFT);
449 OUT_BATCH((1 - 1) << GEN6_WM_NUM_SF_OUTPUTS_SHIFT); /* only position */
455 /* 3DSTATE_DEPTH_BUFFER */
457 uint32_t width, height;
458 params->depth.get_miplevel_dims(&width, &height);
460 uint32_t tile_x = draw_x & tile_mask_x;
461 uint32_t tile_y = draw_y & tile_mask_y;
463 intel_region_get_aligned_offset(params->depth.mt->region,
464 draw_x & ~tile_mask_x,
465 draw_y & ~tile_mask_y);
467 /* According to the Sandy Bridge PRM, volume 2 part 1, pp326-327
468 * (3DSTATE_DEPTH_BUFFER dw5), in the documentation for "Depth
469 * Coordinate Offset X/Y":
471 * "The 3 LSBs of both offsets must be zero to ensure correct
474 * We have no guarantee that tile_x and tile_y are correctly aligned,
475 * since they are determined by the mipmap layout, which is only aligned
478 * So, to avoid hanging the GPU, just smash the low order 3 bits of
479 * tile_x and tile_y to 0. This is a temporary workaround until we come
480 * up with a better solution.
485 intel_emit_post_sync_nonzero_flush(intel);
486 intel_emit_depth_stall_flushes(intel);
489 OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (7 - 2));
490 uint32_t pitch_bytes =
491 params->depth.mt->region->pitch * params->depth.mt->region->cpp;
492 OUT_BATCH((pitch_bytes - 1) |
493 params->depth_format << 18 |
494 1 << 21 | /* separate stencil enable */
495 1 << 22 | /* hiz enable */
496 BRW_TILEWALK_YMAJOR << 26 |
497 1 << 27 | /* y-tiled */
498 BRW_SURFACE_2D << 29);
499 OUT_RELOC(params->depth.mt->region->bo,
500 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
502 OUT_BATCH(BRW_SURFACE_MIPMAPLAYOUT_BELOW << 1 |
503 (width + tile_x - 1) << 6 |
504 (height + tile_y - 1) << 19);
512 /* 3DSTATE_HIER_DEPTH_BUFFER */
514 struct intel_region *hiz_region = params->depth.mt->hiz_mt->region;
515 uint32_t hiz_offset =
516 intel_region_get_aligned_offset(hiz_region,
517 draw_x & ~tile_mask_x,
518 (draw_y & ~tile_mask_y) / 2);
521 OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
522 OUT_BATCH(hiz_region->pitch * hiz_region->cpp - 1);
523 OUT_RELOC(hiz_region->bo,
524 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
529 /* 3DSTATE_STENCIL_BUFFER */
532 OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
538 /* 3DSTATE_CLEAR_PARAMS
540 * From the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE_CLEAR_PARAMS:
541 * [DevSNB] 3DSTATE_CLEAR_PARAMS packet must follow the DEPTH_BUFFER_STATE
542 * packet when HiZ is enabled and the DEPTH_BUFFER_STATE changes.
546 OUT_BATCH(_3DSTATE_CLEAR_PARAMS << 16 | (2 - 2));
551 /* 3DSTATE_DRAWING_RECTANGLE */
554 OUT_BATCH(_3DSTATE_DRAWING_RECTANGLE << 16 | (4 - 2));
556 OUT_BATCH(((params->x1 - 1) & 0xffff) |
557 ((params->y1 - 1) << 16));
565 OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
566 _3DPRIM_RECTLIST << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
567 GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL);
568 OUT_BATCH(3); /* vertex count per instance */
570 OUT_BATCH(1); /* instance count */
576 /* See comments above at first invocation of intel_flush() in
577 * gen6_blorp_emit_batch_head().
582 brw->state.dirty.brw = ~0;
583 brw->state.dirty.cache = ~0;
587 * \param out_offset is relative to
588 * CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
591 gen6_blorp_emit_depth_stencil_state(struct brw_context *brw,
592 const brw_blorp_params *params,
593 uint32_t *out_offset)
595 struct gen6_depth_stencil_state *state;
596 state = (struct gen6_depth_stencil_state *)
597 brw_state_batch(brw, AUB_TRACE_DEPTH_STENCIL_STATE,
600 memset(state, 0, sizeof(*state));
602 /* See the following sections of the Sandy Bridge PRM, Volume 1, Part2:
603 * - 7.5.3.1 Depth Buffer Clear
604 * - 7.5.3.2 Depth Buffer Resolve
605 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
607 state->ds2.depth_write_enable = 1;
608 if (params->hiz_op == GEN6_HIZ_OP_DEPTH_RESOLVE) {
609 state->ds2.depth_test_enable = 1;
610 state->ds2.depth_test_func = COMPAREFUNC_NEVER;
614 /** \see intel_context::vtbl::resolve_hiz_slice */
616 gen6_resolve_hiz_slice(struct intel_context *intel,
617 struct intel_mipmap_tree *mt,
621 brw_hiz_op_params params(mt, level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
622 gen6_blorp_exec(intel, ¶ms);
625 /** \see intel_context::vtbl::resolve_depth_slice */
627 gen6_resolve_depth_slice(struct intel_context *intel,
628 struct intel_mipmap_tree *mt,
632 brw_hiz_op_params params(mt, level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
633 gen6_blorp_exec(intel, ¶ms);