183827bfd04288507c6d81e63c1c2220f3ee4dcd
[mesa.git] / src / gallium / drivers / radeonsi / 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_format_s3tc.h"
31 #include "util/u_math.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "pipebuffer/pb_buffer.h"
35 #include "radeonsi_pipe.h"
36 #include "r600_resource.h"
37 #include "sid.h"
38
39 /* Copy from a full GPU texture to a transfer's staging one. */
40 static void r600_copy_to_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
41 {
42 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
43 struct pipe_resource *texture = transfer->resource;
44
45 ctx->resource_copy_region(ctx, rtransfer->staging_texture,
46 0, 0, 0, 0, texture, transfer->level,
47 &transfer->box);
48 }
49
50
51 /* Copy from a transfer's staging texture to a full GPU one. */
52 static void r600_copy_from_staging_texture(struct pipe_context *ctx, struct r600_transfer *rtransfer)
53 {
54 struct pipe_transfer *transfer = (struct pipe_transfer*)rtransfer;
55 struct pipe_resource *texture = transfer->resource;
56 struct pipe_box sbox;
57
58 sbox.x = sbox.y = sbox.z = 0;
59 sbox.width = transfer->box.width;
60 sbox.height = transfer->box.height;
61 /* XXX that might be wrong */
62 sbox.depth = 1;
63 ctx->resource_copy_region(ctx, texture, transfer->level,
64 transfer->box.x, transfer->box.y, transfer->box.z,
65 rtransfer->staging_texture,
66 0, &sbox);
67 }
68
69 static unsigned r600_texture_get_offset(struct r600_resource_texture *rtex,
70 unsigned level, unsigned layer)
71 {
72 return rtex->surface.level[level].offset +
73 layer * rtex->surface.level[level].slice_size;
74 }
75
76 static int r600_init_surface(struct radeon_surface *surface,
77 const struct pipe_resource *ptex,
78 unsigned array_mode, bool is_transfer)
79 {
80 surface->npix_x = ptex->width0;
81 surface->npix_y = ptex->height0;
82 surface->npix_z = ptex->depth0;
83 surface->blk_w = util_format_get_blockwidth(ptex->format);
84 surface->blk_h = util_format_get_blockheight(ptex->format);
85 surface->blk_d = 1;
86 surface->array_size = 1;
87 surface->last_level = ptex->last_level;
88 surface->bpe = util_format_get_blocksize(ptex->format);
89 /* align byte per element on dword */
90 if (surface->bpe == 3) {
91 surface->bpe = 4;
92 }
93 surface->nsamples = 1;
94 surface->flags = 0;
95 switch (array_mode) {
96 case V_009910_ARRAY_1D_TILED_THIN1:
97 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
98 break;
99 case V_009910_ARRAY_2D_TILED_THIN1:
100 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
101 break;
102 case V_009910_ARRAY_LINEAR_ALIGNED:
103 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED, MODE);
104 break;
105 case V_009910_ARRAY_LINEAR_GENERAL:
106 default:
107 surface->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR, MODE);
108 break;
109 }
110 switch (ptex->target) {
111 case PIPE_TEXTURE_1D:
112 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D, TYPE);
113 break;
114 case PIPE_TEXTURE_RECT:
115 case PIPE_TEXTURE_2D:
116 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D, TYPE);
117 break;
118 case PIPE_TEXTURE_3D:
119 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_3D, TYPE);
120 break;
121 case PIPE_TEXTURE_1D_ARRAY:
122 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY, TYPE);
123 surface->array_size = ptex->array_size;
124 break;
125 case PIPE_TEXTURE_2D_ARRAY:
126 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY, TYPE);
127 surface->array_size = ptex->array_size;
128 break;
129 case PIPE_TEXTURE_CUBE:
130 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP, TYPE);
131 break;
132 case PIPE_BUFFER:
133 default:
134 return -EINVAL;
135 }
136 if (ptex->bind & PIPE_BIND_SCANOUT) {
137 surface->flags |= RADEON_SURF_SCANOUT;
138 }
139 if (util_format_is_depth_and_stencil(ptex->format) && !is_transfer) {
140 surface->flags |= RADEON_SURF_ZBUFFER;
141 surface->flags |= RADEON_SURF_SBUFFER;
142 }
143
144 return 0;
145 }
146
147 static int r600_setup_surface(struct pipe_screen *screen,
148 struct r600_resource_texture *rtex,
149 unsigned array_mode,
150 unsigned pitch_in_bytes_override)
151 {
152 struct r600_screen *rscreen = (struct r600_screen*)screen;
153 int r;
154
155 r = rscreen->ws->surface_init(rscreen->ws, &rtex->surface);
156 if (r) {
157 return r;
158 }
159 if (pitch_in_bytes_override && pitch_in_bytes_override != rtex->surface.level[0].pitch_bytes) {
160 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
161 * for those
162 */
163 rtex->surface.level[0].nblk_x = pitch_in_bytes_override / rtex->surface.bpe;
164 rtex->surface.level[0].pitch_bytes = pitch_in_bytes_override;
165 rtex->surface.level[0].slice_size = pitch_in_bytes_override * rtex->surface.level[0].nblk_y;
166 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
167 rtex->surface.stencil_offset = rtex->surface.level[0].slice_size;
168 }
169 }
170 return 0;
171 }
172
173 /* Figure out whether u_blitter will fallback to a transfer operation.
174 * If so, don't use a staging resource.
175 */
176 static boolean permit_hardware_blit(struct pipe_screen *screen,
177 const struct pipe_resource *res)
178 {
179 unsigned bind;
180
181 if (util_format_is_depth_or_stencil(res->format))
182 bind = PIPE_BIND_DEPTH_STENCIL;
183 else
184 bind = PIPE_BIND_RENDER_TARGET;
185
186 /* hackaround for S3TC */
187 if (util_format_is_compressed(res->format))
188 return TRUE;
189
190 if (!screen->is_format_supported(screen,
191 res->format,
192 res->target,
193 res->nr_samples,
194 bind))
195 return FALSE;
196
197 if (!screen->is_format_supported(screen,
198 res->format,
199 res->target,
200 res->nr_samples,
201 PIPE_BIND_SAMPLER_VIEW))
202 return FALSE;
203
204 switch (res->usage) {
205 case PIPE_USAGE_STREAM:
206 case PIPE_USAGE_STAGING:
207 return FALSE;
208
209 default:
210 return TRUE;
211 }
212 }
213
214 static boolean r600_texture_get_handle(struct pipe_screen* screen,
215 struct pipe_resource *ptex,
216 struct winsys_handle *whandle)
217 {
218 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
219 struct si_resource *resource = &rtex->resource;
220 struct radeon_surface *surface = &rtex->surface;
221 struct r600_screen *rscreen = (struct r600_screen*)screen;
222
223 rscreen->ws->buffer_set_tiling(resource->buf,
224 NULL,
225 surface->level[0].mode >= RADEON_SURF_MODE_1D ?
226 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
227 surface->level[0].mode >= RADEON_SURF_MODE_2D ?
228 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR,
229 surface->bankw, surface->bankh,
230 surface->tile_split,
231 surface->stencil_tile_split,
232 surface->mtilea,
233 surface->level[0].pitch_bytes);
234
235 return rscreen->ws->buffer_get_handle(resource->buf,
236 surface->level[0].pitch_bytes, whandle);
237 }
238
239 static void r600_texture_destroy(struct pipe_screen *screen,
240 struct pipe_resource *ptex)
241 {
242 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
243 struct si_resource *resource = &rtex->resource;
244
245 if (rtex->flushed_depth_texture)
246 si_resource_reference((struct si_resource **)&rtex->flushed_depth_texture, NULL);
247
248 pb_reference(&resource->buf, NULL);
249 FREE(rtex);
250 }
251
252 static void *si_texture_transfer_map(struct pipe_context *ctx,
253 struct pipe_resource *texture,
254 unsigned level,
255 unsigned usage,
256 const struct pipe_box *box,
257 struct pipe_transfer **ptransfer)
258 {
259 struct r600_context *rctx = (struct r600_context *)ctx;
260 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
261 struct pipe_resource resource;
262 struct r600_transfer *trans;
263 int r;
264 boolean use_staging_texture = FALSE;
265 struct radeon_winsys_cs_handle *buf;
266 enum pipe_format format = texture->format;
267 unsigned offset = 0;
268 char *map;
269
270 /* We cannot map a tiled texture directly because the data is
271 * in a different order, therefore we do detiling using a blit.
272 *
273 * Also, use a temporary in GTT memory for read transfers, as
274 * the CPU is much happier reading out of cached system memory
275 * than uncached VRAM.
276 */
277 if (rtex->surface.level[level].mode != RADEON_SURF_MODE_LINEAR_ALIGNED &&
278 rtex->surface.level[level].mode != RADEON_SURF_MODE_LINEAR)
279 use_staging_texture = TRUE;
280
281 /* XXX: Use a staging texture for uploads if the underlying BO
282 * is busy. No interface for checking that currently? so do
283 * it eagerly whenever the transfer doesn't require a readback
284 * and might block.
285 */
286 if ((usage & PIPE_TRANSFER_WRITE) &&
287 !(usage & (PIPE_TRANSFER_READ |
288 PIPE_TRANSFER_DONTBLOCK |
289 PIPE_TRANSFER_UNSYNCHRONIZED)))
290 use_staging_texture = TRUE;
291
292 if (!permit_hardware_blit(ctx->screen, texture) ||
293 (texture->flags & R600_RESOURCE_FLAG_TRANSFER))
294 use_staging_texture = FALSE;
295
296 if (use_staging_texture && (usage & PIPE_TRANSFER_MAP_DIRECTLY))
297 return NULL;
298
299 trans = CALLOC_STRUCT(r600_transfer);
300 if (trans == NULL)
301 return NULL;
302 pipe_resource_reference(&trans->transfer.resource, texture);
303 trans->transfer.level = level;
304 trans->transfer.usage = usage;
305 trans->transfer.box = *box;
306 if (rtex->depth) {
307 /* XXX: only readback the rectangle which is being mapped?
308 */
309 /* XXX: when discard is true, no need to read back from depth texture
310 */
311 r = r600_texture_depth_flush(ctx, texture, FALSE);
312 if (r < 0) {
313 R600_ERR("failed to create temporary texture to hold untiled copy\n");
314 pipe_resource_reference(&trans->transfer.resource, NULL);
315 FREE(trans);
316 return NULL;
317 }
318 trans->transfer.stride = rtex->flushed_depth_texture->surface.level[level].pitch_bytes;
319 trans->offset = r600_texture_get_offset(rtex->flushed_depth_texture, level, box->z);
320 } else if (use_staging_texture) {
321 resource.target = PIPE_TEXTURE_2D;
322 resource.format = texture->format;
323 resource.width0 = box->width;
324 resource.height0 = box->height;
325 resource.depth0 = 1;
326 resource.array_size = 1;
327 resource.last_level = 0;
328 resource.nr_samples = 0;
329 resource.usage = PIPE_USAGE_STAGING;
330 resource.bind = 0;
331 resource.flags = R600_RESOURCE_FLAG_TRANSFER;
332 /* For texture reading, the temporary (detiled) texture is used as
333 * a render target when blitting from a tiled texture. */
334 if (usage & PIPE_TRANSFER_READ) {
335 resource.bind |= PIPE_BIND_RENDER_TARGET;
336 }
337 /* For texture writing, the temporary texture is used as a sampler
338 * when blitting into a tiled texture. */
339 if (usage & PIPE_TRANSFER_WRITE) {
340 resource.bind |= PIPE_BIND_SAMPLER_VIEW;
341 }
342 /* Create the temporary texture. */
343 trans->staging_texture = ctx->screen->resource_create(ctx->screen, &resource);
344 if (trans->staging_texture == NULL) {
345 R600_ERR("failed to create temporary texture to hold untiled copy\n");
346 pipe_resource_reference(&trans->transfer.resource, NULL);
347 FREE(trans);
348 return NULL;
349 }
350
351 trans->transfer.stride = ((struct r600_resource_texture *)trans->staging_texture)
352 ->surface.level[0].pitch_bytes;
353 if (usage & PIPE_TRANSFER_READ) {
354 r600_copy_to_staging_texture(ctx, trans);
355 /* Always referenced in the blit. */
356 radeonsi_flush(ctx, NULL, 0);
357 }
358 } else {
359 trans->transfer.stride = rtex->surface.level[level].pitch_bytes;
360 trans->transfer.layer_stride = rtex->surface.level[level].slice_size;
361 trans->offset = r600_texture_get_offset(rtex, level, box->z);
362 }
363
364 if (trans->staging_texture) {
365 buf = si_resource(trans->staging_texture)->cs_buf;
366 } else {
367 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
368
369 if (rtex->flushed_depth_texture)
370 buf = rtex->flushed_depth_texture->resource.cs_buf;
371 else
372 buf = si_resource(texture)->cs_buf;
373
374 offset = trans->offset +
375 box->y / util_format_get_blockheight(format) * trans->transfer.stride +
376 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
377 }
378
379 if (!(map = rctx->ws->buffer_map(buf, rctx->cs, usage))) {
380 pipe_resource_reference(&trans->staging_texture, NULL);
381 pipe_resource_reference(&trans->transfer.resource, NULL);
382 FREE(trans);
383 return NULL;
384 }
385
386 *ptransfer = &trans->transfer;
387 return map + offset;
388 }
389
390 static void si_texture_transfer_unmap(struct pipe_context *ctx,
391 struct pipe_transfer* transfer)
392 {
393 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
394 struct r600_context *rctx = (struct r600_context*)ctx;
395 struct radeon_winsys_cs_handle *buf;
396 struct pipe_resource *texture = transfer->resource;
397 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
398
399 if (rtransfer->staging_texture) {
400 buf = si_resource(rtransfer->staging_texture)->cs_buf;
401 } else {
402 struct r600_resource_texture *rtex = (struct r600_resource_texture*)transfer->resource;
403
404 if (rtex->flushed_depth_texture) {
405 buf = rtex->flushed_depth_texture->resource.cs_buf;
406 } else {
407 buf = si_resource(transfer->resource)->cs_buf;
408 }
409 }
410 rctx->ws->buffer_unmap(buf);
411
412 if (rtransfer->staging_texture) {
413 if (transfer->usage & PIPE_TRANSFER_WRITE) {
414 r600_copy_from_staging_texture(ctx, rtransfer);
415 }
416 pipe_resource_reference(&rtransfer->staging_texture, NULL);
417 }
418
419 if (rtex->depth && !rtex->is_flushing_texture) {
420 if ((transfer->usage & PIPE_TRANSFER_WRITE) && rtex->flushed_depth_texture)
421 r600_blit_push_depth(ctx, rtex);
422 }
423
424 pipe_resource_reference(&transfer->resource, NULL);
425 FREE(transfer);
426 }
427
428 static const struct u_resource_vtbl r600_texture_vtbl =
429 {
430 r600_texture_get_handle, /* get_handle */
431 r600_texture_destroy, /* resource_destroy */
432 si_texture_transfer_map, /* transfer_map */
433 u_default_transfer_flush_region,/* transfer_flush_region */
434 si_texture_transfer_unmap, /* transfer_unmap */
435 NULL /* transfer_inline_write */
436 };
437
438 static struct r600_resource_texture *
439 r600_texture_create_object(struct pipe_screen *screen,
440 const struct pipe_resource *base,
441 unsigned array_mode,
442 unsigned pitch_in_bytes_override,
443 unsigned max_buffer_size,
444 struct pb_buffer *buf,
445 boolean alloc_bo,
446 struct radeon_surface *surface)
447 {
448 struct r600_resource_texture *rtex;
449 struct si_resource *resource;
450 struct r600_screen *rscreen = (struct r600_screen*)screen;
451 int r;
452
453 rtex = CALLOC_STRUCT(r600_resource_texture);
454 if (rtex == NULL)
455 return NULL;
456
457 resource = &rtex->resource;
458 resource->b.b = *base;
459 resource->b.vtbl = &r600_texture_vtbl;
460 pipe_reference_init(&resource->b.b.reference, 1);
461 resource->b.b.screen = screen;
462 rtex->pitch_override = pitch_in_bytes_override;
463 rtex->real_format = base->format;
464
465 /* only mark depth textures the HW can hit as depth textures */
466 if (util_format_is_depth_or_stencil(rtex->real_format) && permit_hardware_blit(screen, base))
467 rtex->depth = 1;
468
469 rtex->surface = *surface;
470 r = r600_setup_surface(screen, rtex, array_mode, pitch_in_bytes_override);
471 if (r) {
472 FREE(rtex);
473 return NULL;
474 }
475
476 /* Now create the backing buffer. */
477 if (!buf && alloc_bo) {
478 unsigned base_align = rtex->surface.bo_alignment;
479 unsigned size = rtex->surface.bo_size;
480
481 base_align = rtex->surface.bo_alignment;
482 if (!si_init_resource(rscreen, resource, size, base_align, base->bind, base->usage)) {
483 FREE(rtex);
484 return NULL;
485 }
486 } else if (buf) {
487 resource->buf = buf;
488 resource->cs_buf = rscreen->ws->buffer_get_cs_handle(buf);
489 resource->domains = RADEON_DOMAIN_GTT | RADEON_DOMAIN_VRAM;
490 }
491
492 return rtex;
493 }
494
495 struct pipe_resource *si_texture_create(struct pipe_screen *screen,
496 const struct pipe_resource *templ)
497 {
498 struct r600_screen *rscreen = (struct r600_screen*)screen;
499 struct radeon_surface surface;
500 unsigned array_mode = V_009910_ARRAY_LINEAR_ALIGNED;
501 int r;
502
503 #if 0
504 if (!(templ->flags & R600_RESOURCE_FLAG_TRANSFER) &&
505 !(templ->bind & PIPE_BIND_SCANOUT)) {
506 if (permit_hardware_blit(screen, templ)) {
507 array_mode = V_009910_ARRAY_2D_TILED_THIN1;
508 }
509 }
510 #endif
511
512 r = r600_init_surface(&surface, templ, array_mode,
513 templ->flags & R600_RESOURCE_FLAG_TRANSFER);
514 if (r) {
515 return NULL;
516 }
517 r = rscreen->ws->surface_best(rscreen->ws, &surface);
518 if (r) {
519 return NULL;
520 }
521 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
522 0, 0, NULL, TRUE, &surface);
523 }
524
525 static struct pipe_surface *r600_create_surface(struct pipe_context *pipe,
526 struct pipe_resource *texture,
527 const struct pipe_surface *surf_tmpl)
528 {
529 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
530 struct r600_surface *surface = CALLOC_STRUCT(r600_surface);
531 unsigned level = surf_tmpl->u.tex.level;
532
533 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
534 if (surface == NULL)
535 return NULL;
536 /* XXX no offset */
537 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
538 pipe_reference_init(&surface->base.reference, 1);
539 pipe_resource_reference(&surface->base.texture, texture);
540 surface->base.context = pipe;
541 surface->base.format = surf_tmpl->format;
542 surface->base.width = rtex->surface.level[level].npix_x;
543 surface->base.height = rtex->surface.level[level].npix_y;
544 surface->base.usage = surf_tmpl->usage;
545 surface->base.texture = texture;
546 surface->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
547 surface->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
548 surface->base.u.tex.level = level;
549
550 return &surface->base;
551 }
552
553 static void r600_surface_destroy(struct pipe_context *pipe,
554 struct pipe_surface *surface)
555 {
556 pipe_resource_reference(&surface->texture, NULL);
557 FREE(surface);
558 }
559
560 struct pipe_resource *si_texture_from_handle(struct pipe_screen *screen,
561 const struct pipe_resource *templ,
562 struct winsys_handle *whandle)
563 {
564 struct r600_screen *rscreen = (struct r600_screen*)screen;
565 struct pb_buffer *buf = NULL;
566 unsigned stride = 0;
567 unsigned array_mode = V_009910_ARRAY_LINEAR_ALIGNED;
568 enum radeon_bo_layout micro, macro;
569 struct radeon_surface surface;
570 int r;
571
572 /* Support only 2D textures without mipmaps */
573 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT) ||
574 templ->depth0 != 1 || templ->last_level != 0)
575 return NULL;
576
577 buf = rscreen->ws->buffer_from_handle(rscreen->ws, whandle, &stride);
578 if (!buf)
579 return NULL;
580
581 rscreen->ws->buffer_get_tiling(buf, &micro, &macro,
582 &surface.bankw, &surface.bankh,
583 &surface.tile_split,
584 &surface.stencil_tile_split,
585 &surface.mtilea);
586
587 if (macro == RADEON_LAYOUT_TILED)
588 array_mode = V_009910_ARRAY_2D_TILED_THIN1;
589 else if (micro == RADEON_LAYOUT_TILED)
590 array_mode = V_009910_ARRAY_1D_TILED_THIN1;
591 else
592 array_mode = V_009910_ARRAY_LINEAR_ALIGNED;
593
594 r = r600_init_surface(&surface, templ, array_mode, 0);
595 if (r) {
596 return NULL;
597 }
598 return (struct pipe_resource *)r600_texture_create_object(screen, templ, array_mode,
599 stride, 0, buf, FALSE, &surface);
600 }
601
602 int r600_texture_depth_flush(struct pipe_context *ctx,
603 struct pipe_resource *texture, boolean just_create)
604 {
605 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
606 struct pipe_resource resource;
607
608 if (rtex->flushed_depth_texture)
609 goto out;
610
611 resource.target = texture->target;
612 resource.format = texture->format;
613 resource.width0 = texture->width0;
614 resource.height0 = texture->height0;
615 resource.depth0 = texture->depth0;
616 resource.array_size = texture->array_size;
617 resource.last_level = texture->last_level;
618 resource.nr_samples = texture->nr_samples;
619 resource.usage = PIPE_USAGE_DYNAMIC;
620 resource.bind = texture->bind | PIPE_BIND_DEPTH_STENCIL;
621 resource.flags = R600_RESOURCE_FLAG_TRANSFER | texture->flags;
622
623 rtex->flushed_depth_texture = (struct r600_resource_texture *)ctx->screen->resource_create(ctx->screen, &resource);
624 if (rtex->flushed_depth_texture == NULL) {
625 R600_ERR("failed to create temporary texture to hold untiled copy\n");
626 return -ENOMEM;
627 }
628
629 ((struct r600_resource_texture *)rtex->flushed_depth_texture)->is_flushing_texture = TRUE;
630 out:
631 if (just_create)
632 return 0;
633
634 /* XXX: only do this if the depth texture has actually changed:
635 */
636 si_blit_uncompress_depth(ctx, rtex);
637 return 0;
638 }
639
640 void si_init_surface_functions(struct r600_context *r600)
641 {
642 r600->context.create_surface = r600_create_surface;
643 r600->context.surface_destroy = r600_surface_destroy;
644 }