1/*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24#include "r300_transfer.h"
25#include "r300_texture_desc.h"
26#include "r300_screen_buffer.h"
27
28#include "util/u_memory.h"
29#include "util/format/u_format.h"
30#include "util/u_box.h"
31
32struct r300_transfer {
33    /* Parent class */
34    struct pipe_transfer transfer;
35
36    /* Linear texture. */
37    struct r300_resource *linear_texture;
38};
39
40/* Convenience cast wrapper. */
41static inline struct r300_transfer*
42r300_transfer(struct pipe_transfer* transfer)
43{
44    return (struct r300_transfer*)transfer;
45}
46
47/* Copy from a tiled texture to a detiled one. */
48static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
49                                         struct r300_transfer *r300transfer)
50{
51    struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
52    struct pipe_resource *src = transfer->resource;
53    struct pipe_resource *dst = &r300transfer->linear_texture->b;
54
55    if (src->nr_samples <= 1) {
56        ctx->resource_copy_region(ctx, dst, 0, 0, 0, 0,
57                                  src, transfer->level, &transfer->box);
58    } else {
59        /* Resolve the resource. */
60        struct pipe_blit_info blit;
61
62        memset(&blit, 0, sizeof(blit));
63        blit.src.resource = src;
64        blit.src.format = src->format;
65        blit.src.level = transfer->level;
66        blit.src.box = transfer->box;
67        blit.dst.resource = dst;
68        blit.dst.format = dst->format;
69        blit.dst.box.width = transfer->box.width;
70        blit.dst.box.height = transfer->box.height;
71        blit.dst.box.depth = transfer->box.depth;
72        blit.mask = PIPE_MASK_RGBA;
73        blit.filter = PIPE_TEX_FILTER_NEAREST;
74
75        ctx->blit(ctx, &blit);
76    }
77}
78
79/* Copy a detiled texture to a tiled one. */
80static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
81                                         struct r300_transfer *r300transfer)
82{
83    struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
84    struct pipe_resource *tex = transfer->resource;
85    struct pipe_box src_box;
86
87    u_box_3d(0, 0, 0,
88             transfer->box.width, transfer->box.height, transfer->box.depth,
89             &src_box);
90
91    ctx->resource_copy_region(ctx, tex, transfer->level,
92                              transfer->box.x, transfer->box.y, transfer->box.z,
93                              &r300transfer->linear_texture->b, 0, &src_box);
94
95    /* XXX remove this. */
96    r300_flush(ctx, 0, NULL);
97}
98
99void *
100r300_texture_transfer_map(struct pipe_context *ctx,
101                          struct pipe_resource *texture,
102                          unsigned level,
103                          unsigned usage,
104                          const struct pipe_box *box,
105                          struct pipe_transfer **transfer)
106{
107    struct r300_context *r300 = r300_context(ctx);
108    struct r300_resource *tex = r300_resource(texture);
109    struct r300_transfer *trans;
110    boolean referenced_cs, referenced_hw;
111    enum pipe_format format = tex->b.format;
112    char *map;
113
114    referenced_cs =
115        r300->rws->cs_is_buffer_referenced(&r300->cs, tex->buf, RADEON_USAGE_READWRITE);
116    if (referenced_cs) {
117        referenced_hw = TRUE;
118    } else {
119        referenced_hw =
120            !r300->rws->buffer_wait(r300->rws, tex->buf, 0, RADEON_USAGE_READWRITE);
121    }
122
123    trans = CALLOC_STRUCT(r300_transfer);
124    if (trans) {
125        /* Initialize the transfer object. */
126        trans->transfer.resource = texture;
127        trans->transfer.level = level;
128        trans->transfer.usage = usage;
129        trans->transfer.box = *box;
130
131        /* If the texture is tiled, we must create a temporary detiled texture
132         * for this transfer.
133         * Also make write transfers pipelined. */
134        if (tex->tex.microtile || tex->tex.macrotile[level] ||
135            (referenced_hw && !(usage & PIPE_MAP_READ) &&
136             r300_is_blit_supported(texture->format))) {
137            struct pipe_resource base;
138
139            if (r300->blitter->running) {
140                fprintf(stderr, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
141                os_break();
142            }
143
144            memset(&base, 0, sizeof(base));
145            base.target = PIPE_TEXTURE_2D;
146            base.format = texture->format;
147            base.width0 = box->width;
148            base.height0 = box->height;
149            base.depth0 = 1;
150            base.array_size = 1;
151            base.usage = PIPE_USAGE_STAGING;
152            base.flags = R300_RESOURCE_FLAG_TRANSFER;
153
154            /* We must set the correct texture target and dimensions if needed for a 3D transfer. */
155            if (box->depth > 1 && util_max_layer(texture, level) > 0) {
156                base.target = texture->target;
157
158                if (base.target == PIPE_TEXTURE_3D) {
159                    base.depth0 = util_next_power_of_two(box->depth);
160                }
161            }
162
163            /* Create the temporary texture. */
164            trans->linear_texture = r300_resource(
165               ctx->screen->resource_create(ctx->screen,
166                                            &base));
167
168            if (!trans->linear_texture) {
169                /* Oh crap, the thing can't create the texture.
170                 * Let's flush and try again. */
171                r300_flush(ctx, 0, NULL);
172
173                trans->linear_texture = r300_resource(
174                   ctx->screen->resource_create(ctx->screen,
175                                                &base));
176
177                if (!trans->linear_texture) {
178                    fprintf(stderr,
179                            "r300: Failed to create a transfer object.\n");
180                    FREE(trans);
181                    return NULL;
182                }
183            }
184
185            assert(!trans->linear_texture->tex.microtile &&
186                   !trans->linear_texture->tex.macrotile[0]);
187
188            /* Set the stride. */
189            trans->transfer.stride =
190                    trans->linear_texture->tex.stride_in_bytes[0];
191            trans->transfer.layer_stride =
192                    trans->linear_texture->tex.layer_size_in_bytes[0];
193
194            if (usage & PIPE_MAP_READ) {
195                /* We cannot map a tiled texture directly because the data is
196                 * in a different order, therefore we do detiling using a blit. */
197                r300_copy_from_tiled_texture(ctx, trans);
198
199                /* Always referenced in the blit. */
200                r300_flush(ctx, 0, NULL);
201            }
202        } else {
203            /* Unpipelined transfer. */
204            trans->transfer.stride = tex->tex.stride_in_bytes[level];
205            trans->transfer.layer_stride = tex->tex.layer_size_in_bytes[level];
206            trans->transfer.offset = r300_texture_get_offset(tex, level, box->z);
207
208            if (referenced_cs &&
209                !(usage & PIPE_MAP_UNSYNCHRONIZED)) {
210                r300_flush(ctx, 0, NULL);
211            }
212        }
213    }
214
215    if (trans->linear_texture) {
216        /* The detiled texture is of the same size as the region being mapped
217         * (no offset needed). */
218        map = r300->rws->buffer_map(r300->rws, trans->linear_texture->buf,
219                                    &r300->cs, usage);
220        if (!map) {
221            pipe_resource_reference(
222                (struct pipe_resource**)&trans->linear_texture, NULL);
223            FREE(trans);
224            return NULL;
225        }
226	*transfer = &trans->transfer;
227        return map;
228    } else {
229        /* Tiling is disabled. */
230        map = r300->rws->buffer_map(r300->rws, tex->buf, &r300->cs, usage);
231        if (!map) {
232            FREE(trans);
233            return NULL;
234        }
235
236	*transfer = &trans->transfer;
237        return map + trans->transfer.offset +
238            box->y / util_format_get_blockheight(format) * trans->transfer.stride +
239            box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
240    }
241}
242
243void r300_texture_transfer_unmap(struct pipe_context *ctx,
244				 struct pipe_transfer *transfer)
245{
246    struct r300_transfer *trans = r300_transfer(transfer);
247
248    if (trans->linear_texture) {
249        if (transfer->usage & PIPE_MAP_WRITE) {
250            r300_copy_into_tiled_texture(ctx, trans);
251        }
252
253        pipe_resource_reference(
254            (struct pipe_resource**)&trans->linear_texture, NULL);
255    }
256    FREE(transfer);
257}
258