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