gen4_render.c revision 13496ba1
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"
4442542f5fSchristos#include "gen4_common.h"
4503b705cfSriastradh#include "gen4_render.h"
4603b705cfSriastradh#include "gen4_source.h"
4703b705cfSriastradh#include "gen4_vertex.h"
4803b705cfSriastradh
4903b705cfSriastradh/* gen4 has a serious issue with its shaders that we need to flush
5003b705cfSriastradh * after every rectangle... So until that is resolved, prefer
5103b705cfSriastradh * the BLT engine.
5203b705cfSriastradh */
5303b705cfSriastradh#define FORCE_SPANS 0
5403b705cfSriastradh#define FORCE_NONRECTILINEAR_SPANS -1
5503b705cfSriastradh#define FORCE_FLUSH 1 /* https://bugs.freedesktop.org/show_bug.cgi?id=55500 */
5603b705cfSriastradh
5742542f5fSchristos#define ALWAYS_FLUSH 1
5842542f5fSchristos
5903b705cfSriastradh#define NO_COMPOSITE 0
6003b705cfSriastradh#define NO_COMPOSITE_SPANS 0
6103b705cfSriastradh#define NO_COPY 0
6203b705cfSriastradh#define NO_COPY_BOXES 0
6303b705cfSriastradh#define NO_FILL 0
6403b705cfSriastradh#define NO_FILL_ONE 0
6503b705cfSriastradh#define NO_FILL_BOXES 0
6603b705cfSriastradh#define NO_VIDEO 0
6703b705cfSriastradh
6842542f5fSchristos#define MAX_FLUSH_VERTICES 1 /* was 6, https://bugs.freedesktop.org/show_bug.cgi?id=55500 */
6903b705cfSriastradh
7003b705cfSriastradh#define GEN4_GRF_BLOCKS(nreg)    ((nreg + 15) / 16 - 1)
7103b705cfSriastradh
7203b705cfSriastradh/* Set up a default static partitioning of the URB, which is supposed to
7303b705cfSriastradh * allow anything we would want to do, at potentially lower performance.
7403b705cfSriastradh */
7503b705cfSriastradh#define URB_CS_ENTRY_SIZE     1
7603b705cfSriastradh#define URB_CS_ENTRIES	      0
7703b705cfSriastradh
7803b705cfSriastradh#define URB_VS_ENTRY_SIZE     1
7903b705cfSriastradh#define URB_VS_ENTRIES	      32
8003b705cfSriastradh
8103b705cfSriastradh#define URB_GS_ENTRY_SIZE     0
8203b705cfSriastradh#define URB_GS_ENTRIES	      0
8303b705cfSriastradh
8442542f5fSchristos#define URB_CL_ENTRY_SIZE   0
8542542f5fSchristos#define URB_CL_ENTRIES      0
8603b705cfSriastradh
8703b705cfSriastradh#define URB_SF_ENTRY_SIZE     2
8803b705cfSriastradh#define URB_SF_ENTRIES	      64
8903b705cfSriastradh
9003b705cfSriastradh/*
9103b705cfSriastradh * this program computes dA/dx and dA/dy for the texture coordinates along
9203b705cfSriastradh * with the base texture coordinate. It was extracted from the Mesa driver
9303b705cfSriastradh */
9403b705cfSriastradh
9503b705cfSriastradh#define SF_KERNEL_NUM_GRF 16
9603b705cfSriastradh#define PS_KERNEL_NUM_GRF 32
9703b705cfSriastradh
9803b705cfSriastradh#define GEN4_MAX_SF_THREADS 24
9903b705cfSriastradh#define GEN4_MAX_WM_THREADS 32
10003b705cfSriastradh#define G4X_MAX_WM_THREADS 50
10103b705cfSriastradh
10203b705cfSriastradhstatic const uint32_t ps_kernel_packed_static[][4] = {
10303b705cfSriastradh#include "exa_wm_xy.g4b"
10403b705cfSriastradh#include "exa_wm_src_affine.g4b"
10503b705cfSriastradh#include "exa_wm_src_sample_argb.g4b"
10603b705cfSriastradh#include "exa_wm_yuv_rgb.g4b"
10703b705cfSriastradh#include "exa_wm_write.g4b"
10803b705cfSriastradh};
10903b705cfSriastradh
11003b705cfSriastradhstatic const uint32_t ps_kernel_planar_static[][4] = {
11103b705cfSriastradh#include "exa_wm_xy.g4b"
11203b705cfSriastradh#include "exa_wm_src_affine.g4b"
11303b705cfSriastradh#include "exa_wm_src_sample_planar.g4b"
11403b705cfSriastradh#include "exa_wm_yuv_rgb.g4b"
11503b705cfSriastradh#include "exa_wm_write.g4b"
11603b705cfSriastradh};
11703b705cfSriastradh
11803b705cfSriastradh#define NOKERNEL(kernel_enum, func, masked) \
11903b705cfSriastradh    [kernel_enum] = {func, 0, masked}
12003b705cfSriastradh#define KERNEL(kernel_enum, kernel, masked) \
12103b705cfSriastradh    [kernel_enum] = {&kernel, sizeof(kernel), masked}
12203b705cfSriastradhstatic const struct wm_kernel_info {
12303b705cfSriastradh	const void *data;
12403b705cfSriastradh	unsigned int size;
12503b705cfSriastradh	bool has_mask;
12603b705cfSriastradh} wm_kernels[] = {
12703b705cfSriastradh	NOKERNEL(WM_KERNEL, brw_wm_kernel__affine, false),
12803b705cfSriastradh	NOKERNEL(WM_KERNEL_P, brw_wm_kernel__projective, false),
12903b705cfSriastradh
13003b705cfSriastradh	NOKERNEL(WM_KERNEL_MASK, brw_wm_kernel__affine_mask, true),
13103b705cfSriastradh	NOKERNEL(WM_KERNEL_MASK_P, brw_wm_kernel__projective_mask, true),
13203b705cfSriastradh
13303b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKCA, brw_wm_kernel__affine_mask_ca, true),
13403b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKCA_P, brw_wm_kernel__projective_mask_ca, true),
13503b705cfSriastradh
13603b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKSA, brw_wm_kernel__affine_mask_sa, true),
13703b705cfSriastradh	NOKERNEL(WM_KERNEL_MASKSA_P, brw_wm_kernel__projective_mask_sa, true),
13803b705cfSriastradh
13903b705cfSriastradh	NOKERNEL(WM_KERNEL_OPACITY, brw_wm_kernel__affine_opacity, true),
14003b705cfSriastradh	NOKERNEL(WM_KERNEL_OPACITY_P, brw_wm_kernel__projective_opacity, true),
14103b705cfSriastradh
14203b705cfSriastradh	KERNEL(WM_KERNEL_VIDEO_PLANAR, ps_kernel_planar_static, false),
14303b705cfSriastradh	KERNEL(WM_KERNEL_VIDEO_PACKED, ps_kernel_packed_static, false),
14403b705cfSriastradh};
14503b705cfSriastradh#undef KERNEL
14603b705cfSriastradh
14703b705cfSriastradhstatic const struct blendinfo {
14803b705cfSriastradh	bool src_alpha;
14903b705cfSriastradh	uint32_t src_blend;
15003b705cfSriastradh	uint32_t dst_blend;
15103b705cfSriastradh} gen4_blend_op[] = {
15203b705cfSriastradh	/* Clear */	{0, GEN4_BLENDFACTOR_ZERO, GEN4_BLENDFACTOR_ZERO},
15303b705cfSriastradh	/* Src */	{0, GEN4_BLENDFACTOR_ONE, GEN4_BLENDFACTOR_ZERO},
15403b705cfSriastradh	/* Dst */	{0, GEN4_BLENDFACTOR_ZERO, GEN4_BLENDFACTOR_ONE},
15503b705cfSriastradh	/* Over */	{1, GEN4_BLENDFACTOR_ONE, GEN4_BLENDFACTOR_INV_SRC_ALPHA},
15603b705cfSriastradh	/* OverReverse */ {0, GEN4_BLENDFACTOR_INV_DST_ALPHA, GEN4_BLENDFACTOR_ONE},
15703b705cfSriastradh	/* In */	{0, GEN4_BLENDFACTOR_DST_ALPHA, GEN4_BLENDFACTOR_ZERO},
15803b705cfSriastradh	/* InReverse */	{1, GEN4_BLENDFACTOR_ZERO, GEN4_BLENDFACTOR_SRC_ALPHA},
15903b705cfSriastradh	/* Out */	{0, GEN4_BLENDFACTOR_INV_DST_ALPHA, GEN4_BLENDFACTOR_ZERO},
16003b705cfSriastradh	/* OutReverse */ {1, GEN4_BLENDFACTOR_ZERO, GEN4_BLENDFACTOR_INV_SRC_ALPHA},
16103b705cfSriastradh	/* Atop */	{1, GEN4_BLENDFACTOR_DST_ALPHA, GEN4_BLENDFACTOR_INV_SRC_ALPHA},
16203b705cfSriastradh	/* AtopReverse */ {1, GEN4_BLENDFACTOR_INV_DST_ALPHA, GEN4_BLENDFACTOR_SRC_ALPHA},
16303b705cfSriastradh	/* Xor */	{1, GEN4_BLENDFACTOR_INV_DST_ALPHA, GEN4_BLENDFACTOR_INV_SRC_ALPHA},
16403b705cfSriastradh	/* Add */	{0, GEN4_BLENDFACTOR_ONE, GEN4_BLENDFACTOR_ONE},
16503b705cfSriastradh};
16603b705cfSriastradh
16703b705cfSriastradh/**
16803b705cfSriastradh * Highest-valued BLENDFACTOR used in gen4_blend_op.
16903b705cfSriastradh *
17003b705cfSriastradh * This leaves out GEN4_BLENDFACTOR_INV_DST_COLOR,
17103b705cfSriastradh * GEN4_BLENDFACTOR_INV_CONST_{COLOR,ALPHA},
17203b705cfSriastradh * GEN4_BLENDFACTOR_INV_SRC1_{COLOR,ALPHA}
17303b705cfSriastradh */
17403b705cfSriastradh#define GEN4_BLENDFACTOR_COUNT (GEN4_BLENDFACTOR_INV_DST_ALPHA + 1)
17503b705cfSriastradh
17603b705cfSriastradh#define BLEND_OFFSET(s, d) \
17703b705cfSriastradh	(((s) * GEN4_BLENDFACTOR_COUNT + (d)) * 64)
17803b705cfSriastradh
17903b705cfSriastradh#define SAMPLER_OFFSET(sf, se, mf, me, k) \
18003b705cfSriastradh	((((((sf) * EXTEND_COUNT + (se)) * FILTER_COUNT + (mf)) * EXTEND_COUNT + (me)) * KERNEL_COUNT + (k)) * 64)
18103b705cfSriastradh
18203b705cfSriastradhstatic void
18303b705cfSriastradhgen4_emit_pipelined_pointers(struct sna *sna,
18403b705cfSriastradh			     const struct sna_composite_op *op,
18503b705cfSriastradh			     int blend, int kernel);
18603b705cfSriastradh
18703b705cfSriastradh#define OUT_BATCH(v) batch_emit(sna, v)
18803b705cfSriastradh#define OUT_VERTEX(x,y) vertex_emit_2s(sna, x,y)
18903b705cfSriastradh#define OUT_VERTEX_F(v) vertex_emit(sna, v)
19003b705cfSriastradh
19103b705cfSriastradh#define GEN4_MAX_3D_SIZE 8192
19203b705cfSriastradh
19303b705cfSriastradhstatic inline bool too_large(int width, int height)
19403b705cfSriastradh{
19503b705cfSriastradh	return width > GEN4_MAX_3D_SIZE || height > GEN4_MAX_3D_SIZE;
19603b705cfSriastradh}
19703b705cfSriastradh
19803b705cfSriastradhstatic int
19903b705cfSriastradhgen4_choose_composite_kernel(int op, bool has_mask, bool is_ca, bool is_affine)
20003b705cfSriastradh{
20103b705cfSriastradh	int base;
20203b705cfSriastradh
20303b705cfSriastradh	if (has_mask) {
20403b705cfSriastradh		if (is_ca) {
20503b705cfSriastradh			if (gen4_blend_op[op].src_alpha)
20603b705cfSriastradh				base = WM_KERNEL_MASKSA;
20703b705cfSriastradh			else
20803b705cfSriastradh				base = WM_KERNEL_MASKCA;
20903b705cfSriastradh		} else
21003b705cfSriastradh			base = WM_KERNEL_MASK;
21103b705cfSriastradh	} else
21203b705cfSriastradh		base = WM_KERNEL;
21303b705cfSriastradh
21403b705cfSriastradh	return base + !is_affine;
21503b705cfSriastradh}
21603b705cfSriastradh
21703b705cfSriastradhstatic bool gen4_magic_ca_pass(struct sna *sna,
21803b705cfSriastradh			       const struct sna_composite_op *op)
21903b705cfSriastradh{
22003b705cfSriastradh	struct gen4_render_state *state = &sna->render_state.gen4;
22103b705cfSriastradh
22203b705cfSriastradh	if (!op->need_magic_ca_pass)
22303b705cfSriastradh		return false;
22403b705cfSriastradh
22503b705cfSriastradh	assert(sna->render.vertex_index > sna->render.vertex_start);
22603b705cfSriastradh
22703b705cfSriastradh	DBG(("%s: CA fixup\n", __FUNCTION__));
22803b705cfSriastradh	assert(op->mask.bo != NULL);
22903b705cfSriastradh	assert(op->has_component_alpha);
23003b705cfSriastradh
23103b705cfSriastradh	gen4_emit_pipelined_pointers(sna, op, PictOpAdd,
23203b705cfSriastradh				     gen4_choose_composite_kernel(PictOpAdd,
23303b705cfSriastradh								  true, true, op->is_affine));
23403b705cfSriastradh
23503b705cfSriastradh	OUT_BATCH(GEN4_3DPRIMITIVE |
23603b705cfSriastradh		  GEN4_3DPRIMITIVE_VERTEX_SEQUENTIAL |
23703b705cfSriastradh		  (_3DPRIM_RECTLIST << GEN4_3DPRIMITIVE_TOPOLOGY_SHIFT) |
23803b705cfSriastradh		  (0 << 9) |
23903b705cfSriastradh		  4);
24003b705cfSriastradh	OUT_BATCH(sna->render.vertex_index - sna->render.vertex_start);
24103b705cfSriastradh	OUT_BATCH(sna->render.vertex_start);
24203b705cfSriastradh	OUT_BATCH(1);	/* single instance */
24303b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
24403b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
24503b705cfSriastradh
24603b705cfSriastradh	state->last_primitive = sna->kgem.nbatch;
24703b705cfSriastradh	return true;
24803b705cfSriastradh}
24903b705cfSriastradh
25003b705cfSriastradhstatic uint32_t gen4_get_blend(int op,
25103b705cfSriastradh			       bool has_component_alpha,
25203b705cfSriastradh			       uint32_t dst_format)
25303b705cfSriastradh{
25403b705cfSriastradh	uint32_t src, dst;
25503b705cfSriastradh
25603b705cfSriastradh	src = gen4_blend_op[op].src_blend;
25703b705cfSriastradh	dst = gen4_blend_op[op].dst_blend;
25803b705cfSriastradh
25903b705cfSriastradh	/* If there's no dst alpha channel, adjust the blend op so that we'll treat
26003b705cfSriastradh	 * it as always 1.
26103b705cfSriastradh	 */
26203b705cfSriastradh	if (PICT_FORMAT_A(dst_format) == 0) {
26303b705cfSriastradh		if (src == GEN4_BLENDFACTOR_DST_ALPHA)
26403b705cfSriastradh			src = GEN4_BLENDFACTOR_ONE;
26503b705cfSriastradh		else if (src == GEN4_BLENDFACTOR_INV_DST_ALPHA)
26603b705cfSriastradh			src = GEN4_BLENDFACTOR_ZERO;
26703b705cfSriastradh	}
26803b705cfSriastradh
26903b705cfSriastradh	/* If the source alpha is being used, then we should only be in a
27003b705cfSriastradh	 * case where the source blend factor is 0, and the source blend
27103b705cfSriastradh	 * value is the mask channels multiplied by the source picture's alpha.
27203b705cfSriastradh	 */
27303b705cfSriastradh	if (has_component_alpha && gen4_blend_op[op].src_alpha) {
27403b705cfSriastradh		if (dst == GEN4_BLENDFACTOR_SRC_ALPHA)
27503b705cfSriastradh			dst = GEN4_BLENDFACTOR_SRC_COLOR;
27603b705cfSriastradh		else if (dst == GEN4_BLENDFACTOR_INV_SRC_ALPHA)
27703b705cfSriastradh			dst = GEN4_BLENDFACTOR_INV_SRC_COLOR;
27803b705cfSriastradh	}
27903b705cfSriastradh
28003b705cfSriastradh	DBG(("blend op=%d, dst=%x [A=%d] => src=%d, dst=%d => offset=%x\n",
28103b705cfSriastradh	     op, dst_format, PICT_FORMAT_A(dst_format),
28203b705cfSriastradh	     src, dst, BLEND_OFFSET(src, dst)));
28303b705cfSriastradh	return BLEND_OFFSET(src, dst);
28403b705cfSriastradh}
28503b705cfSriastradh
28603b705cfSriastradhstatic uint32_t gen4_get_card_format(PictFormat format)
28703b705cfSriastradh{
28803b705cfSriastradh	switch (format) {
28903b705cfSriastradh	default:
29003b705cfSriastradh		return -1;
29103b705cfSriastradh	case PICT_a8r8g8b8:
29203b705cfSriastradh		return GEN4_SURFACEFORMAT_B8G8R8A8_UNORM;
29303b705cfSriastradh	case PICT_x8r8g8b8:
29403b705cfSriastradh		return GEN4_SURFACEFORMAT_B8G8R8X8_UNORM;
29503b705cfSriastradh	case PICT_a8b8g8r8:
29603b705cfSriastradh		return GEN4_SURFACEFORMAT_R8G8B8A8_UNORM;
29703b705cfSriastradh	case PICT_x8b8g8r8:
29803b705cfSriastradh		return GEN4_SURFACEFORMAT_R8G8B8X8_UNORM;
29942542f5fSchristos#ifdef PICT_a2r10g10b10
30003b705cfSriastradh	case PICT_a2r10g10b10:
30103b705cfSriastradh		return GEN4_SURFACEFORMAT_B10G10R10A2_UNORM;
30203b705cfSriastradh	case PICT_x2r10g10b10:
30303b705cfSriastradh		return GEN4_SURFACEFORMAT_B10G10R10X2_UNORM;
30442542f5fSchristos#endif
30503b705cfSriastradh	case PICT_r8g8b8:
30603b705cfSriastradh		return GEN4_SURFACEFORMAT_R8G8B8_UNORM;
30703b705cfSriastradh	case PICT_r5g6b5:
30803b705cfSriastradh		return GEN4_SURFACEFORMAT_B5G6R5_UNORM;
30903b705cfSriastradh	case PICT_a1r5g5b5:
31003b705cfSriastradh		return GEN4_SURFACEFORMAT_B5G5R5A1_UNORM;
31103b705cfSriastradh	case PICT_a8:
31203b705cfSriastradh		return GEN4_SURFACEFORMAT_A8_UNORM;
31303b705cfSriastradh	case PICT_a4r4g4b4:
31403b705cfSriastradh		return GEN4_SURFACEFORMAT_B4G4R4A4_UNORM;
31503b705cfSriastradh	}
31603b705cfSriastradh}
31703b705cfSriastradh
31803b705cfSriastradhstatic uint32_t gen4_get_dest_format(PictFormat format)
31903b705cfSriastradh{
32003b705cfSriastradh	switch (format) {
32103b705cfSriastradh	default:
32203b705cfSriastradh		return -1;
32303b705cfSriastradh	case PICT_a8r8g8b8:
32403b705cfSriastradh	case PICT_x8r8g8b8:
32503b705cfSriastradh		return GEN4_SURFACEFORMAT_B8G8R8A8_UNORM;
32603b705cfSriastradh	case PICT_a8b8g8r8:
32703b705cfSriastradh	case PICT_x8b8g8r8:
32803b705cfSriastradh		return GEN4_SURFACEFORMAT_R8G8B8A8_UNORM;
32942542f5fSchristos#ifdef PICT_a2r10g10b10
33003b705cfSriastradh	case PICT_a2r10g10b10:
33103b705cfSriastradh	case PICT_x2r10g10b10:
33203b705cfSriastradh		return GEN4_SURFACEFORMAT_B10G10R10A2_UNORM;
33342542f5fSchristos#endif
33403b705cfSriastradh	case PICT_r5g6b5:
33503b705cfSriastradh		return GEN4_SURFACEFORMAT_B5G6R5_UNORM;
33603b705cfSriastradh	case PICT_x1r5g5b5:
33703b705cfSriastradh	case PICT_a1r5g5b5:
33803b705cfSriastradh		return GEN4_SURFACEFORMAT_B5G5R5A1_UNORM;
33903b705cfSriastradh	case PICT_a8:
34003b705cfSriastradh		return GEN4_SURFACEFORMAT_A8_UNORM;
34103b705cfSriastradh	case PICT_a4r4g4b4:
34203b705cfSriastradh	case PICT_x4r4g4b4:
34303b705cfSriastradh		return GEN4_SURFACEFORMAT_B4G4R4A4_UNORM;
34403b705cfSriastradh	}
34503b705cfSriastradh}
34603b705cfSriastradh
34703b705cfSriastradhstatic bool gen4_check_dst_format(PictFormat format)
34803b705cfSriastradh{
34903b705cfSriastradh	if (gen4_get_dest_format(format) != -1)
35003b705cfSriastradh		return true;
35103b705cfSriastradh
35203b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
35303b705cfSriastradh	return false;
35403b705cfSriastradh}
35503b705cfSriastradh
35603b705cfSriastradhstatic bool gen4_check_format(uint32_t format)
35703b705cfSriastradh{
35803b705cfSriastradh	if (gen4_get_card_format(format) != -1)
35903b705cfSriastradh		return true;
36003b705cfSriastradh
36103b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
36203b705cfSriastradh	return false;
36303b705cfSriastradh}
36403b705cfSriastradh
36503b705cfSriastradhtypedef struct gen4_surface_state_padded {
36603b705cfSriastradh	struct gen4_surface_state state;
36703b705cfSriastradh	char pad[32 - sizeof(struct gen4_surface_state)];
36803b705cfSriastradh} gen4_surface_state_padded;
36903b705cfSriastradh
37003b705cfSriastradhstatic void null_create(struct sna_static_stream *stream)
37103b705cfSriastradh{
37203b705cfSriastradh	/* A bunch of zeros useful for legacy border color and depth-stencil */
37303b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
37403b705cfSriastradh}
37503b705cfSriastradh
37603b705cfSriastradhstatic void
37703b705cfSriastradhsampler_state_init(struct gen4_sampler_state *sampler_state,
37803b705cfSriastradh		   sampler_filter_t filter,
37903b705cfSriastradh		   sampler_extend_t extend)
38003b705cfSriastradh{
38103b705cfSriastradh	sampler_state->ss0.lod_preclamp = 1;	/* GL mode */
38203b705cfSriastradh
38303b705cfSriastradh	/* We use the legacy mode to get the semantics specified by
38403b705cfSriastradh	 * the Render extension. */
38503b705cfSriastradh	sampler_state->ss0.border_color_mode = GEN4_BORDER_COLOR_MODE_LEGACY;
38603b705cfSriastradh
38703b705cfSriastradh	switch (filter) {
38803b705cfSriastradh	default:
38903b705cfSriastradh	case SAMPLER_FILTER_NEAREST:
39003b705cfSriastradh		sampler_state->ss0.min_filter = GEN4_MAPFILTER_NEAREST;
39103b705cfSriastradh		sampler_state->ss0.mag_filter = GEN4_MAPFILTER_NEAREST;
39203b705cfSriastradh		break;
39303b705cfSriastradh	case SAMPLER_FILTER_BILINEAR:
39403b705cfSriastradh		sampler_state->ss0.min_filter = GEN4_MAPFILTER_LINEAR;
39503b705cfSriastradh		sampler_state->ss0.mag_filter = GEN4_MAPFILTER_LINEAR;
39603b705cfSriastradh		break;
39703b705cfSriastradh	}
39803b705cfSriastradh
39903b705cfSriastradh	switch (extend) {
40003b705cfSriastradh	default:
40103b705cfSriastradh	case SAMPLER_EXTEND_NONE:
40203b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN4_TEXCOORDMODE_CLAMP_BORDER;
40303b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN4_TEXCOORDMODE_CLAMP_BORDER;
40403b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN4_TEXCOORDMODE_CLAMP_BORDER;
40503b705cfSriastradh		break;
40603b705cfSriastradh	case SAMPLER_EXTEND_REPEAT:
40703b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN4_TEXCOORDMODE_WRAP;
40803b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN4_TEXCOORDMODE_WRAP;
40903b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN4_TEXCOORDMODE_WRAP;
41003b705cfSriastradh		break;
41103b705cfSriastradh	case SAMPLER_EXTEND_PAD:
41203b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN4_TEXCOORDMODE_CLAMP;
41303b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN4_TEXCOORDMODE_CLAMP;
41403b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN4_TEXCOORDMODE_CLAMP;
41503b705cfSriastradh		break;
41603b705cfSriastradh	case SAMPLER_EXTEND_REFLECT:
41703b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN4_TEXCOORDMODE_MIRROR;
41803b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN4_TEXCOORDMODE_MIRROR;
41903b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN4_TEXCOORDMODE_MIRROR;
42003b705cfSriastradh		break;
42103b705cfSriastradh	}
42203b705cfSriastradh}
42303b705cfSriastradh
42403b705cfSriastradhstatic uint32_t gen4_filter(uint32_t filter)
42503b705cfSriastradh{
42603b705cfSriastradh	switch (filter) {
42703b705cfSriastradh	default:
42803b705cfSriastradh		assert(0);
42903b705cfSriastradh	case PictFilterNearest:
43003b705cfSriastradh		return SAMPLER_FILTER_NEAREST;
43103b705cfSriastradh	case PictFilterBilinear:
43203b705cfSriastradh		return SAMPLER_FILTER_BILINEAR;
43303b705cfSriastradh	}
43403b705cfSriastradh}
43503b705cfSriastradh
43603b705cfSriastradhstatic uint32_t gen4_check_filter(PicturePtr picture)
43703b705cfSriastradh{
43803b705cfSriastradh	switch (picture->filter) {
43903b705cfSriastradh	case PictFilterNearest:
44003b705cfSriastradh	case PictFilterBilinear:
44103b705cfSriastradh		return true;
44203b705cfSriastradh	default:
44303b705cfSriastradh		DBG(("%s: unknown filter: %s [%d]\n",
44403b705cfSriastradh		     __FUNCTION__,
44503b705cfSriastradh		     PictureGetFilterName(picture->filter),
44603b705cfSriastradh		     picture->filter));
44703b705cfSriastradh		return false;
44803b705cfSriastradh	}
44903b705cfSriastradh}
45003b705cfSriastradh
45103b705cfSriastradhstatic uint32_t gen4_repeat(uint32_t repeat)
45203b705cfSriastradh{
45303b705cfSriastradh	switch (repeat) {
45403b705cfSriastradh	default:
45503b705cfSriastradh		assert(0);
45603b705cfSriastradh	case RepeatNone:
45703b705cfSriastradh		return SAMPLER_EXTEND_NONE;
45803b705cfSriastradh	case RepeatNormal:
45903b705cfSriastradh		return SAMPLER_EXTEND_REPEAT;
46003b705cfSriastradh	case RepeatPad:
46103b705cfSriastradh		return SAMPLER_EXTEND_PAD;
46203b705cfSriastradh	case RepeatReflect:
46303b705cfSriastradh		return SAMPLER_EXTEND_REFLECT;
46403b705cfSriastradh	}
46503b705cfSriastradh}
46603b705cfSriastradh
46703b705cfSriastradhstatic bool gen4_check_repeat(PicturePtr picture)
46803b705cfSriastradh{
46903b705cfSriastradh	if (!picture->repeat)
47003b705cfSriastradh		return true;
47103b705cfSriastradh
47203b705cfSriastradh	switch (picture->repeatType) {
47303b705cfSriastradh	case RepeatNone:
47403b705cfSriastradh	case RepeatNormal:
47503b705cfSriastradh	case RepeatPad:
47603b705cfSriastradh	case RepeatReflect:
47703b705cfSriastradh		return true;
47803b705cfSriastradh	default:
47903b705cfSriastradh		DBG(("%s: unknown repeat: %d\n",
48003b705cfSriastradh		     __FUNCTION__, picture->repeatType));
48103b705cfSriastradh		return false;
48203b705cfSriastradh	}
48303b705cfSriastradh}
48403b705cfSriastradh
48503b705cfSriastradhstatic uint32_t
48603b705cfSriastradhgen4_tiling_bits(uint32_t tiling)
48703b705cfSriastradh{
48803b705cfSriastradh	switch (tiling) {
48903b705cfSriastradh	default: assert(0);
49003b705cfSriastradh	case I915_TILING_NONE: return 0;
49103b705cfSriastradh	case I915_TILING_X: return GEN4_SURFACE_TILED;
49203b705cfSriastradh	case I915_TILING_Y: return GEN4_SURFACE_TILED | GEN4_SURFACE_TILED_Y;
49303b705cfSriastradh	}
49403b705cfSriastradh}
49503b705cfSriastradh
49603b705cfSriastradh/**
49703b705cfSriastradh * Sets up the common fields for a surface state buffer for the given
49803b705cfSriastradh * picture in the given surface state buffer.
49903b705cfSriastradh */
50003b705cfSriastradhstatic uint32_t
50103b705cfSriastradhgen4_bind_bo(struct sna *sna,
50203b705cfSriastradh	     struct kgem_bo *bo,
50303b705cfSriastradh	     uint32_t width,
50403b705cfSriastradh	     uint32_t height,
50503b705cfSriastradh	     uint32_t format,
50603b705cfSriastradh	     bool is_dst)
50703b705cfSriastradh{
50803b705cfSriastradh	uint32_t domains;
50903b705cfSriastradh	uint16_t offset;
51003b705cfSriastradh	uint32_t *ss;
51103b705cfSriastradh
51203b705cfSriastradh	assert(sna->kgem.gen != 040 || !kgem_bo_is_snoop(bo));
51303b705cfSriastradh
51403b705cfSriastradh	/* After the first bind, we manage the cache domains within the batch */
51503b705cfSriastradh	offset = kgem_bo_get_binding(bo, format | is_dst << 31);
51603b705cfSriastradh	if (offset) {
51742542f5fSchristos		assert(offset >= sna->kgem.surface);
51803b705cfSriastradh		if (is_dst)
51903b705cfSriastradh			kgem_bo_mark_dirty(bo);
52003b705cfSriastradh		return offset * sizeof(uint32_t);
52103b705cfSriastradh	}
52203b705cfSriastradh
52303b705cfSriastradh	offset = sna->kgem.surface -=
52403b705cfSriastradh		sizeof(struct gen4_surface_state_padded) / sizeof(uint32_t);
52503b705cfSriastradh	ss = sna->kgem.batch + offset;
52603b705cfSriastradh
52703b705cfSriastradh	ss[0] = (GEN4_SURFACE_2D << GEN4_SURFACE_TYPE_SHIFT |
52803b705cfSriastradh		 GEN4_SURFACE_BLEND_ENABLED |
52903b705cfSriastradh		 format << GEN4_SURFACE_FORMAT_SHIFT);
53003b705cfSriastradh
53103b705cfSriastradh	if (is_dst) {
53203b705cfSriastradh		ss[0] |= GEN4_SURFACE_RC_READ_WRITE;
53303b705cfSriastradh		domains = I915_GEM_DOMAIN_RENDER << 16 | I915_GEM_DOMAIN_RENDER;
53403b705cfSriastradh	} else
53503b705cfSriastradh		domains = I915_GEM_DOMAIN_SAMPLER << 16;
53603b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, offset + 1, bo, domains, 0);
53703b705cfSriastradh
53803b705cfSriastradh	ss[2] = ((width - 1)  << GEN4_SURFACE_WIDTH_SHIFT |
53903b705cfSriastradh		 (height - 1) << GEN4_SURFACE_HEIGHT_SHIFT);
54003b705cfSriastradh	ss[3] = (gen4_tiling_bits(bo->tiling) |
54103b705cfSriastradh		 (bo->pitch - 1) << GEN4_SURFACE_PITCH_SHIFT);
54203b705cfSriastradh	ss[4] = 0;
54303b705cfSriastradh	ss[5] = 0;
54403b705cfSriastradh
54503b705cfSriastradh	kgem_bo_set_binding(bo, format | is_dst << 31, offset);
54603b705cfSriastradh
54703b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, tiling=%d -> %s\n",
54803b705cfSriastradh	     offset, bo->handle, ss[1],
54903b705cfSriastradh	     format, width, height, bo->pitch, bo->tiling,
55003b705cfSriastradh	     domains & 0xffff ? "render" : "sampler"));
55103b705cfSriastradh
55203b705cfSriastradh	return offset * sizeof(uint32_t);
55303b705cfSriastradh}
55403b705cfSriastradh
55503b705cfSriastradhstatic void gen4_emit_vertex_buffer(struct sna *sna,
55603b705cfSriastradh				    const struct sna_composite_op *op)
55703b705cfSriastradh{
55803b705cfSriastradh	int id = op->u.gen4.ve_id;
55903b705cfSriastradh
56003b705cfSriastradh	assert((sna->render.vb_id & (1 << id)) == 0);
56103b705cfSriastradh
56203b705cfSriastradh	OUT_BATCH(GEN4_3DSTATE_VERTEX_BUFFERS | 3);
56303b705cfSriastradh	OUT_BATCH((id << VB0_BUFFER_INDEX_SHIFT) | VB0_VERTEXDATA |
56403b705cfSriastradh		  (4*op->floats_per_vertex << VB0_BUFFER_PITCH_SHIFT));
56503b705cfSriastradh	assert(sna->render.nvertex_reloc < ARRAY_SIZE(sna->render.vertex_reloc));
56603b705cfSriastradh	sna->render.vertex_reloc[sna->render.nvertex_reloc++] = sna->kgem.nbatch;
56703b705cfSriastradh	OUT_BATCH(0);
56803b705cfSriastradh	OUT_BATCH(0);
56903b705cfSriastradh	OUT_BATCH(0);
57003b705cfSriastradh
57103b705cfSriastradh	sna->render.vb_id |= 1 << id;
57203b705cfSriastradh}
57303b705cfSriastradh
57442542f5fSchristosinline static void
57542542f5fSchristosgen4_emit_pipe_flush(struct sna *sna)
57642542f5fSchristos{
57742542f5fSchristos#if 1
57842542f5fSchristos	OUT_BATCH(GEN4_PIPE_CONTROL |
57942542f5fSchristos		  GEN4_PIPE_CONTROL_WC_FLUSH |
58042542f5fSchristos		  (4 - 2));
58142542f5fSchristos	OUT_BATCH(0);
58242542f5fSchristos	OUT_BATCH(0);
58342542f5fSchristos	OUT_BATCH(0);
58442542f5fSchristos#else
58542542f5fSchristos	OUT_BATCH(MI_FLUSH | MI_INHIBIT_RENDER_CACHE_FLUSH);
58642542f5fSchristos#endif
58742542f5fSchristos}
58842542f5fSchristos
58942542f5fSchristosinline static void
59042542f5fSchristosgen4_emit_pipe_break(struct sna *sna)
59142542f5fSchristos{
59242542f5fSchristos#if !ALWAYS_FLUSH
59342542f5fSchristos	OUT_BATCH(GEN4_PIPE_CONTROL | (4 - 2));
59442542f5fSchristos	OUT_BATCH(0);
59542542f5fSchristos	OUT_BATCH(0);
59642542f5fSchristos	OUT_BATCH(0);
59742542f5fSchristos#else
59842542f5fSchristos	OUT_BATCH(MI_FLUSH | MI_INHIBIT_RENDER_CACHE_FLUSH);
59942542f5fSchristos#endif
60042542f5fSchristos}
60142542f5fSchristos
60242542f5fSchristosinline static void
60342542f5fSchristosgen4_emit_pipe_invalidate(struct sna *sna)
60442542f5fSchristos{
60542542f5fSchristos#if 0
60642542f5fSchristos	OUT_BATCH(GEN4_PIPE_CONTROL |
60742542f5fSchristos		  GEN4_PIPE_CONTROL_WC_FLUSH |
60842542f5fSchristos		  (sna->kgem.gen >= 045 ? GEN4_PIPE_CONTROL_TC_FLUSH : 0) |
60942542f5fSchristos		  (4 - 2));
61042542f5fSchristos	OUT_BATCH(0);
61142542f5fSchristos	OUT_BATCH(0);
61242542f5fSchristos	OUT_BATCH(0);
61342542f5fSchristos#else
61442542f5fSchristos	OUT_BATCH(MI_FLUSH);
61542542f5fSchristos#endif
61642542f5fSchristos}
61742542f5fSchristos
61803b705cfSriastradhstatic void gen4_emit_primitive(struct sna *sna)
61903b705cfSriastradh{
62003b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen4.last_primitive) {
62103b705cfSriastradh		sna->render.vertex_offset = sna->kgem.nbatch - 5;
62203b705cfSriastradh		return;
62303b705cfSriastradh	}
62403b705cfSriastradh
62503b705cfSriastradh	OUT_BATCH(GEN4_3DPRIMITIVE |
62603b705cfSriastradh		  GEN4_3DPRIMITIVE_VERTEX_SEQUENTIAL |
62703b705cfSriastradh		  (_3DPRIM_RECTLIST << GEN4_3DPRIMITIVE_TOPOLOGY_SHIFT) |
62803b705cfSriastradh		  (0 << 9) |
62903b705cfSriastradh		  4);
63003b705cfSriastradh	sna->render.vertex_offset = sna->kgem.nbatch;
63103b705cfSriastradh	OUT_BATCH(0);	/* vertex count, to be filled in later */
63203b705cfSriastradh	OUT_BATCH(sna->render.vertex_index);
63303b705cfSriastradh	OUT_BATCH(1);	/* single instance */
63403b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
63503b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
63603b705cfSriastradh	sna->render.vertex_start = sna->render.vertex_index;
63703b705cfSriastradh
63803b705cfSriastradh	sna->render_state.gen4.last_primitive = sna->kgem.nbatch;
63903b705cfSriastradh}
64003b705cfSriastradh
64103b705cfSriastradhstatic bool gen4_rectangle_begin(struct sna *sna,
64203b705cfSriastradh				 const struct sna_composite_op *op)
64303b705cfSriastradh{
64403b705cfSriastradh	unsigned int id = 1 << op->u.gen4.ve_id;
64503b705cfSriastradh	int ndwords;
64603b705cfSriastradh
64703b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render) && sna->render.vertex_offset)
64803b705cfSriastradh		return true;
64903b705cfSriastradh
65003b705cfSriastradh	/* 7xpipelined pointers + 6xprimitive + 1xflush */
65142542f5fSchristos	ndwords = op->need_magic_ca_pass? 19 : 6;
65203b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
65303b705cfSriastradh		ndwords += 5;
65442542f5fSchristos	ndwords += 8*FORCE_FLUSH;
65503b705cfSriastradh
65603b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, ndwords))
65703b705cfSriastradh		return false;
65803b705cfSriastradh
65903b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
66003b705cfSriastradh		gen4_emit_vertex_buffer(sna, op);
66103b705cfSriastradh	if (sna->render.vertex_offset == 0)
66203b705cfSriastradh		gen4_emit_primitive(sna);
66303b705cfSriastradh
66403b705cfSriastradh	return true;
66503b705cfSriastradh}
66603b705cfSriastradh
66703b705cfSriastradhstatic int gen4_get_rectangles__flush(struct sna *sna,
66803b705cfSriastradh				      const struct sna_composite_op *op)
66903b705cfSriastradh{
67003b705cfSriastradh	/* Preventing discarding new vbo after lock contention */
67103b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render)) {
67203b705cfSriastradh		int rem = vertex_space(sna);
67303b705cfSriastradh		if (rem > op->floats_per_rect)
67403b705cfSriastradh			return rem;
67503b705cfSriastradh	}
67603b705cfSriastradh
67703b705cfSriastradh	if (!kgem_check_batch(&sna->kgem,
67842542f5fSchristos			      8*FORCE_FLUSH + (op->need_magic_ca_pass ? 2*19+6 : 6)))
67903b705cfSriastradh		return 0;
68003b705cfSriastradh	if (!kgem_check_reloc_and_exec(&sna->kgem, 2))
68103b705cfSriastradh		return 0;
68203b705cfSriastradh
68303b705cfSriastradh	if (sna->render.vertex_offset) {
68403b705cfSriastradh		gen4_vertex_flush(sna);
68503b705cfSriastradh		if (gen4_magic_ca_pass(sna, op))
68603b705cfSriastradh			gen4_emit_pipelined_pointers(sna, op, op->op,
68703b705cfSriastradh						     op->u.gen4.wm_kernel);
68803b705cfSriastradh	}
68903b705cfSriastradh
69003b705cfSriastradh	return gen4_vertex_finish(sna);
69103b705cfSriastradh}
69203b705cfSriastradh
69303b705cfSriastradhinline static int gen4_get_rectangles(struct sna *sna,
69403b705cfSriastradh				      const struct sna_composite_op *op,
69503b705cfSriastradh				      int want,
69603b705cfSriastradh				      void (*emit_state)(struct sna *sna, const struct sna_composite_op *op))
69703b705cfSriastradh{
69803b705cfSriastradh	int rem;
69903b705cfSriastradh
70003b705cfSriastradh	assert(want);
70103b705cfSriastradh#if FORCE_FLUSH
70203b705cfSriastradh	rem = sna->render.vertex_offset;
70303b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen4.last_primitive)
70403b705cfSriastradh		rem = sna->kgem.nbatch - 5;
70503b705cfSriastradh	if (rem) {
70603b705cfSriastradh		rem = MAX_FLUSH_VERTICES - (sna->render.vertex_index - sna->render.vertex_start) / 3;
70703b705cfSriastradh		if (rem <= 0) {
70803b705cfSriastradh			if (sna->render.vertex_offset) {
70903b705cfSriastradh				gen4_vertex_flush(sna);
71042542f5fSchristos				if (gen4_magic_ca_pass(sna, op)) {
71142542f5fSchristos					if (kgem_check_batch(&sna->kgem, 19+6))
71242542f5fSchristos						gen4_emit_pipelined_pointers(sna, op, op->op,
71342542f5fSchristos									     op->u.gen4.wm_kernel);
71442542f5fSchristos				}
71503b705cfSriastradh			}
71642542f5fSchristos			gen4_emit_pipe_break(sna);
71703b705cfSriastradh			rem = MAX_FLUSH_VERTICES;
71803b705cfSriastradh		}
71903b705cfSriastradh	} else
72003b705cfSriastradh		rem = MAX_FLUSH_VERTICES;
72103b705cfSriastradh	if (want > rem)
72203b705cfSriastradh		want = rem;
72303b705cfSriastradh#endif
72403b705cfSriastradh
72503b705cfSriastradhstart:
72603b705cfSriastradh	rem = vertex_space(sna);
72703b705cfSriastradh	if (unlikely(rem < op->floats_per_rect)) {
72803b705cfSriastradh		DBG(("flushing vbo for %s: %d < %d\n",
72903b705cfSriastradh		     __FUNCTION__, rem, op->floats_per_rect));
73003b705cfSriastradh		rem = gen4_get_rectangles__flush(sna, op);
73103b705cfSriastradh		if (unlikely(rem == 0))
73203b705cfSriastradh			goto flush;
73303b705cfSriastradh	}
73403b705cfSriastradh
73503b705cfSriastradh	if (unlikely(sna->render.vertex_offset == 0)) {
73603b705cfSriastradh		if (!gen4_rectangle_begin(sna, op))
73703b705cfSriastradh			goto flush;
73803b705cfSriastradh		else
73903b705cfSriastradh			goto start;
74003b705cfSriastradh	}
74103b705cfSriastradh
74203b705cfSriastradh	assert(rem <= vertex_space(sna));
74303b705cfSriastradh	assert(op->floats_per_rect <= rem);
74403b705cfSriastradh	if (want > 1 && want * op->floats_per_rect > rem)
74503b705cfSriastradh		want = rem / op->floats_per_rect;
74603b705cfSriastradh
74703b705cfSriastradh	sna->render.vertex_index += 3*want;
74803b705cfSriastradh	return want;
74903b705cfSriastradh
75003b705cfSriastradhflush:
75103b705cfSriastradh	if (sna->render.vertex_offset) {
75203b705cfSriastradh		gen4_vertex_flush(sna);
75303b705cfSriastradh		gen4_magic_ca_pass(sna, op);
75403b705cfSriastradh	}
75503b705cfSriastradh	sna_vertex_wait__locked(&sna->render);
75603b705cfSriastradh	_kgem_submit(&sna->kgem);
75703b705cfSriastradh	emit_state(sna, op);
75803b705cfSriastradh	goto start;
75903b705cfSriastradh}
76003b705cfSriastradh
76103b705cfSriastradhstatic uint32_t *
76203b705cfSriastradhgen4_composite_get_binding_table(struct sna *sna, uint16_t *offset)
76303b705cfSriastradh{
76403b705cfSriastradh	sna->kgem.surface -=
76503b705cfSriastradh		sizeof(struct gen4_surface_state_padded) / sizeof(uint32_t);
76603b705cfSriastradh
76703b705cfSriastradh	DBG(("%s(%x)\n", __FUNCTION__, 4*sna->kgem.surface));
76803b705cfSriastradh
76903b705cfSriastradh	/* Clear all surplus entries to zero in case of prefetch */
77003b705cfSriastradh	*offset = sna->kgem.surface;
77103b705cfSriastradh	return memset(sna->kgem.batch + sna->kgem.surface,
77203b705cfSriastradh		      0, sizeof(struct gen4_surface_state_padded));
77303b705cfSriastradh}
77403b705cfSriastradh
77503b705cfSriastradhstatic void
77603b705cfSriastradhgen4_emit_urb(struct sna *sna)
77703b705cfSriastradh{
77842542f5fSchristos	int urb_vs_end;
77942542f5fSchristos	int urb_gs_end;
78042542f5fSchristos	int urb_cl_end;
78142542f5fSchristos	int urb_sf_end;
78242542f5fSchristos	int urb_cs_end;
78303b705cfSriastradh
78403b705cfSriastradh	if (!sna->render_state.gen4.needs_urb)
78503b705cfSriastradh		return;
78603b705cfSriastradh
78742542f5fSchristos	urb_vs_end =              URB_VS_ENTRIES * URB_VS_ENTRY_SIZE;
78842542f5fSchristos	urb_gs_end = urb_vs_end + URB_GS_ENTRIES * URB_GS_ENTRY_SIZE;
78942542f5fSchristos	urb_cl_end = urb_gs_end + URB_CL_ENTRIES * URB_CL_ENTRY_SIZE;
79042542f5fSchristos	urb_sf_end = urb_cl_end + URB_SF_ENTRIES * URB_SF_ENTRY_SIZE;
79142542f5fSchristos	urb_cs_end = urb_sf_end + URB_CS_ENTRIES * URB_CS_ENTRY_SIZE;
79242542f5fSchristos	assert(urb_cs_end <= 256);
79303b705cfSriastradh
79403b705cfSriastradh	while ((sna->kgem.nbatch & 15) > 12)
79503b705cfSriastradh		OUT_BATCH(MI_NOOP);
79603b705cfSriastradh
79703b705cfSriastradh	OUT_BATCH(GEN4_URB_FENCE |
79803b705cfSriastradh		  UF0_CS_REALLOC |
79903b705cfSriastradh		  UF0_SF_REALLOC |
80003b705cfSriastradh		  UF0_CLIP_REALLOC |
80103b705cfSriastradh		  UF0_GS_REALLOC |
80203b705cfSriastradh		  UF0_VS_REALLOC |
80303b705cfSriastradh		  1);
80442542f5fSchristos	OUT_BATCH(urb_cl_end << UF1_CLIP_FENCE_SHIFT |
80542542f5fSchristos		  urb_gs_end << UF1_GS_FENCE_SHIFT |
80642542f5fSchristos		  urb_vs_end << UF1_VS_FENCE_SHIFT);
80742542f5fSchristos	OUT_BATCH(urb_cs_end << UF2_CS_FENCE_SHIFT |
80842542f5fSchristos		  urb_sf_end << UF2_SF_FENCE_SHIFT);
80903b705cfSriastradh
81003b705cfSriastradh	/* Constant buffer state */
81103b705cfSriastradh	OUT_BATCH(GEN4_CS_URB_STATE | 0);
81203b705cfSriastradh	OUT_BATCH((URB_CS_ENTRY_SIZE - 1) << 4 | URB_CS_ENTRIES << 0);
81303b705cfSriastradh
81403b705cfSriastradh	sna->render_state.gen4.needs_urb = false;
81503b705cfSriastradh}
81603b705cfSriastradh
81703b705cfSriastradhstatic void
81803b705cfSriastradhgen4_emit_state_base_address(struct sna *sna)
81903b705cfSriastradh{
82003b705cfSriastradh	assert(sna->render_state.gen4.general_bo->proxy == NULL);
82103b705cfSriastradh	OUT_BATCH(GEN4_STATE_BASE_ADDRESS | 4);
82203b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* general */
82303b705cfSriastradh				 sna->kgem.nbatch,
82403b705cfSriastradh				 sna->render_state.gen4.general_bo,
82503b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
82603b705cfSriastradh				 BASE_ADDRESS_MODIFY));
82703b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* surface */
82803b705cfSriastradh				 sna->kgem.nbatch,
82903b705cfSriastradh				 NULL,
83003b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
83103b705cfSriastradh				 BASE_ADDRESS_MODIFY));
83203b705cfSriastradh	OUT_BATCH(0); /* media */
83303b705cfSriastradh
83403b705cfSriastradh	/* upper bounds, all disabled */
83503b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
83603b705cfSriastradh	OUT_BATCH(0);
83703b705cfSriastradh}
83803b705cfSriastradh
83903b705cfSriastradhstatic void
84003b705cfSriastradhgen4_emit_invariant(struct sna *sna)
84103b705cfSriastradh{
84203b705cfSriastradh	assert(sna->kgem.surface == sna->kgem.batch_size);
84303b705cfSriastradh
84403b705cfSriastradh	if (sna->kgem.gen >= 045)
84503b705cfSriastradh		OUT_BATCH(NEW_PIPELINE_SELECT | PIPELINE_SELECT_3D);
84603b705cfSriastradh	else
84703b705cfSriastradh		OUT_BATCH(GEN4_PIPELINE_SELECT | PIPELINE_SELECT_3D);
84803b705cfSriastradh
84903b705cfSriastradh	gen4_emit_state_base_address(sna);
85003b705cfSriastradh
85103b705cfSriastradh	sna->render_state.gen4.needs_invariant = false;
85203b705cfSriastradh}
85303b705cfSriastradh
85403b705cfSriastradhstatic void
85503b705cfSriastradhgen4_get_batch(struct sna *sna, const struct sna_composite_op *op)
85603b705cfSriastradh{
85703b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, op->dst.bo);
85803b705cfSriastradh
85903b705cfSriastradh	if (!kgem_check_batch_with_surfaces(&sna->kgem, 150 + 50*FORCE_FLUSH, 4)) {
86003b705cfSriastradh		DBG(("%s: flushing batch: %d < %d+%d\n",
86103b705cfSriastradh		     __FUNCTION__, sna->kgem.surface - sna->kgem.nbatch,
86203b705cfSriastradh		     150, 4*8));
86303b705cfSriastradh		kgem_submit(&sna->kgem);
86403b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
86503b705cfSriastradh	}
86603b705cfSriastradh
86703b705cfSriastradh	if (sna->render_state.gen4.needs_invariant)
86803b705cfSriastradh		gen4_emit_invariant(sna);
86903b705cfSriastradh}
87003b705cfSriastradh
87103b705cfSriastradhstatic void
87203b705cfSriastradhgen4_align_vertex(struct sna *sna, const struct sna_composite_op *op)
87303b705cfSriastradh{
87403b705cfSriastradh	assert(op->floats_per_rect == 3*op->floats_per_vertex);
87503b705cfSriastradh	if (op->floats_per_vertex != sna->render_state.gen4.floats_per_vertex) {
87642542f5fSchristos		DBG(("aligning vertex: was %d, now %d floats per vertex\n",
87703b705cfSriastradh		     sna->render_state.gen4.floats_per_vertex,
87842542f5fSchristos		     op->floats_per_vertex));
87942542f5fSchristos		gen4_vertex_align(sna, op);
88003b705cfSriastradh		sna->render_state.gen4.floats_per_vertex = op->floats_per_vertex;
88103b705cfSriastradh	}
88203b705cfSriastradh}
88303b705cfSriastradh
88403b705cfSriastradhstatic void
88503b705cfSriastradhgen4_emit_binding_table(struct sna *sna, uint16_t offset)
88603b705cfSriastradh{
88703b705cfSriastradh	if (sna->render_state.gen4.surface_table == offset)
88803b705cfSriastradh		return;
88903b705cfSriastradh
89003b705cfSriastradh	sna->render_state.gen4.surface_table = offset;
89103b705cfSriastradh
89203b705cfSriastradh	/* Binding table pointers */
89303b705cfSriastradh	OUT_BATCH(GEN4_3DSTATE_BINDING_TABLE_POINTERS | 4);
89403b705cfSriastradh	OUT_BATCH(0);		/* vs */
89503b705cfSriastradh	OUT_BATCH(0);		/* gs */
89603b705cfSriastradh	OUT_BATCH(0);		/* clip */
89703b705cfSriastradh	OUT_BATCH(0);		/* sf */
89803b705cfSriastradh	/* Only the PS uses the binding table */
89903b705cfSriastradh	OUT_BATCH(offset*4);
90003b705cfSriastradh}
90103b705cfSriastradh
90203b705cfSriastradhstatic void
90303b705cfSriastradhgen4_emit_pipelined_pointers(struct sna *sna,
90403b705cfSriastradh			     const struct sna_composite_op *op,
90503b705cfSriastradh			     int blend, int kernel)
90603b705cfSriastradh{
90703b705cfSriastradh	uint16_t sp, bp;
90803b705cfSriastradh	uint32_t key;
90903b705cfSriastradh
91003b705cfSriastradh	DBG(("%s: has_mask=%d, src=(%d, %d), mask=(%d, %d),kernel=%d, blend=%d, ca=%d, format=%x\n",
91103b705cfSriastradh	     __FUNCTION__, op->u.gen4.ve_id & 2,
91203b705cfSriastradh	     op->src.filter, op->src.repeat,
91303b705cfSriastradh	     op->mask.filter, op->mask.repeat,
91403b705cfSriastradh	     kernel, blend, op->has_component_alpha, (int)op->dst.format));
91503b705cfSriastradh
91603b705cfSriastradh	sp = SAMPLER_OFFSET(op->src.filter, op->src.repeat,
91703b705cfSriastradh			    op->mask.filter, op->mask.repeat,
91803b705cfSriastradh			    kernel);
91903b705cfSriastradh	bp = gen4_get_blend(blend, op->has_component_alpha, op->dst.format);
92003b705cfSriastradh
92103b705cfSriastradh	DBG(("%s: sp=%d, bp=%d\n", __FUNCTION__, sp, bp));
92203b705cfSriastradh	key = sp | (uint32_t)bp << 16;
92303b705cfSriastradh	if (key == sna->render_state.gen4.last_pipelined_pointers)
92403b705cfSriastradh		return;
92503b705cfSriastradh
92603b705cfSriastradh	OUT_BATCH(GEN4_3DSTATE_PIPELINED_POINTERS | 5);
92703b705cfSriastradh	OUT_BATCH(sna->render_state.gen4.vs);
92803b705cfSriastradh	OUT_BATCH(GEN4_GS_DISABLE); /* passthrough */
92903b705cfSriastradh	OUT_BATCH(GEN4_CLIP_DISABLE); /* passthrough */
93003b705cfSriastradh	OUT_BATCH(sna->render_state.gen4.sf);
93103b705cfSriastradh	OUT_BATCH(sna->render_state.gen4.wm + sp);
93203b705cfSriastradh	OUT_BATCH(sna->render_state.gen4.cc + bp);
93303b705cfSriastradh
93403b705cfSriastradh	sna->render_state.gen4.last_pipelined_pointers = key;
93503b705cfSriastradh	gen4_emit_urb(sna);
93603b705cfSriastradh}
93703b705cfSriastradh
93803b705cfSriastradhstatic bool
93903b705cfSriastradhgen4_emit_drawing_rectangle(struct sna *sna, const struct sna_composite_op *op)
94003b705cfSriastradh{
94103b705cfSriastradh	uint32_t limit = (op->dst.height - 1) << 16 | (op->dst.width - 1);
94203b705cfSriastradh	uint32_t offset = (uint16_t)op->dst.y << 16 | (uint16_t)op->dst.x;
94303b705cfSriastradh
94413496ba1Ssnj	assert(!too_large(abs(op->dst.x), abs(op->dst.y)));
94503b705cfSriastradh	assert(!too_large(op->dst.width, op->dst.height));
94603b705cfSriastradh
94703b705cfSriastradh	if (sna->render_state.gen4.drawrect_limit == limit &&
94803b705cfSriastradh	    sna->render_state.gen4.drawrect_offset == offset)
94903b705cfSriastradh		return true;
95003b705cfSriastradh
95103b705cfSriastradh	sna->render_state.gen4.drawrect_offset = offset;
95203b705cfSriastradh	sna->render_state.gen4.drawrect_limit = limit;
95303b705cfSriastradh
95403b705cfSriastradh	OUT_BATCH(GEN4_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
95503b705cfSriastradh	OUT_BATCH(0);
95603b705cfSriastradh	OUT_BATCH(limit);
95703b705cfSriastradh	OUT_BATCH(offset);
95803b705cfSriastradh	return false;
95903b705cfSriastradh}
96003b705cfSriastradh
96103b705cfSriastradhstatic void
96203b705cfSriastradhgen4_emit_vertex_elements(struct sna *sna,
96303b705cfSriastradh			  const struct sna_composite_op *op)
96403b705cfSriastradh{
96503b705cfSriastradh	/*
96603b705cfSriastradh	 * vertex data in vertex buffer
96703b705cfSriastradh	 *    position: (x, y)
96803b705cfSriastradh	 *    texture coordinate 0: (u0, v0) if (is_affine is true) else (u0, v0, w0)
96903b705cfSriastradh	 *    texture coordinate 1 if (has_mask is true): same as above
97003b705cfSriastradh	 */
97103b705cfSriastradh	struct gen4_render_state *render = &sna->render_state.gen4;
97203b705cfSriastradh	uint32_t src_format, dw;
97303b705cfSriastradh	int id = op->u.gen4.ve_id;
97403b705cfSriastradh
97503b705cfSriastradh	if (render->ve_id == id)
97603b705cfSriastradh		return;
97703b705cfSriastradh	render->ve_id = id;
97803b705cfSriastradh
97903b705cfSriastradh	/* The VUE layout
98003b705cfSriastradh	 *    dword 0-3: position (x, y, 1.0, 1.0),
98103b705cfSriastradh	 *    dword 4-7: texture coordinate 0 (u0, v0, w0, 1.0)
98203b705cfSriastradh	 *    [optional] dword 8-11: texture coordinate 1 (u1, v1, w1, 1.0)
98303b705cfSriastradh	 */
98403b705cfSriastradh	OUT_BATCH(GEN4_3DSTATE_VERTEX_ELEMENTS | (2 * (1 + 2) - 1));
98503b705cfSriastradh
98603b705cfSriastradh	/* x,y */
98703b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
98803b705cfSriastradh		  GEN4_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
98903b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
99003b705cfSriastradh	OUT_BATCH(VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
99103b705cfSriastradh		  VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
99203b705cfSriastradh		  VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT |
99303b705cfSriastradh		  VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT |
99403b705cfSriastradh		  (1*4) << VE1_DESTINATION_ELEMENT_OFFSET_SHIFT);
99503b705cfSriastradh
99603b705cfSriastradh	/* u0, v0, w0 */
99703b705cfSriastradh	/* u0, v0, w0 */
99803b705cfSriastradh	DBG(("%s: first channel %d floats, offset=4b\n", __FUNCTION__, id & 3));
99903b705cfSriastradh	dw = VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
100003b705cfSriastradh	switch (id & 3) {
100103b705cfSriastradh	default:
100203b705cfSriastradh		assert(0);
100303b705cfSriastradh	case 0:
100403b705cfSriastradh		src_format = GEN4_SURFACEFORMAT_R16G16_SSCALED;
100503b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
100603b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
100703b705cfSriastradh		dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
100803b705cfSriastradh		break;
100903b705cfSriastradh	case 1:
101003b705cfSriastradh		src_format = GEN4_SURFACEFORMAT_R32_FLOAT;
101103b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
101203b705cfSriastradh		dw |= VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
101303b705cfSriastradh		dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
101403b705cfSriastradh		break;
101503b705cfSriastradh	case 2:
101603b705cfSriastradh		src_format = GEN4_SURFACEFORMAT_R32G32_FLOAT;
101703b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
101803b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
101903b705cfSriastradh		dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
102003b705cfSriastradh		break;
102103b705cfSriastradh	case 3:
102203b705cfSriastradh		src_format = GEN4_SURFACEFORMAT_R32G32B32_FLOAT;
102303b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
102403b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
102503b705cfSriastradh		dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
102603b705cfSriastradh		break;
102703b705cfSriastradh	}
102803b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
102903b705cfSriastradh		  src_format << VE0_FORMAT_SHIFT |
103003b705cfSriastradh		  4 << VE0_OFFSET_SHIFT);
103103b705cfSriastradh	OUT_BATCH(dw | 8 << VE1_DESTINATION_ELEMENT_OFFSET_SHIFT);
103203b705cfSriastradh
103303b705cfSriastradh	/* u1, v1, w1 */
103403b705cfSriastradh	if (id >> 2) {
103503b705cfSriastradh		unsigned src_offset = 4 + ((id & 3) ?: 1) * sizeof(float);
103603b705cfSriastradh		DBG(("%s: second channel %d floats, offset=%db\n", __FUNCTION__,
103703b705cfSriastradh		     id >> 2, src_offset));
103803b705cfSriastradh		dw = VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
103903b705cfSriastradh		switch (id >> 2) {
104003b705cfSriastradh		case 1:
104103b705cfSriastradh			src_format = GEN4_SURFACEFORMAT_R32_FLOAT;
104203b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
104303b705cfSriastradh			dw |= VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
104403b705cfSriastradh			dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
104503b705cfSriastradh			break;
104603b705cfSriastradh		default:
104703b705cfSriastradh			assert(0);
104803b705cfSriastradh		case 2:
104903b705cfSriastradh			src_format = GEN4_SURFACEFORMAT_R32G32_FLOAT;
105003b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
105103b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
105203b705cfSriastradh			dw |= VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_2_SHIFT;
105303b705cfSriastradh			break;
105403b705cfSriastradh		case 3:
105503b705cfSriastradh			src_format = GEN4_SURFACEFORMAT_R32G32B32_FLOAT;
105603b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
105703b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
105803b705cfSriastradh			dw |= VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
105903b705cfSriastradh			break;
106003b705cfSriastradh		}
106103b705cfSriastradh		OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
106203b705cfSriastradh			  src_format << VE0_FORMAT_SHIFT |
106303b705cfSriastradh			  src_offset << VE0_OFFSET_SHIFT);
106403b705cfSriastradh		OUT_BATCH(dw | 12 << VE1_DESTINATION_ELEMENT_OFFSET_SHIFT);
106503b705cfSriastradh	} else {
106603b705cfSriastradh		OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
106703b705cfSriastradh			  GEN4_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
106803b705cfSriastradh			  0 << VE0_OFFSET_SHIFT);
106903b705cfSriastradh		OUT_BATCH(VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT |
107003b705cfSriastradh			  VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT |
107103b705cfSriastradh			  VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
107203b705cfSriastradh			  VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT |
107303b705cfSriastradh			  12 << VE1_DESTINATION_ELEMENT_OFFSET_SHIFT);
107403b705cfSriastradh	}
107503b705cfSriastradh}
107603b705cfSriastradh
107703b705cfSriastradhstatic void
107803b705cfSriastradhgen4_emit_state(struct sna *sna,
107903b705cfSriastradh		const struct sna_composite_op *op,
108003b705cfSriastradh		uint16_t wm_binding_table)
108103b705cfSriastradh{
108203b705cfSriastradh	bool flush;
108303b705cfSriastradh
108403b705cfSriastradh	assert(op->dst.bo->exec);
108503b705cfSriastradh
108603b705cfSriastradh	flush = wm_binding_table & 1;
108742542f5fSchristos	wm_binding_table &= ~1;
108842542f5fSchristos
108942542f5fSchristos	if (ALWAYS_FLUSH || kgem_bo_is_dirty(op->src.bo) || kgem_bo_is_dirty(op->mask.bo)) {
109003b705cfSriastradh		DBG(("%s: flushing dirty (%d, %d), forced? %d\n", __FUNCTION__,
109103b705cfSriastradh		     kgem_bo_is_dirty(op->src.bo),
109203b705cfSriastradh		     kgem_bo_is_dirty(op->mask.bo),
109303b705cfSriastradh		     flush));
109442542f5fSchristos		gen4_emit_pipe_invalidate(sna);
109503b705cfSriastradh		kgem_clear_dirty(&sna->kgem);
109603b705cfSriastradh		kgem_bo_mark_dirty(op->dst.bo);
109703b705cfSriastradh		flush = false;
109803b705cfSriastradh	}
109903b705cfSriastradh	flush &= gen4_emit_drawing_rectangle(sna, op);
110003b705cfSriastradh	if (flush && op->op > PictOpSrc)
110142542f5fSchristos		gen4_emit_pipe_flush(sna);
110203b705cfSriastradh
110342542f5fSchristos	gen4_emit_binding_table(sna, wm_binding_table);
110403b705cfSriastradh	gen4_emit_pipelined_pointers(sna, op, op->op, op->u.gen4.wm_kernel);
110503b705cfSriastradh	gen4_emit_vertex_elements(sna, op);
110603b705cfSriastradh}
110703b705cfSriastradh
110803b705cfSriastradhstatic void
110903b705cfSriastradhgen4_bind_surfaces(struct sna *sna,
111003b705cfSriastradh		   const struct sna_composite_op *op)
111103b705cfSriastradh{
111203b705cfSriastradh	uint32_t *binding_table;
111342542f5fSchristos	uint16_t offset, dirty;
111403b705cfSriastradh
111503b705cfSriastradh	gen4_get_batch(sna, op);
111642542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
111703b705cfSriastradh
111803b705cfSriastradh	binding_table = gen4_composite_get_binding_table(sna, &offset);
111903b705cfSriastradh
112003b705cfSriastradh	binding_table[0] =
112103b705cfSriastradh		gen4_bind_bo(sna,
112203b705cfSriastradh			    op->dst.bo, op->dst.width, op->dst.height,
112303b705cfSriastradh			    gen4_get_dest_format(op->dst.format),
112403b705cfSriastradh			    true);
112503b705cfSriastradh	binding_table[1] =
112603b705cfSriastradh		gen4_bind_bo(sna,
112703b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
112803b705cfSriastradh			     op->src.card_format,
112903b705cfSriastradh			     false);
113003b705cfSriastradh	if (op->mask.bo) {
113103b705cfSriastradh		assert(op->u.gen4.ve_id >> 2);
113203b705cfSriastradh		binding_table[2] =
113303b705cfSriastradh			gen4_bind_bo(sna,
113403b705cfSriastradh				     op->mask.bo,
113503b705cfSriastradh				     op->mask.width,
113603b705cfSriastradh				     op->mask.height,
113703b705cfSriastradh				     op->mask.card_format,
113803b705cfSriastradh				     false);
113903b705cfSriastradh	}
114003b705cfSriastradh
114103b705cfSriastradh	if (sna->kgem.surface == offset &&
114203b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen4.surface_table) == *(uint64_t*)binding_table &&
114303b705cfSriastradh	    (op->mask.bo == NULL ||
114403b705cfSriastradh	     sna->kgem.batch[sna->render_state.gen4.surface_table+2] == binding_table[2])) {
114503b705cfSriastradh		sna->kgem.surface += sizeof(struct gen4_surface_state_padded) / sizeof(uint32_t);
114603b705cfSriastradh		offset = sna->render_state.gen4.surface_table;
114703b705cfSriastradh	}
114803b705cfSriastradh
114942542f5fSchristos	if (!ALWAYS_FLUSH && sna->kgem.batch[sna->render_state.gen4.surface_table] == binding_table[0])
115042542f5fSchristos		dirty = 0;
115142542f5fSchristos
115203b705cfSriastradh	gen4_emit_state(sna, op, offset | dirty);
115303b705cfSriastradh}
115403b705cfSriastradh
115503b705cfSriastradhfastcall static void
115603b705cfSriastradhgen4_render_composite_blt(struct sna *sna,
115703b705cfSriastradh			  const struct sna_composite_op *op,
115803b705cfSriastradh			  const struct sna_composite_rectangles *r)
115903b705cfSriastradh{
116003b705cfSriastradh	DBG(("%s: src=(%d, %d)+(%d, %d), mask=(%d, %d)+(%d, %d), dst=(%d, %d)+(%d, %d), size=(%d, %d)\n",
116103b705cfSriastradh	     __FUNCTION__,
116203b705cfSriastradh	     r->src.x, r->src.y, op->src.offset[0], op->src.offset[1],
116303b705cfSriastradh	     r->mask.x, r->mask.y, op->mask.offset[0], op->mask.offset[1],
116403b705cfSriastradh	     r->dst.x, r->dst.y, op->dst.x, op->dst.y,
116503b705cfSriastradh	     r->width, r->height));
116603b705cfSriastradh
116703b705cfSriastradh	gen4_get_rectangles(sna, op, 1, gen4_bind_surfaces);
116803b705cfSriastradh	op->prim_emit(sna, op, r);
116903b705cfSriastradh}
117003b705cfSriastradh
117103b705cfSriastradhfastcall static void
117203b705cfSriastradhgen4_render_composite_box(struct sna *sna,
117303b705cfSriastradh			  const struct sna_composite_op *op,
117403b705cfSriastradh			  const BoxRec *box)
117503b705cfSriastradh{
117603b705cfSriastradh	struct sna_composite_rectangles r;
117703b705cfSriastradh
117803b705cfSriastradh	DBG(("  %s: (%d, %d), (%d, %d)\n",
117903b705cfSriastradh	     __FUNCTION__,
118003b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
118103b705cfSriastradh
118203b705cfSriastradh	gen4_get_rectangles(sna, op, 1, gen4_bind_surfaces);
118303b705cfSriastradh
118403b705cfSriastradh	r.dst.x = box->x1;
118503b705cfSriastradh	r.dst.y = box->y1;
118603b705cfSriastradh	r.width  = box->x2 - box->x1;
118703b705cfSriastradh	r.height = box->y2 - box->y1;
118803b705cfSriastradh	r.mask = r.src = r.dst;
118903b705cfSriastradh
119003b705cfSriastradh	op->prim_emit(sna, op, &r);
119103b705cfSriastradh}
119203b705cfSriastradh
119303b705cfSriastradhstatic void
119403b705cfSriastradhgen4_render_composite_boxes__blt(struct sna *sna,
119503b705cfSriastradh				 const struct sna_composite_op *op,
119603b705cfSriastradh				 const BoxRec *box, int nbox)
119703b705cfSriastradh{
119803b705cfSriastradh	DBG(("%s(%d) delta=(%d, %d), src=(%d, %d)/(%d, %d), mask=(%d, %d)/(%d, %d)\n",
119903b705cfSriastradh	     __FUNCTION__, nbox, op->dst.x, op->dst.y,
120003b705cfSriastradh	     op->src.offset[0], op->src.offset[1],
120103b705cfSriastradh	     op->src.width, op->src.height,
120203b705cfSriastradh	     op->mask.offset[0], op->mask.offset[1],
120303b705cfSriastradh	     op->mask.width, op->mask.height));
120403b705cfSriastradh
120503b705cfSriastradh	do {
120603b705cfSriastradh		int nbox_this_time;
120703b705cfSriastradh
120803b705cfSriastradh		nbox_this_time = gen4_get_rectangles(sna, op, nbox,
120903b705cfSriastradh						     gen4_bind_surfaces);
121003b705cfSriastradh		nbox -= nbox_this_time;
121103b705cfSriastradh
121203b705cfSriastradh		do {
121303b705cfSriastradh			struct sna_composite_rectangles r;
121403b705cfSriastradh
121503b705cfSriastradh			DBG(("  %s: (%d, %d), (%d, %d)\n",
121603b705cfSriastradh			     __FUNCTION__,
121703b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
121803b705cfSriastradh
121903b705cfSriastradh			r.dst.x = box->x1;
122003b705cfSriastradh			r.dst.y = box->y1;
122103b705cfSriastradh			r.width  = box->x2 - box->x1;
122203b705cfSriastradh			r.height = box->y2 - box->y1;
122303b705cfSriastradh			r.mask = r.src = r.dst;
122403b705cfSriastradh			op->prim_emit(sna, op, &r);
122503b705cfSriastradh			box++;
122603b705cfSriastradh		} while (--nbox_this_time);
122703b705cfSriastradh	} while (nbox);
122803b705cfSriastradh}
122903b705cfSriastradh
123003b705cfSriastradhstatic void
123103b705cfSriastradhgen4_render_composite_boxes(struct sna *sna,
123203b705cfSriastradh			    const struct sna_composite_op *op,
123303b705cfSriastradh			    const BoxRec *box, int nbox)
123403b705cfSriastradh{
123503b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
123603b705cfSriastradh
123703b705cfSriastradh	do {
123803b705cfSriastradh		int nbox_this_time;
123903b705cfSriastradh		float *v;
124003b705cfSriastradh
124103b705cfSriastradh		nbox_this_time = gen4_get_rectangles(sna, op, nbox,
124203b705cfSriastradh						     gen4_bind_surfaces);
124303b705cfSriastradh		assert(nbox_this_time);
124403b705cfSriastradh		nbox -= nbox_this_time;
124503b705cfSriastradh
124603b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
124703b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
124803b705cfSriastradh
124903b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
125003b705cfSriastradh		box += nbox_this_time;
125103b705cfSriastradh	} while (nbox);
125203b705cfSriastradh}
125303b705cfSriastradh
125403b705cfSriastradh#if !FORCE_FLUSH
125503b705cfSriastradhstatic void
125603b705cfSriastradhgen4_render_composite_boxes__thread(struct sna *sna,
125703b705cfSriastradh				    const struct sna_composite_op *op,
125803b705cfSriastradh				    const BoxRec *box, int nbox)
125903b705cfSriastradh{
126003b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
126103b705cfSriastradh
126203b705cfSriastradh	sna_vertex_lock(&sna->render);
126303b705cfSriastradh	do {
126403b705cfSriastradh		int nbox_this_time;
126503b705cfSriastradh		float *v;
126603b705cfSriastradh
126703b705cfSriastradh		nbox_this_time = gen4_get_rectangles(sna, op, nbox,
126803b705cfSriastradh						     gen4_bind_surfaces);
126903b705cfSriastradh		assert(nbox_this_time);
127003b705cfSriastradh		nbox -= nbox_this_time;
127103b705cfSriastradh
127203b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
127303b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
127403b705cfSriastradh
127503b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
127603b705cfSriastradh		sna_vertex_unlock(&sna->render);
127703b705cfSriastradh
127803b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
127903b705cfSriastradh		box += nbox_this_time;
128003b705cfSriastradh
128103b705cfSriastradh		sna_vertex_lock(&sna->render);
128203b705cfSriastradh		sna_vertex_release__locked(&sna->render);
128303b705cfSriastradh	} while (nbox);
128403b705cfSriastradh	sna_vertex_unlock(&sna->render);
128503b705cfSriastradh}
128603b705cfSriastradh#endif
128703b705cfSriastradh
128803b705cfSriastradh#ifndef MAX
128903b705cfSriastradh#define MAX(a,b) ((a) > (b) ? (a) : (b))
129003b705cfSriastradh#endif
129103b705cfSriastradh
129203b705cfSriastradhstatic uint32_t gen4_bind_video_source(struct sna *sna,
129303b705cfSriastradh				       struct kgem_bo *src_bo,
129403b705cfSriastradh				       uint32_t src_offset,
129503b705cfSriastradh				       int src_width,
129603b705cfSriastradh				       int src_height,
129703b705cfSriastradh				       int src_pitch,
129803b705cfSriastradh				       uint32_t src_surf_format)
129903b705cfSriastradh{
130003b705cfSriastradh	struct gen4_surface_state *ss;
130103b705cfSriastradh
130203b705cfSriastradh	sna->kgem.surface -= sizeof(struct gen4_surface_state_padded) / sizeof(uint32_t);
130303b705cfSriastradh
130403b705cfSriastradh	ss = memset(sna->kgem.batch + sna->kgem.surface, 0, sizeof(*ss));
130503b705cfSriastradh	ss->ss0.surface_type = GEN4_SURFACE_2D;
130603b705cfSriastradh	ss->ss0.surface_format = src_surf_format;
130703b705cfSriastradh	ss->ss0.color_blend = 1;
130803b705cfSriastradh
130903b705cfSriastradh	ss->ss1.base_addr =
131003b705cfSriastradh		kgem_add_reloc(&sna->kgem,
131103b705cfSriastradh			       sna->kgem.surface + 1,
131203b705cfSriastradh			       src_bo,
131303b705cfSriastradh			       I915_GEM_DOMAIN_SAMPLER << 16,
131403b705cfSriastradh			       src_offset);
131503b705cfSriastradh
131603b705cfSriastradh	ss->ss2.width  = src_width - 1;
131703b705cfSriastradh	ss->ss2.height = src_height - 1;
131803b705cfSriastradh	ss->ss3.pitch  = src_pitch - 1;
131903b705cfSriastradh
132003b705cfSriastradh	return sna->kgem.surface * sizeof(uint32_t);
132103b705cfSriastradh}
132203b705cfSriastradh
132303b705cfSriastradhstatic void gen4_video_bind_surfaces(struct sna *sna,
132403b705cfSriastradh				     const struct sna_composite_op *op)
132503b705cfSriastradh{
132603b705cfSriastradh	struct sna_video_frame *frame = op->priv;
132703b705cfSriastradh	uint32_t src_surf_format;
132803b705cfSriastradh	uint32_t src_surf_base[6];
132903b705cfSriastradh	int src_width[6];
133003b705cfSriastradh	int src_height[6];
133103b705cfSriastradh	int src_pitch[6];
133203b705cfSriastradh	uint32_t *binding_table;
133342542f5fSchristos	uint16_t offset, dirty;
133403b705cfSriastradh	int n_src, n;
133503b705cfSriastradh
133603b705cfSriastradh	src_surf_base[0] = 0;
133703b705cfSriastradh	src_surf_base[1] = 0;
133803b705cfSriastradh	src_surf_base[2] = frame->VBufOffset;
133903b705cfSriastradh	src_surf_base[3] = frame->VBufOffset;
134003b705cfSriastradh	src_surf_base[4] = frame->UBufOffset;
134103b705cfSriastradh	src_surf_base[5] = frame->UBufOffset;
134203b705cfSriastradh
134303b705cfSriastradh	if (is_planar_fourcc(frame->id)) {
134403b705cfSriastradh		src_surf_format = GEN4_SURFACEFORMAT_R8_UNORM;
134503b705cfSriastradh		src_width[1]  = src_width[0]  = frame->width;
134603b705cfSriastradh		src_height[1] = src_height[0] = frame->height;
134703b705cfSriastradh		src_pitch[1]  = src_pitch[0]  = frame->pitch[1];
134803b705cfSriastradh		src_width[4]  = src_width[5]  = src_width[2]  = src_width[3] =
134903b705cfSriastradh			frame->width / 2;
135003b705cfSriastradh		src_height[4] = src_height[5] = src_height[2] = src_height[3] =
135103b705cfSriastradh			frame->height / 2;
135203b705cfSriastradh		src_pitch[4]  = src_pitch[5]  = src_pitch[2]  = src_pitch[3] =
135303b705cfSriastradh			frame->pitch[0];
135403b705cfSriastradh		n_src = 6;
135503b705cfSriastradh	} else {
135603b705cfSriastradh		if (frame->id == FOURCC_UYVY)
135703b705cfSriastradh			src_surf_format = GEN4_SURFACEFORMAT_YCRCB_SWAPY;
135803b705cfSriastradh		else
135903b705cfSriastradh			src_surf_format = GEN4_SURFACEFORMAT_YCRCB_NORMAL;
136003b705cfSriastradh
136103b705cfSriastradh		src_width[0]  = frame->width;
136203b705cfSriastradh		src_height[0] = frame->height;
136303b705cfSriastradh		src_pitch[0]  = frame->pitch[0];
136403b705cfSriastradh		n_src = 1;
136503b705cfSriastradh	}
136603b705cfSriastradh
136703b705cfSriastradh	gen4_get_batch(sna, op);
136842542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
136903b705cfSriastradh
137003b705cfSriastradh	binding_table = gen4_composite_get_binding_table(sna, &offset);
137103b705cfSriastradh	binding_table[0] =
137203b705cfSriastradh		gen4_bind_bo(sna,
137303b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
137403b705cfSriastradh			     gen4_get_dest_format(op->dst.format),
137503b705cfSriastradh			     true);
137603b705cfSriastradh	for (n = 0; n < n_src; n++) {
137703b705cfSriastradh		binding_table[1+n] =
137803b705cfSriastradh			gen4_bind_video_source(sna,
137903b705cfSriastradh					       frame->bo,
138003b705cfSriastradh					       src_surf_base[n],
138103b705cfSriastradh					       src_width[n],
138203b705cfSriastradh					       src_height[n],
138303b705cfSriastradh					       src_pitch[n],
138403b705cfSriastradh					       src_surf_format);
138503b705cfSriastradh	}
138603b705cfSriastradh
138742542f5fSchristos	if (!ALWAYS_FLUSH && sna->kgem.batch[sna->render_state.gen4.surface_table] == binding_table[0])
138842542f5fSchristos		dirty = 0;
138942542f5fSchristos
139003b705cfSriastradh	gen4_emit_state(sna, op, offset | dirty);
139103b705cfSriastradh}
139203b705cfSriastradh
139303b705cfSriastradhstatic bool
139403b705cfSriastradhgen4_render_video(struct sna *sna,
139503b705cfSriastradh		  struct sna_video *video,
139603b705cfSriastradh		  struct sna_video_frame *frame,
139703b705cfSriastradh		  RegionPtr dstRegion,
139803b705cfSriastradh		  PixmapPtr pixmap)
139903b705cfSriastradh{
140003b705cfSriastradh	struct sna_composite_op tmp;
140142542f5fSchristos	struct sna_pixmap *priv = sna_pixmap(pixmap);
140203b705cfSriastradh	int dst_width = dstRegion->extents.x2 - dstRegion->extents.x1;
140303b705cfSriastradh	int dst_height = dstRegion->extents.y2 - dstRegion->extents.y1;
140403b705cfSriastradh	int src_width = frame->src.x2 - frame->src.x1;
140503b705cfSriastradh	int src_height = frame->src.y2 - frame->src.y1;
140603b705cfSriastradh	float src_offset_x, src_offset_y;
140703b705cfSriastradh	float src_scale_x, src_scale_y;
140803b705cfSriastradh	int nbox, pix_xoff, pix_yoff;
140942542f5fSchristos	const BoxRec *box;
141003b705cfSriastradh
141103b705cfSriastradh	DBG(("%s: %dx%d -> %dx%d\n", __FUNCTION__,
141203b705cfSriastradh	     src_width, src_height, dst_width, dst_height));
141303b705cfSriastradh
141442542f5fSchristos	assert(priv->gpu_bo);
141503b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
141603b705cfSriastradh
141703b705cfSriastradh	tmp.op = PictOpSrc;
141803b705cfSriastradh	tmp.dst.pixmap = pixmap;
141903b705cfSriastradh	tmp.dst.width  = pixmap->drawable.width;
142003b705cfSriastradh	tmp.dst.height = pixmap->drawable.height;
142103b705cfSriastradh	tmp.dst.format = sna_format_for_depth(pixmap->drawable.depth);
142203b705cfSriastradh	tmp.dst.bo = priv->gpu_bo;
142303b705cfSriastradh
142403b705cfSriastradh	if (src_width == dst_width && src_height == dst_height)
142503b705cfSriastradh		tmp.src.filter = SAMPLER_FILTER_NEAREST;
142603b705cfSriastradh	else
142703b705cfSriastradh		tmp.src.filter = SAMPLER_FILTER_BILINEAR;
142803b705cfSriastradh	tmp.src.repeat = SAMPLER_EXTEND_PAD;
142903b705cfSriastradh	tmp.src.bo = frame->bo;
143003b705cfSriastradh	tmp.mask.bo = NULL;
143103b705cfSriastradh	tmp.u.gen4.wm_kernel =
143203b705cfSriastradh		is_planar_fourcc(frame->id) ? WM_KERNEL_VIDEO_PLANAR : WM_KERNEL_VIDEO_PACKED;
143303b705cfSriastradh	tmp.u.gen4.ve_id = 2;
143403b705cfSriastradh	tmp.is_affine = true;
143503b705cfSriastradh	tmp.floats_per_vertex = 3;
143603b705cfSriastradh	tmp.floats_per_rect = 9;
143703b705cfSriastradh	tmp.priv = frame;
143803b705cfSriastradh
143903b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL)) {
144003b705cfSriastradh		kgem_submit(&sna->kgem);
144142542f5fSchristos		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL))
144242542f5fSchristos			return false;
144303b705cfSriastradh	}
144403b705cfSriastradh
144503b705cfSriastradh	gen4_align_vertex(sna, &tmp);
144642542f5fSchristos	gen4_video_bind_surfaces(sna, &tmp);
144703b705cfSriastradh
144803b705cfSriastradh	/* Set up the offset for translating from the given region (in screen
144903b705cfSriastradh	 * coordinates) to the backing pixmap.
145003b705cfSriastradh	 */
145103b705cfSriastradh#ifdef COMPOSITE
145203b705cfSriastradh	pix_xoff = -pixmap->screen_x + pixmap->drawable.x;
145303b705cfSriastradh	pix_yoff = -pixmap->screen_y + pixmap->drawable.y;
145403b705cfSriastradh#else
145503b705cfSriastradh	pix_xoff = 0;
145603b705cfSriastradh	pix_yoff = 0;
145703b705cfSriastradh#endif
145803b705cfSriastradh
145903b705cfSriastradh	src_scale_x = (float)src_width / dst_width / frame->width;
146003b705cfSriastradh	src_offset_x = (float)frame->src.x1 / frame->width - dstRegion->extents.x1 * src_scale_x;
146103b705cfSriastradh
146203b705cfSriastradh	src_scale_y = (float)src_height / dst_height / frame->height;
146303b705cfSriastradh	src_offset_y = (float)frame->src.y1 / frame->height - dstRegion->extents.y1 * src_scale_y;
146403b705cfSriastradh
146542542f5fSchristos	box = region_rects(dstRegion);
146642542f5fSchristos	nbox = region_num_rects(dstRegion);
146703b705cfSriastradh	do {
146803b705cfSriastradh		int n;
146903b705cfSriastradh
147003b705cfSriastradh		n = gen4_get_rectangles(sna, &tmp, nbox,
147103b705cfSriastradh					gen4_video_bind_surfaces);
147203b705cfSriastradh		assert(n);
147303b705cfSriastradh		nbox -= n;
147403b705cfSriastradh
147503b705cfSriastradh		do {
147603b705cfSriastradh			BoxRec r;
147703b705cfSriastradh
147803b705cfSriastradh			r.x1 = box->x1 + pix_xoff;
147903b705cfSriastradh			r.x2 = box->x2 + pix_xoff;
148003b705cfSriastradh			r.y1 = box->y1 + pix_yoff;
148103b705cfSriastradh			r.y2 = box->y2 + pix_yoff;
148203b705cfSriastradh
148303b705cfSriastradh			OUT_VERTEX(r.x2, r.y2);
148403b705cfSriastradh			OUT_VERTEX_F(box->x2 * src_scale_x + src_offset_x);
148503b705cfSriastradh			OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
148603b705cfSriastradh
148703b705cfSriastradh			OUT_VERTEX(r.x1, r.y2);
148803b705cfSriastradh			OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
148903b705cfSriastradh			OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
149003b705cfSriastradh
149103b705cfSriastradh			OUT_VERTEX(r.x1, r.y1);
149203b705cfSriastradh			OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
149303b705cfSriastradh			OUT_VERTEX_F(box->y1 * src_scale_y + src_offset_y);
149403b705cfSriastradh
149503b705cfSriastradh			if (!DAMAGE_IS_ALL(priv->gpu_damage)) {
149603b705cfSriastradh				sna_damage_add_box(&priv->gpu_damage, &r);
149703b705cfSriastradh				sna_damage_subtract_box(&priv->cpu_damage, &r);
149803b705cfSriastradh			}
149903b705cfSriastradh			box++;
150003b705cfSriastradh		} while (--n);
150103b705cfSriastradh	} while (nbox);
150203b705cfSriastradh	gen4_vertex_flush(sna);
150303b705cfSriastradh
150403b705cfSriastradh	return true;
150503b705cfSriastradh}
150603b705cfSriastradh
150703b705cfSriastradhstatic int
150803b705cfSriastradhgen4_composite_picture(struct sna *sna,
150903b705cfSriastradh		       PicturePtr picture,
151003b705cfSriastradh		       struct sna_composite_channel *channel,
151103b705cfSriastradh		       int x, int y,
151203b705cfSriastradh		       int w, int h,
151303b705cfSriastradh		       int dst_x, int dst_y,
151403b705cfSriastradh		       bool precise)
151503b705cfSriastradh{
151603b705cfSriastradh	PixmapPtr pixmap;
151703b705cfSriastradh	uint32_t color;
151803b705cfSriastradh	int16_t dx, dy;
151903b705cfSriastradh
152003b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d), dst=(%d, %d)\n",
152103b705cfSriastradh	     __FUNCTION__, x, y, w, h, dst_x, dst_y));
152203b705cfSriastradh
152303b705cfSriastradh	channel->is_solid = false;
152403b705cfSriastradh	channel->card_format = -1;
152503b705cfSriastradh
152603b705cfSriastradh	if (sna_picture_is_solid(picture, &color))
152703b705cfSriastradh		return gen4_channel_init_solid(sna, channel, color);
152803b705cfSriastradh
152903b705cfSriastradh	if (picture->pDrawable == NULL) {
153003b705cfSriastradh		int ret;
153103b705cfSriastradh
153203b705cfSriastradh		if (picture->pSourcePict->type == SourcePictTypeLinear)
153303b705cfSriastradh			return gen4_channel_init_linear(sna, picture, channel,
153403b705cfSriastradh							x, y,
153503b705cfSriastradh							w, h,
153603b705cfSriastradh							dst_x, dst_y);
153703b705cfSriastradh
153803b705cfSriastradh		DBG(("%s -- fixup, gradient\n", __FUNCTION__));
153903b705cfSriastradh		ret = -1;
154003b705cfSriastradh		if (!precise)
154103b705cfSriastradh			ret = sna_render_picture_approximate_gradient(sna, picture, channel,
154203b705cfSriastradh								      x, y, w, h, dst_x, dst_y);
154303b705cfSriastradh		if (ret == -1)
154403b705cfSriastradh			ret = sna_render_picture_fixup(sna, picture, channel,
154503b705cfSriastradh						       x, y, w, h, dst_x, dst_y);
154603b705cfSriastradh		return ret;
154703b705cfSriastradh	}
154803b705cfSriastradh
154903b705cfSriastradh	if (picture->alphaMap) {
155003b705cfSriastradh		DBG(("%s -- fallback, alphamap\n", __FUNCTION__));
155103b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
155203b705cfSriastradh						x, y, w, h, dst_x, dst_y);
155303b705cfSriastradh	}
155403b705cfSriastradh
155503b705cfSriastradh	if (!gen4_check_repeat(picture)) {
155603b705cfSriastradh		DBG(("%s: unknown repeat mode fixup\n", __FUNCTION__));
155703b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
155803b705cfSriastradh						x, y, w, h, dst_x, dst_y);
155903b705cfSriastradh	}
156003b705cfSriastradh
156103b705cfSriastradh	if (!gen4_check_filter(picture)) {
156203b705cfSriastradh		DBG(("%s: unhandled filter fixup\n", __FUNCTION__));
156303b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
156403b705cfSriastradh						x, y, w, h, dst_x, dst_y);
156503b705cfSriastradh	}
156603b705cfSriastradh
156703b705cfSriastradh	channel->repeat = picture->repeat ? picture->repeatType : RepeatNone;
156803b705cfSriastradh	channel->filter = picture->filter;
156903b705cfSriastradh
157003b705cfSriastradh	pixmap = get_drawable_pixmap(picture->pDrawable);
157103b705cfSriastradh	get_drawable_deltas(picture->pDrawable, pixmap, &dx, &dy);
157203b705cfSriastradh
157303b705cfSriastradh	x += dx + picture->pDrawable->x;
157403b705cfSriastradh	y += dy + picture->pDrawable->y;
157503b705cfSriastradh
157603b705cfSriastradh	channel->is_affine = sna_transform_is_affine(picture->transform);
157742542f5fSchristos	if (sna_transform_is_imprecise_integer_translation(picture->transform, picture->filter, precise, &dx, &dy)) {
157803b705cfSriastradh		DBG(("%s: integer translation (%d, %d), removing\n",
157903b705cfSriastradh		     __FUNCTION__, dx, dy));
158003b705cfSriastradh		x += dx;
158103b705cfSriastradh		y += dy;
158203b705cfSriastradh		channel->transform = NULL;
158303b705cfSriastradh		channel->filter = PictFilterNearest;
158442542f5fSchristos
158542542f5fSchristos		if (channel->repeat &&
158642542f5fSchristos		    (x >= 0 &&
158742542f5fSchristos		     y >= 0 &&
158842542f5fSchristos		     x + w < pixmap->drawable.width &&
158942542f5fSchristos		     y + h < pixmap->drawable.height)) {
159042542f5fSchristos			struct sna_pixmap *priv = sna_pixmap(pixmap);
159142542f5fSchristos			if (priv && priv->clear) {
159242542f5fSchristos				DBG(("%s: converting large pixmap source into solid [%08x]\n", __FUNCTION__, priv->clear_color));
159342542f5fSchristos				return gen4_channel_init_solid(sna, channel, priv->clear_color);
159442542f5fSchristos			}
159542542f5fSchristos		}
159603b705cfSriastradh	} else
159703b705cfSriastradh		channel->transform = picture->transform;
159803b705cfSriastradh
159903b705cfSriastradh	channel->pict_format = picture->format;
160003b705cfSriastradh	channel->card_format = gen4_get_card_format(picture->format);
160103b705cfSriastradh	if (channel->card_format == -1)
160203b705cfSriastradh		return sna_render_picture_convert(sna, picture, channel, pixmap,
160303b705cfSriastradh						  x, y, w, h, dst_x, dst_y,
160403b705cfSriastradh						  false);
160503b705cfSriastradh
160603b705cfSriastradh	if (too_large(pixmap->drawable.width, pixmap->drawable.height))
160703b705cfSriastradh		return sna_render_picture_extract(sna, picture, channel,
160803b705cfSriastradh						  x, y, w, h, dst_x, dst_y);
160903b705cfSriastradh
161003b705cfSriastradh	return sna_render_pixmap_bo(sna, channel, pixmap,
161103b705cfSriastradh				    x, y, w, h, dst_x, dst_y);
161203b705cfSriastradh}
161303b705cfSriastradh
161403b705cfSriastradhstatic void gen4_composite_channel_convert(struct sna_composite_channel *channel)
161503b705cfSriastradh{
161603b705cfSriastradh	DBG(("%s: repeat %d -> %d, filter %d -> %d\n",
161703b705cfSriastradh	     __FUNCTION__,
161803b705cfSriastradh	     channel->repeat, gen4_repeat(channel->repeat),
161903b705cfSriastradh	     channel->filter, gen4_repeat(channel->filter)));
162003b705cfSriastradh	channel->repeat = gen4_repeat(channel->repeat);
162103b705cfSriastradh	channel->filter = gen4_filter(channel->filter);
162203b705cfSriastradh	if (channel->card_format == (unsigned)-1)
162303b705cfSriastradh		channel->card_format = gen4_get_card_format(channel->pict_format);
162403b705cfSriastradh}
162503b705cfSriastradh
162603b705cfSriastradhstatic void
162703b705cfSriastradhgen4_render_composite_done(struct sna *sna,
162803b705cfSriastradh			   const struct sna_composite_op *op)
162903b705cfSriastradh{
163003b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
163103b705cfSriastradh
163203b705cfSriastradh	if (sna->render.vertex_offset) {
163303b705cfSriastradh		gen4_vertex_flush(sna);
163403b705cfSriastradh		gen4_magic_ca_pass(sna, op);
163503b705cfSriastradh	}
163603b705cfSriastradh
163703b705cfSriastradh	if (op->mask.bo)
163803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->mask.bo);
163903b705cfSriastradh	if (op->src.bo)
164003b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->src.bo);
164103b705cfSriastradh
164203b705cfSriastradh	sna_render_composite_redirect_done(sna, op);
164303b705cfSriastradh}
164403b705cfSriastradh
164503b705cfSriastradhstatic bool
164603b705cfSriastradhgen4_composite_set_target(struct sna *sna,
164703b705cfSriastradh			  struct sna_composite_op *op,
164803b705cfSriastradh			  PicturePtr dst,
164903b705cfSriastradh			  int x, int y, int w, int h,
165003b705cfSriastradh			  bool partial)
165103b705cfSriastradh{
165203b705cfSriastradh	BoxRec box;
165342542f5fSchristos	unsigned hint;
165403b705cfSriastradh
165503b705cfSriastradh	op->dst.pixmap = get_drawable_pixmap(dst->pDrawable);
165603b705cfSriastradh	op->dst.width  = op->dst.pixmap->drawable.width;
165703b705cfSriastradh	op->dst.height = op->dst.pixmap->drawable.height;
165803b705cfSriastradh	op->dst.format = dst->format;
165903b705cfSriastradh	if (w && h) {
166003b705cfSriastradh		box.x1 = x;
166103b705cfSriastradh		box.y1 = y;
166203b705cfSriastradh		box.x2 = x + w;
166303b705cfSriastradh		box.y2 = y + h;
166403b705cfSriastradh	} else
166503b705cfSriastradh		sna_render_picture_extents(dst, &box);
166603b705cfSriastradh
166742542f5fSchristos	hint = PREFER_GPU | FORCE_GPU | RENDER_GPU;
166842542f5fSchristos	if (!partial) {
166942542f5fSchristos		hint |= IGNORE_DAMAGE;
167042542f5fSchristos		if (w == op->dst.width && h == op->dst.height)
167142542f5fSchristos			hint |= REPLACES;
167242542f5fSchristos	}
167342542f5fSchristos
167442542f5fSchristos	op->dst.bo = sna_drawable_use_bo(dst->pDrawable, hint, &box, &op->damage);
167503b705cfSriastradh	if (op->dst.bo == NULL)
167603b705cfSriastradh		return false;
167703b705cfSriastradh
167842542f5fSchristos	if (hint & REPLACES) {
167942542f5fSchristos		struct sna_pixmap *priv = sna_pixmap(op->dst.pixmap);
168042542f5fSchristos		kgem_bo_pair_undo(&sna->kgem, priv->gpu_bo, priv->cpu_bo);
168142542f5fSchristos	}
168242542f5fSchristos
168303b705cfSriastradh	get_drawable_deltas(dst->pDrawable, op->dst.pixmap,
168403b705cfSriastradh			    &op->dst.x, &op->dst.y);
168503b705cfSriastradh
168642542f5fSchristos	DBG(("%s: pixmap=%ld, format=%08x, size=%dx%d, pitch=%d, delta=(%d,%d),damage=%p\n",
168703b705cfSriastradh	     __FUNCTION__,
168842542f5fSchristos	     op->dst.pixmap->drawable.serialNumber, (int)op->dst.format,
168903b705cfSriastradh	     op->dst.width, op->dst.height,
169003b705cfSriastradh	     op->dst.bo->pitch,
169103b705cfSriastradh	     op->dst.x, op->dst.y,
169203b705cfSriastradh	     op->damage ? *op->damage : (void *)-1));
169303b705cfSriastradh
169403b705cfSriastradh	assert(op->dst.bo->proxy == NULL);
169503b705cfSriastradh
169603b705cfSriastradh	if (too_large(op->dst.width, op->dst.height) &&
169703b705cfSriastradh	    !sna_render_composite_redirect(sna, op, x, y, w, h, partial))
169803b705cfSriastradh		return false;
169903b705cfSriastradh
170003b705cfSriastradh	return true;
170103b705cfSriastradh}
170203b705cfSriastradh
170303b705cfSriastradhstatic bool
170403b705cfSriastradhcheck_gradient(PicturePtr picture, bool precise)
170503b705cfSriastradh{
170603b705cfSriastradh	switch (picture->pSourcePict->type) {
170703b705cfSriastradh	case SourcePictTypeSolidFill:
170803b705cfSriastradh	case SourcePictTypeLinear:
170903b705cfSriastradh		return false;
171003b705cfSriastradh	default:
171103b705cfSriastradh		return precise;
171203b705cfSriastradh	}
171303b705cfSriastradh}
171403b705cfSriastradh
171503b705cfSriastradhstatic bool
171603b705cfSriastradhhas_alphamap(PicturePtr p)
171703b705cfSriastradh{
171803b705cfSriastradh	return p->alphaMap != NULL;
171903b705cfSriastradh}
172003b705cfSriastradh
172103b705cfSriastradhstatic bool
172203b705cfSriastradhneed_upload(struct sna *sna, PicturePtr p)
172303b705cfSriastradh{
172403b705cfSriastradh	return p->pDrawable && untransformed(p) &&
172503b705cfSriastradh		!is_gpu(sna, p->pDrawable, PREFER_GPU_RENDER);
172603b705cfSriastradh}
172703b705cfSriastradh
172803b705cfSriastradhstatic bool
172903b705cfSriastradhsource_is_busy(PixmapPtr pixmap)
173003b705cfSriastradh{
173103b705cfSriastradh	struct sna_pixmap *priv = sna_pixmap(pixmap);
173203b705cfSriastradh	if (priv == NULL)
173303b705cfSriastradh		return false;
173403b705cfSriastradh
173503b705cfSriastradh	if (priv->clear)
173603b705cfSriastradh		return false;
173703b705cfSriastradh
173803b705cfSriastradh	if (priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo))
173903b705cfSriastradh		return true;
174003b705cfSriastradh
174103b705cfSriastradh	if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
174203b705cfSriastradh		return true;
174303b705cfSriastradh
174403b705cfSriastradh	return priv->gpu_damage && !priv->cpu_damage;
174503b705cfSriastradh}
174603b705cfSriastradh
174703b705cfSriastradhstatic bool
174803b705cfSriastradhsource_fallback(struct sna *sna, PicturePtr p, PixmapPtr pixmap, bool precise)
174903b705cfSriastradh{
175003b705cfSriastradh	if (sna_picture_is_solid(p, NULL))
175103b705cfSriastradh		return false;
175203b705cfSriastradh
175303b705cfSriastradh	if (p->pSourcePict)
175403b705cfSriastradh		return check_gradient(p, precise);
175503b705cfSriastradh
175603b705cfSriastradh	if (!gen4_check_repeat(p) || !gen4_check_format(p->format))
175703b705cfSriastradh		return true;
175803b705cfSriastradh
175903b705cfSriastradh	/* soft errors: perfer to upload/compute rather than readback */
176003b705cfSriastradh	if (pixmap && source_is_busy(pixmap))
176103b705cfSriastradh		return false;
176203b705cfSriastradh
176303b705cfSriastradh	return has_alphamap(p) || !gen4_check_filter(p) || need_upload(sna, p);
176403b705cfSriastradh}
176503b705cfSriastradh
176603b705cfSriastradhstatic bool
176703b705cfSriastradhgen4_composite_fallback(struct sna *sna,
176803b705cfSriastradh			PicturePtr src,
176903b705cfSriastradh			PicturePtr mask,
177003b705cfSriastradh			PicturePtr dst)
177103b705cfSriastradh{
177203b705cfSriastradh	PixmapPtr src_pixmap;
177303b705cfSriastradh	PixmapPtr mask_pixmap;
177403b705cfSriastradh	PixmapPtr dst_pixmap;
177503b705cfSriastradh	bool src_fallback, mask_fallback;
177603b705cfSriastradh
177703b705cfSriastradh	if (!gen4_check_dst_format(dst->format)) {
177803b705cfSriastradh		DBG(("%s: unknown destination format: %d\n",
177903b705cfSriastradh		     __FUNCTION__, dst->format));
178003b705cfSriastradh		return true;
178103b705cfSriastradh	}
178203b705cfSriastradh
178303b705cfSriastradh	dst_pixmap = get_drawable_pixmap(dst->pDrawable);
178403b705cfSriastradh
178503b705cfSriastradh	src_pixmap = src->pDrawable ? get_drawable_pixmap(src->pDrawable) : NULL;
178603b705cfSriastradh	src_fallback = source_fallback(sna, src, src_pixmap,
178703b705cfSriastradh				       dst->polyMode == PolyModePrecise);
178803b705cfSriastradh
178903b705cfSriastradh	if (mask) {
179003b705cfSriastradh		mask_pixmap = mask->pDrawable ? get_drawable_pixmap(mask->pDrawable) : NULL;
179103b705cfSriastradh		mask_fallback = source_fallback(sna, mask, mask_pixmap,
179203b705cfSriastradh						dst->polyMode == PolyModePrecise);
179303b705cfSriastradh	} else {
179403b705cfSriastradh		mask_pixmap = NULL;
179503b705cfSriastradh		mask_fallback = false;
179603b705cfSriastradh	}
179703b705cfSriastradh
179803b705cfSriastradh	/* If we are using the destination as a source and need to
179903b705cfSriastradh	 * readback in order to upload the source, do it all
180003b705cfSriastradh	 * on the cpu.
180103b705cfSriastradh	 */
180203b705cfSriastradh	if (src_pixmap == dst_pixmap && src_fallback) {
180303b705cfSriastradh		DBG(("%s: src is dst and will fallback\n",__FUNCTION__));
180403b705cfSriastradh		return true;
180503b705cfSriastradh	}
180603b705cfSriastradh	if (mask_pixmap == dst_pixmap && mask_fallback) {
180703b705cfSriastradh		DBG(("%s: mask is dst and will fallback\n",__FUNCTION__));
180803b705cfSriastradh		return true;
180903b705cfSriastradh	}
181003b705cfSriastradh
181103b705cfSriastradh	/* If anything is on the GPU, push everything out to the GPU */
181203b705cfSriastradh	if (dst_use_gpu(dst_pixmap)) {
181303b705cfSriastradh		DBG(("%s: dst is already on the GPU, try to use GPU\n",
181403b705cfSriastradh		     __FUNCTION__));
181503b705cfSriastradh		return false;
181603b705cfSriastradh	}
181703b705cfSriastradh
181803b705cfSriastradh	if (src_pixmap && !src_fallback) {
181903b705cfSriastradh		DBG(("%s: src is already on the GPU, try to use GPU\n",
182003b705cfSriastradh		     __FUNCTION__));
182103b705cfSriastradh		return false;
182203b705cfSriastradh	}
182303b705cfSriastradh	if (mask_pixmap && !mask_fallback) {
182403b705cfSriastradh		DBG(("%s: mask is already on the GPU, try to use GPU\n",
182503b705cfSriastradh		     __FUNCTION__));
182603b705cfSriastradh		return false;
182703b705cfSriastradh	}
182803b705cfSriastradh
182903b705cfSriastradh	/* However if the dst is not on the GPU and we need to
183003b705cfSriastradh	 * render one of the sources using the CPU, we may
183103b705cfSriastradh	 * as well do the entire operation in place onthe CPU.
183203b705cfSriastradh	 */
183303b705cfSriastradh	if (src_fallback) {
183403b705cfSriastradh		DBG(("%s: dst is on the CPU and src will fallback\n",
183503b705cfSriastradh		     __FUNCTION__));
183603b705cfSriastradh		return true;
183703b705cfSriastradh	}
183803b705cfSriastradh
183903b705cfSriastradh	if (mask_fallback) {
184003b705cfSriastradh		DBG(("%s: dst is on the CPU and mask will fallback\n",
184103b705cfSriastradh		     __FUNCTION__));
184203b705cfSriastradh		return true;
184303b705cfSriastradh	}
184403b705cfSriastradh
184503b705cfSriastradh	if (too_large(dst_pixmap->drawable.width,
184603b705cfSriastradh		      dst_pixmap->drawable.height) &&
184703b705cfSriastradh	    dst_is_cpu(dst_pixmap)) {
184803b705cfSriastradh		DBG(("%s: dst is on the CPU and too large\n", __FUNCTION__));
184903b705cfSriastradh		return true;
185003b705cfSriastradh	}
185103b705cfSriastradh
185203b705cfSriastradh	DBG(("%s: dst is not on the GPU and the operation should not fallback\n",
185303b705cfSriastradh	     __FUNCTION__));
185403b705cfSriastradh	return dst_use_cpu(dst_pixmap);
185503b705cfSriastradh}
185603b705cfSriastradh
185703b705cfSriastradhstatic int
185803b705cfSriastradhreuse_source(struct sna *sna,
185903b705cfSriastradh	     PicturePtr src, struct sna_composite_channel *sc, int src_x, int src_y,
186003b705cfSriastradh	     PicturePtr mask, struct sna_composite_channel *mc, int msk_x, int msk_y)
186103b705cfSriastradh{
186203b705cfSriastradh	uint32_t color;
186303b705cfSriastradh
186403b705cfSriastradh	if (src_x != msk_x || src_y != msk_y)
186503b705cfSriastradh		return false;
186603b705cfSriastradh
186703b705cfSriastradh	if (src == mask) {
186803b705cfSriastradh		DBG(("%s: mask is source\n", __FUNCTION__));
186903b705cfSriastradh		*mc = *sc;
187003b705cfSriastradh		mc->bo = kgem_bo_reference(mc->bo);
187103b705cfSriastradh		return true;
187203b705cfSriastradh	}
187303b705cfSriastradh
187403b705cfSriastradh	if (sna_picture_is_solid(mask, &color))
187503b705cfSriastradh		return gen4_channel_init_solid(sna, mc, color);
187603b705cfSriastradh
187703b705cfSriastradh	if (sc->is_solid)
187803b705cfSriastradh		return false;
187903b705cfSriastradh
188003b705cfSriastradh	if (src->pDrawable == NULL || mask->pDrawable != src->pDrawable)
188103b705cfSriastradh		return false;
188203b705cfSriastradh
188303b705cfSriastradh	DBG(("%s: mask reuses source drawable\n", __FUNCTION__));
188403b705cfSriastradh
188503b705cfSriastradh	if (!sna_transform_equal(src->transform, mask->transform))
188603b705cfSriastradh		return false;
188703b705cfSriastradh
188803b705cfSriastradh	if (!sna_picture_alphamap_equal(src, mask))
188903b705cfSriastradh		return false;
189003b705cfSriastradh
189103b705cfSriastradh	if (!gen4_check_repeat(mask))
189203b705cfSriastradh		return false;
189303b705cfSriastradh
189403b705cfSriastradh	if (!gen4_check_filter(mask))
189503b705cfSriastradh		return false;
189603b705cfSriastradh
189703b705cfSriastradh	if (!gen4_check_format(mask->format))
189803b705cfSriastradh		return false;
189903b705cfSriastradh
190003b705cfSriastradh	DBG(("%s: reusing source channel for mask with a twist\n",
190103b705cfSriastradh	     __FUNCTION__));
190203b705cfSriastradh
190303b705cfSriastradh	*mc = *sc;
190403b705cfSriastradh	mc->repeat = gen4_repeat(mask->repeat ? mask->repeatType : RepeatNone);
190503b705cfSriastradh	mc->filter = gen4_filter(mask->filter);
190603b705cfSriastradh	mc->pict_format = mask->format;
190703b705cfSriastradh	mc->card_format = gen4_get_card_format(mask->format);
190803b705cfSriastradh	mc->bo = kgem_bo_reference(mc->bo);
190903b705cfSriastradh	return true;
191003b705cfSriastradh}
191103b705cfSriastradh
191203b705cfSriastradhstatic bool
191303b705cfSriastradhgen4_render_composite(struct sna *sna,
191403b705cfSriastradh		      uint8_t op,
191503b705cfSriastradh		      PicturePtr src,
191603b705cfSriastradh		      PicturePtr mask,
191703b705cfSriastradh		      PicturePtr dst,
191803b705cfSriastradh		      int16_t src_x, int16_t src_y,
191903b705cfSriastradh		      int16_t msk_x, int16_t msk_y,
192003b705cfSriastradh		      int16_t dst_x, int16_t dst_y,
192103b705cfSriastradh		      int16_t width, int16_t height,
192242542f5fSchristos		      unsigned flags,
192303b705cfSriastradh		      struct sna_composite_op *tmp)
192403b705cfSriastradh{
192503b705cfSriastradh	DBG(("%s: %dx%d, current mode=%d\n", __FUNCTION__,
192603b705cfSriastradh	     width, height, sna->kgem.mode));
192703b705cfSriastradh
192803b705cfSriastradh	if (op >= ARRAY_SIZE(gen4_blend_op))
192903b705cfSriastradh		return false;
193003b705cfSriastradh
193103b705cfSriastradh	if (mask == NULL &&
193203b705cfSriastradh	    sna_blt_composite(sna, op,
193303b705cfSriastradh			      src, dst,
193403b705cfSriastradh			      src_x, src_y,
193503b705cfSriastradh			      dst_x, dst_y,
193603b705cfSriastradh			      width, height,
193742542f5fSchristos			      flags, tmp))
193803b705cfSriastradh		return true;
193903b705cfSriastradh
194003b705cfSriastradh	if (gen4_composite_fallback(sna, src, mask, dst))
194142542f5fSchristos		goto fallback;
194203b705cfSriastradh
194303b705cfSriastradh	if (need_tiling(sna, width, height))
194403b705cfSriastradh		return sna_tiling_composite(op, src, mask, dst,
194503b705cfSriastradh					    src_x, src_y,
194603b705cfSriastradh					    msk_x, msk_y,
194703b705cfSriastradh					    dst_x, dst_y,
194803b705cfSriastradh					    width, height,
194903b705cfSriastradh					    tmp);
195003b705cfSriastradh
195103b705cfSriastradh	if (!gen4_composite_set_target(sna, tmp, dst,
195203b705cfSriastradh				       dst_x, dst_y, width, height,
195342542f5fSchristos				       flags & COMPOSITE_PARTIAL || op > PictOpSrc)) {
195403b705cfSriastradh		DBG(("%s: failed to set composite target\n", __FUNCTION__));
195542542f5fSchristos		goto fallback;
195603b705cfSriastradh	}
195703b705cfSriastradh
195803b705cfSriastradh	tmp->op = op;
195903b705cfSriastradh	switch (gen4_composite_picture(sna, src, &tmp->src,
196003b705cfSriastradh				       src_x, src_y,
196103b705cfSriastradh				       width, height,
196203b705cfSriastradh				       dst_x, dst_y,
196303b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
196403b705cfSriastradh	case -1:
196503b705cfSriastradh		DBG(("%s: failed to prepare source\n", __FUNCTION__));
196603b705cfSriastradh		goto cleanup_dst;
196703b705cfSriastradh	case 0:
196803b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->src, 0))
196903b705cfSriastradh			goto cleanup_dst;
197003b705cfSriastradh		/* fall through to fixup */
197103b705cfSriastradh	case 1:
197203b705cfSriastradh		if (mask == NULL &&
197303b705cfSriastradh		    sna_blt_composite__convert(sna,
197403b705cfSriastradh					       dst_x, dst_y, width, height,
197503b705cfSriastradh					       tmp))
197603b705cfSriastradh			return true;
197703b705cfSriastradh
197803b705cfSriastradh		gen4_composite_channel_convert(&tmp->src);
197903b705cfSriastradh		break;
198003b705cfSriastradh	}
198103b705cfSriastradh
198203b705cfSriastradh	tmp->is_affine = tmp->src.is_affine;
198303b705cfSriastradh	tmp->has_component_alpha = false;
198403b705cfSriastradh	tmp->need_magic_ca_pass = false;
198503b705cfSriastradh
198603b705cfSriastradh	if (mask) {
198703b705cfSriastradh		if (mask->componentAlpha && PICT_FORMAT_RGB(mask->format)) {
198803b705cfSriastradh			tmp->has_component_alpha = true;
198903b705cfSriastradh
199003b705cfSriastradh			/* Check if it's component alpha that relies on a source alpha and on
199103b705cfSriastradh			 * the source value.  We can only get one of those into the single
199203b705cfSriastradh			 * source value that we get to blend with.
199303b705cfSriastradh			 */
199403b705cfSriastradh			if (gen4_blend_op[op].src_alpha &&
199503b705cfSriastradh			    (gen4_blend_op[op].src_blend != GEN4_BLENDFACTOR_ZERO)) {
199603b705cfSriastradh				if (op != PictOpOver) {
199703b705cfSriastradh					DBG(("%s -- fallback: unhandled component alpha blend\n",
199803b705cfSriastradh					     __FUNCTION__));
199903b705cfSriastradh
200003b705cfSriastradh					goto cleanup_src;
200103b705cfSriastradh				}
200203b705cfSriastradh
200303b705cfSriastradh				tmp->need_magic_ca_pass = true;
200403b705cfSriastradh				tmp->op = PictOpOutReverse;
200503b705cfSriastradh			}
200603b705cfSriastradh		}
200703b705cfSriastradh
200803b705cfSriastradh		if (!reuse_source(sna,
200903b705cfSriastradh				  src, &tmp->src, src_x, src_y,
201003b705cfSriastradh				  mask, &tmp->mask, msk_x, msk_y)) {
201103b705cfSriastradh			switch (gen4_composite_picture(sna, mask, &tmp->mask,
201203b705cfSriastradh						       msk_x, msk_y,
201303b705cfSriastradh						       width, height,
201403b705cfSriastradh						       dst_x, dst_y,
201503b705cfSriastradh						       dst->polyMode == PolyModePrecise)) {
201603b705cfSriastradh			case -1:
201703b705cfSriastradh				DBG(("%s: failed to prepare mask\n", __FUNCTION__));
201803b705cfSriastradh				goto cleanup_src;
201903b705cfSriastradh			case 0:
202003b705cfSriastradh				if (!gen4_channel_init_solid(sna, &tmp->mask, 0))
202103b705cfSriastradh					goto cleanup_src;
202203b705cfSriastradh				/* fall through to fixup */
202303b705cfSriastradh			case 1:
202403b705cfSriastradh				gen4_composite_channel_convert(&tmp->mask);
202503b705cfSriastradh				break;
202603b705cfSriastradh			}
202703b705cfSriastradh		}
202803b705cfSriastradh
202903b705cfSriastradh		tmp->is_affine &= tmp->mask.is_affine;
203003b705cfSriastradh	}
203103b705cfSriastradh
203203b705cfSriastradh	tmp->u.gen4.wm_kernel =
203303b705cfSriastradh		gen4_choose_composite_kernel(tmp->op,
203403b705cfSriastradh					     tmp->mask.bo != NULL,
203503b705cfSriastradh					     tmp->has_component_alpha,
203603b705cfSriastradh					     tmp->is_affine);
203703b705cfSriastradh	tmp->u.gen4.ve_id = gen4_choose_composite_emitter(sna, tmp);
203803b705cfSriastradh
203903b705cfSriastradh	tmp->blt   = gen4_render_composite_blt;
204003b705cfSriastradh	tmp->box   = gen4_render_composite_box;
204103b705cfSriastradh	tmp->boxes = gen4_render_composite_boxes__blt;
204203b705cfSriastradh	if (tmp->emit_boxes) {
204303b705cfSriastradh		tmp->boxes = gen4_render_composite_boxes;
204403b705cfSriastradh#if !FORCE_FLUSH
204503b705cfSriastradh		tmp->thread_boxes = gen4_render_composite_boxes__thread;
204603b705cfSriastradh#endif
204703b705cfSriastradh	}
204803b705cfSriastradh	tmp->done  = gen4_render_composite_done;
204903b705cfSriastradh
205003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
205103b705cfSriastradh			   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
205203b705cfSriastradh			   NULL)) {
205303b705cfSriastradh		kgem_submit(&sna->kgem);
205403b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
205503b705cfSriastradh				     tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
205603b705cfSriastradh				     NULL))
205703b705cfSriastradh			goto cleanup_mask;
205803b705cfSriastradh	}
205903b705cfSriastradh
206003b705cfSriastradh	gen4_align_vertex(sna, tmp);
206142542f5fSchristos	gen4_bind_surfaces(sna, tmp);
206203b705cfSriastradh	return true;
206303b705cfSriastradh
206403b705cfSriastradhcleanup_mask:
206542542f5fSchristos	if (tmp->mask.bo) {
206603b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->mask.bo);
206742542f5fSchristos		tmp->mask.bo = NULL;
206842542f5fSchristos	}
206903b705cfSriastradhcleanup_src:
207042542f5fSchristos	if (tmp->src.bo) {
207103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->src.bo);
207242542f5fSchristos		tmp->src.bo = NULL;
207342542f5fSchristos	}
207403b705cfSriastradhcleanup_dst:
207542542f5fSchristos	if (tmp->redirect.real_bo) {
207603b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->dst.bo);
207742542f5fSchristos		tmp->redirect.real_bo = NULL;
207842542f5fSchristos	}
207942542f5fSchristosfallback:
208042542f5fSchristos	return (mask == NULL &&
208142542f5fSchristos		sna_blt_composite(sna, op,
208242542f5fSchristos				  src, dst,
208342542f5fSchristos				  src_x, src_y,
208442542f5fSchristos				  dst_x, dst_y,
208542542f5fSchristos				  width, height,
208642542f5fSchristos				  flags | COMPOSITE_FALLBACK, tmp));
208703b705cfSriastradh}
208803b705cfSriastradh
208903b705cfSriastradh#if !NO_COMPOSITE_SPANS
209003b705cfSriastradhfastcall static void
209103b705cfSriastradhgen4_render_composite_spans_box(struct sna *sna,
209203b705cfSriastradh				const struct sna_composite_spans_op *op,
209303b705cfSriastradh				const BoxRec *box, float opacity)
209403b705cfSriastradh{
209503b705cfSriastradh	DBG(("%s: src=+(%d, %d), opacity=%f, dst=+(%d, %d), box=(%d, %d) x (%d, %d)\n",
209603b705cfSriastradh	     __FUNCTION__,
209703b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
209803b705cfSriastradh	     opacity,
209903b705cfSriastradh	     op->base.dst.x, op->base.dst.y,
210003b705cfSriastradh	     box->x1, box->y1,
210103b705cfSriastradh	     box->x2 - box->x1,
210203b705cfSriastradh	     box->y2 - box->y1));
210303b705cfSriastradh
210403b705cfSriastradh	gen4_get_rectangles(sna, &op->base, 1, gen4_bind_surfaces);
210503b705cfSriastradh	op->prim_emit(sna, op, box, opacity);
210603b705cfSriastradh}
210703b705cfSriastradh
210803b705cfSriastradhstatic void
210903b705cfSriastradhgen4_render_composite_spans_boxes(struct sna *sna,
211003b705cfSriastradh				  const struct sna_composite_spans_op *op,
211103b705cfSriastradh				  const BoxRec *box, int nbox,
211203b705cfSriastradh				  float opacity)
211303b705cfSriastradh{
211403b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), opacity=%f, dst=+(%d, %d)\n",
211503b705cfSriastradh	     __FUNCTION__, nbox,
211603b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
211703b705cfSriastradh	     opacity,
211803b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
211903b705cfSriastradh
212003b705cfSriastradh	do {
212103b705cfSriastradh		int nbox_this_time;
212203b705cfSriastradh
212303b705cfSriastradh		nbox_this_time = gen4_get_rectangles(sna, &op->base, nbox,
212403b705cfSriastradh						     gen4_bind_surfaces);
212503b705cfSriastradh		nbox -= nbox_this_time;
212603b705cfSriastradh
212703b705cfSriastradh		do {
212803b705cfSriastradh			DBG(("  %s: (%d, %d) x (%d, %d)\n", __FUNCTION__,
212903b705cfSriastradh			     box->x1, box->y1,
213003b705cfSriastradh			     box->x2 - box->x1,
213103b705cfSriastradh			     box->y2 - box->y1));
213203b705cfSriastradh
213303b705cfSriastradh			op->prim_emit(sna, op, box++, opacity);
213403b705cfSriastradh		} while (--nbox_this_time);
213503b705cfSriastradh	} while (nbox);
213603b705cfSriastradh}
213703b705cfSriastradh
213803b705cfSriastradhfastcall static void
213903b705cfSriastradhgen4_render_composite_spans_boxes__thread(struct sna *sna,
214003b705cfSriastradh					  const struct sna_composite_spans_op *op,
214103b705cfSriastradh					  const struct sna_opacity_box *box,
214203b705cfSriastradh					  int nbox)
214303b705cfSriastradh{
214403b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), dst=+(%d, %d)\n",
214503b705cfSriastradh	     __FUNCTION__, nbox,
214603b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
214703b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
214803b705cfSriastradh	assert(nbox);
214903b705cfSriastradh
215003b705cfSriastradh	sna_vertex_lock(&sna->render);
215103b705cfSriastradh	do {
215203b705cfSriastradh		int nbox_this_time;
215303b705cfSriastradh		float *v;
215403b705cfSriastradh
215503b705cfSriastradh		nbox_this_time = gen4_get_rectangles(sna, &op->base, nbox,
215603b705cfSriastradh						     gen4_bind_surfaces);
215703b705cfSriastradh		assert(nbox_this_time);
215803b705cfSriastradh		nbox -= nbox_this_time;
215903b705cfSriastradh
216003b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
216103b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->base.floats_per_rect;
216203b705cfSriastradh
216303b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
216403b705cfSriastradh		sna_vertex_unlock(&sna->render);
216503b705cfSriastradh
216603b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
216703b705cfSriastradh		box += nbox_this_time;
216803b705cfSriastradh
216903b705cfSriastradh		sna_vertex_lock(&sna->render);
217003b705cfSriastradh		sna_vertex_release__locked(&sna->render);
217103b705cfSriastradh	} while (nbox);
217203b705cfSriastradh	sna_vertex_unlock(&sna->render);
217303b705cfSriastradh}
217403b705cfSriastradh
217503b705cfSriastradhfastcall static void
217603b705cfSriastradhgen4_render_composite_spans_done(struct sna *sna,
217703b705cfSriastradh				 const struct sna_composite_spans_op *op)
217803b705cfSriastradh{
217903b705cfSriastradh	if (sna->render.vertex_offset)
218003b705cfSriastradh		gen4_vertex_flush(sna);
218103b705cfSriastradh
218203b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
218303b705cfSriastradh
218403b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
218503b705cfSriastradh	sna_render_composite_redirect_done(sna, &op->base);
218603b705cfSriastradh}
218703b705cfSriastradh
218803b705cfSriastradhstatic bool
218903b705cfSriastradhgen4_check_composite_spans(struct sna *sna,
219003b705cfSriastradh			   uint8_t op, PicturePtr src, PicturePtr dst,
219103b705cfSriastradh			   int16_t width, int16_t height,
219203b705cfSriastradh			   unsigned flags)
219303b705cfSriastradh{
219403b705cfSriastradh	DBG(("%s: op=%d, width=%d, height=%d, flags=%x\n",
219503b705cfSriastradh	     __FUNCTION__, op, width, height, flags));
219603b705cfSriastradh
219703b705cfSriastradh	if (op >= ARRAY_SIZE(gen4_blend_op))
219803b705cfSriastradh		return false;
219903b705cfSriastradh
220003b705cfSriastradh	if (gen4_composite_fallback(sna, src, NULL, dst)) {
220103b705cfSriastradh		DBG(("%s: operation would fallback\n", __FUNCTION__));
220203b705cfSriastradh		return false;
220303b705cfSriastradh	}
220403b705cfSriastradh
220503b705cfSriastradh	if (need_tiling(sna, width, height) &&
220603b705cfSriastradh	    !is_gpu(sna, dst->pDrawable, PREFER_GPU_SPANS)) {
220703b705cfSriastradh		DBG(("%s: fallback, tiled operation not on GPU\n",
220803b705cfSriastradh		     __FUNCTION__));
220903b705cfSriastradh		return false;
221003b705cfSriastradh	}
221103b705cfSriastradh
221203b705cfSriastradh	if (FORCE_SPANS)
221303b705cfSriastradh		return FORCE_SPANS > 0;
221403b705cfSriastradh
221503b705cfSriastradh	if ((flags & COMPOSITE_SPANS_RECTILINEAR) == 0) {
221603b705cfSriastradh		struct sna_pixmap *priv;
221703b705cfSriastradh
221803b705cfSriastradh		if (FORCE_NONRECTILINEAR_SPANS)
221903b705cfSriastradh			return FORCE_NONRECTILINEAR_SPANS > 0;
222003b705cfSriastradh
222103b705cfSriastradh		if ((sna->render.prefer_gpu & PREFER_GPU_SPANS) == 0)
222203b705cfSriastradh			return false;
222303b705cfSriastradh
222403b705cfSriastradh		priv = sna_pixmap_from_drawable(dst->pDrawable);
222503b705cfSriastradh		assert(priv);
222603b705cfSriastradh
222703b705cfSriastradh		if (priv->cpu_bo &&
222803b705cfSriastradh		    __kgem_bo_is_busy(&sna->kgem, priv->cpu_bo))
222903b705cfSriastradh			return true;
223003b705cfSriastradh
223103b705cfSriastradh		if (flags & COMPOSITE_SPANS_INPLACE_HINT)
223203b705cfSriastradh			return false;
223303b705cfSriastradh
223403b705cfSriastradh		return priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo);
223503b705cfSriastradh	}
223603b705cfSriastradh
223703b705cfSriastradh	return true;
223803b705cfSriastradh}
223903b705cfSriastradh
224003b705cfSriastradhstatic bool
224103b705cfSriastradhgen4_render_composite_spans(struct sna *sna,
224203b705cfSriastradh			    uint8_t op,
224303b705cfSriastradh			    PicturePtr src,
224403b705cfSriastradh			    PicturePtr dst,
224503b705cfSriastradh			    int16_t src_x,  int16_t src_y,
224603b705cfSriastradh			    int16_t dst_x,  int16_t dst_y,
224703b705cfSriastradh			    int16_t width,  int16_t height,
224803b705cfSriastradh			    unsigned flags,
224903b705cfSriastradh			    struct sna_composite_spans_op *tmp)
225003b705cfSriastradh{
225103b705cfSriastradh	DBG(("%s: %dx%d with flags=%x, current mode=%d\n", __FUNCTION__,
225203b705cfSriastradh	     width, height, flags, sna->kgem.ring));
225303b705cfSriastradh
225403b705cfSriastradh	assert(gen4_check_composite_spans(sna, op, src, dst, width, height, flags));
225503b705cfSriastradh
225603b705cfSriastradh	if (need_tiling(sna, width, height)) {
225703b705cfSriastradh		DBG(("%s: tiling, operation (%dx%d) too wide for pipeline\n",
225803b705cfSriastradh		     __FUNCTION__, width, height));
225903b705cfSriastradh		return sna_tiling_composite_spans(op, src, dst,
226003b705cfSriastradh						  src_x, src_y, dst_x, dst_y,
226103b705cfSriastradh						  width, height, flags, tmp);
226203b705cfSriastradh	}
226303b705cfSriastradh
226403b705cfSriastradh	tmp->base.op = op;
226503b705cfSriastradh	if (!gen4_composite_set_target(sna, &tmp->base, dst,
226603b705cfSriastradh				       dst_x, dst_y, width, height, true))
226703b705cfSriastradh		return false;
226803b705cfSriastradh
226903b705cfSriastradh	switch (gen4_composite_picture(sna, src, &tmp->base.src,
227003b705cfSriastradh				       src_x, src_y,
227103b705cfSriastradh				       width, height,
227203b705cfSriastradh				       dst_x, dst_y,
227303b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
227403b705cfSriastradh	case -1:
227503b705cfSriastradh		goto cleanup_dst;
227603b705cfSriastradh	case 0:
227703b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->base.src, 0))
227803b705cfSriastradh			goto cleanup_dst;
227903b705cfSriastradh		/* fall through to fixup */
228003b705cfSriastradh	case 1:
228103b705cfSriastradh		gen4_composite_channel_convert(&tmp->base.src);
228203b705cfSriastradh		break;
228303b705cfSriastradh	}
228403b705cfSriastradh
228503b705cfSriastradh	tmp->base.mask.bo = NULL;
228603b705cfSriastradh	tmp->base.mask.filter = SAMPLER_FILTER_NEAREST;
228703b705cfSriastradh	tmp->base.mask.repeat = SAMPLER_EXTEND_NONE;
228803b705cfSriastradh
228903b705cfSriastradh	tmp->base.is_affine = tmp->base.src.is_affine;
229003b705cfSriastradh	tmp->base.has_component_alpha = false;
229103b705cfSriastradh	tmp->base.need_magic_ca_pass = false;
229203b705cfSriastradh
229303b705cfSriastradh	tmp->base.u.gen4.ve_id = gen4_choose_spans_emitter(sna, tmp);
229403b705cfSriastradh	tmp->base.u.gen4.wm_kernel = WM_KERNEL_OPACITY | !tmp->base.is_affine;
229503b705cfSriastradh
229603b705cfSriastradh	tmp->box   = gen4_render_composite_spans_box;
229703b705cfSriastradh	tmp->boxes = gen4_render_composite_spans_boxes;
229803b705cfSriastradh	if (tmp->emit_boxes)
229903b705cfSriastradh		tmp->thread_boxes = gen4_render_composite_spans_boxes__thread;
230003b705cfSriastradh	tmp->done  = gen4_render_composite_spans_done;
230103b705cfSriastradh
230203b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
230303b705cfSriastradh			   tmp->base.dst.bo, tmp->base.src.bo,
230403b705cfSriastradh			   NULL))  {
230503b705cfSriastradh		kgem_submit(&sna->kgem);
230603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
230703b705cfSriastradh				   tmp->base.dst.bo, tmp->base.src.bo,
230803b705cfSriastradh				   NULL))
230903b705cfSriastradh			goto cleanup_src;
231003b705cfSriastradh	}
231103b705cfSriastradh
231203b705cfSriastradh	gen4_align_vertex(sna, &tmp->base);
231342542f5fSchristos	gen4_bind_surfaces(sna, &tmp->base);
231403b705cfSriastradh	return true;
231503b705cfSriastradh
231603b705cfSriastradhcleanup_src:
231703b705cfSriastradh	if (tmp->base.src.bo)
231803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.src.bo);
231903b705cfSriastradhcleanup_dst:
232003b705cfSriastradh	if (tmp->base.redirect.real_bo)
232103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.dst.bo);
232203b705cfSriastradh	return false;
232303b705cfSriastradh}
232403b705cfSriastradh#endif
232503b705cfSriastradh
232603b705cfSriastradhstatic void
232703b705cfSriastradhgen4_copy_bind_surfaces(struct sna *sna, const struct sna_composite_op *op)
232803b705cfSriastradh{
232903b705cfSriastradh	uint32_t *binding_table;
233042542f5fSchristos	uint16_t offset, dirty;
233103b705cfSriastradh
233203b705cfSriastradh	gen4_get_batch(sna, op);
233342542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
233403b705cfSriastradh
233503b705cfSriastradh	binding_table = gen4_composite_get_binding_table(sna, &offset);
233603b705cfSriastradh
233703b705cfSriastradh	binding_table[0] =
233803b705cfSriastradh		gen4_bind_bo(sna,
233903b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
234003b705cfSriastradh			     gen4_get_dest_format(op->dst.format),
234103b705cfSriastradh			     true);
234203b705cfSriastradh	binding_table[1] =
234303b705cfSriastradh		gen4_bind_bo(sna,
234403b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
234503b705cfSriastradh			     op->src.card_format,
234603b705cfSriastradh			     false);
234703b705cfSriastradh
234803b705cfSriastradh	if (sna->kgem.surface == offset &&
234903b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen4.surface_table) == *(uint64_t*)binding_table) {
235003b705cfSriastradh		sna->kgem.surface += sizeof(struct gen4_surface_state_padded) / sizeof(uint32_t);
235103b705cfSriastradh		offset = sna->render_state.gen4.surface_table;
235203b705cfSriastradh	}
235303b705cfSriastradh
235442542f5fSchristos	if (!ALWAYS_FLUSH && sna->kgem.batch[sna->render_state.gen4.surface_table] == binding_table[0])
235542542f5fSchristos		dirty = 0;
235642542f5fSchristos
235703b705cfSriastradh	gen4_emit_state(sna, op, offset | dirty);
235803b705cfSriastradh}
235903b705cfSriastradh
236003b705cfSriastradhstatic void
236103b705cfSriastradhgen4_render_copy_one(struct sna *sna,
236203b705cfSriastradh		     const struct sna_composite_op *op,
236303b705cfSriastradh		     int sx, int sy,
236403b705cfSriastradh		     int w, int h,
236503b705cfSriastradh		     int dx, int dy)
236603b705cfSriastradh{
236703b705cfSriastradh	gen4_get_rectangles(sna, op, 1, gen4_copy_bind_surfaces);
236803b705cfSriastradh
236903b705cfSriastradh	OUT_VERTEX(dx+w, dy+h);
237003b705cfSriastradh	OUT_VERTEX_F((sx+w)*op->src.scale[0]);
237103b705cfSriastradh	OUT_VERTEX_F((sy+h)*op->src.scale[1]);
237203b705cfSriastradh
237303b705cfSriastradh	OUT_VERTEX(dx, dy+h);
237403b705cfSriastradh	OUT_VERTEX_F(sx*op->src.scale[0]);
237503b705cfSriastradh	OUT_VERTEX_F((sy+h)*op->src.scale[1]);
237603b705cfSriastradh
237703b705cfSriastradh	OUT_VERTEX(dx, dy);
237803b705cfSriastradh	OUT_VERTEX_F(sx*op->src.scale[0]);
237903b705cfSriastradh	OUT_VERTEX_F(sy*op->src.scale[1]);
238003b705cfSriastradh}
238103b705cfSriastradh
238203b705cfSriastradhstatic bool
238303b705cfSriastradhgen4_render_copy_boxes(struct sna *sna, uint8_t alu,
238442542f5fSchristos		       const DrawableRec *src, struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
238542542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
238603b705cfSriastradh		       const BoxRec *box, int n, unsigned flags)
238703b705cfSriastradh{
238803b705cfSriastradh	struct sna_composite_op tmp;
238903b705cfSriastradh
239003b705cfSriastradh	DBG(("%s x %d\n", __FUNCTION__, n));
239103b705cfSriastradh
239242542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
239303b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
239403b705cfSriastradh			       src_bo, src_dx, src_dy,
239503b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
239642542f5fSchristos			       dst->bitsPerPixel,
239703b705cfSriastradh			       box, n))
239803b705cfSriastradh		return true;
239903b705cfSriastradh
240003b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo) {
240103b705cfSriastradhfallback_blt:
240242542f5fSchristos		if (!sna_blt_compare_depth(src, dst))
240303b705cfSriastradh			return false;
240403b705cfSriastradh
240503b705cfSriastradh		return sna_blt_copy_boxes_fallback(sna, alu,
240603b705cfSriastradh						   src, src_bo, src_dx, src_dy,
240703b705cfSriastradh						   dst, dst_bo, dst_dx, dst_dy,
240803b705cfSriastradh						   box, n);
240903b705cfSriastradh	}
241003b705cfSriastradh
241103b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
241203b705cfSriastradh
241303b705cfSriastradh	DBG(("%s (%d, %d)->(%d, %d) x %d\n",
241403b705cfSriastradh	     __FUNCTION__, src_dx, src_dy, dst_dx, dst_dy, n));
241503b705cfSriastradh
241642542f5fSchristos	if (dst->depth == src->depth) {
241742542f5fSchristos		tmp.dst.format = sna_render_format_for_depth(dst->depth);
241803b705cfSriastradh		tmp.src.pict_format = tmp.dst.format;
241903b705cfSriastradh	} else {
242042542f5fSchristos		tmp.dst.format = sna_format_for_depth(dst->depth);
242142542f5fSchristos		tmp.src.pict_format = sna_format_for_depth(src->depth);
242203b705cfSriastradh	}
242303b705cfSriastradh	if (!gen4_check_format(tmp.src.pict_format))
242403b705cfSriastradh		goto fallback_blt;
242503b705cfSriastradh
242603b705cfSriastradh	tmp.op = alu == GXcopy ? PictOpSrc : PictOpClear;
242703b705cfSriastradh
242842542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
242942542f5fSchristos	tmp.dst.width  = dst->width;
243042542f5fSchristos	tmp.dst.height = dst->height;
243103b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
243203b705cfSriastradh	tmp.dst.bo = dst_bo;
243303b705cfSriastradh	tmp.damage = NULL;
243403b705cfSriastradh
243503b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
243603b705cfSriastradh	if (too_large(tmp.dst.width, tmp.dst.height)) {
243703b705cfSriastradh		BoxRec extents = box[0];
243803b705cfSriastradh		int i;
243903b705cfSriastradh
244003b705cfSriastradh		for (i = 1; i < n; i++) {
244103b705cfSriastradh			if (box[i].x1 < extents.x1)
244203b705cfSriastradh				extents.x1 = box[i].x1;
244303b705cfSriastradh			if (box[i].y1 < extents.y1)
244403b705cfSriastradh				extents.y1 = box[i].y1;
244503b705cfSriastradh
244603b705cfSriastradh			if (box[i].x2 > extents.x2)
244703b705cfSriastradh				extents.x2 = box[i].x2;
244803b705cfSriastradh			if (box[i].y2 > extents.y2)
244903b705cfSriastradh				extents.y2 = box[i].y2;
245003b705cfSriastradh		}
245103b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
245203b705cfSriastradh						   extents.x1 + dst_dx,
245303b705cfSriastradh						   extents.y1 + dst_dy,
245403b705cfSriastradh						   extents.x2 - extents.x1,
245503b705cfSriastradh						   extents.y2 - extents.y1,
245603b705cfSriastradh						   n > 1))
245703b705cfSriastradh			goto fallback_tiled;
245803b705cfSriastradh	}
245903b705cfSriastradh
246003b705cfSriastradh	tmp.src.filter = SAMPLER_FILTER_NEAREST;
246103b705cfSriastradh	tmp.src.repeat = SAMPLER_EXTEND_NONE;
246203b705cfSriastradh	tmp.src.card_format = gen4_get_card_format(tmp.src.pict_format);
246342542f5fSchristos	if (too_large(src->width, src->height)) {
246403b705cfSriastradh		BoxRec extents = box[0];
246503b705cfSriastradh		int i;
246603b705cfSriastradh
246703b705cfSriastradh		for (i = 1; i < n; i++) {
246803b705cfSriastradh			if (box[i].x1 < extents.x1)
246903b705cfSriastradh				extents.x1 = box[i].x1;
247003b705cfSriastradh			if (box[i].y1 < extents.y1)
247103b705cfSriastradh				extents.y1 = box[i].y1;
247203b705cfSriastradh
247303b705cfSriastradh			if (box[i].x2 > extents.x2)
247403b705cfSriastradh				extents.x2 = box[i].x2;
247503b705cfSriastradh			if (box[i].y2 > extents.y2)
247603b705cfSriastradh				extents.y2 = box[i].y2;
247703b705cfSriastradh		}
247803b705cfSriastradh
247903b705cfSriastradh		if (!sna_render_pixmap_partial(sna, src, src_bo, &tmp.src,
248003b705cfSriastradh					       extents.x1 + src_dx,
248103b705cfSriastradh					       extents.y1 + src_dy,
248203b705cfSriastradh					       extents.x2 - extents.x1,
248303b705cfSriastradh					       extents.y2 - extents.y1))
248403b705cfSriastradh			goto fallback_tiled_dst;
248503b705cfSriastradh	} else {
248603b705cfSriastradh		tmp.src.bo = kgem_bo_reference(src_bo);
248742542f5fSchristos		tmp.src.width  = src->width;
248842542f5fSchristos		tmp.src.height = src->height;
248903b705cfSriastradh		tmp.src.offset[0] = tmp.src.offset[1] = 0;
249042542f5fSchristos		tmp.src.scale[0] = 1.f/src->width;
249142542f5fSchristos		tmp.src.scale[1] = 1.f/src->height;
249203b705cfSriastradh	}
249303b705cfSriastradh
249403b705cfSriastradh	tmp.is_affine = true;
249503b705cfSriastradh	tmp.floats_per_vertex = 3;
249603b705cfSriastradh	tmp.floats_per_rect = 9;
249703b705cfSriastradh	tmp.u.gen4.wm_kernel = WM_KERNEL;
249803b705cfSriastradh	tmp.u.gen4.ve_id = 2;
249903b705cfSriastradh
250003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
250103b705cfSriastradh		kgem_submit(&sna->kgem);
250242542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
250342542f5fSchristos			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
250442542f5fSchristos			if (tmp.redirect.real_bo)
250542542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
250642542f5fSchristos
250742542f5fSchristos			goto fallback_blt;
250842542f5fSchristos		}
250903b705cfSriastradh	}
251003b705cfSriastradh
251103b705cfSriastradh	dst_dx += tmp.dst.x;
251203b705cfSriastradh	dst_dy += tmp.dst.y;
251303b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
251403b705cfSriastradh
251503b705cfSriastradh	src_dx += tmp.src.offset[0];
251603b705cfSriastradh	src_dy += tmp.src.offset[1];
251703b705cfSriastradh
251803b705cfSriastradh	gen4_align_vertex(sna, &tmp);
251942542f5fSchristos	gen4_copy_bind_surfaces(sna, &tmp);
252003b705cfSriastradh
252103b705cfSriastradh	do {
252203b705cfSriastradh		gen4_render_copy_one(sna, &tmp,
252303b705cfSriastradh				     box->x1 + src_dx, box->y1 + src_dy,
252403b705cfSriastradh				     box->x2 - box->x1, box->y2 - box->y1,
252503b705cfSriastradh				     box->x1 + dst_dx, box->y1 + dst_dy);
252603b705cfSriastradh		box++;
252703b705cfSriastradh	} while (--n);
252803b705cfSriastradh
252903b705cfSriastradh	gen4_vertex_flush(sna);
253003b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
253103b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
253203b705cfSriastradh	return true;
253303b705cfSriastradh
253403b705cfSriastradhfallback_tiled_dst:
253503b705cfSriastradh	if (tmp.redirect.real_bo)
253603b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
253703b705cfSriastradhfallback_tiled:
253842542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
253903b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
254003b705cfSriastradh			       src_bo, src_dx, src_dy,
254103b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
254242542f5fSchristos			       dst->bitsPerPixel,
254303b705cfSriastradh			       box, n))
254403b705cfSriastradh		return true;
254503b705cfSriastradh
254603b705cfSriastradh	return sna_tiling_copy_boxes(sna, alu,
254703b705cfSriastradh				     src, src_bo, src_dx, src_dy,
254803b705cfSriastradh				     dst, dst_bo, dst_dx, dst_dy,
254903b705cfSriastradh				     box, n);
255003b705cfSriastradh}
255103b705cfSriastradh
255203b705cfSriastradhstatic void
255303b705cfSriastradhgen4_render_copy_blt(struct sna *sna,
255403b705cfSriastradh		     const struct sna_copy_op *op,
255503b705cfSriastradh		     int16_t sx, int16_t sy,
255603b705cfSriastradh		     int16_t w,  int16_t h,
255703b705cfSriastradh		     int16_t dx, int16_t dy)
255803b705cfSriastradh{
255903b705cfSriastradh	gen4_render_copy_one(sna, &op->base, sx, sy, w, h, dx, dy);
256003b705cfSriastradh}
256103b705cfSriastradh
256203b705cfSriastradhstatic void
256303b705cfSriastradhgen4_render_copy_done(struct sna *sna, const struct sna_copy_op *op)
256403b705cfSriastradh{
256503b705cfSriastradh	if (sna->render.vertex_offset)
256603b705cfSriastradh		gen4_vertex_flush(sna);
256703b705cfSriastradh}
256803b705cfSriastradh
256903b705cfSriastradhstatic bool
257003b705cfSriastradhgen4_render_copy(struct sna *sna, uint8_t alu,
257103b705cfSriastradh		 PixmapPtr src, struct kgem_bo *src_bo,
257203b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
257303b705cfSriastradh		 struct sna_copy_op *op)
257403b705cfSriastradh{
257503b705cfSriastradh	DBG(("%s: src=%ld, dst=%ld, alu=%d\n",
257603b705cfSriastradh	     __FUNCTION__,
257703b705cfSriastradh	     src->drawable.serialNumber,
257803b705cfSriastradh	     dst->drawable.serialNumber,
257903b705cfSriastradh	     alu));
258003b705cfSriastradh
258103b705cfSriastradh	if (sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
258203b705cfSriastradh	    sna_blt_copy(sna, alu,
258303b705cfSriastradh			 src_bo, dst_bo,
258403b705cfSriastradh			 dst->drawable.bitsPerPixel,
258503b705cfSriastradh			 op))
258603b705cfSriastradh		return true;
258703b705cfSriastradh
258803b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo ||
258903b705cfSriastradh	    too_large(src->drawable.width, src->drawable.height) ||
259003b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height)) {
259103b705cfSriastradhfallback:
259203b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
259303b705cfSriastradh			return false;
259403b705cfSriastradh
259503b705cfSriastradh		return sna_blt_copy(sna, alu, src_bo, dst_bo,
259603b705cfSriastradh				    dst->drawable.bitsPerPixel,
259703b705cfSriastradh				    op);
259803b705cfSriastradh	}
259903b705cfSriastradh
260003b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
260103b705cfSriastradh		op->base.dst.format = sna_render_format_for_depth(dst->drawable.depth);
260203b705cfSriastradh		op->base.src.pict_format = op->base.dst.format;
260303b705cfSriastradh	} else {
260403b705cfSriastradh		op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
260503b705cfSriastradh		op->base.src.pict_format = sna_format_for_depth(src->drawable.depth);
260603b705cfSriastradh	}
260703b705cfSriastradh	if (!gen4_check_format(op->base.src.pict_format))
260803b705cfSriastradh		goto fallback;
260903b705cfSriastradh
261003b705cfSriastradh	op->base.op = alu == GXcopy ? PictOpSrc : PictOpClear;
261103b705cfSriastradh
261203b705cfSriastradh	op->base.dst.pixmap = dst;
261303b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
261403b705cfSriastradh	op->base.dst.height = dst->drawable.height;
261503b705cfSriastradh	op->base.dst.bo = dst_bo;
261603b705cfSriastradh
261703b705cfSriastradh	op->base.src.bo = src_bo;
261803b705cfSriastradh	op->base.src.card_format =
261903b705cfSriastradh		gen4_get_card_format(op->base.src.pict_format);
262003b705cfSriastradh	op->base.src.width  = src->drawable.width;
262103b705cfSriastradh	op->base.src.height = src->drawable.height;
262203b705cfSriastradh	op->base.src.scale[0] = 1.f/src->drawable.width;
262303b705cfSriastradh	op->base.src.scale[1] = 1.f/src->drawable.height;
262403b705cfSriastradh	op->base.src.filter = SAMPLER_FILTER_NEAREST;
262503b705cfSriastradh	op->base.src.repeat = SAMPLER_EXTEND_NONE;
262603b705cfSriastradh
262703b705cfSriastradh	op->base.is_affine = true;
262803b705cfSriastradh	op->base.floats_per_vertex = 3;
262903b705cfSriastradh	op->base.floats_per_rect = 9;
263003b705cfSriastradh	op->base.u.gen4.wm_kernel = WM_KERNEL;
263103b705cfSriastradh	op->base.u.gen4.ve_id = 2;
263203b705cfSriastradh
263303b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
263403b705cfSriastradh		kgem_submit(&sna->kgem);
263503b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL))
263603b705cfSriastradh			goto fallback;
263703b705cfSriastradh	}
263803b705cfSriastradh
263903b705cfSriastradh	if (kgem_bo_is_dirty(src_bo)) {
264003b705cfSriastradh		if (sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
264103b705cfSriastradh		    sna_blt_copy(sna, alu,
264203b705cfSriastradh				 src_bo, dst_bo,
264303b705cfSriastradh				 dst->drawable.bitsPerPixel,
264403b705cfSriastradh				 op))
264503b705cfSriastradh			return true;
264603b705cfSriastradh	}
264703b705cfSriastradh
264803b705cfSriastradh	gen4_align_vertex(sna, &op->base);
264942542f5fSchristos	gen4_copy_bind_surfaces(sna, &op->base);
265003b705cfSriastradh
265103b705cfSriastradh	op->blt  = gen4_render_copy_blt;
265203b705cfSriastradh	op->done = gen4_render_copy_done;
265303b705cfSriastradh	return true;
265403b705cfSriastradh}
265503b705cfSriastradh
265603b705cfSriastradhstatic void
265703b705cfSriastradhgen4_render_fill_rectangle(struct sna *sna,
265803b705cfSriastradh			   const struct sna_composite_op *op,
265903b705cfSriastradh			   int x, int y, int w, int h)
266003b705cfSriastradh{
266103b705cfSriastradh	gen4_get_rectangles(sna, op, 1, gen4_bind_surfaces);
266203b705cfSriastradh
266303b705cfSriastradh	OUT_VERTEX(x+w, y+h);
266403b705cfSriastradh	OUT_VERTEX_F(.5);
266503b705cfSriastradh
266603b705cfSriastradh	OUT_VERTEX(x, y+h);
266703b705cfSriastradh	OUT_VERTEX_F(.5);
266803b705cfSriastradh
266903b705cfSriastradh	OUT_VERTEX(x, y);
267003b705cfSriastradh	OUT_VERTEX_F(.5);
267103b705cfSriastradh}
267203b705cfSriastradh
267303b705cfSriastradhstatic bool
267403b705cfSriastradhgen4_render_fill_boxes(struct sna *sna,
267503b705cfSriastradh		       CARD8 op,
267603b705cfSriastradh		       PictFormat format,
267703b705cfSriastradh		       const xRenderColor *color,
267842542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo,
267903b705cfSriastradh		       const BoxRec *box, int n)
268003b705cfSriastradh{
268103b705cfSriastradh	struct sna_composite_op tmp;
268203b705cfSriastradh	uint32_t pixel;
268303b705cfSriastradh
268403b705cfSriastradh	if (op >= ARRAY_SIZE(gen4_blend_op)) {
268503b705cfSriastradh		DBG(("%s: fallback due to unhandled blend op: %d\n",
268603b705cfSriastradh		     __FUNCTION__, op));
268703b705cfSriastradh		return false;
268803b705cfSriastradh	}
268903b705cfSriastradh
269003b705cfSriastradh	if (op <= PictOpSrc) {
269103b705cfSriastradh		uint8_t alu = GXinvalid;
269203b705cfSriastradh
269303b705cfSriastradh		pixel = 0;
269403b705cfSriastradh		if (op == PictOpClear)
269503b705cfSriastradh			alu = GXclear;
269603b705cfSriastradh		else if (sna_get_pixel_from_rgba(&pixel,
269703b705cfSriastradh						 color->red,
269803b705cfSriastradh						 color->green,
269903b705cfSriastradh						 color->blue,
270003b705cfSriastradh						 color->alpha,
270103b705cfSriastradh						 format))
270203b705cfSriastradh			alu = GXcopy;
270303b705cfSriastradh
270403b705cfSriastradh		if (alu != GXinvalid &&
270503b705cfSriastradh		    sna_blt_fill_boxes(sna, alu,
270642542f5fSchristos				       dst_bo, dst->bitsPerPixel,
270703b705cfSriastradh				       pixel, box, n))
270803b705cfSriastradh			return true;
270903b705cfSriastradh
271003b705cfSriastradh		if (!gen4_check_dst_format(format))
271103b705cfSriastradh			return false;
271203b705cfSriastradh
271342542f5fSchristos		if (too_large(dst->width, dst->height))
271403b705cfSriastradh			return sna_tiling_fill_boxes(sna, op, format, color,
271503b705cfSriastradh						     dst, dst_bo, box, n);
271603b705cfSriastradh	}
271703b705cfSriastradh
271803b705cfSriastradh	if (op == PictOpClear) {
271903b705cfSriastradh		pixel = 0;
272003b705cfSriastradh		op = PictOpSrc;
272103b705cfSriastradh	} else if (!sna_get_pixel_from_rgba(&pixel,
272203b705cfSriastradh					    color->red,
272303b705cfSriastradh					    color->green,
272403b705cfSriastradh					    color->blue,
272503b705cfSriastradh					    color->alpha,
272603b705cfSriastradh					    PICT_a8r8g8b8))
272703b705cfSriastradh		return false;
272803b705cfSriastradh
272903b705cfSriastradh	DBG(("%s(%08x x %d)\n", __FUNCTION__, pixel, n));
273003b705cfSriastradh
273103b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
273203b705cfSriastradh
273303b705cfSriastradh	tmp.op = op;
273403b705cfSriastradh
273542542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
273642542f5fSchristos	tmp.dst.width  = dst->width;
273742542f5fSchristos	tmp.dst.height = dst->height;
273803b705cfSriastradh	tmp.dst.format = format;
273903b705cfSriastradh	tmp.dst.bo = dst_bo;
274003b705cfSriastradh
274103b705cfSriastradh	gen4_channel_init_solid(sna, &tmp.src, pixel);
274203b705cfSriastradh
274303b705cfSriastradh	tmp.is_affine = true;
274403b705cfSriastradh	tmp.floats_per_vertex = 2;
274503b705cfSriastradh	tmp.floats_per_rect = 6;
274603b705cfSriastradh	tmp.u.gen4.wm_kernel = WM_KERNEL;
274703b705cfSriastradh	tmp.u.gen4.ve_id = 1;
274803b705cfSriastradh
274903b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
275003b705cfSriastradh		kgem_submit(&sna->kgem);
275142542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, NULL))
275242542f5fSchristos			return false;
275303b705cfSriastradh	}
275403b705cfSriastradh
275503b705cfSriastradh	gen4_align_vertex(sna, &tmp);
275642542f5fSchristos	gen4_bind_surfaces(sna, &tmp);
275703b705cfSriastradh
275803b705cfSriastradh	do {
275903b705cfSriastradh		gen4_render_fill_rectangle(sna, &tmp,
276003b705cfSriastradh					   box->x1, box->y1,
276103b705cfSriastradh					   box->x2 - box->x1,
276203b705cfSriastradh					   box->y2 - box->y1);
276303b705cfSriastradh		box++;
276403b705cfSriastradh	} while (--n);
276503b705cfSriastradh
276603b705cfSriastradh	gen4_vertex_flush(sna);
276703b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
276803b705cfSriastradh	return true;
276903b705cfSriastradh}
277003b705cfSriastradh
277103b705cfSriastradhstatic void
277203b705cfSriastradhgen4_render_fill_op_blt(struct sna *sna, const struct sna_fill_op *op,
277303b705cfSriastradh			int16_t x, int16_t y, int16_t w, int16_t h)
277403b705cfSriastradh{
277503b705cfSriastradh	gen4_render_fill_rectangle(sna, &op->base, x, y, w, h);
277603b705cfSriastradh}
277703b705cfSriastradh
277803b705cfSriastradhfastcall static void
277903b705cfSriastradhgen4_render_fill_op_box(struct sna *sna,
278003b705cfSriastradh			const struct sna_fill_op *op,
278103b705cfSriastradh			const BoxRec *box)
278203b705cfSriastradh{
278303b705cfSriastradh	gen4_render_fill_rectangle(sna, &op->base,
278403b705cfSriastradh				   box->x1, box->y1,
278503b705cfSriastradh				   box->x2-box->x1, box->y2-box->y1);
278603b705cfSriastradh}
278703b705cfSriastradh
278803b705cfSriastradhfastcall static void
278903b705cfSriastradhgen4_render_fill_op_boxes(struct sna *sna,
279003b705cfSriastradh			  const struct sna_fill_op *op,
279103b705cfSriastradh			  const BoxRec *box,
279203b705cfSriastradh			  int nbox)
279303b705cfSriastradh{
279403b705cfSriastradh	do {
279503b705cfSriastradh		gen4_render_fill_rectangle(sna, &op->base,
279603b705cfSriastradh					   box->x1, box->y1,
279703b705cfSriastradh					   box->x2-box->x1, box->y2-box->y1);
279803b705cfSriastradh		box++;
279903b705cfSriastradh	} while (--nbox);
280003b705cfSriastradh}
280103b705cfSriastradh
280203b705cfSriastradhstatic void
280303b705cfSriastradhgen4_render_fill_op_done(struct sna *sna, const struct sna_fill_op *op)
280403b705cfSriastradh{
280503b705cfSriastradh	if (sna->render.vertex_offset)
280603b705cfSriastradh		gen4_vertex_flush(sna);
280703b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
280803b705cfSriastradh}
280903b705cfSriastradh
281003b705cfSriastradhstatic bool
281103b705cfSriastradhgen4_render_fill(struct sna *sna, uint8_t alu,
281203b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
281342542f5fSchristos		 uint32_t color, unsigned flags,
281403b705cfSriastradh		 struct sna_fill_op *op)
281503b705cfSriastradh{
281603b705cfSriastradh	if (sna_blt_fill(sna, alu,
281703b705cfSriastradh			 dst_bo, dst->drawable.bitsPerPixel,
281803b705cfSriastradh			 color,
281903b705cfSriastradh			 op))
282003b705cfSriastradh		return true;
282103b705cfSriastradh
282203b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
282303b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
282403b705cfSriastradh		return sna_blt_fill(sna, alu,
282503b705cfSriastradh				    dst_bo, dst->drawable.bitsPerPixel,
282603b705cfSriastradh				    color,
282703b705cfSriastradh				    op);
282803b705cfSriastradh
282903b705cfSriastradh	if (alu == GXclear)
283003b705cfSriastradh		color = 0;
283103b705cfSriastradh
283203b705cfSriastradh	op->base.op = color == 0 ? PictOpClear : PictOpSrc;
283303b705cfSriastradh
283403b705cfSriastradh	op->base.dst.pixmap = dst;
283503b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
283603b705cfSriastradh	op->base.dst.height = dst->drawable.height;
283703b705cfSriastradh	op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
283803b705cfSriastradh	op->base.dst.bo = dst_bo;
283903b705cfSriastradh	op->base.dst.x = op->base.dst.y = 0;
284003b705cfSriastradh
284103b705cfSriastradh	op->base.need_magic_ca_pass = 0;
284203b705cfSriastradh	op->base.has_component_alpha = 0;
284303b705cfSriastradh
284403b705cfSriastradh	gen4_channel_init_solid(sna, &op->base.src,
284503b705cfSriastradh				sna_rgba_for_color(color,
284603b705cfSriastradh						   dst->drawable.depth));
284703b705cfSriastradh	op->base.mask.bo = NULL;
284803b705cfSriastradh
284903b705cfSriastradh	op->base.is_affine = true;
285003b705cfSriastradh	op->base.floats_per_vertex = 2;
285103b705cfSriastradh	op->base.floats_per_rect = 6;
285203b705cfSriastradh	op->base.u.gen4.wm_kernel = WM_KERNEL;
285303b705cfSriastradh	op->base.u.gen4.ve_id = 1;
285403b705cfSriastradh
285503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
285603b705cfSriastradh		kgem_submit(&sna->kgem);
285742542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
285842542f5fSchristos			kgem_bo_destroy(&sna->kgem, op->base.src.bo);
285942542f5fSchristos			return false;
286042542f5fSchristos		}
286103b705cfSriastradh	}
286203b705cfSriastradh
286303b705cfSriastradh	gen4_align_vertex(sna, &op->base);
286442542f5fSchristos	gen4_bind_surfaces(sna, &op->base);
286503b705cfSriastradh
286603b705cfSriastradh	op->blt   = gen4_render_fill_op_blt;
286703b705cfSriastradh	op->box   = gen4_render_fill_op_box;
286803b705cfSriastradh	op->boxes = gen4_render_fill_op_boxes;
286942542f5fSchristos	op->points = NULL;
287003b705cfSriastradh	op->done  = gen4_render_fill_op_done;
287103b705cfSriastradh	return true;
287203b705cfSriastradh}
287303b705cfSriastradh
287403b705cfSriastradhstatic bool
287503b705cfSriastradhgen4_render_fill_one_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
287603b705cfSriastradh			     uint32_t color,
287703b705cfSriastradh			     int16_t x1, int16_t y1, int16_t x2, int16_t y2,
287803b705cfSriastradh			     uint8_t alu)
287903b705cfSriastradh{
288003b705cfSriastradh	BoxRec box;
288103b705cfSriastradh
288203b705cfSriastradh	box.x1 = x1;
288303b705cfSriastradh	box.y1 = y1;
288403b705cfSriastradh	box.x2 = x2;
288503b705cfSriastradh	box.y2 = y2;
288603b705cfSriastradh
288703b705cfSriastradh	return sna_blt_fill_boxes(sna, alu,
288803b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
288903b705cfSriastradh				  color, &box, 1);
289003b705cfSriastradh}
289103b705cfSriastradh
289203b705cfSriastradhstatic bool
289303b705cfSriastradhgen4_render_fill_one(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
289403b705cfSriastradh		     uint32_t color,
289503b705cfSriastradh		     int16_t x1, int16_t y1,
289603b705cfSriastradh		     int16_t x2, int16_t y2,
289703b705cfSriastradh		     uint8_t alu)
289803b705cfSriastradh{
289903b705cfSriastradh	struct sna_composite_op tmp;
290003b705cfSriastradh
290103b705cfSriastradh	DBG(("%s: color=%08x\n", __FUNCTION__, color));
290203b705cfSriastradh
290303b705cfSriastradh	if (gen4_render_fill_one_try_blt(sna, dst, bo, color,
290403b705cfSriastradh					 x1, y1, x2, y2, alu))
290503b705cfSriastradh		return true;
290603b705cfSriastradh
290703b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
290803b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
290903b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
291003b705cfSriastradh		return false;
291103b705cfSriastradh
291203b705cfSriastradh	if (alu == GXclear)
291303b705cfSriastradh		color = 0;
291403b705cfSriastradh
291503b705cfSriastradh	tmp.op = color == 0 ? PictOpClear : PictOpSrc;
291603b705cfSriastradh
291703b705cfSriastradh	tmp.dst.pixmap = dst;
291803b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
291903b705cfSriastradh	tmp.dst.height = dst->drawable.height;
292003b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
292103b705cfSriastradh	tmp.dst.bo = bo;
292203b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
292303b705cfSriastradh
292403b705cfSriastradh	gen4_channel_init_solid(sna, &tmp.src,
292503b705cfSriastradh				sna_rgba_for_color(color,
292603b705cfSriastradh						   dst->drawable.depth));
292703b705cfSriastradh	tmp.mask.bo = NULL;
292803b705cfSriastradh	tmp.mask.filter = SAMPLER_FILTER_NEAREST;
292903b705cfSriastradh	tmp.mask.repeat = SAMPLER_EXTEND_NONE;
293003b705cfSriastradh
293103b705cfSriastradh	tmp.is_affine = true;
293203b705cfSriastradh	tmp.floats_per_vertex = 2;
293303b705cfSriastradh	tmp.floats_per_rect = 6;
293403b705cfSriastradh	tmp.has_component_alpha = false;
293503b705cfSriastradh	tmp.need_magic_ca_pass = false;
293603b705cfSriastradh
293703b705cfSriastradh	tmp.u.gen4.wm_kernel = WM_KERNEL;
293803b705cfSriastradh	tmp.u.gen4.ve_id = 1;
293903b705cfSriastradh
294003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
294103b705cfSriastradh		kgem_submit(&sna->kgem);
294203b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
294303b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
294403b705cfSriastradh			return false;
294503b705cfSriastradh		}
294603b705cfSriastradh	}
294703b705cfSriastradh
294803b705cfSriastradh	gen4_align_vertex(sna, &tmp);
294942542f5fSchristos	gen4_bind_surfaces(sna, &tmp);
295003b705cfSriastradh
295103b705cfSriastradh	gen4_render_fill_rectangle(sna, &tmp, x1, y1, x2 - x1, y2 - y1);
295203b705cfSriastradh
295303b705cfSriastradh	gen4_vertex_flush(sna);
295403b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
295503b705cfSriastradh
295603b705cfSriastradh	return true;
295703b705cfSriastradh}
295803b705cfSriastradh
295903b705cfSriastradhstatic void gen4_render_reset(struct sna *sna)
296003b705cfSriastradh{
296103b705cfSriastradh	sna->render_state.gen4.needs_invariant = true;
296203b705cfSriastradh	sna->render_state.gen4.needs_urb = true;
296303b705cfSriastradh	sna->render_state.gen4.ve_id = -1;
296403b705cfSriastradh	sna->render_state.gen4.last_primitive = -1;
296503b705cfSriastradh	sna->render_state.gen4.last_pipelined_pointers = -1;
296603b705cfSriastradh
296703b705cfSriastradh	sna->render_state.gen4.drawrect_offset = -1;
296803b705cfSriastradh	sna->render_state.gen4.drawrect_limit = -1;
296942542f5fSchristos	sna->render_state.gen4.surface_table = 0;
297003b705cfSriastradh
297142542f5fSchristos	if (sna->render.vbo && !kgem_bo_can_map(&sna->kgem, sna->render.vbo)) {
297203b705cfSriastradh		DBG(("%s: discarding unmappable vbo\n", __FUNCTION__));
297303b705cfSriastradh		discard_vbo(sna);
297403b705cfSriastradh	}
297503b705cfSriastradh
297603b705cfSriastradh	sna->render.vertex_offset = 0;
297703b705cfSriastradh	sna->render.nvertex_reloc = 0;
297803b705cfSriastradh	sna->render.vb_id = 0;
297903b705cfSriastradh}
298003b705cfSriastradh
298103b705cfSriastradhstatic void gen4_render_fini(struct sna *sna)
298203b705cfSriastradh{
298303b705cfSriastradh	kgem_bo_destroy(&sna->kgem, sna->render_state.gen4.general_bo);
298403b705cfSriastradh}
298503b705cfSriastradh
298603b705cfSriastradhstatic uint32_t gen4_create_vs_unit_state(struct sna_static_stream *stream)
298703b705cfSriastradh{
298803b705cfSriastradh	struct gen4_vs_unit_state *vs = sna_static_stream_map(stream, sizeof(*vs), 32);
298903b705cfSriastradh
299003b705cfSriastradh	/* Set up the vertex shader to be disabled (passthrough) */
299103b705cfSriastradh	vs->thread4.nr_urb_entries = URB_VS_ENTRIES;
299203b705cfSriastradh	vs->thread4.urb_entry_allocation_size = URB_VS_ENTRY_SIZE - 1;
299303b705cfSriastradh	vs->vs6.vs_enable = 0;
299403b705cfSriastradh	vs->vs6.vert_cache_disable = 1;
299503b705cfSriastradh
299603b705cfSriastradh	return sna_static_stream_offsetof(stream, vs);
299703b705cfSriastradh}
299803b705cfSriastradh
299903b705cfSriastradhstatic uint32_t gen4_create_sf_state(struct sna_static_stream *stream,
300003b705cfSriastradh				     uint32_t kernel)
300103b705cfSriastradh{
300203b705cfSriastradh	struct gen4_sf_unit_state *sf;
300303b705cfSriastradh
300403b705cfSriastradh	sf = sna_static_stream_map(stream, sizeof(*sf), 32);
300503b705cfSriastradh
300603b705cfSriastradh	sf->thread0.grf_reg_count = GEN4_GRF_BLOCKS(SF_KERNEL_NUM_GRF);
300703b705cfSriastradh	sf->thread0.kernel_start_pointer = kernel >> 6;
300803b705cfSriastradh	sf->thread3.const_urb_entry_read_length = 0;	/* no const URBs */
300903b705cfSriastradh	sf->thread3.const_urb_entry_read_offset = 0;	/* no const URBs */
301003b705cfSriastradh	sf->thread3.urb_entry_read_length = 1;	/* 1 URB per vertex */
301103b705cfSriastradh	/* don't smash vertex header, read start from dw8 */
301203b705cfSriastradh	sf->thread3.urb_entry_read_offset = 1;
301303b705cfSriastradh	sf->thread3.dispatch_grf_start_reg = 3;
301403b705cfSriastradh	sf->thread4.max_threads = GEN4_MAX_SF_THREADS - 1;
301503b705cfSriastradh	sf->thread4.urb_entry_allocation_size = URB_SF_ENTRY_SIZE - 1;
301603b705cfSriastradh	sf->thread4.nr_urb_entries = URB_SF_ENTRIES;
301703b705cfSriastradh	sf->sf5.viewport_transform = false;	/* skip viewport */
301803b705cfSriastradh	sf->sf6.cull_mode = GEN4_CULLMODE_NONE;
301903b705cfSriastradh	sf->sf6.scissor = 0;
302003b705cfSriastradh	sf->sf7.trifan_pv = 2;
302103b705cfSriastradh	sf->sf6.dest_org_vbias = 0x8;
302203b705cfSriastradh	sf->sf6.dest_org_hbias = 0x8;
302303b705cfSriastradh
302403b705cfSriastradh	return sna_static_stream_offsetof(stream, sf);
302503b705cfSriastradh}
302603b705cfSriastradh
302703b705cfSriastradhstatic uint32_t gen4_create_sampler_state(struct sna_static_stream *stream,
302803b705cfSriastradh					  sampler_filter_t src_filter,
302903b705cfSriastradh					  sampler_extend_t src_extend,
303003b705cfSriastradh					  sampler_filter_t mask_filter,
303103b705cfSriastradh					  sampler_extend_t mask_extend)
303203b705cfSriastradh{
303303b705cfSriastradh	struct gen4_sampler_state *sampler_state;
303403b705cfSriastradh
303503b705cfSriastradh	sampler_state = sna_static_stream_map(stream,
303603b705cfSriastradh					      sizeof(struct gen4_sampler_state) * 2,
303703b705cfSriastradh					      32);
303803b705cfSriastradh	sampler_state_init(&sampler_state[0], src_filter, src_extend);
303903b705cfSriastradh	sampler_state_init(&sampler_state[1], mask_filter, mask_extend);
304003b705cfSriastradh
304103b705cfSriastradh	return sna_static_stream_offsetof(stream, sampler_state);
304203b705cfSriastradh}
304303b705cfSriastradh
304403b705cfSriastradhstatic void gen4_init_wm_state(struct gen4_wm_unit_state *wm,
304503b705cfSriastradh			       int gen,
304603b705cfSriastradh			       bool has_mask,
304703b705cfSriastradh			       uint32_t kernel,
304803b705cfSriastradh			       uint32_t sampler)
304903b705cfSriastradh{
305003b705cfSriastradh	assert((kernel & 63) == 0);
305103b705cfSriastradh	wm->thread0.kernel_start_pointer = kernel >> 6;
305203b705cfSriastradh	wm->thread0.grf_reg_count = GEN4_GRF_BLOCKS(PS_KERNEL_NUM_GRF);
305303b705cfSriastradh
305403b705cfSriastradh	wm->thread1.single_program_flow = 0;
305503b705cfSriastradh
305603b705cfSriastradh	wm->thread3.const_urb_entry_read_length = 0;
305703b705cfSriastradh	wm->thread3.const_urb_entry_read_offset = 0;
305803b705cfSriastradh
305903b705cfSriastradh	wm->thread3.urb_entry_read_offset = 0;
306003b705cfSriastradh	wm->thread3.dispatch_grf_start_reg = 3;
306103b705cfSriastradh
306203b705cfSriastradh	assert((sampler & 31) == 0);
306303b705cfSriastradh	wm->wm4.sampler_state_pointer = sampler >> 5;
306403b705cfSriastradh	wm->wm4.sampler_count = 1;
306503b705cfSriastradh
306603b705cfSriastradh	wm->wm5.max_threads = gen >= 045 ? G4X_MAX_WM_THREADS - 1 : GEN4_MAX_WM_THREADS - 1;
306703b705cfSriastradh	wm->wm5.transposed_urb_read = 0;
306803b705cfSriastradh	wm->wm5.thread_dispatch_enable = 1;
306903b705cfSriastradh	/* just use 16-pixel dispatch (4 subspans), don't need to change kernel
307003b705cfSriastradh	 * start point
307103b705cfSriastradh	 */
307203b705cfSriastradh	wm->wm5.enable_16_pix = 1;
307303b705cfSriastradh	wm->wm5.enable_8_pix = 0;
307403b705cfSriastradh	wm->wm5.early_depth_test = 1;
307503b705cfSriastradh
307603b705cfSriastradh	/* Each pair of attributes (src/mask coords) is two URB entries */
307703b705cfSriastradh	if (has_mask) {
307803b705cfSriastradh		wm->thread1.binding_table_entry_count = 3;
307903b705cfSriastradh		wm->thread3.urb_entry_read_length = 4;
308003b705cfSriastradh	} else {
308103b705cfSriastradh		wm->thread1.binding_table_entry_count = 2;
308203b705cfSriastradh		wm->thread3.urb_entry_read_length = 2;
308303b705cfSriastradh	}
308403b705cfSriastradh}
308503b705cfSriastradh
308603b705cfSriastradhstatic uint32_t gen4_create_cc_unit_state(struct sna_static_stream *stream)
308703b705cfSriastradh{
308803b705cfSriastradh	uint8_t *ptr, *base;
308903b705cfSriastradh	int i, j;
309003b705cfSriastradh
309103b705cfSriastradh	base = ptr =
309203b705cfSriastradh		sna_static_stream_map(stream,
309303b705cfSriastradh				      GEN4_BLENDFACTOR_COUNT*GEN4_BLENDFACTOR_COUNT*64,
309403b705cfSriastradh				      64);
309503b705cfSriastradh
309603b705cfSriastradh	for (i = 0; i < GEN4_BLENDFACTOR_COUNT; i++) {
309703b705cfSriastradh		for (j = 0; j < GEN4_BLENDFACTOR_COUNT; j++) {
309803b705cfSriastradh			struct gen4_cc_unit_state *state =
309903b705cfSriastradh				(struct gen4_cc_unit_state *)ptr;
310003b705cfSriastradh
310103b705cfSriastradh			state->cc3.blend_enable =
310203b705cfSriastradh				!(j == GEN4_BLENDFACTOR_ZERO && i == GEN4_BLENDFACTOR_ONE);
310303b705cfSriastradh
310403b705cfSriastradh			state->cc5.logicop_func = 0xc;	/* COPY */
310503b705cfSriastradh			state->cc5.ia_blend_function = GEN4_BLENDFUNCTION_ADD;
310603b705cfSriastradh
310703b705cfSriastradh			/* Fill in alpha blend factors same as color, for the future. */
310803b705cfSriastradh			state->cc5.ia_src_blend_factor = i;
310903b705cfSriastradh			state->cc5.ia_dest_blend_factor = j;
311003b705cfSriastradh
311103b705cfSriastradh			state->cc6.blend_function = GEN4_BLENDFUNCTION_ADD;
311203b705cfSriastradh			state->cc6.clamp_post_alpha_blend = 1;
311303b705cfSriastradh			state->cc6.clamp_pre_alpha_blend = 1;
311403b705cfSriastradh			state->cc6.src_blend_factor = i;
311503b705cfSriastradh			state->cc6.dest_blend_factor = j;
311603b705cfSriastradh
311703b705cfSriastradh			ptr += 64;
311803b705cfSriastradh		}
311903b705cfSriastradh	}
312003b705cfSriastradh
312103b705cfSriastradh	return sna_static_stream_offsetof(stream, base);
312203b705cfSriastradh}
312303b705cfSriastradh
312403b705cfSriastradhstatic bool gen4_render_setup(struct sna *sna)
312503b705cfSriastradh{
312603b705cfSriastradh	struct gen4_render_state *state = &sna->render_state.gen4;
312703b705cfSriastradh	struct sna_static_stream general;
312803b705cfSriastradh	struct gen4_wm_unit_state_padded *wm_state;
312903b705cfSriastradh	uint32_t sf, wm[KERNEL_COUNT];
313003b705cfSriastradh	int i, j, k, l, m;
313103b705cfSriastradh
313203b705cfSriastradh	sna_static_stream_init(&general);
313303b705cfSriastradh
313403b705cfSriastradh	/* Zero pad the start. If you see an offset of 0x0 in the batchbuffer
313503b705cfSriastradh	 * dumps, you know it points to zero.
313603b705cfSriastradh	 */
313703b705cfSriastradh	null_create(&general);
313803b705cfSriastradh
313903b705cfSriastradh	sf = sna_static_stream_compile_sf(sna, &general, brw_sf_kernel__mask);
314003b705cfSriastradh	for (m = 0; m < KERNEL_COUNT; m++) {
314103b705cfSriastradh		if (wm_kernels[m].size) {
314203b705cfSriastradh			wm[m] = sna_static_stream_add(&general,
314303b705cfSriastradh						      wm_kernels[m].data,
314403b705cfSriastradh						      wm_kernels[m].size,
314503b705cfSriastradh						      64);
314603b705cfSriastradh		} else {
314703b705cfSriastradh			wm[m] = sna_static_stream_compile_wm(sna, &general,
314803b705cfSriastradh							     wm_kernels[m].data,
314903b705cfSriastradh							     16);
315003b705cfSriastradh		}
315103b705cfSriastradh	}
315203b705cfSriastradh
315303b705cfSriastradh	state->vs = gen4_create_vs_unit_state(&general);
315403b705cfSriastradh	state->sf = gen4_create_sf_state(&general, sf);
315503b705cfSriastradh
315603b705cfSriastradh	wm_state = sna_static_stream_map(&general,
315703b705cfSriastradh					  sizeof(*wm_state) * KERNEL_COUNT *
315803b705cfSriastradh					  FILTER_COUNT * EXTEND_COUNT *
315903b705cfSriastradh					  FILTER_COUNT * EXTEND_COUNT,
316003b705cfSriastradh					  64);
316103b705cfSriastradh	state->wm = sna_static_stream_offsetof(&general, wm_state);
316203b705cfSriastradh	for (i = 0; i < FILTER_COUNT; i++) {
316303b705cfSriastradh		for (j = 0; j < EXTEND_COUNT; j++) {
316403b705cfSriastradh			for (k = 0; k < FILTER_COUNT; k++) {
316503b705cfSriastradh				for (l = 0; l < EXTEND_COUNT; l++) {
316603b705cfSriastradh					uint32_t sampler_state;
316703b705cfSriastradh
316803b705cfSriastradh					sampler_state =
316903b705cfSriastradh						gen4_create_sampler_state(&general,
317003b705cfSriastradh									  i, j,
317103b705cfSriastradh									  k, l);
317203b705cfSriastradh
317303b705cfSriastradh					for (m = 0; m < KERNEL_COUNT; m++) {
317403b705cfSriastradh						gen4_init_wm_state(&wm_state->state,
317503b705cfSriastradh								   sna->kgem.gen,
317603b705cfSriastradh								   wm_kernels[m].has_mask,
317703b705cfSriastradh								   wm[m], sampler_state);
317803b705cfSriastradh						wm_state++;
317903b705cfSriastradh					}
318003b705cfSriastradh				}
318103b705cfSriastradh			}
318203b705cfSriastradh		}
318303b705cfSriastradh	}
318403b705cfSriastradh
318503b705cfSriastradh	state->cc = gen4_create_cc_unit_state(&general);
318603b705cfSriastradh
318703b705cfSriastradh	state->general_bo = sna_static_stream_fini(sna, &general);
318803b705cfSriastradh	return state->general_bo != NULL;
318903b705cfSriastradh}
319003b705cfSriastradh
319103b705cfSriastradhconst char *gen4_render_init(struct sna *sna, const char *backend)
319203b705cfSriastradh{
319303b705cfSriastradh	if (!gen4_render_setup(sna))
319403b705cfSriastradh		return backend;
319503b705cfSriastradh
319603b705cfSriastradh	sna->kgem.retire = gen4_render_retire;
319703b705cfSriastradh	sna->kgem.expire = gen4_render_expire;
319803b705cfSriastradh
319903b705cfSriastradh#if !NO_COMPOSITE
320003b705cfSriastradh	sna->render.composite = gen4_render_composite;
320103b705cfSriastradh	sna->render.prefer_gpu |= PREFER_GPU_RENDER;
320203b705cfSriastradh#endif
320303b705cfSriastradh#if !NO_COMPOSITE_SPANS
320403b705cfSriastradh	sna->render.check_composite_spans = gen4_check_composite_spans;
320503b705cfSriastradh	sna->render.composite_spans = gen4_render_composite_spans;
320603b705cfSriastradh	if (0)
320703b705cfSriastradh		sna->render.prefer_gpu |= PREFER_GPU_SPANS;
320803b705cfSriastradh#endif
320903b705cfSriastradh
321003b705cfSriastradh#if !NO_VIDEO
321103b705cfSriastradh	sna->render.video = gen4_render_video;
321203b705cfSriastradh#endif
321303b705cfSriastradh
321403b705cfSriastradh#if !NO_COPY_BOXES
321503b705cfSriastradh	sna->render.copy_boxes = gen4_render_copy_boxes;
321603b705cfSriastradh#endif
321703b705cfSriastradh#if !NO_COPY
321803b705cfSriastradh	sna->render.copy = gen4_render_copy;
321903b705cfSriastradh#endif
322003b705cfSriastradh
322103b705cfSriastradh#if !NO_FILL_BOXES
322203b705cfSriastradh	sna->render.fill_boxes = gen4_render_fill_boxes;
322303b705cfSriastradh#endif
322403b705cfSriastradh#if !NO_FILL
322503b705cfSriastradh	sna->render.fill = gen4_render_fill;
322603b705cfSriastradh#endif
322703b705cfSriastradh#if !NO_FILL_ONE
322803b705cfSriastradh	sna->render.fill_one = gen4_render_fill_one;
322903b705cfSriastradh#endif
323003b705cfSriastradh
323103b705cfSriastradh	sna->render.flush = gen4_render_flush;
323203b705cfSriastradh	sna->render.reset = gen4_render_reset;
323303b705cfSriastradh	sna->render.fini = gen4_render_fini;
323403b705cfSriastradh
323503b705cfSriastradh	sna->render.max_3d_size = GEN4_MAX_3D_SIZE;
323603b705cfSriastradh	sna->render.max_3d_pitch = 1 << 18;
323703b705cfSriastradh	return sna->kgem.gen >= 045 ? "Eaglelake (gen4.5)" : "Broadwater (gen4)";
323803b705cfSriastradh}
3239