Merge branch 'mesa_7_7_branch'
[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 pf_get_blocksize(texture->base.format)*8/
162 (pf_get_blockwidth(texture->base.format)*pf_get_blockheight(texture->base.format)));
163
164 box.x = st->base.x;
165 box.y = y;
166 box.z = st->base.zslice;
167 box.w = st->base.width;
168 box.h = h;
169 box.d = 1;
170 box.srcx = 0;
171 box.srcy = srcy;
172 box.srcz = 0;
173
174 pipe_mutex_lock(screen->swc_mutex);
175 ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
176 if(ret != PIPE_OK) {
177 screen->swc->flush(screen->swc, NULL);
178 ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
179 assert(ret == PIPE_OK);
180 }
181 pipe_mutex_unlock(screen->swc_mutex);
182 }
183
184
185 static INLINE void
186 svga_transfer_dma(struct svga_transfer *st,
187 SVGA3dTransferType transfer)
188 {
189 struct svga_texture *texture = svga_texture(st->base.texture);
190 struct svga_screen *screen = svga_screen(texture->base.screen);
191 struct svga_winsys_screen *sws = screen->sws;
192 struct pipe_fence_handle *fence = NULL;
193
194 if (transfer == SVGA3D_READ_HOST_VRAM) {
195 SVGA_DBG(DEBUG_PERF, "%s: readback transfer\n", __FUNCTION__);
196 }
197
198
199 if(!st->swbuf) {
200 /* Do the DMA transfer in a single go */
201
202 svga_transfer_dma_band(st, transfer, st->base.y, st->base.height, 0);
203
204 if(transfer == SVGA3D_READ_HOST_VRAM) {
205 svga_screen_flush(screen, &fence);
206 sws->fence_finish(sws, fence, 0);
207 //sws->fence_reference(sws, &fence, NULL);
208 }
209 }
210 else {
211 unsigned y, h, srcy;
212 unsigned blockheight = pf_get_blockheight(st->base.texture->format);
213 h = st->hw_nblocksy * blockheight;
214 srcy = 0;
215 for(y = 0; y < st->base.height; y += h) {
216 unsigned offset, length;
217 void *hw, *sw;
218
219 if (y + h > st->base.height)
220 h = st->base.height - y;
221
222 /* Transfer band must be aligned to pixel block boundaries */
223 assert(y % blockheight == 0);
224 assert(h % blockheight == 0);
225
226 offset = y * st->base.stride / blockheight;
227 length = h * st->base.stride / blockheight;
228
229 sw = (uint8_t *)st->swbuf + offset;
230
231 if(transfer == SVGA3D_WRITE_HOST_VRAM) {
232 /* Wait for the previous DMAs to complete */
233 /* TODO: keep one DMA (at half the size) in the background */
234 if(y) {
235 svga_screen_flush(screen, &fence);
236 sws->fence_finish(sws, fence, 0);
237 //sws->fence_reference(sws, &fence, NULL);
238 }
239
240 hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
241 assert(hw);
242 if(hw) {
243 memcpy(hw, sw, length);
244 sws->buffer_unmap(sws, st->hwbuf);
245 }
246 }
247
248 svga_transfer_dma_band(st, transfer, y, h, srcy);
249
250 if(transfer == SVGA3D_READ_HOST_VRAM) {
251 svga_screen_flush(screen, &fence);
252 sws->fence_finish(sws, fence, 0);
253
254 hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_READ);
255 assert(hw);
256 if(hw) {
257 memcpy(sw, hw, length);
258 sws->buffer_unmap(sws, st->hwbuf);
259 }
260 }
261 }
262 }
263 }
264
265
266 static struct pipe_texture *
267 svga_texture_create(struct pipe_screen *screen,
268 const struct pipe_texture *templat)
269 {
270 struct svga_screen *svgascreen = svga_screen(screen);
271 struct svga_texture *tex = CALLOC_STRUCT(svga_texture);
272 unsigned width, height, depth;
273 unsigned level;
274
275 if (!tex)
276 goto error1;
277
278 tex->base = *templat;
279 pipe_reference_init(&tex->base.reference, 1);
280 tex->base.screen = screen;
281
282 assert(templat->last_level < SVGA_MAX_TEXTURE_LEVELS);
283 if(templat->last_level >= SVGA_MAX_TEXTURE_LEVELS)
284 goto error2;
285
286 width = templat->width0;
287 height = templat->height0;
288 depth = templat->depth0;
289 for(level = 0; level <= templat->last_level; ++level) {
290 width = u_minify(width, 1);
291 height = u_minify(height, 1);
292 depth = u_minify(depth, 1);
293 }
294
295 tex->key.flags = 0;
296 tex->key.size.width = templat->width0;
297 tex->key.size.height = templat->height0;
298 tex->key.size.depth = templat->depth0;
299
300 if(templat->target == PIPE_TEXTURE_CUBE) {
301 tex->key.flags |= SVGA3D_SURFACE_CUBEMAP;
302 tex->key.numFaces = 6;
303 }
304 else {
305 tex->key.numFaces = 1;
306 }
307
308 if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
309 tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
310
311 if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
312 tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
313
314 /*
315 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
316 * know beforehand whether a texture will be used as a rendertarget or not
317 * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
318 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
319 */
320 #if 0
321 if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
322 !pf_is_compressed(templat->format))
323 tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
324 #endif
325
326 if(templat->tex_usage & PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
327 tex->key.flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
328
329 tex->key.numMipLevels = templat->last_level + 1;
330
331 tex->key.format = svga_translate_format(templat->format);
332 if(tex->key.format == SVGA3D_FORMAT_INVALID)
333 goto error2;
334
335 tex->key.cachable = 1;
336
337 SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
338 tex->handle = svga_screen_surface_create(svgascreen, &tex->key);
339 if (tex->handle)
340 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture)\n", tex->handle);
341
342 return &tex->base;
343
344 error2:
345 FREE(tex);
346 error1:
347 return NULL;
348 }
349
350
351 static struct pipe_texture *
352 svga_texture_blanket(struct pipe_screen * screen,
353 const struct pipe_texture *base,
354 const unsigned *stride,
355 struct pipe_buffer *buffer)
356 {
357 struct svga_texture *tex;
358 struct svga_buffer *sbuf = svga_buffer(buffer);
359 struct svga_winsys_screen *sws = svga_winsys_screen(screen);
360 assert(screen);
361
362 /* Only supports one type */
363 if (base->target != PIPE_TEXTURE_2D ||
364 base->last_level != 0 ||
365 base->depth0 != 1) {
366 return NULL;
367 }
368
369 /**
370 * We currently can't do texture blanket on
371 * SVGA3D_BUFFER. Need to blit to a temporary surface?
372 */
373
374 assert(sbuf->handle);
375 if (!sbuf->handle)
376 return NULL;
377
378 if (svga_translate_format(base->format) != sbuf->key.format) {
379 unsigned f1 = svga_translate_format(base->format);
380 unsigned f2 = sbuf->key.format;
381
382 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
383 if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
384 (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
385 (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ) ) {
386 debug_printf("%s wrong format %u != %u\n", __FUNCTION__, f1, f2);
387 return NULL;
388 }
389 }
390
391 tex = CALLOC_STRUCT(svga_texture);
392 if (!tex)
393 return NULL;
394
395 tex->base = *base;
396
397
398 if (sbuf->key.format == 1)
399 tex->base.format = PIPE_FORMAT_X8R8G8B8_UNORM;
400 else if (sbuf->key.format == 2)
401 tex->base.format = PIPE_FORMAT_A8R8G8B8_UNORM;
402
403 pipe_reference_init(&tex->base.reference, 1);
404 tex->base.screen = screen;
405
406 SVGA_DBG(DEBUG_DMA, "blanket sid %p\n", sbuf->handle);
407
408 /* We don't own this storage, so don't try to cache it.
409 */
410 assert(sbuf->key.cachable == 0);
411 tex->key.cachable = 0;
412 sws->surface_reference(sws, &tex->handle, sbuf->handle);
413
414 return &tex->base;
415 }
416
417
418 static void
419 svga_texture_destroy(struct pipe_texture *pt)
420 {
421 struct svga_screen *ss = svga_screen(pt->screen);
422 struct svga_texture *tex = (struct svga_texture *)pt;
423
424 ss->texture_timestamp++;
425
426 svga_sampler_view_reference(&tex->cached_view, NULL);
427
428 /*
429 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
430 */
431 SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
432 svga_screen_surface_destroy(ss, &tex->key, &tex->handle);
433
434 FREE(tex);
435 }
436
437
438 static void
439 svga_texture_copy_handle(struct svga_context *svga,
440 struct svga_screen *ss,
441 struct svga_winsys_surface *src_handle,
442 unsigned src_x, unsigned src_y, unsigned src_z,
443 unsigned src_level, unsigned src_face,
444 struct svga_winsys_surface *dst_handle,
445 unsigned dst_x, unsigned dst_y, unsigned dst_z,
446 unsigned dst_level, unsigned dst_face,
447 unsigned width, unsigned height, unsigned depth)
448 {
449 struct svga_surface dst, src;
450 enum pipe_error ret;
451 SVGA3dCopyBox box, *boxes;
452
453 assert(svga || ss);
454
455 src.handle = src_handle;
456 src.real_level = src_level;
457 src.real_face = src_face;
458 src.real_zslice = 0;
459
460 dst.handle = dst_handle;
461 dst.real_level = dst_level;
462 dst.real_face = dst_face;
463 dst.real_zslice = 0;
464
465 box.x = dst_x;
466 box.y = dst_y;
467 box.z = dst_z;
468 box.w = width;
469 box.h = height;
470 box.d = depth;
471 box.srcx = src_x;
472 box.srcy = src_y;
473 box.srcz = src_z;
474
475 /*
476 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
477 src_handle, src_level, src_x, src_y, src_z,
478 dst_handle, dst_level, dst_x, dst_y, dst_z);
479 */
480
481 if (svga) {
482 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
483 &src.base,
484 &dst.base,
485 &boxes, 1);
486 if(ret != PIPE_OK) {
487 svga_context_flush(svga, NULL);
488 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
489 &src.base,
490 &dst.base,
491 &boxes, 1);
492 assert(ret == PIPE_OK);
493 }
494 *boxes = box;
495 SVGA_FIFOCommitAll(svga->swc);
496 } else {
497 pipe_mutex_lock(ss->swc_mutex);
498 ret = SVGA3D_BeginSurfaceCopy(ss->swc,
499 &src.base,
500 &dst.base,
501 &boxes, 1);
502 if(ret != PIPE_OK) {
503 ss->swc->flush(ss->swc, NULL);
504 ret = SVGA3D_BeginSurfaceCopy(ss->swc,
505 &src.base,
506 &dst.base,
507 &boxes, 1);
508 assert(ret == PIPE_OK);
509 }
510 *boxes = box;
511 SVGA_FIFOCommitAll(ss->swc);
512 pipe_mutex_unlock(ss->swc_mutex);
513 }
514 }
515
516 static struct svga_winsys_surface *
517 svga_texture_view_surface(struct pipe_context *pipe,
518 struct svga_texture *tex,
519 SVGA3dSurfaceFormat format,
520 unsigned start_mip,
521 unsigned num_mip,
522 int face_pick,
523 int zslice_pick,
524 struct svga_host_surface_cache_key *key) /* OUT */
525 {
526 struct svga_screen *ss = svga_screen(tex->base.screen);
527 struct svga_winsys_surface *handle;
528 uint32_t i, j;
529 unsigned z_offset = 0;
530
531 SVGA_DBG(DEBUG_PERF,
532 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
533 face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
534
535 key->flags = 0;
536 key->format = format;
537 key->numMipLevels = num_mip;
538 key->size.width = u_minify(tex->base.width0, start_mip);
539 key->size.height = u_minify(tex->base.height0, start_mip);
540 key->size.depth = zslice_pick < 0 ? u_minify(tex->base.depth0, start_mip) : 1;
541 key->cachable = 1;
542 assert(key->size.depth == 1);
543
544 if(tex->base.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
545 key->flags |= SVGA3D_SURFACE_CUBEMAP;
546 key->numFaces = 6;
547 } else {
548 key->numFaces = 1;
549 }
550
551 if(key->format == SVGA3D_FORMAT_INVALID) {
552 key->cachable = 0;
553 return NULL;
554 }
555
556 SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
557 handle = svga_screen_surface_create(ss, key);
558 if (!handle) {
559 key->cachable = 0;
560 return NULL;
561 }
562
563 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
564
565 if (face_pick < 0)
566 face_pick = 0;
567
568 if (zslice_pick >= 0)
569 z_offset = zslice_pick;
570
571 for (i = 0; i < key->numMipLevels; i++) {
572 for (j = 0; j < key->numFaces; j++) {
573 if(tex->defined[j + face_pick][i + start_mip]) {
574 unsigned depth = (zslice_pick < 0 ?
575 u_minify(tex->base.depth0, i + start_mip) :
576 1);
577
578 svga_texture_copy_handle(svga_context(pipe),
579 ss,
580 tex->handle,
581 0, 0, z_offset,
582 i + start_mip,
583 j + face_pick,
584 handle, 0, 0, 0, i, j,
585 u_minify(tex->base.width0, i + start_mip),
586 u_minify(tex->base.height0, i + start_mip),
587 depth);
588 }
589 }
590 }
591
592 return handle;
593 }
594
595
596 static struct pipe_surface *
597 svga_get_tex_surface(struct pipe_screen *screen,
598 struct pipe_texture *pt,
599 unsigned face, unsigned level, unsigned zslice,
600 unsigned flags)
601 {
602 struct svga_texture *tex = svga_texture(pt);
603 struct svga_surface *s;
604 boolean render = flags & PIPE_BUFFER_USAGE_GPU_WRITE ? TRUE : FALSE;
605 boolean view = FALSE;
606 SVGA3dSurfaceFormat format;
607
608 s = CALLOC_STRUCT(svga_surface);
609 if (!s)
610 return NULL;
611
612 pipe_reference_init(&s->base.reference, 1);
613 pipe_texture_reference(&s->base.texture, pt);
614 s->base.format = pt->format;
615 s->base.width = u_minify(pt->width0, level);
616 s->base.height = u_minify(pt->height0, level);
617 s->base.usage = flags;
618 s->base.level = level;
619 s->base.face = face;
620 s->base.zslice = zslice;
621
622 if (!render)
623 format = svga_translate_format(pt->format);
624 else
625 format = svga_translate_format_render(pt->format);
626
627 assert(format != SVGA3D_FORMAT_INVALID);
628 assert(!(flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE));
629
630
631 if (svga_screen(screen)->debug.force_surface_view)
632 view = TRUE;
633
634 /* Currently only used for compressed textures */
635 if (render &&
636 format != svga_translate_format(pt->format)) {
637 view = TRUE;
638 }
639
640 if (level != 0 &&
641 svga_screen(screen)->debug.force_level_surface_view)
642 view = TRUE;
643
644 if (pt->target == PIPE_TEXTURE_3D)
645 view = TRUE;
646
647 if (svga_screen(screen)->debug.no_surface_view)
648 view = FALSE;
649
650 if (view) {
651 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
652 pt, level, face, zslice, s);
653
654 s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice,
655 &s->key);
656 s->real_face = 0;
657 s->real_level = 0;
658 s->real_zslice = 0;
659 } else {
660 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
661 pt, level, face, zslice, s);
662
663 memset(&s->key, 0, sizeof s->key);
664 s->handle = tex->handle;
665 s->real_face = face;
666 s->real_level = level;
667 s->real_zslice = zslice;
668 }
669
670 return &s->base;
671 }
672
673
674 static void
675 svga_tex_surface_destroy(struct pipe_surface *surf)
676 {
677 struct svga_surface *s = svga_surface(surf);
678 struct svga_texture *t = svga_texture(surf->texture);
679 struct svga_screen *ss = svga_screen(surf->texture->screen);
680
681 if(s->handle != t->handle) {
682 SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
683 svga_screen_surface_destroy(ss, &s->key, &s->handle);
684 }
685
686 pipe_texture_reference(&surf->texture, NULL);
687 FREE(surf);
688 }
689
690
691 static INLINE void
692 svga_mark_surface_dirty(struct pipe_surface *surf)
693 {
694 struct svga_surface *s = svga_surface(surf);
695
696 if(!s->dirty) {
697 struct svga_texture *tex = svga_texture(surf->texture);
698
699 s->dirty = TRUE;
700
701 if (s->handle == tex->handle)
702 tex->defined[surf->face][surf->level] = TRUE;
703 else {
704 /* this will happen later in svga_propagate_surface */
705 }
706 }
707 }
708
709
710 void svga_mark_surfaces_dirty(struct svga_context *svga)
711 {
712 unsigned i;
713
714 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
715 if (svga->curr.framebuffer.cbufs[i])
716 svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
717 }
718 if (svga->curr.framebuffer.zsbuf)
719 svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
720 }
721
722 /**
723 * Progagate any changes from surfaces to texture.
724 * pipe is optional context to inline the blit command in.
725 */
726 void
727 svga_propagate_surface(struct pipe_context *pipe, struct pipe_surface *surf)
728 {
729 struct svga_surface *s = svga_surface(surf);
730 struct svga_texture *tex = svga_texture(surf->texture);
731 struct svga_screen *ss = svga_screen(surf->texture->screen);
732
733 if (!s->dirty)
734 return;
735
736 s->dirty = FALSE;
737 ss->texture_timestamp++;
738 tex->view_age[surf->level] = ++(tex->age);
739
740 if (s->handle != tex->handle) {
741 SVGA_DBG(DEBUG_VIEWS, "svga: Surface propagate: tex %p, level %u, from %p\n", tex, surf->level, surf);
742 svga_texture_copy_handle(svga_context(pipe), ss,
743 s->handle, 0, 0, 0, s->real_level, s->real_face,
744 tex->handle, 0, 0, surf->zslice, surf->level, surf->face,
745 u_minify(tex->base.width0, surf->level),
746 u_minify(tex->base.height0, surf->level), 1);
747 tex->defined[surf->face][surf->level] = TRUE;
748 }
749 }
750
751 /**
752 * Check if we should call svga_propagate_surface on the surface.
753 */
754 extern boolean
755 svga_surface_needs_propagation(struct pipe_surface *surf)
756 {
757 struct svga_surface *s = svga_surface(surf);
758 struct svga_texture *tex = svga_texture(surf->texture);
759
760 return s->dirty && s->handle != tex->handle;
761 }
762
763
764 static struct pipe_transfer *
765 svga_get_tex_transfer(struct pipe_screen *screen,
766 struct pipe_texture *texture,
767 unsigned face, unsigned level, unsigned zslice,
768 enum pipe_transfer_usage usage, unsigned x, unsigned y,
769 unsigned w, unsigned h)
770 {
771 struct svga_screen *ss = svga_screen(screen);
772 struct svga_winsys_screen *sws = ss->sws;
773 struct svga_transfer *st;
774 unsigned nblocksx = pf_get_nblocksx(texture->format, w);
775 unsigned nblocksy = pf_get_nblocksy(texture->format, h);
776
777 /* We can't map texture storage directly */
778 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
779 return NULL;
780
781 st = CALLOC_STRUCT(svga_transfer);
782 if (!st)
783 return NULL;
784
785 st->base.x = x;
786 st->base.y = y;
787 st->base.width = w;
788 st->base.height = h;
789 st->base.stride = nblocksx*pf_get_blocksize(texture->format);
790 st->base.usage = usage;
791 st->base.face = face;
792 st->base.level = level;
793 st->base.zslice = zslice;
794
795 st->hw_nblocksy = nblocksy;
796
797 st->hwbuf = svga_winsys_buffer_create(ss,
798 1,
799 0,
800 st->hw_nblocksy*st->base.stride);
801 while(!st->hwbuf && (st->hw_nblocksy /= 2)) {
802 st->hwbuf = svga_winsys_buffer_create(ss,
803 1,
804 0,
805 st->hw_nblocksy*st->base.stride);
806 }
807
808 if(!st->hwbuf)
809 goto no_hwbuf;
810
811 if(st->hw_nblocksy < nblocksy) {
812 /* We couldn't allocate a hardware buffer big enough for the transfer,
813 * so allocate regular malloc memory instead */
814 debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
815 __FUNCTION__,
816 (nblocksy*st->base.stride + 1023)/1024,
817 (nblocksy + st->hw_nblocksy - 1)/st->hw_nblocksy,
818 (st->hw_nblocksy*st->base.stride + 1023)/1024);
819 st->swbuf = MALLOC(nblocksy*st->base.stride);
820 if(!st->swbuf)
821 goto no_swbuf;
822 }
823
824 pipe_texture_reference(&st->base.texture, texture);
825
826 if (usage & PIPE_TRANSFER_READ)
827 svga_transfer_dma(st, SVGA3D_READ_HOST_VRAM);
828
829 return &st->base;
830
831 no_swbuf:
832 sws->buffer_destroy(sws, st->hwbuf);
833 no_hwbuf:
834 FREE(st);
835 return NULL;
836 }
837
838
839 static void *
840 svga_transfer_map( 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 return st->swbuf;
849 else
850 /* The wait for read transfers already happened when svga_transfer_dma
851 * was called. */
852 return sws->buffer_map(sws, st->hwbuf,
853 pipe_transfer_buffer_flags(transfer));
854 }
855
856
857 static void
858 svga_transfer_unmap(struct pipe_screen *screen,
859 struct pipe_transfer *transfer)
860 {
861 struct svga_screen *ss = svga_screen(screen);
862 struct svga_winsys_screen *sws = ss->sws;
863 struct svga_transfer *st = svga_transfer(transfer);
864
865 if(!st->swbuf)
866 sws->buffer_unmap(sws, st->hwbuf);
867 }
868
869
870 static void
871 svga_tex_transfer_destroy(struct pipe_transfer *transfer)
872 {
873 struct svga_texture *tex = svga_texture(transfer->texture);
874 struct svga_screen *ss = svga_screen(transfer->texture->screen);
875 struct svga_winsys_screen *sws = ss->sws;
876 struct svga_transfer *st = svga_transfer(transfer);
877
878 if (st->base.usage & PIPE_TRANSFER_WRITE) {
879 svga_transfer_dma(st, SVGA3D_WRITE_HOST_VRAM);
880 ss->texture_timestamp++;
881 tex->view_age[transfer->level] = ++(tex->age);
882 tex->defined[transfer->face][transfer->level] = TRUE;
883 }
884
885 pipe_texture_reference(&st->base.texture, NULL);
886 FREE(st->swbuf);
887 sws->buffer_destroy(sws, st->hwbuf);
888 FREE(st);
889 }
890
891 void
892 svga_screen_init_texture_functions(struct pipe_screen *screen)
893 {
894 screen->texture_create = svga_texture_create;
895 screen->texture_destroy = svga_texture_destroy;
896 screen->get_tex_surface = svga_get_tex_surface;
897 screen->tex_surface_destroy = svga_tex_surface_destroy;
898 screen->texture_blanket = svga_texture_blanket;
899 screen->get_tex_transfer = svga_get_tex_transfer;
900 screen->transfer_map = svga_transfer_map;
901 screen->transfer_unmap = svga_transfer_unmap;
902 screen->tex_transfer_destroy = svga_tex_transfer_destroy;
903 }
904
905 /***********************************************************************
906 */
907
908 struct svga_sampler_view *
909 svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
910 unsigned min_lod, unsigned max_lod)
911 {
912 struct svga_screen *ss = svga_screen(pt->screen);
913 struct svga_texture *tex = svga_texture(pt);
914 struct svga_sampler_view *sv = NULL;
915 SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
916 boolean view = TRUE;
917
918 assert(pt);
919 assert(min_lod >= 0);
920 assert(min_lod <= max_lod);
921 assert(max_lod <= pt->last_level);
922
923
924 /* Is a view needed */
925 {
926 /*
927 * Can't control max lod. For first level views and when we only
928 * look at one level we disable mip filtering to achive the same
929 * results as a view.
930 */
931 if (min_lod == 0 && max_lod >= pt->last_level)
932 view = FALSE;
933
934 if (pf_is_compressed(pt->format) && view) {
935 format = svga_translate_format_render(pt->format);
936 }
937
938 if (ss->debug.no_sampler_view)
939 view = FALSE;
940
941 if (ss->debug.force_sampler_view)
942 view = TRUE;
943 }
944
945 /* First try the cache */
946 if (view) {
947 pipe_mutex_lock(ss->tex_mutex);
948 if (tex->cached_view &&
949 tex->cached_view->min_lod == min_lod &&
950 tex->cached_view->max_lod == max_lod) {
951 svga_sampler_view_reference(&sv, tex->cached_view);
952 pipe_mutex_unlock(ss->tex_mutex);
953 SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
954 pt, min_lod, max_lod, pt->last_level);
955 svga_validate_sampler_view(svga_context(pipe), sv);
956 return sv;
957 }
958 pipe_mutex_unlock(ss->tex_mutex);
959 }
960
961 sv = CALLOC_STRUCT(svga_sampler_view);
962 pipe_reference_init(&sv->reference, 1);
963 pipe_texture_reference(&sv->texture, pt);
964 sv->min_lod = min_lod;
965 sv->max_lod = max_lod;
966
967 /* No view needed just use the whole texture */
968 if (!view) {
969 SVGA_DBG(DEBUG_VIEWS,
970 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
971 pt, min_lod, max_lod,
972 max_lod - min_lod + 1,
973 pt->width0,
974 pt->height0,
975 pt->depth0,
976 pt->last_level);
977 sv->key.cachable = 0;
978 sv->handle = tex->handle;
979 return sv;
980 }
981
982 SVGA_DBG(DEBUG_VIEWS,
983 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
984 pt, min_lod, max_lod,
985 max_lod - min_lod + 1,
986 pt->width0,
987 pt->height0,
988 pt->depth0,
989 pt->last_level);
990
991 sv->age = tex->age;
992 sv->handle = svga_texture_view_surface(pipe, tex, format,
993 min_lod,
994 max_lod - min_lod + 1,
995 -1, -1,
996 &sv->key);
997
998 if (!sv->handle) {
999 assert(0);
1000 sv->key.cachable = 0;
1001 sv->handle = tex->handle;
1002 return sv;
1003 }
1004
1005 pipe_mutex_lock(ss->tex_mutex);
1006 svga_sampler_view_reference(&tex->cached_view, sv);
1007 pipe_mutex_unlock(ss->tex_mutex);
1008
1009 return sv;
1010 }
1011
1012 void
1013 svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
1014 {
1015 struct svga_texture *tex = svga_texture(v->texture);
1016 unsigned numFaces;
1017 unsigned age = 0;
1018 int i, k;
1019
1020 assert(svga);
1021
1022 if (v->handle == tex->handle)
1023 return;
1024
1025 age = tex->age;
1026
1027 if(tex->base.target == PIPE_TEXTURE_CUBE)
1028 numFaces = 6;
1029 else
1030 numFaces = 1;
1031
1032 for (i = v->min_lod; i <= v->max_lod; i++) {
1033 for (k = 0; k < numFaces; k++) {
1034 if (v->age < tex->view_age[i])
1035 svga_texture_copy_handle(svga, NULL,
1036 tex->handle, 0, 0, 0, i, k,
1037 v->handle, 0, 0, 0, i - v->min_lod, k,
1038 u_minify(tex->base.width0, i),
1039 u_minify(tex->base.height0, i),
1040 u_minify(tex->base.depth0, i));
1041 }
1042 }
1043
1044 v->age = age;
1045 }
1046
1047 void
1048 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
1049 {
1050 struct svga_texture *tex = svga_texture(v->texture);
1051
1052 if(v->handle != tex->handle) {
1053 struct svga_screen *ss = svga_screen(v->texture->screen);
1054 SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
1055 svga_screen_surface_destroy(ss, &v->key, &v->handle);
1056 }
1057 pipe_texture_reference(&v->texture, NULL);
1058 FREE(v);
1059 }
1060
1061 boolean
1062 svga_screen_buffer_from_texture(struct pipe_texture *texture,
1063 struct pipe_buffer **buffer,
1064 unsigned *stride)
1065 {
1066 struct svga_texture *stex = svga_texture(texture);
1067
1068 *buffer = svga_screen_buffer_wrap_surface
1069 (texture->screen,
1070 svga_translate_format(texture->format),
1071 stex->handle);
1072
1073 *stride = pf_get_stride(texture->format, texture->width0);
1074
1075 return *buffer != NULL;
1076 }
1077
1078
1079 struct svga_winsys_surface *
1080 svga_screen_texture_get_winsys_surface(struct pipe_texture *texture)
1081 {
1082 struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
1083 struct svga_winsys_surface *vsurf = NULL;
1084
1085 assert(svga_texture(texture)->key.cachable == 0);
1086 svga_texture(texture)->key.cachable = 0;
1087 sws->surface_reference(sws, &vsurf, svga_texture(texture)->handle);
1088 return vsurf;
1089 }