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