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