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