r300_transfer.c revision 6ccab620a0e7364ab6c0d902b3ddf58ee988f7fa
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/u_format.h"
30#include "util/u_box.h"
31
32struct r300_transfer {
33    /* Parent class */
34    struct pipe_transfer transfer;
35
36    /* Offset from start of buffer. */
37    unsigned offset;
38
39    /* Linear texture. */
40    struct r300_resource *linear_texture;
41};
42
43/* Convenience cast wrapper. */
44static INLINE struct r300_transfer*
45r300_transfer(struct pipe_transfer* transfer)
46{
47    return (struct r300_transfer*)transfer;
48}
49
50/* Copy from a tiled texture to a detiled one. */
51static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
52                                         struct r300_transfer *r300transfer)
53{
54    struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
55    struct pipe_resource *tex = transfer->resource;
56
57    ctx->resource_copy_region(ctx, &r300transfer->linear_texture->b.b.b, 0,
58                              0, 0, 0,
59                              tex, transfer->level, &transfer->box);
60}
61
62/* Copy a detiled texture to a tiled one. */
63static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
64                                         struct r300_transfer *r300transfer)
65{
66    struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
67    struct pipe_resource *tex = transfer->resource;
68    struct pipe_box src_box;
69    u_box_origin_2d(transfer->box.width, transfer->box.height, &src_box);
70
71    ctx->resource_copy_region(ctx, tex, transfer->level,
72                              transfer->box.x, transfer->box.y, transfer->box.z,
73                              &r300transfer->linear_texture->b.b.b, 0, &src_box);
74
75    ctx->flush(ctx, 0, NULL);
76}
77
78struct pipe_transfer*
79r300_texture_get_transfer(struct pipe_context *ctx,
80                          struct pipe_resource *texture,
81                          unsigned level,
82                          unsigned usage,
83                          const struct pipe_box *box)
84{
85    struct r300_context *r300 = r300_context(ctx);
86    struct r300_resource *tex = r300_resource(texture);
87    struct r300_transfer *trans;
88    struct pipe_resource base;
89    boolean referenced_cs, referenced_hw, blittable;
90
91    referenced_cs =
92        r300->rws->cs_is_buffer_referenced(r300->cs, tex->cs_buf);
93    if (referenced_cs) {
94        referenced_hw = TRUE;
95    } else {
96        referenced_hw =
97            r300->rws->buffer_is_busy(tex->buf);
98    }
99
100    blittable = ctx->screen->is_format_supported(
101            ctx->screen, texture->format, texture->target, 0,
102            PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET, 0);
103
104    trans = CALLOC_STRUCT(r300_transfer);
105    if (trans) {
106        /* Initialize the transfer object. */
107        pipe_resource_reference(&trans->transfer.resource, texture);
108        trans->transfer.level = level;
109        trans->transfer.usage = usage;
110        trans->transfer.box = *box;
111
112        /* If the texture is tiled, we must create a temporary detiled texture
113         * for this transfer.
114         * Also make write transfers pipelined. */
115        if (tex->tex.microtile || tex->tex.macrotile[level] ||
116            ((referenced_hw & !(usage & PIPE_TRANSFER_READ)) && blittable)) {
117            if (r300->blitter->running) {
118                fprintf(stderr, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
119                os_break();
120            }
121
122            base.target = PIPE_TEXTURE_2D;
123            base.format = texture->format;
124            base.width0 = box->width;
125            base.height0 = box->height;
126            base.depth0 = 1;
127            base.array_size = 1;
128            base.last_level = 0;
129            base.nr_samples = 0;
130            base.usage = PIPE_USAGE_DYNAMIC;
131            base.bind = 0;
132            base.flags = R300_RESOURCE_FLAG_TRANSFER;
133
134            /* For texture reading, the temporary (detiled) texture is used as
135             * a render target when blitting from a tiled texture. */
136            if (usage & PIPE_TRANSFER_READ) {
137                base.bind |= PIPE_BIND_RENDER_TARGET;
138            }
139            /* For texture writing, the temporary texture is used as a sampler
140             * when blitting into a tiled texture. */
141            if (usage & PIPE_TRANSFER_WRITE) {
142                base.bind |= PIPE_BIND_SAMPLER_VIEW;
143            }
144
145            /* Create the temporary texture. */
146            trans->linear_texture = r300_resource(
147               ctx->screen->resource_create(ctx->screen,
148                                            &base));
149
150            if (!trans->linear_texture) {
151                /* Oh crap, the thing can't create the texture.
152                 * Let's flush and try again. */
153                ctx->flush(ctx, 0, NULL);
154
155                trans->linear_texture = r300_resource(
156                   ctx->screen->resource_create(ctx->screen,
157                                                &base));
158
159                if (!trans->linear_texture) {
160                    /* For linear textures, it's safe to fallback to
161                     * an unpipelined transfer. */
162                    if (!tex->tex.microtile && !tex->tex.macrotile[level]) {
163                        goto unpipelined;
164                    }
165
166                    /* Otherwise, go to hell. */
167                    fprintf(stderr,
168                        "r300: Failed to create a transfer object, praise.\n");
169                    FREE(trans);
170                    return NULL;
171                }
172            }
173
174            assert(!trans->linear_texture->tex.microtile &&
175                   !trans->linear_texture->tex.macrotile[0]);
176
177            /* Set the stride.
178	     *
179	     * Even though we are using an internal texture for this,
180	     * the transfer level, box and usage parameters still reflect
181	     * the arguments received to get_transfer.  We just do the
182	     * right thing internally.
183	     */
184            trans->transfer.stride =
185                    trans->linear_texture->tex.stride_in_bytes[0];
186
187            if (usage & PIPE_TRANSFER_READ) {
188                /* We cannot map a tiled texture directly because the data is
189                 * in a different order, therefore we do detiling using a blit. */
190                r300_copy_from_tiled_texture(ctx, trans);
191
192                /* Always referenced in the blit. */
193                ctx->flush(ctx, 0, NULL);
194            }
195            return &trans->transfer;
196        }
197
198    unpipelined:
199        /* Unpipelined transfer. */
200        trans->transfer.stride = tex->tex.stride_in_bytes[level];
201        trans->offset = r300_texture_get_offset(tex, level, box->z);
202
203        if (referenced_cs)
204            ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
205        return &trans->transfer;
206    }
207    return NULL;
208}
209
210void r300_texture_transfer_destroy(struct pipe_context *ctx,
211				   struct pipe_transfer *trans)
212{
213    struct r300_transfer *r300transfer = r300_transfer(trans);
214
215    if (r300transfer->linear_texture) {
216        if (trans->usage & PIPE_TRANSFER_WRITE) {
217            r300_copy_into_tiled_texture(ctx, r300transfer);
218        }
219
220        pipe_resource_reference(
221            (struct pipe_resource**)&r300transfer->linear_texture, NULL);
222    }
223    pipe_resource_reference(&trans->resource, NULL);
224    FREE(trans);
225}
226
227void* r300_texture_transfer_map(struct pipe_context *ctx,
228				struct pipe_transfer *transfer)
229{
230    struct r300_context *r300 = r300_context(ctx);
231    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
232    struct r300_transfer *r300transfer = r300_transfer(transfer);
233    struct r300_resource *tex = r300_resource(transfer->resource);
234    char *map;
235    enum pipe_format format = tex->b.b.b.format;
236
237    if (r300transfer->linear_texture) {
238        /* The detiled texture is of the same size as the region being mapped
239         * (no offset needed). */
240        return rws->buffer_map(r300transfer->linear_texture->buf,
241                               r300->cs,
242                               transfer->usage);
243    } else {
244        /* Tiling is disabled. */
245        map = rws->buffer_map(tex->buf, r300->cs,
246                              transfer->usage);
247
248        if (!map) {
249            return NULL;
250        }
251
252        return map + r300_transfer(transfer)->offset +
253            transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
254            transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
255    }
256}
257
258void r300_texture_transfer_unmap(struct pipe_context *ctx,
259				 struct pipe_transfer *transfer)
260{
261    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
262    struct r300_transfer *r300transfer = r300_transfer(transfer);
263    struct r300_resource *tex = r300_resource(transfer->resource);
264
265    if (r300transfer->linear_texture) {
266        rws->buffer_unmap(r300transfer->linear_texture->buf);
267    } else {
268        rws->buffer_unmap(tex->buf);
269    }
270}
271