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