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