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 "gen7_render.h"
4542542f5fSchristos#include "gen4_common.h"
4603b705cfSriastradh#include "gen4_source.h"
4703b705cfSriastradh#include "gen4_vertex.h"
4842542f5fSchristos#include "gen6_common.h"
4942542f5fSchristos
5042542f5fSchristos#define ALWAYS_INVALIDATE 0
5142542f5fSchristos#define ALWAYS_FLUSH 0
5242542f5fSchristos#define ALWAYS_STALL 0
5303b705cfSriastradh
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 GEN7_MAX_SIZE 16384
7203b705cfSriastradh
7303b705cfSriastradh/* XXX Todo
7403b705cfSriastradh *
7503b705cfSriastradh * STR (software tiled rendering) mode. No, really.
7603b705cfSriastradh * 64x32 pixel blocks align with the rendering cache. Worth considering.
7703b705cfSriastradh */
7803b705cfSriastradh
7903b705cfSriastradh#define is_aligned(x, y) (((x) & ((y) - 1)) == 0)
8003b705cfSriastradh
8103b705cfSriastradhstruct gt_info {
8203b705cfSriastradh	const char *name;
8303b705cfSriastradh	uint32_t max_vs_threads;
8403b705cfSriastradh	uint32_t max_gs_threads;
8503b705cfSriastradh	uint32_t max_wm_threads;
8603b705cfSriastradh	struct {
8703b705cfSriastradh		int size;
8803b705cfSriastradh		int max_vs_entries;
8903b705cfSriastradh		int max_gs_entries;
9003b705cfSriastradh		int push_ps_size; /* in 1KBs */
9103b705cfSriastradh	} urb;
9242542f5fSchristos	int gt;
9342542f5fSchristos	uint32_t mocs;
9403b705cfSriastradh};
9503b705cfSriastradh
9603b705cfSriastradhstatic const struct gt_info ivb_gt_info = {
9703b705cfSriastradh	.name = "Ivybridge (gen7)",
9803b705cfSriastradh	.max_vs_threads = 16,
9903b705cfSriastradh	.max_gs_threads = 16,
10003b705cfSriastradh	.max_wm_threads = (16-1) << IVB_PS_MAX_THREADS_SHIFT,
10103b705cfSriastradh	.urb = { 128, 64, 64, 8 },
10242542f5fSchristos	.gt = 0,
10303b705cfSriastradh};
10403b705cfSriastradh
10503b705cfSriastradhstatic const struct gt_info ivb_gt1_info = {
10603b705cfSriastradh	.name = "Ivybridge (gen7, gt1)",
10703b705cfSriastradh	.max_vs_threads = 36,
10803b705cfSriastradh	.max_gs_threads = 36,
10903b705cfSriastradh	.max_wm_threads = (48-1) << IVB_PS_MAX_THREADS_SHIFT,
11003b705cfSriastradh	.urb = { 128, 512, 192, 8 },
11142542f5fSchristos	.gt = 1,
11242542f5fSchristos	.mocs = 3,
11303b705cfSriastradh};
11403b705cfSriastradh
11503b705cfSriastradhstatic const struct gt_info ivb_gt2_info = {
11603b705cfSriastradh	.name = "Ivybridge (gen7, gt2)",
11703b705cfSriastradh	.max_vs_threads = 128,
11803b705cfSriastradh	.max_gs_threads = 128,
11903b705cfSriastradh	.max_wm_threads = (172-1) << IVB_PS_MAX_THREADS_SHIFT,
12003b705cfSriastradh	.urb = { 256, 704, 320, 8 },
12142542f5fSchristos	.gt = 2,
12242542f5fSchristos	.mocs = 3,
12303b705cfSriastradh};
12403b705cfSriastradh
12503b705cfSriastradhstatic const struct gt_info byt_gt_info = {
12603b705cfSriastradh	.name = "Baytrail (gen7)",
12703b705cfSriastradh	.max_vs_threads = 36,
12803b705cfSriastradh	.max_gs_threads = 36,
12903b705cfSriastradh	.max_wm_threads = (48-1) << IVB_PS_MAX_THREADS_SHIFT,
13003b705cfSriastradh	.urb = { 128, 512, 192, 8 },
13142542f5fSchristos	.gt = 1,
13203b705cfSriastradh};
13303b705cfSriastradh
13403b705cfSriastradhstatic const struct gt_info hsw_gt_info = {
13503b705cfSriastradh	.name = "Haswell (gen7.5)",
13603b705cfSriastradh	.max_vs_threads = 8,
13703b705cfSriastradh	.max_gs_threads = 8,
13803b705cfSriastradh	.max_wm_threads =
13903b705cfSriastradh		(8 - 1) << HSW_PS_MAX_THREADS_SHIFT |
14003b705cfSriastradh		1 << HSW_PS_SAMPLE_MASK_SHIFT,
14103b705cfSriastradh	.urb = { 128, 64, 64, 8 },
14242542f5fSchristos	.gt = 0,
14303b705cfSriastradh};
14403b705cfSriastradh
14503b705cfSriastradhstatic const struct gt_info hsw_gt1_info = {
14603b705cfSriastradh	.name = "Haswell (gen7.5, gt1)",
14703b705cfSriastradh	.max_vs_threads = 70,
14803b705cfSriastradh	.max_gs_threads = 70,
14903b705cfSriastradh	.max_wm_threads =
150fe8aea9eSmrg		(70 - 1) << HSW_PS_MAX_THREADS_SHIFT |
15103b705cfSriastradh		1 << HSW_PS_SAMPLE_MASK_SHIFT,
15203b705cfSriastradh	.urb = { 128, 640, 256, 8 },
15342542f5fSchristos	.gt = 1,
15442542f5fSchristos	.mocs = 5,
15503b705cfSriastradh};
15603b705cfSriastradh
15703b705cfSriastradhstatic const struct gt_info hsw_gt2_info = {
15803b705cfSriastradh	.name = "Haswell (gen7.5, gt2)",
15903b705cfSriastradh	.max_vs_threads = 140,
16003b705cfSriastradh	.max_gs_threads = 140,
16103b705cfSriastradh	.max_wm_threads =
16203b705cfSriastradh		(140 - 1) << HSW_PS_MAX_THREADS_SHIFT |
16303b705cfSriastradh		1 << HSW_PS_SAMPLE_MASK_SHIFT,
16403b705cfSriastradh	.urb = { 256, 1664, 640, 8 },
16542542f5fSchristos	.gt = 2,
16642542f5fSchristos	.mocs = 5,
16703b705cfSriastradh};
16803b705cfSriastradh
16903b705cfSriastradhstatic const struct gt_info hsw_gt3_info = {
17003b705cfSriastradh	.name = "Haswell (gen7.5, gt3)",
17103b705cfSriastradh	.max_vs_threads = 280,
17203b705cfSriastradh	.max_gs_threads = 280,
17303b705cfSriastradh	.max_wm_threads =
17403b705cfSriastradh		(280 - 1) << HSW_PS_MAX_THREADS_SHIFT |
17503b705cfSriastradh		1 << HSW_PS_SAMPLE_MASK_SHIFT,
17603b705cfSriastradh	.urb = { 512, 3328, 1280, 16 },
17742542f5fSchristos	.gt = 3,
17842542f5fSchristos	.mocs = 5,
17903b705cfSriastradh};
18003b705cfSriastradh
18103b705cfSriastradhinline static bool is_ivb(struct sna *sna)
18203b705cfSriastradh{
18303b705cfSriastradh	return sna->kgem.gen == 070;
18403b705cfSriastradh}
18503b705cfSriastradh
18603b705cfSriastradhinline static bool is_byt(struct sna *sna)
18703b705cfSriastradh{
18803b705cfSriastradh	return sna->kgem.gen == 071;
18903b705cfSriastradh}
19003b705cfSriastradh
19103b705cfSriastradhinline static bool is_hsw(struct sna *sna)
19203b705cfSriastradh{
19303b705cfSriastradh	return sna->kgem.gen == 075;
19403b705cfSriastradh}
19503b705cfSriastradh
196fe8aea9eSmrgstatic const uint32_t ps_kernel_packed_bt601[][4] = {
19703b705cfSriastradh#include "exa_wm_src_affine.g7b"
19803b705cfSriastradh#include "exa_wm_src_sample_argb.g7b"
199fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt601.g7b"
20003b705cfSriastradh#include "exa_wm_write.g7b"
20103b705cfSriastradh};
20203b705cfSriastradh
203fe8aea9eSmrgstatic const uint32_t ps_kernel_planar_bt601[][4] = {
20403b705cfSriastradh#include "exa_wm_src_affine.g7b"
20503b705cfSriastradh#include "exa_wm_src_sample_planar.g7b"
206fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt601.g7b"
207fe8aea9eSmrg#include "exa_wm_write.g7b"
208fe8aea9eSmrg};
209fe8aea9eSmrg
210fe8aea9eSmrgstatic const uint32_t ps_kernel_nv12_bt601[][4] = {
211fe8aea9eSmrg#include "exa_wm_src_affine.g7b"
212fe8aea9eSmrg#include "exa_wm_src_sample_nv12.g7b"
213fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt601.g7b"
214fe8aea9eSmrg#include "exa_wm_write.g7b"
215fe8aea9eSmrg};
216fe8aea9eSmrg
217fe8aea9eSmrgstatic const uint32_t ps_kernel_packed_bt709[][4] = {
218fe8aea9eSmrg#include "exa_wm_src_affine.g7b"
219fe8aea9eSmrg#include "exa_wm_src_sample_argb.g7b"
220fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt709.g7b"
221fe8aea9eSmrg#include "exa_wm_write.g7b"
222fe8aea9eSmrg};
223fe8aea9eSmrg
224fe8aea9eSmrgstatic const uint32_t ps_kernel_planar_bt709[][4] = {
225fe8aea9eSmrg#include "exa_wm_src_affine.g7b"
226fe8aea9eSmrg#include "exa_wm_src_sample_planar.g7b"
227fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt709.g7b"
228fe8aea9eSmrg#include "exa_wm_write.g7b"
229fe8aea9eSmrg};
230fe8aea9eSmrg
231fe8aea9eSmrgstatic const uint32_t ps_kernel_nv12_bt709[][4] = {
232fe8aea9eSmrg#include "exa_wm_src_affine.g7b"
233fe8aea9eSmrg#include "exa_wm_src_sample_nv12.g7b"
234fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt709.g7b"
235fe8aea9eSmrg#include "exa_wm_write.g7b"
236fe8aea9eSmrg};
237fe8aea9eSmrg
238fe8aea9eSmrgstatic const uint32_t ps_kernel_rgb[][4] = {
239fe8aea9eSmrg#include "exa_wm_src_affine.g7b"
240fe8aea9eSmrg#include "exa_wm_src_sample_argb.g7b"
24103b705cfSriastradh#include "exa_wm_write.g7b"
24203b705cfSriastradh};
24303b705cfSriastradh
24403b705cfSriastradh#define KERNEL(kernel_enum, kernel, num_surfaces) \
24503b705cfSriastradh    [GEN7_WM_KERNEL_##kernel_enum] = {#kernel_enum, kernel, sizeof(kernel), num_surfaces}
24603b705cfSriastradh#define NOKERNEL(kernel_enum, func, num_surfaces) \
24703b705cfSriastradh    [GEN7_WM_KERNEL_##kernel_enum] = {#kernel_enum, (void *)func, 0, num_surfaces}
24803b705cfSriastradhstatic const struct wm_kernel_info {
24903b705cfSriastradh	const char *name;
25003b705cfSriastradh	const void *data;
25103b705cfSriastradh	unsigned int size;
25203b705cfSriastradh	int num_surfaces;
253fe8aea9eSmrg} wm_kernels[GEN7_WM_KERNEL_COUNT] = {
25403b705cfSriastradh	NOKERNEL(NOMASK, brw_wm_kernel__affine, 2),
25503b705cfSriastradh	NOKERNEL(NOMASK_P, brw_wm_kernel__projective, 2),
25603b705cfSriastradh
25703b705cfSriastradh	NOKERNEL(MASK, brw_wm_kernel__affine_mask, 3),
25803b705cfSriastradh	NOKERNEL(MASK_P, brw_wm_kernel__projective_mask, 3),
25903b705cfSriastradh
26003b705cfSriastradh	NOKERNEL(MASKCA, brw_wm_kernel__affine_mask_ca, 3),
26103b705cfSriastradh	NOKERNEL(MASKCA_P, brw_wm_kernel__projective_mask_ca, 3),
26203b705cfSriastradh
26303b705cfSriastradh	NOKERNEL(MASKSA, brw_wm_kernel__affine_mask_sa, 3),
26403b705cfSriastradh	NOKERNEL(MASKSA_P, brw_wm_kernel__projective_mask_sa, 3),
26503b705cfSriastradh
26603b705cfSriastradh	NOKERNEL(OPACITY, brw_wm_kernel__affine_opacity, 2),
26703b705cfSriastradh	NOKERNEL(OPACITY_P, brw_wm_kernel__projective_opacity, 2),
26803b705cfSriastradh
269fe8aea9eSmrg	KERNEL(VIDEO_PLANAR_BT601, ps_kernel_planar_bt601, 7),
270fe8aea9eSmrg	KERNEL(VIDEO_NV12_BT601, ps_kernel_nv12_bt601, 7),
271fe8aea9eSmrg	KERNEL(VIDEO_PACKED_BT601, ps_kernel_packed_bt601, 2),
272fe8aea9eSmrg	KERNEL(VIDEO_PLANAR_BT709, ps_kernel_planar_bt709, 7),
273fe8aea9eSmrg	KERNEL(VIDEO_NV12_BT709, ps_kernel_nv12_bt709, 7),
274fe8aea9eSmrg	KERNEL(VIDEO_PACKED_BT709, ps_kernel_packed_bt709, 2),
275fe8aea9eSmrg	KERNEL(VIDEO_RGB, ps_kernel_rgb, 2),
27603b705cfSriastradh};
27703b705cfSriastradh#undef KERNEL
27803b705cfSriastradh
27903b705cfSriastradhstatic const struct blendinfo {
28003b705cfSriastradh	bool src_alpha;
28103b705cfSriastradh	uint32_t src_blend;
28203b705cfSriastradh	uint32_t dst_blend;
28303b705cfSriastradh} gen7_blend_op[] = {
28403b705cfSriastradh	/* Clear */	{0, GEN7_BLENDFACTOR_ZERO, GEN7_BLENDFACTOR_ZERO},
28503b705cfSriastradh	/* Src */	{0, GEN7_BLENDFACTOR_ONE, GEN7_BLENDFACTOR_ZERO},
28603b705cfSriastradh	/* Dst */	{0, GEN7_BLENDFACTOR_ZERO, GEN7_BLENDFACTOR_ONE},
28703b705cfSriastradh	/* Over */	{1, GEN7_BLENDFACTOR_ONE, GEN7_BLENDFACTOR_INV_SRC_ALPHA},
28803b705cfSriastradh	/* OverReverse */ {0, GEN7_BLENDFACTOR_INV_DST_ALPHA, GEN7_BLENDFACTOR_ONE},
28903b705cfSriastradh	/* In */	{0, GEN7_BLENDFACTOR_DST_ALPHA, GEN7_BLENDFACTOR_ZERO},
29003b705cfSriastradh	/* InReverse */	{1, GEN7_BLENDFACTOR_ZERO, GEN7_BLENDFACTOR_SRC_ALPHA},
29103b705cfSriastradh	/* Out */	{0, GEN7_BLENDFACTOR_INV_DST_ALPHA, GEN7_BLENDFACTOR_ZERO},
29203b705cfSriastradh	/* OutReverse */ {1, GEN7_BLENDFACTOR_ZERO, GEN7_BLENDFACTOR_INV_SRC_ALPHA},
29303b705cfSriastradh	/* Atop */	{1, GEN7_BLENDFACTOR_DST_ALPHA, GEN7_BLENDFACTOR_INV_SRC_ALPHA},
29403b705cfSriastradh	/* AtopReverse */ {1, GEN7_BLENDFACTOR_INV_DST_ALPHA, GEN7_BLENDFACTOR_SRC_ALPHA},
29503b705cfSriastradh	/* Xor */	{1, GEN7_BLENDFACTOR_INV_DST_ALPHA, GEN7_BLENDFACTOR_INV_SRC_ALPHA},
29603b705cfSriastradh	/* Add */	{0, GEN7_BLENDFACTOR_ONE, GEN7_BLENDFACTOR_ONE},
29703b705cfSriastradh};
29803b705cfSriastradh
29903b705cfSriastradh/**
30003b705cfSriastradh * Highest-valued BLENDFACTOR used in gen7_blend_op.
30103b705cfSriastradh *
30203b705cfSriastradh * This leaves out GEN7_BLENDFACTOR_INV_DST_COLOR,
30303b705cfSriastradh * GEN7_BLENDFACTOR_INV_CONST_{COLOR,ALPHA},
30403b705cfSriastradh * GEN7_BLENDFACTOR_INV_SRC1_{COLOR,ALPHA}
30503b705cfSriastradh */
30603b705cfSriastradh#define GEN7_BLENDFACTOR_COUNT (GEN7_BLENDFACTOR_INV_DST_ALPHA + 1)
30703b705cfSriastradh
30803b705cfSriastradh#define GEN7_BLEND_STATE_PADDED_SIZE	ALIGN(sizeof(struct gen7_blend_state), 64)
30903b705cfSriastradh
31003b705cfSriastradh#define BLEND_OFFSET(s, d) \
31103b705cfSriastradh	((d != GEN7_BLENDFACTOR_ZERO) << 15 | \
31203b705cfSriastradh	 (((s) * GEN7_BLENDFACTOR_COUNT + (d)) * GEN7_BLEND_STATE_PADDED_SIZE))
31303b705cfSriastradh
31403b705cfSriastradh#define NO_BLEND BLEND_OFFSET(GEN7_BLENDFACTOR_ONE, GEN7_BLENDFACTOR_ZERO)
31503b705cfSriastradh#define CLEAR BLEND_OFFSET(GEN7_BLENDFACTOR_ZERO, GEN7_BLENDFACTOR_ZERO)
31603b705cfSriastradh
31703b705cfSriastradh#define SAMPLER_OFFSET(sf, se, mf, me) \
31803b705cfSriastradh	((((((sf) * EXTEND_COUNT + (se)) * FILTER_COUNT + (mf)) * EXTEND_COUNT + (me)) + 2) * 2 * sizeof(struct gen7_sampler_state))
31903b705cfSriastradh
32003b705cfSriastradh#define VERTEX_2s2s 0
32103b705cfSriastradh
32203b705cfSriastradh#define COPY_SAMPLER 0
32303b705cfSriastradh#define COPY_VERTEX VERTEX_2s2s
32403b705cfSriastradh#define COPY_FLAGS(a) GEN7_SET_FLAGS(COPY_SAMPLER, (a) == GXcopy ? NO_BLEND : CLEAR, GEN7_WM_KERNEL_NOMASK, COPY_VERTEX)
32503b705cfSriastradh
32603b705cfSriastradh#define FILL_SAMPLER (2 * sizeof(struct gen7_sampler_state))
32703b705cfSriastradh#define FILL_VERTEX VERTEX_2s2s
32803b705cfSriastradh#define FILL_FLAGS(op, format) GEN7_SET_FLAGS(FILL_SAMPLER, gen7_get_blend((op), false, (format)), GEN7_WM_KERNEL_NOMASK, FILL_VERTEX)
32903b705cfSriastradh#define FILL_FLAGS_NOBLEND GEN7_SET_FLAGS(FILL_SAMPLER, NO_BLEND, GEN7_WM_KERNEL_NOMASK, FILL_VERTEX)
33003b705cfSriastradh
33103b705cfSriastradh#define GEN7_SAMPLER(f) (((f) >> 16) & 0xfff0)
33203b705cfSriastradh#define GEN7_BLEND(f) (((f) >> 0) & 0x7ff0)
33303b705cfSriastradh#define GEN7_READS_DST(f) (((f) >> 15) & 1)
33403b705cfSriastradh#define GEN7_KERNEL(f) (((f) >> 16) & 0xf)
33503b705cfSriastradh#define GEN7_VERTEX(f) (((f) >> 0) & 0xf)
33603b705cfSriastradh#define GEN7_SET_FLAGS(S, B, K, V)  (((S) | (K)) << 16 | ((B) | (V)))
33703b705cfSriastradh
33803b705cfSriastradh#define OUT_BATCH(v) batch_emit(sna, v)
33903b705cfSriastradh#define OUT_VERTEX(x,y) vertex_emit_2s(sna, x,y)
34003b705cfSriastradh#define OUT_VERTEX_F(v) vertex_emit(sna, v)
34103b705cfSriastradh
34203b705cfSriastradhstatic inline bool too_large(int width, int height)
34303b705cfSriastradh{
34403b705cfSriastradh	return width > GEN7_MAX_SIZE || height > GEN7_MAX_SIZE;
34503b705cfSriastradh}
34603b705cfSriastradh
34703b705cfSriastradhstatic uint32_t gen7_get_blend(int op,
34803b705cfSriastradh			       bool has_component_alpha,
34903b705cfSriastradh			       uint32_t dst_format)
35003b705cfSriastradh{
35103b705cfSriastradh	uint32_t src, dst;
35203b705cfSriastradh
35303b705cfSriastradh	src = gen7_blend_op[op].src_blend;
35403b705cfSriastradh	dst = gen7_blend_op[op].dst_blend;
35503b705cfSriastradh
35603b705cfSriastradh	/* If there's no dst alpha channel, adjust the blend op so that
35703b705cfSriastradh	 * we'll treat it always as 1.
35803b705cfSriastradh	 */
35903b705cfSriastradh	if (PICT_FORMAT_A(dst_format) == 0) {
36003b705cfSriastradh		if (src == GEN7_BLENDFACTOR_DST_ALPHA)
36103b705cfSriastradh			src = GEN7_BLENDFACTOR_ONE;
36203b705cfSriastradh		else if (src == GEN7_BLENDFACTOR_INV_DST_ALPHA)
36303b705cfSriastradh			src = GEN7_BLENDFACTOR_ZERO;
36403b705cfSriastradh	}
36503b705cfSriastradh
36603b705cfSriastradh	/* If the source alpha is being used, then we should only be in a
36703b705cfSriastradh	 * case where the source blend factor is 0, and the source blend
36803b705cfSriastradh	 * value is the mask channels multiplied by the source picture's alpha.
36903b705cfSriastradh	 */
37003b705cfSriastradh	if (has_component_alpha && gen7_blend_op[op].src_alpha) {
37103b705cfSriastradh		if (dst == GEN7_BLENDFACTOR_SRC_ALPHA)
37203b705cfSriastradh			dst = GEN7_BLENDFACTOR_SRC_COLOR;
37303b705cfSriastradh		else if (dst == GEN7_BLENDFACTOR_INV_SRC_ALPHA)
37403b705cfSriastradh			dst = GEN7_BLENDFACTOR_INV_SRC_COLOR;
37503b705cfSriastradh	}
37603b705cfSriastradh
37703b705cfSriastradh	DBG(("blend op=%d, dst=%x [A=%d] => src=%d, dst=%d => offset=%x\n",
37803b705cfSriastradh	     op, dst_format, PICT_FORMAT_A(dst_format),
37903b705cfSriastradh	     src, dst, (int)BLEND_OFFSET(src, dst)));
38003b705cfSriastradh	return BLEND_OFFSET(src, dst);
38103b705cfSriastradh}
38203b705cfSriastradh
38303b705cfSriastradhstatic uint32_t gen7_get_card_format(PictFormat format)
38403b705cfSriastradh{
38503b705cfSriastradh	switch (format) {
38603b705cfSriastradh	default:
38703b705cfSriastradh		return -1;
38803b705cfSriastradh	case PICT_a8r8g8b8:
38903b705cfSriastradh		return GEN7_SURFACEFORMAT_B8G8R8A8_UNORM;
39003b705cfSriastradh	case PICT_x8r8g8b8:
39103b705cfSriastradh		return GEN7_SURFACEFORMAT_B8G8R8X8_UNORM;
39203b705cfSriastradh	case PICT_a8b8g8r8:
39303b705cfSriastradh		return GEN7_SURFACEFORMAT_R8G8B8A8_UNORM;
39403b705cfSriastradh	case PICT_x8b8g8r8:
39503b705cfSriastradh		return GEN7_SURFACEFORMAT_R8G8B8X8_UNORM;
396fe8aea9eSmrg#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,6,99,900,0)
39703b705cfSriastradh	case PICT_a2r10g10b10:
39803b705cfSriastradh		return GEN7_SURFACEFORMAT_B10G10R10A2_UNORM;
39903b705cfSriastradh	case PICT_x2r10g10b10:
40003b705cfSriastradh		return GEN7_SURFACEFORMAT_B10G10R10X2_UNORM;
40142542f5fSchristos#endif
40203b705cfSriastradh	case PICT_r8g8b8:
40303b705cfSriastradh		return GEN7_SURFACEFORMAT_R8G8B8_UNORM;
40403b705cfSriastradh	case PICT_r5g6b5:
40503b705cfSriastradh		return GEN7_SURFACEFORMAT_B5G6R5_UNORM;
40603b705cfSriastradh	case PICT_a1r5g5b5:
40703b705cfSriastradh		return GEN7_SURFACEFORMAT_B5G5R5A1_UNORM;
40803b705cfSriastradh	case PICT_a8:
40903b705cfSriastradh		return GEN7_SURFACEFORMAT_A8_UNORM;
41003b705cfSriastradh	case PICT_a4r4g4b4:
41103b705cfSriastradh		return GEN7_SURFACEFORMAT_B4G4R4A4_UNORM;
41203b705cfSriastradh	}
41303b705cfSriastradh}
41403b705cfSriastradh
41503b705cfSriastradhstatic uint32_t gen7_get_dest_format(PictFormat format)
41603b705cfSriastradh{
41703b705cfSriastradh	switch (format) {
41803b705cfSriastradh	default:
41903b705cfSriastradh		return -1;
42003b705cfSriastradh	case PICT_a8r8g8b8:
42103b705cfSriastradh	case PICT_x8r8g8b8:
42203b705cfSriastradh		return GEN7_SURFACEFORMAT_B8G8R8A8_UNORM;
42303b705cfSriastradh	case PICT_a8b8g8r8:
42403b705cfSriastradh	case PICT_x8b8g8r8:
42503b705cfSriastradh		return GEN7_SURFACEFORMAT_R8G8B8A8_UNORM;
426fe8aea9eSmrg#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,6,99,900,0)
42703b705cfSriastradh	case PICT_a2r10g10b10:
42803b705cfSriastradh	case PICT_x2r10g10b10:
42903b705cfSriastradh		return GEN7_SURFACEFORMAT_B10G10R10A2_UNORM;
43042542f5fSchristos#endif
43103b705cfSriastradh	case PICT_r5g6b5:
43203b705cfSriastradh		return GEN7_SURFACEFORMAT_B5G6R5_UNORM;
43303b705cfSriastradh	case PICT_x1r5g5b5:
43403b705cfSriastradh	case PICT_a1r5g5b5:
43503b705cfSriastradh		return GEN7_SURFACEFORMAT_B5G5R5A1_UNORM;
43603b705cfSriastradh	case PICT_a8:
43703b705cfSriastradh		return GEN7_SURFACEFORMAT_A8_UNORM;
43803b705cfSriastradh	case PICT_a4r4g4b4:
43903b705cfSriastradh	case PICT_x4r4g4b4:
44003b705cfSriastradh		return GEN7_SURFACEFORMAT_B4G4R4A4_UNORM;
44103b705cfSriastradh	}
44203b705cfSriastradh}
44303b705cfSriastradh
44403b705cfSriastradhstatic bool gen7_check_dst_format(PictFormat format)
44503b705cfSriastradh{
44603b705cfSriastradh	if (gen7_get_dest_format(format) != -1)
44703b705cfSriastradh		return true;
44803b705cfSriastradh
44903b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
45003b705cfSriastradh	return false;
45103b705cfSriastradh}
45203b705cfSriastradh
45303b705cfSriastradhstatic bool gen7_check_format(uint32_t format)
45403b705cfSriastradh{
45503b705cfSriastradh	if (gen7_get_card_format(format) != -1)
45603b705cfSriastradh		return true;
45703b705cfSriastradh
45803b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
45903b705cfSriastradh	return false;
46003b705cfSriastradh}
46103b705cfSriastradh
46203b705cfSriastradhstatic uint32_t gen7_filter(uint32_t filter)
46303b705cfSriastradh{
46403b705cfSriastradh	switch (filter) {
46503b705cfSriastradh	default:
46603b705cfSriastradh		assert(0);
46703b705cfSriastradh	case PictFilterNearest:
46803b705cfSriastradh		return SAMPLER_FILTER_NEAREST;
46903b705cfSriastradh	case PictFilterBilinear:
47003b705cfSriastradh		return SAMPLER_FILTER_BILINEAR;
47103b705cfSriastradh	}
47203b705cfSriastradh}
47303b705cfSriastradh
47403b705cfSriastradhstatic uint32_t gen7_check_filter(PicturePtr picture)
47503b705cfSriastradh{
47603b705cfSriastradh	switch (picture->filter) {
47703b705cfSriastradh	case PictFilterNearest:
47803b705cfSriastradh	case PictFilterBilinear:
47903b705cfSriastradh		return true;
48003b705cfSriastradh	default:
48103b705cfSriastradh		return false;
48203b705cfSriastradh	}
48303b705cfSriastradh}
48403b705cfSriastradh
48503b705cfSriastradhstatic uint32_t gen7_repeat(uint32_t repeat)
48603b705cfSriastradh{
48703b705cfSriastradh	switch (repeat) {
48803b705cfSriastradh	default:
48903b705cfSriastradh		assert(0);
49003b705cfSriastradh	case RepeatNone:
49103b705cfSriastradh		return SAMPLER_EXTEND_NONE;
49203b705cfSriastradh	case RepeatNormal:
49303b705cfSriastradh		return SAMPLER_EXTEND_REPEAT;
49403b705cfSriastradh	case RepeatPad:
49503b705cfSriastradh		return SAMPLER_EXTEND_PAD;
49603b705cfSriastradh	case RepeatReflect:
49703b705cfSriastradh		return SAMPLER_EXTEND_REFLECT;
49803b705cfSriastradh	}
49903b705cfSriastradh}
50003b705cfSriastradh
50103b705cfSriastradhstatic bool gen7_check_repeat(PicturePtr picture)
50203b705cfSriastradh{
50303b705cfSriastradh	if (!picture->repeat)
50403b705cfSriastradh		return true;
50503b705cfSriastradh
50603b705cfSriastradh	switch (picture->repeatType) {
50703b705cfSriastradh	case RepeatNone:
50803b705cfSriastradh	case RepeatNormal:
50903b705cfSriastradh	case RepeatPad:
51003b705cfSriastradh	case RepeatReflect:
51103b705cfSriastradh		return true;
51203b705cfSriastradh	default:
51303b705cfSriastradh		return false;
51403b705cfSriastradh	}
51503b705cfSriastradh}
51603b705cfSriastradh
51703b705cfSriastradhstatic int
51803b705cfSriastradhgen7_choose_composite_kernel(int op, bool has_mask, bool is_ca, bool is_affine)
51903b705cfSriastradh{
52003b705cfSriastradh	int base;
52103b705cfSriastradh
52203b705cfSriastradh	if (has_mask) {
52303b705cfSriastradh		if (is_ca) {
52403b705cfSriastradh			if (gen7_blend_op[op].src_alpha)
52503b705cfSriastradh				base = GEN7_WM_KERNEL_MASKSA;
52603b705cfSriastradh			else
52703b705cfSriastradh				base = GEN7_WM_KERNEL_MASKCA;
52803b705cfSriastradh		} else
52903b705cfSriastradh			base = GEN7_WM_KERNEL_MASK;
53003b705cfSriastradh	} else
53103b705cfSriastradh		base = GEN7_WM_KERNEL_NOMASK;
53203b705cfSriastradh
53303b705cfSriastradh	return base + !is_affine;
53403b705cfSriastradh}
53503b705cfSriastradh
53603b705cfSriastradhstatic void
53703b705cfSriastradhgen7_emit_urb(struct sna *sna)
53803b705cfSriastradh{
53903b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_PS | (2 - 2));
54003b705cfSriastradh	OUT_BATCH(sna->render_state.gen7.info->urb.push_ps_size);
54103b705cfSriastradh
54203b705cfSriastradh	/* num of VS entries must be divisible by 8 if size < 9 */
54303b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_URB_VS | (2 - 2));
54403b705cfSriastradh	OUT_BATCH((sna->render_state.gen7.info->urb.max_vs_entries << GEN7_URB_ENTRY_NUMBER_SHIFT) |
54503b705cfSriastradh		  (2 - 1) << GEN7_URB_ENTRY_SIZE_SHIFT |
54603b705cfSriastradh		  (1 << GEN7_URB_STARTING_ADDRESS_SHIFT));
54703b705cfSriastradh
54803b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_URB_HS | (2 - 2));
54903b705cfSriastradh	OUT_BATCH((0 << GEN7_URB_ENTRY_SIZE_SHIFT) |
55003b705cfSriastradh		  (2 << GEN7_URB_STARTING_ADDRESS_SHIFT));
55103b705cfSriastradh
55203b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_URB_DS | (2 - 2));
55303b705cfSriastradh	OUT_BATCH((0 << GEN7_URB_ENTRY_SIZE_SHIFT) |
55403b705cfSriastradh		  (2 << GEN7_URB_STARTING_ADDRESS_SHIFT));
55503b705cfSriastradh
55603b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_URB_GS | (2 - 2));
55703b705cfSriastradh	OUT_BATCH((0 << GEN7_URB_ENTRY_SIZE_SHIFT) |
55803b705cfSriastradh		  (1 << GEN7_URB_STARTING_ADDRESS_SHIFT));
55903b705cfSriastradh}
56003b705cfSriastradh
56103b705cfSriastradhstatic void
56203b705cfSriastradhgen7_emit_state_base_address(struct sna *sna)
56303b705cfSriastradh{
56442542f5fSchristos	uint32_t mocs = sna->render_state.gen7.info->mocs << 8;
56503b705cfSriastradh
56603b705cfSriastradh	OUT_BATCH(GEN7_STATE_BASE_ADDRESS | (10 - 2));
56703b705cfSriastradh	OUT_BATCH(0); /* general */
56803b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* surface */
56903b705cfSriastradh				 sna->kgem.nbatch,
57003b705cfSriastradh				 NULL,
57103b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
57203b705cfSriastradh				 BASE_ADDRESS_MODIFY));
57303b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* dynamic */
57403b705cfSriastradh				 sna->kgem.nbatch,
57503b705cfSriastradh				 sna->render_state.gen7.general_bo,
57603b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
57703b705cfSriastradh				 mocs | BASE_ADDRESS_MODIFY));
57803b705cfSriastradh	OUT_BATCH(0); /* indirect */
57903b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* instruction */
58003b705cfSriastradh				 sna->kgem.nbatch,
58103b705cfSriastradh				 sna->render_state.gen7.general_bo,
58203b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
58303b705cfSriastradh				 mocs | BASE_ADDRESS_MODIFY));
58403b705cfSriastradh
58503b705cfSriastradh	/* upper bounds, disable */
58603b705cfSriastradh	OUT_BATCH(0);
58703b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
58803b705cfSriastradh	OUT_BATCH(0);
58903b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
59003b705cfSriastradh}
59103b705cfSriastradh
59203b705cfSriastradhstatic void
59303b705cfSriastradhgen7_disable_vs(struct sna *sna)
59403b705cfSriastradh{
59503b705cfSriastradh	/* For future reference:
59603b705cfSriastradh	 * A PIPE_CONTROL with post-sync op set to 1 and a depth stall needs
59703b705cfSriastradh	 * to be emitted just prior to change VS state, i.e. 3DSTATE_VS,
59803b705cfSriastradh	 * 3DSTATE_URB_VS, 3DSTATE_CONSTANT_VS,
59903b705cfSriastradh	 * 3DSTATE_BINDING_TABLE_POINTER_VS, 3DSTATE_SAMPLER_STATE_POINTER_VS.
60003b705cfSriastradh	 *
60103b705cfSriastradh	 * Here we saved by the full-flush incurred when emitting
60203b705cfSriastradh	 * the batchbuffer.
60303b705cfSriastradh	 */
60403b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_VS | (6 - 2));
60503b705cfSriastradh	OUT_BATCH(0); /* no VS kernel */
60603b705cfSriastradh	OUT_BATCH(0);
60703b705cfSriastradh	OUT_BATCH(0);
60803b705cfSriastradh	OUT_BATCH(0);
60903b705cfSriastradh	OUT_BATCH(0); /* pass-through */
61003b705cfSriastradh
61103b705cfSriastradh#if 0
61203b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CONSTANT_VS | (7 - 2));
61303b705cfSriastradh	OUT_BATCH(0);
61403b705cfSriastradh	OUT_BATCH(0);
61503b705cfSriastradh	OUT_BATCH(0);
61603b705cfSriastradh	OUT_BATCH(0);
61703b705cfSriastradh	OUT_BATCH(0);
61803b705cfSriastradh	OUT_BATCH(0);
61903b705cfSriastradh
62003b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_VS | (2 - 2));
62103b705cfSriastradh	OUT_BATCH(0);
62203b705cfSriastradh
62303b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_VS | (2 - 2));
62403b705cfSriastradh	OUT_BATCH(0);
62503b705cfSriastradh#endif
62603b705cfSriastradh}
62703b705cfSriastradh
62803b705cfSriastradhstatic void
62903b705cfSriastradhgen7_disable_hs(struct sna *sna)
63003b705cfSriastradh{
63103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_HS | (7 - 2));
63203b705cfSriastradh	OUT_BATCH(0); /* no HS kernel */
63303b705cfSriastradh	OUT_BATCH(0);
63403b705cfSriastradh	OUT_BATCH(0);
63503b705cfSriastradh	OUT_BATCH(0);
63603b705cfSriastradh	OUT_BATCH(0);
63703b705cfSriastradh	OUT_BATCH(0); /* pass-through */
63803b705cfSriastradh
63903b705cfSriastradh#if 0
64003b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CONSTANT_HS | (7 - 2));
64103b705cfSriastradh	OUT_BATCH(0);
64203b705cfSriastradh	OUT_BATCH(0);
64303b705cfSriastradh	OUT_BATCH(0);
64403b705cfSriastradh	OUT_BATCH(0);
64503b705cfSriastradh	OUT_BATCH(0);
64603b705cfSriastradh	OUT_BATCH(0);
64703b705cfSriastradh
64803b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_HS | (2 - 2));
64903b705cfSriastradh	OUT_BATCH(0);
65003b705cfSriastradh
65103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_HS | (2 - 2));
65203b705cfSriastradh	OUT_BATCH(0);
65303b705cfSriastradh#endif
65403b705cfSriastradh}
65503b705cfSriastradh
65603b705cfSriastradhstatic void
65703b705cfSriastradhgen7_disable_te(struct sna *sna)
65803b705cfSriastradh{
65903b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_TE | (4 - 2));
66003b705cfSriastradh	OUT_BATCH(0);
66103b705cfSriastradh	OUT_BATCH(0);
66203b705cfSriastradh	OUT_BATCH(0);
66303b705cfSriastradh}
66403b705cfSriastradh
66503b705cfSriastradhstatic void
66603b705cfSriastradhgen7_disable_ds(struct sna *sna)
66703b705cfSriastradh{
66803b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_DS | (6 - 2));
66903b705cfSriastradh	OUT_BATCH(0);
67003b705cfSriastradh	OUT_BATCH(0);
67103b705cfSriastradh	OUT_BATCH(0);
67203b705cfSriastradh	OUT_BATCH(0);
67303b705cfSriastradh	OUT_BATCH(0);
67403b705cfSriastradh
67503b705cfSriastradh#if 0
67603b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CONSTANT_DS | (7 - 2));
67703b705cfSriastradh	OUT_BATCH(0);
67803b705cfSriastradh	OUT_BATCH(0);
67903b705cfSriastradh	OUT_BATCH(0);
68003b705cfSriastradh	OUT_BATCH(0);
68103b705cfSriastradh	OUT_BATCH(0);
68203b705cfSriastradh	OUT_BATCH(0);
68303b705cfSriastradh
68403b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_DS | (2 - 2));
68503b705cfSriastradh	OUT_BATCH(0);
68603b705cfSriastradh
68703b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_DS | (2 - 2));
68803b705cfSriastradh	OUT_BATCH(0);
68903b705cfSriastradh#endif
69003b705cfSriastradh}
69103b705cfSriastradh
69203b705cfSriastradhstatic void
69303b705cfSriastradhgen7_disable_gs(struct sna *sna)
69403b705cfSriastradh{
69503b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_GS | (7 - 2));
69603b705cfSriastradh	OUT_BATCH(0); /* no GS kernel */
69703b705cfSriastradh	OUT_BATCH(0);
69803b705cfSriastradh	OUT_BATCH(0);
69903b705cfSriastradh	OUT_BATCH(0);
70003b705cfSriastradh	OUT_BATCH(0);
70103b705cfSriastradh	OUT_BATCH(0); /* pass-through */
70203b705cfSriastradh
70303b705cfSriastradh#if 0
70403b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CONSTANT_GS | (7 - 2));
70503b705cfSriastradh	OUT_BATCH(0);
70603b705cfSriastradh	OUT_BATCH(0);
70703b705cfSriastradh	OUT_BATCH(0);
70803b705cfSriastradh	OUT_BATCH(0);
70903b705cfSriastradh	OUT_BATCH(0);
71003b705cfSriastradh	OUT_BATCH(0);
71103b705cfSriastradh
71203b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_GS | (2 - 2));
71303b705cfSriastradh	OUT_BATCH(0);
71403b705cfSriastradh
71503b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_GS | (2 - 2));
71603b705cfSriastradh	OUT_BATCH(0);
71703b705cfSriastradh#endif
71803b705cfSriastradh}
71903b705cfSriastradh
72003b705cfSriastradhstatic void
72103b705cfSriastradhgen7_disable_streamout(struct sna *sna)
72203b705cfSriastradh{
72303b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_STREAMOUT | (3 - 2));
72403b705cfSriastradh	OUT_BATCH(0);
72503b705cfSriastradh	OUT_BATCH(0);
72603b705cfSriastradh}
72703b705cfSriastradh
72803b705cfSriastradhstatic void
72903b705cfSriastradhgen7_emit_sf_invariant(struct sna *sna)
73003b705cfSriastradh{
73103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SF | (7 - 2));
73203b705cfSriastradh	OUT_BATCH(0);
73303b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SF_CULL_NONE);
73403b705cfSriastradh	OUT_BATCH(2 << GEN7_3DSTATE_SF_TRIFAN_PROVOKE_SHIFT);
73503b705cfSriastradh	OUT_BATCH(0);
73603b705cfSriastradh	OUT_BATCH(0);
73703b705cfSriastradh	OUT_BATCH(0);
73803b705cfSriastradh}
73903b705cfSriastradh
74003b705cfSriastradhstatic void
74103b705cfSriastradhgen7_emit_cc_invariant(struct sna *sna)
74203b705cfSriastradh{
74303b705cfSriastradh#if 0 /* unused, no change */
74403b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CC_STATE_POINTERS | (2 - 2));
74503b705cfSriastradh	OUT_BATCH(0);
74603b705cfSriastradh
74703b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS | (2 - 2));
74803b705cfSriastradh	OUT_BATCH(0);
74903b705cfSriastradh#endif
75003b705cfSriastradh
75103b705cfSriastradh	/* XXX clear to be safe */
75203b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_CC | (2 - 2));
75303b705cfSriastradh	OUT_BATCH(0);
75403b705cfSriastradh}
75503b705cfSriastradh
75603b705cfSriastradhstatic void
75703b705cfSriastradhgen7_disable_clip(struct sna *sna)
75803b705cfSriastradh{
75903b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CLIP | (4 - 2));
76003b705cfSriastradh	OUT_BATCH(0);
76103b705cfSriastradh	OUT_BATCH(0); /* pass-through */
76203b705cfSriastradh	OUT_BATCH(0);
76303b705cfSriastradh
76403b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CL | (2 - 2));
76503b705cfSriastradh	OUT_BATCH(0);
76603b705cfSriastradh}
76703b705cfSriastradh
76803b705cfSriastradhstatic void
76903b705cfSriastradhgen7_emit_wm_invariant(struct sna *sna)
77003b705cfSriastradh{
77103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_WM | (3 - 2));
77203b705cfSriastradh	OUT_BATCH(GEN7_WM_DISPATCH_ENABLE |
77303b705cfSriastradh		  GEN7_WM_PERSPECTIVE_PIXEL_BARYCENTRIC);
77403b705cfSriastradh	OUT_BATCH(0);
77503b705cfSriastradh
77603b705cfSriastradh#if 0
77703b705cfSriastradh	/* XXX length bias of 7 in old spec? */
77803b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CONSTANT_PS | (7 - 2));
77903b705cfSriastradh	OUT_BATCH(0);
78003b705cfSriastradh	OUT_BATCH(0);
78103b705cfSriastradh	OUT_BATCH(0);
78203b705cfSriastradh	OUT_BATCH(0);
78303b705cfSriastradh	OUT_BATCH(0);
78403b705cfSriastradh	OUT_BATCH(0);
78503b705cfSriastradh#endif
78603b705cfSriastradh}
78703b705cfSriastradh
78803b705cfSriastradhstatic void
78903b705cfSriastradhgen7_emit_null_depth_buffer(struct sna *sna)
79003b705cfSriastradh{
79103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_DEPTH_BUFFER | (7 - 2));
79203b705cfSriastradh	OUT_BATCH(GEN7_SURFACE_NULL << GEN7_3DSTATE_DEPTH_BUFFER_TYPE_SHIFT |
79303b705cfSriastradh		  GEN7_DEPTHFORMAT_D32_FLOAT << GEN7_3DSTATE_DEPTH_BUFFER_FORMAT_SHIFT);
79403b705cfSriastradh	OUT_BATCH(0); /* disable depth, stencil and hiz */
79503b705cfSriastradh	OUT_BATCH(0);
79603b705cfSriastradh	OUT_BATCH(0);
79703b705cfSriastradh	OUT_BATCH(0);
79803b705cfSriastradh	OUT_BATCH(0);
79903b705cfSriastradh
80003b705cfSriastradh#if 0
80103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_CLEAR_PARAMS | (3 - 2));
80203b705cfSriastradh	OUT_BATCH(0);
80303b705cfSriastradh	OUT_BATCH(0);
80403b705cfSriastradh#endif
80503b705cfSriastradh}
80603b705cfSriastradh
80703b705cfSriastradhstatic void
80803b705cfSriastradhgen7_emit_invariant(struct sna *sna)
80903b705cfSriastradh{
81003b705cfSriastradh	OUT_BATCH(GEN7_PIPELINE_SELECT | PIPELINE_SELECT_3D);
81103b705cfSriastradh
81203b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_MULTISAMPLE | (4 - 2));
81303b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_MULTISAMPLE_PIXEL_LOCATION_CENTER |
81403b705cfSriastradh		  GEN7_3DSTATE_MULTISAMPLE_NUMSAMPLES_1); /* 1 sample/pixel */
81503b705cfSriastradh	OUT_BATCH(0);
81603b705cfSriastradh	OUT_BATCH(0);
81703b705cfSriastradh
81803b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SAMPLE_MASK | (2 - 2));
81903b705cfSriastradh	OUT_BATCH(1);
82003b705cfSriastradh
82103b705cfSriastradh	gen7_emit_urb(sna);
82203b705cfSriastradh
82303b705cfSriastradh	gen7_emit_state_base_address(sna);
82403b705cfSriastradh
82503b705cfSriastradh	gen7_disable_vs(sna);
82603b705cfSriastradh	gen7_disable_hs(sna);
82703b705cfSriastradh	gen7_disable_te(sna);
82803b705cfSriastradh	gen7_disable_ds(sna);
82903b705cfSriastradh	gen7_disable_gs(sna);
83003b705cfSriastradh	gen7_disable_clip(sna);
83103b705cfSriastradh	gen7_emit_sf_invariant(sna);
83203b705cfSriastradh	gen7_emit_wm_invariant(sna);
83303b705cfSriastradh	gen7_emit_cc_invariant(sna);
83403b705cfSriastradh	gen7_disable_streamout(sna);
83503b705cfSriastradh	gen7_emit_null_depth_buffer(sna);
83603b705cfSriastradh
83703b705cfSriastradh	sna->render_state.gen7.needs_invariant = false;
83803b705cfSriastradh}
83903b705cfSriastradh
84003b705cfSriastradhstatic void
84103b705cfSriastradhgen7_emit_cc(struct sna *sna, uint32_t blend_offset)
84203b705cfSriastradh{
84303b705cfSriastradh	struct gen7_render_state *render = &sna->render_state.gen7;
84403b705cfSriastradh
84503b705cfSriastradh	if (render->blend == blend_offset)
84603b705cfSriastradh		return;
84703b705cfSriastradh
84803b705cfSriastradh	DBG(("%s: blend = %x\n", __FUNCTION__, blend_offset));
84903b705cfSriastradh
850fe8aea9eSmrg	/* XXX can have up to 8 blend states preload, selectable via
85103b705cfSriastradh	 * Render Target Index. What other side-effects of Render Target Index?
85203b705cfSriastradh	 */
85303b705cfSriastradh
85403b705cfSriastradh	assert (is_aligned(render->cc_blend + blend_offset, 64));
85503b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_BLEND_STATE_POINTERS | (2 - 2));
85603b705cfSriastradh	OUT_BATCH((render->cc_blend + blend_offset) | 1);
85703b705cfSriastradh
85803b705cfSriastradh	render->blend = blend_offset;
85903b705cfSriastradh}
86003b705cfSriastradh
86103b705cfSriastradhstatic void
86203b705cfSriastradhgen7_emit_sampler(struct sna *sna, uint32_t state)
86303b705cfSriastradh{
86403b705cfSriastradh	if (sna->render_state.gen7.samplers == state)
86503b705cfSriastradh		return;
86603b705cfSriastradh
86703b705cfSriastradh	sna->render_state.gen7.samplers = state;
86803b705cfSriastradh
86903b705cfSriastradh	DBG(("%s: sampler = %x\n", __FUNCTION__, state));
87003b705cfSriastradh
87103b705cfSriastradh	assert (is_aligned(sna->render_state.gen7.wm_state + state, 32));
87203b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_PS | (2 - 2));
87303b705cfSriastradh	OUT_BATCH(sna->render_state.gen7.wm_state + state);
87403b705cfSriastradh}
87503b705cfSriastradh
87603b705cfSriastradhstatic void
87703b705cfSriastradhgen7_emit_sf(struct sna *sna, bool has_mask)
87803b705cfSriastradh{
87903b705cfSriastradh	int num_sf_outputs = has_mask ? 2 : 1;
88003b705cfSriastradh
88103b705cfSriastradh	if (sna->render_state.gen7.num_sf_outputs == num_sf_outputs)
88203b705cfSriastradh		return;
88303b705cfSriastradh
88403b705cfSriastradh	DBG(("%s: num_sf_outputs=%d, read_length=%d, read_offset=%d\n",
88503b705cfSriastradh	     __FUNCTION__, num_sf_outputs, 1, 0));
88603b705cfSriastradh
88703b705cfSriastradh	sna->render_state.gen7.num_sf_outputs = num_sf_outputs;
88803b705cfSriastradh
88903b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_SBE | (14 - 2));
89003b705cfSriastradh	OUT_BATCH(num_sf_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT |
89103b705cfSriastradh		  1 << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
89203b705cfSriastradh		  1 << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT);
89303b705cfSriastradh	OUT_BATCH(0);
89403b705cfSriastradh	OUT_BATCH(0); /* dw4 */
89503b705cfSriastradh	OUT_BATCH(0);
89603b705cfSriastradh	OUT_BATCH(0);
89703b705cfSriastradh	OUT_BATCH(0);
89803b705cfSriastradh	OUT_BATCH(0); /* dw8 */
89903b705cfSriastradh	OUT_BATCH(0);
90003b705cfSriastradh	OUT_BATCH(0);
90103b705cfSriastradh	OUT_BATCH(0);
90203b705cfSriastradh	OUT_BATCH(0); /* dw12 */
90303b705cfSriastradh	OUT_BATCH(0);
90403b705cfSriastradh	OUT_BATCH(0);
90503b705cfSriastradh}
90603b705cfSriastradh
90703b705cfSriastradhstatic void
90803b705cfSriastradhgen7_emit_wm(struct sna *sna, int kernel)
90903b705cfSriastradh{
91003b705cfSriastradh	const uint32_t *kernels;
91103b705cfSriastradh
91203b705cfSriastradh	if (sna->render_state.gen7.kernel == kernel)
91303b705cfSriastradh		return;
91403b705cfSriastradh
91503b705cfSriastradh	sna->render_state.gen7.kernel = kernel;
91603b705cfSriastradh	kernels = sna->render_state.gen7.wm_kernel[kernel];
91703b705cfSriastradh
91803b705cfSriastradh	DBG(("%s: switching to %s, num_surfaces=%d (8-wide? %d, 16-wide? %d, 32-wide? %d)\n",
91903b705cfSriastradh	     __FUNCTION__,
92003b705cfSriastradh	     wm_kernels[kernel].name,
92103b705cfSriastradh	     wm_kernels[kernel].num_surfaces,
92203b705cfSriastradh	     kernels[0], kernels[1], kernels[2]));
92303b705cfSriastradh
92403b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_PS | (8 - 2));
92503b705cfSriastradh	OUT_BATCH(kernels[0] ?: kernels[1] ?: kernels[2]);
92603b705cfSriastradh	OUT_BATCH(1 << GEN7_PS_SAMPLER_COUNT_SHIFT |
92703b705cfSriastradh		  wm_kernels[kernel].num_surfaces << GEN7_PS_BINDING_TABLE_ENTRY_COUNT_SHIFT);
92803b705cfSriastradh	OUT_BATCH(0); /* scratch address */
92903b705cfSriastradh	OUT_BATCH(sna->render_state.gen7.info->max_wm_threads |
93003b705cfSriastradh		  (kernels[0] ? GEN7_PS_8_DISPATCH_ENABLE : 0) |
93103b705cfSriastradh		  (kernels[1] ? GEN7_PS_16_DISPATCH_ENABLE : 0) |
93203b705cfSriastradh		  (kernels[2] ? GEN7_PS_32_DISPATCH_ENABLE : 0) |
93303b705cfSriastradh		  GEN7_PS_ATTRIBUTE_ENABLE);
93403b705cfSriastradh	OUT_BATCH((kernels[0] ? 4 : kernels[1] ? 6 : 8) << GEN7_PS_DISPATCH_START_GRF_SHIFT_0 |
93503b705cfSriastradh		  8 << GEN7_PS_DISPATCH_START_GRF_SHIFT_1 |
93603b705cfSriastradh		  6 << GEN7_PS_DISPATCH_START_GRF_SHIFT_2);
93703b705cfSriastradh	OUT_BATCH(kernels[2]);
93803b705cfSriastradh	OUT_BATCH(kernels[1]);
93903b705cfSriastradh}
94003b705cfSriastradh
94103b705cfSriastradhstatic bool
94203b705cfSriastradhgen7_emit_binding_table(struct sna *sna, uint16_t offset)
94303b705cfSriastradh{
94403b705cfSriastradh	if (sna->render_state.gen7.surface_table == offset)
94503b705cfSriastradh		return false;
94603b705cfSriastradh
94703b705cfSriastradh	/* Binding table pointers */
94803b705cfSriastradh	assert(is_aligned(4*offset, 32));
94903b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_PS | (2 - 2));
95003b705cfSriastradh	OUT_BATCH(offset*4);
95103b705cfSriastradh
95203b705cfSriastradh	sna->render_state.gen7.surface_table = offset;
95303b705cfSriastradh	return true;
95403b705cfSriastradh}
95503b705cfSriastradh
95603b705cfSriastradhstatic bool
95703b705cfSriastradhgen7_emit_drawing_rectangle(struct sna *sna,
95803b705cfSriastradh			    const struct sna_composite_op *op)
95903b705cfSriastradh{
96003b705cfSriastradh	uint32_t limit = (op->dst.height - 1) << 16 | (op->dst.width - 1);
96103b705cfSriastradh	uint32_t offset = (uint16_t)op->dst.y << 16 | (uint16_t)op->dst.x;
96203b705cfSriastradh
96313496ba1Ssnj	assert(!too_large(abs(op->dst.x), abs(op->dst.y)));
96403b705cfSriastradh	assert(!too_large(op->dst.width, op->dst.height));
96503b705cfSriastradh
96603b705cfSriastradh	if (sna->render_state.gen7.drawrect_limit == limit &&
96703b705cfSriastradh	    sna->render_state.gen7.drawrect_offset == offset)
96803b705cfSriastradh		return true;
96903b705cfSriastradh
97003b705cfSriastradh	sna->render_state.gen7.drawrect_offset = offset;
97103b705cfSriastradh	sna->render_state.gen7.drawrect_limit = limit;
97203b705cfSriastradh
97303b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
97403b705cfSriastradh	OUT_BATCH(0);
97503b705cfSriastradh	OUT_BATCH(limit);
97603b705cfSriastradh	OUT_BATCH(offset);
97703b705cfSriastradh	return false;
97803b705cfSriastradh}
97903b705cfSriastradh
98003b705cfSriastradhstatic void
98103b705cfSriastradhgen7_emit_vertex_elements(struct sna *sna,
98203b705cfSriastradh			  const struct sna_composite_op *op)
98303b705cfSriastradh{
98403b705cfSriastradh	/*
98503b705cfSriastradh	 * vertex data in vertex buffer
98603b705cfSriastradh	 *    position: (x, y)
98703b705cfSriastradh	 *    texture coordinate 0: (u0, v0) if (is_affine is true) else (u0, v0, w0)
98803b705cfSriastradh	 *    texture coordinate 1 if (has_mask is true): same as above
98903b705cfSriastradh	 */
99003b705cfSriastradh	struct gen7_render_state *render = &sna->render_state.gen7;
99103b705cfSriastradh	uint32_t src_format, dw;
99203b705cfSriastradh	int id = GEN7_VERTEX(op->u.gen7.flags);
99303b705cfSriastradh	bool has_mask;
99403b705cfSriastradh
99503b705cfSriastradh	DBG(("%s: setup id=%d\n", __FUNCTION__, id));
99603b705cfSriastradh
99703b705cfSriastradh	if (render->ve_id == id)
99803b705cfSriastradh		return;
99903b705cfSriastradh	render->ve_id = id;
100003b705cfSriastradh
100103b705cfSriastradh	/* The VUE layout
100203b705cfSriastradh	 *    dword 0-3: pad (0.0, 0.0, 0.0. 0.0)
100303b705cfSriastradh	 *    dword 4-7: position (x, y, 1.0, 1.0),
100403b705cfSriastradh	 *    dword 8-11: texture coordinate 0 (u0, v0, w0, 1.0)
100503b705cfSriastradh	 *    dword 12-15: texture coordinate 1 (u1, v1, w1, 1.0)
100603b705cfSriastradh	 *
100703b705cfSriastradh	 * dword 4-15 are fetched from vertex buffer
100803b705cfSriastradh	 */
100903b705cfSriastradh	has_mask = (id >> 2) != 0;
101003b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_VERTEX_ELEMENTS |
101103b705cfSriastradh		((2 * (3 + has_mask)) + 1 - 2));
101203b705cfSriastradh
101303b705cfSriastradh	OUT_BATCH(id << GEN7_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN7_VE0_VALID |
101403b705cfSriastradh		  GEN7_SURFACEFORMAT_R32G32B32A32_FLOAT << GEN7_VE0_FORMAT_SHIFT |
101503b705cfSriastradh		  0 << GEN7_VE0_OFFSET_SHIFT);
101603b705cfSriastradh	OUT_BATCH(GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_0_SHIFT |
101703b705cfSriastradh		  GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_1_SHIFT |
101803b705cfSriastradh		  GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT |
101903b705cfSriastradh		  GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_3_SHIFT);
102003b705cfSriastradh
102103b705cfSriastradh	/* x,y */
102203b705cfSriastradh	OUT_BATCH(id << GEN7_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN7_VE0_VALID |
102303b705cfSriastradh		  GEN7_SURFACEFORMAT_R16G16_SSCALED << GEN7_VE0_FORMAT_SHIFT |
102403b705cfSriastradh		  0 << GEN7_VE0_OFFSET_SHIFT);
102503b705cfSriastradh	OUT_BATCH(GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT |
102603b705cfSriastradh		  GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_1_SHIFT |
102703b705cfSriastradh		  GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT |
102803b705cfSriastradh		  GEN7_VFCOMPONENT_STORE_1_FLT << GEN7_VE1_VFCOMPONENT_3_SHIFT);
102903b705cfSriastradh
103003b705cfSriastradh	/* u0, v0, w0 */
103103b705cfSriastradh	DBG(("%s: first channel %d floats, offset=4b\n", __FUNCTION__, id & 3));
103203b705cfSriastradh	dw = GEN7_VFCOMPONENT_STORE_1_FLT << GEN7_VE1_VFCOMPONENT_3_SHIFT;
103303b705cfSriastradh	switch (id & 3) {
103403b705cfSriastradh	default:
103503b705cfSriastradh		assert(0);
103603b705cfSriastradh	case 0:
103703b705cfSriastradh		src_format = GEN7_SURFACEFORMAT_R16G16_SSCALED;
103803b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
103903b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_1_SHIFT;
104003b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT;
104103b705cfSriastradh		break;
104203b705cfSriastradh	case 1:
104303b705cfSriastradh		src_format = GEN7_SURFACEFORMAT_R32_FLOAT;
104403b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
104503b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_1_SHIFT;
104603b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT;
104703b705cfSriastradh		break;
104803b705cfSriastradh	case 2:
104903b705cfSriastradh		src_format = GEN7_SURFACEFORMAT_R32G32_FLOAT;
105003b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
105103b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_1_SHIFT;
105203b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT;
105303b705cfSriastradh		break;
105403b705cfSriastradh	case 3:
105503b705cfSriastradh		src_format = GEN7_SURFACEFORMAT_R32G32B32_FLOAT;
105603b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
105703b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_1_SHIFT;
105803b705cfSriastradh		dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_2_SHIFT;
105903b705cfSriastradh		break;
106003b705cfSriastradh	}
106103b705cfSriastradh	OUT_BATCH(id << GEN7_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN7_VE0_VALID |
106203b705cfSriastradh		  src_format << GEN7_VE0_FORMAT_SHIFT |
106303b705cfSriastradh		  4 << GEN7_VE0_OFFSET_SHIFT);
106403b705cfSriastradh	OUT_BATCH(dw);
106503b705cfSriastradh
106603b705cfSriastradh	/* u1, v1, w1 */
106703b705cfSriastradh	if (has_mask) {
106803b705cfSriastradh		unsigned offset = 4 + ((id & 3) ?: 1) * sizeof(float);
106903b705cfSriastradh		DBG(("%s: second channel %d floats, offset=%db\n", __FUNCTION__, id >> 2, offset));
107003b705cfSriastradh		dw = GEN7_VFCOMPONENT_STORE_1_FLT << GEN7_VE1_VFCOMPONENT_3_SHIFT;
107103b705cfSriastradh		switch (id >> 2) {
107203b705cfSriastradh		case 1:
107303b705cfSriastradh			src_format = GEN7_SURFACEFORMAT_R32_FLOAT;
107403b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
107503b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_1_SHIFT;
107603b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT;
107703b705cfSriastradh			break;
107803b705cfSriastradh		default:
107903b705cfSriastradh			assert(0);
108003b705cfSriastradh		case 2:
108103b705cfSriastradh			src_format = GEN7_SURFACEFORMAT_R32G32_FLOAT;
108203b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
108303b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_1_SHIFT;
108403b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_0 << GEN7_VE1_VFCOMPONENT_2_SHIFT;
108503b705cfSriastradh			break;
108603b705cfSriastradh		case 3:
108703b705cfSriastradh			src_format = GEN7_SURFACEFORMAT_R32G32B32_FLOAT;
108803b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_0_SHIFT;
108903b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_1_SHIFT;
109003b705cfSriastradh			dw |= GEN7_VFCOMPONENT_STORE_SRC << GEN7_VE1_VFCOMPONENT_2_SHIFT;
109103b705cfSriastradh			break;
109203b705cfSriastradh		}
109303b705cfSriastradh		OUT_BATCH(id << GEN7_VE0_VERTEX_BUFFER_INDEX_SHIFT | GEN7_VE0_VALID |
109403b705cfSriastradh			  src_format << GEN7_VE0_FORMAT_SHIFT |
109503b705cfSriastradh			  offset << GEN7_VE0_OFFSET_SHIFT);
109603b705cfSriastradh		OUT_BATCH(dw);
109703b705cfSriastradh	}
109803b705cfSriastradh}
109903b705cfSriastradh
110003b705cfSriastradhinline static void
110103b705cfSriastradhgen7_emit_pipe_invalidate(struct sna *sna)
110203b705cfSriastradh{
110303b705cfSriastradh	OUT_BATCH(GEN7_PIPE_CONTROL | (4 - 2));
110403b705cfSriastradh	OUT_BATCH(GEN7_PIPE_CONTROL_WC_FLUSH |
110503b705cfSriastradh		  GEN7_PIPE_CONTROL_TC_FLUSH |
110603b705cfSriastradh		  GEN7_PIPE_CONTROL_CS_STALL);
110703b705cfSriastradh	OUT_BATCH(0);
110803b705cfSriastradh	OUT_BATCH(0);
110913496ba1Ssnj	sna->render_state.gen7.pipe_controls_since_stall = 0;
111003b705cfSriastradh}
111103b705cfSriastradh
111203b705cfSriastradhinline static void
111342542f5fSchristosgen7_emit_pipe_flush(struct sna *sna, bool need_stall)
111403b705cfSriastradh{
111542542f5fSchristos	unsigned stall;
111642542f5fSchristos
111742542f5fSchristos	stall = 0;
111813496ba1Ssnj	if (need_stall) {
111913496ba1Ssnj		stall = GEN7_PIPE_CONTROL_CS_STALL;
112013496ba1Ssnj		sna->render_state.gen7.pipe_controls_since_stall = 0;
112113496ba1Ssnj	} else
112213496ba1Ssnj		sna->render_state.gen7.pipe_controls_since_stall++;
112342542f5fSchristos
112403b705cfSriastradh	OUT_BATCH(GEN7_PIPE_CONTROL | (4 - 2));
112542542f5fSchristos	OUT_BATCH(GEN7_PIPE_CONTROL_WC_FLUSH | stall);
112603b705cfSriastradh	OUT_BATCH(0);
112703b705cfSriastradh	OUT_BATCH(0);
112803b705cfSriastradh}
112903b705cfSriastradh
113003b705cfSriastradhinline static void
113103b705cfSriastradhgen7_emit_pipe_stall(struct sna *sna)
113203b705cfSriastradh{
113303b705cfSriastradh	OUT_BATCH(GEN7_PIPE_CONTROL | (4 - 2));
113403b705cfSriastradh	OUT_BATCH(GEN7_PIPE_CONTROL_CS_STALL |
113503b705cfSriastradh		  GEN7_PIPE_CONTROL_STALL_AT_SCOREBOARD);
113603b705cfSriastradh	OUT_BATCH(0);
113703b705cfSriastradh	OUT_BATCH(0);
113813496ba1Ssnj	sna->render_state.gen7.pipe_controls_since_stall = 0;
113903b705cfSriastradh}
114003b705cfSriastradh
114103b705cfSriastradhstatic void
114203b705cfSriastradhgen7_emit_state(struct sna *sna,
114303b705cfSriastradh		const struct sna_composite_op *op,
114403b705cfSriastradh		uint16_t wm_binding_table)
114503b705cfSriastradh{
114642542f5fSchristos	bool need_invalidate;
114742542f5fSchristos	bool need_flush;
114803b705cfSriastradh	bool need_stall;
114903b705cfSriastradh
115003b705cfSriastradh	assert(op->dst.bo->exec);
115103b705cfSriastradh
115242542f5fSchristos	need_flush = wm_binding_table & 1 ||
115342542f5fSchristos		(sna->render_state.gen7.emit_flush && GEN7_READS_DST(op->u.gen7.flags));
115442542f5fSchristos	if (ALWAYS_FLUSH)
115542542f5fSchristos		need_flush = true;
115603b705cfSriastradh
115742542f5fSchristos	wm_binding_table &= ~1;
115842542f5fSchristos
115942542f5fSchristos	need_stall = sna->render_state.gen7.surface_table != wm_binding_table;
116042542f5fSchristos
116142542f5fSchristos	need_invalidate = kgem_bo_is_dirty(op->src.bo) || kgem_bo_is_dirty(op->mask.bo);
116242542f5fSchristos	if (ALWAYS_INVALIDATE)
116342542f5fSchristos		need_invalidate = true;
116403b705cfSriastradh
116503b705cfSriastradh	need_stall &= gen7_emit_drawing_rectangle(sna, op);
116642542f5fSchristos	if (ALWAYS_STALL)
116742542f5fSchristos		need_stall = true;
116813496ba1Ssnj	if (sna->kgem.gen < 075 &&
116913496ba1Ssnj	    sna->render_state.gen7.pipe_controls_since_stall >= 3)
117013496ba1Ssnj		need_stall = true;
117103b705cfSriastradh
117242542f5fSchristos	if (need_invalidate) {
117303b705cfSriastradh		gen7_emit_pipe_invalidate(sna);
117403b705cfSriastradh		kgem_clear_dirty(&sna->kgem);
117503b705cfSriastradh		assert(op->dst.bo->exec);
117603b705cfSriastradh		kgem_bo_mark_dirty(op->dst.bo);
117742542f5fSchristos
117842542f5fSchristos		need_flush = false;
117942542f5fSchristos		need_stall = false;
118042542f5fSchristos	}
118142542f5fSchristos	if (need_flush) {
118242542f5fSchristos		gen7_emit_pipe_flush(sna, need_stall);
118303b705cfSriastradh		need_stall = false;
118403b705cfSriastradh	}
118503b705cfSriastradh	if (need_stall)
118603b705cfSriastradh		gen7_emit_pipe_stall(sna);
118703b705cfSriastradh
118842542f5fSchristos	gen7_emit_cc(sna, GEN7_BLEND(op->u.gen7.flags));
118942542f5fSchristos	gen7_emit_sampler(sna, GEN7_SAMPLER(op->u.gen7.flags));
119042542f5fSchristos	gen7_emit_sf(sna, GEN7_VERTEX(op->u.gen7.flags) >> 2);
119142542f5fSchristos	gen7_emit_wm(sna, GEN7_KERNEL(op->u.gen7.flags));
119242542f5fSchristos	gen7_emit_vertex_elements(sna, op);
119342542f5fSchristos	gen7_emit_binding_table(sna, wm_binding_table);
119442542f5fSchristos
119503b705cfSriastradh	sna->render_state.gen7.emit_flush = GEN7_READS_DST(op->u.gen7.flags);
119603b705cfSriastradh}
119703b705cfSriastradh
119803b705cfSriastradhstatic bool gen7_magic_ca_pass(struct sna *sna,
119903b705cfSriastradh			       const struct sna_composite_op *op)
120003b705cfSriastradh{
120103b705cfSriastradh	struct gen7_render_state *state = &sna->render_state.gen7;
120203b705cfSriastradh
120303b705cfSriastradh	if (!op->need_magic_ca_pass)
120403b705cfSriastradh		return false;
120503b705cfSriastradh
120603b705cfSriastradh	DBG(("%s: CA fixup (%d -> %d)\n", __FUNCTION__,
120703b705cfSriastradh	     sna->render.vertex_start, sna->render.vertex_index));
120803b705cfSriastradh
120903b705cfSriastradh	gen7_emit_pipe_stall(sna);
121003b705cfSriastradh
121103b705cfSriastradh	gen7_emit_cc(sna,
121203b705cfSriastradh		     GEN7_BLEND(gen7_get_blend(PictOpAdd, true,
121303b705cfSriastradh					       op->dst.format)));
121403b705cfSriastradh	gen7_emit_wm(sna,
121503b705cfSriastradh		     gen7_choose_composite_kernel(PictOpAdd,
121603b705cfSriastradh						  true, true,
121703b705cfSriastradh						  op->is_affine));
121803b705cfSriastradh
121903b705cfSriastradh	OUT_BATCH(GEN7_3DPRIMITIVE | (7- 2));
122003b705cfSriastradh	OUT_BATCH(GEN7_3DPRIMITIVE_VERTEX_SEQUENTIAL | _3DPRIM_RECTLIST);
122103b705cfSriastradh	OUT_BATCH(sna->render.vertex_index - sna->render.vertex_start);
122203b705cfSriastradh	OUT_BATCH(sna->render.vertex_start);
122303b705cfSriastradh	OUT_BATCH(1);	/* single instance */
122403b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
122503b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
122603b705cfSriastradh
122703b705cfSriastradh	state->last_primitive = sna->kgem.nbatch;
122803b705cfSriastradh	return true;
122903b705cfSriastradh}
123003b705cfSriastradh
123103b705cfSriastradhstatic void null_create(struct sna_static_stream *stream)
123203b705cfSriastradh{
123303b705cfSriastradh	/* A bunch of zeros useful for legacy border color and depth-stencil */
123403b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
123503b705cfSriastradh}
123603b705cfSriastradh
123703b705cfSriastradhstatic void
123803b705cfSriastradhsampler_state_init(struct gen7_sampler_state *sampler_state,
123903b705cfSriastradh		   sampler_filter_t filter,
124003b705cfSriastradh		   sampler_extend_t extend)
124103b705cfSriastradh{
124203b705cfSriastradh	sampler_state->ss0.lod_preclamp = 1;	/* GL mode */
124303b705cfSriastradh
124403b705cfSriastradh	/* We use the legacy mode to get the semantics specified by
124503b705cfSriastradh	 * the Render extension. */
124603b705cfSriastradh	sampler_state->ss0.default_color_mode = GEN7_BORDER_COLOR_MODE_LEGACY;
124703b705cfSriastradh
124803b705cfSriastradh	switch (filter) {
124903b705cfSriastradh	default:
125003b705cfSriastradh	case SAMPLER_FILTER_NEAREST:
125103b705cfSriastradh		sampler_state->ss0.min_filter = GEN7_MAPFILTER_NEAREST;
125203b705cfSriastradh		sampler_state->ss0.mag_filter = GEN7_MAPFILTER_NEAREST;
125303b705cfSriastradh		break;
125403b705cfSriastradh	case SAMPLER_FILTER_BILINEAR:
125503b705cfSriastradh		sampler_state->ss0.min_filter = GEN7_MAPFILTER_LINEAR;
125603b705cfSriastradh		sampler_state->ss0.mag_filter = GEN7_MAPFILTER_LINEAR;
125703b705cfSriastradh		break;
125803b705cfSriastradh	}
125903b705cfSriastradh
126003b705cfSriastradh	switch (extend) {
126103b705cfSriastradh	default:
126203b705cfSriastradh	case SAMPLER_EXTEND_NONE:
126303b705cfSriastradh		sampler_state->ss3.r_wrap_mode = GEN7_TEXCOORDMODE_CLAMP_BORDER;
126403b705cfSriastradh		sampler_state->ss3.s_wrap_mode = GEN7_TEXCOORDMODE_CLAMP_BORDER;
126503b705cfSriastradh		sampler_state->ss3.t_wrap_mode = GEN7_TEXCOORDMODE_CLAMP_BORDER;
126603b705cfSriastradh		break;
126703b705cfSriastradh	case SAMPLER_EXTEND_REPEAT:
126803b705cfSriastradh		sampler_state->ss3.r_wrap_mode = GEN7_TEXCOORDMODE_WRAP;
126903b705cfSriastradh		sampler_state->ss3.s_wrap_mode = GEN7_TEXCOORDMODE_WRAP;
127003b705cfSriastradh		sampler_state->ss3.t_wrap_mode = GEN7_TEXCOORDMODE_WRAP;
127103b705cfSriastradh		break;
127203b705cfSriastradh	case SAMPLER_EXTEND_PAD:
127303b705cfSriastradh		sampler_state->ss3.r_wrap_mode = GEN7_TEXCOORDMODE_CLAMP;
127403b705cfSriastradh		sampler_state->ss3.s_wrap_mode = GEN7_TEXCOORDMODE_CLAMP;
127503b705cfSriastradh		sampler_state->ss3.t_wrap_mode = GEN7_TEXCOORDMODE_CLAMP;
127603b705cfSriastradh		break;
127703b705cfSriastradh	case SAMPLER_EXTEND_REFLECT:
127803b705cfSriastradh		sampler_state->ss3.r_wrap_mode = GEN7_TEXCOORDMODE_MIRROR;
127903b705cfSriastradh		sampler_state->ss3.s_wrap_mode = GEN7_TEXCOORDMODE_MIRROR;
128003b705cfSriastradh		sampler_state->ss3.t_wrap_mode = GEN7_TEXCOORDMODE_MIRROR;
128103b705cfSriastradh		break;
128203b705cfSriastradh	}
128303b705cfSriastradh}
128403b705cfSriastradh
128503b705cfSriastradhstatic void
128603b705cfSriastradhsampler_copy_init(struct gen7_sampler_state *ss)
128703b705cfSriastradh{
128803b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
128903b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
129003b705cfSriastradh
129103b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
129203b705cfSriastradh}
129303b705cfSriastradh
129403b705cfSriastradhstatic void
129503b705cfSriastradhsampler_fill_init(struct gen7_sampler_state *ss)
129603b705cfSriastradh{
129703b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_REPEAT);
129803b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
129903b705cfSriastradh
130003b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
130103b705cfSriastradh}
130203b705cfSriastradh
130303b705cfSriastradhstatic uint32_t
130403b705cfSriastradhgen7_tiling_bits(uint32_t tiling)
130503b705cfSriastradh{
130603b705cfSriastradh	switch (tiling) {
130703b705cfSriastradh	default: assert(0);
130803b705cfSriastradh	case I915_TILING_NONE: return 0;
130903b705cfSriastradh	case I915_TILING_X: return GEN7_SURFACE_TILED;
131003b705cfSriastradh	case I915_TILING_Y: return GEN7_SURFACE_TILED | GEN7_SURFACE_TILED_Y;
131103b705cfSriastradh	}
131203b705cfSriastradh}
131303b705cfSriastradh
131403b705cfSriastradh/**
131503b705cfSriastradh * Sets up the common fields for a surface state buffer for the given
131603b705cfSriastradh * picture in the given surface state buffer.
131703b705cfSriastradh */
131803b705cfSriastradhstatic uint32_t
131903b705cfSriastradhgen7_bind_bo(struct sna *sna,
132003b705cfSriastradh	     struct kgem_bo *bo,
132103b705cfSriastradh	     uint32_t width,
132203b705cfSriastradh	     uint32_t height,
132303b705cfSriastradh	     uint32_t format,
132403b705cfSriastradh	     bool is_dst)
132503b705cfSriastradh{
132603b705cfSriastradh	uint32_t *ss;
132703b705cfSriastradh	uint32_t domains;
132803b705cfSriastradh	int offset;
132903b705cfSriastradh	uint32_t is_scanout = is_dst && bo->scanout;
133003b705cfSriastradh
133103b705cfSriastradh	COMPILE_TIME_ASSERT(sizeof(struct gen7_surface_state) == 32);
133203b705cfSriastradh
133303b705cfSriastradh	/* After the first bind, we manage the cache domains within the batch */
133403b705cfSriastradh	offset = kgem_bo_get_binding(bo, format | is_dst << 30 | is_scanout << 31);
133503b705cfSriastradh	if (offset) {
133642542f5fSchristos		assert(offset >= sna->kgem.surface);
133703b705cfSriastradh		if (is_dst)
133803b705cfSriastradh			kgem_bo_mark_dirty(bo);
133903b705cfSriastradh		return offset * sizeof(uint32_t);
134003b705cfSriastradh	}
134103b705cfSriastradh
134203b705cfSriastradh	offset = sna->kgem.surface -=
134303b705cfSriastradh		sizeof(struct gen7_surface_state) / sizeof(uint32_t);
134403b705cfSriastradh	ss = sna->kgem.batch + offset;
134503b705cfSriastradh	ss[0] = (GEN7_SURFACE_2D << GEN7_SURFACE_TYPE_SHIFT |
134603b705cfSriastradh		 gen7_tiling_bits(bo->tiling) |
134703b705cfSriastradh		 format << GEN7_SURFACE_FORMAT_SHIFT);
134803b705cfSriastradh	if (bo->tiling == I915_TILING_Y)
134903b705cfSriastradh		ss[0] |= GEN7_SURFACE_VALIGN_4;
135003b705cfSriastradh	if (is_dst) {
135103b705cfSriastradh		ss[0] |= GEN7_SURFACE_RC_READ_WRITE;
135203b705cfSriastradh		domains = I915_GEM_DOMAIN_RENDER << 16 |I915_GEM_DOMAIN_RENDER;
135303b705cfSriastradh	} else
135403b705cfSriastradh		domains = I915_GEM_DOMAIN_SAMPLER << 16;
135503b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, offset + 1, bo, domains, 0);
135603b705cfSriastradh	ss[2] = ((width - 1)  << GEN7_SURFACE_WIDTH_SHIFT |
135703b705cfSriastradh		 (height - 1) << GEN7_SURFACE_HEIGHT_SHIFT);
135803b705cfSriastradh	ss[3] = (bo->pitch - 1) << GEN7_SURFACE_PITCH_SHIFT;
135903b705cfSriastradh	ss[4] = 0;
136042542f5fSchristos	ss[5] = (is_scanout || bo->io) ? 0 : sna->render_state.gen7.info->mocs << 16;
136103b705cfSriastradh	ss[6] = 0;
136203b705cfSriastradh	ss[7] = 0;
136303b705cfSriastradh	if (is_hsw(sna))
136403b705cfSriastradh		ss[7] |= HSW_SURFACE_SWIZZLE(RED, GREEN, BLUE, ALPHA);
136503b705cfSriastradh
136603b705cfSriastradh	kgem_bo_set_binding(bo, format | is_dst << 30 | is_scanout << 31, offset);
136703b705cfSriastradh
136803b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, tiling=%d -> %s\n",
136903b705cfSriastradh	     offset, bo->handle, ss[1],
137003b705cfSriastradh	     format, width, height, bo->pitch, bo->tiling,
137103b705cfSriastradh	     domains & 0xffff ? "render" : "sampler"));
137203b705cfSriastradh
137303b705cfSriastradh	return offset * sizeof(uint32_t);
137403b705cfSriastradh}
137503b705cfSriastradh
137603b705cfSriastradhstatic void gen7_emit_vertex_buffer(struct sna *sna,
137703b705cfSriastradh				    const struct sna_composite_op *op)
137803b705cfSriastradh{
137903b705cfSriastradh	int id = GEN7_VERTEX(op->u.gen7.flags);
138003b705cfSriastradh
138103b705cfSriastradh	OUT_BATCH(GEN7_3DSTATE_VERTEX_BUFFERS | (5 - 2));
138203b705cfSriastradh	OUT_BATCH(id << GEN7_VB0_BUFFER_INDEX_SHIFT |
138303b705cfSriastradh		  GEN7_VB0_VERTEXDATA |
138403b705cfSriastradh		  GEN7_VB0_ADDRESS_MODIFY_ENABLE |
138503b705cfSriastradh		  4*op->floats_per_vertex << GEN7_VB0_BUFFER_PITCH_SHIFT);
138603b705cfSriastradh	sna->render.vertex_reloc[sna->render.nvertex_reloc++] = sna->kgem.nbatch;
138703b705cfSriastradh	OUT_BATCH(0);
138803b705cfSriastradh	OUT_BATCH(~0); /* max address: disabled */
138903b705cfSriastradh	OUT_BATCH(0);
139003b705cfSriastradh
139103b705cfSriastradh	sna->render.vb_id |= 1 << id;
139203b705cfSriastradh}
139303b705cfSriastradh
139403b705cfSriastradhstatic void gen7_emit_primitive(struct sna *sna)
139503b705cfSriastradh{
139603b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen7.last_primitive) {
139703b705cfSriastradh		sna->render.vertex_offset = sna->kgem.nbatch - 5;
139803b705cfSriastradh		return;
139903b705cfSriastradh	}
140003b705cfSriastradh
140103b705cfSriastradh	OUT_BATCH(GEN7_3DPRIMITIVE | (7- 2));
140203b705cfSriastradh	OUT_BATCH(GEN7_3DPRIMITIVE_VERTEX_SEQUENTIAL | _3DPRIM_RECTLIST);
140303b705cfSriastradh	sna->render.vertex_offset = sna->kgem.nbatch;
140403b705cfSriastradh	OUT_BATCH(0);	/* vertex count, to be filled in later */
140503b705cfSriastradh	OUT_BATCH(sna->render.vertex_index);
140603b705cfSriastradh	OUT_BATCH(1);	/* single instance */
140703b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
140803b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
140903b705cfSriastradh	sna->render.vertex_start = sna->render.vertex_index;
141003b705cfSriastradh
141103b705cfSriastradh	sna->render_state.gen7.last_primitive = sna->kgem.nbatch;
141203b705cfSriastradh}
141303b705cfSriastradh
141403b705cfSriastradhstatic bool gen7_rectangle_begin(struct sna *sna,
141503b705cfSriastradh				 const struct sna_composite_op *op)
141603b705cfSriastradh{
141703b705cfSriastradh	int id = 1 << GEN7_VERTEX(op->u.gen7.flags);
141803b705cfSriastradh	int ndwords;
141903b705cfSriastradh
142003b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render) && sna->render.vertex_offset)
142103b705cfSriastradh		return true;
142203b705cfSriastradh
142303b705cfSriastradh	ndwords = op->need_magic_ca_pass ? 60 : 6;
142403b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
142503b705cfSriastradh		ndwords += 5;
142603b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, ndwords))
142703b705cfSriastradh		return false;
142803b705cfSriastradh
142903b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
143003b705cfSriastradh		gen7_emit_vertex_buffer(sna, op);
143103b705cfSriastradh
143203b705cfSriastradh	gen7_emit_primitive(sna);
143303b705cfSriastradh	return true;
143403b705cfSriastradh}
143503b705cfSriastradh
143603b705cfSriastradhstatic int gen7_get_rectangles__flush(struct sna *sna,
143703b705cfSriastradh				      const struct sna_composite_op *op)
143803b705cfSriastradh{
143903b705cfSriastradh	/* Preventing discarding new vbo after lock contention */
144003b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render)) {
144103b705cfSriastradh		int rem = vertex_space(sna);
144203b705cfSriastradh		if (rem > op->floats_per_rect)
144303b705cfSriastradh			return rem;
144403b705cfSriastradh	}
144503b705cfSriastradh
144603b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, op->need_magic_ca_pass ? 65 : 6))
144703b705cfSriastradh		return 0;
144803b705cfSriastradh	if (!kgem_check_reloc_and_exec(&sna->kgem, 2))
144903b705cfSriastradh		return 0;
145003b705cfSriastradh
145103b705cfSriastradh	if (sna->render.vertex_offset) {
145203b705cfSriastradh		gen4_vertex_flush(sna);
145303b705cfSriastradh		if (gen7_magic_ca_pass(sna, op)) {
145403b705cfSriastradh			gen7_emit_pipe_stall(sna);
145503b705cfSriastradh			gen7_emit_cc(sna, GEN7_BLEND(op->u.gen7.flags));
145603b705cfSriastradh			gen7_emit_wm(sna, GEN7_KERNEL(op->u.gen7.flags));
145703b705cfSriastradh		}
145803b705cfSriastradh	}
145903b705cfSriastradh
146003b705cfSriastradh	return gen4_vertex_finish(sna);
146103b705cfSriastradh}
146203b705cfSriastradh
146303b705cfSriastradhinline static int gen7_get_rectangles(struct sna *sna,
146403b705cfSriastradh				      const struct sna_composite_op *op,
146503b705cfSriastradh				      int want,
146603b705cfSriastradh				      void (*emit_state)(struct sna *sna, const struct sna_composite_op *op))
146703b705cfSriastradh{
146803b705cfSriastradh	int rem;
146903b705cfSriastradh
147003b705cfSriastradh	assert(want);
147103b705cfSriastradh
147203b705cfSriastradhstart:
147303b705cfSriastradh	rem = vertex_space(sna);
147403b705cfSriastradh	if (unlikely(rem < op->floats_per_rect)) {
147503b705cfSriastradh		DBG(("flushing vbo for %s: %d < %d\n",
147603b705cfSriastradh		     __FUNCTION__, rem, op->floats_per_rect));
147703b705cfSriastradh		rem = gen7_get_rectangles__flush(sna, op);
147803b705cfSriastradh		if (unlikely(rem == 0))
147903b705cfSriastradh			goto flush;
148003b705cfSriastradh	}
148103b705cfSriastradh
148203b705cfSriastradh	if (unlikely(sna->render.vertex_offset == 0)) {
148303b705cfSriastradh		if (!gen7_rectangle_begin(sna, op))
148403b705cfSriastradh			goto flush;
148503b705cfSriastradh		else
148603b705cfSriastradh			goto start;
148703b705cfSriastradh	}
148803b705cfSriastradh
148903b705cfSriastradh	assert(rem <= vertex_space(sna));
149003b705cfSriastradh	assert(op->floats_per_rect <= rem);
149103b705cfSriastradh	if (want > 1 && want * op->floats_per_rect > rem)
149203b705cfSriastradh		want = rem / op->floats_per_rect;
149303b705cfSriastradh
149403b705cfSriastradh	assert(want > 0);
149503b705cfSriastradh	sna->render.vertex_index += 3*want;
149603b705cfSriastradh	return want;
149703b705cfSriastradh
149803b705cfSriastradhflush:
149903b705cfSriastradh	if (sna->render.vertex_offset) {
150003b705cfSriastradh		gen4_vertex_flush(sna);
150103b705cfSriastradh		gen7_magic_ca_pass(sna, op);
150203b705cfSriastradh	}
150303b705cfSriastradh	sna_vertex_wait__locked(&sna->render);
150403b705cfSriastradh	_kgem_submit(&sna->kgem);
150503b705cfSriastradh	emit_state(sna, op);
150603b705cfSriastradh	goto start;
150703b705cfSriastradh}
150803b705cfSriastradh
150903b705cfSriastradhinline static uint32_t *gen7_composite_get_binding_table(struct sna *sna,
151003b705cfSriastradh							 uint16_t *offset)
151103b705cfSriastradh{
151203b705cfSriastradh	uint32_t *table;
151303b705cfSriastradh
151403b705cfSriastradh	sna->kgem.surface -=
151503b705cfSriastradh		sizeof(struct gen7_surface_state) / sizeof(uint32_t);
151603b705cfSriastradh	/* Clear all surplus entries to zero in case of prefetch */
151703b705cfSriastradh	table = memset(sna->kgem.batch + sna->kgem.surface,
151803b705cfSriastradh		       0, sizeof(struct gen7_surface_state));
151903b705cfSriastradh
152003b705cfSriastradh	DBG(("%s(%x)\n", __FUNCTION__, 4*sna->kgem.surface));
152103b705cfSriastradh
152203b705cfSriastradh	*offset = sna->kgem.surface;
152303b705cfSriastradh	return table;
152403b705cfSriastradh}
152503b705cfSriastradh
152603b705cfSriastradhstatic void
152703b705cfSriastradhgen7_get_batch(struct sna *sna, const struct sna_composite_op *op)
152803b705cfSriastradh{
152903b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, op->dst.bo);
153003b705cfSriastradh
153103b705cfSriastradh	if (!kgem_check_batch_with_surfaces(&sna->kgem, 150, 4)) {
153203b705cfSriastradh		DBG(("%s: flushing batch: %d < %d+%d\n",
153303b705cfSriastradh		     __FUNCTION__, sna->kgem.surface - sna->kgem.nbatch,
153403b705cfSriastradh		     150, 4*8));
153503b705cfSriastradh		_kgem_submit(&sna->kgem);
153603b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
153703b705cfSriastradh	}
153803b705cfSriastradh
153903b705cfSriastradh	assert(sna->kgem.mode == KGEM_RENDER);
154003b705cfSriastradh	assert(sna->kgem.ring == KGEM_RENDER);
154103b705cfSriastradh
154203b705cfSriastradh	if (sna->render_state.gen7.needs_invariant)
154303b705cfSriastradh		gen7_emit_invariant(sna);
154403b705cfSriastradh}
154503b705cfSriastradh
154603b705cfSriastradhstatic void gen7_emit_composite_state(struct sna *sna,
154703b705cfSriastradh				      const struct sna_composite_op *op)
154803b705cfSriastradh{
154903b705cfSriastradh	uint32_t *binding_table;
155042542f5fSchristos	uint16_t offset, dirty;
155103b705cfSriastradh
155203b705cfSriastradh	gen7_get_batch(sna, op);
155303b705cfSriastradh
155403b705cfSriastradh	binding_table = gen7_composite_get_binding_table(sna, &offset);
155503b705cfSriastradh
155642542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
155742542f5fSchristos
155803b705cfSriastradh	binding_table[0] =
155903b705cfSriastradh		gen7_bind_bo(sna,
156003b705cfSriastradh			    op->dst.bo, op->dst.width, op->dst.height,
156103b705cfSriastradh			    gen7_get_dest_format(op->dst.format),
156203b705cfSriastradh			    true);
156303b705cfSriastradh	binding_table[1] =
156403b705cfSriastradh		gen7_bind_bo(sna,
156503b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
156603b705cfSriastradh			     op->src.card_format,
156703b705cfSriastradh			     false);
156803b705cfSriastradh	if (op->mask.bo) {
156903b705cfSriastradh		binding_table[2] =
157003b705cfSriastradh			gen7_bind_bo(sna,
157103b705cfSriastradh				     op->mask.bo,
157203b705cfSriastradh				     op->mask.width,
157303b705cfSriastradh				     op->mask.height,
157403b705cfSriastradh				     op->mask.card_format,
157503b705cfSriastradh				     false);
157603b705cfSriastradh	}
157703b705cfSriastradh
157803b705cfSriastradh	if (sna->kgem.surface == offset &&
157903b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen7.surface_table) == *(uint64_t*)binding_table &&
158003b705cfSriastradh	    (op->mask.bo == NULL ||
158103b705cfSriastradh	     sna->kgem.batch[sna->render_state.gen7.surface_table+2] == binding_table[2])) {
158203b705cfSriastradh		sna->kgem.surface += sizeof(struct gen7_surface_state) / sizeof(uint32_t);
158303b705cfSriastradh		offset = sna->render_state.gen7.surface_table;
158403b705cfSriastradh	}
158503b705cfSriastradh
158642542f5fSchristos	if (sna->kgem.batch[sna->render_state.gen7.surface_table] == binding_table[0])
158742542f5fSchristos		dirty = 0;
158842542f5fSchristos
158942542f5fSchristos	gen7_emit_state(sna, op, offset | dirty);
159003b705cfSriastradh}
159103b705cfSriastradh
159203b705cfSriastradhstatic void
159303b705cfSriastradhgen7_align_vertex(struct sna *sna, const struct sna_composite_op *op)
159403b705cfSriastradh{
159503b705cfSriastradh	if (op->floats_per_vertex != sna->render_state.gen7.floats_per_vertex) {
159642542f5fSchristos		DBG(("aligning vertex: was %d, now %d floats per vertex\n",
159742542f5fSchristos		     sna->render_state.gen7.floats_per_vertex, op->floats_per_vertex));
159842542f5fSchristos		gen4_vertex_align(sna, op);
159903b705cfSriastradh		sna->render_state.gen7.floats_per_vertex = op->floats_per_vertex;
160003b705cfSriastradh	}
160103b705cfSriastradh}
160203b705cfSriastradh
160303b705cfSriastradhfastcall static void
160403b705cfSriastradhgen7_render_composite_blt(struct sna *sna,
160503b705cfSriastradh			  const struct sna_composite_op *op,
160603b705cfSriastradh			  const struct sna_composite_rectangles *r)
160703b705cfSriastradh{
160803b705cfSriastradh	gen7_get_rectangles(sna, op, 1, gen7_emit_composite_state);
160903b705cfSriastradh	op->prim_emit(sna, op, r);
161003b705cfSriastradh}
161103b705cfSriastradh
161203b705cfSriastradhfastcall static void
161303b705cfSriastradhgen7_render_composite_box(struct sna *sna,
161403b705cfSriastradh			  const struct sna_composite_op *op,
161503b705cfSriastradh			  const BoxRec *box)
161603b705cfSriastradh{
161703b705cfSriastradh	struct sna_composite_rectangles r;
161803b705cfSriastradh
161903b705cfSriastradh	gen7_get_rectangles(sna, op, 1, gen7_emit_composite_state);
162003b705cfSriastradh
162103b705cfSriastradh	DBG(("  %s: (%d, %d), (%d, %d)\n",
162203b705cfSriastradh	     __FUNCTION__,
162303b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
162403b705cfSriastradh
162503b705cfSriastradh	r.dst.x = box->x1;
162603b705cfSriastradh	r.dst.y = box->y1;
162703b705cfSriastradh	r.width  = box->x2 - box->x1;
162803b705cfSriastradh	r.height = box->y2 - box->y1;
162903b705cfSriastradh	r.src = r.mask = r.dst;
163003b705cfSriastradh
163103b705cfSriastradh	op->prim_emit(sna, op, &r);
163203b705cfSriastradh}
163303b705cfSriastradh
163403b705cfSriastradhstatic void
163503b705cfSriastradhgen7_render_composite_boxes__blt(struct sna *sna,
163603b705cfSriastradh				 const struct sna_composite_op *op,
163703b705cfSriastradh				 const BoxRec *box, int nbox)
163803b705cfSriastradh{
163903b705cfSriastradh	DBG(("composite_boxes(%d)\n", nbox));
164003b705cfSriastradh
164103b705cfSriastradh	do {
164203b705cfSriastradh		int nbox_this_time;
164303b705cfSriastradh
164403b705cfSriastradh		nbox_this_time = gen7_get_rectangles(sna, op, nbox,
164503b705cfSriastradh						     gen7_emit_composite_state);
164603b705cfSriastradh		nbox -= nbox_this_time;
164703b705cfSriastradh
164803b705cfSriastradh		do {
164903b705cfSriastradh			struct sna_composite_rectangles r;
165003b705cfSriastradh
165103b705cfSriastradh			DBG(("  %s: (%d, %d), (%d, %d)\n",
165203b705cfSriastradh			     __FUNCTION__,
165303b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
165403b705cfSriastradh
165503b705cfSriastradh			r.dst.x = box->x1;
165603b705cfSriastradh			r.dst.y = box->y1;
165703b705cfSriastradh			r.width  = box->x2 - box->x1;
165803b705cfSriastradh			r.height = box->y2 - box->y1;
165903b705cfSriastradh			r.src = r.mask = r.dst;
166003b705cfSriastradh
166103b705cfSriastradh			op->prim_emit(sna, op, &r);
166203b705cfSriastradh			box++;
166303b705cfSriastradh		} while (--nbox_this_time);
166403b705cfSriastradh	} while (nbox);
166503b705cfSriastradh}
166603b705cfSriastradh
166703b705cfSriastradhstatic void
166803b705cfSriastradhgen7_render_composite_boxes(struct sna *sna,
166903b705cfSriastradh			    const struct sna_composite_op *op,
167003b705cfSriastradh			    const BoxRec *box, int nbox)
167103b705cfSriastradh{
167203b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
167303b705cfSriastradh
167403b705cfSriastradh	do {
167503b705cfSriastradh		int nbox_this_time;
167603b705cfSriastradh		float *v;
167703b705cfSriastradh
167803b705cfSriastradh		nbox_this_time = gen7_get_rectangles(sna, op, nbox,
167903b705cfSriastradh						     gen7_emit_composite_state);
168003b705cfSriastradh		assert(nbox_this_time);
168103b705cfSriastradh		nbox -= nbox_this_time;
168203b705cfSriastradh
168303b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
168403b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
168503b705cfSriastradh
168603b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
168703b705cfSriastradh		box += nbox_this_time;
168803b705cfSriastradh	} while (nbox);
168903b705cfSriastradh}
169003b705cfSriastradh
169103b705cfSriastradhstatic void
169203b705cfSriastradhgen7_render_composite_boxes__thread(struct sna *sna,
169303b705cfSriastradh				    const struct sna_composite_op *op,
169403b705cfSriastradh				    const BoxRec *box, int nbox)
169503b705cfSriastradh{
169603b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
169703b705cfSriastradh
169803b705cfSriastradh	sna_vertex_lock(&sna->render);
169903b705cfSriastradh	do {
170003b705cfSriastradh		int nbox_this_time;
170103b705cfSriastradh		float *v;
170203b705cfSriastradh
170303b705cfSriastradh		nbox_this_time = gen7_get_rectangles(sna, op, nbox,
170403b705cfSriastradh						     gen7_emit_composite_state);
170503b705cfSriastradh		assert(nbox_this_time);
170603b705cfSriastradh		nbox -= nbox_this_time;
170703b705cfSriastradh
170803b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
170903b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
171003b705cfSriastradh
171103b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
171203b705cfSriastradh		sna_vertex_unlock(&sna->render);
171303b705cfSriastradh
171403b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
171503b705cfSriastradh		box += nbox_this_time;
171603b705cfSriastradh
171703b705cfSriastradh		sna_vertex_lock(&sna->render);
171803b705cfSriastradh		sna_vertex_release__locked(&sna->render);
171903b705cfSriastradh	} while (nbox);
172003b705cfSriastradh	sna_vertex_unlock(&sna->render);
172103b705cfSriastradh}
172203b705cfSriastradh
172303b705cfSriastradh#ifndef MAX
172403b705cfSriastradh#define MAX(a,b) ((a) > (b) ? (a) : (b))
172503b705cfSriastradh#endif
172603b705cfSriastradh
172703b705cfSriastradhstatic uint32_t
172803b705cfSriastradhgen7_composite_create_blend_state(struct sna_static_stream *stream)
172903b705cfSriastradh{
173003b705cfSriastradh	char *base, *ptr;
173103b705cfSriastradh	int src, dst;
173203b705cfSriastradh
173303b705cfSriastradh	base = sna_static_stream_map(stream,
173403b705cfSriastradh				     GEN7_BLENDFACTOR_COUNT * GEN7_BLENDFACTOR_COUNT * GEN7_BLEND_STATE_PADDED_SIZE,
173503b705cfSriastradh				     64);
173603b705cfSriastradh
173703b705cfSriastradh	ptr = base;
173803b705cfSriastradh	for (src = 0; src < GEN7_BLENDFACTOR_COUNT; src++) {
173903b705cfSriastradh		for (dst= 0; dst < GEN7_BLENDFACTOR_COUNT; dst++) {
174003b705cfSriastradh			struct gen7_blend_state *blend =
174103b705cfSriastradh				(struct gen7_blend_state *)ptr;
174203b705cfSriastradh
174303b705cfSriastradh			blend->blend0.dest_blend_factor = dst;
174403b705cfSriastradh			blend->blend0.source_blend_factor = src;
174503b705cfSriastradh			blend->blend0.blend_func = GEN7_BLENDFUNCTION_ADD;
174603b705cfSriastradh			blend->blend0.blend_enable =
174703b705cfSriastradh				!(dst == GEN7_BLENDFACTOR_ZERO && src == GEN7_BLENDFACTOR_ONE);
174803b705cfSriastradh
174903b705cfSriastradh			blend->blend1.post_blend_clamp_enable = 1;
175003b705cfSriastradh			blend->blend1.pre_blend_clamp_enable = 1;
175103b705cfSriastradh
175203b705cfSriastradh			ptr += GEN7_BLEND_STATE_PADDED_SIZE;
175303b705cfSriastradh		}
175403b705cfSriastradh	}
175503b705cfSriastradh
175603b705cfSriastradh	return sna_static_stream_offsetof(stream, base);
175703b705cfSriastradh}
175803b705cfSriastradh
175903b705cfSriastradhstatic uint32_t gen7_bind_video_source(struct sna *sna,
176003b705cfSriastradh				       struct kgem_bo *bo,
176103b705cfSriastradh				       uint32_t offset,
176203b705cfSriastradh				       int width,
176303b705cfSriastradh				       int height,
176403b705cfSriastradh				       int pitch,
176503b705cfSriastradh				       uint32_t format)
176603b705cfSriastradh{
176703b705cfSriastradh	uint32_t *ss, bind;
176803b705cfSriastradh
176903b705cfSriastradh	bind = sna->kgem.surface -=
177003b705cfSriastradh		sizeof(struct gen7_surface_state) / sizeof(uint32_t);
177103b705cfSriastradh
177203b705cfSriastradh	assert(bo->tiling == I915_TILING_NONE);
177303b705cfSriastradh
177403b705cfSriastradh	ss = sna->kgem.batch + bind;
177503b705cfSriastradh	ss[0] = (GEN7_SURFACE_2D << GEN7_SURFACE_TYPE_SHIFT |
177603b705cfSriastradh		 format << GEN7_SURFACE_FORMAT_SHIFT);
177703b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, bind + 1, bo,
177803b705cfSriastradh			       I915_GEM_DOMAIN_SAMPLER << 16,
177903b705cfSriastradh			       offset);
178003b705cfSriastradh	ss[2] = ((width - 1)  << GEN7_SURFACE_WIDTH_SHIFT |
178103b705cfSriastradh		 (height - 1) << GEN7_SURFACE_HEIGHT_SHIFT);
178203b705cfSriastradh	ss[3] = (pitch - 1) << GEN7_SURFACE_PITCH_SHIFT;
178303b705cfSriastradh	ss[4] = 0;
178403b705cfSriastradh	ss[5] = 0;
178503b705cfSriastradh	ss[6] = 0;
178603b705cfSriastradh	ss[7] = 0;
178703b705cfSriastradh	if (is_hsw(sna))
178803b705cfSriastradh		ss[7] |= HSW_SURFACE_SWIZZLE(RED, GREEN, BLUE, ALPHA);
178903b705cfSriastradh
179003b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, offset=%d\n",
179103b705cfSriastradh	     bind, bo->handle, ss[1],
179203b705cfSriastradh	     format, width, height, pitch, offset));
179303b705cfSriastradh
179403b705cfSriastradh	return bind * sizeof(uint32_t);
179503b705cfSriastradh}
179603b705cfSriastradh
179703b705cfSriastradhstatic void gen7_emit_video_state(struct sna *sna,
179803b705cfSriastradh				  const struct sna_composite_op *op)
179903b705cfSriastradh{
180003b705cfSriastradh	struct sna_video_frame *frame = op->priv;
1801fe8aea9eSmrg	uint32_t src_surf_format[6];
180203b705cfSriastradh	uint32_t src_surf_base[6];
180303b705cfSriastradh	int src_width[6];
180403b705cfSriastradh	int src_height[6];
180503b705cfSriastradh	int src_pitch[6];
180603b705cfSriastradh	uint32_t *binding_table;
180742542f5fSchristos	uint16_t offset, dirty;
180803b705cfSriastradh	int n_src, n;
180903b705cfSriastradh
181003b705cfSriastradh	gen7_get_batch(sna, op);
181103b705cfSriastradh
181203b705cfSriastradh	src_surf_base[0] = 0;
181303b705cfSriastradh	src_surf_base[1] = 0;
181403b705cfSriastradh	src_surf_base[2] = frame->VBufOffset;
181503b705cfSriastradh	src_surf_base[3] = frame->VBufOffset;
181603b705cfSriastradh	src_surf_base[4] = frame->UBufOffset;
181703b705cfSriastradh	src_surf_base[5] = frame->UBufOffset;
181803b705cfSriastradh
181903b705cfSriastradh	if (is_planar_fourcc(frame->id)) {
1820fe8aea9eSmrg		for (n = 0; n < 2; n++) {
1821fe8aea9eSmrg			src_surf_format[n] = GEN7_SURFACEFORMAT_R8_UNORM;
1822fe8aea9eSmrg			src_width[n]  = frame->width;
1823fe8aea9eSmrg			src_height[n] = frame->height;
1824fe8aea9eSmrg			src_pitch[n]  = frame->pitch[1];
1825fe8aea9eSmrg		}
1826fe8aea9eSmrg		for (; n < 6; n++) {
1827fe8aea9eSmrg			if (is_nv12_fourcc(frame->id))
1828fe8aea9eSmrg				src_surf_format[n] = GEN7_SURFACEFORMAT_R8G8_UNORM;
1829fe8aea9eSmrg			else
1830fe8aea9eSmrg				src_surf_format[n] = GEN7_SURFACEFORMAT_R8_UNORM;
1831fe8aea9eSmrg			src_width[n] = frame->width / 2;
1832fe8aea9eSmrg			src_height[n] = frame->height / 2;
1833fe8aea9eSmrg			src_pitch[n] = frame->pitch[0];
1834fe8aea9eSmrg		}
183503b705cfSriastradh		n_src = 6;
183603b705cfSriastradh	} else {
1837fe8aea9eSmrg		if (frame->id == FOURCC_RGB888)
1838fe8aea9eSmrg			src_surf_format[0] = GEN7_SURFACEFORMAT_B8G8R8X8_UNORM;
1839fe8aea9eSmrg		else if (frame->id == FOURCC_UYVY)
1840fe8aea9eSmrg			src_surf_format[0] = GEN7_SURFACEFORMAT_YCRCB_SWAPY;
184103b705cfSriastradh		else
1842fe8aea9eSmrg			src_surf_format[0] = GEN7_SURFACEFORMAT_YCRCB_NORMAL;
184303b705cfSriastradh
184403b705cfSriastradh		src_width[0]  = frame->width;
184503b705cfSriastradh		src_height[0] = frame->height;
184603b705cfSriastradh		src_pitch[0]  = frame->pitch[0];
184703b705cfSriastradh		n_src = 1;
184803b705cfSriastradh	}
184903b705cfSriastradh
185003b705cfSriastradh	binding_table = gen7_composite_get_binding_table(sna, &offset);
185103b705cfSriastradh
185242542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
185342542f5fSchristos
185403b705cfSriastradh	binding_table[0] =
185503b705cfSriastradh		gen7_bind_bo(sna,
185603b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
185703b705cfSriastradh			     gen7_get_dest_format(op->dst.format),
185803b705cfSriastradh			     true);
185903b705cfSriastradh	for (n = 0; n < n_src; n++) {
186003b705cfSriastradh		binding_table[1+n] =
186103b705cfSriastradh			gen7_bind_video_source(sna,
186203b705cfSriastradh					       frame->bo,
186303b705cfSriastradh					       src_surf_base[n],
186403b705cfSriastradh					       src_width[n],
186503b705cfSriastradh					       src_height[n],
186603b705cfSriastradh					       src_pitch[n],
1867fe8aea9eSmrg					       src_surf_format[n]);
186803b705cfSriastradh	}
186903b705cfSriastradh
187042542f5fSchristos	gen7_emit_state(sna, op, offset | dirty);
187103b705cfSriastradh}
187203b705cfSriastradh
1873fe8aea9eSmrgstatic unsigned select_video_kernel(const struct sna_video *video,
1874fe8aea9eSmrg				    const struct sna_video_frame *frame)
1875fe8aea9eSmrg{
1876fe8aea9eSmrg	switch (frame->id) {
1877fe8aea9eSmrg	case FOURCC_YV12:
1878fe8aea9eSmrg	case FOURCC_I420:
1879fe8aea9eSmrg	case FOURCC_XVMC:
1880fe8aea9eSmrg		return video->colorspace ?
1881fe8aea9eSmrg			GEN7_WM_KERNEL_VIDEO_PLANAR_BT709 :
1882fe8aea9eSmrg			GEN7_WM_KERNEL_VIDEO_PLANAR_BT601;
1883fe8aea9eSmrg
1884fe8aea9eSmrg	case FOURCC_NV12:
1885fe8aea9eSmrg		return video->colorspace ?
1886fe8aea9eSmrg			GEN7_WM_KERNEL_VIDEO_NV12_BT709 :
1887fe8aea9eSmrg			GEN7_WM_KERNEL_VIDEO_NV12_BT601;
1888fe8aea9eSmrg
1889fe8aea9eSmrg	case FOURCC_RGB888:
1890fe8aea9eSmrg	case FOURCC_RGB565:
1891fe8aea9eSmrg		return GEN7_WM_KERNEL_VIDEO_RGB;
1892fe8aea9eSmrg
1893fe8aea9eSmrg	default:
1894fe8aea9eSmrg		return video->colorspace ?
1895fe8aea9eSmrg			GEN7_WM_KERNEL_VIDEO_PACKED_BT709 :
1896fe8aea9eSmrg			GEN7_WM_KERNEL_VIDEO_PACKED_BT601;
1897fe8aea9eSmrg	}
1898fe8aea9eSmrg}
1899fe8aea9eSmrg
190003b705cfSriastradhstatic bool
190103b705cfSriastradhgen7_render_video(struct sna *sna,
190203b705cfSriastradh		  struct sna_video *video,
190303b705cfSriastradh		  struct sna_video_frame *frame,
190403b705cfSriastradh		  RegionPtr dstRegion,
190503b705cfSriastradh		  PixmapPtr pixmap)
190603b705cfSriastradh{
190703b705cfSriastradh	struct sna_composite_op tmp;
190842542f5fSchristos	struct sna_pixmap *priv = sna_pixmap(pixmap);
190903b705cfSriastradh	int dst_width = dstRegion->extents.x2 - dstRegion->extents.x1;
191003b705cfSriastradh	int dst_height = dstRegion->extents.y2 - dstRegion->extents.y1;
191103b705cfSriastradh	int src_width = frame->src.x2 - frame->src.x1;
191203b705cfSriastradh	int src_height = frame->src.y2 - frame->src.y1;
191303b705cfSriastradh	float src_offset_x, src_offset_y;
191403b705cfSriastradh	float src_scale_x, src_scale_y;
191503b705cfSriastradh	unsigned filter;
191642542f5fSchristos	const BoxRec *box;
1917fe8aea9eSmrg	int nbox;
191803b705cfSriastradh
191942542f5fSchristos	DBG(("%s: src=(%d, %d), dst=(%d, %d), %dx[(%d, %d), (%d, %d)...]\n",
192003b705cfSriastradh	     __FUNCTION__,
192103b705cfSriastradh	     src_width, src_height, dst_width, dst_height,
192242542f5fSchristos	     region_num_rects(dstRegion),
192303b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x1,
192403b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y1,
192503b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x2,
192603b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y2));
192703b705cfSriastradh
192842542f5fSchristos	assert(priv->gpu_bo);
192903b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
193003b705cfSriastradh
193103b705cfSriastradh	tmp.dst.pixmap = pixmap;
193203b705cfSriastradh	tmp.dst.width  = pixmap->drawable.width;
193303b705cfSriastradh	tmp.dst.height = pixmap->drawable.height;
193403b705cfSriastradh	tmp.dst.format = sna_render_format_for_depth(pixmap->drawable.depth);
193503b705cfSriastradh	tmp.dst.bo = priv->gpu_bo;
193603b705cfSriastradh
193703b705cfSriastradh	tmp.src.bo = frame->bo;
193803b705cfSriastradh	tmp.mask.bo = NULL;
193903b705cfSriastradh
194003b705cfSriastradh	tmp.floats_per_vertex = 3;
194103b705cfSriastradh	tmp.floats_per_rect = 9;
194203b705cfSriastradh
194303b705cfSriastradh	if (src_width == dst_width && src_height == dst_height)
194403b705cfSriastradh		filter = SAMPLER_FILTER_NEAREST;
194503b705cfSriastradh	else
194603b705cfSriastradh		filter = SAMPLER_FILTER_BILINEAR;
194703b705cfSriastradh
194803b705cfSriastradh	tmp.u.gen7.flags =
194903b705cfSriastradh		GEN7_SET_FLAGS(SAMPLER_OFFSET(filter, SAMPLER_EXTEND_PAD,
195003b705cfSriastradh					      SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE),
195103b705cfSriastradh			       NO_BLEND,
1952fe8aea9eSmrg			       select_video_kernel(video, frame),
195303b705cfSriastradh			       2);
195403b705cfSriastradh	tmp.priv = frame;
195503b705cfSriastradh
195603b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
195703b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL)) {
195803b705cfSriastradh		kgem_submit(&sna->kgem);
195942542f5fSchristos		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL))
196042542f5fSchristos			return false;
196142542f5fSchristos
196203b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
196303b705cfSriastradh	}
196403b705cfSriastradh
196503b705cfSriastradh	gen7_align_vertex(sna, &tmp);
196642542f5fSchristos	gen7_emit_video_state(sna, &tmp);
196703b705cfSriastradh
196803b705cfSriastradh	DBG(("%s: src=(%d, %d)x(%d, %d); frame=(%dx%d), dst=(%dx%d)\n",
196903b705cfSriastradh	     __FUNCTION__,
197003b705cfSriastradh	     frame->src.x1, frame->src.y1,
197103b705cfSriastradh	     src_width, src_height,
197203b705cfSriastradh	     dst_width, dst_height,
197303b705cfSriastradh	     frame->width, frame->height));
197403b705cfSriastradh
197503b705cfSriastradh	src_scale_x = (float)src_width / dst_width / frame->width;
197603b705cfSriastradh	src_offset_x = (float)frame->src.x1 / frame->width - dstRegion->extents.x1 * src_scale_x;
197703b705cfSriastradh
197803b705cfSriastradh	src_scale_y = (float)src_height / dst_height / frame->height;
197903b705cfSriastradh	src_offset_y = (float)frame->src.y1 / frame->height - dstRegion->extents.y1 * src_scale_y;
198003b705cfSriastradh
198103b705cfSriastradh	DBG(("%s: scale=(%f, %f), offset=(%f, %f)\n",
198203b705cfSriastradh	     __FUNCTION__,
198303b705cfSriastradh	     src_scale_x, src_scale_y,
198403b705cfSriastradh	     src_offset_x, src_offset_y));
198503b705cfSriastradh
198642542f5fSchristos	box = region_rects(dstRegion);
198742542f5fSchristos	nbox = region_num_rects(dstRegion);
198803b705cfSriastradh	while (nbox--) {
1989fe8aea9eSmrg		DBG(("%s: dst=(%d, %d), (%d, %d); src=(%f, %f), (%f, %f)\n",
199003b705cfSriastradh		     __FUNCTION__,
199103b705cfSriastradh		     box->x1, box->y1,
199203b705cfSriastradh		     box->x2, box->y2,
199303b705cfSriastradh		     box->x1 * src_scale_x + src_offset_x,
199403b705cfSriastradh		     box->y1 * src_scale_y + src_offset_y,
199503b705cfSriastradh		     box->x2 * src_scale_x + src_offset_x,
199603b705cfSriastradh		     box->y2 * src_scale_y + src_offset_y));
199703b705cfSriastradh
199803b705cfSriastradh		gen7_get_rectangles(sna, &tmp, 1, gen7_emit_video_state);
199903b705cfSriastradh
2000fe8aea9eSmrg		OUT_VERTEX(box->x2, box->y2);
200103b705cfSriastradh		OUT_VERTEX_F(box->x2 * src_scale_x + src_offset_x);
200203b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
200303b705cfSriastradh
2004fe8aea9eSmrg		OUT_VERTEX(box->x1, box->y2);
200503b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
200603b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
200703b705cfSriastradh
2008fe8aea9eSmrg		OUT_VERTEX(box->x1, box->y1);
200903b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
201003b705cfSriastradh		OUT_VERTEX_F(box->y1 * src_scale_y + src_offset_y);
201103b705cfSriastradh
201203b705cfSriastradh		box++;
201303b705cfSriastradh	}
201403b705cfSriastradh	gen4_vertex_flush(sna);
2015fe8aea9eSmrg
2016fe8aea9eSmrg	if (!DAMAGE_IS_ALL(priv->gpu_damage))
2017fe8aea9eSmrg		sna_damage_add(&priv->gpu_damage, dstRegion);
2018fe8aea9eSmrg
201903b705cfSriastradh	return true;
202003b705cfSriastradh}
202103b705cfSriastradh
202203b705cfSriastradhstatic int
202303b705cfSriastradhgen7_composite_picture(struct sna *sna,
202403b705cfSriastradh		       PicturePtr picture,
202503b705cfSriastradh		       struct sna_composite_channel *channel,
202603b705cfSriastradh		       int x, int y,
202703b705cfSriastradh		       int w, int h,
202803b705cfSriastradh		       int dst_x, int dst_y,
202903b705cfSriastradh		       bool precise)
203003b705cfSriastradh{
203103b705cfSriastradh	PixmapPtr pixmap;
203203b705cfSriastradh	uint32_t color;
203303b705cfSriastradh	int16_t dx, dy;
203403b705cfSriastradh
203503b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d), dst=(%d, %d)\n",
203603b705cfSriastradh	     __FUNCTION__, x, y, w, h, dst_x, dst_y));
203703b705cfSriastradh
203803b705cfSriastradh	channel->is_solid = false;
203903b705cfSriastradh	channel->card_format = -1;
204003b705cfSriastradh
204103b705cfSriastradh	if (sna_picture_is_solid(picture, &color))
204203b705cfSriastradh		return gen4_channel_init_solid(sna, channel, color);
204303b705cfSriastradh
204403b705cfSriastradh	if (picture->pDrawable == NULL) {
204503b705cfSriastradh		int ret;
204603b705cfSriastradh
204703b705cfSriastradh		if (picture->pSourcePict->type == SourcePictTypeLinear)
204803b705cfSriastradh			return gen4_channel_init_linear(sna, picture, channel,
204903b705cfSriastradh							x, y,
205003b705cfSriastradh							w, h,
205103b705cfSriastradh							dst_x, dst_y);
205203b705cfSriastradh
205303b705cfSriastradh		DBG(("%s -- fixup, gradient\n", __FUNCTION__));
205403b705cfSriastradh		ret = -1;
205503b705cfSriastradh		if (!precise)
205603b705cfSriastradh			ret = sna_render_picture_approximate_gradient(sna, picture, channel,
205703b705cfSriastradh								      x, y, w, h, dst_x, dst_y);
205803b705cfSriastradh		if (ret == -1)
205903b705cfSriastradh			ret = sna_render_picture_fixup(sna, picture, channel,
206003b705cfSriastradh						       x, y, w, h, dst_x, dst_y);
206103b705cfSriastradh		return ret;
206203b705cfSriastradh	}
206303b705cfSriastradh
206403b705cfSriastradh	if (picture->alphaMap) {
206503b705cfSriastradh		DBG(("%s -- fallback, alphamap\n", __FUNCTION__));
206603b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
206703b705cfSriastradh						x, y, w, h, dst_x, dst_y);
206803b705cfSriastradh	}
206903b705cfSriastradh
207003b705cfSriastradh	if (!gen7_check_repeat(picture))
207103b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
207203b705cfSriastradh						x, y, w, h, dst_x, dst_y);
207303b705cfSriastradh
207403b705cfSriastradh	if (!gen7_check_filter(picture))
207503b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
207603b705cfSriastradh						x, y, w, h, dst_x, dst_y);
207703b705cfSriastradh
207803b705cfSriastradh	channel->repeat = picture->repeat ? picture->repeatType : RepeatNone;
207903b705cfSriastradh	channel->filter = picture->filter;
208003b705cfSriastradh
208142542f5fSchristos	assert(picture->pDrawable);
208203b705cfSriastradh	pixmap = get_drawable_pixmap(picture->pDrawable);
208303b705cfSriastradh	get_drawable_deltas(picture->pDrawable, pixmap, &dx, &dy);
208403b705cfSriastradh
208503b705cfSriastradh	x += dx + picture->pDrawable->x;
208603b705cfSriastradh	y += dy + picture->pDrawable->y;
208703b705cfSriastradh
208803b705cfSriastradh	channel->is_affine = sna_transform_is_affine(picture->transform);
208942542f5fSchristos	if (sna_transform_is_imprecise_integer_translation(picture->transform, picture->filter, precise, &dx, &dy)) {
209003b705cfSriastradh		DBG(("%s: integer translation (%d, %d), removing\n",
209103b705cfSriastradh		     __FUNCTION__, dx, dy));
209203b705cfSriastradh		x += dx;
209303b705cfSriastradh		y += dy;
209403b705cfSriastradh		channel->transform = NULL;
209503b705cfSriastradh		channel->filter = PictFilterNearest;
209642542f5fSchristos
209742542f5fSchristos		if (channel->repeat ||
209842542f5fSchristos		    (x >= 0 &&
209942542f5fSchristos		     y >= 0 &&
2100fe8aea9eSmrg		     x + w <= pixmap->drawable.width &&
2101fe8aea9eSmrg		     y + h <= pixmap->drawable.height)) {
210242542f5fSchristos			struct sna_pixmap *priv = sna_pixmap(pixmap);
210342542f5fSchristos			if (priv && priv->clear) {
210442542f5fSchristos				DBG(("%s: converting large pixmap source into solid [%08x]\n", __FUNCTION__, priv->clear_color));
2105fe8aea9eSmrg				return gen4_channel_init_solid(sna, channel,
2106fe8aea9eSmrg							       solid_color(picture->format, priv->clear_color));
210742542f5fSchristos			}
210842542f5fSchristos		}
210903b705cfSriastradh	} else
211003b705cfSriastradh		channel->transform = picture->transform;
211103b705cfSriastradh
211203b705cfSriastradh	channel->pict_format = picture->format;
211303b705cfSriastradh	channel->card_format = gen7_get_card_format(picture->format);
211403b705cfSriastradh	if (channel->card_format == (unsigned)-1)
211503b705cfSriastradh		return sna_render_picture_convert(sna, picture, channel, pixmap,
211603b705cfSriastradh						  x, y, w, h, dst_x, dst_y,
211703b705cfSriastradh						  false);
211803b705cfSriastradh
211903b705cfSriastradh	if (too_large(pixmap->drawable.width, pixmap->drawable.height)) {
212003b705cfSriastradh		DBG(("%s: extracting from pixmap %dx%d\n", __FUNCTION__,
212103b705cfSriastradh		     pixmap->drawable.width, pixmap->drawable.height));
212203b705cfSriastradh		return sna_render_picture_extract(sna, picture, channel,
212303b705cfSriastradh						  x, y, w, h, dst_x, dst_y);
212403b705cfSriastradh	}
212503b705cfSriastradh
212603b705cfSriastradh	DBG(("%s: pixmap, repeat=%d, filter=%d, transform?=%d [affine? %d], format=%08x\n",
212703b705cfSriastradh	     __FUNCTION__,
212803b705cfSriastradh	     channel->repeat, channel->filter,
212903b705cfSriastradh	     channel->transform != NULL, channel->is_affine,
213003b705cfSriastradh	     channel->pict_format));
213103b705cfSriastradh	if (channel->transform) {
213203b705cfSriastradh		DBG(("%s: transform=[%f %f %f, %f %f %f, %f %f %f]\n",
213303b705cfSriastradh		     __FUNCTION__,
213403b705cfSriastradh		     channel->transform->matrix[0][0] / 65536.,
213503b705cfSriastradh		     channel->transform->matrix[0][1] / 65536.,
213603b705cfSriastradh		     channel->transform->matrix[0][2] / 65536.,
213703b705cfSriastradh		     channel->transform->matrix[1][0] / 65536.,
213803b705cfSriastradh		     channel->transform->matrix[1][1] / 65536.,
213903b705cfSriastradh		     channel->transform->matrix[1][2] / 65536.,
214003b705cfSriastradh		     channel->transform->matrix[2][0] / 65536.,
214103b705cfSriastradh		     channel->transform->matrix[2][1] / 65536.,
214203b705cfSriastradh		     channel->transform->matrix[2][2] / 65536.));
214303b705cfSriastradh	}
214403b705cfSriastradh
214503b705cfSriastradh	return sna_render_pixmap_bo(sna, channel, pixmap,
214603b705cfSriastradh				    x, y, w, h, dst_x, dst_y);
214703b705cfSriastradh}
214803b705cfSriastradh
214942542f5fSchristosinline static void gen7_composite_channel_convert(struct sna_composite_channel *channel)
215003b705cfSriastradh{
215103b705cfSriastradh	channel->repeat = gen7_repeat(channel->repeat);
215203b705cfSriastradh	channel->filter = gen7_filter(channel->filter);
215303b705cfSriastradh	if (channel->card_format == (unsigned)-1)
215403b705cfSriastradh		channel->card_format = gen7_get_card_format(channel->pict_format);
215503b705cfSriastradh	assert(channel->card_format != (unsigned)-1);
215603b705cfSriastradh}
215703b705cfSriastradh
215803b705cfSriastradhstatic void gen7_render_composite_done(struct sna *sna,
215903b705cfSriastradh				       const struct sna_composite_op *op)
216003b705cfSriastradh{
216103b705cfSriastradh	if (sna->render.vertex_offset) {
216203b705cfSriastradh		gen4_vertex_flush(sna);
216303b705cfSriastradh		gen7_magic_ca_pass(sna, op);
216403b705cfSriastradh	}
216503b705cfSriastradh
216603b705cfSriastradh	if (op->mask.bo)
216703b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->mask.bo);
216803b705cfSriastradh	if (op->src.bo)
216903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->src.bo);
217003b705cfSriastradh
217103b705cfSriastradh	sna_render_composite_redirect_done(sna, op);
217203b705cfSriastradh}
217303b705cfSriastradh
217442542f5fSchristosinline static bool
217503b705cfSriastradhgen7_composite_set_target(struct sna *sna,
217603b705cfSriastradh			  struct sna_composite_op *op,
217703b705cfSriastradh			  PicturePtr dst,
217803b705cfSriastradh			  int x, int y, int w, int h,
217903b705cfSriastradh			  bool partial)
218003b705cfSriastradh{
218103b705cfSriastradh	BoxRec box;
218242542f5fSchristos	unsigned int hint;
218342542f5fSchristos
218442542f5fSchristos	DBG(("%s: (%d, %d)x(%d, %d), partial?=%d\n", __FUNCTION__, x, y, w, h, partial));
218503b705cfSriastradh
218603b705cfSriastradh	op->dst.pixmap = get_drawable_pixmap(dst->pDrawable);
218703b705cfSriastradh	op->dst.format = dst->format;
218803b705cfSriastradh	op->dst.width  = op->dst.pixmap->drawable.width;
218903b705cfSriastradh	op->dst.height = op->dst.pixmap->drawable.height;
219003b705cfSriastradh
219103b705cfSriastradh	if (w | h) {
219203b705cfSriastradh		assert(w && h);
219303b705cfSriastradh		box.x1 = x;
219403b705cfSriastradh		box.y1 = y;
219503b705cfSriastradh		box.x2 = x + w;
219603b705cfSriastradh		box.y2 = y + h;
219703b705cfSriastradh	} else
219803b705cfSriastradh		sna_render_picture_extents(dst, &box);
219903b705cfSriastradh
2200fe8aea9eSmrg	hint = PREFER_GPU | RENDER_GPU;
2201fe8aea9eSmrg	if (!need_tiling(sna, op->dst.width, op->dst.height))
2202fe8aea9eSmrg		hint |= FORCE_GPU;
220342542f5fSchristos	if (!partial) {
220442542f5fSchristos		hint |= IGNORE_DAMAGE;
220542542f5fSchristos		if (w == op->dst.width && h == op->dst.height)
220642542f5fSchristos			hint |= REPLACES;
220742542f5fSchristos	}
220842542f5fSchristos
220942542f5fSchristos	op->dst.bo = sna_drawable_use_bo(dst->pDrawable, hint, &box, &op->damage);
221003b705cfSriastradh	if (op->dst.bo == NULL)
221103b705cfSriastradh		return false;
221203b705cfSriastradh
221342542f5fSchristos	if (hint & REPLACES) {
221442542f5fSchristos		struct sna_pixmap *priv = sna_pixmap(op->dst.pixmap);
221542542f5fSchristos		kgem_bo_pair_undo(&sna->kgem, priv->gpu_bo, priv->cpu_bo);
221642542f5fSchristos	}
221742542f5fSchristos
221803b705cfSriastradh	get_drawable_deltas(dst->pDrawable, op->dst.pixmap,
221903b705cfSriastradh			    &op->dst.x, &op->dst.y);
222003b705cfSriastradh
222142542f5fSchristos	DBG(("%s: pixmap=%ld, format=%08x, size=%dx%d, pitch=%d, delta=(%d,%d),damage=%p\n",
222203b705cfSriastradh	     __FUNCTION__,
222342542f5fSchristos	     op->dst.pixmap->drawable.serialNumber, (int)op->dst.format,
222403b705cfSriastradh	     op->dst.width, op->dst.height,
222503b705cfSriastradh	     op->dst.bo->pitch,
222603b705cfSriastradh	     op->dst.x, op->dst.y,
222703b705cfSriastradh	     op->damage ? *op->damage : (void *)-1));
222803b705cfSriastradh
222903b705cfSriastradh	assert(op->dst.bo->proxy == NULL);
223003b705cfSriastradh
223103b705cfSriastradh	if (too_large(op->dst.width, op->dst.height) &&
223203b705cfSriastradh	    !sna_render_composite_redirect(sna, op, x, y, w, h, partial))
223303b705cfSriastradh		return false;
223403b705cfSriastradh
223503b705cfSriastradh	return true;
223603b705cfSriastradh}
223703b705cfSriastradh
223803b705cfSriastradhstatic bool
223903b705cfSriastradhtry_blt(struct sna *sna,
2240fe8aea9eSmrg	uint8_t op,
2241fe8aea9eSmrg	PicturePtr src,
2242fe8aea9eSmrg	PicturePtr mask,
2243fe8aea9eSmrg	PicturePtr dst,
2244fe8aea9eSmrg	int16_t src_x, int16_t src_y,
2245fe8aea9eSmrg	int16_t msk_x, int16_t msk_y,
2246fe8aea9eSmrg	int16_t dst_x, int16_t dst_y,
2247fe8aea9eSmrg	int16_t width, int16_t height,
2248fe8aea9eSmrg	unsigned flags,
2249fe8aea9eSmrg	struct sna_composite_op *tmp)
225003b705cfSriastradh{
225103b705cfSriastradh	struct kgem_bo *bo;
225203b705cfSriastradh
225342542f5fSchristos	if (sna->kgem.mode == KGEM_BLT) {
225403b705cfSriastradh		DBG(("%s: already performing BLT\n", __FUNCTION__));
2255fe8aea9eSmrg		goto execute;
225603b705cfSriastradh	}
225703b705cfSriastradh
225803b705cfSriastradh	if (too_large(width, height)) {
225903b705cfSriastradh		DBG(("%s: operation too large for 3D pipe (%d, %d)\n",
226003b705cfSriastradh		     __FUNCTION__, width, height));
2261fe8aea9eSmrg		goto execute;
226203b705cfSriastradh	}
226303b705cfSriastradh
226442542f5fSchristos	bo = __sna_drawable_peek_bo(dst->pDrawable);
226542542f5fSchristos	if (bo == NULL)
2266fe8aea9eSmrg		goto execute;
2267fe8aea9eSmrg
2268fe8aea9eSmrg	if (untiled_tlb_miss(bo))
2269fe8aea9eSmrg		goto execute;
2270fe8aea9eSmrg
2271fe8aea9eSmrg	if (bo->rq) {
2272fe8aea9eSmrg		if (RQ_IS_BLT(bo->rq))
2273fe8aea9eSmrg			goto execute;
2274fe8aea9eSmrg
2275fe8aea9eSmrg		return false;
2276fe8aea9eSmrg	}
2277fe8aea9eSmrg
2278fe8aea9eSmrg	if (bo->tiling == I915_TILING_Y)
2279fe8aea9eSmrg		goto upload;
2280fe8aea9eSmrg
2281fe8aea9eSmrg	if (src->pDrawable == dst->pDrawable &&
2282fe8aea9eSmrg	    (sna->render_state.gt < 3 || width*height < 1024) &&
2283fe8aea9eSmrg	    can_switch_to_blt(sna, bo, 0))
2284fe8aea9eSmrg		goto execute;
228542542f5fSchristos
228642542f5fSchristos	if (sna_picture_is_solid(src, NULL) && can_switch_to_blt(sna, bo, 0))
2287fe8aea9eSmrg		goto execute;
228803b705cfSriastradh
228942542f5fSchristos	if (src->pDrawable) {
2290fe8aea9eSmrg		struct kgem_bo *s = __sna_drawable_peek_bo(src->pDrawable);
2291fe8aea9eSmrg		if (s == NULL)
2292fe8aea9eSmrg			goto upload;
229342542f5fSchristos
2294fe8aea9eSmrg		if (prefer_blt_bo(sna, s, bo))
2295fe8aea9eSmrg			goto execute;
229642542f5fSchristos	}
229742542f5fSchristos
229842542f5fSchristos	if (sna->kgem.ring == KGEM_BLT) {
229942542f5fSchristos		DBG(("%s: already performing BLT\n", __FUNCTION__));
2300fe8aea9eSmrg		goto execute;
230142542f5fSchristos	}
230242542f5fSchristos
2303fe8aea9eSmrgupload:
2304fe8aea9eSmrg	flags |= COMPOSITE_UPLOAD;
2305fe8aea9eSmrgexecute:
2306fe8aea9eSmrg	return sna_blt_composite(sna, op,
2307fe8aea9eSmrg				 src, dst,
2308fe8aea9eSmrg				 src_x, src_y,
2309fe8aea9eSmrg				 dst_x, dst_y,
2310fe8aea9eSmrg				 width, height,
2311fe8aea9eSmrg				 flags, tmp);
231203b705cfSriastradh}
231303b705cfSriastradh
231403b705cfSriastradhstatic bool
231503b705cfSriastradhcheck_gradient(PicturePtr picture, bool precise)
231603b705cfSriastradh{
231703b705cfSriastradh	if (picture->pDrawable)
231803b705cfSriastradh		return false;
231903b705cfSriastradh
232003b705cfSriastradh	switch (picture->pSourcePict->type) {
232103b705cfSriastradh	case SourcePictTypeSolidFill:
232203b705cfSriastradh	case SourcePictTypeLinear:
232303b705cfSriastradh		return false;
232403b705cfSriastradh	default:
232503b705cfSriastradh		return precise;
232603b705cfSriastradh	}
232703b705cfSriastradh}
232803b705cfSriastradh
232903b705cfSriastradhstatic bool
233003b705cfSriastradhhas_alphamap(PicturePtr p)
233103b705cfSriastradh{
233203b705cfSriastradh	return p->alphaMap != NULL;
233303b705cfSriastradh}
233403b705cfSriastradh
233503b705cfSriastradhstatic bool
233603b705cfSriastradhneed_upload(PicturePtr p)
233703b705cfSriastradh{
233803b705cfSriastradh	return p->pDrawable && unattached(p->pDrawable) && untransformed(p);
233903b705cfSriastradh}
234003b705cfSriastradh
234103b705cfSriastradhstatic bool
234203b705cfSriastradhsource_is_busy(PixmapPtr pixmap)
234303b705cfSriastradh{
234403b705cfSriastradh	struct sna_pixmap *priv = sna_pixmap(pixmap);
234503b705cfSriastradh	if (priv == NULL || priv->clear)
234603b705cfSriastradh		return false;
234703b705cfSriastradh
234803b705cfSriastradh	if (priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo))
234903b705cfSriastradh		return true;
235003b705cfSriastradh
235103b705cfSriastradh	if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
235203b705cfSriastradh		return true;
235303b705cfSriastradh
235403b705cfSriastradh	return priv->gpu_damage && !priv->cpu_damage;
235503b705cfSriastradh}
235603b705cfSriastradh
235703b705cfSriastradhstatic bool
235803b705cfSriastradhsource_fallback(PicturePtr p, PixmapPtr pixmap, bool precise)
235903b705cfSriastradh{
236003b705cfSriastradh	if (sna_picture_is_solid(p, NULL))
236103b705cfSriastradh		return false;
236203b705cfSriastradh
236303b705cfSriastradh	if (p->pSourcePict)
236403b705cfSriastradh		return check_gradient(p, precise);
236503b705cfSriastradh
236603b705cfSriastradh	if (!gen7_check_repeat(p) || !gen7_check_format(p->format))
236703b705cfSriastradh		return true;
236803b705cfSriastradh
236903b705cfSriastradh	if (pixmap && source_is_busy(pixmap))
237003b705cfSriastradh		return false;
237103b705cfSriastradh
237203b705cfSriastradh	return has_alphamap(p) || !gen7_check_filter(p) || need_upload(p);
237303b705cfSriastradh}
237403b705cfSriastradh
237503b705cfSriastradhstatic bool
237603b705cfSriastradhgen7_composite_fallback(struct sna *sna,
237703b705cfSriastradh			PicturePtr src,
237803b705cfSriastradh			PicturePtr mask,
237903b705cfSriastradh			PicturePtr dst)
238003b705cfSriastradh{
238103b705cfSriastradh	PixmapPtr src_pixmap;
238203b705cfSriastradh	PixmapPtr mask_pixmap;
238303b705cfSriastradh	PixmapPtr dst_pixmap;
238403b705cfSriastradh	bool src_fallback, mask_fallback;
238503b705cfSriastradh
238603b705cfSriastradh	if (!gen7_check_dst_format(dst->format)) {
238703b705cfSriastradh		DBG(("%s: unknown destination format: %d\n",
238803b705cfSriastradh		     __FUNCTION__, dst->format));
238903b705cfSriastradh		return true;
239003b705cfSriastradh	}
239103b705cfSriastradh
239203b705cfSriastradh	dst_pixmap = get_drawable_pixmap(dst->pDrawable);
239303b705cfSriastradh
239403b705cfSriastradh	src_pixmap = src->pDrawable ? get_drawable_pixmap(src->pDrawable) : NULL;
239503b705cfSriastradh	src_fallback = source_fallback(src, src_pixmap,
239603b705cfSriastradh				       dst->polyMode == PolyModePrecise);
239703b705cfSriastradh
239803b705cfSriastradh	if (mask) {
239903b705cfSriastradh		mask_pixmap = mask->pDrawable ? get_drawable_pixmap(mask->pDrawable) : NULL;
240003b705cfSriastradh		mask_fallback = source_fallback(mask, mask_pixmap,
240103b705cfSriastradh						dst->polyMode == PolyModePrecise);
240203b705cfSriastradh	} else {
240303b705cfSriastradh		mask_pixmap = NULL;
240403b705cfSriastradh		mask_fallback = false;
240503b705cfSriastradh	}
240603b705cfSriastradh
240703b705cfSriastradh	/* If we are using the destination as a source and need to
240803b705cfSriastradh	 * readback in order to upload the source, do it all
240903b705cfSriastradh	 * on the cpu.
241003b705cfSriastradh	 */
241103b705cfSriastradh	if (src_pixmap == dst_pixmap && src_fallback) {
241203b705cfSriastradh		DBG(("%s: src is dst and will fallback\n",__FUNCTION__));
241303b705cfSriastradh		return true;
241403b705cfSriastradh	}
241503b705cfSriastradh	if (mask_pixmap == dst_pixmap && mask_fallback) {
241603b705cfSriastradh		DBG(("%s: mask is dst and will fallback\n",__FUNCTION__));
241703b705cfSriastradh		return true;
241803b705cfSriastradh	}
241903b705cfSriastradh
242003b705cfSriastradh	/* If anything is on the GPU, push everything out to the GPU */
242103b705cfSriastradh	if (dst_use_gpu(dst_pixmap)) {
242203b705cfSriastradh		DBG(("%s: dst is already on the GPU, try to use GPU\n",
242303b705cfSriastradh		     __FUNCTION__));
242403b705cfSriastradh		return false;
242503b705cfSriastradh	}
242603b705cfSriastradh
242703b705cfSriastradh	if (src_pixmap && !src_fallback) {
242803b705cfSriastradh		DBG(("%s: src is already on the GPU, try to use GPU\n",
242903b705cfSriastradh		     __FUNCTION__));
243003b705cfSriastradh		return false;
243103b705cfSriastradh	}
243203b705cfSriastradh	if (mask_pixmap && !mask_fallback) {
243303b705cfSriastradh		DBG(("%s: mask is already on the GPU, try to use GPU\n",
243403b705cfSriastradh		     __FUNCTION__));
243503b705cfSriastradh		return false;
243603b705cfSriastradh	}
243703b705cfSriastradh
243803b705cfSriastradh	/* However if the dst is not on the GPU and we need to
243903b705cfSriastradh	 * render one of the sources using the CPU, we may
244003b705cfSriastradh	 * as well do the entire operation in place onthe CPU.
244103b705cfSriastradh	 */
244203b705cfSriastradh	if (src_fallback) {
244303b705cfSriastradh		DBG(("%s: dst is on the CPU and src will fallback\n",
244403b705cfSriastradh		     __FUNCTION__));
244503b705cfSriastradh		return true;
244603b705cfSriastradh	}
244703b705cfSriastradh
244803b705cfSriastradh	if (mask && mask_fallback) {
244903b705cfSriastradh		DBG(("%s: dst is on the CPU and mask will fallback\n",
245003b705cfSriastradh		     __FUNCTION__));
245103b705cfSriastradh		return true;
245203b705cfSriastradh	}
245303b705cfSriastradh
245403b705cfSriastradh	if (too_large(dst_pixmap->drawable.width,
245503b705cfSriastradh		      dst_pixmap->drawable.height) &&
245603b705cfSriastradh	    dst_is_cpu(dst_pixmap)) {
245703b705cfSriastradh		DBG(("%s: dst is on the CPU and too large\n", __FUNCTION__));
245803b705cfSriastradh		return true;
245903b705cfSriastradh	}
246003b705cfSriastradh
246103b705cfSriastradh	DBG(("%s: dst is not on the GPU and the operation should not fallback\n",
246203b705cfSriastradh	     __FUNCTION__));
246303b705cfSriastradh	return dst_use_cpu(dst_pixmap);
246403b705cfSriastradh}
246503b705cfSriastradh
246603b705cfSriastradhstatic int
246703b705cfSriastradhreuse_source(struct sna *sna,
246803b705cfSriastradh	     PicturePtr src, struct sna_composite_channel *sc, int src_x, int src_y,
246903b705cfSriastradh	     PicturePtr mask, struct sna_composite_channel *mc, int msk_x, int msk_y)
247003b705cfSriastradh{
247103b705cfSriastradh	uint32_t color;
247203b705cfSriastradh
247303b705cfSriastradh	if (src_x != msk_x || src_y != msk_y)
247403b705cfSriastradh		return false;
247503b705cfSriastradh
247603b705cfSriastradh	if (src == mask) {
247703b705cfSriastradh		DBG(("%s: mask is source\n", __FUNCTION__));
247803b705cfSriastradh		*mc = *sc;
247903b705cfSriastradh		mc->bo = kgem_bo_reference(mc->bo);
248003b705cfSriastradh		return true;
248103b705cfSriastradh	}
248203b705cfSriastradh
248303b705cfSriastradh	if (sna_picture_is_solid(mask, &color))
248403b705cfSriastradh		return gen4_channel_init_solid(sna, mc, color);
248503b705cfSriastradh
248603b705cfSriastradh	if (sc->is_solid)
248703b705cfSriastradh		return false;
248803b705cfSriastradh
248903b705cfSriastradh	if (src->pDrawable == NULL || mask->pDrawable != src->pDrawable)
249003b705cfSriastradh		return false;
249103b705cfSriastradh
249203b705cfSriastradh	DBG(("%s: mask reuses source drawable\n", __FUNCTION__));
249303b705cfSriastradh
249403b705cfSriastradh	if (!sna_transform_equal(src->transform, mask->transform))
249503b705cfSriastradh		return false;
249603b705cfSriastradh
249703b705cfSriastradh	if (!sna_picture_alphamap_equal(src, mask))
249803b705cfSriastradh		return false;
249903b705cfSriastradh
250003b705cfSriastradh	if (!gen7_check_repeat(mask))
250103b705cfSriastradh		return false;
250203b705cfSriastradh
250303b705cfSriastradh	if (!gen7_check_filter(mask))
250403b705cfSriastradh		return false;
250503b705cfSriastradh
250603b705cfSriastradh	if (!gen7_check_format(mask->format))
250703b705cfSriastradh		return false;
250803b705cfSriastradh
250903b705cfSriastradh	DBG(("%s: reusing source channel for mask with a twist\n",
251003b705cfSriastradh	     __FUNCTION__));
251103b705cfSriastradh
251203b705cfSriastradh	*mc = *sc;
251303b705cfSriastradh	mc->repeat = gen7_repeat(mask->repeat ? mask->repeatType : RepeatNone);
251403b705cfSriastradh	mc->filter = gen7_filter(mask->filter);
251503b705cfSriastradh	mc->pict_format = mask->format;
251603b705cfSriastradh	mc->card_format = gen7_get_card_format(mask->format);
251703b705cfSriastradh	mc->bo = kgem_bo_reference(mc->bo);
251803b705cfSriastradh	return true;
251903b705cfSriastradh}
252003b705cfSriastradh
252103b705cfSriastradhstatic bool
252203b705cfSriastradhgen7_render_composite(struct sna *sna,
252303b705cfSriastradh		      uint8_t op,
252403b705cfSriastradh		      PicturePtr src,
252503b705cfSriastradh		      PicturePtr mask,
252603b705cfSriastradh		      PicturePtr dst,
252703b705cfSriastradh		      int16_t src_x, int16_t src_y,
252803b705cfSriastradh		      int16_t msk_x, int16_t msk_y,
252903b705cfSriastradh		      int16_t dst_x, int16_t dst_y,
253003b705cfSriastradh		      int16_t width, int16_t height,
253142542f5fSchristos		      unsigned flags,
253203b705cfSriastradh		      struct sna_composite_op *tmp)
253303b705cfSriastradh{
253403b705cfSriastradh	if (op >= ARRAY_SIZE(gen7_blend_op))
253503b705cfSriastradh		return false;
253603b705cfSriastradh
253703b705cfSriastradh	DBG(("%s: %dx%d, current mode=%d/%d\n", __FUNCTION__,
253803b705cfSriastradh	     width, height, sna->kgem.mode, sna->kgem.ring));
253903b705cfSriastradh
254003b705cfSriastradh	if (mask == NULL &&
2541fe8aea9eSmrg	    try_blt(sna, op,
2542fe8aea9eSmrg		    src, mask, dst,
2543fe8aea9eSmrg		    src_x, src_y,
2544fe8aea9eSmrg		    msk_x, msk_y,
2545fe8aea9eSmrg		    dst_x, dst_y,
2546fe8aea9eSmrg		    width, height,
2547fe8aea9eSmrg		    flags, tmp))
254803b705cfSriastradh		return true;
254903b705cfSriastradh
255003b705cfSriastradh	if (gen7_composite_fallback(sna, src, mask, dst))
255142542f5fSchristos		goto fallback;
255203b705cfSriastradh
255303b705cfSriastradh	if (need_tiling(sna, width, height))
255403b705cfSriastradh		return sna_tiling_composite(op, src, mask, dst,
255503b705cfSriastradh					    src_x, src_y,
255603b705cfSriastradh					    msk_x, msk_y,
255703b705cfSriastradh					    dst_x, dst_y,
255803b705cfSriastradh					    width, height,
255903b705cfSriastradh					    tmp);
256003b705cfSriastradh
256142542f5fSchristos	if (op == PictOpClear && src == sna->clear)
256203b705cfSriastradh		op = PictOpSrc;
256303b705cfSriastradh	tmp->op = op;
256403b705cfSriastradh	if (!gen7_composite_set_target(sna, tmp, dst,
256503b705cfSriastradh				       dst_x, dst_y, width, height,
256642542f5fSchristos				       flags & COMPOSITE_PARTIAL || op > PictOpSrc))
256742542f5fSchristos		goto fallback;
256803b705cfSriastradh
256903b705cfSriastradh	switch (gen7_composite_picture(sna, src, &tmp->src,
257003b705cfSriastradh				       src_x, src_y,
257103b705cfSriastradh				       width, height,
257203b705cfSriastradh				       dst_x, dst_y,
257303b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
257403b705cfSriastradh	case -1:
257503b705cfSriastradh		goto cleanup_dst;
257603b705cfSriastradh	case 0:
257703b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->src, 0))
257803b705cfSriastradh			goto cleanup_dst;
257903b705cfSriastradh		/* fall through to fixup */
258003b705cfSriastradh	case 1:
258103b705cfSriastradh		/* Did we just switch rings to prepare the source? */
258203b705cfSriastradh		if (mask == NULL &&
258303b705cfSriastradh		    prefer_blt_composite(sna, tmp) &&
258403b705cfSriastradh		    sna_blt_composite__convert(sna,
258503b705cfSriastradh					       dst_x, dst_y, width, height,
258603b705cfSriastradh					       tmp))
258703b705cfSriastradh			return true;
258803b705cfSriastradh
258903b705cfSriastradh		gen7_composite_channel_convert(&tmp->src);
259003b705cfSriastradh		break;
259103b705cfSriastradh	}
259203b705cfSriastradh
259303b705cfSriastradh	tmp->is_affine = tmp->src.is_affine;
259403b705cfSriastradh	tmp->has_component_alpha = false;
259503b705cfSriastradh	tmp->need_magic_ca_pass = false;
259603b705cfSriastradh
259703b705cfSriastradh	tmp->mask.bo = NULL;
259803b705cfSriastradh	tmp->mask.filter = SAMPLER_FILTER_NEAREST;
259903b705cfSriastradh	tmp->mask.repeat = SAMPLER_EXTEND_NONE;
260003b705cfSriastradh
260103b705cfSriastradh	if (mask) {
260203b705cfSriastradh		if (mask->componentAlpha && PICT_FORMAT_RGB(mask->format)) {
260303b705cfSriastradh			tmp->has_component_alpha = true;
260403b705cfSriastradh
260503b705cfSriastradh			/* Check if it's component alpha that relies on a source alpha and on
260603b705cfSriastradh			 * the source value.  We can only get one of those into the single
260703b705cfSriastradh			 * source value that we get to blend with.
260803b705cfSriastradh			 */
260903b705cfSriastradh			if (gen7_blend_op[op].src_alpha &&
261003b705cfSriastradh			    (gen7_blend_op[op].src_blend != GEN7_BLENDFACTOR_ZERO)) {
261103b705cfSriastradh				if (op != PictOpOver)
261203b705cfSriastradh					goto cleanup_src;
261303b705cfSriastradh
261403b705cfSriastradh				tmp->need_magic_ca_pass = true;
261503b705cfSriastradh				tmp->op = PictOpOutReverse;
261603b705cfSriastradh			}
261703b705cfSriastradh		}
261803b705cfSriastradh
261903b705cfSriastradh		if (!reuse_source(sna,
262003b705cfSriastradh				  src, &tmp->src, src_x, src_y,
262103b705cfSriastradh				  mask, &tmp->mask, msk_x, msk_y)) {
262203b705cfSriastradh			switch (gen7_composite_picture(sna, mask, &tmp->mask,
262303b705cfSriastradh						       msk_x, msk_y,
262403b705cfSriastradh						       width, height,
262503b705cfSriastradh						       dst_x, dst_y,
262603b705cfSriastradh						       dst->polyMode == PolyModePrecise)) {
262703b705cfSriastradh			case -1:
262803b705cfSriastradh				goto cleanup_src;
262903b705cfSriastradh			case 0:
263003b705cfSriastradh				if (!gen4_channel_init_solid(sna, &tmp->mask, 0))
263103b705cfSriastradh					goto cleanup_src;
263203b705cfSriastradh				/* fall through to fixup */
263303b705cfSriastradh			case 1:
263403b705cfSriastradh				gen7_composite_channel_convert(&tmp->mask);
263503b705cfSriastradh				break;
263603b705cfSriastradh			}
263703b705cfSriastradh		}
263803b705cfSriastradh
263903b705cfSriastradh		tmp->is_affine &= tmp->mask.is_affine;
264003b705cfSriastradh	}
264103b705cfSriastradh
264203b705cfSriastradh	tmp->u.gen7.flags =
264303b705cfSriastradh		GEN7_SET_FLAGS(SAMPLER_OFFSET(tmp->src.filter,
264403b705cfSriastradh					      tmp->src.repeat,
264503b705cfSriastradh					      tmp->mask.filter,
264603b705cfSriastradh					      tmp->mask.repeat),
264703b705cfSriastradh			       gen7_get_blend(tmp->op,
264803b705cfSriastradh					      tmp->has_component_alpha,
264903b705cfSriastradh					      tmp->dst.format),
265003b705cfSriastradh			       gen7_choose_composite_kernel(tmp->op,
265103b705cfSriastradh							    tmp->mask.bo != NULL,
265203b705cfSriastradh							    tmp->has_component_alpha,
265303b705cfSriastradh							    tmp->is_affine),
265403b705cfSriastradh			       gen4_choose_composite_emitter(sna, tmp));
265503b705cfSriastradh
265603b705cfSriastradh	tmp->blt   = gen7_render_composite_blt;
265703b705cfSriastradh	tmp->box   = gen7_render_composite_box;
265803b705cfSriastradh	tmp->boxes = gen7_render_composite_boxes__blt;
265903b705cfSriastradh	if (tmp->emit_boxes){
266003b705cfSriastradh		tmp->boxes = gen7_render_composite_boxes;
266103b705cfSriastradh		tmp->thread_boxes = gen7_render_composite_boxes__thread;
266203b705cfSriastradh	}
266303b705cfSriastradh	tmp->done  = gen7_render_composite_done;
266403b705cfSriastradh
266503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->dst.bo);
266603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
266703b705cfSriastradh			   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
266803b705cfSriastradh			   NULL)) {
266903b705cfSriastradh		kgem_submit(&sna->kgem);
267003b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
267103b705cfSriastradh				   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
267203b705cfSriastradh				   NULL))
267303b705cfSriastradh			goto cleanup_mask;
267403b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
267503b705cfSriastradh	}
267603b705cfSriastradh
267703b705cfSriastradh	gen7_align_vertex(sna, tmp);
267842542f5fSchristos	gen7_emit_composite_state(sna, tmp);
267903b705cfSriastradh	return true;
268003b705cfSriastradh
268103b705cfSriastradhcleanup_mask:
268242542f5fSchristos	if (tmp->mask.bo) {
268303b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->mask.bo);
268442542f5fSchristos		tmp->mask.bo = NULL;
268542542f5fSchristos	}
268603b705cfSriastradhcleanup_src:
268742542f5fSchristos	if (tmp->src.bo) {
268803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->src.bo);
268942542f5fSchristos		tmp->src.bo = NULL;
269042542f5fSchristos	}
269103b705cfSriastradhcleanup_dst:
269242542f5fSchristos	if (tmp->redirect.real_bo) {
269303b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->dst.bo);
269442542f5fSchristos		tmp->redirect.real_bo = NULL;
269542542f5fSchristos	}
269642542f5fSchristosfallback:
269742542f5fSchristos	return (mask == NULL &&
269842542f5fSchristos		sna_blt_composite(sna, op,
269942542f5fSchristos				  src, dst,
270042542f5fSchristos				  src_x, src_y,
270142542f5fSchristos				  dst_x, dst_y,
270242542f5fSchristos				  width, height,
270342542f5fSchristos				  flags | COMPOSITE_FALLBACK, tmp));
270403b705cfSriastradh}
270503b705cfSriastradh
270603b705cfSriastradh#if !NO_COMPOSITE_SPANS
270703b705cfSriastradhfastcall static void
270803b705cfSriastradhgen7_render_composite_spans_box(struct sna *sna,
270903b705cfSriastradh				const struct sna_composite_spans_op *op,
271003b705cfSriastradh				const BoxRec *box, float opacity)
271103b705cfSriastradh{
271203b705cfSriastradh	DBG(("%s: src=+(%d, %d), opacity=%f, dst=+(%d, %d), box=(%d, %d) x (%d, %d)\n",
271303b705cfSriastradh	     __FUNCTION__,
271403b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
271503b705cfSriastradh	     opacity,
271603b705cfSriastradh	     op->base.dst.x, op->base.dst.y,
271703b705cfSriastradh	     box->x1, box->y1,
271803b705cfSriastradh	     box->x2 - box->x1,
271903b705cfSriastradh	     box->y2 - box->y1));
272003b705cfSriastradh
272103b705cfSriastradh	gen7_get_rectangles(sna, &op->base, 1, gen7_emit_composite_state);
272203b705cfSriastradh	op->prim_emit(sna, op, box, opacity);
272303b705cfSriastradh}
272403b705cfSriastradh
272503b705cfSriastradhstatic void
272603b705cfSriastradhgen7_render_composite_spans_boxes(struct sna *sna,
272703b705cfSriastradh				  const struct sna_composite_spans_op *op,
272803b705cfSriastradh				  const BoxRec *box, int nbox,
272903b705cfSriastradh				  float opacity)
273003b705cfSriastradh{
273103b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), opacity=%f, dst=+(%d, %d)\n",
273203b705cfSriastradh	     __FUNCTION__, nbox,
273303b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
273403b705cfSriastradh	     opacity,
273503b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
273603b705cfSriastradh
273703b705cfSriastradh	do {
273803b705cfSriastradh		int nbox_this_time;
273903b705cfSriastradh
274003b705cfSriastradh		nbox_this_time = gen7_get_rectangles(sna, &op->base, nbox,
274103b705cfSriastradh						     gen7_emit_composite_state);
274203b705cfSriastradh		nbox -= nbox_this_time;
274303b705cfSriastradh
274403b705cfSriastradh		do {
274503b705cfSriastradh			DBG(("  %s: (%d, %d) x (%d, %d)\n", __FUNCTION__,
274603b705cfSriastradh			     box->x1, box->y1,
274703b705cfSriastradh			     box->x2 - box->x1,
274803b705cfSriastradh			     box->y2 - box->y1));
274903b705cfSriastradh
275003b705cfSriastradh			op->prim_emit(sna, op, box++, opacity);
275103b705cfSriastradh		} while (--nbox_this_time);
275203b705cfSriastradh	} while (nbox);
275303b705cfSriastradh}
275403b705cfSriastradh
275503b705cfSriastradhfastcall static void
275603b705cfSriastradhgen7_render_composite_spans_boxes__thread(struct sna *sna,
275703b705cfSriastradh					  const struct sna_composite_spans_op *op,
275803b705cfSriastradh					  const struct sna_opacity_box *box,
275903b705cfSriastradh					  int nbox)
276003b705cfSriastradh{
276103b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), dst=+(%d, %d)\n",
276203b705cfSriastradh	     __FUNCTION__, nbox,
276303b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
276403b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
276503b705cfSriastradh
276603b705cfSriastradh	sna_vertex_lock(&sna->render);
276703b705cfSriastradh	do {
276803b705cfSriastradh		int nbox_this_time;
276903b705cfSriastradh		float *v;
277003b705cfSriastradh
277103b705cfSriastradh		nbox_this_time = gen7_get_rectangles(sna, &op->base, nbox,
277203b705cfSriastradh						     gen7_emit_composite_state);
277303b705cfSriastradh		assert(nbox_this_time);
277403b705cfSriastradh		nbox -= nbox_this_time;
277503b705cfSriastradh
277603b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
277703b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->base.floats_per_rect;
277803b705cfSriastradh
277903b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
278003b705cfSriastradh		sna_vertex_unlock(&sna->render);
278103b705cfSriastradh
278203b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
278303b705cfSriastradh		box += nbox_this_time;
278403b705cfSriastradh
278503b705cfSriastradh		sna_vertex_lock(&sna->render);
278603b705cfSriastradh		sna_vertex_release__locked(&sna->render);
278703b705cfSriastradh	} while (nbox);
278803b705cfSriastradh	sna_vertex_unlock(&sna->render);
278903b705cfSriastradh}
279003b705cfSriastradh
279103b705cfSriastradhfastcall static void
279203b705cfSriastradhgen7_render_composite_spans_done(struct sna *sna,
279303b705cfSriastradh				 const struct sna_composite_spans_op *op)
279403b705cfSriastradh{
279503b705cfSriastradh	if (sna->render.vertex_offset)
279603b705cfSriastradh		gen4_vertex_flush(sna);
279703b705cfSriastradh
279803b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
279903b705cfSriastradh
280003b705cfSriastradh	if (op->base.src.bo)
280103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->base.src.bo);
280203b705cfSriastradh
280303b705cfSriastradh	sna_render_composite_redirect_done(sna, &op->base);
280403b705cfSriastradh}
280503b705cfSriastradh
280603b705cfSriastradhstatic bool
280703b705cfSriastradhgen7_check_composite_spans(struct sna *sna,
280803b705cfSriastradh			   uint8_t op, PicturePtr src, PicturePtr dst,
280903b705cfSriastradh			   int16_t width, int16_t height, unsigned flags)
281003b705cfSriastradh{
281103b705cfSriastradh	if (op >= ARRAY_SIZE(gen7_blend_op))
281203b705cfSriastradh		return false;
281303b705cfSriastradh
281403b705cfSriastradh	if (gen7_composite_fallback(sna, src, NULL, dst))
281503b705cfSriastradh		return false;
281603b705cfSriastradh
281703b705cfSriastradh	if (need_tiling(sna, width, height) &&
281803b705cfSriastradh	    !is_gpu(sna, dst->pDrawable, PREFER_GPU_SPANS)) {
281903b705cfSriastradh		DBG(("%s: fallback, tiled operation not on GPU\n",
282003b705cfSriastradh		     __FUNCTION__));
282103b705cfSriastradh		return false;
282203b705cfSriastradh	}
282303b705cfSriastradh
282403b705cfSriastradh	return true;
282503b705cfSriastradh}
282603b705cfSriastradh
282703b705cfSriastradhstatic bool
282803b705cfSriastradhgen7_render_composite_spans(struct sna *sna,
282903b705cfSriastradh			    uint8_t op,
283003b705cfSriastradh			    PicturePtr src,
283103b705cfSriastradh			    PicturePtr dst,
283203b705cfSriastradh			    int16_t src_x,  int16_t src_y,
283303b705cfSriastradh			    int16_t dst_x,  int16_t dst_y,
283403b705cfSriastradh			    int16_t width,  int16_t height,
283503b705cfSriastradh			    unsigned flags,
283603b705cfSriastradh			    struct sna_composite_spans_op *tmp)
283703b705cfSriastradh{
283803b705cfSriastradh	DBG(("%s: %dx%d with flags=%x, current mode=%d/%d\n", __FUNCTION__,
283903b705cfSriastradh	     width, height, flags, sna->kgem.mode, sna->kgem.ring));
284003b705cfSriastradh
284103b705cfSriastradh	assert(gen7_check_composite_spans(sna, op, src, dst, width, height, flags));
284203b705cfSriastradh
284303b705cfSriastradh	if (need_tiling(sna, width, height)) {
284403b705cfSriastradh		DBG(("%s: tiling, operation (%dx%d) too wide for pipeline\n",
284503b705cfSriastradh		     __FUNCTION__, width, height));
284603b705cfSriastradh		return sna_tiling_composite_spans(op, src, dst,
284703b705cfSriastradh						  src_x, src_y, dst_x, dst_y,
284803b705cfSriastradh						  width, height, flags, tmp);
284903b705cfSriastradh	}
285003b705cfSriastradh
285103b705cfSriastradh	tmp->base.op = op;
285203b705cfSriastradh	if (!gen7_composite_set_target(sna, &tmp->base, dst,
285303b705cfSriastradh				       dst_x, dst_y, width, height, true))
285403b705cfSriastradh		return false;
285503b705cfSriastradh
285603b705cfSriastradh	switch (gen7_composite_picture(sna, src, &tmp->base.src,
285703b705cfSriastradh				       src_x, src_y,
285803b705cfSriastradh				       width, height,
285903b705cfSriastradh				       dst_x, dst_y,
286003b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
286103b705cfSriastradh	case -1:
286203b705cfSriastradh		goto cleanup_dst;
286303b705cfSriastradh	case 0:
286403b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->base.src, 0))
286503b705cfSriastradh			goto cleanup_dst;
286603b705cfSriastradh		/* fall through to fixup */
286703b705cfSriastradh	case 1:
286803b705cfSriastradh		gen7_composite_channel_convert(&tmp->base.src);
286903b705cfSriastradh		break;
287003b705cfSriastradh	}
287103b705cfSriastradh	tmp->base.mask.bo = NULL;
287203b705cfSriastradh
287303b705cfSriastradh	tmp->base.is_affine = tmp->base.src.is_affine;
287403b705cfSriastradh	tmp->base.need_magic_ca_pass = false;
287503b705cfSriastradh
287603b705cfSriastradh	tmp->base.u.gen7.flags =
287703b705cfSriastradh		GEN7_SET_FLAGS(SAMPLER_OFFSET(tmp->base.src.filter,
287803b705cfSriastradh					      tmp->base.src.repeat,
287903b705cfSriastradh					      SAMPLER_FILTER_NEAREST,
288003b705cfSriastradh					      SAMPLER_EXTEND_PAD),
288103b705cfSriastradh			       gen7_get_blend(tmp->base.op, false, tmp->base.dst.format),
288203b705cfSriastradh			       GEN7_WM_KERNEL_OPACITY | !tmp->base.is_affine,
288303b705cfSriastradh			       gen4_choose_spans_emitter(sna, tmp));
288403b705cfSriastradh
288503b705cfSriastradh	tmp->box   = gen7_render_composite_spans_box;
288603b705cfSriastradh	tmp->boxes = gen7_render_composite_spans_boxes;
288703b705cfSriastradh	if (tmp->emit_boxes)
288803b705cfSriastradh		tmp->thread_boxes = gen7_render_composite_spans_boxes__thread;
288903b705cfSriastradh	tmp->done  = gen7_render_composite_spans_done;
289003b705cfSriastradh
289103b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->base.dst.bo);
289203b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
289303b705cfSriastradh			   tmp->base.dst.bo, tmp->base.src.bo,
289403b705cfSriastradh			   NULL)) {
289503b705cfSriastradh		kgem_submit(&sna->kgem);
289603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
289703b705cfSriastradh				   tmp->base.dst.bo, tmp->base.src.bo,
289803b705cfSriastradh				   NULL))
289903b705cfSriastradh			goto cleanup_src;
290003b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
290103b705cfSriastradh	}
290203b705cfSriastradh
290303b705cfSriastradh	gen7_align_vertex(sna, &tmp->base);
290442542f5fSchristos	gen7_emit_composite_state(sna, &tmp->base);
290503b705cfSriastradh	return true;
290603b705cfSriastradh
290703b705cfSriastradhcleanup_src:
290803b705cfSriastradh	if (tmp->base.src.bo)
290903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.src.bo);
291003b705cfSriastradhcleanup_dst:
291103b705cfSriastradh	if (tmp->base.redirect.real_bo)
291203b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.dst.bo);
291303b705cfSriastradh	return false;
291403b705cfSriastradh}
291503b705cfSriastradh#endif
291603b705cfSriastradh
291703b705cfSriastradhstatic void
291803b705cfSriastradhgen7_emit_copy_state(struct sna *sna,
291903b705cfSriastradh		     const struct sna_composite_op *op)
292003b705cfSriastradh{
292103b705cfSriastradh	uint32_t *binding_table;
292242542f5fSchristos	uint16_t offset, dirty;
292303b705cfSriastradh
292403b705cfSriastradh	gen7_get_batch(sna, op);
292503b705cfSriastradh
292603b705cfSriastradh	binding_table = gen7_composite_get_binding_table(sna, &offset);
292703b705cfSriastradh
292842542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
292942542f5fSchristos
293003b705cfSriastradh	binding_table[0] =
293103b705cfSriastradh		gen7_bind_bo(sna,
293203b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
293303b705cfSriastradh			     gen7_get_dest_format(op->dst.format),
293403b705cfSriastradh			     true);
293503b705cfSriastradh	binding_table[1] =
293603b705cfSriastradh		gen7_bind_bo(sna,
293703b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
293803b705cfSriastradh			     op->src.card_format,
293903b705cfSriastradh			     false);
294003b705cfSriastradh
294103b705cfSriastradh	if (sna->kgem.surface == offset &&
294203b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen7.surface_table) == *(uint64_t*)binding_table) {
294303b705cfSriastradh		sna->kgem.surface += sizeof(struct gen7_surface_state) / sizeof(uint32_t);
294403b705cfSriastradh		offset = sna->render_state.gen7.surface_table;
294503b705cfSriastradh	}
294603b705cfSriastradh
294742542f5fSchristos	if (sna->kgem.batch[sna->render_state.gen7.surface_table] == binding_table[0])
294842542f5fSchristos		dirty = 0;
294942542f5fSchristos
295003b705cfSriastradh	assert(!GEN7_READS_DST(op->u.gen7.flags));
295142542f5fSchristos	gen7_emit_state(sna, op, offset | dirty);
295203b705cfSriastradh}
295303b705cfSriastradh
295442542f5fSchristosstatic inline bool
295542542f5fSchristosprefer_blt_copy(struct sna *sna,
295642542f5fSchristos		struct kgem_bo *src_bo,
295742542f5fSchristos		struct kgem_bo *dst_bo,
295842542f5fSchristos		unsigned flags)
295903b705cfSriastradh{
296003b705cfSriastradh	if (sna->kgem.mode == KGEM_BLT)
296103b705cfSriastradh		return true;
296203b705cfSriastradh
296303b705cfSriastradh	assert((flags & COPY_SYNC) == 0);
296403b705cfSriastradh
296503b705cfSriastradh	if (untiled_tlb_miss(src_bo) ||
296603b705cfSriastradh	    untiled_tlb_miss(dst_bo))
296703b705cfSriastradh		return true;
296803b705cfSriastradh
2969fe8aea9eSmrg	if (flags & COPY_DRI && !sna->kgem.has_semaphores)
2970fe8aea9eSmrg		return false;
2971fe8aea9eSmrg
2972fe8aea9eSmrg	if (force_blt_ring(sna, dst_bo))
2973fe8aea9eSmrg		return true;
2974fe8aea9eSmrg
2975fe8aea9eSmrg	if ((flags & COPY_SMALL ||
2976fe8aea9eSmrg	     (sna->render_state.gt < 3 && src_bo == dst_bo)) &&
2977fe8aea9eSmrg            can_switch_to_blt(sna, dst_bo, flags))
297842542f5fSchristos		return true;
297942542f5fSchristos
298003b705cfSriastradh	if (kgem_bo_is_render(dst_bo) ||
298103b705cfSriastradh	    kgem_bo_is_render(src_bo))
298203b705cfSriastradh		return false;
298303b705cfSriastradh
2984fe8aea9eSmrg	if (flags & COPY_LAST &&
2985fe8aea9eSmrg	    sna->render_state.gt < 3 &&
2986fe8aea9eSmrg            can_switch_to_blt(sna, dst_bo, flags))
2987fe8aea9eSmrg		return true;
2988fe8aea9eSmrg
298942542f5fSchristos	if (prefer_render_ring(sna, dst_bo))
299003b705cfSriastradh		return false;
299103b705cfSriastradh
299242542f5fSchristos	if (!prefer_blt_ring(sna, dst_bo, flags))
299303b705cfSriastradh		return false;
299403b705cfSriastradh
2995fe8aea9eSmrg	return prefer_blt_bo(sna, src_bo, dst_bo);
299603b705cfSriastradh}
299703b705cfSriastradh
299803b705cfSriastradhstatic bool
299903b705cfSriastradhgen7_render_copy_boxes(struct sna *sna, uint8_t alu,
300042542f5fSchristos		       const DrawableRec *src, struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
300142542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
300203b705cfSriastradh		       const BoxRec *box, int n, unsigned flags)
300303b705cfSriastradh{
300403b705cfSriastradh	struct sna_composite_op tmp;
300503b705cfSriastradh	BoxRec extents;
300603b705cfSriastradh
300703b705cfSriastradh	DBG(("%s (%d, %d)->(%d, %d) x %d, alu=%x, flags=%x, self-copy=%d, overlaps? %d\n",
300803b705cfSriastradh	     __FUNCTION__, src_dx, src_dy, dst_dx, dst_dy, n, alu, flags,
300903b705cfSriastradh	     src_bo == dst_bo,
301003b705cfSriastradh	     overlaps(sna,
301103b705cfSriastradh		      src_bo, src_dx, src_dy,
301203b705cfSriastradh		      dst_bo, dst_dx, dst_dy,
301342542f5fSchristos		      box, n, flags, &extents)));
301403b705cfSriastradh
301503b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, flags) &&
301642542f5fSchristos	    sna_blt_compare_depth(src, dst) &&
301703b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
301803b705cfSriastradh			       src_bo, src_dx, src_dy,
301903b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
302042542f5fSchristos			       dst->bitsPerPixel,
302103b705cfSriastradh			       box, n))
302203b705cfSriastradh		return true;
302303b705cfSriastradh
302403b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear)) {
302503b705cfSriastradhfallback_blt:
302642542f5fSchristos		DBG(("%s: fallback blt\n", __FUNCTION__));
302742542f5fSchristos		if (!sna_blt_compare_depth(src, dst))
302803b705cfSriastradh			return false;
302903b705cfSriastradh
303003b705cfSriastradh		return sna_blt_copy_boxes_fallback(sna, alu,
303103b705cfSriastradh						   src, src_bo, src_dx, src_dy,
303203b705cfSriastradh						   dst, dst_bo, dst_dx, dst_dy,
303303b705cfSriastradh						   box, n);
303403b705cfSriastradh	}
303503b705cfSriastradh
303603b705cfSriastradh	if (overlaps(sna,
303703b705cfSriastradh		     src_bo, src_dx, src_dy,
303803b705cfSriastradh		     dst_bo, dst_dx, dst_dy,
303942542f5fSchristos		     box, n, flags,
304042542f5fSchristos		     &extents)) {
304142542f5fSchristos		bool big = too_large(extents.x2-extents.x1, extents.y2-extents.y1);
304203b705cfSriastradh
3043fe8aea9eSmrg		if ((big || !prefer_render_ring(sna, dst_bo)) &&
304403b705cfSriastradh		    sna_blt_copy_boxes(sna, alu,
304503b705cfSriastradh				       src_bo, src_dx, src_dy,
304603b705cfSriastradh				       dst_bo, dst_dx, dst_dy,
304742542f5fSchristos				       dst->bitsPerPixel,
304803b705cfSriastradh				       box, n))
304903b705cfSriastradh			return true;
305003b705cfSriastradh
305142542f5fSchristos		if (big)
305242542f5fSchristos			goto fallback_blt;
305342542f5fSchristos
305442542f5fSchristos		assert(src_bo == dst_bo);
305542542f5fSchristos		assert(src->depth == dst->depth);
305642542f5fSchristos		assert(src->width == dst->width);
305742542f5fSchristos		assert(src->height == dst->height);
3058fe8aea9eSmrg		return sna_render_copy_boxes__overlap(sna, alu, dst, dst_bo,
305942542f5fSchristos						      src_dx, src_dy,
306042542f5fSchristos						      dst_dx, dst_dy,
306103b705cfSriastradh						      box, n, &extents);
306203b705cfSriastradh	}
306303b705cfSriastradh
306442542f5fSchristos	if (dst->depth == src->depth) {
306542542f5fSchristos		tmp.dst.format = sna_render_format_for_depth(dst->depth);
306603b705cfSriastradh		tmp.src.pict_format = tmp.dst.format;
306703b705cfSriastradh	} else {
306842542f5fSchristos		tmp.dst.format = sna_format_for_depth(dst->depth);
306942542f5fSchristos		tmp.src.pict_format = sna_format_for_depth(src->depth);
307003b705cfSriastradh	}
307103b705cfSriastradh	if (!gen7_check_format(tmp.src.pict_format))
307203b705cfSriastradh		goto fallback_blt;
307303b705cfSriastradh
307442542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
307542542f5fSchristos	tmp.dst.width  = dst->width;
307642542f5fSchristos	tmp.dst.height = dst->height;
307703b705cfSriastradh	tmp.dst.bo = dst_bo;
307803b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
307903b705cfSriastradh	tmp.damage = NULL;
308003b705cfSriastradh
308103b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
308203b705cfSriastradh	if (too_large(tmp.dst.width, tmp.dst.height)) {
308303b705cfSriastradh		int i;
308403b705cfSriastradh
308503b705cfSriastradh		extents = box[0];
308603b705cfSriastradh		for (i = 1; i < n; i++) {
308703b705cfSriastradh			if (box[i].x1 < extents.x1)
308803b705cfSriastradh				extents.x1 = box[i].x1;
308903b705cfSriastradh			if (box[i].y1 < extents.y1)
309003b705cfSriastradh				extents.y1 = box[i].y1;
309103b705cfSriastradh
309203b705cfSriastradh			if (box[i].x2 > extents.x2)
309303b705cfSriastradh				extents.x2 = box[i].x2;
309403b705cfSriastradh			if (box[i].y2 > extents.y2)
309503b705cfSriastradh				extents.y2 = box[i].y2;
309603b705cfSriastradh		}
309703b705cfSriastradh
309803b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
309903b705cfSriastradh						   extents.x1 + dst_dx,
310003b705cfSriastradh						   extents.y1 + dst_dy,
310103b705cfSriastradh						   extents.x2 - extents.x1,
310203b705cfSriastradh						   extents.y2 - extents.y1,
310303b705cfSriastradh						   n > 1))
310403b705cfSriastradh			goto fallback_tiled;
310503b705cfSriastradh	}
310603b705cfSriastradh
310703b705cfSriastradh	tmp.src.card_format = gen7_get_card_format(tmp.src.pict_format);
310842542f5fSchristos	if (too_large(src->width, src->height)) {
310903b705cfSriastradh		int i;
311003b705cfSriastradh
311103b705cfSriastradh		extents = box[0];
311203b705cfSriastradh		for (i = 1; i < n; i++) {
311303b705cfSriastradh			if (box[i].x1 < extents.x1)
311403b705cfSriastradh				extents.x1 = box[i].x1;
311503b705cfSriastradh			if (box[i].y1 < extents.y1)
311603b705cfSriastradh				extents.y1 = box[i].y1;
311703b705cfSriastradh
311803b705cfSriastradh			if (box[i].x2 > extents.x2)
311903b705cfSriastradh				extents.x2 = box[i].x2;
312003b705cfSriastradh			if (box[i].y2 > extents.y2)
312103b705cfSriastradh				extents.y2 = box[i].y2;
312203b705cfSriastradh		}
312303b705cfSriastradh
312403b705cfSriastradh		if (!sna_render_pixmap_partial(sna, src, src_bo, &tmp.src,
312503b705cfSriastradh					       extents.x1 + src_dx,
312603b705cfSriastradh					       extents.y1 + src_dy,
312703b705cfSriastradh					       extents.x2 - extents.x1,
312803b705cfSriastradh					       extents.y2 - extents.y1))
312903b705cfSriastradh			goto fallback_tiled_dst;
313003b705cfSriastradh	} else {
313103b705cfSriastradh		tmp.src.bo = src_bo;
313242542f5fSchristos		tmp.src.width  = src->width;
313342542f5fSchristos		tmp.src.height = src->height;
313442542f5fSchristos		tmp.src.offset[0] = tmp.src.offset[1] = 0;
313503b705cfSriastradh	}
313603b705cfSriastradh
313703b705cfSriastradh	tmp.mask.bo = NULL;
313803b705cfSriastradh
313903b705cfSriastradh	tmp.floats_per_vertex = 2;
314003b705cfSriastradh	tmp.floats_per_rect = 6;
314103b705cfSriastradh	tmp.need_magic_ca_pass = 0;
314203b705cfSriastradh
314303b705cfSriastradh	tmp.u.gen7.flags = COPY_FLAGS(alu);
314403b705cfSriastradh
314503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
314603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
314703b705cfSriastradh		kgem_submit(&sna->kgem);
314842542f5fSchristos		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
314942542f5fSchristos			if (tmp.src.bo != src_bo)
315042542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.src.bo);
315142542f5fSchristos			if (tmp.redirect.real_bo)
315242542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
315342542f5fSchristos			goto fallback_blt;
315442542f5fSchristos		}
315503b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
315603b705cfSriastradh	}
315703b705cfSriastradh
315842542f5fSchristos	src_dx += tmp.src.offset[0];
315942542f5fSchristos	src_dy += tmp.src.offset[1];
316042542f5fSchristos
316142542f5fSchristos	dst_dx += tmp.dst.x;
316242542f5fSchristos	dst_dy += tmp.dst.y;
316342542f5fSchristos
316442542f5fSchristos	tmp.dst.x = tmp.dst.y = 0;
316542542f5fSchristos
316603b705cfSriastradh	gen7_align_vertex(sna, &tmp);
316742542f5fSchristos	gen7_emit_copy_state(sna, &tmp);
316803b705cfSriastradh
316903b705cfSriastradh	do {
317003b705cfSriastradh		int16_t *v;
317103b705cfSriastradh		int n_this_time;
317203b705cfSriastradh
317303b705cfSriastradh		n_this_time = gen7_get_rectangles(sna, &tmp, n,
317403b705cfSriastradh						  gen7_emit_copy_state);
317503b705cfSriastradh		n -= n_this_time;
317603b705cfSriastradh
317703b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
317803b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
317903b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
318003b705cfSriastradh		do {
318103b705cfSriastradh
318203b705cfSriastradh			DBG(("	(%d, %d) -> (%d, %d) + (%d, %d)\n",
318303b705cfSriastradh			     box->x1 + src_dx, box->y1 + src_dy,
318403b705cfSriastradh			     box->x1 + dst_dx, box->y1 + dst_dy,
318503b705cfSriastradh			     box->x2 - box->x1, box->y2 - box->y1));
318603b705cfSriastradh			v[0] = box->x2 + dst_dx;
318703b705cfSriastradh			v[2] = box->x2 + src_dx;
318803b705cfSriastradh			v[1]  = v[5] = box->y2 + dst_dy;
318903b705cfSriastradh			v[3]  = v[7] = box->y2 + src_dy;
319003b705cfSriastradh			v[8]  = v[4] = box->x1 + dst_dx;
319103b705cfSriastradh			v[10] = v[6] = box->x1 + src_dx;
319203b705cfSriastradh			v[9]  = box->y1 + dst_dy;
319303b705cfSriastradh			v[11] = box->y1 + src_dy;
319403b705cfSriastradh			v += 12; box++;
319503b705cfSriastradh		} while (--n_this_time);
319603b705cfSriastradh	} while (n);
319703b705cfSriastradh
319803b705cfSriastradh	gen4_vertex_flush(sna);
319903b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
320003b705cfSriastradh	if (tmp.src.bo != src_bo)
320103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.src.bo);
320203b705cfSriastradh	return true;
320303b705cfSriastradh
320403b705cfSriastradhfallback_tiled_dst:
320503b705cfSriastradh	if (tmp.redirect.real_bo)
320603b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
320703b705cfSriastradhfallback_tiled:
320842542f5fSchristos	DBG(("%s: fallback tiled\n", __FUNCTION__));
320942542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
321003b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
321103b705cfSriastradh			       src_bo, src_dx, src_dy,
321203b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
321342542f5fSchristos			       dst->bitsPerPixel,
321403b705cfSriastradh			       box, n))
321503b705cfSriastradh		return true;
321603b705cfSriastradh
321703b705cfSriastradh	return sna_tiling_copy_boxes(sna, alu,
321803b705cfSriastradh				     src, src_bo, src_dx, src_dy,
321903b705cfSriastradh				     dst, dst_bo, dst_dx, dst_dy,
322003b705cfSriastradh				     box, n);
322103b705cfSriastradh}
322203b705cfSriastradh
322303b705cfSriastradhstatic void
322403b705cfSriastradhgen7_render_copy_blt(struct sna *sna,
322503b705cfSriastradh		     const struct sna_copy_op *op,
322603b705cfSriastradh		     int16_t sx, int16_t sy,
322703b705cfSriastradh		     int16_t w,  int16_t h,
322803b705cfSriastradh		     int16_t dx, int16_t dy)
322903b705cfSriastradh{
323003b705cfSriastradh	int16_t *v;
323103b705cfSriastradh
323203b705cfSriastradh	gen7_get_rectangles(sna, &op->base, 1, gen7_emit_copy_state);
323303b705cfSriastradh
323403b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
323503b705cfSriastradh	sna->render.vertex_used += 6;
323603b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
323703b705cfSriastradh
323803b705cfSriastradh	v[0]  = dx+w; v[1]  = dy+h;
323903b705cfSriastradh	v[2]  = sx+w; v[3]  = sy+h;
324003b705cfSriastradh	v[4]  = dx;   v[5]  = dy+h;
324103b705cfSriastradh	v[6]  = sx;   v[7]  = sy+h;
324203b705cfSriastradh	v[8]  = dx;   v[9]  = dy;
324303b705cfSriastradh	v[10] = sx;   v[11] = sy;
324403b705cfSriastradh}
324503b705cfSriastradh
324603b705cfSriastradhstatic void
324703b705cfSriastradhgen7_render_copy_done(struct sna *sna, const struct sna_copy_op *op)
324803b705cfSriastradh{
324903b705cfSriastradh	if (sna->render.vertex_offset)
325003b705cfSriastradh		gen4_vertex_flush(sna);
325103b705cfSriastradh}
325203b705cfSriastradh
325303b705cfSriastradhstatic bool
325403b705cfSriastradhgen7_render_copy(struct sna *sna, uint8_t alu,
325503b705cfSriastradh		 PixmapPtr src, struct kgem_bo *src_bo,
325603b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
325703b705cfSriastradh		 struct sna_copy_op *op)
325803b705cfSriastradh{
325903b705cfSriastradh	DBG(("%s (alu=%d, src=(%dx%d), dst=(%dx%d))\n",
326003b705cfSriastradh	     __FUNCTION__, alu,
326103b705cfSriastradh	     src->drawable.width, src->drawable.height,
326203b705cfSriastradh	     dst->drawable.width, dst->drawable.height));
326303b705cfSriastradh
326403b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, 0) &&
326503b705cfSriastradh	    sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
326603b705cfSriastradh	    sna_blt_copy(sna, alu,
326703b705cfSriastradh			 src_bo, dst_bo,
326803b705cfSriastradh			 dst->drawable.bitsPerPixel,
326903b705cfSriastradh			 op))
327003b705cfSriastradh		return true;
327103b705cfSriastradh
327203b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo ||
327303b705cfSriastradh	    too_large(src->drawable.width, src->drawable.height) ||
327403b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height)) {
327503b705cfSriastradhfallback:
327603b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
327703b705cfSriastradh			return false;
327803b705cfSriastradh
327903b705cfSriastradh		return sna_blt_copy(sna, alu, src_bo, dst_bo,
328003b705cfSriastradh				    dst->drawable.bitsPerPixel,
328103b705cfSriastradh				    op);
328203b705cfSriastradh	}
328303b705cfSriastradh
328403b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
328503b705cfSriastradh		op->base.dst.format = sna_render_format_for_depth(dst->drawable.depth);
328603b705cfSriastradh		op->base.src.pict_format = op->base.dst.format;
328703b705cfSriastradh	} else {
328803b705cfSriastradh		op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
328903b705cfSriastradh		op->base.src.pict_format = sna_format_for_depth(src->drawable.depth);
329003b705cfSriastradh	}
329103b705cfSriastradh	if (!gen7_check_format(op->base.src.pict_format))
329203b705cfSriastradh		goto fallback;
329303b705cfSriastradh
329403b705cfSriastradh	op->base.dst.pixmap = dst;
329503b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
329603b705cfSriastradh	op->base.dst.height = dst->drawable.height;
329703b705cfSriastradh	op->base.dst.bo = dst_bo;
329803b705cfSriastradh
329903b705cfSriastradh	op->base.src.bo = src_bo;
330003b705cfSriastradh	op->base.src.card_format =
330103b705cfSriastradh		gen7_get_card_format(op->base.src.pict_format);
330203b705cfSriastradh	op->base.src.width  = src->drawable.width;
330303b705cfSriastradh	op->base.src.height = src->drawable.height;
330403b705cfSriastradh
330503b705cfSriastradh	op->base.mask.bo = NULL;
330603b705cfSriastradh
330703b705cfSriastradh	op->base.floats_per_vertex = 2;
330803b705cfSriastradh	op->base.floats_per_rect = 6;
330903b705cfSriastradh
331003b705cfSriastradh	op->base.u.gen7.flags = COPY_FLAGS(alu);
331103b705cfSriastradh
331203b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
331303b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
331403b705cfSriastradh		kgem_submit(&sna->kgem);
331503b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL))
331603b705cfSriastradh			goto fallback;
331703b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
331803b705cfSriastradh	}
331903b705cfSriastradh
332003b705cfSriastradh	gen7_align_vertex(sna, &op->base);
332142542f5fSchristos	gen7_emit_copy_state(sna, &op->base);
332203b705cfSriastradh
332303b705cfSriastradh	op->blt  = gen7_render_copy_blt;
332403b705cfSriastradh	op->done = gen7_render_copy_done;
332503b705cfSriastradh	return true;
332603b705cfSriastradh}
332703b705cfSriastradh
332803b705cfSriastradhstatic void
332903b705cfSriastradhgen7_emit_fill_state(struct sna *sna, const struct sna_composite_op *op)
333003b705cfSriastradh{
333142542f5fSchristos	uint16_t dirty;
333203b705cfSriastradh	uint32_t *binding_table;
333303b705cfSriastradh	uint16_t offset;
333403b705cfSriastradh
333503b705cfSriastradh	/* XXX Render Target Fast Clear
333603b705cfSriastradh	 * Set RTFC Enable in PS and render a rectangle.
333703b705cfSriastradh	 * Limited to a clearing the full MSC surface only with a
333803b705cfSriastradh	 * specific kernel.
333903b705cfSriastradh	 */
334003b705cfSriastradh
334103b705cfSriastradh	gen7_get_batch(sna, op);
334203b705cfSriastradh
334303b705cfSriastradh	binding_table = gen7_composite_get_binding_table(sna, &offset);
334403b705cfSriastradh
334542542f5fSchristos	dirty = kgem_bo_is_dirty(op->dst.bo);
334642542f5fSchristos
334703b705cfSriastradh	binding_table[0] =
334803b705cfSriastradh		gen7_bind_bo(sna,
334903b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
335003b705cfSriastradh			     gen7_get_dest_format(op->dst.format),
335103b705cfSriastradh			     true);
335203b705cfSriastradh	binding_table[1] =
335303b705cfSriastradh		gen7_bind_bo(sna,
335403b705cfSriastradh			     op->src.bo, 1, 1,
335503b705cfSriastradh			     GEN7_SURFACEFORMAT_B8G8R8A8_UNORM,
335603b705cfSriastradh			     false);
335703b705cfSriastradh
335803b705cfSriastradh	if (sna->kgem.surface == offset &&
335903b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen7.surface_table) == *(uint64_t*)binding_table) {
336003b705cfSriastradh		sna->kgem.surface +=
336103b705cfSriastradh			sizeof(struct gen7_surface_state)/sizeof(uint32_t);
336203b705cfSriastradh		offset = sna->render_state.gen7.surface_table;
336303b705cfSriastradh	}
336403b705cfSriastradh
336542542f5fSchristos	if (sna->kgem.batch[sna->render_state.gen7.surface_table] == binding_table[0])
336642542f5fSchristos		dirty = 0;
336703b705cfSriastradh
336842542f5fSchristos	gen7_emit_state(sna, op, offset | dirty);
336903b705cfSriastradh}
337003b705cfSriastradh
337103b705cfSriastradhstatic bool
337203b705cfSriastradhgen7_render_fill_boxes(struct sna *sna,
337303b705cfSriastradh		       CARD8 op,
337403b705cfSriastradh		       PictFormat format,
337503b705cfSriastradh		       const xRenderColor *color,
337642542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo,
337703b705cfSriastradh		       const BoxRec *box, int n)
337803b705cfSriastradh{
337903b705cfSriastradh	struct sna_composite_op tmp;
338003b705cfSriastradh	uint32_t pixel;
338103b705cfSriastradh
338203b705cfSriastradh	DBG(("%s (op=%d, color=(%04x, %04x, %04x, %04x) [%08x])\n",
338303b705cfSriastradh	     __FUNCTION__, op,
338403b705cfSriastradh	     color->red, color->green, color->blue, color->alpha, (int)format));
338503b705cfSriastradh
338603b705cfSriastradh	if (op >= ARRAY_SIZE(gen7_blend_op)) {
338703b705cfSriastradh		DBG(("%s: fallback due to unhandled blend op: %d\n",
338803b705cfSriastradh		     __FUNCTION__, op));
338903b705cfSriastradh		return false;
339003b705cfSriastradh	}
339103b705cfSriastradh
339242542f5fSchristos	if (prefer_blt_fill(sna, dst_bo, FILL_BOXES) ||
339342542f5fSchristos	    !gen7_check_dst_format(format)) {
339403b705cfSriastradh		uint8_t alu = GXinvalid;
339503b705cfSriastradh
339603b705cfSriastradh		if (op <= PictOpSrc) {
339703b705cfSriastradh			pixel = 0;
339803b705cfSriastradh			if (op == PictOpClear)
339903b705cfSriastradh				alu = GXclear;
340003b705cfSriastradh			else if (sna_get_pixel_from_rgba(&pixel,
340103b705cfSriastradh							 color->red,
340203b705cfSriastradh							 color->green,
340303b705cfSriastradh							 color->blue,
340403b705cfSriastradh							 color->alpha,
340503b705cfSriastradh							 format))
340603b705cfSriastradh				alu = GXcopy;
340703b705cfSriastradh		}
340803b705cfSriastradh
340903b705cfSriastradh		if (alu != GXinvalid &&
341003b705cfSriastradh		    sna_blt_fill_boxes(sna, alu,
341142542f5fSchristos				       dst_bo, dst->bitsPerPixel,
341203b705cfSriastradh				       pixel, box, n))
341303b705cfSriastradh			return true;
341403b705cfSriastradh
341503b705cfSriastradh		if (!gen7_check_dst_format(format))
341603b705cfSriastradh			return false;
341703b705cfSriastradh	}
341803b705cfSriastradh
341903b705cfSriastradh	if (op == PictOpClear) {
342003b705cfSriastradh		pixel = 0;
342103b705cfSriastradh		op = PictOpSrc;
342203b705cfSriastradh	} else if (!sna_get_pixel_from_rgba(&pixel,
342303b705cfSriastradh					    color->red,
342403b705cfSriastradh					    color->green,
342503b705cfSriastradh					    color->blue,
342603b705cfSriastradh					    color->alpha,
342703b705cfSriastradh					    PICT_a8r8g8b8))
342803b705cfSriastradh		return false;
342903b705cfSriastradh
343003b705cfSriastradh	DBG(("%s(%08x x %d [(%d, %d), (%d, %d) ...])\n",
343103b705cfSriastradh	     __FUNCTION__, pixel, n,
343203b705cfSriastradh	     box[0].x1, box[0].y1, box[0].x2, box[0].y2));
343303b705cfSriastradh
343442542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
343542542f5fSchristos	tmp.dst.width  = dst->width;
343642542f5fSchristos	tmp.dst.height = dst->height;
343703b705cfSriastradh	tmp.dst.format = format;
343803b705cfSriastradh	tmp.dst.bo = dst_bo;
343903b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
344003b705cfSriastradh	tmp.damage = NULL;
344103b705cfSriastradh
344203b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
344342542f5fSchristos	if (too_large(dst->width, dst->height)) {
344403b705cfSriastradh		BoxRec extents;
344503b705cfSriastradh
344603b705cfSriastradh		boxes_extents(box, n, &extents);
344703b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
344803b705cfSriastradh						   extents.x1, extents.y1,
344903b705cfSriastradh						   extents.x2 - extents.x1,
345003b705cfSriastradh						   extents.y2 - extents.y1,
345103b705cfSriastradh						   n > 1))
345203b705cfSriastradh			return sna_tiling_fill_boxes(sna, op, format, color,
345303b705cfSriastradh						     dst, dst_bo, box, n);
345403b705cfSriastradh	}
345503b705cfSriastradh
345603b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, pixel);
345703b705cfSriastradh	tmp.mask.bo = NULL;
345803b705cfSriastradh
345903b705cfSriastradh	tmp.floats_per_vertex = 2;
346003b705cfSriastradh	tmp.floats_per_rect = 6;
346103b705cfSriastradh	tmp.need_magic_ca_pass = false;
346203b705cfSriastradh
346303b705cfSriastradh	tmp.u.gen7.flags = FILL_FLAGS(op, format);
346403b705cfSriastradh
346503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
346603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
346703b705cfSriastradh		kgem_submit(&sna->kgem);
346842542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
346942542f5fSchristos			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
347042542f5fSchristos			if (tmp.redirect.real_bo)
347142542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
347242542f5fSchristos
347342542f5fSchristos			return false;
347442542f5fSchristos		}
347542542f5fSchristos		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
347603b705cfSriastradh	}
347703b705cfSriastradh
347803b705cfSriastradh	gen7_align_vertex(sna, &tmp);
347942542f5fSchristos	gen7_emit_fill_state(sna, &tmp);
348003b705cfSriastradh
348103b705cfSriastradh	do {
348203b705cfSriastradh		int n_this_time;
348303b705cfSriastradh		int16_t *v;
348403b705cfSriastradh
348503b705cfSriastradh		n_this_time = gen7_get_rectangles(sna, &tmp, n,
348603b705cfSriastradh						  gen7_emit_fill_state);
348703b705cfSriastradh		n -= n_this_time;
348803b705cfSriastradh
348903b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
349003b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
349103b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
349203b705cfSriastradh		do {
349303b705cfSriastradh			DBG(("	(%d, %d), (%d, %d)\n",
349403b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
349503b705cfSriastradh
349603b705cfSriastradh			v[0] = box->x2;
349703b705cfSriastradh			v[5] = v[1] = box->y2;
349803b705cfSriastradh			v[8] = v[4] = box->x1;
349903b705cfSriastradh			v[9] = box->y1;
350003b705cfSriastradh			v[2] = v[3]  = v[7]  = 1;
350103b705cfSriastradh			v[6] = v[10] = v[11] = 0;
350203b705cfSriastradh			v += 12; box++;
350303b705cfSriastradh		} while (--n_this_time);
350403b705cfSriastradh	} while (n);
350503b705cfSriastradh
350603b705cfSriastradh	gen4_vertex_flush(sna);
350703b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
350803b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
350903b705cfSriastradh	return true;
351003b705cfSriastradh}
351103b705cfSriastradh
351203b705cfSriastradhstatic void
351303b705cfSriastradhgen7_render_fill_op_blt(struct sna *sna,
351403b705cfSriastradh			const struct sna_fill_op *op,
351503b705cfSriastradh			int16_t x, int16_t y, int16_t w, int16_t h)
351603b705cfSriastradh{
351703b705cfSriastradh	int16_t *v;
351803b705cfSriastradh
351903b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d)\n", __FUNCTION__, x, y, w, h));
352003b705cfSriastradh
352103b705cfSriastradh	gen7_get_rectangles(sna, &op->base, 1, gen7_emit_fill_state);
352203b705cfSriastradh
352303b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
352403b705cfSriastradh	sna->render.vertex_used += 6;
352503b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
352603b705cfSriastradh
352703b705cfSriastradh	v[0] = x+w;
352803b705cfSriastradh	v[4] = v[8] = x;
352903b705cfSriastradh	v[1] = v[5] = y+h;
353003b705cfSriastradh	v[9] = y;
353103b705cfSriastradh
353203b705cfSriastradh	v[2] = v[3]  = v[7]  = 1;
353303b705cfSriastradh	v[6] = v[10] = v[11] = 0;
353403b705cfSriastradh}
353503b705cfSriastradh
353603b705cfSriastradhfastcall static void
353703b705cfSriastradhgen7_render_fill_op_box(struct sna *sna,
353803b705cfSriastradh			const struct sna_fill_op *op,
353903b705cfSriastradh			const BoxRec *box)
354003b705cfSriastradh{
354103b705cfSriastradh	int16_t *v;
354203b705cfSriastradh
354303b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
354403b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
354503b705cfSriastradh
354603b705cfSriastradh	gen7_get_rectangles(sna, &op->base, 1, gen7_emit_fill_state);
354703b705cfSriastradh
354803b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
354903b705cfSriastradh	sna->render.vertex_used += 6;
355003b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
355103b705cfSriastradh
355203b705cfSriastradh	v[0] = box->x2;
355303b705cfSriastradh	v[8] = v[4] = box->x1;
355403b705cfSriastradh	v[5] = v[1] = box->y2;
355503b705cfSriastradh	v[9] = box->y1;
355603b705cfSriastradh
355703b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
355803b705cfSriastradh	v[6] = v[10] = v[11] = 0;
355903b705cfSriastradh}
356003b705cfSriastradh
356103b705cfSriastradhfastcall static void
356203b705cfSriastradhgen7_render_fill_op_boxes(struct sna *sna,
356303b705cfSriastradh			  const struct sna_fill_op *op,
356403b705cfSriastradh			  const BoxRec *box,
356503b705cfSriastradh			  int nbox)
356603b705cfSriastradh{
356703b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)... x %d\n", __FUNCTION__,
356803b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2, nbox));
356903b705cfSriastradh
357003b705cfSriastradh	do {
357103b705cfSriastradh		int nbox_this_time;
357203b705cfSriastradh		int16_t *v;
357303b705cfSriastradh
357403b705cfSriastradh		nbox_this_time = gen7_get_rectangles(sna, &op->base, nbox,
357503b705cfSriastradh						     gen7_emit_fill_state);
357603b705cfSriastradh		nbox -= nbox_this_time;
357703b705cfSriastradh
357803b705cfSriastradh		v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
357903b705cfSriastradh		sna->render.vertex_used += 6 * nbox_this_time;
358003b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
358103b705cfSriastradh
358203b705cfSriastradh		do {
358303b705cfSriastradh			v[0] = box->x2;
358403b705cfSriastradh			v[8] = v[4] = box->x1;
358503b705cfSriastradh			v[5] = v[1] = box->y2;
358603b705cfSriastradh			v[9] = box->y1;
358703b705cfSriastradh			v[7] = v[2]  = v[3]  = 1;
358803b705cfSriastradh			v[6] = v[10] = v[11] = 0;
358903b705cfSriastradh			box++; v += 12;
359003b705cfSriastradh		} while (--nbox_this_time);
359103b705cfSriastradh	} while (nbox);
359203b705cfSriastradh}
359303b705cfSriastradh
359403b705cfSriastradhstatic void
359503b705cfSriastradhgen7_render_fill_op_done(struct sna *sna, const struct sna_fill_op *op)
359603b705cfSriastradh{
359703b705cfSriastradh	if (sna->render.vertex_offset)
359803b705cfSriastradh		gen4_vertex_flush(sna);
359903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
360003b705cfSriastradh}
360103b705cfSriastradh
360203b705cfSriastradhstatic bool
360303b705cfSriastradhgen7_render_fill(struct sna *sna, uint8_t alu,
360403b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
360542542f5fSchristos		 uint32_t color, unsigned flags,
360603b705cfSriastradh		 struct sna_fill_op *op)
360703b705cfSriastradh{
360803b705cfSriastradh	DBG(("%s: (alu=%d, color=%x)\n", __FUNCTION__, alu, color));
360903b705cfSriastradh
361042542f5fSchristos	if (prefer_blt_fill(sna, dst_bo, flags) &&
361103b705cfSriastradh	    sna_blt_fill(sna, alu,
361203b705cfSriastradh			 dst_bo, dst->drawable.bitsPerPixel,
361303b705cfSriastradh			 color,
361403b705cfSriastradh			 op))
361503b705cfSriastradh		return true;
361603b705cfSriastradh
361703b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
361803b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
361903b705cfSriastradh		return sna_blt_fill(sna, alu,
362003b705cfSriastradh				    dst_bo, dst->drawable.bitsPerPixel,
362103b705cfSriastradh				    color,
362203b705cfSriastradh				    op);
362303b705cfSriastradh
362403b705cfSriastradh	if (alu == GXclear)
362503b705cfSriastradh		color = 0;
362603b705cfSriastradh
362703b705cfSriastradh	op->base.dst.pixmap = dst;
362803b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
362903b705cfSriastradh	op->base.dst.height = dst->drawable.height;
363003b705cfSriastradh	op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
363103b705cfSriastradh	op->base.dst.bo = dst_bo;
363203b705cfSriastradh	op->base.dst.x = op->base.dst.y = 0;
363303b705cfSriastradh
363403b705cfSriastradh	op->base.src.bo =
363503b705cfSriastradh		sna_render_get_solid(sna,
363603b705cfSriastradh				     sna_rgba_for_color(color,
363703b705cfSriastradh							dst->drawable.depth));
363803b705cfSriastradh	op->base.mask.bo = NULL;
363903b705cfSriastradh
364003b705cfSriastradh	op->base.need_magic_ca_pass = false;
364103b705cfSriastradh	op->base.floats_per_vertex = 2;
364203b705cfSriastradh	op->base.floats_per_rect = 6;
364303b705cfSriastradh
364403b705cfSriastradh	op->base.u.gen7.flags = FILL_FLAGS_NOBLEND;
364503b705cfSriastradh
364603b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
364703b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
364803b705cfSriastradh		kgem_submit(&sna->kgem);
364942542f5fSchristos		if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
365042542f5fSchristos			kgem_bo_destroy(&sna->kgem, op->base.src.bo);
365142542f5fSchristos			return false;
365242542f5fSchristos		}
365342542f5fSchristos
365442542f5fSchristos		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
365503b705cfSriastradh	}
365603b705cfSriastradh
365703b705cfSriastradh	gen7_align_vertex(sna, &op->base);
365842542f5fSchristos	gen7_emit_fill_state(sna, &op->base);
365903b705cfSriastradh
366003b705cfSriastradh	op->blt   = gen7_render_fill_op_blt;
366103b705cfSriastradh	op->box   = gen7_render_fill_op_box;
366203b705cfSriastradh	op->boxes = gen7_render_fill_op_boxes;
366342542f5fSchristos	op->points = NULL;
366403b705cfSriastradh	op->done  = gen7_render_fill_op_done;
366503b705cfSriastradh	return true;
366603b705cfSriastradh}
366703b705cfSriastradh
366803b705cfSriastradhstatic bool
366903b705cfSriastradhgen7_render_fill_one_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
367003b705cfSriastradh			     uint32_t color,
367103b705cfSriastradh			     int16_t x1, int16_t y1, int16_t x2, int16_t y2,
367203b705cfSriastradh			     uint8_t alu)
367303b705cfSriastradh{
367403b705cfSriastradh	BoxRec box;
367503b705cfSriastradh
367603b705cfSriastradh	box.x1 = x1;
367703b705cfSriastradh	box.y1 = y1;
367803b705cfSriastradh	box.x2 = x2;
367903b705cfSriastradh	box.y2 = y2;
368003b705cfSriastradh
368103b705cfSriastradh	return sna_blt_fill_boxes(sna, alu,
368203b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
368303b705cfSriastradh				  color, &box, 1);
368403b705cfSriastradh}
368503b705cfSriastradh
368603b705cfSriastradhstatic bool
368703b705cfSriastradhgen7_render_fill_one(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
368803b705cfSriastradh		     uint32_t color,
368903b705cfSriastradh		     int16_t x1, int16_t y1,
369003b705cfSriastradh		     int16_t x2, int16_t y2,
369103b705cfSriastradh		     uint8_t alu)
369203b705cfSriastradh{
369303b705cfSriastradh	struct sna_composite_op tmp;
369403b705cfSriastradh	int16_t *v;
369503b705cfSriastradh
369603b705cfSriastradh	/* Prefer to use the BLT if already engaged */
369742542f5fSchristos	if (prefer_blt_fill(sna, bo, FILL_BOXES) &&
369803b705cfSriastradh	    gen7_render_fill_one_try_blt(sna, dst, bo, color,
369903b705cfSriastradh					 x1, y1, x2, y2, alu))
370003b705cfSriastradh		return true;
370103b705cfSriastradh
370203b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
370303b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
370403b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
370503b705cfSriastradh		return gen7_render_fill_one_try_blt(sna, dst, bo, color,
370603b705cfSriastradh						    x1, y1, x2, y2, alu);
370703b705cfSriastradh
370803b705cfSriastradh	if (alu == GXclear)
370903b705cfSriastradh		color = 0;
371003b705cfSriastradh
371103b705cfSriastradh	tmp.dst.pixmap = dst;
371203b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
371303b705cfSriastradh	tmp.dst.height = dst->drawable.height;
371403b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
371503b705cfSriastradh	tmp.dst.bo = bo;
371603b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
371703b705cfSriastradh
371803b705cfSriastradh	tmp.src.bo =
371903b705cfSriastradh		sna_render_get_solid(sna,
372003b705cfSriastradh				     sna_rgba_for_color(color,
372103b705cfSriastradh							dst->drawable.depth));
372203b705cfSriastradh	tmp.mask.bo = NULL;
372303b705cfSriastradh
372403b705cfSriastradh	tmp.floats_per_vertex = 2;
372503b705cfSriastradh	tmp.floats_per_rect = 6;
372603b705cfSriastradh	tmp.need_magic_ca_pass = false;
372703b705cfSriastradh
372803b705cfSriastradh	tmp.u.gen7.flags = FILL_FLAGS_NOBLEND;
372903b705cfSriastradh
373003b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, bo);
373103b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
373203b705cfSriastradh		kgem_submit(&sna->kgem);
373342542f5fSchristos		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
373403b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
373503b705cfSriastradh			return false;
373603b705cfSriastradh		}
373742542f5fSchristos		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
373803b705cfSriastradh	}
373903b705cfSriastradh
374003b705cfSriastradh	gen7_align_vertex(sna, &tmp);
374142542f5fSchristos	gen7_emit_fill_state(sna, &tmp);
374203b705cfSriastradh
374303b705cfSriastradh	gen7_get_rectangles(sna, &tmp, 1, gen7_emit_fill_state);
374403b705cfSriastradh
374503b705cfSriastradh	DBG(("	(%d, %d), (%d, %d)\n", x1, y1, x2, y2));
374603b705cfSriastradh
374703b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
374803b705cfSriastradh	sna->render.vertex_used += 6;
374903b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
375003b705cfSriastradh
375103b705cfSriastradh	v[0] = x2;
375203b705cfSriastradh	v[8] = v[4] = x1;
375303b705cfSriastradh	v[5] = v[1] = y2;
375403b705cfSriastradh	v[9] = y1;
375503b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
375603b705cfSriastradh	v[6] = v[10] = v[11] = 0;
375703b705cfSriastradh
375803b705cfSriastradh	gen4_vertex_flush(sna);
375903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
376003b705cfSriastradh
376103b705cfSriastradh	return true;
376203b705cfSriastradh}
376303b705cfSriastradh
376403b705cfSriastradhstatic bool
376503b705cfSriastradhgen7_render_clear_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
376603b705cfSriastradh{
376703b705cfSriastradh	BoxRec box;
376803b705cfSriastradh
376903b705cfSriastradh	box.x1 = 0;
377003b705cfSriastradh	box.y1 = 0;
377103b705cfSriastradh	box.x2 = dst->drawable.width;
377203b705cfSriastradh	box.y2 = dst->drawable.height;
377303b705cfSriastradh
377403b705cfSriastradh	return sna_blt_fill_boxes(sna, GXclear,
377503b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
377603b705cfSriastradh				  0, &box, 1);
377703b705cfSriastradh}
377803b705cfSriastradh
377903b705cfSriastradhstatic bool
378003b705cfSriastradhgen7_render_clear(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
378103b705cfSriastradh{
378203b705cfSriastradh	struct sna_composite_op tmp;
378303b705cfSriastradh	int16_t *v;
378403b705cfSriastradh
378503b705cfSriastradh	DBG(("%s: %dx%d\n",
378603b705cfSriastradh	     __FUNCTION__,
378703b705cfSriastradh	     dst->drawable.width,
378803b705cfSriastradh	     dst->drawable.height));
378903b705cfSriastradh
379003b705cfSriastradh	/* Prefer to use the BLT if already engaged */
379103b705cfSriastradh	if (sna->kgem.mode == KGEM_BLT &&
379203b705cfSriastradh	    gen7_render_clear_try_blt(sna, dst, bo))
379303b705cfSriastradh		return true;
379403b705cfSriastradh
379503b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
379603b705cfSriastradh	if (too_large(dst->drawable.width, dst->drawable.height))
379703b705cfSriastradh		return gen7_render_clear_try_blt(sna, dst, bo);
379803b705cfSriastradh
379903b705cfSriastradh	tmp.dst.pixmap = dst;
380003b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
380103b705cfSriastradh	tmp.dst.height = dst->drawable.height;
380203b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
380303b705cfSriastradh	tmp.dst.bo = bo;
380403b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
380503b705cfSriastradh
380603b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, 0);
380703b705cfSriastradh	tmp.mask.bo = NULL;
380803b705cfSriastradh
380903b705cfSriastradh	tmp.floats_per_vertex = 2;
381003b705cfSriastradh	tmp.floats_per_rect = 6;
381103b705cfSriastradh	tmp.need_magic_ca_pass = false;
381203b705cfSriastradh
381303b705cfSriastradh	tmp.u.gen7.flags = FILL_FLAGS_NOBLEND;
381403b705cfSriastradh
381503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, bo);
381603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
381703b705cfSriastradh		kgem_submit(&sna->kgem);
381803b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
381903b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
382003b705cfSriastradh			return false;
382103b705cfSriastradh		}
382242542f5fSchristos		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
382303b705cfSriastradh	}
382403b705cfSriastradh
382503b705cfSriastradh	gen7_align_vertex(sna, &tmp);
382642542f5fSchristos	gen7_emit_fill_state(sna, &tmp);
382703b705cfSriastradh
382803b705cfSriastradh	gen7_get_rectangles(sna, &tmp, 1, gen7_emit_fill_state);
382903b705cfSriastradh
383003b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
383103b705cfSriastradh	sna->render.vertex_used += 6;
383203b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
383303b705cfSriastradh
383403b705cfSriastradh	v[0] = dst->drawable.width;
383503b705cfSriastradh	v[5] = v[1] = dst->drawable.height;
383603b705cfSriastradh	v[8] = v[4] = 0;
383703b705cfSriastradh	v[9] = 0;
383803b705cfSriastradh
383903b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
384003b705cfSriastradh	v[6] = v[10] = v[11] = 0;
384103b705cfSriastradh
384203b705cfSriastradh	gen4_vertex_flush(sna);
384303b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
384403b705cfSriastradh
384503b705cfSriastradh	return true;
384603b705cfSriastradh}
384703b705cfSriastradhstatic void gen7_render_reset(struct sna *sna)
384803b705cfSriastradh{
384913496ba1Ssnj	sna->render_state.gen7.pipe_controls_since_stall = 0;
385003b705cfSriastradh	sna->render_state.gen7.emit_flush = false;
385103b705cfSriastradh	sna->render_state.gen7.needs_invariant = true;
385203b705cfSriastradh	sna->render_state.gen7.ve_id = 3 << 2;
385303b705cfSriastradh	sna->render_state.gen7.last_primitive = -1;
385403b705cfSriastradh
385503b705cfSriastradh	sna->render_state.gen7.num_sf_outputs = 0;
385603b705cfSriastradh	sna->render_state.gen7.samplers = -1;
385703b705cfSriastradh	sna->render_state.gen7.blend = -1;
385803b705cfSriastradh	sna->render_state.gen7.kernel = -1;
385903b705cfSriastradh	sna->render_state.gen7.drawrect_offset = -1;
386003b705cfSriastradh	sna->render_state.gen7.drawrect_limit = -1;
386142542f5fSchristos	sna->render_state.gen7.surface_table = 0;
386242542f5fSchristos
386342542f5fSchristos	if (sna->render.vbo && !kgem_bo_can_map(&sna->kgem, sna->render.vbo)) {
386442542f5fSchristos		DBG(("%s: discarding unmappable vbo\n", __FUNCTION__));
386542542f5fSchristos		discard_vbo(sna);
386642542f5fSchristos	}
386703b705cfSriastradh
386803b705cfSriastradh	sna->render.vertex_offset = 0;
386903b705cfSriastradh	sna->render.nvertex_reloc = 0;
387003b705cfSriastradh	sna->render.vb_id = 0;
387103b705cfSriastradh}
387203b705cfSriastradh
387303b705cfSriastradhstatic void gen7_render_fini(struct sna *sna)
387403b705cfSriastradh{
387503b705cfSriastradh	kgem_bo_destroy(&sna->kgem, sna->render_state.gen7.general_bo);
387603b705cfSriastradh}
387703b705cfSriastradh
387842542f5fSchristosstatic bool is_gt3(struct sna *sna, int devid)
387903b705cfSriastradh{
388003b705cfSriastradh	assert(sna->kgem.gen == 075);
388142542f5fSchristos	return devid & 0x20;
388203b705cfSriastradh}
388303b705cfSriastradh
388442542f5fSchristosstatic bool is_gt2(struct sna *sna, int devid)
388503b705cfSriastradh{
388642542f5fSchristos	return devid & (is_hsw(sna)? 0x30 : 0x20);
388703b705cfSriastradh}
388803b705cfSriastradh
388942542f5fSchristosstatic bool is_mobile(struct sna *sna, int devid)
389003b705cfSriastradh{
389142542f5fSchristos	return (devid & 0xf) == 0x6;
389203b705cfSriastradh}
389303b705cfSriastradh
389442542f5fSchristosstatic bool gen7_render_setup(struct sna *sna, int devid)
389503b705cfSriastradh{
389603b705cfSriastradh	struct gen7_render_state *state = &sna->render_state.gen7;
389703b705cfSriastradh	struct sna_static_stream general;
389803b705cfSriastradh	struct gen7_sampler_state *ss;
389903b705cfSriastradh	int i, j, k, l, m;
390003b705cfSriastradh
390103b705cfSriastradh	if (is_ivb(sna)) {
390203b705cfSriastradh		state->info = &ivb_gt_info;
390342542f5fSchristos		if (devid & 0xf) {
390403b705cfSriastradh			state->info = &ivb_gt1_info;
390542542f5fSchristos			if (is_gt2(sna, devid))
390603b705cfSriastradh				state->info = &ivb_gt2_info; /* XXX requires GT_MODE WiZ disabled */
390703b705cfSriastradh		}
390803b705cfSriastradh	} else if (is_byt(sna)) {
390903b705cfSriastradh		state->info = &byt_gt_info;
391003b705cfSriastradh	} else if (is_hsw(sna)) {
391103b705cfSriastradh		state->info = &hsw_gt_info;
391242542f5fSchristos		if (devid & 0xf) {
391342542f5fSchristos			if (is_gt3(sna, devid))
391403b705cfSriastradh				state->info = &hsw_gt3_info;
391542542f5fSchristos			else if (is_gt2(sna, devid))
391603b705cfSriastradh				state->info = &hsw_gt2_info;
391703b705cfSriastradh			else
391803b705cfSriastradh				state->info = &hsw_gt1_info;
391903b705cfSriastradh		}
392003b705cfSriastradh	} else
392103b705cfSriastradh		return false;
392203b705cfSriastradh
392342542f5fSchristos	state->gt = state->info->gt;
392442542f5fSchristos
392503b705cfSriastradh	sna_static_stream_init(&general);
392603b705cfSriastradh
392703b705cfSriastradh	/* Zero pad the start. If you see an offset of 0x0 in the batchbuffer
392803b705cfSriastradh	 * dumps, you know it points to zero.
392903b705cfSriastradh	 */
393003b705cfSriastradh	null_create(&general);
393103b705cfSriastradh
393203b705cfSriastradh	for (m = 0; m < GEN7_WM_KERNEL_COUNT; m++) {
393303b705cfSriastradh		if (wm_kernels[m].size) {
393403b705cfSriastradh			state->wm_kernel[m][1] =
393503b705cfSriastradh				sna_static_stream_add(&general,
393603b705cfSriastradh						      wm_kernels[m].data,
393703b705cfSriastradh						      wm_kernels[m].size,
393803b705cfSriastradh						      64);
393903b705cfSriastradh		} else {
394003b705cfSriastradh			if (USE_8_PIXEL_DISPATCH) {
394103b705cfSriastradh				state->wm_kernel[m][0] =
394203b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
394303b705cfSriastradh								     wm_kernels[m].data, 8);
394403b705cfSriastradh			}
394503b705cfSriastradh
394603b705cfSriastradh			if (USE_16_PIXEL_DISPATCH) {
394703b705cfSriastradh				state->wm_kernel[m][1] =
394803b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
394903b705cfSriastradh								     wm_kernels[m].data, 16);
395003b705cfSriastradh			}
395103b705cfSriastradh
395203b705cfSriastradh			if (USE_32_PIXEL_DISPATCH) {
395303b705cfSriastradh				state->wm_kernel[m][2] =
395403b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
395503b705cfSriastradh								     wm_kernels[m].data, 32);
395603b705cfSriastradh			}
395703b705cfSriastradh		}
395803b705cfSriastradh		assert(state->wm_kernel[m][0]|state->wm_kernel[m][1]|state->wm_kernel[m][2]);
395903b705cfSriastradh	}
396003b705cfSriastradh
396103b705cfSriastradh	ss = sna_static_stream_map(&general,
396203b705cfSriastradh				   2 * sizeof(*ss) *
396303b705cfSriastradh				   (2 +
396403b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT *
396503b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT),
396603b705cfSriastradh				   32);
396703b705cfSriastradh	state->wm_state = sna_static_stream_offsetof(&general, ss);
396803b705cfSriastradh	sampler_copy_init(ss); ss += 2;
396903b705cfSriastradh	sampler_fill_init(ss); ss += 2;
397003b705cfSriastradh	for (i = 0; i < FILTER_COUNT; i++) {
397103b705cfSriastradh		for (j = 0; j < EXTEND_COUNT; j++) {
397203b705cfSriastradh			for (k = 0; k < FILTER_COUNT; k++) {
397303b705cfSriastradh				for (l = 0; l < EXTEND_COUNT; l++) {
397403b705cfSriastradh					sampler_state_init(ss++, i, j);
397503b705cfSriastradh					sampler_state_init(ss++, k, l);
397603b705cfSriastradh				}
397703b705cfSriastradh			}
397803b705cfSriastradh		}
397903b705cfSriastradh	}
398003b705cfSriastradh
398103b705cfSriastradh	state->cc_blend = gen7_composite_create_blend_state(&general);
398203b705cfSriastradh
398303b705cfSriastradh	state->general_bo = sna_static_stream_fini(sna, &general);
398403b705cfSriastradh	return state->general_bo != NULL;
398503b705cfSriastradh}
398603b705cfSriastradh
398703b705cfSriastradhconst char *gen7_render_init(struct sna *sna, const char *backend)
398803b705cfSriastradh{
398913496ba1Ssnj	int devid = intel_get_device_id(sna->dev);
399042542f5fSchristos
399142542f5fSchristos	if (!gen7_render_setup(sna, devid))
399203b705cfSriastradh		return backend;
399303b705cfSriastradh
399442542f5fSchristos	sna->kgem.context_switch = gen6_render_context_switch;
399542542f5fSchristos	sna->kgem.retire = gen6_render_retire;
399642542f5fSchristos	sna->kgem.expire = gen4_render_expire;
399703b705cfSriastradh
399803b705cfSriastradh#if !NO_COMPOSITE
399903b705cfSriastradh	sna->render.composite = gen7_render_composite;
400003b705cfSriastradh	sna->render.prefer_gpu |= PREFER_GPU_RENDER;
400103b705cfSriastradh#endif
400203b705cfSriastradh#if !NO_COMPOSITE_SPANS
400303b705cfSriastradh	sna->render.check_composite_spans = gen7_check_composite_spans;
400403b705cfSriastradh	sna->render.composite_spans = gen7_render_composite_spans;
400542542f5fSchristos	if (is_mobile(sna, devid) || is_gt2(sna, devid) || is_byt(sna))
400603b705cfSriastradh		sna->render.prefer_gpu |= PREFER_GPU_SPANS;
400703b705cfSriastradh#endif
400803b705cfSriastradh	sna->render.video = gen7_render_video;
400903b705cfSriastradh
401003b705cfSriastradh#if !NO_COPY_BOXES
401103b705cfSriastradh	sna->render.copy_boxes = gen7_render_copy_boxes;
401203b705cfSriastradh#endif
401303b705cfSriastradh#if !NO_COPY
401403b705cfSriastradh	sna->render.copy = gen7_render_copy;
401503b705cfSriastradh#endif
401603b705cfSriastradh
401703b705cfSriastradh#if !NO_FILL_BOXES
401803b705cfSriastradh	sna->render.fill_boxes = gen7_render_fill_boxes;
401903b705cfSriastradh#endif
402003b705cfSriastradh#if !NO_FILL
402103b705cfSriastradh	sna->render.fill = gen7_render_fill;
402203b705cfSriastradh#endif
402303b705cfSriastradh#if !NO_FILL_ONE
402403b705cfSriastradh	sna->render.fill_one = gen7_render_fill_one;
402503b705cfSriastradh#endif
402603b705cfSriastradh#if !NO_FILL_CLEAR
402703b705cfSriastradh	sna->render.clear = gen7_render_clear;
402803b705cfSriastradh#endif
402903b705cfSriastradh
403042542f5fSchristos	sna->render.flush = gen4_render_flush;
403103b705cfSriastradh	sna->render.reset = gen7_render_reset;
403203b705cfSriastradh	sna->render.fini = gen7_render_fini;
403303b705cfSriastradh
403403b705cfSriastradh	sna->render.max_3d_size = GEN7_MAX_SIZE;
403503b705cfSriastradh	sna->render.max_3d_pitch = 1 << 18;
403603b705cfSriastradh	return sna->render_state.gen7.info->name;
403703b705cfSriastradh}
4038