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