Improve some nv_bios messages, remove others
[nouveau] / src / nv30_xv_tex.c
1 /*
2  * Copyright 2007-2008 Maarten Maathuis
3  * Copyright 2008 Stephane Marchesin
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 (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "xf86xv.h"
30 #include <X11/extensions/Xv.h>
31 #include "exa.h"
32 #include "damage.h"
33 #include "dixstruct.h"
34 #include "fourcc.h"
35
36 #include "nv_include.h"
37 #include "nv_dma.h"
38
39 #include "nv30_shaders.h"
40
41 extern Atom xvSyncToVBlank, xvSetDefaults;
42
43 /*
44  * The filtering function used for video scaling. We use a cubic filter as defined in 
45  * "Reconstruction Filters in Computer Graphics"
46  * Mitchell & Netravali in SIGGRAPH '88 
47  */
48 static float filter_func(float x)
49 {
50         const double B=0.75;
51         const double C=(1.0-B)/2.0;
52         double x1=fabs(x);
53         double x2=fabs(x)*x1;
54         double x3=fabs(x)*x2;
55
56         if (fabs(x)<1.0) 
57                 return ( (12.0-9.0*B-6.0*C)*x3+(-18.0+12.0*B+6.0*C)*x2+(6.0-2.0*B) )/6.0; 
58         else 
59                 return ( (-B-6.0*C)*x3+(6.0*B+30.0*C)*x2+(-12.0*B-48.0*C)*x1+(8.0*B+24.0*C) )/6.0;
60 }
61
62 static int8_t f32tosb8(float v)
63 {
64         return (int8_t)(v*127.0);
65 }
66
67 /*
68  * 512 means 2048 bytes of VRAM
69  */
70 #define TABLE_SIZE 512
71 static void compute_filter_table(int8_t *t) {
72         int i;
73         float x;
74         for(i=0;i<TABLE_SIZE;i++) {
75                 x=(i+0.5)/TABLE_SIZE;
76
77                 float w0=filter_func(x+1.0);
78                 float w1=filter_func(x);
79                 float w2=filter_func(x-1.0);
80                 float w3=filter_func(x-2.0);
81
82                 t[4*i+2]=f32tosb8(1.0+x-w1/(w0+w1));
83                 t[4*i+1]=f32tosb8(1.0-x+w3/(w2+w3));
84                 t[4*i+0]=f32tosb8(w0+w1);
85                 t[4*i+3]=f32tosb8(0.0);
86         }
87 }
88
89 static struct nouveau_bo *table_mem = NULL;
90 static void
91 NV30_LoadFilterTable(ScrnInfoPtr pScrn)
92 {
93         NVPtr pNv = NVPTR(pScrn);
94
95         if (!table_mem) {
96                 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART,
97                                 0, TABLE_SIZE*sizeof(float)*4, &table_mem)) {
98                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
99                                 "Couldn't alloc filter table!\n");
100                         return;
101                 }
102
103                 if (nouveau_bo_map(table_mem, NOUVEAU_BO_RDWR)) {
104                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
105                                    "Couldn't map filter table!\n");
106                         return;
107                 }
108
109                 int8_t *t=table_mem->map;
110                 compute_filter_table(t);
111         }
112 }
113
114 #define SWIZZLE(ts0x,ts0y,ts0z,ts0w,ts1x,ts1y,ts1z,ts1w)                        \
115         (                                                                       \
116         NV34TCL_TX_SWIZZLE_S0_X_##ts0x | NV34TCL_TX_SWIZZLE_S0_Y_##ts0y |       \
117         NV34TCL_TX_SWIZZLE_S0_Z_##ts0z | NV34TCL_TX_SWIZZLE_S0_W_##ts0w |       \
118         NV34TCL_TX_SWIZZLE_S1_X_##ts1x | NV34TCL_TX_SWIZZLE_S1_Y_##ts1y |       \
119         NV34TCL_TX_SWIZZLE_S1_Z_##ts1z | NV34TCL_TX_SWIZZLE_S1_W_##ts1w         \
120         )
121
122 /*
123  * Texture 0 : filter table
124  * Texture 1 : Y data
125  * Texture 2 : UV data
126  */
127 static Bool
128 NV30VideoTexture(ScrnInfoPtr pScrn, struct nouveau_bo *src, int offset,
129                  uint16_t width, uint16_t height, uint16_t src_pitch, int unit)
130 {
131         NVPtr pNv = NVPTR(pScrn);
132         struct nouveau_channel *chan = pNv->chan;
133         struct nouveau_grobj *rankine = pNv->Nv3D;
134
135         uint32_t card_fmt = 0;
136         uint32_t card_swz = 0;
137
138         switch(unit) {
139                 case 0:
140                 card_fmt = NV34TCL_TX_FORMAT_FORMAT_A8R8G8B8;
141                 card_swz = SWIZZLE(S1, S1, S1, S1, X, Y, Z, W);
142                 break;
143                 case 1:
144                 card_fmt = NV34TCL_TX_FORMAT_FORMAT_A8_RECT2;
145                 card_swz = SWIZZLE(S1, S1, S1, S1, X, X, X, X);
146                 break;
147                 case 2:
148                 card_fmt = NV34TCL_TX_FORMAT_FORMAT_L8A8_RECT;
149 #if X_BYTE_ORDER == X_BIG_ENDIAN
150                 card_swz = SWIZZLE(S1, S1, S1, S1, Z, W, X, Y); /* x = V, y = U */
151 #else
152                 card_swz = SWIZZLE(S1, S1, S1, S1, W, Z, Y, X); /* x = V, y = U */
153 #endif
154                 break;
155         }
156
157         BEGIN_RING(chan, rankine, NV34TCL_TX_OFFSET(unit), 8);
158         OUT_RELOCl(chan, src, offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
159         if (unit==0) {
160                 OUT_RELOCd(chan, pNv->FB, NV34TCL_TX_FORMAT_DIMS_1D | card_fmt |
161                                  (1 << NV34TCL_TX_FORMAT_MIPMAP_LEVELS_SHIFT) |
162                                  (log2i(width) <<
163                                   NV34TCL_TX_FORMAT_BASE_SIZE_U_SHIFT) |
164                                  (log2i(height) <<
165                                   NV34TCL_TX_FORMAT_BASE_SIZE_V_SHIFT) |
166                                  8 /* no idea */,
167                                  NOUVEAU_BO_VRAM | NOUVEAU_BO_RD,
168                                  NV34TCL_TX_FORMAT_DMA0, 0);
169                 OUT_RING  (chan, NV34TCL_TX_WRAP_S_REPEAT |
170                                  NV34TCL_TX_WRAP_T_CLAMP_TO_EDGE |
171                                  NV34TCL_TX_WRAP_R_CLAMP_TO_EDGE);
172         } else {
173                 OUT_RELOCd(chan, pNv->FB, NV34TCL_TX_FORMAT_DIMS_2D | card_fmt |
174                                  (1 << NV34TCL_TX_FORMAT_MIPMAP_LEVELS_SHIFT) |
175                                  (log2i(width) <<
176                                   NV34TCL_TX_FORMAT_BASE_SIZE_U_SHIFT) |
177                                  (log2i(height) <<
178                                   NV34TCL_TX_FORMAT_BASE_SIZE_V_SHIFT) |
179                                  8 /* no idea */,
180                                  NOUVEAU_BO_VRAM | NOUVEAU_BO_RD,
181                                  NV34TCL_TX_FORMAT_DMA0, 0);
182                 OUT_RING  (chan, NV34TCL_TX_WRAP_S_CLAMP_TO_EDGE |
183                                  NV34TCL_TX_WRAP_T_CLAMP_TO_EDGE |
184                                  NV34TCL_TX_WRAP_R_CLAMP_TO_EDGE);
185         }
186
187         OUT_RING  (chan, NV34TCL_TX_ENABLE_ENABLE);
188         OUT_RING  (chan, (src_pitch << NV34TCL_TX_SWIZZLE_RECT_PITCH_SHIFT) |
189                          card_swz);
190         if (unit==0)
191                 OUT_RING  (chan, NV34TCL_TX_FILTER_SIGNED_ALPHA |
192                                  NV34TCL_TX_FILTER_SIGNED_RED |
193                                  NV34TCL_TX_FILTER_SIGNED_GREEN |
194                                  NV34TCL_TX_FILTER_SIGNED_BLUE |
195                                  NV34TCL_TX_FILTER_MINIFY_LINEAR |
196                                  NV34TCL_TX_FILTER_MAGNIFY_LINEAR | 0x2000);
197         else
198                 OUT_RING  (chan, NV34TCL_TX_FILTER_MINIFY_LINEAR |
199                                  NV34TCL_TX_FILTER_MAGNIFY_LINEAR | 0x2000);
200         OUT_RING  (chan, (width << NV34TCL_TX_NPOT_SIZE_W_SHIFT) | height);
201         OUT_RING  (chan, 0); /* border ARGB */
202
203         return TRUE;
204 }
205
206 Bool
207 NV30GetSurfaceFormat(PixmapPtr ppix, int *fmt_ret)
208 {
209         switch (ppix->drawable.bitsPerPixel) {
210                 case 32:
211                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_A8R8G8B8;
212                         break;
213                 case 24:
214                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_X8R8G8B8;
215                         break;
216                 case 16:
217                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_R5G6B5;
218                         break;
219                 case 8:
220                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_B8;
221                         break;
222                 default:
223                         return FALSE;
224         }
225
226         return TRUE;
227 }
228
229 void
230 NV30StopTexturedVideo(ScrnInfoPtr pScrn, pointer data, Bool Exit)
231 {
232 }
233
234 #define VERTEX_OUT(sx,sy,dx,dy) do {                                           \
235         BEGIN_RING(chan, rankine, NV34TCL_VTX_ATTR_2F_X(8), 4);                \
236         OUT_RINGf (chan, (sx)); OUT_RINGf (chan, (sy));                        \
237         OUT_RINGf (chan, (sx)/2.0); OUT_RINGf (chan, (sy)/2.0);                \
238         BEGIN_RING(chan, rankine, NV34TCL_VTX_ATTR_2I(0), 1);                  \
239         OUT_RING  (chan, ((dy)<<16)|(dx));                                     \
240 } while(0)
241
242 int
243 NV30PutTextureImage(ScrnInfoPtr pScrn, struct nouveau_bo *src, int src_offset,
244                     int src_offset2, int id, int src_pitch, BoxPtr dstBox,
245                     int x1, int y1, int x2, int y2,
246                     uint16_t width, uint16_t height,
247                     uint16_t src_w, uint16_t src_h,
248                     uint16_t drw_w, uint16_t drw_h,
249                     RegionPtr clipBoxes,
250                     PixmapPtr ppix,
251                     NVPortPrivPtr pPriv)
252 {
253         NVPtr pNv = NVPTR(pScrn);
254         struct nouveau_channel *chan = pNv->chan;
255         struct nouveau_grobj *rankine = pNv->Nv3D;
256         Bool redirected = FALSE;
257         float X1, X2, Y1, Y2;
258         BoxPtr pbox;
259         int nbox;
260         int dst_format = 0;
261
262         if (drw_w > 4096 || drw_h > 4096) {
263                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
264                         "XV: Draw size too large.\n");
265                 return BadAlloc;
266         }
267
268         if (!NV30GetSurfaceFormat(ppix, &dst_format)) {
269                 ErrorF("No surface format, bad.\n");
270         }
271
272 #ifdef COMPOSITE
273         if (!NVExaPixmapIsOnscreen(ppix))
274                 redirected = TRUE;
275 #endif
276
277         pbox = REGION_RECTS(clipBoxes);
278         nbox = REGION_NUM_RECTS(clipBoxes);
279
280         /* Disable blending */
281         BEGIN_RING(chan, rankine, NV34TCL_BLEND_FUNC_ENABLE, 1);
282         OUT_RING  (chan, 0);
283
284         /* Setup surface */
285         BEGIN_RING(chan, rankine, NV34TCL_RT_FORMAT, 3);
286         OUT_RING  (chan, NV34TCL_RT_FORMAT_TYPE_LINEAR |
287                          NV34TCL_RT_FORMAT_ZETA_Z24S8 |
288                          dst_format);
289         OUT_RING  (chan, (exaGetPixmapPitch(ppix) << 16) |
290                           exaGetPixmapPitch(ppix));
291         OUT_PIXMAPl(chan, ppix, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
292
293         if (pNv->NVArch == 0x30) {
294                 int x = 0;
295                 int y = 0;
296                 int w = ppix->drawable.x + ppix->drawable.width;
297                 int h = ppix->drawable.y + ppix->drawable.height;
298
299                 BEGIN_RING(chan, rankine, NV34TCL_VIEWPORT_HORIZ, 2);
300                 OUT_RING  (chan, (w<<16)|x);
301                 OUT_RING  (chan, (h<<16)|y);
302                 BEGIN_RING(chan, rankine, NV34TCL_VIEWPORT_CLIP_HORIZ(0), 2);
303                 OUT_RING  (chan, (w-1+x)<<16);
304                 OUT_RING  (chan, (h-1+y)<<16);
305                 BEGIN_RING(chan, rankine, NV34TCL_VIEWPORT_TX_ORIGIN, 1);
306                 OUT_RING  (chan, (y<<16)|x);
307         }
308
309         NV30_LoadFilterTable(pScrn);
310
311         BEGIN_RING(chan, rankine, NV34TCL_TX_UNITS_ENABLE, 1);
312         OUT_RING  (chan, NV34TCL_TX_UNITS_ENABLE_TX0 |
313                          NV34TCL_TX_UNITS_ENABLE_TX1);
314
315         NV30VideoTexture(pScrn, table_mem, 0, TABLE_SIZE, 1, 0 , 0);
316         NV30VideoTexture(pScrn, src, src_offset, src_w, src_h, src_pitch, 1);
317         /* We've got NV12 format, which means half width and half height texture of chroma channels. */
318         NV30VideoTexture(pScrn, src, src_offset2, src_w/2, src_h/2, src_pitch, 2);
319
320         BEGIN_RING(chan, rankine, NV34TCL_TX_ENABLE(3), 1);
321         OUT_RING  (chan, 0x0);
322
323         if (pPriv->bicubic)
324                 NV30_LoadFragProg(pScrn, &nv30_fp_yv12_bicubic);
325         else
326                 NV30_LoadFragProg(pScrn, &nv30_fp_yv12_bilinear);
327
328         /* Just before rendering we wait for vblank in the non-composited case. */
329         if (pPriv->SyncToVBlank && !redirected) {
330                 uint8_t crtcs = nv_window_belongs_to_crtc(pScrn, dstBox->x1, dstBox->y1,
331                         dstBox->x2 - dstBox->x1, dstBox->y2 - dstBox->y1);
332
333                 FIRE_RING (chan);
334                 if (crtcs & 0x1)
335                         NVWaitVSync(pScrn, 0);
336                 else if (crtcs & 0x2)
337                         NVWaitVSync(pScrn, 1);
338         }
339
340         /* These are fixed point values in the 16.16 format. */
341         X1 = (float)(x1>>16)+(float)(x1&0xFFFF)/(float)0x10000;
342         Y1 = (float)(y1>>16)+(float)(y1&0xFFFF)/(float)0x10000;
343         X2 = (float)(x2>>16)+(float)(x2&0xFFFF)/(float)0x10000;
344         Y2 = (float)(y2>>16)+(float)(y2&0xFFFF)/(float)0x10000;
345
346         BEGIN_RING(chan, rankine, NV34TCL_VERTEX_BEGIN_END, 1);
347         OUT_RING  (chan, NV34TCL_VERTEX_BEGIN_END_TRIANGLES);
348
349         while(nbox--) {
350                 float tx1=X1+(float)(pbox->x1 - dstBox->x1)*(X2-X1)/(float)(drw_w);
351                 float tx2=X1+(float)(pbox->x2 - dstBox->x1)*(src_w)/(float)(drw_w);
352                 float ty1=Y1+(float)(pbox->y1 - dstBox->y1)*(Y2-Y1)/(float)(drw_h);
353                 float ty2=Y1+(float)(pbox->y2 - dstBox->y1)*(src_h)/(float)(drw_h);
354                 int sx1=pbox->x1;
355                 int sx2=pbox->x2;
356                 int sy1=pbox->y1;
357                 int sy2=pbox->y2;
358
359                 BEGIN_RING(chan, rankine, NV34TCL_SCISSOR_HORIZ, 2);
360                 OUT_RING  (chan, (sx2 << 16) | 0);
361                 OUT_RING  (chan, (sy2 << 16) | 0);
362
363                 VERTEX_OUT(tx1, ty1, sx1, sy1);
364                 VERTEX_OUT(tx2+(tx2-tx1), ty1, sx2+(sx2-sx1), sy1);
365                 VERTEX_OUT(tx1, ty2+(ty2-ty1), sx1, sy2+(sy2-sy1));
366
367                 pbox++;
368         }
369
370         BEGIN_RING(chan, rankine, NV34TCL_VERTEX_BEGIN_END, 1);
371         OUT_RING  (chan, NV34TCL_VERTEX_BEGIN_END_STOP);
372
373         FIRE_RING (chan);
374
375         return Success;
376 }
377
378 /**
379  * NV30SetTexturePortAttribute
380  * sets the attribute "attribute" of port "data" to value "value"
381  * supported attributes:
382  * Sync to vblank.
383  * 
384  * @param pScrenInfo
385  * @param attribute attribute to set
386  * @param value value to which attribute is to be set
387  * @param data port from which the attribute is to be set
388  * 
389  * @return Success, if setting is successful
390  * BadValue/BadMatch, if value/attribute are invalid
391  */
392 int
393 NV30SetTexturePortAttribute(ScrnInfoPtr pScrn, Atom attribute,
394                        INT32 value, pointer data)
395 {
396         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
397         NVPtr           pNv = NVPTR(pScrn);
398
399         if ((attribute == xvSyncToVBlank) && pNv->WaitVSyncPossible) {
400                 if ((value < 0) || (value > 1))
401                         return BadValue;
402                 pPriv->SyncToVBlank = value;
403         } else
404         if (attribute == xvSetDefaults) {
405                 pPriv->SyncToVBlank = pNv->WaitVSyncPossible;
406         } else
407                 return BadMatch;
408
409         return Success;
410 }
411
412 /**
413  * NV30GetTexturePortAttribute
414  * reads the value of attribute "attribute" from port "data" into INT32 "*value"
415  * Sync to vblank.
416  * 
417  * @param pScrn unused
418  * @param attribute attribute to be read
419  * @param value value of attribute will be stored here
420  * @param data port from which attribute will be read
421  * @return Success, if queried attribute exists
422  */
423 int
424 NV30GetTexturePortAttribute(ScrnInfoPtr pScrn, Atom attribute,
425                        INT32 *value, pointer data)
426 {
427         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
428
429         if(attribute == xvSyncToVBlank)
430                 *value = (pPriv->SyncToVBlank) ? 1 : 0;
431         else
432                 return BadMatch;
433
434         return Success;
435 }
436