summaryrefslogtreecommitdiff
path: root/hw/kdrive/ati/radeon_composite.c
blob: f431b428af6b6ec4e854d4af341e6638a6ec9524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * $Id$
 *
 * Copyright © 2003 Eric Anholt
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Eric Anholt not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Eric Anholt makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * ERIC ANHOLT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL ERIC ANHOLT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
/* $Header$ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "ati.h"
#include "ati_reg.h"
#include "ati_draw.h"
#include "radeon_common.h"
#include "r128_common.h"
#include "ati_sarea.h"

#define TAG(x)		x##DMA
#define LOCALS		(void)atic; \
			RING_LOCALS
#define BEGIN(x)	BEGIN_RING(x * 2)
#define OUT_REG(reg, val) OUT_RING_REG(reg, val)
#define END()		ADVANCE_RING()

extern ATIScreenInfo *accel_atis;

static CARD32 RadeonBlendOp[] = {
	/* Clear */
	RADEON_SRC_BLEND_GL_ZERO		| RADEON_DST_BLEND_GL_ZERO,
	/* Src */
	RADEON_SRC_BLEND_GL_ONE			| RADEON_DST_BLEND_GL_ZERO,
	/* Dst */
	RADEON_SRC_BLEND_GL_ZERO		| RADEON_DST_BLEND_GL_ONE,
	/* Over */
	RADEON_SRC_BLEND_GL_ONE			| RADEON_DST_BLEND_GL_ONE_MINUS_SRC_ALPHA,
	/* OverReverse */
	RADEON_SRC_BLEND_GL_ONE_MINUS_DST_ALPHA	| RADEON_DST_BLEND_GL_ONE,
	/* In */
	RADEON_SRC_BLEND_GL_DST_ALPHA		| RADEON_DST_BLEND_GL_ZERO,
	/* InReverse */
	RADEON_SRC_BLEND_GL_ZERO		| RADEON_DST_BLEND_GL_SRC_ALPHA,
	/* Out */
	RADEON_SRC_BLEND_GL_ONE_MINUS_DST_ALPHA	| RADEON_DST_BLEND_GL_ZERO,
	/* OutReverse */
	RADEON_SRC_BLEND_GL_ZERO		| RADEON_DST_BLEND_GL_ONE_MINUS_SRC_ALPHA,
	/* Atop */
	RADEON_SRC_BLEND_GL_DST_ALPHA		| RADEON_DST_BLEND_GL_ONE_MINUS_SRC_ALPHA,
	/* AtopReverse */
	RADEON_SRC_BLEND_GL_ONE_MINUS_DST_ALPHA	| RADEON_DST_BLEND_GL_SRC_ALPHA,
	/* Xor */
	RADEON_SRC_BLEND_GL_ONE_MINUS_DST_ALPHA	| RADEON_DST_BLEND_GL_ONE_MINUS_SRC_ALPHA,
	/* Add */
	RADEON_SRC_BLEND_GL_ONE			| RADEON_DST_BLEND_GL_ONE,
	/* Saturate */
	RADEON_SRC_BLEND_GL_SRC_ALPHA_SATURATE	| RADEON_DST_BLEND_GL_ONE,
	/* DisjointClear */
	RADEON_SRC_BLEND_GL_ZERO		| RADEON_DST_BLEND_GL_ZERO,
	/* DisjointSrc */
	RADEON_SRC_BLEND_GL_ONE			| RADEON_DST_BLEND_GL_ZERO,
	/* DisjointDst */
	RADEON_SRC_BLEND_GL_ZERO		| RADEON_DST_BLEND_GL_ONE,
};

/* Compute log base 2 of val. */
static int
ATILog2(int val)
{
	int bits;

	if (!val)
		return 1;
	for (bits = 0; val != 0; val >>= 1, ++bits)
		;
	return bits;
}

