1 #include "pipe/p_context.h"
2 #include "pipe/p_format.h"
3 #include "util/u_math.h"
4 #include "util/u_memory.h"
6 #include "nouveau/nouveau_winsys.h"
7 #include "nouveau/nouveau_util.h"
8 #include "nouveau/nouveau_screen.h"
9 #include "nv04_surface_2d.h"
12 nv04_surface_format(enum pipe_format format
)
15 case PIPE_FORMAT_A8_UNORM
:
16 case PIPE_FORMAT_L8_UNORM
:
17 case PIPE_FORMAT_I8_UNORM
:
18 return NV04_CONTEXT_SURFACES_2D_FORMAT_Y8
;
19 case PIPE_FORMAT_R16_SNORM
:
20 case PIPE_FORMAT_R5G6B5_UNORM
:
21 case PIPE_FORMAT_Z16_UNORM
:
22 case PIPE_FORMAT_A8L8_UNORM
:
23 return NV04_CONTEXT_SURFACES_2D_FORMAT_R5G6B5
;
24 case PIPE_FORMAT_X8R8G8B8_UNORM
:
25 case PIPE_FORMAT_A8R8G8B8_UNORM
:
26 return NV04_CONTEXT_SURFACES_2D_FORMAT_A8R8G8B8
;
27 case PIPE_FORMAT_Z24S8_UNORM
:
28 case PIPE_FORMAT_Z24X8_UNORM
:
29 return NV04_CONTEXT_SURFACES_2D_FORMAT_Y32
;
36 nv04_rect_format(enum pipe_format format
)
39 case PIPE_FORMAT_A8_UNORM
:
40 return NV04_GDI_RECTANGLE_TEXT_COLOR_FORMAT_A8R8G8B8
;
41 case PIPE_FORMAT_R5G6B5_UNORM
:
42 case PIPE_FORMAT_A8L8_UNORM
:
43 case PIPE_FORMAT_Z16_UNORM
:
44 return NV04_GDI_RECTANGLE_TEXT_COLOR_FORMAT_A16R5G6B5
;
45 case PIPE_FORMAT_A8R8G8B8_UNORM
:
46 case PIPE_FORMAT_Z24S8_UNORM
:
47 case PIPE_FORMAT_Z24X8_UNORM
:
48 return NV04_GDI_RECTANGLE_TEXT_COLOR_FORMAT_A8R8G8B8
;
55 nv04_scaled_image_format(enum pipe_format format
)
58 case PIPE_FORMAT_A8_UNORM
:
59 case PIPE_FORMAT_L8_UNORM
:
60 case PIPE_FORMAT_I8_UNORM
:
61 return NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_Y8
;
62 case PIPE_FORMAT_A1R5G5B5_UNORM
:
63 return NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_A1R5G5B5
;
64 case PIPE_FORMAT_A8R8G8B8_UNORM
:
65 return NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_A8R8G8B8
;
66 case PIPE_FORMAT_X8R8G8B8_UNORM
:
67 return NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_X8R8G8B8
;
68 case PIPE_FORMAT_R5G6B5_UNORM
:
69 case PIPE_FORMAT_R16_SNORM
:
70 case PIPE_FORMAT_A8L8_UNORM
:
71 return NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_R5G6B5
;
77 static INLINE
unsigned
78 nv04_swizzle_bits(unsigned x
, unsigned y
)
80 unsigned u
= (x
& 0x001) << 0 |
93 unsigned v
= (y
& 0x001) << 1 |
109 nv04_surface_copy_swizzle(struct nv04_surface_2d
*ctx
,
110 struct pipe_surface
*dst
, int dx
, int dy
,
111 struct pipe_surface
*src
, int sx
, int sy
,
114 struct nouveau_channel
*chan
= ctx
->swzsurf
->channel
;
115 struct nouveau_grobj
*swzsurf
= ctx
->swzsurf
;
116 struct nouveau_grobj
*sifm
= ctx
->sifm
;
117 struct nouveau_bo
*src_bo
= nouveau_bo(ctx
->buf(src
));
118 struct nouveau_bo
*dst_bo
= nouveau_bo(ctx
->buf(dst
));
119 const unsigned src_pitch
= ((struct nv04_surface
*)src
)->pitch
;
120 /* Max width & height may not be the same on all HW, but must be POT */
121 const unsigned max_w
= 1024;
122 const unsigned max_h
= 1024;
123 unsigned sub_w
= w
> max_w
? max_w
: w
;
124 unsigned sub_h
= h
> max_h
? max_h
: h
;
128 /* Swizzled surfaces must be POT */
129 assert(util_is_pot(dst
->width
) && util_is_pot(dst
->height
));
131 /* If area is too large to copy in one shot we must copy it in POT chunks to meet alignment requirements */
132 assert(sub_w
== w
|| util_is_pot(sub_w
));
133 assert(sub_h
== h
|| util_is_pot(sub_h
));
135 BEGIN_RING(chan
, swzsurf
, NV04_SWIZZLED_SURFACE_DMA_IMAGE
, 1);
136 OUT_RELOCo(chan
, dst_bo
,
137 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
139 BEGIN_RING(chan
, swzsurf
, NV04_SWIZZLED_SURFACE_FORMAT
, 1);
140 OUT_RING (chan
, nv04_surface_format(dst
->format
) |
141 log2i(dst
->width
) << NV04_SWIZZLED_SURFACE_FORMAT_BASE_SIZE_U_SHIFT
|
142 log2i(dst
->height
) << NV04_SWIZZLED_SURFACE_FORMAT_BASE_SIZE_V_SHIFT
);
144 BEGIN_RING(chan
, sifm
, NV04_SCALED_IMAGE_FROM_MEMORY_DMA_IMAGE
, 1);
145 OUT_RELOCo(chan
, src_bo
,
146 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
);
147 BEGIN_RING(chan
, sifm
, NV04_SCALED_IMAGE_FROM_MEMORY_SURFACE
, 1);
148 OUT_RING (chan
, swzsurf
->handle
);
150 for (y
= 0; y
< h
; y
+= sub_h
) {
151 sub_h
= MIN2(sub_h
, h
- y
);
153 for (x
= 0; x
< w
; x
+= sub_w
) {
154 sub_w
= MIN2(sub_w
, w
- x
);
156 /* Must be 64-byte aligned */
157 assert(!((dst
->offset
+ nv04_swizzle_bits(dx
+x
, dy
+y
) * dst
->texture
->block
.size
) & 63));
159 BEGIN_RING(chan
, swzsurf
, NV04_SWIZZLED_SURFACE_OFFSET
, 1);
160 OUT_RELOCl(chan
, dst_bo
, dst
->offset
+ nv04_swizzle_bits(dx
+x
, dy
+y
) * dst
->texture
->block
.size
,
161 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
163 BEGIN_RING(chan
, sifm
, NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_CONVERSION
, 9);
164 OUT_RING (chan
, NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_CONVERSION_TRUNCATE
);
165 OUT_RING (chan
, nv04_scaled_image_format(src
->format
));
166 OUT_RING (chan
, NV04_SCALED_IMAGE_FROM_MEMORY_OPERATION_SRCCOPY
);
168 OUT_RING (chan
, sub_h
<< NV04_SCALED_IMAGE_FROM_MEMORY_CLIP_SIZE_H_SHIFT
| sub_w
);
170 OUT_RING (chan
, sub_h
<< NV04_SCALED_IMAGE_FROM_MEMORY_OUT_SIZE_H_SHIFT
| sub_w
);
171 OUT_RING (chan
, 1 << 20);
172 OUT_RING (chan
, 1 << 20);
174 BEGIN_RING(chan
, sifm
, NV04_SCALED_IMAGE_FROM_MEMORY_SIZE
, 4);
175 OUT_RING (chan
, sub_h
<< NV04_SCALED_IMAGE_FROM_MEMORY_SIZE_H_SHIFT
| sub_w
);
176 OUT_RING (chan
, src_pitch
|
177 NV04_SCALED_IMAGE_FROM_MEMORY_FORMAT_ORIGIN_CENTER
|
178 NV04_SCALED_IMAGE_FROM_MEMORY_FORMAT_FILTER_POINT_SAMPLE
);
179 OUT_RELOCl(chan
, src_bo
, src
->offset
+ (sy
+y
) * src_pitch
+ (sx
+x
) * src
->texture
->block
.size
,
180 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
);
189 nv04_surface_copy_m2mf(struct nv04_surface_2d
*ctx
,
190 struct pipe_surface
*dst
, int dx
, int dy
,
191 struct pipe_surface
*src
, int sx
, int sy
, int w
, int h
)
193 struct nouveau_channel
*chan
= ctx
->m2mf
->channel
;
194 struct nouveau_grobj
*m2mf
= ctx
->m2mf
;
195 struct nouveau_bo
*src_bo
= nouveau_bo(ctx
->buf(src
));
196 struct nouveau_bo
*dst_bo
= nouveau_bo(ctx
->buf(dst
));
197 unsigned src_pitch
= ((struct nv04_surface
*)src
)->pitch
;
198 unsigned dst_pitch
= ((struct nv04_surface
*)dst
)->pitch
;
199 unsigned dst_offset
= dst
->offset
+ dy
* dst_pitch
+
200 dx
* dst
->texture
->block
.size
;
201 unsigned src_offset
= src
->offset
+ sy
* src_pitch
+
202 sx
* src
->texture
->block
.size
;
204 WAIT_RING (chan
, 3 + ((h
/ 2047) + 1) * 9);
205 BEGIN_RING(chan
, m2mf
, NV04_MEMORY_TO_MEMORY_FORMAT_DMA_BUFFER_IN
, 2);
206 OUT_RELOCo(chan
, src_bo
,
207 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
);
208 OUT_RELOCo(chan
, dst_bo
,
209 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
212 int count
= (h
> 2047) ? 2047 : h
;
214 BEGIN_RING(chan
, m2mf
, NV04_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN
, 8);
215 OUT_RELOCl(chan
, src_bo
, src_offset
,
216 NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
);
217 OUT_RELOCl(chan
, dst_bo
, dst_offset
,
218 NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_WR
);
219 OUT_RING (chan
, src_pitch
);
220 OUT_RING (chan
, dst_pitch
);
221 OUT_RING (chan
, w
* src
->texture
->block
.size
);
222 OUT_RING (chan
, count
);
223 OUT_RING (chan
, 0x0101);
227 src_offset
+= src_pitch
* count
;
228 dst_offset
+= dst_pitch
* count
;
235 nv04_surface_copy_blit(struct nv04_surface_2d
*ctx
, struct pipe_surface
*dst
,
236 int dx
, int dy
, struct pipe_surface
*src
, int sx
, int sy
,
239 struct nouveau_channel
*chan
= ctx
->surf2d
->channel
;
240 struct nouveau_grobj
*surf2d
= ctx
->surf2d
;
241 struct nouveau_grobj
*blit
= ctx
->blit
;
242 struct nouveau_bo
*src_bo
= nouveau_bo(ctx
->buf(src
));
243 struct nouveau_bo
*dst_bo
= nouveau_bo(ctx
->buf(dst
));
244 unsigned src_pitch
= ((struct nv04_surface
*)src
)->pitch
;
245 unsigned dst_pitch
= ((struct nv04_surface
*)dst
)->pitch
;
248 format
= nv04_surface_format(dst
->format
);
252 WAIT_RING (chan
, 12);
253 BEGIN_RING(chan
, surf2d
, NV04_CONTEXT_SURFACES_2D_DMA_IMAGE_SOURCE
, 2);
254 OUT_RELOCo(chan
, src_bo
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
);
255 OUT_RELOCo(chan
, dst_bo
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
256 BEGIN_RING(chan
, surf2d
, NV04_CONTEXT_SURFACES_2D_FORMAT
, 4);
257 OUT_RING (chan
, format
);
258 OUT_RING (chan
, (dst_pitch
<< 16) | src_pitch
);
259 OUT_RELOCl(chan
, src_bo
, src
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
);
260 OUT_RELOCl(chan
, dst_bo
, dst
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
262 BEGIN_RING(chan
, blit
, 0x0300, 3);
263 OUT_RING (chan
, (sy
<< 16) | sx
);
264 OUT_RING (chan
, (dy
<< 16) | dx
);
265 OUT_RING (chan
, ( h
<< 16) | w
);
271 nv04_surface_copy(struct nv04_surface_2d
*ctx
, struct pipe_surface
*dst
,
272 int dx
, int dy
, struct pipe_surface
*src
, int sx
, int sy
,
275 unsigned src_pitch
= ((struct nv04_surface
*)src
)->pitch
;
276 unsigned dst_pitch
= ((struct nv04_surface
*)dst
)->pitch
;
277 int src_linear
= src
->texture
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
;
278 int dst_linear
= dst
->texture
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
;
280 assert(src
->format
== dst
->format
);
282 /* Setup transfer to swizzle the texture to vram if needed */
283 if (src_linear
&& !dst_linear
&& w
> 1 && h
> 1) {
284 nv04_surface_copy_swizzle(ctx
, dst
, dx
, dy
, src
, sx
, sy
, w
, h
);
288 /* NV_CONTEXT_SURFACES_2D has buffer alignment restrictions, fallback
289 * to NV_MEMORY_TO_MEMORY_FORMAT in this case.
291 if ((src
->offset
& 63) || (dst
->offset
& 63) ||
292 (src_pitch
& 63) || (dst_pitch
& 63)) {
293 nv04_surface_copy_m2mf(ctx
, dst
, dx
, dy
, src
, sx
, sy
, w
, h
);
297 nv04_surface_copy_blit(ctx
, dst
, dx
, dy
, src
, sx
, sy
, w
, h
);
301 nv04_surface_fill(struct nv04_surface_2d
*ctx
, struct pipe_surface
*dst
,
302 int dx
, int dy
, int w
, int h
, unsigned value
)
304 struct nouveau_channel
*chan
= ctx
->surf2d
->channel
;
305 struct nouveau_grobj
*surf2d
= ctx
->surf2d
;
306 struct nouveau_grobj
*rect
= ctx
->rect
;
307 struct nouveau_bo
*dst_bo
= nouveau_bo(ctx
->buf(dst
));
308 unsigned dst_pitch
= ((struct nv04_surface
*)dst
)->pitch
;
309 int cs2d_format
, gdirect_format
;
311 cs2d_format
= nv04_surface_format(dst
->format
);
312 assert(cs2d_format
>= 0);
314 gdirect_format
= nv04_rect_format(dst
->format
);
315 assert(gdirect_format
>= 0);
317 WAIT_RING (chan
, 16);
318 BEGIN_RING(chan
, surf2d
, NV04_CONTEXT_SURFACES_2D_DMA_IMAGE_SOURCE
, 2);
319 OUT_RELOCo(chan
, dst_bo
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
320 OUT_RELOCo(chan
, dst_bo
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
321 BEGIN_RING(chan
, surf2d
, NV04_CONTEXT_SURFACES_2D_FORMAT
, 4);
322 OUT_RING (chan
, cs2d_format
);
323 OUT_RING (chan
, (dst_pitch
<< 16) | dst_pitch
);
324 OUT_RELOCl(chan
, dst_bo
, dst
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
325 OUT_RELOCl(chan
, dst_bo
, dst
->offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
327 BEGIN_RING(chan
, rect
, NV04_GDI_RECTANGLE_TEXT_COLOR_FORMAT
, 1);
328 OUT_RING (chan
, gdirect_format
);
329 BEGIN_RING(chan
, rect
, NV04_GDI_RECTANGLE_TEXT_COLOR1_A
, 1);
330 OUT_RING (chan
, value
);
331 BEGIN_RING(chan
, rect
,
332 NV04_GDI_RECTANGLE_TEXT_UNCLIPPED_RECTANGLE_POINT(0), 2);
333 OUT_RING (chan
, (dx
<< 16) | dy
);
334 OUT_RING (chan
, ( w
<< 16) | h
);
338 nv04_surface_2d_takedown(struct nv04_surface_2d
**pctx
)
340 struct nv04_surface_2d
*ctx
;
347 nouveau_notifier_free(&ctx
->ntfy
);
348 nouveau_grobj_free(&ctx
->m2mf
);
349 nouveau_grobj_free(&ctx
->surf2d
);
350 nouveau_grobj_free(&ctx
->swzsurf
);
351 nouveau_grobj_free(&ctx
->rect
);
352 nouveau_grobj_free(&ctx
->blit
);
353 nouveau_grobj_free(&ctx
->sifm
);
358 struct nv04_surface_2d
*
359 nv04_surface_2d_init(struct nouveau_screen
*screen
)
361 struct nv04_surface_2d
*ctx
= CALLOC_STRUCT(nv04_surface_2d
);
362 struct nouveau_channel
*chan
= screen
->channel
;
363 unsigned handle
= 0x88000000, class;
369 ret
= nouveau_notifier_alloc(chan
, handle
++, 1, &ctx
->ntfy
);
371 nv04_surface_2d_takedown(&ctx
);
375 ret
= nouveau_grobj_alloc(chan
, handle
++, 0x0039, &ctx
->m2mf
);
377 nv04_surface_2d_takedown(&ctx
);
381 BEGIN_RING(chan
, ctx
->m2mf
, NV04_MEMORY_TO_MEMORY_FORMAT_DMA_NOTIFY
, 1);
382 OUT_RING (chan
, ctx
->ntfy
->handle
);
384 if (chan
->device
->chipset
< 0x10)
385 class = NV04_CONTEXT_SURFACES_2D
;
387 class = NV10_CONTEXT_SURFACES_2D
;
389 ret
= nouveau_grobj_alloc(chan
, handle
++, class, &ctx
->surf2d
);
391 nv04_surface_2d_takedown(&ctx
);
395 BEGIN_RING(chan
, ctx
->surf2d
,
396 NV04_CONTEXT_SURFACES_2D_DMA_IMAGE_SOURCE
, 2);
397 OUT_RING (chan
, chan
->vram
->handle
);
398 OUT_RING (chan
, chan
->vram
->handle
);
400 if (chan
->device
->chipset
< 0x10)
401 class = NV04_IMAGE_BLIT
;
403 class = NV12_IMAGE_BLIT
;
405 ret
= nouveau_grobj_alloc(chan
, handle
++, class, &ctx
->blit
);
407 nv04_surface_2d_takedown(&ctx
);
411 BEGIN_RING(chan
, ctx
->blit
, NV04_IMAGE_BLIT_DMA_NOTIFY
, 1);
412 OUT_RING (chan
, ctx
->ntfy
->handle
);
413 BEGIN_RING(chan
, ctx
->blit
, NV04_IMAGE_BLIT_SURFACE
, 1);
414 OUT_RING (chan
, ctx
->surf2d
->handle
);
415 BEGIN_RING(chan
, ctx
->blit
, NV04_IMAGE_BLIT_OPERATION
, 1);
416 OUT_RING (chan
, NV04_IMAGE_BLIT_OPERATION_SRCCOPY
);
418 ret
= nouveau_grobj_alloc(chan
, handle
++, NV04_GDI_RECTANGLE_TEXT
,
421 nv04_surface_2d_takedown(&ctx
);
425 BEGIN_RING(chan
, ctx
->rect
, NV04_GDI_RECTANGLE_TEXT_DMA_NOTIFY
, 1);
426 OUT_RING (chan
, ctx
->ntfy
->handle
);
427 BEGIN_RING(chan
, ctx
->rect
, NV04_GDI_RECTANGLE_TEXT_SURFACE
, 1);
428 OUT_RING (chan
, ctx
->surf2d
->handle
);
429 BEGIN_RING(chan
, ctx
->rect
, NV04_GDI_RECTANGLE_TEXT_OPERATION
, 1);
430 OUT_RING (chan
, NV04_GDI_RECTANGLE_TEXT_OPERATION_SRCCOPY
);
431 BEGIN_RING(chan
, ctx
->rect
,
432 NV04_GDI_RECTANGLE_TEXT_MONOCHROME_FORMAT
, 1);
433 OUT_RING (chan
, NV04_GDI_RECTANGLE_TEXT_MONOCHROME_FORMAT_LE
);
435 switch (chan
->device
->chipset
& 0xf0) {
438 class = NV04_SWIZZLED_SURFACE
;
441 class = NV20_SWIZZLED_SURFACE
;
444 class = NV30_SWIZZLED_SURFACE
;
448 class = NV40_SWIZZLED_SURFACE
;
451 /* Famous last words: this really can't happen.. */
456 ret
= nouveau_grobj_alloc(chan
, handle
++, class, &ctx
->swzsurf
);
458 nv04_surface_2d_takedown(&ctx
);
462 switch (chan
->device
->chipset
& 0xf0) {
465 class = NV10_SCALED_IMAGE_FROM_MEMORY
;
468 class = NV30_SCALED_IMAGE_FROM_MEMORY
;
472 class = NV40_SCALED_IMAGE_FROM_MEMORY
;
475 class = NV04_SCALED_IMAGE_FROM_MEMORY
;
479 ret
= nouveau_grobj_alloc(chan
, handle
++, class, &ctx
->sifm
);
481 nv04_surface_2d_takedown(&ctx
);
485 ctx
->copy
= nv04_surface_copy
;
486 ctx
->fill
= nv04_surface_fill
;