require libnouveau_drm 0.0.109.1
[nouveau] / src / nv50_exa.c
1 /*
2  * Copyright 2007 NVIDIA, Corporation
3  * Copyright 2008 Ben Skeggs
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #include "nv_include.h"
25 #include "nv_rop.h"
26
27 #include "nv50_accel.h"
28 #include "nv50_texture.h"
29
30 struct nv50_exa_state {
31         Bool have_mask;
32
33         struct {
34                 PictTransformPtr transform;
35                 float width;
36                 float height;
37         } unit[2];
38 };
39 static struct nv50_exa_state exa_state;
40
41 #define NV50EXA_LOCALS(p)                                              \
42         ScrnInfoPtr pScrn = xf86Screens[(p)->drawable.pScreen->myNum]; \
43         NVPtr pNv = NVPTR(pScrn);                                      \
44         struct nouveau_channel *chan = pNv->chan; (void)chan;          \
45         struct nouveau_grobj *eng2d = pNv->Nv2D; (void)eng2d;          \
46         struct nouveau_grobj *tesla = pNv->Nv3D; (void)tesla;          \
47         struct nv50_exa_state *state = &exa_state; (void)state
48
49 #define BF(f) (NV50TCL_BLEND_FUNC_SRC_RGB_##f | 0x4000)
50
51 struct nv50_blend_op {
52         unsigned src_alpha;
53         unsigned dst_alpha;
54         unsigned src_blend;
55         unsigned dst_blend;
56 };
57
58 static struct nv50_blend_op
59 NV50EXABlendOp[] = {
60 /* Clear       */ { 0, 0, BF(               ZERO), BF(               ZERO) },
61 /* Src         */ { 0, 0, BF(                ONE), BF(               ZERO) },
62 /* Dst         */ { 0, 0, BF(               ZERO), BF(                ONE) },
63 /* Over        */ { 1, 0, BF(                ONE), BF(ONE_MINUS_SRC_ALPHA) },
64 /* OverReverse */ { 0, 1, BF(ONE_MINUS_DST_ALPHA), BF(                ONE) },
65 /* In          */ { 0, 1, BF(          DST_ALPHA), BF(               ZERO) },
66 /* InReverse   */ { 1, 0, BF(               ZERO), BF(          SRC_ALPHA) },
67 /* Out         */ { 0, 1, BF(ONE_MINUS_DST_ALPHA), BF(               ZERO) },
68 /* OutReverse  */ { 1, 0, BF(               ZERO), BF(ONE_MINUS_SRC_ALPHA) },
69 /* Atop        */ { 1, 1, BF(          DST_ALPHA), BF(ONE_MINUS_SRC_ALPHA) },
70 /* AtopReverse */ { 1, 1, BF(ONE_MINUS_DST_ALPHA), BF(          SRC_ALPHA) },
71 /* Xor         */ { 1, 1, BF(ONE_MINUS_DST_ALPHA), BF(ONE_MINUS_SRC_ALPHA) },
72 /* Add         */ { 0, 0, BF(                ONE), BF(                ONE) },
73 };
74
75 static Bool
76 NV50EXA2DSurfaceFormat(PixmapPtr ppix, uint32_t *fmt)
77 {
78         NV50EXA_LOCALS(ppix);
79
80         switch (ppix->drawable.depth) {
81         case 8 : *fmt = NV50_2D_SRC_FORMAT_8BPP; break;
82         case 15: *fmt = NV50_2D_SRC_FORMAT_15BPP; break;
83         case 16: *fmt = NV50_2D_SRC_FORMAT_16BPP; break;
84         case 24: *fmt = NV50_2D_SRC_FORMAT_24BPP; break;
85         case 32: *fmt = NV50_2D_SRC_FORMAT_32BPP; break;
86         default:
87                  xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
88                             "Unknown surface format for bpp=%d\n",
89                             ppix->drawable.depth);
90                  return FALSE;
91         }
92
93         return TRUE;
94 }
95
96 static void NV50EXASetClip(PixmapPtr ppix, int x, int y, int w, int h)
97 {
98         NV50EXA_LOCALS(ppix);
99
100         BEGIN_RING(chan, eng2d, NV50_2D_CLIP_X, 4);
101         OUT_RING  (chan, x);
102         OUT_RING  (chan, y);
103         OUT_RING  (chan, w);
104         OUT_RING  (chan, h);
105 }
106
107 static Bool
108 NV50EXAAcquireSurface2D(PixmapPtr ppix, int is_src)
109 {
110         NV50EXA_LOCALS(ppix);
111         int mthd = is_src ? NV50_2D_SRC_FORMAT : NV50_2D_DST_FORMAT;
112         uint32_t fmt, bo_flags;
113         struct nouveau_pixmap *surf = nouveau_pixmap(ppix);
114
115         if (!NV50EXA2DSurfaceFormat(ppix, &fmt))
116                 return FALSE;
117
118         bo_flags  = NOUVEAU_BO_VRAM;
119         bo_flags |= is_src ? NOUVEAU_BO_RD : NOUVEAU_BO_WR;
120
121         if (!surf->bo->tiled) {
122                 BEGIN_RING(chan, eng2d, mthd, 2);
123                 OUT_RING  (chan, fmt);
124                 OUT_RING  (chan, 1);
125                 BEGIN_RING(chan, eng2d, mthd + 0x14, 1);
126                 OUT_RING  (chan, (uint32_t)exaGetPixmapPitch(ppix));
127         } else {
128                 BEGIN_RING(chan, eng2d, mthd, 5);
129                 OUT_RING  (chan, fmt);
130                 OUT_RING  (chan, 0);
131                 OUT_RING  (chan, 0);
132                 OUT_RING  (chan, 1);
133                 OUT_RING  (chan, 0);
134         }
135
136         BEGIN_RING(chan, eng2d, mthd + 0x18, 4);
137         OUT_RING  (chan, ppix->drawable.width);
138         OUT_RING  (chan, ppix->drawable.height);
139         OUT_RELOCh(chan, surf->bo, 0, bo_flags);
140         OUT_RELOCl(chan, surf->bo, 0, bo_flags);
141
142         if (is_src == 0)
143                 NV50EXASetClip(ppix, 0, 0, ppix->drawable.width, ppix->drawable.height);
144
145         return TRUE;
146 }
147
148 static void
149 NV50EXASetPattern(PixmapPtr pdpix, int col0, int col1, int pat0, int pat1)
150 {
151         NV50EXA_LOCALS(pdpix);
152
153         BEGIN_RING(chan, eng2d, NV50_2D_PATTERN_COLOR(0), 4);
154         OUT_RING  (chan, col0);
155         OUT_RING  (chan, col1);
156         OUT_RING  (chan, pat0);
157         OUT_RING  (chan, pat1);
158 }
159
160 static void
161 NV50EXASetROP(PixmapPtr pdpix, int alu, Pixel planemask)
162 {
163         NV50EXA_LOCALS(pdpix);
164         int rop;
165
166         if (planemask != ~0)
167                 rop = NVROP[alu].copy_planemask;
168         else
169                 rop = NVROP[alu].copy;
170
171         BEGIN_RING(chan, eng2d, NV50_2D_OPERATION, 1);
172         if (alu == GXcopy && planemask == ~0) {
173                 OUT_RING  (chan, NV50_2D_OPERATION_SRCCOPY);
174                 return;
175         } else {
176                 OUT_RING  (chan, NV50_2D_OPERATION_SRCCOPY_PREMULT);
177         }
178
179         BEGIN_RING(chan, eng2d, NV50_2D_PATTERN_FORMAT, 2);
180         switch (pdpix->drawable.depth) {
181                 case  8: OUT_RING  (chan, 3); break;
182                 case 15: OUT_RING  (chan, 1); break;
183                 case 16: OUT_RING  (chan, 0); break;
184                 case 24:
185                 case 32:
186                 default:
187                          OUT_RING  (chan, 2);
188                          break;
189         }
190         OUT_RING  (chan, 1);
191
192         /* There are 16 alu's.
193          * 0-15: copy
194          * 16-31: copy_planemask
195          */
196
197         if (planemask != ~0) {
198                 alu += 16;
199                 NV50EXASetPattern(pdpix, 0, planemask, ~0, ~0);
200         } else {
201                 if (pNv->currentRop > 15)
202                         NV50EXASetPattern(pdpix, ~0, ~0, ~0, ~0);
203         }
204
205         if (pNv->currentRop != alu) {
206                 BEGIN_RING(chan, eng2d, NV50_2D_ROP, 1);
207                 OUT_RING  (chan, rop);
208                 pNv->currentRop = alu;
209         }
210 }
211
212 Bool
213 NV50EXAPrepareSolid(PixmapPtr pdpix, int alu, Pixel planemask, Pixel fg)
214 {
215         NV50EXA_LOCALS(pdpix);
216         uint32_t fmt;
217
218         planemask |= ~0 << pScrn->depth;
219
220         if (!NV50EXA2DSurfaceFormat(pdpix, &fmt))
221                 NOUVEAU_FALLBACK("rect format\n");
222         if (!NV50EXAAcquireSurface2D(pdpix, 0))
223                 NOUVEAU_FALLBACK("dest pixmap\n");
224         NV50EXASetROP(pdpix, alu, planemask);
225
226         BEGIN_RING(chan, eng2d, 0x580, 3);
227         OUT_RING  (chan, 4);
228         OUT_RING  (chan, fmt);
229         OUT_RING  (chan, fg);
230
231         return TRUE;
232 }
233
234 void
235 NV50EXASolid(PixmapPtr pdpix, int x1, int y1, int x2, int y2)
236 {
237         NV50EXA_LOCALS(pdpix);
238
239         BEGIN_RING(chan, eng2d, NV50_2D_RECT_X1, 4);
240         OUT_RING  (chan, x1);
241         OUT_RING  (chan, y1);
242         OUT_RING  (chan, x2);
243         OUT_RING  (chan, y2);
244
245         if((x2 - x1) * (y2 - y1) >= 512)
246                 FIRE_RING (chan);
247 }
248
249 void
250 NV50EXADoneSolid(PixmapPtr pdpix)
251 {
252 }
253
254 Bool
255 NV50EXAPrepareCopy(PixmapPtr pspix, PixmapPtr pdpix, int dx, int dy,
256                    int alu, Pixel planemask)
257 {
258         NV50EXA_LOCALS(pdpix);
259
260         planemask |= ~0 << pScrn->depth;
261
262         if (!NV50EXAAcquireSurface2D(pspix, 1))
263                 NOUVEAU_FALLBACK("src pixmap\n");
264         if (!NV50EXAAcquireSurface2D(pdpix, 0))
265                 NOUVEAU_FALLBACK("dest pixmap\n");
266         NV50EXASetROP(pdpix, alu, planemask);
267
268         return TRUE;
269 }
270
271 void
272 NV50EXACopy(PixmapPtr pdpix, int srcX , int srcY,
273                              int dstX , int dstY,
274                              int width, int height)
275 {
276         NV50EXA_LOCALS(pdpix);
277
278         BEGIN_RING(chan, eng2d, 0x0110, 1);
279         OUT_RING  (chan, 0);
280         BEGIN_RING(chan, eng2d, 0x088c, 1);
281         OUT_RING  (chan, 0);
282         BEGIN_RING(chan, eng2d, NV50_2D_BLIT_DST_X, 12);
283         OUT_RING  (chan, dstX);
284         OUT_RING  (chan, dstY);
285         OUT_RING  (chan, width);
286         OUT_RING  (chan, height);
287         OUT_RING  (chan, 0);
288         OUT_RING  (chan, 1);
289         OUT_RING  (chan, 0);
290         OUT_RING  (chan, 1);
291         OUT_RING  (chan, 0);
292         OUT_RING  (chan, srcX);
293         OUT_RING  (chan, 0);
294         OUT_RING  (chan, srcY);
295
296         if(width * height >= 512)
297                 FIRE_RING (chan);
298 }
299
300 void
301 NV50EXADoneCopy(PixmapPtr pdpix)
302 {
303 }
304
305 Bool
306 NV50EXAUploadSIFC(const char *src, int src_pitch,
307                   PixmapPtr pdpix, int x, int y, int w, int h, int cpp)
308 {
309         NV50EXA_LOCALS(pdpix);
310         int line_dwords = (w * cpp + 3) / 4;
311         uint32_t sifc_fmt;
312
313         if (!NV50EXA2DSurfaceFormat(pdpix, &sifc_fmt))
314                 NOUVEAU_FALLBACK("hostdata format\n");
315         if (!NV50EXAAcquireSurface2D(pdpix, 0))
316                 NOUVEAU_FALLBACK("dest pixmap\n");
317
318         /* If the pitch isn't aligned to a dword, then you can get corruption at the end of a line. */
319         NV50EXASetClip(pdpix, x, y, w, h);
320
321         BEGIN_RING(chan, eng2d, NV50_2D_OPERATION, 1);
322         OUT_RING  (chan, NV50_2D_OPERATION_SRCCOPY);
323         BEGIN_RING(chan, eng2d, NV50_2D_SIFC_UNK0800, 2);
324         OUT_RING  (chan, 0);
325         OUT_RING  (chan, sifc_fmt);
326         BEGIN_RING(chan, eng2d, NV50_2D_SIFC_WIDTH, 10);
327         OUT_RING  (chan, (line_dwords * 4) / cpp);
328         OUT_RING  (chan, h);
329         OUT_RING  (chan, 0);
330         OUT_RING  (chan, 1);
331         OUT_RING  (chan, 0);
332         OUT_RING  (chan, 1);
333         OUT_RING  (chan, 0);
334         OUT_RING  (chan, x);
335         OUT_RING  (chan, 0);
336         OUT_RING  (chan, y);
337
338         while (h--) {
339                 int count = line_dwords;
340                 const char *p = src;
341
342                 while(count) {
343                         int size = count > 1792 ? 1792 : count;
344
345                         BEGIN_RING(chan, eng2d,
346                                          NV50_2D_SIFC_DATA | 0x40000000, size);
347                         OUT_RINGp (chan, p, size);
348
349                         p += size * cpp;
350                         count -= size;
351                 }
352
353                 src += src_pitch;
354         }
355
356         return TRUE;
357 }
358
359 static Bool
360 NV50EXACheckRenderTarget(PicturePtr ppict)
361 {
362         if (ppict->pDrawable->width > 8192 ||
363             ppict->pDrawable->height > 8192)
364                 NOUVEAU_FALLBACK("render target dimensions exceeded %dx%d\n",
365                                  ppict->pDrawable->width,
366                                  ppict->pDrawable->height);
367
368         switch (ppict->format) {
369         case PICT_a8r8g8b8:
370         case PICT_x8r8g8b8:
371         case PICT_r5g6b5:
372         case PICT_a8:
373                 break;
374         default:
375                 NOUVEAU_FALLBACK("picture format 0x%08x\n", ppict->format);
376         }
377
378         return TRUE;
379 }
380
381 static Bool
382 NV50EXARenderTarget(PixmapPtr ppix, PicturePtr ppict)
383 {
384         NV50EXA_LOCALS(ppix);
385         unsigned format;
386         struct nouveau_pixmap *rt = nouveau_pixmap(ppix);
387
388         /*XXX: Scanout buffer not tiled, someone needs to figure it out */
389         if (!rt->bo->tiled)
390                 NOUVEAU_FALLBACK("pixmap is scanout buffer\n");
391
392         switch (ppict->format) {
393         case PICT_a8r8g8b8: format = NV50TCL_RT_FORMAT_32BPP; break;
394         case PICT_x8r8g8b8: format = NV50TCL_RT_FORMAT_24BPP; break;
395         case PICT_r5g6b5  : format = NV50TCL_RT_FORMAT_16BPP; break;
396         case PICT_a8      : format = NV50TCL_RT_FORMAT_8BPP; break;
397         default:
398                 NOUVEAU_FALLBACK("invalid picture format\n");
399         }
400
401         BEGIN_RING(chan, tesla, NV50TCL_RT_ADDRESS_HIGH(0), 5);
402         OUT_RELOCh(chan, rt->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
403         OUT_RELOCl(chan, rt->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
404         OUT_RING  (chan, format);
405         OUT_RING  (chan, 0);
406         OUT_RING  (chan, 0x00000000);
407         BEGIN_RING(chan, tesla, NV50TCL_RT_HORIZ(0), 2);
408         OUT_RING  (chan, ppix->drawable.width);
409         OUT_RING  (chan, ppix->drawable.height);
410         BEGIN_RING(chan, tesla, 0x1224, 1);
411         OUT_RING  (chan, 0x00000001);
412
413         return TRUE;
414 }
415
416 static Bool
417 NV50EXACheckTexture(PicturePtr ppict)
418 {
419         if (ppict->pDrawable->width > 8192 ||
420             ppict->pDrawable->height > 8192)
421                 NOUVEAU_FALLBACK("texture dimensions exceeded %dx%d\n",
422                                  ppict->pDrawable->width,
423                                  ppict->pDrawable->height);
424
425         switch (ppict->format) {
426         case PICT_a8r8g8b8:
427         case PICT_a8b8g8r8:
428         case PICT_x8r8g8b8:
429         case PICT_x8b8g8r8:
430         case PICT_r5g6b5:
431         case PICT_a8:
432                 break;
433         default:
434                 NOUVEAU_FALLBACK("picture format 0x%08x\n", ppict->format);
435         }
436
437         switch (ppict->filter) {
438         case PictFilterNearest:
439         case PictFilterBilinear:
440                 break;
441         default:
442                 NOUVEAU_FALLBACK("picture filter %d\n", ppict->filter);
443         }
444
445         return TRUE;
446 }
447
448 static Bool
449 NV50EXATexture(PixmapPtr ppix, PicturePtr ppict, unsigned unit)
450 {
451         NV50EXA_LOCALS(ppix);
452         struct nouveau_pixmap *tex = nouveau_pixmap(ppix);
453
454         /*XXX: Scanout buffer not tiled, someone needs to figure it out */
455         if (!tex->bo->tiled)
456                 NOUVEAU_FALLBACK("pixmap is scanout buffer\n");
457
458         BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
459         OUT_RING  (chan, CB_TIC | ((unit * 8) << NV50TCL_CB_ADDR_ID_SHIFT));
460         BEGIN_RING(chan, tesla, NV50TCL_CB_DATA(0) | 0x40000000, 8);
461         switch (ppict->format) {
462         case PICT_a8r8g8b8:
463                 OUT_RING  (chan, NV50TIC_0_0_MAPA_C3 | NV50TIC_0_0_TYPEA_UNORM |
464                          NV50TIC_0_0_MAPR_C0 | NV50TIC_0_0_TYPER_UNORM |
465                          NV50TIC_0_0_MAPG_C1 | NV50TIC_0_0_TYPEB_UNORM |
466                          NV50TIC_0_0_MAPB_C2 | NV50TIC_0_0_TYPEG_UNORM |
467                          NV50TIC_0_0_FMT_8_8_8_8);
468                 break;
469         case PICT_a8b8g8r8:
470                 OUT_RING  (chan, NV50TIC_0_0_MAPA_C3 | NV50TIC_0_0_TYPEA_UNORM |
471                          NV50TIC_0_0_MAPR_C2 | NV50TIC_0_0_TYPER_UNORM |
472                          NV50TIC_0_0_MAPG_C1 | NV50TIC_0_0_TYPEB_UNORM |
473                          NV50TIC_0_0_MAPB_C0 | NV50TIC_0_0_TYPEG_UNORM |
474                          NV50TIC_0_0_FMT_8_8_8_8);
475                 break;
476         case PICT_x8r8g8b8:
477                 OUT_RING  (chan, NV50TIC_0_0_MAPA_ONE | NV50TIC_0_0_TYPEA_UNORM |
478                          NV50TIC_0_0_MAPR_C0 | NV50TIC_0_0_TYPER_UNORM |
479                          NV50TIC_0_0_MAPG_C1 | NV50TIC_0_0_TYPEB_UNORM |
480                          NV50TIC_0_0_MAPB_C2 | NV50TIC_0_0_TYPEG_UNORM |
481                          NV50TIC_0_0_FMT_8_8_8_8);
482                 break;
483         case PICT_x8b8g8r8:
484                 OUT_RING  (chan, NV50TIC_0_0_MAPA_ONE | NV50TIC_0_0_TYPEA_UNORM |
485                          NV50TIC_0_0_MAPR_C2 | NV50TIC_0_0_TYPER_UNORM |
486                          NV50TIC_0_0_MAPG_C1 | NV50TIC_0_0_TYPEB_UNORM |
487                          NV50TIC_0_0_MAPB_C0 | NV50TIC_0_0_TYPEG_UNORM |
488                          NV50TIC_0_0_FMT_8_8_8_8);
489                 break;
490         case PICT_r5g6b5:
491                 OUT_RING  (chan, NV50TIC_0_0_MAPA_ONE | NV50TIC_0_0_TYPEA_UNORM |
492                          NV50TIC_0_0_MAPR_C0 | NV50TIC_0_0_TYPER_UNORM |
493                          NV50TIC_0_0_MAPG_C1 | NV50TIC_0_0_TYPEB_UNORM |
494                          NV50TIC_0_0_MAPB_C2 | NV50TIC_0_0_TYPEG_UNORM |
495                          NV50TIC_0_0_FMT_5_6_5);
496                 break;
497         case PICT_a8:
498                 OUT_RING  (chan, NV50TIC_0_0_MAPA_C0 | NV50TIC_0_0_TYPEA_UNORM |
499                          NV50TIC_0_0_MAPR_ZERO | NV50TIC_0_0_TYPER_UNORM |
500                          NV50TIC_0_0_MAPG_ZERO | NV50TIC_0_0_TYPEB_UNORM |
501                          NV50TIC_0_0_MAPB_ZERO | NV50TIC_0_0_TYPEG_UNORM |
502                          NV50TIC_0_0_FMT_8);
503                 break;
504         default:
505                 NOUVEAU_FALLBACK("invalid picture format, this SHOULD NOT HAPPEN. Expect trouble.\n");
506         }
507         OUT_RELOCl(chan, tex->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
508         OUT_RING  (chan, 0xd0005000);
509         OUT_RING  (chan, 0x00300000);
510         OUT_RING  (chan, ppix->drawable.width);
511         OUT_RING  (chan, (1 << NV50TIC_0_5_DEPTH_SHIFT) | ppix->drawable.height);
512         OUT_RING  (chan, 0x03000000);
513         OUT_RELOCh(chan, tex->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
514
515         BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
516         OUT_RING  (chan, CB_TSC | ((unit * 8) << NV50TCL_CB_ADDR_ID_SHIFT));
517         BEGIN_RING(chan, tesla, NV50TCL_CB_DATA(0) | 0x40000000, 8);
518         if (ppict->repeat) {
519                 switch (ppict->repeatType) {
520                 case RepeatPad:
521                         OUT_RING  (chan, NV50TSC_1_0_WRAPS_CLAMP |
522                                  NV50TSC_1_0_WRAPT_CLAMP |
523                                  NV50TSC_1_0_WRAPR_CLAMP | 0x00024000);
524                         break;
525                 case RepeatReflect:
526                         OUT_RING  (chan, NV50TSC_1_0_WRAPS_MIRROR_REPEAT |
527                                  NV50TSC_1_0_WRAPT_MIRROR_REPEAT |
528                                  NV50TSC_1_0_WRAPR_MIRROR_REPEAT | 0x00024000);
529                         break;
530                 case RepeatNormal:
531                 default:
532                         OUT_RING  (chan, NV50TSC_1_0_WRAPS_REPEAT |
533                                  NV50TSC_1_0_WRAPT_REPEAT |
534                                  NV50TSC_1_0_WRAPR_REPEAT | 0x00024000);
535                         break;
536                 }
537         } else {
538                 OUT_RING  (chan, NV50TSC_1_0_WRAPS_CLAMP_TO_BORDER |
539                          NV50TSC_1_0_WRAPT_CLAMP_TO_BORDER |
540                          NV50TSC_1_0_WRAPR_CLAMP_TO_BORDER | 0x00024000);
541         }
542         if (ppict->filter == PictFilterBilinear) {
543                 OUT_RING  (chan, NV50TSC_1_1_MAGF_LINEAR |
544                          NV50TSC_1_1_MINF_LINEAR |
545                          NV50TSC_1_1_MIPF_NONE);
546         } else {
547                 OUT_RING  (chan, NV50TSC_1_1_MAGF_NEAREST |
548                          NV50TSC_1_1_MINF_NEAREST |
549                          NV50TSC_1_1_MIPF_NONE);
550         }
551         OUT_RING  (chan, 0x00000000);
552         OUT_RING  (chan, 0x00000000);
553         OUT_RING  (chan, 0x00000000);
554         OUT_RING  (chan, 0x00000000);
555         OUT_RING  (chan, 0x00000000);
556         OUT_RING  (chan, 0x00000000);
557
558         state->unit[unit].width = ppix->drawable.width;
559         state->unit[unit].height = ppix->drawable.height;
560         state->unit[unit].transform = ppict->transform;
561         return TRUE;
562 }
563
564 static Bool
565 NV50EXACheckBlend(int op)
566 {
567         if (op > PictOpAdd)
568                 NOUVEAU_FALLBACK("unsupported blend op %d\n", op);
569         return TRUE;
570 }
571
572 static void
573 NV50EXABlend(PixmapPtr ppix, PicturePtr ppict, int op, int component_alpha)
574 {
575         NV50EXA_LOCALS(ppix);
576         struct nv50_blend_op *b = &NV50EXABlendOp[op];
577         unsigned sblend = b->src_blend;
578         unsigned dblend = b->dst_blend;
579
580         if (b->dst_alpha) {
581                 if (!PICT_FORMAT_A(ppict->format)) {
582                         if (sblend == BF(DST_ALPHA))
583                                 sblend = BF(ONE);
584                         else
585                         if (sblend == BF(ONE_MINUS_DST_ALPHA))
586                                 sblend = BF(ZERO);
587                 } else
588                 if (ppict->format == PICT_a8) {
589                         if (sblend == BF(DST_ALPHA))
590                                 sblend = BF(DST_COLOR);
591                         else
592                         if (sblend == BF(ONE_MINUS_DST_ALPHA))
593                                 sblend = BF(ONE_MINUS_DST_COLOR);
594                 }
595         }
596
597         if (b->src_alpha && (component_alpha || ppict->format == PICT_a8)) {
598                 if (dblend == BF(SRC_ALPHA))
599                         dblend = BF(SRC_COLOR);
600                 else
601                 if (dblend == BF(ONE_MINUS_SRC_ALPHA))
602                         dblend = BF(ONE_MINUS_SRC_COLOR);
603         }
604
605         if (sblend == BF(ONE) && dblend == BF(ZERO)) {
606                 BEGIN_RING(chan, tesla, NV50TCL_BLEND_ENABLE(0), 1);
607                 OUT_RING  (chan, 0);
608         } else {
609                 BEGIN_RING(chan, tesla, NV50TCL_BLEND_ENABLE(0), 1);
610                 OUT_RING  (chan, 1);
611                 BEGIN_RING(chan, tesla, NV50TCL_BLEND_EQUATION_RGB, 5);
612                 OUT_RING  (chan, NV50TCL_BLEND_EQUATION_RGB_FUNC_ADD);
613                 OUT_RING  (chan, sblend);
614                 OUT_RING  (chan, dblend);
615                 OUT_RING  (chan, NV50TCL_BLEND_EQUATION_ALPHA_FUNC_ADD);
616                 OUT_RING  (chan, sblend);
617                 BEGIN_RING(chan, tesla, NV50TCL_BLEND_FUNC_DST_ALPHA, 1);
618                 OUT_RING  (chan, dblend);
619         }
620 }
621
622 Bool
623 NV50EXACheckComposite(int op,
624                       PicturePtr pspict, PicturePtr pmpict, PicturePtr pdpict)
625 {
626         if (!NV50EXACheckBlend(op))
627                 NOUVEAU_FALLBACK("blend not supported\n");
628
629         if (!NV50EXACheckRenderTarget(pdpict))
630                 NOUVEAU_FALLBACK("render target invalid\n");
631
632         if (!NV50EXACheckTexture(pspict))
633                 NOUVEAU_FALLBACK("src picture invalid\n");
634
635         if (pmpict) {
636                 if (pmpict->componentAlpha &&
637                     PICT_FORMAT_RGB(pmpict->format) &&
638                     NV50EXABlendOp[op].src_alpha &&
639                     NV50EXABlendOp[op].src_blend != BF(ZERO))
640                         NOUVEAU_FALLBACK("component-alpha not supported\n");
641
642                 if (!NV50EXACheckTexture(pmpict))
643                         NOUVEAU_FALLBACK("mask picture invalid\n");
644         }
645
646         return TRUE;
647 }
648
649 Bool
650 NV50EXAPrepareComposite(int op,
651                         PicturePtr pspict, PicturePtr pmpict, PicturePtr pdpict,
652                         PixmapPtr pspix, PixmapPtr pmpix, PixmapPtr pdpix)
653 {
654         NV50EXA_LOCALS(pspix);
655
656         BEGIN_RING(chan, eng2d, 0x0110, 1);
657         OUT_RING  (chan, 0);
658
659         if (!NV50EXARenderTarget(pdpix, pdpict))
660                 NOUVEAU_FALLBACK("render target invalid\n");
661
662         NV50EXABlend(pdpix, pdpict, op, pmpict && pmpict->componentAlpha &&
663                      PICT_FORMAT_RGB(pmpict->format));
664
665         if (pmpict) {
666                 if (!NV50EXATexture(pspix, pspict, 0))
667                         NOUVEAU_FALLBACK("src picture invalid\n");
668                 if (!NV50EXATexture(pmpix, pmpict, 1))
669                         NOUVEAU_FALLBACK("mask picture invalid\n");
670                 state->have_mask = TRUE;
671
672                 BEGIN_RING(chan, tesla, NV50TCL_FP_START_ID, 1);
673                 if (pdpict->format == PICT_a8) {
674                         OUT_RING  (chan, PFP_C_A8);
675                 } else {
676                         if (pmpict->componentAlpha &&
677                             PICT_FORMAT_RGB(pmpict->format)) {
678                                 if (NV50EXABlendOp[op].src_alpha)
679                                         OUT_RING  (chan, PFP_CCASA);
680                                 else
681                                         OUT_RING  (chan, PFP_CCA);
682                         } else {
683                                 OUT_RING  (chan, PFP_C);
684                         }
685                 }
686         } else {
687                 if (!NV50EXATexture(pspix, pspict, 0))
688                         NOUVEAU_FALLBACK("src picture invalid\n");
689                 state->have_mask = FALSE;
690
691                 BEGIN_RING(chan, tesla, NV50TCL_FP_START_ID, 1);
692                 if (pdpict->format == PICT_a8)
693                         OUT_RING  (chan, PFP_S_A8);
694                 else
695                         OUT_RING  (chan, PFP_S);
696         }
697
698         BEGIN_RING(chan, tesla, 0x1334, 1);
699         OUT_RING  (chan, 0);
700
701         BEGIN_RING(chan, tesla, 0x1458, 1);
702         OUT_RING  (chan, 1);
703         BEGIN_RING(chan, tesla, 0x1458, 1);
704         OUT_RING  (chan, 0x203);
705
706         BEGIN_RING(chan, tesla, NV50TCL_VERTEX_BEGIN, 1);
707         OUT_RING  (chan, NV50TCL_VERTEX_BEGIN_QUADS);
708
709         return TRUE;
710 }
711
712 #define xFixedToFloat(v) \
713         ((float)xFixedToInt((v)) + ((float)xFixedFrac(v) / 65536.0))
714 static inline void
715 NV50EXATransform(PictTransformPtr t, int x, int y, float sx, float sy,
716                  float *x_ret, float *y_ret)
717 {
718         if (t) {
719                 PictVector v;
720
721                 v.vector[0] = IntToxFixed(x);
722                 v.vector[1] = IntToxFixed(y);
723                 v.vector[2] = xFixed1;
724                 PictureTransformPoint(t, &v);
725                 *x_ret = xFixedToFloat(v.vector[0]) / sx;
726                 *y_ret = xFixedToFloat(v.vector[1]) / sy;
727         } else {
728                 *x_ret = (float)x / sx;
729                 *y_ret = (float)y / sy;
730         }
731 }
732
733 void
734 NV50EXAComposite(PixmapPtr pdpix, int sx, int sy, int mx, int my,
735                  int dx, int dy, int w, int h)
736 {
737         NV50EXA_LOCALS(pdpix);
738         float sX0, sX1, sX2, sX3, sY0, sY1, sY2, sY3;
739         unsigned dX0 = dx, dX1 = dx + w, dY0 = dy, dY1 = dy + h;
740
741         NV50EXATransform(state->unit[0].transform, sx, sy,
742                          state->unit[0].width, state->unit[0].height,
743                          &sX0, &sY0);
744         NV50EXATransform(state->unit[0].transform, sx + w, sy,
745                          state->unit[0].width, state->unit[0].height,
746                          &sX1, &sY1);
747         NV50EXATransform(state->unit[0].transform, sx + w, sy + h,
748                          state->unit[0].width, state->unit[0].height,
749                          &sX2, &sY2);
750         NV50EXATransform(state->unit[0].transform, sx, sy + h,
751                          state->unit[0].width, state->unit[0].height,
752                          &sX3, &sY3);
753
754         if (state->have_mask) {
755                 float mX0, mX1, mX2, mX3, mY0, mY1, mY2, mY3;
756
757                 NV50EXATransform(state->unit[1].transform, mx, my,
758                                  state->unit[1].width, state->unit[1].height,
759                                  &mX0, &mY0);
760                 NV50EXATransform(state->unit[1].transform, mx + w, my,
761                                  state->unit[1].width, state->unit[1].height,
762                                  &mX1, &mY1);
763                 NV50EXATransform(state->unit[1].transform, mx + w, my + h,
764                                  state->unit[1].width, state->unit[1].height,
765                                  &mX2, &mY2);
766                 NV50EXATransform(state->unit[1].transform, mx, my + h,
767                                  state->unit[1].width, state->unit[1].height,
768                                  &mX3, &mY3);
769
770                 VTX2s(pNv, sX0, sY0, mX0, mY0, dX0, dY0);
771                 VTX2s(pNv, sX1, sY1, mX1, mY1, dX1, dY0);
772                 VTX2s(pNv, sX2, sY2, mX2, mY2, dX1, dY1);
773                 VTX2s(pNv, sX3, sY3, mX3, mY3, dX0, dY1);
774         } else {
775                 VTX1s(pNv, sX0, sY0, dX0, dY0);
776                 VTX1s(pNv, sX1, sY1, dX1, dY0);
777                 VTX1s(pNv, sX2, sY2, dX1, dY1);
778                 VTX1s(pNv, sX3, sY3, dX0, dY1);
779         }
780 }
781
782 void
783 NV50EXADoneComposite(PixmapPtr pdpix)
784 {
785         NV50EXA_LOCALS(pdpix);
786
787         BEGIN_RING(chan, tesla, NV50TCL_VERTEX_END, 1);
788         OUT_RING  (chan, 0);
789 }
790