svga: Add svga driver
[mesa.git] / src / gallium / drivers / svga / svga_screen_texture.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "pipe/p_inlines.h"
31 #include "pipe/p_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34
35 #include "svga_screen.h"
36 #include "svga_context.h"
37 #include "svga_screen_texture.h"
38 #include "svga_screen_buffer.h"
39 #include "svga_winsys.h"
40 #include "svga_debug.h"
41 #include "svga_screen_buffer.h"
42
43 #include <util/u_string.h>
44
45
46 /* XXX: This isn't a real hardware flag, but just a hack for kernel to
47 * know about primary surfaces. Find a better way to accomplish this.
48 */
49 #define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)
50
51
52 /*
53 * Helper function and arrays
54 */
55
56 SVGA3dSurfaceFormat
57 svga_translate_format(enum pipe_format format)
58 {
59 switch(format) {
60
61 case PIPE_FORMAT_A8R8G8B8_UNORM:
62 return SVGA3D_A8R8G8B8;
63 case PIPE_FORMAT_X8R8G8B8_UNORM:
64 return SVGA3D_X8R8G8B8;
65
66 /* Required for GL2.1:
67 */
68 case PIPE_FORMAT_A8R8G8B8_SRGB:
69 return SVGA3D_A8R8G8B8;
70
71 case PIPE_FORMAT_R5G6B5_UNORM:
72 return SVGA3D_R5G6B5;
73 case PIPE_FORMAT_A1R5G5B5_UNORM:
74 return SVGA3D_A1R5G5B5;
75 case PIPE_FORMAT_A4R4G4B4_UNORM:
76 return SVGA3D_A4R4G4B4;
77
78
79 /* XXX: Doesn't seem to work properly.
80 case PIPE_FORMAT_Z32_UNORM:
81 return SVGA3D_Z_D32;
82 */
83 case PIPE_FORMAT_Z16_UNORM:
84 return SVGA3D_Z_D16;
85 case PIPE_FORMAT_Z24S8_UNORM:
86 return SVGA3D_Z_D24S8;
87 case PIPE_FORMAT_Z24X8_UNORM:
88 return SVGA3D_Z_D24X8;
89
90 case PIPE_FORMAT_A8_UNORM:
91 return SVGA3D_ALPHA8;
92 case PIPE_FORMAT_L8_UNORM:
93 return SVGA3D_LUMINANCE8;
94
95 case PIPE_FORMAT_DXT1_RGB:
96 case PIPE_FORMAT_DXT1_RGBA:
97 return SVGA3D_DXT1;
98 case PIPE_FORMAT_DXT3_RGBA:
99 return SVGA3D_DXT3;
100 case PIPE_FORMAT_DXT5_RGBA:
101 return SVGA3D_DXT5;
102
103 default:
104 return SVGA3D_FORMAT_INVALID;
105 }
106 }
107
108
109 SVGA3dSurfaceFormat
110 svga_translate_format_render(enum pipe_format format)
111 {
112 switch(format) {
113 case PIPE_FORMAT_A8R8G8B8_UNORM:
114 case PIPE_FORMAT_X8R8G8B8_UNORM:
115 case PIPE_FORMAT_A1R5G5B5_UNORM:
116 case PIPE_FORMAT_A4R4G4B4_UNORM:
117 case PIPE_FORMAT_R5G6B5_UNORM:
118 case PIPE_FORMAT_Z24S8_UNORM:
119 case PIPE_FORMAT_Z24X8_UNORM:
120 case PIPE_FORMAT_Z32_UNORM:
121 case PIPE_FORMAT_Z16_UNORM:
122 case PIPE_FORMAT_L8_UNORM:
123 return svga_translate_format(format);
124
125 #if 1
126 /* For on host conversion */
127 case PIPE_FORMAT_DXT1_RGB:
128 return SVGA3D_X8R8G8B8;
129 case PIPE_FORMAT_DXT1_RGBA:
130 case PIPE_FORMAT_DXT3_RGBA:
131 case PIPE_FORMAT_DXT5_RGBA:
132 return SVGA3D_A8R8G8B8;
133 #endif
134
135 default:
136 return SVGA3D_FORMAT_INVALID;
137 }
138 }
139
140
141 static INLINE void
142 svga_transfer_dma_band(struct svga_transfer *st,
143 SVGA3dTransferType transfer,
144 unsigned y, unsigned h, unsigned srcy)
145 {
146 struct svga_texture *texture = svga_texture(st->base.texture);
147 struct svga_screen *screen = svga_screen(texture->base.screen);
148 SVGA3dCopyBox box;
149 enum pipe_error ret;
150
151 SVGA_DBG(DEBUG_DMA, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
152 transfer == SVGA3D_WRITE_HOST_VRAM ? "to" : "from",
153 texture->handle,
154 st->base.face,
155 st->base.x,
156 y,
157 st->base.zslice,
158 st->base.x + st->base.width,
159 y + h,
160 st->base.zslice + 1,
161 texture->base.block.size*8/(texture->base.block.width*texture->base.block.height));
162
163 box.x = st->base.x;
164 box.y = y;
165 box.z = st->base.zslice;
166 box.w = st->base.width;
167 box.h = h;
168 box.d = 1;
169 box.srcx = 0;
170 box.srcy = srcy;
171 box.srcz = 0;
172
173 pipe_mutex_lock(screen->swc_mutex);
174 ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
175 if(ret != PIPE_OK) {
176 screen->swc->flush(screen->swc, NULL);
177 ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
178 assert(ret == PIPE_OK);
179 }
180 pipe_mutex_unlock(screen->swc_mutex);
181 }
182
183
184 static INLINE void
185 svga_transfer_dma(struct svga_transfer *st,
186 SVGA3dTransferType transfer)
187 {
188 struct svga_texture *texture = svga_texture(st->base.texture);
189 struct svga_screen *screen = svga_screen(texture->base.screen);
190 struct svga_winsys_screen *sws = screen->sws;
191 struct pipe_fence_handle *fence = NULL;
192
193 if (transfer == SVGA3D_READ_HOST_VRAM) {
194 SVGA_DBG(DEBUG_PERF, "%s: readback transfer\n", __FUNCTION__);
195 }
196
197
198 if(!st->swbuf) {
199 /* Do the DMA transfer in a single go */
200
201 svga_transfer_dma_band(st, transfer, st->base.y, st->base.height, 0);
202
203 if(transfer == SVGA3D_READ_HOST_VRAM) {
204 svga_screen_flush(screen, &fence);
205 sws->fence_finish(sws, fence, 0);
206 //sws->fence_reference(sws, &fence, NULL);
207 }
208 }
209 else {
210 unsigned y, h, srcy;
211 h = st->hw_nblocksy * st->base.block.height;
212 srcy = 0;
213 for(y = 0; y < st->base.height; y += h) {
214 unsigned offset, length;
215 void *hw, *sw;
216
217 if (y + h > st->base.height)
218 h = st->base.height - y;
219
220 /* Transfer band must be aligned to pixel block boundaries */
221 assert(y % st->base.block.height == 0);
222 assert(h % st->base.block.height == 0);
223
224 offset = y * st->base.stride / st->base.block.height;
225 length = h * st->base.stride / st->base.block.height;
226
227 sw = (uint8_t *)st->swbuf + offset;
228
229 if(transfer == SVGA3D_WRITE_HOST_VRAM) {
230 /* Wait for the previous DMAs to complete */
231 /* TODO: keep one DMA (at half the size) in the background */
232 if(y) {
233 svga_screen_flush(screen, &fence);
234 sws->fence_finish(sws, fence, 0);
235 //sws->fence_reference(sws, &fence, NULL);
236 }
237
238 hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
239 assert(hw);
240 if(hw) {
241 memcpy(hw, sw, length);
242 sws->buffer_unmap(sws, st->hwbuf);
243 }
244 }
245
246 svga_transfer_dma_band(st, transfer, y, h, srcy);
247
248 if(transfer == SVGA3D_READ_HOST_VRAM) {
249 svga_screen_flush(screen, &fence);
250 sws->fence_finish(sws, fence, 0);
251
252 hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_READ);
253 assert(hw);
254 if(hw) {
255 memcpy(sw, hw, length);
256 sws->buffer_unmap(sws, st->hwbuf);
257 }
258 }
259 }
260 }
261 }
262
263
264 static struct pipe_texture *
265 svga_texture_create(struct pipe_screen *screen,
266 const struct pipe_texture *templat)
267 {
268 struct svga_screen *svgascreen = svga_screen(screen);
269 struct svga_winsys_screen *sws = svgascreen->sws;
270 struct svga_texture *tex = CALLOC_STRUCT(svga_texture);
271 unsigned width, height, depth;
272 SVGA3dSurfaceFlags flags = 0;
273 SVGA3dSurfaceFormat format;
274 SVGA3dSize size;
275 uint32 numFaces;
276 uint32 numMipLevels;
277 unsigned level;
278
279 if (!tex)
280 goto error1;
281
282 tex->base = *templat;
283 pipe_reference_init(&tex->base.reference, 1);
284 tex->base.screen = screen;
285
286 assert(templat->last_level < SVGA_MAX_TEXTURE_LEVELS);
287 if(templat->last_level >= SVGA_MAX_TEXTURE_LEVELS)
288 goto error2;
289
290 width = templat->width[0];
291 height = templat->height[0];
292 depth = templat->depth[0];
293 for(level = 0; level <= templat->last_level; ++level) {
294 tex->base.width[level] = width;
295 tex->base.height[level] = height;
296 tex->base.depth[level] = depth;
297 tex->base.nblocksx[level] = pf_get_nblocksx(&tex->base.block, width);
298 tex->base.nblocksy[level] = pf_get_nblocksy(&tex->base.block, height);
299 width = minify(width);
300 height = minify(height);
301 depth = minify(depth);
302 }
303
304 size.width = templat->width[0];
305 size.height = templat->height[0];
306 size.depth = templat->depth[0];
307
308 if(templat->target == PIPE_TEXTURE_CUBE) {
309 flags |= SVGA3D_SURFACE_CUBEMAP;
310 numFaces = 6;
311 }
312 else {
313 numFaces = 1;
314 }
315
316 if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
317 flags |= SVGA3D_SURFACE_HINT_TEXTURE;
318
319 if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
320 flags |= SVGA3D_SURFACE_HINT_SCANOUT;
321
322 /*
323 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
324 * know beforehand whether a texture will be used as a rendertarget or not
325 * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
326 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
327 */
328 #if 0
329 if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
330 !pf_is_compressed(templat->format))
331 flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
332 #endif
333
334 if(templat->tex_usage & PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
335 flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
336
337 numMipLevels = templat->last_level + 1;
338
339 format = svga_translate_format(templat->format);
340 if(format == SVGA3D_FORMAT_INVALID)
341 goto error2;
342
343 tex->handle = sws->surface_create(sws, flags, format, size, numFaces, numMipLevels);
344 if (tex->handle)
345 SVGA_DBG(DEBUG_DMA, "create sid %p (texture)\n", tex->handle);
346
347 return &tex->base;
348
349 error2:
350 FREE(tex);
351 error1:
352 return NULL;
353 }
354
355
356 static struct pipe_texture *
357 svga_texture_blanket(struct pipe_screen * screen,
358 const struct pipe_texture *base,
359 const unsigned *stride,
360 struct pipe_buffer *buffer)
361 {
362 struct svga_texture *tex;
363 struct svga_buffer *sbuf = svga_buffer(buffer);
364 struct svga_winsys_screen *sws = svga_winsys_screen(screen);
365 assert(screen);
366
367 /* Only supports one type */
368 if (base->target != PIPE_TEXTURE_2D ||
369 base->last_level != 0 ||
370 base->depth[0] != 1) {
371 return NULL;
372 }
373
374 /**
375 * We currently can't do texture blanket on
376 * SVGA3D_BUFFER. Need to blit to a temporary surface?
377 */
378
379 assert(sbuf->handle);
380 if (!sbuf->handle)
381 return NULL;
382
383 if (svga_translate_format(base->format) != sbuf->key.format) {
384 unsigned f1 = svga_translate_format(base->format);
385 unsigned f2 = sbuf->key.format;
386
387 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
388 if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
389 (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
390 (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ) ) {
391 debug_printf("%s wrong format %u != %u\n", __FUNCTION__, f1, f2);
392 return NULL;
393 }
394 }
395
396 tex = CALLOC_STRUCT(svga_texture);
397 if (!tex)
398 return NULL;
399
400 tex->base = *base;
401
402 if (sbuf->key.format == 1)
403 tex->base.format = PIPE_FORMAT_X8R8G8B8_UNORM;
404 else if (sbuf->key.format == 2)
405 tex->base.format = PIPE_FORMAT_A8R8G8B8_UNORM;
406
407 pipe_reference_init(&tex->base.reference, 1);
408 tex->base.screen = screen;
409
410 sws->surface_reference(sws, &tex->handle, sbuf->handle);
411
412 return &tex->base;
413 }
414
415
416 static void
417 svga_texture_destroy(struct pipe_texture *pt)
418 {
419 struct svga_screen *ss = svga_screen(pt->screen);
420 struct svga_texture *tex = (struct svga_texture *)pt;
421
422 ss->texture_timestamp++;
423
424 svga_sampler_view_reference(&tex->cached_view, NULL);
425
426 /*
427 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
428 */
429 SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
430 ss->sws->surface_reference(ss->sws, &tex->handle, NULL);
431
432 FREE(tex);
433 }
434
435
436 static void
437 svga_texture_copy_handle(struct svga_context *svga,
438 struct svga_screen *ss,
439 struct svga_winsys_surface *src_handle,
440 unsigned src_x, unsigned src_y, unsigned src_z,
441 unsigned src_level, unsigned src_face,
442 struct svga_winsys_surface *dst_handle,
443 unsigned dst_x, unsigned dst_y, unsigned dst_z,
444 unsigned dst_level, unsigned dst_face,
445 unsigned width, unsigned height, unsigned depth)
446 {
447 struct svga_surface dst, src;
448 enum pipe_error ret;
449 SVGA3dCopyBox box, *boxes;
450
451 assert(svga || ss);
452
453 src.handle = src_handle;
454 src.real_level = src_level;
455 src.real_face = src_face;
456 src.real_zslice = 0;
457
458 dst.handle = dst_handle;
459 dst.real_level = dst_level;
460 dst.real_face = dst_face;
461 dst.real_zslice = 0;
462
463 box.x = dst_x;
464 box.y = dst_y;
465 box.z = dst_z;
466 box.w = width;
467 box.h = height;
468 box.d = depth;
469 box.srcx = src_x;
470 box.srcy = src_y;
471 box.srcz = src_z;
472
473 /*
474 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
475 src_handle, src_level, src_x, src_y, src_z,
476 dst_handle, dst_level, dst_x, dst_y, dst_z);
477 */
478
479 if (svga) {
480 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
481 &src.base,
482 &dst.base,
483 &boxes, 1);
484 if(ret != PIPE_OK) {
485 svga_context_flush(svga, NULL);
486 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
487 &src.base,
488 &dst.base,
489 &boxes, 1);
490 assert(ret == PIPE_OK);
491 }
492 *boxes = box;
493 SVGA_FIFOCommitAll(svga->swc);
494 } else {
495 pipe_mutex_lock(ss->swc_mutex);
496 ret = SVGA3D_BeginSurfaceCopy(ss->swc,
497 &src.base,
498 &dst.base,
499 &boxes, 1);
500 if(ret != PIPE_OK) {
501 ss->swc->flush(ss->swc, NULL);
502 ret = SVGA3D_BeginSurfaceCopy(ss->swc,
503 &src.base,
504 &dst.base,
505 &boxes, 1);
506 assert(ret == PIPE_OK);
507 }
508 *boxes = box;
509 SVGA_FIFOCommitAll(ss->swc);
510 pipe_mutex_unlock(ss->swc_mutex);
511 }
512 }
513
514 static struct svga_winsys_surface *
515 svga_texture_view_surface(struct pipe_context *pipe,
516 struct svga_texture *tex,
517 SVGA3dSurfaceFormat format,
518 unsigned start_mip,
519 unsigned num_mip,
520 int face_pick,
521 int zslice_pick)
522 {
523 struct svga_screen *ss = svga_screen(tex->base.screen);
524 struct svga_winsys_screen *sws = ss->sws;
525 struct svga_winsys_surface *handle;
526 int i, j;
527 SVGA3dSurfaceFlags flags = 0;
528 SVGA3dSize size;
529 uint32 numFaces;
530 uint32 numMipLevels = num_mip;
531 unsigned z_offset = 0;
532
533 SVGA_DBG(DEBUG_PERF,
534 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
535 face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
536
537 size.width = tex->base.width[start_mip];
538 size.height = tex->base.height[start_mip];
539 size.depth = zslice_pick < 0 ? tex->base.depth[start_mip] : 1;
540 assert(size.depth == 1);
541
542 if(tex->base.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
543 flags |= SVGA3D_SURFACE_CUBEMAP;
544 numFaces = 6;
545 } else {
546 numFaces = 1;
547 }
548
549 if(format == SVGA3D_FORMAT_INVALID)
550 return NULL;
551
552 handle = sws->surface_create(sws, flags, format, size, numFaces, numMipLevels);
553
554 if (!handle)
555 return NULL;
556
557 SVGA_DBG(DEBUG_DMA, "create sid %p (texture view)\n", handle);
558
559 if (face_pick < 0)
560 face_pick = 0;
561
562 if (zslice_pick >= 0)
563 z_offset = zslice_pick;
564
565 for (i = 0; i < num_mip; i++) {
566 for (j = 0; j < numFaces; j++) {
567 if(tex->defined[j + face_pick][i + start_mip]) {
568 unsigned depth = zslice_pick < 0 ? tex->base.depth[i + start_mip] : 1;
569 svga_texture_copy_handle(svga_context(pipe), ss,
570 tex->handle, 0, 0, z_offset, i + start_mip, j + face_pick,
571 handle, 0, 0, 0, i, j,
572 tex->base.width[i + start_mip], tex->base.height[i + start_mip], depth);
573 }
574 }
575 }
576
577 return handle;
578 }
579
580
581 static struct pipe_surface *
582 svga_get_tex_surface(struct pipe_screen *screen,
583 struct pipe_texture *pt,
584 unsigned face, unsigned level, unsigned zslice,
585 unsigned flags)
586 {
587 struct svga_texture *tex = svga_texture(pt);
588 struct svga_surface *s;
589 struct pipe_surface *ps;
590 boolean render = flags & PIPE_BUFFER_USAGE_GPU_WRITE ? TRUE : FALSE;
591 boolean view = FALSE;
592 SVGA3dSurfaceFormat format;
593
594 s = CALLOC_STRUCT(svga_surface);
595 ps = &s->base;
596 if (!ps)
597 return NULL;
598
599 pipe_reference_init(&ps->reference, 1);
600 pipe_texture_reference(&ps->texture, pt);
601 ps->format = pt->format;
602 ps->width = pt->width[level];
603 ps->height = pt->height[level];
604 ps->usage = flags;
605 ps->level = level;
606 ps->face = face;
607 ps->zslice = zslice;
608
609 if (!render)
610 format = svga_translate_format(pt->format);
611 else
612 format = svga_translate_format_render(pt->format);
613
614 assert(format != SVGA3D_FORMAT_INVALID);
615 assert(!(flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE));
616
617
618 if (svga_screen(screen)->debug.force_surface_view)
619 view = TRUE;
620
621 /* Currently only used for compressed textures */
622 if (render && (format != svga_translate_format(pt->format))) {
623 view = TRUE;
624 }
625
626 if (level != 0 && svga_screen(screen)->debug.force_level_surface_view)
627 view = TRUE;
628
629 if (pt->target == PIPE_TEXTURE_3D)
630 view = TRUE;
631
632 if (svga_screen(screen)->debug.no_surface_view)
633 view = FALSE;
634
635 if (view) {
636 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
637 pt, level, face, zslice, ps);
638
639 s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice);
640 s->real_face = 0;
641 s->real_level = 0;
642 s->real_zslice = 0;
643 } else {
644 struct svga_winsys_screen *sws = svga_winsys_screen(screen);
645
646 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
647 pt, level, face, zslice, ps);
648
649 sws->surface_reference(sws, &s->handle, tex->handle);
650 s->real_face = face;
651 s->real_level = level;
652 s->real_zslice = zslice;
653 }
654
655 return ps;
656 }
657
658
659 static void
660 svga_tex_surface_destroy(struct pipe_surface *surf)
661 {
662 struct svga_surface *s = svga_surface(surf);
663 struct svga_screen *ss = svga_screen(surf->texture->screen);
664
665 SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
666 ss->sws->surface_reference(ss->sws, &s->handle, NULL);
667 pipe_texture_reference(&surf->texture, NULL);
668 FREE(surf);
669 }
670
671
672 static INLINE void
673 svga_mark_surface_dirty(struct pipe_surface *surf)
674 {
675 struct svga_surface *s = svga_surface(surf);
676
677 if(!s->dirty) {
678 struct svga_texture *tex = svga_texture(surf->texture);
679
680 s->dirty = TRUE;
681
682 if (s->handle == tex->handle)
683 tex->defined[surf->face][surf->level] = TRUE;
684 else {
685 /* this will happen later in svga_propagate_surface */
686 }
687 }
688 }
689
690
691 void svga_mark_surfaces_dirty(struct svga_context *svga)
692 {
693 unsigned i;
694
695 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
696 if (svga->curr.framebuffer.cbufs[i])
697 svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
698 }
699 if (svga->curr.framebuffer.zsbuf)
700 svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
701 }
702
703 /**
704 * Progagate any changes from surfaces to texture.
705 * pipe is optional context to inline the blit command in.
706 */
707 void
708 svga_propagate_surface(struct pipe_context *pipe, struct pipe_surface *surf)
709 {
710 struct svga_surface *s = svga_surface(surf);
711 struct svga_texture *tex = svga_texture(surf->texture);
712 struct svga_screen *ss = svga_screen(surf->texture->screen);
713
714 if (!s->dirty)
715 return;
716
717 s->dirty = FALSE;
718 ss->texture_timestamp++;
719 tex->view_age[surf->level] = ++(tex->age);
720
721 if (s->handle != tex->handle) {
722 SVGA_DBG(DEBUG_VIEWS, "svga: Surface propagate: tex %p, level %u, from %p\n", tex, surf->level, surf);
723 svga_texture_copy_handle(svga_context(pipe), ss,
724 s->handle, 0, 0, 0, s->real_level, s->real_face,
725 tex->handle, 0, 0, surf->zslice, surf->level, surf->face,
726 tex->base.width[surf->level], tex->base.height[surf->level], 1);
727 tex->defined[surf->face][surf->level] = TRUE;
728 }
729 }
730
731 /**
732 * Check if we should call svga_propagate_surface on the surface.
733 */
734 extern boolean
735 svga_surface_needs_propagation(struct pipe_surface *surf)
736 {
737 struct svga_surface *s = svga_surface(surf);
738 struct svga_texture *tex = svga_texture(surf->texture);
739
740 return s->dirty && s->handle != tex->handle;
741 }
742
743
744 static struct pipe_transfer *
745 svga_get_tex_transfer(struct pipe_screen *screen,
746 struct pipe_texture *texture,
747 unsigned face, unsigned level, unsigned zslice,
748 enum pipe_transfer_usage usage, unsigned x, unsigned y,
749 unsigned w, unsigned h)
750 {
751 struct svga_screen *ss = svga_screen(screen);
752 struct svga_winsys_screen *sws = ss->sws;
753 struct svga_transfer *st;
754
755 /* We can't map texture storage directly */
756 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
757 return NULL;
758
759 st = CALLOC_STRUCT(svga_transfer);
760 if (!st)
761 return NULL;
762
763 st->base.format = texture->format;
764 st->base.block = texture->block;
765 st->base.x = x;
766 st->base.y = y;
767 st->base.width = w;
768 st->base.height = h;
769 st->base.nblocksx = pf_get_nblocksx(&texture->block, w);
770 st->base.nblocksy = pf_get_nblocksy(&texture->block, h);
771 st->base.stride = st->base.nblocksx*st->base.block.size;
772 st->base.usage = usage;
773 st->base.face = face;
774 st->base.level = level;
775 st->base.zslice = zslice;
776
777 st->hw_nblocksy = st->base.nblocksy;
778
779 st->hwbuf = svga_winsys_buffer_create(ss,
780 1,
781 0,
782 st->hw_nblocksy*st->base.stride);
783 while(!st->hwbuf && (st->hw_nblocksy /= 2)) {
784 st->hwbuf = svga_winsys_buffer_create(ss,
785 1,
786 0,
787 st->hw_nblocksy*st->base.stride);
788 }
789
790 if(!st->hwbuf)
791 goto no_hwbuf;
792
793 if(st->hw_nblocksy < st->base.nblocksy) {
794 /* We couldn't allocate a hardware buffer big enough for the transfer,
795 * so allocate regular malloc memory instead */
796 debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
797 __FUNCTION__,
798 (st->base.nblocksy*st->base.stride + 1023)/1024,
799 (st->base.nblocksy + st->hw_nblocksy - 1)/st->hw_nblocksy,
800 (st->hw_nblocksy*st->base.stride + 1023)/1024);
801 st->swbuf = MALLOC(st->base.nblocksy*st->base.stride);
802 if(!st->swbuf)
803 goto no_swbuf;
804 }
805
806 pipe_texture_reference(&st->base.texture, texture);
807
808 if (usage & PIPE_TRANSFER_READ)
809 svga_transfer_dma(st, SVGA3D_READ_HOST_VRAM);
810
811 return &st->base;
812
813 no_swbuf:
814 sws->buffer_destroy(sws, st->hwbuf);
815 no_hwbuf:
816 FREE(st);
817 return NULL;
818 }
819
820
821 static void *
822 svga_transfer_map( struct pipe_screen *screen,
823 struct pipe_transfer *transfer )
824 {
825 struct svga_screen *ss = svga_screen(screen);
826 struct svga_winsys_screen *sws = ss->sws;
827 struct svga_transfer *st = svga_transfer(transfer);
828
829 if(st->swbuf)
830 return st->swbuf;
831 else
832 /* The wait for read transfers already happened when svga_transfer_dma
833 * was called. */
834 return sws->buffer_map(sws, st->hwbuf,
835 pipe_transfer_buffer_flags(transfer));
836 }
837
838
839 static void
840 svga_transfer_unmap(struct pipe_screen *screen,
841 struct pipe_transfer *transfer)
842 {
843 struct svga_screen *ss = svga_screen(screen);
844 struct svga_winsys_screen *sws = ss->sws;
845 struct svga_transfer *st = svga_transfer(transfer);
846
847 if(!st->swbuf)
848 sws->buffer_unmap(sws, st->hwbuf);
849 }
850
851
852 static void
853 svga_tex_transfer_destroy(struct pipe_transfer *transfer)
854 {
855 struct svga_texture *tex = svga_texture(transfer->texture);
856 struct svga_screen *ss = svga_screen(transfer->texture->screen);
857 struct svga_winsys_screen *sws = ss->sws;
858 struct svga_transfer *st = svga_transfer(transfer);
859
860 if (st->base.usage & PIPE_TRANSFER_WRITE) {
861 svga_transfer_dma(st, SVGA3D_WRITE_HOST_VRAM);
862 ss->texture_timestamp++;
863 tex->view_age[transfer->level] = ++(tex->age);
864 tex->defined[transfer->face][transfer->level] = TRUE;
865 }
866
867 pipe_texture_reference(&st->base.texture, NULL);
868 FREE(st->swbuf);
869 sws->buffer_destroy(sws, st->hwbuf);
870 FREE(st);
871 }
872
873 void
874 svga_screen_init_texture_functions(struct pipe_screen *screen)
875 {
876 screen->texture_create = svga_texture_create;
877 screen->texture_destroy = svga_texture_destroy;
878 screen->get_tex_surface = svga_get_tex_surface;
879 screen->tex_surface_destroy = svga_tex_surface_destroy;
880 screen->texture_blanket = svga_texture_blanket;
881 screen->get_tex_transfer = svga_get_tex_transfer;
882 screen->transfer_map = svga_transfer_map;
883 screen->transfer_unmap = svga_transfer_unmap;
884 screen->tex_transfer_destroy = svga_tex_transfer_destroy;
885 }
886
887 /***********************************************************************
888 */
889
890 struct svga_sampler_view *
891 svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
892 unsigned min_lod, unsigned max_lod)
893 {
894 struct svga_screen *ss = svga_screen(pt->screen);
895 struct svga_winsys_screen *sws = ss->sws;
896 struct svga_texture *tex = svga_texture(pt);
897 struct svga_sampler_view *sv = NULL;
898 SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
899 boolean view = TRUE;
900
901 assert(pt);
902 assert(min_lod >= 0);
903 assert(min_lod <= max_lod);
904 assert(max_lod <= pt->last_level);
905
906
907 /* Is a view needed */
908 {
909 /*
910 * Can't control max lod. For first level views and when we only
911 * look at one level we disable mip filtering to achive the same
912 * results as a view.
913 */
914 if (min_lod == 0 && max_lod >= pt->last_level)
915 view = FALSE;
916
917 if (pf_is_compressed(pt->format) && view) {
918 format = svga_translate_format_render(pt->format);
919 }
920
921 if (ss->debug.no_sampler_view)
922 view = FALSE;
923
924 if (ss->debug.force_sampler_view)
925 view = TRUE;
926 }
927
928 /* First try the cache */
929 if (view) {
930 pipe_mutex_lock(ss->tex_mutex);
931 if (tex->cached_view &&
932 tex->cached_view->min_lod == min_lod &&
933 tex->cached_view->max_lod == max_lod) {
934 svga_sampler_view_reference(&sv, tex->cached_view);
935 pipe_mutex_unlock(ss->tex_mutex);
936 SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
937 pt, min_lod, max_lod, pt->last_level);
938 svga_validate_sampler_view(svga_context(pipe), sv);
939 return sv;
940 }
941 pipe_mutex_unlock(ss->tex_mutex);
942 }
943
944 sv = CALLOC_STRUCT(svga_sampler_view);
945 pipe_reference_init(&sv->reference, 1);
946 sv->texture = tex;
947 sv->min_lod = min_lod;
948 sv->max_lod = max_lod;
949
950 /* No view needed just use the whole texture */
951 if (!view) {
952 SVGA_DBG(DEBUG_VIEWS,
953 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
954 pt, min_lod, max_lod,
955 max_lod - min_lod + 1,
956 pt->width[0],
957 pt->height[0],
958 pt->depth[0],
959 pt->last_level);
960 sws->surface_reference(sws, &sv->handle, tex->handle);
961 return sv;
962 }
963
964 SVGA_DBG(DEBUG_VIEWS,
965 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
966 pt, min_lod, max_lod,
967 max_lod - min_lod + 1,
968 pt->width[0],
969 pt->height[0],
970 pt->depth[0],
971 pt->last_level);
972
973 sv->age = tex->age;
974 sv->handle = svga_texture_view_surface(pipe, tex, format,
975 min_lod,
976 max_lod - min_lod + 1,
977 -1, -1);
978
979 if (!sv->handle) {
980 assert(0);
981 sws->surface_reference(sws, &sv->handle, tex->handle);
982 return sv;
983 }
984
985 pipe_mutex_lock(ss->tex_mutex);
986 svga_sampler_view_reference(&tex->cached_view, sv);
987 pipe_mutex_unlock(ss->tex_mutex);
988
989 return sv;
990 }
991
992 void
993 svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
994 {
995 struct svga_texture *tex = v->texture;
996 unsigned numFaces;
997 unsigned age = 0;
998 int i, k;
999
1000 assert(svga);
1001
1002 if (v->handle == v->texture->handle)
1003 return;
1004
1005 age = tex->age;
1006
1007 if(tex->base.target == PIPE_TEXTURE_CUBE)
1008 numFaces = 6;
1009 else
1010 numFaces = 1;
1011
1012 for (i = v->min_lod; i <= v->max_lod; i++) {
1013 for (k = 0; k < numFaces; k++) {
1014 if (v->age < tex->view_age[i])
1015 svga_texture_copy_handle(svga, NULL,
1016 tex->handle, 0, 0, 0, i, k,
1017 v->handle, 0, 0, 0, i - v->min_lod, k,
1018 tex->base.width[i],
1019 tex->base.height[i],
1020 tex->base.depth[i]);
1021 }
1022 }
1023
1024 v->age = age;
1025 }
1026
1027 void
1028 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
1029 {
1030 struct svga_screen *ss = svga_screen(v->texture->base.screen);
1031
1032 SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
1033 ss->sws->surface_reference(ss->sws, &v->handle, NULL);
1034
1035 FREE(v);
1036 }
1037
1038 boolean
1039 svga_screen_buffer_from_texture(struct pipe_texture *texture,
1040 struct pipe_buffer **buffer,
1041 unsigned *stride)
1042 {
1043 struct svga_texture *stex = svga_texture(texture);
1044
1045 *buffer = svga_screen_buffer_wrap_surface
1046 (texture->screen,
1047 svga_translate_format(texture->format),
1048 stex->handle);
1049
1050 *stride = pf_get_nblocksx(&texture->block, texture->width[0]) *
1051 texture->block.size;
1052
1053 return *buffer != NULL;
1054 }
1055
1056
1057 struct svga_winsys_surface *
1058 svga_screen_texture_get_winsys_surface(struct pipe_texture *texture)
1059 {
1060 struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
1061 struct svga_winsys_surface *vsurf = NULL;
1062
1063 sws->surface_reference(sws, &vsurf, svga_texture(texture)->handle);
1064 return vsurf;
1065 }