static Bool
RadeonTextureSetup(PicturePtr pPict, PixmapPtr pPix, int unit)
{
	ATIScreenInfo *atis = accel_atis;
	ATICardInfo *atic = atis->atic;
	KdScreenPriv(pPix->drawable.pScreen);
	CARD32 txformat, txoffset, txpitch;
	int w = pPict->pDrawable->width;
	int h = pPict->pDrawable->height;
	LOCALS;

	if ((w > 0x7ff) || (h > 0x7ff))
		ATI_FALLBACK(("Picture w/h too large (%dx%d)\n", w, h));

	switch (pPict->format) {
	case PICT_a8r8g8b8:
		txformat = RADEON_TXFORMAT_ARGB8888 |
		    RADEON_TXFORMAT_ALPHA_IN_MAP;
		break;
	case PICT_x8r8g8b8:
		txformat = RADEON_TXFORMAT_ARGB8888;
		break;
	case PICT_r5g6b5:
		txformat = RADEON_TXFORMAT_RGB565;
		break;
	case PICT_a8:
		txformat = RADEON_TXFORMAT_I8 | RADEON_TXFORMAT_ALPHA_IN_MAP;
		break;
	default:
		ATI_FALLBACK(("Unsupported picture format 0x%x\n",
		    pPict->format));
	}
	if (pPict->repeat) {
		if ((w & (w - 1)) != 0 || (h & (h - 1)) != 0)
			ATI_FALLBACK(("NPOT repeat unsupported (%dx%d)\n", w,
			    h));

		txformat |= (ATILog2(w) - 1) << RADEON_TXFORMAT_WIDTH_SHIFT;
		txformat |= (ATILog2(h) - 1) << RADEON_TXFORMAT_HEIGHT_SHIFT;
	} else 
		txformat |= RADEON_TXFORMAT_NON_POWER2;
	txformat |= unit << 24; /* RADEON_TXFORMAT_ST_ROUTE_STQX */

	txpitch = pPix->devKind;
	txoffset = ((CARD8 *)pPix->devPrivate.ptr -
	    pScreenPriv->screen->memory_base);

	if ((txoffset & 0x1f) != 0)
		ATI_FALLBACK(("Bad texture offset 0x%x\n", txoffset));
	if ((txpitch & 0x1f) != 0)
		ATI_FALLBACK(("Bad texture pitch 0x%x\n", txpitch));

	/* RADEON_REG_PP_TXFILTER_0, RADEON_REG_PP_TXFORMAT_0,
	 * RADEON_REG_PP_TXOFFSET_0
	 */
	BEGIN_RING(4);
	OUT_RING(DMA_PACKET0(RADEON_REG_PP_TXFILTER_0 + 0x18 * unit, 2));
	OUT_RING(0);
	OUT_RING(txformat);
	OUT_RING(txoffset);
	ADVANCE_RING();

	/* RADEON_REG_PP_TEX_SIZE_0, RADEON_REG_PP_TEX_PITCH_0 */
	BEGIN_RING(3);
	OUT_RING(DMA_PACKET0(RADEON_REG_PP_TEX_SIZE_0 + 0x8 * unit, 1));
	OUT_RING((pPix->drawable.width - 1) |
	    ((pPix->drawable.height - 1) << RADEON_TEX_VSIZE_SHIFT));
	OUT_RING(txpitch - 32);
	ADVANCE_RING();

	return TRUE;
}

