gen5_render.c revision 42542f5f
103b705cfSriastradh/*
203b705cfSriastradh * Copyright © 2006,2008,2011 Intel Corporation
303b705cfSriastradh * Copyright © 2007 Red Hat, Inc.
403b705cfSriastradh *
503b705cfSriastradh * Permission is hereby granted, free of charge, to any person obtaining a
603b705cfSriastradh * copy of this software and associated documentation files (the "Software"),
703b705cfSriastradh * to deal in the Software without restriction, including without limitation
803b705cfSriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
903b705cfSriastradh * and/or sell copies of the Software, and to permit persons to whom the
1003b705cfSriastradh * Software is furnished to do so, subject to the following conditions:
1103b705cfSriastradh *
1203b705cfSriastradh * The above copyright notice and this permission notice (including the next
1303b705cfSriastradh * paragraph) shall be included in all copies or substantial portions of the
1403b705cfSriastradh * Software.
1503b705cfSriastradh *
1603b705cfSriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1703b705cfSriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1803b705cfSriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1903b705cfSriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2003b705cfSriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2103b705cfSriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2203b705cfSriastradh * SOFTWARE.
2303b705cfSriastradh *
2403b705cfSriastradh * Authors:
2503b705cfSriastradh *    Wang Zhenyu <zhenyu.z.wang@sna.com>
2603b705cfSriastradh *    Eric Anholt <eric@anholt.net>
2703b705cfSriastradh *    Carl Worth <cworth@redhat.com>
2803b705cfSriastradh *    Keith Packard <keithp@keithp.com>
2903b705cfSriastradh *    Chris Wilson <chris@chris-wilson.co.uk>
3003b705cfSriastradh *
3103b705cfSriastradh */
3203b705cfSriastradh
3303b705cfSriastradh#ifdef HAVE_CONFIG_H
3403b705cfSriastradh#include "config.h"
3503b705cfSriastradh#endif
3603b705cfSriastradh
3703b705cfSriastradh#include "sna.h"
3803b705cfSriastradh#include "sna_reg.h"
3903b705cfSriastradh#include "sna_render.h"
4003b705cfSriastradh#include "sna_render_inline.h"
4103b705cfSriastradh#include "sna_video.h"
4203b705cfSriastradh
4303b705cfSriastradh#include "brw/brw.h"
4403b705cfSriastradh#include "gen5_render.h"
4542542f5fSchristos#include "gen4_common.h"
4603b705cfSriastradh#include "gen4_source.h"
4703b705cfSriastradh#include "gen4_vertex.h"
4803b705cfSriastradh
4903b705cfSriastradh#define NO_COMPOSITE 0
5003b705cfSriastradh#define NO_COMPOSITE_SPANS 0
5103b705cfSriastradh
5203b705cfSriastradh#define PREFER_BLT_FILL 1
5303b705cfSriastradh
5403b705cfSriastradh#define DBG_NO_STATE_CACHE 0
5503b705cfSriastradh#define DBG_NO_SURFACE_CACHE 0
5603b705cfSriastradh
5742542f5fSchristos#define ALWAYS_FLUSH 0
5842542f5fSchristos
5903b705cfSriastradh#define MAX_3D_SIZE 8192
6003b705cfSriastradh
6103b705cfSriastradh#define GEN5_GRF_BLOCKS(nreg)    ((nreg + 15) / 16 - 1)
6203b705cfSriastradh
6303b705cfSriastradh/* Set up a default static partitioning of the URB, which is supposed to
6403b705cfSriastradh * allow anything we would want to do, at potentially lower performance.
6503b705cfSriastradh */
6603b705cfSriastradh#define URB_CS_ENTRY_SIZE     1
6703b705cfSriastradh#define URB_CS_ENTRIES	      0
6803b705cfSriastradh
6903b705cfSriastradh#define URB_VS_ENTRY_SIZE     1
7003b705cfSriastradh#define URB_VS_ENTRIES	      256 /* minimum of 8 */
7103b705cfSriastradh
7203b705cfSriastradh#define URB_GS_ENTRY_SIZE     0
7303b705cfSriastradh#define URB_GS_ENTRIES	      0
7403b705cfSriastradh
7503b705cfSriastradh#define URB_CLIP_ENTRY_SIZE   0
7603b705cfSriastradh#define URB_CLIP_ENTRIES      0
7703b705cfSriastradh
7803b705cfSriastradh#define URB_SF_ENTRY_SIZE     2
7903b705cfSriastradh#define URB_SF_ENTRIES	      64
8003b705cfSriastradh
8103b705cfSriastradh/*
8203b705cfSriastradh * this program computes dA/dx and dA/dy for the texture coordinates along
8303b705cfSriastradh * with the base texture coordinate. It was extracted from the Mesa driver
8403b705cfSriastradh */
8503b705cfSriastradh
8603b705cfSriastradh#define SF_KERNEL_NUM_GRF  16
8703b705cfSriastradh#define SF_MAX_THREADS	   48
8803b705cfSriastradh
8903b705cfSriastradh#define PS_KERNEL_NUM_GRF   32
9003b705cfSriastradh#define PS_MAX_THREADS	    72
9103b705cfSriastradh
9203b705cfSriastradhstatic const uint32_t ps_kernel_packed_static[][4] = {
9303b705cfSriastradh#include "exa_wm_xy.g5b"
9403b705cfSriastradh#include "exa_wm_src_affine.g5b"
9503b705cfSriastradh#include "exa_wm_src_sample_argb.g5b"
9603b705cfSriastradh#include "exa_wm_yuv_rgb.g5b"
9703b705cfSriastradh#include "exa_wm_write.g5b"
9803b705cfSriastradh};
9903b705cfSriastradh
10003b705cfSriastradhstatic const uint32_t ps_kernel_planar_static[][4] = {
10103b705cfSriastradh#include "exa_wm_xy.g5b"
10203b705cfSriastradh#include "exa_wm_src_affine.g5b"
10303b705cfSriastradh#include "exa_wm_src_sample_planar.g5b"
10403b705cfSriastradh#include "exa_wm_yuv_rgb.g5b"
10503b705cfSriastradh#include "exa_wm_write.g5b"
10603b705cfSriastradh};
10703b705cfSriastradh
10803b705cfSriastradh#define NOKERNEL(kernel_enum, func, masked) \
10903b705cfSriastradh    [kernel_enum] = {func, 0, masked}
11003b705cfSriastradh#define KERNEL(kernel_enum, kernel, masked) \
11103b705cfSriastradh    [kernel_enum] = {&kernel, sizeof(kernel), masked}
11203b705cfSriastradhstatic const struct wm_kernel_info {
11303b705cfSriastradh	const void *data;
11403b705cfSriastradh	unsigned int size;
11503b705cfSriastradh	bool has_mask;
11603b705cfSriastradh} wm_kernels[] = {
11703b705cfSriastradh	NOKERNEL(WM_KERNEL, brw_wm_kernel__affine, false),
11803b705cfSriastradh	NOKERNEL(WM_KERNEL_P, brw_wm_kernel__projective, false),
11903b705cfSriastradh
12003b705cfSriastradh	NOKERNEL(WM_KERNEL_MASK, brw_wm_kernel__affine_mask, true),
12103b705cfSriastradh	NOKERNEL(WM_KERNEL_MASK_P, brw_wm_kernel__projective_mask, true),
12203b705cfSriastradh
12303b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKCA, brw_wm_kernel__affine_mask_ca, true),
12403b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKCA_P, brw_wm_kernel__projective_mask_ca, true),
12503b705cfSriastradh
12603b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKSA, brw_wm_kernel__affine_mask_sa, true),
12703b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKSA_P, brw_wm_kernel__projective_mask_sa, true),
12803b705cfSriastradh
12903b705cfSriastradh	NOKERNEL(WM_KERNEL_OPACITY, brw_wm_kernel__affine_opacity, true),
13003b705cfSriastradh	NOKERNEL(WM_KERNEL_OPACITY_P, brw_wm_kernel__projective_opacity, true),
13103b705cfSriastradh
13203b705cfSriastradh	KERNEL(WM_KERNEL_VIDEO_PLANAR, ps_kernel_planar_static, false),
13303b705cfSriastradh	KERNEL(WM_KERNEL_VIDEO_PACKED, ps_kernel_packed_static, false),
13403b705cfSriastradh};
13503b705cfSriastradh#undef KERNEL
13603b705cfSriastradh
13703b705cfSriastradhstatic const struct blendinfo {
13803b705cfSriastradh	bool src_alpha;
13903b705cfSriastradh	uint32_t src_blend;
14003b705cfSriastradh	uint32_t dst_blend;
14103b705cfSriastradh} gen5_blend_op[] = {
14203b705cfSriastradh	/* Clear */	{0, GEN5_BLENDFACTOR_ZERO, GEN5_BLENDFACTOR_ZERO},
14303b705cfSriastradh	/* Src */	{0, GEN5_BLENDFACTOR_ONE, GEN5_BLENDFACTOR_ZERO},
14403b705cfSriastradh	/* Dst */	{0, GEN5_BLENDFACTOR_ZERO, GEN5_BLENDFACTOR_ONE},
14503b705cfSriastradh	/* Over */	{1, GEN5_BLENDFACTOR_ONE, GEN5_BLENDFACTOR_INV_SRC_ALPHA},
14603b705cfSriastradh	/* OverReverse */ {0, GEN5_BLENDFACTOR_INV_DST_ALPHA, GEN5_BLENDFACTOR_ONE},
14703b705cfSriastradh	/* In */	{0, GEN5_BLENDFACTOR_DST_ALPHA, GEN5_BLENDFACTOR_ZERO},
14803b705cfSriastradh	/* InReverse */	{1, GEN5_BLENDFACTOR_ZERO, GEN5_BLENDFACTOR_SRC_ALPHA},
14903b705cfSriastradh	/* Out */	{0, GEN5_BLENDFACTOR_INV_DST_ALPHA, GEN5_BLENDFACTOR_ZERO},
15003b705cfSriastradh	/* OutReverse */ {1, GEN5_BLENDFACTOR_ZERO, GEN5_BLENDFACTOR_INV_SRC_ALPHA},
15103b705cfSriastradh	/* Atop */	{1, GEN5_BLENDFACTOR_DST_ALPHA, GEN5_BLENDFACTOR_INV_SRC_ALPHA},
15203b705cfSriastradh	/* AtopReverse */ {1, GEN5_BLENDFACTOR_INV_DST_ALPHA, GEN5_BLENDFACTOR_SRC_ALPHA},
15303b705cfSriastradh	/* Xor */	{1, GEN5_BLENDFACTOR_INV_DST_ALPHA, GEN5_BLENDFACTOR_INV_SRC_ALPHA},
15403b705cfSriastradh	/* Add */	{0, GEN5_BLENDFACTOR_ONE, GEN5_BLENDFACTOR_ONE},
15503b705cfSriastradh};
15603b705cfSriastradh
15703b705cfSriastradh/**
15803b705cfSriastradh * Highest-valued BLENDFACTOR used in gen5_blend_op.
15903b705cfSriastradh *
16003b705cfSriastradh * This leaves out GEN5_BLENDFACTOR_INV_DST_COLOR,
16103b705cfSriastradh * GEN5_BLENDFACTOR_INV_CONST_{COLOR,ALPHA},
16203b705cfSriastradh * GEN5_BLENDFACTOR_INV_SRC1_{COLOR,ALPHA}
16303b705cfSriastradh */
16403b705cfSriastradh#define GEN5_BLENDFACTOR_COUNT (GEN5_BLENDFACTOR_INV_DST_ALPHA + 1)
16503b705cfSriastradh
16603b705cfSriastradh#define BLEND_OFFSET(s, d) \
16703b705cfSriastradh	(((s) * GEN5_BLENDFACTOR_COUNT + (d)) * 64)
16803b705cfSriastradh
16903b705cfSriastradh#define SAMPLER_OFFSET(sf, se, mf, me, k) \
17003b705cfSriastradh	((((((sf) * EXTEND_COUNT + (se)) * FILTER_COUNT + (mf)) * EXTEND_COUNT + (me)) * KERNEL_COUNT + (k)) * 64)
17103b705cfSriastradh
17203b705cfSriastradhstatic bool
17303b705cfSriastradhgen5_emit_pipelined_pointers(struct sna *sna,
17403b705cfSriastradh			     const struct sna_composite_op *op,
17503b705cfSriastradh			     int blend, int kernel);
17603b705cfSriastradh
17703b705cfSriastradh#define OUT_BATCH(v) batch_emit(sna, v)
17803b705cfSriastradh#define OUT_VERTEX(x,y) vertex_emit_2s(sna, x,y)
17903b705cfSriastradh#define OUT_VERTEX_F(v) vertex_emit(sna, v)
18003b705cfSriastradh
18103b705cfSriastradhstatic inline bool too_large(int width, int height)
18203b705cfSriastradh{
18303b705cfSriastradh	return width > MAX_3D_SIZE || height > MAX_3D_SIZE;
18403b705cfSriastradh}
18503b705cfSriastradh
18603b705cfSriastradhstatic int
18703b705cfSriastradhgen5_choose_composite_kernel(int op, bool has_mask, bool is_ca, bool is_affine)
18803b705cfSriastradh{
18903b705cfSriastradh	int base;
19003b705cfSriastradh
19103b705cfSriastradh	if (has_mask) {
19203b705cfSriastradh		if (is_ca) {
19303b705cfSriastradh			if (gen5_blend_op[op].src_alpha)
19403b705cfSriastradh				base = WM_KERNEL_MASKSA;
19503b705cfSriastradh			else
19603b705cfSriastradh				base = WM_KERNEL_MASKCA;
19703b705cfSriastradh		} else
19803b705cfSriastradh			base = WM_KERNEL_MASK;
19903b705cfSriastradh	} else
20003b705cfSriastradh		base = WM_KERNEL;
20103b705cfSriastradh
20203b705cfSriastradh	return base + !is_affine;
20303b705cfSriastradh}
20403b705cfSriastradh
20503b705cfSriastradhstatic bool gen5_magic_ca_pass(struct sna *sna,
20603b705cfSriastradh			       const struct sna_composite_op *op)
20703b705cfSriastradh{
20803b705cfSriastradh	struct gen5_render_state *state = &sna->render_state.gen5;
20903b705cfSriastradh
21003b705cfSriastradh	if (!op->need_magic_ca_pass)
21103b705cfSriastradh		return false;
21203b705cfSriastradh
21303b705cfSriastradh	assert(sna->render.vertex_index > sna->render.vertex_start);
21403b705cfSriastradh
21503b705cfSriastradh	DBG(("%s: CA fixup\n", __FUNCTION__));
21603b705cfSriastradh	assert(op->mask.bo != NULL);
21703b705cfSriastradh	assert(op->has_component_alpha);
21803b705cfSriastradh
21903b705cfSriastradh	gen5_emit_pipelined_pointers
22003b705cfSriastradh		(sna, op, PictOpAdd,
22103b705cfSriastradh		 gen5_choose_composite_kernel(PictOpAdd,
22203b705cfSriastradh					      true, true, op->is_affine));
22303b705cfSriastradh
22403b705cfSriastradh	OUT_BATCH(GEN5_3DPRIMITIVE |
22503b705cfSriastradh		  GEN5_3DPRIMITIVE_VERTEX_SEQUENTIAL |
22603b705cfSriastradh		  (_3DPRIM_RECTLIST << GEN5_3DPRIMITIVE_TOPOLOGY_SHIFT) |
22703b705cfSriastradh		  (0 << 9) |
22803b705cfSriastradh		  4);
22903b705cfSriastradh	OUT_BATCH(sna->render.vertex_index - sna->render.vertex_start);
23003b705cfSriastradh	OUT_BATCH(sna->render.vertex_start);
23103b705cfSriastradh	OUT_BATCH(1);	/* single instance */
23203b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
23303b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
23403b705cfSriastradh
23503b705cfSriastradh	state->last_primitive = sna->kgem.nbatch;
23603b705cfSriastradh	return true;
23703b705cfSriastradh}
23803b705cfSriastradh
23903b705cfSriastradhstatic uint32_t gen5_get_blend(int op,
24003b705cfSriastradh			       bool has_component_alpha,
24103b705cfSriastradh			       uint32_t dst_format)
24203b705cfSriastradh{
24303b705cfSriastradh	uint32_t src, dst;
24403b705cfSriastradh
24503b705cfSriastradh	src = gen5_blend_op[op].src_blend;
24603b705cfSriastradh	dst = gen5_blend_op[op].dst_blend;
24703b705cfSriastradh
24803b705cfSriastradh	/* If there's no dst alpha channel, adjust the blend op so that we'll treat
24903b705cfSriastradh	 * it as always 1.
25003b705cfSriastradh	 */
25103b705cfSriastradh	if (PICT_FORMAT_A(dst_format) == 0) {
25203b705cfSriastradh		if (src == GEN5_BLENDFACTOR_DST_ALPHA)
25303b705cfSriastradh			src = GEN5_BLENDFACTOR_ONE;
25403b705cfSriastradh		else if (src == GEN5_BLENDFACTOR_INV_DST_ALPHA)
25503b705cfSriastradh			src = GEN5_BLENDFACTOR_ZERO;
25603b705cfSriastradh	}
25703b705cfSriastradh
25803b705cfSriastradh	/* If the source alpha is being used, then we should only be in a
25903b705cfSriastradh	 * case where the source blend factor is 0, and the source blend
26003b705cfSriastradh	 * value is the mask channels multiplied by the source picture's alpha.
26103b705cfSriastradh	 */
26203b705cfSriastradh	if (has_component_alpha && gen5_blend_op[op].src_alpha) {
26303b705cfSriastradh		if (dst == GEN5_BLENDFACTOR_SRC_ALPHA)
26403b705cfSriastradh			dst = GEN5_BLENDFACTOR_SRC_COLOR;
26503b705cfSriastradh		else if (dst == GEN5_BLENDFACTOR_INV_SRC_ALPHA)
26603b705cfSriastradh			dst = GEN5_BLENDFACTOR_INV_SRC_COLOR;
26703b705cfSriastradh	}
26803b705cfSriastradh
26903b705cfSriastradh	DBG(("blend op=%d, dst=%x [A=%d] => src=%d, dst=%d => offset=%x\n",
27003b705cfSriastradh	     op, dst_format, PICT_FORMAT_A(dst_format),
27103b705cfSriastradh	     src, dst, BLEND_OFFSET(src, dst)));
27203b705cfSriastradh	return BLEND_OFFSET(src, dst);
27303b705cfSriastradh}
27403b705cfSriastradh
27503b705cfSriastradhstatic uint32_t gen5_get_card_format(PictFormat format)
27603b705cfSriastradh{
27703b705cfSriastradh	switch (format) {
27803b705cfSriastradh	default:
27903b705cfSriastradh		return -1;
28003b705cfSriastradh	case PICT_a8r8g8b8:
28103b705cfSriastradh		return GEN5_SURFACEFORMAT_B8G8R8A8_UNORM;
28203b705cfSriastradh	case PICT_x8r8g8b8:
28303b705cfSriastradh		return GEN5_SURFACEFORMAT_B8G8R8X8_UNORM;
28403b705cfSriastradh	case PICT_a8b8g8r8:
28503b705cfSriastradh		return GEN5_SURFACEFORMAT_R8G8B8A8_UNORM;
28603b705cfSriastradh	case PICT_x8b8g8r8:
28703b705cfSriastradh		return GEN5_SURFACEFORMAT_R8G8B8X8_UNORM;
28842542f5fSchristos#ifdef PICT_a2r10g10b10
28903b705cfSriastradh	case PICT_a2r10g10b10:
29003b705cfSriastradh		return GEN5_SURFACEFORMAT_B10G10R10A2_UNORM;
29103b705cfSriastradh	case PICT_x2r10g10b10:
29203b705cfSriastradh		return GEN5_SURFACEFORMAT_B10G10R10X2_UNORM;
29342542f5fSchristos#endif
29403b705cfSriastradh	case PICT_r8g8b8:
29503b705cfSriastradh		return GEN5_SURFACEFORMAT_R8G8B8_UNORM;
29603b705cfSriastradh	case PICT_r5g6b5:
29703b705cfSriastradh		return GEN5_SURFACEFORMAT_B5G6R5_UNORM;
29803b705cfSriastradh	case PICT_a1r5g5b5:
29903b705cfSriastradh		return GEN5_SURFACEFORMAT_B5G5R5A1_UNORM;
30003b705cfSriastradh	case PICT_a8:
30103b705cfSriastradh		return GEN5_SURFACEFORMAT_A8_UNORM;
30203b705cfSriastradh	case PICT_a4r4g4b4:
30303b705cfSriastradh		return GEN5_SURFACEFORMAT_B4G4R4A4_UNORM;
30403b705cfSriastradh	}
30503b705cfSriastradh}
30603b705cfSriastradh
30703b705cfSriastradhstatic uint32_t gen5_get_dest_format(PictFormat format)
30803b705cfSriastradh{
30903b705cfSriastradh	switch (format) {
31003b705cfSriastradh	default:
31103b705cfSriastradh		return -1;
31203b705cfSriastradh	case PICT_a8r8g8b8:
31303b705cfSriastradh	case PICT_x8r8g8b8:
31403b705cfSriastradh		return GEN5_SURFACEFORMAT_B8G8R8A8_UNORM;
31503b705cfSriastradh	case PICT_a8b8g8r8:
31603b705cfSriastradh	case PICT_x8b8g8r8:
31703b705cfSriastradh		return GEN5_SURFACEFORMAT_R8G8B8A8_UNORM;
31842542f5fSchristos#ifdef PICT_a2r10g10b10
31903b705cfSriastradh	case PICT_a2r10g10b10:
32003b705cfSriastradh	case PICT_x2r10g10b10:
32103b705cfSriastradh		return GEN5_SURFACEFORMAT_B10G10R10A2_UNORM;
32242542f5fSchristos#endif
32303b705cfSriastradh	case PICT_r5g6b5:
32403b705cfSriastradh		return GEN5_SURFACEFORMAT_B5G6R5_UNORM;
32503b705cfSriastradh	case PICT_x1r5g5b5:
32603b705cfSriastradh	case PICT_a1r5g5b5:
32703b705cfSriastradh		return GEN5_SURFACEFORMAT_B5G5R5A1_UNORM;
32803b705cfSriastradh	case PICT_a8:
32903b705cfSriastradh		return GEN5_SURFACEFORMAT_A8_UNORM;
33003b705cfSriastradh	case PICT_a4r4g4b4:
33103b705cfSriastradh	case PICT_x4r4g4b4:
33203b705cfSriastradh		return GEN5_SURFACEFORMAT_B4G4R4A4_UNORM;
33303b705cfSriastradh	}
33403b705cfSriastradh}
33503b705cfSriastradh
33603b705cfSriastradhstatic bool gen5_check_dst_format(PictFormat format)
33703b705cfSriastradh{
33803b705cfSriastradh	if (gen5_get_dest_format(format) != -1)
33903b705cfSriastradh		return true;
34003b705cfSriastradh
34103b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
34203b705cfSriastradh	return false;
34303b705cfSriastradh}
34403b705cfSriastradh
34503b705cfSriastradhstatic bool gen5_check_format(uint32_t format)
34603b705cfSriastradh{
34703b705cfSriastradh	if (gen5_get_card_format(format) != -1)
34803b705cfSriastradh		return true;
34903b705cfSriastradh
35003b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
35103b705cfSriastradh	return false;
35203b705cfSriastradh}
35303b705cfSriastradh
35403b705cfSriastradhtypedef struct gen5_surface_state_padded {
35503b705cfSriastradh	struct gen5_surface_state state;
35603b705cfSriastradh	char pad[32 - sizeof(struct gen5_surface_state)];
35703b705cfSriastradh} gen5_surface_state_padded;
35803b705cfSriastradh
35903b705cfSriastradhstatic void null_create(struct sna_static_stream *stream)
36003b705cfSriastradh{
36103b705cfSriastradh	/* A bunch of zeros useful for legacy border color and depth-stencil */
36203b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
36303b705cfSriastradh}
36403b705cfSriastradh
36503b705cfSriastradhstatic void
36603b705cfSriastradhsampler_state_init(struct gen5_sampler_state *sampler_state,
36703b705cfSriastradh		   sampler_filter_t filter,
36803b705cfSriastradh		   sampler_extend_t extend)
36903b705cfSriastradh{
37003b705cfSriastradh	sampler_state->ss0.lod_preclamp = 1;	/* GL mode */
37103b705cfSriastradh
37203b705cfSriastradh	/* We use the legacy mode to get the semantics specified by
37303b705cfSriastradh	 * the Render extension. */
37403b705cfSriastradh	sampler_state->ss0.border_color_mode = GEN5_BORDER_COLOR_MODE_LEGACY;
37503b705cfSriastradh
37603b705cfSriastradh	switch (filter) {
37703b705cfSriastradh	default:
37803b705cfSriastradh	case SAMPLER_FILTER_NEAREST:
37903b705cfSriastradh		sampler_state->ss0.min_filter = GEN5_MAPFILTER_NEAREST;
38003b705cfSriastradh		sampler_state->ss0.mag_filter = GEN5_MAPFILTER_NEAREST;
38103b705cfSriastradh		break;
38203b705cfSriastradh	case SAMPLER_FILTER_BILINEAR:
38303b705cfSriastradh		sampler_state->ss0.min_filter = GEN5_MAPFILTER_LINEAR;
38403b705cfSriastradh		sampler_state->ss0.mag_filter = GEN5_MAPFILTER_LINEAR;
38503b705cfSriastradh		break;
38603b705cfSriastradh	}
38703b705cfSriastradh
38803b705cfSriastradh	switch (extend) {
38903b705cfSriastradh	default:
39003b705cfSriastradh	case SAMPLER_EXTEND_NONE:
39103b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN5_TEXCOORDMODE_CLAMP_BORDER;
39203b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN5_TEXCOORDMODE_CLAMP_BORDER;
39303b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN5_TEXCOORDMODE_CLAMP_BORDER;
39403b705cfSriastradh		break;
39503b705cfSriastradh	case SAMPLER_EXTEND_REPEAT:
39603b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN5_TEXCOORDMODE_WRAP;
39703b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN5_TEXCOORDMODE_WRAP;
39803b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN5_TEXCOORDMODE_WRAP;
39903b705cfSriastradh		break;
40003b705cfSriastradh	case SAMPLER_EXTEND_PAD:
40103b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN5_TEXCOORDMODE_CLAMP;
40203b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN5_TEXCOORDMODE_CLAMP;
40303b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN5_TEXCOORDMODE_CLAMP;
40403b705cfSriastradh		break;
40503b705cfSriastradh	case SAMPLER_EXTEND_REFLECT:
40603b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN5_TEXCOORDMODE_MIRROR;
40703b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN5_TEXCOORDMODE_MIRROR;
40803b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN5_TEXCOORDMODE_MIRROR;
40903b705cfSriastradh		break;
41003b705cfSriastradh	}
41103b705cfSriastradh}
41203b705cfSriastradh
41303b705cfSriastradhstatic uint32_t gen5_filter(uint32_t filter)
41403b705cfSriastradh{
41503b705cfSriastradh	switch (filter) {
41603b705cfSriastradh	default:
41703b705cfSriastradh		assert(0);
41803b705cfSriastradh	case PictFilterNearest:
41903b705cfSriastradh		return SAMPLER_FILTER_NEAREST;
42003b705cfSriastradh	case PictFilterBilinear:
42103b705cfSriastradh		return SAMPLER_FILTER_BILINEAR;
42203b705cfSriastradh	}
42303b705cfSriastradh}
42403b705cfSriastradh
42503b705cfSriastradhstatic uint32_t gen5_check_filter(PicturePtr picture)
42603b705cfSriastradh{
42703b705cfSriastradh	switch (picture->filter) {
42803b705cfSriastradh	case PictFilterNearest:
42903b705cfSriastradh	case PictFilterBilinear:
43003b705cfSriastradh		return true;
43103b705cfSriastradh	default:
43203b705cfSriastradh		DBG(("%s: unknown filter: %x\n", __FUNCTION__, picture->filter));
43303b705cfSriastradh		return false;
43403b705cfSriastradh	}
43503b705cfSriastradh}
43603b705cfSriastradh
43703b705cfSriastradhstatic uint32_t gen5_repeat(uint32_t repeat)
43803b705cfSriastradh{
43903b705cfSriastradh	switch (repeat) {
44003b705cfSriastradh	default:
44103b705cfSriastradh		assert(0);
44203b705cfSriastradh	case RepeatNone:
44303b705cfSriastradh		return SAMPLER_EXTEND_NONE;
44403b705cfSriastradh	case RepeatNormal:
44503b705cfSriastradh		return SAMPLER_EXTEND_REPEAT;
44603b705cfSriastradh	case RepeatPad:
44703b705cfSriastradh		return SAMPLER_EXTEND_PAD;
44803b705cfSriastradh	case RepeatReflect:
44903b705cfSriastradh		return SAMPLER_EXTEND_REFLECT;
45003b705cfSriastradh	}
45103b705cfSriastradh}
45203b705cfSriastradh
45303b705cfSriastradhstatic bool gen5_check_repeat(PicturePtr picture)
45403b705cfSriastradh{
45503b705cfSriastradh	if (!picture->repeat)
45603b705cfSriastradh		return true;
45703b705cfSriastradh
45803b705cfSriastradh	switch (picture->repeatType) {
45903b705cfSriastradh	case RepeatNone:
46003b705cfSriastradh	case RepeatNormal:
46103b705cfSriastradh	case RepeatPad:
46203b705cfSriastradh	case RepeatReflect:
46303b705cfSriastradh		return true;
46403b705cfSriastradh	default:
46503b705cfSriastradh		DBG(("%s: unknown repeat: %x\n",
46603b705cfSriastradh		     __FUNCTION__, picture->repeatType));
46703b705cfSriastradh		return false;
46803b705cfSriastradh	}
46903b705cfSriastradh}
47003b705cfSriastradh
47103b705cfSriastradhstatic uint32_t
47203b705cfSriastradhgen5_tiling_bits(uint32_t tiling)
47303b705cfSriastradh{
47403b705cfSriastradh	switch (tiling) {
47503b705cfSriastradh	default: assert(0);
47603b705cfSriastradh	case I915_TILING_NONE: return 0;
47703b705cfSriastradh	case I915_TILING_X: return GEN5_SURFACE_TILED;
47803b705cfSriastradh	case I915_TILING_Y: return GEN5_SURFACE_TILED | GEN5_SURFACE_TILED_Y;
47903b705cfSriastradh	}
48003b705cfSriastradh}
48103b705cfSriastradh
48203b705cfSriastradh/**
48303b705cfSriastradh * Sets up the common fields for a surface state buffer for the given
48403b705cfSriastradh * picture in the given surface state buffer.
48503b705cfSriastradh */
48603b705cfSriastradhstatic uint32_t
48703b705cfSriastradhgen5_bind_bo(struct sna *sna,
48803b705cfSriastradh	     struct kgem_bo *bo,
48903b705cfSriastradh	     uint32_t width,
49003b705cfSriastradh	     uint32_t height,
49103b705cfSriastradh	     uint32_t format,
49203b705cfSriastradh	     bool is_dst)
49303b705cfSriastradh{
49403b705cfSriastradh	uint32_t domains;
49503b705cfSriastradh	uint16_t offset;
49603b705cfSriastradh	uint32_t *ss;
49703b705cfSriastradh
49803b705cfSriastradh	/* After the first bind, we manage the cache domains within the batch */
49903b705cfSriastradh	if (!DBG_NO_SURFACE_CACHE) {
50003b705cfSriastradh		offset = kgem_bo_get_binding(bo, format | is_dst << 31);
50103b705cfSriastradh		if (offset) {
50203b705cfSriastradh			if (is_dst)
50303b705cfSriastradh				kgem_bo_mark_dirty(bo);
50442542f5fSchristos			assert(offset >= sna->kgem.surface);
50503b705cfSriastradh			return offset * sizeof(uint32_t);
50603b705cfSriastradh		}
50703b705cfSriastradh	}
50803b705cfSriastradh
50903b705cfSriastradh	offset = sna->kgem.surface -=
51003b705cfSriastradh		sizeof(struct gen5_surface_state_padded) / sizeof(uint32_t);
51103b705cfSriastradh	ss = sna->kgem.batch + offset;
51203b705cfSriastradh
51303b705cfSriastradh	ss[0] = (GEN5_SURFACE_2D << GEN5_SURFACE_TYPE_SHIFT |
51403b705cfSriastradh		 GEN5_SURFACE_BLEND_ENABLED |
51503b705cfSriastradh		 format << GEN5_SURFACE_FORMAT_SHIFT);
51603b705cfSriastradh
51703b705cfSriastradh	if (is_dst) {
51803b705cfSriastradh		ss[0] |= GEN5_SURFACE_RC_READ_WRITE;
51903b705cfSriastradh		domains = I915_GEM_DOMAIN_RENDER << 16 | I915_GEM_DOMAIN_RENDER;
52003b705cfSriastradh	} else
52103b705cfSriastradh		domains = I915_GEM_DOMAIN_SAMPLER << 16;
52203b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, offset + 1, bo, domains, 0);
52303b705cfSriastradh
52403b705cfSriastradh	ss[2] = ((width - 1)  << GEN5_SURFACE_WIDTH_SHIFT |
52503b705cfSriastradh		 (height - 1) << GEN5_SURFACE_HEIGHT_SHIFT);
52603b705cfSriastradh	ss[3] = (gen5_tiling_bits(bo->tiling) |
52703b705cfSriastradh		 (bo->pitch - 1) << GEN5_SURFACE_PITCH_SHIFT);
52803b705cfSriastradh	ss[4] = 0;
52903b705cfSriastradh	ss[5] = 0;
53003b705cfSriastradh
53103b705cfSriastradh	kgem_bo_set_binding(bo, format | is_dst << 31, offset);
53203b705cfSriastradh
53303b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, tiling=%d -> %s\n",
53403b705cfSriastradh	     offset, bo->handle, ss[1],
53503b705cfSriastradh	     format, width, height, bo->pitch, bo->tiling,
53603b705cfSriastradh	     domains & 0xffff ? "render" : "sampler"));
53703b705cfSriastradh
53803b705cfSriastradh	return offset * sizeof(uint32_t);
53903b705cfSriastradh}
54003b705cfSriastradh
54103b705cfSriastradhstatic void gen5_emit_vertex_buffer(struct sna *sna,
54203b705cfSriastradh				    const struct sna_composite_op *op)
54303b705cfSriastradh{
54403b705cfSriastradh	int id = op->u.gen5.ve_id;
54503b705cfSriastradh
54603b705cfSriastradh	assert((sna->render.vb_id & (1 << id)) == 0);
54703b705cfSriastradh
54803b705cfSriastradh	OUT_BATCH(GEN5_3DSTATE_VERTEX_BUFFERS | 3);
54903b705cfSriastradh	OUT_BATCH(id << VB0_BUFFER_INDEX_SHIFT | VB0_VERTEXDATA |
55003b705cfSriastradh		  (4*op->floats_per_vertex << VB0_BUFFER_PITCH_SHIFT));
55103b705cfSriastradh	assert(sna->render.nvertex_reloc < ARRAY_SIZE(sna->render.vertex_reloc));
55203b705cfSriastradh	sna->render.vertex_reloc[sna->render.nvertex_reloc++] = sna->kgem.nbatch;
55303b705cfSriastradh	OUT_BATCH(0);
55403b705cfSriastradh	OUT_BATCH(~0); /* max address: disabled */
55503b705cfSriastradh	OUT_BATCH(0);
55603b705cfSriastradh
55703b705cfSriastradh	sna->render.vb_id |= 1 << id;
55803b705cfSriastradh}
55903b705cfSriastradh
56003b705cfSriastradhstatic void gen5_emit_primitive(struct sna *sna)
56103b705cfSriastradh{
56203b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen5.last_primitive) {
56303b705cfSriastradh		sna->render.vertex_offset = sna->kgem.nbatch - 5;
56403b705cfSriastradh		return;
56503b705cfSriastradh	}
56603b705cfSriastradh
56703b705cfSriastradh	OUT_BATCH(GEN5_3DPRIMITIVE |
56803b705cfSriastradh		  GEN5_3DPRIMITIVE_VERTEX_SEQUENTIAL |
56903b705cfSriastradh		  (_3DPRIM_RECTLIST << GEN5_3DPRIMITIVE_TOPOLOGY_SHIFT) |
57003b705cfSriastradh		  (0 << 9) |
57103b705cfSriastradh		  4);
57203b705cfSriastradh	sna->render.vertex_offset = sna->kgem.nbatch;
57303b705cfSriastradh	OUT_BATCH(0);	/* vertex count, to be filled in later */
57403b705cfSriastradh	OUT_BATCH(sna->render.vertex_index);
57503b705cfSriastradh	OUT_BATCH(1);	/* single instance */
57603b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
57703b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
57803b705cfSriastradh	sna->render.vertex_start = sna->render.vertex_index;
57903b705cfSriastradh
58003b705cfSriastradh	sna->render_state.gen5.last_primitive = sna->kgem.nbatch;
58103b705cfSriastradh}
58203b705cfSriastradh
58303b705cfSriastradhstatic bool gen5_rectangle_begin(struct sna *sna,
58403b705cfSriastradh				 const struct sna_composite_op *op)
58503b705cfSriastradh{
58603b705cfSriastradh	int id = op->u.gen5.ve_id;
58703b705cfSriastradh	int ndwords;
58803b705cfSriastradh
58903b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render) && sna->render.vertex_offset)
59003b705cfSriastradh		return true;
59103b705cfSriastradh
59203b705cfSriastradh	ndwords = op->need_magic_ca_pass ? 20 : 6;
59303b705cfSriastradh	if ((sna->render.vb_id & (1 << id)) == 0)
59403b705cfSriastradh		ndwords += 5;
59503b705cfSriastradh
59603b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, ndwords))
59703b705cfSriastradh		return false;
59803b705cfSriastradh
59903b705cfSriastradh	if ((sna->render.vb_id & (1 << id)) == 0)
60003b705cfSriastradh		gen5_emit_vertex_buffer(sna, op);
60103b705cfSriastradh	if (sna->render.vertex_offset == 0)
60203b705cfSriastradh		gen5_emit_primitive(sna);
60303b705cfSriastradh
60403b705cfSriastradh	return true;
60503b705cfSriastradh}
60603b705cfSriastradh
60703b705cfSriastradhstatic int gen5_get_rectangles__flush(struct sna *sna,
60803b705cfSriastradh				      const struct sna_composite_op *op)
60903b705cfSriastradh{
61003b705cfSriastradh	/* Preventing discarding new vbo after lock contention */
61103b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render)) {
61203b705cfSriastradh		int rem = vertex_space(sna);
61303b705cfSriastradh		if (rem > op->floats_per_rect)
61403b705cfSriastradh			return rem;
61503b705cfSriastradh	}
61603b705cfSriastradh
61742542f5fSchristos	if (!kgem_check_batch(&sna->kgem, op->need_magic_ca_pass ? 40 : 6))
61803b705cfSriastradh		return 0;
61903b705cfSriastradh	if (!kgem_check_reloc_and_exec(&sna->kgem, 2))
62003b705cfSriastradh		return 0;
62103b705cfSriastradh
62203b705cfSriastradh	if (sna->render.vertex_offset) {
62303b705cfSriastradh		gen4_vertex_flush(sna);
62403b705cfSriastradh		if (gen5_magic_ca_pass(sna, op))
62503b705cfSriastradh			gen5_emit_pipelined_pointers(sna, op, op->op,
62603b705cfSriastradh						     op->u.gen5.wm_kernel);
62703b705cfSriastradh	}
62803b705cfSriastradh
62903b705cfSriastradh	return gen4_vertex_finish(sna);
63003b705cfSriastradh}
63103b705cfSriastradh
63203b705cfSriastradhinline static int gen5_get_rectangles(struct sna *sna,
63303b705cfSriastradh				      const struct sna_composite_op *op,
63403b705cfSriastradh				      int want,
63503b705cfSriastradh				      void (*emit_state)(struct sna *sna,
63603b705cfSriastradh							 const struct sna_composite_op *op))
63703b705cfSriastradh{
63803b705cfSriastradh	int rem;
63903b705cfSriastradh
64003b705cfSriastradh	assert(want);
64103b705cfSriastradh
64203b705cfSriastradhstart:
64303b705cfSriastradh	rem = vertex_space(sna);
64403b705cfSriastradh	if (unlikely(rem < op->floats_per_rect)) {
64503b705cfSriastradh		DBG(("flushing vbo for %s: %d < %d\n",
64603b705cfSriastradh		     __FUNCTION__, rem, op->floats_per_rect));
64703b705cfSriastradh		rem = gen5_get_rectangles__flush(sna, op);
64803b705cfSriastradh		if (unlikely (rem == 0))
64903b705cfSriastradh			goto flush;
65003b705cfSriastradh	}
65103b705cfSriastradh
65203b705cfSriastradh	if (unlikely(sna->render.vertex_offset == 0)) {
65303b705cfSriastradh		if (!gen5_rectangle_begin(sna, op))
65403b705cfSriastradh			goto flush;
65503b705cfSriastradh		else
65603b705cfSriastradh			goto start;
65703b705cfSriastradh	}
65803b705cfSriastradh
65903b705cfSriastradh	assert(rem <= vertex_space(sna));
66003b705cfSriastradh	assert(op->floats_per_rect <= rem);
66103b705cfSriastradh	if (want > 1 && want * op->floats_per_rect > rem)
66203b705cfSriastradh		want = rem / op->floats_per_rect;
66303b705cfSriastradh
66403b705cfSriastradh	sna->render.vertex_index += 3*want;
66503b705cfSriastradh	return want;
66603b705cfSriastradh
66703b705cfSriastradhflush:
66803b705cfSriastradh	if (sna->render.vertex_offset) {
66903b705cfSriastradh		gen4_vertex_flush(sna);
67003b705cfSriastradh		gen5_magic_ca_pass(sna, op);
67103b705cfSriastradh	}
67203b705cfSriastradh	sna_vertex_wait__locked(&sna->render);
67303b705cfSriastradh	_kgem_submit(&sna->kgem);
67403b705cfSriastradh	emit_state(sna, op);
67503b705cfSriastradh	goto start;
67603b705cfSriastradh}
67703b705cfSriastradh
67803b705cfSriastradhstatic uint32_t *
67903b705cfSriastradhgen5_composite_get_binding_table(struct sna *sna,
68003b705cfSriastradh				 uint16_t *offset)
68103b705cfSriastradh{
68203b705cfSriastradh	sna->kgem.surface -=
68303b705cfSriastradh		sizeof(struct gen5_surface_state_padded) / sizeof(uint32_t);
68403b705cfSriastradh
68503b705cfSriastradh	DBG(("%s(%x)\n", __FUNCTION__, 4*sna->kgem.surface));
68603b705cfSriastradh
68703b705cfSriastradh	/* Clear all surplus entries to zero in case of prefetch */
68803b705cfSriastradh	*offset = sna->kgem.surface;
68903b705cfSriastradh	return memset(sna->kgem.batch + sna->kgem.surface,
69003b705cfSriastradh		      0, sizeof(struct gen5_surface_state_padded));
69103b705cfSriastradh}
69203b705cfSriastradh
69303b705cfSriastradhstatic void
69403b705cfSriastradhgen5_emit_urb(struct sna *sna)
69503b705cfSriastradh{
69603b705cfSriastradh	int urb_vs_start, urb_vs_size;
69703b705cfSriastradh	int urb_gs_start, urb_gs_size;
69803b705cfSriastradh	int urb_clip_start, urb_clip_size;
69903b705cfSriastradh	int urb_sf_start, urb_sf_size;
70003b705cfSriastradh	int urb_cs_start, urb_cs_size;
70103b705cfSriastradh
70203b705cfSriastradh	urb_vs_start = 0;
70303b705cfSriastradh	urb_vs_size = URB_VS_ENTRIES * URB_VS_ENTRY_SIZE;
70403b705cfSriastradh	urb_gs_start = urb_vs_start + urb_vs_size;
70503b705cfSriastradh	urb_gs_size = URB_GS_ENTRIES * URB_GS_ENTRY_SIZE;
70603b705cfSriastradh	urb_clip_start = urb_gs_start + urb_gs_size;
70703b705cfSriastradh	urb_clip_size = URB_CLIP_ENTRIES * URB_CLIP_ENTRY_SIZE;
70803b705cfSriastradh	urb_sf_start = urb_clip_start + urb_clip_size;
70903b705cfSriastradh	urb_sf_size = URB_SF_ENTRIES * URB_SF_ENTRY_SIZE;
71003b705cfSriastradh	urb_cs_start = urb_sf_start + urb_sf_size;
71103b705cfSriastradh	urb_cs_size = URB_CS_ENTRIES * URB_CS_ENTRY_SIZE;
71203b705cfSriastradh
71303b705cfSriastradh	OUT_BATCH(GEN5_URB_FENCE |
71403b705cfSriastradh		  UF0_CS_REALLOC |
71503b705cfSriastradh		  UF0_SF_REALLOC |
71603b705cfSriastradh		  UF0_CLIP_REALLOC |
71703b705cfSriastradh		  UF0_GS_REALLOC |
71803b705cfSriastradh		  UF0_VS_REALLOC |
71903b705cfSriastradh		  1);
72003b705cfSriastradh	OUT_BATCH(((urb_clip_start + urb_clip_size) << UF1_CLIP_FENCE_SHIFT) |
72103b705cfSriastradh		  ((urb_gs_start + urb_gs_size) << UF1_GS_FENCE_SHIFT) |
72203b705cfSriastradh		  ((urb_vs_start + urb_vs_size) << UF1_VS_FENCE_SHIFT));
72303b705cfSriastradh	OUT_BATCH(((urb_cs_start + urb_cs_size) << UF2_CS_FENCE_SHIFT) |
72403b705cfSriastradh		  ((urb_sf_start + urb_sf_size) << UF2_SF_FENCE_SHIFT));
72503b705cfSriastradh
72603b705cfSriastradh	/* Constant buffer state */
72703b705cfSriastradh	OUT_BATCH(GEN5_CS_URB_STATE | 0);
72803b705cfSriastradh	OUT_BATCH((URB_CS_ENTRY_SIZE - 1) << 4 | URB_CS_ENTRIES << 0);
72903b705cfSriastradh}
73003b705cfSriastradh
73103b705cfSriastradhstatic void
73203b705cfSriastradhgen5_emit_state_base_address(struct sna *sna)
73303b705cfSriastradh{
73403b705cfSriastradh	assert(sna->render_state.gen5.general_bo->proxy == NULL);
73503b705cfSriastradh	OUT_BATCH(GEN5_STATE_BASE_ADDRESS | 6);
73603b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* general */
73703b705cfSriastradh				 sna->kgem.nbatch,
73803b705cfSriastradh				 sna->render_state.gen5.general_bo,
73903b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
74003b705cfSriastradh				 BASE_ADDRESS_MODIFY));
74103b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* surface */
74203b705cfSriastradh				 sna->kgem.nbatch,
74303b705cfSriastradh				 NULL,
74403b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
74503b705cfSriastradh				 BASE_ADDRESS_MODIFY));
74603b705cfSriastradh	OUT_BATCH(0); /* media */
74703b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* instruction */
74803b705cfSriastradh				 sna->kgem.nbatch,
74903b705cfSriastradh				 sna->render_state.gen5.general_bo,
75003b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
75103b705cfSriastradh				 BASE_ADDRESS_MODIFY));
75203b705cfSriastradh
75303b705cfSriastradh	/* upper bounds, all disabled */
75403b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
75503b705cfSriastradh	OUT_BATCH(0);
75603b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
75703b705cfSriastradh}
75803b705cfSriastradh
75903b705cfSriastradhstatic void
76003b705cfSriastradhgen5_emit_invariant(struct sna *sna)
76103b705cfSriastradh{
76203b705cfSriastradh	/* Ironlake errata workaround: Before disabling the clipper,
76303b705cfSriastradh	 * you have to MI_FLUSH to get the pipeline idle.
76403b705cfSriastradh	 *
76503b705cfSriastradh	 * However, the kernel flushes the pipeline between batches,
76603b705cfSriastradh	 * so we should be safe....
76703b705cfSriastradh	 *
76803b705cfSriastradh	 * On the other hand, after using BLT we must use a non-pipelined
76903b705cfSriastradh	 * operation...
77003b705cfSriastradh	 */
77103b705cfSriastradh	if (sna->kgem.nreloc)
77203b705cfSriastradh		OUT_BATCH(MI_FLUSH | MI_INHIBIT_RENDER_CACHE_FLUSH);
77303b705cfSriastradh
77403b705cfSriastradh	OUT_BATCH(GEN5_PIPELINE_SELECT | PIPELINE_SELECT_3D);
77503b705cfSriastradh
77603b705cfSriastradh	gen5_emit_state_base_address(sna);
77703b705cfSriastradh
77803b705cfSriastradh	sna->render_state.gen5.needs_invariant = false;
77903b705cfSriastradh}
78003b705cfSriastradh
78103b705cfSriastradhstatic void
78203b705cfSriastradhgen5_get_batch(struct sna *sna, const struct sna_composite_op *op)
78303b705cfSriastradh{
78403b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, op->dst.bo);
78503b705cfSriastradh
78603b705cfSriastradh	if (!kgem_check_batch_with_surfaces(&sna->kgem, 150, 4)) {
78703b705cfSriastradh		DBG(("%s: flushing batch: %d < %d+%d\n",
78803b705cfSriastradh		     __FUNCTION__, sna->kgem.surface - sna->kgem.nbatch,
78903b705cfSriastradh		     150, 4*8));
79003b705cfSriastradh		kgem_submit(&sna->kgem);
79103b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
79203b705cfSriastradh	}
79303b705cfSriastradh
79403b705cfSriastradh	if (sna->render_state.gen5.needs_invariant)
79503b705cfSriastradh		gen5_emit_invariant(sna);
79603b705cfSriastradh}
79703b705cfSriastradh
79803b705cfSriastradhstatic void
79903b705cfSriastradhgen5_align_vertex(struct sna *sna, const struct sna_composite_op *op)
80003b705cfSriastradh{
80103b705cfSriastradh	assert(op->floats_per_rect == 3*op->floats_per_vertex);
80203b705cfSriastradh	if (op->floats_per_vertex != sna->render_state.gen5.floats_per_vertex) {
80342542f5fSchristos		DBG(("aligning vertex: was %d, now %d floats per vertex\n",
80403b705cfSriastradh		     sna->render_state.gen5.floats_per_vertex,
80542542f5fSchristos		     op->floats_per_vertex));
80642542f5fSchristos		gen4_vertex_align(sna, op);
80703b705cfSriastradh		sna->render_state.gen5.floats_per_vertex = op->floats_per_vertex;
80803b705cfSriastradh	}
80903b705cfSriastradh}
81003b705cfSriastradh
81103b705cfSriastradhstatic void
81203b705cfSriastradhgen5_emit_binding_table(struct sna *sna, uint16_t offset)
81303b705cfSriastradh{
81403b705cfSriastradh	if (!DBG_NO_STATE_CACHE &&
81503b705cfSriastradh	    sna->render_state.gen5.surface_table == offset)
81603b705cfSriastradh		return;
81703b705cfSriastradh
81803b705cfSriastradh	sna->render_state.gen5.surface_table = offset;
81903b705cfSriastradh
82003b705cfSriastradh	/* Binding table pointers */
82103b705cfSriastradh	OUT_BATCH(GEN5_3DSTATE_BINDING_TABLE_POINTERS | 4);
82203b705cfSriastradh	OUT_BATCH(0);		/* vs */
82303b705cfSriastradh	OUT_BATCH(0);		/* gs */
82403b705cfSriastradh	OUT_BATCH(0);		/* clip */
82503b705cfSriastradh	OUT_BATCH(0);		/* sf */
82603b705cfSriastradh	/* Only the PS uses the binding table */
82703b705cfSriastradh	OUT_BATCH(offset*4);
82803b705cfSriastradh}
82903b705cfSriastradh
83003b705cfSriastradhstatic bool
83103b705cfSriastradhgen5_emit_pipelined_pointers(struct sna *sna,
83203b705cfSriastradh			     const struct sna_composite_op *op,
83303b705cfSriastradh			     int blend, int kernel)
83403b705cfSriastradh{
83503b705cfSriastradh	uint16_t sp, bp;
83603b705cfSriastradh	uint32_t key;
83703b705cfSriastradh
83803b705cfSriastradh	DBG(("%s: has_mask=%d, src=(%d, %d), mask=(%d, %d),kernel=%d, blend=%d, ca=%d, format=%x\n",
83903b705cfSriastradh	     __FUNCTION__, op->u.gen5.ve_id & 2,
84003b705cfSriastradh	     op->src.filter, op->src.repeat,
84103b705cfSriastradh	     op->mask.filter, op->mask.repeat,
84203b705cfSriastradh	     kernel, blend, op->has_component_alpha, (int)op->dst.format));
84303b705cfSriastradh
84403b705cfSriastradh	sp = SAMPLER_OFFSET(op->src.filter, op->src.repeat,
84503b705cfSriastradh			    op->mask.filter, op->mask.repeat,
84603b705cfSriastradh			    kernel);
84703b705cfSriastradh	bp = gen5_get_blend(blend, op->has_component_alpha, op->dst.format);
84803b705cfSriastradh
84903b705cfSriastradh	key = sp | (uint32_t)bp << 16 | (op->mask.bo != NULL) << 31;
85003b705cfSriastradh	DBG(("%s: sp=%d, bp=%d, key=%08x (current sp=%d, bp=%d, key=%08x)\n",
85103b705cfSriastradh	     __FUNCTION__, sp, bp, key,
85203b705cfSriastradh	     sna->render_state.gen5.last_pipelined_pointers & 0xffff,
85303b705cfSriastradh	     (sna->render_state.gen5.last_pipelined_pointers >> 16) & 0x7fff,
85403b705cfSriastradh	     sna->render_state.gen5.last_pipelined_pointers));
85503b705cfSriastradh	if (key == sna->render_state.gen5.last_pipelined_pointers)
85603b705cfSriastradh		return false;
85703b705cfSriastradh
85803b705cfSriastradh	OUT_BATCH(GEN5_3DSTATE_PIPELINED_POINTERS | 5);
85903b705cfSriastradh	OUT_BATCH(sna->render_state.gen5.vs);
86003b705cfSriastradh	OUT_BATCH(GEN5_GS_DISABLE); /* passthrough */
86103b705cfSriastradh	OUT_BATCH(GEN5_CLIP_DISABLE); /* passthrough */
86203b705cfSriastradh	OUT_BATCH(sna->render_state.gen5.sf[op->mask.bo != NULL]);
86303b705cfSriastradh	OUT_BATCH(sna->render_state.gen5.wm + sp);
86403b705cfSriastradh	OUT_BATCH(sna->render_state.gen5.cc + bp);
86503b705cfSriastradh
86603b705cfSriastradh	bp = (sna->render_state.gen5.last_pipelined_pointers & 0x7fff0000) != ((uint32_t)bp << 16);
86703b705cfSriastradh	sna->render_state.gen5.last_pipelined_pointers = key;
86803b705cfSriastradh
86903b705cfSriastradh	gen5_emit_urb(sna);
87003b705cfSriastradh
87103b705cfSriastradh	return bp;
87203b705cfSriastradh}
87303b705cfSriastradh
87403b705cfSriastradhstatic bool
87503b705cfSriastradhgen5_emit_drawing_rectangle(struct sna *sna, const struct sna_composite_op *op)
87603b705cfSriastradh{
87703b705cfSriastradh	uint32_t limit = (op->dst.height - 1) << 16 | (op->dst.width - 1);
87803b705cfSriastradh	uint32_t offset = (uint16_t)op->dst.y << 16 | (uint16_t)op->dst.x;
87903b705cfSriastradh
88003b705cfSriastradh	assert(!too_large(op->dst.x, op->dst.y));
88103b705cfSriastradh	assert(!too_large(op->dst.width, op->dst.height));
88203b705cfSriastradh
88303b705cfSriastradh	if (!DBG_NO_STATE_CACHE &&
88403b705cfSriastradh	    sna->render_state.gen5.drawrect_limit == limit &&
88503b705cfSriastradh	    sna->render_state.gen5.drawrect_offset == offset)
88603b705cfSriastradh		return false;
88703b705cfSriastradh
88803b705cfSriastradh	sna->render_state.gen5.drawrect_offset = offset;
88903b705cfSriastradh	sna->render_state.gen5.drawrect_limit = limit;
89003b705cfSriastradh
89103b705cfSriastradh	OUT_BATCH(GEN5_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
89203b705cfSriastradh	OUT_BATCH(0x00000000);
89303b705cfSriastradh	OUT_BATCH(limit);
89403b705cfSriastradh	OUT_BATCH(offset);
89503b705cfSriastradh	return true;
89603b705cfSriastradh}
89703b705cfSriastradh
89803b705cfSriastradhstatic void
89903b705cfSriastradhgen5_emit_vertex_elements(struct sna *sna,
90003b705cfSriastradh			  const struct sna_composite_op *op)
90103b705cfSriastradh{
90203b705cfSriastradh	/*
90303b705cfSriastradh	 * vertex data in vertex buffer
90403b705cfSriastradh	 *    position: (x, y)
90503b705cfSriastradh	 *    texture coordinate 0: (u0, v0) if (is_affine is true) else (u0, v0, w0)
90603b705cfSriastradh	 *    texture coordinate 1 if (has_mask is true): same as above
90703b705cfSriastradh	 */
90803b705cfSriastradh	struct gen5_render_state *render = &sna->render_state.gen5;
90903b705cfSriastradh	int id = op->u.gen5.ve_id;
91003b705cfSriastradh	bool has_mask = id >> 2;
91103b705cfSriastradh	uint32_t format, dw;
91203b705cfSriastradh
91303b705cfSriastradh	if (!DBG_NO_STATE_CACHE && render->ve_id == id)
91403b705cfSriastradh		return;
91503b705cfSriastradh
91603b705cfSriastradh	DBG(("%s: changing %d -> %d\n", __FUNCTION__, render->ve_id, id));
91703b705cfSriastradh	render->ve_id = id;
91803b705cfSriastradh
91903b705cfSriastradh	/* The VUE layout
92003b705cfSriastradh	 *    dword 0-3: pad (0.0, 0.0, 0.0. 0.0)
92103b705cfSriastradh	 *    dword 4-7: position (x, y, 1.0, 1.0),
92203b705cfSriastradh	 *    dword 8-11: texture coordinate 0 (u0, v0, w0, 1.0)
92303b705cfSriastradh	 *    dword 12-15: texture coordinate 1 (u1, v1, w1, 1.0)
92403b705cfSriastradh	 *
92503b705cfSriastradh	 * dword 4-15 are fetched from vertex buffer
92603b705cfSriastradh	 */
92703b705cfSriastradh	OUT_BATCH(GEN5_3DSTATE_VERTEX_ELEMENTS |
92803b705cfSriastradh		((2 * (has_mask ? 4 : 3)) + 1 - 2));
92903b705cfSriastradh
93003b705cfSriastradh	OUT_BATCH((id << VE0_VERTEX_BUFFER_INDEX_SHIFT) | VE0_VALID |
93103b705cfSriastradh		  (GEN5_SURFACEFORMAT_R32G32B32A32_FLOAT << VE0_FORMAT_SHIFT) |
93203b705cfSriastradh		  (0 << VE0_OFFSET_SHIFT));
93303b705cfSriastradh	OUT_BATCH((VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT) |
93403b705cfSriastradh		  (VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT) |
93503b705cfSriastradh		  (VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT) |
93603b705cfSriastradh		  (VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_3_SHIFT));
93703b705cfSriastradh
93803b705cfSriastradh	/* x,y */
93903b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
94003b705cfSriastradh		  GEN5_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
94103b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
94203b705cfSriastradh	OUT_BATCH(VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
94303b705cfSriastradh		  VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
94403b705cfSriastradh		  VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT |
94503b705cfSriastradh		  VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
94603b705cfSriastradh
94703b705cfSriastradh	/* u0, v0, w0 */
94803b705cfSriastradh	DBG(("%s: id=%d, first channel %d floats, offset=4b\n", __FUNCTION__,
94903b705cfSriastradh	     id, id & 3));
95003b705cfSriastradh	dw = VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
95103b705cfSriastradh	switch (id & 3) {
95203b705cfSriastradh	default:
95303b705cfSriastradh		assert(0);
95403b705cfSriastradh	case 0:
95503b705cfSriastradh		format = GEN5_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT;
95603b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
95703b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
95803b705cfSriastradh		dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
95903b705cfSriastradh		break;
96003b705cfSriastradh	case 1:
96103b705cfSriastradh		format = GEN5_SURFACEFORMAT_R32_FLOAT << VE0_FORMAT_SHIFT;
96203b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
96303b705cfSriastradh		dw |= VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
96403b705cfSriastradh		dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
96503b705cfSriastradh		break;
96603b705cfSriastradh	case 2:
96703b705cfSriastradh		format = GEN5_SURFACEFORMAT_R32G32_FLOAT << VE0_FORMAT_SHIFT;
96803b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
96903b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
97003b705cfSriastradh		dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
97103b705cfSriastradh		break;
97203b705cfSriastradh	case 3:
97303b705cfSriastradh		format = GEN5_SURFACEFORMAT_R32G32B32_FLOAT << VE0_FORMAT_SHIFT;
97403b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
97503b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
97603b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
97703b705cfSriastradh		break;
97803b705cfSriastradh	}
97903b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
98003b705cfSriastradh		  format | 4 << VE0_OFFSET_SHIFT);
98103b705cfSriastradh	OUT_BATCH(dw);
98203b705cfSriastradh
98303b705cfSriastradh	/* u1, v1, w1 */
98403b705cfSriastradh	if (has_mask) {
98503b705cfSriastradh		unsigned offset = 4 + ((id & 3) ?: 1) * sizeof(float);
98603b705cfSriastradh		DBG(("%s: id=%x, second channel %d floats, offset=%db\n", __FUNCTION__,
98703b705cfSriastradh		     id, id >> 2, offset));
98803b705cfSriastradh		dw = VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
98903b705cfSriastradh		switch (id >> 2) {
99003b705cfSriastradh		case 1:
99103b705cfSriastradh			format = GEN5_SURFACEFORMAT_R32_FLOAT << VE0_FORMAT_SHIFT;
99203b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
99303b705cfSriastradh			dw |= VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
99403b705cfSriastradh			dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
99503b705cfSriastradh			break;
99603b705cfSriastradh		default:
99703b705cfSriastradh			assert(0);
99803b705cfSriastradh		case 2:
99903b705cfSriastradh			format = GEN5_SURFACEFORMAT_R32G32_FLOAT << VE0_FORMAT_SHIFT;
100003b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
100103b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
100203b705cfSriastradh			dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
100303b705cfSriastradh			break;
100403b705cfSriastradh		case 3:
100503b705cfSriastradh			format = GEN5_SURFACEFORMAT_R32G32B32_FLOAT << VE0_FORMAT_SHIFT;
100603b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
100703b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
100803b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
100903b705cfSriastradh			break;
101003b705cfSriastradh		}
101103b705cfSriastradh		OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
101203b705cfSriastradh			  format | offset << VE0_OFFSET_SHIFT);
101303b705cfSriastradh		OUT_BATCH(dw);
101403b705cfSriastradh	}
101503b705cfSriastradh}
101603b705cfSriastradh
101703b705cfSriastradhinline static void
101803b705cfSriastradhgen5_emit_pipe_flush(struct sna *sna)
101903b705cfSriastradh{
102042542f5fSchristos#if 1
102142542f5fSchristos	OUT_BATCH(GEN5_PIPE_CONTROL |
102242542f5fSchristos		  GEN5_PIPE_CONTROL_WC_FLUSH |
102342542f5fSchristos		  (4 - 2));
102403b705cfSriastradh	OUT_BATCH(0);
102503b705cfSriastradh	OUT_BATCH(0);
102642542f5fSchristos	OUT_BATCH(0);
102742542f5fSchristos#else
102842542f5fSchristos	OUT_BATCH(MI_FLUSH | MI_INHIBIT_RENDER_CACHE_FLUSH);
102942542f5fSchristos#endif
103003b705cfSriastradh}
103103b705cfSriastradh
103203b705cfSriastradhstatic void
103303b705cfSriastradhgen5_emit_state(struct sna *sna,
103403b705cfSriastradh		const struct sna_composite_op *op,
103503b705cfSriastradh		uint16_t offset)
103603b705cfSriastradh{
103703b705cfSriastradh	bool flush = false;
103803b705cfSriastradh
103903b705cfSriastradh	assert(op->dst.bo->exec);
104003b705cfSriastradh
104103b705cfSriastradh	/* drawrect must be first for Ironlake BLT workaround */
104203b705cfSriastradh	if (gen5_emit_drawing_rectangle(sna, op))
104303b705cfSriastradh		offset &= ~1;
104403b705cfSriastradh	gen5_emit_binding_table(sna, offset & ~1);
104503b705cfSriastradh	if (gen5_emit_pipelined_pointers(sna, op, op->op, op->u.gen5.wm_kernel)){
104603b705cfSriastradh		DBG(("%s: changed blend state, flush required? %d\n",
104703b705cfSriastradh		     __FUNCTION__, (offset & 1) && op->op > PictOpSrc));
104803b705cfSriastradh		flush = (offset & 1) && op->op > PictOpSrc;
104903b705cfSriastradh	}
105003b705cfSriastradh	gen5_emit_vertex_elements(sna, op);
105103b705cfSriastradh
105242542f5fSchristos	if (ALWAYS_FLUSH || kgem_bo_is_dirty(op->src.bo) || kgem_bo_is_dirty(op->mask.bo)) {
105303b705cfSriastradh		DBG(("%s: flushing dirty (%d, %d)\n", __FUNCTION__,
105403b705cfSriastradh		     kgem_bo_is_dirty(op->src.bo),
105503b705cfSriastradh		     kgem_bo_is_dirty(op->mask.bo)));
105603b705cfSriastradh		OUT_BATCH(MI_FLUSH);
105703b705cfSriastradh		kgem_clear_dirty(&sna->kgem);
105803b705cfSriastradh		kgem_bo_mark_dirty(op->dst.bo);
105903b705cfSriastradh		flush = false;
106003b705cfSriastradh	}
106103b705cfSriastradh	if (flush) {
106203b705cfSriastradh		DBG(("%s: forcing flush\n", __FUNCTION__));
106303b705cfSriastradh		gen5_emit_pipe_flush(sna);
106403b705cfSriastradh	}
106503b705cfSriastradh}
106603b705cfSriastradh
106703b705cfSriastradhstatic void gen5_bind_surfaces(struct sna *sna,
106803b705cfSriastradh			       const struct sna_composite_op *op)
106903b705cfSriastradh{
107003b705cfSriastradh	bool dirty = kgem_bo_is_dirty(op->dst.bo);
107103b705cfSriastradh	uint32_t *binding_table;
107203b705cfSriastradh	uint16_t offset;
107303b705cfSriastradh
107403b705cfSriastradh	gen5_get_batch(sna, op);
107503b705cfSriastradh
107603b705cfSriastradh	binding_table = gen5_composite_get_binding_table(sna, &offset);
107703b705cfSriastradh
107803b705cfSriastradh	binding_table[0] =
107903b705cfSriastradh		gen5_bind_bo(sna,
108003b705cfSriastradh			    op->dst.bo, op->dst.width, op->dst.height,
108103b705cfSriastradh			    gen5_get_dest_format(op->dst.format),
108203b705cfSriastradh			    true);
108303b705cfSriastradh	binding_table[1] =
108403b705cfSriastradh		gen5_bind_bo(sna,
108503b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
108603b705cfSriastradh			     op->src.card_format,
108703b705cfSriastradh			     false);
108803b705cfSriastradh	if (op->mask.bo) {
108903b705cfSriastradh		assert(op->u.gen5.ve_id >> 2);
109003b705cfSriastradh		binding_table[2] =
109103b705cfSriastradh			gen5_bind_bo(sna,
109203b705cfSriastradh				     op->mask.bo,
109303b705cfSriastradh				     op->mask.width,
109403b705cfSriastradh				     op->mask.height,
109503b705cfSriastradh				     op->mask.card_format,
109603b705cfSriastradh				     false);
109703b705cfSriastradh	}
109803b705cfSriastradh
109903b705cfSriastradh	if (sna->kgem.surface == offset &&
110003b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen5.surface_table) == *(uint64_t*)binding_table &&
110103b705cfSriastradh	    (op->mask.bo == NULL ||
110203b705cfSriastradh	     sna->kgem.batch[sna->render_state.gen5.surface_table+2] == binding_table[2])) {
110303b705cfSriastradh		sna->kgem.surface += sizeof(struct gen5_surface_state_padded) / sizeof(uint32_t);
110403b705cfSriastradh		offset = sna->render_state.gen5.surface_table;
110503b705cfSriastradh	}
110603b705cfSriastradh
110703b705cfSriastradh	gen5_emit_state(sna, op, offset | dirty);
110803b705cfSriastradh}
110903b705cfSriastradh
111003b705cfSriastradhfastcall static void
111103b705cfSriastradhgen5_render_composite_blt(struct sna *sna,
111203b705cfSriastradh			  const struct sna_composite_op *op,
111303b705cfSriastradh			  const struct sna_composite_rectangles *r)
111403b705cfSriastradh{
111503b705cfSriastradh	DBG(("%s: src=(%d, %d)+(%d, %d), mask=(%d, %d)+(%d, %d), dst=(%d, %d)+(%d, %d), size=(%d, %d)\n",
111603b705cfSriastradh	     __FUNCTION__,
111703b705cfSriastradh	     r->src.x, r->src.y, op->src.offset[0], op->src.offset[1],
111803b705cfSriastradh	     r->mask.x, r->mask.y, op->mask.offset[0], op->mask.offset[1],
111903b705cfSriastradh	     r->dst.x, r->dst.y, op->dst.x, op->dst.y,
112003b705cfSriastradh	     r->width, r->height));
112103b705cfSriastradh
112203b705cfSriastradh	gen5_get_rectangles(sna, op, 1, gen5_bind_surfaces);
112303b705cfSriastradh	op->prim_emit(sna, op, r);
112403b705cfSriastradh}
112503b705cfSriastradh
112603b705cfSriastradhfastcall static void
112703b705cfSriastradhgen5_render_composite_box(struct sna *sna,
112803b705cfSriastradh			  const struct sna_composite_op *op,
112903b705cfSriastradh			  const BoxRec *box)
113003b705cfSriastradh{
113103b705cfSriastradh	struct sna_composite_rectangles r;
113203b705cfSriastradh
113303b705cfSriastradh	DBG(("  %s: (%d, %d), (%d, %d)\n",
113403b705cfSriastradh	     __FUNCTION__,
113503b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
113603b705cfSriastradh
113703b705cfSriastradh	gen5_get_rectangles(sna, op, 1, gen5_bind_surfaces);
113803b705cfSriastradh
113903b705cfSriastradh	r.dst.x = box->x1;
114003b705cfSriastradh	r.dst.y = box->y1;
114103b705cfSriastradh	r.width  = box->x2 - box->x1;
114203b705cfSriastradh	r.height = box->y2 - box->y1;
114303b705cfSriastradh	r.mask = r.src = r.dst;
114403b705cfSriastradh
114503b705cfSriastradh	op->prim_emit(sna, op, &r);
114603b705cfSriastradh}
114703b705cfSriastradh
114803b705cfSriastradhstatic void
114903b705cfSriastradhgen5_render_composite_boxes__blt(struct sna *sna,
115003b705cfSriastradh				 const struct sna_composite_op *op,
115103b705cfSriastradh				 const BoxRec *box, int nbox)
115203b705cfSriastradh{
115303b705cfSriastradh	DBG(("%s(%d) delta=(%d, %d), src=(%d, %d)/(%d, %d), mask=(%d, %d)/(%d, %d)\n",
115403b705cfSriastradh	     __FUNCTION__, nbox, op->dst.x, op->dst.y,
115503b705cfSriastradh	     op->src.offset[0], op->src.offset[1],
115603b705cfSriastradh	     op->src.width, op->src.height,
115703b705cfSriastradh	     op->mask.offset[0], op->mask.offset[1],
115803b705cfSriastradh	     op->mask.width, op->mask.height));
115903b705cfSriastradh
116003b705cfSriastradh	do {
116103b705cfSriastradh		int nbox_this_time;
116203b705cfSriastradh
116303b705cfSriastradh		nbox_this_time = gen5_get_rectangles(sna, op, nbox,
116403b705cfSriastradh						     gen5_bind_surfaces);
116503b705cfSriastradh		nbox -= nbox_this_time;
116603b705cfSriastradh
116703b705cfSriastradh		do {
116803b705cfSriastradh			struct sna_composite_rectangles r;
116903b705cfSriastradh
117003b705cfSriastradh			DBG(("  %s: (%d, %d), (%d, %d)\n",
117103b705cfSriastradh			     __FUNCTION__,
117203b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
117303b705cfSriastradh
117403b705cfSriastradh			r.dst.x = box->x1;
117503b705cfSriastradh			r.dst.y = box->y1;
117603b705cfSriastradh			r.width  = box->x2 - box->x1;
117703b705cfSriastradh			r.height = box->y2 - box->y1;
117803b705cfSriastradh			r.mask = r.src = r.dst;
117903b705cfSriastradh			op->prim_emit(sna, op, &r);
118003b705cfSriastradh			box++;
118103b705cfSriastradh		} while (--nbox_this_time);
118203b705cfSriastradh	} while (nbox);
118303b705cfSriastradh}
118403b705cfSriastradh
118503b705cfSriastradhstatic void
118603b705cfSriastradhgen5_render_composite_boxes(struct sna *sna,
118703b705cfSriastradh			    const struct sna_composite_op *op,
118803b705cfSriastradh			    const BoxRec *box, int nbox)
118903b705cfSriastradh{
119003b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
119103b705cfSriastradh
119203b705cfSriastradh	do {
119303b705cfSriastradh		int nbox_this_time;
119403b705cfSriastradh		float *v;
119503b705cfSriastradh
119603b705cfSriastradh		nbox_this_time = gen5_get_rectangles(sna, op, nbox,
119703b705cfSriastradh						     gen5_bind_surfaces);
119803b705cfSriastradh		assert(nbox_this_time);
119903b705cfSriastradh		nbox -= nbox_this_time;
120003b705cfSriastradh
120103b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
120203b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
120303b705cfSriastradh
120403b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
120503b705cfSriastradh		box += nbox_this_time;
120603b705cfSriastradh	} while (nbox);
120703b705cfSriastradh}
120803b705cfSriastradh
120903b705cfSriastradhstatic void
121003b705cfSriastradhgen5_render_composite_boxes__thread(struct sna *sna,
121103b705cfSriastradh				    const struct sna_composite_op *op,
121203b705cfSriastradh				    const BoxRec *box, int nbox)
121303b705cfSriastradh{
121403b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
121503b705cfSriastradh
121603b705cfSriastradh	sna_vertex_lock(&sna->render);
121703b705cfSriastradh	do {
121803b705cfSriastradh		int nbox_this_time;
121903b705cfSriastradh		float *v;
122003b705cfSriastradh
122103b705cfSriastradh		nbox_this_time = gen5_get_rectangles(sna, op, nbox,
122203b705cfSriastradh						     gen5_bind_surfaces);
122303b705cfSriastradh		assert(nbox_this_time);
122403b705cfSriastradh		nbox -= nbox_this_time;
122503b705cfSriastradh
122603b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
122703b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
122803b705cfSriastradh
122903b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
123003b705cfSriastradh		sna_vertex_unlock(&sna->render);
123103b705cfSriastradh
123203b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
123303b705cfSriastradh		box += nbox_this_time;
123403b705cfSriastradh
123503b705cfSriastradh		sna_vertex_lock(&sna->render);
123603b705cfSriastradh		sna_vertex_release__locked(&sna->render);
123703b705cfSriastradh	} while (nbox);
123803b705cfSriastradh	sna_vertex_unlock(&sna->render);
123903b705cfSriastradh}
124003b705cfSriastradh
124103b705cfSriastradh#ifndef MAX
124203b705cfSriastradh#define MAX(a,b) ((a) > (b) ? (a) : (b))
124303b705cfSriastradh#endif
124403b705cfSriastradh
124503b705cfSriastradhstatic uint32_t gen5_bind_video_source(struct sna *sna,
124603b705cfSriastradh				       struct kgem_bo *src_bo,
124703b705cfSriastradh				       uint32_t src_offset,
124803b705cfSriastradh				       int src_width,
124903b705cfSriastradh				       int src_height,
125003b705cfSriastradh				       int src_pitch,
125103b705cfSriastradh				       uint32_t src_surf_format)
125203b705cfSriastradh{
125303b705cfSriastradh	struct gen5_surface_state *ss;
125403b705cfSriastradh
125503b705cfSriastradh	sna->kgem.surface -= sizeof(struct gen5_surface_state_padded) / sizeof(uint32_t);
125603b705cfSriastradh
125703b705cfSriastradh	ss = memset(sna->kgem.batch + sna->kgem.surface, 0, sizeof(*ss));
125803b705cfSriastradh	ss->ss0.surface_type = GEN5_SURFACE_2D;
125903b705cfSriastradh	ss->ss0.surface_format = src_surf_format;
126003b705cfSriastradh	ss->ss0.color_blend = 1;
126103b705cfSriastradh
126203b705cfSriastradh	ss->ss1.base_addr =
126303b705cfSriastradh		kgem_add_reloc(&sna->kgem,
126403b705cfSriastradh			       sna->kgem.surface + 1,
126503b705cfSriastradh			       src_bo,
126603b705cfSriastradh			       I915_GEM_DOMAIN_SAMPLER << 16,
126703b705cfSriastradh			       src_offset);
126803b705cfSriastradh
126903b705cfSriastradh	ss->ss2.width  = src_width - 1;
127003b705cfSriastradh	ss->ss2.height = src_height - 1;
127103b705cfSriastradh	ss->ss3.pitch  = src_pitch - 1;
127203b705cfSriastradh
127303b705cfSriastradh	return sna->kgem.surface * sizeof(uint32_t);
127403b705cfSriastradh}
127503b705cfSriastradh
127603b705cfSriastradhstatic void gen5_video_bind_surfaces(struct sna *sna,
127703b705cfSriastradh				     const struct sna_composite_op *op)
127803b705cfSriastradh{
127903b705cfSriastradh	bool dirty = kgem_bo_is_dirty(op->dst.bo);
128003b705cfSriastradh	struct sna_video_frame *frame = op->priv;
128103b705cfSriastradh	uint32_t src_surf_format;
128203b705cfSriastradh	uint32_t src_surf_base[6];
128303b705cfSriastradh	int src_width[6];
128403b705cfSriastradh	int src_height[6];
128503b705cfSriastradh	int src_pitch[6];
128603b705cfSriastradh	uint32_t *binding_table;
128703b705cfSriastradh	uint16_t offset;
128803b705cfSriastradh	int n_src, n;
128903b705cfSriastradh
129003b705cfSriastradh	src_surf_base[0] = 0;
129103b705cfSriastradh	src_surf_base[1] = 0;
129203b705cfSriastradh	src_surf_base[2] = frame->VBufOffset;
129303b705cfSriastradh	src_surf_base[3] = frame->VBufOffset;
129403b705cfSriastradh	src_surf_base[4] = frame->UBufOffset;
129503b705cfSriastradh	src_surf_base[5] = frame->UBufOffset;
129603b705cfSriastradh
129703b705cfSriastradh	if (is_planar_fourcc(frame->id)) {
129803b705cfSriastradh		src_surf_format = GEN5_SURFACEFORMAT_R8_UNORM;
129903b705cfSriastradh		src_width[1]  = src_width[0]  = frame->width;
130003b705cfSriastradh		src_height[1] = src_height[0] = frame->height;
130103b705cfSriastradh		src_pitch[1]  = src_pitch[0]  = frame->pitch[1];
130203b705cfSriastradh		src_width[4]  = src_width[5]  = src_width[2]  = src_width[3] =
130303b705cfSriastradh			frame->width / 2;
130403b705cfSriastradh		src_height[4] = src_height[5] = src_height[2] = src_height[3] =
130503b705cfSriastradh			frame->height / 2;
130603b705cfSriastradh		src_pitch[4]  = src_pitch[5]  = src_pitch[2]  = src_pitch[3] =
130703b705cfSriastradh			frame->pitch[0];
130803b705cfSriastradh		n_src = 6;
130903b705cfSriastradh	} else {
131003b705cfSriastradh		if (frame->id == FOURCC_UYVY)
131103b705cfSriastradh			src_surf_format = GEN5_SURFACEFORMAT_YCRCB_SWAPY;
131203b705cfSriastradh		else
131303b705cfSriastradh			src_surf_format = GEN5_SURFACEFORMAT_YCRCB_NORMAL;
131403b705cfSriastradh
131503b705cfSriastradh		src_width[0]  = frame->width;
131603b705cfSriastradh		src_height[0] = frame->height;
131703b705cfSriastradh		src_pitch[0]  = frame->pitch[0];
131803b705cfSriastradh		n_src = 1;
131903b705cfSriastradh	}
132003b705cfSriastradh
132103b705cfSriastradh	gen5_get_batch(sna, op);
132203b705cfSriastradh
132303b705cfSriastradh	binding_table = gen5_composite_get_binding_table(sna, &offset);
132403b705cfSriastradh	binding_table[0] =
132503b705cfSriastradh		gen5_bind_bo(sna,
132603b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
132703b705cfSriastradh			     gen5_get_dest_format(op->dst.format),
132803b705cfSriastradh			     true);
132903b705cfSriastradh	for (n = 0; n < n_src; n++) {
133003b705cfSriastradh		binding_table[1+n] =
133103b705cfSriastradh			gen5_bind_video_source(sna,
133203b705cfSriastradh					       frame->bo,
133303b705cfSriastradh					       src_surf_base[n],
133403b705cfSriastradh					       src_width[n],
133503b705cfSriastradh					       src_height[n],
133603b705cfSriastradh					       src_pitch[n],
133703b705cfSriastradh					       src_surf_format);
133803b705cfSriastradh	}
133903b705cfSriastradh
134003b705cfSriastradh	gen5_emit_state(sna, op, offset | dirty);
134103b705cfSriastradh}
134203b705cfSriastradh
134303b705cfSriastradhstatic bool
134403b705cfSriastradhgen5_render_video(struct sna *sna,
134503b705cfSriastradh		  struct sna_video *video,
134603b705cfSriastradh		  struct sna_video_frame *frame,
134703b705cfSriastradh		  RegionPtr dstRegion,
134803b705cfSriastradh		  PixmapPtr pixmap)
134903b705cfSriastradh{
135003b705cfSriastradh	struct sna_composite_op tmp;
135142542f5fSchristos	struct sna_pixmap *priv = sna_pixmap(pixmap);
135203b705cfSriastradh	int dst_width = dstRegion->extents.x2 - dstRegion->extents.x1;
135303b705cfSriastradh	int dst_height = dstRegion->extents.y2 - dstRegion->extents.y1;
135403b705cfSriastradh	int src_width = frame->src.x2 - frame->src.x1;
135503b705cfSriastradh	int src_height = frame->src.y2 - frame->src.y1;
135603b705cfSriastradh	float src_offset_x, src_offset_y;
135703b705cfSriastradh	float src_scale_x, src_scale_y;
135803b705cfSriastradh	int nbox, pix_xoff, pix_yoff;
135942542f5fSchristos	const BoxRec *box;
136003b705cfSriastradh
136103b705cfSriastradh	DBG(("%s: %dx%d -> %dx%d\n", __FUNCTION__,
136203b705cfSriastradh	     src_width, src_height, dst_width, dst_height));
136303b705cfSriastradh
136442542f5fSchristos	assert(priv->gpu_bo);
136503b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
136603b705cfSriastradh
136703b705cfSriastradh	tmp.op = PictOpSrc;
136803b705cfSriastradh	tmp.dst.pixmap = pixmap;
136903b705cfSriastradh	tmp.dst.width  = pixmap->drawable.width;
137003b705cfSriastradh	tmp.dst.height = pixmap->drawable.height;
137103b705cfSriastradh	tmp.dst.format = sna_format_for_depth(pixmap->drawable.depth);
137203b705cfSriastradh	tmp.dst.bo = priv->gpu_bo;
137303b705cfSriastradh
137403b705cfSriastradh	if (src_width == dst_width && src_height == dst_height)
137503b705cfSriastradh		tmp.src.filter = SAMPLER_FILTER_NEAREST;
137603b705cfSriastradh	else
137703b705cfSriastradh		tmp.src.filter = SAMPLER_FILTER_BILINEAR;
137803b705cfSriastradh	tmp.src.repeat = SAMPLER_EXTEND_PAD;
137903b705cfSriastradh	tmp.src.bo = frame->bo;
138003b705cfSriastradh	tmp.mask.bo = NULL;
138103b705cfSriastradh	tmp.u.gen5.wm_kernel =
138203b705cfSriastradh		is_planar_fourcc(frame->id) ? WM_KERNEL_VIDEO_PLANAR : WM_KERNEL_VIDEO_PACKED;
138303b705cfSriastradh	tmp.u.gen5.ve_id = 2;
138403b705cfSriastradh	tmp.is_affine = true;
138503b705cfSriastradh	tmp.floats_per_vertex = 3;
138603b705cfSriastradh	tmp.floats_per_rect = 9;
138703b705cfSriastradh	tmp.priv = frame;
138803b705cfSriastradh
138903b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL)) {
139003b705cfSriastradh		kgem_submit(&sna->kgem);
139142542f5fSchristos		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL))
139242542f5fSchristos			return false;
139303b705cfSriastradh	}
139403b705cfSriastradh
139503b705cfSriastradh	gen5_align_vertex(sna, &tmp);
139642542f5fSchristos	gen5_video_bind_surfaces(sna, &tmp);
139703b705cfSriastradh
139803b705cfSriastradh	/* Set up the offset for translating from the given region (in screen
139903b705cfSriastradh	 * coordinates) to the backing pixmap.
140003b705cfSriastradh	 */
140103b705cfSriastradh#ifdef COMPOSITE
140203b705cfSriastradh	pix_xoff = -pixmap->screen_x + pixmap->drawable.x;
140303b705cfSriastradh	pix_yoff = -pixmap->screen_y + pixmap->drawable.y;
140403b705cfSriastradh#else
140503b705cfSriastradh	pix_xoff = 0;
140603b705cfSriastradh	pix_yoff = 0;
140703b705cfSriastradh#endif
140803b705cfSriastradh
140903b705cfSriastradh	src_scale_x = (float)src_width / dst_width / frame->width;
141003b705cfSriastradh	src_offset_x = (float)frame->src.x1 / frame->width - dstRegion->extents.x1 * src_scale_x;
141103b705cfSriastradh
141203b705cfSriastradh	src_scale_y = (float)src_height / dst_height / frame->height;
141303b705cfSriastradh	src_offset_y = (float)frame->src.y1 / frame->height - dstRegion->extents.y1 * src_scale_y;
141403b705cfSriastradh
141542542f5fSchristos	box = region_rects(dstRegion);
141642542f5fSchristos	nbox = region_num_rects(dstRegion);
141703b705cfSriastradh	while (nbox--) {
141803b705cfSriastradh		BoxRec r;
141903b705cfSriastradh
142003b705cfSriastradh		r.x1 = box->x1 + pix_xoff;
142103b705cfSriastradh		r.x2 = box->x2 + pix_xoff;
142203b705cfSriastradh		r.y1 = box->y1 + pix_yoff;
142303b705cfSriastradh		r.y2 = box->y2 + pix_yoff;
142403b705cfSriastradh
142503b705cfSriastradh		gen5_get_rectangles(sna, &tmp, 1, gen5_video_bind_surfaces);
142603b705cfSriastradh
142703b705cfSriastradh		OUT_VERTEX(r.x2, r.y2);
142803b705cfSriastradh		OUT_VERTEX_F(box->x2 * src_scale_x + src_offset_x);
142903b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
143003b705cfSriastradh
143103b705cfSriastradh		OUT_VERTEX(r.x1, r.y2);
143203b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
143303b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
143403b705cfSriastradh
143503b705cfSriastradh		OUT_VERTEX(r.x1, r.y1);
143603b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
143703b705cfSriastradh		OUT_VERTEX_F(box->y1 * src_scale_y + src_offset_y);
143803b705cfSriastradh
143903b705cfSriastradh		if (!DAMAGE_IS_ALL(priv->gpu_damage)) {
144003b705cfSriastradh			sna_damage_add_box(&priv->gpu_damage, &r);
144103b705cfSriastradh			sna_damage_subtract_box(&priv->cpu_damage, &r);
144203b705cfSriastradh		}
144303b705cfSriastradh		box++;
144403b705cfSriastradh	}
144503b705cfSriastradh
144603b705cfSriastradh	gen4_vertex_flush(sna);
144703b705cfSriastradh	return true;
144803b705cfSriastradh}
144903b705cfSriastradh
145003b705cfSriastradhstatic int
145103b705cfSriastradhgen5_composite_picture(struct sna *sna,
145203b705cfSriastradh		       PicturePtr picture,
145303b705cfSriastradh		       struct sna_composite_channel *channel,
145403b705cfSriastradh		       int x, int y,
145503b705cfSriastradh		       int w, int h,
145603b705cfSriastradh		       int dst_x, int dst_y,
145703b705cfSriastradh		       bool precise)
145803b705cfSriastradh{
145903b705cfSriastradh	PixmapPtr pixmap;
146003b705cfSriastradh	uint32_t color;
146103b705cfSriastradh	int16_t dx, dy;
146203b705cfSriastradh
146303b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d), dst=(%d, %d)\n",
146403b705cfSriastradh	     __FUNCTION__, x, y, w, h, dst_x, dst_y));
146503b705cfSriastradh
146603b705cfSriastradh	channel->is_solid = false;
146703b705cfSriastradh	channel->card_format = -1;
146803b705cfSriastradh
146903b705cfSriastradh	if (sna_picture_is_solid(picture, &color))
147003b705cfSriastradh		return gen4_channel_init_solid(sna, channel, color);
147103b705cfSriastradh
147203b705cfSriastradh	if (picture->pDrawable == NULL) {
147303b705cfSriastradh		int ret;
147403b705cfSriastradh
147503b705cfSriastradh		if (picture->pSourcePict->type == SourcePictTypeLinear)
147603b705cfSriastradh			return gen4_channel_init_linear(sna, picture, channel,
147703b705cfSriastradh							x, y,
147803b705cfSriastradh							w, h,
147903b705cfSriastradh							dst_x, dst_y);
148003b705cfSriastradh
148103b705cfSriastradh		DBG(("%s -- fixup, gradient\n", __FUNCTION__));
148203b705cfSriastradh		ret = -1;
148303b705cfSriastradh		if (!precise)
148403b705cfSriastradh			ret = sna_render_picture_approximate_gradient(sna, picture, channel,
148503b705cfSriastradh								      x, y, w, h, dst_x, dst_y);
148603b705cfSriastradh		if (ret == -1)
148703b705cfSriastradh			ret = sna_render_picture_fixup(sna, picture, channel,
148803b705cfSriastradh						       x, y, w, h, dst_x, dst_y);
148903b705cfSriastradh		return ret;
149003b705cfSriastradh	}
149103b705cfSriastradh
149203b705cfSriastradh	if (picture->alphaMap) {
149303b705cfSriastradh		DBG(("%s -- fallback, alphamap\n", __FUNCTION__));
149403b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
149503b705cfSriastradh						x, y, w, h, dst_x, dst_y);
149603b705cfSriastradh	}
149703b705cfSriastradh
149803b705cfSriastradh	if (!gen5_check_repeat(picture))
149903b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
150003b705cfSriastradh						x, y, w, h, dst_x, dst_y);
150103b705cfSriastradh
150203b705cfSriastradh	if (!gen5_check_filter(picture))
150303b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
150403b705cfSriastradh						x, y, w, h, dst_x, dst_y);
150503b705cfSriastradh
150603b705cfSriastradh	channel->repeat = picture->repeat ? picture->repeatType : RepeatNone;
150703b705cfSriastradh	channel->filter = picture->filter;
150803b705cfSriastradh
150903b705cfSriastradh	pixmap = get_drawable_pixmap(picture->pDrawable);
151003b705cfSriastradh	get_drawable_deltas(picture->pDrawable, pixmap, &dx, &dy);
151103b705cfSriastradh
151203b705cfSriastradh	x += dx + picture->pDrawable->x;
151303b705cfSriastradh	y += dy + picture->pDrawable->y;
151403b705cfSriastradh
151503b705cfSriastradh	channel->is_affine = sna_transform_is_affine(picture->transform);
151642542f5fSchristos	if (sna_transform_is_imprecise_integer_translation(picture->transform, picture->filter, precise, &dx, &dy)) {
151703b705cfSriastradh		DBG(("%s: integer translation (%d, %d), removing\n",
151803b705cfSriastradh		     __FUNCTION__, dx, dy));
151903b705cfSriastradh		x += dx;
152003b705cfSriastradh		y += dy;
152103b705cfSriastradh		channel->transform = NULL;
152203b705cfSriastradh		channel->filter = PictFilterNearest;
152342542f5fSchristos
152442542f5fSchristos		if (channel->repeat ||
152542542f5fSchristos		    (x >= 0 &&
152642542f5fSchristos		     y >= 0 &&
152742542f5fSchristos		     x + w < pixmap->drawable.width &&
152842542f5fSchristos		     y + h < pixmap->drawable.height)) {
152942542f5fSchristos			struct sna_pixmap *priv = sna_pixmap(pixmap);
153042542f5fSchristos			if (priv && priv->clear) {
153142542f5fSchristos				DBG(("%s: converting large pixmap source into solid [%08x]\n", __FUNCTION__, priv->clear_color));
153242542f5fSchristos				return gen4_channel_init_solid(sna, channel, priv->clear_color);
153342542f5fSchristos			}
153442542f5fSchristos		}
153503b705cfSriastradh	} else
153603b705cfSriastradh		channel->transform = picture->transform;
153703b705cfSriastradh
153803b705cfSriastradh	channel->pict_format = picture->format;
153903b705cfSriastradh	channel->card_format = gen5_get_card_format(picture->format);
154003b705cfSriastradh	if (channel->card_format == -1)
154103b705cfSriastradh		return sna_render_picture_convert(sna, picture, channel, pixmap,
154203b705cfSriastradh						  x, y, w, h, dst_x, dst_y,
154303b705cfSriastradh						  false);
154403b705cfSriastradh
154503b705cfSriastradh	if (too_large(pixmap->drawable.width, pixmap->drawable.height))
154603b705cfSriastradh		return sna_render_picture_extract(sna, picture, channel,
154703b705cfSriastradh						  x, y, w, h, dst_x, dst_y);
154803b705cfSriastradh
154942542f5fSchristos	DBG(("%s: pixmap, repeat=%d, filter=%d, transform?=%d [affine? %d], format=%08x\n",
155042542f5fSchristos	     __FUNCTION__,
155142542f5fSchristos	     channel->repeat, channel->filter,
155242542f5fSchristos	     channel->transform != NULL, channel->is_affine,
155342542f5fSchristos	     channel->pict_format));
155442542f5fSchristos	if (channel->transform) {
155542542f5fSchristos		DBG(("%s: transform=[%f %f %f, %f %f %f, %f %f %f]\n",
155642542f5fSchristos		     __FUNCTION__,
155742542f5fSchristos		     channel->transform->matrix[0][0] / 65536.,
155842542f5fSchristos		     channel->transform->matrix[0][1] / 65536.,
155942542f5fSchristos		     channel->transform->matrix[0][2] / 65536.,
156042542f5fSchristos		     channel->transform->matrix[1][0] / 65536.,
156142542f5fSchristos		     channel->transform->matrix[1][1] / 65536.,
156242542f5fSchristos		     channel->transform->matrix[1][2] / 65536.,
156342542f5fSchristos		     channel->transform->matrix[2][0] / 65536.,
156442542f5fSchristos		     channel->transform->matrix[2][1] / 65536.,
156542542f5fSchristos		     channel->transform->matrix[2][2] / 65536.));
156642542f5fSchristos	}
156742542f5fSchristos
156803b705cfSriastradh	return sna_render_pixmap_bo(sna, channel, pixmap,
156903b705cfSriastradh				    x, y, w, h, dst_x, dst_y);
157003b705cfSriastradh}
157103b705cfSriastradh
157203b705cfSriastradhstatic void gen5_composite_channel_convert(struct sna_composite_channel *channel)
157303b705cfSriastradh{
157403b705cfSriastradh	channel->repeat = gen5_repeat(channel->repeat);
157503b705cfSriastradh	channel->filter = gen5_filter(channel->filter);
157603b705cfSriastradh	if (channel->card_format == (unsigned)-1)
157703b705cfSriastradh		channel->card_format = gen5_get_card_format(channel->pict_format);
157803b705cfSriastradh}
157903b705cfSriastradh
158003b705cfSriastradhstatic void
158103b705cfSriastradhgen5_render_composite_done(struct sna *sna,
158203b705cfSriastradh			   const struct sna_composite_op *op)
158303b705cfSriastradh{
158403b705cfSriastradh	if (sna->render.vertex_offset) {
158503b705cfSriastradh		gen4_vertex_flush(sna);
158603b705cfSriastradh		gen5_magic_ca_pass(sna,op);
158703b705cfSriastradh	}
158803b705cfSriastradh
158903b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
159003b705cfSriastradh
159103b705cfSriastradh	if (op->mask.bo)
159203b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->mask.bo);
159303b705cfSriastradh	if (op->src.bo)
159403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->src.bo);
159503b705cfSriastradh
159603b705cfSriastradh	sna_render_composite_redirect_done(sna, op);
159703b705cfSriastradh}
159803b705cfSriastradh
159903b705cfSriastradhstatic bool
160003b705cfSriastradhgen5_composite_set_target(struct sna *sna,
160103b705cfSriastradh			  struct sna_composite_op *op,
160203b705cfSriastradh			  PicturePtr dst,
160303b705cfSriastradh			  int x, int y, int w, int h,
160403b705cfSriastradh			  bool partial)
160503b705cfSriastradh{
160603b705cfSriastradh	BoxRec box;
160742542f5fSchristos	unsigned hint;
160803b705cfSriastradh
160903b705cfSriastradh	op->dst.pixmap = get_drawable_pixmap(dst->pDrawable);
161003b705cfSriastradh	op->dst.width  = op->dst.pixmap->drawable.width;
161103b705cfSriastradh	op->dst.height = op->dst.pixmap->drawable.height;
161203b705cfSriastradh	op->dst.format = dst->format;
161303b705cfSriastradh	if (w && h) {
161403b705cfSriastradh		box.x1 = x;
161503b705cfSriastradh		box.y1 = y;
161603b705cfSriastradh		box.x2 = x + w;
161703b705cfSriastradh		box.y2 = y + h;
161803b705cfSriastradh	} else
161903b705cfSriastradh		sna_render_picture_extents(dst, &box);
162003b705cfSriastradh
162142542f5fSchristos	hint = PREFER_GPU | FORCE_GPU | RENDER_GPU;
162242542f5fSchristos	if (!partial) {
162342542f5fSchristos		hint |= IGNORE_DAMAGE;
162442542f5fSchristos		if (w == op->dst.width && h == op->dst.height)
162542542f5fSchristos			hint |= REPLACES;
162642542f5fSchristos	}
162742542f5fSchristos
162842542f5fSchristos	op->dst.bo = sna_drawable_use_bo(dst->pDrawable, hint, &box, &op->damage);
162903b705cfSriastradh	if (op->dst.bo == NULL)
163003b705cfSriastradh		return false;
163103b705cfSriastradh
163242542f5fSchristos	if (hint & REPLACES) {
163342542f5fSchristos		struct sna_pixmap *priv = sna_pixmap(op->dst.pixmap);
163442542f5fSchristos		kgem_bo_pair_undo(&sna->kgem, priv->gpu_bo, priv->cpu_bo);
163542542f5fSchristos	}
163642542f5fSchristos
163703b705cfSriastradh	get_drawable_deltas(dst->pDrawable, op->dst.pixmap,
163803b705cfSriastradh			    &op->dst.x, &op->dst.y);
163903b705cfSriastradh
164042542f5fSchristos	DBG(("%s: pixmap=%ld, format=%08x, size=%dx%d, pitch=%d, delta=(%d,%d),damage=%p\n",
164103b705cfSriastradh	     __FUNCTION__,
164242542f5fSchristos	     op->dst.pixmap->drawable.serialNumber, (int)op->dst.format,
164303b705cfSriastradh	     op->dst.width, op->dst.height,
164403b705cfSriastradh	     op->dst.bo->pitch,
164503b705cfSriastradh	     op->dst.x, op->dst.y,
164603b705cfSriastradh	     op->damage ? *op->damage : (void *)-1));
164703b705cfSriastradh
164803b705cfSriastradh	assert(op->dst.bo->proxy == NULL);
164903b705cfSriastradh
165003b705cfSriastradh	if (too_large(op->dst.width, op->dst.height) &&
165103b705cfSriastradh	    !sna_render_composite_redirect(sna, op, x, y, w, h, partial))
165203b705cfSriastradh		return false;
165303b705cfSriastradh
165403b705cfSriastradh	return true;
165503b705cfSriastradh}
165603b705cfSriastradh
165703b705cfSriastradhstatic bool
165803b705cfSriastradhis_gradient(PicturePtr picture, bool precise)
165903b705cfSriastradh{
166003b705cfSriastradh	if (picture->pDrawable)
166103b705cfSriastradh		return false;
166203b705cfSriastradh
166303b705cfSriastradh	switch (picture->pSourcePict->type) {
166403b705cfSriastradh	case SourcePictTypeSolidFill:
166503b705cfSriastradh	case SourcePictTypeLinear:
166603b705cfSriastradh		return false;
166703b705cfSriastradh	default:
166803b705cfSriastradh		return precise;
166903b705cfSriastradh	}
167003b705cfSriastradh}
167103b705cfSriastradh
167203b705cfSriastradhstatic bool
167303b705cfSriastradhhas_alphamap(PicturePtr p)
167403b705cfSriastradh{
167503b705cfSriastradh	return p->alphaMap != NULL;
167603b705cfSriastradh}
167703b705cfSriastradh
167803b705cfSriastradhstatic bool
167903b705cfSriastradhneed_upload(struct sna *sna, PicturePtr p)
168003b705cfSriastradh{
168103b705cfSriastradh	return p->pDrawable && untransformed(p) &&
168203b705cfSriastradh		!is_gpu(sna, p->pDrawable, PREFER_GPU_RENDER);
168303b705cfSriastradh}
168403b705cfSriastradh
168503b705cfSriastradhstatic bool
168603b705cfSriastradhsource_is_busy(PixmapPtr pixmap)
168703b705cfSriastradh{
168803b705cfSriastradh	struct sna_pixmap *priv = sna_pixmap(pixmap);
168903b705cfSriastradh	if (priv == NULL)
169003b705cfSriastradh		return false;
169103b705cfSriastradh
169203b705cfSriastradh	if (priv->clear)
169303b705cfSriastradh		return false;
169403b705cfSriastradh
169503b705cfSriastradh	if (priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo))
169603b705cfSriastradh		return true;
169703b705cfSriastradh
169803b705cfSriastradh	if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
169903b705cfSriastradh		return true;
170003b705cfSriastradh
170103b705cfSriastradh	return priv->gpu_damage && !priv->cpu_damage;
170203b705cfSriastradh}
170303b705cfSriastradh
170403b705cfSriastradhstatic bool
170503b705cfSriastradhsource_fallback(struct sna *sna, PicturePtr p, PixmapPtr pixmap, bool precise)
170603b705cfSriastradh{
170703b705cfSriastradh	if (sna_picture_is_solid(p, NULL))
170803b705cfSriastradh		return false;
170903b705cfSriastradh
171003b705cfSriastradh	if (is_gradient(p, precise) ||
171103b705cfSriastradh	    !gen5_check_repeat(p) ||
171203b705cfSriastradh	    !gen5_check_format(p->format))
171303b705cfSriastradh		return true;
171403b705cfSriastradh
171503b705cfSriastradh	if (pixmap && source_is_busy(pixmap))
171603b705cfSriastradh		return false;
171703b705cfSriastradh
171803b705cfSriastradh	return has_alphamap(p) || !gen5_check_filter(p) || need_upload(sna, p);
171903b705cfSriastradh}
172003b705cfSriastradh
172103b705cfSriastradhstatic bool
172203b705cfSriastradhgen5_composite_fallback(struct sna *sna,
172303b705cfSriastradh			PicturePtr src,
172403b705cfSriastradh			PicturePtr mask,
172503b705cfSriastradh			PicturePtr dst)
172603b705cfSriastradh{
172703b705cfSriastradh	PixmapPtr src_pixmap;
172803b705cfSriastradh	PixmapPtr mask_pixmap;
172903b705cfSriastradh	PixmapPtr dst_pixmap;
173003b705cfSriastradh	bool src_fallback, mask_fallback;
173103b705cfSriastradh
173203b705cfSriastradh	if (!gen5_check_dst_format(dst->format)) {
173303b705cfSriastradh		DBG(("%s: unknown destination format: %d\n",
173403b705cfSriastradh		     __FUNCTION__, dst->format));
173503b705cfSriastradh		return true;
173603b705cfSriastradh	}
173703b705cfSriastradh
173803b705cfSriastradh	dst_pixmap = get_drawable_pixmap(dst->pDrawable);
173903b705cfSriastradh
174003b705cfSriastradh	src_pixmap = src->pDrawable ? get_drawable_pixmap(src->pDrawable) : NULL;
174103b705cfSriastradh	src_fallback = source_fallback(sna, src, src_pixmap,
174203b705cfSriastradh				       dst->polyMode == PolyModePrecise);
174303b705cfSriastradh
174403b705cfSriastradh	if (mask) {
174503b705cfSriastradh		mask_pixmap = mask->pDrawable ? get_drawable_pixmap(mask->pDrawable) : NULL;
174603b705cfSriastradh		mask_fallback = source_fallback(sna, mask, mask_pixmap,
174703b705cfSriastradh						dst->polyMode == PolyModePrecise);
174803b705cfSriastradh	} else {
174903b705cfSriastradh		mask_pixmap = NULL;
175003b705cfSriastradh		mask_fallback = false;
175103b705cfSriastradh	}
175203b705cfSriastradh
175303b705cfSriastradh	/* If we are using the destination as a source and need to
175403b705cfSriastradh	 * readback in order to upload the source, do it all
175503b705cfSriastradh	 * on the cpu.
175603b705cfSriastradh	 */
175703b705cfSriastradh	if (src_pixmap == dst_pixmap && src_fallback) {
175803b705cfSriastradh		DBG(("%s: src is dst and will fallback\n",__FUNCTION__));
175903b705cfSriastradh		return true;
176003b705cfSriastradh	}
176103b705cfSriastradh	if (mask_pixmap == dst_pixmap && mask_fallback) {
176203b705cfSriastradh		DBG(("%s: mask is dst and will fallback\n",__FUNCTION__));
176303b705cfSriastradh		return true;
176403b705cfSriastradh	}
176503b705cfSriastradh
176603b705cfSriastradh	/* If anything is on the GPU, push everything out to the GPU */
176703b705cfSriastradh	if (dst_use_gpu(dst_pixmap)) {
176803b705cfSriastradh		DBG(("%s: dst is already on the GPU, try to use GPU\n",
176903b705cfSriastradh		     __FUNCTION__));
177003b705cfSriastradh		return false;
177103b705cfSriastradh	}
177203b705cfSriastradh
177303b705cfSriastradh	if (src_pixmap && !src_fallback) {
177403b705cfSriastradh		DBG(("%s: src is already on the GPU, try to use GPU\n",
177503b705cfSriastradh		     __FUNCTION__));
177603b705cfSriastradh		return false;
177703b705cfSriastradh	}
177803b705cfSriastradh	if (mask_pixmap && !mask_fallback) {
177903b705cfSriastradh		DBG(("%s: mask is already on the GPU, try to use GPU\n",
178003b705cfSriastradh		     __FUNCTION__));
178103b705cfSriastradh		return false;
178203b705cfSriastradh	}
178303b705cfSriastradh
178403b705cfSriastradh	/* However if the dst is not on the GPU and we need to
178503b705cfSriastradh	 * render one of the sources using the CPU, we may
178603b705cfSriastradh	 * as well do the entire operation in place onthe CPU.
178703b705cfSriastradh	 */
178803b705cfSriastradh	if (src_fallback) {
178903b705cfSriastradh		DBG(("%s: dst is on the CPU and src will fallback\n",
179003b705cfSriastradh		     __FUNCTION__));
179103b705cfSriastradh		return true;
179203b705cfSriastradh	}
179303b705cfSriastradh
179403b705cfSriastradh	if (mask_fallback) {
179503b705cfSriastradh		DBG(("%s: dst is on the CPU and mask will fallback\n",
179603b705cfSriastradh		     __FUNCTION__));
179703b705cfSriastradh		return true;
179803b705cfSriastradh	}
179903b705cfSriastradh
180003b705cfSriastradh	if (too_large(dst_pixmap->drawable.width,
180103b705cfSriastradh		      dst_pixmap->drawable.height) &&
180203b705cfSriastradh	    dst_is_cpu(dst_pixmap)) {
180303b705cfSriastradh		DBG(("%s: dst is on the CPU and too large\n", __FUNCTION__));
180403b705cfSriastradh		return true;
180503b705cfSriastradh	}
180603b705cfSriastradh
180703b705cfSriastradh	DBG(("%s: dst is not on the GPU and the operation should not fallback\n",
180803b705cfSriastradh	     __FUNCTION__));
180903b705cfSriastradh	return dst_use_cpu(dst_pixmap);
181003b705cfSriastradh}
181103b705cfSriastradh
181203b705cfSriastradhstatic int
181303b705cfSriastradhreuse_source(struct sna *sna,
181403b705cfSriastradh	     PicturePtr src, struct sna_composite_channel *sc, int src_x, int src_y,
181503b705cfSriastradh	     PicturePtr mask, struct sna_composite_channel *mc, int msk_x, int msk_y)
181603b705cfSriastradh{
181703b705cfSriastradh	uint32_t color;
181803b705cfSriastradh
181903b705cfSriastradh	if (src_x != msk_x || src_y != msk_y)
182003b705cfSriastradh		return false;
182103b705cfSriastradh
182203b705cfSriastradh	if (src == mask) {
182303b705cfSriastradh		DBG(("%s: mask is source\n", __FUNCTION__));
182403b705cfSriastradh		*mc = *sc;
182503b705cfSriastradh		mc->bo = kgem_bo_reference(mc->bo);
182603b705cfSriastradh		return true;
182703b705cfSriastradh	}
182803b705cfSriastradh
182903b705cfSriastradh	if (sna_picture_is_solid(mask, &color))
183003b705cfSriastradh		return gen4_channel_init_solid(sna, mc, color);
183103b705cfSriastradh
183203b705cfSriastradh	if (sc->is_solid)
183303b705cfSriastradh		return false;
183403b705cfSriastradh
183503b705cfSriastradh	if (src->pDrawable == NULL || mask->pDrawable != src->pDrawable)
183603b705cfSriastradh		return false;
183703b705cfSriastradh
183803b705cfSriastradh	DBG(("%s: mask reuses source drawable\n", __FUNCTION__));
183903b705cfSriastradh
184003b705cfSriastradh	if (!sna_transform_equal(src->transform, mask->transform))
184103b705cfSriastradh		return false;
184203b705cfSriastradh
184303b705cfSriastradh	if (!sna_picture_alphamap_equal(src, mask))
184403b705cfSriastradh		return false;
184503b705cfSriastradh
184603b705cfSriastradh	if (!gen5_check_repeat(mask))
184703b705cfSriastradh		return false;
184803b705cfSriastradh
184903b705cfSriastradh	if (!gen5_check_filter(mask))
185003b705cfSriastradh		return false;
185103b705cfSriastradh
185203b705cfSriastradh	if (!gen5_check_format(mask->format))
185303b705cfSriastradh		return false;
185403b705cfSriastradh
185503b705cfSriastradh	DBG(("%s: reusing source channel for mask with a twist\n",
185603b705cfSriastradh	     __FUNCTION__));
185703b705cfSriastradh
185803b705cfSriastradh	*mc = *sc;
185903b705cfSriastradh	mc->repeat = gen5_repeat(mask->repeat ? mask->repeatType : RepeatNone);
186003b705cfSriastradh	mc->filter = gen5_filter(mask->filter);
186103b705cfSriastradh	mc->pict_format = mask->format;
186203b705cfSriastradh	mc->card_format = gen5_get_card_format(mask->format);
186303b705cfSriastradh	mc->bo = kgem_bo_reference(mc->bo);
186403b705cfSriastradh	return true;
186503b705cfSriastradh}
186603b705cfSriastradh
186703b705cfSriastradhstatic bool
186803b705cfSriastradhgen5_render_composite(struct sna *sna,
186903b705cfSriastradh		      uint8_t op,
187003b705cfSriastradh		      PicturePtr src,
187103b705cfSriastradh		      PicturePtr mask,
187203b705cfSriastradh		      PicturePtr dst,
187303b705cfSriastradh		      int16_t src_x, int16_t src_y,
187403b705cfSriastradh		      int16_t msk_x, int16_t msk_y,
187503b705cfSriastradh		      int16_t dst_x, int16_t dst_y,
187603b705cfSriastradh		      int16_t width, int16_t height,
187742542f5fSchristos		      unsigned flags,
187803b705cfSriastradh		      struct sna_composite_op *tmp)
187903b705cfSriastradh{
188003b705cfSriastradh	DBG(("%s: %dx%d, current mode=%d\n", __FUNCTION__,
188103b705cfSriastradh	     width, height, sna->kgem.mode));
188203b705cfSriastradh
188303b705cfSriastradh	if (op >= ARRAY_SIZE(gen5_blend_op)) {
188403b705cfSriastradh		DBG(("%s: unhandled blend op %d\n", __FUNCTION__, op));
188503b705cfSriastradh		return false;
188603b705cfSriastradh	}
188703b705cfSriastradh
188803b705cfSriastradh	if (mask == NULL &&
188903b705cfSriastradh	    sna_blt_composite(sna, op,
189003b705cfSriastradh			      src, dst,
189103b705cfSriastradh			      src_x, src_y,
189203b705cfSriastradh			      dst_x, dst_y,
189303b705cfSriastradh			      width, height,
189442542f5fSchristos			      flags, tmp))
189503b705cfSriastradh		return true;
189603b705cfSriastradh
189703b705cfSriastradh	if (gen5_composite_fallback(sna, src, mask, dst))
189842542f5fSchristos		goto fallback;
189903b705cfSriastradh
190003b705cfSriastradh	if (need_tiling(sna, width, height))
190103b705cfSriastradh		return sna_tiling_composite(op, src, mask, dst,
190203b705cfSriastradh					    src_x, src_y,
190303b705cfSriastradh					    msk_x, msk_y,
190403b705cfSriastradh					    dst_x, dst_y,
190503b705cfSriastradh					    width, height,
190603b705cfSriastradh					    tmp);
190703b705cfSriastradh
190803b705cfSriastradh	if (!gen5_composite_set_target(sna, tmp, dst,
190903b705cfSriastradh				       dst_x, dst_y, width, height,
191042542f5fSchristos				       flags & COMPOSITE_PARTIAL || op > PictOpSrc)) {
191103b705cfSriastradh		DBG(("%s: failed to set composite target\n", __FUNCTION__));
191242542f5fSchristos		goto fallback;
191303b705cfSriastradh	}
191403b705cfSriastradh
191503b705cfSriastradh	DBG(("%s: preparing source\n", __FUNCTION__));
191603b705cfSriastradh	tmp->op = op;
191703b705cfSriastradh	switch (gen5_composite_picture(sna, src, &tmp->src,
191803b705cfSriastradh				       src_x, src_y,
191903b705cfSriastradh				       width, height,
192003b705cfSriastradh				       dst_x, dst_y,
192103b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
192203b705cfSriastradh	case -1:
192303b705cfSriastradh		DBG(("%s: failed to prepare source picture\n", __FUNCTION__));
192403b705cfSriastradh		goto cleanup_dst;
192503b705cfSriastradh	case 0:
192603b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->src, 0))
192703b705cfSriastradh			goto cleanup_dst;
192803b705cfSriastradh		/* fall through to fixup */
192903b705cfSriastradh	case 1:
193003b705cfSriastradh		if (mask == NULL &&
193103b705cfSriastradh		    sna_blt_composite__convert(sna,
193203b705cfSriastradh					       dst_x, dst_y, width, height,
193303b705cfSriastradh					       tmp))
193403b705cfSriastradh			return true;
193503b705cfSriastradh
193603b705cfSriastradh		gen5_composite_channel_convert(&tmp->src);
193703b705cfSriastradh		break;
193803b705cfSriastradh	}
193903b705cfSriastradh
194003b705cfSriastradh	tmp->is_affine = tmp->src.is_affine;
194103b705cfSriastradh	tmp->has_component_alpha = false;
194203b705cfSriastradh	tmp->need_magic_ca_pass = false;
194303b705cfSriastradh
194403b705cfSriastradh	if (mask) {
194503b705cfSriastradh		if (mask->componentAlpha && PICT_FORMAT_RGB(mask->format)) {
194603b705cfSriastradh			tmp->has_component_alpha = true;
194703b705cfSriastradh
194803b705cfSriastradh			/* Check if it's component alpha that relies on a source alpha and on
194903b705cfSriastradh			 * the source value.  We can only get one of those into the single
195003b705cfSriastradh			 * source value that we get to blend with.
195103b705cfSriastradh			 */
195203b705cfSriastradh			if (gen5_blend_op[op].src_alpha &&
195303b705cfSriastradh			    (gen5_blend_op[op].src_blend != GEN5_BLENDFACTOR_ZERO)) {
195403b705cfSriastradh				if (op != PictOpOver) {
195503b705cfSriastradh					DBG(("%s: unhandled CA blend op %d\n", __FUNCTION__, op));
195603b705cfSriastradh					goto cleanup_src;
195703b705cfSriastradh				}
195803b705cfSriastradh
195903b705cfSriastradh				tmp->need_magic_ca_pass = true;
196003b705cfSriastradh				tmp->op = PictOpOutReverse;
196103b705cfSriastradh			}
196203b705cfSriastradh		}
196303b705cfSriastradh
196403b705cfSriastradh		if (!reuse_source(sna,
196503b705cfSriastradh				  src, &tmp->src, src_x, src_y,
196603b705cfSriastradh				  mask, &tmp->mask, msk_x, msk_y)) {
196703b705cfSriastradh			DBG(("%s: preparing mask\n", __FUNCTION__));
196803b705cfSriastradh			switch (gen5_composite_picture(sna, mask, &tmp->mask,
196903b705cfSriastradh						       msk_x, msk_y,
197003b705cfSriastradh						       width, height,
197103b705cfSriastradh						       dst_x, dst_y,
197203b705cfSriastradh						       dst->polyMode == PolyModePrecise)) {
197303b705cfSriastradh			case -1:
197403b705cfSriastradh				DBG(("%s: failed to prepare mask picture\n", __FUNCTION__));
197503b705cfSriastradh				goto cleanup_src;
197603b705cfSriastradh			case 0:
197703b705cfSriastradh				if (!gen4_channel_init_solid(sna, &tmp->mask, 0))
197803b705cfSriastradh					goto cleanup_src;
197903b705cfSriastradh				/* fall through to fixup */
198003b705cfSriastradh			case 1:
198103b705cfSriastradh				gen5_composite_channel_convert(&tmp->mask);
198203b705cfSriastradh				break;
198303b705cfSriastradh			}
198403b705cfSriastradh		}
198503b705cfSriastradh
198603b705cfSriastradh		tmp->is_affine &= tmp->mask.is_affine;
198703b705cfSriastradh	}
198803b705cfSriastradh
198903b705cfSriastradh	tmp->u.gen5.wm_kernel =
199003b705cfSriastradh		gen5_choose_composite_kernel(tmp->op,
199103b705cfSriastradh					     tmp->mask.bo != NULL,
199203b705cfSriastradh					     tmp->has_component_alpha,
199303b705cfSriastradh					     tmp->is_affine);
199403b705cfSriastradh	tmp->u.gen5.ve_id = gen4_choose_composite_emitter(sna, tmp);
199503b705cfSriastradh
199603b705cfSriastradh	tmp->blt   = gen5_render_composite_blt;
199703b705cfSriastradh	tmp->box   = gen5_render_composite_box;
199803b705cfSriastradh	tmp->boxes = gen5_render_composite_boxes__blt;
199903b705cfSriastradh	if (tmp->emit_boxes) {
200003b705cfSriastradh		tmp->boxes = gen5_render_composite_boxes;
200103b705cfSriastradh		tmp->thread_boxes = gen5_render_composite_boxes__thread;
200203b705cfSriastradh	}
200303b705cfSriastradh	tmp->done  = gen5_render_composite_done;
200403b705cfSriastradh
200503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
200603b705cfSriastradh			   tmp->dst.bo, tmp->src.bo, tmp->mask.bo, NULL)) {
200703b705cfSriastradh		kgem_submit(&sna->kgem);
200803b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
200903b705cfSriastradh				   tmp->dst.bo, tmp->src.bo, tmp->mask.bo, NULL))
201003b705cfSriastradh			goto cleanup_mask;
201103b705cfSriastradh	}
201203b705cfSriastradh
201303b705cfSriastradh	gen5_align_vertex(sna, tmp);
201442542f5fSchristos	gen5_bind_surfaces(sna, tmp);
201503b705cfSriastradh	return true;
201603b705cfSriastradh
201703b705cfSriastradhcleanup_mask:
201842542f5fSchristos	if (tmp->mask.bo) {
201903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->mask.bo);
202042542f5fSchristos		tmp->mask.bo = NULL;
202142542f5fSchristos	}
202203b705cfSriastradhcleanup_src:
202342542f5fSchristos	if (tmp->src.bo) {
202403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->src.bo);
202542542f5fSchristos		tmp->src.bo = NULL;
202642542f5fSchristos	}
202703b705cfSriastradhcleanup_dst:
202842542f5fSchristos	if (tmp->redirect.real_bo) {
202903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->dst.bo);
203042542f5fSchristos		tmp->redirect.real_bo = NULL;
203142542f5fSchristos	}
203242542f5fSchristosfallback:
203342542f5fSchristos	return (mask == NULL &&
203442542f5fSchristos		sna_blt_composite(sna, op,
203542542f5fSchristos				  src, dst,
203642542f5fSchristos				  src_x, src_y,
203742542f5fSchristos				  dst_x, dst_y,
203842542f5fSchristos				  width, height,
203942542f5fSchristos				  flags | COMPOSITE_FALLBACK, tmp));
204003b705cfSriastradh}
204103b705cfSriastradh
204203b705cfSriastradh#if !NO_COMPOSITE_SPANS
204303b705cfSriastradhfastcall static void
204403b705cfSriastradhgen5_render_composite_spans_box(struct sna *sna,
204503b705cfSriastradh				const struct sna_composite_spans_op *op,
204603b705cfSriastradh				const BoxRec *box, float opacity)
204703b705cfSriastradh{
204803b705cfSriastradh	DBG(("%s: src=+(%d, %d), opacity=%f, dst=+(%d, %d), box=(%d, %d) x (%d, %d)\n",
204903b705cfSriastradh	     __FUNCTION__,
205003b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
205103b705cfSriastradh	     opacity,
205203b705cfSriastradh	     op->base.dst.x, op->base.dst.y,
205303b705cfSriastradh	     box->x1, box->y1,
205403b705cfSriastradh	     box->x2 - box->x1,
205503b705cfSriastradh	     box->y2 - box->y1));
205603b705cfSriastradh
205703b705cfSriastradh	gen5_get_rectangles(sna, &op->base, 1, gen5_bind_surfaces);
205803b705cfSriastradh	op->prim_emit(sna, op, box, opacity);
205903b705cfSriastradh}
206003b705cfSriastradh
206103b705cfSriastradhstatic void
206203b705cfSriastradhgen5_render_composite_spans_boxes(struct sna *sna,
206303b705cfSriastradh				  const struct sna_composite_spans_op *op,
206403b705cfSriastradh				  const BoxRec *box, int nbox,
206503b705cfSriastradh				  float opacity)
206603b705cfSriastradh{
206703b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), opacity=%f, dst=+(%d, %d)\n",
206803b705cfSriastradh	     __FUNCTION__, nbox,
206903b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
207003b705cfSriastradh	     opacity,
207103b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
207203b705cfSriastradh
207303b705cfSriastradh	do {
207403b705cfSriastradh		int nbox_this_time;
207503b705cfSriastradh
207603b705cfSriastradh		nbox_this_time = gen5_get_rectangles(sna, &op->base, nbox,
207703b705cfSriastradh						     gen5_bind_surfaces);
207803b705cfSriastradh		nbox -= nbox_this_time;
207903b705cfSriastradh
208003b705cfSriastradh		do {
208103b705cfSriastradh			DBG(("  %s: (%d, %d) x (%d, %d)\n", __FUNCTION__,
208203b705cfSriastradh			     box->x1, box->y1,
208303b705cfSriastradh			     box->x2 - box->x1,
208403b705cfSriastradh			     box->y2 - box->y1));
208503b705cfSriastradh
208603b705cfSriastradh			op->prim_emit(sna, op, box++, opacity);
208703b705cfSriastradh		} while (--nbox_this_time);
208803b705cfSriastradh	} while (nbox);
208903b705cfSriastradh}
209003b705cfSriastradh
209103b705cfSriastradhfastcall static void
209203b705cfSriastradhgen5_render_composite_spans_boxes__thread(struct sna *sna,
209303b705cfSriastradh					  const struct sna_composite_spans_op *op,
209403b705cfSriastradh					  const struct sna_opacity_box *box,
209503b705cfSriastradh					  int nbox)
209603b705cfSriastradh{
209703b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), dst=+(%d, %d)\n",
209803b705cfSriastradh	     __FUNCTION__, nbox,
209903b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
210003b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
210103b705cfSriastradh
210203b705cfSriastradh	sna_vertex_lock(&sna->render);
210303b705cfSriastradh	do {
210403b705cfSriastradh		int nbox_this_time;
210503b705cfSriastradh		float *v;
210603b705cfSriastradh
210703b705cfSriastradh		nbox_this_time = gen5_get_rectangles(sna, &op->base, nbox,
210803b705cfSriastradh						     gen5_bind_surfaces);
210903b705cfSriastradh		assert(nbox_this_time);
211003b705cfSriastradh		nbox -= nbox_this_time;
211103b705cfSriastradh
211203b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
211303b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->base.floats_per_rect;
211403b705cfSriastradh
211503b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
211603b705cfSriastradh		sna_vertex_unlock(&sna->render);
211703b705cfSriastradh
211803b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
211903b705cfSriastradh		box += nbox_this_time;
212003b705cfSriastradh
212103b705cfSriastradh		sna_vertex_lock(&sna->render);
212203b705cfSriastradh		sna_vertex_release__locked(&sna->render);
212303b705cfSriastradh	} while (nbox);
212403b705cfSriastradh	sna_vertex_unlock(&sna->render);
212503b705cfSriastradh}
212603b705cfSriastradh
212703b705cfSriastradhfastcall static void
212803b705cfSriastradhgen5_render_composite_spans_done(struct sna *sna,
212903b705cfSriastradh				 const struct sna_composite_spans_op *op)
213003b705cfSriastradh{
213103b705cfSriastradh	if (sna->render.vertex_offset)
213203b705cfSriastradh		gen4_vertex_flush(sna);
213303b705cfSriastradh
213403b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
213503b705cfSriastradh
213603b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
213703b705cfSriastradh	sna_render_composite_redirect_done(sna, &op->base);
213803b705cfSriastradh}
213903b705cfSriastradh
214003b705cfSriastradhstatic bool
214103b705cfSriastradhgen5_check_composite_spans(struct sna *sna,
214203b705cfSriastradh			   uint8_t op, PicturePtr src, PicturePtr dst,
214303b705cfSriastradh			   int16_t width, int16_t height,
214403b705cfSriastradh			   unsigned flags)
214503b705cfSriastradh{
214603b705cfSriastradh	DBG(("%s: op=%d, width=%d, height=%d, flags=%x\n",
214703b705cfSriastradh	     __FUNCTION__, op, width, height, flags));
214803b705cfSriastradh
214903b705cfSriastradh	if (op >= ARRAY_SIZE(gen5_blend_op))
215003b705cfSriastradh		return false;
215103b705cfSriastradh
215203b705cfSriastradh	if (gen5_composite_fallback(sna, src, NULL, dst)) {
215303b705cfSriastradh		DBG(("%s: operation would fallback\n", __FUNCTION__));
215403b705cfSriastradh		return false;
215503b705cfSriastradh	}
215603b705cfSriastradh
215703b705cfSriastradh	if (need_tiling(sna, width, height) &&
215803b705cfSriastradh	    !is_gpu(sna, dst->pDrawable, PREFER_GPU_SPANS)) {
215903b705cfSriastradh		DBG(("%s: fallback, tiled operation not on GPU\n",
216003b705cfSriastradh		     __FUNCTION__));
216103b705cfSriastradh		return false;
216203b705cfSriastradh	}
216303b705cfSriastradh
216403b705cfSriastradh	if ((flags & COMPOSITE_SPANS_RECTILINEAR) == 0) {
216503b705cfSriastradh		struct sna_pixmap *priv = sna_pixmap_from_drawable(dst->pDrawable);
216603b705cfSriastradh		assert(priv);
216703b705cfSriastradh
216803b705cfSriastradh		if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
216903b705cfSriastradh			return true;
217003b705cfSriastradh
217103b705cfSriastradh		if (flags & COMPOSITE_SPANS_INPLACE_HINT)
217203b705cfSriastradh			return false;
217303b705cfSriastradh
217403b705cfSriastradh		if ((sna->render.prefer_gpu & PREFER_GPU_SPANS) == 0 &&
217503b705cfSriastradh		    dst->format == PICT_a8)
217603b705cfSriastradh			return false;
217703b705cfSriastradh
217803b705cfSriastradh		return priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo);
217903b705cfSriastradh	}
218003b705cfSriastradh
218103b705cfSriastradh	return true;
218203b705cfSriastradh}
218303b705cfSriastradh
218403b705cfSriastradhstatic bool
218503b705cfSriastradhgen5_render_composite_spans(struct sna *sna,
218603b705cfSriastradh			    uint8_t op,
218703b705cfSriastradh			    PicturePtr src,
218803b705cfSriastradh			    PicturePtr dst,
218903b705cfSriastradh			    int16_t src_x,  int16_t src_y,
219003b705cfSriastradh			    int16_t dst_x,  int16_t dst_y,
219103b705cfSriastradh			    int16_t width,  int16_t height,
219203b705cfSriastradh			    unsigned flags,
219303b705cfSriastradh			    struct sna_composite_spans_op *tmp)
219403b705cfSriastradh{
219503b705cfSriastradh	DBG(("%s: %dx%d with flags=%x, current mode=%d\n", __FUNCTION__,
219603b705cfSriastradh	     width, height, flags, sna->kgem.ring));
219703b705cfSriastradh
219803b705cfSriastradh	assert(gen5_check_composite_spans(sna, op, src, dst, width, height, flags));
219903b705cfSriastradh
220003b705cfSriastradh	if (need_tiling(sna, width, height)) {
220103b705cfSriastradh		DBG(("%s: tiling, operation (%dx%d) too wide for pipeline\n",
220203b705cfSriastradh		     __FUNCTION__, width, height));
220303b705cfSriastradh		return sna_tiling_composite_spans(op, src, dst,
220403b705cfSriastradh						  src_x, src_y, dst_x, dst_y,
220503b705cfSriastradh						  width, height, flags, tmp);
220603b705cfSriastradh	}
220703b705cfSriastradh
220803b705cfSriastradh	tmp->base.op = op;
220903b705cfSriastradh	if (!gen5_composite_set_target(sna, &tmp->base, dst,
221003b705cfSriastradh				       dst_x, dst_y, width, height,
221103b705cfSriastradh				       true))
221203b705cfSriastradh		return false;
221303b705cfSriastradh
221403b705cfSriastradh	switch (gen5_composite_picture(sna, src, &tmp->base.src,
221503b705cfSriastradh				       src_x, src_y,
221603b705cfSriastradh				       width, height,
221703b705cfSriastradh				       dst_x, dst_y,
221803b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
221903b705cfSriastradh	case -1:
222003b705cfSriastradh		goto cleanup_dst;
222103b705cfSriastradh	case 0:
222203b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->base.src, 0))
222303b705cfSriastradh			goto cleanup_dst;
222403b705cfSriastradh		/* fall through to fixup */
222503b705cfSriastradh	case 1:
222603b705cfSriastradh		gen5_composite_channel_convert(&tmp->base.src);
222703b705cfSriastradh		break;
222803b705cfSriastradh	}
222903b705cfSriastradh
223003b705cfSriastradh	tmp->base.mask.bo = NULL;
223103b705cfSriastradh
223203b705cfSriastradh	tmp->base.is_affine = tmp->base.src.is_affine;
223303b705cfSriastradh	tmp->base.has_component_alpha = false;
223403b705cfSriastradh	tmp->base.need_magic_ca_pass = false;
223503b705cfSriastradh
223603b705cfSriastradh	tmp->base.u.gen5.ve_id = gen4_choose_spans_emitter(sna, tmp);
223703b705cfSriastradh	tmp->base.u.gen5.wm_kernel = WM_KERNEL_OPACITY | !tmp->base.is_affine;
223803b705cfSriastradh
223903b705cfSriastradh	tmp->box   = gen5_render_composite_spans_box;
224003b705cfSriastradh	tmp->boxes = gen5_render_composite_spans_boxes;
224103b705cfSriastradh	if (tmp->emit_boxes)
224203b705cfSriastradh		tmp->thread_boxes = gen5_render_composite_spans_boxes__thread;
224303b705cfSriastradh	tmp->done  = gen5_render_composite_spans_done;
224403b705cfSriastradh
224503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
224603b705cfSriastradh			   tmp->base.dst.bo, tmp->base.src.bo,
224703b705cfSriastradh			   NULL))  {
224803b705cfSriastradh		kgem_submit(&sna->kgem);
224903b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
225003b705cfSriastradh				   tmp->base.dst.bo, tmp->base.src.bo,
225103b705cfSriastradh				   NULL))
225203b705cfSriastradh			goto cleanup_src;
225303b705cfSriastradh	}
225403b705cfSriastradh
225503b705cfSriastradh	gen5_align_vertex(sna, &tmp->base);
225642542f5fSchristos	gen5_bind_surfaces(sna, &tmp->base);
225703b705cfSriastradh	return true;
225803b705cfSriastradh
225903b705cfSriastradhcleanup_src:
226003b705cfSriastradh	if (tmp->base.src.bo)
226103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.src.bo);
226203b705cfSriastradhcleanup_dst:
226303b705cfSriastradh	if (tmp->base.redirect.real_bo)
226403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.dst.bo);
226503b705cfSriastradh	return false;
226603b705cfSriastradh}
226703b705cfSriastradh#endif
226803b705cfSriastradh
226903b705cfSriastradhstatic void
227003b705cfSriastradhgen5_copy_bind_surfaces(struct sna *sna,
227103b705cfSriastradh			const struct sna_composite_op *op)
227203b705cfSriastradh{
227303b705cfSriastradh	bool dirty = kgem_bo_is_dirty(op->dst.bo);
227403b705cfSriastradh	uint32_t *binding_table;
227503b705cfSriastradh	uint16_t offset;
227603b705cfSriastradh
227703b705cfSriastradh	gen5_get_batch(sna, op);
227803b705cfSriastradh
227903b705cfSriastradh	binding_table = gen5_composite_get_binding_table(sna, &offset);
228003b705cfSriastradh
228103b705cfSriastradh	binding_table[0] =
228203b705cfSriastradh		gen5_bind_bo(sna,
228303b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
228403b705cfSriastradh			     gen5_get_dest_format(op->dst.format),
228503b705cfSriastradh			     true);
228603b705cfSriastradh	binding_table[1] =
228703b705cfSriastradh		gen5_bind_bo(sna,
228803b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
228903b705cfSriastradh			     op->src.card_format,
229003b705cfSriastradh			     false);
229103b705cfSriastradh
229203b705cfSriastradh	if (sna->kgem.surface == offset &&
229303b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen5.surface_table) == *(uint64_t*)binding_table) {
229403b705cfSriastradh		sna->kgem.surface += sizeof(struct gen5_surface_state_padded) / sizeof(uint32_t);
229503b705cfSriastradh		offset = sna->render_state.gen5.surface_table;
229603b705cfSriastradh	}
229703b705cfSriastradh
229803b705cfSriastradh	gen5_emit_state(sna, op, offset | dirty);
229903b705cfSriastradh}
230003b705cfSriastradh
230103b705cfSriastradhstatic bool
230203b705cfSriastradhgen5_render_copy_boxes(struct sna *sna, uint8_t alu,
230342542f5fSchristos		       const DrawableRec *src, struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
230442542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
230503b705cfSriastradh		       const BoxRec *box, int n, unsigned flags)
230603b705cfSriastradh{
230703b705cfSriastradh	struct sna_composite_op tmp;
230803b705cfSriastradh
230903b705cfSriastradh	DBG(("%s alu=%d, src=%ld:handle=%d, dst=%ld:handle=%d boxes=%d x [((%d, %d), (%d, %d))...], flags=%x\n",
231003b705cfSriastradh	     __FUNCTION__, alu,
231142542f5fSchristos	     src->serialNumber, src_bo->handle,
231242542f5fSchristos	     dst->serialNumber, dst_bo->handle,
231303b705cfSriastradh	     n, box->x1, box->y1, box->x2, box->y2,
231403b705cfSriastradh	     flags));
231503b705cfSriastradh
231642542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
231703b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
231803b705cfSriastradh			       src_bo, src_dx, src_dy,
231903b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
232042542f5fSchristos			       dst->bitsPerPixel,
232103b705cfSriastradh			       box, n))
232203b705cfSriastradh		return true;
232303b705cfSriastradh
232403b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo) {
232503b705cfSriastradhfallback_blt:
232642542f5fSchristos		if (!sna_blt_compare_depth(src, dst))
232703b705cfSriastradh			return false;
232803b705cfSriastradh
232903b705cfSriastradh		return sna_blt_copy_boxes_fallback(sna, alu,
233003b705cfSriastradh						   src, src_bo, src_dx, src_dy,
233103b705cfSriastradh						   dst, dst_bo, dst_dx, dst_dy,
233203b705cfSriastradh						   box, n);
233303b705cfSriastradh	}
233403b705cfSriastradh
233503b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
233603b705cfSriastradh
233742542f5fSchristos	if (dst->depth == src->depth) {
233842542f5fSchristos		tmp.dst.format = sna_render_format_for_depth(dst->depth);
233903b705cfSriastradh		tmp.src.pict_format = tmp.dst.format;
234003b705cfSriastradh	} else {
234142542f5fSchristos		tmp.dst.format = sna_format_for_depth(dst->depth);
234242542f5fSchristos		tmp.src.pict_format = sna_format_for_depth(src->depth);
234303b705cfSriastradh	}
234403b705cfSriastradh	if (!gen5_check_format(tmp.src.pict_format)) {
234503b705cfSriastradh		DBG(("%s: unsupported source format, %x, use BLT\n",
234603b705cfSriastradh		     __FUNCTION__, tmp.src.pict_format));
234703b705cfSriastradh		goto fallback_blt;
234803b705cfSriastradh	}
234903b705cfSriastradh
235003b705cfSriastradh	DBG(("%s (%d, %d)->(%d, %d) x %d\n",
235103b705cfSriastradh	     __FUNCTION__, src_dx, src_dy, dst_dx, dst_dy, n));
235203b705cfSriastradh
235303b705cfSriastradh	tmp.op = alu == GXcopy ? PictOpSrc : PictOpClear;
235403b705cfSriastradh
235542542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
235642542f5fSchristos	tmp.dst.width  = dst->width;
235742542f5fSchristos	tmp.dst.height = dst->height;
235803b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
235903b705cfSriastradh	tmp.dst.bo = dst_bo;
236003b705cfSriastradh	tmp.damage = NULL;
236103b705cfSriastradh
236203b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
236303b705cfSriastradh	if (too_large(tmp.dst.width, tmp.dst.height)) {
236403b705cfSriastradh		BoxRec extents = box[0];
236503b705cfSriastradh		int i;
236603b705cfSriastradh
236703b705cfSriastradh		for (i = 1; i < n; i++) {
236803b705cfSriastradh			if (box[i].x1 < extents.x1)
236903b705cfSriastradh				extents.x1 = box[i].x1;
237003b705cfSriastradh			if (box[i].y1 < extents.y1)
237103b705cfSriastradh				extents.y1 = box[i].y1;
237203b705cfSriastradh
237303b705cfSriastradh			if (box[i].x2 > extents.x2)
237403b705cfSriastradh				extents.x2 = box[i].x2;
237503b705cfSriastradh			if (box[i].y2 > extents.y2)
237603b705cfSriastradh				extents.y2 = box[i].y2;
237703b705cfSriastradh		}
237803b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
237903b705cfSriastradh						   extents.x1 + dst_dx,
238003b705cfSriastradh						   extents.y1 + dst_dy,
238103b705cfSriastradh						   extents.x2 - extents.x1,
238203b705cfSriastradh						   extents.y2 - extents.y1,
238303b705cfSriastradh						   n > 1))
238403b705cfSriastradh			goto fallback_tiled;
238503b705cfSriastradh	}
238603b705cfSriastradh
238703b705cfSriastradh	tmp.src.filter = SAMPLER_FILTER_NEAREST;
238803b705cfSriastradh	tmp.src.repeat = SAMPLER_EXTEND_NONE;
238903b705cfSriastradh	tmp.src.card_format = gen5_get_card_format(tmp.src.pict_format);
239042542f5fSchristos	if (too_large(src->width, src->height)) {
239103b705cfSriastradh		BoxRec extents = box[0];
239203b705cfSriastradh		int i;
239303b705cfSriastradh
239403b705cfSriastradh		for (i = 1; i < n; i++) {
239503b705cfSriastradh			if (box[i].x1 < extents.x1)
239603b705cfSriastradh				extents.x1 = box[i].x1;
239703b705cfSriastradh			if (box[i].y1 < extents.y1)
239803b705cfSriastradh				extents.y1 = box[i].y1;
239903b705cfSriastradh
240003b705cfSriastradh			if (box[i].x2 > extents.x2)
240103b705cfSriastradh				extents.x2 = box[i].x2;
240203b705cfSriastradh			if (box[i].y2 > extents.y2)
240303b705cfSriastradh				extents.y2 = box[i].y2;
240403b705cfSriastradh		}
240503b705cfSriastradh
240603b705cfSriastradh		if (!sna_render_pixmap_partial(sna, src, src_bo, &tmp.src,
240703b705cfSriastradh					       extents.x1 + src_dx,
240803b705cfSriastradh					       extents.y1 + src_dy,
240903b705cfSriastradh					       extents.x2 - extents.x1,
241003b705cfSriastradh					       extents.y2 - extents.y1))
241103b705cfSriastradh			goto fallback_tiled_dst;
241203b705cfSriastradh	} else {
241303b705cfSriastradh		tmp.src.bo = kgem_bo_reference(src_bo);
241442542f5fSchristos		tmp.src.width  = src->width;
241542542f5fSchristos		tmp.src.height = src->height;
241603b705cfSriastradh		tmp.src.offset[0] = tmp.src.offset[1] = 0;
241742542f5fSchristos		tmp.src.scale[0] = 1.f/src->width;
241842542f5fSchristos		tmp.src.scale[1] = 1.f/src->height;
241903b705cfSriastradh	}
242003b705cfSriastradh
242103b705cfSriastradh	tmp.is_affine = true;
242203b705cfSriastradh	tmp.floats_per_vertex = 3;
242303b705cfSriastradh	tmp.floats_per_rect = 9;
242403b705cfSriastradh	tmp.u.gen5.wm_kernel = WM_KERNEL;
242503b705cfSriastradh	tmp.u.gen5.ve_id = 2;
242603b705cfSriastradh
242703b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
242803b705cfSriastradh		kgem_submit(&sna->kgem);
242903b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
243003b705cfSriastradh			DBG(("%s: aperture check failed\n", __FUNCTION__));
243142542f5fSchristos			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
243242542f5fSchristos			if (tmp.redirect.real_bo)
243342542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
243442542f5fSchristos
243542542f5fSchristos			goto fallback_blt;
243603b705cfSriastradh		}
243703b705cfSriastradh	}
243803b705cfSriastradh
243903b705cfSriastradh	dst_dx += tmp.dst.x;
244003b705cfSriastradh	dst_dy += tmp.dst.y;
244103b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
244203b705cfSriastradh
244303b705cfSriastradh	src_dx += tmp.src.offset[0];
244403b705cfSriastradh	src_dy += tmp.src.offset[1];
244503b705cfSriastradh
244603b705cfSriastradh	gen5_align_vertex(sna, &tmp);
244742542f5fSchristos	gen5_copy_bind_surfaces(sna, &tmp);
244803b705cfSriastradh
244903b705cfSriastradh	do {
245003b705cfSriastradh		int n_this_time;
245103b705cfSriastradh
245203b705cfSriastradh		n_this_time = gen5_get_rectangles(sna, &tmp, n,
245303b705cfSriastradh						  gen5_copy_bind_surfaces);
245403b705cfSriastradh		n -= n_this_time;
245503b705cfSriastradh
245603b705cfSriastradh		do {
245703b705cfSriastradh			DBG(("	(%d, %d) -> (%d, %d) + (%d, %d)\n",
245803b705cfSriastradh			     box->x1 + src_dx, box->y1 + src_dy,
245903b705cfSriastradh			     box->x1 + dst_dx, box->y1 + dst_dy,
246003b705cfSriastradh			     box->x2 - box->x1, box->y2 - box->y1));
246103b705cfSriastradh			OUT_VERTEX(box->x2 + dst_dx, box->y2 + dst_dy);
246203b705cfSriastradh			OUT_VERTEX_F((box->x2 + src_dx) * tmp.src.scale[0]);
246303b705cfSriastradh			OUT_VERTEX_F((box->y2 + src_dy) * tmp.src.scale[1]);
246403b705cfSriastradh
246503b705cfSriastradh			OUT_VERTEX(box->x1 + dst_dx, box->y2 + dst_dy);
246603b705cfSriastradh			OUT_VERTEX_F((box->x1 + src_dx) * tmp.src.scale[0]);
246703b705cfSriastradh			OUT_VERTEX_F((box->y2 + src_dy) * tmp.src.scale[1]);
246803b705cfSriastradh
246903b705cfSriastradh			OUT_VERTEX(box->x1 + dst_dx, box->y1 + dst_dy);
247003b705cfSriastradh			OUT_VERTEX_F((box->x1 + src_dx) * tmp.src.scale[0]);
247103b705cfSriastradh			OUT_VERTEX_F((box->y1 + src_dy) * tmp.src.scale[1]);
247203b705cfSriastradh
247303b705cfSriastradh			box++;
247403b705cfSriastradh		} while (--n_this_time);
247503b705cfSriastradh	} while (n);
247603b705cfSriastradh
247703b705cfSriastradh	gen4_vertex_flush(sna);
247803b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
247903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
248003b705cfSriastradh	return true;
248103b705cfSriastradh
248203b705cfSriastradhfallback_tiled_dst:
248303b705cfSriastradh	if (tmp.redirect.real_bo)
248403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
248503b705cfSriastradhfallback_tiled:
248642542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
248703b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
248803b705cfSriastradh			       src_bo, src_dx, src_dy,
248903b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
249042542f5fSchristos			       dst->bitsPerPixel,
249103b705cfSriastradh			       box, n))
249203b705cfSriastradh		return true;
249303b705cfSriastradh
249403b705cfSriastradh	DBG(("%s: tiled fallback\n", __FUNCTION__));
249503b705cfSriastradh	return sna_tiling_copy_boxes(sna, alu,
249603b705cfSriastradh				     src, src_bo, src_dx, src_dy,
249703b705cfSriastradh				     dst, dst_bo, dst_dx, dst_dy,
249803b705cfSriastradh				     box, n);
249903b705cfSriastradh}
250003b705cfSriastradh
250103b705cfSriastradhstatic void
250203b705cfSriastradhgen5_render_copy_blt(struct sna *sna,
250303b705cfSriastradh		     const struct sna_copy_op *op,
250403b705cfSriastradh		     int16_t sx, int16_t sy,
250503b705cfSriastradh		     int16_t w,  int16_t h,
250603b705cfSriastradh		     int16_t dx, int16_t dy)
250703b705cfSriastradh{
250803b705cfSriastradh	DBG(("%s: src=(%d, %d), dst=(%d, %d), size=(%d, %d)\n", __FUNCTION__,
250903b705cfSriastradh	     sx, sy, dx, dy, w, h));
251003b705cfSriastradh
251103b705cfSriastradh	gen5_get_rectangles(sna, &op->base, 1, gen5_copy_bind_surfaces);
251203b705cfSriastradh
251303b705cfSriastradh	OUT_VERTEX(dx+w, dy+h);
251403b705cfSriastradh	OUT_VERTEX_F((sx+w)*op->base.src.scale[0]);
251503b705cfSriastradh	OUT_VERTEX_F((sy+h)*op->base.src.scale[1]);
251603b705cfSriastradh
251703b705cfSriastradh	OUT_VERTEX(dx, dy+h);
251803b705cfSriastradh	OUT_VERTEX_F(sx*op->base.src.scale[0]);
251903b705cfSriastradh	OUT_VERTEX_F((sy+h)*op->base.src.scale[1]);
252003b705cfSriastradh
252103b705cfSriastradh	OUT_VERTEX(dx, dy);
252203b705cfSriastradh	OUT_VERTEX_F(sx*op->base.src.scale[0]);
252303b705cfSriastradh	OUT_VERTEX_F(sy*op->base.src.scale[1]);
252403b705cfSriastradh}
252503b705cfSriastradh
252603b705cfSriastradhstatic void
252703b705cfSriastradhgen5_render_copy_done(struct sna *sna,
252803b705cfSriastradh		      const struct sna_copy_op *op)
252903b705cfSriastradh{
253003b705cfSriastradh	if (sna->render.vertex_offset)
253103b705cfSriastradh		gen4_vertex_flush(sna);
253203b705cfSriastradh
253303b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
253403b705cfSriastradh}
253503b705cfSriastradh
253603b705cfSriastradhstatic bool
253703b705cfSriastradhgen5_render_copy(struct sna *sna, uint8_t alu,
253803b705cfSriastradh		 PixmapPtr src, struct kgem_bo *src_bo,
253903b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
254003b705cfSriastradh		 struct sna_copy_op *op)
254103b705cfSriastradh{
254203b705cfSriastradh	DBG(("%s (alu=%d)\n", __FUNCTION__, alu));
254303b705cfSriastradh
254403b705cfSriastradh	if (sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
254503b705cfSriastradh	    sna_blt_copy(sna, alu,
254603b705cfSriastradh			 src_bo, dst_bo,
254703b705cfSriastradh			 dst->drawable.bitsPerPixel,
254803b705cfSriastradh			 op))
254903b705cfSriastradh		return true;
255003b705cfSriastradh
255103b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo ||
255203b705cfSriastradh	    too_large(src->drawable.width, src->drawable.height) ||
255303b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height)) {
255403b705cfSriastradhfallback:
255503b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
255603b705cfSriastradh			return false;
255703b705cfSriastradh
255803b705cfSriastradh		return sna_blt_copy(sna, alu, src_bo, dst_bo,
255903b705cfSriastradh				    dst->drawable.bitsPerPixel,
256003b705cfSriastradh				    op);
256103b705cfSriastradh	}
256203b705cfSriastradh
256303b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
256403b705cfSriastradh		op->base.dst.format = sna_render_format_for_depth(dst->drawable.depth);
256503b705cfSriastradh		op->base.src.pict_format = op->base.dst.format;
256603b705cfSriastradh	} else {
256703b705cfSriastradh		op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
256803b705cfSriastradh		op->base.src.pict_format = sna_format_for_depth(src->drawable.depth);
256903b705cfSriastradh	}
257003b705cfSriastradh	if (!gen5_check_format(op->base.src.pict_format))
257103b705cfSriastradh		goto fallback;
257203b705cfSriastradh
257303b705cfSriastradh	op->base.op = alu == GXcopy ? PictOpSrc : PictOpClear;
257403b705cfSriastradh
257503b705cfSriastradh	op->base.dst.pixmap = dst;
257603b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
257703b705cfSriastradh	op->base.dst.height = dst->drawable.height;
257803b705cfSriastradh	op->base.dst.bo = dst_bo;
257903b705cfSriastradh
258003b705cfSriastradh	op->base.src.bo = src_bo;
258103b705cfSriastradh	op->base.src.card_format =
258203b705cfSriastradh		gen5_get_card_format(op->base.src.pict_format);
258303b705cfSriastradh	op->base.src.width  = src->drawable.width;
258403b705cfSriastradh	op->base.src.height = src->drawable.height;
258503b705cfSriastradh	op->base.src.scale[0] = 1.f/src->drawable.width;
258603b705cfSriastradh	op->base.src.scale[1] = 1.f/src->drawable.height;
258703b705cfSriastradh	op->base.src.filter = SAMPLER_FILTER_NEAREST;
258803b705cfSriastradh	op->base.src.repeat = SAMPLER_EXTEND_NONE;
258903b705cfSriastradh
259003b705cfSriastradh	op->base.is_affine = true;
259103b705cfSriastradh	op->base.floats_per_vertex = 3;
259203b705cfSriastradh	op->base.floats_per_rect = 9;
259303b705cfSriastradh	op->base.u.gen5.wm_kernel = WM_KERNEL;
259403b705cfSriastradh	op->base.u.gen5.ve_id = 2;
259503b705cfSriastradh
259603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
259703b705cfSriastradh		kgem_submit(&sna->kgem);
259803b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL))
259903b705cfSriastradh			goto fallback;
260003b705cfSriastradh	}
260103b705cfSriastradh
260203b705cfSriastradh	if (kgem_bo_is_dirty(src_bo)) {
260303b705cfSriastradh		if (sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
260403b705cfSriastradh		    sna_blt_copy(sna, alu,
260503b705cfSriastradh				 src_bo, dst_bo,
260603b705cfSriastradh				 dst->drawable.bitsPerPixel,
260703b705cfSriastradh				 op))
260803b705cfSriastradh			return true;
260903b705cfSriastradh	}
261003b705cfSriastradh
261103b705cfSriastradh	gen5_align_vertex(sna, &op->base);
261242542f5fSchristos	gen5_copy_bind_surfaces(sna, &op->base);
261303b705cfSriastradh
261403b705cfSriastradh	op->blt  = gen5_render_copy_blt;
261503b705cfSriastradh	op->done = gen5_render_copy_done;
261603b705cfSriastradh	return true;
261703b705cfSriastradh}
261803b705cfSriastradh
261903b705cfSriastradhstatic void
262003b705cfSriastradhgen5_fill_bind_surfaces(struct sna *sna,
262103b705cfSriastradh			const struct sna_composite_op *op)
262203b705cfSriastradh{
262303b705cfSriastradh	bool dirty = kgem_bo_is_dirty(op->dst.bo);
262403b705cfSriastradh	uint32_t *binding_table;
262503b705cfSriastradh	uint16_t offset;
262603b705cfSriastradh
262703b705cfSriastradh	gen5_get_batch(sna, op);
262803b705cfSriastradh
262903b705cfSriastradh	binding_table = gen5_composite_get_binding_table(sna, &offset);
263003b705cfSriastradh
263103b705cfSriastradh	binding_table[0] =
263203b705cfSriastradh		gen5_bind_bo(sna,
263303b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
263403b705cfSriastradh			     gen5_get_dest_format(op->dst.format),
263503b705cfSriastradh			     true);
263603b705cfSriastradh	binding_table[1] =
263703b705cfSriastradh		gen5_bind_bo(sna,
263803b705cfSriastradh			     op->src.bo, 1, 1,
263903b705cfSriastradh			     GEN5_SURFACEFORMAT_B8G8R8A8_UNORM,
264003b705cfSriastradh			     false);
264103b705cfSriastradh
264203b705cfSriastradh	if (sna->kgem.surface == offset &&
264303b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen5.surface_table) == *(uint64_t*)binding_table) {
264403b705cfSriastradh		sna->kgem.surface +=
264503b705cfSriastradh			sizeof(struct gen5_surface_state_padded)/sizeof(uint32_t);
264603b705cfSriastradh		offset = sna->render_state.gen5.surface_table;
264703b705cfSriastradh	}
264803b705cfSriastradh
264903b705cfSriastradh	gen5_emit_state(sna, op, offset | dirty);
265003b705cfSriastradh}
265103b705cfSriastradh
265203b705cfSriastradhstatic inline bool prefer_blt_fill(struct sna *sna)
265303b705cfSriastradh{
265403b705cfSriastradh#if PREFER_BLT_FILL
265503b705cfSriastradh	return true;
265603b705cfSriastradh#else
265703b705cfSriastradh	return sna->kgem.mode != KGEM_RENDER;
265803b705cfSriastradh#endif
265903b705cfSriastradh}
266003b705cfSriastradh
266103b705cfSriastradhstatic bool
266203b705cfSriastradhgen5_render_fill_boxes(struct sna *sna,
266303b705cfSriastradh		       CARD8 op,
266403b705cfSriastradh		       PictFormat format,
266503b705cfSriastradh		       const xRenderColor *color,
266642542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo,
266703b705cfSriastradh		       const BoxRec *box, int n)
266803b705cfSriastradh{
266903b705cfSriastradh	struct sna_composite_op tmp;
267003b705cfSriastradh	uint32_t pixel;
267103b705cfSriastradh
267203b705cfSriastradh	DBG(("%s op=%x, color=(%04x,%04x,%04x,%04x), boxes=%d x [((%d, %d), (%d, %d))...]\n",
267303b705cfSriastradh	     __FUNCTION__, op,
267403b705cfSriastradh	     color->red, color->green, color->blue, color->alpha,
267503b705cfSriastradh	     n, box->x1, box->y1, box->x2, box->y2));
267603b705cfSriastradh
267703b705cfSriastradh	if (op >= ARRAY_SIZE(gen5_blend_op)) {
267803b705cfSriastradh		DBG(("%s: fallback due to unhandled blend op: %d\n",
267903b705cfSriastradh		     __FUNCTION__, op));
268003b705cfSriastradh		return false;
268103b705cfSriastradh	}
268203b705cfSriastradh
268303b705cfSriastradh	if (op <= PictOpSrc &&
268403b705cfSriastradh	    (prefer_blt_fill(sna) ||
268542542f5fSchristos	     too_large(dst->width, dst->height) ||
268603b705cfSriastradh	     !gen5_check_dst_format(format))) {
268703b705cfSriastradh		uint8_t alu = GXinvalid;
268803b705cfSriastradh
268903b705cfSriastradh		pixel = 0;
269003b705cfSriastradh		if (op == PictOpClear)
269103b705cfSriastradh			alu = GXclear;
269203b705cfSriastradh		else if (sna_get_pixel_from_rgba(&pixel,
269303b705cfSriastradh						 color->red,
269403b705cfSriastradh						 color->green,
269503b705cfSriastradh						 color->blue,
269603b705cfSriastradh						 color->alpha,
269703b705cfSriastradh						 format))
269803b705cfSriastradh			alu = GXcopy;
269903b705cfSriastradh
270003b705cfSriastradh		if (alu != GXinvalid &&
270103b705cfSriastradh		    sna_blt_fill_boxes(sna, alu,
270242542f5fSchristos				       dst_bo, dst->bitsPerPixel,
270303b705cfSriastradh				       pixel, box, n))
270403b705cfSriastradh			return true;
270503b705cfSriastradh
270603b705cfSriastradh		if (!gen5_check_dst_format(format))
270703b705cfSriastradh			return false;
270803b705cfSriastradh
270942542f5fSchristos		if (too_large(dst->width, dst->height))
271003b705cfSriastradh			return sna_tiling_fill_boxes(sna, op, format, color,
271103b705cfSriastradh						     dst, dst_bo, box, n);
271203b705cfSriastradh	}
271303b705cfSriastradh
271403b705cfSriastradh	if (op == PictOpClear) {
271503b705cfSriastradh		pixel = 0;
271603b705cfSriastradh		op = PictOpSrc;
271703b705cfSriastradh	} else if (!sna_get_pixel_from_rgba(&pixel,
271803b705cfSriastradh					    color->red,
271903b705cfSriastradh					    color->green,
272003b705cfSriastradh					    color->blue,
272103b705cfSriastradh					    color->alpha,
272203b705cfSriastradh					    PICT_a8r8g8b8))
272303b705cfSriastradh		return false;
272403b705cfSriastradh
272503b705cfSriastradh	DBG(("%s(%08x x %d)\n", __FUNCTION__, pixel, n));
272603b705cfSriastradh
272703b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
272803b705cfSriastradh
272903b705cfSriastradh	tmp.op = op;
273003b705cfSriastradh
273142542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
273242542f5fSchristos	tmp.dst.width  = dst->width;
273342542f5fSchristos	tmp.dst.height = dst->height;
273403b705cfSriastradh	tmp.dst.format = format;
273503b705cfSriastradh	tmp.dst.bo = dst_bo;
273603b705cfSriastradh
273703b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, pixel);
273803b705cfSriastradh	tmp.src.filter = SAMPLER_FILTER_NEAREST;
273903b705cfSriastradh	tmp.src.repeat = SAMPLER_EXTEND_REPEAT;
274003b705cfSriastradh
274103b705cfSriastradh	tmp.is_affine = true;
274203b705cfSriastradh	tmp.floats_per_vertex = 2;
274303b705cfSriastradh	tmp.floats_per_rect = 6;
274403b705cfSriastradh	tmp.u.gen5.wm_kernel = WM_KERNEL;
274503b705cfSriastradh	tmp.u.gen5.ve_id = 1;
274603b705cfSriastradh
274703b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
274803b705cfSriastradh		kgem_submit(&sna->kgem);
274942542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
275042542f5fSchristos			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
275142542f5fSchristos			return false;
275242542f5fSchristos		}
275303b705cfSriastradh	}
275403b705cfSriastradh
275503b705cfSriastradh	gen5_align_vertex(sna, &tmp);
275642542f5fSchristos	gen5_fill_bind_surfaces(sna, &tmp);
275703b705cfSriastradh
275803b705cfSriastradh	do {
275903b705cfSriastradh		int n_this_time;
276003b705cfSriastradh
276103b705cfSriastradh		n_this_time = gen5_get_rectangles(sna, &tmp, n,
276203b705cfSriastradh						  gen5_fill_bind_surfaces);
276303b705cfSriastradh		n -= n_this_time;
276403b705cfSriastradh
276503b705cfSriastradh		do {
276603b705cfSriastradh			DBG(("	(%d, %d), (%d, %d)\n",
276703b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
276803b705cfSriastradh			OUT_VERTEX(box->x2, box->y2);
276903b705cfSriastradh			OUT_VERTEX_F(.5);
277003b705cfSriastradh
277103b705cfSriastradh			OUT_VERTEX(box->x1, box->y2);
277203b705cfSriastradh			OUT_VERTEX_F(.5);
277303b705cfSriastradh
277403b705cfSriastradh			OUT_VERTEX(box->x1, box->y1);
277503b705cfSriastradh			OUT_VERTEX_F(.5);
277603b705cfSriastradh
277703b705cfSriastradh			box++;
277803b705cfSriastradh		} while (--n_this_time);
277903b705cfSriastradh	} while (n);
278003b705cfSriastradh
278103b705cfSriastradh	gen4_vertex_flush(sna);
278203b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
278303b705cfSriastradh	return true;
278403b705cfSriastradh}
278503b705cfSriastradh
278603b705cfSriastradhstatic void
278703b705cfSriastradhgen5_render_fill_op_blt(struct sna *sna,
278803b705cfSriastradh			const struct sna_fill_op *op,
278903b705cfSriastradh			int16_t x, int16_t y, int16_t w, int16_t h)
279003b705cfSriastradh{
279103b705cfSriastradh	DBG(("%s (%d, %d)x(%d, %d)\n", __FUNCTION__, x,y,w,h));
279203b705cfSriastradh
279303b705cfSriastradh	gen5_get_rectangles(sna, &op->base, 1, gen5_fill_bind_surfaces);
279403b705cfSriastradh
279503b705cfSriastradh	OUT_VERTEX(x+w, y+h);
279603b705cfSriastradh	OUT_VERTEX_F(.5);
279703b705cfSriastradh
279803b705cfSriastradh	OUT_VERTEX(x, y+h);
279903b705cfSriastradh	OUT_VERTEX_F(.5);
280003b705cfSriastradh
280103b705cfSriastradh	OUT_VERTEX(x, y);
280203b705cfSriastradh	OUT_VERTEX_F(.5);
280303b705cfSriastradh}
280403b705cfSriastradh
280503b705cfSriastradhfastcall static void
280603b705cfSriastradhgen5_render_fill_op_box(struct sna *sna,
280703b705cfSriastradh			const struct sna_fill_op *op,
280803b705cfSriastradh			const BoxRec *box)
280903b705cfSriastradh{
281003b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
281103b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
281203b705cfSriastradh
281303b705cfSriastradh	gen5_get_rectangles(sna, &op->base, 1, gen5_fill_bind_surfaces);
281403b705cfSriastradh
281503b705cfSriastradh	OUT_VERTEX(box->x2, box->y2);
281603b705cfSriastradh	OUT_VERTEX_F(.5);
281703b705cfSriastradh
281803b705cfSriastradh	OUT_VERTEX(box->x1, box->y2);
281903b705cfSriastradh	OUT_VERTEX_F(.5);
282003b705cfSriastradh
282103b705cfSriastradh	OUT_VERTEX(box->x1, box->y1);
282203b705cfSriastradh	OUT_VERTEX_F(.5);
282303b705cfSriastradh}
282403b705cfSriastradh
282503b705cfSriastradhfastcall static void
282603b705cfSriastradhgen5_render_fill_op_boxes(struct sna *sna,
282703b705cfSriastradh			  const struct sna_fill_op *op,
282803b705cfSriastradh			  const BoxRec *box,
282903b705cfSriastradh			  int nbox)
283003b705cfSriastradh{
283103b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)... x %d\n", __FUNCTION__,
283203b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2, nbox));
283303b705cfSriastradh
283403b705cfSriastradh	do {
283503b705cfSriastradh		int nbox_this_time;
283603b705cfSriastradh
283703b705cfSriastradh		nbox_this_time = gen5_get_rectangles(sna, &op->base, nbox,
283803b705cfSriastradh						     gen5_fill_bind_surfaces);
283903b705cfSriastradh		nbox -= nbox_this_time;
284003b705cfSriastradh
284103b705cfSriastradh		do {
284203b705cfSriastradh			OUT_VERTEX(box->x2, box->y2);
284303b705cfSriastradh			OUT_VERTEX_F(.5);
284403b705cfSriastradh
284503b705cfSriastradh			OUT_VERTEX(box->x1, box->y2);
284603b705cfSriastradh			OUT_VERTEX_F(.5);
284703b705cfSriastradh
284803b705cfSriastradh			OUT_VERTEX(box->x1, box->y1);
284903b705cfSriastradh			OUT_VERTEX_F(.5);
285003b705cfSriastradh			box++;
285103b705cfSriastradh		} while (--nbox_this_time);
285203b705cfSriastradh	} while (nbox);
285303b705cfSriastradh}
285403b705cfSriastradh
285503b705cfSriastradhstatic void
285603b705cfSriastradhgen5_render_fill_op_done(struct sna *sna,
285703b705cfSriastradh			 const struct sna_fill_op *op)
285803b705cfSriastradh{
285903b705cfSriastradh	if (sna->render.vertex_offset)
286003b705cfSriastradh		gen4_vertex_flush(sna);
286103b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
286203b705cfSriastradh
286303b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
286403b705cfSriastradh}
286503b705cfSriastradh
286603b705cfSriastradhstatic bool
286703b705cfSriastradhgen5_render_fill(struct sna *sna, uint8_t alu,
286803b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
286942542f5fSchristos		 uint32_t color, unsigned flags,
287003b705cfSriastradh		 struct sna_fill_op *op)
287103b705cfSriastradh{
287203b705cfSriastradh	DBG(("%s(alu=%d, color=%08x)\n", __FUNCTION__, alu, color));
287303b705cfSriastradh
287403b705cfSriastradh	if (prefer_blt_fill(sna) &&
287503b705cfSriastradh	    sna_blt_fill(sna, alu,
287603b705cfSriastradh			 dst_bo, dst->drawable.bitsPerPixel,
287703b705cfSriastradh			 color,
287803b705cfSriastradh			 op))
287903b705cfSriastradh		return true;
288003b705cfSriastradh
288103b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
288203b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
288303b705cfSriastradh		return sna_blt_fill(sna, alu,
288403b705cfSriastradh				    dst_bo, dst->drawable.bitsPerPixel,
288503b705cfSriastradh				    color,
288603b705cfSriastradh				    op);
288703b705cfSriastradh
288803b705cfSriastradh	if (alu == GXclear)
288903b705cfSriastradh		color = 0;
289003b705cfSriastradh
289103b705cfSriastradh	op->base.op = color == 0 ? PictOpClear : PictOpSrc;
289203b705cfSriastradh
289303b705cfSriastradh	op->base.dst.pixmap = dst;
289403b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
289503b705cfSriastradh	op->base.dst.height = dst->drawable.height;
289603b705cfSriastradh	op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
289703b705cfSriastradh	op->base.dst.bo = dst_bo;
289803b705cfSriastradh	op->base.dst.x = op->base.dst.y = 0;
289903b705cfSriastradh
290003b705cfSriastradh	op->base.need_magic_ca_pass = 0;
290103b705cfSriastradh	op->base.has_component_alpha = 0;
290203b705cfSriastradh
290303b705cfSriastradh	op->base.src.bo =
290403b705cfSriastradh		sna_render_get_solid(sna,
290503b705cfSriastradh				     sna_rgba_for_color(color,
290603b705cfSriastradh							dst->drawable.depth));
290703b705cfSriastradh	op->base.src.filter = SAMPLER_FILTER_NEAREST;
290803b705cfSriastradh	op->base.src.repeat = SAMPLER_EXTEND_REPEAT;
290903b705cfSriastradh
291003b705cfSriastradh	op->base.mask.bo = NULL;
291103b705cfSriastradh	op->base.mask.filter = SAMPLER_FILTER_NEAREST;
291203b705cfSriastradh	op->base.mask.repeat = SAMPLER_EXTEND_NONE;
291303b705cfSriastradh
291403b705cfSriastradh	op->base.is_affine = true;
291503b705cfSriastradh	op->base.floats_per_vertex = 2;
291603b705cfSriastradh	op->base.floats_per_rect = 6;
291703b705cfSriastradh	op->base.u.gen5.wm_kernel = WM_KERNEL;
291803b705cfSriastradh	op->base.u.gen5.ve_id = 1;
291903b705cfSriastradh
292003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
292103b705cfSriastradh		kgem_submit(&sna->kgem);
292242542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
292342542f5fSchristos			kgem_bo_destroy(&sna->kgem, op->base.src.bo);
292442542f5fSchristos			return false;
292542542f5fSchristos		}
292603b705cfSriastradh	}
292703b705cfSriastradh
292803b705cfSriastradh	gen5_align_vertex(sna, &op->base);
292942542f5fSchristos	gen5_fill_bind_surfaces(sna, &op->base);
293003b705cfSriastradh
293103b705cfSriastradh	op->blt   = gen5_render_fill_op_blt;
293203b705cfSriastradh	op->box   = gen5_render_fill_op_box;
293303b705cfSriastradh	op->boxes = gen5_render_fill_op_boxes;
293442542f5fSchristos	op->points = NULL;
293503b705cfSriastradh	op->done  = gen5_render_fill_op_done;
293603b705cfSriastradh	return true;
293703b705cfSriastradh}
293803b705cfSriastradh
293903b705cfSriastradhstatic bool
294003b705cfSriastradhgen5_render_fill_one_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
294103b705cfSriastradh			     uint32_t color,
294203b705cfSriastradh			     int16_t x1, int16_t y1, int16_t x2, int16_t y2,
294303b705cfSriastradh			     uint8_t alu)
294403b705cfSriastradh{
294503b705cfSriastradh	BoxRec box;
294603b705cfSriastradh
294703b705cfSriastradh	box.x1 = x1;
294803b705cfSriastradh	box.y1 = y1;
294903b705cfSriastradh	box.x2 = x2;
295003b705cfSriastradh	box.y2 = y2;
295103b705cfSriastradh
295203b705cfSriastradh	return sna_blt_fill_boxes(sna, alu,
295303b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
295403b705cfSriastradh				  color, &box, 1);
295503b705cfSriastradh}
295603b705cfSriastradh
295703b705cfSriastradhstatic bool
295803b705cfSriastradhgen5_render_fill_one(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
295903b705cfSriastradh		     uint32_t color,
296003b705cfSriastradh		     int16_t x1, int16_t y1,
296103b705cfSriastradh		     int16_t x2, int16_t y2,
296203b705cfSriastradh		     uint8_t alu)
296303b705cfSriastradh{
296403b705cfSriastradh	struct sna_composite_op tmp;
296503b705cfSriastradh
296603b705cfSriastradh#if NO_FILL_ONE
296703b705cfSriastradh	return gen5_render_fill_one_try_blt(sna, dst, bo, color,
296803b705cfSriastradh					    x1, y1, x2, y2, alu);
296903b705cfSriastradh#endif
297003b705cfSriastradh
297103b705cfSriastradh	/* Prefer to use the BLT if already engaged */
297203b705cfSriastradh	if (prefer_blt_fill(sna) &&
297303b705cfSriastradh	    gen5_render_fill_one_try_blt(sna, dst, bo, color,
297403b705cfSriastradh					 x1, y1, x2, y2, alu))
297503b705cfSriastradh		return true;
297603b705cfSriastradh
297703b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
297803b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
297903b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
298003b705cfSriastradh		return gen5_render_fill_one_try_blt(sna, dst, bo, color,
298103b705cfSriastradh						    x1, y1, x2, y2, alu);
298203b705cfSriastradh
298303b705cfSriastradh	if (alu == GXclear)
298403b705cfSriastradh		color = 0;
298503b705cfSriastradh
298603b705cfSriastradh	tmp.op = color == 0 ? PictOpClear : PictOpSrc;
298703b705cfSriastradh
298803b705cfSriastradh	tmp.dst.pixmap = dst;
298903b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
299003b705cfSriastradh	tmp.dst.height = dst->drawable.height;
299103b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
299203b705cfSriastradh	tmp.dst.bo = bo;
299303b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
299403b705cfSriastradh
299503b705cfSriastradh	tmp.src.bo =
299603b705cfSriastradh		sna_render_get_solid(sna,
299703b705cfSriastradh				     sna_rgba_for_color(color,
299803b705cfSriastradh							dst->drawable.depth));
299903b705cfSriastradh	tmp.src.filter = SAMPLER_FILTER_NEAREST;
300003b705cfSriastradh	tmp.src.repeat = SAMPLER_EXTEND_REPEAT;
300103b705cfSriastradh
300203b705cfSriastradh	tmp.mask.bo = NULL;
300303b705cfSriastradh	tmp.mask.filter = SAMPLER_FILTER_NEAREST;
300403b705cfSriastradh	tmp.mask.repeat = SAMPLER_EXTEND_NONE;
300503b705cfSriastradh
300603b705cfSriastradh	tmp.is_affine = true;
300703b705cfSriastradh	tmp.floats_per_vertex = 2;
300803b705cfSriastradh	tmp.floats_per_rect = 6;
300903b705cfSriastradh	tmp.has_component_alpha = 0;
301003b705cfSriastradh	tmp.need_magic_ca_pass = false;
301103b705cfSriastradh
301203b705cfSriastradh	tmp.u.gen5.wm_kernel = WM_KERNEL;
301303b705cfSriastradh	tmp.u.gen5.ve_id = 1;
301403b705cfSriastradh
301503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
301603b705cfSriastradh		kgem_submit(&sna->kgem);
301703b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
301803b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
301903b705cfSriastradh			return false;
302003b705cfSriastradh		}
302103b705cfSriastradh	}
302203b705cfSriastradh
302303b705cfSriastradh	gen5_align_vertex(sna, &tmp);
302442542f5fSchristos	gen5_fill_bind_surfaces(sna, &tmp);
302503b705cfSriastradh
302603b705cfSriastradh	gen5_get_rectangles(sna, &tmp, 1, gen5_fill_bind_surfaces);
302703b705cfSriastradh
302803b705cfSriastradh	DBG(("	(%d, %d), (%d, %d)\n", x1, y1, x2, y2));
302903b705cfSriastradh	OUT_VERTEX(x2, y2);
303003b705cfSriastradh	OUT_VERTEX_F(.5);
303103b705cfSriastradh
303203b705cfSriastradh	OUT_VERTEX(x1, y2);
303303b705cfSriastradh	OUT_VERTEX_F(.5);
303403b705cfSriastradh
303503b705cfSriastradh	OUT_VERTEX(x1, y1);
303603b705cfSriastradh	OUT_VERTEX_F(.5);
303703b705cfSriastradh
303803b705cfSriastradh	gen4_vertex_flush(sna);
303903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
304003b705cfSriastradh
304103b705cfSriastradh	return true;
304203b705cfSriastradh}
304303b705cfSriastradhstatic void
304403b705cfSriastradhgen5_render_context_switch(struct kgem *kgem,
304503b705cfSriastradh			   int new_mode)
304603b705cfSriastradh{
304703b705cfSriastradh	if (!kgem->nbatch)
304803b705cfSriastradh		return;
304903b705cfSriastradh
305003b705cfSriastradh	/* WaNonPipelinedStateCommandFlush
305103b705cfSriastradh	 *
305203b705cfSriastradh	 * Ironlake has a limitation that a 3D or Media command can't
305303b705cfSriastradh	 * be the first command after a BLT, unless it's
305403b705cfSriastradh	 * non-pipelined.
305503b705cfSriastradh	 *
305603b705cfSriastradh	 * We do this by ensuring that the non-pipelined drawrect
305703b705cfSriastradh	 * is always emitted first following a switch from BLT.
305803b705cfSriastradh	 */
305903b705cfSriastradh	if (kgem->mode == KGEM_BLT) {
306003b705cfSriastradh		struct sna *sna = to_sna_from_kgem(kgem);
306103b705cfSriastradh		DBG(("%s: forcing drawrect on next state emission\n",
306203b705cfSriastradh		     __FUNCTION__));
306303b705cfSriastradh		sna->render_state.gen5.drawrect_limit = -1;
306403b705cfSriastradh	}
306503b705cfSriastradh
306603b705cfSriastradh	if (kgem_ring_is_idle(kgem, kgem->ring)) {
306703b705cfSriastradh		DBG(("%s: GPU idle, flushing\n", __FUNCTION__));
306803b705cfSriastradh		_kgem_submit(kgem);
306903b705cfSriastradh	}
307003b705cfSriastradh}
307103b705cfSriastradh
307203b705cfSriastradhstatic void gen5_render_reset(struct sna *sna)
307303b705cfSriastradh{
307403b705cfSriastradh	sna->render_state.gen5.needs_invariant = true;
307503b705cfSriastradh	sna->render_state.gen5.ve_id = -1;
307603b705cfSriastradh	sna->render_state.gen5.last_primitive = -1;
307703b705cfSriastradh	sna->render_state.gen5.last_pipelined_pointers = 0;
307803b705cfSriastradh
307903b705cfSriastradh	sna->render_state.gen5.drawrect_offset = -1;
308003b705cfSriastradh	sna->render_state.gen5.drawrect_limit = -1;
308103b705cfSriastradh	sna->render_state.gen5.surface_table = -1;
308203b705cfSriastradh
308342542f5fSchristos	if (sna->render.vbo && !kgem_bo_can_map(&sna->kgem, sna->render.vbo)) {
308403b705cfSriastradh		DBG(("%s: discarding unmappable vbo\n", __FUNCTION__));
308503b705cfSriastradh		discard_vbo(sna);
308603b705cfSriastradh	}
308703b705cfSriastradh
308803b705cfSriastradh	sna->render.vertex_offset = 0;
308903b705cfSriastradh	sna->render.nvertex_reloc = 0;
309003b705cfSriastradh	sna->render.vb_id = 0;
309103b705cfSriastradh}
309203b705cfSriastradh
309303b705cfSriastradhstatic void gen5_render_fini(struct sna *sna)
309403b705cfSriastradh{
309503b705cfSriastradh	kgem_bo_destroy(&sna->kgem, sna->render_state.gen5.general_bo);
309603b705cfSriastradh}
309703b705cfSriastradh
309803b705cfSriastradhstatic uint32_t gen5_create_vs_unit_state(struct sna_static_stream *stream)
309903b705cfSriastradh{
310003b705cfSriastradh	struct gen5_vs_unit_state *vs = sna_static_stream_map(stream, sizeof(*vs), 32);
310103b705cfSriastradh
310203b705cfSriastradh	/* Set up the vertex shader to be disabled (passthrough) */
310303b705cfSriastradh	vs->thread4.nr_urb_entries = URB_VS_ENTRIES >> 2;
310403b705cfSriastradh	vs->thread4.urb_entry_allocation_size = URB_VS_ENTRY_SIZE - 1;
310503b705cfSriastradh	vs->vs6.vs_enable = 0;
310603b705cfSriastradh	vs->vs6.vert_cache_disable = 1;
310703b705cfSriastradh
310803b705cfSriastradh	return sna_static_stream_offsetof(stream, vs);
310903b705cfSriastradh}
311003b705cfSriastradh
311103b705cfSriastradhstatic uint32_t gen5_create_sf_state(struct sna_static_stream *stream,
311203b705cfSriastradh				     uint32_t kernel)
311303b705cfSriastradh{
311403b705cfSriastradh	struct gen5_sf_unit_state *sf_state;
311503b705cfSriastradh
311603b705cfSriastradh	sf_state = sna_static_stream_map(stream, sizeof(*sf_state), 32);
311703b705cfSriastradh
311803b705cfSriastradh	sf_state->thread0.grf_reg_count = GEN5_GRF_BLOCKS(SF_KERNEL_NUM_GRF);
311903b705cfSriastradh	sf_state->thread0.kernel_start_pointer = kernel >> 6;
312003b705cfSriastradh
312103b705cfSriastradh	sf_state->thread3.const_urb_entry_read_length = 0;	/* no const URBs */
312203b705cfSriastradh	sf_state->thread3.const_urb_entry_read_offset = 0;	/* no const URBs */
312303b705cfSriastradh	sf_state->thread3.urb_entry_read_length = 1;	/* 1 URB per vertex */
312403b705cfSriastradh	/* don't smash vertex header, read start from dw8 */
312503b705cfSriastradh	sf_state->thread3.urb_entry_read_offset = 1;
312603b705cfSriastradh	sf_state->thread3.dispatch_grf_start_reg = 3;
312703b705cfSriastradh	sf_state->thread4.max_threads = SF_MAX_THREADS - 1;
312803b705cfSriastradh	sf_state->thread4.urb_entry_allocation_size = URB_SF_ENTRY_SIZE - 1;
312903b705cfSriastradh	sf_state->thread4.nr_urb_entries = URB_SF_ENTRIES;
313003b705cfSriastradh	sf_state->sf5.viewport_transform = false;	/* skip viewport */
313103b705cfSriastradh	sf_state->sf6.cull_mode = GEN5_CULLMODE_NONE;
313203b705cfSriastradh	sf_state->sf6.scissor = 0;
313303b705cfSriastradh	sf_state->sf7.trifan_pv = 2;
313403b705cfSriastradh	sf_state->sf6.dest_org_vbias = 0x8;
313503b705cfSriastradh	sf_state->sf6.dest_org_hbias = 0x8;
313603b705cfSriastradh
313703b705cfSriastradh	return sna_static_stream_offsetof(stream, sf_state);
313803b705cfSriastradh}
313903b705cfSriastradh
314003b705cfSriastradhstatic uint32_t gen5_create_sampler_state(struct sna_static_stream *stream,
314103b705cfSriastradh					  sampler_filter_t src_filter,
314203b705cfSriastradh					  sampler_extend_t src_extend,
314303b705cfSriastradh					  sampler_filter_t mask_filter,
314403b705cfSriastradh					  sampler_extend_t mask_extend)
314503b705cfSriastradh{
314603b705cfSriastradh	struct gen5_sampler_state *sampler_state;
314703b705cfSriastradh
314803b705cfSriastradh	sampler_state = sna_static_stream_map(stream,
314903b705cfSriastradh					      sizeof(struct gen5_sampler_state) * 2,
315003b705cfSriastradh					      32);
315103b705cfSriastradh	sampler_state_init(&sampler_state[0], src_filter, src_extend);
315203b705cfSriastradh	sampler_state_init(&sampler_state[1], mask_filter, mask_extend);
315303b705cfSriastradh
315403b705cfSriastradh	return sna_static_stream_offsetof(stream, sampler_state);
315503b705cfSriastradh}
315603b705cfSriastradh
315703b705cfSriastradhstatic void gen5_init_wm_state(struct gen5_wm_unit_state *state,
315803b705cfSriastradh			       bool has_mask,
315903b705cfSriastradh			       uint32_t kernel,
316003b705cfSriastradh			       uint32_t sampler)
316103b705cfSriastradh{
316203b705cfSriastradh	state->thread0.grf_reg_count = GEN5_GRF_BLOCKS(PS_KERNEL_NUM_GRF);
316303b705cfSriastradh	state->thread0.kernel_start_pointer = kernel >> 6;
316403b705cfSriastradh
316503b705cfSriastradh	state->thread1.single_program_flow = 0;
316603b705cfSriastradh
316703b705cfSriastradh	/* scratch space is not used in our kernel */
316803b705cfSriastradh	state->thread2.scratch_space_base_pointer = 0;
316903b705cfSriastradh	state->thread2.per_thread_scratch_space = 0;
317003b705cfSriastradh
317103b705cfSriastradh	state->thread3.const_urb_entry_read_length = 0;
317203b705cfSriastradh	state->thread3.const_urb_entry_read_offset = 0;
317303b705cfSriastradh
317403b705cfSriastradh	state->thread3.urb_entry_read_offset = 0;
317503b705cfSriastradh	/* wm kernel use urb from 3, see wm_program in compiler module */
317603b705cfSriastradh	state->thread3.dispatch_grf_start_reg = 3;	/* must match kernel */
317703b705cfSriastradh
317803b705cfSriastradh	state->wm4.sampler_count = 0;	/* hardware requirement */
317903b705cfSriastradh
318003b705cfSriastradh	state->wm4.sampler_state_pointer = sampler >> 5;
318103b705cfSriastradh	state->wm5.max_threads = PS_MAX_THREADS - 1;
318203b705cfSriastradh	state->wm5.transposed_urb_read = 0;
318303b705cfSriastradh	state->wm5.thread_dispatch_enable = 1;
318403b705cfSriastradh	/* just use 16-pixel dispatch (4 subspans), don't need to change kernel
318503b705cfSriastradh	 * start point
318603b705cfSriastradh	 */
318703b705cfSriastradh	state->wm5.enable_16_pix = 1;
318803b705cfSriastradh	state->wm5.enable_8_pix = 0;
318903b705cfSriastradh	state->wm5.early_depth_test = 1;
319003b705cfSriastradh
319103b705cfSriastradh	/* Each pair of attributes (src/mask coords) is two URB entries */
319203b705cfSriastradh	if (has_mask) {
319303b705cfSriastradh		state->thread1.binding_table_entry_count = 3;	/* 2 tex and fb */
319403b705cfSriastradh		state->thread3.urb_entry_read_length = 4;
319503b705cfSriastradh	} else {
319603b705cfSriastradh		state->thread1.binding_table_entry_count = 2;	/* 1 tex and fb */
319703b705cfSriastradh		state->thread3.urb_entry_read_length = 2;
319803b705cfSriastradh	}
319903b705cfSriastradh
320003b705cfSriastradh	/* binding table entry count is only used for prefetching,
320103b705cfSriastradh	 * and it has to be set 0 for Ironlake
320203b705cfSriastradh	 */
320303b705cfSriastradh	state->thread1.binding_table_entry_count = 0;
320403b705cfSriastradh}
320503b705cfSriastradh
320603b705cfSriastradhstatic uint32_t gen5_create_cc_unit_state(struct sna_static_stream *stream)
320703b705cfSriastradh{
320803b705cfSriastradh	uint8_t *ptr, *base;
320903b705cfSriastradh	int i, j;
321003b705cfSriastradh
321103b705cfSriastradh	base = ptr =
321203b705cfSriastradh		sna_static_stream_map(stream,
321303b705cfSriastradh				      GEN5_BLENDFACTOR_COUNT*GEN5_BLENDFACTOR_COUNT*64,
321403b705cfSriastradh				      64);
321503b705cfSriastradh
321603b705cfSriastradh	for (i = 0; i < GEN5_BLENDFACTOR_COUNT; i++) {
321703b705cfSriastradh		for (j = 0; j < GEN5_BLENDFACTOR_COUNT; j++) {
321803b705cfSriastradh			struct gen5_cc_unit_state *state =
321903b705cfSriastradh				(struct gen5_cc_unit_state *)ptr;
322003b705cfSriastradh
322103b705cfSriastradh			state->cc3.blend_enable =
322203b705cfSriastradh				!(j == GEN5_BLENDFACTOR_ZERO && i == GEN5_BLENDFACTOR_ONE);
322303b705cfSriastradh
322403b705cfSriastradh			state->cc5.logicop_func = 0xc;	/* COPY */
322503b705cfSriastradh			state->cc5.ia_blend_function = GEN5_BLENDFUNCTION_ADD;
322603b705cfSriastradh
322703b705cfSriastradh			/* Fill in alpha blend factors same as color, for the future. */
322803b705cfSriastradh			state->cc5.ia_src_blend_factor = i;
322903b705cfSriastradh			state->cc5.ia_dest_blend_factor = j;
323003b705cfSriastradh
323103b705cfSriastradh			state->cc6.blend_function = GEN5_BLENDFUNCTION_ADD;
323203b705cfSriastradh			state->cc6.clamp_post_alpha_blend = 1;
323303b705cfSriastradh			state->cc6.clamp_pre_alpha_blend = 1;
323403b705cfSriastradh			state->cc6.src_blend_factor = i;
323503b705cfSriastradh			state->cc6.dest_blend_factor = j;
323603b705cfSriastradh
323703b705cfSriastradh			ptr += 64;
323803b705cfSriastradh		}
323903b705cfSriastradh	}
324003b705cfSriastradh
324103b705cfSriastradh	return sna_static_stream_offsetof(stream, base);
324203b705cfSriastradh}
324303b705cfSriastradh
324403b705cfSriastradhstatic bool gen5_render_setup(struct sna *sna)
324503b705cfSriastradh{
324603b705cfSriastradh	struct gen5_render_state *state = &sna->render_state.gen5;
324703b705cfSriastradh	struct sna_static_stream general;
324803b705cfSriastradh	struct gen5_wm_unit_state_padded *wm_state;
324903b705cfSriastradh	uint32_t sf[2], wm[KERNEL_COUNT];
325003b705cfSriastradh	int i, j, k, l, m;
325103b705cfSriastradh
325203b705cfSriastradh	sna_static_stream_init(&general);
325303b705cfSriastradh
325403b705cfSriastradh	/* Zero pad the start. If you see an offset of 0x0 in the batchbuffer
325503b705cfSriastradh	 * dumps, you know it points to zero.
325603b705cfSriastradh	 */
325703b705cfSriastradh	null_create(&general);
325803b705cfSriastradh
325903b705cfSriastradh	/* Set up the two SF states (one for blending with a mask, one without) */
326003b705cfSriastradh	sf[0] = sna_static_stream_compile_sf(sna, &general, brw_sf_kernel__nomask);
326103b705cfSriastradh	sf[1] = sna_static_stream_compile_sf(sna, &general, brw_sf_kernel__mask);
326203b705cfSriastradh
326303b705cfSriastradh	for (m = 0; m < KERNEL_COUNT; m++) {
326403b705cfSriastradh		if (wm_kernels[m].size) {
326503b705cfSriastradh			wm[m] = sna_static_stream_add(&general,
326603b705cfSriastradh						      wm_kernels[m].data,
326703b705cfSriastradh						      wm_kernels[m].size,
326803b705cfSriastradh						      64);
326903b705cfSriastradh		} else {
327003b705cfSriastradh			wm[m] = sna_static_stream_compile_wm(sna, &general,
327103b705cfSriastradh							     wm_kernels[m].data,
327203b705cfSriastradh							     16);
327303b705cfSriastradh		}
327403b705cfSriastradh		assert(wm[m]);
327503b705cfSriastradh	}
327603b705cfSriastradh
327703b705cfSriastradh	state->vs = gen5_create_vs_unit_state(&general);
327803b705cfSriastradh
327903b705cfSriastradh	state->sf[0] = gen5_create_sf_state(&general, sf[0]);
328003b705cfSriastradh	state->sf[1] = gen5_create_sf_state(&general, sf[1]);
328103b705cfSriastradh
328203b705cfSriastradh
328303b705cfSriastradh	/* Set up the WM states: each filter/extend type for source and mask, per
328403b705cfSriastradh	 * kernel.
328503b705cfSriastradh	 */
328603b705cfSriastradh	wm_state = sna_static_stream_map(&general,
328703b705cfSriastradh					  sizeof(*wm_state) * KERNEL_COUNT *
328803b705cfSriastradh					  FILTER_COUNT * EXTEND_COUNT *
328903b705cfSriastradh					  FILTER_COUNT * EXTEND_COUNT,
329003b705cfSriastradh					  64);
329103b705cfSriastradh	state->wm = sna_static_stream_offsetof(&general, wm_state);
329203b705cfSriastradh	for (i = 0; i < FILTER_COUNT; i++) {
329303b705cfSriastradh		for (j = 0; j < EXTEND_COUNT; j++) {
329403b705cfSriastradh			for (k = 0; k < FILTER_COUNT; k++) {
329503b705cfSriastradh				for (l = 0; l < EXTEND_COUNT; l++) {
329603b705cfSriastradh					uint32_t sampler_state;
329703b705cfSriastradh
329803b705cfSriastradh					sampler_state =
329903b705cfSriastradh						gen5_create_sampler_state(&general,
330003b705cfSriastradh									  i, j,
330103b705cfSriastradh									  k, l);
330203b705cfSriastradh
330303b705cfSriastradh					for (m = 0; m < KERNEL_COUNT; m++) {
330403b705cfSriastradh						gen5_init_wm_state(&wm_state->state,
330503b705cfSriastradh								   wm_kernels[m].has_mask,
330603b705cfSriastradh								   wm[m], sampler_state);
330703b705cfSriastradh						wm_state++;
330803b705cfSriastradh					}
330903b705cfSriastradh				}
331003b705cfSriastradh			}
331103b705cfSriastradh		}
331203b705cfSriastradh	}
331303b705cfSriastradh
331403b705cfSriastradh	state->cc = gen5_create_cc_unit_state(&general);
331503b705cfSriastradh
331603b705cfSriastradh	state->general_bo = sna_static_stream_fini(sna, &general);
331703b705cfSriastradh	return state->general_bo != NULL;
331803b705cfSriastradh}
331903b705cfSriastradh
332003b705cfSriastradhconst char *gen5_render_init(struct sna *sna, const char *backend)
332103b705cfSriastradh{
332203b705cfSriastradh	if (!gen5_render_setup(sna))
332303b705cfSriastradh		return backend;
332403b705cfSriastradh
332503b705cfSriastradh	sna->kgem.context_switch = gen5_render_context_switch;
332642542f5fSchristos	sna->kgem.retire = gen4_render_retire;
332742542f5fSchristos	sna->kgem.expire = gen4_render_expire;
332803b705cfSriastradh
332903b705cfSriastradh#if !NO_COMPOSITE
333003b705cfSriastradh	sna->render.composite = gen5_render_composite;
333103b705cfSriastradh	sna->render.prefer_gpu |= PREFER_GPU_RENDER;
333203b705cfSriastradh#endif
333303b705cfSriastradh#if !NO_COMPOSITE_SPANS
333403b705cfSriastradh	sna->render.check_composite_spans = gen5_check_composite_spans;
333503b705cfSriastradh	sna->render.composite_spans = gen5_render_composite_spans;
333642542f5fSchristos	if (intel_get_device_id(sna->scrn) == 0x0044)
333703b705cfSriastradh		sna->render.prefer_gpu |= PREFER_GPU_SPANS;
333803b705cfSriastradh#endif
333903b705cfSriastradh	sna->render.video = gen5_render_video;
334003b705cfSriastradh
334103b705cfSriastradh	sna->render.copy_boxes = gen5_render_copy_boxes;
334203b705cfSriastradh	sna->render.copy = gen5_render_copy;
334303b705cfSriastradh
334403b705cfSriastradh	sna->render.fill_boxes = gen5_render_fill_boxes;
334503b705cfSriastradh	sna->render.fill = gen5_render_fill;
334603b705cfSriastradh	sna->render.fill_one = gen5_render_fill_one;
334703b705cfSriastradh
334842542f5fSchristos	sna->render.flush = gen4_render_flush;
334903b705cfSriastradh	sna->render.reset = gen5_render_reset;
335003b705cfSriastradh	sna->render.fini = gen5_render_fini;
335103b705cfSriastradh
335203b705cfSriastradh	sna->render.max_3d_size = MAX_3D_SIZE;
335303b705cfSriastradh	sna->render.max_3d_pitch = 1 << 18;
335403b705cfSriastradh	return "Ironlake (gen5)";
335503b705cfSriastradh}
3356