r600g: flush differences back to DB copy.
[mesa.git] / src / gallium / drivers / r600 / r600_texture.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include <errno.h>
28 #include <pipe/p_screen.h>
29 #include <util/u_format.h>
30 #include <util/u_math.h>
31 #include <util/u_inlines.h>
32 #include <util/u_memory.h>
33 #include "state_tracker/drm_driver.h"
34 #include "pipebuffer/pb_buffer.h"
35 #include "r600_pipe.h"
36 #include "r600_resource.h"
37 #include "r600_state_inlines.h"
38 #include "r600d.h"
39 #include "r600_formats.h"
40
41 extern struct u_resource_vtbl r600_texture_vtbl;
42
43 /* Copy from a full GPU texture to a transfer's staging one. */
44 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
45 {
46 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
47 struct pipe_resource *texture = transfer->resource;
48
49 ctx->resource_copy_region(ctx, rtransfer->staging_texture,
50 0, 0, 0, 0, texture, transfer->level,
51 &transfer->box);
52 }
53
54
55 /* Copy from a transfer's staging texture to a full GPU one. */
56 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
57 {
58 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
59 struct pipe_resource *texture = transfer->resource;
60 struct pipe_box sbox;
61
62 sbox.x = sbox.y = sbox.z = 0;
63 sbox.width = transfer->box.width;
64 sbox.height = transfer->box.height;
65 /* XXX that might be wrong */
66 sbox.depth = 1;
67 ctx->resource_copy_region(ctx, texture, transfer->level,
68 transfer->box.x, transfer->box.y, transfer->box.z,
69 rtransfer->staging_texture,
70 0, &sbox);
71
72 ctx->flush(ctx, 0, NULL);
73 }
74
75 unsigned r600_texture_get_offset(struct r600_resource_texture *rtex,
76 unsigned level, unsigned layer)
77 {
78 unsigned offset = rtex->offset[level];
79
80 switch (rtex->resource.base.b.target) {
81 case PIPE_TEXTURE_3D:
82 case PIPE_TEXTURE_CUBE:
83 return offset + layer * rtex->layer_size[level];
84 default:
85 assert(layer == 0);
86 return offset;
87 }
88 }
89
90 static unsigned r600_get_pixel_alignment(struct pipe_screen *screen,
91 enum pipe_format format,
92 unsigned array_mode)
93 {
94 struct r600_screen* rscreen = (struct r600_screen *)screen;
95 unsigned pixsize = util_format_get_blocksize(format);
96 int p_align;
97
98 switch(array_mode) {
99 case V_038000_ARRAY_1D_TILED_THIN1:
100 p_align = MAX2(8,
101 ((rscreen->tiling_info->group_bytes / 8 / pixsize)));
102 break;
103 case V_038000_ARRAY_2D_TILED_THIN1:
104 p_align = MAX2(rscreen->tiling_info->num_banks,
105 (((rscreen->tiling_info->group_bytes / 8 / pixsize)) *
106 rscreen->tiling_info->num_banks)) * 8;
107 break;
108 case V_038000_ARRAY_LINEAR_GENERAL:
109 default:
110 p_align = rscreen->tiling_info->group_bytes / pixsize;
111 break;
112 }
113 return p_align;
114 }
115
116 static unsigned r600_get_height_alignment(struct pipe_screen *screen,
117 unsigned array_mode)
118 {
119 struct r600_screen* rscreen = (struct r600_screen *)screen;
120 int h_align;
121
122 switch (array_mode) {
123 case V_038000_ARRAY_2D_TILED_THIN1:
124 h_align = rscreen->tiling_info->num_channels * 8;
125 break;
126 case V_038000_ARRAY_1D_TILED_THIN1:
127 h_align = 8;
128 break;
129 default:
130 h_align = 1;
131 break;
132 }
133 return h_align;
134 }
135
136 static unsigned r600_get_base_alignment(struct pipe_screen *screen,
137 enum pipe_format format,
138 unsigned array_mode)
139 {
140 struct r600_screen* rscreen = (struct r600_screen *)screen;
141 unsigned pixsize = util_format_get_blocksize(format);
142 int p_align = r600_get_pixel_alignment(screen, format, array_mode);
143 int h_align = r600_get_height_alignment(screen, array_mode);
144 int b_align;
145
146 switch (array_mode) {
147 case V_038000_ARRAY_2D_TILED_THIN1:
148 b_align = MAX2(rscreen->tiling_info->num_banks * rscreen->tiling_info->num_channels * 8 * 8 * pixsize,
149 p_align * pixsize * h_align);
150 break;
151 case V_038000_ARRAY_1D_TILED_THIN1:
152 default:
153 b_align = rscreen->tiling_info->group_bytes;
154 break;
155 }
156 return b_align;
157 }
158
159 static unsigned mip_minify(unsigned size, unsigned level)
160 {
161 unsigned val;
162 val = u_minify(size, level);
163 if (level > 0)
164 val = util_next_power_of_two(val);
165 return val;
166 }
167
168 static unsigned r600_texture_get_stride(struct pipe_screen *screen,
169 struct r600_resource_texture *rtex,
170 unsigned level)
171 {
172 struct pipe_resource *ptex = &rtex->resource.base.b;
173 unsigned width, stride, tile_width;
174
175 if (rtex->pitch_override)
176 return rtex->pitch_override;
177
178 width = mip_minify(ptex->width0, level);
179 if (util_format_is_plain(ptex->format)) {
180 tile_width = r600_get_pixel_alignment(screen, ptex->format,
181 rtex->array_mode[level]);
182 width = align(width, tile_width);
183 }
184 stride = util_format_get_stride(ptex->format, width);
185
186 return stride;
187 }
188
189 static unsigned r600_texture_get_nblocksy(struct pipe_screen *screen,
190 struct r600_resource_texture *rtex,
191 unsigned level)
192 {
193 struct pipe_resource *ptex = &rtex->resource.base.b;
194 unsigned height, tile_height;
195
196 height = mip_minify(ptex->height0, level);
197 if (util_format_is_plain(ptex->format)) {
198 tile_height = r600_get_height_alignment(screen,
199 rtex->array_mode[level]);
200 height = align(height, tile_height);
201 }
202 return util_format_get_nblocksy(ptex->format, height);
203 }
204
205 /* Get a width in pixels from a stride in bytes. */
206 static unsigned pitch_to_width(enum pipe_format format, unsigned pitch_in_bytes)
207 {
208 return (pitch_in_bytes / util_format_get_blocksize(format)) *
209 util_format_get_blockwidth(format);
210 }
211
212 static void r600_texture_set_array_mode(struct pipe_screen *screen,
213 struct r600_resource_texture *rtex,
214 unsigned level, unsigned array_mode)
215 {
216 struct pipe_resource *ptex = &rtex->resource.base.b;
217
218 switch (array_mode) {
219 case V_0280A0_ARRAY_LINEAR_GENERAL:
220 case V_0280A0_ARRAY_LINEAR_ALIGNED:
221 case V_0280A0_ARRAY_1D_TILED_THIN1:
222 default:
223 rtex->array_mode[level] = array_mode;
224 break;
225 case V_0280A0_ARRAY_2D_TILED_THIN1:
226 {
227 unsigned w, h, tile_height, tile_width;
228
229 tile_height = r600_get_height_alignment(screen, array_mode);
230 tile_width = r600_get_pixel_alignment(screen, ptex->format, array_mode);
231
232 w = mip_minify(ptex->width0, level);
233 h = mip_minify(ptex->height0, level);
234 if (w < tile_width || h < tile_height)
235 rtex->array_mode[level] = V_0280A0_ARRAY_1D_TILED_THIN1;
236 else
237 rtex->array_mode[level] = array_mode;
238 }
239 break;
240 }
241 }
242
243 static void r600_setup_miptree(struct pipe_screen *screen,
244 struct r600_resource_texture *rtex,
245 unsigned array_mode)
246 {
247 struct pipe_resource *ptex = &rtex->resource.base.b;
248 struct radeon *radeon = (struct radeon *)screen->winsys;
249 enum chip_class chipc = r600_get_family_class(radeon);
250 unsigned pitch, size, layer_size, i, offset;
251 unsigned nblocksy;
252
253 for (i = 0, offset = 0; i <= ptex->last_level; i++) {
254 r600_texture_set_array_mode(screen, rtex, i, array_mode);
255
256 pitch = r600_texture_get_stride(screen, rtex, i);
257 nblocksy = r600_texture_get_nblocksy(screen, rtex, i);
258
259 layer_size = pitch * nblocksy;
260
261 if (ptex->target == PIPE_TEXTURE_CUBE) {
262 if (chipc >= R700)
263 size = layer_size * 8;
264 else
265 size = layer_size * 6;
266 }
267 else
268 size = layer_size * u_minify(ptex->depth0, i);
269 /* align base image and start of miptree */
270 if ((i == 0) || (i == 1))
271 offset = align(offset, r600_get_base_alignment(screen, ptex->format, array_mode));
272 rtex->offset[i] = offset;
273 rtex->layer_size[i] = layer_size;
274 rtex->pitch_in_bytes[i] = pitch;
275 rtex->pitch_in_pixels[i] = pitch_to_width(ptex->format, pitch);
276 offset += size;
277 }
278 rtex->size = offset;
279 }
280
281 /* Figure out whether u_blitter will fallback to a transfer operation.
282 * If so, don't use a staging resource.
283 */
284 static boolean permit_hardware_blit(struct pipe_screen *screen,
285 const struct pipe_resource *res)
286 {
287 unsigned bind;
288
289 if (util_format_is_depth_or_stencil(res->format))
290 bind = PIPE_BIND_DEPTH_STENCIL;
291 else
292 bind = PIPE_BIND_RENDER_TARGET;
293
294 if (!screen->is_format_supported(screen,
295 res->format,
296 res->target,
297 res->nr_samples,
298 bind, 0))
299 return FALSE;
300
301 if (!screen->is_format_supported(screen,
302 res->format,
303 res->target,
304 res->nr_samples,
305 PIPE_BIND_SAMPLER_VIEW, 0))
306 return FALSE;
307
308 return TRUE;
309 }
310
311 static struct r600_resource_texture *
312 r600_texture_create_object(struct pipe_screen *screen,
313 const struct pipe_resource *base,
314 unsigned array_mode,
315 unsigned pitch_in_bytes_override,
316 unsigned max_buffer_size,
317 struct r600_bo *bo)
318 {
319 struct r600_resource_texture *rtex;
320 struct r600_resource *resource;
321 struct radeon *radeon = (struct radeon *)screen->winsys;
322
323 rtex = CALLOC_STRUCT(r600_resource_texture);
324 if (rtex == NULL)
325 return NULL;
326
327 resource = &rtex->resource;
328 resource->base.b = *base;
329 resource->base.vtbl = &r600_texture_vtbl;
330 pipe_reference_init(&resource->base.b.reference, 1);
331 resource->base.b.screen = screen;
332 resource->bo = bo;
333 rtex->pitch_override = pitch_in_bytes_override;
334 /* only mark depth textures the HW can hit as depth textures */
335 if (util_format_is_depth_or_stencil(base->format) && permit_hardware_blit(screen, base))
336 rtex->depth = 1;
337
338 if (array_mode)
339 rtex->tiled = 1;
340 r600_setup_miptree(screen, rtex, array_mode);
341
342 resource->size = rtex->size;
343
344 if (!resource->bo) {
345 struct pipe_resource *ptex = &rtex->resource.base.b;
346 int base_align = r600_get_base_alignment(screen, ptex->format, array_mode);
347
348 resource->bo = r600_bo(radeon, rtex->size, base_align, base->bind, base->usage);
349 if (!resource->bo) {
350 FREE(rtex);
351 return NULL;
352 }
353 }
354 return rtex;
355 }
356
357 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
358 const struct pipe_resource *templ)
359 {
360 unsigned array_mode = 0;
361 static int force_tiling = -1;
362
363 /* Would like some magic "get_bool_option_once" routine.
364 */
365 if (force_tiling == -1)
366 force_tiling = debug_get_bool_option("R600_FORCE_TILING", FALSE);
367
368 if (force_tiling && permit_hardware_blit(screen, templ)) {
369 if (!(templ->flags & R600_RESOURCE_FLAG_TRANSFER) &&
370 !(templ->bind & PIPE_BIND_SCANOUT)) {
371 array_mode = V_038000_ARRAY_2D_TILED_THIN1;
372 }
373 }
374
375 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
376 0, 0, NULL);
377
378 }
379
380 static void r600_texture_destroy(struct pipe_screen *screen,
381 struct pipe_resource *ptex)
382 {
383 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
384 struct r600_resource *resource = &rtex->resource;
385 struct radeon *radeon = (struct radeon *)screen->winsys;
386
387 if (rtex->flushed_depth_texture)
388 pipe_resource_reference((struct pipe_resource **)&rtex->flushed_depth_texture, NULL);
389
390 if (resource->bo) {
391 r600_bo_reference(radeon, &resource->bo, NULL);
392 }
393 FREE(rtex);
394 }
395
396 static boolean r600_texture_get_handle(struct pipe_screen* screen,
397 struct pipe_resource *ptex,
398 struct winsys_handle *whandle)
399 {
400 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
401 struct r600_resource *resource = &rtex->resource;
402 struct radeon *radeon = (struct radeon *)screen->winsys;
403
404 return r600_bo_get_winsys_handle(radeon, resource->bo,
405 rtex->pitch_in_bytes[0], whandle);
406 }
407
408 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
409 struct pipe_resource *texture,
410 const struct pipe_surface *surf_tmpl)
411 {
412 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
413 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
414 unsigned tile_height;
415 unsigned level = surf_tmpl->u.tex.level;
416
417 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
418 if (surface == NULL)
419 return NULL;
420 /* XXX no offset */
421 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
422 pipe_reference_init(&surface->base.reference, 1);
423 pipe_resource_reference(&surface->base.texture, texture);
424 surface->base.context = pipe;
425 surface->base.format = surf_tmpl->format;
426 surface->base.width = mip_minify(texture->width0, level);
427 surface->base.height = mip_minify(texture->height0, level);
428 surface->base.usage = surf_tmpl->usage;
429 surface->base.texture = texture;
430 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
431 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
432 surface->base.u.tex.level = level;
433
434 tile_height = r600_get_height_alignment(pipe->screen, rtex->array_mode[level]);
435 surface->aligned_height = align(surface->base.height, tile_height);
436 return &surface->base;
437 }
438
439 static void r600_surface_destroy(struct pipe_context *pipe,
440 struct pipe_surface *surface)
441 {
442 pipe_resource_reference(&surface->texture, NULL);
443 FREE(surface);
444 }
445
446
447 struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
448 const struct pipe_resource *templ,
449 struct winsys_handle *whandle)
450 {
451 struct radeon *rw = (struct radeon*)screen->winsys;
452 struct r600_bo *bo = NULL;
453 unsigned array_mode = 0;
454
455 /* Support only 2D textures without mipmaps */
456 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
457 templ->depth0 != 1 || templ->last_level != 0)
458 return NULL;
459
460 bo = r600_bo_handle(rw, whandle->handle, &array_mode);
461 if (bo == NULL) {
462 return NULL;
463 }
464
465 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
466 whandle->stride,
467 0,
468 bo);
469 }
470
471 static unsigned int r600_texture_is_referenced(struct pipe_context *context,
472 struct pipe_resource *texture,
473 unsigned level, int layer)
474 {
475 /* FIXME */
476 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
477 }
478
479 int r600_texture_depth_flush(struct pipe_context *ctx,
480 struct pipe_resource *texture)
481 {
482 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
483 struct pipe_resource resource;
484
485 if (rtex->flushed_depth_texture)
486 goto out;
487
488 resource.target = PIPE_TEXTURE_2D;
489 resource.format = texture->format;
490 resource.width0 = texture->width0;
491 resource.height0 = texture->height0;
492 resource.depth0 = 1;
493 resource.last_level = 0;
494 resource.nr_samples = 0;
495 resource.usage = PIPE_USAGE_DYNAMIC;
496 resource.bind = 0;
497 resource.flags = R600_RESOURCE_FLAG_TRANSFER;
498
499 resource.bind |= PIPE_BIND_DEPTH_STENCIL;
500
501 rtex->flushed_depth_texture = (struct r600_resource_texture *)ctx->screen->resource_create(ctx->screen, &resource);
502 if (rtex->flushed_depth_texture == NULL) {
503 R600_ERR("failed to create temporary texture to hold untiled copy\n");
504 return -ENOMEM;
505 }
506
507 ((struct r600_resource_texture *)rtex->flushed_depth_texture)->is_flushing_texture = TRUE;
508 out:
509 /* XXX: only do this if the depth texture has actually changed:
510 */
511 r600_blit_uncompress_depth(ctx, rtex);
512 return 0;
513 }
514
515 /* Needs adjustment for pixelformat:
516 */
517 static INLINE unsigned u_box_volume( const struct pipe_box *box )
518 {
519 return box->width * box->depth * box->height;
520 };
521
522 struct pipe_transfer* r600_texture_get_transfer(struct pipe_context *ctx,
523 struct pipe_resource *texture,
524 unsigned level,
525 unsigned usage,
526 const struct pipe_box *box)
527 {
528 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
529 struct pipe_resource resource;
530 struct r600_transfer *trans;
531 int r;
532 boolean use_staging_texture = FALSE;
533
534 /* We cannot map a tiled texture directly because the data is
535 * in a different order, therefore we do detiling using a blit.
536 *
537 * Also, use a temporary in GTT memory for read transfers, as
538 * the CPU is much happier reading out of cached system memory
539 * than uncached VRAM.
540 */
541 if (rtex->tiled)
542 use_staging_texture = TRUE;
543
544 if ((usage & PIPE_TRANSFER_READ) && u_box_volume(box) > 1024)
545 use_staging_texture = TRUE;
546
547 /* XXX: Use a staging texture for uploads if the underlying BO
548 * is busy. No interface for checking that currently? so do
549 * it eagerly whenever the transfer doesn't require a readback
550 * and might block.
551 */
552 if ((usage & PIPE_TRANSFER_WRITE) &&
553 !(usage & (PIPE_TRANSFER_READ |
554 PIPE_TRANSFER_DONTBLOCK |
555 PIPE_TRANSFER_UNSYNCHRONIZED)))
556 use_staging_texture = TRUE;
557
558 if (!permit_hardware_blit(ctx->screen, texture) ||
559 (texture->flags & R600_RESOURCE_FLAG_TRANSFER))
560 use_staging_texture = FALSE;
561
562 trans = CALLOC_STRUCT(r600_transfer);
563 if (trans == NULL)
564 return NULL;
565 pipe_resource_reference(&trans->transfer.resource, texture);
566 trans->transfer.level = level;
567 trans->transfer.usage = usage;
568 trans->transfer.box = *box;
569 if (rtex->depth) {
570 /* XXX: only readback the rectangle which is being mapped?
571 */
572 /* XXX: when discard is true, no need to read back from depth texture
573 */
574 r = r600_texture_depth_flush(ctx, texture);
575 if (r < 0) {
576 R600_ERR("failed to create temporary texture to hold untiled copy\n");
577 pipe_resource_reference(&trans->transfer.resource, NULL);
578 FREE(trans);
579 return NULL;
580 }
581 trans->transfer.stride = rtex->flushed_depth_texture->pitch_in_bytes[level];
582 trans->offset = r600_texture_get_offset(rtex->flushed_depth_texture, level, box->z);
583 return &trans->transfer;
584 } else if (use_staging_texture) {
585 resource.target = PIPE_TEXTURE_2D;
586 resource.format = texture->format;
587 resource.width0 = box->width;
588 resource.height0 = box->height;
589 resource.depth0 = 1;
590 resource.array_size = 1;
591 resource.last_level = 0;
592 resource.nr_samples = 0;
593 resource.usage = PIPE_USAGE_STAGING;
594 resource.bind = 0;
595 resource.flags = R600_RESOURCE_FLAG_TRANSFER;
596 /* For texture reading, the temporary (detiled) texture is used as
597 * a render target when blitting from a tiled texture. */
598 if (usage & PIPE_TRANSFER_READ) {
599 resource.bind |= PIPE_BIND_RENDER_TARGET;
600 }
601 /* For texture writing, the temporary texture is used as a sampler
602 * when blitting into a tiled texture. */
603 if (usage & PIPE_TRANSFER_WRITE) {
604 resource.bind |= PIPE_BIND_SAMPLER_VIEW;
605 }
606 /* Create the temporary texture. */
607 trans->staging_texture = ctx->screen->resource_create(ctx->screen, &resource);
608 if (trans->staging_texture == NULL) {
609 R600_ERR("failed to create temporary texture to hold untiled copy\n");
610 pipe_resource_reference(&trans->transfer.resource, NULL);
611 FREE(trans);
612 return NULL;
613 }
614
615 trans->transfer.stride =
616 ((struct r600_resource_texture *)trans->staging_texture)->pitch_in_bytes[0];
617 if (usage & PIPE_TRANSFER_READ) {
618 r600_copy_to_staging_texture(ctx, trans);
619 /* Always referenced in the blit. */
620 ctx->flush(ctx, 0, NULL);
621 }
622 return &trans->transfer;
623 }
624 trans->transfer.stride = rtex->pitch_in_bytes[level];
625 trans->offset = r600_texture_get_offset(rtex, level, box->z);
626 return &trans->transfer;
627 }
628
629 void r600_texture_transfer_destroy(struct pipe_context *ctx,
630 struct pipe_transfer *transfer)
631 {
632 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
633 struct pipe_resource *texture = transfer->resource;
634 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
635
636 if (rtransfer->staging_texture) {
637 if (transfer->usage & PIPE_TRANSFER_WRITE) {
638 r600_copy_from_staging_texture(ctx, rtransfer);
639 }
640 pipe_resource_reference(&rtransfer->staging_texture, NULL);
641 }
642
643 if (rtex->depth && !rtex->is_flushing_texture) {
644 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtex->flushed_depth_texture)
645 r600_blit_push_depth(ctx, rtex);
646 }
647
648 pipe_resource_reference(&transfer->resource, NULL);
649 FREE(transfer);
650 }
651
652 void* r600_texture_transfer_map(struct pipe_context *ctx,
653 struct pipe_transfer* transfer)
654 {
655 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
656 struct r600_bo *bo;
657 enum pipe_format format = transfer->resource->format;
658 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
659 unsigned offset = 0;
660 unsigned usage = 0;
661 char *map;
662
663 if (rtransfer->staging_texture) {
664 bo = ((struct r600_resource *)rtransfer->staging_texture)->bo;
665 } else {
666 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
667
668 if (rtex->flushed_depth_texture)
669 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
670 else
671 bo = ((struct r600_resource *)transfer->resource)->bo;
672
673 offset = rtransfer->offset +
674 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
675 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
676 }
677
678 if (transfer->usage & PIPE_TRANSFER_WRITE) {
679 usage |= PB_USAGE_CPU_WRITE;
680
681 if (transfer->usage & PIPE_TRANSFER_DISCARD) {
682 }
683
684 if (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) {
685 }
686 }
687
688 if (transfer->usage & PIPE_TRANSFER_READ) {
689 usage |= PB_USAGE_CPU_READ;
690 }
691
692 if (transfer->usage & PIPE_TRANSFER_DONTBLOCK) {
693 usage |= PB_USAGE_DONTBLOCK;
694 }
695
696 if (transfer->usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
697 usage |= PB_USAGE_UNSYNCHRONIZED;
698 }
699
700 map = r600_bo_map(radeon, bo, usage, ctx);
701 if (!map) {
702 return NULL;
703 }
704
705 return map + offset;
706 }
707
708 void r600_texture_transfer_unmap(struct pipe_context *ctx,
709 struct pipe_transfer* transfer)
710 {
711 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
712 struct radeon *radeon = (struct radeon *)ctx->screen->winsys;
713 struct r600_bo *bo;
714
715 if (rtransfer->staging_texture) {
716 bo = ((struct r600_resource *)rtransfer->staging_texture)->bo;
717 } else {
718 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
719
720 if (rtex->flushed_depth_texture) {
721 bo = ((struct r600_resource *)rtex->flushed_depth_texture)->bo;
722 } else {
723 bo = ((struct r600_resource *)transfer->resource)->bo;
724 }
725 }
726 r600_bo_unmap(radeon, bo);
727 }
728
729 struct u_resource_vtbl r600_texture_vtbl =
730 {
731 r600_texture_get_handle, /* get_handle */
732 r600_texture_destroy, /* resource_destroy */
733 r600_texture_is_referenced, /* is_resource_referenced */
734 r600_texture_get_transfer, /* get_transfer */
735 r600_texture_transfer_destroy, /* transfer_destroy */
736 r600_texture_transfer_map, /* transfer_map */
737 u_default_transfer_flush_region,/* transfer_flush_region */
738 r600_texture_transfer_unmap, /* transfer_unmap */
739 u_default_transfer_inline_write /* transfer_inline_write */
740 };
741
742 void r600_init_surface_functions(struct r600_pipe_context *r600)
743 {
744 r600->context.create_surface = r600_create_surface;
745 r600->context.surface_destroy = r600_surface_destroy;
746 }
747
748 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
749 const unsigned char *swizzle_view)
750 {
751 unsigned i;
752 unsigned char swizzle[4];
753 unsigned result = 0;
754 const uint32_t swizzle_shift[4] = {
755 16, 19, 22, 25,
756 };
757 const uint32_t swizzle_bit[4] = {
758 0, 1, 2, 3,
759 };
760
761 if (swizzle_view) {
762 /* Combine two sets of swizzles. */
763 for (i = 0; i < 4; i++) {
764 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
765 swizzle_format[swizzle_view[i]] : swizzle_view[i];
766 }
767 } else {
768 memcpy(swizzle, swizzle_format, 4);
769 }
770
771 /* Get swizzle. */
772 for (i = 0; i < 4; i++) {
773 switch (swizzle[i]) {
774 case UTIL_FORMAT_SWIZZLE_Y:
775 result |= swizzle_bit[1] << swizzle_shift[i];
776 break;
777 case UTIL_FORMAT_SWIZZLE_Z:
778 result |= swizzle_bit[2] << swizzle_shift[i];
779 break;
780 case UTIL_FORMAT_SWIZZLE_W:
781 result |= swizzle_bit[3] << swizzle_shift[i];
782 break;
783 case UTIL_FORMAT_SWIZZLE_0:
784 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
785 break;
786 case UTIL_FORMAT_SWIZZLE_1:
787 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
788 break;
789 default: /* UTIL_FORMAT_SWIZZLE_X */
790 result |= swizzle_bit[0] << swizzle_shift[i];
791 }
792 }
793 return result;
794 }
795
796 /* texture format translate */
797 uint32_t r600_translate_texformat(enum pipe_format format,
798 const unsigned char *swizzle_view,
799 uint32_t *word4_p, uint32_t *yuv_format_p)
800 {
801 uint32_t result = 0, word4 = 0, yuv_format = 0;
802 const struct util_format_description *desc;
803 boolean uniform = TRUE;
804 int i;
805 const uint32_t sign_bit[4] = {
806 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
807 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
808 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
809 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
810 };
811 desc = util_format_description(format);
812
813 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
814
815 /* Colorspace (return non-RGB formats directly). */
816 switch (desc->colorspace) {
817 /* Depth stencil formats */
818 case UTIL_FORMAT_COLORSPACE_ZS:
819 switch (format) {
820 case PIPE_FORMAT_Z16_UNORM:
821 result = FMT_16;
822 goto out_word4;
823 case PIPE_FORMAT_X24S8_USCALED:
824 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
825 case PIPE_FORMAT_Z24X8_UNORM:
826 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
827 result = FMT_8_24;
828 goto out_word4;
829 case PIPE_FORMAT_S8X24_USCALED:
830 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
831 case PIPE_FORMAT_X8Z24_UNORM:
832 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
833 result = FMT_24_8;
834 goto out_word4;
835 case PIPE_FORMAT_S8_USCALED:
836 result = FMT_8;
837 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
838 goto out_word4;
839 default:
840 goto out_unknown;
841 }
842
843 case UTIL_FORMAT_COLORSPACE_YUV:
844 yuv_format |= (1 << 30);
845 switch (format) {
846 case PIPE_FORMAT_UYVY:
847 case PIPE_FORMAT_YUYV:
848 default:
849 break;
850 }
851 goto out_unknown; /* TODO */
852
853 case UTIL_FORMAT_COLORSPACE_SRGB:
854 word4 |= S_038010_FORCE_DEGAMMA(1);
855 if (format == PIPE_FORMAT_L8A8_SRGB || format == PIPE_FORMAT_L8_SRGB)
856 goto out_unknown; /* fails for some reason - TODO */
857 break;
858
859 default:
860 break;
861 }
862
863 /* S3TC formats. TODO */
864 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
865 static int r600_enable_s3tc = -1;
866
867 if (r600_enable_s3tc == -1)
868 r600_enable_s3tc =
869 debug_get_bool_option("R600_ENABLE_S3TC", FALSE);
870
871 if (!r600_enable_s3tc)
872 goto out_unknown;
873
874 switch (format) {
875 case PIPE_FORMAT_DXT1_RGB:
876 case PIPE_FORMAT_DXT1_RGBA:
877 result = FMT_BC1;
878 goto out_word4;
879 case PIPE_FORMAT_DXT3_RGBA:
880 result = FMT_BC2;
881 goto out_word4;
882 case PIPE_FORMAT_DXT5_RGBA:
883 result = FMT_BC3;
884 goto out_word4;
885 default:
886 goto out_unknown;
887 }
888 }
889
890
891 for (i = 0; i < desc->nr_channels; i++) {
892 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
893 word4 |= sign_bit[i];
894 }
895 }
896
897 /* R8G8Bx_SNORM - TODO CxV8U8 */
898
899 /* RGTC - TODO */
900
901 /* See whether the components are of the same size. */
902 for (i = 1; i < desc->nr_channels; i++) {
903 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
904 }
905
906 /* Non-uniform formats. */
907 if (!uniform) {
908 switch(desc->nr_channels) {
909 case 3:
910 if (desc->channel[0].size == 5 &&
911 desc->channel[1].size == 6 &&
912 desc->channel[2].size == 5) {
913 result = FMT_5_6_5;
914 goto out_word4;
915 }
916 goto out_unknown;
917 case 4:
918 if (desc->channel[0].size == 5 &&
919 desc->channel[1].size == 5 &&
920 desc->channel[2].size == 5 &&
921 desc->channel[3].size == 1) {
922 result = FMT_1_5_5_5;
923 goto out_word4;
924 }
925 if (desc->channel[0].size == 10 &&
926 desc->channel[1].size == 10 &&
927 desc->channel[2].size == 10 &&
928 desc->channel[3].size == 2) {
929 result = FMT_2_10_10_10;
930 goto out_word4;
931 }
932 goto out_unknown;
933 }
934 goto out_unknown;
935 }
936
937 /* Find the first non-VOID channel. */
938 for (i = 0; i < 4; i++) {
939 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
940 break;
941 }
942 }
943
944 if (i == 4)
945 goto out_unknown;
946
947 /* uniform formats */
948 switch (desc->channel[i].type) {
949 case UTIL_FORMAT_TYPE_UNSIGNED:
950 case UTIL_FORMAT_TYPE_SIGNED:
951 if (!desc->channel[i].normalized &&
952 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
953 goto out_unknown;
954 }
955
956 switch (desc->channel[i].size) {
957 case 4:
958 switch (desc->nr_channels) {
959 case 2:
960 result = FMT_4_4;
961 goto out_word4;
962 case 4:
963 result = FMT_4_4_4_4;
964 goto out_word4;
965 }
966 goto out_unknown;
967 case 8:
968 switch (desc->nr_channels) {
969 case 1:
970 result = FMT_8;
971 goto out_word4;
972 case 2:
973 result = FMT_8_8;
974 goto out_word4;
975 case 4:
976 result = FMT_8_8_8_8;
977 goto out_word4;
978 }
979 goto out_unknown;
980 case 16:
981 switch (desc->nr_channels) {
982 case 1:
983 result = FMT_16;
984 goto out_word4;
985 case 2:
986 result = FMT_16_16;
987 goto out_word4;
988 case 4:
989 result = FMT_16_16_16_16;
990 goto out_word4;
991 }
992 }
993 goto out_unknown;
994
995 case UTIL_FORMAT_TYPE_FLOAT:
996 switch (desc->channel[i].size) {
997 case 16:
998 switch (desc->nr_channels) {
999 case 1:
1000 result = FMT_16_FLOAT;
1001 goto out_word4;
1002 case 2:
1003 result = FMT_16_16_FLOAT;
1004 goto out_word4;
1005 case 4:
1006 result = FMT_16_16_16_16_FLOAT;
1007 goto out_word4;
1008 }
1009 goto out_unknown;
1010 case 32:
1011 switch (desc->nr_channels) {
1012 case 1:
1013 result = FMT_32_FLOAT;
1014 goto out_word4;
1015 case 2:
1016 result = FMT_32_32_FLOAT;
1017 goto out_word4;
1018 case 4:
1019 result = FMT_32_32_32_32_FLOAT;
1020 goto out_word4;
1021 }
1022 }
1023
1024 }
1025 out_word4:
1026 if (word4_p)
1027 *word4_p = word4;
1028 if (yuv_format_p)
1029 *yuv_format_p = yuv_format;
1030 return result;
1031 out_unknown:
1032 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
1033 return ~0;
1034 }