Bool
RadeonPrepareComposite(int op, PicturePtr pSrcPicture, PicturePtr pMaskPicture,
    PicturePtr pDstPicture, PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
{
	KdScreenPriv(pDst->drawable.pScreen);
	ATIScreenInfo(pScreenPriv);
	ATICardInfo(pScreenPriv);
	CARD32 dst_format, dst_offset, dst_pitch;
	CARD32 pp_cntl;
	int pixel_shift;
	LOCALS;

	/*ErrorF("RadeonPrepareComposite\n");*/
	
	/* Check for unsupported compositing operations. */
	if (op >= sizeof(RadeonBlendOp) / sizeof(RadeonBlendOp[0]))
		ATI_FALLBACK(("Unsupported Composite op 0x%x\n", op));
	if (pSrcPicture->transform)
		ATI_FALLBACK(("Source transform unsupported.\n"));
	if (pMaskPicture && pMaskPicture->transform)
		ATI_FALLBACK(("Mask transform unsupported.\n"));

	accel_atis = atis;

	switch (pDstPicture->format) {
	case PICT_r5g6b5:
		dst_format = RADEON_COLOR_FORMAT_RGB565;
		pixel_shift = 1;
		break;
	case PICT_a1r5g5b5:
		dst_format = RADEON_COLOR_FORMAT_ARGB1555;
		pixel_shift = 1;
		break;
	case PICT_a8r8g8b8:
	case PICT_x8r8g8b8:
		dst_format = RADEON_COLOR_FORMAT_ARGB8888;
		pixel_shift = 2;
		break;
	default:
		ATI_FALLBACK(("Unsupported dest format 0x%x\n",
		    pDstPicture->format));
	}

	dst_offset = ((CARD8 *)pDst->devPrivate.ptr -
	    pScreenPriv->screen->memory_base);
	dst_pitch = pDst->devKind;
	if ((dst_offset & 0xff) != 0)
		ATI_FALLBACK(("Bad destination offset 0x%x\n", dst_offset));
	if (((dst_pitch >> pixel_shift) & 0x7) != 0)
		ATI_FALLBACK(("Bad destination pitch 0x%x\n", dst_pitch));

	if (!RadeonTextureSetup(pSrcPicture, pSrc, 0))
		return FALSE;
	pp_cntl = RADEON_TEX_0_ENABLE | RADEON_TEX_BLEND_0_ENABLE;

	if (pMask != NULL) {
		if (!RadeonTextureSetup(pMaskPicture, pMask, 1))
			return FALSE;
		pp_cntl |= RADEON_TEX_1_ENABLE;
	}

	BEGIN(2);
	OUT_REG(RADEON_REG_RE_TOP_LEFT, 0);
	OUT_REG(RADEON_REG_RE_WIDTH_HEIGHT, 0xffffffff);
	END();

	BEGIN(11);
	OUT_REG(RADEON_REG_PP_CNTL, pp_cntl);
	OUT_REG(RADEON_REG_RB3D_CNTL, dst_format | RADEON_ALPHA_BLEND_ENABLE);
	/* COLORPITCH is multiples of 8 pixels, but located at the 3rd bit */
	OUT_REG(RADEON_REG_RB3D_COLORPITCH, (dst_pitch >> pixel_shift));
	OUT_REG(RADEON_REG_RB3D_COLOROFFSET, dst_offset);
	OUT_REG(RADEON_REG_RB3D_PLANEMASK, 0xffffffff);
	OUT_REG(RADEON_REG_SE_CNTL, RADEON_FFACE_CULL_CCW | RADEON_FFACE_SOLID |
	    RADEON_VTX_PIX_CENTER_OGL);
	OUT_REG(RADEON_REG_SE_CNTL_STATUS, RADEON_TCL_BYPASS);
	OUT_REG(RADEON_REG_SE_COORD_FMT,
	    RADEON_VTX_XY_PRE_MULT_1_OVER_W0 |
	    RADEON_VTX_Z_PRE_MULT_1_OVER_W0 |
	    RADEON_VTX_ST0_NONPARAMETRIC |
	    RADEON_VTX_ST1_NONPARAMETRIC |
	    RADEON_TEX1_W_ROUTING_USE_W0);
	if (pMask != NULL) {
		/* IN operator: Multiply src by mask components or mask alpha.
		 * BLEND_CTL_ADD is A * B + C
		 */
		if (pMaskPicture->componentAlpha)
			OUT_REG(RADEON_REG_PP_TXCBLEND_0,
			    RADEON_COLOR_ARG_A_T0_COLOR |
			    RADEON_COLOR_ARG_B_T1_COLOR |
			    RADEON_COLOR_ARG_C_ZERO |
			    RADEON_BLEND_CTL_ADD |
			    RADEON_CLAMP_TX);
		else
			OUT_REG(RADEON_REG_PP_TXCBLEND_0,
			    RADEON_COLOR_ARG_A_T0_COLOR |
			    RADEON_COLOR_ARG_B_T1_ALPHA |
			    RADEON_COLOR_ARG_C_ZERO |
			    RADEON_BLEND_CTL_ADD |
			    RADEON_CLAMP_TX);
		OUT_REG(RADEON_REG_PP_TXABLEND_0,
		    RADEON_ALPHA_ARG_A_T0_ALPHA |
		    RADEON_ALPHA_ARG_B_T1_ALPHA |
		    RADEON_ALPHA_ARG_C_ZERO |
		    RADEON_BLEND_CTL_ADD);
	} else {
		OUT_REG(RADEON_REG_PP_TXCBLEND_0,
		    RADEON_COLOR_ARG_A_ZERO |
		    RADEON_COLOR_ARG_B_ZERO |
		    RADEON_COLOR_ARG_C_T0_COLOR |
		    RADEON_BLEND_CTL_ADD |
		    RADEON_CLAMP_TX);
		OUT_REG(RADEON_REG_PP_TXABLEND_0,
		    RADEON_ALPHA_ARG_A_ZERO |
		    RADEON_ALPHA_ARG_B_ZERO |
		    RADEON_ALPHA_ARG_C_T0_ALPHA |
		    RADEON_BLEND_CTL_ADD);
	}
	/* Op operator. */
	OUT_REG(RADEON_RB3D_BLENDCNTL, RadeonBlendOp[op]);
	END();

	RadeonSwitchTo3D();

	return TRUE;
}

union intfloat {
	float f;
	CARD32 i;
};

struct blend_vertex {
	union intfloat x, y, z;
	union intfloat s0, t0;
	union intfloat s1, t1;
};

#define VTX_REG_COUNT 7

#define VTX_OUT(vtx)		\
do {				\
	OUT_RING(vtx.x.i);	\
	OUT_RING(vtx.y.i);	\
	OUT_RING(vtx.z.i);	\
	OUT_RING(vtx.s0.i);	\
	OUT_RING(vtx.t0.i);	\
	OUT_RING(vtx.s1.i);	\
	OUT_RING(vtx.t1.i);	\
	/*ErrorF("%f,%f,%f %f,%f %f,%f\n", vtx.x.f, vtx.y.f, vtx.z.f, vtx.s0.f, \
	    vtx.t0.f, vtx.s1.f, vtx.t1.f);*/ \
} while (0)

void
RadeonComposite(int srcX, int srcY, int maskX, int maskY, int dstX, int dstY,
    int w, int h)
{
	ATIScreenInfo *atis = accel_atis;
	ATICardInfo *atic = atis->atic;
	LOCALS;
	struct blend_vertex vtx[4];

	/*ErrorF("RadeonComposite %d %d %d %d %d %d\n", srcX, srcY, maskX, maskY,
	    dstX, dstY, w, h);*/
	BEGIN_RING(3 + 6 * VTX_REG_COUNT);
	OUT_RING(RADEON_CP_PACKET3_3D_DRAW_IMMD |
	    ((6 * VTX_REG_COUNT + 1) << 16));
	OUT_RING(RADEON_CP_VC_FRMT_XY |
	    RADEON_CP_VC_FRMT_Z |
	    RADEON_CP_VC_FRMT_ST0 |
	    RADEON_CP_VC_FRMT_ST1);
	OUT_RING(RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_LIST |
	    RADEON_CP_VC_CNTL_PRIM_WALK_RING |
	    RADEON_CP_VC_CNTL_MAOS_ENABLE |
	    RADEON_CP_VC_CNTL_VTX_FMT_RADEON_MODE |
	    (6 << RADEON_CP_VC_CNTL_NUM_SHIFT));

	vtx[0].x.f = dstX;
	vtx[0].y.f = dstY;
	vtx[0].z.f = 0.5;
	vtx[0].s0.f = srcX;
	vtx[0].t0.f = srcY;
	vtx[0].s1.f = maskX;
	vtx[0].t1.f = maskY;

	vtx[1].x.f = dstX;
	vtx[1].y.f = dstY + h;
	vtx[1].z.f = 0.5;
	vtx[1].s0.f = srcX;
	vtx[1].t0.f = srcY + h;
	vtx[1].s1.f = maskX;
	vtx[1].t1.f = maskY + h;

	vtx[2].x.f = dstX + w;
	vtx[2].y.f = dstY + h;
	vtx[2].z.f = 0.5;
	vtx[2].s0.f = srcX + w;
	vtx[2].t0.f = srcY + h;
	vtx[2].s1.f = maskX + w;
	vtx[2].t1.f = maskY + h;

	vtx[3].x.f = dstX + w;
	vtx[3].y.f = dstY;
	vtx[3].z.f = 0.5;
	vtx[3].s0.f = srcX + w;
	vtx[3].t0.f = srcY;
	vtx[3].s1.f = maskX + w;
	vtx[3].t1.f = maskY;

	VTX_OUT(vtx[0]);
	VTX_OUT(vtx[1]);
	VTX_OUT(vtx[2]);
	VTX_OUT(vtx[0]);
	VTX_OUT(vtx[2]);
	VTX_OUT(vtx[3]);

	ADVANCE_RING();
}

void
RadeonDoneComposite(void)
{
	/*ErrorF("RadeonDoneComposite\n");*/
}

Bool
RadeonPrepareBlend(int op, PicturePtr pSrcPicture, PicturePtr pDstPicture,
    PixmapPtr pSrc, PixmapPtr pDst)
{
	return RadeonPrepareComposite(op, pSrcPicture, NULL, pDstPicture, pSrc,
	    NULL, pDst);
}

void
RadeonBlend(int srcX, int srcY, int dstX, int dstY, int width, int height)
{
	RadeonComposite(srcX, srcY, 0, 0, dstX, dstY, width, height);
}

void
RadeonDoneBlend(void)
{
	RadeonDoneComposite();
}