svga: add DXGenMips command support
[mesa.git] / src / gallium / drivers / svga / svga_resource_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 "svga3d_reg.h"
27 #include "svga3d_surfacedefs.h"
28
29 #include "pipe/p_state.h"
30 #include "pipe/p_defines.h"
31 #include "os/os_thread.h"
32 #include "os/os_time.h"
33 #include "util/u_format.h"
34 #include "util/u_inlines.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37 #include "util/u_resource.h"
38
39 #include "svga_cmd.h"
40 #include "svga_format.h"
41 #include "svga_screen.h"
42 #include "svga_context.h"
43 #include "svga_resource_texture.h"
44 #include "svga_resource_buffer.h"
45 #include "svga_sampler_view.h"
46 #include "svga_winsys.h"
47 #include "svga_debug.h"
48
49
50 static void
51 svga_transfer_dma_band(struct svga_context *svga,
52 struct svga_transfer *st,
53 SVGA3dTransferType transfer,
54 unsigned y, unsigned h, unsigned srcy,
55 SVGA3dSurfaceDMAFlags flags)
56 {
57 struct svga_texture *texture = svga_texture(st->base.resource);
58 SVGA3dCopyBox box;
59 enum pipe_error ret;
60
61 assert(!st->use_direct_map);
62
63 box.x = st->base.box.x;
64 box.y = y;
65 box.z = st->base.box.z;
66 box.w = st->base.box.width;
67 box.h = h;
68 box.d = 1;
69 box.srcx = 0;
70 box.srcy = srcy;
71 box.srcz = 0;
72
73 SVGA_DBG(DEBUG_DMA, "dma %s sid %p, face %u, (%u, %u, %u) - "
74 "(%u, %u, %u), %ubpp\n",
75 transfer == SVGA3D_WRITE_HOST_VRAM ? "to" : "from",
76 texture->handle,
77 st->slice,
78 st->base.box.x,
79 y,
80 box.z,
81 st->base.box.x + st->base.box.width,
82 y + h,
83 box.z + 1,
84 util_format_get_blocksize(texture->b.b.format) * 8 /
85 (util_format_get_blockwidth(texture->b.b.format)
86 * util_format_get_blockheight(texture->b.b.format)));
87
88 ret = SVGA3D_SurfaceDMA(svga->swc, st, transfer, &box, 1, flags);
89 if (ret != PIPE_OK) {
90 svga_context_flush(svga, NULL);
91 ret = SVGA3D_SurfaceDMA(svga->swc, st, transfer, &box, 1, flags);
92 assert(ret == PIPE_OK);
93 }
94 }
95
96
97 static void
98 svga_transfer_dma(struct svga_context *svga,
99 struct svga_transfer *st,
100 SVGA3dTransferType transfer,
101 SVGA3dSurfaceDMAFlags flags)
102 {
103 struct svga_texture *texture = svga_texture(st->base.resource);
104 struct svga_screen *screen = svga_screen(texture->b.b.screen);
105 struct svga_winsys_screen *sws = screen->sws;
106 struct pipe_fence_handle *fence = NULL;
107
108 assert(!st->use_direct_map);
109
110 if (transfer == SVGA3D_READ_HOST_VRAM) {
111 SVGA_DBG(DEBUG_PERF, "%s: readback transfer\n", __FUNCTION__);
112 }
113
114 /* Ensure any pending operations on host surfaces are queued on the command
115 * buffer first.
116 */
117 svga_surfaces_flush( svga );
118
119 if (!st->swbuf) {
120 /* Do the DMA transfer in a single go */
121 svga_transfer_dma_band(svga, st, transfer,
122 st->base.box.y, st->base.box.height, 0,
123 flags);
124
125 if (transfer == SVGA3D_READ_HOST_VRAM) {
126 svga_context_flush(svga, &fence);
127 sws->fence_finish(sws, fence, 0);
128 sws->fence_reference(sws, &fence, NULL);
129 }
130 }
131 else {
132 int y, h, srcy;
133 unsigned blockheight =
134 util_format_get_blockheight(st->base.resource->format);
135
136 h = st->hw_nblocksy * blockheight;
137 srcy = 0;
138
139 for (y = 0; y < st->base.box.height; y += h) {
140 unsigned offset, length;
141 void *hw, *sw;
142
143 if (y + h > st->base.box.height)
144 h = st->base.box.height - y;
145
146 /* Transfer band must be aligned to pixel block boundaries */
147 assert(y % blockheight == 0);
148 assert(h % blockheight == 0);
149
150 offset = y * st->base.stride / blockheight;
151 length = h * st->base.stride / blockheight;
152
153 sw = (uint8_t *) st->swbuf + offset;
154
155 if (transfer == SVGA3D_WRITE_HOST_VRAM) {
156 unsigned usage = PIPE_TRANSFER_WRITE;
157
158 /* Wait for the previous DMAs to complete */
159 /* TODO: keep one DMA (at half the size) in the background */
160 if (y) {
161 svga_context_flush(svga, NULL);
162 usage |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
163 }
164
165 hw = sws->buffer_map(sws, st->hwbuf, usage);
166 assert(hw);
167 if (hw) {
168 memcpy(hw, sw, length);
169 sws->buffer_unmap(sws, st->hwbuf);
170 }
171 }
172
173 svga_transfer_dma_band(svga, st, transfer, y, h, srcy, flags);
174
175 /*
176 * Prevent the texture contents to be discarded on the next band
177 * upload.
178 */
179 flags.discard = FALSE;
180
181 if (transfer == SVGA3D_READ_HOST_VRAM) {
182 svga_context_flush(svga, &fence);
183 sws->fence_finish(sws, fence, 0);
184
185 hw = sws->buffer_map(sws, st->hwbuf, PIPE_TRANSFER_READ);
186 assert(hw);
187 if (hw) {
188 memcpy(sw, hw, length);
189 sws->buffer_unmap(sws, st->hwbuf);
190 }
191 }
192 }
193 }
194 }
195
196
197 static boolean
198 svga_texture_get_handle(struct pipe_screen *screen,
199 struct pipe_resource *texture,
200 struct winsys_handle *whandle)
201 {
202 struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
203 unsigned stride;
204
205 assert(svga_texture(texture)->key.cachable == 0);
206 svga_texture(texture)->key.cachable = 0;
207
208 stride = util_format_get_nblocksx(texture->format, texture->width0) *
209 util_format_get_blocksize(texture->format);
210
211 return sws->surface_get_handle(sws, svga_texture(texture)->handle,
212 stride, whandle);
213 }
214
215
216 static void
217 svga_texture_destroy(struct pipe_screen *screen,
218 struct pipe_resource *pt)
219 {
220 struct svga_screen *ss = svga_screen(screen);
221 struct svga_texture *tex = svga_texture(pt);
222
223 ss->texture_timestamp++;
224
225 svga_sampler_view_reference(&tex->cached_view, NULL);
226
227 /*
228 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
229 */
230 SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
231 svga_screen_surface_destroy(ss, &tex->key, &tex->handle);
232
233 ss->hud.total_resource_bytes -= tex->size;
234
235 FREE(tex->defined);
236 FREE(tex->rendered_to);
237 FREE(tex);
238
239 assert(ss->hud.num_resources > 0);
240 if (ss->hud.num_resources > 0)
241 ss->hud.num_resources--;
242 }
243
244
245 /**
246 * Determine if we need to read back a texture image before mapping it.
247 */
248 static boolean
249 need_tex_readback(struct pipe_transfer *transfer)
250 {
251 struct svga_texture *t = svga_texture(transfer->resource);
252
253 if (transfer->usage & PIPE_TRANSFER_READ)
254 return TRUE;
255
256 if ((transfer->usage & PIPE_TRANSFER_WRITE) &&
257 ((transfer->usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) == 0)) {
258 unsigned face;
259
260 if (transfer->resource->target == PIPE_TEXTURE_CUBE) {
261 assert(transfer->box.depth == 1);
262 face = transfer->box.z;
263 }
264 else {
265 face = 0;
266 }
267 if (svga_was_texture_rendered_to(t, face, transfer->level)) {
268 return TRUE;
269 }
270 }
271
272 return FALSE;
273 }
274
275
276 static enum pipe_error
277 readback_image_vgpu9(struct svga_context *svga,
278 struct svga_winsys_surface *surf,
279 unsigned slice,
280 unsigned level)
281 {
282 enum pipe_error ret;
283
284 ret = SVGA3D_ReadbackGBImage(svga->swc, surf, slice, level);
285 if (ret != PIPE_OK) {
286 svga_context_flush(svga, NULL);
287 ret = SVGA3D_ReadbackGBImage(svga->swc, surf, slice, level);
288 }
289 return ret;
290 }
291
292
293 static enum pipe_error
294 readback_image_vgpu10(struct svga_context *svga,
295 struct svga_winsys_surface *surf,
296 unsigned slice,
297 unsigned level,
298 unsigned numMipLevels)
299 {
300 enum pipe_error ret;
301 unsigned subResource;
302
303 subResource = slice * numMipLevels + level;
304 ret = SVGA3D_vgpu10_ReadbackSubResource(svga->swc, surf, subResource);
305 if (ret != PIPE_OK) {
306 svga_context_flush(svga, NULL);
307 ret = SVGA3D_vgpu10_ReadbackSubResource(svga->swc, surf, subResource);
308 }
309 return ret;
310 }
311
312
313 static void *
314 svga_texture_transfer_map(struct pipe_context *pipe,
315 struct pipe_resource *texture,
316 unsigned level,
317 unsigned usage,
318 const struct pipe_box *box,
319 struct pipe_transfer **ptransfer)
320 {
321 struct svga_context *svga = svga_context(pipe);
322 struct svga_screen *ss = svga_screen(pipe->screen);
323 struct svga_winsys_screen *sws = ss->sws;
324 struct svga_texture *tex = svga_texture(texture);
325 struct svga_transfer *st;
326 unsigned nblocksx, nblocksy;
327 boolean use_direct_map = svga_have_gb_objects(svga) &&
328 !svga_have_gb_dma(svga);
329 unsigned d;
330 void *returnVal;
331 int64_t begin = os_time_get();
332
333 /* We can't map texture storage directly unless we have GB objects */
334 if (usage & PIPE_TRANSFER_MAP_DIRECTLY) {
335 if (svga_have_gb_objects(svga))
336 use_direct_map = TRUE;
337 else
338 return NULL;
339 }
340
341 st = CALLOC_STRUCT(svga_transfer);
342 if (!st)
343 return NULL;
344
345 {
346 unsigned w, h;
347 if (use_direct_map) {
348 /* we'll directly access the guest-backed surface */
349 w = u_minify(texture->width0, level);
350 h = u_minify(texture->height0, level);
351 d = u_minify(texture->depth0, level);
352 }
353 else {
354 /* we'll put the data into a tightly packed buffer */
355 w = box->width;
356 h = box->height;
357 d = box->depth;
358 }
359 nblocksx = util_format_get_nblocksx(texture->format, w);
360 nblocksy = util_format_get_nblocksy(texture->format, h);
361 }
362
363 pipe_resource_reference(&st->base.resource, texture);
364
365 st->base.level = level;
366 st->base.usage = usage;
367 st->base.box = *box;
368 st->base.stride = nblocksx*util_format_get_blocksize(texture->format);
369 st->base.layer_stride = st->base.stride * nblocksy;
370
371 switch (tex->b.b.target) {
372 case PIPE_TEXTURE_CUBE:
373 case PIPE_TEXTURE_2D_ARRAY:
374 case PIPE_TEXTURE_1D_ARRAY:
375 st->slice = st->base.box.z;
376 st->base.box.z = 0; /* so we don't apply double offsets below */
377 break;
378 default:
379 st->slice = 0;
380 break;
381 }
382
383 if (usage & PIPE_TRANSFER_WRITE) {
384 /* record texture upload for HUD */
385 svga->hud.num_bytes_uploaded +=
386 nblocksx * nblocksy * d * util_format_get_blocksize(texture->format);
387 }
388
389 if (!use_direct_map) {
390 /* Use a DMA buffer */
391 st->hw_nblocksy = nblocksy;
392
393 st->hwbuf = svga_winsys_buffer_create(svga, 1, 0,
394 st->hw_nblocksy * st->base.stride * d);
395 while(!st->hwbuf && (st->hw_nblocksy /= 2)) {
396 st->hwbuf = svga_winsys_buffer_create(svga, 1, 0,
397 st->hw_nblocksy * st->base.stride * d);
398 }
399
400 if (!st->hwbuf) {
401 FREE(st);
402 return NULL;
403 }
404
405 if (st->hw_nblocksy < nblocksy) {
406 /* We couldn't allocate a hardware buffer big enough for the transfer,
407 * so allocate regular malloc memory instead */
408 if (0) {
409 debug_printf("%s: failed to allocate %u KB of DMA, "
410 "splitting into %u x %u KB DMA transfers\n",
411 __FUNCTION__,
412 (nblocksy*st->base.stride + 1023)/1024,
413 (nblocksy + st->hw_nblocksy - 1)/st->hw_nblocksy,
414 (st->hw_nblocksy*st->base.stride + 1023)/1024);
415 }
416
417 st->swbuf = MALLOC(nblocksy * st->base.stride * d);
418 if (!st->swbuf) {
419 sws->buffer_destroy(sws, st->hwbuf);
420 FREE(st);
421 return NULL;
422 }
423 }
424
425 if (usage & PIPE_TRANSFER_READ) {
426 SVGA3dSurfaceDMAFlags flags;
427 memset(&flags, 0, sizeof flags);
428 svga_transfer_dma(svga, st, SVGA3D_READ_HOST_VRAM, flags);
429 }
430 } else {
431 struct pipe_transfer *transfer = &st->base;
432 struct svga_winsys_surface *surf = tex->handle;
433
434 if (!surf) {
435 FREE(st);
436 return NULL;
437 }
438
439 if (need_tex_readback(transfer)) {
440 enum pipe_error ret;
441
442 svga_surfaces_flush(svga);
443
444 if (svga_have_vgpu10(svga)) {
445 ret = readback_image_vgpu10(svga, surf, st->slice, transfer->level,
446 tex->b.b.last_level + 1);
447 } else {
448 ret = readback_image_vgpu9(svga, surf, st->slice, transfer->level);
449 }
450
451 assert(ret == PIPE_OK);
452 (void) ret;
453
454 svga_context_flush(svga, NULL);
455
456 /*
457 * Note: if PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE were specified
458 * we could potentially clear the flag for all faces/layers/mips.
459 */
460 svga_clear_texture_rendered_to(tex, st->slice, transfer->level);
461 }
462 else {
463 assert(transfer->usage & PIPE_TRANSFER_WRITE);
464 if ((transfer->usage & PIPE_TRANSFER_UNSYNCHRONIZED) == 0) {
465 svga_surfaces_flush(svga);
466 if (!sws->surface_is_flushed(sws, surf))
467 svga_context_flush(svga, NULL);
468 }
469 }
470 }
471
472 st->use_direct_map = use_direct_map;
473
474 *ptransfer = &st->base;
475
476 /*
477 * Begin mapping code
478 */
479 if (st->swbuf) {
480 returnVal = st->swbuf;
481 }
482 else if (!st->use_direct_map) {
483 returnVal = sws->buffer_map(sws, st->hwbuf, usage);
484 }
485 else {
486 SVGA3dSize baseLevelSize;
487 struct svga_texture *tex = svga_texture(texture);
488 struct svga_winsys_surface *surf = tex->handle;
489 uint8_t *map;
490 boolean retry;
491 unsigned offset, mip_width, mip_height;
492 unsigned xoffset = st->base.box.x;
493 unsigned yoffset = st->base.box.y;
494 unsigned zoffset = st->base.box.z;
495
496 map = svga->swc->surface_map(svga->swc, surf, usage, &retry);
497 if (map == NULL && retry) {
498 /*
499 * At this point, the svga_surfaces_flush() should already have
500 * called in svga_texture_get_transfer().
501 */
502 svga_context_flush(svga, NULL);
503 map = svga->swc->surface_map(svga->swc, surf, usage, &retry);
504 }
505
506 /*
507 * Make sure we return NULL if the map fails
508 */
509 if (!map) {
510 FREE(st);
511 return map;
512 }
513
514 /**
515 * Compute the offset to the specific texture slice in the buffer.
516 */
517 baseLevelSize.width = tex->b.b.width0;
518 baseLevelSize.height = tex->b.b.height0;
519 baseLevelSize.depth = tex->b.b.depth0;
520
521 offset = svga3dsurface_get_image_offset(tex->key.format, baseLevelSize,
522 tex->b.b.last_level + 1, /* numMips */
523 st->slice, level);
524 if (level > 0) {
525 assert(offset > 0);
526 }
527
528 mip_width = u_minify(tex->b.b.width0, level);
529 mip_height = u_minify(tex->b.b.height0, level);
530
531 offset += svga3dsurface_get_pixel_offset(tex->key.format,
532 mip_width, mip_height,
533 xoffset, yoffset, zoffset);
534 returnVal = (void *) (map + offset);
535 }
536
537 svga->hud.map_buffer_time += (os_time_get() - begin);
538 svga->hud.num_resources_mapped++;
539
540 return returnVal;
541 }
542
543
544 /**
545 * Unmap a GB texture surface.
546 */
547 static void
548 svga_texture_surface_unmap(struct svga_context *svga,
549 struct pipe_transfer *transfer)
550 {
551 struct svga_winsys_surface *surf = svga_texture(transfer->resource)->handle;
552 struct svga_winsys_context *swc = svga->swc;
553 boolean rebind;
554
555 assert(surf);
556
557 swc->surface_unmap(swc, surf, &rebind);
558 if (rebind) {
559 enum pipe_error ret;
560 ret = SVGA3D_BindGBSurface(swc, surf);
561 if (ret != PIPE_OK) {
562 /* flush and retry */
563 svga_context_flush(svga, NULL);
564 ret = SVGA3D_BindGBSurface(swc, surf);
565 assert(ret == PIPE_OK);
566 }
567 }
568 }
569
570
571 static enum pipe_error
572 update_image_vgpu9(struct svga_context *svga,
573 struct svga_winsys_surface *surf,
574 const SVGA3dBox *box,
575 unsigned slice,
576 unsigned level)
577 {
578 enum pipe_error ret;
579
580 ret = SVGA3D_UpdateGBImage(svga->swc, surf, box, slice, level);
581 if (ret != PIPE_OK) {
582 svga_context_flush(svga, NULL);
583 ret = SVGA3D_UpdateGBImage(svga->swc, surf, box, slice, level);
584 }
585 return ret;
586 }
587
588
589 static enum pipe_error
590 update_image_vgpu10(struct svga_context *svga,
591 struct svga_winsys_surface *surf,
592 const SVGA3dBox *box,
593 unsigned slice,
594 unsigned level,
595 unsigned numMipLevels)
596 {
597 enum pipe_error ret;
598 unsigned subResource;
599
600 subResource = slice * numMipLevels + level;
601 ret = SVGA3D_vgpu10_UpdateSubResource(svga->swc, surf, box, subResource);
602 if (ret != PIPE_OK) {
603 svga_context_flush(svga, NULL);
604 ret = SVGA3D_vgpu10_UpdateSubResource(svga->swc, surf, box, subResource);
605 }
606 return ret;
607 }
608
609
610 static void
611 svga_texture_transfer_unmap(struct pipe_context *pipe,
612 struct pipe_transfer *transfer)
613 {
614 struct svga_context *svga = svga_context(pipe);
615 struct svga_screen *ss = svga_screen(pipe->screen);
616 struct svga_winsys_screen *sws = ss->sws;
617 struct svga_transfer *st = svga_transfer(transfer);
618 struct svga_texture *tex = svga_texture(transfer->resource);
619
620 if (!st->swbuf) {
621 if (st->use_direct_map) {
622 svga_texture_surface_unmap(svga, transfer);
623 }
624 else {
625 sws->buffer_unmap(sws, st->hwbuf);
626 }
627 }
628
629 if (!st->use_direct_map && (st->base.usage & PIPE_TRANSFER_WRITE)) {
630 /* Use DMA to transfer texture data */
631 SVGA3dSurfaceDMAFlags flags;
632
633 memset(&flags, 0, sizeof flags);
634 if (transfer->usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
635 flags.discard = TRUE;
636 }
637 if (transfer->usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
638 flags.unsynchronized = TRUE;
639 }
640
641 svga_transfer_dma(svga, st, SVGA3D_WRITE_HOST_VRAM, flags);
642 } else if (transfer->usage & PIPE_TRANSFER_WRITE) {
643 struct svga_winsys_surface *surf =
644 svga_texture(transfer->resource)->handle;
645 SVGA3dBox box;
646 enum pipe_error ret;
647
648 assert(svga_have_gb_objects(svga));
649
650 /* update the effected region */
651 box.x = transfer->box.x;
652 box.y = transfer->box.y;
653 switch (tex->b.b.target) {
654 case PIPE_TEXTURE_CUBE:
655 case PIPE_TEXTURE_2D_ARRAY:
656 box.z = 0;
657 break;
658 case PIPE_TEXTURE_1D_ARRAY:
659 box.y = box.z = 0;
660 break;
661 default:
662 box.z = transfer->box.z;
663 break;
664 }
665 box.w = transfer->box.width;
666 box.h = transfer->box.height;
667 box.d = transfer->box.depth;
668
669 if (0)
670 debug_printf("%s %d, %d, %d %d x %d x %d\n",
671 __FUNCTION__,
672 box.x, box.y, box.z,
673 box.w, box.h, box.d);
674
675 if (svga_have_vgpu10(svga)) {
676 ret = update_image_vgpu10(svga, surf, &box, st->slice, transfer->level,
677 tex->b.b.last_level + 1);
678 } else {
679 ret = update_image_vgpu9(svga, surf, &box, st->slice, transfer->level);
680 }
681
682 assert(ret == PIPE_OK);
683 (void) ret;
684 }
685
686 ss->texture_timestamp++;
687 svga_age_texture_view(tex, transfer->level);
688 if (transfer->resource->target == PIPE_TEXTURE_CUBE)
689 svga_define_texture_level(tex, st->slice, transfer->level);
690 else
691 svga_define_texture_level(tex, 0, transfer->level);
692
693 pipe_resource_reference(&st->base.resource, NULL);
694
695 FREE(st->swbuf);
696 if (!st->use_direct_map) {
697 sws->buffer_destroy(sws, st->hwbuf);
698 }
699 FREE(st);
700 }
701
702
703 /**
704 * Does format store depth values?
705 */
706 static inline boolean
707 format_has_depth(enum pipe_format format)
708 {
709 const struct util_format_description *desc = util_format_description(format);
710 return util_format_has_depth(desc);
711 }
712
713
714 struct u_resource_vtbl svga_texture_vtbl =
715 {
716 svga_texture_get_handle, /* get_handle */
717 svga_texture_destroy, /* resource_destroy */
718 svga_texture_transfer_map, /* transfer_map */
719 u_default_transfer_flush_region, /* transfer_flush_region */
720 svga_texture_transfer_unmap, /* transfer_unmap */
721 u_default_transfer_inline_write /* transfer_inline_write */
722 };
723
724
725 struct pipe_resource *
726 svga_texture_create(struct pipe_screen *screen,
727 const struct pipe_resource *template)
728 {
729 struct svga_screen *svgascreen = svga_screen(screen);
730 struct svga_texture *tex;
731 unsigned bindings = template->bind;
732
733 assert(template->last_level < SVGA_MAX_TEXTURE_LEVELS);
734 if (template->last_level >= SVGA_MAX_TEXTURE_LEVELS) {
735 return NULL;
736 }
737
738 tex = CALLOC_STRUCT(svga_texture);
739 if (!tex) {
740 return NULL;
741 }
742
743 tex->defined = CALLOC(template->depth0 * template->array_size,
744 sizeof(tex->defined[0]));
745 if (!tex->defined) {
746 FREE(tex);
747 return NULL;
748 }
749
750 tex->rendered_to = CALLOC(template->depth0 * template->array_size,
751 sizeof(tex->rendered_to[0]));
752 if (!tex->rendered_to) {
753 FREE(tex->defined);
754 FREE(tex);
755 return NULL;
756 }
757
758 tex->b.b = *template;
759 tex->b.vtbl = &svga_texture_vtbl;
760 pipe_reference_init(&tex->b.b.reference, 1);
761 tex->b.b.screen = screen;
762
763 tex->key.flags = 0;
764 tex->key.size.width = template->width0;
765 tex->key.size.height = template->height0;
766 tex->key.size.depth = template->depth0;
767 tex->key.arraySize = 1;
768 tex->key.numFaces = 1;
769 tex->key.sampleCount = template->nr_samples;
770
771 if (template->nr_samples > 1) {
772 tex->key.flags |= SVGA3D_SURFACE_MASKABLE_ANTIALIAS;
773 }
774
775 if (svgascreen->sws->have_vgpu10) {
776 switch (template->target) {
777 case PIPE_TEXTURE_1D:
778 tex->key.flags |= SVGA3D_SURFACE_1D;
779 break;
780 case PIPE_TEXTURE_1D_ARRAY:
781 tex->key.flags |= SVGA3D_SURFACE_1D;
782 /* fall-through */
783 case PIPE_TEXTURE_2D_ARRAY:
784 tex->key.flags |= SVGA3D_SURFACE_ARRAY;
785 tex->key.arraySize = template->array_size;
786 break;
787 case PIPE_TEXTURE_3D:
788 tex->key.flags |= SVGA3D_SURFACE_VOLUME;
789 break;
790 case PIPE_TEXTURE_CUBE:
791 tex->key.flags |= (SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_ARRAY);
792 tex->key.numFaces = 6;
793 break;
794 default:
795 break;
796 }
797 }
798 else {
799 switch (template->target) {
800 case PIPE_TEXTURE_3D:
801 tex->key.flags |= SVGA3D_SURFACE_VOLUME;
802 break;
803 case PIPE_TEXTURE_CUBE:
804 tex->key.flags |= SVGA3D_SURFACE_CUBEMAP;
805 tex->key.numFaces = 6;
806 break;
807 default:
808 break;
809 }
810 }
811
812 tex->key.cachable = 1;
813
814 if (bindings & PIPE_BIND_SAMPLER_VIEW) {
815 tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
816 tex->key.flags |= SVGA3D_SURFACE_BIND_SHADER_RESOURCE;
817
818 if (!(bindings & PIPE_BIND_RENDER_TARGET)) {
819 /* Also check if the format is renderable */
820 if (screen->is_format_supported(screen, template->format,
821 template->target,
822 template->nr_samples,
823 PIPE_BIND_RENDER_TARGET)) {
824 bindings |= PIPE_BIND_RENDER_TARGET;
825 }
826 }
827 }
828
829 if (bindings & PIPE_BIND_DISPLAY_TARGET) {
830 tex->key.cachable = 0;
831 }
832
833 if (bindings & PIPE_BIND_SHARED) {
834 tex->key.cachable = 0;
835 }
836
837 if (bindings & (PIPE_BIND_SCANOUT | PIPE_BIND_CURSOR)) {
838 tex->key.scanout = 1;
839 tex->key.cachable = 0;
840 }
841
842 /*
843 * Note: Previously we never passed the
844 * SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
845 * know beforehand whether a texture will be used as a rendertarget or not
846 * and it always requests PIPE_BIND_RENDER_TARGET, therefore
847 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
848 *
849 * However, this was changed since other state trackers
850 * (XA for example) uses it accurately and certain device versions
851 * relies on it in certain situations to render correctly.
852 */
853 if ((bindings & PIPE_BIND_RENDER_TARGET) &&
854 !util_format_is_s3tc(template->format)) {
855 tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
856 tex->key.flags |= SVGA3D_SURFACE_BIND_RENDER_TARGET;
857 }
858
859 if (bindings & PIPE_BIND_DEPTH_STENCIL) {
860 tex->key.flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
861 tex->key.flags |= SVGA3D_SURFACE_BIND_DEPTH_STENCIL;
862 }
863
864 tex->key.numMipLevels = template->last_level + 1;
865
866 tex->key.format = svga_translate_format(svgascreen, template->format,
867 bindings);
868 if (tex->key.format == SVGA3D_FORMAT_INVALID) {
869 FREE(tex->defined);
870 FREE(tex->rendered_to);
871 FREE(tex);
872 return NULL;
873 }
874
875 /* Use typeless formats for sRGB and depth resources. Typeless
876 * formats can be reinterpreted as other formats. For example,
877 * SVGA3D_R8G8B8A8_UNORM_TYPELESS can be interpreted as
878 * SVGA3D_R8G8B8A8_UNORM_SRGB or SVGA3D_R8G8B8A8_UNORM.
879 */
880 if (svgascreen->sws->have_vgpu10 &&
881 (util_format_is_srgb(template->format) ||
882 format_has_depth(template->format))) {
883 SVGA3dSurfaceFormat typeless = svga_typeless_format(tex->key.format);
884 if (0) {
885 debug_printf("Convert resource type %s -> %s (bind 0x%x)\n",
886 svga_format_name(tex->key.format),
887 svga_format_name(typeless),
888 bindings);
889 }
890 tex->key.format = typeless;
891 }
892
893 SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
894 tex->handle = svga_screen_surface_create(svgascreen, bindings,
895 tex->b.b.usage, &tex->key);
896 if (!tex->handle) {
897 FREE(tex->defined);
898 FREE(tex->rendered_to);
899 FREE(tex);
900 return NULL;
901 }
902
903 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture)\n", tex->handle);
904
905 debug_reference(&tex->b.b.reference,
906 (debug_reference_descriptor)debug_describe_resource, 0);
907
908 tex->size = util_resource_size(template);
909 svgascreen->hud.total_resource_bytes += tex->size;
910 svgascreen->hud.num_resources++;
911
912 return &tex->b.b;
913 }
914
915
916 struct pipe_resource *
917 svga_texture_from_handle(struct pipe_screen *screen,
918 const struct pipe_resource *template,
919 struct winsys_handle *whandle)
920 {
921 struct svga_winsys_screen *sws = svga_winsys_screen(screen);
922 struct svga_screen *ss = svga_screen(screen);
923 struct svga_winsys_surface *srf;
924 struct svga_texture *tex;
925 enum SVGA3dSurfaceFormat format = 0;
926 assert(screen);
927
928 /* Only supports one type */
929 if ((template->target != PIPE_TEXTURE_2D &&
930 template->target != PIPE_TEXTURE_RECT) ||
931 template->last_level != 0 ||
932 template->depth0 != 1) {
933 return NULL;
934 }
935
936 srf = sws->surface_from_handle(sws, whandle, &format);
937
938 if (!srf)
939 return NULL;
940
941 if (svga_translate_format(svga_screen(screen), template->format,
942 template->bind) != format) {
943 unsigned f1 = svga_translate_format(svga_screen(screen),
944 template->format, template->bind);
945 unsigned f2 = format;
946
947 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up.
948 */
949 if (f1 == SVGA3D_B8G8R8A8_UNORM)
950 f1 = SVGA3D_A8R8G8B8;
951 if (f1 == SVGA3D_B8G8R8X8_UNORM)
952 f1 = SVGA3D_X8R8G8B8;
953
954 if ( !( (f1 == f2) ||
955 (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
956 (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_B8G8R8X8_UNORM) ||
957 (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
958 (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_B8G8R8A8_UNORM) ||
959 (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ||
960 (f1 == SVGA3D_Z_DF24 && f2 == SVGA3D_Z_D24S8_INT) ) ) {
961 debug_printf("%s wrong format %s != %s\n", __FUNCTION__,
962 svga_format_name(f1), svga_format_name(f2));
963 return NULL;
964 }
965 }
966
967 tex = CALLOC_STRUCT(svga_texture);
968 if (!tex)
969 return NULL;
970
971 tex->defined = CALLOC(template->depth0 * template->array_size,
972 sizeof(tex->defined[0]));
973 if (!tex->defined) {
974 FREE(tex);
975 return NULL;
976 }
977
978 tex->b.b = *template;
979 tex->b.vtbl = &svga_texture_vtbl;
980 pipe_reference_init(&tex->b.b.reference, 1);
981 tex->b.b.screen = screen;
982
983 SVGA_DBG(DEBUG_DMA, "wrap surface sid %p\n", srf);
984
985 tex->key.cachable = 0;
986 tex->key.format = format;
987 tex->handle = srf;
988
989 tex->rendered_to = CALLOC(1, sizeof(tex->rendered_to[0]));
990 tex->imported = TRUE;
991
992 ss->hud.num_resources++;
993
994 return &tex->b.b;
995 }
996
997 boolean
998 svga_texture_generate_mipmap(struct pipe_context *pipe,
999 struct pipe_resource *pt,
1000 enum pipe_format format,
1001 unsigned base_level,
1002 unsigned last_level,
1003 unsigned first_layer,
1004 unsigned last_layer)
1005 {
1006 struct pipe_sampler_view templ, *psv;
1007 struct svga_pipe_sampler_view *sv;
1008 struct svga_context *svga = svga_context(pipe);
1009 struct svga_texture *tex = svga_texture(pt);
1010 enum pipe_error ret;
1011
1012 assert(svga_have_vgpu10(svga));
1013
1014 /* Only support 2D texture for now */
1015 if (pt->target != PIPE_TEXTURE_2D)
1016 return FALSE;
1017
1018 /* Fallback to the mipmap generation utility for those formats that
1019 * do not support hw generate mipmap
1020 */
1021 if (!svga_format_support_gen_mips(format))
1022 return FALSE;
1023
1024 /* Make sure the texture surface was created with
1025 * SVGA3D_SURFACE_BIND_RENDER_TARGET
1026 */
1027 if (!tex->handle || !(tex->key.flags & SVGA3D_SURFACE_BIND_RENDER_TARGET))
1028 return FALSE;
1029
1030 templ.format = format;
1031 templ.u.tex.first_layer = first_layer;
1032 templ.u.tex.last_layer = last_layer;
1033 templ.u.tex.first_level = base_level;
1034 templ.u.tex.last_level = last_level;
1035
1036 psv = pipe->create_sampler_view(pipe, pt, &templ);
1037 if (psv == NULL)
1038 return FALSE;
1039
1040 sv = svga_pipe_sampler_view(psv);
1041 svga_validate_pipe_sampler_view(svga, sv);
1042
1043 ret = SVGA3D_vgpu10_GenMips(svga->swc, sv->id, tex->handle);
1044 if (ret != PIPE_OK) {
1045 svga_context_flush(svga, NULL);
1046 ret = SVGA3D_vgpu10_GenMips(svga->swc, sv->id, tex->handle);
1047 }
1048 pipe_sampler_view_reference(&psv, NULL);
1049
1050 svga->hud.num_generate_mipmap++;
1051
1052 return TRUE;
1053 }