gen6_render.c revision 13496ba1
103b705cfSriastradh/*
203b705cfSriastradh * Copyright © 2006,2008,2011 Intel Corporation
303b705cfSriastradh * Copyright © 2007 Red Hat, Inc.
403b705cfSriastradh *
503b705cfSriastradh * Permission is hereby granted, free of charge, to any person obtaining a
603b705cfSriastradh * copy of this software and associated documentation files (the "Software"),
703b705cfSriastradh * to deal in the Software without restriction, including without limitation
803b705cfSriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
903b705cfSriastradh * and/or sell copies of the Software, and to permit persons to whom the
1003b705cfSriastradh * Software is furnished to do so, subject to the following conditions:
1103b705cfSriastradh *
1203b705cfSriastradh * The above copyright notice and this permission notice (including the next
1303b705cfSriastradh * paragraph) shall be included in all copies or substantial portions of the
1403b705cfSriastradh * Software.
1503b705cfSriastradh *
1603b705cfSriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1703b705cfSriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1803b705cfSriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1903b705cfSriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2003b705cfSriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2103b705cfSriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2203b705cfSriastradh * SOFTWARE.
2303b705cfSriastradh *
2403b705cfSriastradh * Authors:
2503b705cfSriastradh *    Wang Zhenyu <zhenyu.z.wang@sna.com>
2603b705cfSriastradh *    Eric Anholt <eric@anholt.net>
2703b705cfSriastradh *    Carl Worth <cworth@redhat.com>
2803b705cfSriastradh *    Keith Packard <keithp@keithp.com>
2903b705cfSriastradh *    Chris Wilson <chris@chris-wilson.co.uk>
3003b705cfSriastradh *
3103b705cfSriastradh */
3203b705cfSriastradh
3303b705cfSriastradh#ifdef HAVE_CONFIG_H
3403b705cfSriastradh#include "config.h"
3503b705cfSriastradh#endif
3603b705cfSriastradh
3703b705cfSriastradh#include "sna.h"
3803b705cfSriastradh#include "sna_reg.h"
3903b705cfSriastradh#include "sna_render.h"
4003b705cfSriastradh#include "sna_render_inline.h"
4103b705cfSriastradh#include "sna_video.h"
4203b705cfSriastradh
4303b705cfSriastradh#include "brw/brw.h"
4403b705cfSriastradh#include "gen6_render.h"
4542542f5fSchristos#include "gen6_common.h"
4642542f5fSchristos#include "gen4_common.h"
4703b705cfSriastradh#include "gen4_source.h"
4803b705cfSriastradh#include "gen4_vertex.h"
4903b705cfSriastradh
5013496ba1Ssnj#define ALWAYS_INVALIDATE 0
5113496ba1Ssnj#define ALWAYS_FLUSH 0
5213496ba1Ssnj#define ALWAYS_STALL 0
5313496ba1Ssnj
5403b705cfSriastradh#define NO_COMPOSITE 0
5503b705cfSriastradh#define NO_COMPOSITE_SPANS 0
5603b705cfSriastradh#define NO_COPY 0
5703b705cfSriastradh#define NO_COPY_BOXES 0
5803b705cfSriastradh#define NO_FILL 0
5903b705cfSriastradh#define NO_FILL_BOXES 0
6003b705cfSriastradh#define NO_FILL_ONE 0
6103b705cfSriastradh#define NO_FILL_CLEAR 0
6203b705cfSriastradh
6303b705cfSriastradh#define USE_8_PIXEL_DISPATCH 1
6403b705cfSriastradh#define USE_16_PIXEL_DISPATCH 1
6503b705cfSriastradh#define USE_32_PIXEL_DISPATCH 0
6603b705cfSriastradh
6703b705cfSriastradh#if !USE_8_PIXEL_DISPATCH && !USE_16_PIXEL_DISPATCH && !USE_32_PIXEL_DISPATCH
6803b705cfSriastradh#error "Must select at least 8, 16 or 32 pixel dispatch"
6903b705cfSriastradh#endif
7003b705cfSriastradh
7103b705cfSriastradh#define GEN6_MAX_SIZE 8192
7203b705cfSriastradh
7303b705cfSriastradhstruct gt_info {
7403b705cfSriastradh	const char *name;
7503b705cfSriastradh	int max_vs_threads;
7603b705cfSriastradh	int max_gs_threads;
7703b705cfSriastradh	int max_wm_threads;
7803b705cfSriastradh	struct {
7903b705cfSriastradh		int size;
8003b705cfSriastradh		int max_vs_entries;
8103b705cfSriastradh		int max_gs_entries;
8203b705cfSriastradh	} urb;
8342542f5fSchristos	int gt;
8403b705cfSriastradh};
8503b705cfSriastradh
8603b705cfSriastradhstatic const struct gt_info gt1_info = {
8703b705cfSriastradh	.name = "Sandybridge (gen6, gt1)",
8803b705cfSriastradh	.max_vs_threads = 24,
8903b705cfSriastradh	.max_gs_threads = 21,
9003b705cfSriastradh	.max_wm_threads = 40,
9103b705cfSriastradh	.urb = { 32, 256, 256 },
9242542f5fSchristos	.gt = 1,
9303b705cfSriastradh};
9403b705cfSriastradh
9503b705cfSriastradhstatic const struct gt_info gt2_info = {
9603b705cfSriastradh	.name = "Sandybridge (gen6, gt2)",
9703b705cfSriastradh	.max_vs_threads = 60,
9803b705cfSriastradh	.max_gs_threads = 60,
9903b705cfSriastradh	.max_wm_threads = 80,
10003b705cfSriastradh	.urb = { 64, 256, 256 },
10142542f5fSchristos	.gt = 2,
10203b705cfSriastradh};
10303b705cfSriastradh
10403b705cfSriastradhstatic const uint32_t ps_kernel_packed[][4] = {
10503b705cfSriastradh#include "exa_wm_src_affine.g6b"
10603b705cfSriastradh#include "exa_wm_src_sample_argb.g6b"
10703b705cfSriastradh#include "exa_wm_yuv_rgb.g6b"
10803b705cfSriastradh#include "exa_wm_write.g6b"
10903b705cfSriastradh};
11003b705cfSriastradh
11103b705cfSriastradhstatic const uint32_t ps_kernel_planar[][4] = {
11203b705cfSriastradh#include "exa_wm_src_affine.g6b"
11303b705cfSriastradh#include "exa_wm_src_sample_planar.g6b"
11403b705cfSriastradh#include "exa_wm_yuv_rgb.g6b"
11503b705cfSriastradh#include "exa_wm_write.g6b"
11603b705cfSriastradh};
11703b705cfSriastradh
11803b705cfSriastradh#define NOKERNEL(kernel_enum, func, ns) \
11903b705cfSriastradh    [GEN6_WM_KERNEL_##kernel_enum] = {#kernel_enum, func, 0, ns}
12003b705cfSriastradh#define KERNEL(kernel_enum, kernel, ns) \
12103b705cfSriastradh    [GEN6_WM_KERNEL_##kernel_enum] = {#kernel_enum, kernel, sizeof(kernel), ns}
12203b705cfSriastradh
12303b705cfSriastradhstatic const struct wm_kernel_info {
12403b705cfSriastradh	const char *name;
12503b705cfSriastradh	const void *data;
12603b705cfSriastradh	unsigned int size;
12703b705cfSriastradh	unsigned int num_surfaces;
12803b705cfSriastradh} wm_kernels[] = {
12903b705cfSriastradh	NOKERNEL(NOMASK, brw_wm_kernel__affine, 2),
13003b705cfSriastradh	NOKERNEL(NOMASK_P, brw_wm_kernel__projective, 2),
13103b705cfSriastradh
13203b705cfSriastradh	NOKERNEL(MASK, brw_wm_kernel__affine_mask, 3),
13303b705cfSriastradh	NOKERNEL(MASK_P, brw_wm_kernel__projective_mask, 3),
13403b705cfSriastradh
13503b705cfSriastradh	NOKERNEL(MASKCA, brw_wm_kernel__affine_mask_ca, 3),
13603b705cfSriastradh	NOKERNEL(MASKCA_P, brw_wm_kernel__projective_mask_ca, 3),
13703b705cfSriastradh
13803b705cfSriastradh	NOKERNEL(MASKSA, brw_wm_kernel__affine_mask_sa, 3),
13903b705cfSriastradh	NOKERNEL(MASKSA_P, brw_wm_kernel__projective_mask_sa, 3),
14003b705cfSriastradh
14103b705cfSriastradh	NOKERNEL(OPACITY, brw_wm_kernel__affine_opacity, 2),
14203b705cfSriastradh	NOKERNEL(OPACITY_P, brw_wm_kernel__projective_opacity, 2),
14303b705cfSriastradh
14403b705cfSriastradh	KERNEL(VIDEO_PLANAR, ps_kernel_planar, 7),
14503b705cfSriastradh	KERNEL(VIDEO_PACKED, ps_kernel_packed, 2),
14603b705cfSriastradh};
14703b705cfSriastradh#undef KERNEL
14803b705cfSriastradh
14903b705cfSriastradhstatic const struct blendinfo {
15003b705cfSriastradh	bool src_alpha;
15103b705cfSriastradh	uint32_t src_blend;
15203b705cfSriastradh	uint32_t dst_blend;
15303b705cfSriastradh} gen6_blend_op[] = {
15403b705cfSriastradh	/* Clear */	{0, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ZERO},
15503b705cfSriastradh	/* Src */	{0, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ZERO},
15603b705cfSriastradh	/* Dst */	{0, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ONE},
15703b705cfSriastradh	/* Over */	{1, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
15803b705cfSriastradh	/* OverReverse */ {0, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_ONE},
15903b705cfSriastradh	/* In */	{0, GEN6_BLENDFACTOR_DST_ALPHA, GEN6_BLENDFACTOR_ZERO},
16003b705cfSriastradh	/* InReverse */	{1, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_SRC_ALPHA},
16103b705cfSriastradh	/* Out */	{0, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_ZERO},
16203b705cfSriastradh	/* OutReverse */ {1, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
16303b705cfSriastradh	/* Atop */	{1, GEN6_BLENDFACTOR_DST_ALPHA, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
16403b705cfSriastradh	/* AtopReverse */ {1, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_SRC_ALPHA},
16503b705cfSriastradh	/* Xor */	{1, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
16603b705cfSriastradh	/* Add */	{0, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ONE},
16703b705cfSriastradh};
16803b705cfSriastradh
16903b705cfSriastradh/**
17003b705cfSriastradh * Highest-valued BLENDFACTOR used in gen6_blend_op.
17103b705cfSriastradh *
17203b705cfSriastradh * This leaves out GEN6_BLENDFACTOR_INV_DST_COLOR,
17303b705cfSriastradh * GEN6_BLENDFACTOR_INV_CONST_{COLOR,ALPHA},
17403b705cfSriastradh * GEN6_BLENDFACTOR_INV_SRC1_{COLOR,ALPHA}
17503b705cfSriastradh */
17603b705cfSriastradh#define GEN6_BLENDFACTOR_COUNT (GEN6_BLENDFACTOR_INV_DST_ALPHA + 1)
17703b705cfSriastradh
17803b705cfSriastradh#define GEN6_BLEND_STATE_PADDED_SIZE	ALIGN(sizeof(struct gen6_blend_state), 64)
17903b705cfSriastradh
18003b705cfSriastradh#define BLEND_OFFSET(s, d) \
18103b705cfSriastradh	(((s) * GEN6_BLENDFACTOR_COUNT + (d)) * GEN6_BLEND_STATE_PADDED_SIZE)
18203b705cfSriastradh
18303b705cfSriastradh#define NO_BLEND BLEND_OFFSET(GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ZERO)
18403b705cfSriastradh#define CLEAR BLEND_OFFSET(GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ZERO)
18503b705cfSriastradh
18603b705cfSriastradh#define SAMPLER_OFFSET(sf, se, mf, me) \
18703b705cfSriastradh	(((((sf) * EXTEND_COUNT + (se)) * FILTER_COUNT + (mf)) * EXTEND_COUNT + (me) + 2) * 2 * sizeof(struct gen6_sampler_state))
18803b705cfSriastradh
18903b705cfSriastradh#define VERTEX_2s2s 0
19003b705cfSriastradh
19103b705cfSriastradh#define COPY_SAMPLER 0
19203b705cfSriastradh#define COPY_VERTEX VERTEX_2s2s
19303b705cfSriastradh#define COPY_FLAGS(a) GEN6_SET_FLAGS(COPY_SAMPLER, (a) == GXcopy ? NO_BLEND : CLEAR, GEN6_WM_KERNEL_NOMASK, COPY_VERTEX)
19403b705cfSriastradh
19503b705cfSriastradh#define FILL_SAMPLER (2 * sizeof(struct gen6_sampler_state))
19603b705cfSriastradh#define FILL_VERTEX VERTEX_2s2s
19703b705cfSriastradh#define FILL_FLAGS(op, format) GEN6_SET_FLAGS(FILL_SAMPLER, gen6_get_blend((op), false, (format)), GEN6_WM_KERNEL_NOMASK, FILL_VERTEX)
19803b705cfSriastradh#define FILL_FLAGS_NOBLEND GEN6_SET_FLAGS(FILL_SAMPLER, NO_BLEND, GEN6_WM_KERNEL_NOMASK, FILL_VERTEX)
19903b705cfSriastradh
20003b705cfSriastradh#define GEN6_SAMPLER(f) (((f) >> 16) & 0xfff0)
20103b705cfSriastradh#define GEN6_BLEND(f) (((f) >> 0) & 0xfff0)
20203b705cfSriastradh#define GEN6_KERNEL(f) (((f) >> 16) & 0xf)
20303b705cfSriastradh#define GEN6_VERTEX(f) (((f) >> 0) & 0xf)
20403b705cfSriastradh#define GEN6_SET_FLAGS(S, B, K, V)  (((S) | (K)) << 16 | ((B) | (V)))
20503b705cfSriastradh
20603b705cfSriastradh#define OUT_BATCH(v) batch_emit(sna, v)
20703b705cfSriastradh#define OUT_VERTEX(x,y) vertex_emit_2s(sna, x,y)
20803b705cfSriastradh#define OUT_VERTEX_F(v) vertex_emit(sna, v)
20903b705cfSriastradh
21003b705cfSriastradhstatic inline bool too_large(int width, int height)
21103b705cfSriastradh{
21203b705cfSriastradh	return width > GEN6_MAX_SIZE || height > GEN6_MAX_SIZE;
21303b705cfSriastradh}
21403b705cfSriastradh
21503b705cfSriastradhstatic uint32_t gen6_get_blend(int op,
21603b705cfSriastradh			       bool has_component_alpha,
21703b705cfSriastradh			       uint32_t dst_format)
21803b705cfSriastradh{
21903b705cfSriastradh	uint32_t src, dst;
22003b705cfSriastradh
22103b705cfSriastradh	src = gen6_blend_op[op].src_blend;
22203b705cfSriastradh	dst = gen6_blend_op[op].dst_blend;
22303b705cfSriastradh
22403b705cfSriastradh	/* If there's no dst alpha channel, adjust the blend op so that
22503b705cfSriastradh	 * we'll treat it always as 1.
22603b705cfSriastradh	 */
22703b705cfSriastradh	if (PICT_FORMAT_A(dst_format) == 0) {
22803b705cfSriastradh		if (src == GEN6_BLENDFACTOR_DST_ALPHA)
22903b705cfSriastradh			src = GEN6_BLENDFACTOR_ONE;
23003b705cfSriastradh		else if (src == GEN6_BLENDFACTOR_INV_DST_ALPHA)
23103b705cfSriastradh			src = GEN6_BLENDFACTOR_ZERO;
23203b705cfSriastradh	}
23303b705cfSriastradh
23403b705cfSriastradh	/* If the source alpha is being used, then we should only be in a
23503b705cfSriastradh	 * case where the source blend factor is 0, and the source blend
23603b705cfSriastradh	 * value is the mask channels multiplied by the source picture's alpha.
23703b705cfSriastradh	 */
23803b705cfSriastradh	if (has_component_alpha && gen6_blend_op[op].src_alpha) {
23903b705cfSriastradh		if (dst == GEN6_BLENDFACTOR_SRC_ALPHA)
24003b705cfSriastradh			dst = GEN6_BLENDFACTOR_SRC_COLOR;
24103b705cfSriastradh		else if (dst == GEN6_BLENDFACTOR_INV_SRC_ALPHA)
24203b705cfSriastradh			dst = GEN6_BLENDFACTOR_INV_SRC_COLOR;
24303b705cfSriastradh	}
24403b705cfSriastradh
24503b705cfSriastradh	DBG(("blend op=%d, dst=%x [A=%d] => src=%d, dst=%d => offset=%x\n",
24603b705cfSriastradh	     op, dst_format, PICT_FORMAT_A(dst_format),
24703b705cfSriastradh	     src, dst, (int)BLEND_OFFSET(src, dst)));
24803b705cfSriastradh	return BLEND_OFFSET(src, dst);
24903b705cfSriastradh}
25003b705cfSriastradh
25103b705cfSriastradhstatic uint32_t gen6_get_card_format(PictFormat format)
25203b705cfSriastradh{
25303b705cfSriastradh	switch (format) {
25403b705cfSriastradh	default:
25503b705cfSriastradh		return -1;
25603b705cfSriastradh	case PICT_a8r8g8b8:
25703b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8A8_UNORM;
25803b705cfSriastradh	case PICT_x8r8g8b8:
25903b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8X8_UNORM;
26003b705cfSriastradh	case PICT_a8b8g8r8:
26103b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8A8_UNORM;
26203b705cfSriastradh	case PICT_x8b8g8r8:
26303b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8X8_UNORM;
26442542f5fSchristos#ifdef PICT_a2r10g10b10
26503b705cfSriastradh	case PICT_a2r10g10b10:
26603b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10A2_UNORM;
26703b705cfSriastradh	case PICT_x2r10g10b10:
26803b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10X2_UNORM;
26942542f5fSchristos#endif
27003b705cfSriastradh	case PICT_r8g8b8:
27103b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8_UNORM;
27203b705cfSriastradh	case PICT_r5g6b5:
27303b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G6R5_UNORM;
27403b705cfSriastradh	case PICT_a1r5g5b5:
27503b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G5R5A1_UNORM;
27603b705cfSriastradh	case PICT_a8:
27703b705cfSriastradh		return GEN6_SURFACEFORMAT_A8_UNORM;
27803b705cfSriastradh	case PICT_a4r4g4b4:
27903b705cfSriastradh		return GEN6_SURFACEFORMAT_B4G4R4A4_UNORM;
28003b705cfSriastradh	}
28103b705cfSriastradh}
28203b705cfSriastradh
28303b705cfSriastradhstatic uint32_t gen6_get_dest_format(PictFormat format)
28403b705cfSriastradh{
28503b705cfSriastradh	switch (format) {
28603b705cfSriastradh	default:
28703b705cfSriastradh		return -1;
28803b705cfSriastradh	case PICT_a8r8g8b8:
28903b705cfSriastradh	case PICT_x8r8g8b8:
29003b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8A8_UNORM;
29103b705cfSriastradh	case PICT_a8b8g8r8:
29203b705cfSriastradh	case PICT_x8b8g8r8:
29303b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8A8_UNORM;
29442542f5fSchristos#ifdef PICT_a2r10g10b10
29503b705cfSriastradh	case PICT_a2r10g10b10:
29603b705cfSriastradh	case PICT_x2r10g10b10:
29703b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10A2_UNORM;
29842542f5fSchristos#endif
29903b705cfSriastradh	case PICT_r5g6b5:
30003b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G6R5_UNORM;
30103b705cfSriastradh	case PICT_x1r5g5b5:
30203b705cfSriastradh	case PICT_a1r5g5b5:
30303b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G5R5A1_UNORM;
30403b705cfSriastradh	case PICT_a8:
30503b705cfSriastradh		return GEN6_SURFACEFORMAT_A8_UNORM;
30603b705cfSriastradh	case PICT_a4r4g4b4:
30703b705cfSriastradh	case PICT_x4r4g4b4:
30803b705cfSriastradh		return GEN6_SURFACEFORMAT_B4G4R4A4_UNORM;
30903b705cfSriastradh	}
31003b705cfSriastradh}
31103b705cfSriastradh
31203b705cfSriastradhstatic bool gen6_check_dst_format(PictFormat format)
31303b705cfSriastradh{
31403b705cfSriastradh	if (gen6_get_dest_format(format) != -1)
31503b705cfSriastradh		return true;
31603b705cfSriastradh
31703b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
31803b705cfSriastradh	return false;
31903b705cfSriastradh}
32003b705cfSriastradh
32103b705cfSriastradhstatic bool gen6_check_format(uint32_t format)
32203b705cfSriastradh{
32303b705cfSriastradh	if (gen6_get_card_format(format) != -1)
32403b705cfSriastradh		return true;
32503b705cfSriastradh
32603b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
32703b705cfSriastradh	return false;
32803b705cfSriastradh}
32903b705cfSriastradh
33003b705cfSriastradhstatic uint32_t gen6_filter(uint32_t filter)
33103b705cfSriastradh{
33203b705cfSriastradh	switch (filter) {
33303b705cfSriastradh	default:
33403b705cfSriastradh		assert(0);
33503b705cfSriastradh	case PictFilterNearest:
33603b705cfSriastradh		return SAMPLER_FILTER_NEAREST;
33703b705cfSriastradh	case PictFilterBilinear:
33803b705cfSriastradh		return SAMPLER_FILTER_BILINEAR;
33903b705cfSriastradh	}
34003b705cfSriastradh}
34103b705cfSriastradh
34203b705cfSriastradhstatic uint32_t gen6_check_filter(PicturePtr picture)
34303b705cfSriastradh{
34403b705cfSriastradh	switch (picture->filter) {
34503b705cfSriastradh	case PictFilterNearest:
34603b705cfSriastradh	case PictFilterBilinear:
34703b705cfSriastradh		return true;
34803b705cfSriastradh	default:
34903b705cfSriastradh		return false;
35003b705cfSriastradh	}
35103b705cfSriastradh}
35203b705cfSriastradh
35303b705cfSriastradhstatic uint32_t gen6_repeat(uint32_t repeat)
35403b705cfSriastradh{
35503b705cfSriastradh	switch (repeat) {
35603b705cfSriastradh	default:
35703b705cfSriastradh		assert(0);
35803b705cfSriastradh	case RepeatNone:
35903b705cfSriastradh		return SAMPLER_EXTEND_NONE;
36003b705cfSriastradh	case RepeatNormal:
36103b705cfSriastradh		return SAMPLER_EXTEND_REPEAT;
36203b705cfSriastradh	case RepeatPad:
36303b705cfSriastradh		return SAMPLER_EXTEND_PAD;
36403b705cfSriastradh	case RepeatReflect:
36503b705cfSriastradh		return SAMPLER_EXTEND_REFLECT;
36603b705cfSriastradh	}
36703b705cfSriastradh}
36803b705cfSriastradh
36903b705cfSriastradhstatic bool gen6_check_repeat(PicturePtr picture)
37003b705cfSriastradh{
37103b705cfSriastradh	if (!picture->repeat)
37203b705cfSriastradh		return true;
37303b705cfSriastradh
37403b705cfSriastradh	switch (picture->repeatType) {
37503b705cfSriastradh	case RepeatNone:
37603b705cfSriastradh	case RepeatNormal:
37703b705cfSriastradh	case RepeatPad:
37803b705cfSriastradh	case RepeatReflect:
37903b705cfSriastradh		return true;
38003b705cfSriastradh	default:
38103b705cfSriastradh		return false;
38203b705cfSriastradh	}
38303b705cfSriastradh}
38403b705cfSriastradh
38503b705cfSriastradhstatic int
38603b705cfSriastradhgen6_choose_composite_kernel(int op, bool has_mask, bool is_ca, bool is_affine)
38703b705cfSriastradh{
38803b705cfSriastradh	int base;
38903b705cfSriastradh
39003b705cfSriastradh	if (has_mask) {
39103b705cfSriastradh		if (is_ca) {
39203b705cfSriastradh			if (gen6_blend_op[op].src_alpha)
39303b705cfSriastradh				base = GEN6_WM_KERNEL_MASKSA;
39403b705cfSriastradh			else
39503b705cfSriastradh				base = GEN6_WM_KERNEL_MASKCA;
39603b705cfSriastradh		} else
39703b705cfSriastradh			base = GEN6_WM_KERNEL_MASK;
39803b705cfSriastradh	} else
39903b705cfSriastradh		base = GEN6_WM_KERNEL_NOMASK;
40003b705cfSriastradh
40103b705cfSriastradh	return base + !is_affine;
40203b705cfSriastradh}
40303b705cfSriastradh
40413496ba1Ssnjinline static void
40513496ba1Ssnjgen6_emit_pipe_invalidate(struct sna *sna)
40613496ba1Ssnj{
40713496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
40813496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL_WC_FLUSH |
40913496ba1Ssnj		  GEN6_PIPE_CONTROL_TC_FLUSH |
41013496ba1Ssnj		  GEN6_PIPE_CONTROL_CS_STALL);
41113496ba1Ssnj	OUT_BATCH(0);
41213496ba1Ssnj	OUT_BATCH(0);
41313496ba1Ssnj}
41413496ba1Ssnj
41513496ba1Ssnjinline static void
41613496ba1Ssnjgen6_emit_pipe_flush(struct sna *sna, bool need_stall)
41713496ba1Ssnj{
41813496ba1Ssnj	unsigned stall;
41913496ba1Ssnj
42013496ba1Ssnj	stall = 0;
42113496ba1Ssnj	if (need_stall)
42213496ba1Ssnj		stall = GEN6_PIPE_CONTROL_CS_STALL;
42313496ba1Ssnj
42413496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
42513496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL_WC_FLUSH | stall);
42613496ba1Ssnj	OUT_BATCH(0);
42713496ba1Ssnj	OUT_BATCH(0);
42813496ba1Ssnj}
42913496ba1Ssnj
43013496ba1Ssnjinline static void
43113496ba1Ssnjgen6_emit_pipe_stall(struct sna *sna)
43213496ba1Ssnj{
43313496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
43413496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL_CS_STALL |
43513496ba1Ssnj		  GEN6_PIPE_CONTROL_STALL_AT_SCOREBOARD);
43613496ba1Ssnj	OUT_BATCH(0);
43713496ba1Ssnj	OUT_BATCH(0);
43813496ba1Ssnj}
43913496ba1Ssnj
44003b705cfSriastradhstatic void
44103b705cfSriastradhgen6_emit_urb(struct sna *sna)
44203b705cfSriastradh{
44303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_URB | (3 - 2));
44403b705cfSriastradh	OUT_BATCH(((1 - 1) << GEN6_3DSTATE_URB_VS_SIZE_SHIFT) |
44503b705cfSriastradh		  (sna->render_state.gen6.info->urb.max_vs_entries << GEN6_3DSTATE_URB_VS_ENTRIES_SHIFT)); /* at least 24 on GEN6 */
44603b705cfSriastradh	OUT_BATCH((0 << GEN6_3DSTATE_URB_GS_SIZE_SHIFT) |
44703b705cfSriastradh		  (0 << GEN6_3DSTATE_URB_GS_ENTRIES_SHIFT)); /* no GS thread */
44803b705cfSriastradh}
44903b705cfSriastradh
45003b705cfSriastradhstatic void
45103b705cfSriastradhgen6_emit_state_base_address(struct sna *sna)
45203b705cfSriastradh{
45303b705cfSriastradh	OUT_BATCH(GEN6_STATE_BASE_ADDRESS | (10 - 2));
45403b705cfSriastradh	OUT_BATCH(0); /* general */
45503b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* surface */
45603b705cfSriastradh				 sna->kgem.nbatch,
45703b705cfSriastradh				 NULL,
45803b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
45903b705cfSriastradh				 BASE_ADDRESS_MODIFY));
46003b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* instruction */
46103b705cfSriastradh				 sna->kgem.nbatch,
46203b705cfSriastradh				 sna->render_state.gen6.general_bo,
46303b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
46403b705cfSriastradh				 BASE_ADDRESS_MODIFY));
46503b705cfSriastradh	OUT_BATCH(0); /* indirect */
46603b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem,
46703b705cfSriastradh				 sna->kgem.nbatch,
46803b705cfSriastradh				 sna->render_state.gen6.general_bo,
46903b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
47003b705cfSriastradh				 BASE_ADDRESS_MODIFY));
47103b705cfSriastradh
47203b705cfSriastradh	/* upper bounds, disable */
47303b705cfSriastradh	OUT_BATCH(0);
47403b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
47503b705cfSriastradh	OUT_BATCH(0);
47603b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
47703b705cfSriastradh}
47803b705cfSriastradh
47903b705cfSriastradhstatic void
48003b705cfSriastradhgen6_emit_viewports(struct sna *sna)
48103b705cfSriastradh{
48203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VIEWPORT_STATE_POINTERS |
48303b705cfSriastradh		  GEN6_3DSTATE_VIEWPORT_STATE_MODIFY_CC |
48403b705cfSriastradh		  (4 - 2));
48503b705cfSriastradh	OUT_BATCH(0);
48603b705cfSriastradh	OUT_BATCH(0);
48703b705cfSriastradh	OUT_BATCH(0);
48803b705cfSriastradh}
48903b705cfSriastradh
49003b705cfSriastradhstatic void
49103b705cfSriastradhgen6_emit_vs(struct sna *sna)
49203b705cfSriastradh{
49303b705cfSriastradh	/* disable VS constant buffer */
49403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_VS | (5 - 2));
49503b705cfSriastradh	OUT_BATCH(0);
49603b705cfSriastradh	OUT_BATCH(0);
49703b705cfSriastradh	OUT_BATCH(0);
49803b705cfSriastradh	OUT_BATCH(0);
49903b705cfSriastradh
50003b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VS | (6 - 2));
50103b705cfSriastradh	OUT_BATCH(0); /* no VS kernel */
50203b705cfSriastradh	OUT_BATCH(0);
50303b705cfSriastradh	OUT_BATCH(0);
50403b705cfSriastradh	OUT_BATCH(0);
50503b705cfSriastradh	OUT_BATCH(0); /* pass-through */
50603b705cfSriastradh}
50703b705cfSriastradh
50803b705cfSriastradhstatic void
50903b705cfSriastradhgen6_emit_gs(struct sna *sna)
51003b705cfSriastradh{
51103b705cfSriastradh	/* disable GS constant buffer */
51203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_GS | (5 - 2));
51303b705cfSriastradh	OUT_BATCH(0);
51403b705cfSriastradh	OUT_BATCH(0);
51503b705cfSriastradh	OUT_BATCH(0);
51603b705cfSriastradh	OUT_BATCH(0);
51703b705cfSriastradh
51803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_GS | (7 - 2));
51903b705cfSriastradh	OUT_BATCH(0); /* no GS kernel */
52003b705cfSriastradh	OUT_BATCH(0);
52103b705cfSriastradh	OUT_BATCH(0);
52203b705cfSriastradh	OUT_BATCH(0);
52303b705cfSriastradh	OUT_BATCH(0);
52403b705cfSriastradh	OUT_BATCH(0); /* pass-through */
52503b705cfSriastradh}
52603b705cfSriastradh
52703b705cfSriastradhstatic void
52803b705cfSriastradhgen6_emit_clip(struct sna *sna)
52903b705cfSriastradh{
53003b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CLIP | (4 - 2));
53103b705cfSriastradh	OUT_BATCH(0);
53203b705cfSriastradh	OUT_BATCH(0); /* pass-through */
53303b705cfSriastradh	OUT_BATCH(0);
53403b705cfSriastradh}
53503b705cfSriastradh
53603b705cfSriastradhstatic void
53703b705cfSriastradhgen6_emit_wm_constants(struct sna *sna)
53803b705cfSriastradh{
53903b705cfSriastradh	/* disable WM constant buffer */
54003b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_PS | (5 - 2));
54103b705cfSriastradh	OUT_BATCH(0);
54203b705cfSriastradh	OUT_BATCH(0);
54303b705cfSriastradh	OUT_BATCH(0);
54403b705cfSriastradh	OUT_BATCH(0);
54503b705cfSriastradh}
54603b705cfSriastradh
54703b705cfSriastradhstatic void
54803b705cfSriastradhgen6_emit_null_depth_buffer(struct sna *sna)
54903b705cfSriastradh{
55003b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_DEPTH_BUFFER | (7 - 2));
55103b705cfSriastradh	OUT_BATCH(GEN6_SURFACE_NULL << GEN6_3DSTATE_DEPTH_BUFFER_TYPE_SHIFT |
55203b705cfSriastradh		  GEN6_DEPTHFORMAT_D32_FLOAT << GEN6_3DSTATE_DEPTH_BUFFER_FORMAT_SHIFT);
55303b705cfSriastradh	OUT_BATCH(0);
55403b705cfSriastradh	OUT_BATCH(0);
55503b705cfSriastradh	OUT_BATCH(0);
55603b705cfSriastradh	OUT_BATCH(0);
55703b705cfSriastradh	OUT_BATCH(0);
55803b705cfSriastradh
55903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CLEAR_PARAMS | (2 - 2));
56003b705cfSriastradh	OUT_BATCH(0);
56103b705cfSriastradh}
56203b705cfSriastradh
56303b705cfSriastradhstatic void
56403b705cfSriastradhgen6_emit_invariant(struct sna *sna)
56503b705cfSriastradh{
56603b705cfSriastradh	OUT_BATCH(GEN6_PIPELINE_SELECT | PIPELINE_SELECT_3D);
56703b705cfSriastradh
56803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_MULTISAMPLE | (3 - 2));
56903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_MULTISAMPLE_PIXEL_LOCATION_CENTER |
57003b705cfSriastradh		  GEN6_3DSTATE_MULTISAMPLE_NUMSAMPLES_1); /* 1 sample/pixel */
57103b705cfSriastradh	OUT_BATCH(0);
57203b705cfSriastradh
57303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SAMPLE_MASK | (2 - 2));
57403b705cfSriastradh	OUT_BATCH(1);
57503b705cfSriastradh
57603b705cfSriastradh	gen6_emit_urb(sna);
57703b705cfSriastradh
57803b705cfSriastradh	gen6_emit_state_base_address(sna);
57903b705cfSriastradh
58003b705cfSriastradh	gen6_emit_viewports(sna);
58103b705cfSriastradh	gen6_emit_vs(sna);
58203b705cfSriastradh	gen6_emit_gs(sna);
58303b705cfSriastradh	gen6_emit_clip(sna);
58403b705cfSriastradh	gen6_emit_wm_constants(sna);
58503b705cfSriastradh	gen6_emit_null_depth_buffer(sna);
58603b705cfSriastradh
58703b705cfSriastradh	sna->render_state.gen6.needs_invariant = false;
58803b705cfSriastradh}
58903b705cfSriastradh
59013496ba1Ssnjstatic void
59103b705cfSriastradhgen6_emit_cc(struct sna *sna, int blend)
59203b705cfSriastradh{
59303b705cfSriastradh	struct gen6_render_state *render = &sna->render_state.gen6;
59403b705cfSriastradh
59503b705cfSriastradh	if (render->blend == blend)
59613496ba1Ssnj		return;
59703b705cfSriastradh
59803b705cfSriastradh	DBG(("%s: blend = %x\n", __FUNCTION__, blend));
59903b705cfSriastradh
60003b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CC_STATE_POINTERS | (4 - 2));
60103b705cfSriastradh	OUT_BATCH((render->cc_blend + blend) | 1);
60203b705cfSriastradh	if (render->blend == (unsigned)-1) {
60303b705cfSriastradh		OUT_BATCH(1);
60403b705cfSriastradh		OUT_BATCH(1);
60503b705cfSriastradh	} else {
60603b705cfSriastradh		OUT_BATCH(0);
60703b705cfSriastradh		OUT_BATCH(0);
60803b705cfSriastradh	}
60903b705cfSriastradh
61003b705cfSriastradh	render->blend = blend;
61103b705cfSriastradh}
61203b705cfSriastradh
61303b705cfSriastradhstatic void
61403b705cfSriastradhgen6_emit_sampler(struct sna *sna, uint32_t state)
61503b705cfSriastradh{
61603b705cfSriastradh	if (sna->render_state.gen6.samplers == state)
61703b705cfSriastradh		return;
61803b705cfSriastradh
61903b705cfSriastradh	sna->render_state.gen6.samplers = state;
62003b705cfSriastradh
62103b705cfSriastradh	DBG(("%s: sampler = %x\n", __FUNCTION__, state));
62203b705cfSriastradh
62303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SAMPLER_STATE_POINTERS |
62403b705cfSriastradh		  GEN6_3DSTATE_SAMPLER_STATE_MODIFY_PS |
62503b705cfSriastradh		  (4 - 2));
62603b705cfSriastradh	OUT_BATCH(0); /* VS */
62703b705cfSriastradh	OUT_BATCH(0); /* GS */
62803b705cfSriastradh	OUT_BATCH(sna->render_state.gen6.wm_state + state);
62903b705cfSriastradh}
63003b705cfSriastradh
63103b705cfSriastradhstatic void
63203b705cfSriastradhgen6_emit_sf(struct sna *sna, bool has_mask)
63303b705cfSriastradh{
63403b705cfSriastradh	int num_sf_outputs = has_mask ? 2 : 1;
63503b705cfSriastradh
63603b705cfSriastradh	if (sna->render_state.gen6.num_sf_outputs == num_sf_outputs)
63703b705cfSriastradh		return;
63803b705cfSriastradh
63903b705cfSriastradh	DBG(("%s: num_sf_outputs=%d, read_length=%d, read_offset=%d\n",
64003b705cfSriastradh	     __FUNCTION__, num_sf_outputs, 1, 0));
64103b705cfSriastradh
64203b705cfSriastradh	sna->render_state.gen6.num_sf_outputs = num_sf_outputs;
64303b705cfSriastradh
64403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SF | (20 - 2));
64503b705cfSriastradh	OUT_BATCH(num_sf_outputs << GEN6_3DSTATE_SF_NUM_OUTPUTS_SHIFT |
64603b705cfSriastradh		  1 << GEN6_3DSTATE_SF_URB_ENTRY_READ_LENGTH_SHIFT |
64703b705cfSriastradh		  1 << GEN6_3DSTATE_SF_URB_ENTRY_READ_OFFSET_SHIFT);
64803b705cfSriastradh	OUT_BATCH(0);
64903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SF_CULL_NONE);
65003b705cfSriastradh	OUT_BATCH(2 << GEN6_3DSTATE_SF_TRIFAN_PROVOKE_SHIFT); /* DW4 */
65103b705cfSriastradh	OUT_BATCH(0);
65203b705cfSriastradh	OUT_BATCH(0);
65303b705cfSriastradh	OUT_BATCH(0);
65403b705cfSriastradh	OUT_BATCH(0);
65503b705cfSriastradh	OUT_BATCH(0); /* DW9 */
65603b705cfSriastradh	OUT_BATCH(0);
65703b705cfSriastradh	OUT_BATCH(0);
65803b705cfSriastradh	OUT_BATCH(0);
65903b705cfSriastradh	OUT_BATCH(0);
66003b705cfSriastradh	OUT_BATCH(0); /* DW14 */
66103b705cfSriastradh	OUT_BATCH(0);
66203b705cfSriastradh	OUT_BATCH(0);
66303b705cfSriastradh	OUT_BATCH(0);
66403b705cfSriastradh	OUT_BATCH(0);
66503b705cfSriastradh	OUT_BATCH(0); /* DW19 */
66603b705cfSriastradh}
66703b705cfSriastradh
66803b705cfSriastradhstatic void
66903b705cfSriastradhgen6_emit_wm(struct sna *sna, unsigned int kernel, bool has_mask)
67003b705cfSriastradh{
67103b705cfSriastradh	const uint32_t *kernels;
67203b705cfSriastradh
67303b705cfSriastradh	if (sna->render_state.gen6.kernel == kernel)
67403b705cfSriastradh		return;
67503b705cfSriastradh
67603b705cfSriastradh	sna->render_state.gen6.kernel = kernel;
67703b705cfSriastradh	kernels = sna->render_state.gen6.wm_kernel[kernel];
67803b705cfSriastradh
67903b705cfSriastradh	DBG(("%s: switching to %s, num_surfaces=%d (8-pixel? %d, 16-pixel? %d,32-pixel? %d)\n",
68003b705cfSriastradh	     __FUNCTION__,
68103b705cfSriastradh	     wm_kernels[kernel].name, wm_kernels[kernel].num_surfaces,
68203b705cfSriastradh	    kernels[0], kernels[1], kernels[2]));
68303b705cfSriastradh
68403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_WM | (9 - 2));
68503b705cfSriastradh	OUT_BATCH(kernels[0] ?: kernels[1] ?: kernels[2]);
68603b705cfSriastradh	OUT_BATCH(1 << GEN6_3DSTATE_WM_SAMPLER_COUNT_SHIFT |
68703b705cfSriastradh		  wm_kernels[kernel].num_surfaces << GEN6_3DSTATE_WM_BINDING_TABLE_ENTRY_COUNT_SHIFT);
68803b705cfSriastradh	OUT_BATCH(0); /* scratch space */
68903b705cfSriastradh	OUT_BATCH((kernels[0] ? 4 : kernels[1] ? 6 : 8) << GEN6_3DSTATE_WM_DISPATCH_0_START_GRF_SHIFT |
69003b705cfSriastradh		  8 << GEN6_3DSTATE_WM_DISPATCH_1_START_GRF_SHIFT |
69103b705cfSriastradh		  6 << GEN6_3DSTATE_WM_DISPATCH_2_START_GRF_SHIFT);
69203b705cfSriastradh	OUT_BATCH((sna->render_state.gen6.info->max_wm_threads - 1) << GEN6_3DSTATE_WM_MAX_THREADS_SHIFT |
69303b705cfSriastradh		  (kernels[0] ? GEN6_3DSTATE_WM_8_DISPATCH_ENABLE : 0) |
69403b705cfSriastradh		  (kernels[1] ? GEN6_3DSTATE_WM_16_DISPATCH_ENABLE : 0) |
69503b705cfSriastradh		  (kernels[2] ? GEN6_3DSTATE_WM_32_DISPATCH_ENABLE : 0) |
69603b705cfSriastradh		  GEN6_3DSTATE_WM_DISPATCH_ENABLE);
69703b705cfSriastradh	OUT_BATCH((1 + has_mask) << GEN6_3DSTATE_WM_NUM_SF_OUTPUTS_SHIFT |
69803b705cfSriastradh		  GEN6_3DSTATE_WM_PERSPECTIVE_PIXEL_BARYCENTRIC);
69903b705cfSriastradh	OUT_BATCH(kernels[2]);
70003b705cfSriastradh	OUT_BATCH(kernels[1]);
70103b705cfSriastradh}
70203b705cfSriastradh
70303b705cfSriastradhstatic bool
70403b705cfSriastradhgen6_emit_binding_table(struct sna *sna, uint16_t offset)
70503b705cfSriastradh{
70603b705cfSriastradh	if (sna->render_state.gen6.surface_table == offset)
70703b705cfSriastradh		return false;
70803b705cfSriastradh
70903b705cfSriastradh	/* Binding table pointers */
71003b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_BINDING_TABLE_POINTERS |
71103b705cfSriastradh		  GEN6_3DSTATE_BINDING_TABLE_MODIFY_PS |
71203b705cfSriastradh		  (4 - 2));
71303b705cfSriastradh	OUT_BATCH(0);		/* vs */
71403b705cfSriastradh	OUT_BATCH(0);		/* gs */
71503b705cfSriastradh	/* Only the PS uses the binding table */
71603b705cfSriastradh	OUT_BATCH(offset*4);
71703b705cfSriastradh
71803b705cfSriastradh	sna->render_state.gen6.surface_table = offset;
71903b705cfSriastradh	return true;
72003b705cfSriastradh}
72103b705cfSriastradh
72203b705cfSriastradhstatic bool
72303b705cfSriastradhgen6_emit_drawing_rectangle(struct sna *sna,
72403b705cfSriastradh			    const struct sna_composite_op *op)
72503b705cfSriastradh{
72603b705cfSriastradh	uint32_t limit = (op->dst.height - 1) << 16 | (op->dst.width - 1);
72703b705cfSriastradh	uint32_t offset = (uint16_t)op->dst.y << 16 | (uint16_t)op->dst.x;
72803b705cfSriastradh
72913496ba1Ssnj	assert(!too_large(abs(op->dst.x), abs(op->dst.y)));
73003b705cfSriastradh	assert(!too_large(op->dst.width, op->dst.height));
73103b705cfSriastradh
73203b705cfSriastradh	if (sna->render_state.gen6.drawrect_limit  == limit &&
73303b705cfSriastradh	    sna->render_state.gen6.drawrect_offset == offset)
73413496ba1Ssnj		return true;
73503b705cfSriastradh
73603b705cfSriastradh	/* [DevSNB-C+{W/A}] Before any depth stall flush (including those
73703b705cfSriastradh	 * produced by non-pipelined state commands), software needs to first
73803b705cfSriastradh	 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
73903b705cfSriastradh	 * 0.
74003b705cfSriastradh	 *
74103b705cfSriastradh	 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
74203b705cfSriastradh	 * BEFORE the pipe-control with a post-sync op and no write-cache
74303b705cfSriastradh	 * flushes.
74403b705cfSriastradh	 */
74513496ba1Ssnj	if (!sna->render_state.gen6.first_state_packet)
74613496ba1Ssnj		gen6_emit_pipe_stall(sna);
74703b705cfSriastradh
74803b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
74903b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL_WRITE_TIME);
75003b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, sna->kgem.nbatch,
75103b705cfSriastradh				 sna->render_state.gen6.general_bo,
75203b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16 |
75303b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION,
75403b705cfSriastradh				 64));
75503b705cfSriastradh	OUT_BATCH(0);
75603b705cfSriastradh
75742542f5fSchristos	DBG(("%s: offset=(%d, %d), limit=(%d, %d)\n",
75813496ba1Ssnj	     __FUNCTION__, op->dst.x, op->dst.y, op->dst.width, op->dst.height));
75903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
76003b705cfSriastradh	OUT_BATCH(0);
76103b705cfSriastradh	OUT_BATCH(limit);
76203b705cfSriastradh	OUT_BATCH(offset);
76303b705cfSriastradh
76403b705cfSriastradh	sna->render_state.gen6.drawrect_offset = offset;
76503b705cfSriastradh	sna->render_state.gen6.drawrect_limit = limit;
76613496ba1Ssnj	return false;
76703b705cfSriastradh}
76803b705cfSriastradh
76903b705cfSriastradhstatic void
77003b705cfSriastradhgen6_emit_vertex_elements(struct sna *sna,
77103b705cfSriastradh			  const struct sna_composite_op *op)
77203b705cfSriastradh{
77303b705cfSriastradh	/*
77403b705cfSriastradh	 * vertex data in vertex buffer
77503b705cfSriastradh	 *    position: (x, y)
77603b705cfSriastradh	 *    texture coordinate 0: (u0, v0) if (is_affine is true) else (u0, v0, w0)
77703b705cfSriastradh	 *    texture coordinate 1 if (has_mask is true): same as above
77803b705cfSriastradh	 */
77903b705cfSriastradh	struct gen6_render_state *render = &sna->render_state.gen6;
78003b705cfSriastradh	uint32_t src_format, dw;
78103b705cfSriastradh	int id = GEN6_VERTEX(op->u.gen6.flags);
78203b705cfSriastradh	bool has_mask;
78303b705cfSriastradh
78403b705cfSriastradh	DBG(("%s: setup id=%d\n", __FUNCTION__, id));
78503b705cfSriastradh
78603b705cfSriastradh	if (render->ve_id == id)
78703b705cfSriastradh		return;
78803b705cfSriastradh	render->ve_id = id;
78903b705cfSriastradh
79003b705cfSriastradh	/* The VUE layout
79103b705cfSriastradh	 *    dword 0-3: pad (0.0, 0.0, 0.0. 0.0)
79203b705cfSriastradh	 *    dword 4-7: position (x, y, 1.0, 1.0),
79303b705cfSriastradh	 *    dword 8-11: texture coordinate 0 (u0, v0, w0, 1.0)
79403b705cfSriastradh	 *    dword 12-15: texture coordinate 1 (u1, v1, w1, 1.0)
79503b705cfSriastradh	 *
79603b705cfSriastradh	 * dword 4-15 are fetched from vertex buffer
79703b705cfSriastradh	 */
79803b705cfSriastradh	has_mask = (id >> 2) != 0;
79903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VERTEX_ELEMENTS |
80003b705cfSriastradh		((2 * (3 + has_mask)) + 1 - 2));
80103b705cfSriastradh
80203b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
80303b705cfSriastradh		  GEN6_SURFACEFORMAT_R32G32B32A32_FLOAT << VE0_FORMAT_SHIFT |
80403b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
80503b705cfSriastradh	OUT_BATCH(GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT |
80603b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT |
80703b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
80803b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_3_SHIFT);
80903b705cfSriastradh
81003b705cfSriastradh	/* x,y */
81103b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
81203b705cfSriastradh		  GEN6_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
81303b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
81403b705cfSriastradh	OUT_BATCH(GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
81503b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
81603b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
81703b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
81803b705cfSriastradh
81903b705cfSriastradh	/* u0, v0, w0 */
82003b705cfSriastradh	DBG(("%s: first channel %d floats, offset=4b\n", __FUNCTION__, id & 3));
82103b705cfSriastradh	dw = GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
82203b705cfSriastradh	switch (id & 3) {
82303b705cfSriastradh	default:
82403b705cfSriastradh		assert(0);
82503b705cfSriastradh	case 0:
82603b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R16G16_SSCALED;
82703b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
82803b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
82903b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
83003b705cfSriastradh		break;
83103b705cfSriastradh	case 1:
83203b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32_FLOAT;
83303b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
83403b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
83503b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
83603b705cfSriastradh		break;
83703b705cfSriastradh	case 2:
83803b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32G32_FLOAT;
83903b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
84003b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
84103b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
84203b705cfSriastradh		break;
84303b705cfSriastradh	case 3:
84403b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32G32B32_FLOAT;
84503b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
84603b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
84703b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
84803b705cfSriastradh		break;
84903b705cfSriastradh	}
85003b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
85103b705cfSriastradh		  src_format << VE0_FORMAT_SHIFT |
85203b705cfSriastradh		  4 << VE0_OFFSET_SHIFT);
85303b705cfSriastradh	OUT_BATCH(dw);
85403b705cfSriastradh
85503b705cfSriastradh	/* u1, v1, w1 */
85603b705cfSriastradh	if (has_mask) {
85703b705cfSriastradh		unsigned offset = 4 + ((id & 3) ?: 1) * sizeof(float);
85803b705cfSriastradh		DBG(("%s: second channel %d floats, offset=%db\n", __FUNCTION__, id >> 2, offset));
85903b705cfSriastradh		dw = GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
86003b705cfSriastradh		switch (id >> 2) {
86103b705cfSriastradh		case 1:
86203b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32_FLOAT;
86303b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
86403b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
86503b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
86603b705cfSriastradh			break;
86703b705cfSriastradh		default:
86803b705cfSriastradh			assert(0);
86903b705cfSriastradh		case 2:
87003b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32G32_FLOAT;
87103b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
87203b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
87303b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
87403b705cfSriastradh			break;
87503b705cfSriastradh		case 3:
87603b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32G32B32_FLOAT;
87703b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
87803b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
87903b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
88003b705cfSriastradh			break;
88103b705cfSriastradh		}
88203b705cfSriastradh		OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
88303b705cfSriastradh			  src_format << VE0_FORMAT_SHIFT |
88403b705cfSriastradh			  offset << VE0_OFFSET_SHIFT);
88503b705cfSriastradh		OUT_BATCH(dw);
88603b705cfSriastradh	}
88703b705cfSriastradh}
88803b705cfSriastradh
88903b705cfSriastradhstatic void
89003b705cfSriastradhgen6_emit_state(struct sna *sna,
89103b705cfSriastradh		const struct sna_composite_op *op,
89203b705cfSriastradh		uint16_t wm_binding_table)
89303b705cfSriastradh{
89413496ba1Ssnj	bool need_invalidate;
89513496ba1Ssnj	bool need_flush;
89613496ba1Ssnj	bool need_stall;
89703b705cfSriastradh
89803b705cfSriastradh	assert(op->dst.bo->exec);
89903b705cfSriastradh
90013496ba1Ssnj	need_flush = wm_binding_table & 1;
90113496ba1Ssnj	if (ALWAYS_FLUSH)
90213496ba1Ssnj		need_flush = true;
90303b705cfSriastradh
90413496ba1Ssnj	wm_binding_table &= ~1;
90513496ba1Ssnj	need_stall = sna->render_state.gen6.surface_table != wm_binding_table;
90613496ba1Ssnj	if (ALWAYS_STALL)
90713496ba1Ssnj		need_stall = true;
90813496ba1Ssnj
90913496ba1Ssnj	need_invalidate = kgem_bo_is_dirty(op->src.bo) || kgem_bo_is_dirty(op->mask.bo);
91013496ba1Ssnj	if (ALWAYS_INVALIDATE)
91113496ba1Ssnj		need_invalidate = true;
91213496ba1Ssnj
91313496ba1Ssnj	if (need_invalidate) {
91413496ba1Ssnj		gen6_emit_pipe_invalidate(sna);
91503b705cfSriastradh		kgem_clear_dirty(&sna->kgem);
91603b705cfSriastradh		assert(op->dst.bo->exec);
91703b705cfSriastradh		kgem_bo_mark_dirty(op->dst.bo);
91813496ba1Ssnj
91913496ba1Ssnj		need_flush = false;
92003b705cfSriastradh		need_stall = false;
92113496ba1Ssnj		sna->render_state.gen6.first_state_packet = true;
92203b705cfSriastradh	}
92313496ba1Ssnj	if (need_flush) {
92413496ba1Ssnj		gen6_emit_pipe_flush(sna, need_stall);
92513496ba1Ssnj		need_stall = false;
92613496ba1Ssnj		sna->render_state.gen6.first_state_packet = true;
92703b705cfSriastradh	}
92813496ba1Ssnj
92913496ba1Ssnj	need_stall &= gen6_emit_drawing_rectangle(sna, op);
93013496ba1Ssnj	if (need_stall)
93113496ba1Ssnj		gen6_emit_pipe_stall(sna);
93213496ba1Ssnj
93313496ba1Ssnj	gen6_emit_cc(sna, GEN6_BLEND(op->u.gen6.flags));
93413496ba1Ssnj	gen6_emit_sampler(sna, GEN6_SAMPLER(op->u.gen6.flags));
93513496ba1Ssnj	gen6_emit_sf(sna, GEN6_VERTEX(op->u.gen6.flags) >> 2);
93613496ba1Ssnj	gen6_emit_wm(sna, GEN6_KERNEL(op->u.gen6.flags), GEN6_VERTEX(op->u.gen6.flags) >> 2);
93713496ba1Ssnj	gen6_emit_vertex_elements(sna, op);
93813496ba1Ssnj	gen6_emit_binding_table(sna, wm_binding_table);
93913496ba1Ssnj
94003b705cfSriastradh	sna->render_state.gen6.first_state_packet = false;
94103b705cfSriastradh}
94203b705cfSriastradh
94303b705cfSriastradhstatic bool gen6_magic_ca_pass(struct sna *sna,
94403b705cfSriastradh			       const struct sna_composite_op *op)
94503b705cfSriastradh{
94603b705cfSriastradh	struct gen6_render_state *state = &sna->render_state.gen6;
94703b705cfSriastradh
94803b705cfSriastradh	if (!op->need_magic_ca_pass)
94903b705cfSriastradh		return false;
95003b705cfSriastradh
95103b705cfSriastradh	DBG(("%s: CA fixup (%d -> %d)\n", __FUNCTION__,
95203b705cfSriastradh	     sna->render.vertex_start, sna->render.vertex_index));
95303b705cfSriastradh
95413496ba1Ssnj	gen6_emit_pipe_stall(sna);
95503b705cfSriastradh
95603b705cfSriastradh	gen6_emit_cc(sna, gen6_get_blend(PictOpAdd, true, op->dst.format));
95703b705cfSriastradh	gen6_emit_wm(sna,
95803b705cfSriastradh		     gen6_choose_composite_kernel(PictOpAdd,
95903b705cfSriastradh						  true, true,
96003b705cfSriastradh						  op->is_affine),
96103b705cfSriastradh		     true);
96203b705cfSriastradh
96303b705cfSriastradh	OUT_BATCH(GEN6_3DPRIMITIVE |
96403b705cfSriastradh		  GEN6_3DPRIMITIVE_VERTEX_SEQUENTIAL |
96503b705cfSriastradh		  _3DPRIM_RECTLIST << GEN6_3DPRIMITIVE_TOPOLOGY_SHIFT |
96603b705cfSriastradh		  0 << 9 |
96703b705cfSriastradh		  4);
96803b705cfSriastradh	OUT_BATCH(sna->render.vertex_index - sna->render.vertex_start);
96903b705cfSriastradh	OUT_BATCH(sna->render.vertex_start);
97003b705cfSriastradh	OUT_BATCH(1);	/* single instance */
97103b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
97203b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
97303b705cfSriastradh
97403b705cfSriastradh	state->last_primitive = sna->kgem.nbatch;
97503b705cfSriastradh	return true;
97603b705cfSriastradh}
97703b705cfSriastradh
97803b705cfSriastradhtypedef struct gen6_surface_state_padded {
97903b705cfSriastradh	struct gen6_surface_state state;
98003b705cfSriastradh	char pad[32 - sizeof(struct gen6_surface_state)];
98103b705cfSriastradh} gen6_surface_state_padded;
98203b705cfSriastradh
98303b705cfSriastradhstatic void null_create(struct sna_static_stream *stream)
98403b705cfSriastradh{
98503b705cfSriastradh	/* A bunch of zeros useful for legacy border color and depth-stencil */
98603b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
98703b705cfSriastradh}
98803b705cfSriastradh
98903b705cfSriastradhstatic void scratch_create(struct sna_static_stream *stream)
99003b705cfSriastradh{
99103b705cfSriastradh	/* 64 bytes of scratch space for random writes, such as
99203b705cfSriastradh	 * the pipe-control w/a.
99303b705cfSriastradh	 */
99403b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
99503b705cfSriastradh}
99603b705cfSriastradh
99703b705cfSriastradhstatic void
99803b705cfSriastradhsampler_state_init(struct gen6_sampler_state *sampler_state,
99903b705cfSriastradh		   sampler_filter_t filter,
100003b705cfSriastradh		   sampler_extend_t extend)
100103b705cfSriastradh{
100203b705cfSriastradh	sampler_state->ss0.lod_preclamp = 1;	/* GL mode */
100303b705cfSriastradh
100403b705cfSriastradh	/* We use the legacy mode to get the semantics specified by
100503b705cfSriastradh	 * the Render extension. */
100603b705cfSriastradh	sampler_state->ss0.border_color_mode = GEN6_BORDER_COLOR_MODE_LEGACY;
100703b705cfSriastradh
100803b705cfSriastradh	switch (filter) {
100903b705cfSriastradh	default:
101003b705cfSriastradh	case SAMPLER_FILTER_NEAREST:
101103b705cfSriastradh		sampler_state->ss0.min_filter = GEN6_MAPFILTER_NEAREST;
101203b705cfSriastradh		sampler_state->ss0.mag_filter = GEN6_MAPFILTER_NEAREST;
101303b705cfSriastradh		break;
101403b705cfSriastradh	case SAMPLER_FILTER_BILINEAR:
101503b705cfSriastradh		sampler_state->ss0.min_filter = GEN6_MAPFILTER_LINEAR;
101603b705cfSriastradh		sampler_state->ss0.mag_filter = GEN6_MAPFILTER_LINEAR;
101703b705cfSriastradh		break;
101803b705cfSriastradh	}
101903b705cfSriastradh
102003b705cfSriastradh	switch (extend) {
102103b705cfSriastradh	default:
102203b705cfSriastradh	case SAMPLER_EXTEND_NONE:
102303b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
102403b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
102503b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
102603b705cfSriastradh		break;
102703b705cfSriastradh	case SAMPLER_EXTEND_REPEAT:
102803b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
102903b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
103003b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
103103b705cfSriastradh		break;
103203b705cfSriastradh	case SAMPLER_EXTEND_PAD:
103303b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
103403b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
103503b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
103603b705cfSriastradh		break;
103703b705cfSriastradh	case SAMPLER_EXTEND_REFLECT:
103803b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
103903b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
104003b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
104103b705cfSriastradh		break;
104203b705cfSriastradh	}
104303b705cfSriastradh}
104403b705cfSriastradh
104503b705cfSriastradhstatic void
104603b705cfSriastradhsampler_copy_init(struct gen6_sampler_state *ss)
104703b705cfSriastradh{
104803b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
104903b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
105003b705cfSriastradh
105103b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
105203b705cfSriastradh}
105303b705cfSriastradh
105403b705cfSriastradhstatic void
105503b705cfSriastradhsampler_fill_init(struct gen6_sampler_state *ss)
105603b705cfSriastradh{
105703b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_REPEAT);
105803b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
105903b705cfSriastradh
106003b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
106103b705cfSriastradh}
106203b705cfSriastradh
106303b705cfSriastradhstatic uint32_t
106403b705cfSriastradhgen6_tiling_bits(uint32_t tiling)
106503b705cfSriastradh{
106603b705cfSriastradh	switch (tiling) {
106703b705cfSriastradh	default: assert(0);
106803b705cfSriastradh	case I915_TILING_NONE: return 0;
106903b705cfSriastradh	case I915_TILING_X: return GEN6_SURFACE_TILED;
107003b705cfSriastradh	case I915_TILING_Y: return GEN6_SURFACE_TILED | GEN6_SURFACE_TILED_Y;
107103b705cfSriastradh	}
107203b705cfSriastradh}
107303b705cfSriastradh
107403b705cfSriastradh/**
107503b705cfSriastradh * Sets up the common fields for a surface state buffer for the given
107603b705cfSriastradh * picture in the given surface state buffer.
107703b705cfSriastradh */
107803b705cfSriastradhstatic int
107903b705cfSriastradhgen6_bind_bo(struct sna *sna,
108003b705cfSriastradh	     struct kgem_bo *bo,
108103b705cfSriastradh	     uint32_t width,
108203b705cfSriastradh	     uint32_t height,
108303b705cfSriastradh	     uint32_t format,
108403b705cfSriastradh	     bool is_dst)
108503b705cfSriastradh{
108603b705cfSriastradh	uint32_t *ss;
108703b705cfSriastradh	uint32_t domains;
108803b705cfSriastradh	uint16_t offset;
108903b705cfSriastradh	uint32_t is_scanout = is_dst && bo->scanout;
109003b705cfSriastradh
109103b705cfSriastradh	/* After the first bind, we manage the cache domains within the batch */
109203b705cfSriastradh	offset = kgem_bo_get_binding(bo, format | is_dst << 30 | is_scanout << 31);
109303b705cfSriastradh	if (offset) {
109403b705cfSriastradh		DBG(("[%x]  bo(handle=%d), format=%d, reuse %s binding\n",
109503b705cfSriastradh		     offset, bo->handle, format,
109603b705cfSriastradh		     is_dst ? "render" : "sampler"));
109742542f5fSchristos		assert(offset >= sna->kgem.surface);
109803b705cfSriastradh		if (is_dst)
109903b705cfSriastradh			kgem_bo_mark_dirty(bo);
110003b705cfSriastradh		return offset * sizeof(uint32_t);
110103b705cfSriastradh	}
110203b705cfSriastradh
110303b705cfSriastradh	offset = sna->kgem.surface -=
110403b705cfSriastradh		sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
110503b705cfSriastradh	ss = sna->kgem.batch + offset;
110603b705cfSriastradh	ss[0] = (GEN6_SURFACE_2D << GEN6_SURFACE_TYPE_SHIFT |
110703b705cfSriastradh		 GEN6_SURFACE_BLEND_ENABLED |
110803b705cfSriastradh		 format << GEN6_SURFACE_FORMAT_SHIFT);
110903b705cfSriastradh	if (is_dst) {
111003b705cfSriastradh		ss[0] |= GEN6_SURFACE_RC_READ_WRITE;
111103b705cfSriastradh		domains = I915_GEM_DOMAIN_RENDER << 16 |I915_GEM_DOMAIN_RENDER;
111203b705cfSriastradh	} else
111303b705cfSriastradh		domains = I915_GEM_DOMAIN_SAMPLER << 16;
111403b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, offset + 1, bo, domains, 0);
111503b705cfSriastradh	ss[2] = ((width - 1)  << GEN6_SURFACE_WIDTH_SHIFT |
111603b705cfSriastradh		 (height - 1) << GEN6_SURFACE_HEIGHT_SHIFT);
111703b705cfSriastradh	assert(bo->pitch <= (1 << 18));
111803b705cfSriastradh	ss[3] = (gen6_tiling_bits(bo->tiling) |
111903b705cfSriastradh		 (bo->pitch - 1) << GEN6_SURFACE_PITCH_SHIFT);
112003b705cfSriastradh	ss[4] = 0;
112142542f5fSchristos	ss[5] = (is_scanout || bo->io) ? 0 : 3 << 16;
112203b705cfSriastradh
112303b705cfSriastradh	kgem_bo_set_binding(bo, format | is_dst << 30 | is_scanout << 31, offset);
112403b705cfSriastradh
112503b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, tiling=%d -> %s\n",
112603b705cfSriastradh	     offset, bo->handle, ss[1],
112703b705cfSriastradh	     format, width, height, bo->pitch, bo->tiling,
112803b705cfSriastradh	     domains & 0xffff ? "render" : "sampler"));
112903b705cfSriastradh
113003b705cfSriastradh	return offset * sizeof(uint32_t);
113103b705cfSriastradh}
113203b705cfSriastradh
113303b705cfSriastradhstatic void gen6_emit_vertex_buffer(struct sna *sna,
113403b705cfSriastradh				    const struct sna_composite_op *op)
113503b705cfSriastradh{
113603b705cfSriastradh	int id = GEN6_VERTEX(op->u.gen6.flags);
113703b705cfSriastradh
113803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VERTEX_BUFFERS | 3);
113903b705cfSriastradh	OUT_BATCH(id << VB0_BUFFER_INDEX_SHIFT | VB0_VERTEXDATA |
114003b705cfSriastradh		  4*op->floats_per_vertex << VB0_BUFFER_PITCH_SHIFT);
114103b705cfSriastradh	sna->render.vertex_reloc[sna->render.nvertex_reloc++] = sna->kgem.nbatch;
114203b705cfSriastradh	OUT_BATCH(0);
114303b705cfSriastradh	OUT_BATCH(~0); /* max address: disabled */
114403b705cfSriastradh	OUT_BATCH(0);
114503b705cfSriastradh
114603b705cfSriastradh	sna->render.vb_id |= 1 << id;
114703b705cfSriastradh}
114803b705cfSriastradh
114903b705cfSriastradhstatic void gen6_emit_primitive(struct sna *sna)
115003b705cfSriastradh{
115103b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen6.last_primitive) {
115203b705cfSriastradh		DBG(("%s: continuing previous primitive, start=%d, index=%d\n",
115303b705cfSriastradh		     __FUNCTION__,
115403b705cfSriastradh		     sna->render.vertex_start,
115503b705cfSriastradh		     sna->render.vertex_index));
115603b705cfSriastradh		sna->render.vertex_offset = sna->kgem.nbatch - 5;
115703b705cfSriastradh		return;
115803b705cfSriastradh	}
115903b705cfSriastradh
116003b705cfSriastradh	OUT_BATCH(GEN6_3DPRIMITIVE |
116103b705cfSriastradh		  GEN6_3DPRIMITIVE_VERTEX_SEQUENTIAL |
116203b705cfSriastradh		  _3DPRIM_RECTLIST << GEN6_3DPRIMITIVE_TOPOLOGY_SHIFT |
116303b705cfSriastradh		  0 << 9 |
116403b705cfSriastradh		  4);
116503b705cfSriastradh	sna->render.vertex_offset = sna->kgem.nbatch;
116603b705cfSriastradh	OUT_BATCH(0);	/* vertex count, to be filled in later */
116703b705cfSriastradh	OUT_BATCH(sna->render.vertex_index);
116803b705cfSriastradh	OUT_BATCH(1);	/* single instance */
116903b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
117003b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
117103b705cfSriastradh	sna->render.vertex_start = sna->render.vertex_index;
117203b705cfSriastradh	DBG(("%s: started new primitive: index=%d\n",
117303b705cfSriastradh	     __FUNCTION__, sna->render.vertex_start));
117403b705cfSriastradh
117503b705cfSriastradh	sna->render_state.gen6.last_primitive = sna->kgem.nbatch;
117603b705cfSriastradh}
117703b705cfSriastradh
117803b705cfSriastradhstatic bool gen6_rectangle_begin(struct sna *sna,
117903b705cfSriastradh				 const struct sna_composite_op *op)
118003b705cfSriastradh{
118103b705cfSriastradh	int id = 1 << GEN6_VERTEX(op->u.gen6.flags);
118203b705cfSriastradh	int ndwords;
118303b705cfSriastradh
118403b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render) && sna->render.vertex_offset)
118503b705cfSriastradh		return true;
118603b705cfSriastradh
118703b705cfSriastradh	ndwords = op->need_magic_ca_pass ? 60 : 6;
118803b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
118903b705cfSriastradh		ndwords += 5;
119003b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, ndwords))
119103b705cfSriastradh		return false;
119203b705cfSriastradh
119303b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
119403b705cfSriastradh		gen6_emit_vertex_buffer(sna, op);
119503b705cfSriastradh
119603b705cfSriastradh	gen6_emit_primitive(sna);
119703b705cfSriastradh	return true;
119803b705cfSriastradh}
119903b705cfSriastradh
120003b705cfSriastradhstatic int gen6_get_rectangles__flush(struct sna *sna,
120103b705cfSriastradh				      const struct sna_composite_op *op)
120203b705cfSriastradh{
120303b705cfSriastradh	/* Preventing discarding new vbo after lock contention */
120403b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render)) {
120503b705cfSriastradh		int rem = vertex_space(sna);
120603b705cfSriastradh		if (rem > op->floats_per_rect)
120703b705cfSriastradh			return rem;
120803b705cfSriastradh	}
120903b705cfSriastradh
121003b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, op->need_magic_ca_pass ? 65 : 5))
121103b705cfSriastradh		return 0;
121203b705cfSriastradh	if (!kgem_check_reloc_and_exec(&sna->kgem, 2))
121303b705cfSriastradh		return 0;
121403b705cfSriastradh
121503b705cfSriastradh	if (sna->render.vertex_offset) {
121603b705cfSriastradh		gen4_vertex_flush(sna);
121703b705cfSriastradh		if (gen6_magic_ca_pass(sna, op)) {
121813496ba1Ssnj			gen6_emit_pipe_stall(sna);
121903b705cfSriastradh			gen6_emit_cc(sna, GEN6_BLEND(op->u.gen6.flags));
122003b705cfSriastradh			gen6_emit_wm(sna,
122103b705cfSriastradh				     GEN6_KERNEL(op->u.gen6.flags),
122203b705cfSriastradh				     GEN6_VERTEX(op->u.gen6.flags) >> 2);
122303b705cfSriastradh		}
122403b705cfSriastradh	}
122503b705cfSriastradh
122603b705cfSriastradh	return gen4_vertex_finish(sna);
122703b705cfSriastradh}
122803b705cfSriastradh
122903b705cfSriastradhinline static int gen6_get_rectangles(struct sna *sna,
123003b705cfSriastradh				      const struct sna_composite_op *op,
123103b705cfSriastradh				      int want,
123203b705cfSriastradh				      void (*emit_state)(struct sna *, const struct sna_composite_op *op))
123303b705cfSriastradh{
123403b705cfSriastradh	int rem;
123503b705cfSriastradh
123603b705cfSriastradh	assert(want);
123703b705cfSriastradh
123803b705cfSriastradhstart:
123903b705cfSriastradh	rem = vertex_space(sna);
124003b705cfSriastradh	if (unlikely(rem < op->floats_per_rect)) {
124103b705cfSriastradh		DBG(("flushing vbo for %s: %d < %d\n",
124203b705cfSriastradh		     __FUNCTION__, rem, op->floats_per_rect));
124303b705cfSriastradh		rem = gen6_get_rectangles__flush(sna, op);
124403b705cfSriastradh		if (unlikely(rem == 0))
124503b705cfSriastradh			goto flush;
124603b705cfSriastradh	}
124703b705cfSriastradh
124803b705cfSriastradh	if (unlikely(sna->render.vertex_offset == 0)) {
124903b705cfSriastradh		if (!gen6_rectangle_begin(sna, op))
125003b705cfSriastradh			goto flush;
125103b705cfSriastradh		else
125203b705cfSriastradh			goto start;
125303b705cfSriastradh	}
125403b705cfSriastradh
125503b705cfSriastradh	assert(rem <= vertex_space(sna));
125603b705cfSriastradh	assert(op->floats_per_rect <= rem);
125703b705cfSriastradh	if (want > 1 && want * op->floats_per_rect > rem)
125803b705cfSriastradh		want = rem / op->floats_per_rect;
125903b705cfSriastradh
126003b705cfSriastradh	assert(want > 0);
126103b705cfSriastradh	sna->render.vertex_index += 3*want;
126203b705cfSriastradh	return want;
126303b705cfSriastradh
126403b705cfSriastradhflush:
126503b705cfSriastradh	if (sna->render.vertex_offset) {
126603b705cfSriastradh		gen4_vertex_flush(sna);
126703b705cfSriastradh		gen6_magic_ca_pass(sna, op);
126803b705cfSriastradh	}
126903b705cfSriastradh	sna_vertex_wait__locked(&sna->render);
127003b705cfSriastradh	_kgem_submit(&sna->kgem);
127103b705cfSriastradh	emit_state(sna, op);
127203b705cfSriastradh	goto start;
127303b705cfSriastradh}
127403b705cfSriastradh
127503b705cfSriastradhinline static uint32_t *gen6_composite_get_binding_table(struct sna *sna,
127603b705cfSriastradh							 uint16_t *offset)
127703b705cfSriastradh{
127803b705cfSriastradh	uint32_t *table;
127903b705cfSriastradh
128003b705cfSriastradh	sna->kgem.surface -=
128103b705cfSriastradh		sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
128203b705cfSriastradh	/* Clear all surplus entries to zero in case of prefetch */
128303b705cfSriastradh	table = memset(sna->kgem.batch + sna->kgem.surface,
128403b705cfSriastradh		       0, sizeof(struct gen6_surface_state_padded));
128503b705cfSriastradh
128603b705cfSriastradh	DBG(("%s(%x)\n", __FUNCTION__, 4*sna->kgem.surface));
128703b705cfSriastradh
128803b705cfSriastradh	*offset = sna->kgem.surface;
128903b705cfSriastradh	return table;
129003b705cfSriastradh}
129103b705cfSriastradh
129203b705cfSriastradhstatic bool
129303b705cfSriastradhgen6_get_batch(struct sna *sna, const struct sna_composite_op *op)
129403b705cfSriastradh{
129503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, op->dst.bo);
129603b705cfSriastradh
129703b705cfSriastradh	if (!kgem_check_batch_with_surfaces(&sna->kgem, 150, 4)) {
129803b705cfSriastradh		DBG(("%s: flushing batch: %d < %d+%d\n",
129903b705cfSriastradh		     __FUNCTION__, sna->kgem.surface - sna->kgem.nbatch,
130003b705cfSriastradh		     150, 4*8));
130103b705cfSriastradh		kgem_submit(&sna->kgem);
130203b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
130303b705cfSriastradh	}
130403b705cfSriastradh
130503b705cfSriastradh	if (sna->render_state.gen6.needs_invariant)
130603b705cfSriastradh		gen6_emit_invariant(sna);
130703b705cfSriastradh
130803b705cfSriastradh	return kgem_bo_is_dirty(op->dst.bo);
130903b705cfSriastradh}
131003b705cfSriastradh
131103b705cfSriastradhstatic void gen6_emit_composite_state(struct sna *sna,
131203b705cfSriastradh				      const struct sna_composite_op *op)
131303b705cfSriastradh{
131403b705cfSriastradh	uint32_t *binding_table;
131503b705cfSriastradh	uint16_t offset;
131603b705cfSriastradh	bool dirty;
131703b705cfSriastradh
131803b705cfSriastradh	dirty = gen6_get_batch(sna, op);
131903b705cfSriastradh
132003b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
132103b705cfSriastradh
132203b705cfSriastradh	binding_table[0] =
132303b705cfSriastradh		gen6_bind_bo(sna,
132403b705cfSriastradh			    op->dst.bo, op->dst.width, op->dst.height,
132503b705cfSriastradh			    gen6_get_dest_format(op->dst.format),
132603b705cfSriastradh			    true);
132703b705cfSriastradh	binding_table[1] =
132803b705cfSriastradh		gen6_bind_bo(sna,
132903b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
133003b705cfSriastradh			     op->src.card_format,
133103b705cfSriastradh			     false);
133203b705cfSriastradh	if (op->mask.bo) {
133303b705cfSriastradh		binding_table[2] =
133403b705cfSriastradh			gen6_bind_bo(sna,
133503b705cfSriastradh				     op->mask.bo,
133603b705cfSriastradh				     op->mask.width,
133703b705cfSriastradh				     op->mask.height,
133803b705cfSriastradh				     op->mask.card_format,
133903b705cfSriastradh				     false);
134003b705cfSriastradh	}
134103b705cfSriastradh
134203b705cfSriastradh	if (sna->kgem.surface == offset &&
134303b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table &&
134403b705cfSriastradh	    (op->mask.bo == NULL ||
134503b705cfSriastradh	     sna->kgem.batch[sna->render_state.gen6.surface_table+2] == binding_table[2])) {
134603b705cfSriastradh		sna->kgem.surface += sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
134703b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
134803b705cfSriastradh	}
134903b705cfSriastradh
135003b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
135103b705cfSriastradh}
135203b705cfSriastradh
135303b705cfSriastradhstatic void
135403b705cfSriastradhgen6_align_vertex(struct sna *sna, const struct sna_composite_op *op)
135503b705cfSriastradh{
135603b705cfSriastradh	assert (sna->render.vertex_offset == 0);
135703b705cfSriastradh	if (op->floats_per_vertex != sna->render_state.gen6.floats_per_vertex) {
135842542f5fSchristos		DBG(("aligning vertex: was %d, now %d floats per vertex\n",
135903b705cfSriastradh		     sna->render_state.gen6.floats_per_vertex,
136042542f5fSchristos		     op->floats_per_vertex));
136142542f5fSchristos		gen4_vertex_align(sna, op);
136203b705cfSriastradh		sna->render_state.gen6.floats_per_vertex = op->floats_per_vertex;
136303b705cfSriastradh	}
136403b705cfSriastradh	assert((sna->render.vertex_used % op->floats_per_vertex) == 0);
136503b705cfSriastradh}
136603b705cfSriastradh
136703b705cfSriastradhfastcall static void
136803b705cfSriastradhgen6_render_composite_blt(struct sna *sna,
136903b705cfSriastradh			  const struct sna_composite_op *op,
137003b705cfSriastradh			  const struct sna_composite_rectangles *r)
137103b705cfSriastradh{
137203b705cfSriastradh	gen6_get_rectangles(sna, op, 1, gen6_emit_composite_state);
137303b705cfSriastradh	op->prim_emit(sna, op, r);
137403b705cfSriastradh}
137503b705cfSriastradh
137603b705cfSriastradhfastcall static void
137703b705cfSriastradhgen6_render_composite_box(struct sna *sna,
137803b705cfSriastradh			  const struct sna_composite_op *op,
137903b705cfSriastradh			  const BoxRec *box)
138003b705cfSriastradh{
138103b705cfSriastradh	struct sna_composite_rectangles r;
138203b705cfSriastradh
138303b705cfSriastradh	gen6_get_rectangles(sna, op, 1, gen6_emit_composite_state);
138403b705cfSriastradh
138503b705cfSriastradh	DBG(("  %s: (%d, %d), (%d, %d)\n",
138603b705cfSriastradh	     __FUNCTION__,
138703b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
138803b705cfSriastradh
138903b705cfSriastradh	r.dst.x = box->x1;
139003b705cfSriastradh	r.dst.y = box->y1;
139103b705cfSriastradh	r.width  = box->x2 - box->x1;
139203b705cfSriastradh	r.height = box->y2 - box->y1;
139303b705cfSriastradh	r.src = r.mask = r.dst;
139403b705cfSriastradh
139503b705cfSriastradh	op->prim_emit(sna, op, &r);
139603b705cfSriastradh}
139703b705cfSriastradh
139803b705cfSriastradhstatic void
139903b705cfSriastradhgen6_render_composite_boxes__blt(struct sna *sna,
140003b705cfSriastradh				 const struct sna_composite_op *op,
140103b705cfSriastradh				 const BoxRec *box, int nbox)
140203b705cfSriastradh{
140303b705cfSriastradh	DBG(("composite_boxes(%d)\n", nbox));
140403b705cfSriastradh
140503b705cfSriastradh	do {
140603b705cfSriastradh		int nbox_this_time;
140703b705cfSriastradh
140803b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
140903b705cfSriastradh						     gen6_emit_composite_state);
141003b705cfSriastradh		nbox -= nbox_this_time;
141103b705cfSriastradh
141203b705cfSriastradh		do {
141303b705cfSriastradh			struct sna_composite_rectangles r;
141403b705cfSriastradh
141503b705cfSriastradh			DBG(("  %s: (%d, %d), (%d, %d)\n",
141603b705cfSriastradh			     __FUNCTION__,
141703b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
141803b705cfSriastradh
141903b705cfSriastradh			r.dst.x = box->x1;
142003b705cfSriastradh			r.dst.y = box->y1;
142103b705cfSriastradh			r.width  = box->x2 - box->x1;
142203b705cfSriastradh			r.height = box->y2 - box->y1;
142303b705cfSriastradh			r.src = r.mask = r.dst;
142403b705cfSriastradh
142503b705cfSriastradh			op->prim_emit(sna, op, &r);
142603b705cfSriastradh			box++;
142703b705cfSriastradh		} while (--nbox_this_time);
142803b705cfSriastradh	} while (nbox);
142903b705cfSriastradh}
143003b705cfSriastradh
143103b705cfSriastradhstatic void
143203b705cfSriastradhgen6_render_composite_boxes(struct sna *sna,
143303b705cfSriastradh			    const struct sna_composite_op *op,
143403b705cfSriastradh			    const BoxRec *box, int nbox)
143503b705cfSriastradh{
143603b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
143703b705cfSriastradh
143803b705cfSriastradh	do {
143903b705cfSriastradh		int nbox_this_time;
144003b705cfSriastradh		float *v;
144103b705cfSriastradh
144203b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
144303b705cfSriastradh						     gen6_emit_composite_state);
144403b705cfSriastradh		assert(nbox_this_time);
144503b705cfSriastradh		nbox -= nbox_this_time;
144603b705cfSriastradh
144703b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
144803b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
144903b705cfSriastradh
145003b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
145103b705cfSriastradh		box += nbox_this_time;
145203b705cfSriastradh	} while (nbox);
145303b705cfSriastradh}
145403b705cfSriastradh
145503b705cfSriastradhstatic void
145603b705cfSriastradhgen6_render_composite_boxes__thread(struct sna *sna,
145703b705cfSriastradh				    const struct sna_composite_op *op,
145803b705cfSriastradh				    const BoxRec *box, int nbox)
145903b705cfSriastradh{
146003b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
146103b705cfSriastradh
146203b705cfSriastradh	sna_vertex_lock(&sna->render);
146303b705cfSriastradh	do {
146403b705cfSriastradh		int nbox_this_time;
146503b705cfSriastradh		float *v;
146603b705cfSriastradh
146703b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
146803b705cfSriastradh						     gen6_emit_composite_state);
146903b705cfSriastradh		assert(nbox_this_time);
147003b705cfSriastradh		nbox -= nbox_this_time;
147103b705cfSriastradh
147203b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
147303b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
147403b705cfSriastradh
147503b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
147603b705cfSriastradh		sna_vertex_unlock(&sna->render);
147703b705cfSriastradh
147803b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
147903b705cfSriastradh		box += nbox_this_time;
148003b705cfSriastradh
148103b705cfSriastradh		sna_vertex_lock(&sna->render);
148203b705cfSriastradh		sna_vertex_release__locked(&sna->render);
148303b705cfSriastradh	} while (nbox);
148403b705cfSriastradh	sna_vertex_unlock(&sna->render);
148503b705cfSriastradh}
148603b705cfSriastradh
148703b705cfSriastradh#ifndef MAX
148803b705cfSriastradh#define MAX(a,b) ((a) > (b) ? (a) : (b))
148903b705cfSriastradh#endif
149003b705cfSriastradh
149103b705cfSriastradhstatic uint32_t
149203b705cfSriastradhgen6_composite_create_blend_state(struct sna_static_stream *stream)
149303b705cfSriastradh{
149403b705cfSriastradh	char *base, *ptr;
149503b705cfSriastradh	int src, dst;
149603b705cfSriastradh
149703b705cfSriastradh	base = sna_static_stream_map(stream,
149803b705cfSriastradh				     GEN6_BLENDFACTOR_COUNT * GEN6_BLENDFACTOR_COUNT * GEN6_BLEND_STATE_PADDED_SIZE,
149903b705cfSriastradh				     64);
150003b705cfSriastradh
150103b705cfSriastradh	ptr = base;
150203b705cfSriastradh	for (src = 0; src < GEN6_BLENDFACTOR_COUNT; src++) {
150303b705cfSriastradh		for (dst= 0; dst < GEN6_BLENDFACTOR_COUNT; dst++) {
150403b705cfSriastradh			struct gen6_blend_state *blend =
150503b705cfSriastradh				(struct gen6_blend_state *)ptr;
150603b705cfSriastradh
150703b705cfSriastradh			blend->blend0.dest_blend_factor = dst;
150803b705cfSriastradh			blend->blend0.source_blend_factor = src;
150903b705cfSriastradh			blend->blend0.blend_func = GEN6_BLENDFUNCTION_ADD;
151003b705cfSriastradh			blend->blend0.blend_enable =
151103b705cfSriastradh				!(dst == GEN6_BLENDFACTOR_ZERO && src == GEN6_BLENDFACTOR_ONE);
151203b705cfSriastradh
151303b705cfSriastradh			blend->blend1.post_blend_clamp_enable = 1;
151403b705cfSriastradh			blend->blend1.pre_blend_clamp_enable = 1;
151503b705cfSriastradh
151603b705cfSriastradh			ptr += GEN6_BLEND_STATE_PADDED_SIZE;
151703b705cfSriastradh		}
151803b705cfSriastradh	}
151903b705cfSriastradh
152003b705cfSriastradh	return sna_static_stream_offsetof(stream, base);
152103b705cfSriastradh}
152203b705cfSriastradh
152303b705cfSriastradhstatic uint32_t gen6_bind_video_source(struct sna *sna,
152403b705cfSriastradh				       struct kgem_bo *src_bo,
152503b705cfSriastradh				       uint32_t src_offset,
152603b705cfSriastradh				       int src_width,
152703b705cfSriastradh				       int src_height,
152803b705cfSriastradh				       int src_pitch,
152903b705cfSriastradh				       uint32_t src_surf_format)
153003b705cfSriastradh{
153103b705cfSriastradh	struct gen6_surface_state *ss;
153203b705cfSriastradh
153303b705cfSriastradh	sna->kgem.surface -= sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
153403b705cfSriastradh
153503b705cfSriastradh	ss = memset(sna->kgem.batch + sna->kgem.surface, 0, sizeof(*ss));
153603b705cfSriastradh	ss->ss0.surface_type = GEN6_SURFACE_2D;
153703b705cfSriastradh	ss->ss0.surface_format = src_surf_format;
153803b705cfSriastradh
153903b705cfSriastradh	ss->ss1.base_addr =
154003b705cfSriastradh		kgem_add_reloc(&sna->kgem,
154103b705cfSriastradh			       sna->kgem.surface + 1,
154203b705cfSriastradh			       src_bo,
154303b705cfSriastradh			       I915_GEM_DOMAIN_SAMPLER << 16,
154403b705cfSriastradh			       src_offset);
154503b705cfSriastradh
154603b705cfSriastradh	ss->ss2.width  = src_width - 1;
154703b705cfSriastradh	ss->ss2.height = src_height - 1;
154803b705cfSriastradh	ss->ss3.pitch  = src_pitch - 1;
154903b705cfSriastradh
155003b705cfSriastradh	return sna->kgem.surface * sizeof(uint32_t);
155103b705cfSriastradh}
155203b705cfSriastradh
155303b705cfSriastradhstatic void gen6_emit_video_state(struct sna *sna,
155403b705cfSriastradh				  const struct sna_composite_op *op)
155503b705cfSriastradh{
155603b705cfSriastradh	struct sna_video_frame *frame = op->priv;
155703b705cfSriastradh	uint32_t src_surf_format;
155803b705cfSriastradh	uint32_t src_surf_base[6];
155903b705cfSriastradh	int src_width[6];
156003b705cfSriastradh	int src_height[6];
156103b705cfSriastradh	int src_pitch[6];
156203b705cfSriastradh	uint32_t *binding_table;
156303b705cfSriastradh	uint16_t offset;
156403b705cfSriastradh	bool dirty;
156503b705cfSriastradh	int n_src, n;
156603b705cfSriastradh
156703b705cfSriastradh	dirty = gen6_get_batch(sna, op);
156803b705cfSriastradh
156903b705cfSriastradh	src_surf_base[0] = 0;
157003b705cfSriastradh	src_surf_base[1] = 0;
157103b705cfSriastradh	src_surf_base[2] = frame->VBufOffset;
157203b705cfSriastradh	src_surf_base[3] = frame->VBufOffset;
157303b705cfSriastradh	src_surf_base[4] = frame->UBufOffset;
157403b705cfSriastradh	src_surf_base[5] = frame->UBufOffset;
157503b705cfSriastradh
157603b705cfSriastradh	if (is_planar_fourcc(frame->id)) {
157703b705cfSriastradh		src_surf_format = GEN6_SURFACEFORMAT_R8_UNORM;
157803b705cfSriastradh		src_width[1]  = src_width[0]  = frame->width;
157903b705cfSriastradh		src_height[1] = src_height[0] = frame->height;
158003b705cfSriastradh		src_pitch[1]  = src_pitch[0]  = frame->pitch[1];
158103b705cfSriastradh		src_width[4]  = src_width[5]  = src_width[2]  = src_width[3] =
158203b705cfSriastradh			frame->width / 2;
158303b705cfSriastradh		src_height[4] = src_height[5] = src_height[2] = src_height[3] =
158403b705cfSriastradh			frame->height / 2;
158503b705cfSriastradh		src_pitch[4]  = src_pitch[5]  = src_pitch[2]  = src_pitch[3] =
158603b705cfSriastradh			frame->pitch[0];
158703b705cfSriastradh		n_src = 6;
158803b705cfSriastradh	} else {
158903b705cfSriastradh		if (frame->id == FOURCC_UYVY)
159003b705cfSriastradh			src_surf_format = GEN6_SURFACEFORMAT_YCRCB_SWAPY;
159103b705cfSriastradh		else
159203b705cfSriastradh			src_surf_format = GEN6_SURFACEFORMAT_YCRCB_NORMAL;
159303b705cfSriastradh
159403b705cfSriastradh		src_width[0]  = frame->width;
159503b705cfSriastradh		src_height[0] = frame->height;
159603b705cfSriastradh		src_pitch[0]  = frame->pitch[0];
159703b705cfSriastradh		n_src = 1;
159803b705cfSriastradh	}
159903b705cfSriastradh
160003b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
160103b705cfSriastradh
160203b705cfSriastradh	binding_table[0] =
160303b705cfSriastradh		gen6_bind_bo(sna,
160403b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
160503b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
160603b705cfSriastradh			     true);
160703b705cfSriastradh	for (n = 0; n < n_src; n++) {
160803b705cfSriastradh		binding_table[1+n] =
160903b705cfSriastradh			gen6_bind_video_source(sna,
161003b705cfSriastradh					       frame->bo,
161103b705cfSriastradh					       src_surf_base[n],
161203b705cfSriastradh					       src_width[n],
161303b705cfSriastradh					       src_height[n],
161403b705cfSriastradh					       src_pitch[n],
161503b705cfSriastradh					       src_surf_format);
161603b705cfSriastradh	}
161703b705cfSriastradh
161803b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
161903b705cfSriastradh}
162003b705cfSriastradh
162103b705cfSriastradhstatic bool
162203b705cfSriastradhgen6_render_video(struct sna *sna,
162303b705cfSriastradh		  struct sna_video *video,
162403b705cfSriastradh		  struct sna_video_frame *frame,
162503b705cfSriastradh		  RegionPtr dstRegion,
162603b705cfSriastradh		  PixmapPtr pixmap)
162703b705cfSriastradh{
162803b705cfSriastradh	struct sna_composite_op tmp;
162942542f5fSchristos	struct sna_pixmap *priv = sna_pixmap(pixmap);
163003b705cfSriastradh	int dst_width = dstRegion->extents.x2 - dstRegion->extents.x1;
163103b705cfSriastradh	int dst_height = dstRegion->extents.y2 - dstRegion->extents.y1;
163203b705cfSriastradh	int src_width = frame->src.x2 - frame->src.x1;
163303b705cfSriastradh	int src_height = frame->src.y2 - frame->src.y1;
163403b705cfSriastradh	float src_offset_x, src_offset_y;
163503b705cfSriastradh	float src_scale_x, src_scale_y;
163603b705cfSriastradh	int nbox, pix_xoff, pix_yoff;
163703b705cfSriastradh	unsigned filter;
163842542f5fSchristos	const BoxRec *box;
163903b705cfSriastradh
164042542f5fSchristos	DBG(("%s: src=(%d, %d), dst=(%d, %d), %dx[(%d, %d), (%d, %d)...]\n",
164103b705cfSriastradh	     __FUNCTION__,
164203b705cfSriastradh	     src_width, src_height, dst_width, dst_height,
164342542f5fSchristos	     region_num_rects(dstRegion),
164403b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x1,
164503b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y1,
164603b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x2,
164703b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y2));
164803b705cfSriastradh
164942542f5fSchristos	assert(priv->gpu_bo);
165003b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
165103b705cfSriastradh
165203b705cfSriastradh	tmp.dst.pixmap = pixmap;
165303b705cfSriastradh	tmp.dst.width  = pixmap->drawable.width;
165403b705cfSriastradh	tmp.dst.height = pixmap->drawable.height;
165503b705cfSriastradh	tmp.dst.format = sna_render_format_for_depth(pixmap->drawable.depth);
165603b705cfSriastradh	tmp.dst.bo = priv->gpu_bo;
165703b705cfSriastradh
165803b705cfSriastradh	tmp.src.bo = frame->bo;
165903b705cfSriastradh	tmp.mask.bo = NULL;
166003b705cfSriastradh
166103b705cfSriastradh	tmp.floats_per_vertex = 3;
166203b705cfSriastradh	tmp.floats_per_rect = 9;
166303b705cfSriastradh
166403b705cfSriastradh	if (src_width == dst_width && src_height == dst_height)
166503b705cfSriastradh		filter = SAMPLER_FILTER_NEAREST;
166603b705cfSriastradh	else
166703b705cfSriastradh		filter = SAMPLER_FILTER_BILINEAR;
166803b705cfSriastradh
166903b705cfSriastradh	tmp.u.gen6.flags =
167003b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(filter, SAMPLER_EXTEND_PAD,
167103b705cfSriastradh					       SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE),
167203b705cfSriastradh			       NO_BLEND,
167303b705cfSriastradh			       is_planar_fourcc(frame->id) ?
167403b705cfSriastradh			       GEN6_WM_KERNEL_VIDEO_PLANAR :
167503b705cfSriastradh			       GEN6_WM_KERNEL_VIDEO_PACKED,
167603b705cfSriastradh			       2);
167703b705cfSriastradh	tmp.priv = frame;
167803b705cfSriastradh
167903b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
168003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL)) {
168103b705cfSriastradh		kgem_submit(&sna->kgem);
168203b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL));
168303b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
168403b705cfSriastradh	}
168503b705cfSriastradh
168603b705cfSriastradh	gen6_align_vertex(sna, &tmp);
168742542f5fSchristos	gen6_emit_video_state(sna, &tmp);
168803b705cfSriastradh
168903b705cfSriastradh	/* Set up the offset for translating from the given region (in screen
169003b705cfSriastradh	 * coordinates) to the backing pixmap.
169103b705cfSriastradh	 */
169203b705cfSriastradh#ifdef COMPOSITE
169303b705cfSriastradh	pix_xoff = -pixmap->screen_x + pixmap->drawable.x;
169403b705cfSriastradh	pix_yoff = -pixmap->screen_y + pixmap->drawable.y;
169503b705cfSriastradh#else
169603b705cfSriastradh	pix_xoff = 0;
169703b705cfSriastradh	pix_yoff = 0;
169803b705cfSriastradh#endif
169903b705cfSriastradh
170003b705cfSriastradh	src_scale_x = (float)src_width / dst_width / frame->width;
170103b705cfSriastradh	src_offset_x = (float)frame->src.x1 / frame->width - dstRegion->extents.x1 * src_scale_x;
170203b705cfSriastradh
170303b705cfSriastradh	src_scale_y = (float)src_height / dst_height / frame->height;
170403b705cfSriastradh	src_offset_y = (float)frame->src.y1 / frame->height - dstRegion->extents.y1 * src_scale_y;
170503b705cfSriastradh
170642542f5fSchristos	box = region_rects(dstRegion);
170742542f5fSchristos	nbox = region_num_rects(dstRegion);
170803b705cfSriastradh	while (nbox--) {
170903b705cfSriastradh		BoxRec r;
171003b705cfSriastradh
171103b705cfSriastradh		r.x1 = box->x1 + pix_xoff;
171203b705cfSriastradh		r.x2 = box->x2 + pix_xoff;
171303b705cfSriastradh		r.y1 = box->y1 + pix_yoff;
171403b705cfSriastradh		r.y2 = box->y2 + pix_yoff;
171503b705cfSriastradh
171603b705cfSriastradh		gen6_get_rectangles(sna, &tmp, 1, gen6_emit_video_state);
171703b705cfSriastradh
171803b705cfSriastradh		OUT_VERTEX(r.x2, r.y2);
171903b705cfSriastradh		OUT_VERTEX_F(box->x2 * src_scale_x + src_offset_x);
172003b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
172103b705cfSriastradh
172203b705cfSriastradh		OUT_VERTEX(r.x1, r.y2);
172303b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
172403b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
172503b705cfSriastradh
172603b705cfSriastradh		OUT_VERTEX(r.x1, r.y1);
172703b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
172803b705cfSriastradh		OUT_VERTEX_F(box->y1 * src_scale_y + src_offset_y);
172903b705cfSriastradh
173003b705cfSriastradh		if (!DAMAGE_IS_ALL(priv->gpu_damage)) {
173103b705cfSriastradh			sna_damage_add_box(&priv->gpu_damage, &r);
173203b705cfSriastradh			sna_damage_subtract_box(&priv->cpu_damage, &r);
173303b705cfSriastradh		}
173403b705cfSriastradh		box++;
173503b705cfSriastradh	}
173603b705cfSriastradh
173703b705cfSriastradh	gen4_vertex_flush(sna);
173803b705cfSriastradh	return true;
173903b705cfSriastradh}
174003b705cfSriastradh
174103b705cfSriastradhstatic int
174203b705cfSriastradhgen6_composite_picture(struct sna *sna,
174303b705cfSriastradh		       PicturePtr picture,
174403b705cfSriastradh		       struct sna_composite_channel *channel,
174503b705cfSriastradh		       int x, int y,
174603b705cfSriastradh		       int w, int h,
174703b705cfSriastradh		       int dst_x, int dst_y,
174803b705cfSriastradh		       bool precise)
174903b705cfSriastradh{
175003b705cfSriastradh	PixmapPtr pixmap;
175103b705cfSriastradh	uint32_t color;
175203b705cfSriastradh	int16_t dx, dy;
175303b705cfSriastradh
175442542f5fSchristos	DBG(("%s: (%d, %d)x(%d, %d), dst=(%d, %d), precise=%d\n",
175542542f5fSchristos	     __FUNCTION__, x, y, w, h, dst_x, dst_y, precise));
175603b705cfSriastradh
175703b705cfSriastradh	channel->is_solid = false;
175803b705cfSriastradh	channel->card_format = -1;
175903b705cfSriastradh
176003b705cfSriastradh	if (sna_picture_is_solid(picture, &color))
176103b705cfSriastradh		return gen4_channel_init_solid(sna, channel, color);
176203b705cfSriastradh
176303b705cfSriastradh	if (picture->pDrawable == NULL) {
176403b705cfSriastradh		int ret;
176503b705cfSriastradh
176603b705cfSriastradh		if (picture->pSourcePict->type == SourcePictTypeLinear)
176703b705cfSriastradh			return gen4_channel_init_linear(sna, picture, channel,
176803b705cfSriastradh							x, y,
176903b705cfSriastradh							w, h,
177003b705cfSriastradh							dst_x, dst_y);
177103b705cfSriastradh
177203b705cfSriastradh		DBG(("%s -- fixup, gradient\n", __FUNCTION__));
177303b705cfSriastradh		ret = -1;
177403b705cfSriastradh		if (!precise)
177503b705cfSriastradh			ret = sna_render_picture_approximate_gradient(sna, picture, channel,
177603b705cfSriastradh								      x, y, w, h, dst_x, dst_y);
177703b705cfSriastradh		if (ret == -1)
177803b705cfSriastradh			ret = sna_render_picture_fixup(sna, picture, channel,
177903b705cfSriastradh						       x, y, w, h, dst_x, dst_y);
178003b705cfSriastradh		return ret;
178103b705cfSriastradh	}
178203b705cfSriastradh
178303b705cfSriastradh	if (picture->alphaMap) {
178403b705cfSriastradh		DBG(("%s -- fixup, alphamap\n", __FUNCTION__));
178503b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
178603b705cfSriastradh						x, y, w, h, dst_x, dst_y);
178703b705cfSriastradh	}
178803b705cfSriastradh
178903b705cfSriastradh	if (!gen6_check_repeat(picture))
179003b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
179103b705cfSriastradh						x, y, w, h, dst_x, dst_y);
179203b705cfSriastradh
179303b705cfSriastradh	if (!gen6_check_filter(picture))
179403b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
179503b705cfSriastradh						x, y, w, h, dst_x, dst_y);
179603b705cfSriastradh
179703b705cfSriastradh	channel->repeat = picture->repeat ? picture->repeatType : RepeatNone;
179803b705cfSriastradh	channel->filter = picture->filter;
179903b705cfSriastradh
180003b705cfSriastradh	pixmap = get_drawable_pixmap(picture->pDrawable);
180103b705cfSriastradh	get_drawable_deltas(picture->pDrawable, pixmap, &dx, &dy);
180203b705cfSriastradh
180303b705cfSriastradh	x += dx + picture->pDrawable->x;
180403b705cfSriastradh	y += dy + picture->pDrawable->y;
180503b705cfSriastradh
180603b705cfSriastradh	channel->is_affine = sna_transform_is_affine(picture->transform);
180742542f5fSchristos	if (sna_transform_is_imprecise_integer_translation(picture->transform, picture->filter, precise, &dx, &dy)) {
180803b705cfSriastradh		DBG(("%s: integer translation (%d, %d), removing\n",
180903b705cfSriastradh		     __FUNCTION__, dx, dy));
181003b705cfSriastradh		x += dx;
181103b705cfSriastradh		y += dy;
181203b705cfSriastradh		channel->transform = NULL;
181303b705cfSriastradh		channel->filter = PictFilterNearest;
181442542f5fSchristos
181542542f5fSchristos		if (channel->repeat &&
181642542f5fSchristos		    (x >= 0 &&
181742542f5fSchristos		     y >= 0 &&
181842542f5fSchristos		     x + w < pixmap->drawable.width &&
181942542f5fSchristos		     y + h < pixmap->drawable.height)) {
182042542f5fSchristos			struct sna_pixmap *priv = sna_pixmap(pixmap);
182142542f5fSchristos			if (priv && priv->clear) {
182242542f5fSchristos				DBG(("%s: converting large pixmap source into solid [%08x]\n", __FUNCTION__, priv->clear_color));
182342542f5fSchristos				return gen4_channel_init_solid(sna, channel, priv->clear_color);
182442542f5fSchristos			}
182542542f5fSchristos		}
182603b705cfSriastradh	} else
182703b705cfSriastradh		channel->transform = picture->transform;
182803b705cfSriastradh
182903b705cfSriastradh	channel->pict_format = picture->format;
183003b705cfSriastradh	channel->card_format = gen6_get_card_format(picture->format);
183103b705cfSriastradh	if (channel->card_format == (unsigned)-1)
183203b705cfSriastradh		return sna_render_picture_convert(sna, picture, channel, pixmap,
183303b705cfSriastradh						  x, y, w, h, dst_x, dst_y,
183403b705cfSriastradh						  false);
183503b705cfSriastradh
183603b705cfSriastradh	if (too_large(pixmap->drawable.width, pixmap->drawable.height)) {
183703b705cfSriastradh		DBG(("%s: extracting from pixmap %dx%d\n", __FUNCTION__,
183803b705cfSriastradh		     pixmap->drawable.width, pixmap->drawable.height));
183903b705cfSriastradh		return sna_render_picture_extract(sna, picture, channel,
184003b705cfSriastradh						  x, y, w, h, dst_x, dst_y);
184103b705cfSriastradh	}
184203b705cfSriastradh
184342542f5fSchristos	DBG(("%s: pixmap, repeat=%d, filter=%d, transform?=%d [affine? %d], format=%08x\n",
184442542f5fSchristos	     __FUNCTION__,
184542542f5fSchristos	     channel->repeat, channel->filter,
184642542f5fSchristos	     channel->transform != NULL, channel->is_affine,
184742542f5fSchristos	     channel->pict_format));
184842542f5fSchristos	if (channel->transform) {
184942542f5fSchristos#define f2d(x) (((double)(x))/65536.)
185042542f5fSchristos		DBG(("%s: transform=[%f %f %f, %f %f %f, %f %f %f] (raw [%x %x %x, %x %x %x, %x %x %x])\n",
185142542f5fSchristos		     __FUNCTION__,
185242542f5fSchristos		     f2d(channel->transform->matrix[0][0]),
185342542f5fSchristos		     f2d(channel->transform->matrix[0][1]),
185442542f5fSchristos		     f2d(channel->transform->matrix[0][2]),
185542542f5fSchristos		     f2d(channel->transform->matrix[1][0]),
185642542f5fSchristos		     f2d(channel->transform->matrix[1][1]),
185742542f5fSchristos		     f2d(channel->transform->matrix[1][2]),
185842542f5fSchristos		     f2d(channel->transform->matrix[2][0]),
185942542f5fSchristos		     f2d(channel->transform->matrix[2][1]),
186042542f5fSchristos		     f2d(channel->transform->matrix[2][2]),
186142542f5fSchristos		     channel->transform->matrix[0][0],
186242542f5fSchristos		     channel->transform->matrix[0][1],
186342542f5fSchristos		     channel->transform->matrix[0][2],
186442542f5fSchristos		     channel->transform->matrix[1][0],
186542542f5fSchristos		     channel->transform->matrix[1][1],
186642542f5fSchristos		     channel->transform->matrix[1][2],
186742542f5fSchristos		     channel->transform->matrix[2][0],
186842542f5fSchristos		     channel->transform->matrix[2][1],
186942542f5fSchristos		     channel->transform->matrix[2][2]));
187042542f5fSchristos#undef f2d
187142542f5fSchristos	}
187242542f5fSchristos
187303b705cfSriastradh	return sna_render_pixmap_bo(sna, channel, pixmap,
187403b705cfSriastradh				    x, y, w, h, dst_x, dst_y);
187503b705cfSriastradh}
187603b705cfSriastradh
187703b705cfSriastradhinline static void gen6_composite_channel_convert(struct sna_composite_channel *channel)
187803b705cfSriastradh{
187903b705cfSriastradh	channel->repeat = gen6_repeat(channel->repeat);
188003b705cfSriastradh	channel->filter = gen6_filter(channel->filter);
188103b705cfSriastradh	if (channel->card_format == (unsigned)-1)
188203b705cfSriastradh		channel->card_format = gen6_get_card_format(channel->pict_format);
188303b705cfSriastradh	assert(channel->card_format != (unsigned)-1);
188403b705cfSriastradh}
188503b705cfSriastradh
188603b705cfSriastradhstatic void gen6_render_composite_done(struct sna *sna,
188703b705cfSriastradh				       const struct sna_composite_op *op)
188803b705cfSriastradh{
188903b705cfSriastradh	DBG(("%s\n", __FUNCTION__));
189003b705cfSriastradh
189103b705cfSriastradh	assert(!sna->render.active);
189203b705cfSriastradh	if (sna->render.vertex_offset) {
189303b705cfSriastradh		gen4_vertex_flush(sna);
189403b705cfSriastradh		gen6_magic_ca_pass(sna, op);
189503b705cfSriastradh	}
189603b705cfSriastradh
189703b705cfSriastradh	if (op->mask.bo)
189803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->mask.bo);
189903b705cfSriastradh	if (op->src.bo)
190003b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->src.bo);
190103b705cfSriastradh
190203b705cfSriastradh	sna_render_composite_redirect_done(sna, op);
190303b705cfSriastradh}
190403b705cfSriastradh
190542542f5fSchristosinline static bool
190603b705cfSriastradhgen6_composite_set_target(struct sna *sna,
190703b705cfSriastradh			  struct sna_composite_op *op,
190803b705cfSriastradh			  PicturePtr dst,
190903b705cfSriastradh			  int x, int y, int w, int h,
191003b705cfSriastradh			  bool partial)
191103b705cfSriastradh{
191203b705cfSriastradh	BoxRec box;
191342542f5fSchristos	unsigned int hint;
191442542f5fSchristos
191542542f5fSchristos	DBG(("%s: (%d, %d)x(%d, %d), partial?=%d\n", __FUNCTION__, x, y, w, h, partial));
191603b705cfSriastradh
191703b705cfSriastradh	op->dst.pixmap = get_drawable_pixmap(dst->pDrawable);
191803b705cfSriastradh	op->dst.format = dst->format;
191903b705cfSriastradh	op->dst.width = op->dst.pixmap->drawable.width;
192003b705cfSriastradh	op->dst.height = op->dst.pixmap->drawable.height;
192103b705cfSriastradh
192203b705cfSriastradh	if (w && h) {
192303b705cfSriastradh		box.x1 = x;
192403b705cfSriastradh		box.y1 = y;
192503b705cfSriastradh		box.x2 = x + w;
192603b705cfSriastradh		box.y2 = y + h;
192703b705cfSriastradh	} else
192803b705cfSriastradh		sna_render_picture_extents(dst, &box);
192903b705cfSriastradh
193042542f5fSchristos	hint = PREFER_GPU | FORCE_GPU | RENDER_GPU;
193142542f5fSchristos	if (!partial) {
193242542f5fSchristos		hint |= IGNORE_DAMAGE;
193342542f5fSchristos		if (w == op->dst.width && h == op->dst.height)
193442542f5fSchristos			hint |= REPLACES;
193542542f5fSchristos	}
193642542f5fSchristos
193742542f5fSchristos	op->dst.bo = sna_drawable_use_bo(dst->pDrawable, hint, &box, &op->damage);
193803b705cfSriastradh	if (op->dst.bo == NULL)
193903b705cfSriastradh		return false;
194003b705cfSriastradh
194142542f5fSchristos	if (hint & REPLACES) {
194242542f5fSchristos		struct sna_pixmap *priv = sna_pixmap(op->dst.pixmap);
194342542f5fSchristos		kgem_bo_pair_undo(&sna->kgem, priv->gpu_bo, priv->cpu_bo);
194442542f5fSchristos	}
194542542f5fSchristos
194603b705cfSriastradh	get_drawable_deltas(dst->pDrawable, op->dst.pixmap,
194703b705cfSriastradh			    &op->dst.x, &op->dst.y);
194803b705cfSriastradh
194942542f5fSchristos	DBG(("%s: pixmap=%ld, format=%08x, size=%dx%d, pitch=%d, delta=(%d,%d),damage=%p\n",
195003b705cfSriastradh	     __FUNCTION__,
195142542f5fSchristos	     op->dst.pixmap->drawable.serialNumber, (int)op->dst.format,
195203b705cfSriastradh	     op->dst.width, op->dst.height,
195303b705cfSriastradh	     op->dst.bo->pitch,
195403b705cfSriastradh	     op->dst.x, op->dst.y,
195503b705cfSriastradh	     op->damage ? *op->damage : (void *)-1));
195603b705cfSriastradh
195703b705cfSriastradh	assert(op->dst.bo->proxy == NULL);
195803b705cfSriastradh
195903b705cfSriastradh	if (too_large(op->dst.width, op->dst.height) &&
196003b705cfSriastradh	    !sna_render_composite_redirect(sna, op, x, y, w, h, partial))
196103b705cfSriastradh		return false;
196203b705cfSriastradh
196303b705cfSriastradh	return true;
196403b705cfSriastradh}
196503b705cfSriastradh
196603b705cfSriastradhstatic bool
196703b705cfSriastradhtry_blt(struct sna *sna,
196803b705cfSriastradh	PicturePtr dst, PicturePtr src,
196903b705cfSriastradh	int width, int height)
197003b705cfSriastradh{
197103b705cfSriastradh	struct kgem_bo *bo;
197203b705cfSriastradh
197342542f5fSchristos	if (sna->kgem.mode == KGEM_BLT) {
197403b705cfSriastradh		DBG(("%s: already performing BLT\n", __FUNCTION__));
197503b705cfSriastradh		return true;
197603b705cfSriastradh	}
197703b705cfSriastradh
197803b705cfSriastradh	if (too_large(width, height)) {
197903b705cfSriastradh		DBG(("%s: operation too large for 3D pipe (%d, %d)\n",
198003b705cfSriastradh		     __FUNCTION__, width, height));
198103b705cfSriastradh		return true;
198203b705cfSriastradh	}
198303b705cfSriastradh
198442542f5fSchristos	bo = __sna_drawable_peek_bo(dst->pDrawable);
198542542f5fSchristos	if (bo == NULL)
198642542f5fSchristos		return true;
198742542f5fSchristos	if (bo->rq)
198842542f5fSchristos		return RQ_IS_BLT(bo->rq);
198942542f5fSchristos
199042542f5fSchristos	if (sna_picture_is_solid(src, NULL) && can_switch_to_blt(sna, bo, 0))
199142542f5fSchristos		return true;
199242542f5fSchristos
199342542f5fSchristos	if (src->pDrawable) {
199442542f5fSchristos		bo = __sna_drawable_peek_bo(src->pDrawable);
199542542f5fSchristos		if (bo == NULL)
199642542f5fSchristos			return true;
199742542f5fSchristos
199842542f5fSchristos		if (prefer_blt_bo(sna, bo))
199942542f5fSchristos			return true;
200042542f5fSchristos	}
200142542f5fSchristos
200242542f5fSchristos	if (sna->kgem.ring == KGEM_BLT) {
200342542f5fSchristos		DBG(("%s: already performing BLT\n", __FUNCTION__));
200403b705cfSriastradh		return true;
200542542f5fSchristos	}
200603b705cfSriastradh
200703b705cfSriastradh	return false;
200803b705cfSriastradh}
200903b705cfSriastradh
201003b705cfSriastradhstatic bool
201103b705cfSriastradhcheck_gradient(PicturePtr picture, bool precise)
201203b705cfSriastradh{
201303b705cfSriastradh	if (picture->pDrawable)
201403b705cfSriastradh		return false;
201503b705cfSriastradh
201603b705cfSriastradh	switch (picture->pSourcePict->type) {
201703b705cfSriastradh	case SourcePictTypeSolidFill:
201803b705cfSriastradh	case SourcePictTypeLinear:
201903b705cfSriastradh		return false;
202003b705cfSriastradh	default:
202103b705cfSriastradh		return precise;
202203b705cfSriastradh	}
202303b705cfSriastradh}
202403b705cfSriastradh
202503b705cfSriastradhstatic bool
202603b705cfSriastradhhas_alphamap(PicturePtr p)
202703b705cfSriastradh{
202803b705cfSriastradh	return p->alphaMap != NULL;
202903b705cfSriastradh}
203003b705cfSriastradh
203103b705cfSriastradhstatic bool
203203b705cfSriastradhneed_upload(PicturePtr p)
203303b705cfSriastradh{
203403b705cfSriastradh	return p->pDrawable && unattached(p->pDrawable) && untransformed(p);
203503b705cfSriastradh}
203603b705cfSriastradh
203703b705cfSriastradhstatic bool
203803b705cfSriastradhsource_is_busy(PixmapPtr pixmap)
203903b705cfSriastradh{
204003b705cfSriastradh	struct sna_pixmap *priv = sna_pixmap(pixmap);
204103b705cfSriastradh	if (priv == NULL || priv->clear)
204203b705cfSriastradh		return false;
204303b705cfSriastradh
204403b705cfSriastradh	if (priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo))
204503b705cfSriastradh		return true;
204603b705cfSriastradh
204703b705cfSriastradh	if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
204803b705cfSriastradh		return true;
204903b705cfSriastradh
205003b705cfSriastradh	return priv->gpu_damage && !priv->cpu_damage;
205103b705cfSriastradh}
205203b705cfSriastradh
205303b705cfSriastradhstatic bool
205403b705cfSriastradhsource_fallback(PicturePtr p, PixmapPtr pixmap, bool precise)
205503b705cfSriastradh{
205603b705cfSriastradh	if (sna_picture_is_solid(p, NULL))
205703b705cfSriastradh		return false;
205803b705cfSriastradh
205903b705cfSriastradh	if (p->pSourcePict)
206003b705cfSriastradh		return check_gradient(p, precise);
206103b705cfSriastradh
206203b705cfSriastradh	if (!gen6_check_repeat(p) || !gen6_check_format(p->format))
206303b705cfSriastradh		return true;
206403b705cfSriastradh
206503b705cfSriastradh	if (pixmap && source_is_busy(pixmap))
206603b705cfSriastradh		return false;
206703b705cfSriastradh
206803b705cfSriastradh	return has_alphamap(p) || !gen6_check_filter(p) || need_upload(p);
206903b705cfSriastradh}
207003b705cfSriastradh
207103b705cfSriastradhstatic bool
207203b705cfSriastradhgen6_composite_fallback(struct sna *sna,
207303b705cfSriastradh			PicturePtr src,
207403b705cfSriastradh			PicturePtr mask,
207503b705cfSriastradh			PicturePtr dst)
207603b705cfSriastradh{
207703b705cfSriastradh	PixmapPtr src_pixmap;
207803b705cfSriastradh	PixmapPtr mask_pixmap;
207903b705cfSriastradh	PixmapPtr dst_pixmap;
208003b705cfSriastradh	bool src_fallback, mask_fallback;
208103b705cfSriastradh
208203b705cfSriastradh	if (!gen6_check_dst_format(dst->format)) {
208303b705cfSriastradh		DBG(("%s: unknown destination format: %d\n",
208403b705cfSriastradh		     __FUNCTION__, dst->format));
208503b705cfSriastradh		return true;
208603b705cfSriastradh	}
208703b705cfSriastradh
208803b705cfSriastradh	dst_pixmap = get_drawable_pixmap(dst->pDrawable);
208903b705cfSriastradh
209003b705cfSriastradh	src_pixmap = src->pDrawable ? get_drawable_pixmap(src->pDrawable) : NULL;
209103b705cfSriastradh	src_fallback = source_fallback(src, src_pixmap,
209203b705cfSriastradh				       dst->polyMode == PolyModePrecise);
209303b705cfSriastradh
209403b705cfSriastradh	if (mask) {
209503b705cfSriastradh		mask_pixmap = mask->pDrawable ? get_drawable_pixmap(mask->pDrawable) : NULL;
209603b705cfSriastradh		mask_fallback = source_fallback(mask, mask_pixmap,
209703b705cfSriastradh						dst->polyMode == PolyModePrecise);
209803b705cfSriastradh	} else {
209903b705cfSriastradh		mask_pixmap = NULL;
210003b705cfSriastradh		mask_fallback = false;
210103b705cfSriastradh	}
210203b705cfSriastradh
210303b705cfSriastradh	/* If we are using the destination as a source and need to
210403b705cfSriastradh	 * readback in order to upload the source, do it all
210503b705cfSriastradh	 * on the cpu.
210603b705cfSriastradh	 */
210703b705cfSriastradh	if (src_pixmap == dst_pixmap && src_fallback) {
210803b705cfSriastradh		DBG(("%s: src is dst and will fallback\n",__FUNCTION__));
210903b705cfSriastradh		return true;
211003b705cfSriastradh	}
211103b705cfSriastradh	if (mask_pixmap == dst_pixmap && mask_fallback) {
211203b705cfSriastradh		DBG(("%s: mask is dst and will fallback\n",__FUNCTION__));
211303b705cfSriastradh		return true;
211403b705cfSriastradh	}
211503b705cfSriastradh
211603b705cfSriastradh	/* If anything is on the GPU, push everything out to the GPU */
211703b705cfSriastradh	if (dst_use_gpu(dst_pixmap)) {
211803b705cfSriastradh		DBG(("%s: dst is already on the GPU, try to use GPU\n",
211903b705cfSriastradh		     __FUNCTION__));
212003b705cfSriastradh		return false;
212103b705cfSriastradh	}
212203b705cfSriastradh
212303b705cfSriastradh	if (src_pixmap && !src_fallback) {
212403b705cfSriastradh		DBG(("%s: src is already on the GPU, try to use GPU\n",
212503b705cfSriastradh		     __FUNCTION__));
212603b705cfSriastradh		return false;
212703b705cfSriastradh	}
212803b705cfSriastradh	if (mask_pixmap && !mask_fallback) {
212903b705cfSriastradh		DBG(("%s: mask is already on the GPU, try to use GPU\n",
213003b705cfSriastradh		     __FUNCTION__));
213103b705cfSriastradh		return false;
213203b705cfSriastradh	}
213303b705cfSriastradh
213403b705cfSriastradh	/* However if the dst is not on the GPU and we need to
213503b705cfSriastradh	 * render one of the sources using the CPU, we may
213603b705cfSriastradh	 * as well do the entire operation in place onthe CPU.
213703b705cfSriastradh	 */
213803b705cfSriastradh	if (src_fallback) {
213903b705cfSriastradh		DBG(("%s: dst is on the CPU and src will fallback\n",
214003b705cfSriastradh		     __FUNCTION__));
214103b705cfSriastradh		return true;
214203b705cfSriastradh	}
214303b705cfSriastradh
214403b705cfSriastradh	if (mask && mask_fallback) {
214503b705cfSriastradh		DBG(("%s: dst is on the CPU and mask will fallback\n",
214603b705cfSriastradh		     __FUNCTION__));
214703b705cfSriastradh		return true;
214803b705cfSriastradh	}
214903b705cfSriastradh
215003b705cfSriastradh	if (too_large(dst_pixmap->drawable.width,
215103b705cfSriastradh		      dst_pixmap->drawable.height) &&
215203b705cfSriastradh	    dst_is_cpu(dst_pixmap)) {
215303b705cfSriastradh		DBG(("%s: dst is on the CPU and too large\n", __FUNCTION__));
215403b705cfSriastradh		return true;
215503b705cfSriastradh	}
215603b705cfSriastradh
215703b705cfSriastradh	DBG(("%s: dst is not on the GPU and the operation should not fallback\n",
215803b705cfSriastradh	     __FUNCTION__));
215903b705cfSriastradh	return dst_use_cpu(dst_pixmap);
216003b705cfSriastradh}
216103b705cfSriastradh
216203b705cfSriastradhstatic int
216303b705cfSriastradhreuse_source(struct sna *sna,
216403b705cfSriastradh	     PicturePtr src, struct sna_composite_channel *sc, int src_x, int src_y,
216503b705cfSriastradh	     PicturePtr mask, struct sna_composite_channel *mc, int msk_x, int msk_y)
216603b705cfSriastradh{
216703b705cfSriastradh	uint32_t color;
216803b705cfSriastradh
216903b705cfSriastradh	if (src_x != msk_x || src_y != msk_y)
217003b705cfSriastradh		return false;
217103b705cfSriastradh
217203b705cfSriastradh	if (src == mask) {
217303b705cfSriastradh		DBG(("%s: mask is source\n", __FUNCTION__));
217403b705cfSriastradh		*mc = *sc;
217503b705cfSriastradh		mc->bo = kgem_bo_reference(mc->bo);
217603b705cfSriastradh		return true;
217703b705cfSriastradh	}
217803b705cfSriastradh
217903b705cfSriastradh	if (sna_picture_is_solid(mask, &color))
218003b705cfSriastradh		return gen4_channel_init_solid(sna, mc, color);
218103b705cfSriastradh
218203b705cfSriastradh	if (sc->is_solid)
218303b705cfSriastradh		return false;
218403b705cfSriastradh
218503b705cfSriastradh	if (src->pDrawable == NULL || mask->pDrawable != src->pDrawable)
218603b705cfSriastradh		return false;
218703b705cfSriastradh
218803b705cfSriastradh	DBG(("%s: mask reuses source drawable\n", __FUNCTION__));
218903b705cfSriastradh
219003b705cfSriastradh	if (!sna_transform_equal(src->transform, mask->transform))
219103b705cfSriastradh		return false;
219203b705cfSriastradh
219303b705cfSriastradh	if (!sna_picture_alphamap_equal(src, mask))
219403b705cfSriastradh		return false;
219503b705cfSriastradh
219603b705cfSriastradh	if (!gen6_check_repeat(mask))
219703b705cfSriastradh		return false;
219803b705cfSriastradh
219903b705cfSriastradh	if (!gen6_check_filter(mask))
220003b705cfSriastradh		return false;
220103b705cfSriastradh
220203b705cfSriastradh	if (!gen6_check_format(mask->format))
220303b705cfSriastradh		return false;
220403b705cfSriastradh
220503b705cfSriastradh	DBG(("%s: reusing source channel for mask with a twist\n",
220603b705cfSriastradh	     __FUNCTION__));
220703b705cfSriastradh
220803b705cfSriastradh	*mc = *sc;
220903b705cfSriastradh	mc->repeat = gen6_repeat(mask->repeat ? mask->repeatType : RepeatNone);
221003b705cfSriastradh	mc->filter = gen6_filter(mask->filter);
221103b705cfSriastradh	mc->pict_format = mask->format;
221203b705cfSriastradh	mc->card_format = gen6_get_card_format(mask->format);
221303b705cfSriastradh	mc->bo = kgem_bo_reference(mc->bo);
221403b705cfSriastradh	return true;
221503b705cfSriastradh}
221603b705cfSriastradh
221703b705cfSriastradhstatic bool
221803b705cfSriastradhgen6_render_composite(struct sna *sna,
221903b705cfSriastradh		      uint8_t op,
222003b705cfSriastradh		      PicturePtr src,
222103b705cfSriastradh		      PicturePtr mask,
222203b705cfSriastradh		      PicturePtr dst,
222303b705cfSriastradh		      int16_t src_x, int16_t src_y,
222403b705cfSriastradh		      int16_t msk_x, int16_t msk_y,
222503b705cfSriastradh		      int16_t dst_x, int16_t dst_y,
222603b705cfSriastradh		      int16_t width, int16_t height,
222742542f5fSchristos		      unsigned flags,
222803b705cfSriastradh		      struct sna_composite_op *tmp)
222903b705cfSriastradh{
223003b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op))
223103b705cfSriastradh		return false;
223203b705cfSriastradh
223303b705cfSriastradh	DBG(("%s: %dx%d, current mode=%d\n", __FUNCTION__,
223403b705cfSriastradh	     width, height, sna->kgem.ring));
223503b705cfSriastradh
223603b705cfSriastradh	if (mask == NULL &&
223703b705cfSriastradh	    try_blt(sna, dst, src, width, height) &&
223803b705cfSriastradh	    sna_blt_composite(sna, op,
223903b705cfSriastradh			      src, dst,
224003b705cfSriastradh			      src_x, src_y,
224103b705cfSriastradh			      dst_x, dst_y,
224203b705cfSriastradh			      width, height,
224342542f5fSchristos			      flags, tmp))
224403b705cfSriastradh		return true;
224503b705cfSriastradh
224603b705cfSriastradh	if (gen6_composite_fallback(sna, src, mask, dst))
224742542f5fSchristos		goto fallback;
224803b705cfSriastradh
224903b705cfSriastradh	if (need_tiling(sna, width, height))
225003b705cfSriastradh		return sna_tiling_composite(op, src, mask, dst,
225103b705cfSriastradh					    src_x, src_y,
225203b705cfSriastradh					    msk_x, msk_y,
225303b705cfSriastradh					    dst_x, dst_y,
225403b705cfSriastradh					    width, height,
225503b705cfSriastradh					    tmp);
225603b705cfSriastradh
225742542f5fSchristos	if (op == PictOpClear && src == sna->clear)
225803b705cfSriastradh		op = PictOpSrc;
225903b705cfSriastradh	tmp->op = op;
226003b705cfSriastradh	if (!gen6_composite_set_target(sna, tmp, dst,
226103b705cfSriastradh				       dst_x, dst_y, width, height,
226242542f5fSchristos				       flags & COMPOSITE_PARTIAL || op > PictOpSrc))
226342542f5fSchristos		goto fallback;
226403b705cfSriastradh
226503b705cfSriastradh	switch (gen6_composite_picture(sna, src, &tmp->src,
226603b705cfSriastradh				       src_x, src_y,
226703b705cfSriastradh				       width, height,
226803b705cfSriastradh				       dst_x, dst_y,
226903b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
227003b705cfSriastradh	case -1:
227103b705cfSriastradh		goto cleanup_dst;
227203b705cfSriastradh	case 0:
227303b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->src, 0))
227403b705cfSriastradh			goto cleanup_dst;
227503b705cfSriastradh		/* fall through to fixup */
227603b705cfSriastradh	case 1:
227703b705cfSriastradh		/* Did we just switch rings to prepare the source? */
227803b705cfSriastradh		if (mask == NULL &&
227903b705cfSriastradh		    prefer_blt_composite(sna, tmp) &&
228003b705cfSriastradh		    sna_blt_composite__convert(sna,
228103b705cfSriastradh					       dst_x, dst_y, width, height,
228203b705cfSriastradh					       tmp))
228303b705cfSriastradh			return true;
228403b705cfSriastradh
228503b705cfSriastradh		gen6_composite_channel_convert(&tmp->src);
228603b705cfSriastradh		break;
228703b705cfSriastradh	}
228803b705cfSriastradh
228903b705cfSriastradh	tmp->is_affine = tmp->src.is_affine;
229003b705cfSriastradh	tmp->has_component_alpha = false;
229103b705cfSriastradh	tmp->need_magic_ca_pass = false;
229203b705cfSriastradh
229303b705cfSriastradh	tmp->mask.bo = NULL;
229403b705cfSriastradh	tmp->mask.filter = SAMPLER_FILTER_NEAREST;
229503b705cfSriastradh	tmp->mask.repeat = SAMPLER_EXTEND_NONE;
229603b705cfSriastradh
229703b705cfSriastradh	if (mask) {
229803b705cfSriastradh		if (mask->componentAlpha && PICT_FORMAT_RGB(mask->format)) {
229903b705cfSriastradh			tmp->has_component_alpha = true;
230003b705cfSriastradh
230103b705cfSriastradh			/* Check if it's component alpha that relies on a source alpha and on
230203b705cfSriastradh			 * the source value.  We can only get one of those into the single
230303b705cfSriastradh			 * source value that we get to blend with.
230403b705cfSriastradh			 */
230503b705cfSriastradh			if (gen6_blend_op[op].src_alpha &&
230603b705cfSriastradh			    (gen6_blend_op[op].src_blend != GEN6_BLENDFACTOR_ZERO)) {
230703b705cfSriastradh				if (op != PictOpOver)
230803b705cfSriastradh					goto cleanup_src;
230903b705cfSriastradh
231003b705cfSriastradh				tmp->need_magic_ca_pass = true;
231103b705cfSriastradh				tmp->op = PictOpOutReverse;
231203b705cfSriastradh			}
231303b705cfSriastradh		}
231403b705cfSriastradh
231503b705cfSriastradh		if (!reuse_source(sna,
231603b705cfSriastradh				  src, &tmp->src, src_x, src_y,
231703b705cfSriastradh				  mask, &tmp->mask, msk_x, msk_y)) {
231803b705cfSriastradh			switch (gen6_composite_picture(sna, mask, &tmp->mask,
231903b705cfSriastradh						       msk_x, msk_y,
232003b705cfSriastradh						       width, height,
232103b705cfSriastradh						       dst_x, dst_y,
232203b705cfSriastradh						       dst->polyMode == PolyModePrecise)) {
232303b705cfSriastradh			case -1:
232403b705cfSriastradh				goto cleanup_src;
232503b705cfSriastradh			case 0:
232603b705cfSriastradh				if (!gen4_channel_init_solid(sna, &tmp->mask, 0))
232703b705cfSriastradh					goto cleanup_src;
232803b705cfSriastradh				/* fall through to fixup */
232903b705cfSriastradh			case 1:
233003b705cfSriastradh				gen6_composite_channel_convert(&tmp->mask);
233103b705cfSriastradh				break;
233203b705cfSriastradh			}
233303b705cfSriastradh		}
233403b705cfSriastradh
233503b705cfSriastradh		tmp->is_affine &= tmp->mask.is_affine;
233603b705cfSriastradh	}
233703b705cfSriastradh
233803b705cfSriastradh	tmp->u.gen6.flags =
233903b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(tmp->src.filter,
234003b705cfSriastradh					      tmp->src.repeat,
234103b705cfSriastradh					      tmp->mask.filter,
234203b705cfSriastradh					      tmp->mask.repeat),
234303b705cfSriastradh			       gen6_get_blend(tmp->op,
234403b705cfSriastradh					      tmp->has_component_alpha,
234503b705cfSriastradh					      tmp->dst.format),
234603b705cfSriastradh			       gen6_choose_composite_kernel(tmp->op,
234703b705cfSriastradh							    tmp->mask.bo != NULL,
234803b705cfSriastradh							    tmp->has_component_alpha,
234903b705cfSriastradh							    tmp->is_affine),
235003b705cfSriastradh			       gen4_choose_composite_emitter(sna, tmp));
235103b705cfSriastradh
235203b705cfSriastradh	tmp->blt   = gen6_render_composite_blt;
235303b705cfSriastradh	tmp->box   = gen6_render_composite_box;
235403b705cfSriastradh	tmp->boxes = gen6_render_composite_boxes__blt;
235503b705cfSriastradh	if (tmp->emit_boxes) {
235603b705cfSriastradh		tmp->boxes = gen6_render_composite_boxes;
235703b705cfSriastradh		tmp->thread_boxes = gen6_render_composite_boxes__thread;
235803b705cfSriastradh	}
235903b705cfSriastradh	tmp->done  = gen6_render_composite_done;
236003b705cfSriastradh
236103b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->dst.bo);
236203b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
236303b705cfSriastradh			   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
236403b705cfSriastradh			   NULL)) {
236503b705cfSriastradh		kgem_submit(&sna->kgem);
236603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
236703b705cfSriastradh				   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
236803b705cfSriastradh				   NULL))
236903b705cfSriastradh			goto cleanup_mask;
237003b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
237103b705cfSriastradh	}
237203b705cfSriastradh
237303b705cfSriastradh	gen6_align_vertex(sna, tmp);
237442542f5fSchristos	gen6_emit_composite_state(sna, tmp);
237503b705cfSriastradh	return true;
237603b705cfSriastradh
237703b705cfSriastradhcleanup_mask:
237842542f5fSchristos	if (tmp->mask.bo) {
237903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->mask.bo);
238042542f5fSchristos		tmp->mask.bo = NULL;
238142542f5fSchristos	}
238203b705cfSriastradhcleanup_src:
238342542f5fSchristos	if (tmp->src.bo) {
238403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->src.bo);
238542542f5fSchristos		tmp->src.bo = NULL;
238642542f5fSchristos	}
238703b705cfSriastradhcleanup_dst:
238842542f5fSchristos	if (tmp->redirect.real_bo) {
238903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->dst.bo);
239042542f5fSchristos		tmp->redirect.real_bo = NULL;
239142542f5fSchristos	}
239242542f5fSchristosfallback:
239342542f5fSchristos	return (mask == NULL &&
239442542f5fSchristos		sna_blt_composite(sna, op,
239542542f5fSchristos				  src, dst,
239642542f5fSchristos				  src_x, src_y,
239742542f5fSchristos				  dst_x, dst_y,
239842542f5fSchristos				  width, height,
239942542f5fSchristos				  flags | COMPOSITE_FALLBACK, tmp));
240003b705cfSriastradh}
240103b705cfSriastradh
240203b705cfSriastradh#if !NO_COMPOSITE_SPANS
240303b705cfSriastradhfastcall static void
240403b705cfSriastradhgen6_render_composite_spans_box(struct sna *sna,
240503b705cfSriastradh				const struct sna_composite_spans_op *op,
240603b705cfSriastradh				const BoxRec *box, float opacity)
240703b705cfSriastradh{
240803b705cfSriastradh	DBG(("%s: src=+(%d, %d), opacity=%f, dst=+(%d, %d), box=(%d, %d) x (%d, %d)\n",
240903b705cfSriastradh	     __FUNCTION__,
241003b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
241103b705cfSriastradh	     opacity,
241203b705cfSriastradh	     op->base.dst.x, op->base.dst.y,
241303b705cfSriastradh	     box->x1, box->y1,
241403b705cfSriastradh	     box->x2 - box->x1,
241503b705cfSriastradh	     box->y2 - box->y1));
241603b705cfSriastradh
241703b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_composite_state);
241803b705cfSriastradh	op->prim_emit(sna, op, box, opacity);
241903b705cfSriastradh}
242003b705cfSriastradh
242103b705cfSriastradhstatic void
242203b705cfSriastradhgen6_render_composite_spans_boxes(struct sna *sna,
242303b705cfSriastradh				  const struct sna_composite_spans_op *op,
242403b705cfSriastradh				  const BoxRec *box, int nbox,
242503b705cfSriastradh				  float opacity)
242603b705cfSriastradh{
242703b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), opacity=%f, dst=+(%d, %d)\n",
242803b705cfSriastradh	     __FUNCTION__, nbox,
242903b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
243003b705cfSriastradh	     opacity,
243103b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
243203b705cfSriastradh
243303b705cfSriastradh	do {
243403b705cfSriastradh		int nbox_this_time;
243503b705cfSriastradh
243603b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
243703b705cfSriastradh						     gen6_emit_composite_state);
243803b705cfSriastradh		nbox -= nbox_this_time;
243903b705cfSriastradh
244003b705cfSriastradh		do {
244103b705cfSriastradh			DBG(("  %s: (%d, %d) x (%d, %d)\n", __FUNCTION__,
244203b705cfSriastradh			     box->x1, box->y1,
244303b705cfSriastradh			     box->x2 - box->x1,
244403b705cfSriastradh			     box->y2 - box->y1));
244503b705cfSriastradh
244603b705cfSriastradh			op->prim_emit(sna, op, box++, opacity);
244703b705cfSriastradh		} while (--nbox_this_time);
244803b705cfSriastradh	} while (nbox);
244903b705cfSriastradh}
245003b705cfSriastradh
245103b705cfSriastradhfastcall static void
245203b705cfSriastradhgen6_render_composite_spans_boxes__thread(struct sna *sna,
245303b705cfSriastradh					  const struct sna_composite_spans_op *op,
245403b705cfSriastradh					  const struct sna_opacity_box *box,
245503b705cfSriastradh					  int nbox)
245603b705cfSriastradh{
245703b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), dst=+(%d, %d)\n",
245803b705cfSriastradh	     __FUNCTION__, nbox,
245903b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
246003b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
246103b705cfSriastradh
246203b705cfSriastradh	sna_vertex_lock(&sna->render);
246303b705cfSriastradh	do {
246403b705cfSriastradh		int nbox_this_time;
246503b705cfSriastradh		float *v;
246603b705cfSriastradh
246703b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
246803b705cfSriastradh						     gen6_emit_composite_state);
246903b705cfSriastradh		assert(nbox_this_time);
247003b705cfSriastradh		nbox -= nbox_this_time;
247103b705cfSriastradh
247203b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
247303b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->base.floats_per_rect;
247403b705cfSriastradh
247503b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
247603b705cfSriastradh		sna_vertex_unlock(&sna->render);
247703b705cfSriastradh
247803b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
247903b705cfSriastradh		box += nbox_this_time;
248003b705cfSriastradh
248103b705cfSriastradh		sna_vertex_lock(&sna->render);
248203b705cfSriastradh		sna_vertex_release__locked(&sna->render);
248303b705cfSriastradh	} while (nbox);
248403b705cfSriastradh	sna_vertex_unlock(&sna->render);
248503b705cfSriastradh}
248603b705cfSriastradh
248703b705cfSriastradhfastcall static void
248803b705cfSriastradhgen6_render_composite_spans_done(struct sna *sna,
248903b705cfSriastradh				 const struct sna_composite_spans_op *op)
249003b705cfSriastradh{
249103b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
249203b705cfSriastradh	assert(!sna->render.active);
249303b705cfSriastradh
249403b705cfSriastradh	if (sna->render.vertex_offset)
249503b705cfSriastradh		gen4_vertex_flush(sna);
249603b705cfSriastradh
249703b705cfSriastradh	if (op->base.src.bo)
249803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->base.src.bo);
249903b705cfSriastradh
250003b705cfSriastradh	sna_render_composite_redirect_done(sna, &op->base);
250103b705cfSriastradh}
250203b705cfSriastradh
250303b705cfSriastradhstatic bool
250403b705cfSriastradhgen6_check_composite_spans(struct sna *sna,
250503b705cfSriastradh			   uint8_t op, PicturePtr src, PicturePtr dst,
250603b705cfSriastradh			   int16_t width, int16_t height,
250703b705cfSriastradh			   unsigned flags)
250803b705cfSriastradh{
250903b705cfSriastradh	DBG(("%s: op=%d, width=%d, height=%d, flags=%x\n",
251003b705cfSriastradh	     __FUNCTION__, op, width, height, flags));
251103b705cfSriastradh
251203b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op))
251303b705cfSriastradh		return false;
251403b705cfSriastradh
251503b705cfSriastradh	if (gen6_composite_fallback(sna, src, NULL, dst)) {
251603b705cfSriastradh		DBG(("%s: operation would fallback\n", __FUNCTION__));
251703b705cfSriastradh		return false;
251803b705cfSriastradh	}
251903b705cfSriastradh
252003b705cfSriastradh	if (need_tiling(sna, width, height) &&
252103b705cfSriastradh	    !is_gpu(sna, dst->pDrawable, PREFER_GPU_SPANS)) {
252203b705cfSriastradh		DBG(("%s: fallback, tiled operation not on GPU\n",
252303b705cfSriastradh		     __FUNCTION__));
252403b705cfSriastradh		return false;
252503b705cfSriastradh	}
252603b705cfSriastradh
252703b705cfSriastradh	if ((flags & COMPOSITE_SPANS_RECTILINEAR) == 0) {
252803b705cfSriastradh		struct sna_pixmap *priv = sna_pixmap_from_drawable(dst->pDrawable);
252903b705cfSriastradh		assert(priv);
253003b705cfSriastradh
253103b705cfSriastradh		if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
253203b705cfSriastradh			return true;
253303b705cfSriastradh
253403b705cfSriastradh		if (flags & COMPOSITE_SPANS_INPLACE_HINT)
253503b705cfSriastradh			return false;
253603b705cfSriastradh
253703b705cfSriastradh		return priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo);
253803b705cfSriastradh	}
253903b705cfSriastradh
254003b705cfSriastradh	return true;
254103b705cfSriastradh}
254203b705cfSriastradh
254303b705cfSriastradhstatic bool
254403b705cfSriastradhgen6_render_composite_spans(struct sna *sna,
254503b705cfSriastradh			    uint8_t op,
254603b705cfSriastradh			    PicturePtr src,
254703b705cfSriastradh			    PicturePtr dst,
254803b705cfSriastradh			    int16_t src_x,  int16_t src_y,
254903b705cfSriastradh			    int16_t dst_x,  int16_t dst_y,
255003b705cfSriastradh			    int16_t width,  int16_t height,
255103b705cfSriastradh			    unsigned flags,
255203b705cfSriastradh			    struct sna_composite_spans_op *tmp)
255303b705cfSriastradh{
255403b705cfSriastradh	DBG(("%s: %dx%d with flags=%x, current mode=%d\n", __FUNCTION__,
255503b705cfSriastradh	     width, height, flags, sna->kgem.ring));
255603b705cfSriastradh
255703b705cfSriastradh	assert(gen6_check_composite_spans(sna, op, src, dst, width, height, flags));
255803b705cfSriastradh
255903b705cfSriastradh	if (need_tiling(sna, width, height)) {
256003b705cfSriastradh		DBG(("%s: tiling, operation (%dx%d) too wide for pipeline\n",
256103b705cfSriastradh		     __FUNCTION__, width, height));
256203b705cfSriastradh		return sna_tiling_composite_spans(op, src, dst,
256303b705cfSriastradh						  src_x, src_y, dst_x, dst_y,
256403b705cfSriastradh						  width, height, flags, tmp);
256503b705cfSriastradh	}
256603b705cfSriastradh
256703b705cfSriastradh	tmp->base.op = op;
256803b705cfSriastradh	if (!gen6_composite_set_target(sna, &tmp->base, dst,
256903b705cfSriastradh				       dst_x, dst_y, width, height, true))
257003b705cfSriastradh		return false;
257103b705cfSriastradh
257203b705cfSriastradh	switch (gen6_composite_picture(sna, src, &tmp->base.src,
257303b705cfSriastradh				       src_x, src_y,
257403b705cfSriastradh				       width, height,
257503b705cfSriastradh				       dst_x, dst_y,
257603b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
257703b705cfSriastradh	case -1:
257803b705cfSriastradh		goto cleanup_dst;
257903b705cfSriastradh	case 0:
258003b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->base.src, 0))
258103b705cfSriastradh			goto cleanup_dst;
258203b705cfSriastradh		/* fall through to fixup */
258303b705cfSriastradh	case 1:
258403b705cfSriastradh		gen6_composite_channel_convert(&tmp->base.src);
258503b705cfSriastradh		break;
258603b705cfSriastradh	}
258703b705cfSriastradh	tmp->base.mask.bo = NULL;
258803b705cfSriastradh
258903b705cfSriastradh	tmp->base.is_affine = tmp->base.src.is_affine;
259003b705cfSriastradh	tmp->base.need_magic_ca_pass = false;
259103b705cfSriastradh
259203b705cfSriastradh	tmp->base.u.gen6.flags =
259303b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(tmp->base.src.filter,
259403b705cfSriastradh					      tmp->base.src.repeat,
259503b705cfSriastradh					      SAMPLER_FILTER_NEAREST,
259603b705cfSriastradh					      SAMPLER_EXTEND_PAD),
259703b705cfSriastradh			       gen6_get_blend(tmp->base.op, false, tmp->base.dst.format),
259803b705cfSriastradh			       GEN6_WM_KERNEL_OPACITY | !tmp->base.is_affine,
259903b705cfSriastradh			       gen4_choose_spans_emitter(sna, tmp));
260003b705cfSriastradh
260103b705cfSriastradh	tmp->box   = gen6_render_composite_spans_box;
260203b705cfSriastradh	tmp->boxes = gen6_render_composite_spans_boxes;
260303b705cfSriastradh	if (tmp->emit_boxes)
260403b705cfSriastradh		tmp->thread_boxes = gen6_render_composite_spans_boxes__thread;
260503b705cfSriastradh	tmp->done  = gen6_render_composite_spans_done;
260603b705cfSriastradh
260703b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->base.dst.bo);
260803b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
260903b705cfSriastradh			   tmp->base.dst.bo, tmp->base.src.bo,
261003b705cfSriastradh			   NULL)) {
261103b705cfSriastradh		kgem_submit(&sna->kgem);
261203b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
261303b705cfSriastradh				   tmp->base.dst.bo, tmp->base.src.bo,
261403b705cfSriastradh				   NULL))
261503b705cfSriastradh			goto cleanup_src;
261603b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
261703b705cfSriastradh	}
261803b705cfSriastradh
261903b705cfSriastradh	gen6_align_vertex(sna, &tmp->base);
262042542f5fSchristos	gen6_emit_composite_state(sna, &tmp->base);
262103b705cfSriastradh	return true;
262203b705cfSriastradh
262303b705cfSriastradhcleanup_src:
262403b705cfSriastradh	if (tmp->base.src.bo)
262503b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.src.bo);
262603b705cfSriastradhcleanup_dst:
262703b705cfSriastradh	if (tmp->base.redirect.real_bo)
262803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.dst.bo);
262903b705cfSriastradh	return false;
263003b705cfSriastradh}
263103b705cfSriastradh#endif
263203b705cfSriastradh
263303b705cfSriastradhstatic void
263403b705cfSriastradhgen6_emit_copy_state(struct sna *sna,
263503b705cfSriastradh		     const struct sna_composite_op *op)
263603b705cfSriastradh{
263703b705cfSriastradh	uint32_t *binding_table;
263803b705cfSriastradh	uint16_t offset;
263903b705cfSriastradh	bool dirty;
264003b705cfSriastradh
264103b705cfSriastradh	dirty = gen6_get_batch(sna, op);
264203b705cfSriastradh
264303b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
264403b705cfSriastradh
264503b705cfSriastradh	binding_table[0] =
264603b705cfSriastradh		gen6_bind_bo(sna,
264703b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
264803b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
264903b705cfSriastradh			     true);
265003b705cfSriastradh	binding_table[1] =
265103b705cfSriastradh		gen6_bind_bo(sna,
265203b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
265303b705cfSriastradh			     op->src.card_format,
265403b705cfSriastradh			     false);
265503b705cfSriastradh
265603b705cfSriastradh	if (sna->kgem.surface == offset &&
265703b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table) {
265803b705cfSriastradh		sna->kgem.surface += sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
265903b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
266003b705cfSriastradh	}
266103b705cfSriastradh
266203b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
266303b705cfSriastradh}
266403b705cfSriastradh
266503b705cfSriastradhstatic inline bool prefer_blt_copy(struct sna *sna,
266603b705cfSriastradh				   struct kgem_bo *src_bo,
266703b705cfSriastradh				   struct kgem_bo *dst_bo,
266803b705cfSriastradh				   unsigned flags)
266903b705cfSriastradh{
267003b705cfSriastradh	if (flags & COPY_SYNC)
267103b705cfSriastradh		return false;
267203b705cfSriastradh
267303b705cfSriastradh	if (PREFER_RENDER)
267403b705cfSriastradh		return PREFER_RENDER > 0;
267503b705cfSriastradh
267603b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT)
267703b705cfSriastradh		return true;
267803b705cfSriastradh
267903b705cfSriastradh	if (src_bo == dst_bo && can_switch_to_blt(sna, dst_bo, flags))
268003b705cfSriastradh		return true;
268103b705cfSriastradh
268203b705cfSriastradh	if (untiled_tlb_miss(src_bo) ||
268303b705cfSriastradh	    untiled_tlb_miss(dst_bo))
268403b705cfSriastradh		return true;
268503b705cfSriastradh
268642542f5fSchristos	if (force_blt_ring(sna))
268742542f5fSchristos		return true;
268842542f5fSchristos
268903b705cfSriastradh	if (kgem_bo_is_render(dst_bo) ||
269003b705cfSriastradh	    kgem_bo_is_render(src_bo))
269103b705cfSriastradh		return false;
269203b705cfSriastradh
269342542f5fSchristos	if (prefer_render_ring(sna, dst_bo))
269403b705cfSriastradh		return false;
269503b705cfSriastradh
269642542f5fSchristos	if (!prefer_blt_ring(sna, dst_bo, flags))
269703b705cfSriastradh		return false;
269803b705cfSriastradh
269942542f5fSchristos	return prefer_blt_bo(sna, src_bo) || prefer_blt_bo(sna, dst_bo);
270003b705cfSriastradh}
270103b705cfSriastradh
270203b705cfSriastradhstatic bool
270303b705cfSriastradhgen6_render_copy_boxes(struct sna *sna, uint8_t alu,
270442542f5fSchristos		       const DrawableRec *src, struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
270542542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
270603b705cfSriastradh		       const BoxRec *box, int n, unsigned flags)
270703b705cfSriastradh{
270803b705cfSriastradh	struct sna_composite_op tmp;
270903b705cfSriastradh	BoxRec extents;
271003b705cfSriastradh
271103b705cfSriastradh	DBG(("%s (%d, %d)->(%d, %d) x %d, alu=%x, self-copy=%d, overlaps? %d\n",
271203b705cfSriastradh	     __FUNCTION__, src_dx, src_dy, dst_dx, dst_dy, n, alu,
271303b705cfSriastradh	     src_bo == dst_bo,
271403b705cfSriastradh	     overlaps(sna,
271503b705cfSriastradh		      src_bo, src_dx, src_dy,
271603b705cfSriastradh		      dst_bo, dst_dx, dst_dy,
271742542f5fSchristos		      box, n, flags, &extents)));
271803b705cfSriastradh
271903b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, flags) &&
272042542f5fSchristos	    sna_blt_compare_depth(src, dst) &&
272103b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
272203b705cfSriastradh			       src_bo, src_dx, src_dy,
272303b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
272442542f5fSchristos			       dst->bitsPerPixel,
272503b705cfSriastradh			       box, n))
272603b705cfSriastradh		return true;
272703b705cfSriastradh
272803b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear)) {
272903b705cfSriastradhfallback_blt:
273042542f5fSchristos		if (!sna_blt_compare_depth(src, dst))
273103b705cfSriastradh			return false;
273203b705cfSriastradh
273303b705cfSriastradh		return sna_blt_copy_boxes_fallback(sna, alu,
273403b705cfSriastradh						   src, src_bo, src_dx, src_dy,
273503b705cfSriastradh						   dst, dst_bo, dst_dx, dst_dy,
273603b705cfSriastradh						   box, n);
273703b705cfSriastradh	}
273803b705cfSriastradh
273903b705cfSriastradh	if (overlaps(sna,
274003b705cfSriastradh		     src_bo, src_dx, src_dy,
274103b705cfSriastradh		     dst_bo, dst_dx, dst_dy,
274242542f5fSchristos		     box, n, flags,
274342542f5fSchristos		     &extents)) {
274442542f5fSchristos		bool big = too_large(extents.x2-extents.x1, extents.y2-extents.y1);
274503b705cfSriastradh
274642542f5fSchristos		if ((big || can_switch_to_blt(sna, dst_bo, flags)) &&
274703b705cfSriastradh		    sna_blt_copy_boxes(sna, alu,
274803b705cfSriastradh				       src_bo, src_dx, src_dy,
274903b705cfSriastradh				       dst_bo, dst_dx, dst_dy,
275042542f5fSchristos				       dst->bitsPerPixel,
275103b705cfSriastradh				       box, n))
275203b705cfSriastradh			return true;
275303b705cfSriastradh
275442542f5fSchristos		if (big)
275542542f5fSchristos			goto fallback_blt;
275642542f5fSchristos
275742542f5fSchristos		assert(src_bo == dst_bo);
275842542f5fSchristos		assert(src->depth == dst->depth);
275942542f5fSchristos		assert(src->width == dst->width);
276042542f5fSchristos		assert(src->height == dst->height);
276103b705cfSriastradh		return sna_render_copy_boxes__overlap(sna, alu,
276242542f5fSchristos						      src, src_bo,
276342542f5fSchristos						      src_dx, src_dy,
276442542f5fSchristos						      dst_dx, dst_dy,
276503b705cfSriastradh						      box, n, &extents);
276603b705cfSriastradh	}
276703b705cfSriastradh
276842542f5fSchristos	if (dst->depth == src->depth) {
276942542f5fSchristos		tmp.dst.format = sna_render_format_for_depth(dst->depth);
277003b705cfSriastradh		tmp.src.pict_format = tmp.dst.format;
277103b705cfSriastradh	} else {
277242542f5fSchristos		tmp.dst.format = sna_format_for_depth(dst->depth);
277342542f5fSchristos		tmp.src.pict_format = sna_format_for_depth(src->depth);
277403b705cfSriastradh	}
277503b705cfSriastradh	if (!gen6_check_format(tmp.src.pict_format))
277603b705cfSriastradh		goto fallback_blt;
277703b705cfSriastradh
277842542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
277942542f5fSchristos	tmp.dst.width  = dst->width;
278042542f5fSchristos	tmp.dst.height = dst->height;
278103b705cfSriastradh	tmp.dst.bo = dst_bo;
278203b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
278303b705cfSriastradh	tmp.damage = NULL;
278403b705cfSriastradh
278503b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
278603b705cfSriastradh	if (too_large(tmp.dst.width, tmp.dst.height)) {
278703b705cfSriastradh		int i;
278803b705cfSriastradh
278903b705cfSriastradh		extents = box[0];
279003b705cfSriastradh		for (i = 1; i < n; i++) {
279103b705cfSriastradh			if (box[i].x1 < extents.x1)
279203b705cfSriastradh				extents.x1 = box[i].x1;
279303b705cfSriastradh			if (box[i].y1 < extents.y1)
279403b705cfSriastradh				extents.y1 = box[i].y1;
279503b705cfSriastradh
279603b705cfSriastradh			if (box[i].x2 > extents.x2)
279703b705cfSriastradh				extents.x2 = box[i].x2;
279803b705cfSriastradh			if (box[i].y2 > extents.y2)
279903b705cfSriastradh				extents.y2 = box[i].y2;
280003b705cfSriastradh		}
280103b705cfSriastradh
280203b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
280303b705cfSriastradh						   extents.x1 + dst_dx,
280403b705cfSriastradh						   extents.y1 + dst_dy,
280503b705cfSriastradh						   extents.x2 - extents.x1,
280603b705cfSriastradh						   extents.y2 - extents.y1,
280703b705cfSriastradh						   n > 1))
280803b705cfSriastradh			goto fallback_tiled;
280903b705cfSriastradh	}
281003b705cfSriastradh
281103b705cfSriastradh	tmp.src.card_format = gen6_get_card_format(tmp.src.pict_format);
281242542f5fSchristos	if (too_large(src->width, src->height)) {
281303b705cfSriastradh		int i;
281403b705cfSriastradh
281503b705cfSriastradh		extents = box[0];
281603b705cfSriastradh		for (i = 1; i < n; i++) {
281703b705cfSriastradh			if (box[i].x1 < extents.x1)
281803b705cfSriastradh				extents.x1 = box[i].x1;
281903b705cfSriastradh			if (box[i].y1 < extents.y1)
282003b705cfSriastradh				extents.y1 = box[i].y1;
282103b705cfSriastradh
282203b705cfSriastradh			if (box[i].x2 > extents.x2)
282303b705cfSriastradh				extents.x2 = box[i].x2;
282403b705cfSriastradh			if (box[i].y2 > extents.y2)
282503b705cfSriastradh				extents.y2 = box[i].y2;
282603b705cfSriastradh		}
282703b705cfSriastradh
282803b705cfSriastradh		if (!sna_render_pixmap_partial(sna, src, src_bo, &tmp.src,
282903b705cfSriastradh					       extents.x1 + src_dx,
283003b705cfSriastradh					       extents.y1 + src_dy,
283103b705cfSriastradh					       extents.x2 - extents.x1,
283203b705cfSriastradh					       extents.y2 - extents.y1)) {
283303b705cfSriastradh			DBG(("%s: unable to extract partial pixmap\n", __FUNCTION__));
283403b705cfSriastradh			goto fallback_tiled_dst;
283503b705cfSriastradh		}
283603b705cfSriastradh	} else {
283703b705cfSriastradh		tmp.src.bo = src_bo;
283842542f5fSchristos		tmp.src.width  = src->width;
283942542f5fSchristos		tmp.src.height = src->height;
284042542f5fSchristos		tmp.src.offset[0] = tmp.src.offset[1] = 0;
284103b705cfSriastradh	}
284203b705cfSriastradh
284303b705cfSriastradh	tmp.mask.bo = NULL;
284403b705cfSriastradh
284503b705cfSriastradh	tmp.floats_per_vertex = 2;
284603b705cfSriastradh	tmp.floats_per_rect = 6;
284703b705cfSriastradh	tmp.need_magic_ca_pass = 0;
284803b705cfSriastradh
284903b705cfSriastradh	tmp.u.gen6.flags = COPY_FLAGS(alu);
285003b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
285103b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == COPY_SAMPLER);
285203b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == COPY_VERTEX);
285303b705cfSriastradh
285403b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
285503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
285603b705cfSriastradh		kgem_submit(&sna->kgem);
285703b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
285803b705cfSriastradh			DBG(("%s: too large for a single operation\n",
285903b705cfSriastradh			     __FUNCTION__));
286042542f5fSchristos			if (tmp.src.bo != src_bo)
286142542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.src.bo);
286242542f5fSchristos			if (tmp.redirect.real_bo)
286342542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
286442542f5fSchristos			goto fallback_blt;
286503b705cfSriastradh		}
286603b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
286703b705cfSriastradh	}
286803b705cfSriastradh
286942542f5fSchristos	src_dx += tmp.src.offset[0];
287042542f5fSchristos	src_dy += tmp.src.offset[1];
287142542f5fSchristos
287242542f5fSchristos	dst_dx += tmp.dst.x;
287342542f5fSchristos	dst_dy += tmp.dst.y;
287442542f5fSchristos
287542542f5fSchristos	tmp.dst.x = tmp.dst.y = 0;
287642542f5fSchristos
287703b705cfSriastradh	gen6_align_vertex(sna, &tmp);
287842542f5fSchristos	gen6_emit_copy_state(sna, &tmp);
287903b705cfSriastradh
288003b705cfSriastradh	do {
288103b705cfSriastradh		int16_t *v;
288203b705cfSriastradh		int n_this_time;
288303b705cfSriastradh
288403b705cfSriastradh		n_this_time = gen6_get_rectangles(sna, &tmp, n,
288503b705cfSriastradh						  gen6_emit_copy_state);
288603b705cfSriastradh		n -= n_this_time;
288703b705cfSriastradh
288803b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
288903b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
289003b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
289103b705cfSriastradh		do {
289203b705cfSriastradh
289303b705cfSriastradh			DBG(("	(%d, %d) -> (%d, %d) + (%d, %d)\n",
289403b705cfSriastradh			     box->x1 + src_dx, box->y1 + src_dy,
289503b705cfSriastradh			     box->x1 + dst_dx, box->y1 + dst_dy,
289603b705cfSriastradh			     box->x2 - box->x1, box->y2 - box->y1));
289703b705cfSriastradh			v[0] = box->x2 + dst_dx;
289803b705cfSriastradh			v[2] = box->x2 + src_dx;
289903b705cfSriastradh			v[1]  = v[5] = box->y2 + dst_dy;
290003b705cfSriastradh			v[3]  = v[7] = box->y2 + src_dy;
290103b705cfSriastradh			v[8]  = v[4] = box->x1 + dst_dx;
290203b705cfSriastradh			v[10] = v[6] = box->x1 + src_dx;
290303b705cfSriastradh			v[9]  = box->y1 + dst_dy;
290403b705cfSriastradh			v[11] = box->y1 + src_dy;
290503b705cfSriastradh			v += 12; box++;
290603b705cfSriastradh		} while (--n_this_time);
290703b705cfSriastradh	} while (n);
290803b705cfSriastradh
290903b705cfSriastradh	gen4_vertex_flush(sna);
291003b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
291103b705cfSriastradh	if (tmp.src.bo != src_bo)
291203b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.src.bo);
291303b705cfSriastradh	return true;
291403b705cfSriastradh
291503b705cfSriastradhfallback_tiled_dst:
291603b705cfSriastradh	if (tmp.redirect.real_bo)
291703b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
291803b705cfSriastradhfallback_tiled:
291942542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
292003b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
292103b705cfSriastradh			       src_bo, src_dx, src_dy,
292203b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
292342542f5fSchristos			       dst->bitsPerPixel,
292403b705cfSriastradh			       box, n))
292503b705cfSriastradh		return true;
292603b705cfSriastradh
292703b705cfSriastradh	return sna_tiling_copy_boxes(sna, alu,
292803b705cfSriastradh				     src, src_bo, src_dx, src_dy,
292903b705cfSriastradh				     dst, dst_bo, dst_dx, dst_dy,
293003b705cfSriastradh				     box, n);
293103b705cfSriastradh}
293203b705cfSriastradh
293303b705cfSriastradhstatic void
293403b705cfSriastradhgen6_render_copy_blt(struct sna *sna,
293503b705cfSriastradh		     const struct sna_copy_op *op,
293603b705cfSriastradh		     int16_t sx, int16_t sy,
293703b705cfSriastradh		     int16_t w,  int16_t h,
293803b705cfSriastradh		     int16_t dx, int16_t dy)
293903b705cfSriastradh{
294003b705cfSriastradh	int16_t *v;
294103b705cfSriastradh
294203b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_copy_state);
294303b705cfSriastradh
294403b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
294503b705cfSriastradh	sna->render.vertex_used += 6;
294603b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
294703b705cfSriastradh
294803b705cfSriastradh	v[0]  = dx+w; v[1]  = dy+h;
294903b705cfSriastradh	v[2]  = sx+w; v[3]  = sy+h;
295003b705cfSriastradh	v[4]  = dx;   v[5]  = dy+h;
295103b705cfSriastradh	v[6]  = sx;   v[7]  = sy+h;
295203b705cfSriastradh	v[8]  = dx;   v[9]  = dy;
295303b705cfSriastradh	v[10] = sx;   v[11] = sy;
295403b705cfSriastradh}
295503b705cfSriastradh
295603b705cfSriastradhstatic void
295703b705cfSriastradhgen6_render_copy_done(struct sna *sna, const struct sna_copy_op *op)
295803b705cfSriastradh{
295903b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
296003b705cfSriastradh
296103b705cfSriastradh	assert(!sna->render.active);
296203b705cfSriastradh	if (sna->render.vertex_offset)
296303b705cfSriastradh		gen4_vertex_flush(sna);
296403b705cfSriastradh}
296503b705cfSriastradh
296603b705cfSriastradhstatic bool
296703b705cfSriastradhgen6_render_copy(struct sna *sna, uint8_t alu,
296803b705cfSriastradh		 PixmapPtr src, struct kgem_bo *src_bo,
296903b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
297003b705cfSriastradh		 struct sna_copy_op *op)
297103b705cfSriastradh{
297203b705cfSriastradh	DBG(("%s (alu=%d, src=(%dx%d), dst=(%dx%d))\n",
297303b705cfSriastradh	     __FUNCTION__, alu,
297403b705cfSriastradh	     src->drawable.width, src->drawable.height,
297503b705cfSriastradh	     dst->drawable.width, dst->drawable.height));
297603b705cfSriastradh
297703b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, 0) &&
297803b705cfSriastradh	    sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
297903b705cfSriastradh	    sna_blt_copy(sna, alu,
298003b705cfSriastradh			 src_bo, dst_bo,
298103b705cfSriastradh			 dst->drawable.bitsPerPixel,
298203b705cfSriastradh			 op))
298303b705cfSriastradh		return true;
298403b705cfSriastradh
298503b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo ||
298603b705cfSriastradh	    too_large(src->drawable.width, src->drawable.height) ||
298703b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height)) {
298803b705cfSriastradhfallback:
298903b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
299003b705cfSriastradh			return false;
299103b705cfSriastradh
299203b705cfSriastradh		return sna_blt_copy(sna, alu, src_bo, dst_bo,
299303b705cfSriastradh				    dst->drawable.bitsPerPixel,
299403b705cfSriastradh				    op);
299503b705cfSriastradh	}
299603b705cfSriastradh
299703b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
299803b705cfSriastradh		op->base.dst.format = sna_render_format_for_depth(dst->drawable.depth);
299903b705cfSriastradh		op->base.src.pict_format = op->base.dst.format;
300003b705cfSriastradh	} else {
300103b705cfSriastradh		op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
300203b705cfSriastradh		op->base.src.pict_format = sna_format_for_depth(src->drawable.depth);
300303b705cfSriastradh	}
300403b705cfSriastradh	if (!gen6_check_format(op->base.src.pict_format))
300503b705cfSriastradh		goto fallback;
300603b705cfSriastradh
300703b705cfSriastradh	op->base.dst.pixmap = dst;
300803b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
300903b705cfSriastradh	op->base.dst.height = dst->drawable.height;
301003b705cfSriastradh	op->base.dst.bo = dst_bo;
301103b705cfSriastradh
301203b705cfSriastradh	op->base.src.bo = src_bo;
301303b705cfSriastradh	op->base.src.card_format =
301403b705cfSriastradh		gen6_get_card_format(op->base.src.pict_format);
301503b705cfSriastradh	op->base.src.width  = src->drawable.width;
301603b705cfSriastradh	op->base.src.height = src->drawable.height;
301703b705cfSriastradh
301803b705cfSriastradh	op->base.mask.bo = NULL;
301903b705cfSriastradh
302003b705cfSriastradh	op->base.floats_per_vertex = 2;
302103b705cfSriastradh	op->base.floats_per_rect = 6;
302203b705cfSriastradh
302303b705cfSriastradh	op->base.u.gen6.flags = COPY_FLAGS(alu);
302403b705cfSriastradh	assert(GEN6_KERNEL(op->base.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
302503b705cfSriastradh	assert(GEN6_SAMPLER(op->base.u.gen6.flags) == COPY_SAMPLER);
302603b705cfSriastradh	assert(GEN6_VERTEX(op->base.u.gen6.flags) == COPY_VERTEX);
302703b705cfSriastradh
302803b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
302903b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
303003b705cfSriastradh		kgem_submit(&sna->kgem);
303103b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL))
303203b705cfSriastradh			goto fallback;
303303b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
303403b705cfSriastradh	}
303503b705cfSriastradh
303603b705cfSriastradh	gen6_align_vertex(sna, &op->base);
303742542f5fSchristos	gen6_emit_copy_state(sna, &op->base);
303803b705cfSriastradh
303903b705cfSriastradh	op->blt  = gen6_render_copy_blt;
304003b705cfSriastradh	op->done = gen6_render_copy_done;
304103b705cfSriastradh	return true;
304203b705cfSriastradh}
304303b705cfSriastradh
304403b705cfSriastradhstatic void
304503b705cfSriastradhgen6_emit_fill_state(struct sna *sna, const struct sna_composite_op *op)
304603b705cfSriastradh{
304703b705cfSriastradh	uint32_t *binding_table;
304803b705cfSriastradh	uint16_t offset;
304903b705cfSriastradh	bool dirty;
305003b705cfSriastradh
305103b705cfSriastradh	dirty = gen6_get_batch(sna, op);
305203b705cfSriastradh
305303b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
305403b705cfSriastradh
305503b705cfSriastradh	binding_table[0] =
305603b705cfSriastradh		gen6_bind_bo(sna,
305703b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
305803b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
305903b705cfSriastradh			     true);
306003b705cfSriastradh	binding_table[1] =
306103b705cfSriastradh		gen6_bind_bo(sna,
306203b705cfSriastradh			     op->src.bo, 1, 1,
306303b705cfSriastradh			     GEN6_SURFACEFORMAT_B8G8R8A8_UNORM,
306403b705cfSriastradh			     false);
306503b705cfSriastradh
306603b705cfSriastradh	if (sna->kgem.surface == offset &&
306703b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table) {
306803b705cfSriastradh		sna->kgem.surface +=
306903b705cfSriastradh			sizeof(struct gen6_surface_state_padded)/sizeof(uint32_t);
307003b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
307103b705cfSriastradh	}
307203b705cfSriastradh
307303b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
307403b705cfSriastradh}
307503b705cfSriastradh
307603b705cfSriastradhstatic bool
307703b705cfSriastradhgen6_render_fill_boxes(struct sna *sna,
307803b705cfSriastradh		       CARD8 op,
307903b705cfSriastradh		       PictFormat format,
308003b705cfSriastradh		       const xRenderColor *color,
308142542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo,
308203b705cfSriastradh		       const BoxRec *box, int n)
308303b705cfSriastradh{
308403b705cfSriastradh	struct sna_composite_op tmp;
308503b705cfSriastradh	uint32_t pixel;
308603b705cfSriastradh
308703b705cfSriastradh	DBG(("%s (op=%d, color=(%04x, %04x, %04x, %04x) [%08x])\n",
308803b705cfSriastradh	     __FUNCTION__, op,
308903b705cfSriastradh	     color->red, color->green, color->blue, color->alpha, (int)format));
309003b705cfSriastradh
309103b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op)) {
309203b705cfSriastradh		DBG(("%s: fallback due to unhandled blend op: %d\n",
309303b705cfSriastradh		     __FUNCTION__, op));
309403b705cfSriastradh		return false;
309503b705cfSriastradh	}
309603b705cfSriastradh
309742542f5fSchristos	if (prefer_blt_fill(sna, dst_bo, FILL_BOXES) ||
309842542f5fSchristos	    !gen6_check_dst_format(format)) {
309903b705cfSriastradh		uint8_t alu = GXinvalid;
310003b705cfSriastradh
310103b705cfSriastradh		if (op <= PictOpSrc) {
310203b705cfSriastradh			pixel = 0;
310303b705cfSriastradh			if (op == PictOpClear)
310403b705cfSriastradh				alu = GXclear;
310503b705cfSriastradh			else if (sna_get_pixel_from_rgba(&pixel,
310603b705cfSriastradh							 color->red,
310703b705cfSriastradh							 color->green,
310803b705cfSriastradh							 color->blue,
310903b705cfSriastradh							 color->alpha,
311003b705cfSriastradh							 format))
311103b705cfSriastradh				alu = GXcopy;
311203b705cfSriastradh		}
311303b705cfSriastradh
311403b705cfSriastradh		if (alu != GXinvalid &&
311503b705cfSriastradh		    sna_blt_fill_boxes(sna, alu,
311642542f5fSchristos				       dst_bo, dst->bitsPerPixel,
311703b705cfSriastradh				       pixel, box, n))
311803b705cfSriastradh			return true;
311903b705cfSriastradh
312003b705cfSriastradh		if (!gen6_check_dst_format(format))
312103b705cfSriastradh			return false;
312203b705cfSriastradh	}
312303b705cfSriastradh
312403b705cfSriastradh	if (op == PictOpClear) {
312503b705cfSriastradh		pixel = 0;
312603b705cfSriastradh		op = PictOpSrc;
312703b705cfSriastradh	} else if (!sna_get_pixel_from_rgba(&pixel,
312803b705cfSriastradh					    color->red,
312903b705cfSriastradh					    color->green,
313003b705cfSriastradh					    color->blue,
313103b705cfSriastradh					    color->alpha,
313203b705cfSriastradh					    PICT_a8r8g8b8))
313303b705cfSriastradh		return false;
313403b705cfSriastradh
313503b705cfSriastradh	DBG(("%s(%08x x %d [(%d, %d), (%d, %d) ...])\n",
313603b705cfSriastradh	     __FUNCTION__, pixel, n,
313703b705cfSriastradh	     box[0].x1, box[0].y1, box[0].x2, box[0].y2));
313803b705cfSriastradh
313942542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
314042542f5fSchristos	tmp.dst.width  = dst->width;
314142542f5fSchristos	tmp.dst.height = dst->height;
314203b705cfSriastradh	tmp.dst.format = format;
314303b705cfSriastradh	tmp.dst.bo = dst_bo;
314403b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
314503b705cfSriastradh	tmp.damage = NULL;
314603b705cfSriastradh
314703b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
314842542f5fSchristos	if (too_large(dst->width, dst->height)) {
314903b705cfSriastradh		BoxRec extents;
315003b705cfSriastradh
315103b705cfSriastradh		boxes_extents(box, n, &extents);
315203b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
315303b705cfSriastradh						   extents.x1, extents.y1,
315403b705cfSriastradh						   extents.x2 - extents.x1,
315503b705cfSriastradh						   extents.y2 - extents.y1,
315603b705cfSriastradh						   n > 1))
315703b705cfSriastradh			return sna_tiling_fill_boxes(sna, op, format, color,
315803b705cfSriastradh						     dst, dst_bo, box, n);
315903b705cfSriastradh	}
316003b705cfSriastradh
316103b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, pixel);
316203b705cfSriastradh	tmp.mask.bo = NULL;
316303b705cfSriastradh
316403b705cfSriastradh	tmp.floats_per_vertex = 2;
316503b705cfSriastradh	tmp.floats_per_rect = 6;
316603b705cfSriastradh	tmp.need_magic_ca_pass = false;
316703b705cfSriastradh
316803b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS(op, format);
316903b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
317003b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
317103b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
317203b705cfSriastradh
317342542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
317403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
317503b705cfSriastradh		kgem_submit(&sna->kgem);
317603b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, dst_bo, NULL));
317703b705cfSriastradh	}
317803b705cfSriastradh
317903b705cfSriastradh	gen6_align_vertex(sna, &tmp);
318042542f5fSchristos	gen6_emit_fill_state(sna, &tmp);
318103b705cfSriastradh
318203b705cfSriastradh	do {
318303b705cfSriastradh		int n_this_time;
318403b705cfSriastradh		int16_t *v;
318503b705cfSriastradh
318603b705cfSriastradh		n_this_time = gen6_get_rectangles(sna, &tmp, n,
318703b705cfSriastradh						  gen6_emit_fill_state);
318803b705cfSriastradh		n -= n_this_time;
318903b705cfSriastradh
319003b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
319103b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
319203b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
319303b705cfSriastradh		do {
319403b705cfSriastradh			DBG(("	(%d, %d), (%d, %d)\n",
319503b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
319603b705cfSriastradh
319703b705cfSriastradh			v[0] = box->x2;
319803b705cfSriastradh			v[5] = v[1] = box->y2;
319903b705cfSriastradh			v[8] = v[4] = box->x1;
320003b705cfSriastradh			v[9] = box->y1;
320103b705cfSriastradh			v[2] = v[3]  = v[7]  = 1;
320203b705cfSriastradh			v[6] = v[10] = v[11] = 0;
320303b705cfSriastradh			v += 12; box++;
320403b705cfSriastradh		} while (--n_this_time);
320503b705cfSriastradh	} while (n);
320603b705cfSriastradh
320703b705cfSriastradh	gen4_vertex_flush(sna);
320803b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
320903b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
321003b705cfSriastradh	return true;
321103b705cfSriastradh}
321203b705cfSriastradh
321303b705cfSriastradhstatic void
321403b705cfSriastradhgen6_render_op_fill_blt(struct sna *sna,
321503b705cfSriastradh			const struct sna_fill_op *op,
321603b705cfSriastradh			int16_t x, int16_t y, int16_t w, int16_t h)
321703b705cfSriastradh{
321803b705cfSriastradh	int16_t *v;
321903b705cfSriastradh
322003b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d)\n", __FUNCTION__, x, y, w, h));
322103b705cfSriastradh
322203b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_fill_state);
322303b705cfSriastradh
322403b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
322503b705cfSriastradh	sna->render.vertex_used += 6;
322603b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
322703b705cfSriastradh
322803b705cfSriastradh	v[0] = x+w;
322903b705cfSriastradh	v[4] = v[8] = x;
323003b705cfSriastradh	v[1] = v[5] = y+h;
323103b705cfSriastradh	v[9] = y;
323203b705cfSriastradh
323303b705cfSriastradh	v[2] = v[3]  = v[7]  = 1;
323403b705cfSriastradh	v[6] = v[10] = v[11] = 0;
323503b705cfSriastradh}
323603b705cfSriastradh
323703b705cfSriastradhfastcall static void
323803b705cfSriastradhgen6_render_op_fill_box(struct sna *sna,
323903b705cfSriastradh			const struct sna_fill_op *op,
324003b705cfSriastradh			const BoxRec *box)
324103b705cfSriastradh{
324203b705cfSriastradh	int16_t *v;
324303b705cfSriastradh
324403b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
324503b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
324603b705cfSriastradh
324703b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_fill_state);
324803b705cfSriastradh
324903b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
325003b705cfSriastradh	sna->render.vertex_used += 6;
325103b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
325203b705cfSriastradh
325303b705cfSriastradh	v[0] = box->x2;
325403b705cfSriastradh	v[8] = v[4] = box->x1;
325503b705cfSriastradh	v[5] = v[1] = box->y2;
325603b705cfSriastradh	v[9] = box->y1;
325703b705cfSriastradh
325803b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
325903b705cfSriastradh	v[6] = v[10] = v[11] = 0;
326003b705cfSriastradh}
326103b705cfSriastradh
326203b705cfSriastradhfastcall static void
326303b705cfSriastradhgen6_render_op_fill_boxes(struct sna *sna,
326403b705cfSriastradh			  const struct sna_fill_op *op,
326503b705cfSriastradh			  const BoxRec *box,
326603b705cfSriastradh			  int nbox)
326703b705cfSriastradh{
326803b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)... x %d\n", __FUNCTION__,
326903b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2, nbox));
327003b705cfSriastradh
327103b705cfSriastradh	do {
327203b705cfSriastradh		int nbox_this_time;
327303b705cfSriastradh		int16_t *v;
327403b705cfSriastradh
327503b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
327603b705cfSriastradh						     gen6_emit_fill_state);
327703b705cfSriastradh		nbox -= nbox_this_time;
327803b705cfSriastradh
327903b705cfSriastradh		v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
328003b705cfSriastradh		sna->render.vertex_used += 6 * nbox_this_time;
328103b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
328203b705cfSriastradh
328303b705cfSriastradh		do {
328403b705cfSriastradh			v[0] = box->x2;
328503b705cfSriastradh			v[8] = v[4] = box->x1;
328603b705cfSriastradh			v[5] = v[1] = box->y2;
328703b705cfSriastradh			v[9] = box->y1;
328803b705cfSriastradh			v[7] = v[2]  = v[3]  = 1;
328903b705cfSriastradh			v[6] = v[10] = v[11] = 0;
329003b705cfSriastradh			box++; v += 12;
329103b705cfSriastradh		} while (--nbox_this_time);
329203b705cfSriastradh	} while (nbox);
329303b705cfSriastradh}
329403b705cfSriastradh
329503b705cfSriastradhstatic void
329603b705cfSriastradhgen6_render_op_fill_done(struct sna *sna, const struct sna_fill_op *op)
329703b705cfSriastradh{
329803b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
329903b705cfSriastradh
330003b705cfSriastradh	assert(!sna->render.active);
330103b705cfSriastradh	if (sna->render.vertex_offset)
330203b705cfSriastradh		gen4_vertex_flush(sna);
330303b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
330403b705cfSriastradh}
330503b705cfSriastradh
330603b705cfSriastradhstatic bool
330703b705cfSriastradhgen6_render_fill(struct sna *sna, uint8_t alu,
330803b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
330942542f5fSchristos		 uint32_t color, unsigned flags,
331003b705cfSriastradh		 struct sna_fill_op *op)
331103b705cfSriastradh{
331203b705cfSriastradh	DBG(("%s: (alu=%d, color=%x)\n", __FUNCTION__, alu, color));
331303b705cfSriastradh
331442542f5fSchristos	if (prefer_blt_fill(sna, dst_bo, flags) &&
331503b705cfSriastradh	    sna_blt_fill(sna, alu,
331603b705cfSriastradh			 dst_bo, dst->drawable.bitsPerPixel,
331703b705cfSriastradh			 color,
331803b705cfSriastradh			 op))
331903b705cfSriastradh		return true;
332003b705cfSriastradh
332103b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
332203b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
332303b705cfSriastradh		return sna_blt_fill(sna, alu,
332403b705cfSriastradh				    dst_bo, dst->drawable.bitsPerPixel,
332503b705cfSriastradh				    color,
332603b705cfSriastradh				    op);
332703b705cfSriastradh
332803b705cfSriastradh	if (alu == GXclear)
332903b705cfSriastradh		color = 0;
333003b705cfSriastradh
333103b705cfSriastradh	op->base.dst.pixmap = dst;
333203b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
333303b705cfSriastradh	op->base.dst.height = dst->drawable.height;
333403b705cfSriastradh	op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
333503b705cfSriastradh	op->base.dst.bo = dst_bo;
333603b705cfSriastradh	op->base.dst.x = op->base.dst.y = 0;
333703b705cfSriastradh
333803b705cfSriastradh	op->base.src.bo =
333903b705cfSriastradh		sna_render_get_solid(sna,
334003b705cfSriastradh				     sna_rgba_for_color(color,
334103b705cfSriastradh							dst->drawable.depth));
334203b705cfSriastradh	op->base.mask.bo = NULL;
334303b705cfSriastradh
334403b705cfSriastradh	op->base.need_magic_ca_pass = false;
334503b705cfSriastradh	op->base.floats_per_vertex = 2;
334603b705cfSriastradh	op->base.floats_per_rect = 6;
334703b705cfSriastradh
334803b705cfSriastradh	op->base.u.gen6.flags = FILL_FLAGS_NOBLEND;
334903b705cfSriastradh	assert(GEN6_KERNEL(op->base.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
335003b705cfSriastradh	assert(GEN6_SAMPLER(op->base.u.gen6.flags) == FILL_SAMPLER);
335103b705cfSriastradh	assert(GEN6_VERTEX(op->base.u.gen6.flags) == FILL_VERTEX);
335203b705cfSriastradh
335342542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
335403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
335503b705cfSriastradh		kgem_submit(&sna->kgem);
335603b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, dst_bo, NULL));
335703b705cfSriastradh	}
335803b705cfSriastradh
335903b705cfSriastradh	gen6_align_vertex(sna, &op->base);
336042542f5fSchristos	gen6_emit_fill_state(sna, &op->base);
336103b705cfSriastradh
336203b705cfSriastradh	op->blt  = gen6_render_op_fill_blt;
336303b705cfSriastradh	op->box  = gen6_render_op_fill_box;
336403b705cfSriastradh	op->boxes = gen6_render_op_fill_boxes;
336542542f5fSchristos	op->points = NULL;
336603b705cfSriastradh	op->done = gen6_render_op_fill_done;
336703b705cfSriastradh	return true;
336803b705cfSriastradh}
336903b705cfSriastradh
337003b705cfSriastradhstatic bool
337103b705cfSriastradhgen6_render_fill_one_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
337203b705cfSriastradh			     uint32_t color,
337303b705cfSriastradh			     int16_t x1, int16_t y1, int16_t x2, int16_t y2,
337403b705cfSriastradh			     uint8_t alu)
337503b705cfSriastradh{
337603b705cfSriastradh	BoxRec box;
337703b705cfSriastradh
337803b705cfSriastradh	box.x1 = x1;
337903b705cfSriastradh	box.y1 = y1;
338003b705cfSriastradh	box.x2 = x2;
338103b705cfSriastradh	box.y2 = y2;
338203b705cfSriastradh
338303b705cfSriastradh	return sna_blt_fill_boxes(sna, alu,
338403b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
338503b705cfSriastradh				  color, &box, 1);
338603b705cfSriastradh}
338703b705cfSriastradh
338803b705cfSriastradhstatic bool
338903b705cfSriastradhgen6_render_fill_one(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
339003b705cfSriastradh		     uint32_t color,
339103b705cfSriastradh		     int16_t x1, int16_t y1,
339203b705cfSriastradh		     int16_t x2, int16_t y2,
339303b705cfSriastradh		     uint8_t alu)
339403b705cfSriastradh{
339503b705cfSriastradh	struct sna_composite_op tmp;
339603b705cfSriastradh	int16_t *v;
339703b705cfSriastradh
339803b705cfSriastradh	/* Prefer to use the BLT if already engaged */
339942542f5fSchristos	if (prefer_blt_fill(sna, bo, FILL_BOXES) &&
340003b705cfSriastradh	    gen6_render_fill_one_try_blt(sna, dst, bo, color,
340103b705cfSriastradh					 x1, y1, x2, y2, alu))
340203b705cfSriastradh		return true;
340303b705cfSriastradh
340403b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
340503b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
340603b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
340703b705cfSriastradh		return gen6_render_fill_one_try_blt(sna, dst, bo, color,
340803b705cfSriastradh						    x1, y1, x2, y2, alu);
340903b705cfSriastradh
341003b705cfSriastradh	if (alu == GXclear)
341103b705cfSriastradh		color = 0;
341203b705cfSriastradh
341303b705cfSriastradh	tmp.dst.pixmap = dst;
341403b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
341503b705cfSriastradh	tmp.dst.height = dst->drawable.height;
341603b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
341703b705cfSriastradh	tmp.dst.bo = bo;
341803b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
341903b705cfSriastradh
342003b705cfSriastradh	tmp.src.bo =
342103b705cfSriastradh		sna_render_get_solid(sna,
342203b705cfSriastradh				     sna_rgba_for_color(color,
342303b705cfSriastradh							dst->drawable.depth));
342403b705cfSriastradh	tmp.mask.bo = NULL;
342503b705cfSriastradh
342603b705cfSriastradh	tmp.floats_per_vertex = 2;
342703b705cfSriastradh	tmp.floats_per_rect = 6;
342803b705cfSriastradh	tmp.need_magic_ca_pass = false;
342903b705cfSriastradh
343003b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS_NOBLEND;
343103b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
343203b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
343303b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
343403b705cfSriastradh
343542542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, bo);
343603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
343703b705cfSriastradh		kgem_submit(&sna->kgem);
343803b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
343903b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
344003b705cfSriastradh			return false;
344103b705cfSriastradh		}
344203b705cfSriastradh	}
344303b705cfSriastradh
344403b705cfSriastradh	gen6_align_vertex(sna, &tmp);
344542542f5fSchristos	gen6_emit_fill_state(sna, &tmp);
344603b705cfSriastradh
344703b705cfSriastradh	gen6_get_rectangles(sna, &tmp, 1, gen6_emit_fill_state);
344803b705cfSriastradh
344903b705cfSriastradh	DBG(("	(%d, %d), (%d, %d)\n", x1, y1, x2, y2));
345003b705cfSriastradh
345103b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
345203b705cfSriastradh	sna->render.vertex_used += 6;
345303b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
345403b705cfSriastradh
345503b705cfSriastradh	v[0] = x2;
345603b705cfSriastradh	v[8] = v[4] = x1;
345703b705cfSriastradh	v[5] = v[1] = y2;
345803b705cfSriastradh	v[9] = y1;
345903b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
346003b705cfSriastradh	v[6] = v[10] = v[11] = 0;
346103b705cfSriastradh
346203b705cfSriastradh	gen4_vertex_flush(sna);
346303b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
346403b705cfSriastradh
346503b705cfSriastradh	return true;
346603b705cfSriastradh}
346703b705cfSriastradh
346803b705cfSriastradhstatic bool
346903b705cfSriastradhgen6_render_clear_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
347003b705cfSriastradh{
347103b705cfSriastradh	BoxRec box;
347203b705cfSriastradh
347303b705cfSriastradh	box.x1 = 0;
347403b705cfSriastradh	box.y1 = 0;
347503b705cfSriastradh	box.x2 = dst->drawable.width;
347603b705cfSriastradh	box.y2 = dst->drawable.height;
347703b705cfSriastradh
347803b705cfSriastradh	return sna_blt_fill_boxes(sna, GXclear,
347903b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
348003b705cfSriastradh				  0, &box, 1);
348103b705cfSriastradh}
348203b705cfSriastradh
348303b705cfSriastradhstatic bool
348403b705cfSriastradhgen6_render_clear(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
348503b705cfSriastradh{
348603b705cfSriastradh	struct sna_composite_op tmp;
348703b705cfSriastradh	int16_t *v;
348803b705cfSriastradh
348903b705cfSriastradh	DBG(("%s: %dx%d\n",
349003b705cfSriastradh	     __FUNCTION__,
349103b705cfSriastradh	     dst->drawable.width,
349203b705cfSriastradh	     dst->drawable.height));
349303b705cfSriastradh
349403b705cfSriastradh	/* Prefer to use the BLT if, and only if, already engaged */
349503b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT &&
349603b705cfSriastradh	    gen6_render_clear_try_blt(sna, dst, bo))
349703b705cfSriastradh		return true;
349803b705cfSriastradh
349903b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
350003b705cfSriastradh	if (too_large(dst->drawable.width, dst->drawable.height))
350103b705cfSriastradh		return gen6_render_clear_try_blt(sna, dst, bo);
350203b705cfSriastradh
350303b705cfSriastradh	tmp.dst.pixmap = dst;
350403b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
350503b705cfSriastradh	tmp.dst.height = dst->drawable.height;
350603b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
350703b705cfSriastradh	tmp.dst.bo = bo;
350803b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
350903b705cfSriastradh
351003b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, 0);
351103b705cfSriastradh	tmp.mask.bo = NULL;
351203b705cfSriastradh
351303b705cfSriastradh	tmp.floats_per_vertex = 2;
351403b705cfSriastradh	tmp.floats_per_rect = 6;
351503b705cfSriastradh	tmp.need_magic_ca_pass = false;
351603b705cfSriastradh
351703b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS_NOBLEND;
351803b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
351903b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
352003b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
352103b705cfSriastradh
352242542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, bo);
352303b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
352403b705cfSriastradh		kgem_submit(&sna->kgem);
352503b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
352603b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
352703b705cfSriastradh			return false;
352803b705cfSriastradh		}
352903b705cfSriastradh	}
353003b705cfSriastradh
353103b705cfSriastradh	gen6_align_vertex(sna, &tmp);
353242542f5fSchristos	gen6_emit_fill_state(sna, &tmp);
353303b705cfSriastradh
353403b705cfSriastradh	gen6_get_rectangles(sna, &tmp, 1, gen6_emit_fill_state);
353503b705cfSriastradh
353603b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
353703b705cfSriastradh	sna->render.vertex_used += 6;
353803b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
353903b705cfSriastradh
354003b705cfSriastradh	v[0] = dst->drawable.width;
354103b705cfSriastradh	v[5] = v[1] = dst->drawable.height;
354203b705cfSriastradh	v[8] = v[4] = 0;
354303b705cfSriastradh	v[9] = 0;
354403b705cfSriastradh
354503b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
354603b705cfSriastradh	v[6] = v[10] = v[11] = 0;
354703b705cfSriastradh
354803b705cfSriastradh	gen4_vertex_flush(sna);
354903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
355003b705cfSriastradh
355103b705cfSriastradh	return true;
355203b705cfSriastradh}
355303b705cfSriastradh
355403b705cfSriastradhstatic void gen6_render_reset(struct sna *sna)
355503b705cfSriastradh{
355603b705cfSriastradh	sna->render_state.gen6.needs_invariant = true;
355703b705cfSriastradh	sna->render_state.gen6.first_state_packet = true;
355803b705cfSriastradh	sna->render_state.gen6.ve_id = 3 << 2;
355903b705cfSriastradh	sna->render_state.gen6.last_primitive = -1;
356003b705cfSriastradh
356103b705cfSriastradh	sna->render_state.gen6.num_sf_outputs = 0;
356203b705cfSriastradh	sna->render_state.gen6.samplers = -1;
356303b705cfSriastradh	sna->render_state.gen6.blend = -1;
356403b705cfSriastradh	sna->render_state.gen6.kernel = -1;
356503b705cfSriastradh	sna->render_state.gen6.drawrect_offset = -1;
356603b705cfSriastradh	sna->render_state.gen6.drawrect_limit = -1;
356703b705cfSriastradh	sna->render_state.gen6.surface_table = -1;
356803b705cfSriastradh
356942542f5fSchristos	if (sna->render.vbo && !kgem_bo_can_map(&sna->kgem, sna->render.vbo)) {
357042542f5fSchristos		DBG(("%s: discarding unmappable vbo\n", __FUNCTION__));
357142542f5fSchristos		discard_vbo(sna);
357242542f5fSchristos	}
357342542f5fSchristos
357403b705cfSriastradh	sna->render.vertex_offset = 0;
357503b705cfSriastradh	sna->render.nvertex_reloc = 0;
357603b705cfSriastradh	sna->render.vb_id = 0;
357703b705cfSriastradh}
357803b705cfSriastradh
357903b705cfSriastradhstatic void gen6_render_fini(struct sna *sna)
358003b705cfSriastradh{
358103b705cfSriastradh	kgem_bo_destroy(&sna->kgem, sna->render_state.gen6.general_bo);
358203b705cfSriastradh}
358303b705cfSriastradh
358442542f5fSchristosstatic bool is_gt2(struct sna *sna, int devid)
358503b705cfSriastradh{
358642542f5fSchristos	return devid & 0x30;
358703b705cfSriastradh}
358803b705cfSriastradh
358942542f5fSchristosstatic bool is_mobile(struct sna *sna, int devid)
359003b705cfSriastradh{
359142542f5fSchristos	return (devid & 0xf) == 0x6;
359203b705cfSriastradh}
359303b705cfSriastradh
359442542f5fSchristosstatic bool gen6_render_setup(struct sna *sna, int devid)
359503b705cfSriastradh{
359603b705cfSriastradh	struct gen6_render_state *state = &sna->render_state.gen6;
359703b705cfSriastradh	struct sna_static_stream general;
359803b705cfSriastradh	struct gen6_sampler_state *ss;
359903b705cfSriastradh	int i, j, k, l, m;
360003b705cfSriastradh
360103b705cfSriastradh	state->info = &gt1_info;
360242542f5fSchristos	if (is_gt2(sna, devid))
360303b705cfSriastradh		state->info = &gt2_info; /* XXX requires GT_MODE WiZ disabled */
360442542f5fSchristos	state->gt = state->info->gt;
360503b705cfSriastradh
360603b705cfSriastradh	sna_static_stream_init(&general);
360703b705cfSriastradh
360803b705cfSriastradh	/* Zero pad the start. If you see an offset of 0x0 in the batchbuffer
360903b705cfSriastradh	 * dumps, you know it points to zero.
361003b705cfSriastradh	 */
361103b705cfSriastradh	null_create(&general);
361203b705cfSriastradh	scratch_create(&general);
361303b705cfSriastradh
361403b705cfSriastradh	for (m = 0; m < GEN6_KERNEL_COUNT; m++) {
361503b705cfSriastradh		if (wm_kernels[m].size) {
361603b705cfSriastradh			state->wm_kernel[m][1] =
361703b705cfSriastradh				sna_static_stream_add(&general,
361803b705cfSriastradh						      wm_kernels[m].data,
361903b705cfSriastradh						      wm_kernels[m].size,
362003b705cfSriastradh						      64);
362103b705cfSriastradh		} else {
362203b705cfSriastradh			if (USE_8_PIXEL_DISPATCH) {
362303b705cfSriastradh				state->wm_kernel[m][0] =
362403b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
362503b705cfSriastradh								     wm_kernels[m].data, 8);
362603b705cfSriastradh			}
362703b705cfSriastradh
362803b705cfSriastradh			if (USE_16_PIXEL_DISPATCH) {
362903b705cfSriastradh				state->wm_kernel[m][1] =
363003b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
363103b705cfSriastradh								     wm_kernels[m].data, 16);
363203b705cfSriastradh			}
363303b705cfSriastradh
363403b705cfSriastradh			if (USE_32_PIXEL_DISPATCH) {
363503b705cfSriastradh				state->wm_kernel[m][2] =
363603b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
363703b705cfSriastradh								     wm_kernels[m].data, 32);
363803b705cfSriastradh			}
363903b705cfSriastradh		}
364003b705cfSriastradh		if ((state->wm_kernel[m][0]|state->wm_kernel[m][1]|state->wm_kernel[m][2]) == 0) {
364103b705cfSriastradh			state->wm_kernel[m][1] =
364203b705cfSriastradh				sna_static_stream_compile_wm(sna, &general,
364303b705cfSriastradh							     wm_kernels[m].data, 16);
364403b705cfSriastradh		}
364503b705cfSriastradh	}
364603b705cfSriastradh
364703b705cfSriastradh	ss = sna_static_stream_map(&general,
364803b705cfSriastradh				   2 * sizeof(*ss) *
364903b705cfSriastradh				   (2 +
365003b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT *
365103b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT),
365203b705cfSriastradh				   32);
365303b705cfSriastradh	state->wm_state = sna_static_stream_offsetof(&general, ss);
365403b705cfSriastradh	sampler_copy_init(ss); ss += 2;
365503b705cfSriastradh	sampler_fill_init(ss); ss += 2;
365603b705cfSriastradh	for (i = 0; i < FILTER_COUNT; i++) {
365703b705cfSriastradh		for (j = 0; j < EXTEND_COUNT; j++) {
365803b705cfSriastradh			for (k = 0; k < FILTER_COUNT; k++) {
365903b705cfSriastradh				for (l = 0; l < EXTEND_COUNT; l++) {
366003b705cfSriastradh					sampler_state_init(ss++, i, j);
366103b705cfSriastradh					sampler_state_init(ss++, k, l);
366203b705cfSriastradh				}
366303b705cfSriastradh			}
366403b705cfSriastradh		}
366503b705cfSriastradh	}
366603b705cfSriastradh
366703b705cfSriastradh	state->cc_blend = gen6_composite_create_blend_state(&general);
366803b705cfSriastradh
366903b705cfSriastradh	state->general_bo = sna_static_stream_fini(sna, &general);
367003b705cfSriastradh	return state->general_bo != NULL;
367103b705cfSriastradh}
367203b705cfSriastradh
367303b705cfSriastradhconst char *gen6_render_init(struct sna *sna, const char *backend)
367403b705cfSriastradh{
367513496ba1Ssnj	int devid = intel_get_device_id(sna->dev);
367642542f5fSchristos
367742542f5fSchristos	if (!gen6_render_setup(sna, devid))
367803b705cfSriastradh		return backend;
367903b705cfSriastradh
368003b705cfSriastradh	sna->kgem.context_switch = gen6_render_context_switch;
368103b705cfSriastradh	sna->kgem.retire = gen6_render_retire;
368242542f5fSchristos	sna->kgem.expire = gen4_render_expire;
368303b705cfSriastradh
368403b705cfSriastradh#if !NO_COMPOSITE
368503b705cfSriastradh	sna->render.composite = gen6_render_composite;
368603b705cfSriastradh	sna->render.prefer_gpu |= PREFER_GPU_RENDER;
368703b705cfSriastradh#endif
368803b705cfSriastradh
368903b705cfSriastradh#if !NO_COMPOSITE_SPANS
369003b705cfSriastradh	sna->render.check_composite_spans = gen6_check_composite_spans;
369103b705cfSriastradh	sna->render.composite_spans = gen6_render_composite_spans;
369242542f5fSchristos	if (is_mobile(sna, devid))
369303b705cfSriastradh		sna->render.prefer_gpu |= PREFER_GPU_SPANS;
369403b705cfSriastradh#endif
369503b705cfSriastradh	sna->render.video = gen6_render_video;
369603b705cfSriastradh
369703b705cfSriastradh#if !NO_COPY_BOXES
369803b705cfSriastradh	sna->render.copy_boxes = gen6_render_copy_boxes;
369903b705cfSriastradh#endif
370003b705cfSriastradh#if !NO_COPY
370103b705cfSriastradh	sna->render.copy = gen6_render_copy;
370203b705cfSriastradh#endif
370303b705cfSriastradh
370403b705cfSriastradh#if !NO_FILL_BOXES
370503b705cfSriastradh	sna->render.fill_boxes = gen6_render_fill_boxes;
370603b705cfSriastradh#endif
370703b705cfSriastradh#if !NO_FILL
370803b705cfSriastradh	sna->render.fill = gen6_render_fill;
370903b705cfSriastradh#endif
371003b705cfSriastradh#if !NO_FILL_ONE
371103b705cfSriastradh	sna->render.fill_one = gen6_render_fill_one;
371203b705cfSriastradh#endif
371303b705cfSriastradh#if !NO_FILL_CLEAR
371403b705cfSriastradh	sna->render.clear = gen6_render_clear;
371503b705cfSriastradh#endif
371603b705cfSriastradh
371742542f5fSchristos	sna->render.flush = gen4_render_flush;
371803b705cfSriastradh	sna->render.reset = gen6_render_reset;
371903b705cfSriastradh	sna->render.fini = gen6_render_fini;
372003b705cfSriastradh
372103b705cfSriastradh	sna->render.max_3d_size = GEN6_MAX_SIZE;
372203b705cfSriastradh	sna->render.max_3d_pitch = 1 << 18;
372303b705cfSriastradh	return sna->render_state.gen6.info->name;
372403b705cfSriastradh}
3725